This is the solution for quiz 4 in Practical Machine Learning Course by JHU/Coursera
library(AppliedPredictiveModeling); library(caret); library(ElemStatLearn); library(pgmm)
library(rpart); library(gbm); library(lubridate); library(forecast); library(e1071)
data("vowel.train")
data("vowel.test")
###set Y as factor
vowel.train$y = as.factor(vowel.train$y)
vowel.test$y = as.factor(vowel.test$y)
set.seed(33833)
###fit 1 as a rf model
mod1 <- train(y~., method = "rf", data = vowel.train)
mod2 <- train(y~., method = "gbm", data = vowel.train)
###check the accuracy
pred1 <- predict(mod1, vowel.test)
pred2 <- predict(mod2, vowel.test)
r1 = confusionMatrix(pred1, vowel.test$y)
r2 = confusionMatrix(pred2, vowel.test$y)
r3 = confusionMatrix(pred1, pred2)
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 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").
set.seed(62433)
mod1 <- train(diagnosis~., data = training, method = "rf")
mod2 <- train(diagnosis~., data = training, method = "gbm")
mod3 <- train(diagnosis~., data = training, method = "lda")
## Warning in lda.default(x, grouping, ...): variables are collinear
## Warning in lda.default(x, grouping, ...): variables are collinear
pred1 <- predict(mod1, testing)
pred2 <- predict(mod2, testing)
pred3 <- predict(mod3, testing)
predDF <- data.frame(pred1, pred2, pred3, diagnosis = testing$diagnosis)
comModFit <- train(diagnosis~., method = "gam", data = predDF )
combPred <- predict(comModFit, predDF)
confusionMatrix(pred1, testing$diagnosis) ###0.76
## Confusion Matrix and Statistics
##
## Reference
## Prediction Impaired Control
## Impaired 10 7
## Control 12 53
##
## Accuracy : 0.7683
## 95% CI : (0.662, 0.8544)
## No Information Rate : 0.7317
## P-Value [Acc > NIR] : 0.2707
##
## Kappa : 0.3641
## Mcnemar's Test P-Value : 0.3588
##
## Sensitivity : 0.4545
## Specificity : 0.8833
## Pos Pred Value : 0.5882
## Neg Pred Value : 0.8154
## Prevalence : 0.2683
## Detection Rate : 0.1220
## Detection Prevalence : 0.2073
## Balanced Accuracy : 0.6689
##
## 'Positive' Class : Impaired
##
confusionMatrix(pred2, testing$diagnosis) ###0.79
## Confusion Matrix and Statistics
##
## Reference
## Prediction Impaired Control
## Impaired 14 9
## Control 8 51
##
## Accuracy : 0.7927
## 95% CI : (0.6889, 0.8743)
## No Information Rate : 0.7317
## P-Value [Acc > NIR] : 0.1297
##
## Kappa : 0.4795
## Mcnemar's Test P-Value : 1.0000
##
## Sensitivity : 0.6364
## Specificity : 0.8500
## Pos Pred Value : 0.6087
## Neg Pred Value : 0.8644
## Prevalence : 0.2683
## Detection Rate : 0.1707
## Detection Prevalence : 0.2805
## Balanced Accuracy : 0.7432
##
## 'Positive' Class : Impaired
##
confusionMatrix(pred3, testing$diagnosis) ###0.76
## Confusion Matrix and Statistics
##
## Reference
## Prediction Impaired Control
## Impaired 16 13
## Control 6 47
##
## Accuracy : 0.7683
## 95% CI : (0.662, 0.8544)
## No Information Rate : 0.7317
## P-Value [Acc > NIR] : 0.2707
##
## Kappa : 0.4639
## Mcnemar's Test P-Value : 0.1687
##
## Sensitivity : 0.7273
## Specificity : 0.7833
## Pos Pred Value : 0.5517
## Neg Pred Value : 0.8868
## Prevalence : 0.2683
## Detection Rate : 0.1951
## Detection Prevalence : 0.3537
## Balanced Accuracy : 0.7553
##
## 'Positive' Class : Impaired
##
confusionMatrix(combPred, testing$diagnosis) ##0.80
## Confusion Matrix and Statistics
##
## Reference
## Prediction Impaired Control
## Impaired 11 5
## Control 11 55
##
## Accuracy : 0.8049
## 95% CI : (0.7026, 0.8842)
## No Information Rate : 0.7317
## P-Value [Acc > NIR] : 0.08208
##
## Kappa : 0.4561
## Mcnemar's Test P-Value : 0.21130
##
## Sensitivity : 0.5000
## Specificity : 0.9167
## Pos Pred Value : 0.6875
## Neg Pred Value : 0.8333
## Prevalence : 0.2683
## Detection Rate : 0.1341
## Detection Prevalence : 0.1951
## Balanced Accuracy : 0.7083
##
## 'Positive' Class : Impaired
##
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)
mod1 <- train(CompressiveStrength~., data = training, method = "lasso")
pred1 <- predict(mod1, testing)
plot.enet(mod1$finalModel, xvar = "penalty", use.color = TRUE)
library(lubridate) # For year() function below
library(forecast)
dat = read.csv("~/Desktop/gaData.csv")
training = dat[year(dat$date) < 2012,]
testing = dat[(year(dat$date)) > 2011,]
tstrain = ts(training$visitsTumblr)
tstesting = ts()
#######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?
mod1 <- bats(tstrain)
pred <- forecast(mod1, h = length(testing$visitsTumblr), level = 95)
#####calculating the answer
sum(pred$lower < testing$visitsTumblr & testing$visitsTumblr < pred$upper)/length(testing$visitsTumblr)
## [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)
mod1 <- svm(CompressiveStrength~., data = training)
pred1 <- predict(mod1, testing)
accuracy(pred1, testing$CompressiveStrength)
## ME RMSE MAE MPE MAPE
## Test set 0.1682863 6.715009 5.120835 -7.102348 19.27739