できれば混乱行列()関数を使用して混同行列を作成しようとしていますが、次のエラーが発生します: 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)
混乱マトリックス()を使用して混乱マトリックスを作成する際の助けをいただければ幸いです。
ありがとう