使用“预测”包中的前馈神经网络时出错

时间:2017-06-16 13:46:40

标签: r time-series

使用nnetar包中的forecast函数时出现以下错误:

  

rowMeans中的错误(sapply(fit,predict)):    'x'必须是至少两个维度的数组   来自:rowMeans(sapply(fit,predict))

但是当我采用相同的输入数据并逐行运行nnetar函数时没有错误(我可以这样做,因为它的源可用)。你能帮我理解错误背后的原因吗?

重现错误的代码:

library("forecast")

x <- structure(c(75.4076478, 74.77923336, 74.27204018, 73.47822515, 
            73.29823134, 72.91366804, 73.26790178, 74.09299464, 74.95585689, 
            75.43382793, 77.15040162, 78.22886226, 78.30629811, 78.79626749, 
            78.58930659, 77.3918156, 76.9386785, 76.39462267, 75.96094635, 
            75.20484764, 73.75982041, 73.37559835, 73.28544776, 72.63796541
), .Tsp = c(1, 1.95833333333333, 24), class = "ts")

nnetar(x, p = 6)

会话信息:

R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forecast_8.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9       quadprog_1.5-5    lattice_0.20-33   zoo_1.7-14        lmtest_0.9-35    
 [6] assertthat_0.1    grid_3.3.0        plyr_1.8.4        gtable_0.2.0      magrittr_1.5     
[11] scales_0.4.1      ggplot2_2.2.1     lazyeval_0.2.0    tseries_0.10-40   fracdiff_1.4-2   
[16] timeDate_3012.100 tools_3.3.0       munsell_0.4.3     parallel_3.3.0    colorspace_1.3-2 
[21] nnet_7.3-12       tibble_1.2

1 个答案:

答案 0 :(得分:3)

问题在于季节性。默认情况下,nnetar()会尝试在季节性系列中包含一个季节性延迟。因此在这种情况下它会尝试使用24的延迟,但系列只有24个观察长度,因此没有数据需要训练。

使用nnetar(as.numeric(x), p=6)通过剥离ts属性来解决问题,因此nnetar不再知道数据是季节性的。

更简洁的方法是nnetar(x, p=6, P=0)

我已在github更新了该功能,因此它现在测试短序列,如果数据不足则不包括季节性延迟。

相关问题