Statalist The Stata Listserver


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

RE: st: easy way to save regressor variable names?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: easy way to save regressor variable names?
Date   Sat, 11 Mar 2006 17:27:47 -0000

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 
[email protected] 

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/



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