Hello There!

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

 About Manisha Sharma

Manisha Sharma is a Customer Success Manager at Tatvic Analytics. She is very passionate about her work and enthusiastic about learning new technologies. She has a keen interest in animation and love to draw anime sketches.

Forecasting the number of visitors on your website using R. Part II

Visitors in GA

Visitors in GA

This blog is the second post of a series of three blogs. In the  Previous Blog I had introduce the reader to the importance of forecasting “views” of an e-Commerce website. The process of forecasting can be implemented using the time-series approach and decomposing the “views” signal into four components namely seasonal, trend, cyclic and irregularity.

The decoupling can be achieved through exponential smoothing using the HoltWinters function of the forecast package of R.  After smoothing up the data the predict function can be fired to forecast the number of visits for the coming days. The output of the parameters (alpha, beta and gamma) will tell us the amount of weight that HoltWinters have put to recent and past data while minimizing the mean square error.

Implementing the time-series exponential smoothing in R:

I have used the HoltWinters (also a function in the forecasting package of R ) model to implement the exponential smoothing on the visitors data. You can get the resources for getting started with R here. This model will take care of the Seasonality, Trend, Cycling and Irregularity components of the time-series by adjusting the three smoothing parameters namely alpha, beta and gamma.

HoltWinters(x, alpha = NULL, beta = NULL, gamma = NULL)

Here, x is the time-series object which usually is a vector of the independent variable. (Number of visits for our example.)

Alpha is the smoothing parameter used for the estimation of the current underlying level. Value of alpha closer to 1 indicates that more weight is on the recent value of the independent variable and less on the previous data. And a value closer to zero is a forerunner that the model has put more weight on the past data for exponential smoothing.

Beta is for the estimate of the slope of the trend component at the current time point. This parameter of Holt-Winters filter specifies how to smooth the trend component. Like alpha it also runs from 0 to 1 where 0 means less weight on recent data and 1 means more weight is assigned to the new data.

The parameter gamma is used for the seasonality component which also runs from 0 to 1.

Setting the values of alpha= FALSE, beta=FALSE, gamma=FALSE will tell R that these components should be ignored while performing the smoothing. And setting them NULL will tell R to automatically select the appropriate values of these parameters by minimizing the mean square prediction error from one-stepped forecast.

Visits.csv is a Comma Separated Values file with the dimension “date” and the corresponding metric “#of visits” on that particular date. This csv file has been extracted from Google Analytics, through a package developed at Tatvic. You can download and get started with it here. The data is for two years, starting from Oct 2011 to Oct 2013. It has 731 data points, the following are the first four rows of the file “Visits.csv”.

Index

Date

Visits

1

2011/10/01

2328

2

2011/10/02

2203

3

2011/10/03

2049

4

2011/10/04

2294

library(forecast)
# It loads the forecasting package to R’s environment.
date_visits

Understanding the output:

The above graph is the ggplot2 version of the plot function mentioned in the code described above.

Smoothing Parameter

Value

Inference

Alpha

0.66

Estimates are based upon recent as well as past data.

Beta

0

No trend is observed and so the value of beta is not updated.

Gamma

0.81

The high value indicates that the seasonal component is based upon very recent observations.

The value of alpha (0.66) is relatively low, indicating that the estimate of the level at the current time point is based upon both recent observations and some observations in the more distant past. The value of beta is 0.00, indicating that the estimate of the slope b of the trend component is not updated over the time series, and instead is set equal to its initial value. In contrast, the value of gamma (0.81) is high, indicating that the estimate of the seasonal component t the current time point is just based upon very recent observations.

Let me recapitulate the gist of the post. I have used the HoltWinters function to implement the exponential smoothing for forecasting number of visits to a particular website. After smoothing up the data I have used the predict function to forecast the number of visits for the next 10 days. The output of the parameters (alpha, beta and gamma) will tell us the amount of weight that HoltWinters have put to recent and past data while minimizing the mean square error.

The next post in line is about detecting anomaly in the number of visits, using the concepts of upper and lower limit of the HoltWinters function, from the forecasting package of R.

 

Forecasting the number of visitors on your website using R. Part I

visitors

visitors

 

 

 

 

 

 

 

 

 

 

This blog post is the first in the series of three blogs. The current blog will introduce the reader to the importance of forecasting “views” of an e-Commerce website. The process of forecasting can be implemented using the time-series approach and decomposing the “views” signal into four components namely seasonal, trend, cyclic and irregularity. This decoupling can be achieved through exponential smoothing using the HoltWinters function of the forecast package of R.

Businesses around the globe have been forecasting their sales for a long time. The primary reason for it is planning, for instance, amount of inventory to store or when to allot the budget on marketing etc. Now a day every e-Commerce business, big or small, has their online presence. This gives them an opportunity to accumulate data about  their consumer’s behaviour, demographics, sources, no of new visits, etc, which can be indirectly used to predict sales. A precursor to sales can also be found by calculating some correlation between the number of visitors and sales. (If this is not the case then one should first rethink about the efficacy of the website in generating revenue) Since many consumers thoroughly research the products and services online before they buy, the web analytics forecasted no of visitors can quickly alert you on any new trend, than what the sales data can.

ABC of forecasting:                                                                     
Forecasting is the process of estimating a future event based on recent and past time series data. It may not reduce the uncertainty of future; however, it gives the decision makers an idea and a basic premise for planning. Short term forecast will always be more accurate than Long term forecast.

We will use a Time-Series Model for our forecasting purpose. People may visit a particular website for many different reasons which are next to impossible for us to fathom all the underlying factors. So,we presume to know nothing about the causality that affects the variable we are trying to forecast. Instead, we examine the past behaviour of a time series in order to infer something about its future behaviour. Time-series models are particularly useful when little is known about the underlying process one is trying to forecast.

A variety of factors influence the time series data. We shall use decomposition analysis to identify certain patterns that appear concurrently in the time series. There are four components in the decomposition analysis that we’ll methodically dissect namely Seasonality, Trend, Cycling and Irregularity.

For an arbitrary observed data running from 1955 to 1995, we’ll decompose it into the desired components mentioned above:

  • Seasonality: When a repetitive pattern is observed over a period of time, such series is known to have a seasonal component. Trend: It is growth or decay that is the tendencies for data to increase or decrease fairly steadily over time. A time series may be stationary or exhibit trend over time.

  • Irregularity: This component of the time series is unexplainable; therefore it is unpredictable.
  • Cyclic:  An upturn or downturn not tied to seasonal variation. Usually results from changes in economic conditions.
Inherent in the collection of data taken over time is some form of random variation. There exist methods for reducing of cancelling the effect due to random variation. A widely used technique is “smoothing“. This technique, when properly applied, reveals more clearly the underlying trend, seasonal and cyclic components.Smoothing techniques are used to reduce irregularities in time series data. Smoothing techniques, such as the Moving Average, Weighted Moving Average, and Exponential Smoothing, are well suited for one-period-ahead forecasting.

 

Exponential smoothing is a very popular scheme to produce a smoothed time series. It assigns exponentially decreasing weights as the observation gets older. In other words, recent observations are given relatively more weight in forecasting than the older observations.

I have implemented the exponential smoothing method for forecasting number of visitors to a website using Holt Winters model in the next blog.

 

Bot Icon
Bot Icon

Tatvic Bot

Explore About Tatvic and Services