r - How to Repeat some consecutive lines Nth Times in R
问题描述
How can I repeat my line 22 to line 26 ten times serially and add the result of line 26 from first to the 10th and finally get the average of the 10 consecutive sum in my r code bellow:
# simulate arima(1,1,0)
library(forecast)
set.seed(100)
wn <- rnorm(10, mean = 0, sd = 1)
ts <- wn[1:2]
for (i in 3:10){
ts<-arima.sim(n=10,model=list(ar=-0.7048,order=c(1,1,0)),start.innov=4.1,n.start=1,innov=wn)
}
ts <-ts[-1]
#
# write the function for RMSE
rmse <- function(x) {
m <- auto.arima(x)
acu <- accuracy(m)
acu[1, 2]
}
#
t<-length(ts)# the length of the time series
l<- 2# block size
m <- ceiling(t / l) # number of blocks
blk<-split(ts, rep(1:m, each=l, length.out = t)) # divides the series into blocks
res<-sample(blk, replace=T, 1000) # resamples the blocks
unlist<-unlist(res, use.names = F) # unlist the bootstrap series
tsunlist<-ts(unlist) # turns the bootstrap series into time series data
# use the RMSE function
RMSE <- rmse(tsunlist)
The above R
code performs the following algorithm in steps:
- Simulate
ARIMA(1,1,0)
time series (line 1 to line 9) - Split the time series into blocks of equal size of 2 (line 18 to line 21)
- Resample each block at random 1000 times (line 22 to line 23)
- Rearrange the resampled series into time series data (line 24)
- Obtain the RMSE of the resampled time series (line 26)
I want to repeat step 3 to 5 100 times, add up the results produced in step 5 100 times and get the average of the results.
解决方案
Here is an approach for how to perform the resample 100 times. Replace your code starting on line 22 with this.
finalresult <- vector() #initialize vector to receive result from for loop
for(i in 1:100){
res<-sample(blk, replace=T, 1000) # resamples the blocks
res.unlist<-unlist(res, use.names = F) # unlist the bootstrap series
tsunlist<-ts(res.unlist) # turns the bootstrap series into time series data
# use the RMSE function
RMSE <- rmse(tsunlist)
finalresult[i] <- RMSE # Assign RMSE value to final result vector element i
}
Once you run the loop you will find the results in the finalresult
object.
finalresult
[1] 0.4695763 0.4702997 0.4727734 0.4677841 0.4633566 0.4619570 0.4645237 0.4657333 0.4734756 0.6035923 0.4676718 0.4563636
[13] 0.4653432 0.4741868 0.4638185 0.4679926 0.4652872 0.4644442 0.4673774 0.4654423 0.4574012 0.4613827 0.4689168 0.4652262
[25] 0.4621680 0.4714052 0.4544405 0.4781833 0.4640436 0.4708187 0.4562165 0.4631720 0.4638906 0.4654569 0.4691919 0.4644442
[37] 0.4635361 0.4657427 0.4682825 0.4626979 0.4636363 0.4562305 0.4582826 0.4689343 0.4648181 0.4659912 0.4597617 0.4701386
[49] 0.4678786 0.4658028 0.4633929 0.4759015 0.4719038 0.4685480 0.4639831 0.4663984 0.4631158 0.4636240 0.4677248 0.4680744
[61] 0.4633999 0.4734937 0.4545364 0.4671157 0.4656818 0.4638791 0.4676848 0.4650673 0.4710859 0.4680129 0.4641621 0.4632793
[73] 0.4664942 0.4596942 0.4643477 0.4626547 0.4684443 0.4572568 0.4695198 0.4566412 0.4650888 0.4643392 0.4603766 0.4694628
[85] 0.4579581 0.4627443 0.4729837 0.4668802 0.4723182 0.4688657 0.4684541 0.4655471 0.4687285 0.4504862 0.4599657 0.4660643
[97] 0.5093069 0.4620558 0.4655066 0.4682742
推荐阅读
- redux - TypeError: Object(...) is not a function (redux)
- templates - 在 helm 图表中附加 yaml 锚点
- javascript - handel 从 dropbix-api (nodejs 和 express) 返回的对象
- java - 空白的最终字段 conn 可能尚未在非最终成员上初始化
- linux - 验证多个目录存在于其相应的分支上
- .net-core - 使用 Swashbuckle 从多个 .NET Core 项目中获取一份 OpenAPI 文档
- r - 从预测函数导出信息的问题 - 特别是在 R 中的日期
- c++ - c ++ OpenCV Rect大小增加
- java - 无法在类 org.apache.camel.builder.DeadLetterChannelBuilder 上找到属性描述符 useOriginalBody
- java - 如何在 Java 中捕获异常?