Announcement

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

  • Correlations table with groups of variables in the rows

    Hello,

    I'm trying to make a correlation table with cpcorr by @Nick Cox.
    I have no problem implementing the code; what I would like, however, is to create groups of variables in the rows, such as group A and from below the variables, group B and from below the variables, using their labels, and to export to Latex in horizontal order.

    Also, I would like, in addition to the correlations, to display the covariance and P-values in the same cell with a note at the bottom of the table noting correlations, covariance, and the p values

    I am using the following code, and sample data is provided below.

    Code:
    cpcorr fd ka ext dom pr la \ type1 type2 indicator1 decade1 decade2 decade3
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id year) str97 country str1 group float(fd ka ext dom pr la type1 type2 indicator1 decade) byte(decade1 decade2 decade3)
    1 1992 "Country1" "A"   .524175   2.321955  .75 .7777778 .071428575        1 0 1       12.2 1990 1 0 0
    1 1993 "Country1" "A"  .5846328   2.321955  .75 .7777778 .071428575        1 0 1  17.553352 1990 1 0 0
    1 1994 "Country1" "A"  .6478685   2.321955  .75 .9444444 .071428575 .7912164 0 1      19.11 1990 1 0 0
    1 2000 "Country1" "A"  .8414965   1.048859  .75        1  .53571427 .7782844 0 1     28.634 2000 0 1 0
    1 2001 "Country1" "A"   .873997   1.048859  .75        1   .6190476 .7782844 0 1   26.41307 2000 0 1 0
    1 2002 "Country1" "A"  .8985876   1.048859  .75        1   .6190476 .7661784 0 1   5.536341 2000 0 1 0
    1 2003 "Country1" "A"  .9015374   1.048859  .75        1   .6190476 .7661784 0 1   5.536341 2000 0 1 0
    1 2004 "Country1" "A"  .9041975   1.048859  .75        1   .6190476 .7661784 0 1   7.350179 2000 0 1 0
    1 2010 "Country1" "A"   .941813   1.048859 .875        1   .8452381 .7704133 0 1   6.241802 2010 0 0 1
    1 2011 "Country1" "A"   .934702   1.048859 .875        1   .8452381 .7704133 0 1      5.068 2010 0 0 1
    1 2012 "Country1" "A"  .9079577   1.303478 .875        1   .8452381 .7704133 0 1      5.068 2010 0 0 1
    1 2013 "Country1" "A"  .8901507  1.5580974 .875        1   .8452381 .7694158 0 1   7.929047 2010 0 0 1
    2 1992 "Country2" "A"   .399736   .6099069 .875 .7777778          0 .5697657 0 1        1.6 1990 1 0 0
    2 1993 "Country2" "A"   .461959  2.0673358 .875 .9444444          0 .5697657 0 1   6.996447 1990 1 0 0
    2 1994 "Country2" "A"  .4953545   2.321955 .875 .9444444          0 .5697657 0 1    8.69136 1990 1 0 0
    2 2000 "Country2" "A"  .7018992   2.321955    1        1   .4404762 .5697657 0 1   2.364474 2000 0 1 0
    2 2001 "Country2" "A"  .7035186   2.321955    1        1   .5952381 .5697657 0 1   2.364474 2000 0 1 0
    2 2002 "Country2" "A"  .6993845   2.321955    1        1   .5952381 .5457992 0 1  -.7668521 2000 0 1 0
    2 2003 "Country2" "A"  .7077955   2.321955    1        1   .5952381 .5457992 0 1 -2.9592714 2000 0 1 0
    2 2004 "Country2" "A"   .728983   2.321955    1        1   .5952381 .5457992 0 1  -2.950783 2000 0 1 0
    2 2010 "Country2" "A"  .7894757   2.321955    1 .9444444   .9166667 .5718316 0 1      3.891 2010 0 0 1
    2 2011 "Country2" "A"  .8007692   2.321955    1 .9444444   .9166667 .5718316 0 1      3.891 2010 0 0 1
    2 2012 "Country2" "A"  .7593077   2.321955    1 .9444444   .9166667 .5718316 0 1 -2.5410414 2010 0 0 1
    2 2013 "Country2" "A"  .7435991   2.321955    1 .9444444   .9166667 .5718316 0 1  -6.333579 2010 0 0 1
    3 1992 "Country3" "B"  .5840359   2.321955    1 .7777778  .08333334 .6281998 0 1   .3835026 1990 1 0 0
    3 1993 "Country3" "B"  .6147648   2.321955    1 .8888889  .08333334 .6281998 0 1   .3835026 1990 1 0 0
    3 1994 "Country3" "B"   .632942   2.321955    1 .8888889  .08333334 .6260909 0 1   .8169867 1990 1 0 0
    3 2000 "Country3" "B"  .7692201   2.321955    1 .8888889        .75 .6297867 0 1  1.9049623 2000 0 1 0
    3 2001 "Country3" "B"  .7502941   2.321955    1 .8888889        .75 .6297867 0 1  1.9049622 2000 0 1 0
    3 2002 "Country3" "B"  .7244223   2.321955    1 .8888889   .8333333 .6297867 0 1   1.283737 2000 0 1 0
    3 2003 "Country3" "B"  .7401305   2.321955    1 .8888889   .8333333 .6297867 0 1 -1.3254085 2000 0 1 0
    3 2004 "Country3" "B"  .7518873   2.321955    1 .8888889   .8333333 .6547069 0 1 -1.3254085 2000 0 1 0
    3 2010 "Country3" "B"  .7356597   2.321955    1 .9444444          1 .6547069 0 1  1.1606808 2010 0 0 1
    3 2011 "Country3" "B"  .7201834   2.321955    1 .9444444          1 .6547069 0 1  1.1606808 2010 0 0 1
    3 2012 "Country3" "B"  .7283527   2.321955    1 .9444444          1 .6547069 0 1  1.1606808 2010 0 0 1
    3 2013 "Country3" "B"  .7077386   2.321955    1 .9444444          1 .6547069 0 1  1.1165322 2010 0 0 1
    4 1992 "Country4" "B"  .3452282   .6099069 .875 .5555556 .071428575 .6121462 0 1   2.615249 1990 1 0 0
    4 1993 "Country4" "B"  .3790323  2.0673358    1 .7222222 .071428575 .6121462 0 1  1.4437795 1990 1 0 0
    4 1994 "Country5" "C"  .4175321   2.321955    1 .7222222 .071428575 .6121462 0 1  4.0116615 1990 1 0 0
    4 2000 "Country5" "C"   .759881   2.321955    1 .8888889   .7619048 .6121462 0 1    5.97681 2000 0 1 0
    4 2001 "Country5" "C"  .7410694   2.321955    1 .8888889   .7619048 .6121462 0 1   7.402936 2000 0 1 0
    4 2002 "Country5" "C"  .7281089   2.321955    1 .8888889   .8333333 .6121462 0 1      8.534 2000 0 1 0
    4 2003 "Country5" "C"   .735869   2.321955    1 .8888889   .8333333 .6263371 0 1      8.534 2000 0 1 0
    4 2004 "Country5" "C"  .7334849   2.321955    1 .8888889   .8333333 .6263371 0 1      8.534 2000 0 1 0
    4 2010 "Country5" "C"  .7798166   2.321955    1 .9444444   .9166667 .6263371 0 1     -16.25 2010 0 0 1
    4 2011 "Country5" "C"  .7637033   2.321955    1 .9444444   .9166667 .6263371 0 1 -14.330358 2010 0 0 1
    4 2012 "Country5" "C"  .7608096   2.321955    1 .9444444   .9166667 .6263371 0 1          0 2010 0 0 1
    4 2013 "Country5" "C"  .7455279   2.321955    1        1   .9166667  .607849 0 1   .6806767 2010 0 0 1
    5 1992 "Country6" "C"  .1757301          .    .        .          .        . 0 1          . 1990 1 0 0
    5 1993 "Country6" "C"  .1761862          .    .        .          .        . 1 0          . 1990 1 0 0
    5 1994 "Country6" "C"  .1562944          .    .        .          .        . 1 0          . 1990 1 0 0
    5 2000 "Country6" "C"  .2008834 -1.2261546    .        .          .        . 1 0          . 2000 0 1 0
    5 2001 "Country6" "C" .17261477 -1.2261546    .        .          .        . 1 0          . 2000 0 1 0
    5 2002 "Country6" "C" .21249928 -1.2261546    .        .          .        . 1 0          . 2000 0 1 0
    5 2003 "Country6" "C"  .2206576 -.02334493    .        .          .        . 1 0          . 2000 0 1 0
    5 2004 "Country6" "C" .23541167 -.02334493    .        .          .        . 1 0          . 2000 0 1 0
    5 2010 "Country6" "C" .27683625   1.249751    .        .          .        . 0 1          . 2010 0 0 1
    5 2011 "Country6" "C" .27487376   1.249751    .        .          .        . 0 1          . 2010 0 0 1
    5 2012 "Country6" "C" .27767062   1.249751    .        .          .        . 0 1          . 2010 0 0 1
    5 2013 "Country6" "C"  .2884341   1.249751    .        .          .        . 0 1          . 2010 0 0 1
    6 1992 "Country7" "C" .25534174  -.1539507 .625       .5          0 .8271327 1 0          . 1990 1 0 0
    6 1993 "Country7" "C" .28078705  -.1539507 .625       .5  .16666667 .8271327 1 0          . 1990 1 0 0
    6 1994 "Country7" "C"  .3567602  -.1539507 .625 .5555556  .16666667 .8271327 1 0          . 1990 1 0 0
    6 2000 "Country7" "C"  .4190661 -1.2261546 .625 .6111111  .23809524 .8271327 0 1          . 2000 0 1 0
    6 2001 "Country7" "C"  .3738855 -1.2261546 .625 .6666667   .3928571 .8271327 0 1          . 2000 0 1 0
    6 2002 "Country7" "C"   .404098 -1.2261546 .625 .6666667   .3928571 .8271327 0 1          . 2000 0 1 0
    6 2003 "Country7" "C"  .4157713 -1.2261546 .875 .6666667   .3928571 .7653575 0 1          . 2000 0 1 0
    6 2004 "Country7" "C"  .4085605 -1.2261546 .875 .6666667  .53571427 .7653575 0 1          . 2000 0 1 0
    6 2010 "Country7" "C"  .4888775 -.02334493 .875 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
    6 2011 "Country7" "C"  .4789392 -.02334493 .875 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
    6 2012 "Country7" "C"  .4922497 -.02334493 .875 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
    6 2013 "Country7" "C"   .508882 -.02334493  .75 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
    7 1992 "Country8" "D" .56565696   2.321955    1 .9444444   .7738096 .8009957 0 1  13.403038 1990 1 0 0
    7 1993 "Country8" "D"  .6403931   2.321955    1 .9444444   .7738096 .8070084 0 1       12.1 1990 1 0 0
    7 1994 "Country8" "D"  .6921942   2.321955    1 .9444444   .8452381 .8070084 0 1       12.1 1990 1 0 0
    7 2000 "Country8" "D"  .8552948   2.321955    1        1   .9285714 .8314556 0 1      1.806 2000 0 1 0
    7 2001 "Country8" "D"  .8805222   2.321955    1        1   .9285714 .8314556 0 1   1.958802 2000 0 1 0
    7 2002 "Country8" "D"  .8265579   2.321955    1        1   .9285714 .8314556 0 1      2.076 2000 0 1 0
    7 2003 "Country8" "D"  .8580942   2.321955    1        1          1 .8314556 0 1      2.076 2000 0 1 0
    7 2004 "Country8" "D"  .8708485   2.321955    1        1          1 .8314556 0 1      2.076 2000 0 1 0
    7 2010 "Country8" "D"  .8683383   2.321955    1        1          1 .8314556 0 1   3.564247 2010 0 0 1
    7 2011 "Country8" "D"  .8672714   2.321955    1 .9444444          1 .8314556 0 1   5.454876 2010 0 0 1
    7 2012 "Country8" "D"  .8684435   2.321955    1 .8888889          1 .8429364 0 1   5.454876 2010 0 0 1
    7 2013 "Country8" "D"  .8866873   2.321955    1 .9444444          1 .8429364 0 1   5.454876 2010 0 0 1
    8 1992 "Country8" "D"  .5748482   2.321955    1 .8888889  .52380955 .9702252 0 1       14.9 1990 1 0 0
    8 1993 "Country8" "D"  .6702072   2.321955    1 .8888889  .52380955 .9702252 0 1   .0375137 1990 1 0 0
    8 1994 "Country8" "D"  .6812267   2.321955    1 .8888889  .52380955 .9702252 0 1      -.781 1990 1 0 0
    8 2000 "Country8" "D"  .8790284   2.321955    1        1        .75 .9702252 0 1      1.463 2000 0 1 0
    8 2001 "Country8" "D"   .889005   2.321955    1        1        .75 .9702252 0 1   5.542341 2000 0 1 0
    8 2002 "Country8" "D"  .8849987   2.321955    1        1        .75 .9702252 0 1      5.767 2000 0 1 0
    8 2003 "Country8" "D"  .8916724   2.321955    1        1        .75 .9702252 0 1      5.767 2000 0 1 0
    8 2004 "Country8" "D"  .8910962   2.321955    1        1        .75 .9702252 0 1      5.767 2000 0 1 0
    8 2010 "Country8" "D"  .8951228   2.321955    1        1   .8333333 .9702252 0 1       -.36 2010 0 0 1
    8 2011 "Country8" "D"  .8785759   2.321955    1        1   .8333333 .9702252 0 1       -.36 2010 0 0 1
    8 2012 "Country8" "D"  .8894278   2.321955    1        1   .8333333 .9702252 0 1       -.36 2010 0 0 1
    8 2013 "Country8" "D"  .8901961   2.321955    1        1   .8333333 .9702252 0 1   1.298654 2010 0 0 1
    end

  • #2
    Your needs go far beyond what cpcorr can do, so the problem is wide open.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Your needs go far beyond what cpcorr can do, so the problem is wide open.

      Any ideas how to address that?

      Comment


      • #4
        The new table command is highly flexible. All I can guess is that it may get you closer to what you want than cpcorr will. I have no ambitions to extend the scope of cpcorr (from SSC, given that people may be wondering). Your table is quite easy to specify verbally but you will need custom code to get there.

        Comment


        • #5

          If you have Stata 17 or newer, you can use the collect with
          cpcorr to build your table for LaTeX. One challenge here is that
          cpcorr does not apply the same row and column names of
          r(C) to r(p), so I had to write a short (byable)
          wrap-around program to accomplish this. Also the example data does not
          have labels on the variables, but if it did, then those labels would
          show up in the table instead of the variable names.

          Here is the code I used.
          Code:
          program mod_cpcorr, rclass byable(recall)
              syntax anything [if] [in] [, *]
              marksample touse
              cpcorr `anything' if `touse', `options'
              tempname C p
              matrix `C' = r(C)
              matrix `p' = r(p)
              matrix colname `p' = `:colfullname `C', quote'
              matrix rowname `p' = `:rowfullname `C', quote'
              return scalar N = r(N)
              return matrix C = `C'
              return matrix p = `p'
          end
          
          sort group
          
          by group : ///
              collect corr=(r(C)) pvalue=(r(p)) : ///
              mod_cpcorr fd ka ext dom pr la \ ///
                  type1 type2 indicator1 decade1 decade2 decade3
          by group : ///
              collect cov=(r(C)) : ///
              mod_cpcorr fd ka ext dom pr la \ ///
                  type1 type2 indicator1 decade1 decade2 decade3, covariance
          
          * apply some style choices on the results of interest
          collect style cell result[cov], sformat("(%s)")
          collect style cell result[corr cov] , nformat(%12.2f)
          collect style cell result[pvalue] , nformat(%5.3f)
          * define composite result named show
          collect composite define show = corr cov pvalue, trim
          * hide result names from the header
          collect style header result, level(hide)
          * add note detailing the cell contents
          collect notes "Correlations, (covariances), and p-values."
          
          collect layout (group#rowname) (colname#result[show])
          
          collect export table.tex, replace
          Here is the table.
          Code:
          . collect layout (group#rowname) (colname#result[show])
          
          Collection: default
                Rows: group#rowname
             Columns: colname#result[show]
             Table 1: 28 x 6
          
          ---------------------------------------------------------------------------------------------------------------------
                |          type1          type2          indicator1             decade1             decade2             decade3
          ------+--------------------------------------------------------------------------------------------------------------
          A     |
            fd  | . (0.00) 1.000 . (0.00) 1.000   0.06 (0.08) 0.798 -0.83 (-0.06) 0.000   0.29 (0.02) 0.175   0.47 (0.04) 0.022
            ka  | . (0.00) 1.000 . (0.00) 1.000 -0.30 (-1.69) 0.150   0.19 (0.05) 0.386 -0.15 (-0.05) 0.490 -0.02 (-0.00) 0.943
            ext | . (0.00) 1.000 . (0.00) 1.000 -0.76 (-0.72) 0.000 -0.37 (-0.02) 0.073 -0.04 (-0.00) 0.846   0.39 (0.02) 0.063
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.12 (-0.08) 0.578 -0.76 (-0.02) 0.000   0.52 (0.02) 0.009   0.16 (0.01) 0.456
            pr  | . (0.00) 1.000 . (0.00) 1.000 -0.35 (-1.01) 0.096 -0.91 (-0.13) 0.000   0.10 (0.02) 0.648   0.73 (0.12) 0.000
            la  | . (0.00) 1.000 . (0.00) 1.000   0.65 (0.79) 0.001   0.26 (0.02) 0.214 -0.15 (-0.01) 0.485 -0.09 (-0.01) 0.692
          B     |
            fd  | . (0.00) 1.000 . (0.00) 1.000 -0.37 (-0.06) 0.198 -0.82 (-0.06) 0.000   0.52 (0.04) 0.058   0.32 (0.02) 0.261
            ka  | . (0.00) 1.000 . (0.00) 1.000 -0.46 (-0.24) 0.096 -0.43 (-0.10) 0.127   0.24 (0.05) 0.413   0.20 (0.04) 0.490
            ext | . (0.00) 1.000 . (0.00) 1.000 -0.44 (-0.02) 0.114 -0.37 (-0.01) 0.190   0.21 (0.00) 0.478   0.18 (0.00) 0.549
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.35 (-0.04) 0.222 -0.67 (-0.04) 0.008   0.20 (0.01) 0.497   0.50 (0.03) 0.066
            pr  | . (0.00) 1.000 . (0.00) 1.000 -0.15 (-0.07) 0.621 -0.98 (-0.20) 0.000   0.38 (0.08) 0.185   0.64 (0.12) 0.014
            la  | . (0.00) 1.000 . (0.00) 1.000 -0.30 (-0.01) 0.296 -0.70 (-0.01) 0.005 -0.04 (-0.00) 0.881   0.79 (0.01) 0.001
          C     |
            fd  | . (0.00) 1.000 . (0.00) 1.000 -0.23 (-0.23) 0.525 -0.99 (-0.03) 0.000   0.23 (0.01) 0.524   0.37 (0.02) 0.291
            ka  | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.40 (-0.27) 0.254 -0.85 (-0.02) 0.002 -0.16 (-0.01) 0.659   0.69 (0.03) 0.028
            pr  | . (0.00) 1.000 . (0.00) 1.000 -0.27 (-0.64) 0.451 -0.97 (-0.08) 0.000   0.12 (0.02) 0.745   0.47 (0.06) 0.166
            la  | . (0.00) 1.000 . (0.00) 1.000 -0.42 (-0.03) 0.228 -0.29 (-0.00) 0.414 -0.13 (-0.00) 0.721   0.31 (0.00) 0.382
          D     |
            fd  | . (0.00) 1.000 . (0.00) 1.000 -0.62 (-0.31) 0.001 -0.96 (-0.05) 0.000   0.44 (0.02) 0.030   0.42 (0.02) 0.040
            ka  | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.37 (-0.07) 0.077 -0.73 (-0.01) 0.000   0.60 (0.01) 0.002   0.04 (0.00) 0.858
            pr  | . (0.00) 1.000 . (0.00) 1.000 -0.16 (-0.11) 0.467 -0.65 (-0.04) 0.001   0.16 (0.01) 0.467   0.43 (0.03) 0.035
            la  | . (0.00) 1.000 . (0.00) 1.000 -0.34 (-0.11) 0.107 -0.09 (-0.00) 0.687   0.03 (0.00) 0.899   0.05 (0.00) 0.813
          ---------------------------------------------------------------------------------------------------------------------
          Correlations, (covariances), and p-values.
          This table is rather wide, but you can use collect layout to select
          columns and export them separately. For example
          Code:
          . collect layout (group#rowname) (colname[type1 type2]#result[show])
          
          Collection: default
                Rows: group#rowname
             Columns: colname[type1 type2]#result[show]
             Table 1: 28 x 2
          
          -------------------------------------
                |          type1          type2
          ------+------------------------------
          A     |
            fd  | . (0.00) 1.000 . (0.00) 1.000
            ka  | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr  | . (0.00) 1.000 . (0.00) 1.000
            la  | . (0.00) 1.000 . (0.00) 1.000
          B     |
            fd  | . (0.00) 1.000 . (0.00) 1.000
            ka  | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr  | . (0.00) 1.000 . (0.00) 1.000
            la  | . (0.00) 1.000 . (0.00) 1.000
          C     |
            fd  | . (0.00) 1.000 . (0.00) 1.000
            ka  | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr  | . (0.00) 1.000 . (0.00) 1.000
            la  | . (0.00) 1.000 . (0.00) 1.000
          D     |
            fd  | . (0.00) 1.000 . (0.00) 1.000
            ka  | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr  | . (0.00) 1.000 . (0.00) 1.000
            la  | . (0.00) 1.000 . (0.00) 1.000
          -------------------------------------
          Correlations, (covariances), and p-values.
          
          . collect export table_type.tex, replace
          (collection default exported to file table_type.tex)
          
          .
          . collect layout (group#rowname) (colname[indicator1]#result[show])
          
          Collection: default
                Rows: group#rowname
             Columns: colname[indicator1]#result[show]
             Table 1: 28 x 1
          
          ---------------------------
                |          indicator1
          ------+--------------------
          A     |
            fd  |   0.06 (0.08) 0.798
            ka  | -0.30 (-1.69) 0.150
            ext | -0.76 (-0.72) 0.000
            dom | -0.12 (-0.08) 0.578
            pr  | -0.35 (-1.01) 0.096
            la  |   0.65 (0.79) 0.001
          B     |
            fd  | -0.37 (-0.06) 0.198
            ka  | -0.46 (-0.24) 0.096
            ext | -0.44 (-0.02) 0.114
            dom | -0.35 (-0.04) 0.222
            pr  | -0.15 (-0.07) 0.621
            la  | -0.30 (-0.01) 0.296
          C     |
            fd  | -0.23 (-0.23) 0.525
            ka  |      . (0.00) 1.000
            ext |      . (0.00) 1.000
            dom | -0.40 (-0.27) 0.254
            pr  | -0.27 (-0.64) 0.451
            la  | -0.42 (-0.03) 0.228
          D     |
            fd  | -0.62 (-0.31) 0.001
            ka  |      . (0.00) 1.000
            ext |      . (0.00) 1.000
            dom | -0.37 (-0.07) 0.077
            pr  | -0.16 (-0.11) 0.467
            la  | -0.34 (-0.11) 0.107
          ---------------------------
          Correlations, (covariances), and p-values.
          
          . collect export table_ind.tex, replace
          (collection default exported to file table_ind.tex)
          
          .
          . collect layout (group#rowname) (colname[decade1 decade2 decade3]#result[show]
          > )
          
          Collection: default
                Rows: group#rowname
             Columns: colname[decade1 decade2 decade3]#result[show]
             Table 1: 28 x 3
          
          -------------------------------------------------------------------
                |             decade1             decade2             decade3
          ------+------------------------------------------------------------
          A     |
            fd  | -0.83 (-0.06) 0.000   0.29 (0.02) 0.175   0.47 (0.04) 0.022
            ka  |   0.19 (0.05) 0.386 -0.15 (-0.05) 0.490 -0.02 (-0.00) 0.943
            ext | -0.37 (-0.02) 0.073 -0.04 (-0.00) 0.846   0.39 (0.02) 0.063
            dom | -0.76 (-0.02) 0.000   0.52 (0.02) 0.009   0.16 (0.01) 0.456
            pr  | -0.91 (-0.13) 0.000   0.10 (0.02) 0.648   0.73 (0.12) 0.000
            la  |   0.26 (0.02) 0.214 -0.15 (-0.01) 0.485 -0.09 (-0.01) 0.692
          B     |
            fd  | -0.82 (-0.06) 0.000   0.52 (0.04) 0.058   0.32 (0.02) 0.261
            ka  | -0.43 (-0.10) 0.127   0.24 (0.05) 0.413   0.20 (0.04) 0.490
            ext | -0.37 (-0.01) 0.190   0.21 (0.00) 0.478   0.18 (0.00) 0.549
            dom | -0.67 (-0.04) 0.008   0.20 (0.01) 0.497   0.50 (0.03) 0.066
            pr  | -0.98 (-0.20) 0.000   0.38 (0.08) 0.185   0.64 (0.12) 0.014
            la  | -0.70 (-0.01) 0.005 -0.04 (-0.00) 0.881   0.79 (0.01) 0.001
          C     |
            fd  | -0.99 (-0.03) 0.000   0.23 (0.01) 0.524   0.37 (0.02) 0.291
            ka  |      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            ext |      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            dom | -0.85 (-0.02) 0.002 -0.16 (-0.01) 0.659   0.69 (0.03) 0.028
            pr  | -0.97 (-0.08) 0.000   0.12 (0.02) 0.745   0.47 (0.06) 0.166
            la  | -0.29 (-0.00) 0.414 -0.13 (-0.00) 0.721   0.31 (0.00) 0.382
          D     |
            fd  | -0.96 (-0.05) 0.000   0.44 (0.02) 0.030   0.42 (0.02) 0.040
            ka  |      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            ext |      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
            dom | -0.73 (-0.01) 0.000   0.60 (0.01) 0.002   0.04 (0.00) 0.858
            pr  | -0.65 (-0.04) 0.001   0.16 (0.01) 0.467   0.43 (0.03) 0.035
            la  | -0.09 (-0.00) 0.687   0.03 (0.00) 0.899   0.05 (0.00) 0.813
          -------------------------------------------------------------------
          Correlations, (covariances), and p-values.
          
          . collect export table_decade.tex, replace
          (collection default exported to file table_decade.tex)

          Comment


          • #6
            Originally posted by Jeff Pitblado (StataCorp) View Post
            If you have Stata 17 or newer, you can use the collect with
            cpcorr to build your table for LaTeX. One challenge here is that
            cpcorr does not apply the same row and column names of
            r(C) to r(p), so I had to write a short (byable)
            wrap-around program to accomplish this. Also the example data does not
            have labels on the variables, but if it did, then those labels would
            show up in the table instead of the variable names.

            Here is the code I used.
            Code:
            program mod_cpcorr, rclass byable(recall)
            syntax anything [if] [in] [, *]
            marksample touse
            cpcorr `anything' if `touse', `options'
            tempname C p
            matrix `C' = r(C)
            matrix `p' = r(p)
            matrix colname `p' = `:colfullname `C', quote'
            matrix rowname `p' = `:rowfullname `C', quote'
            return scalar N = r(N)
            return matrix C = `C'
            return matrix p = `p'
            end
            
            sort group
            
            by group : ///
            collect corr=(r(C)) pvalue=(r(p)) : ///
            mod_cpcorr fd ka ext dom pr la \ ///
            type1 type2 indicator1 decade1 decade2 decade3
            by group : ///
            collect cov=(r(C)) : ///
            mod_cpcorr fd ka ext dom pr la \ ///
            type1 type2 indicator1 decade1 decade2 decade3, covariance
            
            * apply some style choices on the results of interest
            collect style cell result[cov], sformat("(%s)")
            collect style cell result[corr cov] , nformat(%12.2f)
            collect style cell result[pvalue] , nformat(%5.3f)
            * define composite result named show
            collect composite define show = corr cov pvalue, trim
            * hide result names from the header
            collect style header result, level(hide)
            * add note detailing the cell contents
            collect notes "Correlations, (covariances), and p-values."
            
            collect layout (group#rowname) (colname#result[show])
            
            collect export table.tex, replace
            Here is the table.
            Code:
            . collect layout (group#rowname) (colname#result[show])
            
            Collection: default
            Rows: group#rowname
            Columns: colname#result[show]
            Table 1: 28 x 6
            
            ---------------------------------------------------------------------------------------------------------------------
            | type1 type2 indicator1 decade1 decade2 decade3
            ------+--------------------------------------------------------------------------------------------------------------
            A |
            fd | . (0.00) 1.000 . (0.00) 1.000 0.06 (0.08) 0.798 -0.83 (-0.06) 0.000 0.29 (0.02) 0.175 0.47 (0.04) 0.022
            ka | . (0.00) 1.000 . (0.00) 1.000 -0.30 (-1.69) 0.150 0.19 (0.05) 0.386 -0.15 (-0.05) 0.490 -0.02 (-0.00) 0.943
            ext | . (0.00) 1.000 . (0.00) 1.000 -0.76 (-0.72) 0.000 -0.37 (-0.02) 0.073 -0.04 (-0.00) 0.846 0.39 (0.02) 0.063
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.12 (-0.08) 0.578 -0.76 (-0.02) 0.000 0.52 (0.02) 0.009 0.16 (0.01) 0.456
            pr | . (0.00) 1.000 . (0.00) 1.000 -0.35 (-1.01) 0.096 -0.91 (-0.13) 0.000 0.10 (0.02) 0.648 0.73 (0.12) 0.000
            la | . (0.00) 1.000 . (0.00) 1.000 0.65 (0.79) 0.001 0.26 (0.02) 0.214 -0.15 (-0.01) 0.485 -0.09 (-0.01) 0.692
            B |
            fd | . (0.00) 1.000 . (0.00) 1.000 -0.37 (-0.06) 0.198 -0.82 (-0.06) 0.000 0.52 (0.04) 0.058 0.32 (0.02) 0.261
            ka | . (0.00) 1.000 . (0.00) 1.000 -0.46 (-0.24) 0.096 -0.43 (-0.10) 0.127 0.24 (0.05) 0.413 0.20 (0.04) 0.490
            ext | . (0.00) 1.000 . (0.00) 1.000 -0.44 (-0.02) 0.114 -0.37 (-0.01) 0.190 0.21 (0.00) 0.478 0.18 (0.00) 0.549
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.35 (-0.04) 0.222 -0.67 (-0.04) 0.008 0.20 (0.01) 0.497 0.50 (0.03) 0.066
            pr | . (0.00) 1.000 . (0.00) 1.000 -0.15 (-0.07) 0.621 -0.98 (-0.20) 0.000 0.38 (0.08) 0.185 0.64 (0.12) 0.014
            la | . (0.00) 1.000 . (0.00) 1.000 -0.30 (-0.01) 0.296 -0.70 (-0.01) 0.005 -0.04 (-0.00) 0.881 0.79 (0.01) 0.001
            C |
            fd | . (0.00) 1.000 . (0.00) 1.000 -0.23 (-0.23) 0.525 -0.99 (-0.03) 0.000 0.23 (0.01) 0.524 0.37 (0.02) 0.291
            ka | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.40 (-0.27) 0.254 -0.85 (-0.02) 0.002 -0.16 (-0.01) 0.659 0.69 (0.03) 0.028
            pr | . (0.00) 1.000 . (0.00) 1.000 -0.27 (-0.64) 0.451 -0.97 (-0.08) 0.000 0.12 (0.02) 0.745 0.47 (0.06) 0.166
            la | . (0.00) 1.000 . (0.00) 1.000 -0.42 (-0.03) 0.228 -0.29 (-0.00) 0.414 -0.13 (-0.00) 0.721 0.31 (0.00) 0.382
            D |
            fd | . (0.00) 1.000 . (0.00) 1.000 -0.62 (-0.31) 0.001 -0.96 (-0.05) 0.000 0.44 (0.02) 0.030 0.42 (0.02) 0.040
            ka | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000 -0.37 (-0.07) 0.077 -0.73 (-0.01) 0.000 0.60 (0.01) 0.002 0.04 (0.00) 0.858
            pr | . (0.00) 1.000 . (0.00) 1.000 -0.16 (-0.11) 0.467 -0.65 (-0.04) 0.001 0.16 (0.01) 0.467 0.43 (0.03) 0.035
            la | . (0.00) 1.000 . (0.00) 1.000 -0.34 (-0.11) 0.107 -0.09 (-0.00) 0.687 0.03 (0.00) 0.899 0.05 (0.00) 0.813
            ---------------------------------------------------------------------------------------------------------------------
            Correlations, (covariances), and p-values.
            This table is rather wide, but you can use collect layout to select
            columns and export them separately. For example
            Code:
            . collect layout (group#rowname) (colname[type1 type2]#result[show])
            
            Collection: default
            Rows: group#rowname
            Columns: colname[type1 type2]#result[show]
            Table 1: 28 x 2
            
            -------------------------------------
            | type1 type2
            ------+------------------------------
            A |
            fd | . (0.00) 1.000 . (0.00) 1.000
            ka | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr | . (0.00) 1.000 . (0.00) 1.000
            la | . (0.00) 1.000 . (0.00) 1.000
            B |
            fd | . (0.00) 1.000 . (0.00) 1.000
            ka | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr | . (0.00) 1.000 . (0.00) 1.000
            la | . (0.00) 1.000 . (0.00) 1.000
            C |
            fd | . (0.00) 1.000 . (0.00) 1.000
            ka | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr | . (0.00) 1.000 . (0.00) 1.000
            la | . (0.00) 1.000 . (0.00) 1.000
            D |
            fd | . (0.00) 1.000 . (0.00) 1.000
            ka | . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000
            dom | . (0.00) 1.000 . (0.00) 1.000
            pr | . (0.00) 1.000 . (0.00) 1.000
            la | . (0.00) 1.000 . (0.00) 1.000
            -------------------------------------
            Correlations, (covariances), and p-values.
            
            . collect export table_type.tex, replace
            (collection default exported to file table_type.tex)
            
            .
            . collect layout (group#rowname) (colname[indicator1]#result[show])
            
            Collection: default
            Rows: group#rowname
            Columns: colname[indicator1]#result[show]
            Table 1: 28 x 1
            
            ---------------------------
            | indicator1
            ------+--------------------
            A |
            fd | 0.06 (0.08) 0.798
            ka | -0.30 (-1.69) 0.150
            ext | -0.76 (-0.72) 0.000
            dom | -0.12 (-0.08) 0.578
            pr | -0.35 (-1.01) 0.096
            la | 0.65 (0.79) 0.001
            B |
            fd | -0.37 (-0.06) 0.198
            ka | -0.46 (-0.24) 0.096
            ext | -0.44 (-0.02) 0.114
            dom | -0.35 (-0.04) 0.222
            pr | -0.15 (-0.07) 0.621
            la | -0.30 (-0.01) 0.296
            C |
            fd | -0.23 (-0.23) 0.525
            ka | . (0.00) 1.000
            ext | . (0.00) 1.000
            dom | -0.40 (-0.27) 0.254
            pr | -0.27 (-0.64) 0.451
            la | -0.42 (-0.03) 0.228
            D |
            fd | -0.62 (-0.31) 0.001
            ka | . (0.00) 1.000
            ext | . (0.00) 1.000
            dom | -0.37 (-0.07) 0.077
            pr | -0.16 (-0.11) 0.467
            la | -0.34 (-0.11) 0.107
            ---------------------------
            Correlations, (covariances), and p-values.
            
            . collect export table_ind.tex, replace
            (collection default exported to file table_ind.tex)
            
            .
            . collect layout (group#rowname) (colname[decade1 decade2 decade3]#result[show]
            > )
            
            Collection: default
            Rows: group#rowname
            Columns: colname[decade1 decade2 decade3]#result[show]
            Table 1: 28 x 3
            
            -------------------------------------------------------------------
            | decade1 decade2 decade3
            ------+------------------------------------------------------------
            A |
            fd | -0.83 (-0.06) 0.000 0.29 (0.02) 0.175 0.47 (0.04) 0.022
            ka | 0.19 (0.05) 0.386 -0.15 (-0.05) 0.490 -0.02 (-0.00) 0.943
            ext | -0.37 (-0.02) 0.073 -0.04 (-0.00) 0.846 0.39 (0.02) 0.063
            dom | -0.76 (-0.02) 0.000 0.52 (0.02) 0.009 0.16 (0.01) 0.456
            pr | -0.91 (-0.13) 0.000 0.10 (0.02) 0.648 0.73 (0.12) 0.000
            la | 0.26 (0.02) 0.214 -0.15 (-0.01) 0.485 -0.09 (-0.01) 0.692
            B |
            fd | -0.82 (-0.06) 0.000 0.52 (0.04) 0.058 0.32 (0.02) 0.261
            ka | -0.43 (-0.10) 0.127 0.24 (0.05) 0.413 0.20 (0.04) 0.490
            ext | -0.37 (-0.01) 0.190 0.21 (0.00) 0.478 0.18 (0.00) 0.549
            dom | -0.67 (-0.04) 0.008 0.20 (0.01) 0.497 0.50 (0.03) 0.066
            pr | -0.98 (-0.20) 0.000 0.38 (0.08) 0.185 0.64 (0.12) 0.014
            la | -0.70 (-0.01) 0.005 -0.04 (-0.00) 0.881 0.79 (0.01) 0.001
            C |
            fd | -0.99 (-0.03) 0.000 0.23 (0.01) 0.524 0.37 (0.02) 0.291
            ka | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            dom | -0.85 (-0.02) 0.002 -0.16 (-0.01) 0.659 0.69 (0.03) 0.028
            pr | -0.97 (-0.08) 0.000 0.12 (0.02) 0.745 0.47 (0.06) 0.166
            la | -0.29 (-0.00) 0.414 -0.13 (-0.00) 0.721 0.31 (0.00) 0.382
            D |
            fd | -0.96 (-0.05) 0.000 0.44 (0.02) 0.030 0.42 (0.02) 0.040
            ka | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            ext | . (0.00) 1.000 . (0.00) 1.000 . (0.00) 1.000
            dom | -0.73 (-0.01) 0.000 0.60 (0.01) 0.002 0.04 (0.00) 0.858
            pr | -0.65 (-0.04) 0.001 0.16 (0.01) 0.467 0.43 (0.03) 0.035
            la | -0.09 (-0.00) 0.687 0.03 (0.00) 0.899 0.05 (0.00) 0.813
            -------------------------------------------------------------------
            Correlations, (covariances), and p-values.
            
            . collect export table_decade.tex, replace
            (collection default exported to file table_decade.tex)

            Thank you warmly for your work , Jeff! I really appreciate it!
            That was beyond my abilities!
            I have some questions about the code, rather silly ones.

            Assume that I would like to rename each of the groups A, B, C, D, E, etc. to another group name, say "Whatever A" and "Whatever B," etc. How could I do that?


            Secondly, on my dataset, the variables are already labeled. I would like to have them placed vertically on the table rather than horizontally.

            Finally, for a new similar table, I would like to have the group description (A, B, etc.) the same way they are for both the rows and the column ("Whatever in column A," etc.).

            Thank you so much! .

            Comment


            • #7


              To provide clarity, let me say that in the rows for the data example here, group A is composed of fd and ka.

              Group B is composed of ext and dom.

              Group C is composed of pr and la.

              In the real data, I have five groups and more variables in each group.


              Comment


              • #8
                I assumed string variable group in your example dataset defined
                the groups A, B, C, .... Let me start over again.

                I made a new program named both_cpcorr that calls cpcorr
                to get correlations, covariances, and p-values in one call (and still
                copies the matrix stripes from the correlations matrix to the p-values
                matrix).

                In this example I collect results for the given variable groups
                A (fd ka), B (ext dom), and C (pr la), separately and provide a custom tag for
                each group using a dimension I name var_group with levels A,
                B, and C. Next I show how to attach your own labels to these
                levels, apply the same cell and header styles at the previous example, and
                finally change the layout to put all variables in the row specification,
                nesting the reported column variables within the row variables which are
                nested within the variables group dimension var_group.
                Code:
                program both_cpcorr, rclass byable(recall)
                    // use -cpcorr- to post both CORRelations and COVariances;
                    // ensure posted p-values matrix has the same matrix stripes as
                    // the corr/cov matrices
                    version 17
                    syntax anything [if] [in]
                    marksample touse
                    cpcorr `anything' if `touse'
                    tempname C p
                    matrix `C' = r(C)
                    matrix `p' = r(p)
                    matrix colname `p' = `:colfullname `C', quote'
                    matrix rowname `p' = `:rowfullname `C', quote'
                    return scalar N = r(N)
                    return matrix corr = `C'
                    return matrix p = `p'
                    cpcorr `anything' if `touse', covariance
                    matrix `C' = r(C)
                    return matrix cov = `C'
                end
                
                local common type1 type2 indicator1 decade1 decade2 decade3
                
                * Group A variables
                collect, tags(var_group[A]) : both_cpcorr fd ka \ `common'
                * Group B variables
                collect, tags(var_group[B]) : both_cpcorr ext dom \ `common'
                * Group C variables
                collect, tags(var_group[C]) : both_cpcorr pr la \ `common'
                
                * label the var_group levels
                collect label levels var_group ///
                    A "Whatever A" ///
                    B "Whatever B" ///
                    C "Whatever C"
                
                * apply some style choices on the results of interest
                collect style cell result[cov], sformat("(%s)")
                collect style cell result[corr cov] , nformat(%12.2f)
                collect style cell result[p] , nformat(%5.3f)
                * define composite result named show
                collect composite define show = corr cov p, trim
                * hide result names from the header
                collect style header result, level(hide)
                * add note detailing the cell contents
                collect notes "Correlations, (covariances), and p-values."
                
                collect layout (var_group#rowname#colname) (result[show])
                Here is the resulting table.
                Code:
                . collect layout (var_group#rowname#colname) (result[show])
                
                Collection: default
                      Rows: var_group#rowname#colname
                   Columns: result[show]
                   Table 1: 45 x 1
                
                ------------------------------------
                Whatever A     |
                  fd           |
                    type1      |      . (0.00) 1.000
                    type2      |      . (0.00) 1.000
                    indicator1 | -0.04 (-0.04) 0.727
                    decade1    | -0.80 (-0.05) 0.000
                    decade2    |   0.34 (0.02) 0.003
                    decade3    |   0.38 (0.03) 0.001
                  ka           |
                    type1      |      . (0.00) 1.000
                    type2      |      . (0.00) 1.000
                    indicator1 | -0.29 (-0.97) 0.012
                    decade1    | -0.02 (-0.00) 0.875
                    decade2    | -0.02 (-0.00) 0.893
                    decade3    |   0.03 (0.01) 0.775
                Whatever B     |
                  ext          |
                    type1      |      . (0.00) 1.000
                    type2      |      . (0.00) 1.000
                    indicator1 | -0.58 (-0.34) 0.000
                    decade1    | -0.19 (-0.01) 0.105
                    decade2    | -0.00 (-0.00) 1.000
                    decade3    |   0.18 (0.01) 0.137
                  dom          |
                    type1      |      . (0.00) 1.000
                    type2      |      . (0.00) 1.000
                    indicator1 | -0.01 (-0.01) 0.910
                    decade1    | -0.62 (-0.02) 0.000
                    decade2    |   0.29 (0.01) 0.013
                    decade3    |   0.26 (0.01) 0.026
                Whatever C     |
                  pr           |
                    type1      |      . (0.00) 1.000
                    type2      |      . (0.00) 1.000
                    indicator1 | -0.25 (-0.54) 0.034
                    decade1    | -0.79 (-0.11) 0.000
                    decade2    |   0.18 (0.03) 0.140
                    decade3    |   0.54 (0.08) 0.000
                  la           |
                    type1      |      . (0.00) 1.000
                    type2      |      . (0.00) 1.000
                    indicator1 |   0.29 (0.30) 0.013
                    decade1    |   0.06 (0.00) 0.639
                    decade2    | -0.05 (-0.00) 0.697
                    decade3    | -0.00 (-0.00) 0.981
                ------------------------------------
                Correlations, (covariances), and p-values.

                Comment


                • #9
                  Here is a modified version of the previous where you do want the
                  correlations within the levels of the group variable.
                  Code:
                  collect create within_group, replace
                  sort group
                  local common type1 type2 indicator1 decade1 decade2 decade3
                  
                  * Group A variables
                  by group: collect, tags(var_group[A]) : both_cpcorr fd ka \ `common'
                  * Group B variables
                  by group: collect, tags(var_group[B]) : both_cpcorr ext dom \ `common'
                  * Group C variables
                  by group: collect, tags(var_group[C]) : both_cpcorr pr la \ `common'
                  
                  * label the var_group levels
                  collect label levels var_group ///
                      A "Whatever A" ///
                      B "Whatever B" ///
                      C "Whatever C"
                  
                  collect levels group
                  local levels = s(levels)
                  foreach lev of local levels {
                      collect label levels group `lev' "Sample `lev'"
                  }
                  
                  * apply some style choices on the results of interest
                  collect style cell result[cov], sformat("(%s)")
                  collect style cell result[corr cov] , nformat(%12.2f)
                  collect style cell result[p] , nformat(%5.3f)
                  * define composite result named show
                  collect composite define show = corr cov p, trim
                  * hide result names from the header
                  collect style header result, level(hide)
                  * add note detailing the cell contents
                  collect notes "Correlations, (covariances), and p-values."
                  
                  collect layout (group#var_group#rowname#colname) (result[show])
                  Here is the resulting table.
                  Code:
                  . collect layout (group#var_group#rowname#colname) (result[show])
                  
                  Collection: within_group
                        Rows: group#var_group#rowname#colname
                     Columns: result[show]
                     Table 1: 184 x 1
                  
                  --------------------------------------
                  Sample A         |
                    Whatever A     |
                      fd           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 |   0.06 (0.08) 0.798
                        decade1    | -0.83 (-0.06) 0.000
                        decade2    |   0.29 (0.02) 0.175
                        decade3    |   0.47 (0.04) 0.022
                      ka           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.30 (-1.69) 0.150
                        decade1    |   0.19 (0.05) 0.386
                        decade2    | -0.15 (-0.05) 0.490
                        decade3    | -0.02 (-0.00) 0.943
                    Whatever B     |
                      ext          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.76 (-0.72) 0.000
                        decade1    | -0.37 (-0.02) 0.073
                        decade2    | -0.04 (-0.00) 0.846
                        decade3    |   0.39 (0.02) 0.063
                      dom          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.12 (-0.08) 0.578
                        decade1    | -0.76 (-0.02) 0.000
                        decade2    |   0.52 (0.02) 0.009
                        decade3    |   0.16 (0.01) 0.456
                    Whatever C     |
                      pr           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.35 (-1.01) 0.096
                        decade1    | -0.91 (-0.13) 0.000
                        decade2    |   0.10 (0.02) 0.648
                        decade3    |   0.73 (0.12) 0.000
                      la           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 |   0.65 (0.79) 0.001
                        decade1    |   0.26 (0.02) 0.214
                        decade2    | -0.15 (-0.01) 0.485
                        decade3    | -0.09 (-0.01) 0.692
                  Sample B         |
                    Whatever A     |
                      fd           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.37 (-0.06) 0.198
                        decade1    | -0.82 (-0.06) 0.000
                        decade2    |   0.52 (0.04) 0.058
                        decade3    |   0.32 (0.02) 0.261
                      ka           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.46 (-0.24) 0.096
                        decade1    | -0.43 (-0.10) 0.127
                        decade2    |   0.24 (0.05) 0.413
                        decade3    |   0.20 (0.04) 0.490
                    Whatever B     |
                      ext          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.44 (-0.02) 0.114
                        decade1    | -0.37 (-0.01) 0.190
                        decade2    |   0.21 (0.00) 0.478
                        decade3    |   0.18 (0.00) 0.549
                      dom          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.35 (-0.04) 0.222
                        decade1    | -0.67 (-0.04) 0.008
                        decade2    |   0.20 (0.01) 0.497
                        decade3    |   0.50 (0.03) 0.066
                    Whatever C     |
                      pr           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.15 (-0.07) 0.621
                        decade1    | -0.98 (-0.20) 0.000
                        decade2    |   0.38 (0.08) 0.185
                        decade3    |   0.64 (0.12) 0.014
                      la           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.30 (-0.01) 0.296
                        decade1    | -0.70 (-0.01) 0.005
                        decade2    | -0.04 (-0.00) 0.881
                        decade3    |   0.79 (0.01) 0.001
                  Sample C         |
                    Whatever A     |
                      fd           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.23 (-0.23) 0.525
                        decade1    | -0.99 (-0.03) 0.000
                        decade2    |   0.23 (0.01) 0.524
                        decade3    |   0.37 (0.02) 0.291
                      ka           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 |      . (0.00) 1.000
                        decade1    |      . (0.00) 1.000
                        decade2    |      . (0.00) 1.000
                        decade3    |      . (0.00) 1.000
                    Whatever B     |
                      ext          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 |      . (0.00) 1.000
                        decade1    |      . (0.00) 1.000
                        decade2    |      . (0.00) 1.000
                        decade3    |      . (0.00) 1.000
                      dom          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.40 (-0.27) 0.254
                        decade1    | -0.85 (-0.02) 0.002
                        decade2    | -0.16 (-0.01) 0.659
                        decade3    |   0.69 (0.03) 0.028
                    Whatever C     |
                      pr           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.27 (-0.64) 0.451
                        decade1    | -0.97 (-0.08) 0.000
                        decade2    |   0.12 (0.02) 0.745
                        decade3    |   0.47 (0.06) 0.166
                      la           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.42 (-0.03) 0.228
                        decade1    | -0.29 (-0.00) 0.414
                        decade2    | -0.13 (-0.00) 0.721
                        decade3    |   0.31 (0.00) 0.382
                  Sample D         |
                    Whatever A     |
                      fd           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.62 (-0.31) 0.001
                        decade1    | -0.96 (-0.05) 0.000
                        decade2    |   0.44 (0.02) 0.030
                        decade3    |   0.42 (0.02) 0.040
                      ka           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 |      . (0.00) 1.000
                        decade1    |      . (0.00) 1.000
                        decade2    |      . (0.00) 1.000
                        decade3    |      . (0.00) 1.000
                    Whatever B     |
                      ext          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 |      . (0.00) 1.000
                        decade1    |      . (0.00) 1.000
                        decade2    |      . (0.00) 1.000
                        decade3    |      . (0.00) 1.000
                      dom          |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.37 (-0.07) 0.077
                        decade1    | -0.73 (-0.01) 0.000
                        decade2    |   0.60 (0.01) 0.002
                        decade3    |   0.04 (0.00) 0.858
                    Whatever C     |
                      pr           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.16 (-0.11) 0.467
                        decade1    | -0.65 (-0.04) 0.001
                        decade2    |   0.16 (0.01) 0.467
                        decade3    |   0.43 (0.03) 0.035
                      la           |
                        type1      |      . (0.00) 1.000
                        type2      |      . (0.00) 1.000
                        indicator1 | -0.34 (-0.11) 0.107
                        decade1    | -0.09 (-0.00) 0.687
                        decade2    |   0.03 (0.00) 0.899
                        decade3    |   0.05 (0.00) 0.813
                  --------------------------------------
                  Correlations, (covariances), and p-values.

                  Comment


                  • #10
                    If neither of these examples matches what you want, please provide example tables.

                    Comment


                    • #11
                      Originally posted by Jeff Pitblado (StataCorp) View Post
                      If neither of these examples matches what you want, please provide example tables.
                      Table in #5 is the desired wide form. However, with two modifications, I would like to be able to label A, B, C, D, E, etc. as in #8.

                      Code:
                      * Label the var_group levels
                      
                      collect label levels var_group ///
                      
                      A "Whatever A"
                      
                      B "Whatever B" ///
                      
                       C "Whatever C"


                      In addition to being able to add the wanted variables to each group in A, B, C etc.



                      Say for “Whatever A" fd and ka, for "Whatever B " ext and dom, and for “Whatever C” pr and la. and so on . The rest looks fine!

                      Comment


                      • #12
                        Jeff Pitblado (StataCorp) I
                        I got


                        Code:
                         collect layout (group#rowname) (colname#result[show]),name(within_group)
                        
                        Collection: within_group
                              Rows: group#rowname
                           Columns: colname#result[show]
                        
                        Your layout specification does not uniquely match any items. One or more of the following dimensions
                        might help uniquely match items: cmdset, rowname, var_group.
                        .
                        The full code I run is :


                        Code:
                        program both_cpcorr, rclass byable(recall)
                            // use -cpcorr- to post both CORRelations and COVariances;
                            // ensure posted p-values matrix has the same matrix stripes as
                            // the corr/cov matrices
                            version 17
                            syntax anything [if] [in]
                            marksample touse
                            cpcorr `anything' if `touse'
                            tempname C p
                            matrix `C' = r(C)
                            matrix `p' = r(p)
                            matrix colname `p' = `:colfullname `C', quote'
                            matrix rowname `p' = `:rowfullname `C', quote'
                            return scalar N = r(N)
                            return matrix corr = `C'
                            return matrix p = `p'
                            cpcorr `anything' if `touse', covariance
                            matrix `C' = r(C)
                            return matrix cov = `C'
                        end
                        
                        
                        collect create within_group, replace
                        sort group
                        local common type1 type2 indicator1 decade1 decade2 decade3
                        
                        * Group A variables
                        by group: collect, tags(var_group[A]) : both_cpcorr fd ka \ `common'
                        * Group B variables
                        by group: collect, tags(var_group[B]) : both_cpcorr ext dom \ `common'
                        * Group C variables
                        by group: collect, tags(var_group[C]) : both_cpcorr pr la \ `common'
                        
                        * label the var_group levels
                        collect label levels var_group ///
                            A "Whatever A" ///
                            B "Whatever B" ///
                            C "Whatever C"
                            
                            
                            
                        * apply some style choices on the results of interest
                        collect style cell result[cov], sformat("(%s)")
                        collect style cell result[corr cov] , nformat(%12.2f)
                        collect style cell result[p] , nformat(%5.3f)
                        * define composite result named show
                        collect composite define show = corr cov p, trim
                        * hide result names from the header
                        collect style header result, level(hide)
                        * add note detailing the cell contents
                        collect notes "Correlations, (covariances), and p-values."
                        
                        collect layout (group#var_group#rowname#colname) (result[show])

                        Any suggestion ?

                        Comment


                        • #13
                          It appears you are missing var_group.
                          Code:
                          collect layout (group#var_group#rowname) (colname#result[show]), name(within_group)

                          Comment


                          • #14
                            Originally posted by Jeff Pitblado (StataCorp) View Post
                            It appears you are missing var_group.
                            Code:
                            collect layout (group#var_group#rowname) (colname#result[show]), name(within_group)
                            I still get
                            Code:
                            Your layout specification does not uniquely match any items. One or more of the following dimensions
                            might help uniquely match items: cmdset, rowname, var_group.

                            My desired layout is
                            Var1. Var2 Var3 Var4 etc a columns


                            Group A name
                            GROUP_A_Var 1
                            GROUP_A_Var 2

                            Group B name
                            GROUP_B_Var 1
                            GROUP_B_Var 2
                            GROUP_B_Var 3

                            Group C name
                            GROUP_C_Var 1
                            GROUP_C_Var 2
                            GROUP_C_Var 3
                            GROUP_C_Var 4

                            etc Enable GingerCannot connect to Ginger Check your internet connection
                            or reload the browserDisable in this text fieldRephraseRephrase current sentence10Edit in Ginger×
                            Last edited by Giorgio Di Stefano; 04 Oct 2023, 15:37.

                            Comment


                            • #15
                              Maybe start with a fresh Stata session, or type clear all before rerunning your code.

                              This is the code I ran.
                              Code:
                              clear all
                              
                              * Example generated by -dataex-. For more info, type help dataex
                              clear
                              input float(id year) str97 country str1 group float(fd ka ext dom pr la type1 type2 indicator1 decade) byte(decade1 decade2 decade3)
                              1 1992 "Country1" "A"   .524175   2.321955  .75 .7777778 .071428575        1 0 1       12.2 1990 1 0 0
                              1 1993 "Country1" "A"  .5846328   2.321955  .75 .7777778 .071428575        1 0 1  17.553352 1990 1 0 0
                              1 1994 "Country1" "A"  .6478685   2.321955  .75 .9444444 .071428575 .7912164 0 1      19.11 1990 1 0 0
                              1 2000 "Country1" "A"  .8414965   1.048859  .75        1  .53571427 .7782844 0 1     28.634 2000 0 1 0
                              1 2001 "Country1" "A"   .873997   1.048859  .75        1   .6190476 .7782844 0 1   26.41307 2000 0 1 0
                              1 2002 "Country1" "A"  .8985876   1.048859  .75        1   .6190476 .7661784 0 1   5.536341 2000 0 1 0
                              1 2003 "Country1" "A"  .9015374   1.048859  .75        1   .6190476 .7661784 0 1   5.536341 2000 0 1 0
                              1 2004 "Country1" "A"  .9041975   1.048859  .75        1   .6190476 .7661784 0 1   7.350179 2000 0 1 0
                              1 2010 "Country1" "A"   .941813   1.048859 .875        1   .8452381 .7704133 0 1   6.241802 2010 0 0 1
                              1 2011 "Country1" "A"   .934702   1.048859 .875        1   .8452381 .7704133 0 1      5.068 2010 0 0 1
                              1 2012 "Country1" "A"  .9079577   1.303478 .875        1   .8452381 .7704133 0 1      5.068 2010 0 0 1
                              1 2013 "Country1" "A"  .8901507  1.5580974 .875        1   .8452381 .7694158 0 1   7.929047 2010 0 0 1
                              2 1992 "Country2" "A"   .399736   .6099069 .875 .7777778          0 .5697657 0 1        1.6 1990 1 0 0
                              2 1993 "Country2" "A"   .461959  2.0673358 .875 .9444444          0 .5697657 0 1   6.996447 1990 1 0 0
                              2 1994 "Country2" "A"  .4953545   2.321955 .875 .9444444          0 .5697657 0 1    8.69136 1990 1 0 0
                              2 2000 "Country2" "A"  .7018992   2.321955    1        1   .4404762 .5697657 0 1   2.364474 2000 0 1 0
                              2 2001 "Country2" "A"  .7035186   2.321955    1        1   .5952381 .5697657 0 1   2.364474 2000 0 1 0
                              2 2002 "Country2" "A"  .6993845   2.321955    1        1   .5952381 .5457992 0 1  -.7668521 2000 0 1 0
                              2 2003 "Country2" "A"  .7077955   2.321955    1        1   .5952381 .5457992 0 1 -2.9592714 2000 0 1 0
                              2 2004 "Country2" "A"   .728983   2.321955    1        1   .5952381 .5457992 0 1  -2.950783 2000 0 1 0
                              2 2010 "Country2" "A"  .7894757   2.321955    1 .9444444   .9166667 .5718316 0 1      3.891 2010 0 0 1
                              2 2011 "Country2" "A"  .8007692   2.321955    1 .9444444   .9166667 .5718316 0 1      3.891 2010 0 0 1
                              2 2012 "Country2" "A"  .7593077   2.321955    1 .9444444   .9166667 .5718316 0 1 -2.5410414 2010 0 0 1
                              2 2013 "Country2" "A"  .7435991   2.321955    1 .9444444   .9166667 .5718316 0 1  -6.333579 2010 0 0 1
                              3 1992 "Country3" "B"  .5840359   2.321955    1 .7777778  .08333334 .6281998 0 1   .3835026 1990 1 0 0
                              3 1993 "Country3" "B"  .6147648   2.321955    1 .8888889  .08333334 .6281998 0 1   .3835026 1990 1 0 0
                              3 1994 "Country3" "B"   .632942   2.321955    1 .8888889  .08333334 .6260909 0 1   .8169867 1990 1 0 0
                              3 2000 "Country3" "B"  .7692201   2.321955    1 .8888889        .75 .6297867 0 1  1.9049623 2000 0 1 0
                              3 2001 "Country3" "B"  .7502941   2.321955    1 .8888889        .75 .6297867 0 1  1.9049622 2000 0 1 0
                              3 2002 "Country3" "B"  .7244223   2.321955    1 .8888889   .8333333 .6297867 0 1   1.283737 2000 0 1 0
                              3 2003 "Country3" "B"  .7401305   2.321955    1 .8888889   .8333333 .6297867 0 1 -1.3254085 2000 0 1 0
                              3 2004 "Country3" "B"  .7518873   2.321955    1 .8888889   .8333333 .6547069 0 1 -1.3254085 2000 0 1 0
                              3 2010 "Country3" "B"  .7356597   2.321955    1 .9444444          1 .6547069 0 1  1.1606808 2010 0 0 1
                              3 2011 "Country3" "B"  .7201834   2.321955    1 .9444444          1 .6547069 0 1  1.1606808 2010 0 0 1
                              3 2012 "Country3" "B"  .7283527   2.321955    1 .9444444          1 .6547069 0 1  1.1606808 2010 0 0 1
                              3 2013 "Country3" "B"  .7077386   2.321955    1 .9444444          1 .6547069 0 1  1.1165322 2010 0 0 1
                              4 1992 "Country4" "B"  .3452282   .6099069 .875 .5555556 .071428575 .6121462 0 1   2.615249 1990 1 0 0
                              4 1993 "Country4" "B"  .3790323  2.0673358    1 .7222222 .071428575 .6121462 0 1  1.4437795 1990 1 0 0
                              4 1994 "Country5" "C"  .4175321   2.321955    1 .7222222 .071428575 .6121462 0 1  4.0116615 1990 1 0 0
                              4 2000 "Country5" "C"   .759881   2.321955    1 .8888889   .7619048 .6121462 0 1    5.97681 2000 0 1 0
                              4 2001 "Country5" "C"  .7410694   2.321955    1 .8888889   .7619048 .6121462 0 1   7.402936 2000 0 1 0
                              4 2002 "Country5" "C"  .7281089   2.321955    1 .8888889   .8333333 .6121462 0 1      8.534 2000 0 1 0
                              4 2003 "Country5" "C"   .735869   2.321955    1 .8888889   .8333333 .6263371 0 1      8.534 2000 0 1 0
                              4 2004 "Country5" "C"  .7334849   2.321955    1 .8888889   .8333333 .6263371 0 1      8.534 2000 0 1 0
                              4 2010 "Country5" "C"  .7798166   2.321955    1 .9444444   .9166667 .6263371 0 1     -16.25 2010 0 0 1
                              4 2011 "Country5" "C"  .7637033   2.321955    1 .9444444   .9166667 .6263371 0 1 -14.330358 2010 0 0 1
                              4 2012 "Country5" "C"  .7608096   2.321955    1 .9444444   .9166667 .6263371 0 1          0 2010 0 0 1
                              4 2013 "Country5" "C"  .7455279   2.321955    1        1   .9166667  .607849 0 1   .6806767 2010 0 0 1
                              5 1992 "Country6" "C"  .1757301          .    .        .          .        . 0 1          . 1990 1 0 0
                              5 1993 "Country6" "C"  .1761862          .    .        .          .        . 1 0          . 1990 1 0 0
                              5 1994 "Country6" "C"  .1562944          .    .        .          .        . 1 0          . 1990 1 0 0
                              5 2000 "Country6" "C"  .2008834 -1.2261546    .        .          .        . 1 0          . 2000 0 1 0
                              5 2001 "Country6" "C" .17261477 -1.2261546    .        .          .        . 1 0          . 2000 0 1 0
                              5 2002 "Country6" "C" .21249928 -1.2261546    .        .          .        . 1 0          . 2000 0 1 0
                              5 2003 "Country6" "C"  .2206576 -.02334493    .        .          .        . 1 0          . 2000 0 1 0
                              5 2004 "Country6" "C" .23541167 -.02334493    .        .          .        . 1 0          . 2000 0 1 0
                              5 2010 "Country6" "C" .27683625   1.249751    .        .          .        . 0 1          . 2010 0 0 1
                              5 2011 "Country6" "C" .27487376   1.249751    .        .          .        . 0 1          . 2010 0 0 1
                              5 2012 "Country6" "C" .27767062   1.249751    .        .          .        . 0 1          . 2010 0 0 1
                              5 2013 "Country6" "C"  .2884341   1.249751    .        .          .        . 0 1          . 2010 0 0 1
                              6 1992 "Country7" "C" .25534174  -.1539507 .625       .5          0 .8271327 1 0          . 1990 1 0 0
                              6 1993 "Country7" "C" .28078705  -.1539507 .625       .5  .16666667 .8271327 1 0          . 1990 1 0 0
                              6 1994 "Country7" "C"  .3567602  -.1539507 .625 .5555556  .16666667 .8271327 1 0          . 1990 1 0 0
                              6 2000 "Country7" "C"  .4190661 -1.2261546 .625 .6111111  .23809524 .8271327 0 1          . 2000 0 1 0
                              6 2001 "Country7" "C"  .3738855 -1.2261546 .625 .6666667   .3928571 .8271327 0 1          . 2000 0 1 0
                              6 2002 "Country7" "C"   .404098 -1.2261546 .625 .6666667   .3928571 .8271327 0 1          . 2000 0 1 0
                              6 2003 "Country7" "C"  .4157713 -1.2261546 .875 .6666667   .3928571 .7653575 0 1          . 2000 0 1 0
                              6 2004 "Country7" "C"  .4085605 -1.2261546 .875 .6666667  .53571427 .7653575 0 1          . 2000 0 1 0
                              6 2010 "Country7" "C"  .4888775 -.02334493 .875 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
                              6 2011 "Country7" "C"  .4789392 -.02334493 .875 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
                              6 2012 "Country7" "C"  .4922497 -.02334493 .875 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
                              6 2013 "Country7" "C"   .508882 -.02334493  .75 .6666667   .7619048 .7653575 0 1          . 2010 0 0 1
                              7 1992 "Country8" "D" .56565696   2.321955    1 .9444444   .7738096 .8009957 0 1  13.403038 1990 1 0 0
                              7 1993 "Country8" "D"  .6403931   2.321955    1 .9444444   .7738096 .8070084 0 1       12.1 1990 1 0 0
                              7 1994 "Country8" "D"  .6921942   2.321955    1 .9444444   .8452381 .8070084 0 1       12.1 1990 1 0 0
                              7 2000 "Country8" "D"  .8552948   2.321955    1        1   .9285714 .8314556 0 1      1.806 2000 0 1 0
                              7 2001 "Country8" "D"  .8805222   2.321955    1        1   .9285714 .8314556 0 1   1.958802 2000 0 1 0
                              7 2002 "Country8" "D"  .8265579   2.321955    1        1   .9285714 .8314556 0 1      2.076 2000 0 1 0
                              7 2003 "Country8" "D"  .8580942   2.321955    1        1          1 .8314556 0 1      2.076 2000 0 1 0
                              7 2004 "Country8" "D"  .8708485   2.321955    1        1          1 .8314556 0 1      2.076 2000 0 1 0
                              7 2010 "Country8" "D"  .8683383   2.321955    1        1          1 .8314556 0 1   3.564247 2010 0 0 1
                              7 2011 "Country8" "D"  .8672714   2.321955    1 .9444444          1 .8314556 0 1   5.454876 2010 0 0 1
                              7 2012 "Country8" "D"  .8684435   2.321955    1 .8888889          1 .8429364 0 1   5.454876 2010 0 0 1
                              7 2013 "Country8" "D"  .8866873   2.321955    1 .9444444          1 .8429364 0 1   5.454876 2010 0 0 1
                              8 1992 "Country8" "D"  .5748482   2.321955    1 .8888889  .52380955 .9702252 0 1       14.9 1990 1 0 0
                              8 1993 "Country8" "D"  .6702072   2.321955    1 .8888889  .52380955 .9702252 0 1   .0375137 1990 1 0 0
                              8 1994 "Country8" "D"  .6812267   2.321955    1 .8888889  .52380955 .9702252 0 1      -.781 1990 1 0 0
                              8 2000 "Country8" "D"  .8790284   2.321955    1        1        .75 .9702252 0 1      1.463 2000 0 1 0
                              8 2001 "Country8" "D"   .889005   2.321955    1        1        .75 .9702252 0 1   5.542341 2000 0 1 0
                              8 2002 "Country8" "D"  .8849987   2.321955    1        1        .75 .9702252 0 1      5.767 2000 0 1 0
                              8 2003 "Country8" "D"  .8916724   2.321955    1        1        .75 .9702252 0 1      5.767 2000 0 1 0
                              8 2004 "Country8" "D"  .8910962   2.321955    1        1        .75 .9702252 0 1      5.767 2000 0 1 0
                              8 2010 "Country8" "D"  .8951228   2.321955    1        1   .8333333 .9702252 0 1       -.36 2010 0 0 1
                              8 2011 "Country8" "D"  .8785759   2.321955    1        1   .8333333 .9702252 0 1       -.36 2010 0 0 1
                              8 2012 "Country8" "D"  .8894278   2.321955    1        1   .8333333 .9702252 0 1       -.36 2010 0 0 1
                              8 2013 "Country8" "D"  .8901961   2.321955    1        1   .8333333 .9702252 0 1   1.298654 2010 0 0 1
                              end
                              
                              program both_cpcorr, rclass byable(recall)
                                  // use -cpcorr- to post both CORRelations and COVariances;
                                  // ensure posted p-values matrix has the same matrix stripes as
                                  // the corr/cov matrices
                                  version 17
                                  syntax anything [if] [in]
                                  marksample touse
                                  cpcorr `anything' if `touse'
                                  tempname C p
                                  matrix `C' = r(C)
                                  matrix `p' = r(p)
                                  matrix colname `p' = `:colfullname `C', quote'
                                  matrix rowname `p' = `:rowfullname `C', quote'
                                  return scalar N = r(N)
                                  return matrix corr = `C'
                                  return matrix p = `p'
                                  cpcorr `anything' if `touse', covariance
                                  matrix `C' = r(C)
                                  return matrix cov = `C'
                              end
                              
                              collect create within_group, replace
                              sort group
                              local common type1 type2 indicator1 decade1 decade2 decade3
                              
                              * Group A variables
                              by group: collect, tags(var_group[A]) : both_cpcorr fd ka \ `common'
                              * Group B variables
                              by group: collect, tags(var_group[B]) : both_cpcorr ext dom \ `common'
                              * Group C variables
                              by group: collect, tags(var_group[C]) : both_cpcorr pr la \ `common'
                              
                              * label the var_group levels
                              collect label levels var_group ///
                                  A "Whatever A" ///
                                  B "Whatever B" ///
                                  C "Whatever C"
                              
                              * apply some style choices on the results of interest
                              collect style cell result[cov], sformat("(%s)")
                              collect style cell result[corr cov] , nformat(%12.2f)
                              collect style cell result[p] , nformat(%5.3f)
                              * define composite result named show
                              collect composite define show = corr cov p, trim
                              * hide result names from the header
                              collect style header result, level(hide)
                              * add note detailing the cell contents
                              collect notes "Correlations, (covariances), and p-values."
                              
                              collect layout (group#var_group#rowname) (colname#result[show])
                              This is the resulting table.
                              Code:
                              . collect layout (group#var_group#rowname) (colname#result[show])
                              
                              Collection: within_group
                                    Rows: group#var_group#rowname
                                 Columns: colname#result[show]
                                 Table 1: 40 x 6
                              
                              ----------------------------------------------------------------------------------------------------------------------------
                                           |          type1          type2          indicator1             decade1             decade2             decade3
                              -------------+--------------------------------------------------------------------------------------------------------------
                              A            |                                                                                                              
                                Whatever A |                                                                                                              
                                  fd       | . (0.00) 1.000 . (0.00) 1.000   0.06 (0.08) 0.798 -0.83 (-0.06) 0.000   0.29 (0.02) 0.175   0.47 (0.04) 0.022
                                  ka       | . (0.00) 1.000 . (0.00) 1.000 -0.30 (-1.69) 0.150   0.19 (0.05) 0.386 -0.15 (-0.05) 0.490 -0.02 (-0.00) 0.943
                                Whatever B |                                                                                                              
                                  ext      | . (0.00) 1.000 . (0.00) 1.000 -0.76 (-0.72) 0.000 -0.37 (-0.02) 0.073 -0.04 (-0.00) 0.846   0.39 (0.02) 0.063
                                  dom      | . (0.00) 1.000 . (0.00) 1.000 -0.12 (-0.08) 0.578 -0.76 (-0.02) 0.000   0.52 (0.02) 0.009   0.16 (0.01) 0.456
                                Whatever C |                                                                                                              
                                  pr       | . (0.00) 1.000 . (0.00) 1.000 -0.35 (-1.01) 0.096 -0.91 (-0.13) 0.000   0.10 (0.02) 0.648   0.73 (0.12) 0.000
                                  la       | . (0.00) 1.000 . (0.00) 1.000   0.65 (0.79) 0.001   0.26 (0.02) 0.214 -0.15 (-0.01) 0.485 -0.09 (-0.01) 0.692
                              B            |                                                                                                              
                                Whatever A |                                                                                                              
                                  fd       | . (0.00) 1.000 . (0.00) 1.000 -0.37 (-0.06) 0.198 -0.82 (-0.06) 0.000   0.52 (0.04) 0.058   0.32 (0.02) 0.261
                                  ka       | . (0.00) 1.000 . (0.00) 1.000 -0.46 (-0.24) 0.096 -0.43 (-0.10) 0.127   0.24 (0.05) 0.413   0.20 (0.04) 0.490
                                Whatever B |                                                                                                              
                                  ext      | . (0.00) 1.000 . (0.00) 1.000 -0.44 (-0.02) 0.114 -0.37 (-0.01) 0.190   0.21 (0.00) 0.478   0.18 (0.00) 0.549
                                  dom      | . (0.00) 1.000 . (0.00) 1.000 -0.35 (-0.04) 0.222 -0.67 (-0.04) 0.008   0.20 (0.01) 0.497   0.50 (0.03) 0.066
                                Whatever C |                                                                                                              
                                  pr       | . (0.00) 1.000 . (0.00) 1.000 -0.15 (-0.07) 0.621 -0.98 (-0.20) 0.000   0.38 (0.08) 0.185   0.64 (0.12) 0.014
                                  la       | . (0.00) 1.000 . (0.00) 1.000 -0.30 (-0.01) 0.296 -0.70 (-0.01) 0.005 -0.04 (-0.00) 0.881   0.79 (0.01) 0.001
                              C            |                                                                                                              
                                Whatever A |                                                                                                              
                                  fd       | . (0.00) 1.000 . (0.00) 1.000 -0.23 (-0.23) 0.525 -0.99 (-0.03) 0.000   0.23 (0.01) 0.524   0.37 (0.02) 0.291
                                  ka       | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
                                Whatever B |                                                                                                              
                                  ext      | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
                                  dom      | . (0.00) 1.000 . (0.00) 1.000 -0.40 (-0.27) 0.254 -0.85 (-0.02) 0.002 -0.16 (-0.01) 0.659   0.69 (0.03) 0.028
                                Whatever C |                                                                                                              
                                  pr       | . (0.00) 1.000 . (0.00) 1.000 -0.27 (-0.64) 0.451 -0.97 (-0.08) 0.000   0.12 (0.02) 0.745   0.47 (0.06) 0.166
                                  la       | . (0.00) 1.000 . (0.00) 1.000 -0.42 (-0.03) 0.228 -0.29 (-0.00) 0.414 -0.13 (-0.00) 0.721   0.31 (0.00) 0.382
                              D            |                                                                                                              
                                Whatever A |                                                                                                              
                                  fd       | . (0.00) 1.000 . (0.00) 1.000 -0.62 (-0.31) 0.001 -0.96 (-0.05) 0.000   0.44 (0.02) 0.030   0.42 (0.02) 0.040
                                  ka       | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
                                Whatever B |                                                                                                              
                                  ext      | . (0.00) 1.000 . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000      . (0.00) 1.000
                                  dom      | . (0.00) 1.000 . (0.00) 1.000 -0.37 (-0.07) 0.077 -0.73 (-0.01) 0.000   0.60 (0.01) 0.002   0.04 (0.00) 0.858
                                Whatever C |                                                                                                              
                                  pr       | . (0.00) 1.000 . (0.00) 1.000 -0.16 (-0.11) 0.467 -0.65 (-0.04) 0.001   0.16 (0.01) 0.467   0.43 (0.03) 0.035
                                  la       | . (0.00) 1.000 . (0.00) 1.000 -0.34 (-0.11) 0.107 -0.09 (-0.00) 0.687   0.03 (0.00) 0.899   0.05 (0.00) 0.813
                              ----------------------------------------------------------------------------------------------------------------------------
                              Correlations, (covariances), and p-values.

                              Comment

                              Working...
                              X