sorting - Ordering a varlist in Stata code -


i see following programming exercise, rather statistically grounded way of doing things.

basically, i'd run n logistic regressions 1 predictor variable , each variable store variable name chi-squared value. after predictions done, want display each predictor variable ordered chi-squared highest lowest.

so far have following:

local depvar    binvar1 local indepvars predvar1 predvar2 predvar3  * expand , check collinearity * _rmdcoll `depvar' `indepvars', expand local indepvars "`r(varlist)'"  * first order individual variables best chi-squared * local vars local chis foreach v in `indepvars' {     di "run: logistic `depvar' `v'"     quietly logistic `depvar' `v'      * check if variable not omitted (constant , iv) *     if `e(rank)' < 2 {         di "omitted (rank < 2): `v'"         continue     }      * check if chi-squared > 0 *     if `e(chi2)' <= 0 {         di "omitted (chi2 <= 0): `v'"         continue     }      * store *     local vars "`vars' `v'"     local chis "`chis' `e(chi2)'"     di "added: `v' (chi2: `e(chi2)')" }  * ... sort each variable (from varlist vars) chi2 (from varlist chis) ... *  

how sort each variable returned chi-square in last line , display list of variables chi-squared ordered highest chi-squared lowest chi-squared?

to clear, if following varlists resulted above:

local vars predvar1 predvar2 predvar3 local chis 2 3 1 

then following:

local ordered predvar2 3 predvar1 2 predvar3 1 

or, alternatively,

local varso predvar2 predvar1 predvar3 local chiso 3 2 1 

here 1 way it.

local depvar    binvar1 local indepvars predvar1 predvar2 predvar3  * expand , check collinearity * _rmdcoll `depvar' `indepvars', expand local indepvars "`r(varlist)'"  * first order individual variables best chi-squared *  gen chisq = .  gen vars = ""  local = 1   foreach v in `indepvars' {      di "run: logistic `depvar' `v'"      quietly logistic `depvar' `v'       * check if variable not omitted (constant , iv) *      if `e(rank)' < 2 {           di "omitted (rank < 2): `v'"      }       * check if chi-squared > 0 *      else if `e(chi2)' <= 0 {           di "omitted (chi2 <= 0): `v'"      }       * store *      else {             quietly replace vars  = "`v'" in `i'            quietly replace chisq = -e(chi2) in `i'            local ++i              di "added: `v' (chi2: `e(chi2)')"      } }  sort chisq replace chisq = -chisq  l vars chisq if chisq < ., noobs  

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -