# 6 SVMs and Unsupervised Learning

#### Datathon 1

The second datathon exercise is due next week. Click on the button below for details:

## 6.1 Seminar

You will need to load the package from the course textbook:

`library(ISLR)`

### 6.1.1 Exercise

This problem involves the `OJ`

dataset which is part of the `ISLR`

package.

- Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
- Fit a support vector classifier to the training data using
`cost=0.01`

, with`Purchase`

as the response and the other variables as predictors. Use the`summary()`

function to produce summary statistics, and describe the results obtained. - What are the training and test error rates?
- Use the
`tune()`

function to select an optimal cost. Consider values in the range 0.01 to 10. - Compute the training and test error rates using this new value for
`cost`

. - Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for
`gamma`

. - Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set
`degree=2`

. - Overall, which approach seems to give the best results on this data?

### 6.1.2 Exercise

In this problem, you will generate simulated data, and then perform PCA and K-means clustering on the data.

- Generate a simulated dataset with 20 observations in each of three classes (i.e. 60 observations total), and 50 variables.
*Hint: There are a number of functions in*`R`

that you can use to generate data. One example is the`rnorm()`

function;`runif()`

is another option. Be sure to add a mean shift to the observations in each class so that there are three distinct classes. - Perform PCA on the 60 observations and plot the first two principal component score vectors. Use a different color to indicate the observations in each of the three classes. If the three classes appear separated in this plot, then continue on to part (c). If not, then return to part (a) and modify the simulation so that there is greater separation between the three classes. Do not continue to part (c) until the three classes show at least some separation in the first two principal component score vectors.
- Perform \(K\)-means clustering of the observations with \(K = 3\). How well do the clusters that you obtained in \(K\)-means clustering compare to the true class labels?
*Hint: You can use the*`table()`

function in`R`

to compare the true class labels to the class labels obtained by clustering. Be careful how you interpret the results: \(K\)-means clustering will arbitrarily number the clusters, so you cannot simply check whether the true class labels and clustering labels are the same. - Perform \(K\)-means clustering with \(K = 2\). Describe your results.
- Now perform \(K\)-means clustering with \(K = 4\), and describe your results.
- Now perform \(K\)-means clustering with \(K = 3\) on the first two principal component score vectors, rather than on the raw data. That is, perform \(K\)-means clustering on the \(60 \times 2\) matrix of which the first column is the first principal component score vector, and the second column is the second principal component score vector. Comment on the results.
- Using the
`scale()`

function, perform \(K\)-means clustering with \(K = 3\) on the data after scaling each variable to have standard deviation one. How do these results compare to those obtained in (b)? Explain.