Ошибка при использовании mean() в R: Ошибка в Ops.factor(obs, pred): наборы факторов разного уровня

#r #mean

#r #среднее

Вопрос:

Я работаю с набором данных, который выглядит следующим образом:

 > head(test_accuracy)
        observed predicted new_predicted
1 Moving/Feeding  Foraging      Standing
2       Standing  Foraging      Standing
3       Standing  Foraging      Standing
4       Standing  Foraging      Standing
5       Standing  Foraging      Standing
6       Standing  Foraging      Standing
  

Мой вопрос прост. Я хотел бы рассчитать показатель точности классификации путем сравнения процента совпадений между test_accuracy$observed и test_accuracy$new_predicted . Я просто использую приведенный ниже код, но получаю соответствующую ошибку:

 > head(test_accuracy)
        observed predicted new_predicted
1 Moving/Feeding  Foraging      Standing
2       Standing  Foraging      Standing
3       Standing  Foraging      Standing
4       Standing  Foraging      Standing
5       Standing  Foraging      Standing
6       Standing  Foraging      Standing
> obs<-as.factor(test_accuracy$observed)
> pred<-as.factor(test_accuracy$new_predicted)
> mean(obs == pred)
Error in Ops.factor(obs, pred) : level sets of factors are different
  

Не мог бы кто-нибудь, пожалуйста, сообщить мне, что я делаю не так? Я могу загрузить dput() пример ниже:

 > dput(test_accuracy)
structure(list(observed = c("Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Feeding/Moving", "Standing", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Standing", "Standing", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Moving/Feeding", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Feeding/Moving", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Standing", "Feeding/Moving", 
"Feeding/Moving", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Moving/Feeding", 
"Moving/Feeding", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Standing", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Moving/Feeding", "Moving/Feeding", 
"Feeding/Moving", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Standing", "Moving/Feeding", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Feeding/Moving", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Feeding/Moving", "Feeding/Moving", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving"), predicted = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 1L, 2L, 2L, 3L, 3L, 3L, 
1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
3L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 
3L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 
2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 1L, 3L, 1L, 1L, 3L, 1L, 2L, 2L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
3L, 2L, 3L, 1L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 1L, 
2L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 2L, 2L, 3L, 3L, 
2L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 3L, 1L, 
1L, 1L, 1L, 1L, 3L, 1L), .Label = c("Feeding", "Foraging", "Standing"
), class = "factor"), new_predicted = c("Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Moving/Feeding", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Standing")), class = "data.frame", row.names = c(NA, -215L))
  

Приветствуется любой ввод!

Комментарии:

1. К чему обращаться factor ? observed имеет 3, а new_predicted имеет два, следовательно, ваша ошибка.

Ответ №1:

Как насчет того, чтобы просто выполнить следующее без преобразования в factors,

 mean(test_accuracy$observed == test_accuracy$new_predicted)
# 0.5069767