st: Re: corr() with weights, -by-

 From "Michael Blasnik" To Subject st: Re: corr() with weights, -by- Date Sun, 08 Jun 2008 11:01:35 -0400

...
You want the -statsby- command. Something like this work:

statsby cov=r(cov_12), by(year country) : corr x l2.x [aw= weight], cov

M Blasnik

----- Original Message ----- From: "Philipp Rehm" <philipp.rehm@gmx.de>
To: <statalist@hsphsun2.harvard.edu>
Sent: Sunday, June 08, 2008 8:42 AM
Subject: st: corr() with weights, -by-

Dear all,

I am looking for an efficient way to extract weighted covariances, for a by-able problem.

Currently, I am running a very inefficient loop (using a lot of -levelof-s and if-qualifiers), along these lines (my data is -tsset-):

corr x l2.x [aweight=weight] if year==`i' & country==`j', covariance

I then write the results [r(cov_12)] into a variable, again using if-qualifiers.

A much quicker way to do this is to use the corr() function (written by Nick Winter) from the egenmore package. It does exactly what I want, except that it does not accept weights.

I can also run something like:

bys country year: correlate(x l2.x) [aweight=weight], covariance

but r(C) leaves behind the covariance matrix only for the very last estimation.

So, ideally I am looking for a version of Nick Winter's corr() which accepts weights.

Any ideas / suggestions would be appreciated. I prefer a solution that works in Stata 9, but I do have access to Stata 10.

Thank you,
Philipp
*
*   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/