Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: bys var: regression and predict??
Date   Mon, 4 Apr 2011 11:08:39 +0100 (BST)

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index