Question 1

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)
## 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 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

set.seed(125)
trainSet <- segmentationOriginal[segmentationOriginal$Case =="Train",]
testSet <- segmentationOriginal[segmentationOriginal$Case =="Test",]

set.seed(125)
fit <- train(Class~.,data=trainSet,method="rpart")
plot(fit$finalModel, uniform=TRUE, 
    main="Classification Tree")
text(fit$finalModel, use.n=TRUE, all=TRUE, cex=.8)

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

Answer:
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)
setwd("D:\\Coursera\\Data_Science_Specialization\\08 - Practical Machine Learing\\Week_3\\Practical machine learning - Quiz 3")
load(file="olive.rda")
olive = olive[,-1]

``` 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)))
# https://www.r-bloggers.com/classification-trees/
fit <- 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.
predict(fit,newdata=newdata)
##        1 
## 2.783282

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

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)}
set.seed(13234)
fit4 <- 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.
pred <- predict(fit4, testSA)
missClassTrain <- missClass(trainSA$chd,predict(fit4,newdata=trainSA))
missClassTest <- missClass(testSA$chd,predict(fit4,newdata=testSA))
missClassTrain
## [1] 0.2727273
missClassTest
## [1] 0.3116883

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

vowel.train$y <- factor(vowel.train$y)
vowel.test$y <- factor(vowel.test$y)


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:ggplot2':
## 
##     margin
set.seed(33833)
fit5 <- randomForest(y~., data=vowel.train)
order( varImp(fit5),decreasing=TRUE)
##  [1]  1  2  5  6  8  4  3  9  7 10

Answer:
There is no such version of answer. Possible due to wrong version of some library.