1. Random Forest versus Gradient Boosting

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

Predicting Accuracy

##  Accuracy 
## 0.5779221
##  Accuracy 
## 0.5238095
## [1] 0.635514

2. Individual predictions using RF, GBM, LDA vs stack predictions using RF

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, ] set.seed(62433) modell_rf <- train(diagnosis ~ ., data = training, method = “rf”) modell_gbm <- train(diagnosis ~ ., data = training, method = “gbm”) modell_lda <- train(diagnosis ~ ., data = training, method = “lda”)

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

Is it better or worse than each of the individual predictions? ANSWER: It is better than random forest and linear discriminant analysis but interestingly, the same result as the boosted trees method.

Predicting Accuracy of individual vs stack method

confusionMatrix(pred_rf, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.7682927
confusionMatrix(pred_gbm, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.8170732
confusionMatrix(pred_lda, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.7682927
confusionMatrix(combinedPredict, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.8170732

3. Predicting Compression Strength using Lasso method

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

ANSWER: Cement is the last coefficient variable to be to set to zero as the penalty increases per plot shown.

## Loading required package: lars
## Loaded lars 1.2

4. Individual predictions using RF, GBM, LDA vs stack predictions using RF

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(“~/Desktop/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?

ANSWER: 0.9617021 ~ 96%

library(lubridate) # For year() function below
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
dat = read.csv("c:/Users/nolic/Documents/Learning/R_Coursera/8.MachineLearning/gaData.csv")
training = dat[year(dat$date) < 2012,]
testing = dat[(year(dat$date)) > 2011,]
tstrain = ts(training$visitsTumblr)

library(forecast)
mod_ts <- bats(tstrain)
fcast <- forecast(mod_ts, level = 95, h = dim(testing)[1])
sum(fcast$lower < testing$visitsTumblr & testing$visitsTumblr < fcast$upper) / 
    dim(testing)[1]
## [1] 0.9617021

5. Individual predictions using RF, GBM, LDA vs stack predictions using RF

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(3523)
library(AppliedPredictiveModeling)
data(concrete)
inTrain = createDataPartition(concrete$CompressiveStrength, p = 3/4)[[1]]
training = concrete[inTrain, ]
testing = concrete[-inTrain, ]

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

ANSWER: RMSE=6.715009