Hello,
I'm very sorry if this is a duplicate question. I've tried to browse the existing threads but I haven't seen any 2019 updates on the status of prophet's multivariate functionality. Currently I am using it to forecast customer counts by training with univariate date/value data. I would like to add a few other features for which I have historical as well as forecasted values. Does the add_regressor feature allow one to incorporate information from multiple other values (such as investment/sales/promotion types) ? If yes, is there any source that shows an example of how to load multivariate data into prophet? If not, how are plans to implement this going? Thank you for your help!
If you have other time series with historical and forecasted values that you want to incorporate into the forecast, then yes that sounds like a good fit for add_regressor.
Basically what that will do is include it as a linear regressor into the model. So for extra regressor z(t), the model will be something like
y(t) = trend(t) + seasonality(t) + beta*z(t)
where the parameter beta will be fit to the data.
The example in the documentation shows adding a binary extra regressor, but that can just as easily be a time series. For a real example, this notebook is a really cool, it adds weather regressors to a forecast of bike share usage: https://nbviewer.jupyter.org/github/nicolasfauchereau/Auckland_Cycling/blob/master/notebooks/Auckland_cycling_and_weather.ipynb
(We have an open issue about multivariate forecasting but that is for the slightly different problem where you are trying to jointly forecast a bunch of time series, as opposed to already having the forecast for one and using it for another. If you're trying to jointly forecast things, then we can discuss that).
Thank you for the lightning fast reply Ben!! This is actually _exactly_ what I was looking for! My question was how to incorporate additional regressors to forecast a single series. I will work through this example, I really can't thank you enough! This was amazingly helpful
Hello fellow prophet wizards, I'm not sure if anyone can help with this question as well, but I need my forecasts at different customer levels (we call them "segment_channels"). Basically we have 9 customer segments of interest so each ds or date row is essentially duplicated 9 times with 9 unique values corresponding to the counts of the different segments for that date. I was able to get a grouped forecast to work using this code:
data = df %>%
group_by(Segment_Channel) %>%
do(predict(prophet(., seasonality.mode = 'multiplicative', holidays = holidays, seasonality.prior.scale = 10, changepoint.prior.scale = .034), make_future_dataframe(prophet(.), periods = 11, freq='month'))) %>%
dplyr::select(ds, Segment_Channel, yhat)
I would like to also be able to add regressors using this "grouped" method, but I haven't figured out a way to make it work. I have been adding regressors using this R code:
m <- prophet(holidays = holidays)
m<- add_regressor(m, name = "L_1_Circ_Inv")
m<- add_regressor(m, name = "recency_total_investment")
m<- add_regressor(m, name = "Flash_Sale_Days")
m <- fit.prophet(m, df[recency == 'React' & channel == 'R'])
forecast <- predict(m, df)
prophet_plot_components(m, forecast)
But I'm wondering if anyone might know of a way to combine the two methods? Basically, would it be possible to add my regressors for each group and generate predictions by group all in one go? Hopefully this question makes sense...not sure if anyone has needed to do this yet. Also happy to clarify if anything is confusing. Thank you so much !
Here is a link to this same question on stack overflow, hopefully it is maybe a little bit more clear there
I'm not sure I understand the issue. You can have multi-line statements inside of do, so you should be able to just plug the second block (minus the plot) inside the first. And just add inside that block a column for Segment_Channel.
Hi Ben,
Thanks again for the ping! I updated my stack overflow post with some code where I think I try what you are suggesting there--but I am definitely doing it wrong. (I included my attempts and the error messages). If you have some time to take a look at what I'm trying out hopefully it will be a little more clear to you?
The model object is stateful, so you can't re-instantiate it inside the do like that every time. The better thing to do is to put it in a function like this:
make_forecast <- function(df) {
m <- prophet(seasonality.mode = 'multiplicative', holidays = holidays, seasonality.prior.scale = 10, changepoint.prior.scale = .034)
m <- add_regressor(m, name = 'flash_sale')
m <- fit.prophet(m, df)
future <- make_future_dataframe(m, periods = 11, freq = 'month')
future$flash_sale = 1 # FIXME this needs to be correctly specified
fcst <- predict(m, future)
return(fcst)
}
fcst = fake_data %>%
group_by(segment_channel) %>%
do(make_forecast(.)) %>%
dplyr::select(ds, segment_channel, yhat)
Ah, yes this is exactly what I was looking for!! Thank you so much Ben! Makes complete sense now. Is it recommended to scale the regressors before fitting? You've been amazingly helpful throughout this process. Do you mind if I throw this solution onto my stackoverflow post and cite you as the wizard?
Sure, go ahead!
Scaling regressors before fitting is usually a good idea, and Prophet will be default standardize regressors (subtract mean, divide by SD) unless they are binary. So that should all be handled for you.
Most helpful comment
If you have other time series with historical and forecasted values that you want to incorporate into the forecast, then yes that sounds like a good fit for
add_regressor.Basically what that will do is include it as a linear regressor into the model. So for extra regressor z(t), the model will be something like
where the parameter
betawill be fit to the data.The example in the documentation shows adding a binary extra regressor, but that can just as easily be a time series. For a real example, this notebook is a really cool, it adds weather regressors to a forecast of bike share usage: https://nbviewer.jupyter.org/github/nicolasfauchereau/Auckland_Cycling/blob/master/notebooks/Auckland_cycling_and_weather.ipynb
(We have an open issue about multivariate forecasting but that is for the slightly different problem where you are trying to jointly forecast a bunch of time series, as opposed to already having the forecast for one and using it for another. If you're trying to jointly forecast things, then we can discuss that).