Prophet function in r Part - 2

      Forecasting Air Passengers data with Prophet model using R programming.

Hi guys ,

After the discussion of Introduction to Data Forecasting here the practical example on Data forecasting with Prophet, if you miss it first refer Part-1 for clear understanding the example
Prophet is a procedure 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. It works best with time series that have strong seasonal effects and several seasons of historical data.

Why Prophet is best ?

Prophet is developed by Facebook Data science Team . It is automated to forecast the data by seasons , Weekly and Day by Day data . Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.

Let’s go to practical , here I am using Air Passengers data set. We can get many data sets from Rstudio Data sets.

Step 1:
Here I am loading  the data to Rstudio from csv file,
Check whether any null values are present in the data and replace with ‘NA’

#Reading Data
source_Data <-read. table ("passengers.csv", header =TRUE , sep=",", strip.white = TRUE,na.strings = "NA");

check the data type of Date column if it has  any other data type then  convert into Date ,here I am converting my date column from factor to date

#Factor  To Date Conversion
factor_Date_conversion<- as.Date(factor(Source_Data$ds), '%Y-%m-%d')

Step 2:
Rename your date columns as ‘ds’ which indicate date stamp and independent variable as ‘y’ which indicates number of passengers traveled

#Rename columns
>> Colnames(source_Data)< - c(“ds”, ”y”)

Step 3:
Install and import required packages , here I am importing prophet for forecasting and plotly for plotting graphs

#Library's used
>> library('prophet')
>> library('plotly')

Step 4:
For our code example we will transform the data and use the log of entrances.For transformation of time series in case of seasonal and random fluctuations  and also this will help us make sense of the prediction visualizations.

>> source_Data$y <- log(source_Data$y)

Step 5:
Let’s explore our data by plotting graph to check Trend in data

#Plot Source Data
>> Plot(Source_Data)















Step 6:
#Fitting Model
>>Fit_Model<-prophet(Dataframe_source)

We can also add the holidays list, weekly seasonality to our model depends on requirement and it’s optional

>> off_days <- data.frame(ds = as.Date(c("2010-12-24”, ...)))

>> Fit_Model<-prophet(Dataframe_source,
yearly.seasonality = TRUE,
weekly.seasonlity=TRUE,
holidays=off_days )

Step 7:
Now our model is ready to predict the data ,make a data frame for future data. Here I am predicting for 10 months .

>> Future_DataFrame <- make_future_dataframe(Fit_Model, periods =10, freq = 'm')
Frequency =yearly , Quarterly, monthly , weekly etc.

>> Future_DataFrame


Step 8:
Predict data using Predict function

#Predicate Future Data
>> Predicate_Future <- predict(Fit_Model, Future_DataFrame)
>> Predicate_Future

Here yhat Values are the predicted values




Step 9:
Plot the graph for the Predicted values

#Plot of Predicted Data
>> plot(Fit_Model,Predicate_Future)
>> dyplot.prophet(Fit_Model,Predicate_Future,uncertainty = TRUE)

You can also see the Weekly and Monthly trend for the predicted data

>>Prophet_plot_Components <- (Fit_model,Predicate_future)




Now , Let's see Same example with ARIMA Model in Part-3
     
                                                           Thank you 

Comments

  1. Awesome Man...Really gave me an amazing insight On Data Forecasting...cant wait for your next one...

    ReplyDelete
  2. Good work.Nice Info. Please provide blogs regarding other forecasting models also.

    ReplyDelete
  3. Good Work Man....Keep it up 👍
    Waiting for the next one :)

    ReplyDelete

Post a Comment

Popular posts from this blog

What is linear regression ?

Work flow to build a Machine Learning Algorithm