Marks: 60
The advent of e-news, or electronic news, portals have offered us a great opportunity to quickly get updates on the day-to-day events occurring globally. The information on these portals is retrieved electronically from online databases, processed using a variety of software, and then transmitted to the users. There are multiple advantages of transmitting new electronically, like faster access to the content and the ability to utilize different technologies such as audio, graphics, video, and other interactive elements that are either not being used or aren’t common yet in traditional newspapers.
E-news Express, an online news portal, aims to expand its business by acquiring new subscribers. With every visitor to the website taking certain actions based on their interest, the company plans to analyze these actions to understand user interests and determine how to drive better engagement. The executives at E-news Express are of the opinion that there has been a decline in new monthly subscribers compared to the past year because the current webpage is not designed well enough in terms of the outline & recommended content to keep customers engaged long enough to make a decision to subscribe.
[Companies often analyze user responses to two variants of a product to decide which of the two variants is more effective. This experimental technique, known as A/B testing, is used to determine whether a new feature attracts users based on a chosen metric.]
The design team of the company has researched and created a new landing page that has a new outline & more relevant content shown compared to the old page. In order to test the effectiveness of the new landing page in gathering new subscribers, the Data Science team conducted an experiment by randomly selecting 100 users and dividing them equally into two groups. The existing landing page was served to the first group (control group) and the new landing page to the second group (treatment group). Data regarding the interaction of users in both groups with the two versions of the landing page was collected. Being a data scientist in E-news Express, you have been asked to explore the data and perform a statistical analysis (at a significance level of 5%) to determine the effectiveness of the new landing page in gathering new subscribers for the news portal by answering the following questions:
Do the users spend more time on the new landing page than on the existing landing page?
Is the conversion rate (the proportion of users who visit the landing page and get converted) for the new page greater than the conversion rate for the old page?
Does the converted status depend on the preferred language? [Hint: Create a contingency table using the pandas.crosstab() function]
Is the time spent on the new page the same for the different language users?
The data contains information regarding the interaction of users in both groups with the two versions of the landing page.
user_id - Unique user ID of the person visiting the website
group - Whether the user belongs to the first group (control) or the second group (treatment)
landing_page - Whether the landing page is new or old
time_spent_on_the_page - Time (in minutes) spent by the user on the landing page
converted - Whether the user gets converted to a subscriber of the news portal or not
language_preferred - Language chosen by the user to view the landing page
# Libraries to help with reading and manipulating data
import pandas as pd
import numpy as np
# Libraries to help with data visualization
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# Library to help with statistical analysis
import scipy.stats as stats
df = pd.read_csv('abtest.csv')
The initial steps to get an overview of any dataset is to:
df.head()
df.tail()
user_id
and time_spent_on_the_page
, rest all the variables are categorical in naturedf.shape
df.info()
There are a total of 100 non-null observations in each of the columns
There are 6 columns named 'user_id', 'group', 'landing_page', 'time_spent_on_the_page', 'converted', 'language_preferred' whose data types are int64, object, object, float64, object, object respectively
df.describe().T
df.describe(include = ['object']).T
group
, landing_page
and converted
have only two categoriesgroup
and landing_page
are equal# missing value check
df.isna().sum()
df.duplicated().sum()
Let us first explore the numerical variables
sns.histplot(data=df,x='time_spent_on_the_page')
plt.show()
sns.boxplot(data=df,x='time_spent_on_the_page')
plt.show()
Let us now explore the categorical variables
df['group'].value_counts()
sns.countplot(data=df,x='group')
plt.show()
df['landing_page'].value_counts()
sns.countplot(data=df,x='landing_page')
plt.show()
df['converted'].value_counts()
sns.countplot(data=df,x='converted')
plt.show()
df['language_preferred'].value_counts()
sns.countplot(data=df,x='language_preferred')
plt.show()
plt.figure(figsize=(10,6))
sns.boxplot(data=df,x='landing_page',y='time_spent_on_the_page')
plt.show()
plt.figure(figsize=(10,6))
sns.boxplot(data=df,x='converted',y='time_spent_on_the_page')
plt.show()
plt.figure(figsize=(8,8))
sns.boxplot(x = 'language_preferred', y = 'time_spent_on_the_page', showmeans = True, data = df)
plt.show()
# visual analysis of the time spent by users on the new and old landing pages
plt.figure(figsize=(8,6))
sns.boxplot(x = 'landing_page', y = 'time_spent_on_the_page', data = df)
plt.show()
Let's perform a hypothesis test to see if there is enough statistical evidence to support our observation.
$H_0:$ The mean time spent by the users on the new page is equal to the mean time spent by the users on the old page.
$H_a:$ The mean time spent by the users on the new page is greater than the mean time spent by the users on the old page.
Let $\mu_1$ and $\mu_2$ be the mean time spent by the users on the new and old page respectively. Then the above formulated hypotheses can be mathematically written as:
$H_0: \mu_1 = \mu_2 \\ H_a: \mu_1 > \mu_2$
This is a one-tailed test concerning two population means from two independent populations. As the population standard deviations are unknown, the two sample independent t-test will be the appropriate test for this problem.
As given in the problem statement, we select α = 0.05.
time_spent_new = df[df['landing_page'] == 'new']['time_spent_on_the_page']
time_spent_old = df[df['landing_page'] == 'old']['time_spent_on_the_page']
print('The sample standard deviation of the time spent on the new page is:', round(time_spent_new.std(),2))
print('The sample standard deviation of the time spent on the new page is:', round(time_spent_old.std(),2))
As the sample standard deviations are different, the population standard deviations may be assumed to be different.
# import the required function
from scipy.stats import ttest_ind
# find the p-value
test_stat, p_value = ttest_ind(time_spent_new, time_spent_old, equal_var = False, alternative = 'greater')
print('The p-value is', p_value)
# print the conclusion based on p-value
if p_value < 0.05:
print(f'As the p-value {p_value} is less than the level of significance, we reject the null hypothesis.')
else:
print(f'As the p-value {p_value} is greater than the level of significance, we fail to reject the null hypothesis.')
Since the p-value is less than the 5% significance level, we reject the null hypothesis. Hence, we have enough statistical evidence to say that the mean time spent by the users on the new page is greater than the mean time spent by the users on the old page.
# visual analysis of the conversion rate for the new page and the conversion rate for the old page
pd.crosstab(df['converted'],df['landing_page'],normalize='index').plot(kind="bar", figsize=(6,8),stacked=True)
plt.legend()
plt.show()
By observing the above plot, we can say that overall the number of users who get converted is more for the new page than the old page. Let's perform a hypothesis test to see if there is enough statistical evidence to say that the conversion rate for the new page is greater than the old page.
$H_0:$ The conversion rate for the new page is equal to the conversion rate for the old page.
$H_a:$ The conversion rate for the new page is greater than the conversion rate for the old page.
Let $p_1$ and $p_2$ be the conversion rate for the new and old page respectively.
Mathematically, the above formulated hypotheses can be written as:
$H_0: p_1 = p_2 \\ H_a: p_1 > p_2$
This is a one-tailed test concerning two population proportions from two independent populations. Hence, the two-sample proportion z-test will be the appropriate test for this problem.
As given in the problem statement, we select α = 0.05.
new_converted = df[df['group'] == 'treatment']['converted'].value_counts()['yes']
old_converted = df[df['group'] == 'control']['converted'].value_counts()['yes']
print('The numbers of converted users for the new and old pages are {0} and {1} respectively'.format(new_converted, old_converted))
n_control = df.group.value_counts()['control'] # number of users in the control group
n_treatment = df.group.value_counts()['treatment'] #number of users in the treatment group
print('The numbers of users served the new and old pages are {0} and {1} respectively'.format(n_control, n_treatment ))
# import the required function
from statsmodels.stats.proportion import proportions_ztest
# find the p-value
test_stat, p_value = proportions_ztest([new_converted, old_converted] , [n_treatment, n_control], alternative = 'larger')
print('The p-value is', p_value)
# print the conclusion based on p-value
if p_value < 0.05:
print(f'As the p-value {p_value} is less than the level of significance, we reject the null hypothesis.')
else:
print(f'As the p-value {p_value} is greater than the level of significance, we fail to reject the null hypothesis.')
Since the p-value is less than the 5% significance level, we reject the null hypothesis. Hence, we have enough statistical evidence to say that the conversion rate for the new page is greater than the conversion rate for the old page.
# visual analysis of the dependency between conversion status and preferred langauge
pd.crosstab(df['converted'],df['language_preferred'],normalize='index').plot(kind="bar", figsize=(6,8),
stacked=True)
plt.legend()
plt.show()
The distribution of conversion status for English and French language users is not uniformly distributed. Let's perform the hypothesis test to check whether we have enough statistical evidence to say that the conversion status and preferred language are independent or not.
$H_0:$ The converted status is independent of the preferred language.
$H_a:$ The converted status is not independent of the preferred language.
This is a problem of Chi-square test of independence, concerning the two independent categorical variables, converted status and preferred language.
As given in the problem statement, we select α = 0.05.
# create the contingency table showing the distribution of two categorical variables
contingency_table = pd.crosstab(df['converted'], df['language_preferred'])
contingency_table
#import the required function
from scipy.stats import chi2_contingency
# use chi2_contingency() to find the p-value
chi_2, p_value, dof, exp_freq = chi2_contingency(contingency_table)
# print the p-value
print('The p-value is', p_value)
# print the conclusion based on p-value
if p_value < 0.05:
print(f'As the p-value {p_value} is less than the level of significance, we reject the null hypothesis.')
else:
print(f'As the p-value {p_value} is greater than the level of significance, we fail to reject the null hypothesis.')
Since the p-value is greater than the 5% significance level, we fail to reject the null hypothesis. Hence, we do not have enough statistical evidence to say that the converted status depends on the preferred language.
# visual analysis of the mean time spent on the new page for different language users
plt.figure(figsize=(8,8))
# create a new DataFrame for users served the new page
df_new = df[df['landing_page'] == 'new']
sns.boxplot(x = 'language_preferred', y = 'time_spent_on_the_page', showmeans = True, data = df_new)
plt.show()
# Checking the mean time spent on the new page for different language users
df_new.groupby(['language_preferred'])['time_spent_on_the_page'].mean()
The mean time spent on the new page by English users is a bit higher than the mean time spent by French and Spanish users, but we need to test if this difference is statistically significant or not.
$H_0:$ The mean times spent on the new page by English, French, and Spanish users are equal.
$H_a:$ At least one of the mean times spent on the new page by English, French, and Spanish users is unequal.
This is a problem, concerning three population means. One-way ANOVA could be the appropriate test here provided normality and equality of variance assumptions are verified.
For testing of normality, Shapiro-Wilk’s test is applied to the response variable.
For equality of variance, Levene test is applied to the response variable.
We will test the null hypothesis
$H_0:$ Time spent on the new page follows a normal distribution
against the alternative hypothesis
$H_a:$ Time spent on the new page does not follow a normal distribution
# Assumption 1: Normality
# import the required function
from scipy.stats import shapiro
# find the p-value
w, p_value = shapiro(df_new['time_spent_on_the_page'])
print('The p-value is', p_value)
Since p-value of the test is very large than the 5% significance level, we fail to reject the null hypothesis that the response follows the normal distribution.
We will test the null hypothesis
$H_0$: All the population variances are equal
against the alternative hypothesis
$H_a$: At least one variance is different from the rest
#Assumption 2: Homogeneity of Variance
#import the required function
from scipy.stats import levene
statistic, p_value = levene( df_new[df_new['language_preferred']=="English"]['time_spent_on_the_page'],
df_new[df_new['language_preferred']=="French"]['time_spent_on_the_page'],
df_new[df_new['language_preferred']=="Spanish"]['time_spent_on_the_page'])
# find the p-value
print('The p-value is', p_value)
Since the p-value is large than the 5% significance level, we fail to reject the null hypothesis of homogeneity of variances.
As given in the problem statement, we select α = 0.05.
time_spent_English = df_new[df_new['language_preferred']=="English"]['time_spent_on_the_page']
time_spent_French = df_new[df_new['language_preferred']=="French"]['time_spent_on_the_page']
time_spent_Spanish = df_new[df_new['language_preferred']=="Spanish"]['time_spent_on_the_page']
# import the required function
from scipy.stats import f_oneway
# find the p-value
test_stat, p_value = f_oneway(time_spent_English, time_spent_French, time_spent_Spanish)
# print the p-value
print('The p-value is', p_value)
# print the conclusion based on p-value
if p_value < 0.05:
print(f'As the p-value {p_value} is less than the level of significance, we reject the null hypothesis.')
else:
print(f'As the p-value {p_value} is greater than the level of significance, we fail to reject the null hypothesis.')
Since the p-value is greater than the 5% significance level, we fail to reject the null hypothesis. Hence, we do not have enough statistical evidence to say that the mean times spent on the new page by English, French, and Spanish users differ to any meaningful degree.
The users spend more time on the new page.
The conversion rate for the new page is greater than the conversion rate of the old page
The conversion status is independent of the preferred language
The time spent on the new page does not differ with the language of the content
It is recommended that the news company uses the new landing page to gather more subscribers