首页 > 解决方案 > “在验证窗口 1 中返回模型 1 的类 'try-error'”

问题描述

我正在尝试使用 ARIMA 模型对我的数据集进行提前预测。我正在预测“总”(y)对“年”(x)。我设法解决了由某些步骤导致的一些错误,这些步骤要求我的数据集采用数据框或时间序列的形式。但是,现在我在定义“data_fit”的步骤中收到以下错误。有人可以帮助我了解如何解决此错误吗?

Error in rep(1, n.ahead) : invalid 'times' argument
Error in if (is.null(outcome_levels) && method == "multi_output" && ncol(data_pred) !=  : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In FUN(X[[i]], ...) :
  Model 'ARIMA' returned class 'try-error' for model 1 in validation window 1

这是我正在使用的数据集的示例:

> str(EF_PP)
'data.frame':   57 obs. of  13 variables:
 $ Country.Name      : chr  "United States of America" "United States of America" "United States of America" "United States of America" ...
 $ Short.Name        : chr  "United States" "United States" "United States" "United States" ...
 $ year              : int  1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 ...
 $ Record            : chr  "EFConsPerCap" "EFConsPerCap" "EFConsPerCap" "EFConsPerCap" ...
 $ Built.up.Land     : num  0.0357 0.0351 0.0366 0.0347 0.0387 ...
 $ Carbon            : num  5.14 5.32 5.59 5.79 6.06 ...
 $ Cropland          : num  0.988 0.942 0.968 0.841 0.948 ...
 $ Fishing.Grounds   : num  0.1031 0.1006 0.0978 0.0916 0.0924 ...
 $ Forest.Products   : num  1.11 1.14 1.11 1.16 1.18 ...
 $ Grazing.Land      : num  0.619 0.619 0.604 0.563 0.565 ...
 $ Total             : num  8 8.16 8.4 8.48 8.89 ...
 $ Data.Quality.Score: chr  "3A" "3A" "3A" "3A" ...
 $ isoa2             : chr  "US" "US" "US" "US" ...

这是我正在使用的代码:

# Ecological Footprint 10 year forecast

lb = 56
# 56 years of total data for lookback
hr = 10
# 10 years for the forecast horizon
outcome_col = 1 
# EF_PP$Total
data = as.data.frame(EF_PP$Total)

data_train = forecastML::create_lagged_df(data, type = "train", method = "multi_output",
                                          outcome_col = 1, lookback = 1:lb, horizons = 1:hr)

windows = forecastML::create_windows(data_train, window_length = 0)

model_fn <- function(data, my_outcome_col) {
  outcome_names <- names(data)[1]
  model_formula <- formula(paste0(outcome_names,  "~ ."))  
  model <- arima(EF_PP$Total)
  return(model)
}

model_results = forecastML::train_model(data_train, windows, model_name = "ARIMA", model_function = model_fn)

predict_fn = function(model, data) {
  data_pred = as.data.frame(predict(model, as.matrix(data)))
}

data_fit = predict(model_results, prediction_function = list(predict_fn), data = data_train)

residuals = residuals(data_fit)

data_forecast = forecastML::create_lagged_df(data, type = "forecast", method = "multi_output",
                                             outcome_col = 1, lookback = 1:lb, horizons = 1:hr)

data_forecasts = predict(model_results, prediction_function = list(predict_fn), data = data_forecast)

data_forecasts = forecastML::combine_forecasts(data_forecasts)

plot(data_forecasts, data[-(1:lb), ], (1:nrow(data))[-(1:lb)], interval_alpha = seq(.1, .2, length.out = 10))

标签: rmodelpredictarimaforecast

解决方案


推荐阅读