Combined Pearson/Spearman rank correlation matrix with significance stars in Stata

我只是一个虾纸丫 提交于 2020-01-15 07:11:34

问题


I want to calculate a correlation matrix, where the lower triangle consists of Pearson and the upper triangle consists of Spearman rank correlation coefficients. I am using corr and spearman, which works fine. But with corr I cannot get the significance (p-value or stars and so on). So I tried pwcorr, list which gives me the exact results but with significance.

Using this "new combination" I cannot create the matrix as with corr.

//Get Pearson Matrix
corr var1 var2 var3

matrix R = r(C)

//Get Row and Column Names
local rnames : rownames R
local cnames : colnames R

//Get Spearman Rank Matrix
spearman var1 var2 var3, matrix star(0.05)
matrix S = r(Rho)

//Convert Pearson Matrix to Mata Matrix
mata: mataR = st_matrix("R")

//Convert Spearman Rank Matrix to Mata Matrix
mata: mataS = st_matrix("S")

//Clone Mata Pearson Matrix for Combined mataRS Mata Matrix
//Pearsson and Spearman Rank Matrix in Mata
mata: mataRS = mataR

//Replace Pearson r with Spearman rho in Top Half of Combined mataRS Mata Matrix
mata: mataRS[1,2] = mataS[2,1]
mata.... and so on.

//Display Pearson, Spearman Rank, and combined Matrices in Mata
mata: mataR
mata: mataS
mata: mataRS

//Convert combined mataRS Mata Matrix to Stata Matrix RS
mata: st_matrix("RS", mataRS)
matrix rownames RS = `rnames'
matrix colnames RS = `cnames'

//Display combined Stata Matrix RS
matlist RS, format(%8.4f)

When I replace corr with pwcorr, list I get the following error:

mata: mataRS[1,2] = mataS[2,1]
<istmt>:  3301  subscript invalid" for the command

(The code using is taken from http://www.stata.com/statalist/archive/2014-01/msg00349.html.)

Is there a "smart" way to solve this? (By the way, I am working with TeXMaker, so it would be great if the output can be transferred for LaTeX.)


回答1:


This approach adopts the method from Ben Jann's estout documentation mostly out of Friday afternoon laziness, and uses the fact that Spearman correlation is just Pearson correlation on ranks. Stacking the ranks and the raw data in the same variable makes stitching this Frankenmatrix somewhat easier.

It takes a variable list and produces a LaTeX file that contains Pearson correlations below the main diagonal and Spearman correlations above it. Both will have significance stars.

eststo clear
set more off
sysuse auto, clear
capture ssc install estout

local vlist "price mpg weight"
local upper
local lower `vlist'

expand 2, gen(version)

foreach v of local vlist {
    egen rank = rank(`v') if version == 1
    replace `v' = rank if version ==1
    drop rank
}

foreach v of local vlist {

   estpost correlate `v' `lower' if version == 0
   foreach m in b rho p count {
       matrix `m' = e(`m')
   }

   if "`upper'"!="" {
   estpost correlate `v' `upper' if version == 1
       foreach m in b rho p count {
           matrix `m' = e(`m'), `m'
       }
   }
   ereturn post b
   foreach m in rho p count {
       quietly estadd matrix `m' = `m'
   }
   eststo `v', title(`v')
   local lower: list lower - v
   local upper `upper' `v'
 }

/* Export the LaTeX matrix */
esttab using "frankenmatrix.tex", nonumbers mtitles noobs not tex replace title("Correlations")

/* Clean up the data and make sure we did things right */
drop if version ==1
drop version

corr `vlist'
spearman `vlist'

You should use \input{frankenmatrix.tex} in your tex document to incorporate this file. The output should look something like this:




回答2:


Follow the thread you cited on (old) Statalist. It points to corsp, a user-written command on SSC that also produces p-values.

As for export to LaTeX, there are several user-written commands, just type findit latex in Stata.



来源:https://stackoverflow.com/questions/25211192/combined-pearson-spearman-rank-correlation-matrix-with-significance-stars-in-sta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!