Time Series forecasting with Prophet by Facebook

If you like this, Share!

Forecasting has been hard and novice users especially have a hard time dealing with this. Couple of reasons for this is that things like stock prices or bullion prices are affected by many different factors. However it can be relatively easy for forecasting things like GDP. This is because they don’t change drastically overnight.

There are many packages on R that help in forecasting. But the generic models that are built by novice users is not that robust. This is not the fault of the available packages but the use of it by inexperienced users. Choosing the right ARIMA model or using state space models will help, but the usability is the main issue.

This is where Prophet by Facebook comes into the picture. It fits additive regression models instead of the traditional ARIMA models. The main features of Prophet are –

  • A reasonable number of missing observations.
  • Handles a large number of outliers.
  • Historical trend changes.
This just awesome right?

So lets start off –

You can install Prophet in R by


Now that prophet is installed


We need to get some data and read it into a data frame. Here I am using the the log number of views to Petyon Manning’s Wikipedia page, which can be downloaded from here.

Now import the csv into the R environment and store in a data frame.

>df <- example_wp_peyton_manning

Lets look at the head of the data frame.

> head(df)
# A tibble: 6 x 2
 ds y
 <date> <int>
1 2007-12-10 14629
2 2007-12-11 5012
3 2007-12-12 3582
4 2007-12-13 3205
5 2007-12-14 2680
6 2007-12-15 2401

Plotting the data results in


The seasonality nor the trend is quite visible. Lets log the time series and plot it.

>df1 <- df
>df1$y <- log(df$y)

Now that is much better. Lets fit the model on the data.

>m <- prophet(df1)

Predictions can now be made on a data frame containing the dates for the forecast.

> future <- make_future_dataframe(m , periods = 365)
> tail(future)
3265 2017-01-14
3266 2017-01-15
3267 2017-01-16
3268 2017-01-17
3269 2017-01-18
3270 2017-01-19
> head(future)
1 2007-12-10
2 2007-12-11
3 2007-12-12
4 2007-12-13
5 2007-12-14
6 2007-12-15

Lets predict.

> pred <- predict(m, future)
|=============================================|100% ~0 s remaining 
> tail(pred$yhat)
[1] 7.823991 8.205770 8.530798 8.318204 8.150827 8.162741

Plotting the forecast.

> plot(m, pred)

This is what the creators of Prophet had to say –

“We have found Prophet’s default settings to produce forecasts that are often accurate as those produced by skilled forecasters, with much less effort.”

“With Prophet, you are not stuck with the results of a completely automatic procedure if the forecast is not satisfactory — an analyst with no training in time series methods can improve or tweak forecasts using a variety of easily-interpretable parameters.”

So go on and experiment with it to get more accurate results.

Leave a Reply

Be the First to Comment!

Notify of