1. What is the final model prediction for a data set using CART with rpart method?

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

  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:

The final model prediction for cases have 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

ANSWER = a. PS, b. WS, c. PS, d. Not possible to predict

## Warning: package 'AppliedPredictiveModeling' was built under R version
## 3.3.3
## Warning: package 'caret' was built under R version 3.3.3
## Loading required package: lattice
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.3
## Warning: package 'rattle' was built under R version 3.3.3
## Rattle: A free graphical interface for data science with R.
## Version 5.1.0 Copyright (c) 2006-2017 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
modFit1 <- train(Class ~., data=inTrain,  method="rpart")
print(modFit1$finalModel)
## n= 1009 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
## 1) root 1009 373 PS (0.63032706 0.36967294)  
##   2) TotalIntenCh2< 45323.5 454  34 PS (0.92511013 0.07488987) *
##   3) TotalIntenCh2>=45323.5 555 216 WS (0.38918919 0.61081081)  
##     6) FiberWidthCh1< 9.673245 154  47 PS (0.69480519 0.30519481) *
##     7) FiberWidthCh1>=9.673245 401 109 WS (0.27182045 0.72817955) *
fancyRpartPlot(modFit1$finalModel) 

2. K-fold cross validation

If K is small in a K-fold cross validation is the bias in the estimate of outof-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?

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

3. Olive oil data set

Predict the value of area using the tree command with all defaults for the following data frame: newdata = as.data.frame(t(colMeans(olive)))

(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.

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

Answer: 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

## Warning: package 'pgmm' was built under R version 3.3.3
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info =
## trainInfo, : There were missing values in resampled performance measures.
## n= 572 
## 
## node), split, n, deviance, yval
##       * denotes terminal node
## 
## 1) root 572 3171.32000 4.599650  
##   2) Eicosenoic>=6.5 323  176.82970 2.783282 *
##   3) Eicosenoic< 6.5 249  546.51410 6.955823  
##     6) Linoleic>=1053.5 98   21.88776 5.336735 *
##     7) Linoleic< 1053.5 151  100.99340 8.006623 *
##        1 
## 2.783282

4. South Africa Heart Disease Data

Load the South Africa Heart Disease Data and create training and test sets. 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:

library(ElemStatLearn) 
## Warning: package 'ElemStatLearn' was built under R version 3.3.3
data(SAheart) 
set.seed(8484) 
train = sample(1:dim(SAheart)[1],size=dim(SAheart)[1]/2,replace=F) 
trainSA = SAheart[train,] 
testSA = SAheart[-train,]
set.seed  = 13234

modelHeart=train(chd ~ age+alcohol+obesity+tobacco+typea+ldl
                 , data=trainSA, method="glm", family="binomial")
## 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.
missClass = function(values,modelHeart){sum(((modelHeart > 0.5)*1) != values )/length(values)}

missClassTrain = missClass(trainSA$chd, predict(modelHeart, newdata = trainSA))
missClassTrain
## [1] 0.2727273
missClassTest = missClass(testSA$chd, predict(modelHeart, newdata = testSA))
missClassTest
## [1] 0.3116883

ANSWER: TRAIN = 0.273 TEST = 0.312

5. Vowel Data variable of importance

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]

## [1] 528  11
## [1] 462  11
## rf variable importance
## 
##       Overall
## x.2  100.0000
## x.1   97.4680
## x.5   40.0023
## x.6   27.3641
## x.8   18.7926
## x.4    8.5685
## x.3    7.7887
## x.9    3.5333
## x.10   0.1629
## x.7    0.0000

ANSWER: 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