Announcement

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

  • Output t-test results

    Hi,

    I performed t-tests for many variables in my dataset. I have written code as following:

    foreach var of varlist var1 var2 var3 var4 {
    di "`var'"
    ttest `var', by(event_indicator)
    }

    How can I only save t-statistic and p-value from these results?

    Many thanks!

  • #2
    You don't say where you want to save them. There are several options: you can save them in a new data set, you can save them as local macros, you can save them in a matrix. You can also save them in the data set you started with, although there doesn't appear to be any sensible place in this data set to put them, so I'd leave that one aside at least for now.

    On the supposition that you want to save them in a separate Stata data set:

    Code:
    capture postutil clear
    postfile handle str32 variable float (t_stat p_value) using ttest_results, replace
    
    foreach v of varlist var1 var2 var3 var4 {
        display "`var'"
        ttest `var', by(event_indicator)
        post handle ("`var'") (`r(t)') (`r(p)')
    }
    
    postclose handle
    At the end of this code the new file ttest_results.dta will contain one observation for each variable in your original varlist, followed by the corresponding t-statistic and p-value.

    Note: Code not tested because no example data was provided. Beware of typos or other errors.

    In the future, when asking for help with code, please use the -dataex- command to post an example of your data. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      In this thread, I show how to use asdoc to export ttest results to MS Word. The use of asdoc is pretty easy. You need to just add asdoc to the beginning of any Stata. command.
      For those who are not yet familiar with asdoc, asdoc can be downloaded from SSC and can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command. You can also watch several YouTube videos that show the use of asdoc

      In your case, the following code should work

      Code:
      * Install asdoc
      ssc install asdoc
      
      foreach var of varlist var1 var2 var3 var4 {
         
           asdoc ttest `var', by(event_indicator)
      }
      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


      • #4
        Thank you so much! I'm new to statalist. It's my first time learning about -dataex-. Next time I will post a sample of my data. Thanks for the advice!

        Comment


        • #5
          I forgot to add the rowappend option to the asdoc command, so the correct code would be

          Code:
           * Install asdoc
           ssc install asdoc
           foreach var of varlist var1 var2 var3 var4 {
                   asdoc ttest `var', by(event_indicator) rowappend
          }
          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


          • #6
            Hi Attaullah Shah , I just used your asdoc command for the first time, as I am currently searching for a neat way to output two-group mean-comparison tests. First of all, very nice and hand-on command, great!
            Using
            Code:
            estpost ttest "Variables", by(group)
            I get the same result, however using additionally the unequal option gives me different results. I assume that your command is assuming equal standard deviations? Is there also an unequal option implemented in asdoc ttest?
            Thanks in advance.

            Comment


            • #7
              Dear Attaullah Shah
              As pointed by Florian Schramm , what is the way to get unequal results.

              For my case, I tried @ Clyde Schechter #2 but I couldnt get " the new file ttest_results.dta". I am attaching a demo of what I have done

              Code:
              input str1 firm float (cashflow assets sales) int year float industry
              "a" 100 500 300 1991 1
              "a" 125 550 410 1992 1
              "a" 129 550 350 1993 1
              "a" 118 450 216 1994 1
              "a" 96 600 175 1995 1
              "b" 350 1500 600 1991 1
              "b" 560 1675 850 1992 1
              "b" 730 1300 755 1993 1
              "b" 900 1800 1065 1994 1
              "b" 1050 2000 1800 1995 1
              "c"  60 120 155 1991 2
              "c"  -10  120 180 1992 2
              "c"  50 160 168 1993 2
              "c"  200 150 260 1994 2
              "c"  -60 140 200 1995 2
              "d" 155  230 200 1991 2
              "d" 255 398 400 1992 2
              "d" 179 398 268 1993 2
              "d" 196 423 318 1994 2
              "d" 165 300 215 1995 2
              end
              
              
              capture postutil clear
              postfile handle str32 variable float (t_stat p_value) using ttest_results, replace
              foreach var of varlist  cashflow  assets  sales  {
                  display "`var'"
                  ttest `var', by( industry ) unequal
                  post handle ("`var'") (`r(t)') (`r(p)')
              }
              postclose handle
              I cross-checked and find "unequal" option works, but I couldn't get the results in a separate file (dta
              Last edited by lal mohan kumar; 31 Mar 2021, 02:45.

              Comment


              • #8
                Clyde Schechter The code works fine. Some how my directory was changed were results got stored

                Comment


                • #9
                  Dear all
                  How to get the mean difference as an additional variable in the code posted by Clyde Schechter (#2) I tried the below code, but I dont know how to take it for 3 requirements

                  Code:
                  capture postutil clear
                  postfile handle str3 variable float (diff t_stat p_value) using ttest_results, replace
                  foreach var of varlist *_w age  {
                      display "`var'"
                      ttest `var', by( list_unlist ) unequal
                      post handle ("`var'")  (`r(t)') (`r(diff)') (`r(p)')
                  }
                  postclose handle
                  
                  invalid syntax
                  post:  above message corresponds to expression 3, variable t_stat
                  r(198);

                  Comment


                  • #10
                    The issue is that there is no such scalar as -r(diff)- stored in r() after ttest. You need to calculate this as -r(mu_1)- minus -r(mu_2)-. Also, consider the order of the statistics.

                    Code:
                    capture postutil clear
                    postfile handle str3 variable float (t_stat diff p_value) using ttest_results, replace
                    foreach var of varlist *_w age  {
                        display "`var'"
                        ttest `var', by( list_unlist ) unequal
                        post handle ("`var'")  (`r(t)') (`=`r(mu_1)'- `r(mu_2)'') (`r(p)')
                    }
                    postclose handle

                    Comment


                    • #11
                      Thanks a lot Andrew Musau

                      Comment


                      • #12
                        Andrew Musau Please help me how i can post the ttest and p value results to a word document. I have read all the posts under this thread and tried to run the given code, for eg

                        Code:
                         capture postutil clear
                        postfile handle str32 variable float (t_stat p_value) using ttest_results, replace 
                         foreach v of varlist var1 var2 var3 var4 {     display "`var'"     ttest `var', by(event_indicator)     post handle ("`var'") (`r(t)') (`r(p)') }  postclose handle
                        i got a response as below
                        (file ttest_results.dta not found). I then use the same code on my sample data as shared by dataex below


                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input float(qdate beta herd4 herd5)
                        189   .4586343    .5259123   -.067278
                        190   .2936215   -6.574652   6.868274
                        191 -.22337463    5.537047  -5.760422
                        192   .3705105    3.113961 -2.7434506
                        193 -.02737196   -.4873249  .45995295
                        194 -.02881779     4.99395  -5.022768
                        195   .1827204    1.310633 -1.1279125
                        196   .3091845 -.002421041   .3116055
                        197  -.8219998   -9.716269    8.89427
                        198  -.4085979   -.6943686  .28577068
                        199  -.1040041      3.5533  -3.657304
                        200 .013869005    9.677533  -9.663664
                        201  .17271216   114.27222  -114.0995
                        202  .05642999   119.07375 -119.01733
                        203  -.4056795    6.943067  -7.348747
                        204  -.1838167   1.4199234   -1.60374
                        205 .036806118    23.73501 -23.698206
                        206   .3218515   19.156345 -18.834494
                        207 -.08603384   -4.076575   3.990541
                        208  -.3245456  -18.297401  17.972857
                        end
                        format %tq qdate
                        I want to find the average of my variables, obtain t statistic (for each of the var) and p value and so i run the following code

                        Code:
                        tabstat beta herd4 herd5
                        ttest beta = 0
                        test herd4 =0
                        ttest herd5 = 0
                        please help me, how i can post the results to a table in word. Many thanks

                        Comment


                        • #13
                          tabstat beta herd4 herd5
                          ttest beta = 0
                          ttest herd4 =0
                          ttest herd5 = 0

                          I would use a regression for this. Below, I use estout from SSC to output in rtf format which you may open in MS Word.

                          Code:
                          * Example generated by -dataex-. For more info, type help dataex
                          clear
                          input float(qdate beta herd4 herd5)
                          189   .4586343    .5259123   -.067278
                          190   .2936215   -6.574652   6.868274
                          191 -.22337463    5.537047  -5.760422
                          192   .3705105    3.113961 -2.7434506
                          193 -.02737196   -.4873249  .45995295
                          194 -.02881779     4.99395  -5.022768
                          195   .1827204    1.310633 -1.1279125
                          196   .3091845 -.002421041   .3116055
                          197  -.8219998   -9.716269    8.89427
                          198  -.4085979   -.6943686  .28577068
                          199  -.1040041      3.5533  -3.657304
                          200 .013869005    9.677533  -9.663664
                          201  .17271216   114.27222  -114.0995
                          202  .05642999   119.07375 -119.01733
                          203  -.4056795    6.943067  -7.348747
                          204  -.1838167   1.4199234   -1.60374
                          205 .036806118    23.73501 -23.698206
                          206   .3218515   19.156345 -18.834494
                          207 -.08603384   -4.076575   3.990541
                          208  -.3245456  -18.297401  17.972857
                          end
                          format %tq qdate
                          
                          frame put *, into(ttest)
                          frame ttest{
                              rename (beta herd4 herd5) var=
                              reshape long var, i(qdate) j(which) string
                              encode which, gen(cat)
                              regress var ibn.cat, nocons robust
                              esttab . using myfile.rtf, replace label cells("b(fmt(3)) p(fmt(3))") nonumb nomtitle collab(Mean p-value)
                          }
                          frame drop ttest
                          Res.:

                          Code:
                          . esttab ., label cells("b(fmt(3)) p(fmt(3))") nonumb nomtitle collab(Mean p-value)
                          
                          ----------------------------------------------
                                                       Mean      p-value
                          ----------------------------------------------
                          beta                       -0.020        0.780
                          herd4                      13.673        0.098
                          herd5                     -13.693        0.097
                          ----------------------------------------------
                          Observations                   60            
                          ----------------------------------------------

                          Comment


                          • #14
                            Andrew Musau Many thanks. Am grateful for your help.

                            Comment


                            • #15
                              Attaullah Shah hi! I came across this forum and want to use asdoc. however I'm having trouble installing. the stata error is:

                              . net install asdoc, from(http://fintechprofessor.com) replace
                              connection timed out -- see help r(2) for troubleshooting
                              http://fintechprofessor.com/ either
                              1) is not a valid URL, or
                              2) could not be contacted, or
                              3) is not a Stata download site (has no stata.toc file).
                              r(2);

                              Is your website down? Thank you!!

                              Comment

                              Working...
                              X