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
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.
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)
