Load required packages

suppressMessages(library(AppliedPredictiveModeling))
suppressMessages(library(caret))
suppressMessages(library(pgmm))
suppressMessages(library(rpart))
suppressMessages(library(gbm))
suppressMessages(library(lubridate))
suppressMessages(library(ElemStatLearn))
suppressMessages(library(ggplot2))
suppressMessages(library(forecast))
suppressMessages(library(e1071))

Question - 1

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

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

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

vowel.train$y<-as.factor(vowel.train$y)
vowel.test$y<-as.factor(vowel.test$y)
set.seed(33833)
fit1<-train(y~., method="rf", data = vowel.train)
## Loading required package: randomForest
## randomForest 4.6-12
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
## 
##     margin
pred1<-predict(fit1, vowel.test)
fit2<-train(y~., method="gbm", data = vowel.train)
## Loading required package: plyr
## 
## Attaching package: 'plyr'
## The following object is masked from 'package:ElemStatLearn':
## 
##     ozone
## The following object is masked from 'package:lubridate':
## 
##     here
pred2<-predict(fit2, vowel.test)
rf<-postResample(pred1, vowel.test$y)[1]
gbm<-postResample(pred2, vowel.test$y)[1]
agreeSub<-vowel.test[pred1 == pred2,]
pred_comb<-predict(fit1, agreeSub)
aa<-sum(pred_comb == agreeSub$y) / length(pred_comb)

RF Accuracy = 0.6147186

GBM Accuracy = 0.5367965

Agreement Accuracy = 0.6656051

Question - 2

Load the Alzheimer’s data using the following commands:

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

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?

Answer

set.seed(62433)
fit1<-train(diagnosis~., data = training, method="rf")
pred1<-predict(fit1, testing)
rfAcc<-postResample(pred1, testing$diagnosis)[1]
fit2<-train(diagnosis~., data = training, method="gbm")
pred2<-predict(fit2, testing)
gbmAcc<-postResample(pred2, testing$diagnosis)[1]
fit3<-train(diagnosis~., data = training, method="lda")
## Loading required package: MASS
## Warning in lda.default(x, grouping, ...): variables are collinear

## Warning in lda.default(x, grouping, ...): variables are collinear

## Warning in lda.default(x, grouping, ...): variables are collinear
pred3<-predict(fit3, testing)
ldaAcc<-postResample(pred3, testing$diagnosis)[1]
combDF<-data.frame(pred1, pred2, pred3, diag=testing$diagnosis)
combFit<-train(diag~., data = combDF, method="rf")
pred4<-predict(combFit, combDF)
combAcc<-postResample(pred4, testing$diagnosis)[1]
data.frame(rfAcc=rfAcc, gbmAcc=gbmAcc, ldaAcc=ldaAcc, combAcc=combAcc)

Question - 3

Load the concrete data with the commands:

set.seed(3523)
library(AppliedPredictiveModeling)
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).

set.seed(233)
fit<-train(CompressiveStrength~., data = training, method="lasso")
## Loading required package: elasticnet
## Loading required package: lars
## Loaded lars 1.2
library(elasticnet)
plot.enet(fit$finalModel, xvar = "penalty", 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

remdata<-dat[year(dat$date) > 2011,]
tsrem<-ts(remdata$visitsTumblr)
model<-bats(tstrain)
pred <- forecast(model, h=length(tsrem),level=c(95))
accuracy(pred, remdata$visitsTumblr)
##                     ME     RMSE       MAE      MPE     MAPE      MASE
## Training set  10.47617 258.7813  40.17811     -Inf      Inf 0.8426869
## Test set     174.70772 295.0175 199.95205 22.75223 40.04742 4.1937509
##                    ACF1
## Training set 0.01157855
## Test set             NA
acc = sum(remdata$visitsTumblr <= pred$upper) / nrow(remdata)

Question - 5

Load the concrete data with the commands:

set.seed(3523)
library(AppliedPredictiveModeling)
data(concrete)
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?

set.seed(325)
model = svm(CompressiveStrength ~ ., data = training)
pred<-predict(model, testing)
accuracy(pred, testing$CompressiveStrength)
##                 ME     RMSE      MAE       MPE     MAPE
## Test set 0.1682863 6.715009 5.120835 -7.102348 19.27739