--- title: 'Lab #5: Regression Trees and Random Forests' output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) set.seed(24) ``` We will now analyze the dementia dataset using tree-based methods. ## Classification Trees 1. Load the `rpart` package ```{r} ``` 2. Read in the dementia data "dementia2.csv" into a data frame called `dat`. ```{r} ``` 3. Generate a table of the elements in the `Dementia` variable ```{r} ``` 4. Fit a classification tree with `Dementia` as response and all other variables as predictors. Then run the `summary` command on the fitted tree. (Note that the `variable.importance` values are calculated only for the variables that were considered for surrogate or primary splits.) ```{r} ``` 5. Plot the tree and add text indicating the splits. ```{r} ``` 6. Install the `rpart.plot` package and load it. Plot the tree again using the `rpart.plot` function. Play with the `type` option and select the tree visualization that you prefer most. ```{r} ``` 7. Select 200 rows at random to be the training set and call it `train` ```{r} ``` 8. Build a classification tree on the training data where a split is attempted when the minimum number of observations is `minsplit = 10`. Call this tree `treeTrain`. Plot this tree and compare it to the one trained on all the data. How different are the two trees? ```{r} ``` 9. Predict the classes of the test data based on the fitted tree to the training data ```{r} ``` 10. Generate a table of the predicted vs. true values in the test data and calculate the classification accuracy ```{r} ``` 11. Let's now use cross-validation results to prune the tree. The CV results from `rpart` are stored in the `cp` table. The `error` column corresponds to the training error, relative to the root node error. The `xerror` column corresponds to the (relative) CV error. Use the `printcp` command to display results and `plotcp` to visualize. ```{r} ``` 12. Prune the tree to a good value of $k$ that minimizes the `xerror` using the `prune` command. Plot the resulting tree. (The CP table can be accessed through the `cptable` attribute.) ```{r} ``` 13. Generate the confusion matrix of the pruned tree for the test data. What is the test accuracy of the model? ```{r} ``` 14. Compare the classification accuracy of the pruned tree to the unpruned tree. Did pruning help? ```{r} ``` ## Random Forests 15. Install the `ranger` package ```{r} ``` 16. Load the `ranger` package ```{r} ``` 17. Fit a random forest to the training data using the `ranger` command. The out of bag (OOB) error estimate are a clever way that random forests gets honest error estimates (i.e. not over-fitted). What is the OOB error for the RF? ```{r} ``` 18. Use your fitted RF to make predictions on the test dataset. What is the test error? How does it compare to OOB error? ```{r} ``` 19. Let's try various values of `mtry` ranging from 1 to 50 in a loop. Record the accuracy results and make a plot. ```{r} ``` 20. Make a plot of error rates in OOB vs. test set across values of `mtry` ```{r} ``` 21. How does the OOB error compare to the test error? Would you prefer to rely on OOB or test set error? ```{r} ``` 22. What do you think is a good choice for `mtry`? ```{r} ```