오류
# Error: `data` and `reference` should be factors with the same levels.
발생
분류 분석 이후 caret::confusionMatrix 를 이용하여 성능을 평가하려고 시도하다가 발생
재현
library(caret)
mtcars.glm <- glm(formula=am ~ .,data=mtcars,family = binomial)
mtcars.glm.step <- step(mtcars.glm,direction = "both")
pred <- predict(mtcars.glm.step,mtcars,type = "response")
pred.f <- factor(ifelse(pred>0.5,1,0))
confusionMatrix(pred.f,mtcars$am)
원인
mtcars$am 이 factor 가 아님
해결
confusionMatrix 의 인수에 mtcars$am 대신 as.factor(mtcars$am) 또는 factor(mtcars$am)를 사용
mtcars.glm <- glm(formula=am ~ .,data=mtcars,family = binomial)
mtcars.glm.step <- step(mtcars.glm,direction = "both")
pred <- predict(mtcars.glm.step,mtcars,type = "response")
confusionMatrix(pred,mtcars$am)
pred.f <- factor(ifelse(pred>0.5,1,0))
confusionMatrix(pred.f,as.factor(mtcars$am))
library(caret)
library(dplyr)
library(car)
Duncan.rf <- train(type~., Duncan,
method = "rf",
tuneLength = 10,
trControl = trainControl(method = "cv"))
Duncan.pred <- predict(Duncan.rf,Duncan)
# Error: `data` and `reference` should be factors with the same levels.
confusionMatrix(Duncan.pred,Duncan)
# 형식을 맞춰주니 해결됨
confusionMatrix(Duncan.pred,Duncan$type)
'ADP (R)' 카테고리의 다른 글
[R] use column names for `x` 오류 해결 (0) | 2022.11.15 |
---|---|
[R] length of 'dimnames' [2] not equal to array extent (0) | 2022.10.30 |
[R] factor() vs as.factor() (0) | 2022.10.28 |
[Adp 실기 기출 풀이] 25회 평균 계산 문제 (0) | 2022.10.27 |
ADP 실기 기출 풀이 모음 (~26회) (1) | 2022.10.27 |