### Monday, March 13, 2006

## Fwd: st: easy way to save regressor variable names?

Apologies - please ignore my previous email. A while loop solves the problem fine without having to reference the regression equation number.

bw,

Toby

local clist local j = 1 while index("``j''","_")==0 { local clist `clist' ``j'' local j = `j' + 1 di "`clist' " }

Begin forwarded message:

> From: Toby Andrew <t.andrew@ic.ac.uk> > Date: 12 March 2006 21:48:44 GMT > To: statalist@hsphsun2.harvard.edu > Subject: Re: st: easy way to save regressor variable names? > > Thank you very much Rose & Nick for your help with this. > > The code seems to work just as you suggest ... except for mlogit > predictor variable names, which are duplicated for each outcome > factor level. > Could you tell me how to reference equation numbers? i.e. what do > i need to add to the following line of code: > > if B[1, `j'] != 0 & "``j''" != "_cons" { > } > > so that only the variables listed in one of the factor equations > are saved in the macro list? > Thanks, > > Toby > > ********************************************************************** > ** > Example output: > > Multinomial logistic regression Number of obs > = 841 > LR chi2(4) > = 256.37 > Prob > chi2 > = 0.0000 > Log likelihood = -733.84352 Pseudo R2 > = 0.1487 > > ---------------------------------------------------------------------- > -------- > v54 | Coef. Std. Err. z P>|z| [95% > Conf. Interval] > ------------- > +---------------------------------------------------------------- > 0 | > v52 | -1.30788 .3571904 -3.66 0.000 > -2.00796 -.6077996 > v59 | -33.65493 3135026 -0.00 1.000 > -6144572 6144505 > _cons | -.0186747 .0888481 -0.21 0.834 -. > 1928137 .1554643 > ------------- > +---------------------------------------------------------------- > 2 | > v52 | .9150756 .2319136 3.95 0.000 . > 4605334 1.369618 > v59 | .1055173 .2495306 0.42 0.672 -. > 3835537 .5945883 > _cons | -1.522468 .1388951 -10.96 0.000 > -1.794697 -1.250239 > ---------------------------------------------------------------------- > -------- > (Outcome v54==1 is the comparison group) > > mat B=e(b) > > .mat lis B > > B[1,6] > 0: 0: 0: 2: > 2: 2: > v52 v59 _cons v52 > v59 _cons > y1 -1.30788 -33.654931 -.01867473 .91507565 .10551731 > -1.522468 > > . local clist > > . tokenize "`: colnames e(b)'" > > . forval j = 1/`= colsof(B)' { > 2. if B[1, `j'] != 0 & index("``j''","_")==0 { > 3. local clist `clist' ``j'' > 4. di "`clist'" > 5. } > 6. } > v52 > v52 v59 > v52 v59 v52 > v52 v59 v52 v59 > ********************************************************************** > ** > > > On 12 Mar 2006, at 00:47, Rose Medeiros wrote: > >> Thank you Nick! >> >> Nick Cox wrote: >> >>> I want to comment on the code just posted. >>> local cons "_cons" >>> matrix define B = get(_b) >>> local p : colfullnames e(b) >>> local n = "`s(after)'" >>> local r : list sizeof n >>> local clist = " " >>> forvalues j = 1/`r' { >>> scalar define c`j' = B[1,`j'] >>> if (c`j'~=0) { >>> local newpred : word `j' of `p' >>> local clist : list clist | newpred >>> } >>> } >>> local p_list : list clist - cons >>> >>> Note that evaluating when you only need to copy is a source of >>> bugs. (This is one of the outputs of -njc-.) The result of >>> local n = "`s(after)'" >>> will be truncated to at most 80 characters >>> as a side-effect of the evaluation. >>> Also, "`s(after)'" may include options, -if- >>> and -in- stuff, and indeed much else, so the number of words in >>> it may easily differ from the number of columns in e(b). Leave it >>> well alone for this problem. >>> More a matter of style: there is some unnecessary copying and >>> setting up here. The code may be an extract from a longer code >>> chunk in which things are defined for >>> later use, but as far as what is visible >>> is concerned it can be slimmed down to >>> mat B = e(b) tokenize "`: colnames e(b)'" local clist forval j = >>> 1/`= colsof(B)' { if B[1, `j'] != 0 & "``j''" != "_cons" { >>> local clist `clist' ``j'' >>> } >>> } >>> I wouldn't be surprised at a shorter way of doing it. >>> Nick n.j.cox@durham.ac.uk >>> Rose Medeiros >>> >>>> My answer to your first question will depend on the regression >>>> command used. (I say my answer, since someone else may have a >>>> better way of doing this.) After you run regress, if covariates >>>> are dropped for collinearity, they will appear in the >>>> coefficient vector (e(b)), but the coefficient value in the >>>> vector e(b) will be zero. For commands like logit, ologit, and >>>> mlogit, independent variables dropped for collinearity will not >>>> appear in the coefficient vector. So you have to accommodate the >>>> particular regression command used. Also note that the constant >>>> (_cons) will be in the coefficient vector, so you'll have to >>>> remove that as well. Below is some syntax that I've been using >>>> to do what you describe (the list of variables is returned as >>>> the local macro p_list), it appears to work, but I make no >>>> guarantees. >>>> >>> >>>> * -------- begin example when command is regress ----------- >>>> * run regress >>>> local cons "_cons" >>>> matrix define B = get(_b) >>>> local p : colfullnames e(b) >>>> local n = "`s(after)'" >>>> local r : list sizeof n >>>> local clist = " " >>>> forvalues j = 1/`r' { >>>> scalar define c`j' = B[1,`j'] >>>> if (c`j'~=0) { >>>> local newpred : word `j' of `p' >>>> local clist : list clist | newpred >>>> } >>>> } >>>> local p_list : list clist - cons >>>> *-------- end example when command is regress ----------- >>>> >>>> *-------- begin example when command is logit, ologit, or mlogit >>>> ----------- >>>> * run logit or mlogit >>>> local p : colnames e(b) >>>> local p_list : list p - cons >>>> *-------- end example when command is logit, ologit, or mlogit >>>> ----------- >>>> >>> >>> Toby Andrew >>>>> 1) Is there a generic way to access the list of variable >>>> names used >>>>> in a regression such as regress, mlogit & ologit? >>>>> The reason i ask is because i am performing many regressions >>>>> by looping over variables & the list of recorded variables i save >>>>> will differ from the *actual* list retained in the analysis >>>> if there >>>>> is collinearity (or i wish to use stepwise sw). >>>>> >>> >>> >>> * >>> * 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/ >>> >>> >> >> >> -- >> Rose Anne Medeiros >> Department of Sociology / Family Research Laboratory >> University of New Hampshire >> 126 Horton Social Science Center >> 20 College Road >> Durham, NH 03824 >> U.S.A. >> >> * >> * 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/ >

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

Tag: statalist