--- title: 'Homework #3: Support Vector Machines and Tree-based methods' output: html_document --- ## Maximum margin classifiers 1. Run the following code to create a simple dataset. ```{r simulation} X1 <- c(3,1,3,1,2,4,4) X2 <- c(4,2,3,4,1,3,1) Y <- as.factor(c(rep("Red",4),rep("Blue",3))) dat <- data.frame(X1,X2,Y) print(dat) ``` 2. Plot the data. Color-code the points by their outcome value `Y`. ```{r} ``` 3. (3 points) Fit a maximal margin classifier with a linear decision boundary to this dataset using the `svm` function from `e1071`. Use the option `scale=FALSE` so that the model does not do post-processing of the training data. Plot the fitted model. ```{r} ``` 4. (1 point) Use the `coef` function to extract model coefficients from the maximum margin classifier. ```{r} ``` 5. (2 points) Suppose we are going to add another observation to the dataset and refit the maximum margin classifier. Give a new datapoint that, when added to this dataset, will not change the decision boundary. Refit the model to double check. Call this new datapoint `point1`. ```{r} ``` 6. (2 points) Now give a new datapoint that, when added to dataset `dat`, WILL change the decision boundary. Call this new datapoint `point2`. Pick an example point where the new dataset remains linearly separable. ```{r} ``` ## Support vector machines 7. Read in the dementia data "dementia2.csv" into a data frame called `dementia_dat`. This data is from the UCSF Memory and Aging Center. The goal was to predict the type of dementia based on patterns of brain loss as measured through structural MRI. ```{r} ``` 8. (1 point) We will now predict the dementia diagnosis based on the available predictors. The diagnosis is given in the multiclass outcome of `MacCohort_kr`. To start, generate a table of the elements in the `MacCohort_kr` variable. ```{r} ``` 9. (1 point) Set the random seed to 7 and then split the data into 2 sets (440 train, and 220 test) ```{r} ``` 10. (3 points) Fit a support vector machine to predict `MacCohort_kr` using predictors `Left_MTG_middle_temporal_gyrus`, `Left_Amygdala` and `Left_AIns_anterior_insula`, with a radial basis kernel. Use 3-fold CV to tune the value of the `C` and `sigma` hyperparameters. Use the `caret` package with `method=svmRadial` and tune over the values `C = 1e-2,1e-1,1,10,100,1000,10000` and `sigma=1e-4,1e-3,1e-2,1e-1, 1,10`. (You may need the `kernlab` package to run this.) ```{r} ``` 11. (1 points) Determine the predictions of the SVM fit on the test dataset. What is the test accuracy? ```{r} ``` 12. (3 points) What is the accuracy of a model that randomly guesses the class label simply based on the fraction of observations in each class? Is the SVM doing better than random guessing? ```{r} ``` ## Classification and Regression Trees 13. Read in the breast cancer imaging data "ispy1doctored.csv" into a data frame called `dat`. The iSPY1 trial was a prospective study to test if we could use MRIs to predict response to treatment and risk-of-recurrence in patients with stage 2 or 3 breast cancer receiving neoadjuvant chemotherapy (NACT). ```{r} ``` 14. Load the `rpart` package. Set the random seed to 10. ```{r} ``` 15. (1 point) Generate a histogram of the `MRI_LD_Tfinal` variable that will be our outcome to predict. ```{r} ``` 16. (1 point) Split the dataset into a training set of size 70 and a testset consisting of the remaining data. ```{r} ``` 17. (3 points) Fit a regression tree to the training data with `MRI_LD_Tfinal` as outcome and all other variables as candidate predictors. Make sure that you specify the correct method for regression. Plot the fitted tree with text labels. ```{r} ``` 18. (1 point) How many partitions does the fitted tree have? ```{r} ``` 19. (2 points) Determine the mean squared error (MSE) of the fitted tree based on the test data. ```{r} ``` ## Bagging 20. Load the `randomForest` package and set the random seed to 4 ```{r} ``` 21. (2 points) Perform bagging with `MRI_LD_Tfinal` as outcome and all other variables as candidate predictors. ```{r} ``` 22. (1 point) Determine the mean squared error (MSE) of the bagging based on the test data. ```{r} ``` ## Random Forests 23. (2 points) Set the seed to 4 again and perform random forests with 6 candidate predictors per tree, `MRI_LD_Tfinal` as outcome and all other variables as candidate predictors. ```{r} ``` 24. (1 point) Determine the mean squared error (MSE) of the random forest based on the test data. ```{r} ``` 25. (2 points) Plot the variable importance from the random forest. Use the importance measured in terms of the mean increase in mean squared error. Based on this importance measure, which variable is most important? Which variable is least important? ```{r} ```