[R] GLM vs GAM
Ehsan Karim
wildscop at hotmail.com
Fri Mar 14 04:43:56 CET 2014
Dear R-list,
I am wondering whether anyone could explain what'd be the difference between running a 'generalized additive regression' versus 'generalized linear regression' with splines.
Are they same models theoretically? My apologies if this is a silly question. Any comments or direction to references will be highly appreciated.
Thanks in advance,
Ehsan
#####################
set.seed(545)
require(mgcv)
n <- 200
x1 <- c(rnorm(n), 1+rnorm(n))
x2 <- sqrt(c(rnorm(n,4),rnorm(n,6)))
y <- c(rep(0,n), rep(1,n))
#####################
# GAM version
#####################
r1 <- gam(y~s(x1, bs = "cr")+s(x2, bs = "cr"),family=binomial)
pr1 <- predict(r1, type='response')
summary(pr1)
hist(pr1)
#####################
# GLM version
#####################
r2 <- glm(y~ns(x1)+ns(x2),family=binomial)
pr2 <- predict(r2, type='response')
summary(pr2)
hist(pr2)
#####################
# Results
#####################
> summary(pr1)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000394 0.0550200 0.5027000 0.5000000 0.9322000 1.0000000
> summary(pr2)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000403 0.0573300 0.5229000 0.5000000 0.9159000 0.9992000
More information about the R-help
mailing list