结合Pearson / Spearman等级相关矩阵与Stata中的显着星

时间:2014-08-08 19:48:10

标签: matrix latex stata correlation

我想计算相关矩阵,其中下三角由Pearson组成,上三角由Spearman等级相关系数组成。 我使用的是corrspearman,效果很好。但是corr我无法获得重要性(p值或星等)。所以我尝试了pwcorr, list,这给了我确切的结果,但具有重要意义。

使用这个“新组合”我不能像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)

当我用corr替换pwcorr, list时,我收到以下错误:

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

(使用的代码来自http://www.stata.com/statalist/archive/2014-01/msg00349.html。)

有解决这个问题的“智能”方法吗? (顺便说一句,我正在使用TeXMaker,所以如果可以为LaTeX传输输出会很棒。)

2 个答案:

答案 0 :(得分:1)

这种方法采用Ben Jann的estout documentation方法,主要是在星期五下午的懒惰之后,并使用Spearman相关性只是Pearson相关性的事实。将等级和原始数据堆叠在同一个变量中可以使这个Frankenmatrix更容易拼接。

它需要一个变量列表并生成一个LaTeX文件,其中包含主对角线下方的Pearson相关性以及它上方的Spearman相关性。两者都有重要的明星。

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'

您应该在tex文档中使用\input{frankenmatrix.tex}来合并此文件。输出应该如下所示: enter image description here

答案 1 :(得分:0)

按照您(旧)Statalist上引用的主题进行操作。它指向corsp,一个用户编写的SSC命令,它也产生p值。

对于导出到LaTeX,有几个用户编写的命令,只需在Stata中键入findit latex