Announcement

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

  • Getting exact numbers when extracting p-values from ttests

    Hello,

    I wrote code that uses local macros and matrices to extract p-values from many ttests, and outputs them to excel. However, at some point the p-values get rounded to the nearest hundredth (i.e., a p-value of 0.0008 will show up in my document as 0. Using the below code, how would I adjust so that this rounding does not occur?

    Code:
    local ttest "var1 var2 var3"
    local n: word count `ttest'
    local i=1
    foreach var of local ttest {
    ttest `var', by(CST4v1)
    // local pvalue: display %05.4f chi2tail(r(df), r(chi2))
    local pvalue: display (r(p))
    mat r`i'=  `pvalue'
    local ++i
    }
    
    forvalues j=2/`n'{
    local all "`all' \r`j'"
    }
    mat R= r1`all'
    mat rownames R = `ttest'
    mat colnames R= "P-value"
    
    putexcel set ttest_doc
    putexcel A1=matrix(R), names
    Thanks so much!
    Clare

  • #2
    I don't know where you're losing precision. I don't know anything much about putexcel but your code can be slimmed down and still produce good-looking results:

    Code:
    sysuse auto, clear 
    
    local ttest "mpg price weight"
    
    foreach var of local ttest {
        ttest `var', by(foreign)
        mat R = nullmat(R) \ `r(p)' 
    }
    
    mat rownames R = `ttest'
    mat colnames R= "P-value"
    
    
    mat li R
    
    R[3,1]
              P-value
       mpg  .00052542
     price  .68018509
    weight  2.622e-08
    
    mat li R, format(%08.6f)
    
    R[3,1]
             P-value
       mpg  0.000525
     price  0.680185
    weight  0.000000

    Comment


    • #3
      Thanks, Nick! That code is definitely prettier and outputs exact p-values in Stata.

      Appreciate your help!

      Comment


      • #4
        Hi,

        I have a follow-up question for this: I am now trying to extract p-values from ranksum tests. I have adjusted the p-value statement to calculate the p-value from the ranksum scalars. However, my code is only outputting a matrix with the first variable's ranksum p-value, and I don't know why.

        Here is my loop:
        Code:
        local radial "pq_rad_key_zc_fd pq_rad_key_zc_td pq_rad_key_gamma_real_1h pq_rad_key_buckberg_index"
        local n: word count `radial'
        local i=1
        
        foreach var of local radial {
        ranksum `var', by(crispandpec) 
        local pvalue: display(2 * normprob(-abs(r(z))))
        mat r`i' = `pvalue' 
        local ++i
        }
        
        forvalues j=2/`n' {
        local all "`all' \r`j'"
        }
        mat R= r1`all'
        mat rownames R = `radial'
        mat colnames R = "P-value" 
        mat li R, format(%08.6f)
        Thanks!
        Clare

        Comment

        Working...
        X