



Welcome to the second part of the series on predicting user’s revisit to the website. In my earlier blog Logistic Regression with R, I discussed what is logistic regression. In the first part of the series, we applied logistic regression to available data set. The problem statement there was whether a user will return in the next 24 hours or not. The model is built and till now it was showing us 88% accuracy in predicting user’s revisit.
In this post, I’d try to showcase ways to improve this accuracy and take it to the next level. This is more about technical optimization so if you are a business reader you may want to skip and check how can you use this for your benefit. But, if you are techwiz or Data modeling guy like me, let’s get rolling.
As I have discussed in blog Improving Bounce Rate prediction Model for Google Analytics Data, the first step of the model improvement is variable selection and the second step is outlier detection (If you want to know more details of steps, refer mentioned blog). Let’s apply these steps one by one.
I have used stepwise backward selection method for variable selection. R code for the stepwise backward selection method is as below.
>Model_1 <- glm(revisit ~ DaySinceLastVisit + visitCount +f.medium +f.landingPagePath +f.exitPagepath+pageDepth, data=data ,family = binomial("logit"))
>library(MASS)
>stepAIC(Model_1, direction="backward")
Output
Start: AIC=2119.37
revisit ~ DaySinceLastVisit + visitCount + f.medium + f.landingPagePath + f.exitPagepath + pageDepth
Df Deviance AIC
- f.exitPagepath 152 1732.4 1966.4
- f.landingPagePath 87 1751.0 2115.0
1581.4 2119.4
- pageDepth 1 1583.4 2119.4
- f.medium 11 1656.5 2172.5
- visitCount 1 1740.1 2276.1
- DaySinceLastVisit 1 1826.4 2362.4
Step: AIC=1966.42
revisit ~ DaySinceLastVisit + visitCount + f.medium + f.landingPagePath + pageDepth
Df Deviance AIC
1732.4 1966.4
- pageDepth 1 1738.9 1970.9
- f.landingPagePath 101 1987.5 2019.5
- f.medium 12 1821.2 2031.2
- visitCount 1 1929.3 2161.3
- DaySinceLastVisit 1 1978.4 2210.4
Before we understand the output, let me explain how the variables are selected in stepwise backward selection? In the stepwise backward selection method, AIC is used as the selection criterion. General rule is lower the AIC, best the model(i.e. For a group of variables, if AIC decrease by removing any variable(s) from group,then remaining variables are used in the model. This process continues until AIC stops decreasing). From the output, we can see that AIC is decreased and variable exitPageapath is excluded from the model. Now, we will create new model(Model_2 ) which does not include exitPageapath. R code for new model is as below.
>Model_2<-glm(revisit ~ DaySinceLastVisit + visitCount +f.medium +f.landingPagePath +pageDepth, data=data,family = binomial("logit"))
After generating the new model ,let’s check the accuracy of the new model and it is as below.
>predicted_revisit<- round(predict(Model_2,in_d,type="response")) >confusion_matrix<- ftable(revisit, predicted_revisit) >accuracy<- sum(diag(confusion_matrix))/2555*100
Output 86.57534
From the output, we can see that accuracy of the new model is decreased. This does not seem good to us. Variable selection method did not help us in improving the model. Let’s try second step for model improvement which is outlier detection.
As we know that data set contains some unreliable observations which make model’s quality poor. We always need to detect outlier and remove them. For numerical variables, outliers can be removed by observing the histogram of frequency distribution of the values of each variable (Process is described in blog Improving Bounce Rate Prediction Model for Google Analytics Data). In our data set, there are three numerical variables named visitCount, daySinceLastVisit and pageDepth. I have generated new data set after removing outliers. Let’s create new model based on new data set and check the accuracy of the new model. R code for new model is as below.
>Model_3 <- glm(revisit ~ DaySinceLastVisit + visitCount +f.medium +f.landingPagePath +f.exitPagepath+pageDepth, data=data_outlier_removed ,family = binomial("logit"))
Now, we will check the accuracy of the new model and it is as below.
>predicted_revisit <- round(predict(Model_3,in_d,type="response")) >confusion_matrix <- ftable(revisit, predicted_revisit) >accuracy <- sum(diag(confusion_matrix))/2292*100
Output 98.42932
From the result, we can see that model has more accuracy than previous models (Model_1 and Model_2) and it is good for us. So, removing the outliers from the data set, the model got more improvement and prediction accuracy. For now, we can conclude that through this model (Model_3), we can predict more accurately whether a user will return to website in next 24 hours. If you want to do exercise, Click here for R code and sample data set. In the next blog, we will discuss about logistic regression with Google Prediction API, check the accuracy of the Google Prediction API for our data set and try to predict for a user that will user return to website in next 24 hours?
Would you like to understand the value of predictive analysis when applied on web analytics data to help improve your understanding relationship between different variables? We think you may like to watch our Webinar - How to perform predictive analysis on your web analytics tool data. Watch the Replay now!

