library(AppliedPredictiveModeling)
data(segmentationOriginal)
library(caret)
library(randomForest)

1.

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?

set.seed(33833)
vowel.train$y <- factor(vowel.train$y)
vowel.test$y <- factor(vowel.test$y)
fit.rf <- randomForest(y~., data=vowel.train) 
fit.gbm <- train(y~., method="gbm", data=vowel.train, verbose = F)
predict.rf <- predict(fit.rf, vowel.test)
predict.gbm <- predict(fit.gbm, vowel.test)
confusionMatrix(predict.rf, vowel.test$y)$overall[1]
##  Accuracy 
## 0.5800866
confusionMatrix(predict.gbm, vowel.test$y)$overall[1]
## Accuracy 
## 0.517316
confusionMatrix(predict.gbm, predict.rf)$overall[1]
##  Accuracy 
## 0.6904762

2.

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

set.seed(62433)
ad.fit.rf <- train(diagnosis~., method="rf", data=training) 
ad.fit.gbm <- train(diagnosis~., method="gbm", data=training, verbose = F)
ad.fit.lda <- train(diagnosis~., method="lda", data=training)
## 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

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

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

## Warning in lda.default(x, grouping, ...): variables are collinear
ad.pdt.rf <- predict(ad.fit.rf, testing)
ad.pdt.gbm <- predict(ad.fit.gbm, testing)
ad.pdt.lda <- predict(ad.fit.lda, testing)
Stacked <- data.frame(rf = ad.pdt.rf, gbm = ad.pdt.gbm, lda = ad.pdt.lda, diagnosis = testing$diagnosis)
stack.fit <- train(diagnosis ~., method="rf", data=Stacked)
## note: only 2 unique complexity parameters in default grid. Truncating the grid to 2 .
stack.pdt <- predict(stack.fit, Stacked)
confusionMatrix(ad.pdt.rf, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.7682927
confusionMatrix(ad.pdt.gbm, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.8170732
confusionMatrix(ad.pdt.lda, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.7682927
confusionMatrix(stack.pdt, testing$diagnosis)$overall[1]
##  Accuracy 
## 0.8170732

3.

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)
lassoFit <- train(CompressiveStrength ~.,  data = training,
               method = "lasso")
lassoFit$finalModel
## $call
## elasticnet::enet(x = as.matrix(x), y = y, lambda = 0)
## 
## $actions
## $actions[[1]]
## Cement 
##      1 
## 
## $actions[[2]]
## Superplasticizer 
##                5 
## 
## $actions[[3]]
## Age 
##   8 
## 
## $actions[[4]]
## BlastFurnaceSlag 
##                2 
## 
## $actions[[5]]
## Water 
##     4 
## 
## $actions[[6]]
## FineAggregate 
##             7 
## 
## $actions[[7]]
## FlyAsh 
##      3 
## 
## $actions[[8]]
## FineAggregate 
##            -7 
## 
## $actions[[9]]
## FineAggregate 
##             7 
## 
## $actions[[10]]
## CoarseAggregate 
##               6 
## 
## $actions[[11]]
## [1] 11
## 
## 
## $allset
## [1] 1 2 3 4 5 6 7 8
## 
## $beta.pure
##        Cement BlastFurnaceSlag     FlyAsh      Water Superplasticizer
## 0  0.00000000       0.00000000 0.00000000  0.0000000        0.0000000
## 1  0.01802441       0.00000000 0.00000000  0.0000000        0.0000000
## 2  0.02729231       0.00000000 0.00000000  0.0000000        0.1559023
## 3  0.03847584       0.00000000 0.00000000  0.0000000        0.4343197
## 4  0.04599070       0.01006844 0.00000000  0.0000000        0.5510774
## 5  0.06499050       0.03893552 0.00000000 -0.1174308        0.5737881
## 6  0.06678431       0.04187677 0.00000000 -0.1401379        0.5739760
## 7  0.08502706       0.06390658 0.03312720 -0.1768114        0.4070855
## 8  0.10150634       0.08388323 0.06377012 -0.2190396        0.2504283
## 9  0.10301008       0.08567182 0.06624407 -0.2188092        0.2387808
## 10 0.11351343       0.09842089 0.08097549 -0.1794448        0.2567532
##    CoarseAggregate FineAggregate        Age
## 0       0.00000000   0.000000000 0.00000000
## 1       0.00000000   0.000000000 0.00000000
## 2       0.00000000   0.000000000 0.00000000
## 3       0.00000000   0.000000000 0.02765634
## 4       0.00000000   0.000000000 0.04042040
## 5       0.00000000   0.000000000 0.07968720
## 6       0.00000000  -0.003483276 0.08499933
## 7       0.00000000   0.000000000 0.09810130
## 8       0.00000000   0.000000000 0.11088807
## 9       0.00000000   0.001412076 0.11162794
## 10      0.01130084   0.014480343 0.11346049
## attr(,"scaled:scale")
## [1] 2831.2770 2416.1259 1767.8101  595.3663  168.3104 2133.9390 2234.6943
## [8] 1756.7394
## 
## $vn
## [1] "Cement"           "BlastFurnaceSlag" "FlyAsh"          
## [4] "Water"            "Superplasticizer" "CoarseAggregate" 
## [7] "FineAggregate"    "Age"             
## 
## $mu
## [1] 35.83765
## 
## $normx
## [1] 2831.2770 2416.1259 1767.8101  595.3663  168.3104 2133.9390 2234.6943
## [8] 1756.7394
## 
## $meanx
##           Cement BlastFurnaceSlag           FlyAsh            Water 
##       281.108398        75.020284        53.444057       181.400904 
## Superplasticizer  CoarseAggregate    FineAggregate              Age 
##         6.321318       972.082300       774.158527        44.727390 
## 
## $lambda
## [1] 0
## 
## $L1norm
##  [1]    0.00000   51.03211  103.51208  230.62128  318.29920  584.55773
##  [7]  627.40970  799.82678  970.15842  985.46860 1108.17911
## 
## $penalty
##  [1] 4.532905e+02 3.512262e+02 2.914402e+02 2.078625e+02 1.686523e+02
##  [6] 7.241288e+01 5.892304e+01 3.131011e+01 3.697331e+00 2.375469e+00
## [11] 1.313172e-12
## 
## $df
##  [1] 1 2 3 4 5 6 7 7 7 8 9
## 
## $Cp
##           0           1           2           3           4           5 
## 1201.603156 1014.917230  861.914214  572.237452  422.520823  129.536499 
##           6           7           8           9          10 
##  105.671332   34.171119    6.766949    8.339652    9.000000 
## 
## $sigma2
##      10 
## 108.795 
## 
## $xNames
## [1] "Cement"           "BlastFurnaceSlag" "FlyAsh"          
## [4] "Water"            "Superplasticizer" "CoarseAggregate" 
## [7] "FineAggregate"    "Age"             
## 
## $problemType
## [1] "Regression"
## 
## $tuneValue
##   fraction
## 3      0.9
## 
## $obsLevels
## [1] NA
## 
## $param
## list()
## 
## attr(,"class")
## [1] "enet"

4.

suppressMessages(library(lubridate))  # For year() function below
## Warning: package 'lubridate' was built under R version 3.4.3
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?

suppressMessages(library(forecast))
## Warning: package 'forecast' was built under R version 3.4.3
visit.fit <- bats(training[,3])
visit.pdt <- forecast(visit.fit, h=length(testing[,3]))
inbounds <- sum((testing$visitsTumblr <= visit.pdt$upper[,2]) & (testing$visitsTumblr >= visit.pdt$lower[,2]))
inbounds/length(testing[,3])
## [1] 0.9617021
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)
suppressMessages(library(e1071))
## Warning: package 'e1071' was built under R version 3.4.3
fit.svm <- svm(CompressiveStrength~., data=training)
pdt.svm <- predict(fit.svm, testing)
RMSE(testing$CompressiveStrength, pdt.svm)
## [1] 6.715009