# 1-factor ANOVA # install.packages("ggplot2") library(ggplot2) # Here we'll run the ANOVA from the 'one_factor_ANOVA_tutorial' which looked at the mean # preferred Exam1 across the levels of how much class affects your mood. rm(list = ls()) # load in the data. Note the new option 'na.strings = ""'. This converts missing # responses in nominal data to 'NA's. survey <-read.csv("http://www.courses.washington.edu/psy315/datasets/Psych315W21survey.csv", na.strings = "") out <- lm(Exam1 ~ class,data = survey,na.action = na.omit) anova.out <- anova(out) sprintf('F(%g,%g) = %0.2f, p = %0.4f',anova.out$Df[1],anova.out$Df[2], anova.out$`F value`[1],anova.out$`Pr(>F)`[1]) # Next we'll make a bar plot of means across levels with error bars representing the # standard error of the mean. This will use 'ggplot', much like we did in # 'TwoSampleIndependentTTest.R'. # This generates a data frame containing statistics for each level of 'class'. Note # the way we're removing NA's summary <- data.frame( mean <- tapply(survey$Exam1,survey$class,mean,na.rm = TRUE), n <- tapply(survey$Exam1,survey$class,function(x) sum(!is.na(x))), sd <- tapply(survey$Exam1,survey$class,sd,na.rm = TRUE)) summary$sem <- summary$sd/sqrt(summary$n) colnames(summary) = c("mean","n","sd","sem") summary # The levels in 'class' come out in alphabetical order. To re-order them # from 'Not at all' to 'Very much' we'll define a list in right order # and use this list in ggplot levels <- row.names(summary) levels <- levels[c(3,2,1,4)] # Bar graph: # Define y limits for the bar graph ylimit <- c(min(summary$mean-1.5*summary$sem), max(summary$mean+1.5*summary$sem)) # Plot bar graph with error bar as one standard error (standard error of the mean/SEM) ggplot(summary, aes(x = row.names(summary), y = mean)) + xlab("How much do you think you'll like this class?") + geom_bar(position = position_dodge(), stat="identity", fill="blue") + geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem),width = .5) + scale_y_continuous(name = "Predicted Exam 1 Score") + scale_x_discrete(limits = levels) + coord_cartesian(ylim=ylimit)