在先知中使用组和回归进行预测

问题描述

我正在尝试使用先知库通过Group and Regressors来预测y。我的代码和收到的错误如下。

  1. 在Model1中:

我收到此错误:setup_dataframe(object,df)中的错误
数据框缺少回归指标“ x1”

  1. 在Model2中:

Model2运行。但我无法弄清楚如何添加回归变量x1 和x2。

图书馆(先知) 库(dplyr)

df <- data.frame(ds = rep(c("2020-01-01","2020-01-02","2020-01-03","2020-01-04","2020-01-05","2020-01-06","2020-01-07","2020-01-08","2020-01-09","2020-01-10","2020-01-11","2020-01-12","2020-01-13","2020-01-14","2020-01-15"),2),group = rep(c("A","B"),each = 15),y = c(8.15,1.74,2.97,2.36,0.94,1.84,3.17,12.51,0.63,6.92,5.51,7.50,-2.47,4.38,6.28,7.69,2.89,3.77,7.27,-1.19,4.64,9.49,5.43,0.36,14.12,8.77,-3.05,-0.72,10.99,10.33),x1 = c(3.11,2.16,0.91,2.78,0.06,1.12,1.73,3.95,1.43,3.40,2.37,1.80,0.95,1.66,3.06,-0.23,3.11,3.07,-0.39,0.13,2.15,1.61,1.54,5.50,2.21,0.89,3.24,4.27,2.55),x2 = c(2.52,-0.21,1.03,0.44,0.72,4.28,-0.40,1.76,1.57,2.85,-1.71,1.36,3.96,-0.11,0.35,3.83,-0.66,3.67,1.91,-0.59,4.31,3.28,-1.97,-1.98,3.36,3.89))

    df$ds <- as.Date(df$ds)


# Model 1

Model1 <- function(df) {
  m <- prophet(seasonality.mode = 'multiplicative')
  m <- add_regressor(m,'x1')
  m <- add_regressor(m,'x2')
  m <- fit.prophet(m,df)
  future <- make_future_dataframe(m,periods = 5,freq = 'day')
  mod1 <- predict(m,future)
  return(mod1)
}

mod1 <-df %>%  
  group_by(group) %>%
  do(Model1(.)) %>% 
  dplyr::select(ds,group,yhat)



# Model 2

library(prophet)
library(dplyr)
library(purrr)
library(tidyr)

Model2 <- df %>% 
  nest(-group) %>% 
  mutate(m = map(data,prophet)) %>% 
  mutate(future = map(m,make_future_dataframe,period = 5)) %>% 
  mutate(forecast = map2(m,future,predict))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)