Load the packages we want.

knitr::opts_chunk$set(echo = TRUE)
rm(list = ls()) # Clear the workspace
library(dplyr)
library(readxl)
library(knitr)
library(tidyverse)
library(ggplot2)
library(lmtest) 
library(sandwich)

Read your data here.

md_data2 = read.csv('C:/Users/havil/Documents/1a Homework 1 Clinic Sales Case Study/rocketfuel_deciles.csv', header = TRUE)

casedata = read_excel ("C:/Users/havil/Documents/1a Homework 1 Clinic Sales Case Study/rocketfuel_deciles.xlsx")

1. Check for Balance

In a table, compare the shares of individuals by treatment status, and the means and standard deviations of pre-experiment variables. Comment in your markdown file on your findings.

balalnce_table = md_data2 %>% group_by(test) %>% 
  summarise(N=n(),   
  converted_m = mean(converted),
converted_sd = sd(converted),
  tot_impr_m = mean(tot_impr), tot_impr_sd = sd(tot_impr),
  day_impr_m = mean(mode_impr_day), day_impr_sd = sd(mode_impr_day),
  hour_impr_m = mean(mode_impr_hour), hour_impr_sd = sd(mode_impr_hour)
  ) %>% mutate(share = N/sum(N))
### do not change anything in {}
### Comment below
kable(balalnce_table, digits = 2)
test N converted_m converted_sd tot_impr_m tot_impr_sd day_impr_m day_impr_sd hour_impr_m hour_impr_sd share
0 23524 0.02 0.13 24.76 42.86 3.95 1.95 14.30 4.66 0.04
1 564577 0.03 0.16 24.82 43.75 4.03 2.01 14.48 4.84 0.96

There seems to be balance in the data.

2. Plot Outcome

Plot the means and confidence intervals of the main outcome “converted,” treatment status. In your markdown, file, interpret the result (mean and confidence intervals, noting width).

### do not change anything in {}
### Insert your code for developing plot

outcome_table = casedata %>% group_by(test) %>% 
  summarise(N=n(),
          
            converted_m = mean(converted),
            converted_sd = sd(converted)
            ) %>% 
  mutate(
    converted_se = converted_sd / sqrt(N) ,
    converted_lb = converted_m - 1.96*converted_se ,
    converted_ub = converted_m + 1.96*converted_se
  )
simple_table = select(outcome_table ,test, converted_m, converted_lb , converted_ub, 
                      )

kable(simple_table, digits = 4)
test converted_m converted_lb converted_ub
0 0.0179 0.0162 0.0195
1 0.0255 0.0251 0.0260
### do not change anything in {}
### Comment below
ggplot(outcome_table, aes(x=test, y=converted_m,ymin=converted_lb, ymax=converted_ub)) + 
  geom_point() + geom_errorbar()

It appears that the mean value for the control group 0 significantly lower then of the test group 1.Furthermore, the confidence interval width for the control group is approximately three times greater than the test group

3. Find ATE

Estimate of the Average Treatment Effect (ATE) of the ads for treatment relative to control, the associated standard error, and the 95% confidence interval on the Average Treatment Effects. You can do this by hand or with regression (but be careful about standard errors).

### do not change anything in {}
### Insert your code for calculating ATE

ate_table = outcome_table %>% 
  mutate(converted_ate = converted_m - converted_m[1],
          converted_ate_se = sqrt(converted_se^2 + converted_se[1]^2),
          converted_ate_lb = converted_ate - 1.96*converted_ate_se,
          converted_ate_ub = converted_ate + 1.96*converted_ate_se,)
### do not change anything in {}
### Insert your code for output

ate_table = ate_table %>%  
  select(
    test, 
    converted_ate, converted_ate_lb, converted_ate_ub,
   ) %>% 
  filter(test != 0)

kable(ate_table, digits=5)
test converted_ate converted_ate_lb converted_ate_ub
1 0.00769 0.00595 0.00943

4. Subgroup Analysis

Create a summary table showing the sample size, the mean and the standard deviation of variables in the data set for both treatment and control group over the 10 deciles of total impressions. Organize your table so that it’s easy to see treatment effects for each decile. Comment about your findings in the file.

### do not change anything in {}
### Insert your code for subgroup analysis

outcome_sg = casedata %>% group_by(tot_impr_decile, test) %>% summarise(converted_m = mean(converted), converted_sd = sd(converted), N=n() ) %>%
  mutate(converted_m_se = converted_sd / sqrt(N)) %>%  mutate(converted_lb = converted_m - 1.96*converted_m_se, converted_ub = converted_m + 1.96*converted_m_se)
