您的示例无法复制,但这通常是一个非常简单的问题。第一步是编写一个函数,对每个时间序列做任何你想做的事情。我相信您想在上面的示例中执行所有操作,因此这样的函数应该可以解决问题:
runArima <- function(ts){
Higher.R2.18. <- auto.arima(ts,test="adf")
p <- plot(forecast(Higher.R2.18.,h=22))
pred <- forecast(Higher.R2.18.,h=22)
correlation6 <- cor(fitted(Higher.R2.18.),ts)
return(list(arima=Higher.R2.18.,plot=p,forecast=pred,cor=correlation6))
}
然后,您只需将该函数应用于您的时间序列列表。更“R”的方式是使用 apply 语句:
arima.list <- lapply(<list of timeseries>,runArima)
但是,如果您愿意,可以使用循环:
arima.list <- vector(length(<list of timeseries>),"list")
i <- 1
for(ts in <list of timeseries>){
arima.list[[i]] <- runArima(ts)
i <- i + 1
}
这是一个包含一些琐碎数据的示例:
> library(forecast)
> #make trivial list of time series since i don't have one
> ts.list <- lapply(1:5,function(x) WWWusage)
> #run your function across list of time series
> arima.list <- lapply(ts.list,runArima)
> #look at some results
> arima.list[[1]]$arima
Series: ts
ARIMA(2,2,0)
Coefficients:
ar1 ar2
0.2579 -0.4407
s.e. 0.0915 0.0906
sigma^2 estimated as 10.13: log likelihood=-252.73
AIC=511.46 AICc=511.72 BIC=519.22
> arima.list[[1]]$cor
[1] 0.9972077