Put add_regressor inside mutate context

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?

did you test this fragment ? it shows me exactly the same error as your subsquent nest/mutate/map related error.

No I did not test it, that is the fragment I would ideally include inside a mutate context.

Before tackling the more complex case, do you want to take some time to tackle the single case and update this post with a working version, or are you looking for support on the forum to get that part before the iteration challenge ?

The solution to the error was simple, I just had to change the names of Date and Value into ds and y:

> library(prophet)
> 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(ds = Date, Group, y = Value, Expln)
> 
> df
           ds Group         y    Expln
1  2010-01-01     A 44.907724 18.21944
2  2011-01-01     A  5.110549 88.22997
3  2012-01-01     A 41.627854 81.31806
4  2013-01-01     A 87.261591 31.91806
5  2014-01-01     A 82.931747 89.88972
6  2010-01-01     B 27.985192 53.98613
7  2011-01-01     B 82.574708 35.00410
8  2012-01-01     B 21.125819 56.96952
9  2013-01-01     B 12.400783 44.10640
10 2014-01-01     B 50.554773 49.89784
11 2010-01-01     C  2.086168 72.10425
12 2011-01-01     C 54.932624 58.57923
13 2012-01-01     C 52.649403 50.39760
14 2013-01-01     C 91.927291 56.70027
15 2014-01-01     C 25.771383 80.64624

> dfx = df[df$Group == "A",]
> dfx = dfx[c(1, 3, 4)]
> dfx
          ds         y    Expln
1 2010-01-01 44.907724 18.21944
2 2011-01-01  5.110549 88.22997
3 2012-01-01 41.627854 81.31806
4 2013-01-01 87.261591 31.91806
5 2014-01-01 82.931747 89.88972

> m <- prophet()
> m <- add_regressor(m = m, name = 'Expln')
> m <- fit.prophet(m = m, df = df)
Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
n.changepoints greater than number of observations. Using 11
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.