Forecasting Bitcoin Prices using Prophet in R

Justin Eloriaga
Analytics Vidhya
Published in
3 min readJun 17, 2020
Dynamic Forecast using Prophet

Forecasting can be quite daunting, especially given the knowledge required to be able to identify forecasting strategies which may involve heavy concepts with a rigorous mathematical underpinning. Fortunately, the bright devs over at Facebook developed a simple forecasting library called Prophet which makes even a forecasting newbie look like a pro. I’d like to preface this by saying that the Prophet model is not perfect, nor can it be considered best in most cases. It is usually used for models with a fair bit of seasonality, trends, and drifts. Such is the case with business sales data in which higher values may be seen during the holiday quarter or on weekends as compared to regular quarters or weekdays.

To start, we need to install the prophet library in R. This involves using the install.packages() command.

install.packages('prophet')

We then call this package for use using the library() command.

library(prophet)
library(tidyverse)

For this example, we will use the Daily Bitcoin Adjusted Close values which can be obtained in Yahoo Finance. A link to the dataset can be found here which is located in the folder called Forecasting with Prophet

https://drive.google.com/drive/folders/11mAXh0trxjuf1y_yh-AJXgxulHQwcBEn

bitcoin <- read.csv(file.choose())
head(bitcoin)
tail(bitcoin)

We use the read.csv() command to import the dataset. The file.choose() command opens up the dialogue box for you to be able to select the dataset. We created an object named bitcoin to store the dataset. The head() command brings up the first few rows while the tail() command shows the last few row. Note that the ds column contains dates and the y column contains the Bitcoin closing price. By convention, prophet reads the ds column as a date and the y column as the series. As such, you may need to rename columns to fit with this convention. Moreover, you may need to set the date to YYYY-mm-dd format as this is the format recognized by prophet.

Model1 <- prophet(bitcoin)
Future1 <- make_future_dataframe(Model1, periods = 31)

We will now build the model. We use the prophet() command to create a dynamic forecasting model. We will store this model as an object named Model1. After which, we will add 31 periods to the sample using the make_future_dataframe() command. This will be filled up with future forecasted values in the next step.

Forecast1 <- predict(Model1, Future1)
tail(Forecast1[c('ds','yhat','yhat_lower','yhat_upper')])

We will predict the forecasted values using predict() and create an object Forecast1 to store these. The tail command will list the last few forecasted observations which contain the details on the date (ds), the forecasted value (that), and the confidence interval of the estimate.

dyplot.prophet(Model1, Forecast1)

We use the dyplot.prophet() command to graph out the forecast which yields us the figure at the start of the article. We can also adjust the span by using the slider in the bottom of the plot area.

Forecasted Values

As we can see, there seems to be an upward trend to Bitcoin prices for the next month. You may view the specific values by hovering around the area or by printing the values in R. We can also see certain seasonality or day of the week effects by using the prophet_plot_components() command.

prophet_plot_components(Model1, Forecast1)
Decomposing the Forecast Series

I hope you were able to see how easy it is to forecast using the prophet function and is ideal for generating quick reports wherein a simply analysis is needed. For a more hands on approach, I made a video detailing all commands which is seen below.

--

--