Announcement

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

  • Problem adding the p-value of a Kruskal-wallis test to a table created with commands "table" and "collect" in Stata 17

    Dear all,

    This is my first post in the community (and also learning how to use Stata), so I hope I manage to follow all the rules. I am using the commands "table" and "collect" to build a table in Stata 17. According to a webinar on the topic presented by Chuck Huber (and hoping that I understood it correctly), to add the results of tests (like p-values), one runs the command for the test and then runs "return list" like in the following example:

    ttest age, by(highbp)
    return list


    After this, appears a list of scalars that can be added to the table. My problem is that I need to add the p-value of a Kruskal_Wallis test and when I run "return list," I only obtain the following scalars:

    kwallis f1dpi, by(f1dpiquar)

    Kruskal–Wallis equality-of-populations rank test

    +----------------------------+
    | f1dpiq~r | Obs | Rank sum |
    |----------+-----+-----------|
    | 1 | 972 | 472878.00 |
    | 2 | 981 | 1.44e+06 |
    | 3 | 960 | 2.34e+06 |
    | 4 | 966 | 3.28e+06 |
    +----------------------------+

    chi2(3) = 3635.579
    Prob = 0.0001

    chi2(3) with ties = 3635.609
    Prob = 0.0001

    return list

    scalars:
    r(chi2_adj) = 3635.609319306581
    r(df) = 3
    r(chi2) = 3635.579049760407

    There is no scalar for the p-value. Is there a way to add the statistical significance to the table if it does not appear as a scalar? Thank you so much for your help.

    Magda


  • #2
    You can calculate this and add to r() using the -chi2tail()- function. However, the p-value is ambiguous here. Does it refer to the \(\chi^2\) test or the Adjusted \(\chi^2\) test? Something for you to think about!

    Code:
    cap prog drop mystats
    program mystats, rclass
    return scalar p_adj= chi2tail(r(df), r(chi2_adj))
    return scalar Chi2_adj =  r(chi2_adj)
    return scalar  DF =  r(df)
    return scalar p= chi2tail(r(df), r(chi2))
    return scalar  Chi2 =   r(chi2)
    end
    
    sysuse census, clear
    kwallis medage, by(region)
    mystats
    return list
    Res.:

    Code:
    . kwallis medage, by(region)
    
    Kruskal-Wallis equality-of-populations rank test
    
      +--------------------------+
      | region  | Obs | Rank Sum |
      |---------+-----+----------|
      | NE      |   9 |   376.50 |
      | N Cntrl |  12 |   294.00 |
      | South   |  16 |   398.00 |
      | West    |  13 |   206.50 |
      +--------------------------+
    
    chi-squared =    17.041 with 3 d.f.
    probability =     0.0007
    
    chi-squared with ties =    17.062 with 3 d.f.
    probability =     0.0007
    
    .
    . mystats
    
    .
    . return list
    
    scalars:
                   r(Chi2) =  17.0408088235294
                      r(p) =  .000693214758395
                     r(DF) =  3
               r(Chi2_adj) =  17.06211085869512
                  r(p_adj) =  .0006862559273437
    
    .
    Last edited by Andrew Musau; 22 Aug 2022, 10:32.

    Comment


    • #3
      Dear Andrew,

      Thank you so much for sharing the code with me. It worked, and now I got the p-value as a scalar.

      Kind regards,

      Magda

      Comment

      Working...
      X