我正在尝试构建一个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)
任何想法都欢迎。
编辑:当我使用@laterow建议的代码glm(y〜。,data = newdata)时,它给出了一条错误声明:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]:
contrasts can be applied only to factors with 2 or more levels
如果您的预测变量是data.frame中所有其余变量,请使用以下内容进行选择:
x <- names(newdata)[-which(names(newdata) == "response.variable")]
该names()
函数返回data.frame中所有变量的名称。超级有用which()
函数返回向量中与某些条件表达式匹配的元素的索引。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句