Example weather forecast
data from: http://www.weather.uwaterloo.ca/data.html#archive
# #read waterloo temperature data
# files <- dir("data", full.names = TRUE)
# l <- list()
# for (i in seq_along(files)) {
# x <- read.csv(files[i])
# x$index <- 1:nrow(x)
# x$year <- i
# l[[i]] <- x
# }
# x <- do.call(rbind, l)
# x <- subset(x, index <= 365) # remove day 366
x <- read.csv("data/Daily_summary_2007.csv")
x$index <- 1:nrow(x)
plot(x$index, x$High.temperature, pch=16, cex=.5)

Fitting
degree = 1:12
mm = list()
m.error = rep(NA, 10)
for (d in degree) {
mm[[d]] <- lm(High.temperature ~ poly(index, d), data=x)
m.error[d] = mean(residuals(mm[[d]])^2)
}
ps = c(3,12)
plot(x$index, x$High.temperature, pch=16, cex=.5, las=1)
lines(x$index, predict(mm[[ps[1]]]), col="red", lwd=2)
lines(x$index, predict(mm[[ps[2]]]), col="blue", lwd=2)
legend("topleft", legend = paste(c("Polynomial of order"), ps),
fill = c("red", "blue"), cex = .7)

x <- read.csv("data/Daily_summary_2007.csv")
x$index <- 1:nrow(x)
x2 <- read.csv("data/Daily_summary_2009.csv")
x2$index <- 1:nrow(x)
plot(x$index, x$High.temperature, pch=16, cex=.5, las=1)
points(x2$index, x2$High.temperature, pch=16, cex=.5, col="red")

m.pred.error = NA
m.error = NA
cv.error = NA
loocv.error = NA
degree = 1:20
k=5
for (d in degree) {
m <- glm(High.temperature ~ poly(index, d), data=x)
m.error[d] = mean(residuals(m)^2)
cv.error[d] = cv.glm(x, m, K = k)$delta[1]
loocv.error[d] = loocv(m)
m.pred.error[d] = mean((x2$High.temperature - predict(m, newdata=x2))^2)
}
# error by polynomial
plot(degree, m.error, type="l", col="darkgreen", las=1, xlim=c(2, 20))
points(degree, m.error, pch=16, col="darkgreen")
lines(degree, m.pred.error, col="red")
points(degree, m.pred.error, pch=16, col="red")
lines(degree, cv.error, col="blue")
points(degree, cv.error, pch=16, col="blue")
lines(degree, loocv.error, col="brown")
points(degree, loocv.error, pch=16, col="brown")
