r语言 - 通过指数平滑获取预测的 RMSE



在使用R对时间序列进行指数平滑时,我以纽黑文的年平均气温为例。

该代码使用 1912 年至 1960 年作为训练数据,并生成未来 11 年的预测。

我想将预测与 1961 年至 1971 年的实际数据进行比较,但存在 2 个问题:

  1. 获取实际值的"nht_1"返回一些错误的数字
  2. 尝试获取 RMSE 时弹出错误:

    order(y( 中的错误:在 orderVector1 中未实现类型"list">

如何更正它们,并获得预测值的 RMSE?谢谢。

(注意:除了使用预测包中的精度命令。我想尝试以这种方式获得RMSE...

df <- read.csv("D:\Documents\nhtemp.csv")
nht <- ts(df$value, 
start = c(1912),
end = c(1960),
frequency = 1)
nht.hw1 <- HoltWinters(df$value, gamma = F); nht.hw1
library(forecast)
nht.forecast <- forecast(nht.hw1, h = 11)
nht.forecast
# I want to compare the forecast with the actual of year 1961 to 1971:
nht_1 <- ts(df$value, 
start = c(1961),
end = c(1971),
frequency = 1)
nht_1
# returns wrong numbers: 49.9 52.3 49.4 51.1 49.4 47.9 49.8 50.9 49.3 51.9 50.8
# For getting its RMSE
library(caret)
postResample(nht_1, nht.forecast)
# Error in order(y) : unimplemented type 'list' in 'orderVector1'

下面是有关如何检查预测对象准确性的示例:

library(forecast)
data(woolyrnq) #data I will use, it is already a ts object

stats::window函数可用于子集ts

train <- window(woolyrnq, end = c(1984,4)) #a vector of two numbers, year and quarter since its quarterly ts
test <- window(woolyrnq, start = c(1985,1), end = c(1987, 4))

估算模型:

nht.hw1 <- HoltWinters(train, gamma = FALSE)

获取预测:

nht.forecast <- forecast(nht.hw1, h = 12)

检查精度:

accuracy(nht.forecast, x = test)
#output
ME     RMSE      MAE       MPE     MAPE     MASE      ACF1 Theil's U
Training set  -69.69645 679.9740 554.6501 -1.877270 10.31036 1.136701 0.1882675        NA
Test set     -504.14620 809.8686 638.8314 -9.699182 11.78262 1.309222 0.1399736 0.9250198

如果要使用caret

library(caret)
RMSE(pred = nht.forecast$mean, #just the mean and not the data frame with the CIs
obs = test)
#output
809.8686

编辑 使用相关数据:

df <- read.csv("nhtemp.csv")

根据所有数据创建时间序列:

nht <- ts(df$value, 
start = c(1912),
end = c(1971),
frequency = 1)

创建训练集和测试集:

train <- window(nht, end = 1960) #just one number as end since its yearly data
test <- window(nht, start = 1961)

适合:

nht.hw1 <- HoltWinters(train, gamma = FALSE)

预测

nht.forecast <- forecast(nht.hw1, h = 10)

评价

accuracy(nht.forecast, x = test)
ME      RMSE       MAE        MPE      MAPE      MASE        ACF1 Theil's U
Training set -0.25921398 1.7027155 1.3840629 -0.5616971 2.7249636 1.0462208 -0.05478676        NA
Test set     -0.04523057 0.5478937 0.4651413 -0.0981410 0.9080928 0.3516029  0.08720340 0.7664426

最新更新