-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz 04.Rmd
167 lines (152 loc) · 6.06 KB
/
Quiz 04.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
output:
md_document:
variant: markdown_github
---
Question 01
-----------
Load the vowel.train and vowel.test data sets:
```{r warning=FALSE}
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?
### Answer
RF Accuracy = 0.6081
GBM Accuracy = 0.5152
Agreement Accuracy = 0.6362
#### Explanation
```{r warning=FALSE}
library(caret)
vowel.train$y <- as.factor(vowel.train$y)
vowel.test$y <- as.factor(vowel.test$y)
set.seed(33833)
# fit rf predictor to y
fitRf <- train(y ~ ., data=vowel.train, method = "rf")
fitGBM <- train(y ~ ., data=vowel.train, method = "gbm")
predRf <- predict(fitRf, vowel.test)
predGBM <- predict(fitGBM, vowel.test)
# RF Accuracy:
confusionMatrix(predRf, vowel.test$y)$overall[1]
# GBM Accuracy:
confusionMatrix(predGBM, vowel.test$y)$overall[1]
pred <- data.frame(predRf, predGBM, y = vowel.test$y, agree=predRf == predGBM)
head(pred)
accuracy <- sum(predRf[pred$agree] == pred$y[pred$agree]) / sum(pred$agree)
accuracy
```
Question 02
-----------
Load the Alzheimer's data using the following commands:
```{r warning=FALSE}
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"). What is the resulting accuracy on the test set? Is it better or worse than each of the individual predictions?
### Answer
Stacked Accuracy: 0.80 is better than random forests and lda and the same as boosting.
#### Explanation
```{r warning=FALSE}
dim(adData)
set.seed(62433)
fitRf <- train(diagnosis ~ ., data = training, method = "rf")
fitGBM <- train(diagnosis ~ ., data = training, method = "gbm")
fitLDA <- train(diagnosis ~ ., data = training, method = "lda")
predRf <- predict(fitRf, testing)
predGBM <- predict(fitGBM, testing)
predLDA <- predict(fitLDA, testing)
pred <- data.frame(predRf, predGBM, predLDA, diagnosis=testing$diagnosis)
# Stack the predictions together using random forests ("rf")
fit <- train(diagnosis ~., data = pred, method = "rf")
predFit <- predict(fit, testing)
c1 <- confusionMatrix(predRf, testing$diagnosis)$overall[1]
c2 <- confusionMatrix(predGBM, testing$diagnosis)$overall[1]
c3 <- confusionMatrix(predLDA, testing$diagnosis)$overall[1]
c4 <- confusionMatrix(predFit, testing$diagnosis)$overall[1]
print(paste(c1, c2, c3, c4))
```
Question 03
-----------
Load the concrete data with the commands:
```{r warning=FALSE}
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.
#### Explanation
```{r warning=FALSE}
set.seed(233)
fit <- train(CompressiveStrength ~ ., data=training, method = "lasso")
fit
plot.enet(fit$finalModel, xvar = "penalty", use.color = T)
```
Question 04
-----------
Load the data on the number of visitors to the instructors blog from here:
[https://d396qusza40orc.cloudfront.net/predmachlearn/gaData.csv](https://d396qusza40orc.cloudfront.net/predmachlearn/gaData.csv)
Using the commands:
```{r warning=FALSE, error=TRUE}
library(lubridate) # For year() function below
download.file("https://d396qusza40orc.cloudfront.net/predmachlearn/gaData.csv", destfile = "C:/Users/Utkarsh/Documents/GitHub/Practical-Machine-Learning-Johns-Hopkins-Bloomberg-School-of-Public-Health-Coursera/Week 04/gaData.csv", method = "wininet")
dat <- read.csv("C:/Users/Utkarsh/Documents/GitHub/Practical-Machine-Learning-Johns-Hopkins-Bloomberg-School-of-Public-Health-Coursera/Week 04/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
96 %.
#### Explanation
```{r warning=FALSE}
library(forecast)
fit <- bats(tstrain)
fit
pred <- forecast(fit, level = 95, h = dim(testing)[1])
names(data.frame(pred))
predComb <- cbind(testing, data.frame(pred))
names(testing)
names(predComb)
predComb$in95 <- (predComb$Lo.95 < predComb$visitsTumblr) & (predComb$visitsTumblr < predComb$Hi.95)
# How many of the testing points is the true value within the 95% prediction interval bounds?
prop.table(table(predComb$in95))[2]
```
Question 05
-----------
Load the concrete data with the commands:
```{r warning=FALSE}
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?
### Answer
6.72
#### Explanation
```{r warning=FALSE}
library(e1071)
set.seed(325)
fit <- svm(CompressiveStrength ~., data = training)
pred <- predict(fit, testing)
acc <- accuracy(pred, testing$CompressiveStrength)
acc
acc[2]
```