Time Series Forecasting With Prophet In R
By: Jason Lzp
Tags: Computer Science, Programming, Data Analysis, Data Science, Forecasting

Earlier this week, as I was scrolling through LinkedIn, I came across a post briefly mentioning the “prophet” library which can be used for forecasting time series.

From the CRAN documentation, the library is described as an “Automatic Forecasting Procedure” which can be applied for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. And works best with time series that have strong seasonal effects and several seasons of historical data.

For the full documentation: https://cran.r-project.org/web/packages/prophet/prophet.pdf

Being a curious individual, I decided to give it a go and here are the results:

I’ve decided to use a well-known stock (AAPL) for this example and plan to apply the forecasting technique on it. This is based on a daily frequency of five years of historical data of the stock. I’ve downloaded the CSV file from Yahoo Finance. Let’s take a look.

Exploring the dataset in R

Reading our dataset

A quick view of our dataset

Before proceeding, let’s do a quick check for missing data as part of our pre-processing.

Checking for missing values

Great! 0 missing data and we’re good to proceed. P.S missing values are labelled as NA in R.

One thing I realised while working with this library is that the dates have to be in the YYYY-MM-DD format. It also works with just two columns mainly, a Datestamp and a “y” column which represents the measurement we want to forecast. The variable for measurement must also strictly be a numeric type.

We can begin by loading the following libraries to do some formatting and the prophet library for our forecasting. We would have to rename the columns “Date” to “ds” and would be using the “Close” as our measurement which has to be renamed to “y” for prophet to perform its magic.

Removing unwanted columns and keeping only the Date and Close columns

Renaming the columns of Date and Close as ds and y for the prophet technique to work

Great, now that we have 0 missing values and renamed the columns to be used by the library. We can proceed to run our forecast with the following commands.

As our time-frequency is based on the daily close, we would apply the forecast with a period of 365 days or a year. Followed by the predict function to get our projections.

Lastly, we would create an additional vector with the following columns namely: “yhat”, “yhat_lower” and “yhat_upper”. These variables represent the predicted value, lower predicted and upper predicted values respectively.

The final forecasted output

Breakdown of forecast components such as trend, yearly seasonality, and weekly seasonality with the prophet_plot_ components function in the code above.

Breakdown forecast components

Lastly, if you prefer an interactive plot, you can do so by applying the function below.

An interactive plot of our forecast

There you have it!

I had fun trying out this library for time-series forecasting. Although not the best dataset representing seasonality, it is definitely a package which allows one to breakdown components of a time-series and allows for easy forecasting.

New to programming with R and unsure where to start? check out my eBook with step by step guides!