Announcement

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

  • Exporting svy: mean and test results

    Hello,

    I am doing ttest with survey data for 10 variables. Is there any way to get the output into table format consisting of means of the variables by the group variable, followed by the difference between the means and then the t-statistic with significance stars?

    I'm using the following codes:
    Code:
    svy: mean var1 if sample==1 & area==0 & total_int_women1==1, over(Covid19) coeflegend
    test _b[[email protected]] - _b[[email protected]]
    Thanks.


  • #2
    svy: mean allows a variable list, so with multiple variables and the same set of -if- restrictions, estimate everything in one go and then store the test results one by one. Here is a reproducible example. When creating the matrix containing the test statistic and the p-value matrix, it is important to name them as the coefficients from svy: mean so as to create the table more efficiently. I use estout from SSC below.

    Code:
    webuse nhanes2f, clear
    svyset psuid [pweight=finalwgt], strata(stratid)
    estimates clear
    eststo m1: svy: mean zinc age weight, over(black)
    local i 0
    foreach var in zinc age weight{
        local ++i
        test _b[c.`var'@0.black] = _b[c.`var'@1.black]
        mat F`i'= `:di %3.2f `r(F)''
        mat colname F`i'= "c.`var'@1.black"
        mat pval`i'= `r(p)'
        mat colname pval`i'= "c.`var'@1.black"
    }
    mat F= F1,F2,F3
    mat pval= pval1,pval2,pval3
    estadd mat F: m1
    estadd mat pval: m1
    esttab m1, cells("b  F(star pval(pval))") ///
    starl(* 0.1 ** 0.05 *** 0.01) nonumb mlab(none) ///
    collab(Coef. F-stat.) label
    Res.:

    Code:
    . eststo: svy: mean zinc age weight, over(black)
    (running mean on estimation sample)
    
    Survey: Mean estimation
    
    Number of strata = 31              Number of obs   =       9,189
    Number of PSUs   = 62              Population size = 104,176,071
                                       Design df       =          31
    
    ----------------------------------------------------------------
                   |             Linearized
                   |       Mean   std. err.     [95% conf. interval]
    ---------------+------------------------------------------------
      c.zinc@black |
        Not Black  |   87.38342   .4985891      86.36654    88.40029
            Black  |   85.08574   1.165209      82.70929     87.4622
                   |
       c.age@black |
        Not Black  |   42.45728   .3302272      41.78377    43.13078
            Black  |   40.38561   .7003786      38.95718    41.81404
                   |
    c.weight@black |
        Not Black  |    71.5006    .182618      71.12815    71.87305
            Black  |   75.34783   .7636796       73.7903    76.90537
    ----------------------------------------------------------------
    (est1 stored)
    
    
    Adjusted Wald test
    
     ( 1)  [email protected] - [email protected] = 0
    
           F(  1,    31) =    4.02
                Prob > F =    0.0537
    
    Adjusted Wald test
    
     ( 1)  [email protected] - [email protected] = 0
    
           F(  1,    31) =    7.78
                Prob > F =    0.0090
    
    Adjusted Wald test
    
     ( 1)  [email protected] - [email protected] = 0
    
           F(  1,    31) =   23.57
                Prob > F =    0.0000
    
    . esttab m1, cells("b  F(star pval(pval))") ///
    > starl(* 0.1 ** 0.05 *** 0.01) nonumb mlab(none) ///
    > collab(Coef. F-stat.) label
    
    -------------------------------------------------
                                Coef.      F-stat.  
    -------------------------------------------------
    [email protected]           87.38342                
    [email protected]           85.08574         4.02*  
    [email protected]            42.45728                
    [email protected]            40.38561         7.78***
    [email protected]          71.5006                
    [email protected]         75.34783        23.57***
    -------------------------------------------------
    Observations                 9189                
    -------------------------------------------------
    Last edited by Andrew Musau; 14 Dec 2023, 06:53.

    Comment


    • #3
      Thank you, Andrew.

      Comment


      • #4
        Hello, Andrew

        How can I export the above table in the format as shown in the image.


        Attached Files
        Last edited by Varsha Vaishnav; 15 Dec 2023, 07:11.

        Comment


        • #5
          You can work with a variant of this.To add the observation counts as columns, you need to place them in a matrix. I do not have time to illustrate now. The trick below lies in recoding the -over()- indicator, and keeping one category in the table.

          Code:
          webuse nhanes2f, clear
          svyset psuid [pweight=finalwgt], strata(stratid)
          estimates clear
          eststo m1: svy: mean zinc age weight, over(black)
          preserve
          replace black= abs(black-1)
          eststo m2: svy: mean zinc age weight, over(black)
          restore
          
          local i 0
          svy: mean zinc age weight, over(black)
          foreach var in zinc age weight{
              local ++i
              test _b[c.`var'@0.black] = _b[c.`var'@1.black]
              mat F`i'= `:di %3.2f `r(F)''
              mat colname F`i'= "c.`var'@1.black"
              mat pval`i'= `r(p)'
              mat colname pval`i'= "c.`var'@1.black"
          }
          mat F= F1,F2,F3
          mat pval= pval1,pval2,pval3
          estadd mat F: m1
          estadd mat pval: m1
          esttab m2 m1, cells("b  F(star pval(pval) pattern(0 1))") ///
          starl(* 0.1 ** 0.05 *** 0.01) nonumb collab(Mean F-stat.) ///
          mlab(Non-black Black)  label drop(*@0.*)
          Res.:

          Code:
          . esttab m2 m1, cells("b  F(star pval(pval) pattern(0 1))") ///
          > starl(* 0.1 ** 0.05 *** 0.01) nonumb collab(Mean F-stat.) ///
          > mlab(Non-black Black)  label drop(*@0.*)
          
          --------------------------------------------------------------
                                  Non-black        Black                
                                       Mean         Mean      F-stat.   
          --------------------------------------------------------------
          [email protected]           87.38342     85.08574         4.02*  
          [email protected]            42.45728     40.38561         7.78***
          [email protected]          71.5006     75.34783        23.57***
          --------------------------------------------------------------
          Observations                 9189         9189                
          --------------------------------------------------------------
          Last edited by Andrew Musau; 15 Dec 2023, 09:27.

          Comment


          • #6
            Here is the table with observation counts:

            Code:
            webuse nhanes2f, clear
            svyset psuid [pweight=finalwgt], strata(stratid)
            estimates clear
            eststo m1: svy: mean zinc age weight if black, over(black)
            matrix N1= [e(N),e(N),e(N)]
            mat colname N1= [email protected] [email protected] [email protected]
            estadd mat N1: m1
            preserve
            replace black= abs(black-1)
            eststo m2: svy: mean zinc age weight if black, over(black)
            matrix N1= [e(N),e(N),e(N)]
            mat colname N1= [email protected] [email protected] [email protected]
            estadd mat N1: m2
            restore
            
            local i 0
            svy: mean zinc age weight, over(black)
            foreach var in zinc age weight{
                local ++i
                test _b[c.`var'@0.black] = _b[c.`var'@1.black]
                mat F`i'= `:di %3.2f `r(F)''
                mat colname F`i'= "c.`var'@1.black"
                mat pval`i'= `r(p)'
                mat colname pval`i'= "c.`var'@1.black"
            }
            mat F= F1,F2,F3
            mat pval= pval1,pval2,pval3
            estadd mat F: m1
            estadd mat pval: m1
            esttab m2 m1 m1, noobs nonumb ///
            cells("b(pattern(1 1 0)) N1(pattern(1 1 0)) F(star pval(pval) pattern(0 0 1))") ///
            starl(* 0.1 ** 0.05 *** 0.01) collab(Mean Obs. F-stat.) mlab(Non-black Black Difference) ///
            coeflab([email protected] "Zinc" [email protected] "Age" [email protected] "Weight")
            Res.:

            Code:
            . esttab m2 m1 m1, noobs nonumb ///
            > cells("b(pattern(1 1 0)) N1(pattern(1 1 0)) F(star pval(pval) pattern(0 0 1))") ///
            > starl(* 0.1 ** 0.05 *** 0.01) collab(Mean Obs. F-stat.) mlab(Non-black Black Difference) ///
            > coeflab([email protected] "Zinc" [email protected] "Age" [email protected] "Weight")
            
            --------------------------------------------------------------------------------
                            Non-black                     Black                Difference   
                                 Mean         Obs.         Mean         Obs.      F-stat.   
            --------------------------------------------------------------------------------
            Zinc             87.38342         8304     85.08574          885         4.02*  
            Age              42.45728         8304     40.38561          885         7.78***
            Weight            71.5006         8304     75.34783          885        23.57***
            --------------------------------------------------------------------------------

            Comment


            • #7
              Thank you, Andrew.

              Comment

              Working...
              X