--- title: "ants" author: "Chris Parrish" date: "January 22, 2016" output: pdf_document --- ants reference: - Cannon, et al., Stat2, chapter 07, example 7.16 Import the data. ```{r} data <- read.csv("SandwichAnts.csv", header=TRUE) head(data) dim(data) ``` Scatterplot matrix. ```{r} pairs(~ Ants + Bread + Filling + Butter, data=data, col="darkred") ``` Ant statistics. There is an R package called [`plyr`](http://plyr.had.co.nz/) written by [Hadley Wickham](http://had.co.nz/) for doing this sort of thing much more elegantly. The basic idea is to break up a data structure into pieces, apply a function to the pieces, and then reassemble the results. Hadley calls this the *split-apply-combine strategy*. ```{r} ants.vegemite <- with(data, data[Filling=="Vegemite", c(2, 5)]) ants.peanutbutter <- with(data, data[Filling=="PeanutButter", c(2, 5)]) ants.hampickles <- with(data, data[Filling=="HamPickles", c(2, 5)]) ants.vegemite <- with(ants.vegemite, tapply(Ants, Bread, mean)) ants.peanutbutter <- with(ants.peanutbutter, tapply(Ants, Bread, mean)) ants.hampickles <- with(ants.hampickles, tapply(Ants, Bread, mean)) ants.statistics <- rbind(ants.vegemite, ants.peanutbutter, ants.hampickles) ants.statistics <- ants.statistics[ , c(2, 4, 1, 3)] rownames(ants.statistics) <- c("Vegemite", "PeanutButter", "HamPickles") ants.statistics ``` Add margins to ant statistics. Compare with Table 7.2, p.365. ```{r} row.means <- apply(ants.statistics, 1, mean) col.means <- apply(ants.statistics, 2, mean) ants.table <- rbind(cbind(ants.statistics, row.means), c(col.means, mean(row.means))) colnames(ants.table)[5] <- "Row Mean" rownames(ants.table)[4] <- "Column Mean" ants.table ``` Two-way ANOVA with interaction. ```{r} ants.aov <- aov(Ants ~ Filling*Bread, data=data) options(show.signif.stars=FALSE) summary(ants.aov) ``` Multiple regression with interaction. ```{r} ants.lm <- lm(Ants ~ Filling*Bread, data=data) summary(ants.lm) anova(ants.lm) ``` Main effects. Which sandwiches attract the most ants? ... the least? Have you ever tried vegemite on rye? ```{r message=FALSE} library(alr4) plot(allEffects(ants.lm)) ```