Announcement

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

  • Automate text based on p-value in a t-test

    I am using `ttest` inside a `forvalues` loop to run the test across a bunch of values.

    Is it possible to generate inside the loop, some text based on the p value for Ha: s != 0? e.g. something even as simple as "Reject H0" if p<0.05
    Thank you for your help!

    Stata SE/17.0, Windows 10 Enterprise

  • #2
    Yes. Inside the loop, immediately after the -ttest- command you can put something like this:

    Code:
    if r(p) < 0.05 {
        display "Reject H0"
    }

    Comment


    • #3
      something like:
      Code:
      sysuse auto
      ttest price, by(foreign )
      return list
      
      foreach v of varlist price mpg rep78 headroom trunk weight {
          quietly ttest `v', by(foreign)
          if `r(p)' > 0.05 {
              display "H0 not rejected, p=`r(p)'"
              }
              
          else {
              display  "H0 rejected, p=`r(p)'"
              }
          }
      ?

      EDIT: sorry, forgot to reload the page, Didn't see, that Schechter had given an answer
      Last edited by Dennis Lund Hansen; 27 Oct 2018, 15:45. Reason: sorry, forgot to reload the page, Didn't see, that Schechter had given an answer

      Comment


      • #4
        the line:
        Code:
         
         if `r(p)' > 0.05
        shall of course be:
        Code:
         
         if `r(p)' >= 0.05

        Comment


        • #5
          Thanks! You guys are stars.
          Thank you for your help!

          Stata SE/17.0, Windows 10 Enterprise

          Comment


          • #6
            If you want to export results from ttest commands to a word document, you can use asdoc (from SSC). For example
            Code:
            ssc install asdoc
            sysuse auto, clear
            asdoc ttest rep78==0, replace title(T-test results : H0: mean = 0) tzok dec(2)
            asdoc ttest price==0, rowappend tzok dec(2)
            asdoc ttest mpg==0, rowappend tzok dec(2)
            asdoc ttest turn==0, rowappend tzok dec(2)
            asdoc ttest weight==0, rowappend tzok dec(2)
            asdoc ttest length==0, rowappend tzok dec(2)
            asdoc, text(\i Reject H0 if p<0.05)
            Click image for larger version

Name:	asdoc ttest.png
Views:	1
Size:	24.8 KB
ID:	1467805

            Last edited by Attaullah Shah; 27 Oct 2018, 16:24.
            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


            • #7
              Hi Prof. Shah, I tried to replicate your code block above, however, I keep getting an error from Microsoft word. I have posted the error message below. Is there an easy solution to this issue?

              Word was unable to read this document. It may be corrupt.
              Try one or more of the following:
              * Open and Repair the file.
              * Open the file with the Text Recovery converter.
              (Myfile.doc)

              I am using Microsoft Word for Mac version 16.14.1 and Stata 15.

              Thank you for your help.
              Last edited by Biraj Bisht; 18 Feb 2019, 09:15.

              Comment


              • #8
                Biraj Bisht I think the problem is caused by using asdoc on Mac. I remember adding support for Mac in asdoc. Can you please try the beta version from my website and tell me whether that solves the problems.


                * For installation of the new beta version
                Code:
                net install asdoc, from(http://fintechprofessor.com) replace
                help asdoc
                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


                • #9
                  The new version of asdoc has been uploaded to the SSC. Details of the updates are given in this post
                  https://www.statalist.org/forums/for...ord-from-stata
                  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

                  Working...
                  X