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

st: Re: retaining residuals using "by group" with regression


From   Roger Newson <[email protected]>
To   [email protected]
Subject   st: Re: retaining residuals using "by group" with regression
Date   Tue, 27 Jan 2004 15:36:34 +0000

At 06:49 27/01/04 -0800, David Bernstein wrote:
Hello STATers,

How do I retain my predicted values from a regression
when using the "by group" command?

for example:

by company: reg x y1 y2 y3
predict zresid, rstudent

Is it possible to create a data file that contains the
zresids that is associated with the regression for
each each company?
I think the best way is probably in a loop, using the official Stata -levels- command and my own -dsconcat- package (downloadable from SSC), similarly to the following (assuming that -company- is an integer-valued variable):

levels company
local coms "`r(levels)'"
preserve
local flist ""
local i1=0
foreach com of numlist `coms' {
local i1=`i1'+1
tempfile tf`i1'
restore, preserve
keep if company==`com'
regress x y1 y2 y3
predict zresid, rstudent
save `tf`i1'', replace
local flist "`flist' `tf`i1''"
}
dsconcat `flist'

This should cause the memory to contain a data set with the existing variables plus a new variable -zresid- containing studentised residuals. In the more general case, where by-groups may be based on multiple variables, you can use the official Stata -gsort- command with the -generate()- option. And, if you also want to save the model parameters, then you might use the -parmest- package, also downloadable from SSC.

I hope this helps.

Roger


--
Roger Newson
Lecturer in Medical Statistics
Department of Public Health Sciences
King's College London
5th Floor, Capital House
42 Weston Street
London SE1 3QD
United Kingdom

Tel: 020 7848 6648 International +44 20 7848 6648
Fax: 020 7848 6620 International +44 20 7848 6620
or 020 7848 6605 International +44 20 7848 6605
Email: [email protected]
Website: http://www.kcl-phs.org.uk/rogernewson

Opinions expressed are those of the author, not the institution.

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




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