如何创建一个将新变量添加到预定义glm模型的循环 [英] How to create a loop that will add new variables to a pre define glm model
问题描述
我想创建一个过程,该过程将为每个循环将新变量(来自变量池)添加到glm模型中,该模型已经准备好包含需要成为最终模型一部分的变量了.我希望将循环的结果包含在包含glm公式和结果的列表中.我知道如何手动执行(下面编写了代码),但很高兴知道如何自动执行. 这是一个玩具数据集和用于手动执行任务的相关代码:
I would like to create a procedure that will add per each loop a new variable (from a pool of variables) to a glm model that allready contains few of the variables that need to be part of the final model.I than would like to have the results of the loop in a list that will contain the glm formula and results.I know how to do it manually (code is written below) but I would be happy to know how to do it automaticaly. Here is a toy dataset and the relevant code to do the task manually:
dat <- read.table(text = "target birds wolfs Country
0 21 7 a
0 8 4 b
1 2 5 c
1 2 4 a
0 8 3 a
1 1 12 a
1 7 10 b
1 1 9 c",header = TRUE)
#birds is a mandatory variable so I'll need to add one of the other variables in addition to birds
glm<-glm(target~birds,data=dat)
dat$glm_predict_response <- ifelse(predict(glm,newdata=dat, type="response")>.5, 1, 0)
xtabs(~target + glm_predict_response, data = dat)
glm_predict_response
target 0 1
0 1 2
1 0 5
glm_predict_response
prop.table(xtabs(~target + glm_predict_response, data = dat), 2)
target 0 1
0 1.0000000 0.2857143
1 0.0000000 0.7142857
#manually I would add the next variable (wolfs) to the model and look at the results:
glm<-glm(target~birds+wolfs,data=dat)
dat$glm_predict_response <- ifelse(predict(glm,newdata=dat, type="response")>.5, 1, 0)
xtabs(~target + glm_predict_response, data = dat)
glm_predict_response
target 0 1
0 3 0
1 0 5
prop.table(xtabs(~target + glm_predict_response, data = dat), 2)
glm_predict_response
target 0 1
0 1 0
1 0 1
在下一个循环中,我将添加变量国家/地区"并执行相同的过程,在现实生活中,我有数百个变量,因此将其转换为自动过程会很棒.
In the next loop I would add the variable "country" and do the same procedure, In the real life I have hundreds of variables so turning it to an automatic proccess would be great.
推荐答案
我将使用update
每次在循环中更新公式来做到这一点:
I would do it using update
to update the formula each time in the loop:
#initiate formula
myform <- target~1
for ( i in c('birds', 'wolfs' , 'Country')) {
#update formula each time in the loop with the above variables
#this line below is practically the only thing I changed
myform <- update(myform, as.formula(paste('~ . +', i)))
glm<-glm(myform,data=dat)
dat$glm_predict_response <- ifelse(predict(glm,newdata=dat, type="response")>.5, 1, 0)
print(myform)
print(xtabs(~ target + glm_predict_response, data = dat))
print(prop.table(xtabs(~target + glm_predict_response, data = dat), 2))
}
输出:
target ~ birds
glm_predict_response
target 0 1
0 1 2
1 0 5
glm_predict_response
target 0 1
0 1.0000000 0.2857143
1 0.0000000 0.7142857
target ~ birds + wolfs
glm_predict_response
target 0 1
0 3 0
1 0 5
glm_predict_response
target 0 1
0 1 0
1 0 1
target ~ birds + wolfs + Country
glm_predict_response
target 0 1
0 3 0
1 0 5
glm_predict_response
target 0 1
0 1 0
1 0 1
这篇关于如何创建一个将新变量添加到预定义glm模型的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!