Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: create a variable with estimated coefficients on dummies

 From "Bernini, Michele" To "statalist@hsphsun2.harvard.edu" Subject Re: st: create a variable with estimated coefficients on dummies Date Thu, 16 Aug 2012 07:31:10 +0200

```Thanks a lot Nick for your suggestion. It works very well.
On 15 Aug 2012, at 20:14, Nick Cox wrote:

> I wouldn't approach it this way. You can save the coefficients to a
> different dataset
>
> sysuse auto, clear
> xi : statsby , by(foreign) : regress mpg i.rep78
> reshape long _b_Irep78_ , i(foreign)
>
> . l
>
>     +--------------------------------------+
>     |  foreign   _j   _b_Ir~78_    _b_cons |
>     |--------------------------------------|
>  1. | Domestic    2      -1.875         21 |
>  2. | Domestic    3          -2         21 |
>  3. | Domestic    4   -2.555556         21 |
>  4. | Domestic    5          11         21 |
>  5. |  Foreign    2           0   23.33333 |
>     |--------------------------------------|
>  6. |  Foreign    3           0   23.33333 |
>  7. |  Foreign    4    1.555556   23.33333 |
>  8. |  Foreign    5           3   23.33333 |
>     +--------------------------------------+
>
> and then (if I understand what you want) -merge- back to the original dataset.
>
> On Wed, Aug 15, 2012 at 7:27 PM, Bernini, Michele
> <Michele.Bernini@unitn.it> wrote:
>> Dear Statalister,
>>
>> I am programming a loop to execute  the following operations:
>>
>> 1- estimate a regression on a big set of dummies (country-hs6 product categories)
>>
>> 2-create a variable with the estimated coefficients on the dummies. For each observation (row) the value of this variable should correspond to the coeff. on the dummy that assumes value 1 for that observation.
>>
>> I tried something like that:
>>
>> gen ch=.
>> levelsof hs4, local(lvl_hs4) \\ level at which I run separate regressions
>> foreach element in `lvl_hs4'{
>> xi: reg Y X i.CH i.t if hs4==`element'
>> levelsof CH, local(clist)
>> foreach c of local clist {
>> replace ch = _b[_ICH_`c'] if CH == `c' \\ here I try to give the variable ch the value of the coefficients
>> }
>> }
>>
>> my problem is that the local `clist' includes also the missing category of the dummies, so when it looks for _b[_ICH_`c'] where c = "missing category" the loop aborts. In either words I would either need the local `clist' to include only the dummies created from CH in each regression, or to find another way to do that. I also tried with svmat but then I cannot match coefficients on the new variable 'ch' with the observations with each category in the dummies.
>
> *
> *   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/

Michele Bernini
Phd Candidate
School of International Studies (SIS)
University of Trento
Via Verdi, 8/10
I-38122 Trento
Italy

Tel. +39 3491831687

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