用R多次重复相同的代码并收集最终结果 [英] Repeat many times the same code with R and collect the final results
本文介绍了用R多次重复相同的代码并收集最终结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题.我希望你能理解我.我的英文不是很好. 我需要重复B次(其中B至少为一千),我编写并收集1000个最终结果的R代码.我报告代码:
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
我需要收集"errore.prev.boot"的B值.我该怎么办..请您能帮帮我吗? 你好. 马里奥(Mario)
I need to collect B values of "errore.prev.boot". How can i do it.. Please can you help me?? Greetings. Mario
推荐答案
@Shree在上面的评论中提到了答案提示.这是一种方法(我在我的机器上进行了测试,并且可以正常工作):
@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
这篇关于用R多次重复相同的代码并收集最终结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文