Announcement

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

  • How to draw matrix graph and correlation matrix graph as an integrated table

    Dear Stata users,

    I find a graph in a R teaching website. The graph (I attach it below) combines matrix graph and correlation matrix graph together which provides more informations than individual graph. I know some community-contributed commands such as -corrtable- to display a correlation matrix (and optionally corresponding sample sizes and/or P-values) as a table. I wonder is there any command or any way to draw matrix graph and correlation matrix graph as an integrated table in Stata? Thank you.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id wt2 ht2 wt9 ht9 lg9 st9 wt18 ht18 lg18 st18 soma)
    201 13.6 90.2 41.5 139.4 31.6 74 110.2   179 44.1 226   7
    202 12.7 91.4   31 144.3   26 73  79.4 195.1 36.1 252   4
    203 12.6 86.4 30.1 136.5 26.6 64  76.3 183.7 36.9 216   6
    204 14.8 87.6 34.1 135.4 28.2 75  74.5 178.7 37.3 220   3
    205 12.7 86.7 24.5 128.9 24.2 63  55.7 171.5   31 200 1.5
    206 11.9 88.1 29.8   136 26.7 77  68.2 181.8   37 215   3
    207 11.5 82.2   26 128.5 26.5 45  78.2 172.5 39.1 152   6
    209 13.2 83.8 30.1 133.2 27.6 70  66.5 174.6 37.3 189   4
    210 16.9   91 37.9 145.6   29 61  70.5 190.4 33.9 183   3
    211 12.7 87.4   27 132.4   26 74  57.3 173.8 33.3 193   3
    212 11.4 84.2 25.9 133.7 25.8 68  50.3 172.6 31.6 202   3
    213 14.2 88.4 31.1 138.3 27.3 59  70.8 185.2 36.6 208   4
    214 17.2 87.7 34.6 134.6 30.6 87  73.7 178.4 39.2 227   3
    215 13.7 89.6 34.6   139 28.9 71  75.2 177.6 36.8 204 2.5
    216 14.2 91.4 43.1   146 32.4 98  83.1 183.5   38 226   4
    217 15.9   90 33.2 133.2 28.5 82  74.3 178.1 37.8 233 2.5
    218 14.3 86.4 30.7 133.3 27.3 73  72.2   177 36.5 237   2
    219 13.3   90 31.6 130.3 27.5 68  88.6 172.9 40.4 230   7
    221 13.8 91.4 33.4 144.5   27 92  75.9 188.4 36.5 250   1
    222 11.3 81.3 29.4 125.4 27.7 70  64.9 169.4 35.7 236   3
    223 14.3 90.6 30.2 135.8 26.7 70  65.6 180.2 35.4 177   4
    224 13.4 92.2 31.1 139.9 27.2 63  66.4   189 35.3 186   4
    225 12.2 87.1 27.6 136.8 25.8 73    59 182.4 33.5 199   3
    226 15.9 91.4 32.3 140.6 27.9 69  68.1 185.8 34.2 227   1
    227 11.5 89.7   29 138.6 24.6 61  67.7 180.7 34.3 164   4
    228 14.2 92.2 31.4   140 28.2 74  68.5 178.7   37 219   2
    end

    Click image for larger version

