Multiple Linear Regression

advertisement
#################################
## Lecture 4 Linear models III ##
##
Multiple Regression
##
#################################
################
## SLIDES 3-7 ##
################
library(faraway)
data(seatpos)
attach(seatpos)
names(seatpos)
summary(lm(hipcenter
summary(lm(hipcenter
summary(lm(hipcenter
summary(lm(hipcenter
summary(lm(hipcenter
summary(lm(hipcenter
summary(lm(hipcenter
summary(lm(hipcenter
~
~
~
~
~
~
~
~
Age))
Weight))
HtShoes))
Ht))
Seated))
Arm))
Thigh))
Leg))
summary(mod <- lm(hipcenter ~ Ht + Leg))
drop1(mod, test="F")
cor(Ht,Leg)
plot(Ht ~ Leg)
#############
## SLIDE 8 ##
#############
add1( lm(hipcenter~Ht),
~. +Age +Weight +HtShoes +Seated +Arm +Thigh +Leg,
test="F")
##################
## SLIDES 13-19 ##
##################
mod <- lm(hipcenter ~. , data=seatpos)
# starting with every variable in the data.frame
s.mod <- step(mod)
summary(s.mod)
anova(s.mod)
drop1(s.mod, test="F")
plot(hipcenter ~ s.mod$fit)
abline(0,1, col="red", lwd=2)
##############
## SLIDE 19 ##
##############
null <- lm(hipcenter ~1, data=seatpos)
## starting from the null model (only an intercept)
s.mod <- step(null,
~. +Age+Weight+Ht+HtShoes+Seated+Arm+Thigh+Leg,
direction="forward")
#################################
## Lecture 4 Linear models III ##
##
Polynomial Regression
##
#################################
##################
## SLIDES 20-23 ##
##################
ozone.pollution <- read.table("ozone.data.txt", header=T)
## in datasets folder on t:drive
dim(ozone.pollution)
names(ozone.pollution)
attach(ozone.pollution)
pairs(ozone.pollution,
panel=panel.smooth,
pch=16, lwd=2)
model <- lm(ozone ~ ., data=ozone.pollution)
summary(model)
drop1(model, test="F")
plot(ozone ~ model$fitted, pch=16,
xlab="Model Predictions",
ylab="Ozone Concentration")
abline(0,1, col="red", lwd=2)
shapiro.test(model$res)
plot(model, which=1)
library(car)
cr.plot(model, rad, pch=16, main="")
cr.plot(model, temp, pch=16, main="")
cr.plot(model, wind, pch=16, main="")
##################
## SLIDES 24-25 ##
##################
model2 <- lm(ozone ~ poly(rad,2)+poly(temp,2)+poly(wind,2))
summary(model2)
extractAIC(model2) ## Simplified AIC Version
AIC(model2) ## Exact AIC Version (BEWARE !!)
model3 <- lm(ozone ~ rad +poly(temp,2) +poly(wind,2))
extractAIC(model3)
model4 <- lm(ozone ~ rad+poly(temp,3)+poly(wind,2) )
extractAIC(model4)
model5 <- lm(ozone ~ rad+poly(temp,2)+poly(wind,3) )
extractAIC(model5)
model6 <- lm(ozone ~ rad+poly(temp,3)+poly(wind,3) )
extractAIC(model6)
extractAIC(model)
## original, strictly linear model
##############
## SLIDE 26 ##
##############
plot(model4, which=1)
shapiro.test(model4$res)
plot(model4, which=2)
##################
## SLIDES 27-29 ##
##################
library(MASS)
boxcox(model4, plotit=T)
boxcox(model4, plotit=T,
lambda=seq(0,1,by=.1) )
new.ozone <- ozone^(1/3)
mod4 <- lm(new.ozone ~
rad +poly(temp,3) +poly(wind,2) )
extractAIC(mod4)
## YOU COULD CHECK THAT mod4 IS INDEED THE
## BEST POLYNOMIAL MODEL, AS ON SLIDE 25
shapiro.test(mod4$res)
par(mfrow=c(2,2))
plot(mod4)
anova(mod4)
summary(mod4)
par(mfrow=c(1,1))
plot(new.ozone ~ mod4$fitted)
abline(0,1, col="red", lwd=2)
par(mfrow=c(1,3))
cr.plot(mod4, rad, pch=16, main="")
cr.plot(mod4, poly(temp,3), pch=16, main="")
cr.plot(mod4, poly(wind,2), pch=16, main="")
Download