R evaluation of linear model: Error in table: all arguments must have the same length, how to solve?

Viewed 33

I am new to R, and I am using the biopsy data set included with R, but I am encountering an error that says

Error in table(training$Direct.Deposit, predict.status) : all arguments must have the same length.

The issue arises when I run this line :

ct=table(eval$class, predict.status)

I tried troubleshooting but could not figure out the solution to this error message; any help will be appreciated! Here is my code:

library(MASS)
data("biopsy")

summary(biopsy)

#the thinnest clump is 1, while the largest is 10. The average clump thickness is 4.418.

sum(biopsy$class == "benign", na.rm=TRUE)
# 458 tumors are benign
458/699
#65.55% of the tumors are benign

sum(biopsy$class == "malignant", na.rm=TRUE)
# 241 tumors are malignant
241/699
#34.48% of the tumors are malignant



biopsy60train <- floor(0.6 * nrow(biopsy))
#419 observations
biopsy40eval <- floor(0.4 * nrow(biopsy))
#279 observations

training<-(sample(nrow(biopsy), nrow(biopsy)*.6))
training<-biopsy[training,]
#Training dataset 60%


eval<-(sample(nrow(biopsy), nrow(biopsy)*.4))
eval<-biopsy[eval,]
#eval dataset 40%


training$class<-ifelse(training$class=="benign",1,0)
training
#Convert class to binary (1,0)

#Fit logistic regression
training[1,]
m1=glm(class~V1+V2+V3+V4+V5+V6+V7+V8+V9, data=training, family="binomial")
summary(m1)

#p value of #9.14e-09 is small so we Reject the null hypothesis(ho), the relationship is significant.


eval$class<-ifelse(eval$class=="benign",1,0)
eval
#Convert class to binary (1,0)

#Fit logistic regression
eval[1,]
m2=glm(class~V1+V2+V3+V4+V5+V6+V7+V8+V9, data=eval, family="binomial")
summary(m2)

# Conclusion ---------------------------------------------------
# V2,V5, V7, V8, V9  are not significant, therefore can be removed from the analysis
 
m3=glm(class~V1+V3+V4+V6, data=eval, family="binomial")
summary(m3)
# Conclusion ---------------------------------------------------
# All independent variables are significant


# Next, we want to evaluate the model. To do that we should calculate 
# the TRUE POSITIVE RATE and the FALSE POSITIVE RATE

predict.prob=predict.glm(m3,type="response")
predict.status=1*(predict.prob>0.5)

ct=table(eval$class, predict.status)

TPR=ct[2,2]/sum(ct[2,])
FPR=ct[1,2]/(sum(ct[1,]))

1 Answers

The problem is caused by different lengths of eval$class and predict.status. These should in theory be of the same length, but when you have NAs in your data glm will by default omit such cases and the prediction from the model is thus shorter than the data you feed it.

Indices of the omitted cases are stored in m3$na.action which you can use to leave out these cases also from the eval$class vector:

table(eval$class[-m3$na.action], predict.status)
Related