Announcement

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

  • Getting means from a regression with clustered standard errors and exporting them to TEX file

    Hi,

    I need to create a table similar to that in the below picture and export it to a TEX file (for use in LaTeX).

    Click image for larger version

Name:	Skærmbillede 2023-03-30 183902.png
Views:	1
Size:	40.0 KB
ID:	1707843

    It is obviously similar to a ttest, but I cannot use ttest, as I need to cluster standard errors by the District1 variable and weigh them by "withinwt".

    I get the results using

    Code:
     reg elections Quota [aweight=withinwt], vce(cluster District1)
    Is there a way to get Stata to save the means and produce the above table in a TEX file using the regression output? Or do you have other suggestions for how to do it?

    My data looks as follows

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float elections byte Quota float withinwt str14 District1
    4 0 1.1162767 "D0309"
    4 0 1.1162767 "D0309"
    4 0 1.1162767 "D0309"
    4 0 1.1162767 "D0309"
    1 0 1.1162767 "D0308"
    1 0 1.1162767 "D0309"
    4 0 1.1162767 "D0308"
    3 0 1.1162767 "D0308"
    1 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    3 1  .9732189 "B0706"
    3 1  .9732189 "B0706"
    1 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    4 0  .6487038 "A0308"
    2 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    2 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    2 0  .6487038 "A0308"
    1 0  .9725117 "E1208"
    1 0  .9725117 "E1208"
    4 0  .9725117 "E1208"
    3 0  .9725117 "E1208"
    2 0  .9725117 "E1208"
    3 0  .9725117 "E1209"
    3 0  .9725117 "E1209"
    2 0  .9725117 "E1209"
    3 0  .9393414 "C1009"
    4 0  .9393414 "C1009"
    3 0  .9393414 "C1010"
    4 0  .9393414 "C1010"
    2 0  .9393414 "C1010"
    4 0  .9393414 "C1010"
    3 1  .6602322 "J1106"
    . 1  .6602322 "J1106"
    1 1  .6602322 "J1106"
    4 1  .6602322 "J1106"
    4 1  .6602322 "J1106"
    1 1  .6602322 "J1106"
    2 1  .6602322 "J1106"
    3 1  .6602322 "J1106"
    1 1 1.0847074 "C1505"
    3 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    1 1 1.0847074 "C1505"
    2 1 1.0847074 "C1505"
    3 0   .895265 "G0609"
    3 0   .895265 "G0609"
    1 0 1.0417415 "E0206"
    1 0 1.0417415 "E0206"
    1 0 1.0417415 "E0206"
    4 0 1.0417415 "E0206"
    3 0 1.0417415 "E0206"
    2 0 1.0417415 "E0206"
    1 0 1.0417415 "E0206"
    3 0 1.0417415 "E0206"
    2 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    1 1 1.1030433 "F0406"
    2 1 1.1030433 "F0406"
    1 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    1 1  .6517281 "G0802"
    1 1  .6517281 "G0802"
    4 0   .674803 "F1307"
    1 0   .674803 "F1307"
    4 0   .674803 "F1307"
    . 0   .674803 "F1307"
    end

  • #2
    Use margins to get the adjusted means. The following uses estout from SSC, with some manipulation of column names to achieve the desired format. No separate column for p-values is created as the significance stars indicate this.

    Code:
    ssc install estout, replace
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float elections byte Quota float withinwt str14 District1
    4 0 1.1162767 "D0309"
    4 0 1.1162767 "D0309"
    4 0 1.1162767 "D0309"
    4 0 1.1162767 "D0309"
    1 0 1.1162767 "D0308"
    1 0 1.1162767 "D0309"
    4 0 1.1162767 "D0308"
    3 0 1.1162767 "D0308"
    1 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    3 1  .9732189 "B0706"
    3 1  .9732189 "B0706"
    1 1  .9732189 "B0706"
    4 1  .9732189 "B0706"
    4 0  .6487038 "A0308"
    2 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    2 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    4 0  .6487038 "A0308"
    2 0  .6487038 "A0308"
    1 0  .9725117 "E1208"
    1 0  .9725117 "E1208"
    4 0  .9725117 "E1208"
    3 0  .9725117 "E1208"
    2 0  .9725117 "E1208"
    3 0  .9725117 "E1209"
    3 0  .9725117 "E1209"
    2 0  .9725117 "E1209"
    3 0  .9393414 "C1009"
    4 0  .9393414 "C1009"
    3 0  .9393414 "C1010"
    4 0  .9393414 "C1010"
    2 0  .9393414 "C1010"
    4 0  .9393414 "C1010"
    3 1  .6602322 "J1106"
    . 1  .6602322 "J1106"
    1 1  .6602322 "J1106"
    4 1  .6602322 "J1106"
    4 1  .6602322 "J1106"
    1 1  .6602322 "J1106"
    2 1  .6602322 "J1106"
    3 1  .6602322 "J1106"
    1 1 1.0847074 "C1505"
    3 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    4 1 1.0847074 "C1505"
    1 1 1.0847074 "C1505"
    2 1 1.0847074 "C1505"
    3 0   .895265 "G0609"
    3 0   .895265 "G0609"
    1 0 1.0417415 "E0206"
    1 0 1.0417415 "E0206"
    1 0 1.0417415 "E0206"
    4 0 1.0417415 "E0206"
    3 0 1.0417415 "E0206"
    2 0 1.0417415 "E0206"
    1 0 1.0417415 "E0206"
    3 0 1.0417415 "E0206"
    2 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    1 1 1.1030433 "F0406"
    2 1 1.1030433 "F0406"
    1 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    4 1 1.1030433 "F0406"
    1 1  .6517281 "G0802"
    1 1  .6517281 "G0802"
    4 0   .674803 "F1307"
    1 0   .674803 "F1307"
    4 0   .674803 "F1307"
    . 0   .674803 "F1307"
    end
    
    eststo diff: reg elections i.Quota [aweight=withinwt], vce(cluster District1)
    eststo m1: margins 1.Quota, post
    est restore diff
    margins 0.Quota, post
    *RENAME COEFFICIENT
    mat b=e(b)
    mat colname b= "1.Quota"
    erepost b=b, rename
    est sto m2
    
    *OUTPUT
    esttab m1 m2 diff using myfile.tex, replace keep(1.Quota) ///
    mlab("Mean 1" "Mean 2" "Difference") nonumb ///
    starlevels(* 0.1 ** 0.05 *** 0.01) coeflab(1.Quota "Some description") ///
    noobs varwidth(20)
    Res.:

    Code:
    . esttab m1 m2 diff, replace keep(1.Quota) ///
    > mlab("Mean 1" "Mean 2" "Difference") nonumb ///
    > starlevels(* 0.1 ** 0.05 *** 0.01) coeflab(1.Quota "Some description") ///
    > noobs varwidth(20)
    
    --------------------------------------------------------------------
                               Mean 1          Mean 2      Difference   
    --------------------------------------------------------------------
    Some description            2.748***        2.776***      -0.0280   
                              (24.89)         (12.33)         (-0.11)   
    --------------------------------------------------------------------
    t statistics in parentheses
    * p<0.1, ** p<0.05, *** p<0.01

    Comment


    • #3
      Hi Andrew,

      Thank you so much for your reply! I have been stuck at this for so long, so I really appreciate it. I see that margins do exactly what I need :-)


      I did not consider that the significance and t statistics would also be shown for the means. It is maybe a bit strange to show that the means are significantly different from 0, when the variable elections can only take values 1-4. Is there a way to only show significance and t statistics for the difference? Maybe that is "breaking the rules" a bit, so I am considering showing the p-value instead, making it more like the table in my original post. I tried to save it and then add it the table by adding a line to your code:

      Code:
      eststo diff: reg elections i.Quota [aweight=withinwt], vce(cluster District1)
      scalar p1 = 2*ttail(e(df_r), abs(_b[1.Quota]/_se[1.Quota]))
      di 2*ttail(e(df_r), abs(_b[1.Quota]/_se[1.Quota]))
      eststo m1: margins 1.Quota, post
      est restore diff
      margins 0.Quota, post
      *RENAME COEFFICIENT
      mat b=e(b)
      mat colname b= "1.Quota"
      erepost b=b, rename
      //
      est sto m2
      
      
      
      *OUTPUT
      esttab m1 m2 diff p1 using myfile.tex, replace keep(1.Quota) ///
      mlab("Mean 1" "Mean 2" "Difference") nonumb ///
      starlevels(* 0.1 ** 0.05 *** 0.01) coeflab(1.Quota "Some description") ///
      noobs varwidth(20)
      The display function shows the right p-value, but in the table the p1 column shows the same value as the difference, cf. below. Do you know what is wrong?

      Code:
      . esttab m1 m2 diff p1, replace keep(1.Quota) ///
      > mlab("Mean 1" "Mean 2" "Difference") nonumb  ///
      > starlevels(* 0.1 ** 0.05 *** 0.01) coeflab(1.Quota "Some description") ///
      > noobs varwidth(20)
      
      ------------------------------------------------------------------------------------
                                 Mean 1          Mean 2      Difference              p1   
      ------------------------------------------------------------------------------------
      Some description            2.773***        2.955***       -0.182**        -0.182** 
                                (40.74)         (69.03)         (-2.26)         (-2.26)   
      ------------------------------------------------------------------------------------
      t statistics in parentheses
      * p<0.1, ** p<0.05, *** p<0.01
      Finally, I would like to add more than variable to the table, i.e. have another row for another variable (like in the table in #1), but I am uncertain how to add it, now that the first rows are entered as 3 (or 4) seperate entries. Is there a way to do that?
      Last edited by Agathe Engell; 31 Mar 2023, 04:08.

      Comment


      • #4
        This will give you what you want for one regression. Please provide an example that includes multiple regressions.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float elections byte Quota float withinwt str14 District1
        4 0 1.1162767 "D0309"
        4 0 1.1162767 "D0309"
        4 0 1.1162767 "D0309"
        4 0 1.1162767 "D0309"
        1 0 1.1162767 "D0308"
        1 0 1.1162767 "D0309"
        4 0 1.1162767 "D0308"
        3 0 1.1162767 "D0308"
        1 1  .9732189 "B0706"
        4 1  .9732189 "B0706"
        4 1  .9732189 "B0706"
        4 1  .9732189 "B0706"
        3 1  .9732189 "B0706"
        3 1  .9732189 "B0706"
        1 1  .9732189 "B0706"
        4 1  .9732189 "B0706"
        4 0  .6487038 "A0308"
        2 0  .6487038 "A0308"
        4 0  .6487038 "A0308"
        4 0  .6487038 "A0308"
        2 0  .6487038 "A0308"
        4 0  .6487038 "A0308"
        4 0  .6487038 "A0308"
        2 0  .6487038 "A0308"
        1 0  .9725117 "E1208"
        1 0  .9725117 "E1208"
        4 0  .9725117 "E1208"
        3 0  .9725117 "E1208"
        2 0  .9725117 "E1208"
        3 0  .9725117 "E1209"
        3 0  .9725117 "E1209"
        2 0  .9725117 "E1209"
        3 0  .9393414 "C1009"
        4 0  .9393414 "C1009"
        3 0  .9393414 "C1010"
        4 0  .9393414 "C1010"
        2 0  .9393414 "C1010"
        4 0  .9393414 "C1010"
        3 1  .6602322 "J1106"
        . 1  .6602322 "J1106"
        1 1  .6602322 "J1106"
        4 1  .6602322 "J1106"
        4 1  .6602322 "J1106"
        1 1  .6602322 "J1106"
        2 1  .6602322 "J1106"
        3 1  .6602322 "J1106"
        1 1 1.0847074 "C1505"
        3 1 1.0847074 "C1505"
        4 1 1.0847074 "C1505"
        4 1 1.0847074 "C1505"
        4 1 1.0847074 "C1505"
        4 1 1.0847074 "C1505"
        1 1 1.0847074 "C1505"
        2 1 1.0847074 "C1505"
        3 0   .895265 "G0609"
        3 0   .895265 "G0609"
        1 0 1.0417415 "E0206"
        1 0 1.0417415 "E0206"
        1 0 1.0417415 "E0206"
        4 0 1.0417415 "E0206"
        3 0 1.0417415 "E0206"
        2 0 1.0417415 "E0206"
        1 0 1.0417415 "E0206"
        3 0 1.0417415 "E0206"
        2 1 1.1030433 "F0406"
        4 1 1.1030433 "F0406"
        1 1 1.1030433 "F0406"
        2 1 1.1030433 "F0406"
        1 1 1.1030433 "F0406"
        4 1 1.1030433 "F0406"
        4 1 1.1030433 "F0406"
        4 1 1.1030433 "F0406"
        1 1  .6517281 "G0802"
        1 1  .6517281 "G0802"
        4 0   .674803 "F1307"
        1 0   .674803 "F1307"
        4 0   .674803 "F1307"
        . 0   .674803 "F1307"
        end
        
        eststo diff: reg elections i.Quota [aweight=withinwt], vce(cluster District1)
        eststo m1: margins 1.Quota, post
        est restore diff
        margins 0.Quota, post
        *RENAME COEFFICIENT
        mat b=e(b)
        mat colname b= "1.Quota"
        erepost b=b, rename
        est sto m2
        
        *OUTPUT
        esttab m1 m2 diff diff, cells("b(pattern(1 1 1 0)) p(pattern(0 0 0 1) star)") replace keep(1.Quota) ///
        mlab("Mean 1" "Mean 2" "Difference" "p-value") nonumb ///
        starlevels(* 0.1 ** 0.05 *** 0.01) coeflab(1.Quota "Some description") ///
        noobs varwidth(20) collab(none)
        Res.:

        Code:
        . esttab m1 m2 diff diff, cells("b(pattern(1 1 1 0)) p(pattern(0 0 0 1) star)") replace keep(1.Quota) ///
        > mlab("Mean 1" "Mean 2" "Difference" "p-value") nonumb ///
        > starlevels(* 0.1 ** 0.05 *** 0.01) coeflab(1.Quota "Some description") ///
        > noobs varwidth(20) collab(none)
        
        ---------------------------------------------------------------------------
                                   Mean 1       Mean 2   Difference      p-value   
        ---------------------------------------------------------------------------
        Some description         2.747591     2.775629    -.0280383     .9125291   
        ---------------------------------------------------------------------------

        Comment


        • #5
          Thank you once again!

          Apologies for not adding other variables. I have now added demo_sat and demo_fut to the data below, which also needs to be added to the table along with a couple of other variables.


          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float elections byte(demo_sat demo_fut Quota) float withinwt str14 District1
          4 1  4 0 1.1162767 "D0309"
          4 1 10 0 1.1162767 "D0309"
          4 2 10 0 1.1162767 "D0309"
          4 3 10 0 1.1162767 "D0309"
          1 .  . 0 1.1162767 "D0309"
          1 4  9 0 1.1162767 "D0310"
          4 4 10 0 1.1162767 "D0310"
          3 3  4 0 1.1162767 "D0310"
          1 4  6 1  .9732189 "B0706"
          4 1  . 1  .9732189 "B0706"
          4 1  8 1  .9732189 "B0706"
          4 4 10 1  .9732189 "B0706"
          3 2  0 1  .9732189 "B0706"
          3 .  . 1  .9732189 "B0706"
          1 3  . 1  .9732189 "B0707"
          4 4 10 1  .9732189 "B0707"
          4 4 10 0  .6487038 "A0308"
          2 3  . 0  .6487038 "A0308"
          4 4 10 0  .6487038 "A0308"
          4 1  . 0  .6487038 "A0308"
          2 2  5 0  .6487038 "A0308"
          4 4 10 0  .6487038 "A0308"
          4 1 10 0  .6487038 "A0308"
          2 3  7 0  .6487038 "A0308"
          1 3  8 0  .9725117 "E1208"
          1 1 10 0  .9725117 "E1208"
          4 2 10 0  .9725117 "E1208"
          3 3  . 0  .9725117 "E1208"
          2 2  0 0  .9725117 "E1208"
          3 2  8 0  .9725117 "E1209"
          3 3 10 0  .9725117 "E1209"
          2 3 10 0  .9725117 "E1209"
          3 2 10 0  .9393414 "C1009"
          4 2  . 0  .9393414 "C1009"
          3 4  . 0  .9393414 "C1009"
          4 3 10 0  .9393414 "C1009"
          2 1  0 0  .9393414 "C1009"
          4 .  . 0  .9393414 "C1009"
          3 3 10 1  .6602322 "J1106"
          . 4  . 1  .6602322 "J1106"
          1 1  . 1  .6602322 "J1106"
          4 3 10 1  .6602322 "J1106"
          4 3 10 1  .6602322 "J1106"
          1 0  . 1  .6602322 "J1106"
          2 3 10 1  .6602322 "J1106"
          3 1 10 1  .6602322 "J1106"
          1 2  5 1 1.0847074 "C1505"
          3 4  8 1 1.0847074 "C1505"
          4 4 10 1 1.0847074 "C1505"
          4 2 10 1 1.0847074 "C1505"
          4 1 10 1 1.0847074 "C1505"
          4 1  . 1 1.0847074 "C1505"
          1 4 10 1 1.0847074 "C1505"
          2 3  7 1 1.0847074 "C1505"
          3 4  . 0   .895265 "G0609"
          3 .  . 0   .895265 "G0609"
          1 3 10 0 1.0417415 "E0206"
          1 3  . 0 1.0417415 "E0206"
          1 1 10 0 1.0417415 "E0206"
          4 3 10 0 1.0417415 "E0206"
          3 2  5 0 1.0417415 "E0206"
          2 2  0 0 1.0417415 "E0206"
          1 .  . 0 1.0417415 "E0206"
          3 3  6 0 1.0417415 "E0206"
          2 2  9 1 1.1030433 "F0406"
          4 1  . 1 1.1030433 "F0406"
          1 4  . 1 1.1030433 "F0406"
          2 2  . 1 1.1030433 "F0406"
          1 3  5 1 1.1030433 "F0406"
          4 1 10 1 1.1030433 "F0406"
          4 4 10 1 1.1030433 "F0406"
          4 4 10 1 1.1030433 "F0406"
          1 2  0 1  .6517281 "G0802"
          1 3 10 1  .6517281 "G0802"
          2 .  . 3   .674803 "F1307"
          1 2 10 0   .674803 "F1307"
          4 1 10 0   .674803 "F1308"
          . 3 10 0   .674803 "F1308"
          3 1  . 1 1.2975078 "A1203"
          3 4 10 1 1.2975078 "A1203"
          4 3  . 1 1.2975078 "A1203"
          2 . 10 1 1.2975078 "A1203"
          4 4 10 1  .9651449 "H0106"
          . 2 10 1  .9651449 "H0106"
          3 2 10 1  .9651449 "H0106"
          4 3 10 1  .9651449 "H0106"
          1 1 10 0   .674803 "F1307"
          4 1  . 0   .674803 "F1307"
          2 4 10 0   .674803 "F1307"
          4 3  . 0   .674803 "F1307"
          4 3 10 1 1.0614818 "B0408"
          4 2  1 1 1.0614818 "B0408"
          4 2 10 1 1.0614818 "B0408"
          4 . 10 1 1.0614818 "B0408"
          4 1  5 1 1.0614818 "B0409"
          4 1  5 1 1.0614818 "B0409"
          4 3 10 1 1.0614818 "B0408"
          4 4 10 1 1.0614818 "B0408"
          3 2 10 0  .7088118 "F0101"
          1 .  . 0  .7088118 "F0101"
          end
          Last edited by Agathe Engell; 01 Apr 2023, 05:34.

          Comment


          • #6
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float elections byte(demo_sat demo_fut Quota) float withinwt str14 District1
            4 1  4 0 1.1162767 "D0309"
            4 1 10 0 1.1162767 "D0309"
            4 2 10 0 1.1162767 "D0309"
            4 3 10 0 1.1162767 "D0309"
            1 .  . 0 1.1162767 "D0309"
            1 4  9 0 1.1162767 "D0310"
            4 4 10 0 1.1162767 "D0310"
            3 3  4 0 1.1162767 "D0310"
            1 4  6 1  .9732189 "B0706"
            4 1  . 1  .9732189 "B0706"
            4 1  8 1  .9732189 "B0706"
            4 4 10 1  .9732189 "B0706"
            3 2  0 1  .9732189 "B0706"
            3 .  . 1  .9732189 "B0706"
            1 3  . 1  .9732189 "B0707"
            4 4 10 1  .9732189 "B0707"
            4 4 10 0  .6487038 "A0308"
            2 3  . 0  .6487038 "A0308"
            4 4 10 0  .6487038 "A0308"
            4 1  . 0  .6487038 "A0308"
            2 2  5 0  .6487038 "A0308"
            4 4 10 0  .6487038 "A0308"
            4 1 10 0  .6487038 "A0308"
            2 3  7 0  .6487038 "A0308"
            1 3  8 0  .9725117 "E1208"
            1 1 10 0  .9725117 "E1208"
            4 2 10 0  .9725117 "E1208"
            3 3  . 0  .9725117 "E1208"
            2 2  0 0  .9725117 "E1208"
            3 2  8 0  .9725117 "E1209"
            3 3 10 0  .9725117 "E1209"
            2 3 10 0  .9725117 "E1209"
            3 2 10 0  .9393414 "C1009"
            4 2  . 0  .9393414 "C1009"
            3 4  . 0  .9393414 "C1009"
            4 3 10 0  .9393414 "C1009"
            2 1  0 0  .9393414 "C1009"
            4 .  . 0  .9393414 "C1009"
            3 3 10 1  .6602322 "J1106"
            . 4  . 1  .6602322 "J1106"
            1 1  . 1  .6602322 "J1106"
            4 3 10 1  .6602322 "J1106"
            4 3 10 1  .6602322 "J1106"
            1 0  . 1  .6602322 "J1106"
            2 3 10 1  .6602322 "J1106"
            3 1 10 1  .6602322 "J1106"
            1 2  5 1 1.0847074 "C1505"
            3 4  8 1 1.0847074 "C1505"
            4 4 10 1 1.0847074 "C1505"
            4 2 10 1 1.0847074 "C1505"
            4 1 10 1 1.0847074 "C1505"
            4 1  . 1 1.0847074 "C1505"
            1 4 10 1 1.0847074 "C1505"
            2 3  7 1 1.0847074 "C1505"
            3 4  . 0   .895265 "G0609"
            3 .  . 0   .895265 "G0609"
            1 3 10 0 1.0417415 "E0206"
            1 3  . 0 1.0417415 "E0206"
            1 1 10 0 1.0417415 "E0206"
            4 3 10 0 1.0417415 "E0206"
            3 2  5 0 1.0417415 "E0206"
            2 2  0 0 1.0417415 "E0206"
            1 .  . 0 1.0417415 "E0206"
            3 3  6 0 1.0417415 "E0206"
            2 2  9 1 1.1030433 "F0406"
            4 1  . 1 1.1030433 "F0406"
            1 4  . 1 1.1030433 "F0406"
            2 2  . 1 1.1030433 "F0406"
            1 3  5 1 1.1030433 "F0406"
            4 1 10 1 1.1030433 "F0406"
            4 4 10 1 1.1030433 "F0406"
            4 4 10 1 1.1030433 "F0406"
            1 2  0 1  .6517281 "G0802"
            1 3 10 1  .6517281 "G0802"
            2 .  . 3   .674803 "F1307"
            1 2 10 0   .674803 "F1307"
            4 1 10 0   .674803 "F1308"
            . 3 10 0   .674803 "F1308"
            3 1  . 1 1.2975078 "A1203"
            3 4 10 1 1.2975078 "A1203"
            4 3  . 1 1.2975078 "A1203"
            2 . 10 1 1.2975078 "A1203"
            4 4 10 1  .9651449 "H0106"
            . 2 10 1  .9651449 "H0106"
            3 2 10 1  .9651449 "H0106"
            4 3 10 1  .9651449 "H0106"
            1 1 10 0   .674803 "F1307"
            4 1  . 0   .674803 "F1307"
            2 4 10 0   .674803 "F1307"
            4 3  . 0   .674803 "F1307"
            4 3 10 1 1.0614818 "B0408"
            4 2  1 1 1.0614818 "B0408"
            4 2 10 1 1.0614818 "B0408"
            4 . 10 1 1.0614818 "B0408"
            4 1  5 1 1.0614818 "B0409"
            4 1  5 1 1.0614818 "B0409"
            4 3 10 1 1.0614818 "B0408"
            4 4 10 1 1.0614818 "B0408"
            3 2 10 0  .7088118 "F0101"
            1 .  . 0  .7088118 "F0101"
            end
            replace Quota=1 if Quota==3
            
            local depvars elections demo_sat demo_fut
            local i 0
            foreach depvar of local depvars{
                local ++i
                rename (`depvar' Quota) (Quota `depvar')
                eststo diff_`i': reg Quota i.`depvar' [aweight=withinwt], vce(cluster District1)
                eststo m1_`i': margins 1.`depvar', post
                est restore diff_`i'
                margins 0.`depvar', post
                *RENAME COEFFICIENT
                mat b=e(b)
                mat colname b= "1.`depvar'"
                erepost b=b, rename
                est sto m2_`i'
                rename (`depvar' Quota) (Quota `depvar')
            }
            
            *PROGRAM TO APPEND MODELS
            capt prog drop appendmodels
            *! version 1.0.0  14aug2007  Ben Jann
            program appendmodels, eclass
                // using first equation of model
                version 8
                syntax namelist
                tempname b V tmp
                foreach name of local namelist {
                    qui est restore `name'
                    mat `tmp' = e(b)
                    local eq1: coleq `tmp'
                    gettoken eq1 : eq1
                    mat `tmp' = `tmp'[1,"`eq1':"]
                    local cons = colnumb(`tmp',"_cons")
                    if `cons'<. & `cons'>1 {
                        mat `tmp' = `tmp'[1,1..`cons'-1]
                    }
                    mat `b' = nullmat(`b') , `tmp'
                    mat `tmp' = e(V)
                    mat `tmp' = `tmp'["`eq1':","`eq1':"]
                    if `cons'<. & `cons'>1 {
                        mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
                    }
                    capt confirm matrix `V'
                    if _rc {
                        mat `V' = `tmp'
                    }
                    else {
                        mat `V' = ///
                        ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
                        ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
                    }
                }
                local names: colfullnames `b'
                mat coln `V' = `names'
                mat rown `V' = `names'
                eret post `b' `V'
                eret local cmd "whatever"
            end
            
            *APPEND THE MODELS
            local diff 
            local m1 
            local m2
            forval i=1/`=wordcount("`depvars'")'{
               local diff "`diff' diff_`i'" 
               local m1 "`m1' m1_`i'"
               local m2 "`m2' m2_`i'"  
            }
            eststo diff: appendmodels `diff'
            eststo m1: appendmodels `m1'
            eststo m2: appendmodels `m2'
            
            esttab m1 m2 diff diff, cells("b(pattern(1 1 1 0)) p(pattern(0 0 0 1) star)") replace keep(1.*) ///
            mlab("Mean 1" "Mean 2" "Difference" "p-value", lhs(Variables)) nonumb ///
            starlevels(* 0.1 ** 0.05 *** 0.01) substitute(1.e "e" 1.d "d") ///
            noobs varwidth(20) collab(none)
            Res.:

            Code:
            . esttab m1 m2 diff diff, cells("b(pattern(1 1 1 0)) p(pattern(0 0 0 1) star)") replace keep(1.*) ///
            > mlab("Mean 1" "Mean 2" "Difference" "p-value", lhs(Variables)) nonumb ///
            > starlevels(* 0.1 ** 0.05 *** 0.01) substitute(1.e "e" 1.d "d") ///
            > noobs varwidth(20) collab(none)
            
            ---------------------------------------------------------------------------
            Variables                  Mean 1       Mean 2   Difference      p-value   
            ---------------------------------------------------------------------------
            elections              3.038563     2.729372     .3091909     .2594785   
            demo_sat               2.550968     2.479386     .0715826     .6979601   
            demo_fut               8.408523     8.026123     .3824004     .5102858   
            ---------------------------------------------------------------------------

            Comment


            • #7
              Thank you once again!

              One finale question: When I look at outcomes of the regressions as they are run in the first loop, I get slightly different p-values than in the final table. It's down at the third decimal point, so it is very small differences, but is there an explanation for this? I get .0238954** in the table for elections, but 0.026 in the regression, and 0.1739278 for demo_fut in the table, but 0.176 in the regression.

              Comment


              • #8
                There is some issue with the calculations once we combine the results. I don't have time to figure it out, but you can save the p-values directly into matrices to output in the final step.

                Code:
                clear
                input float elections byte(demo_sat demo_fut Quota) float withinwt str14 District1
                4 1  4 0 1.1162767 "D0309"
                4 1 10 0 1.1162767 "D0309"
                4 2 10 0 1.1162767 "D0309"
                4 3 10 0 1.1162767 "D0309"
                1 .  . 0 1.1162767 "D0309"
                1 4  9 0 1.1162767 "D0310"
                4 4 10 0 1.1162767 "D0310"
                3 3  4 0 1.1162767 "D0310"
                1 4  6 1  .9732189 "B0706"
                4 1  . 1  .9732189 "B0706"
                4 1  8 1  .9732189 "B0706"
                4 4 10 1  .9732189 "B0706"
                3 2  0 1  .9732189 "B0706"
                3 .  . 1  .9732189 "B0706"
                1 3  . 1  .9732189 "B0707"
                4 4 10 1  .9732189 "B0707"
                4 4 10 0  .6487038 "A0308"
                2 3  . 0  .6487038 "A0308"
                4 4 10 0  .6487038 "A0308"
                4 1  . 0  .6487038 "A0308"
                2 2  5 0  .6487038 "A0308"
                4 4 10 0  .6487038 "A0308"
                4 1 10 0  .6487038 "A0308"
                2 3  7 0  .6487038 "A0308"
                1 3  8 0  .9725117 "E1208"
                1 1 10 0  .9725117 "E1208"
                4 2 10 0  .9725117 "E1208"
                3 3  . 0  .9725117 "E1208"
                2 2  0 0  .9725117 "E1208"
                3 2  8 0  .9725117 "E1209"
                3 3 10 0  .9725117 "E1209"
                2 3 10 0  .9725117 "E1209"
                3 2 10 0  .9393414 "C1009"
                4 2  . 0  .9393414 "C1009"
                3 4  . 0  .9393414 "C1009"
                4 3 10 0  .9393414 "C1009"
                2 1  0 0  .9393414 "C1009"
                4 .  . 0  .9393414 "C1009"
                3 3 10 1  .6602322 "J1106"
                . 4  . 1  .6602322 "J1106"
                1 1  . 1  .6602322 "J1106"
                4 3 10 1  .6602322 "J1106"
                4 3 10 1  .6602322 "J1106"
                1 0  . 1  .6602322 "J1106"
                2 3 10 1  .6602322 "J1106"
                3 1 10 1  .6602322 "J1106"
                1 2  5 1 1.0847074 "C1505"
                3 4  8 1 1.0847074 "C1505"
                4 4 10 1 1.0847074 "C1505"
                4 2 10 1 1.0847074 "C1505"
                4 1 10 1 1.0847074 "C1505"
                4 1  . 1 1.0847074 "C1505"
                1 4 10 1 1.0847074 "C1505"
                2 3  7 1 1.0847074 "C1505"
                3 4  . 0   .895265 "G0609"
                3 .  . 0   .895265 "G0609"
                1 3 10 0 1.0417415 "E0206"
                1 3  . 0 1.0417415 "E0206"
                1 1 10 0 1.0417415 "E0206"
                4 3 10 0 1.0417415 "E0206"
                3 2  5 0 1.0417415 "E0206"
                2 2  0 0 1.0417415 "E0206"
                1 .  . 0 1.0417415 "E0206"
                3 3  6 0 1.0417415 "E0206"
                2 2  9 1 1.1030433 "F0406"
                4 1  . 1 1.1030433 "F0406"
                1 4  . 1 1.1030433 "F0406"
                2 2  . 1 1.1030433 "F0406"
                1 3  5 1 1.1030433 "F0406"
                4 1 10 1 1.1030433 "F0406"
                4 4 10 1 1.1030433 "F0406"
                4 4 10 1 1.1030433 "F0406"
                1 2  0 1  .6517281 "G0802"
                1 3 10 1  .6517281 "G0802"
                2 .  . 3   .674803 "F1307"
                1 2 10 0   .674803 "F1307"
                4 1 10 0   .674803 "F1308"
                . 3 10 0   .674803 "F1308"
                3 1  . 1 1.2975078 "A1203"
                3 4 10 1 1.2975078 "A1203"
                4 3  . 1 1.2975078 "A1203"
                2 . 10 1 1.2975078 "A1203"
                4 4 10 1  .9651449 "H0106"
                . 2 10 1  .9651449 "H0106"
                3 2 10 1  .9651449 "H0106"
                4 3 10 1  .9651449 "H0106"
                1 1 10 0   .674803 "F1307"
                4 1  . 0   .674803 "F1307"
                2 4 10 0   .674803 "F1307"
                4 3  . 0   .674803 "F1307"
                4 3 10 1 1.0614818 "B0408"
                4 2  1 1 1.0614818 "B0408"
                4 2 10 1 1.0614818 "B0408"
                4 . 10 1 1.0614818 "B0408"
                4 1  5 1 1.0614818 "B0409"
                4 1  5 1 1.0614818 "B0409"
                4 3 10 1 1.0614818 "B0408"
                4 4 10 1 1.0614818 "B0408"
                3 2 10 0  .7088118 "F0101"
                1 .  . 0  .7088118 "F0101"
                end
                replace Quota=1 if Quota==3
                
                local depvars elections demo_sat demo_fut
                local i 0
                foreach depvar of local depvars{
                    local ++i
                    rename (`depvar' Quota) (Quota `depvar')
                    eststo diff_`i': reg Quota i.`depvar' [aweight=withinwt], vce(cluster District1)
                    mat pval_`i'= r(table)["pvalue", "0b.`depvar'".."1.`depvar'"]
                    eststo m1_`i': margins 1.`depvar', post
                    est restore diff_`i'
                    margins 0.`depvar', post
                    *RENAME COEFFICIENT
                    mat b=e(b)
                    mat colname b= "1.`depvar'"
                    erepost b=b, rename
                    est sto m2_`i'
                    rename (`depvar' Quota) (Quota `depvar')
                }
                
                *PROGRAM TO APPEND MODELS
                capt prog drop appendmodels
                *! version 1.0.0  14aug2007  Ben Jann
                program appendmodels, eclass
                    // using first equation of model
                    version 8
                    syntax namelist
                    tempname b V tmp
                    foreach name of local namelist {
                        qui est restore `name'
                        mat `tmp' = e(b)
                        local eq1: coleq `tmp'
                        gettoken eq1 : eq1
                        mat `tmp' = `tmp'[1,"`eq1':"]
                        local cons = colnumb(`tmp',"_cons")
                        if `cons'<. & `cons'>1 {
                            mat `tmp' = `tmp'[1,1..`cons'-1]
                        }
                        mat `b' = nullmat(`b') , `tmp'
                        mat `tmp' = e(V)
                        mat `tmp' = `tmp'["`eq1':","`eq1':"]
                        if `cons'<. & `cons'>1 {
                            mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
                        }
                        capt confirm matrix `V'
                        if _rc {
                            mat `V' = `tmp'
                        }
                        else {
                            mat `V' = ///
                            ( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
                            ( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
                        }
                    }
                    local names: colfullnames `b'
                    mat coln `V' = `names'
                    mat rown `V' = `names'
                    eret post `b' `V'
                    eret local cmd "whatever"
                end
                
                *APPEND THE MODELS
                local diff 
                local m1 
                local m2
                forval i=1/`=wordcount("`depvars'")'{
                   local diff "`diff' diff_`i'" 
                   local m1 "`m1' m1_`i'"
                   local m2 "`m2' m2_`i'" 
                   local pval "`pval' pval_`i',"
                }
                eststo diff: appendmodels `diff'
                eststo m1: appendmodels `m1'
                eststo m2: appendmodels `m2'
                local pval= substr("`pval'", 1,length("`pval'")-1)
                mat pval= `pval'
                est restore diff
                estadd matrix pval= pval
                
                esttab m1 m2 diff diff, cells("b(pattern(1 1 1 0)) pval(pattern(0 0 0 1) star)") replace keep(1.*) ///
                mlab("Mean 1" "Mean 2" "Difference" "p-value", lhs(Variables)) nonumb ///
                starlevels(* 0.1 ** 0.05 *** 0.01) substitute(1.e "e" 1.d "d") ///
                noobs varwidth(20) collab(none)
                Res.:

                Code:
                . esttab m1 m2 diff diff, cells("b(pattern(1 1 1 0)) pval(pattern(0 0 0 1) star)") replace keep(1.*) ///
                > mlab("Mean 1" "Mean 2" "Difference" "p-value", lhs(Variables)) nonumb ///
                > starlevels(* 0.1 ** 0.05 *** 0.01) substitute(1.e "e" 1.d "d") ///
                > noobs varwidth(20) collab(none)
                
                ---------------------------------------------------------------------------
                Variables                  Mean 1       Mean 2   Difference      p-value   
                ---------------------------------------------------------------------------
                elections              3.038563     2.729372     .3091909     .2728253   
                demo_sat               2.550968     2.479386     .0715826     .7020599   
                demo_fut               8.408523     8.026123     .3824004      .518185   
                ---------------------------------------------------------------------------

                Comment


                • #9
                  Hi Andrew, thank you once again for taking the time to reply and clarifying how to get the right values into the table.

                  Comment

                  Working...
                  X