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

st: RE: Residuals from sequence of regressions

From   "Maarten Buis" <[email protected]>
To   <[email protected]>
Subject   st: RE: Residuals from sequence of regressions
Date   Sun, 17 Oct 2004 12:01:47 +0200

Dear Alex,

The following sollution may be less then elegant but it does work. The first few lines generate an example dataset. If you want to apply this to your own dataset you need the for loop and the -gen resid=.- command.




generate a dataset with hundred

`observations' in 10 `years'


capture drop _all

set obs 1000

gen year = floor((_n-1)/100) 

/* first hundred are year 0,

second hundred are year 1, etc. */

tab year

gen x = invnorm(uniform()) 

/* observations of x are draws from

a standard normal distribution */

gen y = 1 + 2*x + invnorm(uniform())

gen resid = . 

/* create the variable in which

the residuals will be stored */ 


/* do the regressions and store the residuals */

forvalues i=0(1)9 {

reg y x if year==`i'

predict resid`i' if year==`i', resid

replace resid = resid`i' if resid==.


	-----Oorspronkelijk bericht----- 
	Van: Alex Edmans [mailto:[email protected]] 
	Verzonden: zo 17-10-2004 7:35 
	Aan: [email protected] 
	Onderwerp: st: Residuals from sequence of regressions

	I have a large balanced panel of N observations over T years. I would like
	to do a cross-sectional regression for each year, and record the residuals
	for the regression for each of the N observations for that year. E.g. for
	1960, I want each of the N observations to have a new variable which is the
	residual from the 1960 cross-sectional regression across the N observations.
	Then I want the same for 1961 etc.
	I know how to run individual regressions for each year, but I am unsure how
	to store the residuals from the regression for each year. To run the
	regressions, I type:
	Sort year
	By year: quietly reg y x1 x2 x3
	Normally, after a regression you can type "predict res, residual", but I am
	not sure how to incorporate it in the above framework. Stata doesn't seem to
	let me type more than one command after the "by" function. E.g. it won't let
	me type something like
	By year: quietly reg y x1 x2 x3, predict res, residual
	and typing "predict res, residual" at the end merely gives me the residuals
	for the cross-sectional regression for the last year.


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