Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

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!

question from:https://stackoverflow.com/questions/65921530/error-in-logistic-regression-in-r-list-object-cannot-be-coerced-to-type-doub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...