在 postResample 预测中返回 NA

Getting NA returned on postResample Prediction

在尝试测试逻辑回归模型的预测准确性时得到此结果。好像不太对。感谢任何帮助!

> dput(head(test$subscribed))
structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("no", "yes"), class = 
"factor")

输入

predictions <- predict(final_model, test, type = "response")`
class_pred<- as.factor(ifelse(predictions > .5, "Yes", "No"))
postResample(class_pred, test$subscribed)

输出

 Accuracy    Kappa 
  NA       NA 

假设您的数据是这样的:

df = data.frame(subscribed=sample(c("yes","no"),100,replace=TRUE),
x1 = runif(100),x2=runif(100))

正确设置系数:

df$subscribed = factor(df$subscribed,levels=c("no","yes"))

做模型:

traindf = df[1:70,]
test = df[1:30,]
final_model = glm(subscribed ~ .,data=traindf,family="binomial")

并预测,并设置相同级别的因素,注意级别区分大小写,使用 "yes" 不同于 "Yes" :

predictions <- predict(final_model, test, type = "response")
class_pred<- ifelse(predictions > .5, "yes", "no")
class_pred = factor(class_pred,levels=c("no","yes"))

然后:

confusionMatrix(table(class_pred, test$subscribed))
Confusion Matrix and Statistics

          
class_pred no yes
       no   0   1
       yes 14  15