Load the vowel.train and vowel.test data sets:
library(ElemStatLearn)
## Warning: package 'ElemStatLearn' was built under R version 3.5.1
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?
if(FALSE)
{
library(caret)
vowel.train$y <- factor(vowel.train$y)
vowel.test$y <- factor(vowel.test$y)
set.seed(33833)
fitRf <- train(y ~ ., data=vowel.train, method="rf")
fitGbm <- train(y ~ ., data=vowel.train, method="gbm", verbose=FALSE)
prRf <- predict(fitRf, vowel.test)
prGbm <- predict(fitGbm, vowel.test)
print(paste0("RF accuracy = ", confusionMatrix(prRf, vowel.test$y)$overall['Accuracy']))
print(paste0("GBM accuracy = ", confusionMatrix(prGbm, vowel.test$y)$overall['Accuracy']))
agreeIdx <- prRf == prGbm
print(paste0("Agreement accuracy = ", confusionMatrix(vowel.test$y[agreeIdx], prRf[agreeIdx])$overall['Accuracy']))
}
Answer: The result does not fit to any possible answer. (by aprox) RF Accuracy = 0.6082 GBM Accuracy = 0.5152 Agreement Accuracy = 0.6361
Load the Alzheimer’s data using the following commands
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
library(gbm)
## Warning: package 'gbm' was built under R version 3.5.1
## Loading required package: survival
##
## Attaching package: 'survival'
## The following object is masked from 'package:caret':
##
## cluster
## Loading required package: splines
## Loading required package: parallel
## Loaded gbm 2.1.3
set.seed(3433)
library(AppliedPredictiveModeling)
## Warning: package 'AppliedPredictiveModeling' was built under R version
## 3.5.1
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?
if(FALSE){
set.seed(62433)
fitRf <- train(diagnosis ~ ., data=training, method="rf")
fitGbm <- train(diagnosis ~ ., data=training, method="gbm", verbose=FALSE)
fitLda <- train(diagnosis ~ ., data=training, method="lda")
prRf <- predict(fitRf, testing)
prGbm <- predict(fitGbm, testing)
prLda <- predict(fitLda, testing)
combo <- data.frame(prRf, prGbm, prLda, diagnosis = testing$diagnosis)
fitSt <- train(diagnosis ~ ., data=combo, method="rf")
prSt <- predict(fitSt, testing)
print(paste0("RF accuracy = ", confusionMatrix(prRf, testing$diagnosis)$overall['Accuracy']))
print(paste0("GBM accuracy = ", confusionMatrix(prGbm, testing$diagnosis)$overall['Accuracy']))
print(paste0("LDA accuracy = ", confusionMatrix(prLda, testing$diagnosis)$overall['Accuracy']))
print(paste0("Stacked accuracy = ", confusionMatrix(prSt, testing$diagnosis)$overall['Accuracy']))
}
Answer:
Stacked Accuracy: 0.80 is better than all three other methods
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)
fitLs <- train(CompressiveStrength~., data=training, method = "lasso")
library(elasticnet)
## Loading required package: lars
## Loaded lars 1.2
plot.enet(fitLs$finalModel, xvar = "penalty", use.color = TRUE)
Answer:
The coefficient path shows that the variable Cement is the last coefficient to be set to zero as the penalty increases.
Load the data on the number of visitors to the instructors blog from here:
https://d396qusza40orc.cloudfront.net/predmachlearn/gaData.csv
Using the commands:
setwd("D:\\Coursera\\Data_Science_Specialization\\08 - Practical Machine Learing\\Week_4")
library(lubridate) # For year() function below
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
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?
library(forecast)
## Warning: package 'forecast' was built under R version 3.5.1
fit <- bats(tstrain)
fcast <- forecast(fit, nrow(testing), level = c(95))
plot(fcast)
points(dat$visitsTumblr)
print(sum(fcast$lower <= testing$visitsTumblr & testing$visitsTumblr <= fcast$upper) / dim(testing)[1])
## [1] 0.9617021
Answer:
About 96% percents of the testing points is the true value within the 95% prediction interval bounds.
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)
library(e1071)
## Warning: package 'e1071' was built under R version 3.5.1
svm_model <- svm(CompressiveStrength~., data=training)
#summary(svm_model)
pred <- predict(svm_model, testing)
print(paste0("RMSE = ", sqrt(mean((pred - testing$CompressiveStrength)^2))))
## [1] "RMSE = 6.71500922203364"
Answer:
RMSE = 6.72