通过阅读类似的问题,我知道问题在于yhat.logisticReg
不是两个水平的因子,而training.prepped$TARGET_FLAG
是两个水平的因子。我认为可以通过修改模型或预测,使yhat.logisticReg
成为两个水平的因子来解决这个问题。我该如何操作呢?
logisticReg = glm(TARGET_FLAG ~ ., data = training.prepped, family = binomial())yhat.logisticReg = predict(logisticReg, training.prepped, type = "response")confusionMatrix(yhat.logisticReg, training.prepped$TARGET_FLAG)Error: `data`和`reference`应为具有相同水平的因子。
str(training.prepped$TARGET_FLAG)Factor w/ 2 levels "0","1": 1 1 1 1 1 2 1 2 2 1 ...str(yhat.logisticReg) Named num [1:8161] 0.1656 0.2792 0.3717 0.0894 0.272 ... - attr(*, "names")= chr [1:8161] "1" "2" "3" "4" ...
回答:
您可能需要先选择一个阈值,然后将您的实值数据转换为二元值,例如:
a <- c(0.2, 0.7, 0.4)threshold <- 0.5binary_a <- factor(as.numeric(a>threshold))str(binary_a)Factor w/ 2 levels "0","1": 1 2 1