Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: bys var: regression and predict??

 From D-Ta To statalist@hsphsun2.harvard.edu Subject Re: st: bys var: regression and predict?? Date Mon, 04 Apr 2011 19:49:58 +0200

thanks, given that i am controlling for a lot of other covariates (many dummies and interactions as well) this will be a lot of code. im suprised there is no routine for such problems yet...
```
Am 04.04.2011 12:08, schrieb Maarten buis:
```
```--- On Mon, 4/4/11, D-Ta wrote:
```
```I would like to estimate a model on sub-sets of data using

bys splitting_var: regress y x1 x2 x3

and then predict (and plot) y at different values of x1
while holding x2 and x3 constant at their mean values.

apparently -predict- does not work in the usual way after
"bys splitting_var:  regress"
```
```
*--------------------- begin example -------------------
sysuse auto, clear

// estimate and store models
reg price mpg weight if foreign == 1
est store for
reg price mpg weight if foreign == 0
est store dom

// we are going to dramatically change the data
// so we use preserve
preserve

// fix weight at its subgroup-mean
sum weight if foreign == 1
replace weight = r(mean) if foreign == 1
sum weight if foreign == 0
replace weight = r(mean) if foreign == 0

// predict
est restore for
predict yhat1 if foreign == 1, xb
label variable yhat1 "foreign"

est restore dom
predict yhat2 if foreign == 0, xb
label variable yhat2 "domestic"

// displays 10000 as 10,000 making it easier to read
format yhat* %8.0gc

// graph
sort mpg
twoway line yhat* mpg , ///
ytitle("predicted price (US {c S|})")

restore
*------------------- end example --------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------

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

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