Created
February 11, 2022 13:05
-
-
Save florianhartig/e6019a35ece02ae9bea2146cc8a5a5d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 50 | |
x = seq(-1,1, len = n) | |
f = function(x) 0.3 * x^2 | |
y = f(x) + rnorm(n, sd = 0.2) | |
par(mfrow = c(1,2)) | |
lmFit <- lm(y ~ x + I(x^2)) | |
plot(x,y, main = "linearRegression") | |
curve(f, -1, 1, add = T) | |
pred = predict(lmFit, interval = "confidence") | |
lines(x, pred[,1], col = 2, lwd = 2) | |
polygon( c(x, rev(x)), | |
c(pred[,2], rev(pred[,3])), | |
border = F, col = "#FF000022" ) | |
pred = predict(lmFit, interval = "predict") | |
lines(x, pred[,2], lty = 5, col = 2) | |
lines(x, pred[,3], lty = 5, col = 2) | |
library(mgcv) | |
gamFit <- gam(y ~ s(x)) | |
plot(x,y, main = "GAM") | |
curve(f, -1, 1, add = T) | |
pred <- predict(gamFit, se.fit = T) | |
lines(x, pred$fit, col = "green", lwd = 2) | |
polygon( c(x, rev(x)), | |
c(pred$fit + 1.96 * pred$se.fit, rev(pred$fit - 1.96 * pred$se.fit)), | |
border = F, col = "#00FF0022" ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment