Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: predict after by varlist: regress

From   "Nick Cox" <>
To   <>
Subject   RE: predict after by varlist: regress
Date   Mon, 15 Sep 2003 16:58:21 +0100

Oleksandr Shepotylo
> Is there possibility to use predict after estimation with by?
> For example, I estimated linear regression using file X1:
>       by pensiontype: reg pension age gender region_dummy
> then I wanted to predict pension using another file X2. But 
> predict did not
> work after regression with by.

Not so; it works. The only question is whether it 
works the way you think it should; evidently not. 

The rule is that -predict- picks up the results
of the last model fitted. Thus after 

. bysort foreign : regress mpg weight 

. predict mpgpre 

will pick up results for the last group of 
-foreign- and use those -- and calculate predictions 
for the whole data set unless you specify otherwise. 

I guess what you want is some sort of 
composite, a variable containing (in this example) 

if foreign == 0 
	predicted values from -regress mpg weight if 
	foreign == 0- 

if foreign == 1 
	predicted values from -regress mpg weight if 
	foreign == 1- 

Here is one way of doing that, generalised, 

gen predict = . 
levels <varname>, local(levels) 
qui foreach l of local levels { 
	regress <response> <covariates> if <varname> == `l' 
	predict temp 
	replace predict = temp if e(sample) 
	// OR replace predict = temp if <varname> == `l' 
	// depending on what you want, precisely 
	drop temp 

As said, this is a composite, but it may be what you 

If <varname> is string use "`l'" or `"`l'"' instead. 


*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index