Requires knowledge of statistics and code
#Installing libraries install.packages('rpart') install.packages('caret') install.packages('rpart.plot') install.packages('rattle') install.packages('readxl') #Loading libraries library(rpart,quietly = TRUE) library(caret,quietly = TRUE) library(rpart.plot,quietly = TRUE) library(rattle) library(readxl) #Reading the data set as a dataframe mushrooms <- read_excel("mushrooms.xlsx") # structure of the data str(mushrooms) # number of rows with missing values nrow(mushrooms) - sum(complete.cases(mushrooms)) # deleting redundant variable `veil.type` mushrooms$veil.type <- NULL #analyzing the odor variable table(mushrooms$class,mushrooms$odor) number.perfect.splits <- apply(X=mushrooms[-1], MARGIN = 2, FUN = function(col){ t <- table(mushrooms$class,col) sum(t == 0) }) # Descending order of perfect splits order <- order(number.perfect.splits,decreasing = TRUE) number.perfect.splits <- number.perfect.splits[order] # Plot graph par(mar=c(10,2,2,2)) barplot(number.perfect.splits, main="Number of perfect splits vs feature", xlab="",ylab="Feature",las=2,col="wheat") #data splicing set.seed(12345) train <- sample(1:nrow(mushrooms),size = ceiling(0.80*nrow(mushrooms)),replace = FALSE) # training set mushrooms_train <- mushrooms[train,] # test set mushrooms_test <- mushrooms[-train,] # penalty matrix penalty.matrix <- matrix(c(0,1,10,0), byrow=TRUE, nrow=2) # building the classification tree with rpart tree <- rpart(class~., data=mushrooms_train, parms = list(loss = penalty.matrix), method = "class") # Visualize the decision tree with rpart.plot rpart.plot(tree, nn=TRUE) # choosing the best complexity parameter "cp" to prune the tree cp.optim <- tree$cptable[which.min(tree$cptable[,"xerror"]),"CP"] # tree prunning using the best complexity parameter. For more in tree <- prune(tree, cp=cp.optim) #Testing the model pred <- predict(object=tree,mushrooms_test[-1],type="class") #Calculating accuracy t <- table(mushrooms_test$class,pred) confusionMatrix(t)