6

There is an R package called prophet which is very good. It is a generalized additive model. The dependent variable is the the metric you are trying to solve and the independent variables are: the growth function, seasonality function, and a variable that will account for things not found in those two variables. I want to be able to add another independent variable. For example:

Let's say I want to solve for Page Views. I have the past nine years worth of data and in this package it will take the seasonality and growth rate into account to solve for this. How would I include another independent variable such as "Temperature"?

This is what the equation looks like behind the scenes:

Page_Views = g(t) + s(t) + e(t)

I want to add another variable:

Page_Views = g(t) + s(t) + Beta(Temperature) + e(t)

How would I do this in the prophet package?

Here is a tutorial on how to use the package: https://cran.r-project.org/web/packages/prophet/vignettes/quick_start.html

Data is found here: https://github.com/facebookincubator/prophet/blob/master/examples/example_wp_peyton_manning.csv

library(prophet)
m<-prophet(df)
future <- make_future_dataframe(m, period = 365)
forecast <- prophet:::predict.prophet(m, future)
plot(m, forecast)

The main question I want to know is: "Is there a way to add an additional independent variable to my generalized additive model in the prophet package?

Thanks, any help would be great!

Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
nak5120
  • 4,089
  • 4
  • 35
  • 94
  • I'm not seeing a variable named 'Temperature' in the data that you linked to. – IRTFM Jun 25 '17 at 14:57
  • There isn't one. I'm asking if there was a variable called temperature, would I be able to include it in this package as another independent variable? – nak5120 Jun 25 '17 at 15:42
  • After reviewing help pages for what appear to be the main functions in that package, it appears to be a fairly narrow package and only documents the option to switch between linear and logistic links in a Stan model. The model specification appears to be otherwise fixed. If this is a prototype or demonstration model for which further development is intended, then perhaps the package maintainer will know how to extend it. – IRTFM Jun 25 '17 at 16:04
  • Ok thanks for looking into this. Do you have a recommendation for another R package that can do something like this? – nak5120 Jun 25 '17 at 16:06
  • Two thoughts occurred to me. One was the `forecast` package and the other was to get the source-code for prophet and see if it can be re-implemented in pkg:`rstanarm`. You can get the underlying model specification with `prophet::fit.prophet` – IRTFM Jun 25 '17 at 16:16
  • Thanks, I'll look into this. In the meantime, it's definitely not the best route probably but I created an ARIMA model and added the argument xreg() to include independent regressors. – nak5120 Jun 26 '17 at 20:55
  • You might want to provide some code for the next "time-traveler". – IRTFM Jun 26 '17 at 21:28
  • The links do not work. – G. Grothendieck Jan 05 '20 at 15:03

1 Answers1

7

Currently library developers have added an add_regressor function, which models external regressors in the linear part of the model. See the documentation.

knst4444
  • 88
  • 1
  • 4
  • It seems to only accept boolean-type variables, though, not numerical ones like "Temperature" would be – Laurent Dec 13 '17 at 08:49
  • 2
    @Laurent No, you can add any type of feature. See [here](https://github.com/facebook/prophet/issues/311) – knst4444 Dec 14 '17 at 12:01
  • 1
    You can add an extra regressor, but the real problem is adding a continuous variable because for that you need to have future values for the extra regressors also in order to predict future values. This beats the purpose of adding extra regressors. – Rahib Apr 23 '19 at 05:16