1

I'm trying to use prophet library to predict y using Group and Regressors. My code and the errors received are below.

  1. In Model1:

I've received this error: Error in setup_dataframe(object, df) :
Regressor "x1" missing from dataframe

  1. In Model2:

Model2 runs. But I'm unable to figure it out how to add regressors x1 and x2.

library(prophet) library(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,  4.38,  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.21,  0.44,  0.36 , 0.72,  4.28, -0.40,  1.76,  1.57,  
    2.85, -1.71,  1.36,  1.61, 3.96, -0.11 , 0.35,  3.83, -0.66,  0.13,  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))
Progman
  • 16,827
  • 6
  • 33
  • 48
Deb
  • 77
  • 6

0 Answers0