Skip to content

Instantly share code, notes, and snippets.

@timcdlucas
Created July 9, 2019 08:33
Show Gist options
  • Save timcdlucas/942ed44b973299c3b39a38bd19654a45 to your computer and use it in GitHub Desktop.
Save timcdlucas/942ed44b973299c3b39a38bd19654a45 to your computer and use it in GitHub Desktop.
Orthogonal polygnomials
library(ggplot2)
set.seed(1001)
start <- 2
end <- 20
N <- 100
x <- runif(N, start, end)
y <- x + x^2 + rnorm(N, 0, 5)
d <- data.frame(y, x)
plot(x, y)
m1 <- lm(y ~ x + I(x^2), d)
predN <- 1000
pred1 <- data.frame(x = seq(0, end, length.out = predN),
y1 = predict(m1, newdata = data.frame(x = seq(0, end, length.out = predN))))
ggplot(d, aes(x, y)) +
geom_point() +
geom_line(data = pred1, aes(x, y1))
m2 <- lm(y ~ poly(x, 2), d)
pred2 <- data.frame(x = seq(0, end, length.out = predN),
y1 = predict(m2, newdata = data.frame(x = seq(0, end, length.out = predN))))
ggplot(d, aes(x, y)) +
geom_point() +
geom_line(data = pred2, aes(x, y1))
summary(m1)
summary(m2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment