# Re: st: AW: how to get slopes by clusters in a linear regression

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject Re: st: AW: how to get slopes by clusters in a linear regression Date Tue, 25 Aug 2009 14:37:23 -0400

```László Sándor <sandorl@gmail.com>:
Your code does not run as written, and the approach you outline does
not do what you want (unless I am misunderstanding your problem):

webuse grunfeld, clear
set seed 1
ren company c
keep if c<5
qui ta c, g(d_)
g byte t=uniform()>.5
forv i=1/4 {
g byte td_`i'=d_`i'*t
}
drop d_1
reg mvalue kstock d_* td_*
g good=.
forv i=1/4 {
replace good=_b[td_`i'] if c==`i'
}
qui reg mvalue kstock
predict m2, res
qui reg t kstock
predict t2, res
forv i=1/4 {
qui reg m2 t2 if c==`i'
}

Again: Make sure you are getting the right answer in a small subsample
before you go too far...

2009/8/25 László Sándor <sandorl@gmail.com>:
> Austin,
> thanks for the code. I meant the latter approach, or even simpler, as
> you don't need interactions if you will estimate it cluster by
> cluster.
>
> In your code (without collecting the coefficients):
>
> qui reg mvalue kstock
> predict m2, res
> qui reg t kstock
> predict t2, res
> sortby c: reg m2 t2
>
> The last can be made more useful with statsby, as Martin suggested.
>
> Thanks,
>
> Laszlo
>
>
> On Tue, Aug 25, 2009 at 1:31 PM, Austin Nichols<austinnichols@gmail.com> wrote:
>> László Sándor <sandorl@gmail.com>:
>> Using the FWL thm is the fastest way to go, probably, but I am not
>> clear on what you are actually doing.  Is it something like one of the
>> approaches below?  Or are you trying to get the individual-specific
>> coefs on the treatment dummy (harder, but can be done in Mata without
>> making any really big matrices)?
>>
>> webuse grunfeld, clear
>> set seed 1
>> ren company c
>> keep if c<5
>> qui ta c, g(d_)
>> g byte t=uniform()>.5
>> forv i=1/4 {
>>  g byte td_`i'=d_`i'*t
>>  }
>> drop d_1
>> reg mvalue kstock d_* td_*
>> est sto reg1
>> egen double m1=mean(mvalue), by(c t)
>> g double mres=mvalue-m1
>> egen double k1=mean(kstock), by(c t)
>> g double kres=kstock-k1
>> areg mres kres, a(c)
>> est sto reg2
>> egen c2=group(t c)
>> areg mvalue kstock, a(c2)
>> est sto reg3
>> est table reg?
>>
>> Or are you doing something like:
>>
>> qui reg mvalue td_2 td_3 td_4 d_* kstock
>> predict m2, res
>> qui reg td_1 td_2 td_3 td_4 d_* kstock
>> predict t2, res
>> reg m2 t2
>>
>> which still needs big matrices as the problem gets big?  Make sure you
>> are getting the right answer in a small subsample before you go too
>> far...
>>
>> 2009/8/25 László Sándor <sandorl@gmail.com>:
>>> Thank you, Martin!
>>>
>>> I don't see how this could be used to estimate the model in a single
>>> command -- statsby still seem to break down the regression by
>>> clusters, without the intercluster restrictions on the
>>> controls/covariates.
>>>
>>> However, this led me to try to apply the Frisch-Waugh-Lowell theorem:
>>> I estimate the univariate regression of outcome on treatment by each
>>> cluster, I must only use the residuals for both after regressing them
>>> on the set of controls.
>>>
>>> If there is no other way, this seems to be doable.
>>>
>>> Thanks again!
>>>
>>> Laszlo
>>>
>>> On Tue, Aug 25, 2009 at 11:23 AM, Martin Weiss<martin.weiss1@gmx.de> wrote:
>>>>
>>>> <>
>>>>
>>>>
>>>> ******
>>>> h statsby
>>>> ******
>>>>
>>>>
>>>> HTH
>>>> Martin
>>>>
>>>>
>>>> -----Ursprüngliche Nachricht-----
>>>> Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von László Sándor
>>>> Gesendet: Dienstag, 25. August 2009 17:20
>>>> An: statalist@hsphsun2.harvard.edu
>>>> Betreff: st: how to get slopes by clusters in a linear regression
>>>>
>>>> Dear Fellow Statalisters,
>>>>
>>>> I want to extend a fixed-effects-type model to allow for different
>>>> coefficients on a variable (actually a treatment dummy) by each
>>>> cluster I have. The richness of my data would allow for that. However,
>>>> I did not find a way to do it in Stata that would report (and collect)
>>>> the coefficients themselves. -xtmixed- doesn't seem to do so. I would
>>>> like to restrict the coefficients on controls to be equal across
>>>> clusters, so estimation by cluster is not a solution either.
>>>>
>>>> If there were a way that could collect the slopes to a single new
>>>> variable (with the same value for observations in the same cluster,
>>>> naturally), that would be the best. It would be great if I did not
>>>> need to introduce all the 1438 cluster-indicator variables and
>>>> interactions myself, and collect the coefficients.
>>>>
>>>> Thank you for any guidance in advance!
>>>>
>>>> Laszlo

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```