Time Series Analysis using Facebook Prophet in R Programming

ts()

Mathematical Equation of Prophet Model

where, y(t) refers to the forecast g(t) refers to the trend s(t) refers to the seasonality h(t) refers to the holidays for the forecast e(t) refers to the error term while forecasting

Some Important Terms Used in Facebook Prophet Model

  • Trend A trend is a shift in development either in increment or decrement direction. Mathematically,
    where, C indicates the carry capacity k indicates the growth m indicates the offset parameter
  • Seasonality Seasonality is a feature of time series object that occurs at a particular time/season and changes the trend.
  • Holidays Holidays are a time period that changes a lot to the business. It can make a profit or loss depending upon the business.

Implementation in R

Step 1:
# Install the library
install.packages("prophet")

                    
Step 2:
# Load the library
library(prophet)

                    
Step 3:
here
# Dataset
ap <- read.csv("example_air_passengers.csv")

                    
Step 4:
m <- prophet(ap)

                    
Step 5:
# Predictions
future <- make_future_dataframe(m,
                    periods = 365)
  
# Print predictions
cat("\nPredictions:\n")
tail(future)

                    
Output:
Predictions:
        ds
504 1961-11-26
505 1961-11-27
506 1961-11-28
507 1961-11-29
508 1961-11-30
509 1961-12-01
Step 6:
# Forecast
forecast <- predict(m, future)
tail(forecast[c('ds', 'yhat'
    'yhat_lower', 'yhat_upper')])

                    
      ds         yhat    yhat_lower yhat_upper
504 1961-11-26 497.2056   468.6301   525.7918
505 1961-11-27 496.0703   467.8579   525.6728
506 1961-11-28 493.1698   465.5788   522.9650
507 1961-11-29 496.0497   469.2889   524.6313
508 1961-11-30 492.8452   463.7279   520.4519
509 1961-12-01 493.6417   466.3496   522.9887
Step 7:
# Output to be present
# As PNG file
png(file = "facebookprophetGFG.png")
  
# Plot
plot(m, forecast)
  
# Saving the file
dev.off()

                    
Output:
Black dots
Dark blue line
Light blue area
Step 8:
# Output to be present 
# As PNG file
png(file = "facebookprophettrendGFG.png")
  
# Plot
prophet_plot_components(m, forecast)
  
# Saving the file
dev.off()

                    
Output:


Contact Us