Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: RE: extending levelsof to more than one variable


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: extending levelsof to more than one variable
Date   Fri, 29 Jun 2012 15:48:13 +0100

-levelsof- is a command, not a function. 

Note that your -syntax- makes the -by()- option optional, but your program will fail if it is not specified. 

-levelsof- is not a good approach to your problem even for one variable. 

At the moment you are tacitly assuming that what you specify in -by()- is a numeric variable and that you can refer to its distinct levels without precision problems. 

It is better just to avoid any such assumptions altogether, even if they might never bite you. 

   levelsof `by', local(groups)
   foreach a of local groups {
       regress `varlist' if `by'==`a'
       tempvar d
       predict `d', residuals
       replace `g'=`d' if `by'==`a'
       }

would be better as something like

	tempvar group 
	bysort `by': gen `group' = _n == 1 
	replace `group' = sum(`group') 
	su `group', meanonly 

	qui forval i = 1/`r(max)' {
      	capture regress `varlist' if `group' == `i'
	      tempvar d
       	predict `d', residuals
		replace `g' = `d' if `group' == `i'
		drop `d' 
     }
 
The program is not especially smart about missing values, in `varlist' or `by', but -capture- may be sufficient protection about most such uses. But watch out. 



Nick 
[email protected] 


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Pradipto Banerjee
Sent: 29 June 2012 15:27
To: [email protected]
Subject: st: extending levelsof to more than one variable

I have a code below that I use as follows: "by group1 : egen var1new = neutralize (var1 var2 var3)" . The problem is that if I extend it to "by group1 group2 : egen var1new = neutralize (var1 var2 var3)" or "by group1 group2, sort : egen var1new = neutralize (var1 var2 var3)"  etc., then the code does not work as it depends upon levelsof to loop.

program define _gneutralize
   gettoken type 0 : 0
   gettoken g    0 : 0
   gettoken eqs  0 : 0
   syntax varlist(min=2) [, BY(string)]

   gen `type' `g' = .
   levelsof `by', local(groups)
   foreach a of local groups {
       regress `varlist' if `by'==`a'
       tempvar d
       predict `d', residuals
       replace `g'=`d' if `by'==`a'
       }
end

Is there is  an easy way to change this function to handle more than one group1 and also to include the ", sort" option within "by" ?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index