ate_sg = outcome_sg %>% 
  mutate(converted_ate = converted_m - converted_m[1], converted_ate_se = sqrt(converted_m_se^2 + converted_m_se[1]^2),
     converted_lb = converted_ate - 1.96*converted_ate_se, converted_ub = converted_ate + 1.96*converted_ate_se)

ate_sg %>% filter(test != 0) %>% 
  select(tot_impr_decile, test, converted_ate, converted_lb, converted_ub)
### do not change anything in {}
### Comment below
### Insert your code for output

kable(ate_sg, digits=5)
tot_impr_decile test converted_m converted_sd N converted_m_se converted_lb converted_ub converted_ate converted_ate_se
1 0 0.00130 0.03604 2308 0.00075 -0.00208 0.00208 0.00000 0.00106
1 1 0.00158 0.03977 54298 0.00017 -0.00122 0.00179 0.00028 0.00077
2 0 0.00215 0.04637 3249 0.00081 -0.00226 0.00226 0.00000 0.00115
2 1 0.00258 0.05068 65239 0.00020 -0.00122 0.00206 0.00042 0.00084
3 0 0.00521 0.07200 2304 0.00150 -0.00416 0.00416 0.00000 0.00212
3 1 0.00343 0.05847 50425 0.00026 -0.00476 0.00121 -0.00178 0.00152
4 0 0.00562 0.07479 2490 0.00150 -0.00415 0.00415 0.00000 0.00212
4 1 0.00421 0.06475 56051 0.00027 -0.00440 0.00157 -0.00141 0.00152
5 0 0.00667 0.08140 2250 0.00172 -0.00476 0.00476 0.00000 0.00243
5 1 0.00683 0.08238 60882 0.00033 -0.00326 0.00359 0.00017 0.00175
6 0 0.00807 0.08952 1734 0.00215 -0.00596 0.00596 0.00000 0.00304
6 1 0.00779 0.08791 56368 0.00037 -0.00456 0.00399 -0.00029 0.00218
7 0 0.01390 0.11710 2662 0.00227 -0.00629 0.00629 0.00000 0.00321
7 1 0.01295 0.11304 61873 0.00045 -0.00549 0.00358 -0.00095 0.00231
8 0 0.01981 0.13937 1868 0.00322 -0.00894 0.00894 0.00000 0.00456
8 1 0.02274 0.14908 47226 0.00069 -0.00353 0.00940 0.00293 0.00330
9 0 0.03447 0.18247 2234 0.00386 -0.01070 0.01070 0.00000 0.00546
9 1 0.05240 0.22284 57194 0.00093 0.01015 0.02572 0.01793 0.00397
10 0 0.08412 0.27763 2425 0.00564 -0.01563 0.01563 0.00000 0.00797
10 1 0.14600 0.35311 55021 0.00151 0.05044 0.07331 0.06188 0.00584

As the decile size increases, so does the average conversion rate. This observation also holds true for the (ATE) in conversions.

5. Subgroup Plots – means + CI

Create a graph that shows the mean and 95% CI on “converted” separately for treatment and control plotted over the 10 deciles of total impressions. Briefly interpret the plot in your market down file. (Bonus: make a graph of the treatment effects and 95% CIs by decile.)

### do not change anything in {}
### Insert your code for subgroup plot
### do not change anything in {}
### Comment below
### Insert your code for output

ggplot(outcome_sg, mapping = aes(x=test, y=converted_m, 
                                 ymin= converted_lb, ymax=converted_ub,
  color = tot_impr_decile)) + 
  geom_point() + geom_errorbar()

There seems to be a positive correlation between the decile size and the average conversion rate, with larger deciles corresponding to higher conversion rates. Additionally, test “1”increases conversation rate in higher decile groups compared to control.

6. Value of advertising

Based on the overall treatment effect (question 3), what do you estimate is the increased profit per individual from running these ads? You should use information from the case to determine costs and benefits. Explain how you calculate costs and benefits.

To calculate benifit Benefit per individual = ATE * Profit per Conversion Cost per individual = Total Advertising Costs / Number of Individuals Net Profit per individual = Benefit per individual - Cost per individual

7. Targeting

Now suppose you could target ads, serving only based on deciles of impression (so you can control which decile is included in the campaign, but the total impression and costs will differ by decile). Based on your heterogeneous treatment effect estimates, and with a calculation analogous to the one for (6), which decile would you want to target?

10th decile would be targeted because treatement”test=1” is the the one witht the highest conversion rate and ATE.

8. Value of targeting

Discuss what your results imply about the value of selectively targeting ads.

It appears that focusing on the top decile yields significant value.