通过指数平滑获得预测的RMSE

时间:2018-09-13 05:54:01

标签: r time-series

在使用R进行时间序列的指数平滑时,我以Average Yearly Temperatures in New Haven为例。

该代码使用1912年至1960年作为培训数据,并对未来11年进行了预测。

我想将预测值与1961年到1971年的实际值进行比较,但是有两个问题:

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

    order(y)错误:'orderVector1'中未实现的类型'list'

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

(注意:除了使用预报包中的precision命令。我想尝试以这种方式获取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'

2 个答案:

答案 0 :(得分:1)

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

sizeof(pData)

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)

答案 1 :(得分:0)

您可以仅在插入符号中使用已实施的RMSE。只要确保预测和观察确实在同一时期即可:

pred <- nht.forecast
obs <- nht_1
RMSE(pred=pred,obs=obs)