0

I think my problem is easy, I just could not find a solution for it. So, I am trying to do bootstrapping in r and the results have been successful. However, I want to automate my code to make it run for 10000,15000, etc.. times without changing these variables every time.

My code is:

mydata #is a time series data

port<-as.xts(mydata, order.by = as.Date(dates, "%d-%b-%Y"))

# created a function for bootstrapping
sim<-function(nsim,series,size){
    result<-replicate(nsim, Return.cumulative(sample(series,size,replace=F), geometric=TRUE))
    return(result)
    }

output<-sim<-(10000,port,12) # running the function

mean(output) # finding the mean of the bootstrap output 

so instead of changing the nsim which is=10000 or the size =12 everytime I want to run the function, is there a way where I can loop the function to run lets say 10000, 150000,200000.

Thanks for your help!

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Soh
  • 1
  • Why would you want to change the number of times it runs? Isn't the idea to chose a number balancing robustness (i.e. the larger the better) and computational resources (i.e. the smaller the better)? – Jasper Jul 19 '17 at 09:38
  • I want to be able to compare the results for analytical purposes – Soh Jul 19 '17 at 10:21
  • Then I would take a loot at the `apply()` family: e.g. `lapply(c(10000, 15000), sim, series = ***, size = ****)` – Jasper Jul 19 '17 at 10:48
  • Thanks Jasper for your help, I tried it and I think it worked. however, how do I take for example the mean or sd of each output. for example the outcome of 10000 sim I want to take the mean, and the outcome of 15000 to take the mean also. – Soh Jul 19 '17 at 11:05
  • You can then use `lapply` on your results again (because they are in a list), and run the function you want to use. – Jasper Jul 19 '17 at 11:53
  • Thanks Jasper, you have been such a good help. last question if you let me: in orginal file I have two columns of different return series. when I do the analysis (bootstrapping, lapply, etc..) on a defined column it works fine. however if I do the same analysis on the two columns at once, it comes up as an error "Error in `[.xts`(x, sample.int(length(x), size, replace, prob)) : subscript out of bounds" is there a way to do lapply on both columns at the same time? – Soh Jul 20 '17 at 07:12

0 Answers0