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) # Add the tidyverse package to my current library.
library(haven) # Handle labelled data.
library(texreg) #output result
library(splitstackshape) #transform wide data (with stacked variables) to long data
library(plm) #linear models for panel data
library(did) #for staggered difference in difference

##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.3057       -0.1934      1.4663  
##      2    3  -0.1154     0.3978       -1.1951      0.9643  
##      2    4  -0.0283     0.2940       -0.8264      0.7699  
##      2    5   0.1268     0.2675       -0.5992      0.8528  
##      2    6  -0.1344     0.2904       -0.9226      0.6538  
##      3    2   0.1890     0.3604       -0.7894      1.1673  
##      3    3   0.4683     0.3533       -0.4907      1.4273  
##      3    4   0.2713     0.3312       -0.6277      1.1704  
##      3    5  -0.3048     0.3994       -1.3891      0.7794  
##      3    6  -0.0345     0.3853       -1.0804      1.0114  
##      4    2  -0.5418     0.3333       -1.4466      0.3629  
##      4    3   0.6849     0.2663       -0.0379      1.4076  
##      4    4   0.4158     0.2509       -0.2652      1.0968  
##      4    5  -1.0459     0.3504       -1.9970     -0.0949 *
##      4    6  -0.5254     0.2493       -1.2022      0.1514  
##      5    2   0.1981     0.3666       -0.7971      1.1933  
##      5    3   0.2336     0.3253       -0.6493      1.1166  
##      5    4   0.5715     0.2265       -0.0433      1.1864  
##      5    5   0.0140     0.2907       -0.7752      0.8032  
##      5    6  -0.8749     0.3806       -1.9080      0.1583  
##      6    2  -0.4040     0.3056       -1.2335      0.4254  
##      6    3   0.1991     0.3296       -0.6955      1.0937  
##      6    4  -0.2025     0.3175       -1.0644      0.6594  
##      6    5   0.9996     0.2817        0.2349      1.7643 *
##      6    6   0.2850     0.2216       -0.3166      0.8866  
## ---
## 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.1376    -0.3456      0.1936 
## 
## 
## ---
## 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.1119    -0.2923      0.1464 
## 
## 
## Group Effects:
##  Group Estimate Std. Error [95% Simult.  Conf. Band] 
##      2   0.0970     0.2291       -0.4408      0.6349 
##      3   0.1001     0.3336       -0.6830      0.8832 
##      4  -0.3852     0.2139       -0.8873      0.1170 
##      5  -0.4304     0.2500       -1.0173      0.1565 
##      6   0.2850     0.2244       -0.2418      0.8119 
## ---
## 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.1394     -0.386      0.1604 
## 
## 
## Dynamic Effects:
##  Event time Estimate Std. Error [95% Simult.  Conf. Band]  
##          -4  -0.4040     0.3303       -1.2703      0.4622  
##          -3   0.1986     0.2444       -0.4424      0.8395  
##          -2  -0.1713     0.1896       -0.6684      0.3259  
##          -1   0.6238     0.1436        0.2473      1.0004 *
##           0   0.3410     0.1266        0.0089      0.6732 *
##           1  -0.4796     0.1896       -0.9768      0.0177  
##           2  -0.3210     0.1846       -0.8052      0.1632  
##           3   0.0301     0.2662       -0.6681      0.7282  
##           4  -0.1344     0.2857       -0.8837      0.6150  
## ---
## Signif. codes: `*' confidence band does not cover 0
## 
## Control Group:  Never Treated,  Anticipation Periods:  0
## Estimation Method:  Doubly Robust
ggdid(agg3)