# Visualize treatement effect in Graph (difference in differences)

Hello
i'm trying to finish my graph for my difference-in-differences analysis. I try to visualize the difference in difference estimator in my graph. The following codes I created with some help visualize both trends. Now i'm trying to add a line to the Hong Kong line (treated group) that would include the effect of the differences in differences analysis. It would show the trend HK Airport woul've "normally" followed. The effect starts by the end of july 2019 where the drop in passangers is important. I haven't found an example online to try out. I've calculated the effect with the codes below.

thank you!

``````library(readxl)
col_types = c("text", "date", "numeric"))
View(HONGKONG)

library(dplyr)
library(zoo)
library(foreign)

y <- HONGKONG\$Passengers
x <- HONGKONG\$Datum
HONGKONG\$Datum<-as.yearmon(x)
sort(as.yearmon(HONGKONG\$Datum,format="%y-%m"))
limit<-c("2019-07-31")
HONGKONG\$P = ifelse(HONGKONG\$Datum >limit, 1, 0)
HONGKONG\$S = ifelse(HONGKONG\$City == "HongKong", 0, 1)
HONGKONG\$did = HONGKONG\$P * HONGKONG\$S
city1<-HONGKONG\$City==as.character("HongKong")
city2<-HONGKONG\$City==as.character("Singapur")

HONGKONG\$did = HONGKONG\$P * HONGKONG\$S
didreg = lm(y~S+P+did, data=HONGKONG)
summary(didreg)

mean_effects <- HONGKONG %>% group_by(P, S) %>%
summarise(mean_passengers = mean(Passengers))

a = mean_effects %>% filter(P == 0, S == 0) %>% pull(mean_passengers) #HKvorher
b = mean_effects %>% filter(P == 0, S == 1) %>% pull(mean_passengers) #SINvorhher
c = mean_effects %>% filter(P == 1, S == 0) %>% pull(mean_passengers) #HKnachher
d = mean_effects %>% filter(P == 1, S == 1) %>% pull(mean_passengers) #SINnachher

a
b
c
d

(c-a)-(d-b)

mean_effects <- HONGKONG %>% group_by(P,S) %>%
summarise(mean_passengers = mean(Passengers))

library(ggplot2)

options(scipen=1000000)

p<- HONGKONG %>%
ggplot(aes(x=Datum, y = Passengers, color = City)) +
geom_line() +
scale_y_continuous(limits = c(4000000,7000000)) +
theme_classic() +
labs(title = "Trend zwischen Hong Kong und Singapur",
x="Zeitraum", y="Anzahl FluggĂ¤ste") +
theme(plot.title = element_text(hjust=0.5)) +
theme(legend.justification=c(1,0), legend.position = c(1,0))+
scale_color_manual(values=c('Blue','darkgreen'))

p + geom_vline(xintercept=as.numeric(HONGKONG\$Datum[31]), linetype=3)
``````

The data i used:

``````structure(list(City = c("HongKong", "HongKong", "HongKong", "HongKong",
"HongKong", "HongKong", "HongKong", "HongKong", "HongKong", "HongKong",
"HongKong", "HongKong", "HongKong", "HongKong", "HongKong", "HongKong",
"HongKong", "HongKong", "HongKong", "HongKong", "HongKong", "HongKong",
"HongKong", "HongKong", "HongKong", "HongKong", "HongKong", "HongKong",
"HongKong", "HongKong", "HongKong", "HongKong", "HongKong", "HongKong",
"HongKong", "HongKong", "Singapur", "Singapur", "Singapur", "Singapur",
"Singapur", "Singapur", "Singapur", "Singapur", "Singapur", "Singapur",
"Singapur", "Singapur", "Singapur", "Singapur", "Singapur", "Singapur",
"Singapur", "Singapur", "Singapur", "Singapur", "Singapur", "Singapur",
"Singapur", "Singapur", "Singapur", "Singapur", "Singapur", "Singapur",
"Singapur", "Singapur", "Singapur", "Singapur", "Singapur", "Singapur",
"Singapur", "Singapur"), Datum = structure(c(2017, 2017.08333333333,
2017.16666666667, 2017.25, 2017.33333333333, 2017.41666666667,
2017.5, 2017.58333333333, 2017.66666666667, 2017.75, 2017.83333333333,
2017.91666666667, 2018, 2018.08333333333, 2018.16666666667, 2018.25,
2018.33333333333, 2018.41666666667, 2018.5, 2018.58333333333, 2018.66666666667, 2018.75, 2018.83333333333, 2018.91666666667,
2019, 2019.08333333333, 2019.16666666667, 2019.25, 2019.33333333333,
2019.41666666667, 2019.5, 2019.58333333333, 2019.66666666667,
2019.75, 2019.83333333333, 2019.91666666667, 2017, 2017.08333333333,
2017.16666666667, 2017.25, 2017.33333333333, 2017.41666666667,
2017.5, 2017.58333333333, 2017.66666666667, 2017.75, 2017.83333333333,
2017.91666666667, 2018, 2018.08333333333, 2018.16666666667, 2018.25,
2018.33333333333, 2018.41666666667, 2018.5, 2018.58333333333,
2018.66666666667, 2018.75, 2018.83333333333, 2018.91666666667,
2019, 2019.08333333333, 2019.16666666667, 2019.25, 2019.33333333333,
2019.41666666667, 2019.5, 2019.58333333333, 2019.66666666667,
2019.75, 2019.83333333333, 2019.91666666667), class = "yearmon"),
Passengers = c(6190000, 5489000, 5888000, 6268000, 5982000,
5906000, 6521000, 6503000, 5601000, 6159000, 5938000, 6421000,
6128000, 5820000, 6128000, 5820000, 6398000, 6301000, 6038000,
6213000, 6661000, 6847000, 5566000, 6176000, 5995000, 6528000,
6420000, 6491000, 6236000, 6347000, 6729000, 5994000, 4857000,
5374000, 5026000, 5715000, 5256301, 4669729, 5112576, 5168548,
5003578, 5208779, 5415734, 5265703, 4927561, 5155327, 5173747,
5861990, 5303639, 4932345, 5303639, 4932345, 5555117, 5430745,
5294980, 5565775, 5723094, 5682688, 5225903, 5376234, 5408993,
6127843, 5630780, 5580503, 5407308, 5816089, 5910782, 5900629,
5469342, 5646643, 5718386, 6414495), P = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1), S = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), did = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1)), row.names = c(NA, -72L), class = c("tbl_df",                                                                                                                                                                                                          "tbl", "data.frame"))
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.