library(gamair)
# Load our data
data(chicago)

# Make a scatter-plot of deaths per day versus temperature
  # Many (5114) small filled-in points
plot(death ~ tmpd, data=chicago, pch=16, cex=0.2,
     xlab="Mean daily temperature (Farenheit)",
     ylab="Deaths per day")

# Run a linear regression
death.lm <- lm(death ~ tmpd, data=chicago)
summary(death.lm)

abline(death.lm)

# Exercise 1:
#### How would our prediction for any given day change if the temperture increased
#### by 5 degrees?  Can you give the _average_ change over all days?

library(FNN)
# If the test argument isn't a column matrix, knn.reg() gets unhappy
death.30nn <- knn.reg(train=chicago$tmpd, test=matrix(chicago$tmpd, ncol=1),
                     y=chicago$death, k=30)
# Put temperatures in a nice order for plotting
lines(x=sort(chicago$tmpd), y=death.30nn$pred[order(chicago$tmpd)], lwd=3,
      col="blue")

# Exercise 2:
#### How would our prediction for any given day change if the temperature
#### increased by 5 degrees?  What calculation would you need to do?

