我认为以下是编程练习,而不是统计学上的做法。
基本上,我希望使用一个预测变量运行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) ... *
我如何通过最后一行中返回的卡方对每个变量进行排序,然后显示变量列表,其中的卡方从最高卡方到最小卡方排序?
要明确的是,如果以上变更列表产生以下内容:
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
答案 0 :(得分: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