Using -statsby- and the other approach are strictly solutions to
different problems.
-statsby- is what has been called a reduction command. It can be clever
about doing repeated -if- selections, by virtue of suggestions made by
Michael himself. There is nothing in my code that is clever on that
score.
I doubt that using -levelsof- is outstandingly inefficient in machine
time as such; it's just that it is unnecessary.
Nick
[email protected]
Philipp Rehm
Thanks, Nick.
That's a good idea, too.
I haven't compared the efficiency of Michael's and your solution. But
given that I have 162 county-years with a total of around 3.5 million
observations, either solution should be much much more efficient than my
-levelsof- approach.
Nick Cox wrote:
> On the other hand, if you want the covariances alongside the data,
this
> approach is cleaner than recourse to -levelsof-:
>
> gen cov = .
> egen group = group(country year)
> su group, meanonly
>
> quietly forval i = 1/`r(max)' {
> corr x l2.x [aw=weight] if group == `i', cov
> replace cov = r(cov_12) if group == `i'
> }
>
> See also
>
> FAQ . . . . . . . . . . Making foreach go through all values of a
> variable
> 8/05 Is there a way to tell Stata to try all values of a
> particular variable in a foreach statement without
> specifying them?
> http://www.stata.com/support/faqs/data/foreach.html
>
> However, -statsby- and then using -merge- may be just as efficient.
>
> Michael Blasnik
>
> ...
> You want the -statsby- command. Something like this work:
>
> statsby cov=r(cov_12), by(year country) : corr x l2.x [aw= weight],
cov
>
> Philipp Rehm
>
>> 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.
*
* 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/