在R中计算时间序列模型的准确性时发生错误(NextMethod(.Generic)中的错误:(列表)对象无法强制键入“ double”类型)

时间:2018-09-24 09:30:37

标签: r time-series forecasting

使用以下代码:

#plotting time series from year 1998 to 2008 
    year.time_series <- ts(t_AMOUNT,start = c(1998) , frequency = 12 ) #Monthly 12
    plot(year.time_series)
#splitting the timeseries for further model evaluation 
    train <- window(year.timeseries, start=1998,end=2005)
    test <- window(year.timeseries, start=2005, end=2008)

#using models to check the accuracy results
    etsfit <- ets(train)
    summary(etsfit)

    plot(train, main="ETS Forecast", ylab = "ets(training set)", cex.lab = 1.5, cex.main = 1.5, cex.axis = 1.5)
    lines(etsfit$fitted, col="orange")

#forecast
    forecast.ets <- forecast(etsfit, h=24)
    summary(forecast.ets)
    plot(forecast.ets)

    plot(forecast.ets, main = "2 Year Forecast Using ETS Model",
         xlim = c(1998, 2008), cex.lab = 1.5, cex.main = 1.5, cex.axis = 1.5)
    lines(test, col="red")

    library(Metrics)
#input = forecast values, actual values
    accuracy(forecast.ets,test)

我在accuracy(forecast.ets,test)遇到以下错误:

  

NextMethod(.Generic)中的错误:
    (列表)对象不能强制输入“ double”

     

此外:警告消息:
  在!=.default(实际,预期)中:
    较长的物体长度不是较短的物体长度的倍数

是否可以拆分时间序列并计算其准确性?

1 个答案:

答案 0 :(得分:1)

问题是您使用的是Metrics::accuracy()而不是forecast::accuracy(),该功能可以实现我认为想要的功能。在解释了原因之后,我还提供了一些有关在Stack Overflow上提问的一般注释,如果将来对该站点有其他疑问,这可能对您有所帮助。

Metrics::accuracy()forecast::accuracy()

如果查看帮助文件(help("forecast::accuracy")help("Metrics::accuracy")),我们可以看到功能之间的某些差异。

关于预测准确性的参数就像

accuracy(f, x, test = NULL, d = NULL, D = NULL, ...)

其中f是“预测类的对象,或包含预测的数字向量...”,而x是“包含与长度相同的实际值的可选数字向量对象或与f的时间重叠的时间序列。”这与您尝试使用它的方式相匹配,将预测类对象作为第一个参数,第二个传递实际值的向量。

如果您想使用Metrics::accuracy(),则其参数就像

accuracy(actual, predicted)

其中actual是“地面真实向量,其中向量的元素可以是任何变量类型”,而predicted是“预测的向量,其中向量的元素表示对应的预测实际价值”。换句话说,您的第一个参数只能是预测本身,而不是forecast对象中存在的所有其他信息。我也不认为它可以为您提供这种分析所需的精度指标。它给出了“实际元素与预测元素相对应的比例”。

将来提出问题的一些建议

首先,我将查看强大的资源How to make a great R reproducible example。接下来,我将为您提供用来重现问题的代码,您将看到我什至必须开始进行的一些更改(我的评论以###开头):

#plotting time series from year 1998 to 2008 
### Since we don't have t_AMOUNT, we can't recreate your data
# year.time_series <- ts(t_AMOUNT, start = c(1998), frequency = 12) #Monthly 12
### So I did the following to make some dummy data
set.seed(42)
year.time_series <- ts(rnorm(12*11), start = c(1998), frequency = 12 )
plot(year.time_series)
#splitting the timeseries for further model evaluation
### Since there are spelling changes below for some reason,
### I had to do the next line (or change the variable names below)
year.timeseries <- year.time_series 
train <- window(year.timeseries, start=1998, end=2005)
test <- window(year.timeseries, start=2005, end=2008)

#using models to check the accuracy results
### We need the forecast library for ets(),
### but it wasn't loaded in your code
library(forecast) 
etsfit <- ets(train)
summary(etsfit)

plot(train, main = "ETS Forecast", ylab = "ets(training set)",
     cex.lab = 1.5, cex.main = 1.5, cex.axis = 1.5)
lines(etsfit$fitted, col = "orange")

#forecast
forecast.ets <- forecast(etsfit, h = 24)
summary(forecast.ets)
plot(forecast.ets)

plot(forecast.ets, main = "2 Year Forecast Using ETS Model",
     xlim = c(1998, 2008), cex.lab = 1.5, cex.main = 1.5, cex.axis = 1.5)
lines(test, col = "red")

library(Metrics)
#input = forecast values, actual values
accuracy(forecast.ets,test)
forecast::accuracy(forecast.ets, test)