--- title: "perch" author: "Chris Parrish" date: "January 8, 2016" output: pdf_document --- perch references: - Cannon, et al., Stat2, chapter 03, examples 3.10, 3.12-3.13, 3.16 Import the data. ```{r} data <- read.csv("Perch.csv", header=TRUE) head(data) dim(data) ``` Scatterplot matrix. ```{r} pairs(~ Weight + Length + Width, data=data, col="darkred") ``` Separate linear models for length and width ```{r} # length plot(Weight ~ Length, data=data, pch=20, col="darkred") perch.length.lm <- lm(Weight ~ Length, data=data) coef(perch.length.lm) options(show.signif.stars=FALSE) summary(perch.length.lm) # width plot(Weight ~ Width, data=data, pch=20, col="darkred") perch.width.lm <- lm(Weight ~ Width, data=data) coef(perch.width.lm) summary(perch.width.lm) ``` Multiple regression without interaction. ```{r} perch.lm1<- lm(Weight ~ Length + Width, data=data) summary(perch.lm1) anova(perch.lm1) ``` Residuals without interaction. ```{r} plot(predict(perch.lm1), resid(perch.lm1), pch=20, col="darkred") abline(h=0, col="orange") ``` Multiple regression with interaction. ```{r} perch.lm2 <- lm(Weight ~ Length * Width, data=data) summary(perch.lm2) anova(perch.lm2) ``` Residuals with interaction. ```{r} hist(resid(perch.lm2), col="wheat") qqnorm(resid(perch.lm2), col="orchid") qqline(resid(perch.lm2), col="orange") plot(predict(perch.lm2), resid(perch.lm2), pch=20, col="darkred") abline(h=0, col="orange") ``` Complete second-order model. ```{r} perch.lm3 <- lm(Weight ~ Length + Width + I(Length^2) + I(Width^2) + Length:Width, data=data) summary(perch.lm3) anova(perch.lm3) ``` Correlated predictors. ```{r} with(data, cor(cbind(Weight, Length, Width))) plot(Length ~ Width, data=data, pch=20, col="darkred") perch.lm4 <- lm(Weight ~ Length + Width, data=data) summary(perch.lm4) perch.lm5 <- lm(Weight ~ Length + Width + Length:Width, data=data) summary(perch.lm5) ``` Collinearity. ```{r} cor.matrix <- with(data, cor(cbind(Length, Width, Length^2, Width^2, Length * Width))) var.names <- c("Length", "Width", "Length^2", "Width^2", "Length*Width") rownames(cor.matrix) <- colnames(cor.matrix) <- var.names cor.matrix ``` Nested (or incremental) F-tests for testing subsets of predictors. ```{r} # perch.lm5 <- lm(Weight ~ Length + Width + Length:Width, data=data) # perch.lm3 <- lm(Weight ~ Length + Width + I(Length^2) + I(Width^2) + Length:Width, data=data) anova(perch.lm5, perch.lm3) ```