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

No. 1

Question

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

Answer for step 1

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 )
       ) 

Answer for step 2

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.3136       -0.2356      1.5084  
##      2    3  -0.1154     0.3849       -1.1857      0.9549  
##      2    4  -0.0283     0.2857       -0.8228      0.7662  
##      2    5   0.1268     0.2827       -0.6594      0.9130  
##      2    6  -0.1344     0.2768       -0.9042      0.6355  
##      3    2   0.1890     0.3557       -0.8001      1.1780  
##      3    3   0.4683     0.3867       -0.6071      1.5436  
##      3    4   0.2713     0.3694       -0.7559      1.2986  
##      3    5  -0.3048     0.3688       -1.3304      0.7208  
##      3    6  -0.0345     0.3911       -1.1222      1.0533  
##      4    2  -0.5418     0.3550       -1.5292      0.4455  
##      4    3   0.6849     0.2712       -0.0692      1.4390  
##      4    4   0.4158     0.2378       -0.2455      1.0772  
##      4    5  -1.0459     0.3523       -2.0257     -0.0661 *
##      4    6  -0.5254     0.2613       -1.2522      0.2014  
##      5    2   0.1981     0.3728       -0.8387      1.2349  
##      5    3   0.2336     0.3084       -0.6241      1.0913  
##      5    4   0.5715     0.2110       -0.0153      1.1584  
##      5    5   0.0140     0.2670       -0.7285      0.7564  
##      5    6  -0.8749     0.3647       -1.8891      0.1394  
##      6    2  -0.4040     0.3280       -1.3163      0.5082  
##      6    3   0.1991     0.3193       -0.6889      1.0870  
##      6    4  -0.2025     0.3112       -1.0679      0.6629  
##      6    5   0.9996     0.2846        0.2080      1.7912 *
##      6    6   0.2850     0.2123       -0.3053      0.8753  
## ---
## 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

Answer for step 3

ggdid(did1)

No. 2

Question

Estimate the weighted average effect of all group-time specific treatment effects

Answer

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.1335    -0.3376      0.1855 
## 
## 
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust

No. 3

Question

Estimate the average treatment effect by groups and plot the result

Answer

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.1133     -0.295      0.1491 
## 
## 
## Group Effects:
##  Group Estimate Std. Error [95% Simult.  Conf. Band] 
##      2   0.0970     0.2334       -0.4866      0.6807 
##      3   0.1001     0.3298       -0.7245      0.9247 
##      4  -0.3852     0.2036       -0.8943      0.1240 
##      5  -0.4304     0.2361       -1.0207      0.1598 
##      6   0.2850     0.2224       -0.2711      0.8412 
## ---
## 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`.

No. 4

Question

Estimate the average time-dynamic effect and plot the result

Answer

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.142    -0.3911      0.1655 
## 
## 
## Dynamic Effects:
##  Event time Estimate Std. Error [95% Simult.  Conf. Band]  
##          -4  -0.4040     0.3112       -1.2121      0.4040  
##          -3   0.1986     0.2397       -0.4238      0.8210  
##          -2  -0.1713     0.1833       -0.6471      0.3046  
##          -1   0.6238     0.1442        0.2494      0.9983 *
##           0   0.3410     0.1214        0.0257      0.6564 *
##           1  -0.4796     0.1820       -0.9521     -0.0071 *
##           2  -0.3210     0.1888       -0.8112      0.1693  
##           3   0.0301     0.2525       -0.6258      0.6859  
##           4  -0.1344     0.2912       -0.8905      0.6218  
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust
ggdid(agg3)