如何从 stepAIC 获取所有变量

how to obtain all variable from stepAIC

我想保留stepAIC的所有系数。将省略的变量设置为 0 并显示为 coef(glm.model)

glm.model=suppressWarnings(glm(as.factor(diagnosis)~.,family = "binomial",data = dat))
step.model=stepAIC(glm.model,trace = FALSE,direction="both")

本来我有30个变量,我想把它全部从stepAIC中显示出来,如果在stepwise中省略了,就把值设置为0

您可以在下面尝试这个,我使用来自 MASS 的示例数据集。

library(MASS)
example(birthwt)
glm.model <- glm(low ~ ., family = binomial, data = bwt)
step.model <- stepAIC(glm.model, trace = FALSE)

# on a vector
# create an empty vector of zeros
STEP_COEF = vector("numeric",length(coefficients(glm.model)))
#same names
names(STEP_COEF) = names(coefficients(glm.model))
#fill in the ones found in step
STEP_COEF[names(coefficients(step.model))] = as.numeric(coefficients(step.model))

> STEP_COEF
(Intercept)         age         lwt   raceblack   raceother   smokeTRUE 
-0.12532604  0.00000000 -0.01591847  1.30085571  0.85441418  0.86658183 
    ptdTRUE      htTRUE      uiTRUE        ftv1       ftv2+ 
 1.12885661  1.86689526  0.75064880  0.00000000  0.00000000