This is a continuation of the exercise Basic Tree 1
Answers to the exercises are available here.
If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.
load the tree library. If it is not installed then use the install.packages() command to install it.
Convert all the feaures(columns) into factors, including the class column
Use the sample methods that you learnt from the sample_exercise to split the data into two sets with a SplitRatio of 0.7. Hint: Use caTools library and sample.split() function. Store the results into Train and Test.
tree() command to build the model. Use class as the target variable and everything else as the predictor variable. Also, use the Train variable as the data source. Store the model in a variable called model1
plot() command to plot the model and use the text() command to add the text.
predict() command to predict the classes using the Test dataset. We want to predict the classes. Store this in the variable pred_class
table() command to print the confusion matrix. Hint: You are comparing the class from the Test set and the predicted vector. This tells you wether the model is answering anything right or wrong
summary() to print the summary of the model and note the misclassification error rate.
Now find the misclassification error rate of the model on the Test data. Use the formula. mean(Test$class != pred_class)
Compare the two misclassification error rates and determine which is worse and why. How can we improve the model?