rstudio – R Studio glm function “Error in xy.coords(x, y) : ‘x’ and ‘y’ lengths differ”

I’m writing a code in R Studio for statistical analysis of a project and I’ve run into an issue when trying to plot an ROC curve. The code that I ran is as follows:

glm.fit=glm(unlist(final_data$CDIV) ~ final_data$Combined_mFI5, family = binomial)
lines(Combined_mFI5, glm.fit$fitted.values)

roc_(unlist(final_data$CDIV), glm.fit$fitted.values, plot = T, legacy.axes = T, col = “#377eb8”, lwd = 4)

The error message that appears after I run the lines function is as follows:

Error in xy.coords(x, y) : ‘x’ and ‘y’ lengths differ

**When I view glm.fit, under fitted values it shows: double [1187]
However, my dataset should have a lenghth of 1188.

Furthermore, when I run the roc_ function line of code I get the following error: **

Error in if (!response %in% colnames(data)) { :
the condition has length > 1

I’m not quite sure how to resolve this issue. There are no NA’s in any of the variables. A similar question was asked previously, but I tried the remedy and it didn’t seem to help my case. Any help would be greatly appreciated as I am on a time crunch! Thanks in advance!

error

code

glm.fit

Originally in the code, I did not have the unlist function there and I got the same error. When I checked the lenghth of final_data$CDIV it returned a length of 1. After I added the unlist function, it returned the proper length of 1188. So, before running the lines of code, unlist(final_data$CDIV) and final_data$Combined_mFI5 both had the same length of 1188. This is why I am confused by this error.

Read more here: Source link