Commit 782a907a authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+105 −0
Changes for MasteringMachineLearningwithRSecondEdition_Code/AppendixA/codeAppendix.R: 105 added lines, 0 removed lines.
Original line number Diff line number Diff line
install.packages("abc")

#....
"Let's Go Sioux!"
15
((22+5)/9)*2
c(0,1,1,2,3,5,8,13,21,34) #fibonnaci sequence 
x <- c(0,1,1,2,3,5,8,13,21,34) 
x
x[1:3]
x[-5:-6]
plot(x)
plot(x, main = "Fibonacci Sequence", xlab = "Order", ylab = "Value")
y <- sqrt(x)
y
?sqrt
plot(x, y)
z <- 3
x2 <- x * z
x2
#Boolean
5 < 6 
6 < 5
x == 0

rep("North Dakota Hockey, 2016 NCAA Division 1 Champions", times=3)
seq(0, 10, by = 2)
p <- seq(1:3)
p
q = seq(1, 2, by = 0.5)
q
r <- rbind(p,q)
r
str(r)
s <- cbind(p, q)
s
s <- data.frame(s)
str(s)
names(s) <- c("column 1", "column 2")
s
t <- as.matrix(s)
t
t[1, 1]
t[, 2]
t[1:2, ]
new <- old[1:70, c(1, 3, 7:10)]
new = old[, -1]

#summary stats
a <- c(1, 2, 3, NA)
sum(a)
sum(a, na.rm = TRUE)
data <- c(4, 3, 2, 5.5, 7.8, 9, 14, 20)
mean(data)
median(data)
sd(data)
max(data)
min(data)
range(data)
quantile(data)
barplot(data)
abline(h = mean(data))
abline(h = median(data), lty = 2)

set.seed(1)
norm = rnorm(100) 
plot(norm)
hist(norm)

# tidyverse
library(dplyr)
summarize(group_by(iris, Species), average = mean(Sepal.Length))

iris %>% group_by(Species) %>% summarize(average = mean(Sepal.Length))

distinct(iris, Species)

count(iris, Species)

df <- filter(iris, Sepal.Width > 3.5)
df <- arrange(iris, desc(Petal.Length))
head(df)

iris2 <- select(iris, starts_with("Se"))
iris3 <- select(iris, -starts_with("Se"))

theIris <- bind_cols(iris2, iris3)
head(theIris)
head(iris)

summarize(iris, n_distinct(Sepal.Width))

dedupe <- iris %>% distinct(Sepal.Width, .keep_all = T)
str(dedupe)










          
 No newline at end of file
+108 −0
Changes for MasteringMachineLearningwithRSecondEdition_Code/Chapter02/code2.R: 108 added lines, 0 removed lines.
Original line number Diff line number Diff line
#call up and explore the data
data(anscombe)
attach(anscombe)
anscombe
cor(x1, y1) #correlation of x1 and y1
cor(x2, y1) #correlation of x2 and y2
par(mfrow = c(2,2)) #create a 2x2 grid for plotting
plot(x1, y1, main = "Plot 1")
plot(x2, y2, main = "Plot 2")
plot(x3, y3, main = "Plot 3")
plot(x4, y4, main = "Plot 4")
install.packages("alr3")
library(alr3)
data(snake)
dim(snake)
head(snake)
names(snake) = c("content", "yield")
attach(snake) #reattach data with new names
head(snake)
#produce a scatterplot
plot(content, yield, xlab = "water content of snow", ylab = "water yield",
     main="Scatterplot of Snow vs. Yield")
#build a linear model
yield.fit <- lm(yield ~ content)
summary(yield.fit)
plot(content, yield)
abline(yield.fit, lwd=3, col = "black")
par(mfrow = c(2,2))
plot(yield.fit)
qqPlot(yield.fit)


data(water)
dim(water)
str(water)
head(water)
socal.water <- water[ ,-1]
head(socal.water)
library(corrplot)
water.cor <- cor(socal.water)
water.cor
corrplot(water.cor, method = "ellipse")

pairs(~ ., data = socal.water)

library(leaps)
fit <- lm(BSAAM ~ ., data = socal.water)
summary(fit)
sub.fit <- regsubsets(BSAAM ~ ., data = socal.water)
best.summary <- summary(sub.fit)
names(best.summary)
which.min(best.summary$rss)
par(mfrow = c(1,2))
plot(best.summary$cp, xlab = "number of features", ylab = "cp")
plot(sub.fit, scale = "Cp")
which.min(best.summary$bic)
which.max(best.summary$adjr2)



