Question 1

For this quiz we will be using several R packages. R package versions change over time, the right answers have been checked using the following versions of the packages.

AppliedPredictiveModeling: v1.1.6 caret: v6.0.47 ElemStatLearn: v2012.04-0 pgmm: v1.1 rpart: v4.1.8

If you aren’t using these versions of the packages, your answers may not exactly match the right answer, but hopefully should be close.

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

library(AppliedPredictiveModeling)
## Warning: package 'AppliedPredictiveModeling' was built under R version
## 3.5.1
data(segmentationOriginal)
library(caret)
## Warning: package 'caret' was built under R version 3.5.1
## Loading required package: lattice
## Loading required package: ggplot2
  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 to predict Class 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 TIP: Plot the resulting tree and to use the plot to answer this question.

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) 
training <- segmentationOriginal[inTrain, ]
testing <- segmentationOriginal[-inTrain, ]

Set the seed to 125 and fit a CART model to predict Class with the rpart method using all predictor variables and default caret settings

set.seed(125)
modFit <- train(Class ~ ., method = "rpart", data = training)

Final model predictions for the defined set of cases

modFit$finalModel
## n= 1212 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 1212 424 PS (0.65016502 0.34983498)  
##    2) TotalIntenCh2< 41706.5 536  32 PS (0.94029851 0.05970149) *
##    3) TotalIntenCh2>=41706.5 676 284 WS (0.42011834 0.57988166)  
##      6) ShapeP2ACh1>=1.319586 465 216 PS (0.53548387 0.46451613)  
##       12) Cell>=2.08321e+08 400 166 PS (0.58500000 0.41500000) *
##       13) Cell< 2.08321e+08 65  15 WS (0.23076923 0.76923077) *
##      7) ShapeP2ACh1< 1.319586 211  35 WS (0.16587678 0.83412322) *

Plot of the resulting tree:

library(rattle)
## Warning: package 'rattle' was built under R version 3.5.1
## Rattle: A free graphical interface for data science with R.
## Version 5.2.0 Copyright (c) 2006-2018 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 3.5.1
## Loading required package: rpart
## Warning: package 'rpart' was built under R version 3.5.1
fancyRpartPlot(modFit$finalModel)

Solution: a: PS;b: WS;c: PS;d: 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 -code-olive-/code- dataset here: olive_data.zip. After unzipping the archive, you can load the file using the -code-load()-/code- 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?

olive_rpart <- train(Area~.,data=olive,method="rpart")
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info =
## trainInfo, : There were missing values in resampled performance measures.
fancyRpartPlot(olive_rpart$finalModel)

predict(olive_rpart,newdata=newdata)
##        1 
## 2.783282

Solution: 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)
## Warning: package 'ElemStatLearn' was built under R version 3.5.1
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?

set.seed(13234)
modelfit <- train(chd ~ age + alcohol + obesity + tobacco + typea + ldl, method = "glm", family = "binomial", data = trainSA)
## Warning in train.default(x, y, weights = w, ...): You are trying to do
## regression and your outcome only has two possible values Are you trying to
## do classification? If so, use a 2 level factor as your outcome column.
trainingpredict <- predict(modelfit, trainSA)
testingpredict <- predict(modelfit, testSA)

missClass(trainSA$chd, trainingpredict)
## [1] 0.2727273
missClass(testSA$chd, testingpredict)
## [1] 0.3116883

Solution: Test Set Misclassification: 0.31. Training Set: 0.27.

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 default the Gini importance. Calculate the variable importance using the varImp function in the caret package. What is the order of variable importance? [NOTE: Use randomForest() specifically, not caret, as there’s been some issues reported with that approach. 11/6/2016]

library(randomForest)
## Warning: package 'randomForest' was built under R version 3.5.1
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:rattle':
## 
##     importance
## The following object is masked from 'package:ggplot2':
## 
##     margin
vowel.train$y <- as.factor(vowel.train$y)
vowel.test$y <- as.factor(vowel.test$y)
modelRF <- randomForest(y~.,data=vowel.train)
order(varImp(modelRF),decreasing=TRUE)
##  [1]  2  1  5  6  8  4  3  9  7 10

Solution: The order of variable importance is 2 1 5 6 8 4 3 9 7 10.