Announcement

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

  • Export SEM models into formatted tables

    Dear all,

    Because I have to use FIML in my analyses, I'll have to rewrite my code from using the 'regress' to using the 'sem' command. Before I used regout2 to export the tables, but now I have no clue to export the models from my SEM model into formatted tables.

    My current code:

    Code:
    qui reg z_post_`var' i. treatment if group ==0
    outreg2 using "$filename1", $outreg2 ctitle ("Model 1", "`lab'") addtext (Families, `g1', Robust SE, NO, Random effects, NO, Missing, MIM, Pretest, NO) nor2
    Has to be rewritten to something like this:

    Code:
    sem (z_post_`var' <- treatment) if group ==0, method(mlmv)
    *... a way to export the results into tables
    Any ideas are welcome!


  • #2
    If you are running a regression with full information maximum likelihood (FIML) and want to export the coefficients, then you can use estout from SSC, specifying the equation of the outcome variable. In my example below, my outcome is mpg, so its equation is mpg:, i.e., variable name + colon.

    Code:
    sysuse auto, clear
    sem (mpg <- weight disp rep78), method(mlmv)
    esttab, keep(mpg:) eqlab(none)
    See

    Code:
    mat list e(b)
    to see how the equation is named, if unsure.

    Res.:

    Code:
    . esttab, keep(mpg:) eqlab(none)
    
    ----------------------------
                          (1)   
                                
    ----------------------------
    weight           -0.00647***
                      (-5.68)   
    
    displacement      0.00701   
                       (0.72)   
    
    rep78               0.565   
                       (1.28)   
    
    _cons               37.53***
                      (13.39)   
    ----------------------------
    N                      74   
    ----------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      Dear Andrew, thank you so much for your response. This worked perfectly. I'm doing the following now:

      Code:
      foreach var of varlist $outcome {
      local lab: variable label `var'    
         sem (z_post_`var' <- treatment) if group ==0, method(mlmv)    
         esttab using Intervention_effects_`var'.rtf, replace keep(z_post_`var':) eqlab(none) label ///    
         title({\b Table 1.} {\i Intervention effects on `var'}) ///    
         nonumbers mtitles ("Model A")  
      mat list e(b)
      }
      Yet I'm still struggling with some things:
      1.The tables end up with z-scores (the t-statistics) in brackets instead of the standard errors. How can I make sure that the standard errors are displayed?

      2.How can more models (e.g. B, C, D etc...) be added in the rtf files in this loop set-up with esttab?

      3.I want to add rows in the table below _cons, stating what is included in the model, for example:

      _cons 37.53***
      (13.39)
      FIML Yes
      Control Yes
      Random effects No
      Last edited by Marinka Willemsen; 09 Sep 2021, 02:59.

      Comment


      • #4
        Specifying different conditions within a loop requires a handle on the use of locals and the -cond()- function. The code is just illustrative of how you do it. The condition to identify FIML is fine, for control variables, you specify them, e.g., using the -regexm()- function. However, I am not sure about the RE condition. Look at

        Code:
        ereturn list
        for ideas. The rest are just standard esttab/ estout options, see

        Code:
        help esttab
        and

        Code:
        help estout
        Code:
        sysuse auto, clear
        eststo clear
        foreach var in weight displacement gear{
            eststo: sem (mpg <- `var' rep78), method(mlmv)
            estadd local FIML= cond("`e(method)'" == "mlmv","Yes", "No")
            estadd local Control= cond(regexm("`e(oxvars)'", "(weight|displacement)"), "Yes", "No")
            estadd local RE= cond("`e(technique)'" ==  "nr", "No", "Yes")
        }
        esttab est*, se keep( mpg:_cons) eqlab(none) s(FIML Control RE) varlab(_cons Intercept)
        Res.:

        Code:
        . esttab est*, se keep( mpg:_cons) eqlab(none) s(FIML Control RE) varlab(_cons Intercept)
        
        ------------------------------------------------------------
                              (1)             (2)             (3)  
                                                                    
        ------------------------------------------------------------
        Intercept           36.76***        26.89***       -3.089  
                          (2.685)         (2.622)         (3.512)  
        ------------------------------------------------------------
        FIML                  Yes             Yes             Yes  
        Control               Yes             Yes              No  
        RE                     No              No              No  
        ------------------------------------------------------------
        Standard errors in parentheses
        * p<0.05, ** p<0.01, *** p<0.001
        Last edited by Andrew Musau; 09 Sep 2021, 05:17.

        Comment


        • #5
          Originally posted by Marinka Willemsen View Post
          ...
          [FONT=arial]Yet I'm still struggling with some things:
          1.The tables end up with z-scores (the t-statistics) in brackets instead of the standard errors. How can I make sure that the standard errors are displayed?
          ...
          estout est*, cells(b se) keep(mpg:_cons) eqlab(none) s(FIML Control RE) varlab(_cons Intercept)
          estout est*, cells(b(fmt(2)) se) keep(mpg:_cons) eqlab(none) s(FIML Control RE) varlab(_cons Intercept)
          estout est*, cells((b se)) keep(mpg:_cons) eqlab(none) s(FIML Control RE) varlab(_cons Intercept)
          First row puts the SEs below the betas.

          Second row additionally formats that to 2 decimal places - but keep in mind that it formats the data in Stata then writes to Excel, and you might or might not want to do some post-processing in Excel.

          Third row gives you the SEs in additional columns, not rows. Note the extra set of brackets in the cells() option.
          Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

          When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

          Comment


          • #6
            Dear both,

            Thank you so much for your fast replies. This helped a lot.


            My bad, I will try to use more code delimiters next time.

            Comment

            Working...
            X