Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: Creating loops to drop observations each time regression is run


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: Creating loops to drop observations each time regression is run
Date   Sun, 2 Apr 2006 17:14:48 +0100

I support the idea of using -foreach- here, also 
recommended by Thuy Le. But I would approach 
the matter differently in detail. 

First, 

levelsof code, local(C) 

or in Stata 8 

levels code, local(C) 

removes the need to type in all the names. 

Second, why the repeated -use- and -drop-? You 
could also do it this way: 

use dataset 
levelsof code, local(C) 
foreach c of local C { 
	di "omitting `c':" 
	regress y x if code != "`c'" 
} 

If there are any problems with the -levelsof-/-levels- 
approach, you can do this: 

use dataset 
egen CODE = group(code), label 
su CODE, meanonly 
forval i = 1/`r(max)' { 
	di "omitting `: label (CODE) `i''" 
	regress y x if code != `i' 
}

See also

FAQ     . . . . . . . . . . Making foreach go through all values of a variable
            8/05    Is there a way to tell Stata to try all values of a
                particular variable in a foreach statement without
                specifying them?
                http://www.stata.com/support/faqs/data/foreach.html

Nick 
[email protected] 

Neil Shephard
 
> Although in this example forval may be more appropriate...
> 
> >> use dataset.dta, clear
> >> drop if code=="country1"
> >> regress y x
> >>
> >> use dataset.dta, clear
> >> drop if code=="country2"
> >> regress y x
> >
> 
> Assuming that code is always countryN then you could...
> 
> forval x = 1/N{
>    use dataset, clear
>    drop if(code == "country`x'")
>    regress y x
> }
> 
> You would substitute N in the above for the maximum country number.
> 
> If however you actually have named countries such as "Australia",
> "United States", "Germany", "Nepal", "United Kingdom", "France" etc.
> then you would have to use foreach....
> 
> foreach x in Australia "United States" France Germany "United 
> Kingdom" Nepal{
>    use dataset, clear
>    drop if(code == "`x'")
>    regress y x
> }
> 
> Note that countries where there are two parts to the name need to be
> encapsulated in double quotes.
> 
> Nick Cox has written a "Speaking Stata" article on this very topic...
> 
> Cox, NJ (2002) Speaking Stata: How to face lists with fortitude. Stata
> Journal 2:202-222
> 
> http://ideas.repec.org/a/tsj/stataj/v2y2002i2p202-222.html
> 

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