Say I have a data frame which I generated like this:
library(prophet)
library(dplyr)
library(purrr)
library(tidyr)
Date <- rep(seq(as.Date("2010-01-01"), as.Date("2014-01-01"), "years"), 3)
Group <- rep(LETTERS[1:3], each = 5)
Value <- runif(15, 0, 100)
Expln <- runif(15, 0, 100)
df <- data.frame(Group, Date, Value, Expln)
df
Group Date Value Expln
1 A 2010-01-01 26.452296 12.523203
2 A 2011-01-01 73.942038 7.205811
3 A 2012-01-01 90.154247 69.233391
4 A 2013-01-01 3.324741 70.151808
5 A 2014-01-01 13.971040 69.852875
6 B 2010-01-01 17.227020 82.362905
7 B 2011-01-01 71.766615 51.208684
8 B 2012-01-01 45.633992 6.352804
9 B 2013-01-01 80.455529 31.771419
10 B 2014-01-01 98.697114 85.065733
11 C 2010-01-01 19.515260 21.620745
12 C 2011-01-01 73.782559 16.365095
13 C 2012-01-01 35.919595 93.249789
14 C 2013-01-01 81.888110 85.566056
15 C 2014-01-01 22.216348 31.731399
What I really want is to model the variable Value
while using Expln
as an explanatory variable through the function add_regressor()
within the context of mutate
, like this:
m <- prophet()
add_regressor(m = m, name = 'Expln')
m <- fit.prophet(m = m, df = df)
however, when I run a simple model it gives me an error:
> mod <- df %>%
+ nest(-Group) %>%
+ mutate(m = map(data, prophet, growth = 'logistic'))
Error in `mutate()`:
! Problem while computing `m = map(data, prophet, growth = "logistic")`.
Caused by error in `fit.prophet()`:
! Dataframe must have columns 'ds' and 'y' with the dates and values respectively.
Run `rlang::last_error()` to see where the error occurred.
Warning message:
All elements of `...` must be named.
Did you want `data = -Group`?
Does anybody know what is causing this error? How can I put add_regressor
inside mutate
?