在R中计算时间序列模型的精度时出错(NextMethod(.Generic)中的错误:(列表)对象不能强制为类型“double”)

qjp7pelc  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(97)

使用此代码:

#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(.泛型)中的错误:
不能将(列表)对象强制为类型“double”
此外:警告消息:
!=.default(实际,预测)中:
较长的对象长度不是较短对象长度的倍数
有没有一种方法可以分割时间序列并计算其准确性?

fdbelqdn

fdbelqdn1#

问题是你使用的是Metrics::accuracy()而不是forecast::accuracy(),这是我认为你想要的功能。在解释了原因之后,我也有一些关于在Stack Overflow上提问的一般性说明,如果你将来对这个网站有其他问题,可能会对你有帮助。

Metrics::accuracy() vs. forecast::accuracy()

如果我们查看帮助文件(help("forecast::accuracy")help("Metrics::accuracy")),我们可以看到函数之间的一些差异。
预测准确性的论据如下

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

其中f是“An object of class“forecast”,or a numeric vector containing forecasts....”,x是“An optional numeric vector containing actual values of the same length as object,or a time series overlapping with the times of f.”这与您尝试使用它的方式相匹配,将forecast class对象作为第一个参数传递,将实际值的向量作为第二个参数传递。
如果你想使用Metrics::accuracy(),它的参数如下:

accuracy(actual, predicted)

其中actual是“真实向量,其中向量的元素可以是任何变量类型”,predicted是“预测向量,其中向量的元素表示实际对应值的预测”。换句话说,您的第一个参数必须是only预测本身,并不是所有的其他信息都存在于一个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)

相关问题