R混淆矩阵()&;Table()给出sort.list(y)中的错误:';x';必须是'的原子;排序列表';你打过电话吗;排序';在名单上?

R混淆矩阵()&;Table()给出sort.list(y)中的错误:';x';必须是'的原子;排序列表';你打过电话吗;排序';在名单上?,r,confusion-matrix,R,Confusion Matrix,我试图创建一个混淆矩阵,最好是使用confusionMatrix()函数,但是我得到了这个错误:sort.list中的错误(y):'x'必须是'sort.list'的原子 您在列表中调用过“排序”吗? 我还尝试使用table()函数,但得到了相同的错误 下面是我的全部代码: #install load libraries install.packages('MASS') install.packages('tree') install.packages("e1071") install.pack

我试图创建一个混淆矩阵,最好是使用confusionMatrix()函数,但是我得到了这个错误: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)
#安装加载库
install.packages('MASS'))
install.packages('tree'))
安装软件包(“e1071”)
安装程序包(“插入符号”)
图书馆(“质量”)
库('树')
图书馆('e1071')
库('插入符号')
种子集(1985)
#获取数据
trainingtree()函数是R构建决策树的基本函数,但是confusionMatrix()是插入符号包的一部分,因此tree()的输出出于某种原因与confusionMatrix()不兼容。当我用


exercise.model
str(testing_data_subset)
返回什么?@bjoseph
str(testing_data_subset)
返回:“data.frame”:4904 obs。共15个变量:$avg_roll_belt:num 0…$最大滚压皮带:数量0…$变量加速臂:数值0…$磁铁臂:int-371-367-375-372-373-370-375-363-375-370美元磁铁臂:国际331 340 337 338 333 340 339 342 339 332美元磁铁臂:int 523 509 513 510 509 512 508 515 510 506。。。等输出太长,无法在Stackoverflow上发布您特别想要什么?测试数据的类$classe子集