Announcement

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

  • heatplot for spearman correlation

    Hello, this is my code for constructing heatplot using Pearson correlation:


    pwcorr SWLS Sleepquality Sleepduration Sleephygiene Bedtimeprocrastination Age Education EmploymentStatus Income LivingSituation Nightshift Kids, sig
    matrix C = r(C)
    matrix sig = r(sig)
    heatplot C, values(label(sig)) lower nodraw nodiagonal generate
    gen str lab = string(_Z, "%4.3f") + ///
    cond(_Mlab<.001, "***", cond(_Mlab<.01, "**", cond(_Mlab<.05, "*", "")))

    heatplot C, color(hcl bluered) lower aspectratio(1) xlabel(, labsize(tiny) angle(45)) ylabel(, labsize(tiny)) nodiagonal ///
    addplot(scatter _Y _X, msymbol(i) mlab(lab) mlabpos(0) mlabcolor(black) mlabsize(tiny))


    As I have to use Spearman correlation, I have to rewrite it. This was my attempt:

    spearman SWLS Sleepquality Sleepduration Sleephygiene Bedtimeprocrastination Age Education EmploymentStatus Income LivingSituation Nightshift Kids, stats(rho p)
    matrix C = r(Rho)
    matrix sig = r(sig)
    heatplot C, values(label(sig)) lower nodraw nodiagonal generate
    gen str lab = string(_Z, "%4.3f") + ///
    cond(_Mlab<.001, "***", cond(_Mlab<.01, "**", cond(_Mlab<.05, "*", "")))

    heatplot C, color(hcl bluered) lower aspectratio(1) xlabel(, labsize(tiny) angle(45)) ylabel(, labsize(tiny)) nodiagonal ///
    addplot(scatter _Y _X, msymbol(i) mlab(lab) mlabpos(0) mlabcolor(black) mlabsize(tiny))

    However:
    . heatplot C, values(label(sig)) lower nodraw nodiagonal generate
    matrix specified in values(label()) must have same dimension as main matrix

    Can someone help me please? Thank you!
    Last edited by Adela Kralova; 27 May 2024, 11:14.

  • #2
    As documented r(rho) after spearman contains only the last correlation calculated. So, you're feeding heatplot a scalar if I understand all this correctly.

    (Presumably you did type

    Code:
    mat C = r(rho)
    not

    Code:
    mat C = r(Rho)
    -- but that could have been another error.)

    I think you may need to get the ranks first and then fire up a correlation command. That is, the Spearman correlation is the Pearson correlation of the ranks.

    I don't much like using pwcorr because it can be inconsistent on which observations are used. To those who like it, that is the point!

    Code:
    gen touse = !missing(SWLS, Sleepquality, Sleepduration, Sleephygiene, Bedtimeprocrastination, Age, Education, EmploymentStatus. Income. LivingSituation, Nightshift, Kids)
    
    foreach v in SWLS Sleepquality Sleepduration Sleephygiene Bedtimeprocrastination Age Education EmploymentStatus Income LivingSituation Nightshift Kids {
       egen rank_`v'  = rank(`v') if touse
    }
    gets consistent ranks across the variables named.

    Comment


    • #3
      Nick Cox, thank you so much! Your answer was really helpful.

      Comment


      • #4
        Nick Cox , thank you once again for your help; I appreciate it. I would like to ask you if you know how to tell Stata to use shades of blue for negative correlations and shades of red for positive correlations, so 0 = white. Thanks!

        Code:
        gen touse = !missing(SWLS, Sleepquality, Sleepduration, Sleephygiene, Bedtimeprocrastination, Age, Education, EmploymentStatus, Income, LivingSituation, Nightshift, Kids)
        
        foreach v in SWLS Sleepquality Sleepduration Sleephygiene Bedtimeprocrastination Age Education EmploymentStatus Income LivingSituation Nightshift Kids {
           egen rank_`v'  = rank(`v') if touse
        }
        
        pwcorr rank_SWLS rank_Sleepquality rank_Sleepduration rank_Sleephygiene rank_Bedtimeprocrastination rank_Age rank_Education rank_EmploymentStatus rank_Income rank_LivingSituation rank_Nightshift rank_Kids, sig
        matrix C = r(C)
        matrix sig = r(sig)
        heatplot C, values(label(sig)) lower nodraw nodiagonal generate
        gen str lab = string(_Z, "%4.3f") + ///
            cond(_Mlab<.001, "***", cond(_Mlab<.01, "**", cond(_Mlab<.05, "*", "")))
        
        heatplot C, color(hcl bluered) lower aspectratio(1) xlabel(, labsize(tiny) angle(45)) ylabel(, labsize(tiny))  nodiagonal ///
            addplot(scatter _Y _X, msymbol(i) mlab(lab) mlabpos(0) mlabcolor(black) mlabsize(tiny))


        Attached Files
        Last edited by Adela Kralova; 27 May 2024, 15:31.

        Comment


        • #5
          Sorry; I haven't used heatplot that I can recall.

          Comment


          • #6
            Continued at https://www.statalist.org/forums/for...lation-colours

            Comment

            Working...
            X