在 R 中计算时间序列模型的准确性时出错(NextMethod(.Generic) 中的错误:(list) 对象不能强制键入 'double')
Error while calculating accuracy of a timeseries model in R (Error in NextMethod(.Generic) : (list) object cannot be coerced to type 'double')
使用此代码:
#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)
收到以下错误:
Error in NextMethod(.Generic) :
(list) object cannot be coerced to type 'double'
In addition: Warning message:
In !=.default
(actual, predicted) :
longer object length is not a multiple of shorter object length
有没有办法拆分时间序列并计算其准确性?
问题是您使用的是 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
是 "An object of class “forecast”, or a numerical vector containing forecasts...." 而 x
是 "An optional numerical vector containing actual values of the same length as object, or a time series overlapping with the times of f." 这与您尝试使用它的方式相匹配,将预测作为第一个参数传递 class 对象,第二个是实际值的向量。
如果你想使用Metrics::accuracy()
,它的参数就像
accuracy(actual, predicted)
其中 actual
是 "The ground truth vector, where elements of the vector can be any variable type" 而 predicted
是 "The predicted vector, where elements of the vector represent a prediction for the corresponding value in actual." 换句话说,您的第一个参数必须是 only 预测本身,而不是 forecast
对象中存在的所有其他信息。我也不认为它可以为您提供此类分析所需的准确度指标类型;它给出 "the proportion of elements in actual that are equal to the corresponding element in predicted".
以后提问的一些建议
首先,我会查看很棒的资源 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)
使用此代码:
#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)
收到以下错误:
Error in NextMethod(.Generic) :
(list) object cannot be coerced to type 'double'In addition: Warning message:
In!=.default
(actual, predicted) :
longer object length is not a multiple of shorter object length
有没有办法拆分时间序列并计算其准确性?
问题是您使用的是 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
是 "An object of class “forecast”, or a numerical vector containing forecasts...." 而 x
是 "An optional numerical vector containing actual values of the same length as object, or a time series overlapping with the times of f." 这与您尝试使用它的方式相匹配,将预测作为第一个参数传递 class 对象,第二个是实际值的向量。
如果你想使用Metrics::accuracy()
,它的参数就像
accuracy(actual, predicted)
其中 actual
是 "The ground truth vector, where elements of the vector can be any variable type" 而 predicted
是 "The predicted vector, where elements of the vector represent a prediction for the corresponding value in actual." 换句话说,您的第一个参数必须是 only 预测本身,而不是 forecast
对象中存在的所有其他信息。我也不认为它可以为您提供此类分析所需的准确度指标类型;它给出 "the proportion of elements in actual that are equal to the corresponding element in predicted".
以后提问的一些建议
首先,我会查看很棒的资源 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)