best.fit <- lm(BSAAM ~ APSLAKE + OPRC + OPSLAKE, data = socal.water)
summary(best.fit)
par(mfrow = c(2,2))
plot(best.fit)
vif(best.fit)
plot(socal.water$OPRC, socal.water$OPSLAKE, xlab = "OPRC", ylab = "OPSLAKE")
best.summary$adjr2
fit.2 <- lm(BSAAM ~ APSLAKE + OPSLAKE, data = socal.water)
summary(fit.2)
par(mfrow=c(2,2))
plot(fit.2)
vif(fit.2)
library(lmtest)
bptest(fit.2)
plot(fit.2$fitted.values, socal.water$BSAAM, xlab = "predicted", ylab = "actual", 
     main = "Predicted vs. Actual")



#ggplot2 for pretty picture
socal.water["Actual"] = water$BSAAM
socal.water$Forecast = predict(fit.2)
head(socal.water)
library(ggplot2)
ggplot(socal.water, aes(x = Forecast, y = Actual)) +
    geom_point() + geom_smooth(method = lm) +
    labs(title = "Forecast versus Actuals")

library(MPV)
PRESS(best.fit)
PRESS(fit.2)
PRESS.best = sum((resid(best.fit)/(1 - hatvalues(best.fit)))^2)
PRESS.fit.2 = sum((resid(fit.2)/(1 - hatvalues(fit.2)))^2)
PRESS.best
PRESS.fit.2

library(ISLR)
data(Carseats)
str(Carseats)
sales.fit = lm(Sales ~ Advertising + ShelveLoc, data = Carseats)
summary(sales.fit)
contrasts(Carseats$ShelveLoc)

library(MASS)
data(Boston)
str(Boston)
value.fit <- lm(medv ~ lstat * age, data = Boston)
summary(value.fit)
+133 −0
Changes for MasteringMachineLearningwithRSecondEdition_Code/Chapter03/code3.R: 133 added lines, 0 removed lines.
Original line number Diff line number Diff line
library(MASS)
data(biopsy)
str(biopsy)
biopsy$ID = NULL
names(biopsy) = c("thick", "u.size", "u.shape", "adhsn", 
                  "s.size", "nucl", "chrom", "n.nuc", "mit", "class")
names(biopsy)
biopsy.v2 <- na.omit(biopsy)
y <- ifelse(biopsy.v2$class == "malignant", 1, 0)
library(reshape2)
library(ggplot2)
biop.m <- melt(biopsy.v2, id.var = "class")
ggplot(data = biop.m, aes(x = class, y = value)) + 
  geom_boxplot() +
  facet_wrap(~ variable, ncol = 3)
library(corrplot)
bc <- cor(biopsy.v2[ ,1:9]) #create an object of the features
corrplot.mixed(bc)

set.seed(123) #random number generator
ind <- sample(2, nrow(biopsy.v2), replace = TRUE, prob = c(0.7, 0.3))
train <- biopsy.v2[ind==1, ] #the training data set
test <- biopsy.v2[ind==2, ] #the test data set
str(test) #confirm it worked
table(train$class)
table(test$class)

full.fit <- glm(class ~ ., family = binomial, data = train)
summary(full.fit)
confint(full.fit)
exp(coef(full.fit))
library(car)
vif(full.fit)

train.probs <- predict(full.fit, type = "response")
train.probs[1:5] #inspect the first 5 predicted probabilities
contrasts(train$class)

library(InformationValue)
trainY <- y[ind==1]
testY <- y[ind==2]
confusionMatrix(trainY, train.probs)
# optimalCutoff(trainY, train.probs)
misClassError(trainY, train.probs)
confusionMatrix(trainY, train.probs)

test.probs <- predict(full.fit, newdata = test, type = "response")
misClassError(testY, test.probs)
confusionMatrix(testY, test.probs)
####

####

library(bestglm)
X <- train[, 1:9]
Xy <- data.frame(cbind(X, trainY))
bestglm(Xy = Xy, IC = "CV", CVArgs = list(Method = "HTF", K = 10, REP = 1), 
        family=binomial)
reduce.fit <- glm(class ~ thick + u.size + nucl, family = binomial, data = train)

test.cv.probs = predict(reduce.fit, newdata = test, type = "response")
misClassError(testY, test.cv.probs)
confusionMatrix(testY, test.cv.probs)

bestglm(Xy = Xy, IC = "BIC", family = binomial)
bic.fit <- glm(class ~ thick + adhsn + nucl + n.nuc, 
               family = binomial, data = train)
