Announcement

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

  • #16
    Many thanks, Andrew.
    I've tried to run your code but there is a error message.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . local byvar rep78
    
    . gen Mean= price
    
    . drop if missing(`byvar')
    (5 observations deleted)
    
    . expand 2, g(new)
    (69 observations created)
    
    . qui sum `byvar'
    
    . assert `byvar'!=99999
    
    . replace `byvar'= 99999 if new
    variable rep78 was int now long
    (69 real changes made)
    
    . bys `byvar': egen Std_Err=sd(Mean)
    
    . bys `byvar': egen Freq=count(Mean)
    
    . collapse Mean Std_Err Freq, by("`byvar'")
    
    . estpost tabstat  Mean Std_Err Freq, by("`byvar'")
    
    Summary statistics: mean
         for variables: Mean Std_Err Freq
      by categories of: rep78
    
           rep78 |   e(Mean)  e(Std_E~)    e(Freq)
    -------------+---------------------------------
               1 |    4564.5   522.5519          2
               2 |  5967.625   3579.357          8
               3 |  6429.233    3525.14         30
               4 |    6071.5   1709.608         18
               5 |      5913   2615.763         11
           99999 |  6146.043    2912.44         69
    -------------+---------------------------------
           Total |   5848.65   2477.477         23
    
    . esttab, cells("Mean Std_Err Freq") noobs nomtitle nonumber ///
    > collab(,lhs("`:var lab `byvar''")) drop(Total) ///
    > substitute("99999" "Total" "Std_Err" "Std. Err") varwidth(20)
    coefficient Total not found
    r(111);
    
    end of do-file
    
    r(111);
    
    
    
    .

    Code:
    CODE]
    
    
    sysuse auto, clear
    local byvar rep78
    gen Mean= price
    drop if missing(`byvar')
    expand 2, g(new)
    qui sum `byvar'
    assert `byvar'!=99999
    replace `byvar'= 99999 if new
    bys `byvar': egen Std_Err=sd(Mean)
    bys `byvar': egen Freq=count(Mean)
    collapse Mean Std_Err Freq, by("`byvar'")
    estpost tabstat  Mean Std_Err Freq, by("`byvar'")
    esttab, cells("Mean Std_Err Freq") noobs nomtitle nonumber ///
    collab(,lhs("`:var lab `byvar''")) drop(Total) ///
    substitute("99999" "Total" "Std_Err" "Std. Err") varwidth(20)
    *LaTeX Table
    esttab, cells("Mean Std_Err Freq") noobs nomtitle nonumber ///
    collab(,lhs("`:var lab `byvar''")) drop(Total) ///
    substitute("99999" "Total" "Std_Err" "Std. Err") varwidth(20) tex
    Last edited by Tiago Munhoz; 22 Apr 2021, 20:52.

    Comment


    • #17
      It works for me. Update estout and see if it solves it.

      Code:
      ssc install estout, replace

      Comment


      • #18
        Thanks. Now it's working fine.

        Comment


        • #19
          Is there any new way to export two-way tabulations to latex since then? I'm currently trying to export the following tables to latex. Ideally append them on top of each other.

          tab categorical_1 categorical_2, column nofreq

          tab categorical_1 categorical_2, cell nofreq

          Comment


          • #20
            If you are asking about the updated table and new collect commands, then you should start a new thread and indicate this in the title so that those who are familiar with creating such tables may easily find your question.

            Comment


            • #21
              Andrew, the test you use is not a paired test. The loss in efficiency in this case is minor, but the proper test is a paired test. I'm guessing that estpost doesn't support it. Am I wrong?

              Comment


              • #22
                Originally posted by lucas reddinger View Post
                Andrew, the test you use is not a paired test. The loss in efficiency in this case is minor, but the proper test is a paired test. I'm guessing that estpost doesn't support it. Am I wrong?
                A paired test is equivalent to running the mean command on the difference between variables.

                Code:
                webuse fuel, clear
                ttest mpg1==mpg2
                preserve
                local means mpg1 mpg2 mpg1-mpg2
                local i 0
                foreach mean of local means{
                    local ++i
                    gen diff= `mean'
                    eststo m`i': mean diff     
                    drop diff
                }
                
                esttab m1 m2 m3, ///
                cell("b(fmt(a2) pattern(1 1 0)) b(fmt(a2) star pattern(0 0 1))" ///
                "se(par fmt(a2) pattern(1 1 0)) se(par fmt(a2) pattern(0 0 1))") ///
                mlab(mpg1 mpg2 difference) collab(none) coeflab(diff "Mean") ///
                starlevels(* 0.10 ** 0.05 *** 0.01)
                restore
                Res.:

                Code:
                . ttest mpg1==mpg2
                
                Paired t test
                ------------------------------------------------------------------------------
                Variable |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
                ---------+--------------------------------------------------------------------
                    mpg1 |      12          21    .7881701    2.730301    19.26525    22.73475
                    mpg2 |      12       22.75    .9384465    3.250874    20.68449    24.81551
                ---------+--------------------------------------------------------------------
                    diff |      12       -1.75    .7797144     2.70101    -3.46614   -.0338602
                ------------------------------------------------------------------------------
                     mean(diff) = mean(mpg1 - mpg2)                               t =  -2.2444
                 H0: mean(diff) = 0                              Degrees of freedom =       11
                
                 Ha: mean(diff) < 0           Ha: mean(diff) != 0           Ha: mean(diff) > 0
                 Pr(T < t) = 0.0232         Pr(|T| > |t|) = 0.0463          Pr(T > t) = 0.9768
                
                
                . 
                . esttab m1 m2 m3, ///
                > cell("b(fmt(a2) pattern(1 1 0)) b(fmt(a2) star pattern(0 0 1))" ///
                > "se(par fmt(a2) pattern(1 1 0)) se(par fmt(a2) pattern(0 0 1))") ///
                > mlab(mpg1 mpg2 difference) collab(none) coeflab(diff "Mean") ///
                > starlevels(* 0.10 ** 0.05 *** 0.01)
                
                ------------------------------------------------------
                                      (1)          (2)          (3)   
                                     mpg1         mpg2   difference   
                ------------------------------------------------------
                Mean                   21         22.8        -1.75** 
                                   (0.79)       (0.94)       (0.78)   
                ------------------------------------------------------
                N                      12           12           12   
                ------------------------------------------------------

                Comment


                • #23
                  Thanks, that looks good. (I was responding to post #2 in which you had a paired t-test followed by a two-sample t-test.)

                  Comment

                  Working...
                  X