Now, the question is “does first birth affect women’s life satisfaction?” use th staggered difference in difference to estimate the effectl
Prepare the dataset
library(tidyverse) # recoding
library(haven) # import data
library(janitor) # tabulation
library(splitstackshape) # transform wide data to long data
library(plm) # panel data analysis
library(did) # difference in difference analysis
##Import 6 waves of women data
women1 <- read_dta("wave1_women.dta")
women2 <- read_dta("wave2_women.dta")
women3 <- read_dta("wave3_women.dta")
women4 <- read_dta("wave4_women.dta")
women5 <- read_dta("wave5_women.dta")
women6 <- read_dta("wave6_women.dta")
##Clean 6 waves of women data
clean_fun <- function(df) { df %>%
transmute(
id,
age,
wave=as.numeric(wave),
relstat=as_factor(relstat), #make relstat as a factor
relstat=case_when(relstat== "-7 Incomplete data" ~ as.character(NA), #specify when is missing for relstat
TRUE ~ as.character(relstat))%>% as_factor(), #make relstat as a factor again
health=case_when(hlt1<0 ~ as.numeric(NA), #specify when hlt1 is missing
TRUE ~ as.numeric(hlt1)),
childno=case_when(nkidsbio==-7~ as.numeric(NA), #specify when is missing for relstat
TRUE ~ as.numeric(nkidsbio)),
sat=case_when(sat6<0 ~ as.numeric(NA), #specify when sat6 is missing
TRUE ~ as.numeric(sat6)),
)%>% drop_na() }
women1a <- clean_fun(women1)
women2a <- clean_fun(women2)
women3a <- clean_fun(women3)
women4a <- clean_fun(women4)
women5a <- clean_fun(women5)
women6a <- clean_fun(women6)
women1b <- women1a %>% filter(childno==0)%>% #keep individuals who are childless in the first wave
rename(wave.1=wave, age.1=age, relstat.1=relstat, health.1=health, childno.1=childno, sat.1=sat ) #rename variables
women2b <- women2a %>%
rename(wave.2=wave, age.2=age, relstat.2=relstat, health.2=health, childno.2=childno, sat.2=sat )
women3b <- women3a %>%
rename(wave.3=wave, age.3=age, relstat.3=relstat, health.3=health, childno.3=childno, sat.3=sat )
women4b <- women4a %>%
rename(wave.4=wave, age.4=age, relstat.4=relstat, health.4=health, childno.4=childno, sat.4=sat )
women5b <- women5a %>%
rename(wave.5=wave, age.5=age, relstat.5=relstat, health.5=health, childno.5=childno, sat.5=sat )
women6b <- women6a %>%
rename(wave.6=wave, age.6=age, relstat.6=relstat, health.6=health, childno.6=childno, sat.6=sat )
###six waves of women data
women_6wave_wide <- left_join(women1b, women2b, by = "id") %>% # left join women1b and women2b
left_join(women3b, by = "id") %>% # left join with women3b
left_join(women4b, by = "id") %>% # left join with women4b
left_join(women5b, by = "id") %>% # left join with women5b
left_join(women6b, by = "id") # left join with women6b
#by using left_join I keep those have no kids in the first wave and follow them
women_6wave_long<- merged.stack(women_6wave_wide, #dataset for transfrom
var.stubs = c("age", "wave", "relstat", "health","childno", "sat"),
#var.stubs is to specify the prefixes of the variable groups
sep = ".") %>%
#sep is to specify the character that separates the "variable name" from the "times" in the source
drop_na(wave)
#drop the observations which did not join the wave
women_6wave_long <- women_6wave_long %>%
group_by(id) %>%
mutate(
firstkid=case_when( childno!=dplyr::lag(childno, 1) & dplyr::lag(childno, 1)==0 & childno>0 ~ 1,
TRUE ~ 0),
#when the person has 0 children at t-1 while has at least 1 child at t, define it first childbirth
twin=case_when( childno!=dplyr::lag(childno, 1) & dplyr::lag(childno, 1)==0 & childno==1 ~ 1, #single birth
childno!=dplyr::lag(childno, 1) & dplyr::lag(childno, 1)==0 & childno==2 ~ 2, #twin birth
TRUE ~ 0)
#when the person has 0 children at t-1 while has 1 child at t, define it a single birth, i.e. 1
#when the person has 0 children at t-1 while has 2 children at t, define it a twin birth, i.e. 2
)
#second, remove individuals who have twins
twinid <- women_6wave_long$id[women_6wave_long$twin==2] #the id of women who have twin for their first birth
women_6wave_long1 <- women_6wave_long[!(women_6wave_long$id %in% twinid),] #now the data does not have twin situations
#third, remove observations when people start to have a second or higher-order birth.
women_6wave_long2 <- women_6wave_long1%>%
filter(childno<2)
##women_6wave_long2 is a cleaned data, which removes twin cases and observations when people start to have a second or higher-order birth
Use the staggered difference in difference method to estimate the group-period specific effect of first birth on women’s wellbeing and plot the result
Step 1: create the treat group variable, a variable that tell us at which wave a woman is treated.
Step 2: then estimate the group-period specific effect of first birth on women’s wellbeing
Step 3: plot the result
women_6wave_long3 <- women_6wave_long2 %>%
group_by(id) %>%
mutate(
wave=as.numeric(wave),
birthwave=case_when(firstkid==1 ~ wave,
TRUE ~ 99), #identify the timing of first birth
anchorwave=min(birthwave), #generate the anchor wave
treatgroup=case_when(anchorwave %in% c(2:6) ~ anchorwave,
anchorwave==99 ~0 )
)
did1 <- att_gt(yname = "sat", #dependent variable
tname = "wave", #time variable
idname = "id", #id
gname = "treatgroup", #the variable in data that contains the first period when a particular observation is treated.
xformla = ~ health, #when you don't have any covariates to control, use "~ 1"; if yes, you can add covariates here by ~ x1+x2
data = women_6wave_long3 #specify your data
)
## Warning in pre_process_did(yname = yname, tname = tname, idname = idname, :
## Dropped 2346 observations while converting to balanced panel.
summary(did1)
##
## Call:
## att_gt(yname = "sat", tname = "wave", idname = "id", gname = "treatgroup",
## xformla = ~health, data = women_6wave_long3)
##
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna. "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015>
##
## Group-Time Average Treatment Effects:
## Group Time ATT(g,t) Std. Error [95% Simult. Conf. Band]
## 2 2 0.6364 0.3248 -0.2602 1.5330
## 2 3 -0.1154 0.3960 -1.2085 0.9777
## 2 4 -0.0283 0.2968 -0.8475 0.7909
## 2 5 0.1268 0.2821 -0.6518 0.9054
## 2 6 -0.1344 0.2762 -0.8967 0.6280
## 3 2 0.1890 0.3879 -0.8817 1.2597
## 3 3 0.4683 0.3655 -0.5407 1.4773
## 3 4 0.2713 0.3334 -0.6491 1.1917
## 3 5 -0.3048 0.3987 -1.4053 0.7956
## 3 6 -0.0345 0.4066 -1.1568 1.0879
## 4 2 -0.5418 0.3321 -1.4585 0.3748
## 4 3 0.6849 0.2557 -0.0210 1.3908
## 4 4 0.4158 0.2249 -0.2049 1.0365
## 4 5 -1.0459 0.3480 -2.0065 -0.0853 *
## 4 6 -0.5254 0.2596 -1.2421 0.1913
## 5 2 0.1981 0.3535 -0.7778 1.1739
## 5 3 0.2336 0.3174 -0.6425 1.1098
## 5 4 0.5715 0.2230 -0.0440 1.1870
## 5 5 0.0140 0.2790 -0.7561 0.7840
## 5 6 -0.8749 0.3991 -1.9766 0.2269
## 6 2 -0.4040 0.2864 -1.1945 0.3864
## 6 3 0.1991 0.3101 -0.6569 1.0550
## 6 4 -0.2025 0.3238 -1.0963 0.6913
## 6 5 0.9996 0.2782 0.2317 1.7675 *
## 6 6 0.2850 0.2133 -0.3038 0.8738
## ---
## Signif. codes: `*' confidence band does not cover 0
##
## P-value for pre-test of parallel trends assumption: 1e-05
## Control Group: Never Treated, Anticipation Periods: 0
## Estimation Method: Doubly Robust
ggdid(did1)
Estimate the weighted average effect of all group-time specific treatment effects
agg1 <- aggte(did1, type = "simple")
summary(agg1)
##
## Call:
## aggte(MP = did1, type = "simple")
##
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna. "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015>
##
##
## ATT Std. Error [ 95% Conf. Int.]
## -0.076 0.134 -0.3387 0.1866
##
##
## ---
## Signif. codes: `*' confidence band does not cover 0
##
## Control Group: Never Treated, Anticipation Periods: 0
## Estimation Method: Doubly Robust
Estimate the average treatment effect by groups and plot the result
agg2 <- aggte(did1, type = "group")
summary(agg2)
##
## Call:
## aggte(MP = did1, type = "group")
##
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna. "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015>
##
##
## Overall summary of ATT's based on group/cohort aggregation:
## ATT Std. Error [ 95% Conf. Int.]
## -0.0729 0.1073 -0.2832 0.1374
##
##
## Group Effects:
## Group Estimate Std. Error [95% Simult. Conf. Band]
## 2 0.0970 0.2531 -0.4951 0.6892
## 3 0.1001 0.3336 -0.6804 0.8806
## 4 -0.3852 0.2105 -0.8776 0.1073
## 5 -0.4304 0.2241 -0.9548 0.0939
## 6 0.2850 0.2312 -0.2559 0.8259
## ---
## Signif. codes: `*' confidence band does not cover 0
##
## Control Group: Never Treated, Anticipation Periods: 0
## Estimation Method: Doubly Robust
ggdid(agg2)
## `height` was translated to `width`.
Estimate the average time-dynamic effect and plot the result
agg3 <- aggte(did1, type = "dynamic")
summary(agg3)
##
## Call:
## aggte(MP = did1, type = "dynamic")
##
## Reference: Callaway, Brantly and Pedro H.C. Sant'Anna. "Difference-in-Differences with Multiple Time Periods." Journal of Econometrics, Vol. 225, No. 2, pp. 200-230, 2021. <https://doi.org/10.1016/j.jeconom.2020.12.001>, <https://arxiv.org/abs/1803.09015>
##
##
## Overall summary of ATT's based on event-study/dynamic aggregation:
## ATT Std. Error [ 95% Conf. Int.]
## -0.1128 0.1395 -0.3862 0.1607
##
##
## Dynamic Effects:
## Event time Estimate Std. Error [95% Simult. Conf. Band]
## -4 -0.4040 0.2835 -1.1532 0.3451
## -3 0.1986 0.2238 -0.3928 0.7900
## -2 -0.1713 0.1896 -0.6722 0.3297
## -1 0.6238 0.1517 0.2231 1.0246 *
## 0 0.3410 0.1221 0.0185 0.6636 *
## 1 -0.4796 0.1919 -0.9867 0.0275
## 2 -0.3210 0.1921 -0.8286 0.1867
## 3 0.0301 0.2491 -0.6283 0.6884
## 4 -0.1344 0.2807 -0.8762 0.6074
## ---
## Signif. codes: `*' confidence band does not cover 0
##
## Control Group: Never Treated, Anticipation Periods: 0
## Estimation Method: Doubly Robust
ggdid(agg3)