Plot the means

# clear the environment
rm(list=ls())
# read in Data Set #2
data <- read.csv("RClub_DataSet2_11.3.15.csv", header = TRUE)
# Lets take a look at the data - do we see a linear trend?
# Since we're not plotting the data themselves but rather means, first we need to calculate that, so we can feed it into the plot.
# To do that, we'll use a few functions from the dplyr package. Check out this tutorial: http://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html
library(dplyr)

Factorial ANOVA

First, get a plot of your means, so you can see what’s going on.

# I'm choosing to plot with instruction type across the x-axis and grouped by age. It would also be fine to do it the other way around.
library(dplyr)
groups <- group_by(data, instructions, age) # this just prepares it for us to calculate everything within each combination of instructions and age
plot.data <- summarise(groups,
mean = mean(score, na.rm=TRUE),
sd = sd(score, na.rm=TRUE),
n = n(),
se=sd/sqrt(n),
ci = qt(0.975,df=n-1)*se)
plot.data # take a peek

# The best way to run this is actually with the lm() command, not aov(). It stands for "linear model". ANOVAs, regressions, t-tests, etc. are all examples of the general linear model, so you can use this one command to do pretty much any of them in R.
# aov() works, and it will generate exactly the same source table for you (the math is all identical), but lm() gives you more useful output.
model <- lm(score ~ instructions*age , data=data)
# When you specify an interaction with *, R automatically assumes you want the main effects as well.
# So "instructions*age" is shorthand for "instructions + age + instructions*age".
anova(model) # to get a source table