R - Running a loop over a few actions - r

I've asked a similar question late last year but couldn't find the time to work on that project until now.
So in the code below, I make a map of the bikes available in Paris using live data. The idea I have is to make a loop of this over time (I read that loops take a while to run so I might not have to add a time component to this) and save each map under a different name to see the "flow" of bike availability over a day for example.
Where I'm stuck is that I don't know how to run a loop over this. I've tried "for" I've tried "while" I've tried to put this as a function and then loop it and while I'm sure this is feasible I'm just out of options.
If someone has an idea of how to do this or direct me towards a tutorial I'd be forever grateful.
Thanks!
sta.Paris <- read.csv(url("https://opendata.paris.fr/explore/dataset/velib-disponibilite-en-temps-reel/download/?format=csv&timezone=Europe/Berlin&use_labels_for_header=true"),sep=";")
sta.Paris1 <- sta.Paris %>% separate(Coordonnées.géographiques,into=c("lat","lon"),sep=",") %>%
mutate(lat=as.numeric(lat),lon=as.numeric(lon))
sta.Paris1[15] <- NULL
sta.Paris2 <- na.omit(sta.Paris1)
sta.Paris2$velorange = cut(sta.Paris2$Nombre.total.vélos.disponibles,
breaks = c(0,5,10,15,20,25,100), right=F,
labels = c("Tres peu [0-5)", "Peu [5-10)","Moyen [10-15)","Assez [15-20)","Beaucoup [20-25)", "Enormement [25-100)")
)
colorFactor(palette = c("azure2","yellow","orange","red","red4","black"), domain = sta.Paris2$velorange)
map.velib1 <- leaflet(data = sta.Paris2) %>%
addTiles(urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png") %>%
addCircleMarkers(~ lon,~ lat,
color = ~ pal(velorange),
popupOptions(T),
radius=~ 3 ,stroke = FALSE, fillOpacity = 0.7,popup = ~ sprintf("<b> Vélos dispos: %s</b>",as.character(Nombre.total.vélos.disponibles)))
map.velib1

Related

I can't get my plots to a single grid please help correct my code

I have 11 plots and used a looping function to plot them see my code below. However, I can't get them to fit in just 1 page or less. The plots are actually too big. I am using R software and writing my work in RMarkdown. I have spent almost an entire week trying to resolve this.
group_by(Firm_category) %>%
doo(
~ggboxplot(
data =., x = "Means.type", y = "means",
fill ="grey", palette = "npg", legend = "none",
ggtheme = theme_pubr()
),
result = "plots"
)
graph3
# Add statistical tests to each corresponding plot
Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
graph3.i <- graph3$plots[[i]] +
labs(title = Firm_category[i]) +
stat_pvalue_manual(stat.test[i, ], label = "p.adj.signif")
print(graph3.i)
}
#output3.long data sample below as comments
#Firm_category billmonth Means.type means
#Agric 1 Before 38.4444
#Agric 1 After 51.9
Complete data is on my github: https://github.com/Fridahnyakundi/Descriptives-in-R/blob/master/Output3.csv
This code prints all the graphs but in like 4 pages. I want to group them into a grid. I have tried to add all these codes below just before my last curly bracket and none is working, please help me out.
library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11], nrow = 4, ncol = 3))
library(ggpubr)
print(ggarrange(graph3.i[1:11], nrow = 4, ncol = 3))
I tried the gridExtra command as well (they all seem to do the same thing). I am the one with a mistake and I guess it has to do with my list. I read a lot of similar work here, some suggested
dev.new()
dev.off()
I still didn't get what they do. But adding either of them caused my code to stop.
I tried defining my 'for' loop function say call it 'XX', then later call it to make a list of graph but it returned NULL output.
I have tried defining an empty list (as I read in some answers here) then counting them to make a list that can be printed but I got so many errors.
I have done this for almost 3 days and will appreciate your help in resolving this.
Thanks!
I tried to complete your code ... and this works (but I don't have your 'stat.test' object). Basically, I added a graph3.i <- list() and replaced graph3.i in the loop ..
Is it what you wanted to do ?
library(magrittr)
library(dplyr)
library(rstatix)
library(ggplot2)
library(ggpubr)
data <- read.csv(url('http://raw.githubusercontent.com/Fridahnyakundi/Descriptives-in-R/master/Output3.csv'))
graph3 <- data %>% group_by(Firm_category) %>%
doo(
~ggboxplot(
data =., x = "Means.type", y = "means",
fill ="grey", palette = "npg", legend = "none",
ggtheme = theme_pubr()
),
result = "plots"
)
graph3
# Add statistical tests to each corresponding plot
graph3.i <- list()
Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
graph3.i[[i]] <- graph3$plots[[i]] +
labs(title = Firm_category[i]) # +
# stat_pvalue_manual(stat.test[i, ], label = "p.adj.signif")
print(graph3.i)
}
library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11], nrow = 4, ncol = 3))

