简体   繁体   中英

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

I am attempting to execute a logistic regression in Rstudio using a loaded CSV file that is converted to a dataframe. I have one dependent variable ( result ) and 9 independent variables, which are all in 10 columns in the dataframe.

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'

Is anyone able to help with why this error is occurring even though all of the variables being used in my model are numeric? Thanks!

You can't compare model1 with 0.5

This is model1 structure:

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 

You have to pass new data to the model and than compare the prediction (using function predict ) value with 0.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM