从列表中提取ARIMA对象并存储在R中的数据框中

时间:2020-01-28 17:44:05

标签: r apply arima

我的目标是针对多个时间序列运行ARIMA,并将结果存储在数据框中。这是我的示例:

library(forecast)

my_df <- data.frame(gr  = c(rep("a", 10), rep("b", 10)), 
                    val = sample(1:100, 20))

get_arima <- function(sample_name = NA,df = NA){
  fit = auto.arima(df[df$gr == sample_name, ]$val)
  return(fit)
}

result <- sapply(c("a", "b"), get_arima, df = my_df, simplify = F)
result_df <- data.frame(gr = names(result), 
  model_result = unlist(result, use.names =  F))

它会产生以下错误:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ‘c("forecast_ARIMA", "ARIMA", "Arima")’ to a data.frame

目标是在第二栏中提供ARIMA模型,以便我执行类似forecast(result_df$model_result, h=3)

的操作

我猜想可能有多种方法,但是我希望用apply回答,并将结果从列表中放入数据框。

1 个答案:

答案 0 :(得分:1)

模型输出为list

str(result[[1]])
#List of 18
# $ coef     : Named num 54.3
#  ..- attr(*, "names")= chr "intercept"
# $ sigma2   : num 1454
# $ var.coef : num [1, 1] 131
# ...

,“结果”也是list。如果我们将模型保留为“ data.frame”中的列,则将其保留为list


df1 <- data.frame(gr = names(result), model_result = I(result)) 

使用tibble,我们可以直接创建一个list列,而无需任何I

library(tibble)
df1 <- tibble(gr = names(result), model_result = result)
df1
# A tibble: 2 x 2
#   gr    model_result
#  <chr> <named list>
#1 a     <ARIMA>     
#2 b     <ARIMA>     

[[

提取每个元素
df1$model_result[[1]]
#Series: df[df$gr == sample_name, ]$val 
#ARIMA(0,0,0) with non-zero mean 

#Coefficients:
#         mean
#      54.3000
#s.e.  11.4378

#sigma^2 estimated as 1454:  log likelihood=-50.07
#AIC=104.14   AICc=105.86   BIC=104.75

并应用forecast

forecast::forecast( df1$model_result[[1]], h = 3)
#Point Forecast    Lo 80  Hi 80     Lo 95    Hi 95
#11           54.3 5.439989 103.16 -20.42494 129.0249
#12           54.3 5.439989 103.16 -20.42494 129.0249
#13           54.3 5.439989 103.16 -20.42494 129.0249

如果我们要获取所有元素的forecast,请使用map

library(purrr)
map(df1$model_result, ~ forecast::forecast(.x, h = 3))