问题
i have a problem. I hope that you will understand me. My english is not very good. I need to repeat B times (where B is at least one thousand) a R code that i write and collect the 1000 final results. I report the code:
phi=0.5
p=1
n=100
M=1000
serie1=arima.sim(model=list(ar =phi ,order=c(p,0,0)),n = n,innov=rnorm(100,0,1))
stima<-arima(serie1,order=c(p,0,0),include.mean=F)
phi.stima=stima$coef
res=stima$residual
ress=res[2:n]
res.c=ress-mean(ress)
serie.b=vector("numeric",length=n+M)
ps=round(runif(1,1,n-1))
serie.b[1]=ps
for(i in (2):(n+M))
serie.b[i]=phi.stima*serie.b[i-1]+res.c[round(runif(1,1,n-1))]
serie.bb=serie.b[(M+1):(n+M)]
serie.bb=ts(serie.bb)
stima.boot<-arima(serie.bb,order=c(1,0,0),include.mean=F)
phi.stima.boot=stima.boot$coef
phi.res.boot=stima.boot$residual
serie.bb[100]=serie1[100]
previsioni.boot=vector("numeric",length=6)
previsioni.boot[1]=serie1[100]
for(i in (2):(6))
previsioni.boot[i]=phi.stima.boot*previsioni.boot[i-1]
for(i in (2):(100+1000))
serie.b[i]=phi.stima*serie.b[i-1]+res.c[round(runif(1,1,100))]
future_obs_boot=vector("numeric",length=6)
future_obs_boot[1]=serie1[100]
for(i in (2):(6))
future_obs_boot[i]=phi.stima*future_obs_boot[i-1]+res.c[round(runif(1,1,100))]
errore.prev.boot=future_obs_boot[6]-previsioni.boot[6]
errore.prev.boot
I need to collect B values of "errore.prev.boot". How can i do it.. Please can you help me?? Greetings. Mario
回答1:
@Shree mentioned a hint to an answer in a comment above. This is one way (I tested on my machine and it works):
error.fun <- function(...){
# put your code here
}
map_dbl(1:1000, error.fun)
... sample results ...
[1] 0.503403119 0.836490778 1.355713864 -0.020078987 0.417293942 -0.677124914
[7] 1.187536510 -1.802907914 1.479408818 -2.383291163 0.242726786 0.197536877
[13] -0.590292009 -0.104944644 -1.776636919 0.143165564 -0.363264225 1.443686564
来源:https://stackoverflow.com/questions/56605763/repeat-many-times-the-same-code-with-r-and-collect-the-final-results