Bookmark and Share

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, is already up and running.

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

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

From   Maarten buis <>
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 

// 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|})")   

*------------------- end example --------------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

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

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index