简体   繁体   English

如何从 stepAIC 获取所有变量

[英]how to obtain all variable from stepAIC

I would like to keep all the coefficient of stepAIC.我想保留 stepAIC 的所有系数。 Set 0 for the omitted variable and display it same as coef(glm.model)为省略的变量设置 0 并显示它与 coef(glm.model) 相同

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

Originally I have 30 variables, I would like to display all it out from stepAIC and set the value to 0 if it was omitted from stepwise最初我有 30 个变量,我想从 stepAIC 中显示所有变量,如果从 stepAIC 中省略,则将值设置为 0

You can try this below, i use an example dataset from MASS.您可以在下面尝试这个,我使用 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM