Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing results of indirect effects after SEM

    Dear all,

    I am trying to format the results after Structural Equation Model (SEM). The results of direct effects can easily be called by programs like 'estout' or 'esttab' or outreg2 and be formatted according to the taste. The problem I am facing is after I run 'teffects' for indirect and total effects. The information stored after 'teffects' command are the followings:

    Code:
    return list
    
    scalars:
               r(N_groups) =  1
    
    matrices:
                r(V_total) :  35 x 35
                  r(total) :  1 x 35
             r(V_indirect) :  35 x 35
               r(indirect) :  1 x 35
               r(V_direct) :  35 x 35
                 r(direct) :  1 x 35
                   r(nobs) :  1 x 1
    The usual programmes like estout, outreg2, do not recognise these matrices as they read matrices from the original model. Can anyone suggest, after 'teffects' command, how to access the indirect effect coefficient, 95% CI and 'p' values and be formated as per requirements.

    Your help is much appreciated.
    Regards,
    Roman
    Roman

  • #2
    Roman,

    Were you able to find an answer to this question?

    Also, do you know anything about how to get estout to handle results after sem multiple group analysis?

    Thanks.

    --David

    Comment


    • #3
      I should add: when running estout after sem multiple group analysis, I get this error:

      . estout test, cells(b(star fmt(3)) se(par fmt(2)))
      1bn: operator invalid

      "1bn:" is the prefix that labels the coefficients for group 1 in the multiple group analysis.

      Thanks for any help anyone can offer.

      --David

      Comment


      • #4

        It seems that estadd should place the r matrices from the estat teffects postestimation command into the stored results of e (which are stored after an sem multiple group command). I don't get an error when I try to do this, but I haven't been able to get estout to include the added stats in the table as I would like.

        Ideally, I would place the total and indirect coefficients in columns adjacent to the direct effects. Estout handles the direct effects from the sem regression just fine (sorry for the earlier email on this -- I found out that I had an old verison of estout installed and after upgrading to the new version, estout handled the results from an sem multiple group command very well). So I have one column with all the variables and coefficients for each model and for each of the two groups in my sem analysis. So far so good.

        But now: how to place the coefficients for the total and direct effects, which are available from the estat teffects postestimation command, as columns right next to the appropriate direct effect.

        It is true that estat teffects does print the direct effects as well as the indirect and total effects. So perhaps rather than "merging" e and r class results, I should try to get all the r class results into something estout can read.

        Any guidance would be greatly appreciated.

        --David



        But somehow need to inform estout what these new

        Comment


        • #5
          This may be clunkier than necessary but I think it is in the ballpark. This could all be put in a do file and run and you could just keep changing the sem command, or you can break the te_ programs into separate ado files. Note that you could add things to the ereturns, e.g. the model chi-square values if you wanted to. Since such things are redundant you may just want to store them with the direct results.

          Code:
          capture program drop te_direct
          program te_direct, eclass
              quietly estat teffects
              mat b = r(direct)
              mat V = r(V_direct)
              local N = e(N)
              ereturn post b V, obs(`N')
              ereturn local cmd te_direct 
          end
          
          capture program drop te_indirect
          program te_indirect, eclass
              quietly estat teffects
              mat b = r(indirect)
              mat V = r(V_indirect)
              ereturn post b V
              ereturn local cmd te_indirect 
          end
          
          capture program drop te_total
          program te_total, eclass
              quietly estat teffects
              mat b = r(total)
              mat V = r(V_total)
              ereturn post b V
              ereturn local cmd te_total 
          end
          
          
          webuse nhanes2f, clear
          sem (height black female -> weight)(female black -> height)
          est store main
          te_direct
          est store direct
          est restore main
          te_indirect
          est store indirect
          est restore main
          te_total
          est store total
          esttab direct indirect total, mtitles(direct indirect total)
          
          . esttab direct indirect total, mtitles(direct indirect total)
          
          ------------------------------------------------------------
                                (1)             (2)             (3)   
                             direct        indirect           total   
          ------------------------------------------------------------
          height                                                      
          black               0.398               0           0.398   
                             (1.80)             (.)          (1.80)   
          
          female             -13.52***            0          -13.52***
                           (-99.32)             (.)        (-99.32)   
          ------------------------------------------------------------
          weight                                                      
          height              0.662***            0           0.662***
                            (34.69)             (.)         (34.69)   
          
          black               3.490***        0.264           3.754***
                             (8.11)          (1.79)          (8.26)   
          
          female             -2.664***       -8.955***       -11.62***
                            (-7.21)        (-32.75)        (-41.62)   
          ------------------------------------------------------------
          N                   10337                                   
          ------------------------------------------------------------
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 18.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6

            That works perfectly. In an age of divisiveness, Prof Williams has gone the extra mile to bring the e class and the r class closer together. That is a beautiful thing.

            Thanks again to my now even more highly esteemed colleague!

            --David

            Comment


            • #7
              Getting back to Roman's original Q, you might want to tweak the te_ programs to include a replay option:

              Code:
              capture program drop te_direct
              program te_direct, eclass
                  if replay() {
                      ereturn display
                      exit
                  }
                  quietly estat teffects
                  mat b = r(direct)
                  mat V = r(V_direct)
                  local N = e(N)
                  ereturn post b V, obs(`N')
                  ereturn local cmd te_direct
                  est store direct
              end
              
              capture program drop te_indirect
              program te_indirect, eclass
                  if replay() {
                      ereturn display
                      exit
                  }
                  quietly estat teffects
                  mat b = r(indirect)
                  mat V = r(V_indirect)
                  ereturn post b V
                  ereturn local cmd te_indirect
                  est store indirect
              end
              
              capture program drop te_total
              program te_total, eclass
                  if replay() {
                      ereturn display
                      exit
                  }
                  quietly estat teffects
                  mat b = r(total)
                  mat V = r(V_total)
                  ereturn post b V
                  ereturn local cmd te_total
                  est store total
              end
              
              
              webuse nhanes2f, clear
              sem (height black female -> weight)(female black -> height)
              est store main
              te_direct
              
              est restore main
              te_indirect
              
              est restore main
              te_total
              
              esttab direct indirect total, mtitles(direct indirect total)
              
              est replay direct
              est replay indirect
              est replay total
              Code:
              . est replay direct
              
              -------------------------------------------------------------------------------------------------------------------------------------------------------------------
              Model direct
              -------------------------------------------------------------------------------------------------------------------------------------------------------------------
              ------------------------------------------------------------------------------
                           |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
              height       |
                     black |   .3979381   .2216452     1.80   0.073    -.0364786    .8323548
                    female |  -13.51765   .1360978   -99.32   0.000     -13.7844   -13.25091
              -------------+----------------------------------------------------------------
              weight       |
                    height |   .6624414   .0190954    34.69   0.000     .6250151    .6998677
                     black |   3.490113   .4303801     8.11   0.000     2.646583    4.333642
                    female |  -2.664485   .3693839    -7.21   0.000    -3.388464   -1.940505
              ------------------------------------------------------------------------------
              
              . est replay indirect
              
              -------------------------------------------------------------------------------------------------------------------------------------------------------------------
              Model indirect
              -------------------------------------------------------------------------------------------------------------------------------------------------------------------
              ------------------------------------------------------------------------------
                           |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
              height       |
                     black |          0  (omitted)
                    female |          0  (omitted)
              -------------+----------------------------------------------------------------
              weight       |
                    height |          0  (omitted)
                     black |   .2636107   .1470235     1.79   0.073    -.0245501    .5517714
                    female |  -8.954654   .2734169   -32.75   0.000    -9.490541   -8.418767
              ------------------------------------------------------------------------------
              
              . est replay total
              
              -------------------------------------------------------------------------------------------------------------------------------------------------------------------
              Model total
              -------------------------------------------------------------------------------------------------------------------------------------------------------------------
              ------------------------------------------------------------------------------
                           |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
              height       |
                     black |   .3979381   .2216452     1.80   0.073    -.0364786    .8323548
                    female |  -13.51765   .1360978   -99.32   0.000     -13.7844   -13.25091
              -------------+----------------------------------------------------------------
              weight       |
                    height |   .6624414   .0190954    34.69   0.000     .6250151    .6998677
                     black |   3.753723   .4546729     8.26   0.000     2.862581    4.644866
                    female |  -11.61914   .2791849   -41.62   0.000    -12.16633   -11.07195
              ------------------------------------------------------------------------------
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              StataNow Version: 18.5 MP (2 processor)

              EMAIL: [email protected]
              WWW: https://www3.nd.edu/~rwilliam

              Comment


              • #8
                Many thanks for the solution Richard. I actually forgot how I went about solving this problem when I made this post one and half year back. But your solution definitely is a relief for the future.
                All the best.
                Roman

                Comment


                • #9
                  Just be careful of ereturned results you create this way. It may not be legitimate for using them for much of anything besides displaying results, e.g. I don't know if you could use test commands or margins or predict or whatever. It might depend on whether you have ereturned enough information.
                  -------------------------------------------
                  Richard Williams, Notre Dame Dept of Sociology
                  StataNow Version: 18.5 MP (2 processor)

                  EMAIL: [email protected]
                  WWW: https://www3.nd.edu/~rwilliam

                  Comment


                  • #10
                    Thank you very much, Richard Williams!

                    Is there a way to modify the code so that it shows the standardized coefficients? I tried adding the option 'standardized' after the 'quietly estat teffects'-command and than adding the option 'beta' after the 'esttab'-command but the output gave out a red error message.

                    Thanks for any help anyone can offer.

                    Comment


                    • #11
                      asdocx offers a convenient way to export the table of total, direct, and indirect effects to MS Word, Excel, LaTeX, or HTML. asdocx compiles various statistics from the stored matrices and creates a complete table of coefficient, standard errors, z-statistics, and p-values. Here is one example (More examples and details of the options are discussed on this page https://fintechprofessor.com/asdocx/...l-latex-stata/ )
                      Code:
                      * Load another dataset
                      use https://stats.idre.ucla.edu/stat/data/hsbdemo, clear
                      
                      sem (read <- write math ses socst ) ///
                          (science <- read math awards ) ///
                          (write <- math read awards )
                      
                      asdocx estat teffects, effects(te de ie) tzok replace


                      Two more tables produced (total effects and direct effects). However, they are not reported here. The above example does not follow any theory for model building. It just shows the use of asdocx with estat teffects.

                      Regards
                      --------------------------------------------------
                      Attaullah Shah, PhD.
                      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                      FinTechProfessor.com
                      https://asdocx.com
                      Check out my asdoc program, which sends outputs to MS Word.
                      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                      Comment


                      • #12
                        Dear Prof. Richard Williams,

                        I always benefit tremendously from your valuable advice.
                        Recently I have been running the exact commands posted 24 Oct 2016, 09:59.
                        However, in the end I have the error message "estimation result direct not found".
                        Do you think that this is because of some changes in STATA settings or problems with my PC?
                        Below I have copied and pasted the results of my running the commands posted 24 Oct 2016, 09:59.
                        If you see them, strangely, only direct effects have been produced; the indirect and total effects are the same as the direct effects.
                        I would appreciate it very much if you could show me the way out.
                        Best regards,
                        Yasushi

                        . capture program drop te_direct

                        . program te_direct, eclass
                        1. if replay() {
                        2. ereturn display
                        3. exit
                        4. }
                        5. quietly estat teffects
                        6. mat b = r(direct)
                        7. mat V = r(V_direct)
                        8. local N = e(N)
                        9. ereturn post b V, obs(`N')
                        10. ereturn local cmd te_direct
                        11. est store direct
                        12. end

                        .
                        . capture program drop te_indirect

                        . program te_indirect, eclass
                        1. if replay() {
                        2. ereturn display
                        3. exit
                        4. }
                        5. quietly estat teffects
                        6. mat b = r(indirect)
                        7. mat V = r(V_indirect)
                        8. ereturn post b V
                        9. ereturn local cmd te_indirect
                        10. est store indirect
                        11. end

                        .
                        . capture program drop te_total

                        . program te_total, eclass
                        1. if replay() {
                        2. ereturn display
                        3. exit
                        4. }
                        5. quietly estat teffects
                        6. mat b = r(total)
                        7. mat V = r(V_total)
                        8. ereturn post b V
                        9. ereturn local cmd te_total
                        10. est store total
                        11. end

                        .
                        .
                        .
                        . sem (height black female -> weight)(female black -> height)

                        Endogenous variables
                        Observed: height weight

                        Exogenous variables
                        Observed: black female

                        Fitting target model:
                        Iteration 0: log likelihood = -86090.901
                        Iteration 1: log likelihood = -86090.901

                        Structural equation model Number of obs = 10,337
                        Estimation method: ml

                        Log likelihood = -86090.901

                        ------------------------------------------------------------------------------
                        | OIM
                        | Coefficient std. err. z P>|z| [95% conf. interval]
                        -------------+----------------------------------------------------------------
                        Structural |
                        height |
                        black | .3979381 .2216452 1.80 0.073 -.0364786 .8323548
                        female | -13.51765 .1360978 -99.32 0.000 -13.7844 -13.25091
                        _cons | 174.7076 .1011681 1726.90 0.000 174.5093 174.9059
                        -----------+----------------------------------------------------------------
                        weight |
                        height | .6624414 .0190954 34.69 0.000 .6250151 .6998677
                        black | 3.490113 .4303801 8.11 0.000 2.646583 4.333642
                        female | -2.664485 .3693839 -7.21 0.000 -3.388464 -1.940505
                        _cons | -38.12579 3.341889 -11.41 0.000 -44.67577 -31.57581
                        -------------+----------------------------------------------------------------
                        var(e.height)| 47.7417 .6640727 46.45773 49.06117
                        var(e.weight)| 179.9492 2.50304 175.1096 184.9226
                        ------------------------------------------------------------------------------
                        LR test of model vs. saturated: chi2(0) = 0.00 Prob > chi2 = .

                        . est store main

                        . te_direct
                        -------------------------------------------------------------------------------
                        | OIM
                        | Coefficient std. err. z P>|z| [95% conf. interval]
                        --------------+----------------------------------------------------------------
                        height |
                        black | .3979381 .2216452 1.80 0.073 -.0364786 .8323548
                        female | -13.51765 .1360978 -99.32 0.000 -13.7844 -13.25091
                        _cons | 174.7076 .1011681 1726.90 0.000 174.5093 174.9059
                        --------------+----------------------------------------------------------------
                        weight |
                        height | .6624414 .0190954 34.69 0.000 .6250151 .6998677
                        black | 3.490113 .4303801 8.11 0.000 2.646583 4.333642
                        female | -2.664485 .3693839 -7.21 0.000 -3.388464 -1.940505
                        _cons | -38.12579 3.341889 -11.41 0.000 -44.67577 -31.57581
                        --------------+----------------------------------------------------------------
                        var(e.height)| 47.7417 .6640727 71.89 0.000 46.44014 49.04326
                        var(e.weight)| 179.9492 2.50304 71.89 0.000 175.0434 184.8551
                        -------------------------------------------------------------------------------

                        .
                        . est restore main
                        (results main are active now)

                        . te_indirect
                        -------------------------------------------------------------------------------
                        | OIM
                        | Coefficient std. err. z P>|z| [95% conf. interval]
                        --------------+----------------------------------------------------------------
                        height |
                        black | .3979381 .2216452 1.80 0.073 -.0364786 .8323548
                        female | -13.51765 .1360978 -99.32 0.000 -13.7844 -13.25091
                        _cons | 174.7076 .1011681 1726.90 0.000 174.5093 174.9059
                        --------------+----------------------------------------------------------------
                        weight |
                        height | .6624414 .0190954 34.69 0.000 .6250151 .6998677
                        black | 3.490113 .4303801 8.11 0.000 2.646583 4.333642
                        female | -2.664485 .3693839 -7.21 0.000 -3.388464 -1.940505
                        _cons | -38.12579 3.341889 -11.41 0.000 -44.67577 -31.57581
                        --------------+----------------------------------------------------------------
                        var(e.height)| 47.7417 .6640727 71.89 0.000 46.44014 49.04326
                        var(e.weight)| 179.9492 2.50304 71.89 0.000 175.0434 184.8551
                        -------------------------------------------------------------------------------

                        .
                        . est restore main
                        (results main are active now)

                        . te_total
                        -------------------------------------------------------------------------------
                        | OIM
                        | Coefficient std. err. z P>|z| [95% conf. interval]
                        --------------+----------------------------------------------------------------
                        height |
                        black | .3979381 .2216452 1.80 0.073 -.0364786 .8323548
                        female | -13.51765 .1360978 -99.32 0.000 -13.7844 -13.25091
                        _cons | 174.7076 .1011681 1726.90 0.000 174.5093 174.9059
                        --------------+----------------------------------------------------------------
                        weight |
                        height | .6624414 .0190954 34.69 0.000 .6250151 .6998677
                        black | 3.490113 .4303801 8.11 0.000 2.646583 4.333642
                        female | -2.664485 .3693839 -7.21 0.000 -3.388464 -1.940505
                        _cons | -38.12579 3.341889 -11.41 0.000 -44.67577 -31.57581
                        --------------+----------------------------------------------------------------
                        var(e.height)| 47.7417 .6640727 71.89 0.000 46.44014 49.04326
                        var(e.weight)| 179.9492 2.50304 71.89 0.000 175.0434 184.8551
                        -------------------------------------------------------------------------------

                        .
                        . esttab direct indirect total, mtitles(direct indirect total)
                        estimation result direct not found
                        r(111);

                        end of do-file

                        r(111);

                        .

                        Last edited by Yasushi Hazama; 16 Jan 2025, 19:42.

                        Comment


                        • #13
                          I'd completely forgotten about this thread, let alone recall that I had contributed to it!

                          My code in post #5 continues to work fine for me. Try it instead of the code that had the replay option in it.

                          NOTE: Your output would be much easier to read if you used code tags.
                          Last edited by Richard Williams; 20 Jan 2025, 16:56.
                          -------------------------------------------
                          Richard Williams, Notre Dame Dept of Sociology
                          StataNow Version: 18.5 MP (2 processor)

                          EMAIL: [email protected]
                          WWW: https://www3.nd.edu/~rwilliam

                          Comment


                          • #14
                            Dear Prof. Richard Williams,

                            Thank you very much for your generous and valuable help, as always.
                            As you suggested, I tried your code in post #5 that had the replay option in it, and then it worked perfectly.
                            I am sorry for bothering you with my lack of understanding.
                            Best regards,
                            Yasushi

                            Comment


                            • #15
                              I wish I could edit posts from 8 years ago! If future generations see this thread, use my code from post #5, not post #7. Or, maybe the strategy outlined in post # 11 is best — I haven’t tried it but it sounds promising.
                              -------------------------------------------
                              Richard Williams, Notre Dame Dept of Sociology
                              StataNow Version: 18.5 MP (2 processor)

                              EMAIL: [email protected]
                              WWW: https://www3.nd.edu/~rwilliam

                              Comment

                              Working...
                              X