Chapter 16 Solutions to Selected Practice Problems
5.3.2
ggplot(data = alc.dat, aes(x = alc.drinks, y = alc.weights)) + 
  geom_point(col = 'blue', size = 2, shape = "square") + 
  labs(x = "Number of Drinks", 
       y = "Weight")6.7.2
separate(data = data.birds, col = recordingInfo, sep = "-",
     into = c("site", "year", "month", "day"))6.8.7
gm %>% 
  filter(country == 'Afghanistan') %>% 
  select(c("year", "lifeExp")) %>% 
  arrange(desc(lifeExp))6.8.11
iris %>% 
  mutate(s.p.ratio = Sepal.Length / Petal.Length) %>% 
  group_by(Species) %>% 
  summarize(mean.ratio = mean(s.p.ratio)) %>% 
  arrange(desc(mean.ratio))10.2.2
falsePositives <- rep(0, 40)
for (i in 1:length(falsePositives)) {
  knn_Pima <- knn(Pima.tr[,c(2,5)], Pima.te[,c(2,5)], Pima.tr[,8], k = i, prob=TRUE)
  falsePositives[i] <- table(knn_Pima, Pima.te[,8])[2, 1]
}
# Using graphics
plot(falsePositives, pch = 19, las = 1, xlab = "k", ylab = "# of False Positives")
# Using ggplot2
dat <- data.frame(k = 1:40, falsePositives)
ggplot(data = dat, aes(x = k, y = falsePositives)) + 
  geom_point() + 
  labs(y = "# of False Positives")