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: RE: extending levelsof to more than one variable


From   Pradipto Banerjee <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: RE: extending levelsof to more than one variable
Date   Fri, 29 Jun 2012 10:18:40 -0500

Nick, thanks. I tried out your code, but Stata throws up an error: invalid syntax, r(198). It appears to be in the 2nd half of your code, but I can't figure out what is the issue ...

        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'
     }


I suspected the -regress- is causing an issue, particularly for the missing values , and so I tried another variant

        qui forval i = `r(max)' {
                capture regress `varlist' if `group' == `i'
                if _rc==0 {
                        tempvar d
                        predict `d', residuals
                        replace `g' = `d' if `group' == `i'
                        drop `d'
                        }
                }

But it make any difference either. Same error



-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Friday, June 29, 2012 10:48 AM
To: [email protected]
Subject: st: RE: extending levelsof to more than one variable

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

 This communication is for informational purposes only. It is not intended to be, nor should it be construed or used as, financial, legal, tax or investment advice or an offer to sell, or a solicitation of any offer to buy, an interest in any fund advised by Ada Investment Management LP, the Investment advisor.  Any offer or solicitation of an investment in any of the Funds may be made only by delivery of such Funds confidential offering materials to authorized prospective investors.  An investment in any of the Funds is not suitable for all investors.  No representation is made that the Funds will or are likely to achieve their objectives, or that any investor will or is likely to achieve results comparable to those shown, or will make any profit at all or will be able to avoid incurring substantial losses.  Performance results are net of applicable fees, are unaudited and reflect reinvestment of income and profits.  Past performance is no guarantee of future results. All f!
 inancial data and other information are not warranted as to completeness or accuracy and are subject to change without notice.

Any comments or statements made herein do not necessarily reflect those of Ada Investment Management LP and its affiliates. This transmission may contain information that is confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format.

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