Announcement

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

  • Heatplot help

    Hi I am sorry to bother you I am using STATA S/E 18 and trying to do a heatplot I have several variables of interest
    auc_cpeptidebaseline auc_cpeptide12mon final0child final12child final24child final36child final48child

    I have got the graph how I like it
    using the following code

    pwcorr auc_cpeptidebaseline auc_cpeptide12mon final0child final12child final24child final36child final48child , sig
    matrix C = r(C)
    matrix sig = r(sig)

    heatplot C, values (label(sig)) lower nodraw generate
    // significance stars
    gen str sig = cond(_Mlab<.001, "***", cond(_Mlab<.01, "**", cond(_Mlab<.05, "*", "")))

    heatplot C, color(hcl diverging, intensity(.6)) lower 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))

    However I only want the graph to show how auc_cpeptidebaseline and auc_cpeptide12mon relate to the others I am not interested in how final24child and final36child relate etc is there anyway to do this. I can edit graph tp make axis look niceI only want it to show first 2 columns essentially
    All help gratefully received



  • #2
    Graph I have made is this
    Attached Files

    Comment


    • #3
      Extract the submatrix you want to graph, and use that instead. See -help matrix extraction-.

      Code:
      matrix C_tograph = C[. .. .,"auc_cpeptidebaseline" .. "auc_cpeptide12mon"]
      matrix sig_tograph = sig[. .. .,"auc_cpeptidebaseline" .. "auc_cpeptide12mon"]

      Comment


      • #4
        I suspect the issue is that OP wants all the rows, but only the first two columns.

        Comment


        • #5
          Yes, that's what the above code produces.

          Comment


          • #6
            Ah of course. Sorry, didn't pay adequate attention to your code.

            Comment


            • #7
              I had a similar question but to use spearman instead of pearson

              Comment


              • #8
                Hi Doug, whether you want to graph results of pwcorr or spearman, the method is similar. The difference lies that pwcorr and spearman have different matrix names. Below is an example. And as you can see, pwcorr stores pairwise correlation matrix as r(C) and significance level of each correlation coefficient as r(sig), whereas, spearman stores rho as r(Rho) and two-sided p-value as r(P). By the way, OP's code comes from Ben Jann and Andrew Musau in here: https://www.statalist.org/forums/for...heatplot/page2

                Code:
                sysuse auto, clear
                
                // compute correlations and p-values
                spearman price mpg trunk weight length turn foreign, stats(rho p)
                matrix C = r(Rho)
                matrix sig = r(P)
                matrix list C
                matrix list sig
                matrix C_tograph = C[....,"price" . . "weight"]
                matrix sig_tograph = sig[....,"price" . . "weight"]
                
                // apply heatplot without displaying a graph, just to collect information;
                // option generate stores the information (coordinates etc) as variables
                heatplot C_tograph, values(label(sig_tograph)) 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
                
                format _Z %9.2f
                
                heatplot C_tograph, color(hcl diverging, intensity(.6)) lower legend(off) aspectratio(1) ///
                    addplot(scatter _Y _X if _Y!=_X, msym(i) mlab(_Z) mlabpos(0) mlabc(black) mlabsize(2) ///
                        || scatter _Y _X if _Y==_X, msym(i) mlab(_Z)  mlabpos(0) mlabc(black) ///
                        || scatter _Y _X if _Y!=_X, msym(i) mlab(sig) mlabpos(6) mlabgap(2) mlabc(black) ///
                           mlabsize(2) xlab(, noticks) ylab(, noticks) plotregion(margin(zero)))
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	160.7 KB
ID:	1771729

                Comment


                • #9
                  If you want a heat plot for such results, a minimal requirement, I suggest, is that positive and negative correlations display as different colours and (for correlations) that intensity conveys magnitude.

                  @Chen Samulsion's display does I think do that. Chen's colours are to me the wrong way round, but that may be cultural.

                  Beyond that, I find the heat plot design distracting for here about 10 values. The reader has to decode different patches of colour; the numbers are given but they can be shown more directly.

                  Without understanding most of the detail I roughed out a quite different design with the aim that showing that different displays should be considered.

                  Fond as I am of graphics, it is not obvious to me that any design so far is much better than a table.

                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input float(corr1 corr2) str29 which float order
                  -09 -19 "0"                             1
                  -03 -05 "12"                            2
                  -26  15 "child 24"                            3
                  -25 -11 "36"                            4
                  -27 -15 "48"                            5
                     .    . ""                              6
                     .    . "AUC peptide: baseline & 12" 7
                  end
                  
                  gen corr3 = 21 in 7 
                  
                  foreach i in 1 2 3 4 5 7 {
                      label def order `i' "`=which[`i']'", add 
                  }
                  
                  label val order order 
                  
                  twoway scatter order corr1, ms(Oh) mla(corr1) mlabpos(6) mlabsize(medsmall) ///
                  || scatter order corr2, ms(+) mla(corr2) mlabpos(12)  mlabsize(medsmall) ///
                  || scatter order corr3, ms(Dh) mla(corr3) mlabpos(6)  mlabsize(medsmall) ///
                  ysc(reverse) ysc(r(. 7.5)) yla(1/5 7, tlc(none) glp(solid) glw(vthin) valuelabel) ///
                  xline(0, lp(solid)) xla(, nogrid) ytitle("") ///
                  legend(order(1 "baseline" 2 "12 months") row(1) pos(12)) subtitle(Correlations x 100)
                  Click image for larger version

Name:	notaheatplot.png
Views:	1
Size:	36.1 KB
ID:	1771749

                  Comment

                  Working...
                  X