Announcement

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

  • Changing display format of correlations

    A colleague wanted display of correlations from correlate with fewer decimal places. The best answer I could come up with was pushing the resulting matrix through matrix list.

    Code:
    *  You can pick up the correlation result as a matrix and display the elements as you wish. Or you can push that into your own matrix, which is not so ephemeral. 
    
    . sysuse auto, clear
    (1978 automobile data)
    
    . corr length-gear_ratio
    (obs=74)
    
                 |   length     turn displa~t gear_r~o
    -------------+------------------------------------
          length |   1.0000
            turn |   0.8643   1.0000
    displacement |   0.8351   0.7768   1.0000
      gear_ratio |  -0.6964  -0.6763  -0.8289   1.0000
    
    . mat li r(C), format(%9.2f)
    
    symmetric r(C)[4,4]
                        length          turn  displacement    gear_ratio
          length          1.00
            turn          0.86          1.00
    displacement          0.84          0.78          1.00
      gear_ratio         -0.70         -0.68         -0.83          1.00
    
    . mat rho = r(C)
    
    . mat list rho, format(%3.2f)
    
    symmetric rho[4,4]
                        length          turn  displacement    gear_ratio
          length          1.00
            turn          0.86          1.00
    displacement          0.84          0.78          1.00
      gear_ratio         -0.70         -0.68         -0.83          1.00
    The header text -- here symmetric rho[4.4] -- could be suppressed with an option noheader.

    A similar trick should work with commands for rank correlations.

    The code for pwcorr could be hacked to allow a user-specified format. But in that case, as in the others, I agree with my colleague's implication that there should be an option on the official commands to do this directly. Am I missing something simpler?

    Incidentally, corrci from the Stata Journal has such an option, and several others, but there are always users unwilling or unable to install community-contributed commands.

    Code:
     
    SJ-21-3 pr0041_4  . . . . . . . . . . . . . . . . . Software update for corrci
            (help corrci, corrcii if installed) . . . . . . . . . . . .  N. J. Cox
            Q3/21   SJ 21(3):847
            improves explanation of the format() option and fixes a bug
            concerning saving results to a new dataset
    
    SJ-20-4 pr0041_3  . . . . . . . . . . . . . . . . . Software update for corrci
            (help corrci, corrcii if installed) . . . . . . . . . . . .  N. J. Cox
            Q4/20   SJ 20(4):1028--1030
            corrects code for a bias correction used if (and only if) the
            fisher option is specified
    
    SJ-17-3 pr0041_2  . . . . . . . . . . . . . . . . . Software update for corrci
            (help corrci, corrcii if installed) . . . . . . . . . . . .  N. J. Cox
            Q3/17   SJ 17(3):779
            new options added
    
    SJ-10-4 pr0041_1  . . . . . . . . . . . . . . . . . Software update for corrci
            (help corrci, corrcii if installed) . . . . . . . . . . . .  N. J. Cox
            Q4/10   SJ 10(4):691
            update to fix corrci so that it always saves r-class results
    
    SJ-8-3  pr0041  .  Speaking Stata: Corr. with confidence, Fisher's z revisited
            (help corrci, corrcii if installed) . . . . . . . . . . . .  N. J. Cox
            Q3/08   SJ 8(3):413--439
            reviews Fisher's z transformation and its inverse, the
            hyperbolic tangent, and reviews their use in inference
            with correlations
    
     . corrci length-gear_ratio
    
    (obs=74)
    
                               correlations and 95% limits
    length       turn              0.864    0.792    0.913
    length       displacement      0.835    0.750    0.893
    length       gear_ratio       -0.696   -0.798   -0.556
    turn         displacement      0.777    0.667    0.854
    turn         gear_ratio       -0.676   -0.784   -0.530
    displacement gear_ratio       -0.829   -0.889   -0.741
    
    . corrci length-gear_ratio, format(%3.2f)
    
    (obs=74)
    
                               correlations and 95% limits
    length       turn               0.86     0.79     0.91
    length       displacement       0.84     0.75     0.89
    length       gear_ratio        -0.70    -0.80    -0.56
    turn         displacement       0.78     0.67     0.85
    turn         gear_ratio        -0.68    -0.78    -0.53
    displacement gear_ratio        -0.83    -0.89    -0.74
    
    * corrci has other options, including matrix output and saving to a dataset

  • #2
    Here is an alternative way of doing this without downloading community contributed packages. It is more lines of code, but has the advantage that afterwards you can directly export the table to LaTeX, Word, Excel, and other formats using collect export.

    Code:
    . clear all
    
    . sysuse auto, clear
    (1978 automobile data)
    
    . corr length-gear_ratio
    (obs=74)
    
                 |   length     turn displa~t gear_r~o
    -------------+------------------------------------
          length |   1.0000
            turn |   0.8643   1.0000
    displacement |   0.8351   0.7768   1.0000
      gear_ratio |  -0.6964  -0.6763  -0.8289   1.0000
    
    
    . collect get corr=vech(r(C))
    
    . collect style header result, level(hide)
    
    . collect style cell, nformat(%9.2f)
    
    . collect layout (rowname#result) (roweq)
    
    Collection: default
          Rows: rowname#result
       Columns: roweq
       Table 1: 4 x 4
    
    -----------------------------------------------------------------------------------------
                           | Length (in.) Turn circle (ft.) Displacement (cu. in.) Gear ratio
    -----------------------+-----------------------------------------------------------------
    Length (in.)           |         1.00                                                    
    Turn circle (ft.)      |         0.86              1.00                                  
    Displacement (cu. in.) |         0.84              0.78                   1.00           
    Gear ratio             |        -0.70             -0.68                  -0.83       1.00
    -----------------------------------------------------------------------------------------
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      I recall suggesting on Statalist at some earlier date that the cformat, sformat, pformat suite of settings be expanded to accommodate other results' displays (e.g. correlation matrixes). This seems to me an obvious and easy solution, but maybe one that would be challenging to implement under the hood.
      Code:
      . sysuse auto
      (1978 automobile data)
      
      . reg price mpg
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(1, 72)        =     20.26
             Model |   139449474         1   139449474   Prob > F        =    0.0000
          Residual |   495615923        72  6883554.48   R-squared       =    0.2196
      -------------+----------------------------------   Adj R-squared   =    0.2087
             Total |   635065396        73  8699525.97   Root MSE        =    2623.7
      
      ------------------------------------------------------------------------------
             price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879
             _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03
      ------------------------------------------------------------------------------
      
      . corr price mpg
      (obs=74)
      
                   |    price      mpg
      -------------+------------------
             price |   1.0000
               mpg |  -0.4686   1.0000
      
      
      . set cformat %9.2f
      
      . reg price mpg
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(1, 72)        =     20.26
             Model |   139449474         1   139449474   Prob > F        =    0.0000
          Residual |   495615923        72  6883554.48   R-squared       =    0.2196
      -------------+----------------------------------   Adj R-squared   =    0.2087
             Total |   635065396        73  8699525.97   Root MSE        =    2623.7
      
      ------------------------------------------------------------------------------
             price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               mpg |    -238.89      53.08    -4.50   0.000      -344.70     -133.09
             _cons |   11253.06    1170.81     9.61   0.000      8919.09    13587.03
      ------------------------------------------------------------------------------
      
      . corr price mpg
      (obs=74)
      
                   |    price      mpg
      -------------+------------------
             price |   1.0000
               mpg |  -0.4686   1.0000
      
      
      .

      Comment


      • #4
        #2 Maarten Buis helpfully explained how to use collect. An advantage of this route is that you get to see variable labels. There's presumably a route that uses variable names only when you prefer them or there is not enough space for variable labels.

        #3 John Mullahy Indeed in practice those options seem quite independent of correlate commands.

        Comment


        • #5
          It's actually possible to wrap what Maarten Buis proposed in a -table- one-liner:

          Code:
          sysuse auto, clear
          
          table (rowname) (roweq), command(vech(r(C)): corr length-gear_ratio) nformat ("%9.2f")

          And to drop variable labels one can run this afterwards:
          Code:
          collect label drop rowname
          collect label drop roweq
          collect preview

          Comment


          • #6
            Evgeny Saburov Thanks to you too. #5 certainly shows how the collect code can be compressed to one line.

            I want to flag, just in case any readers missed it, that the first work-around in #1 doesn't hinge on installing anything extra.

            I still favour something like an explicit format() option for these commands -- or a little more subtly rformat(), pformat(), etc.

            As John Mullahy hinted a generic setting would also be welcome. so that rformat defaults to some format with 4 d.p. but is honoured by correlation commands if you set it to something different When I learned about correlations in the 1960s they were almost always presented with 2 d.p. Giving more d.p. seems on a par with grade inflation and is defensible insofar as a researcher can always round but knife-edge cases can always exist. 0.2345 might mean 0.23449 or 0.23451, and so on. Sure, you can always look at saved results, but the issue is only wanting small changes from StataCorp to ease a researcher's work.

            Comment

            Working...
            X