Я пытаюсь создать матрицу путаницы, предпочтительно используя функцию confusionMatrix(), однако я получаю эту ошибку: Ошибка в sort.list (y): 'x' должен быть атомарным для 'sort.list' Вы назвали «сортировку» в списке?R confusionMatrix() & Table() дает ошибку в sort.list (y): 'x' должен быть атомарным для 'sort.list' Вы вызвали сортировку в списке?
Я также попытался использовать функцию table(), но получил ту же ошибку.
Ниже весь мой код:
#install load libraries
install.packages('MASS')
install.packages('tree')
install.packages("e1071")
install.packages("caret")
library('MASS')
library('tree')
library('e1071')
library('caret')
set.seed(1985)
#GET DATA
training <- read.csv("C:/Users/anaim/data_minig_project/pml-training.csv",header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
training_df <- data.frame(training,stringsAsFactors=FALSE)
nrow(training_df)
ncol(training_df)
#create train & test set splits
inTrain <- createDataPartition(y=training_df$classe, p=0.75, list=FALSE)
training_data <- training_df[inTrain,]
testing_data<- training_df[-inTrain,]
#FEATURE SELECTION & DATA CLEANING
#one can see numbers of features is quite large with 160 columns, therefore we will refer to the studies such as paper #1 to start and reduce the number of features
#subset based on features mentioned studies
training_data_subset <- subset(training_data, select=c("avg_roll_belt","var_roll_belt","var_total_accel_belt","amplitude_roll_belt","max_roll_belt","var_roll_belt",
"var_accel_arm","magnet_arm_x","magnet_arm_y","magnet_arm_z","accel_dumbbell_y","accel_dumbbell_z","magnet_dumbbell_x","gyros_dumbbell_x",
"gyros_dumbbell_y","gyros_dumbbell_z","pitch_forearm","gyros_forearm_x","gyros_forearm_y","classe"))
#subset based on features mentioned studies
testing_data_subset <- subset(testing_data, select=c("avg_roll_belt","var_roll_belt","var_total_accel_belt","amplitude_roll_belt","max_roll_belt","var_roll_belt",
"var_accel_arm","magnet_arm_x","magnet_arm_y","magnet_arm_z","accel_dumbbell_y","accel_dumbbell_z","magnet_dumbbell_x","gyros_dumbbell_x",
"gyros_dumbbell_y","gyros_dumbbell_z","pitch_forearm","gyros_forearm_x","gyros_forearm_y","classe"))
#all NAs to 0
testing_data_subset[is.na(testing_data_subset)] <- 0
training_data_subset[is.na(training_data_subset)] <- 0
#load library(e1071) before using skewness()
#load library(e1071) befortraining_datae using skewness()
#investigate skewness
# Interpretation of skewness - http://www.tc3.edu/instruct/sbrown/stat/shape.htm#SkewnessCompute
skewness_result <- apply(training_data_subset[, sapply(training_data_subset, is.numeric)], 2, skewness)
skewness_df <- data.frame(skewness_result)
#remove highly skewed columns
remove <- c("var_roll_belt","var_total_accel_belt","amplitude_roll_belt","var_roll_belt","var_roll_belt.1","magnet_dumbbell_x")
training_data_subset <- training_data_subset[, !(colnames(training_data_subset) %in% remove), drop=FALSE]
testing_data_subset <- testing_data_subset[, !(colnames(testing_data_subset) %in% remove), drop=FALSE]
#valid columns were removed
ncol(training_data_subset)
ncol(testing_data_subset)
#BUILD MODEL
#1)decision tree
exercise.model <- tree(formula = classe ~ ., data = training_data_subset)
summary(exercise.model)
plot(exercise.model)
text(exercise.model ,pretty =0)
#MODEL EVALUATION
exercise.prediction <- predict(exercise.model,newdata = testing_data_subset, type="tree")
**#THIS IS WERE I GET THE ERROR**
confusionMatrix(exercise.prediction,testing_data_subset[['classe']])
confusionMatrix(exercise.prediction,testing_data_subset$classe)
**# I also tried table() just to get raw True (positive + True Negatives/Total) values but I got the same error**
table(exercise.prediction, testing_data_subset[['classe']])
table(exercise.prediction,testing_data_subset$classe)
Любая помощь в создании матрицы неточностей с помощью confusionMatrix() будет оценен.
Благодаря
Что делает 'str (testing_data_subset)' return? – bjoseph
@bjoseph 'str (testing_data_subset)' возвращает: ''data.frame': \t 4904 obs. из 15 переменных: $ avg_roll_belt: num 0 0 0 0 0 0 0 0 0 0 ... $ max_roll_belt: num 0 0 0 0 0 0 0 0 0 0 ... $ var_accel_arm: num 0 0 0 0 0 0 0 0 0 0 ... $ magnet_arm_x: int -371 -367 -375 -372 -373 -370 -375 -363 -375 -370 ... $ magnet_arm_y: int 331 340 337 338 333 340 339 342 339 332 ... $ magnet_arm_z: int 523 509 513 510 509 512 508 515 510 506 ... Etc .. Выход слишком длинный, чтобы опубликовать на Stackoverflow то, что вы ищете в частности? – cyber101
класс 'testing_data_subset $ classe' – bjoseph