test.bic.probs = predict(bic.fit, newdata = test, type = "response")
misClassError(testY, test.bic.probs)
confusionMatrix(testY, test.bic.probs)
####

lda.fit <- lda(class ~ ., data = train)
lda.fit
plot(lda.fit, type="both")
train.lda.probs <- predict(lda.fit)$posterior[, 2]
misClassError(trainY, train.lda.probs)
confusionMatrix(trainY, train.lda.probs)

test.lda.probs <- predict(lda.fit, newdata = test)$posterior[, 2]
misClassError(testY, test.lda.probs)
confusionMatrix(testY, test.lda.probs)

qda.fit <- qda(class ~ ., data =train)
qda.fit
train.qda.probs <- predict(qda.fit)$posterior[, 2]
misClassError(trainY, train.qda.probs)
confusionMatrix(trainY, train.qda.probs)

test.qda.probs <- predict(qda.fit, newdata = test)$posterior[, 2]
misClassError(testY, test.qda.probs)
confusionMatrix(testY, test.qda.probs)
## MARS###########################################
library(earth)
set.seed(1)
earth.fit <- earth(class ~ ., data = train,
                   pmethod = "cv",
                   nfold = 5,
                   ncross = 3,
                   degree = 1,
                   minspan = -1,
                   glm=list(family=binomial)
                   )
summary(earth.fit)
plotmo(earth.fit)
plotd(earth.fit)
evimp(earth.fit)

test.earth.probs <- predict(earth.fit, newdata = test, type = "response")
misClassError(testY, test.earth.probs)
confusionMatrix(testY, test.earth.probs)
##################################################
library(ROCR)
bad.fit <- glm(class ~ thick, family = binomial, data = train)
test.bad.probs <- predict(bad.fit, newdata = test, type = "response") #save probabilities
pred.full <- prediction(test.probs, test$class)
perf.full <- performance(pred.full, "tpr", "fpr")
plot(perf.full, main = "ROC", col = 1)
pred.bic <- prediction(test.bic.probs, test$class)
perf.bic <- performance(pred.bic, "tpr", "fpr")
plot(perf.bic, col = 2, add = TRUE)
pred.bad <- prediction(test.bad.probs, test$class)
perf.bad <- performance(pred.bad, "tpr", "fpr")
plot(perf.bad, col = 3, add = TRUE)
pred.earth <- prediction(test.earth.probs, test$class)
perf.earth <- performance(pred.earth, "tpr", "fpr")
plot(perf.earth, col = 4, add = TRUE)
legend(0.6, 0.6, c("FULL", "BIC", "BAD", "EARTH"), 1:4)
performance(pred.full, "auc")@y.values
performance(pred.bic, "auc")@y.values
performance(pred.bad, "auc")@y.values
performance(pred.earth, "auc")@y.values
+139 −0
Changes for MasteringMachineLearningwithRSecondEdition_Code/Chapter04/code4.R: 139 added lines, 0 removed lines.
Original line number Diff line number Diff line
library(ElemStatLearn) #contains the data
library(car) #package to calculate Variance Inflation Factor
library(corrplot) #correlation plots
library(leaps) #best subsets regression
library(glmnet) #allows ridge regression, LASSO and elastic net
library(caret) #this will help identify the appropriate parameters

data(prostate)
str(prostate)
plot(prostate)
plot(prostate$gleason, ylab = "Gleason Score")
table(prostate$gleason)
boxplot(prostate$lpsa ~ prostate$gleason, xlab = "Gleason Score", 
        ylab = "Log of PSA")

prostate$gleason <- ifelse(prostate$gleason == 6, 0, 1)
table(prostate$gleason)

p.cor = cor(prostate)
corrplot.mixed(p.cor)

train <- subset(prostate, train == TRUE)[, 1:9]
str(train)
test = subset(prostate, train==FALSE)[,1:9]
str(test)

subfit <- regsubsets(lpsa ~ ., data = train)
b.sum <- summary(subfit)
which.min(b.sum$bic)
plot(b.sum$bic, type = "l", xlab = "# of Features", ylab = "BIC", 
     main = "BIC score by Feature Inclusion")
plot(subfit, scale = "bic", main = "Best Subset Features")

ols <- lm(lpsa ~ lcavol + lweight + gleason, data = train)
plot(ols$fitted.values, train$lpsa, xlab = "Predicted", ylab = "Actual", 
     main = "Predicted vs Actual")
