rm(list = ls()) # Clear the workspace
library(dplyr)
library(ggplot2)
library(fixest) # use for regression approach + cluster-robust robust standard errors
data.sales <- read.csv("sales_DID.csv")
summary(data.sales)
## month storenum treatregion sales
## Min. : 1.00 Min. : 1.00 Min. :0.0 Min. :16284
## 1st Qu.: 6.75 1st Qu.: 25.75 1st Qu.:0.0 1st Qu.:24423
## Median :12.50 Median : 50.50 Median :0.5 Median :27123
## Mean :12.50 Mean : 50.50 Mean :0.5 Mean :27547
## 3rd Qu.:18.25 3rd Qu.: 75.25 3rd Qu.:1.0 3rd Qu.:30424
## Max. :24.00 Max. :100.00 Max. :1.0 Max. :41940
We start by creating a graph of the average sales in each region by month (averaged across the 50 stores in that region for that month).Notice that the last bit of code adds a vertical line separating the pre- and post-periods.
summary <- data.sales %>%
mutate(treatregion = as.factor(treatregion)) %>%
mutate(month = as.factor(month)) %>%
group_by(treatregion,month) %>% #group the data by treatregion and month
summarise (n = length(storenum),
m.sales= mean(sales),
error = sd(sales)/sqrt(n),
lci = m.sales - 1.96*error,
uci = m.sales + 1.96*error)
summary %>%
ggplot(aes(x=month, y = m.sales, group = treatregion, color = treatregion)) +
geom_point()+
geom_line()+
geom_errorbar(aes(ymin = lci, ymax = uci))+
ylab("Average Sales") +
xlab("Month") +
scale_color_discrete(name=NULL, labels = c("Control Region", "Treatment Region"))+
# add a vertical line
geom_vline(xintercept=12.5, linetype="dashed", color = "blue")
Describe the plot and note (i) whether it appears consistent with the parallel trends assumption and (ii) whether we should expect to see a positive, negative, or zero DID estimate. (Bonus (iii): use the plot to get a rough guess of the DID estimate. Explain your reasoning.)
The graph shows the mean sales trends over time in both the “treatment” region, which has redesign in month 12, and the “control” region, which remains the same in month 12. Notably, the treatment group experiences an increase in average sales following the redesign, while the control group shows no clear change. The visual representation aligns with the assumption of parallel trends before the treatment, followed by a normalization in the post-treatment period. Consequently, a positive estimate for the Difference-in-Differences (DID) is anticipated. As an additional note, the bonus is set at 5,000; the delta for the treatment group (deltaTreat) is calculated as 33,000 - 28,000, resulting in 5,000, while the delta for the control group (deltaControl) is 0. The DID is then computed as the difference between deltaTreat and deltaControl, yielding a value of 5,000.
Suppose your smart and experienced colleague after seeing this graph stated: “Well, I guess it is helpful to have the control region as a comparison to validate that the changes we see in the treatment region were not due to other factors at that time. But ultimately it looks like the difference-in-difference approach will give us basically the same estimate as just a simple event-study analysis for the treatment region.” Briefly explain what your colleague means by that statement and what she is looking at in the graph that leads her to that conclusion.
It is important to make the assumption that the control (comparison) group serves as a reliable counterfactual, representing the scenario where the treatment group did not undergo the redesign. The sudden shift she is examining this. The aim is to rely solely on exogenous variation, minimizing any confounding factors between the treatment and control groups. Additionally, the presence of data clustering introduces complexities that affect DID method.
The code below makes the “post” and “post times treat” variables we ened for the DID regression, and then estimates the regression. Notice that we use the fixest package to get correct standard errors. YOu’ll need to install this package if you don’t have it.
#generate a dummy variable named post to indicate post-period (after month 12)
# and make post*treat
data.sales = data.sales %>% mutate(
post = ifelse(month>12,1,0) ,
post_treat = post*treatregion )
# estimate model - use feols to get correct standard errors
fit.sales = feols(sales~treatregion+post+post_treat, data = data.sales)
# correct standard errors
summary(fit.sales, cluster~storenum)
## OLS estimation, Dep. Var.: sales
## Observations: 2,400
## Standard-errors: Clustered (storenum)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 25082.1850 124.012 202.255550 < 2.2e-16 ***
## treatregion 2510.1546 176.354 14.233639 < 2.2e-16 ***
## post -45.3107 145.184 -0.312091 0.75563
## post_treat 4929.5619 210.270 23.443939 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## RMSE: 3,008.1 Adj. R2: 0.502703
The table reports the estimated coefficient on each term (treat dummy, post dummy, interaction) in the first column and the standard error in the second.
Q3 ANSWER HERE
The adjusted estimate for the treatment region, represented by beta, is 2,164.5 with a 95% confidence interval from 2,164.5-2,855.8. The trend in the post-treatment period (post beta) is assumed to be linear and has a 95% confidence interval from -329.9 - 239.2. The coefficient on interaction, post_treat beta, serves as the DID estimate, with a 95% confidence interval 4,887.9 - 4,971.3.
data.rating <- read.csv("RatingData.csv")
summary(data.rating)
## id month rating treat
## Min. : 75 Min. : 1.00 Min. :1.000 Min. :0.0000
## 1st Qu.: 4224 1st Qu.: 7.00 1st Qu.:3.000 1st Qu.:0.0000
## Median : 16821 Median :14.00 Median :4.000 Median :0.0000
## Mean : 29071 Mean :13.39 Mean :3.742 Mean :0.2341
## 3rd Qu.: 47383 3rd Qu.:20.00 3rd Qu.:5.000 3rd Qu.:0.0000
## Max. :104668 Max. :24.00 Max. :5.000 Max. :1.0000
Plot the average rating for each website by month and include 95% confidence intervals on the average rating, similar to the example case. Make sure to label the axes and legend appropriately so that it is easy to process the information in the graph.
Interpret what you are seeing in the graph. Does the “parallel trends” assumption between Trip Advisor ratings and Yelp ratings appear valid here? Is there a discernable effect of the treatment at Trip Advisor that you can see in the graph?
# code for q4
summary <- data.rating %>%
mutate(treat = as.factor(treat)) %>%
mutate(month = as.factor(month)) %>%
group_by(treat,month) %>% #group the data by treat and month
summarise (n = length(id),
m.rating= mean(rating),
error = sd(rating)/sqrt(n),
lci = m.rating - 1.96*error,
uci = m.rating + 1.96*error)
summary %>%
ggplot(aes(x=month, y = m.rating, group = treat, color = treat)) +
geom_point()+
geom_line()+
geom_errorbar(aes(ymin = lci, ymax = uci))+
ylab("Average Rating") +
xlab("Month") +
scale_color_discrete(name=NULL, labels = c("Control", "Treatment"))+
# add a vertical line
geom_vline(xintercept=12.5, linetype="dashed", color = "blue")
Q4 ANSWER HERE
The treatment starts in month 12. Notably, the treatment group exhibits more variability than the control group, especially after the treatment. It looks like the the parallel trends assumption holds before the treatment and might continue after the treatment. However, it’s difficult to determine due to the increased variability in the treatment group post-treatment. The graph doesn’t show a clear impact of the treatment. Additionally, The higher variability suggests that some restaurants may have improved as a result of the treatment.
Now run a regression (as in the example case) to quantify the difference-in-difference estimate of the effect of the switch to multi-attribute rating on average restaurant ratings. What is the DID estimate and its 95% confidence interval? Is the estimate statistically significant? Business significant?
# code for q5
data.rating = data.rating %>% mutate(
post = ifelse(month>12,1,0) ,
post_treat = post*treat )
# estimate model - use feols to get correct standard errors
fit.rating = feols(rating~treat+post+post_treat, data = data.rating)
# correct standard errors
summary(fit.rating, cluster~id)
## OLS estimation, Dep. Var.: rating
## Observations: 38,004
## Standard-errors: Clustered (id)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.713904 0.024881 149.26665 < 2.2e-16 ***
## treat 0.172921 0.029289 5.90402 5.1461e-09 ***
## post -0.036300 0.017546 -2.06880 3.8870e-02 *
## post_treat 0.094929 0.036589 2.59442 9.6402e-03 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## RMSE: 1.07243 Adj. R2: 0.007578
Q5 ANSWER HERE
The DID estimate is 0.094929 from the post_treat with its 95% confidence interval being 0.0232-0.1666 . The estimate is statistically significant because of the p value. It is not business significant because there is not a lot of benefit that could be derived.
Focusing on the regression estimates and ignoring graphical evidence for the moment, what do you conclude about the effect of the switch to the multivariate rating system on average ratings at Trip Advisor? Please be specific about the evidence you are using for your conclusion.
Q6 ANSWER HERE
The regression findings suggest that using a multivariate rating system on Trip Advisor could increase restaurant ratings.
Thus, this has the potential for restaurants having an increased desire to advertise on Trip Advisor. This conclusion comes from the positive DID estimate, which allows us to reject the null hypothesis asserting that the control and treatment groups are undifferentiated after the treatment.
Briefly discuss how the regression results compare to your visual analysis of the graph you created. Do you reach a similar conclusion or different? Which approach would you rely on more in a business situation for this case, and why?
Q7 ANSWER HERE
In comparison between the regression results and the visual analysis of the graph we two things. The regression results indicate that multivariate rating system increased average restaurant ratings on Trip Advisor. The visual analysis however tells us that there a multitude of month to month variance along with overlapping error bars in some months. This tells us that there is minimal impact.
In a business situation for this case I would rely visual interpretation to grasp the overall trends and patterns in the data and then regression analysis to make precise predictions.
Did you use generative AI in this homework? If yes, please explain how you used it.
Q8 ANSWER HERE
I did not use generative AI.