【问题标题】:Cognitive logistic regression modelling in R [duplicate]R中的认知逻辑回归建模
【发布时间】:2016-04-11 22:23:46
【问题描述】:

我正在尝试构建一个逻辑回归模型,只要响应变量保持不变,该模型就能够运行而不管预测变量的性质如何。这是我的想法的一个示例:

#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

【问题讨论】:

  • glm(y ~ ., data = newdata)

标签: r data-modeling logistic-regression


【解决方案1】:

如果您的预测变量是 data.frame 中的所有剩余变量,请使用以下命令选择它们:

x <- names(newdata)[-which(names(newdata) == "response.variable")]

names() 函数返回 data.frame 中所有变量的名称。超级有用的which() 函数返回向量中匹配某个条件表达式的元素的索引。

【讨论】:

  • 谢谢@Imo。这对我的想法非常有效。
猜你喜欢
  • 2021-07-01
  • 2021-04-28
  • 2018-01-26
  • 1970-01-01
  • 2016-12-11
  • 2018-01-26
  • 2018-02-12
  • 2014-06-20
  • 2021-11-13
相关资源
最近更新 更多