R 中的逻辑回归错误:无法强制 'list' 对象键入 'double'

Error in logistic regression in R: 'list' object cannot be coerced to type 'double'

我正在尝试使用转换为数据帧的已加载 CSV 文件在 Rstudio 中执行逻辑回归。我有一个因变量(result)和9个自变量,它们都在数据框中的10 columns中。

sapply(mydata, mode)

> result cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 cat9
> "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric"

sapply(mydata, class)

> result cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 cat9  
> "numeric" "integer" "integer" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "factor"

model1 <- glm(formula = result ~ cat3 + cat4 + cat5 + cat6 + cat7 + cat8, 
              data = mydata, 
              family = "binomial")

model1.pred <- ifelse(model1 > 0.5, "Win", "Loss")

> Error in ifelse(win2 > 0.5, "Win", "Loss") : 
> 'list' object cannot be coerced to type 'double'

即使我的模型中使用的所有变量都是数字,是否有人能够帮助解释为什么会出现此错误? 谢谢!

你无法将 model1 与 0.5

进行比较

这是 model1 结构:

model1

Call:  glm(formula = id ~ speed + dist, family = "binomial", data = cars)

Coefficients:
(Intercept)        speed         dist  
  -1158.863       73.588        1.366  

Degrees of Freedom: 49 Total (i.e. Null);  47 Residual
Null Deviance:      69.31 
Residual Deviance: 2.932e-08    AIC: 6 

您必须将新数据传递给模型,然后将预测值(使用函数 predict)与 0.5

进行比较