--- title: "ethanol" author: "Chris Parrish" date: "January 21, 2016" output: pdf_document --- ethanol reference: - Cannon, et al., chapter 6, case study Import the data. ```{r} data <- read.table("Ethanol.csv", header=TRUE, sep=",") head(data) dim(data) str(data) # O2Conc is a numerical variable ``` Scatterplot matrix. ```{r} pairs(~ Ethanol + Sugar + O2Conc, data=data, col="darkred") ``` Plot Ethanol ~ O2Conc | Sugar ```{r} plot(Ethanol ~ O2Conc, data=data, las=1, type="n") glucose <- data[data\$Sugar=="Glucose", c(2, 3)] galactose <- data[data\$Sugar=="Galactose", c(2, 3)] points(glucose, pch=20, col="sandybrown") points(galactose, pch=20, col="skyblue") legend("topright", pch=20, inset=0.02, legend=c("Glucose", "Galactose"), col=c("sandybrown", "skyblue")) ``` Interaction plots. ```{r fig.width=7, fig.height=4.2} with(data, interaction.plot(O2Conc, Sugar, Ethanol, col=c("tomato", "darkseagreen"), lwd=2)) with(data, interaction.plot(Sugar, O2Conc, Ethanol, col=terrain.colors(4), lwd=2)) ``` Two-way ANOVA with interaction. Note that this display does not at all match the display on p.304. Here, O2Conc is a numerical variable, but on p.307 the authors mention that O2Conc is a categorical factor, hence their three degrees of freedom. ```{r} ethanol.aov<- aov(Ethanol ~ O2Conc + Sugar + O2Conc:Sugar, data=data) options(show.signif.stars=FALSE) anova(ethanol.aov) ``` Change O2Conc to a categorical factor. The ANOVA now matches the display on p.304. ```{r} data.mod <- data data.mod\$O2Conc <- factor(data.mod\$O2Conc) str(data.mod) ethanol.mod.aov<- aov(Ethanol ~ Sugar + O2Conc + Sugar:O2Conc, data=data.mod) anova(ethanol.mod.aov) ``` Residuals. ```{r} plot(predict(ethanol.aov), resid(ethanol.aov), pch=20, col="darkred") abline(h=0, col="orange", lty="dashed") qqnorm(resid(ethanol.aov), col="orchid") qqline(resid(ethanol.aov), col="orange") ``` Interaction plots for square root, cube root, and log. ```{r fig.width=7, fig.height=4.2} with(data, interaction.plot(O2Conc, Sugar, sqrt(Ethanol), col=c("tomato", "darkseagreen"), lwd=2, main="Square Root")) with(data, interaction.plot(O2Conc, Sugar, Ethanol^(1/3), col=c("tomato", "darkseagreen"), lwd=2, main="Cube Root")) with(data, interaction.plot(O2Conc, Sugar, log10(Ethanol + 1), col=c("tomato", "darkseagreen"), lwd=2, main="Log to Base 10")) ``` Model 1: original scale (for ethanol concentration) ```{r} ethanol.lm1<- lm(Ethanol ~ Sugar + O2Conc, data=data) summary(ethanol.lm1) ``` Model 2: square roots ```{r} ethanol.lm2<- lm(sqrt(Ethanol) ~ Sugar + O2Conc, data=data) summary(ethanol.lm2) ``` Model 3: cube roots ```{r} ethanol.lm3<- lm(Ethanol^(1/3) ~ Sugar + O2Conc, data=data) summary(ethanol.lm3) ``` Model 4: log ```{r} ethanol.lm4<- lm(log(Ethanol + 1) ~ Sugar + O2Conc, data=data) summary(ethanol.lm4) ``` Residuals for model 3. ```{r} plot(predict(ethanol.lm3), resid(ethanol.lm3), pch=20, col="darkred") abline(h=0, col="orange", lty="dashed") qqnorm(resid(ethanol.lm3), col="orchid") qqline(resid(ethanol.lm3), col="orange") ``` Effects plots for model 3. ```{r message=FALSE} ethanol.lm3<- lm(Ethanol^(1/3) ~ Sugar + O2Conc, data=data) library(alr4) plot(allEffects(ethanol.lm3)) ```