1

我将以下内容视为编程练习,而不是基于统计的做事方式。

基本上,我想N用一个预测变量运行逻辑回归,然后为每个变量存储变量名称及其chi-squared值。完成所有预测后,我想显示按卡方从最高到最低排序的每个预测变量。

到目前为止,我有以下内容:

local depvar    binvar1
local indepvars predvar1 predvar2 predvar3

* expand and check collinearity *
_rmdcoll `depvar' `indepvars', expand
local indepvars "`r(varlist)'"

* first order individual variables by best chi-squared *
local vars
local chis
foreach v in `indepvars' {
    di "RUN: logistic `depvar' `v'"
    quietly logistic `depvar' `v'

    * check if variable is not omitted (constant and iv) *
    if `e(rank)' < 2 {
        di "OMITTED (rank < 2): `v'"
        continue
    }

    * check if chi-squared is > 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)')"
}

* ... now sort each variable (from varlist vars) by chi2 (from varlist chis) ... * 

如何通过最后一行返回的卡方对每个变量进行排序,然后显示变量列表,其卡方从最高卡方到最低卡方排序?

需要明确的是,如果以下 varlists 是由上述结果产生的:

local vars predvar1 predvar2 predvar3
local chis 2 3 1

然后我想得到类似以下的东西:

local ordered predvar2 3 predvar1 2 predvar3 1

或者,或者,

local varso predvar2 predvar1 predvar3
local chiso 3 2 1
4

1 回答 1

2

这是一种方法。

local depvar    binvar1
local indepvars predvar1 predvar2 predvar3

* expand and check collinearity *
_rmdcoll `depvar' `indepvars', expand
local indepvars "`r(varlist)'"

* first order individual variables by best chi-squared *

gen chisq = . 
gen vars = "" 
local i = 1 

foreach v in `indepvars' {
     di "RUN: logistic `depvar' `v'"
     quietly logistic `depvar' `v'

     * check if variable is not omitted (constant and iv) *
     if `e(rank)' < 2 {
          di "OMITTED (rank < 2): `v'"
     }

     * check if chi-squared is > 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 
于 2013-04-20T00:01:57.923 回答