###
pred.subfit = predict(ols, newdata=test)
plot(pred.subfit, test$lpsa , xlab = "Predicted", 
     ylab = "Actual", main = "Predicted vs Actual")
resid.subfit = test$lpsa - pred.subfit
mean(resid.subfit^2)

x <- as.matrix(train[, 1:8])
y <- train[, 9]
ridge <- glmnet(x, y, family = "gaussian", alpha = 0)
print(ridge)
plot(ridge, label = TRUE)
plot(ridge, xvar = "lambda", label = TRUE)
ridge.coef <- coef(ridge, s=0.1, exact=TRUE)
ridge.coef
plot(ridge, xvar = "dev", label = TRUE)

newx <- as.matrix(test[, 1:8])
ridge.y = predict(ridge, newx = newx, type = "response", s=0.1)
plot(ridge.y, test$lpsa, xlab = "Predicted", 
     ylab = "Actual", main = "Ridge Regression")
ridge.resid <- ridge.y - test$lpsa 
mean(ridge.resid^2)

lasso <- glmnet(x, y, family = "gaussian", alpha = 1)
print(lasso)
plot(lasso, xvar = "lambda", label = TRUE)
lasso.coef <- coef(lasso, s = 0.045, exact = TRUE)
lasso.coef
lasso.y <- predict(lasso, newx = newx, 
                   type = "response", s = 0.045)
plot(lasso.y, test$lpsa, xlab = "Predicted", ylab = "Actual", 
     main = "LASSO")
lasso.resid <- lasso.y - test$lpsa
mean(lasso.resid^2)

grid <- expand.grid(.alpha = seq(0,1, by=.2), 
                    .lambda = seq(0.00, 0.2, by = 0.02))
table(grid)
head(grid)
control <- trainControl(method = "LOOCV") #selectionFunction="best"
set.seed(701) #our random seed
enet.train = train(lpsa ~ ., data = train, 
                   method = "glmnet", 
                   trControl = control, 
                   tuneGrid = grid)
enet.train

enet <- glmnet(x, y,family = "gaussian", 
               alpha = 0, 
               lambda = .08)
enet.coef <- coef(enet, s = .08, exact = TRUE)
enet.coef
enet.y <- predict(enet, newx = newx, type = "response",  s= .08)
plot(enet.y, test$lpsa, xlab = "Predicted", 
     ylab = "Actual", main = "Elastic Net")
enet.resid <- enet.y - test$lpsa
mean(enet.resid^2)

set.seed(317)
lasso.cv = cv.glmnet(x, y, nfolds = 3)
plot(lasso.cv)
lasso.cv$lambda.min #minimum
lasso.cv$lambda.1se #one standard error away
coef(lasso.cv, s = "lambda.1se")
lasso.y.cv = predict(lasso.cv, newx=newx, type = "response", 
                     s = "lambda.1se")
lasso.cv.resid = lasso.y.cv - test$lpsa
mean(lasso.cv.resid^2)

library(MASS)
biopsy$ID = NULL
names(biopsy) = c("thick", "u.size", "u.shape", "adhsn",
                    + "s.size", "nucl", "chrom", "n.nuc", "mit", "class")
biopsy.v2 <- na.omit(biopsy)
set.seed(123) #random number generator
ind <- sample(2, nrow(biopsy.v2), replace = TRUE, prob = c(0.7, 0.3))
train <- biopsy.v2[ind==1, ] #the training data set
test <- biopsy.v2[ind==2, ] #the test data set

x <- as.matrix(train[, 1:9])
y <- train[, 10]

set.seed(3)
fitCV <- cv.glmnet(x, y, family = "binomial",
                   type.measure = "auc",
                   nfolds = 5)
plot(fitCV)
fitCV$lambda.1se
coef(fitCV, s = "lambda.1se")

library(InformationValue)
predCV <- predict(fitCV, newx = as.matrix(test[, 1:9]),
                  s = "lambda.1se",
                  type = "response")
actuals <- ifelse(test$class == "malignant", 1, 0)
misClassError(actuals, predCV)
plotROC(actuals, predCV)

predCV.min <- predict(fitCV, newx = as.matrix(test[, 1:9]),
                  s = "lambda.min",
                  type = "response")
misClassError(actuals, predCV.min)
+126 −0
Changes for MasteringMachineLearningwithRSecondEdition_Code/Chapter05/code5.R: 126 added lines, 0 removed lines.
Original line number Diff line number Diff line
library(class)
library(kknn)
library(e1071)
library(kernlab)
library(caret)
library(MASS)
library(reshape2)
library(ggplot2)
library(pROC)

data(Pima.tr)
str(Pima.tr)
data(Pima.te)
str(Pima.te)
pima <- rbind(Pima.tr, Pima.te)
str(pima)

pima.melt <- melt(pima, id.var = "type")
ggplot(data = pima.melt, aes(x = type, y = value)) +
  geom_boxplot() + facet_wrap(~ variable, ncol = 2)
pima.scale <- data.frame(scale(pima[, -8]))
#scale.pima = as.data.frame(scale(pima[,1:7], byrow=FALSE)) #do not create own function
str(pima.scale)
pima.scale$type <- pima$type

pima.scale.melt <- melt(pima.scale, id.var = "type")
ggplot(data=pima.scale.melt, aes(x = type, y = value)) + 
  geom_boxplot() + facet_wrap(~ variable, ncol = 2)
cor(pima.scale[-8])
table(pima.scale$type)
set.seed(502)
ind <- sample(2, nrow(pima.scale), replace = TRUE, prob = c(0.7, 0.3))
train <- pima.scale[ind == 1, ]
test <- pima.scale[ind == 2, ]
str(train)
str(test)

grid1 <- expand.grid(.k = seq(2, 20, by = 1))
control = trainControl(method = "cv")
set.seed(123)
knn.train <- train(type ~ ., data = train, 
                   method = "knn", 
                   trControl = control, 
                   tuneGrid = grid1)
knn.train

knn.test <- knn(train[, -8], test[, -8], train[, 8], k = 17)
table(knn.test, test$type)
(77+28)/147
#calculate Kappa
prob.agree <- (77+28)/147
prob.chance <- ((77+26)/147) * ((77+16)/147)
prob.chance
kappa <- (prob.agree - prob.chance) / (1 - prob.chance)
kappa

set.seed(123)
kknn.train <- train.kknn(type ~ ., data = train, 
                         kmax = 25, distance = 2, 
                         kernel = c("rectangular", "triangular", "epanechnikov"))
plot(kknn.train)
kknn.train
kknn.pred <- predict(kknn.train, newdata = test)
table(kknn.pred, test$type)

#linear tune
set.seed(123)
linear.tune <- tune.svm(type ~ ., data = train, 
                        kernel = "linear", 
                        cost = c(0.001, 0.01, 0.1, 1, 5, 10))
summary(linear.tune)
best.linear <- linear.tune$best.model
tune.test <- predict(best.linear, newdata = test)
table(tune.test, test$type)
(82+30)/147

#SVM with e1071; tune the poly only
set.seed(123)
poly.tune <- tune.svm(type ~ ., data = train, 
                      kernel = "polynomial", 
                      degree = c(3, 4, 5), 
                      coef0 = c(0.1, 0.5, 1, 2, 3, 4))
summary(poly.tune)
best.poly <- poly.tune$best.model
poly.test <- predict(best.poly, newdata = test)
table(poly.test, test$type)
(81 + 26) / 147

#tune the rbf
set.seed(123)
rbf.tune <- tune.svm(type ~ ., data = train, 
                     kernel = "radial", 
                     gamma = c(0.1, 0.5, 1, 2, 3, 4))
summary(rbf.tune)
best.rbf <- rbf.tune$best.model
rbf.test <- predict(best.rbf, newdata = test)
table(rbf.test, test$type)
(73+21)/147

#tune the sigmoid
set.seed(123)
sigmoid.tune <- tune.svm(type ~ ., data = train, 
                         kernel = "sigmoid", 
                         gamma = c(0.1, 0.5, 1, 2, 3, 4),
                         coef0 = c(0.1, 0.5, 1, 2, 3, 4))
summary(sigmoid.tune)
best.sigmoid <- sigmoid.tune$best.model
sigmoid.test <- predict(best.sigmoid, newdata = test)
table(sigmoid.test, test$type)
(82+35)/147

confusionMatrix(sigmoid.test, test$type, positive = "Yes")
confusionMatrix(tune.test, test$type, positive = "Yes")

set.seed(123)
rfeCNTL <- rfeControl(functions = lrFuncs, method = "cv", number = 10)
svm.features <- rfe(train[, 1:7], train[, 8],
                   sizes = c(7, 6, 5, 4), 
                   rfeControl = rfeCNTL, 
                   method = "svmLinear")
svm.features
svm.5 <- svm(type ~ glu + ped + npreg + bmi + age, 
             data = train, 
             kernel = "linear")
svm.5.predict = predict(svm.5, newdata=test[c(1,2,5,6,7)])
table(svm.5.predict, test$type)
Loading