attach(Prestige)
### Simple regression
plot(income, prestige)
lines(lowess(income, prestige,f=0.7,iter=0), lwd=2) # f: the smother span
### Multiple resgression
mod.lo <- loess(prestige~income+education, span=.7, degree=1)
summary(mod.lo)
inc <- seq(min(income), max(income), len=25)
ed <- seq(min(education), max(education), len=25)
# 25*25 combinations of inc and ed
newdata <- expand.grid(income=inc, education=ed)
fit.prestige <- matrix(predict(mod.lo, newdata), 25, 25)
persp(inc, ed, fit.prestige, theta=45, phi=30, ticktype='detailed',
xlab='Income', ylab='Education', zlab='Prestige', expand=2/3, shade=0.5)
### Statistical significance of predictors
mod.lo.inc <- loess(prestige~income, span = 0.7, degree=1)
mod.lo.ed <- loess(prestige~education, spanc=0.7, degree=1)
anova(mod.lo.inc, mod.lo)
anova(mod.lo.ed, mod.lo)
### Smoothing Splines
inc.100 <- seq(min(income), max(income), len=100)
mod.inc <- loess(prestige~income, span=.7, degree=1) # fit local-regression
pres <- predict(mod.inc, data.frame(income=inc.100)) # fitted values
plot(income, prestige)
lines(inc.100, pres, lty=2, lwd=2) # loess curve
lines(smooth.spline(income, prestige, df=3.85), lwd=2) # smoothing spline
### Additive Nonparametric Regression
library(mgcv)
mod.gam <- gam(prestige~s(income)+s(education))
mod.gam
fit.prestige <- matrix(predict(mod.gam, newdata), 25, 25)
persp(inc, ed, fit.prestige, theta=45, phi=30, ticktype='detailed',
xlab='Income', ylab='Education', zlab='Prestige', expand=2/3, shade=0.5)
plot(mod.gam)
# interaction term
mod.gam <- gam(prestige~s(income)+s(education)+s(income, education))
# semi-parametric
mod.gam <- gam(prestige~s(income)+education)
# Generalized Nonparametric Regression
mod.gam <- gam(prestige~s(income)+education, family=poisson)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment