Question 1

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

library(ElemStatLearn)
data(vowel.train)
data(vowel.test)
vowel.test$y<-as.factor(vowel.test$y)

Set the variable y to be a factor variable in both the training and test set. Then set the seed to 33833.

Fit (1) a random forest predictor relating the factor variable y to the remaining variables and (2) a boosted predictor using the “gbm” method. Fit these both with the train() command in the caret package.

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

library(caret); library(randomForest)
library(ggplot2); library(gbm)

set.seed(33833)

# random forest model
modFit1<-train(y~., data=vowel.train, method="rf")
predrf<-predict(modFit1, vowel.test)
predrfRight<-predrf==vowel.test$y
sum(predrfRight)/length(predrfRight)

# gbm
modFit2<-train(y~., method="gbm", data=vowel.train, verbose=FALSE)
predgbm<-predict(modFit2, vowel.test)
predgbmRight<-predgbm==vowel.test$y
sum(predgbmRight)/length(predgbmRight)

# how do the two methods agree with each other? 
agree<-predrf==predgbm
sum(agree)/length(agree)

What are the accuracies for the two approaches on the test data set? What is the accuracy among the test set samples where the two methods agree?

Answer: RF Accuracy = 0.6082; GBM Accuracy = 0.5152; Agreement Accuracy = 0.6361

Question 2

Load the Alzheimer’s data using the following commands:

library(caret); library(gbm)

set.seed(3433)
library(AppliedPredictiveModeling)
data(AlzheimerDisease)
adData = data.frame(diagnosis,predictors)
inTrain = createDataPartition(adData$diagnosis, p = 3/4)[[1]]
training = adData[ inTrain,]
testing = adData[-inTrain,]

dim(training); dim(testing)

Set the seed to 62433 and predict diagnosis with all the other variables using a random forest (“rf”), boosted trees (“gbm”) and linear discriminant analysis (“lda”) model. Stack the predictions together using random forests (“rf”). What is the resulting accuracy on the test set? Is it better or worse than each of the individual predictions?

mod1 <- train(diagnosis ~.,method="rf", data=training)
mod2 <- train(diagnosis ~., method="gbm", data=training, verbose=FALSE)
mod3 <- train(diagnosis ~., method="lda", data=training)

pred1<- predict(mod1, testing); pred2<-predict(mod2, testing); pred3<-predict(mod3, testing)

predDF <- data.frame(pred1,pred2, pred3, diagnosis=testing$diagnosis)
combModFit <- train(diagnosis ~.,method="rf",data=predDF)
combPred <- predict(combModFit,predDF)

Accuracy<-combPred==testing$diagnosis

sum(Accuracy)/length(Accuracy)

Answer: Stacked Accuracy: 0.80 is better than random forests and lda, and the same as boosting.

Note: the prediction from the stacked model is optimstic because the predictions used to predict performance of the model are from the test set. It is better to set aside a validation set besides the training and testing set (like in the lecture).

Question 3

Load the concrete data with the commands:

set.seed(3523)
library(AppliedPredictiveModeling);library(caret); library(elasticnet)
data(concrete)
inTrain = createDataPartition(concrete$CompressiveStrength, p = 3/4)[[1]]
training = concrete[ inTrain,]
testing = concrete[-inTrain,]

Set the seed to 233 and fit a lasso model to predict Compressive Strength. Which variable is the last coefficient to be set to zero as the penalty increases? (Hint: it may be useful to look up ?plot.enet).

mod_lasso <- train(CompressiveStrength~., data=training, method="lasso")

#the link helps with the explanation: https://stats.stackexchange.com/questions/78694/how-to-interpret-the-lasso-selection-plot
plot(mod_lasso$finalModel, use.color=TRUE)

Question 4

Load the data on the number of visitors to the instructors blog from here: https://d396qusza40orc.cloudfront.net/predmachlearn/gaData.csv Using the commands:

library(lubridate) # For year() function below
dat = read.csv("./gaData.csv")
training = dat[year(dat$date) < 2012,]
testing = dat[(year(dat$date)) > 2011,]
tstrain = ts(training$visitsTumblr)

Fit a model using the bats() function in the forecast package to the training time series. Then forecast this model for the remaining time points. For how many of the testing points is the true value within the 95% prediction interval bounds?

# help(package=forecast)
# require("sos"); findFn("bats");

library(forecast) # to use bats() function 
tsfit<-bats(training$visitsTumblr)

tspred<-forecast(tsfit, h=235, level=95) #testing set has 235 observations

in95 <- ifelse(sapply(testing$visitsTumblr, function(p) any(tspred$lower <= p & tspred$upper >= p)), 1, 0)

sum(in95)/length(in95)

Question 5

Load the concrete data with the commands:

set.seed(3523)
library(AppliedPredictiveModeling)
data(concrete)

#inTrain = createDataPartition(concrete$CompressiveStrength, p = 3/4, list=FALSE)
inTrain = createDataPartition(concrete$CompressiveStrength, p = 3/4)[[1]]

training = concrete[ inTrain,]
testing = concrete[-inTrain,]

Set the seed to 325 and fit a support vector machine using the e1071 package to predict Compressive Strength using the default settings. Predict on the testing set. What is the RMSE?

install.packages("e1071", dependencies = TRUE); library(e1071)

svmfit<-svm(CompressiveStrength~., data=training)
svmpred<-predict(svmfit, testing[,-9])
sqrt(sum(testing$CompressiveStrength-svmpred)^2/length(svmpred))

Answer: RMSE=6.72

Copyright © 2019 Cathy Gao at cathygao.2019@outlook.com.