我认为以下是一种编程练习,而不是一种基于统计数据的做事方式。
基本上,我想用一个预测变量运行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发布于 2013-04-20 08:01:58
这里有一种方法可以做到。
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 https://stackoverflow.com/questions/16113800
复制相似问题