Dear Statalist-ers,

I hope this message finds everyone well. I am currently running some Difference-in-difference analysis (DiD) in Stata/MP 15.1. My data set has two time points: pre-test and post-test. It also has a treated group and a control group. The data are set-up in the long format.

I am trying to do some path analysis of how my treatment affects someone's happiness via its effect on their number of friends (the indirect-effect), using Stata's GSEM command. I then want to look at whether the impact of the treatment on happiness via number of friends is conditional on someone's level of disadvantage - what I think is a moderated mediation test. So, the outcome I am interested in is 'happiness'. The mediator I am interested in is 'number of friends'. The moderator I am interested in is an individual's level of 'disadvantage'.

I am hoping to find out if my current approach to this is correct, and to ask some specific questions if possible.

The variables are described here:

time: time (binary): 0 (pre-test) and 1 (post-test)
treatment: treated (binary): 0 (control) and 1 (treated)
outcome: happy (continuous): 0-11
mediator: nfriends (continuous): 0-10
moderator: disadvantage (continuous): 0 to 55.7
id: personal identifier
weight: is a weight variable accounting for attrition

The data are set-up in long format and set to panel data:

Code:
 xtset id time
My first step has been to look at the overall DiD score for treatment on happiness via its effect on number of friends. The DiD score is specified using an interaction between i.time##i.treatment

Code:
 gsem (nfriend <- i.time##i.treatment M1[id])(happy<- i.time##i.treatment nfriend M2[id])  [pw=weight], vce(robust) cov(M1[id]*M2[id]@0)
Using this model I then calculate the indirect effect of treatment on happiness via number of friends using the
Code:
nlcom
function:

Code:
 nlcom [nfriend]_b[1.time#1.treatment]*[happy]_b[nfriend]
Is this correct so far?

The second step has been to look at whether the effect of the treatment on happiness via number of friends is conditional someone's level of disadvantage. To test this I include an interaction-term between i.time##i.treatment##c.disadvantage:

Code:
 gsem (nfriend <- i.time##i.treatment##c.disadvantage  M1[id])(happy <- i.time##i.treatment##c.disadvantage nfriend M2[id]) if nms==0 [pw=weight], vce(robust) cov(M1[id]*M2[id]@0)
I would now like to test the indirect-effect of treatment on happiness via n of friends at the highest and lowest levels of disadvantage: that is, 0.7 and 55.7. Is the following code correct?

Indirect-effect at lowest (0.7) disadvantage:

Code:
 nlcom (_b[nfriend:1.time#1.treatment#c.disadvantage]+0.7*_b[nfriend:1.time#1.treatment#c.disadvantage])*_b[happy:nfriend]
Indirect-effect at highest (55.7) disadvantage:

Code:
 nlcom (_b[nfriend:1.time#1.treatment#c.disadvantage]+55.7*_b[nfriend:1.time#1.treatment#c.disadvantage])*_b[happy:nfriend]
I was wondering if this code is giving me what I want? The indirect-effects of treatment on happiness via friends at different levels of disadvantage?

I hope this is clear and the code comes out correctly (first time poster). Thanks so much in advance for your help.

James