Question 1

Load the cell segmentation data from the AppliedPredictiveModeling package using the commands:

library(AppliedPredictiveModeling)
data(segmentationOriginal)
suppressMessages(library(caret))
  1. Subset the data to a training set and testing set based on the Case variable in the data set.
  2. Set the seed to 125 and fit a CART model with the rpart method using all predictor variables and default caret settings.
  3. In the final model what would be the final model prediction for cases with the following variable values:
  1. TotalIntench2 = 23,000; FiberWidthCh1 = 10; PerimStatusCh1 = 2
  2. TotalIntench2 = 50,000; FiberWidthCh1 = 10; VarIntenCh4 = 100
  3. TotalIntench2 = 57,000; FiberWidthCh1 = 8; VarIntenCh4 = 100
  4. FiberWidthCh1 = 8; VarIntenCh4 = 100; PerimStatusCh1 = 2

Solution:

# 1. Subset the data to a training set and testing set based on the Case variable in the data set. 
inTrain <- createDataPartition(y = segmentationOriginal$Case, p = 0.6, 
                               list = FALSE) # 60% training
training <- segmentationOriginal[inTrain, ]
testing <- segmentationOriginal[-inTrain, ]
# 2. Set the seed to 125 and fit a CART model with the rpart method using all predictor variables and default caret settings. (The outcome class is contained in a factor variable called Class with levels "PS" for poorly segmented and "WS" for well segmented.)
set.seed(125)
modFit <- train(Class ~ ., method = "rpart", data = training)
## Loading required package: rpart
# 3. 
modFit$finalModel
## n= 1212 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
## 1) root 1212 431 PS (0.64438944 0.35561056)  
##   2) TotalIntenCh2< 44626 572  37 PS (0.93531469 0.06468531) *
##   3) TotalIntenCh2>=44626 640 246 WS (0.38437500 0.61562500)  
##     6) FiberWidthCh1< 9.866294 172  55 PS (0.68023256 0.31976744) *
##     7) FiberWidthCh1>=9.866294 468 129 WS (0.27564103 0.72435897) *
suppressMessages(library(rattle))
library(rpart.plot)
fancyRpartPlot(modFit$finalModel)

Based on the decision tree, the model prediction is

  1. PS

  2. WS

  3. PS

  4. Not possible to predict

Question 2

If K is small in a K-fold cross validation is the bias in the estimate of out-of-sample (test set) accuracy smaller or bigger? If K is small is the variance in the estimate of out-of-sample (test set) accuracy smaller or bigger. Is K large or small in leave one out cross validation?

Solution: The bias is larger and the variance is smaller. Under leave one out cross validation K is equal to the sample size.

Question 3

Load the olive oil data using the commands:

library(pgmm)
data(olive)
olive = olive[, -1]

(NOTE: If you have trouble installing the pgmm package, you can download the olive dataset here: olive_data.zip. After unzipping the archive, you can load the file using the load() function in R.) These data contain information on 572 different Italian olive oils from multiple regions in Italy. Fit a classification tree where Area is the outcome variable. Then predict the value of area for the following data frame using the tree command with all defaults

newdata = as.data.frame(t(colMeans(olive)))

What is the resulting prediction? Is the resulting prediction strange? Why or why not?

Solution:

modolive <- train(Area ~ ., method = "rpart", data = olive)
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info =
## trainInfo, : There were missing values in resampled performance measures.
predict(modolive, newdata = newdata)
## [1] 2.783282

The predicted value is 2.783. It is strange because Area should be a qualitative variable - but tree is reporting the average value of Area as a numeric variable in the leaf predicted for newdata.

Question 4

Load the South Africa Heart Disease Data and create training and test sets with the following code:

library(ElemStatLearn)
data(SAheart)
set.seed(8484)
train = sample(1:dim(SAheart)[1], size = dim(SAheart)[1] / 2, replace = F)
trainSA = SAheart[train, ]
testSA = SAheart[-train, ]

Then set the seed to 13234 and fit a logistic regression model (method = “glm”, be sure to specify family = “binomial”) with Coronary Heart Disease (chd) as the outcome and age at onset, current alcohol consumption, obesity levels, cumulative tabacco, type-A behavior, and low density lipoprotein cholesterol as predictors. Calculate the misclassification rate for your model using this function and a prediction on the “response” scale:

missClass = function(values, prediction){sum(((prediction > 0.5) * 1) != values) / length(values)}

What is the misclassification rate on the training set? What is the misclassification rate on the test set?

Solution:

set.seed(13234)
modelSA <- train(chd ~ age + alcohol + obesity + tobacco + typea + ldl, 
               data = trainSA, method = "glm", family = "binomial")
missClass(testSA$chd, predict(modelSA, newdata = testSA))
## [1] 0.3116883
missClass(trainSA$chd, predict(modelSA, newdata = trainSA))
## [1] 0.2727273

Hence, the misclassification rate on the training set is 0.27 and the misclassification rate on the test set is 0.31.

Question 5

Load the vowel.train and vowel.test data sets:

library(ElemStatLearn)
data(vowel.train)
data(vowel.test) 

Set the variable y to be a factor variable in both the training and test set. Then set the seed to 33833. Fit a random forest predictor relating the factor variable y to the remaining variables. Read about variable importance in random forests here: http://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm#ooberr The caret package uses by defualt the Gini importance. Calculate the variable importance using the varImp function in the caret package. What is the order of variable importance?

Solution:

vowel.train$y <- as.factor(vowel.train$y)
vowel.test$y <- as.factor(vowel.test$y)
set.seed(33833)
library(randomForest)
## randomForest 4.6-10
## Type rfNews() to see new features/changes/bug fixes.
modvowel <- randomForest(y ~ ., data = vowel.train)
order(varImp(modvowel), decreasing = T)
##  [1]  2  1  5  6  8  4  9  3  7 10

Therefore, the order of the variables is: x.2, x.1, x.5, x.6, x.8, x.4, x.9, x.3, x.7, x.10