On Thursday, xiaoming wang wrote:
> I want to retrieve all correlation coefficients from command PWCORR.
>
> How to do it?
The command -pwcorr- does not save much in the way of results. So to get
hold of all the correlations, we need to calculate them ourselves.
Below is some code that calculates the pairwise correlation coefficients
and stores them in a matrix. It also saves, in another matrix, the
number of observations used in the computation of each correlation.
This is very important because this is the big difference between -corr-
and -pwcorr-: -pwcorr- uses as many observations as possible in the
computation of the correlation coefficient of each pair, whereas -corr-
only uses those observations that are complete (no missing data) for all
variables in the variable list.
*------ generate some data-----------
clear
set obs 100
local varnum=5
forvalues i=1/`varnum'{
gen var`i'=uniform()
}
replace var1=. if _n>80
replace var4=. if _n<10
*--------calculate correlations--------
matrix C = I(`varnum')
matrix N = I(`varnum')
forvalues i=1(1)`varnum' {
forvalues j=1(1)`i'{
capture corr var`i' var`j'
if _rc==0{
matrix C[`i',`j']=r(rho)
matrix C[`j',`i']=r(rho)
matrix N[`i',`j']=r(N)
matrix N[`j',`i']=r(N)
}
else{
matrix C[`i',`j']=.
matrix C[`j',`i']=.
matrix N[`i',`j']=.
matrix N[`j',`i']=.
}
matrix C[`i',`i']=1
matrix N[`i',`i']=r(N)
}
}
*---display results-------------
display as text "sample correlations"
matrix list C, format(%6.4f) noheader
display as text "number of observations"
matrix list N, noheader
*--- check its the same as from pwcorr-----
pwcorr var1 var2 var3 var4 var5
As you can see, for this code to work easily, the variables need to be
named something nice. You can do this by renaming them, and if you want
the dataset to return to it's previous state once you're done, put
-preserve- at the start and -restore- at the end.
What's the easiest way to change a bunch of variables names? Try
something like this:
*--- get some data--------
clear
sysuse auto
keep mpg price weight length
*---- my list of variables------
local varlist="price mpg weight length"
*-----rename them----
tokenize `varlist'
local i=1
while "`1'"!=""{
rename `1' var`i'
local i=`i'+1
macro shift
}
yours,
May
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/