prophet: Getting regression coefficients for additional regressors

Hi, How can I get the regression coefficients for additional regressors added in prophet. I am using the below code, using two regressors. If I look at beta values in params, it has 28 values. Is it possible to get regression coefficients from them?

    m = Prophet(changepoint_prior_scale=cp, 
            weekly_seasonality=False, daily_seasonality=False)
    m.add_seasonality(name='monthly', period=30, fourier_order=3)
    m.add_regressor('Price per Unit')
    m.add_regressor('Promo_present')

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 20 (6 by maintainers)

Most upvoted comments

Something to note: As of #1572, there is now in Python a utility for correctly extracting the regression coefficients of a model. This utility has shipped with v0.7.1 now in PyPI: https://github.com/facebook/prophet/blob/77da5b8c06d05d478af510198c685d454342db1f/python/fbprophet/utilities.py#L28

It is used like:

from fbprophet.utilities import regressor_coefficients

regressor_df = regressor_coefficients(m)

It will properly account for all of the standardization happening under the hood.

@carrasq for your specific questions:

  • Yes the normalizations are reversible. You can see the code in the utility above for how it is done, but basically the scaling on y is stored in m.y_scale and the standardization (mean and std) for each regressor is stored in the m.extra_regressors dict.
  • Scaling y is to allow having reasonable priors in the model. The magnitudes of all of the priors (noise, trend changes, seasonality) are scale dependent, so normalizing the data makes it so default values can be reasonable across a larger set of time series.

@AnupaS that is right, there are 26 coefficients for seasonality and 2 for the extra regressor. To see which coefficients correspond to which components, you can look at m.train_component_cols. This is a dataframe with 28 rows (one for each of the coefficients), and each column is a component (and is named for that component). The value in the dataframe is 1 if that beta is involved in that component, and 0 otherwise. For example, weekly seasonality uses 6 coefficients, and so you will see that the ‘weekly’ column has 6 1s. The extra regressors will use 1 each, and so you will find for their columns the one row that has a 1, and that is the index of the beta you want.

An important thing to note when looking at the regression coefficients is that Prophet does a lot of data normalization prior to model fitting. The y data that is giving in fit is scaled by its max value. Extra regressor columns are also by default standardized (that is, subtract mean, divide by sd) if non-binary. So the coefficient value that you see will be one that is applied to the standardized values of the extra regressors, and then added to the scaled trend forecast. The raw values of the coefficients thus may not be that useful to you.

Hi,

Just want to clarify and make sure I completely understand this. The code is written in R language.

I have included 6 regressors in the model.

m = prophet() %>% add_regressor(‘no_hours_open’) %>% add_regressor(‘easter_fri_indc’) %>% add_regressor(‘easter_sat_indc’) %>% add_regressor(‘easter_sun_indc’) %>% add_regressor(‘easter_mon_indc’) %>% add_regressor(‘anzac_day_indc’) %>% fit.prophet(dat)

The model returns 12 coefficients.

m$params$beta [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 0.03722567 0.0259044 0.01105772 -0.0102999 0.007605983 0.0007221441 0.09742383 -0.2358922 0.04465266 0.155911 0.1140141 0.1049005

And also > as.matrix(m$train.component.cols) returns a matrix with 12 rows and 10 columns.

To obtain the coefficients, I use the following:

m$params$beta %*% as.matrix(m$train.component.cols) additive_terms anzac_day_indc easter_fri_indc easter_mon_indc easter_sat_indc easter_sun_indc extra_regressors_additive no_hours_open [1,] 0.353226 0.1049005 -0.2358922 0.1140141 0.04465266 0.155911 0.2810099 0.09742383 weekly multiplicative_terms [1,] 0.07221602 0

Am I on the right track?

@AmbikaSowmyan can you check what version of fbprophet you’re using? It will be there for 0.7 and later.

import fbprophet
print(fbprophet.__version__)

I’m guessing it’s old, in which case you should upgrade (and note that for the latest version the package name has changed to prophet)