Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book edits #9

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
.dropbox
code_check*
nohup.out
+*.pdf
*.pdf
3 changes: 1 addition & 2 deletions 01-Introduction/ufo_sightings.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# All rights reserved.

# NOTE: If you are running this in the R console you must use the 'setwd' command to set the
# working directory for the console to whereever you have saved this file prior to running.
# working directory for the console to wherever you have saved this file prior to running.
# Otherwise you will see errors when loading data or saving figures!

# Load libraries and data
Expand Down Expand Up @@ -119,7 +119,6 @@ head(ufo.us)
quick.hist <- ggplot(ufo.us, aes(x = DateOccurred)) +
geom_histogram() +
scale_x_date(breaks = "50 years")

ggsave(plot = quick.hist,
filename = file.path("images", "quick_hist.pdf"),
height = 6,
Expand Down
4 changes: 4 additions & 0 deletions 02-Exploration/chapter02.R
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ logit.model <- glm(Male ~ Weight + Height,

ggplot(heights.weights, aes(x = Height, y = Weight)) +
geom_point(aes(color = Gender, alpha = 0.25)) +
<<<<<<< HEAD
scale_alpha(guide= "none") +
=======
scale_alpha(guide = "none") +
>>>>>>> jmw/master
scale_color_manual(values = c("Male" = "black", "Female" = "gray")) +
theme_bw() +
stat_abline(intercept = -coef(logit.model)[1] / coef(logit.model)[2],
Expand Down
2 changes: 1 addition & 1 deletion 03-Classification/email_classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ex1 <- ggplot(val, aes(x, V2)) +
geom_jitter(aes(shape = as.factor(V3)),
position = position_jitter(height = 2)) +
scale_shape_discrete(guide = "none", solid = FALSE) +
geom_hline(aes(yintercept = c(10,30), linetype = 2)) +
geom_hline(aes(yintercept = c(10,30)), linetype = 2) +
theme_bw() +
xlab("X") +
ylab("Y")
Expand Down
1 change: 1 addition & 0 deletions 06-Regularization/chapter06.R
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ rmse <- function(y, h)
}

# Twenty-second code snippet

library('glmnet')

glmnet.fit <- with(training.df, glmnet(poly(X, degree = 10), Y))
Expand Down
10 changes: 5 additions & 5 deletions 07-Optimization/chapter07.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ a.error <- function(a)
}

# Seventh code snippet
curve(sapply(x, function (a) {a.error(a)}), from = -1000, to = 1000)
curve(sapply(x, function (a) {a.error(a)}), from = -1000, to = 1000, ylab = "Squared Error")

# Eighth code snippet
b.error <- function(b)
{
return(squared.error(heights.weights, 0, b))
}

curve(sapply(x, function (b) {b.error(b)}), from = -1000, to = 1000)
curve(sapply(x, function (b) {b.error(b)}), from = -1000, to = 1000, ylab = "Squared Error")

# Ninth code snippet
ridge.error <- function(heights.weights, a, b, lambda)
Expand Down Expand Up @@ -124,13 +124,13 @@ a.ridge.error <- function(a, lambda)
{
return(ridge.error(heights.weights, a, 0, lambda))
}
curve(sapply(x, function (a) {a.ridge.error(a, lambda)}), from = -1000, to = 1000)
curve(sapply(x, function (a) {a.ridge.error(a, lambda)}), from = -1000, to = 1000, ylab = "Ridge Error")

b.ridge.error <- function(b, lambda)
{
return(ridge.error(heights.weights, 0, b, lambda))
}
curve(sapply(x, function (b) {b.ridge.error(b, lambda)}), from = -1000, to = 1000)
curve(sapply(x, function (b) {b.ridge.error(b, lambda)}), from = -1000, to = 1000, ylab = "Ridge Error")

# Twelfth code snippet
absolute.error <- function(heights.weights, a, b)
Expand All @@ -146,7 +146,7 @@ a.absolute.error <- function(a)
return(absolute.error(heights.weights, a, 0))
}

curve(sapply(x, function (a) {a.absolute.error(a)}), from = -1000, to = 1000)
curve(sapply(x, function (a) {a.absolute.error(a)}), from = -1000, to = 1000, ylab = "Absolute Error")

