Announcement

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

  • question about heatplot?

    Dear all, I want to use heatplot command to plot correlation coefficients graph.
    here is the example code.Can I add significance level for displaying with a star(* ** ***) to the graph?

    Code:
    . quietly sysuse auto, clear
    . quietly correlate price mpg trunk weight length turn foreign
    . matrix C = r(C)
    . heatplot C, values(format(%9.3f)) color(hcl, diverging intensity(.6)) ///
      legend(off) aspectratio(1) lower
    Code:
    
    


    Attached Files
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

  • #2
    @Ben Jann
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

    Comment


    • #3
      I want to add stars to show the significance level. just like the graph below,
      Attached Files
      Best regards.

      Raymond Zhang
      Stata 17.0,MP

      Comment


      • #4
        This cannot be done directly, but here's a (somewhat involved) workaround using two calls to heatplot:

        Code:
        sysuse auto, clear
        // compute correlations and p-values
        pwcorr price mpg trunk weight length turn foreign, sig
        matrix C = r(C)
        matrix sig = r(sig)
        // apply heatplot without displaying a graph, just to collect information;
        // option generate stores the information (coordinates etc) as variables
        heatplot C, values(label(sig)) lower nodraw generate
        // generate a new variable containing the text for the labels; the correlations
        // have been stored in variable _Z, the p-values in variable _Mlab
        gen str lab = string(_Z, "%9.2f") + ///
            cond(_Mlab<.001, "***", cond(_Mlab<.01, "**", cond(_Mlab<.1, "*", "")))
        // second call to heatplot useing addplot to print the marker labels
        heatplot C, color(hcl diverging, intensity(.6)) lower legend(off) aspectratio(1) ///
            addplot(scatter _Y _X, msymbol(i) mlab(lab) mlabpos(0) mlabcolor(black))
        Click image for larger version

Name:	Graph1.png
Views:	1
Size:	67.5 KB
ID:	1624725


        Or if you want the stars below the numbers:

        Code:
        sysuse auto, clear
        // compute correlations and p-values
        pwcorr price mpg trunk weight length turn foreign, sig
        matrix C = r(C)
        matrix sig = r(sig)
        // apply heatplot without displaying a graph, just to collect information;
        // option generate stores the information (coordinates etc) as variables
        heatplot C, values(label(sig)) lower nodraw generate
        // significance stars
        gen str sig = cond(_Mlab<.001, "***", cond(_Mlab<.01, "**", cond(_Mlab<.1, "*", "")))
        // second call to heatplot useing addplot to print the marker labels
        heatplot C, color(hcl diverging, intensity(.6)) lower legend(off) aspectratio(1) ///
            addplot(scatter _Y _X if _Y!=_X, msym(i) mlab(_Z) mlabf(%9.2f) mlabpos(0) mlabc(black) ///
                || scatter _Y _X if _Y==_X, msym(i) mlab(_Z) mlabf(%9.0f) mlabpos(0) mlabc(black) ///
                || scatter _Y _X if _Y!=_X, msym(i) mlab(sig) mlabpos(6) mlabgap(2) mlabc(black))
        Click image for larger version

Name:	Graph2.png
Views:	1
Size:	63.1 KB
ID:	1624726


        This example also illustrates how you can use different labels on the diagonal.

        Comment


        • #5
          @Thanks a lot.I find that you have updated heatplot to version 1.0.9. And you have updated the helpfile. After updating the latest version,Now your codes works well.
          Best regards.

          Raymond Zhang
          Stata 17.0,MP

          Comment


          • #6
            Wondering if this code can show the actual p value under the correlation rather than the stars? I have Stata 17.

            Comment


            • #7
              The p-values define the stars, so you may just ask for these values to be displayed directly. You could also change the font size.

              Code:
              sysuse auto, clear
              // compute correlations and p-values
              pwcorr price mpg trunk weight length turn foreign, sig
              matrix C = r(C)
              matrix sig = r(sig)
              // apply heatplot without displaying a graph, just to collect information;
              // option generate stores the information (coordinates etc) as variables
              heatplot C, values(label(sig)) lower nodraw generate
              // significance stars
              gen str sig = cond(_Mlab<.001, "<.001", string(_Mlab, "%4.3f"))
              // second call to heatplot useing addplot to print the marker labels
              heatplot C, color(hcl diverging, intensity(.6)) lower legend(off) aspectratio(1) ///
                  addplot(scatter _Y _X if _Y!=_X, msym(i) mlab(_Z) mlabf(%9.2f) mlabpos(0) mlabc(black) ///
                      || scatter _Y _X if _Y==_X, msym(i) mlab(_Z) mlabf(%9.0f) mlabpos(0) mlabc(black) ///
                      || scatter _Y _X if _Y!=_X, msym(i) mlab(sig) mlabpos(6) mlabgap(2) mlabc(black) mlabsize(vsmall))
              Res.:

              Click image for larger version

