Skip to content

Instantly share code, notes, and snippets.

@JWiley
Created December 2, 2010 19:23
Show Gist options
  • Save JWiley/725884 to your computer and use it in GitHub Desktop.
Save JWiley/725884 to your computer and use it in GitHub Desktop.
dfm <- data.frame(y = c(30, 43, 36, 33, 50, 45, 40) +
scale(rnorm(7, 2, 5), scale = FALSE), x = 1:7)
dalt <- data.frame(x = seq(1, 7, length.out = 100))
m <- lapply(1:6, function(i) {lm(y ~ poly(x = x, degree = i), data = dfm)})
pdf(file = "tmp.pdf", height = 11, width = 8.5, onefile = TRUE)
par(mfrow = c(3, 2))
for(i in as.numeric(1:6)) {
plot(y ~ x, data = dfm, main =
substitute("Polynomial Trend Line: degree " == x, list(x = i)),
pch = 16, cex = 1.2, ylim = range(dfm$y)*c(.9, 1.1))
points(x = 1:7, y = predict(m[[i]], data.frame(x = 1:7)),
type = "l", col = "plum4", lwd = 1.2)
points(x = dalt$x, y = predict(m[[i]], dalt),
type = "l", col = "lightslategrey", lwd = .2)
text(x = 5, y = 35, labels =
substitute(R^2 == x, list(x = round(summary(m[[i]])$r.squared, digits = 3))))
}
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment