如何从stepAIC获取所有变量

时间:2019-11-04 08:22:04

标签: r glm

我想保留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中显示所有变量,并将其值设置为0(如果逐步将其省略)

1 个答案:

答案 0 :(得分: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