使用fable Prophet训练PM2.5时序模型返回null model的解决方法
解决Prophet模型返回NULL Model的问题
问题描述
用户拥有以下4小时频率的tsibble数据集,尝试基于此构建Prophet模型预测PM2.5,但无论调整公式,模型始终返回<NULL model>:
modi_delhi_tsib # A tsibble: 6,576 x 21 [4h] <UTC> `Time Periods` PM2.5 NO NO2 NOx NH3 SO2 CO Ozone Benzene Toluene Temp RH WS WD SR BP AT RF `TOT-RF` VWS <dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 2020-01-01 00:00:00 549. 181. 91.2 188. 79.7 10.2 3.52 23.4 NA NA 7.75 93.4 0.254 168. 9.05 921. 6.75 0 0 -0.00947 2 2020-01-01 04:00:00 435. 194. 79.7 203. 66.3 11.6 3.46 29.3 9.83 NA 7.49 95.3 0.233 192. 20.7 932. 5.99 0 0 -0.0125 3 2020-01-01 08:00:00 453. 112. 122. 154. 74.4 10.6 2.46 30.2 9.09 NA 9.45 67.7 0.805 110. 258. 918. 11.3 0 0 -0.00434 4 2020-01-01 12:00:00 187. 56.9 114. 91.5 74.4 12.2 1.41 30.5 3.88 104. 11.3 51.0 1.16 96.5 271. 904. 15.6 0 0 0.0549 5 2020-01-01 16:00:00 263. 77.4 124. 117. 74.4 13.3 2.69 18.0 5.61 96.7 10.2 69.7 0.375 157. 41.1 916. 12.5 0 0 0.0547 6 2020-01-01 20:00:00 725. 77.4 124. 117. 74.4 20.8 3.12 15.7 5.61 182. 8.76 89.1 0.220 185. 8.82 926. 9.50 0 0 0.0197 7 2020-01-02 00:00:00 541. 77.4 124. 117. 74.4 16.1 5.31 16.6 5.61 181. 8.4 92.3 0.219 183. 6.05 917. 8.52 0 0 0.0152 8 2020-01-02 04:00:00 428. 77.4 124. 117. 74.4 13.8 3.13 19.0 5.61 130. 8.14 94.6 0.247 186. 16.3 929. 7.62 0 0 0.0171 9 2020-01-02 08:00:00 383. 127. 126. 164. 74.4 22.3 1.97 23.8 7.40 92.8 10.4 68.9 0.494 145. 269. 923. 13.3 0 0 0.0359 10 2020-01-02 12:00:00 166. 53.6 113. 86.6 74.4 16.9 0.975 28.2 4.26 26.0 11.9 46.8 0.755 80.0 268. 895. 17.9 0 0 0.0756 # ℹ 6,566 more rows # ℹ Use `print(n = ...)` to see more rows
运行以下代码时均返回NULL模型:
modi_delhi_tsib |> model(prophet(PM2.5~season("year", 4, type = "multiplicative"))) # A mable: 1 x 1 `prophet(PM2.5 ~ season("year", 4, type = "multiplicative"))` <model> 1 <NULL model> Warning message: 1 error encountered for prophet(PM2.5 ~ season("year", 4, type = "multiplicative")) [1] > modi_delhi_tsib |> + model(prophet(PM2.5)) # A mable: 1 x 1 `prophet(PM2.5)` <model> 1 <NULL model> Warning message: 1 error encountered for prophet(PM2.5) [1]
用户的会话信息:
R version 4.3.1 (2023-06-16 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22621) Matrix products: default locale: [1] LC_COLLATE=English_India.utf8 LC_CTYPE=English_India.utf8 LC_MONETARY=English_India.utf8 LC_NUMERIC=C LC_TIME=English_India.utf8 time zone: Asia/Calcutta tzcode source: internal attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] fable_0.3.3 feasts_0.3.1 tsibbledata_0.4.1 fpp3_0.5 tsibble_1.1.3 fable.prophet_0.1.0.9000 [7] fabletools_0.3.3 Rcpp_1.0.11 hms_1.1.3 patchwork_1.1.3 stringdist_0.9.10 YaleToolkit_4.2.3 [13] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.3 purrr_1.0.2 readr_2.1.4 [19] tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.3 tidyverse_2.0.0 loaded via a namespace (and not attached): [1] gtable_0.3.4 anytime_0.3.9 QuickJSR_1.0.6 xfun_0.40 processx_3.8.2 inline_0.3.19 corrr_0.4.4 [8] callr_3.7.3 tzdb_0.4.0 vctrs_0.6.3 tools_4.3.1 ps_1.7.5 generics_0.1.3 stats4_4.3.1 [15] parallel_4.3.1 fansi_1.0.4 highr_0.10 pkgconfig_2.0.3 prophet_1.0 distributional_0.3.2 RcppParallel_5.1.7 [22] lifecycle_1.0.3 compiler_4.3.1 farver_2.1.1 munsell_0.5.0 codetools_0.2-19 pillar_1.9.0 crayon_1.5.2 [29] ellipsis_0.3.2 StanHeaders_2.26.28 iterators_1.0.14 foreach_1.5.2 rstan_2.26.23 tidyselect_1.2.0 digest_0.6.33 [36] stringi_1.7.12 colorspace_2.1-0 cli_3.6.1 magrittr_2.0.3 loo_2.6.0 pkgbuild_1.4.2 utf8_1.2.3 [43] withr_2.5.1 rappdirs_0.3.3 prettyunits_1.2.0 scales_1.2.1 timechange_0.2.0 matrixStats_1.0.0 gridExtra_2.3 [50] progressr_0.14.0 evaluate_0.22 knitr_1.44 rlang_1.1.1 glue_1.6.2 jsonlite_1.8.7 rstudioapi_0.15.0 [57] R6_2.5.1
解决方案
确认tsibble的索引设置:tsibble必须有明确的时间索引,重新指定索引确保识别正确:
modi_delhi_tsib <- as_tsibble(modi_delhi_tsib, index = `Time Periods`) # 验证索引频率 interval(modi_delhi_tsib)如果频率识别错误,手动指定4小时间隔:
modi_delhi_tsib <- modi_delhi_tsib %>% as_tsibble(index = `Time Periods`, interval = hours(4))处理PM2.5列的缺失值:Prophet无法处理含缺失值的目标变量,先排查并填充:
# 统计缺失值数量 sum(is.na(modi_delhi_tsib$PM2.5)) # 用线性插值填充缺失值(可根据数据特性更换填充方法) modi_delhi_tsib <- modi_delhi_tsib %>% mutate(PM2.5 = na_interpolation(PM2.5))切换fable.prophet到稳定版:当前使用的开发版
fable.prophet_0.1.0.9000可能存在兼容性问题,更换为CRAN稳定版:remove.packages("fable.prophet") install.packages("fable.prophet") library(fable.prophet)用原生prophet包验证:先脱离fable框架测试模型是否能运行,排除接口问题:
library(prophet) # 转换为prophet要求的格式 prophet_data <- modi_delhi_tsib %>% select(ds = `Time Periods`, y = PM2.5) %>% drop_na() # 训练模型 m <- prophet(prophet_data) # 生成预测序列并预测 future <- make_future_dataframe(m, periods = 12, freq = "4 hours") forecast <- predict(m, future)如果原生模型能正常运行,再回到fable框架调试接口问题。
统一时间列时区:数据集时区为UTC,但系统时区是Asia/Calcutta,可能存在冲突,统一时区后重新设置索引:
modi_delhi_tsib <- modi_delhi_tsib %>% mutate(`Time Periods` = force_tz(`Time Periods`, tzone = "Asia/Calcutta")) %>% as_tsibble(index = `Time Periods`)
内容的提问来源于stack exchange,提问作者Rishav Dhariwal
相关产品推荐
相关产品推荐

