Examples from the help file

The examples in help sgmediation2, run in full

The examples at the end of help sgmediation2, run in order: the basic test, then control variables, survey weights, cluster-robust standard errors, and bootstrapped confidence intervals. The overview page explains what each part of the output is.

use "https://tdmize.github.io/data/data/cda_ah4", clear
(cda_ah4.dta | Add Health Wave 4 - | )
drop if missing(health, edyrs, income, race, woman, age)
(131 observations deleted)
sgmediation2 health, iv(edyrs) mv(income)
Model with dv regressed on iv (path c)
 regress health edyrs , vce() 

      Source |       SS           df       MS      Number of obs   =     4,983
-------------+----------------------------------   F(1, 4981)      =    264.55
       Model |  210.169008         1  210.169008   Prob > F        =    0.0000
    Residual |   3957.0993     4,981  .794438728   R-squared       =    0.0504
-------------+----------------------------------   Adj R-squared   =    0.0502
       Total |  4167.26831     4,982  .836464936   Root MSE        =    .89131

------------------------------------------------------------------------------
      health | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
       edyrs |      0.089      0.005   16.265   0.000        0.078       0.100
       _cons |      2.393      0.079   30.241   0.000        2.238       2.548
------------------------------------------------------------------------------

Model with mediator regressed on iv (path a)
 regress income edyrs , vce() 

      Source |       SS           df       MS      Number of obs   =     4,983
-------------+----------------------------------   F(1, 4981)      =    502.00
       Model |  329198.443         1  329198.443   Prob > F        =    0.0000
    Residual |  3266426.93     4,981  655.777341   R-squared       =    0.0916
-------------+----------------------------------   Adj R-squared   =    0.0914
       Total |  3595625.38     4,982  721.723279   Root MSE        =    25.608

------------------------------------------------------------------------------
      income | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
       edyrs |      3.519      0.157   22.405   0.000        3.211       3.827
       _cons |    -17.676      2.273   -7.776   0.000      -22.133     -13.220
------------------------------------------------------------------------------

