简体   繁体   中英

ConfusionMatrix on Logistic Regression model in R

I'm trying to calculate confusion matrix on my logistic regression model but getting an error

Error: data and reference should be factors with the same levels. In addition: Warning message: In is.factor(reference): NAs introduced by coercion

Code:

usage_model_rl <- glm(data = train_usage_rl, formula = Subscription ~., family = binomial(link = "logit"))
usage_predictions <- predict(usage_model_rl, test_usage_rl, type = "response") %>%
  bind_cols(test_usage_rl %>% dplyr::select(Subscription), preds = .)


cut04 <- usage_predictions %>%mutate(predicted = ifelse(preds >= 0.4, 1, 0)) %>% dplyr::select(-preds) %>% dplyr::select(predicted, Subscription) %>%mutate_all(list(~ factor(., levels = c(1, 0)))) %>%table()

confusionMatrix(usage_predictions,as.numeric(test_usage$Subscription),positive==1)

Regards

Try this:

confusionMatrix(usage_predictions,test_usage$Subscription,positive="1", dnn=c("Prediction", "Reference"))

Make sure they're all factors.

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