R confusionMatrix()& Table()给出sort.list(y)中的错误:'x'必须是'sort.list'的原子'你有没有在列表上调用'sort'?

时间:2015-08-06 23:24:23

标签: r confusion-matrix

我正在尝试使用confusionMatrix()函数创建一个混淆矩阵,但是我收到了这个错误: sort.list(y)中的错误:'x'必须是'sort'的原子。清单” 你有没有在名单上打电话给“排序”?

我也试过使用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()创建混淆矩阵的任何帮助都将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

tree()函数是R用于构建决策树的基函数,但是confusionMatrix()是CARET包的一部分,因此tree()的输出由于某种原因与confusionMatrix()不兼容。当我用APIConfigValue替换tree()时,我得到了confusionMatrix()和table()函数。