Model with dv regressed on mediator and iv (paths b and c')
 regress health income edyrs , vce() 

      Source |       SS           df       MS      Number of obs   =     4,983
-------------+----------------------------------   F(2, 4980)      =    168.16
       Model |   263.63457         2  131.817285   Prob > F        =    0.0000
    Residual |  3903.63374     4,980  .783862197   R-squared       =    0.0633
-------------+----------------------------------   Adj R-squared   =    0.0629
       Total |  4167.26831     4,982  .836464936   Root MSE        =    .88536

------------------------------------------------------------------------------
      health | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
      income |      0.004      0.000    8.259   0.000        0.003       0.005
       edyrs |      0.075      0.006   13.108   0.000        0.064       0.086
       _cons |      2.464      0.079   31.165   0.000        2.309       2.619
------------------------------------------------------------------------------

Sobel-Goodman Mediation Tests

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
               Sobel |      0.014       0.002       7.749       0.000 
              Aroian |      0.014       0.002       7.742       0.000 
             Goodman |      0.014       0.002       7.756       0.000 

Indirect, Direct, and Total Effects

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
       a_coefficient |      3.519       0.157      22.405       0.000 
       b_coefficient |      0.004       0.000       8.259       0.000 
 Indirect_effect_aXb |      0.014       0.002       7.749       0.000 
    Direct_effect_c' |      0.075       0.006      13.108       0.000 
      Total_effect_c |      0.089       0.005      16.265       0.000 


Proportion of total effect that is mediated:       0.160
Ratio of indirect to direct effect:                0.191
Ratio of total to direct effect:                   1.191

Add control variables:

sgmediation2 health, iv(edyrs) mv(income) cv(i.race i.woman age)
Model with dv regressed on iv (path c)
 regress health edyrs i.race i.woman age, vce() 

      Source |       SS           df       MS      Number of obs   =     4,983
-------------+----------------------------------   F(6, 4976)      =     56.32
       Model |  264.985975         6  44.1643291   Prob > F        =    0.0000
    Residual |  3902.28234     4,976  .784220727   R-squared       =    0.0636
-------------+----------------------------------   Adj R-squared   =    0.0625
       Total |  4167.26831     4,982  .836464936   Root MSE        =    .88556

----------------------------------------------------------------------------------
          health | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
           edyrs |      0.093      0.005   16.979   0.000        0.083       0.104
                 |
            race |
          Black  |     -0.111      0.030   -3.747   0.000       -0.169      -0.053
Native American  |     -0.171      0.145   -1.179   0.238       -0.454       0.113
          Asian  |     -0.201      0.073   -2.735   0.006       -0.345      -0.057
                 |
           woman |
          Woman  |     -0.172      0.025   -6.756   0.000       -0.222      -0.122
             age |     -0.013      0.007   -1.829   0.068       -0.026       0.001
           _cons |      2.817      0.214   13.179   0.000        2.398       3.236
----------------------------------------------------------------------------------

Model with mediator regressed on iv (path a)
 regress income edyrs i.race i.woman age, vce() 

      Source |       SS           df       MS      Number of obs   =     4,983
-------------+----------------------------------   F(6, 4976)      =    171.22
       Model |  615297.309         6  102549.551   Prob > F        =    0.0000
    Residual |  2980328.07     4,976  598.940528   R-squared       =    0.1711
-------------+----------------------------------   Adj R-squared   =    0.1701
       Total |  3595625.38     4,982  721.723279   Root MSE        =    24.473

----------------------------------------------------------------------------------
          income | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
           edyrs |      3.836      0.152   25.246   0.000        3.538       4.134
                 |
            race |
          Black  |     -5.922      0.821   -7.215   0.000       -7.531      -4.313
Native American  |      0.113      3.997    0.028   0.977       -7.723       7.949
          Asian  |      4.917      2.030    2.422   0.015        0.937       8.897
                 |
           woman |
          Woman  |    -13.135      0.704  -18.664   0.000      -14.515     -11.755
             age |      1.167      0.192    6.086   0.000        0.791       1.543
           _cons |    -47.033      5.906   -7.963   0.000      -58.612     -35.454
----------------------------------------------------------------------------------

Model with dv regressed on mediator and iv (paths b and c')
 regress health income edyrs i.race i.woman age, vce() 

      Source |       SS           df       MS      Number of obs   =     4,983
-------------+----------------------------------   F(7, 4975)      =     55.12
       Model |  299.936161         7   42.848023   Prob > F        =    0.0000
    Residual |  3867.33215     4,975  .777353196   R-squared       =    0.0720
-------------+----------------------------------   Adj R-squared   =    0.0707
       Total |  4167.26831     4,982  .836464936   Root MSE        =    .88168

----------------------------------------------------------------------------------
          health | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
          income |      0.003      0.001    6.705   0.000        0.002       0.004
           edyrs |      0.080      0.006   13.797   0.000        0.069       0.092
                 |
            race |
          Black  |     -0.091      0.030   -3.061   0.002       -0.149      -0.033
Native American  |     -0.171      0.144   -1.187   0.235       -0.453       0.111
          Asian  |     -0.218      0.073   -2.975   0.003       -0.361      -0.074
                 |
           woman |
          Woman  |     -0.127      0.026   -4.845   0.000       -0.178      -0.076
             age |     -0.017      0.007   -2.406   0.016       -0.030      -0.003
           _cons |      2.978      0.214   13.906   0.000        2.558       3.398
----------------------------------------------------------------------------------

Sobel-Goodman Mediation Tests

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
               Sobel |      0.013       0.002       6.481       0.000 
              Aroian |      0.013       0.002       6.476       0.000 
             Goodman |      0.013       0.002       6.485       0.000 

Indirect, Direct, and Total Effects

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
       a_coefficient |      3.836       0.152      25.246       0.000 
       b_coefficient |      0.003       0.001       6.705       0.000 
 Indirect_effect_aXb |      0.013       0.002       6.481       0.000 
    Direct_effect_c' |      0.080       0.006      13.797       0.000 
      Total_effect_c |      0.093       0.005      16.979       0.000 


Proportion of total effect that is mediated:       0.141
Ratio of indirect to direct effect:                0.164
Ratio of total to direct effect:                   1.164

Add survey weights already set with svyset:

sgmediation2 health, iv(edyrs) mv(income) cv(i.race i.woman age) prefix(svy:)
Model with dv regressed on iv (path c)
svy: regress health edyrs i.race i.woman age, vce() 
(running regress on estimation sample)

Survey: Linear regression

Number of strata =   1                            Number of obs   =      4,983
Number of PSUs   = 132                            Population size = 21,417,540
                                                  Design df       =        131
                                                  F(6, 126)       =      65.63
                                                  Prob > F        =     0.0000
                                                  R-squared       =     0.0714

----------------------------------------------------------------------------------
                 |             Linearized
          health | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
           edyrs |      0.101      0.006   16.787   0.000        0.089       0.113
                 |
            race |
          Black  |     -0.093      0.042   -2.204   0.029       -0.176      -0.009
Native American  |     -0.105      0.088   -1.196   0.234       -0.280       0.069
          Asian  |     -0.255      0.086   -2.969   0.004       -0.424      -0.085
                 |
           woman |
          Woman  |     -0.161      0.027   -5.856   0.000       -0.215      -0.106
             age |     -0.011      0.008   -1.311   0.192       -0.028       0.006
           _cons |      2.648      0.280    9.475   0.000        2.095       3.201
----------------------------------------------------------------------------------

Model with mediator regressed on iv (path a)
svy: regress income edyrs i.race i.woman age, vce() 
(running regress on estimation sample)

Survey: Linear regression

Number of strata =   1                            Number of obs   =      4,983
Number of PSUs   = 132                            Population size = 21,417,540
                                                  Design df       =        131
                                                  F(6, 126)       =     118.21
                                                  Prob > F        =     0.0000
                                                  R-squared       =     0.1706

----------------------------------------------------------------------------------
                 |             Linearized
          income | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
           edyrs |      3.690      0.195   18.899   0.000        3.304       4.077
                 |
            race |
          Black  |     -7.333      1.051   -6.979   0.000       -9.411      -5.254
Native American  |      3.006      3.656    0.822   0.412       -4.225      10.238
          Asian  |      4.844      3.015    1.607   0.111       -1.120      10.807
                 |
           woman |
          Woman  |    -13.312      0.849  -15.682   0.000      -14.991     -11.633
             age |      1.230      0.267    4.608   0.000        0.702       1.759
           _cons |    -46.830      8.828   -5.305   0.000      -64.295     -29.366
----------------------------------------------------------------------------------

Model with dv regressed on mediator and iv (paths b and c')
svy: regress health income edyrs i.race i.woman age, vce() 
(running regress on estimation sample)

Survey: Linear regression

Number of strata =   1                            Number of obs   =      4,983
Number of PSUs   = 132                            Population size = 21,417,540
                                                  Design df       =        131
                                                  F(7, 125)       =      57.38
                                                  Prob > F        =     0.0000
                                                  R-squared       =     0.0778

----------------------------------------------------------------------------------
                 |             Linearized
          health | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
          income |      0.003      0.001    3.919   0.000        0.001       0.004
           edyrs |      0.090      0.007   13.012   0.000        0.076       0.104
                 |
            race |
          Black  |     -0.071      0.041   -1.718   0.088       -0.153       0.011
Native American  |     -0.114      0.093   -1.234   0.220       -0.298       0.069
          Asian  |     -0.269      0.086   -3.143   0.002       -0.438      -0.100
                 |
           woman |
          Woman  |     -0.121      0.027   -4.441   0.000       -0.175      -0.067
             age |     -0.015      0.008   -1.761   0.081       -0.031       0.002
           _cons |      2.787      0.278   10.010   0.000        2.236       3.338
----------------------------------------------------------------------------------

Sobel-Goodman Mediation Tests

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
               Sobel |      0.011       0.003       3.837       0.000 
              Aroian |      0.011       0.003       3.832       0.000 
             Goodman |      0.011       0.003       3.842       0.000 

Indirect, Direct, and Total Effects

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
       a_coefficient |      3.690       0.195      18.899       0.000 
       b_coefficient |      0.003       0.001       3.919       0.000 
 Indirect_effect_aXb |      0.011       0.003       3.837       0.000 
    Direct_effect_c' |      0.090       0.007      13.012       0.000 
      Total_effect_c |      0.101       0.006      16.787       0.000 


Proportion of total effect that is mediated:       0.108
Ratio of indirect to direct effect:                0.121
Ratio of total to direct effect:                   1.121

Obtain cluster-robust variance estimates for clustering on occcat:

sgmediation2 health, iv(edyrs) mv(income) cv(i.race i.woman age) vce(cluster occcat)
Model with dv regressed on iv (path c)
 regress health edyrs i.race i.woman age, vce(cluster occcat) 

Linear regression                               Number of obs     =      4,959
                                                F(6, 8)           =     431.43
                                                Prob > F          =     0.0000
                                                R-squared         =     0.0638
                                                Root MSE          =     .88549

                                     (Std. err. adjusted for 9 clusters in occcat)
----------------------------------------------------------------------------------
                 |               Robust
          health | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
           edyrs |      0.094      0.005   19.542   0.000        0.083       0.105
                 |
            race |
          Black  |     -0.110      0.040   -2.738   0.026       -0.203      -0.017
Native American  |     -0.169      0.109   -1.558   0.158       -0.420       0.081
          Asian  |     -0.199      0.116   -1.718   0.124       -0.465       0.068
                 |
           woman |
          Woman  |     -0.172      0.011  -15.251   0.000       -0.198      -0.146
             age |     -0.012      0.006   -2.098   0.069       -0.026       0.001
           _cons |      2.806      0.209   13.419   0.000        2.324       3.289
----------------------------------------------------------------------------------

Model with mediator regressed on iv (path a)
 regress income edyrs i.race i.woman age, vce(cluster occcat) 

Linear regression                               Number of obs     =      4,959
                                                F(6, 8)           =     645.55
                                                Prob > F          =     0.0000
                                                R-squared         =     0.1711
                                                Root MSE          =     24.512

                                     (Std. err. adjusted for 9 clusters in occcat)
----------------------------------------------------------------------------------
                 |               Robust
          income | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
           edyrs |      3.838      0.312   12.295   0.000        3.118       4.557
                 |
            race |
          Black  |     -5.885      1.132   -5.197   0.001       -8.497      -3.274
Native American  |      0.112      4.084    0.027   0.979       -9.306       9.529
          Asian  |      4.964      2.969    1.672   0.133       -1.883      11.811
                 |
           woman |
          Woman  |    -13.179      0.697  -18.920   0.000      -14.785     -11.572
             age |      1.160      0.307    3.776   0.005        0.452       1.869
           _cons |    -46.824     11.890   -3.938   0.004      -74.242     -19.405
----------------------------------------------------------------------------------

Model with dv regressed on mediator and iv (paths b and c')
 regress health income edyrs i.race i.woman age, vce(cluster occcat) 

Linear regression                               Number of obs     =      4,959
                                                F(7, 8)           =     753.00
                                                Prob > F          =     0.0000
                                                R-squared         =     0.0721
                                                Root MSE          =     .88161

                                     (Std. err. adjusted for 9 clusters in occcat)
----------------------------------------------------------------------------------
                 |               Robust
          health | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-----------------+----------------------------------------------------------------
          income |      0.003      0.001    6.769   0.000        0.002       0.005
           edyrs |      0.080      0.004   18.574   0.000        0.070       0.090
                 |
            race |
          Black  |     -0.090      0.044   -2.061   0.073       -0.191       0.011
Native American  |     -0.170      0.099   -1.720   0.124       -0.397       0.058
          Asian  |     -0.216      0.122   -1.763   0.116       -0.497       0.066
                 |
           woman |
          Woman  |     -0.127      0.012  -10.308   0.000       -0.155      -0.099
             age |     -0.016      0.006   -2.734   0.026       -0.030      -0.003
           _cons |      2.966      0.203   14.582   0.000        2.497       3.436
----------------------------------------------------------------------------------

Sobel-Goodman Mediation Tests

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
               Sobel |      0.013       0.002       5.930       0.000 
              Aroian |      0.013       0.002       5.915       0.000 
             Goodman |      0.013       0.002       5.945       0.000 

Indirect, Direct, and Total Effects

                     |        Est     Std_err           z       P>|z| 
---------------------+-----------------------------------------------
       a_coefficient |      3.838       0.312      12.295       0.000 
       b_coefficient |      0.003       0.001       6.769       0.000 
 Indirect_effect_aXb |      0.013       0.002       5.930       0.000 
    Direct_effect_c' |      0.080       0.004      18.574       0.000 
      Total_effect_c |      0.094       0.005      19.542       0.000 


Proportion of total effect that is mediated:       0.140
Ratio of indirect to direct effect:                0.163
Ratio of total to direct effect:                   1.163

Use bootstrapping to obtain standard errors and confidence intervals. 1,000 replications are recommended for serious use; 100 are used here for illustration.

bootstrap r(ind_eff) r(dir_eff) r(tot_eff), reps(100) seed(2026): ///
    sgmediation2 health, iv(edyrs) mv(income) cv(i.race i.woman age)
(running sgmediation2 on estimation sample)
 regress health edyrs i.race i.woman age, vce() 
 regress income edyrs i.race i.woman age, vce() 
 regress health income edyrs i.race i.woman age, vce() 

Bootstrap replications (100): .........10.........20.........30.........40.........50.........60....
> .....70.........80.........90.........100 done

Bootstrap results                                        Number of obs = 4,983
                                                         Replications  =   100

      Command: sgmediation2 health, iv(edyrs) mv(income) cv(i.race i.woman age)
        _bs_1: r(ind_eff)
        _bs_2: r(dir_eff)
        _bs_3: r(tot_eff)

------------------------------------------------------------------------------
             |   Observed   Bootstrap                         Normal-based
             | coefficient  std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
       _bs_1 |      0.013      0.002    7.532   0.000        0.010       0.017
       _bs_2 |      0.080      0.005   14.984   0.000        0.070       0.091
       _bs_3 |      0.093      0.005   17.746   0.000        0.083       0.104
------------------------------------------------------------------------------

Obtain bias-corrected and percentile confidence intervals based on the bootstrapped samples:

estat bootstrap, bc percentile
Bootstrap results                               Number of obs     =      4,983
                                                Replications      =        100

      Command: sgmediation2 health, iv(edyrs) mv(income) cv(i.race i.woman age)
        _bs_1: r(ind_eff)
        _bs_2: r(dir_eff)
        _bs_3: r(tot_eff)

------------------------------------------------------------------------------
             |    Observed               Bootstrap
             | coefficient       Bias    std. err.  [95% conf. interval]
-------------+----------------------------------------------------------------
       _bs_1 |   .01313674   .0001615   .00174404    .0100487   .0166817   (P)
             |                                       .0095707   .0165165  (BC)
       _bs_2 |   .08021862   .0003122   .00535347    .0689332   .0904137   (P)
             |                                       .0676583     .08868  (BC)
       _bs_3 |   .09335536   .0004736   .00526056    .0822798   .1035904   (P)
             |                                       .0815194   .1035485  (BC)
------------------------------------------------------------------------------
Key:  P: Percentile
     BC: Bias-corrected
Back to top