# Fourteenth code snippet
english.letters <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
Expand Down
43 changes: 22 additions & 21 deletions 09-MDS/chapter09.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ ex.dist
ex.mds <- cmdscale(ex.dist)
plot(ex.mds, type = 'n')
text(ex.mds, c('A', 'B', 'C', 'D'))
grid()

# Seventh code snippet
library('foreign')
Expand Down Expand Up @@ -183,15 +184,15 @@ head(rollcall.mds[[1]])
cong.110 <- rollcall.mds[[9]]

base.110 <- ggplot(cong.110, aes(x = x, y = y)) +
scale_size(to = c(2,2), legend = FALSE) +
scale_alpha(legend = FALSE) + theme_bw() +
opts(axis.ticks = theme_blank(),
axis.text.x = theme_blank(),
axis.text.y = theme_blank(),
title = "Roll Call Vote MDS Clustering for 110th U.S. Senate",
panel.grid.major = theme_blank()) +
scale_size(range = c(2,2), guide="none") +
scale_alpha(guide = "none") + theme_bw() +
theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.grid.major = element_blank()) +
xlab("") +
ylab("") +
ggtitle("Roll Call Vote MDS Clustering for 110th U.S. Senate") +
scale_shape(name = "Party", breaks = c("100", "200", "328"),
labels = c("Dem.", "Rep.", "Ind."), solid = FALSE) +
scale_color_manual(name = "Party", values = c("100" = "black",
Expand All @@ -213,14 +214,14 @@ print(base.110 + geom_text(aes(color = party,
all.mds <- do.call(rbind, rollcall.mds)
all.plot <- ggplot(all.mds, aes(x = x, y = y)) +
geom_point(aes(shape = party, alpha = 0.75, size = 2)) +
scale_size(to = c(2, 2), legend = FALSE) +
scale_alpha(legend = FALSE) +
scale_size(range = c(2, 2), guide="none") +
scale_alpha(guide="none") +
theme_bw() +
opts(axis.ticks = theme_blank(),
axis.text.x = theme_blank(),
axis.text.y = theme_blank(),
title = "Roll Call Vote MDS Clustering for U.S. Senate (101st - 111th Congress)",
panel.grid.major = theme_blank()) +
theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.grid.major = element_blank()) +
ggtitle("Roll Call Vote MDS Clustering for U.S. Senate (101st - 111th Congress)") +
xlab("") +
ylab("") +
scale_shape(name = "Party",
Expand All @@ -243,14 +244,14 @@ for(i in 1:length(rollcall.mds))

# Build base plot
mds.plot <- ggplot(mds, aes(x = x, y = y)) +
scale_size(to = c(2, 2), legend = FALSE) +
scale_alpha(legend = FALSE) +
scale_size(range = c(2, 2), guide="none") +
scale_alpha(guide="none") +
theme_bw() +
opts(axis.ticks = theme_blank(),
axis.text.x = theme_blank(),
axis.text.y = theme_blank(),
title = plot.title,
panel.grid.major = theme_blank()) +
theme(axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
panel.grid.major = element_blank()) +
ggtitle(plot.title) +
xlab("") +
ylab("")

Expand Down
4 changes: 2 additions & 2 deletions 11-SNA/02_twitter_net.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ user.net <- set.vertex.attribute(user.net, "Label", value = get.vertex.attribut

# Next, extract the 2-core, and remove pendants generated as a result
user.cores <- graph.coreness(user.net, mode = "in")
user.clean <- subgraph(user.net, which(user.cores > 1) - 1)
user.clean <- induced.subgraph(user.net, which(user.cores > 1) - 1)

# Finally, extract ego.net
user.ego <- subgraph(user.net, c(0, neighbors(user.net, user, mode = "out")))
user.ego <- induced.subgraph(user.net, c(0, neighbors(user.net, user, mode = "out")))

# Add hierarchical clustering data to network
user.sp <- shortest.paths(user.ego)
Expand Down
3 changes: 2 additions & 1 deletion package_installer.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File-Name: package_installer.R
# Name: package_installer.R
# Date: 2012-02-10
# Author: Drew Conway (drew.conway@nyu.edu)
# Purpose: Install all of the packages needed for the Machine Learning for Hackers case studies
Expand Down Expand Up @@ -45,3 +45,4 @@ for(p in cran.packages) {

print("### All required packages installed ###")