Announcement

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

  • Plotting p-value of ttest in twoway bar graph

    Hello,

    I have used the following code to create a bar plot showing the mean time in medium-to-vigorous physical activity (MVPA) split up by gender und Walkscore (a measure of the walkability of a neighborhood). I was wondering whether it is possible to plot the p-value of a ttest into the graph? Either manually or by automatically transferring it from the output of the ttest (preferred). Thanks a lot for your hints in advance.

    collapse (mean) mmvpa_all=mvpa_all (sd) sdmvpa_all=mvpa_all (count) n=mvpa_all, by(girl ws_half) cw
    gen himvpa_all=mmvpa_all + invttail(n-1,0.025)*(sdmvpa_all/sqrt(n))
    gen lomvpa_all=mmvpa_all - invttail(n-1,0.025)*(sdmvpa_all/sqrt(n))
    gen girlws=girl if ws_half==1
    replace girlws=girl+3 if ws_half==2
    twoway (bar mmvpa_all girlws if girl==0) ///
    (bar mmvpa_all girlws if girl==1) ///
    (rcap himvpa_all lomvpa_all girlws, lc(black)), ///
    legend(row(1) order(1 "Male" 2 "Female")) ///
    xlabel(0.5 "Low" 3.5 "High", noticks) ///
    xtitle("Walkscore") ytitle("Mean Time in MVPA")

  • #2
    Indeed; all you need to do is save the results somewhere. I wouldn't use a bar chart as you have done; if there are just two categories, there is plenty of scope for something much more informative. Here I use stripplot (SSC), but the technique of saving results is one you could copy.
    Click image for larger version

Name:	stripplot_ttest.png
Views:	1
Size:	11.7 KB
ID:	939995


    Code:
    sysuse auto, clear
    ttest mpg, by(foreign)
    local text1 =  "means " + string(`r(mu_1)', "%2.1f") + "  " + string(`r(mu_2)', "%2.1f")
    local text2 =  "{it:t} statistic " + string(`r(t)', "%4.3f") + "  {it:P}-value " + string(`r(p)', "%06.4f")
    stripplot mpg, over(foreign) cumul cumprob bar t1title(`text1') t2title(`text2') yla(, ang(h)) vertical boffset(0) height(0.6) xla(,  ang(-.001) labgap(2) noticks)
    P.S. As the sign of the t statistic is just arbitrarily dependent on the coding of categories, it would make more sense just to show its absolute value, which can be coded too.
    Last edited by Nick Cox; 02 Mar 2015, 08:39.

    Comment


    • #3
      Naturally it would be even better if the confidence intervals were related not to group summaries but to the difference between the means which is the focus of the test. That's a project for another day.

      Comment


      • #4
        Hello Nick
        I tried to replicate the stata commands from #2
        It says
        "option cumul not allowed
        r(198);
        "
        after the strip plot command.

        Thank you,
        Anwar

        Comment


        • #5
          Anwar:

          Thanks for your interest.

          Your stripplot is out-of-date. That on SSC is

          *! 2.5.2 NJC 23 September 2014

          typing

          Code:
          which stripplot, all
          will, I conjecture, show you that you need to update. For documentation of the update
          see e.g. http://www.statalist.org/forums/foru...updated-on-ssc
          Last edited by Nick Cox; 02 Mar 2015, 11:59.

          Comment


          • #6
            Thank you
            Got it
            Anwar

            Comment


            • #7
              Thanks a lot for your help.

              Comment


              • #8
                Can stripplot only be used for independent samples t test? I wanted to use it for paired-samples t test with two different variables but it did not accept the syntax.

                Comment


                • #9
                  RE #8

                  Code:
                  webuse "lutkepohl2.dta", clear 
                  
                  ttest ln_inc == ln_consump 
                  local text1 =  "means " + string(`r(mu_1)', "%2.1f") + "  " + string(`r(mu_2)', "%2.1f")
                  local text2 =  "{it:t} statistic " + string(`r(t)', "%4.3f") + "  {it:P}-value " + string(`r(p)', "%06.4f")
                  
                  stripplot ln_inc ln_consump,  cumul cumprob bar t1title(`text1') t2title(`text2') /// 
                             yla(, ang(h)) vertical boffset(0) height(0.6) xla(,  ang(-.001) labgap(2) noticks)

                  Comment


                  • #10
                    Horses for courses. I am happy if #9 is helpful. But that plot necessarily says nothing about the paired nature of the data and is further from the point of the test in #8 than would be say a plot of differences between variables versus their means.

                    https://www.stata-journal.com/articl...article=gr0005 cycles around this theme.

                    Naturally you could calculate the differences directly and look at their distribution, but then I would want to use a normal quantile plot as reference.

                    Comment

                    Working...
                    X