Announcement

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

  • Correlation table with bold values for correlations with a p-value below 0.10

    I am using the following command from the Stata Manual to create a descriptive table including the mean, standard deviation and correlations for my variables:
    Code:
    sysuse auto
    pwcorr price mpg rep78, sig
    table (result coleq) (colname), statistic(mean price mpg rep78) statistic(sd price mpg rep78 ) command(corr=(vech(r(C))') sig=(vech(r(sig))'): pwcorr price mpg rep78, sig) nformat(%5.2f mean sd)
    collect style cell result[corr], maximum(0.99, label(" - ")) nformat(%6.2f) halign(center)
    collect stars sig 0.01 "***" 0.05 "**" 0.1 "*", attach(corr) shownote
    collect layout (colname) (result[mean sd] coleq#result[corr])
    collect style header result[corr], level(hide)
    collect label levels result sd "SD", modify
    collect label levels colname price "1. Price" mpg "2. Mileage" rep78 "3. Repair record 1978", modify
    collect label levels coleq price "1." mpg "2." rep78 "3.", modify
    collect preview
    collect export CorrelationTable.docx, replace
    Instead of having starts to show the significance, I would like to make correlations bold if the p-value < 0.1 instead of showing significance stars.

    Stata Version: 18
    OS: Windows 10
    Last edited by Felix Kaysers; 06 Aug 2023, 09:09. Reason: Added information on Stata and OS.
    Cheers,
    Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2

    You will have to write loop that will add a cell style for result[corr] where coleq#colname elements of matrix sig meet your requirement.

    The following modified version of your example does this. To help visualize the change, I also add option smcl(error) so we can see that some of the correlations are red in the Stata output.
    Code:
    sysuse auto
    
    pwcorr price mpg rep78, sig
    matrix sig = vech(r(sig))'
    table (result coleq) (colname), statistic(mean price mpg rep78) statistic(sd price mpg rep78 ) command(corr=(vech(r(C))') sig=(vech(r(sig))'): pwcorr price mpg rep78, sig) nformat(%5.2f mean sd)
    collect style cell result[corr], maximum(0.99, label(" - ")) nformat(%6.2f) halign(center)
    local stripe : colfullname sig
    foreach el of local stripe {
        if sig[1,"`el'"] < 0.1 {
            tokenize "`el'", parse(":")
            collect style cell result[corr]#coleq[`1']#colname[`3'], ///
                font(, bold) smcl(error)
        }
    }
    collect layout (colname) (result[mean sd] coleq#result[corr])
    collect style header result[corr], level(hide)
    collect label levels result sd "SD", modify
    collect label levels colname price "1. Price" mpg "2. Mileage" rep78 "3. Repair record 1978", modify
    collect label levels coleq price "1." mpg "2." rep78 "3.", modify
    collect preview
    collect export CorrelationTable.docx, replace
    Here is a screenshot of the resulting table.

    Click image for larger version

Name:	Screenshot 2023-08-06 at 12.27.40 PM.png
Views:	1
Size:	195.2 KB
ID:	1723038

    Comment


    • #3
      Thank you, this work perfectly!
      Cheers,
      Felix
      Stata Version: MP 18.0
      OS: Windows 11

      Comment

      Working...
      X