oxide_read.table("oxide.dat",skip=18) names(oxide)_c("day","wind","temp","humid","insol","ox") attach(oxide) motif() pairs(oxide) #multiple regression ox.lm.1_lm(ox ~ temp) ox.lm.2_lm(ox ~ temp + wind) summary(ox.lm.1,cor=F) summary(ox.lm.2,cor=F) #the design matrix model.matrix(ox.lm.2) #examine the parts of the output summary(ox.lm.2,cor=F) #analysis of residuals par(mfrow=c(2,2)) plot(temp,ox.lm.2$resid) plot(wind,ox.lm.2$resid) plot(ox,ox.lm.2$resid) plot(ox.lm.2$fitted,ox.lm.2$resid) par(mfrow=c(1,1)) qqnorm(ox.lm.2$resid) #visualize in three dimensions persp(interp(temp,wind,ox)) summary(oxide) persp(interp(temp,wind,ox),eye=c(-6*(92-67),8*(65-35),5*(25-4))) persp(interp(temp,wind,ox),eye=c(-6*(92-67),4*(65-35),3*(25-4))) par(mfrow=c(2,2)) persp(interp(temp,wind,ox),eye=c(-6*(92-67),4*(65-35),3*(25-4))) title("Data") persp(interp(temp,wind,ox.lm.2$fitted),eye=c(-6*(92-67),4*(65-35),3*(25-4))) title("Fitted") persp(interp(temp,wind,ox.lm.2$resid),eye=c(-6*(92-67),4*(65-35),3*(25-4))) title("Resid") par(mfrow=c(1,1)) spin( matrix(c(temp,wind,ox),30,3), collab=c("temp","wind","ox"),highlight=rep(T,30)) #examining predictions plot(temp,ox) lines(temp[order(temp)],fitted(ox.lm.2)[order(temp)]) coef(ox.lm.2) lines(temp[order(temp)],coef(ox.lm.2)[2]*temp[order(temp)]+coef(ox.lm.2)[1]+coef(ox.lm.2)[3]*mean(wind),lty=2) lines(temp[order(temp)],fitted(ox.lm.1)[order(temp)],lty=4) #finding important variables #first look at t tests ox.lm.3_lm(ox ~ .,data=oxide) summary(ox.lm.3) ox.lm.4_lm(ox ~ .-day,data=oxide) summary(ox.lm.4,cor=F) ox.lm.5_lm(ox ~ .-day-insol,data=oxide) summary(ox.lm.5,cor=F) #stepwise algorithms step(lm(ox~.,data=oxide)) stepwise(oxide[,-6],ox) leaps(oxide[,-6],ox) #F tests ox.lm.3_lm(ox ~ .,data=oxide) anova(ox.lm.3) ox.lm.6_lm(ox ~ day + insol + humid + temp + wind) anova(ox.lm.6) ox.lm.7_lm(ox ~ wind + temp + humid + insol + day) anova(ox.lm.7) #examine residuals: constant variance? other variables? ox.lm.8_lm(ox ~ day) qqnorm(ox.lm.8$resid) plot(day,ox.lm.8$resid) plot(wind,ox.lm.8$resid) #transformations detach() cars_read.table("mercedes.dat",skip=24,header=T) attach(cars) pairs(cars) plot(Age,Price) cars.lm.1_lm(Price~Age) summary(cars.lm.1,cor=F) plot(Age,resid(cars.lm.1)) cars.lm.2_lm(log(Price)~Age) summary(cars.lm.2,cor=F) plot(Age,resid(cars.lm.2)) #multicollinearity pairs(cars) cor(cars) plot(Age,Mile) cars.lm.3_lm(log(Price)~Age+Mile) summary(cars.lm.3,cor=F) summary(cars.lm.2,cor=F) summary(lm(log(Price)~Mile),cor=F) #polynomial regression cars.lm.4_lm(log(Price)~Age^2+Age+Mile) summary(cars.lm.4) #interactions ox.lm.9_lm(ox~wind*temp) summary(ox.lm.9) step(lm(ox~.^2,data=oxide)) #factor models cars$Mod_as.factor(cars$Mod) detach() attach(cars) cars.lm.5_lm(log(Price)~Age+Mod) summary(cars.lm.5,cor=F) model.matrix(cars.lm.5) contrasts(Mod) contrasts(Mod)_contr.treatment(6) contrasts(Mod) cars.lm.6_lm(log(Price)~Age+Mod) model.matrix(cars.lm.6) summary(cars.lm.5,cor=F) summary(cars.lm.6,cor=F) anova(cars.lm.6) #factor model 2 cars.lm.7_lm(log(Price)~Age*Mod) model.matrix(cars.lm.7) summary(cars.lm.7,cor=F) anova(cars.lm.7) #autoregression acf(ox) acf(ox,type="partial") ox lag(ox) lag(ox,-1) ox.lm.10_lm(ox[2:30]~ox[1:29]) summary(ox.lm.10,cor=F) plot(ox.lm.2$resid) acf(ox.lm.2$resid)