Name:	ex13_bgs-1.png
Views:	1
Size:	285.6 KB
ID:	1775430


  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id wt2 ht2 wt9 ht9 lg9 st9 wt18 ht18 lg18 st18 soma)
    201 13.6 90.2 41.5 139.4 31.6 74 110.2   179 44.1 226   7
    202 12.7 91.4   31 144.3   26 73  79.4 195.1 36.1 252   4
    203 12.6 86.4 30.1 136.5 26.6 64  76.3 183.7 36.9 216   6
    204 14.8 87.6 34.1 135.4 28.2 75  74.5 178.7 37.3 220   3
    205 12.7 86.7 24.5 128.9 24.2 63  55.7 171.5   31 200 1.5
    206 11.9 88.1 29.8   136 26.7 77  68.2 181.8   37 215   3
    207 11.5 82.2   26 128.5 26.5 45  78.2 172.5 39.1 152   6
    209 13.2 83.8 30.1 133.2 27.6 70  66.5 174.6 37.3 189   4
    210 16.9   91 37.9 145.6   29 61  70.5 190.4 33.9 183   3
    211 12.7 87.4   27 132.4   26 74  57.3 173.8 33.3 193   3
    212 11.4 84.2 25.9 133.7 25.8 68  50.3 172.6 31.6 202   3
    213 14.2 88.4 31.1 138.3 27.3 59  70.8 185.2 36.6 208   4
    214 17.2 87.7 34.6 134.6 30.6 87  73.7 178.4 39.2 227   3
    215 13.7 89.6 34.6   139 28.9 71  75.2 177.6 36.8 204 2.5
    216 14.2 91.4 43.1   146 32.4 98  83.1 183.5   38 226   4
    217 15.9   90 33.2 133.2 28.5 82  74.3 178.1 37.8 233 2.5
    218 14.3 86.4 30.7 133.3 27.3 73  72.2   177 36.5 237   2
    219 13.3   90 31.6 130.3 27.5 68  88.6 172.9 40.4 230   7
    221 13.8 91.4 33.4 144.5   27 92  75.9 188.4 36.5 250   1
    222 11.3 81.3 29.4 125.4 27.7 70  64.9 169.4 35.7 236   3
    223 14.3 90.6 30.2 135.8 26.7 70  65.6 180.2 35.4 177   4
    224 13.4 92.2 31.1 139.9 27.2 63  66.4   189 35.3 186   4
    225 12.2 87.1 27.6 136.8 25.8 73    59 182.4 33.5 199   3
    226 15.9 91.4 32.3 140.6 27.9 69  68.1 185.8 34.2 227   1
    227 11.5 89.7   29 138.6 24.6 61  67.7 180.7 34.3 164   4
    228 14.2 92.2 31.4   140 28.2 74  68.5 178.7   37 219   2
    end
    
    local vars = "ht9 lg9 st9"
    
    local k : word count `vars'
    forvalues row = 1/`k' {
        forvalues col = `=`row'+1'/`k' {
            local rowvar : word `row' of `vars'
            local colvar : word `col' of `vars'
            
            scatter `colvar' `rowvar', ///
            name(gr`row'_`col', replace)
        }
    }
    local row = 1
    foreach var of varlist `vars' {
        twoway kdensity `var', ///
        xtitle("`var'") ///
         name(gr`row'_`row++', replace)
    }
    forvalues col = 1/`k' {
        forvalues row = `=`col'+1'/`k' {
            local rowvar : word `row' of `vars'
            local colvar : word `col' of `vars'
            cor `rowvar' `colvar'
            twoway scatteri 1 1 (0) "corr:", msymbol(i) mlabcolor(black) mlabsize(*3) || ///
                   scatteri 0 1 (0) "`:display %9.3g `r(rho)''", ///
                   msymbol(i) mlabcolor(black) mlabsize(*3) ///
                   yscale(range(-2 3) off)  xscale(off) ///
                   legend(off) ylab(,noticks nogrid) ///
                   xlab(,noticks nogrid) ///
                   name(gr`row'_`col', replace)
        }
    }
    
    local gr = ""
    forvalues col = 1/`k' {
        forvalues row = 1/`k' {
            local gr = "`gr' gr`row'_`col'"
        }
    }
    
    graph combine `gr', imargin(zero)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	123.0 KB
ID:	1775533
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you very much Maarten Buis. R's corresponding command ggpairs is very simple, but I don't know whether its code behind-the-scene is simple or complicated.

      Comment


      • #4
        You could easily wrap my code in a program with simple syntax if you want.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Dear Maarten Buis, yes, of course. I can try to make an ado program to wrap your code. I checked Nick Cox's -corrtable- command, however, I am not yet able to hack it, or else I will not need to reinvent the wheel.

          Comment


          • #6
            Just to confirm if confirmation helps that I have no plans to extend corrtable in this or any other direction. Feel free to take the code and build on it.

            For myself, I recognise that people might want to enhance scatter plots with a correlation or equivalent measure. I have done that myself using e..g. aaplot from SSC.

            @Maarten Buis's code is very neat and does what was intended very well, but the business (busy-ness?) of finding the correlation for each or even any scatter plot on the other side of the diagonal might seem a complication too far.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              but the business (busy-ness?) of finding the correlation for each or even any scatter plot on the other side of the diagonal might seem a complication too far.
              My thoughts exactly. Why not place the correlations directly within the scatterplots?

              Comment


              • #8
                Dear Nick Cox Andrew Musau, thank you for your attentions. I agree with Nick's point. When I read the R's plot in #1 carefully, I do find that it is hard to set up a corresponding relationship between scatterplots in lower triangle and corrtables in upper triangle. It is easy to check lower triangle or upper triangle separately, however, thus we do not need to get them together in one plot. For me, the easiest way is to place the correlations directly within the scatterplots (as Andrew said), the second easiest way is to place the correlations aside the scatterplots, and the hard way is to put them in opposite triangles. Of course, if people do want to have a square plot composed of two triangles, they'd better read the two triangles separately.

                Comment

                Working...
                X