【问题标题】:R remove saturday and sunday from weekly seasonality analysis with prophetR从先知的每周季节性分析中删除周六和周日
【发布时间】:2020-03-07 17:33:05
【问题描述】:

使用先知库,我能够提取每周的季节性。

library("prophet")

history <- data.frame(ds = seq(as.Date('2015-01-01'), as.Date('2016-01-01'), by = 'd'),y = sin(1:366/200) + rnorm(366)/10)

m <- prophet(history)
future <- make_future_dataframe(m, periods = 365)
forecast <- predict(m, future)
prophet_plot_components(m, forecast)

但即使基础数据不包括这些日子,该分析也包括周六和周日。我需要在先知函数中添加哪些参数来排除 Sat 和 Sun? 感谢您的帮助。

【问题讨论】:

    标签: r


    【解决方案1】:

    我不知道prophet 包中是否有删除周末的选项,但您可以使用基础R 中的weekdays 函数来确定日期是星期几,然后排除周六和周日,例如,dplyr:

    history <- data.frame(ds = seq(as.Date('2015-01-01'), as.Date('2016-01-01'), by = 'd'),y = sin(1:366/200) + rnorm(366)/10)
    nrow(history)
    #> [1] 366
    
    library(dplyr)
    history_week <- history %>% 
      mutate(wd = weekdays(ds)) %>% 
      filter(wd != c("Saturday", "Sunday"))
    nrow(history_week)
    #> [1] 314
    

    【讨论】:

    • 编辑:误读了您的代码,现在注意到您正在修改“历史”变量。?我认为 OP 是在说明历史已经不包含周末。他希望 Prophet 也不要预测周末(因为历史数据也不包含那些日子)。
    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2019-02-18
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    相关资源
    最近更新 更多