Here, we want to examine the extent to which the number of best friends (z) mediates the association between parental mental illness (x) and a poor grade point average (y). We also include sex as a confounder (z).
Poor grade point average does not exist as a variable in the dataset, so first we will have to create it based on the variable gpa:
gen gpa_dic=gpa |
recode gpa_dic (1.0/2.0=1) (2.1/5.0=0) |
| Dataset |
| StataData1.dta |
| Variable name | bestfriends |
| Variable label | Number of best friends (Age 15, Year 1985) |
| Value labels | 1=No best friends 2=One best friend 3=Two best friends 4=Three best friends 5=Four or more best friends |
| Variable name | gpa |
| Variable label | Grade point average (Age 15, Year 1985) |
| Value labels | N/A |
| Variable name | parmental |
| Variable label | Parental mental illness (Ages 0-14, Years 1970-1984) |
| Value labels | 0=No 1=Yes |
| Variable name | sex |
| Variable label | Sex |
| Value labels | 0=Man 1=Woman |
Define the analytical sample
We start by defining the analytical sample:
gen pop_mediate1=1 if bestfriends!=. & gpa_dic!=. & parmental!=. & sex!=. |
Let us have a quick look at the variables:
sum bestfriends gpa_dic parmental sex if pop_mediate1==1 |

Regression models
Now, we can run the regression model with the khb command.
khb logit gpa_dic parmental || bestfriends sex if pop_mediate1==1, summary disentangle or |

The model without any z-variables (the “reduced” model) shows that there is a positive (OR=2.09) and statistically significant association (95 % CI=1.57 to 2.79) between parmental and gpa_dic. In other words, individuals whose parents suffered from mental illness have higher odds of obtaining a poor grade point average. In the model where the z-variables bestfriends and sex are included (the “full” model), the association is weakened but remains stastistically significant (OR=1.82, 95% CI=1.36 to 2.43).
In the table called Summary of confounding, we can see that the amount of the association explained by the z-variables (in this case, bestfriends and sex), is 19%. This amount is specified further in the table called Components of Difference. Here, we can see that the inclusion of bestfriends reduces the association by 26%. There is nonetheless a negative contribution of sex (-6%), meaning that the inclusion of this variable strengthens the association between parental mental illness and poor grade point average. This could (but does not have to) be an indication of an interaction effect by sex (which could be further examined with interaction analysis).