Is it possible to do multivariate multi-step forecasting using FB Prophet?
Asked Answered
L

9

37

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.

Lowestoft answered 5/2, 2019 at 22:54 Comment(2)
The answer to the original question is yes! Here is a link to specific Neural prophet documentation with several examples of how to use multivariate inputs. For neuralprophet, these are referred to as 'lagged regressors'. neuralprophet.com/html/lagged_covariates_energy_ercot.htmlRoid
You can also use 'future regressors' neuralprophet.com/html/future-regressors.htmlRoid
S
58

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.

Solange answered 9/11, 2019 at 16:29 Comment(5)
Thank you. This is really helpful. I have a question on this though. I followed your instructions. And used the Train_Test_Split to create Train Test data and noticed that it had taken random data from all over the data including the last row. I was intending to predict the latter half of the dataset..... Would this be a good way to do a forecast. Or should I split the data by indices..... @SolangePsychotherapy
I am confused about your comment about Prophet not using past values of the regressors. From the documentation: 'the extra regressor must be known for both the history and for future dates. ' Although I can't find any specification / clarification anywherePhyllome
@aze45sq6d: my understanding is that the past values of the regressors are indeed needed, but only used during the training. For prediction it just uses the value at t+1, not the past values as univariate time series do.Solange
Interesting, following your post I actually tested it out by adding lagged values of the variables to the model as regressors. However, due to the way to Prophet model seems to be set up, the lagged values of the regressors are therefore included in the training and test set and included in the fitting step. If there's a way to split up the data used for training and the data used for prediction (and dealing with the changes in dimension since you'd add a lot of extra features then Id be very interested to know! But assuming that there's no way to do that (1)Phyllome
I tested it with a lag period up until 8 (tested with 1 lags up to 8 lags and the error of my prediction went up as the amount of lags increased. So theres two options I guess - Im making an implementation mistake and the Prophet algo is confused (including the lags of regressors in the fitting step instead of splitting the fitting from the prediction) - it already uses the past values of the regressors in the forecasting step and the prophet algo is confusedPhyllome
H
4

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/

Holeproof answered 8/2, 2019 at 5:30 Comment(0)
S
4

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

Shanon answered 30/9, 2020 at 14:19 Comment(1)
the target time series is univariate, but you can you can use multiple regressorsAssemble
L
0

This might be late, however if you are reading this in 2019, you can implement multivariate time series using LSTM, Keras.

Lindi answered 7/9, 2019 at 8:34 Comment(0)
C
0

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.

Carol answered 4/5, 2021 at 1:25 Comment(0)
R
0

The answer to the original question is yes!

Here is a link to specific Neural prophet documentation with several examples of how to use multivariate inputs. For neuralprophet, these are referred to as 'lagged regressors'.

https://neuralprophet.com/html/lagged_covariates_energy_ercot.html

Roid answered 29/1, 2022 at 14:41 Comment(0)
A
0

yes indeed we can now apply multivariate time series forecasting, here is the solution. https://medium.com/@bobrupakroy/yes-our-favorite-fbprophet-is-back-with-multivariate-forecasting-785fbe412731 enter image description here

Adamo answered 18/7, 2022 at 2:47 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Masticate
H
0

VAR is a pure econometric model, but after reading a lot of literature on forecasting , i see that VAR also suffers from not able to capture the trend. So then we are not having a good forecast, but still VAR is a workhorse model for multivariate analysis. I think prophet is not for a multivariate, rather use the ML models like RF,XGBOOST, NNET ... but keep in mind that if you want to capture the trend then be sure which model is better. Else go for deep learning

Hightail answered 6/9, 2022 at 16:20 Comment(0)
I
0

I like using Darts for MV forecasting. It seems a little bit more advanced and provides multiple forecasting options: https://pypi.org/project/darts/. Haven't had the opportunity to use FB Prophet or Neural Prophet. I'll give it a try.

Intromission answered 17/1, 2024 at 21:10 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewWinson
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Masticate

© 2022 - 2025 — McMap. All rights reserved.