Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: correlate by group and collapse


From   Roger Harbord <[email protected]>
To   [email protected]
Subject   st: correlate by group and collapse
Date   Wed, 10 Jul 2002 20:44:15 +0100

Dear Statalisters,

I want to collapse my dataset by a group variable and retain the 
correlation coefficient of two variables.  In other 
words, I'd like to be able to do something like:
. collapse (correlation) var1 var2, by(group)
or maybe:
. by group: egen corr12=corr(var1 var2)
. collapse corr12, by(group)

However, collapse doesn't have correlation among its stats (it only 
allows a selection of univariate statistics) and egen doesn't have a 
corr function.
I know I can do:
. by group: correlate var1 var2
- but I want to save the results and do further analysis on them rather
than just displaying them.

The best I've come up with is (supposing I have 100 groups):
. gen corr12=.
. for num 1/100, noheader: qui correlate var1 var2 if group==X \ 
      qui replace corr12=r(rho) if study==X
. collapse corr12, by(group)

This seems kind of clumsy though, and it took me a while to work out 
that I needed _noheader_ and _quietly_ to stop my screen filling with 
output. It also becomes quite lengthy if I want several pairwise 
correlations. Is there a better way? 

I think I'd like egen to have a _corr_ and/or a _cov_ function - I 
would have thought it would be of wider interest than the calculation 
of U.S. marginal income tax rates, which is already implemented as egen 
function mtr! I've checked the extensions to egen in the STB package 
_egenodd_ and tried a couple of _findit_'s, but I didn't find anything 
suitable.

Thanks in advance for any ideas,
Roger.

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index