Name:	Graph.png
Views:	1
Size:	61.1 KB
ID:	1670394

              Comment


              • #8
                Thank you, that change worked for me.

                Comment


                • #9
                  This heatplot looks like it fits my use case really well, except that I'm generating a correlation matrix using the "spearman" command with the parameters "stats(rho p)" added to it. Apart from this I'm fairly sure my code is the same as yours Andrew. However, when running the do file I get the error "variable __00000O not found". What could be causing this? I imagine it's an error due to a missing variable but I'm not sure which one it could be

                  Comment


                  • #10
                    Actually don't worry, got it all sorted now. Needed to use r(Rho) and r(P) instead of r(C) and r(sig).

                    Comment


                    • #11
                      Originally posted by Larry Gill View Post
                      Apart from this I'm fairly sure my code is the same as yours Andrew. However, when running the do file I get the error "variable __00000O not found". What could be causing this? I imagine it's an error due to a missing variable but I'm not sure which one it could be
                      The code in #7 still works for me. Make sure your installation is up to date.

                      Code:
                      ssc install heatplot, replace

                      Comment


                      • #12
                        Thanks for the help Andrew, I've got the heatplot displaying but the text is overflowing from each square of it. Is there any way to enlarge/specify the size of the colour squares (or just make the text more of a pretty display generally)?

                        Click image for larger version

Name:	Heaplot Dodgy Version.png
Views:	1
Size:	137.0 KB
ID:	1714316
                        Attached Files

                        Comment


                        • #13
                          There is some space to be recovered by eliminating the plot region margin. But ultimately, something has to give: either reduce the size of the marker labels or the size of the axes labels.

                          Code:
                          sysuse auto, clear
                          // compute correlations and p-values
                          pwcorr price mpg trunk weight length turn foreign, sig
                          matrix C = r(C)
                          matrix sig = r(sig)
                          // apply heatplot without displaying a graph, just to collect information;
                          // option generate stores the information (coordinates etc) as variables
                          heatplot C, values(label(sig)) lower nodraw generate
                          // significance stars
                          gen str sig = cond(_Mlab<.001, "<.001", string(_Mlab, "%4.3f"))
                          // second call to heatplot useing addplot to print the marker labels
                          set scheme s1color
                          heatplot C, color(hcl diverging, intensity(.6)) lower legend(off) aspectratio(1) ///
                              addplot(scatter _Y _X if _Y!=_X, msym(i) mlab(_Z) mlabf(%9.2f) mlabpos(0) mlabc(black) ///
                                  || scatter _Y _X if _Y==_X, msym(i) mlab(_Z) mlabf(%9.0f) mlabpos(0) mlabc(black) ///
                                  || scatter _Y _X if _Y!=_X, msym(i) mlab(sig) mlabpos(6) mlabgap(2) mlabc(black) ///
                                     mlabsize(vsmall) plotregion(margin(zero)))
                          Res.:

                          Click image for larger version

Name:	Graph.png
Views:	1
Size:	66.5 KB
ID:	1714327

                          Comment


                          • #14
                            Originally posted by Larry Gill View Post
                            Is there any way to enlarge/specify the size of the colour squares (or just make the text more of a pretty display generally)?
                            Here is a suggestion for modifying the axes labels based on the example in #4. To left align the y-axis labels, I alternate the y-axis and move the labels to the LHS, so this will involve manually specifying the label gap.

                            Code:
                            sysuse auto, clear
                            
                            *MODIFY LABELS
                            local ylab
                            local i 0
                            foreach var of varlist foreign turn length weight trunk mpg price{
                                local ++i  
                                local ylab `ylab' `i' "(`i') `var'"  
                            }
                            
                            
                            local xlab
                            local i 0
                            foreach var of varlist price mpg trunk weight length turn foreign{
                            local ++i  
                                local xlab `xlab' `i' "(`i')"
                            } 
                            
                            
                            
                            // compute correlations and p-values
                            pwcorr price mpg trunk weight length turn foreign, sig
                            matrix C = r(C)
                            matrix sig = r(sig)
                            // apply heatplot without displaying a graph, just to collect information;
                            // option generate stores the information (coordinates etc) as variables
                            set scheme s2color
                            heatplot C, values(label(sig)) lower nodraw generate
                            // significance stars
                            gen str sig = cond(_Mlab<.001, "<.001", string(_Mlab, "%4.3f"))
                            // second call to heatplot useing addplot to print the marker labels
                            heatplot C, color(hcl diverging, intensity(.6)) lower legend(off) aspectratio(1) ///
                                xlab(`xlab', noticks labgap(2)) ylab(`ylab', noticks nogrid labgap(-104)) ///
                                yscale(lstyle(none)) xscale(lstyle(none))  plotregion(margin(zero)) ///
                                graphregion(color(white)) ysc(alt)  ///
                                addplot(scatter _Y _X if _Y!=_X, msym(i) mlab(_Z) mlabf(%9.2f) mlabpos(0) mlabc(black) ///
                                    || scatter _Y _X if _Y==_X, msym(i) mlab(_Z) mlabf(%9.0f) mlabpos(0) mlabc(black) ///
                                    || scatter _Y _X if _Y!=_X, msym(i) mlab(sig) mlabpos(6) mlabgap(2) mlabc(black) ///
                                       mlabsize(vsmall) )
                            Click image for larger version

Name:	Graph.png
Views:	1
Size:	37.5 KB
ID:	1714371

                            Last edited by Andrew Musau; 21 May 2023, 07:22.

                            Comment


                            • #15
                              Hi. I am not managing to fit all rho and p-values into the heatplot squares (my matrix is very big = 34 x 34), and my font size seems oversized. Is there any way to reduce it with syntax? Option mlabize(vtiny) is not working (it sets it as medium, I think). I have managed to do it with graph editor, but I wondered if label size can be further reduced by means of syntax.

                              Kind regards,
                              Juan Vicente
                              StataIC16
                              Attached Files

                              Comment

                              Working...
                              X