【问题标题】:get RMSE of forecast by Exponential Smoothing通过指数平滑获得预测的 RMSE
【发布时间】:2019-02-17 19:37:40
【问题描述】:

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

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

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

  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'

【问题讨论】:

    标签: r time-series


    【解决方案1】:

    这是一个关于如何检查预测对象准确性的示例:

    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
    

    【讨论】:

    • 这是一个很棒的答案和帮助!了不起的工作,继续分享,@missue!
    猜你喜欢
    • 2021-01-08
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2021-07-08
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多