Skip to content

Instantly share code, notes, and snippets.

@smbache
Last active April 30, 2019 18:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smbache/345ec35bea3a104bc3b4 to your computer and use it in GitHub Desktop.
Save smbache/345ec35bea3a104bc3b4 to your computer and use it in GitHub Desktop.
library(dplyr) # These two need that it is magrittr@dev that is installed.
library(magrittr) # i.e. the dev branch @ GitHub, not master.
library(ggplot2)
library(mgcv)
"http://zevross.com/blog/wp-content/uploads/2014/08/chicago-nmmaps.csv" %>%
read.csv(as.is = TRUE) %>%
mutate(date = as.Date(date)) %>%
filter(date > as.Date("1996-12-31")) %>%
mutate(year = substring(date, 1, 4)) %T>%
{
thegam <<- gam(o3 ~ s(temp), data = .) %T>%
plot(residuals=TRUE, main="Yuck, not a nice plot")
} %T>%
{
t <- data.frame(temp = seq(min(.$temp), max(.$temp), length = 300))
predict(
thegam
, type = "terms"
, newdata = t
, se.fit = TRUE
) %T>%
{
plot(t$temp, .$fit, type = "n", lwd = 3, xlim = c(-3, 90), ylim = c(-20, 30),
main = "Ahhh, definitely better",
ylab = paste("s(temp,", round(sum(thegam$edf[-1]),2), ")", sep = ""))
} %T>%
{
polygon(c(t$temp, rev(t$temp)),
c(.$fit + 1.96*.$se.fit, rev(.$fit - 1.96*.$se.fit)),
col = "grey", border = NA)
} %T>%
{
lines(t$temp, .$fit, lwd = 2)
}
} %T>%
{ origdata;
predict(thegam, type = "terms") + residuals(thegam) %T>%
points(origdata$temp, ., pch = 16, col = rgb(0, 0, 1, 0.25))
{ rug(.$temp) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment