I couldn’t find a solution on how to include individual and time fixed effects in a mediation analysis using stata.
I found solutions on how to include individual and time fixed effects in a regression when using sem (see 3 in the program example). The sem command using the data in wide format shows the same results as the xtreg command (see 1) and the same estimates of the coefficients using demeaned variables (see 2 -> however, when first demeaning the variables by hand and then running the analysis it gives the correct coefficients but the wrong standard errors).
But how would the program look like when calculating a mediation analysis?
I first calculated the mediation analysis with the demeaned variables (see 4) to be able to check afterwards whether my results of the coefficients of the mediation analysis including individual and time fixed are correct. I then transformed the data to wide format and included alpha in the sem command to capture individual fixed effects.
However, when comparing the results of the mediation analysis in the wide format (see 5) and the mediation analysis with the demeaned variables (see 4) the coefficients differ strongly.
Could someone tell me, how to correct the program? Or should I use another approach e.g. lagged dependent variable model?
I found solutions on how to include individual and time fixed effects in a regression when using sem (see 3 in the program example). The sem command using the data in wide format shows the same results as the xtreg command (see 1) and the same estimates of the coefficients using demeaned variables (see 2 -> however, when first demeaning the variables by hand and then running the analysis it gives the correct coefficients but the wrong standard errors).
But how would the program look like when calculating a mediation analysis?
I first calculated the mediation analysis with the demeaned variables (see 4) to be able to check afterwards whether my results of the coefficients of the mediation analysis including individual and time fixed are correct. I then transformed the data to wide format and included alpha in the sem command to capture individual fixed effects.
However, when comparing the results of the mediation analysis in the wide format (see 5) and the mediation analysis with the demeaned variables (see 4) the coefficients differ strongly.
Could someone tell me, how to correct the program? Or should I use another approach e.g. lagged dependent variable model?
Code:
** import the dataset
use https://www3.nd.edu/~rwilliam/statafiles/wages, clear
keep wks lwage union id t
**************************************************
***Regression using two fixed effects (id & t) ***
**************************************************
/* dependent/endogenous variable: wks
independent/exogeneous variable: union lwage */
** (1) Fixed effects regression using xtreg
xtset id t
xtreg wks lwage union i.t, fe
** (2) Fixed effects regression using demeaning (right coefficients but wrong standard errors)
foreach var in wks lwage union {
bys id: egen midnum`var'= mean(`var')
bys t: egen myear`var'= mean(`var')
gen md`var'=`var' - midnum`var' - myear`var'
}
regress mdwks mdlwage mdunion
drop midnum* myear* md*
** (3) Fixed effects regression using sem and data in wide format (also see the command xtdpdml)
preserve
*reshape the data to wide format
xtset, clear
reshape wide wks union lwage, i(id) j(t)
sem (wks1 <- lwage1@b1 union1@b2 Alpha@1 _cons) ///
(wks2 <- lwage2@b1 union2@b2 Alpha@1 _cons) ///
(wks3 <- lwage3@b1 union3@b2 Alpha@1 _cons) ///
(wks4 <- lwage4@b1 union4@b2 Alpha@1 _cons) ///
(wks5 <- lwage5@b1 union5@b2 Alpha@1 _cons) ///
(wks6 <- lwage6@b1 union6@b2 Alpha@1 _cons) ///
(wks7 <- lwage7@b1 union7@b2 Alpha@1 _cons), ///
var(Alpha) var(e.wks1@v1 e.wks2@v1 e.wks3@v1 e.wks4@v1 e.wks5@v1 e.wks6@v1 e.wks7@v1) ///
iterate(250) technique(nr 25 bhhh 25) noxconditional
restore
**********************************************************
***Mediation analysis using two fixed effects (id & t) ***
**********************************************************
/* dependent/endogenous variable: wks
independent/exogeneous variable: union
mediator variable: lwage */
** (4) Fixed effects mediation analysis using demeaning (right coefficients but wrong standard errors)
foreach var in wks lwage union {
bys id: egen midnum`var'= mean(`var')
bys t: egen myear`var'= mean(`var')
gen md`var'=`var' - midnum`var' - myear`var'
}
sem (mdlwage mdunion -> mdwks, ) (mdunion -> mdlwage, )
drop mid* myear* md*
** (5) Fixed effects mediation analysis using sem and data in wide format
preserve
*reshape the data to wide format
xtset, clear
reshape wide wks union lwage, i(id) j(t)
sem (lwage1@b1 union1@b2 Alpha@1 _cons-> wks1, ) (union1@b3 Alpha@1 _cons-> lwage1, ) ///
(lwage2@b1 union2@b2 Alpha@1 _cons-> wks2, ) (union2@b3 Alpha@1 _cons-> lwage2, ) ///
(lwage3@b1 union3@b2 Alpha@1 _cons-> wks3, ) (union3@b3 Alpha@1 _cons-> lwage3, ) ///
(lwage4@b1 union4@b2 Alpha@1 _cons-> wks4, ) (union4@b3 Alpha@1 _cons-> lwage4, ) ///
(lwage5@b1 union5@b2 Alpha@1 _cons-> wks5, ) (union5@b3 Alpha@1 _cons-> lwage5, ) ///
(lwage6@b1 union6@b2 Alpha@1 _cons-> wks6, ) (union6@b3 Alpha@1 _cons-> lwage6, ) ///
(lwage7@b1 union7@b2 Alpha@1 _cons-> wks7, ) (union7@b3 Alpha@1 _cons-> lwage7, ) , ///
var(e.wks1@v1 e.wks2@v1 e.wks3@v1 e.wks4@v1 e.wks5@v1 e.wks6@v1 e.wks7@v1) ///
iterate(250) technique(nr 25 bhhh 25) noxconditional
restore
