我正在尝试构建一个Logistic回归模型,只要响应变量保持不变,就可以运行而不管预测变量的性质如何。以下是我的想法:
#Take in the data
newdata = read.csv("book1.csv", head = TRUE)
#Store the response variable whose column heading you know beforehand
y = "response.variable"
#Identify the predictor variables (this is where I am stuck)
#Below is the algorithm of what I have in mind though
1) Take remaining column headings except "response.variable"
2) Store them as x = c(other headings)
#Form of model
model.form = reformulate(x, response = y)
#Build model
logit = glm(model.form, family = "binomial", data = newdata)
欢迎任何想法。
编辑: 当我使用代码 glm(y~。,data = newdata) 正如@laterow所建议的,它给出了一个错误陈述:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]:
contrasts can be applied only to factors with 2 or more levels
答案 0 :(得分:0)
如果预测变量是data.frame中的所有剩余变量,请使用以下选项:
x <- names(newdata)[-which(names(newdata) == "response.variable")]
names()
函数返回data.frame中所有变量的名称。超级有用的which()
函数返回向量中与某些条件表达式匹配的元素的索引。