2016-12-04 76 views
0

我正在運行一個邏輯迴歸,包含755個觀察值和16個變量。我正在使用glm函數進行變量選擇。 glm已經找到了8個變量的最佳模型。我希望這些變量被迫留在中,並使用glm和step來查找下一個最佳的9變量模型(見下文)。我想這樣做,直到我完成了9-16變量模型 (全部選定的16個變量)的前向選擇。R正向選擇強制變量保留在方程

我的代碼看起來像

飽和模型

full=glm(PREVAP ~ SEX + TOTCHOL + AGE + SYSBP + DIABP + as.factor(CURSMOKE)  
+ CIGPDAY + BMI + as.factor(DIABETES) + as.factor(BPMEDS) + HEARTRTE + 
    GLUCOSE + as.factor(EDUC) + TIME + HDLC + LDLC, data=training, 
    family=binomial(link="logit")) 
summary(full) 
anova(full,test="Chisq") 
full.forward <- step(null,  
scope=list(lower=null,upper=full),direction="forward", 
     family=binomial(link="logit")) 

這給我的8個因子模型 我需要強制這些因素在接下來的模型,並使用正向選擇找到的9個因子的模型。怎麼做?

我被告知bestglm和glmnet也允許這個,但我不知道這些包。

你能幫忙嗎?這些軟件包有很多選項。

真誠, 瑪麗A.馬里昂

回答

0

您可以通過塞汀的step功能objectscope參數做到這一點。

在這裏,用糖尿病皮馬印第安人婦女的數據集:

library(MASS) 
data = rbind(Pima.te, Pima.tr) 
data$type = ifelse(data$type == "Yes", 1, 0) 

full = glm(type~., family = "binomial", data = data) 

summary(full) 
anova(full, test = "Chisq") 

nothing = glm(type~1, data = data, family = "binomial") 

full.forward = step(nothing, 
        scope = list(lower = formula(nothing), 
           upper = formula(full)), 
        direction = "forward") 

forward = step(full.forward, 
       scope = list(lower = formula(full.forward), 
          upper = formula(full)), 
       direction = "forward") 
+0

是否有可能像做 GLM(full.forward,數據=訓練,家族=二項式(鏈接= 「Logit模型」))? 我收到一個錯誤。 eval(expr,envir,enclos)中的錯誤:找不到對象'type'我有沒有保留family = binomial logit鏈接的問題。 –

+0

嘗試'glm(formula(full.forward),data = training,family = binomial(link =「logit」))'' –