tmap - changing the behaviour of tm_markers

Here is a reproducible example
#load the packages
library(easypackages)
packages("tidyverse","readxl","sf","tmaptools","tmap","lubridate",
"lwgeom","Cairo","nngeo","purrr","scales", "ggthemes","janitor")
polls<-st_as_sf(read.csv(url("https://www.caerphilly.gov.uk/CaerphillyDocs/FOI/Datasets_polling_stations_csv.aspx")),
coords = c("Easting","Northing"),crs = 27700)%>%
mutate(date = sample(seq(as.Date('2020/01/01'), as.Date('2020/05/31'), by="day"), 147))
test_stack<-polls%>%st_join(polls%>%st_buffer(dist=1000),join=st_within)%>%
filter(Ballot.Box.Polling.Station.x!=Ballot.Box.Polling.Station.y)%>%
add_count(Ballot.Box.Polling.Station.x)%>%
rename(number_of_neighbours = n)%>%
mutate(interval_date = date.x-date.y)%>%
subset(select = -c(6:8,10,11,13:18))## removing this comment will summarise the data so that only number of neighbours is returned %>%
distinct(Ballot.Box.Polling.Station.x,number_of_neighbours,date.x)%>%
filter(number_of_neighbours >=2)
polls%>%mutate(id = as.numeric(row_number()))%>% mutate(thing = case_when(id %% 2 == 0 ~ "stuff",
id %% 2 !=0 ~ "type"))->polls
qtm(polls)
tmap_mode("view")
tm_shape(polls) + tm_markers(col = "thing")
tm_shape(polls) + tm_dots(col ="thing", size = 0.75)
what I'd like to do is change the colour and size of tm_markers, because in the thing I want to use it in it would be nice to make use of different colour markers easily.
Linked to this, is understanding how the clustering of markers works when the map mode is "view" and an html is generated.
Any help on tm_marker behaviour and tm_marker clustering would be brilliant.
thanks == "MANY!"
In the end it turns out to be much simpler than using markers. I don't like "markers", aesthetically, but I do like "dots" and tm_dots lets you sort out the colours more easily (or in my head it's easier..) . And the thing is. Well. Clustering can be applied to dots, bubbles, and tm_symbols.
It's all in here:
https://cran.r-project.org/web/packages/tmap/tmap.pdf
(p 89/90)
ANYWAY
tm_shape(polls) + tm_dots(col ="thing", size = 0.75,clustering = T)
that's the answer (for me). I can cluster and then colour by a field.

Using ifelse to create a running tally in R

I am trying to do some quantitative modeling in R. I'm not getting an error message, but the results are not what I actually need.
I am a newbie, but here is my complete code sample.
`library(quantmod)
#Building the data frame and xts to show dividends, splits and technical indicators
getSymbols(c("AMZN"))
Playground <- data.frame(AMZN)
Playground$date <- as.Date(row.names(Playground))
Playground$wday <- as.POSIXlt(Playground$date)$wday #day of the week
Playground$yday <- as.POSIXlt(Playground$date)$mday #day of the month
Playground$mon <- as.POSIXlt(Playground$date)$mon #month of the year
Playground$RSI <- RSI(Playground$AMZN.Adjusted, n = 5, maType="EMA") #can add Moving Average Type with maType =
Playground$MACD <- MACD(AMZN, nFast = 12, nSlow = 26, nSig = 9)
Playground$Div <- getDividends('AMZN', from = "2007-01-01", to = Sys.Date(), src = "google", auto.assign = FALSE)
Playground$Split <- getSplits('AMZN', from = "2007-01-01", to = Sys.Date(), src = "google", auto.assign = FALSE)
Playground$BuySignal <- ifelse(Playground$RSI < 30 & Playground$MACD < 0, "Buy", "Hold")
All is well up until this point when I start using some logical conditions to come up with decision points.
Playground$boughts <- ifelse(Playground$BuySignal == "Buy", lag(Playground$boughts) + 1000, lag(Playground$boughts))
It will execute but the result will be nothing but NA. I suppose this is because you are trying to add NA to a number, but I'm not 100% sure. How do you tell the computer I want you to keep a running tally of how much you have bought?
Thanks so much for the help.
So we want ot buy 1000 shares every time a buy signal is generated?
Your problem stems from MACD idicator. It actually generates two columns, macd and signal. You have to decide which one you want to keep.
Playground$MACD <- MACD(AMZN, nFast = 12, nSlow = 26, nSig = 9)$signal
This should solve the problem at hand.
Also, please check the reference for ifelse. The class of return value can be tricky at times, and so the approach suggested by Floo0 is preferable.
Also, I'd advocate using 1 and 0 instead of buy and sell to show weather you are holding . It makes the math much easier.
And I'd strongly suggest reading some beginner tutorial on backtesting with PerformanceAnalytics. They make the going much much easier.
BTW, you missed this line in the code:
Playground$boughts<- 0
Hope it helps.
EDIT: And I forgot to mention the obvious. discard the first few rows where MACD will be NA
Something like:
Playground<- Playground[-c(1:26),]
Whenever you want to do an ifelse like
if ... Do something, else stay the same: Do not use ifelse
Try this instead
ind <- which(Playground$BuySignal == "Buy")
Playground$boughts[ind] <- lag(Playground$boughts) + 1000

Using revgeocode function in a FOR loop. Help required

My problem is as detailed below:
My input data is of the format as given in the small example below:
USERID LONGITUDE LATITUDE
1 -8.79659 55.879554
2 -6.874743 56.87896
3 -3.874743 58.87896
4 -10.874743 80.87896
I have used the follwoing code to reverse geocode the latitiude and longitude
dset <- as.data.frame(dataset[,2:3])
dset <- na.omit(dset)
library (ggmap)
location <- dset
nrow(location)
locaddr <- matrix(0,nrow(location),1)
location <- as.matrix(location)
for (i in 1:nrow(location))
{
locaddr[i,] <- revgeocode(location[i,], output = c("address"), messaging = FALSE, sensor = FALSE, override_limit = FALSE)
}
Now certain longitude-latitude return NA from Google Maps API. But when this happens the for loop is terminated for some reason. I would like to circumvent this and continue processing for the remaining data points. One idea I had was the following pseudocode:
if i = nrow(location)
continue
else
repeat revgeocode for loop here
end-for
end-if.
Kindly advise how this can be done or if there is a better way to do this.
Thank you in advance for your time and help.
No need to use a for-loop here. I recommand you to use lapply to avoid side effect, and pre-allocate problems:
locaddr <- lapply(seq(nrow(location)), function(i){
revgeocode(location[i,],
output = c("address"),
messaging = FALSE,
sensor = FALSE,
override_limit = FALSE)
})

Annotation Track in Gviz

Does anyone with experience using the bioconductor package: Gviz know how to add an AnnotationTrack directly over a DataTrack?
For example, in ggplot2 I can add to a prexitsting plot using + geom_text, but I haven't been able to locate a similar feature for Gviz
Thanks!
Although it's not exactly what you want, one possible solution is to add a HighlightTrack that covers your region of interest. Although this won't specifically label / add the key elements on to your DataTrack, it will help highlight the alignment between differing DataTracks.
Example:
library(Gviz)
library(GenomicRanges)
data(geneModels)
data(cpgIslands)
gen <- genome(cpgIslands)
chr <- 1
start <- 120005434
end <- 129695434
itrack <- IdeogramTrack(genome = gen, chromosome = chr)
gtrack <- GenomeAxisTrack()
grtrack <- GeneRegionTrack(geneModels, genome = gen, chromosome = chr, name = "foo")
htrack <- HighlightTrack(trackList = list(gtrack, grtrack), start = 121535434, end = 124535434, chromosome = chr)
plotTracks(list(itrack, htrack), from = start, to = end)
Graphical output
Although the grtrack is empty, it demonstrates how the HighlightTrack will span the specified DataTracks (in this case, grtrack and gtrack).
See the GViz documentation for more on info.

Resources