business-science / modeltime.gluonts

GluonTS Deep Learning with Modeltime
https://business-science.github.io/modeltime.gluonts/
Other
38 stars 11 forks source link

modeltime_forecast() with option new_data=NULL throws an error #21

Closed GitHunter0 closed 3 years ago

GitHunter0 commented 3 years ago

First, congratulation for modeltime, what a fantastic ecosystem you are building.

So, consider this example:

library(modeltime.gluonts)
library(tidymodels)
library(tidyverse)

model_fit <- modeltime.gluonts::deep_ar(
    id                    = "id",
    freq                  = "M",
    prediction_length     = 24,
    lookback_length       = 36,
    epochs                = 10, 
    num_batches_per_epoch = 50,
    learn_rate            = 0.001,
    num_layers            = 2,
    dropout               = 0.10
  ) %>%
  set_engine("gluonts_deepar") %>%
  fit(formula = value ~ ., data = training(m750_splits))

calib <- modeltime_table(model_fit) %>%
  modeltime_calibrate(new_data = testing(m750_splits))

calib %>% modeltime_forecast(new_data = NULL, actual_data = m750) throws this error:

Using '.calibration_data' to forecast.
Error: Problem occurred during prediction. Most likely cause is missing external regressors. Try using 'new_data' and supply a dataset containing all required columns. Error in model.frame.default(mod_terms, new_data, na.action = na.action, : object is not a matrix

Error: Can't subset columns that don't exist.
x Column `.value` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
Unknown or uninitialised column: `.key`.

Thank you Matt

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

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

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

other attached packages:
 [1] modeltime.gluonts_0.2.2
 [2] timetk_2.6.1           
 [3] lubridate_1.7.10       
 [4] forcats_0.5.1          
 [5] stringr_1.4.0          
 [6] readr_1.4.0            
 [7] tidyverse_1.3.0        
 [8] modeltime_0.4.1        
 [9] yardstick_0.0.7        
[10] workflows_0.2.2        
[11] tune_0.1.2             
[12] tidyr_1.1.1            
[13] tibble_3.0.4           
[14] rsample_0.0.9          
[15] recipes_0.1.15         
[16] purrr_0.3.4            
[17] parsnip_0.1.5          
[18] modeldata_0.0.2        
[19] infer_0.5.3            
[20] ggplot2_3.3.3          
[21] dplyr_1.0.4            
[22] dials_0.0.9            
[23] scales_1.1.1           
[24] broom_0.7.0            
[25] tidymodels_0.1.1       

loaded via a namespace (and not attached):
  [1] PMCMRplus_1.9.0           
  [2] benchmarkmeData_1.0.4     
  [3] clisymbols_1.2.0          
  [4] pbapply_1.4-3             
  [5] lattice_0.20-41           
  [6] haven_2.3.1               
  [7] bigassertr_0.1.3          
  [8] vctrs_0.3.6.9000          
  [9] expm_0.999-5              
 [10] V8_3.4.0                  
 [11] usethis_1.6.1             
 [12] gmp_0.6-2                 
 [13] blob_1.2.1                
 [14] survival_3.2-7            
 [15] prodlim_2019.11.13        
 [16] later_1.1.0.1             
 [17] nloptr_1.2.2.2            
 [18] reactable_0.2.3           
 [19] prophet_0.6.1             
 [20] DBI_1.1.0                 
 [21] rappdirs_0.3.3            
 [22] forecast_8.14             
 [23] plm_2.2-4                 
 [24] jpeg_0.1-8.1              
 [25] DataExplorer_0.8.1        
 [26] MatrixModels_0.4-1        
 [27] anytime_0.3.9             
 [28] htmlwidgets_1.5.3         
 [29] mvtnorm_1.1-1             
 [30] future_1.21.0             
 [31] tseries_0.10-48           
 [32] miscTools_0.6-26          
 [33] inline_0.3.17             
 [34] pairwiseComparisons_3.1.2 
 [35] parallel_4.0.3            
 [36] Rcpp_1.0.5                
 [37] DT_0.15                   
 [38] promises_1.2.0.9000       
 [39] kSamples_1.2-9            
 [40] ppsr_0.0.1                
 [41] pkgload_1.1.0             
 [42] statsExpressions_0.7.0    
 [43] debugme_1.1.0             
 [44] RcppParallel_5.0.2        
 [45] Hmisc_4.4-1               
 [46] fs_1.5.0                  
 [47] expss_0.10.6              
 [48] bigreadr_0.2.0            
 [49] mnormt_2.0.2              
 [50] fastGHQuad_1.0            
 [51] digest_0.6.27             
 [52] png_0.1-7                 
 [53] disk.frame_0.3.7          
 [54] cowplot_1.1.1             
 [55] pkgconfig_2.0.3           
 [56] gower_0.2.2               
 [57] gt_0.2.2                  
 [58] iterators_1.0.12          
 [59] minqa_1.2.4               
 [60] GPfit_1.0-8               
 [61] correlationfunnel_0.2.0   
 [62] reticulate_1.18           
 [63] rstan_2.21.2              
 [64] xfun_0.19                 
 [65] zoo_1.8-9                 
 [66] tidyselect_1.1.0          
 [67] performance_0.6.1         
 [68] reshape2_1.4.4            
 [69] tsibble_0.9.3             
 [70] labelled_2.5.0            
 [71] viridisLite_0.3.0         
 [72] pkgbuild_1.2.0            
 [73] rlang_0.4.10              
 [74] manipulateWidget_0.10.1   
 [75] reactR_0.4.4              
 [76] tibbletime_0.1.6          
 [77] Rmpfr_0.8-2               
 [78] glue_1.4.2                
 [79] waldo_0.2.5               
 [80] gdtools_0.2.2             
 [81] RColorBrewer_1.1-2        
 [82] pryr_0.1.4                
 [83] lhs_1.1.1                 
 [84] modelr_0.1.8              
 [85] matrixStats_0.58.0        
 [86] ggcorrplot_0.1.3          
 [87] multcompView_0.1-8        
 [88] lava_1.6.9                
 [89] benchmarkme_1.0.4         
 [90] ggsignif_0.6.0            
 [91] bayestestR_0.8.2          
 [92] bigrquery_1.3.2           
 [93] LaplacesDemon_16.1.4      
 [94] labeling_0.4.2            
 [95] gbRd_0.4-11               
 [96] httpuv_1.5.4              
 [97] class_7.3-17              
 [98] Rttf2pt1_1.3.8            
 [99] PerformanceAnalytics_2.0.4
[100] tidyquant_1.0.1           
[101] rayshader_0.19.2          
[102] webshot_0.5.2             
[103] jsonlite_1.7.0            
[104] tmvnsim_1.0-2             
[105] bit_4.0.4                 
[106] mime_0.10                 
[107] systemfonts_0.3.2         
[108] fracdiff_1.5-1            
[109] gridExtra_2.3             
[110] Exact_2.0                 
[111] stringi_1.5.3             
[112] insight_0.12.0            
[113] BWStest_0.2.2             
[114] processx_3.4.5            
[115] RcppRoll_0.3.0            
[116] logspline_2.1.16          
[117] hardhat_0.1.5             
[118] quadprog_1.5-8            
[119] cli_2.3.1                 
[120] Rdpack_1.0.0              
[121] RSQLite_2.2.0             
[122] wrapr_2.0.2               
[123] data.table_1.14.0         
[124] correlation_0.5.0         
[125] rstudioapi_0.13           
[126] earth_5.3.0               
[127] nlme_3.1-149              
[128] janitor_2.1.0             
[129] listenv_0.8.0             
[130] ggthemes_4.2.0            
[131] pak_0.1.2.1               
[132] miniUI_0.1.1.1            
[133] simputation_0.2.4         
[134] listviewer_3.0.0          
[135] dbplyr_2.1.0              
[136] sessioninfo_1.1.1         
[137] TTR_0.24.2                
[138] readxl_1.3.1              
[139] lifecycle_1.0.0           
[140] networkD3_0.4             
[141] quantmod_0.4.18           
[142] timeDate_3043.102         
[143] Quandl_2.10.0             
[144] munsell_0.5.0             
[145] cellranger_1.1.0          
[146] codetools_0.2-16          
[147] coda_0.19-4               
[148] fst_0.9.4                 
[149] lmtest_0.9-38             
[150] shinyWidgets_0.5.3        
[151] htmlTable_2.0.1           
[152] rstantools_2.1.1          
[153] xtable_1.8-4              
[154] StanHeaders_2.21.0-6      
[155] abind_1.4-5               
[156] farver_2.1.0              
[157] parallelly_1.24.0         
[158] ggExtra_0.9               
[159] visdat_0.5.3              
[160] duckdb_0.2.1              
[161] pins_0.4.3                
[162] SuppDists_1.1-9.5         
[163] bibtex_0.4.2.2            
[164] rgl_0.103.5               
[165] dtplyr_1.1.0              
[166] cluster_2.1.0             
[167] future.apply_1.7.0        
[168] zeallot_0.1.0             
[169] extrafontdb_1.0           
[170] Matrix_1.2-18             
[171] ellipsis_0.3.1            
[172] prettyunits_1.1.1         
[173] reprex_0.3.0              
[174] ggridges_0.5.2            
[175] igraph_1.2.5              
[176] hrbrthemes_0.8.6          
[177] surveytoolbox_0.1.0       
[178] remotes_2.2.0.9000        
[179] lmerTest_3.1-3            
[180] paletteer_1.3.0           
[181] parameters_0.11.0         
[182] testthat_3.0.2            
[183] mc2d_0.1-18               
[184] htmltools_0.5.0.9003      
[185] yaml_2.2.1                
[186] ipmisc_5.0.2              
[187] loo_2.4.1                 
[188] utf8_1.2.1                
[189] plotly_4.9.3              
[190] huxtable_5.0.0            
[191] foreign_0.8-80            
[192] withr_2.4.1               
[193] xgboost_1.3.2.1           
[194] BayesFactor_0.9.12-4.2    
[195] bit64_4.0.2               
[196] plotmo_3.6.0              
[197] effectsize_0.4.3          
[198] foreach_1.5.0             
[199] progressr_0.7.0           
[200] devtools_2.3.1            
[201] memoise_1.1.0             
[202] evaluate_0.14             
[203] arrow_3.0.0               
[204] rio_0.5.16                
[205] extrafont_0.17            
[206] callr_3.5.1               
[207] estimatr_0.22.0           
[208] ps_1.5.0                  
[209] curl_4.3                  
[210] metafor_2.4-0             
[211] fansi_0.4.1               
[212] tidylog_1.0.2.9000        
[213] xts_0.12.1                
[214] furrr_0.2.2               
[215] checkmate_2.0.0           
[216] desc_1.3.0                
[217] maxLik_1.4-4              
[218] TeachingDemos_2.12        
[219] openxlsx_4.2.3            
[220] ggrepel_0.9.1             
[221] tsbox_0.2.1               
[222] rprojroot_2.0.2           
[223] tools_4.0.3               
[224] sass_0.3.1.9000           
[225] sandwich_2.5-1            
[226] magrittr_2.0.1            
[227] pacman_0.5.1              
[228] car_3.0-10                
[229] xml2_1.3.2                
[230] httr_1.4.2                
[231] assertthat_0.2.1          
[232] rmarkdown_2.6             
[233] afex_0.28-1               
[234] boot_1.3-25               
[235] globals_0.14.0            
[236] R6_2.5.0                  
[237] nnet_7.3-14               
[238] progress_1.2.2            
[239] Brobdingnag_1.2-6         
[240] gtools_3.8.2              
[241] statmod_1.4.35            
[242] repr_1.1.0                
[243] urca_1.3-0                
[244] rematch2_2.1.2            
[245] splines_4.0.3             
[246] snakecase_0.11.0          
[247] carData_3.0-4             
[248] colorspace_2.0-0          
[249] generics_0.1.0            
[250] stats4_4.0.3              
[251] base64enc_0.1-3           
[252] metaBMA_0.6.6             
[253] pillar_1.5.1              
[254] WRS2_1.1-0                
[255] naniar_0.6.0.9000         
[256] plyr_1.8.6                
[257] gtable_0.3.0              
[258] rvest_0.3.6               
[259] unikn_0.3.0.9009          
[260] bdsmatrix_1.3-4           
[261] zip_2.1.0                 
[262] psych_2.0.12              
[263] knitr_1.30                
[264] latticeExtra_0.6-29       
[265] fastmap_1.0.1             
[266] crosstalk_1.1.1           
[267] metaplus_0.7-11           
[268] doParallel_1.0.15         
[269] librarian_1.7.2           
[270] backports_1.2.1           
[271] plotrix_3.8-1             
[272] writexl_1.3.1             
[273] mongolite_2.2.1           
[274] ipred_0.9-11              
[275] vroom_1.3.0               
[276] connections_0.1.1         
[277] lme4_1.1-26               
[278] hms_1.0.0                 
[279] clock_0.0.0.9000          
[280] shiny_1.5.0.9005          
[281] grid_4.0.3                
[282] numDeriv_2016.8-1.1       
[283] ggstatsplot_0.6.8         
[284] DiceDesign_1.9            
[285] bbmle_1.0.23.1            
[286] DescTools_0.99.37         
[287] lazyeval_0.2.2            
[288] Formula_1.2-3             
[289] crayon_1.4.1              
[290] MASS_7.3-53               
[291] skimr_2.1.2               
[292] pROC_1.16.2               
[293] rscontract_0.1.1          
[294] reshape_0.8.8             
[295] bridgesampling_1.0-0      
[296] rpart_4.1-15              
[297] compiler_4.0.3  
mdancho84 commented 3 years ago

Error Cause

The error occurs here because it's looking for missing regressors. In this case, it wants a column in your new_data data frame called "id", since this was provided during training.

> calib %>%
+     modeltime_forecast()
Using '.calibration_data' to forecast.
Error: Problem occurred during prediction. Most likely cause is missing external regressors. Try using 'new_data' and supply a dataset containing all required columns. Error in model.frame.default(mod_terms, new_data, na.action = na.action, : object is not a matrix

Error: Can't subset columns that don't exist.
x Column `.value` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.

Used during training:

value ~ . converts to value ~ date + id.

> training(m750_splits)
# A tibble: 282 x 3
   id    date       value
   <fct> <date>     <dbl>
 1 M750  1990-01-01  6370
 2 M750  1990-02-01  6430
 3 M750  1990-03-01  6520
 4 M750  1990-04-01  6580
 5 M750  1990-05-01  6620
 6 M750  1990-06-01  6690
 7 M750  1990-07-01  6000
 8 M750  1990-08-01  5450
 9 M750  1990-09-01  6480
10 M750  1990-10-01  6820
# … with 272 more rows

Solution

You'll need to provide the testing data again in modeltime_forecast().

> calib %>%
+     modeltime_forecast(new_data = testing(m750_splits))
# A tibble: 24 x 7
   .model_id .model_desc .key       .index     .value .conf_lo .conf_hi
       <int> <chr>       <fct>      <date>      <dbl>    <dbl>    <dbl>
 1         1 DEEPAR      prediction 2013-07-01  9283.    8030.   10536.
 2         1 DEEPAR      prediction 2013-08-01  9234.    7981.   10487.
 3         1 DEEPAR      prediction 2013-09-01  9746.    8493.   10999.
 4         1 DEEPAR      prediction 2013-10-01 10324.    9071.   11577.
 5         1 DEEPAR      prediction 2013-11-01 10637.    9384.   11890.
 6         1 DEEPAR      prediction 2013-12-01 10446.    9193.   11699.
 7         1 DEEPAR      prediction 2014-01-01 10376.    9123.   11630.
 8         1 DEEPAR      prediction 2014-02-01 10531.    9278.   11784.
 9         1 DEEPAR      prediction 2014-03-01 10622.    9369.   11875.
10         1 DEEPAR      prediction 2014-04-01 10575.    9322.   11828.
# … with 14 more rows