I'm working on a multivariate (100+ variables) multi-step (t1 to t30) forecasting problem where the time series frequency is every 1 minute. The problem requires to forecast one of the 100+ variables as target. I'm interested to know if it's possible to do it using FB Prophet's Python API. I was able to do it in a univariate fashion using only the target variable and the datetime variable. Any help and direction is appreciated. Please let me know if any further input or clarity is needed on the question.
5 Answers
You can add additional variables in Prophet using the add_regressor method.
For example if we want to predict variable y using also the values of the additional variables add1 and add2.
Let's first create a sample df:
import pandas as pd
df = pd.DataFrame(pd.date_range(start="2019-09-01", end="2019-09-30", freq='D', name='ds'))
df["y"] = range(1,31)
df["add1"] = range(101,131)
df["add2"] = range(201,231)
df.head()
ds y add1 add2
0 2019-09-01 1 101 201
1 2019-09-02 2 102 202
2 2019-09-03 3 103 203
3 2019-09-04 4 104 204
4 2019-09-05 5 105 205
and split train and test:
df_train = df.loc[df["ds"]<"2019-09-21"]
df_test = df.loc[df["ds"]>="2019-09-21"]
Before training the forecaster, we can add regressors that use the additional variables. Here the argument of add_regressor is the column name of the additional variable in the training df.
from fbprophet import Prophet
m = Prophet()
m.add_regressor('add1')
m.add_regressor('add2')
m.fit(df_train)
The predict method will then use the additional variables to forecast:
forecast = m.predict(df_test.drop(columns="y"))
Note that the additional variables should have values for your future (test) data. If you don't have them, you could start by predicting add1 and add2 with univariate timeseries, and then predict y with add_regressor and the predicted add1 and add2 as future values of the additional variables.
From the documentation I understand that the forecast of y for t+1 will only use the values of add1 and add2 at t+1, and not their values at t, t-1, ..., t-n as it does with y. If that is important for you, you could create new additional variables with the lags.
See also this notebook, with an example of using weather factors as extra regressors in a forecast of bicycle usage.
To do forecasting for more than one dependent variable you need to implement that time series using Vector Auto Regression.
In VAR model, each variable is a linear function of the past values of itself and the past values of all the other variables.
for more information on VAR go to https://www.analyticsvidhya.com/blog/2018/09/multivariate-time-series-guide-forecasting-modeling-python-codes/
I am confused, it seems like there is no agreement if Prophet works in multivariate way, see the github issues here and here. Judging by some comments, queise's answer and a nice youtube tutorial you can somehow make a work around to multivariate functionality, see the video here: https://www.youtube.com/watch?v=XZhPO043lqU
You can do this with one line using the timemachines package that wraps prophet in a functional form. See prophet skaters to be precise. Here's an example of use:
from timemachines.skatertools.data import hospital_with_exog
from timemachines.skatertools.visualization.priorplot import prior_plot
import matplotlib.pyplot as plt
k = 11
y, a = hospital_with_exog(k=k, n=450, offset=True)
f = fbprophet_exogenous
err2 = prior_plot(f=f, k=k, y=y, n=450, n_plot=50)
print(err2)
plt.show()
Note that you can set k to be whatever you want. That is the number of steps ahead to use. Now be careful, because when prophet says multivariate they are really referring to variables known in advance (the a argument). It doesn't really address multivariate prediction. But you can use the facebook skater called _recursive to use prophet to predict the exogenous variables before it predicts the one you really care about.
Having said all that, I strongly advise you to read this critique of prophet and also check its position on the Elo ratings before using it in anger.