When a user visits your website and performs any activity involved with your website. i.e. viewing videos, purchasing products, requesting services that you may offer. Now in this competitive age it becomes necessary to keep up to the pace with the taste of your user base and constantly upgrading with your offerings. Using Google Analytics as a constant input always helps website owners to decide on the critical business needs, to grow or to reach to end user. Now if you can identify user’s behavior tagging them anonymously would give a detailed analysis which would give you additional advantage while fulfilling your user’s requirement.
For example, if an e-commerce owner would know that people from certain region prefer free-shipping and from other region they prefer using discount coupon codes and purchases more. While identifying such users would help website owner to create unique offers on his web store.
Now we have come up with a small piece of JavaScript code that tags each visitor as a uniquely generated number and sets it into custom variable on visitor level and pushes it to Google Analytics. Once the data is collected in your Google Analytics profile. You can analyze these data for each user’s behavior, which would help you to create unique offerings from your website.
You can download the code “Visitor Id Tracking” from our JavaScript Resource section. Once you download the code you need to integrate it on your website using the instructions provided with it.
Note: This solution uses custom variable 1 on visitor level so make sure if you are already using custom variable on your site update the code accordingly to use available custom variable on your website.
Once you have all the data collected you can view it in your GA interface like below:

Now you can either use GA interface to create different reports or extract data using GA API v3 and use the data for your next level of analysis.
Some use cases of implementation:
Want us to help you implement or analyze the data for your visitors. Contact us
In this post, I am going to explain how can we build model for transactional product revenue prediction application with Google Prediction API as we already discussed same stuff (Product revenue prediction with R)on R. With the help of Prediction API, we can build prediction model without any programming. Here we just have to focus on our dataset to make more accurate predictions. Google provides this service with Google Cloud Storage and Google Prediction API.
We can store our dataset at Google cloud storage and fire queries from Google prediction API for generating prediction on sets of your dataset which is easy. Google Prediction API is developed with set of optimized Machine Learning Algorithms for model based prediction. Here is description on the base of the Prediction API v1.5
How to train data with Google Prediction API:
First, we require minimum one number of project at our Google API console, enabled Google Prediction API and Google Cloud Storage service with that project. Our datasets have to meet certain format like
Then we can upload our dataset to Google Cloud Storage Engine by creating bucket. After uploading dataset to Google Cloud Storage Engine, we have to query from Google Prediction API by Google API explorer. There are numbers of methods to Prediction API service to deal with the data like
As in previous blog, we have already developed model in R for same dataset. For doing same with Google Prediction API, We can start training of dataset with prediction.trainedmodels.insert with model unique id, data storage location, and type of model as parameters.
Will give Response like:
It means request accepted by Google Prediction server and have started training. We can check the training status of model with the help of prediction.trainedmodels.get
Here is response of Get function of Prediction API:
Where it describes
Toatal numbers of instance = 4061
Type of model = Regression
Mean squared Error(MSE) = 1606123.17
Here, this model summary will not provide intercept and variable coefficient like R. To make prediction with above model we can query by Predict method (prediction.trainedmodels.predict)
Test 1:
Actual (Productrevenue) = 110.06
Output (Productrevenue) = 155.15717487938028
Test 2:
Actual (Productrevenue) = 803.81
Output (Productrevenue) = 934
with R (after removing outliers + subsets of independent variable), we predicted 115.8346013 for test 1 and 955.153476 for test 2 on same dataset. Therefore, getting prediction with R and Google Prediction API are nearly same. When we are doing with prediction API we can improve prediction with improving dataset quality but another side in R, we can improve prediction accuracy by improving dataset quality as well as prediction model as we already discussed in Product revenue prediction with R - part 2
Explore About Tatvic and Services