# This set of commands calculate means, standard deviations, standard errors, and confidence intervals for a data set # Written by Dave Jenkins, Oct. 2016, based on code at http://www.cookbook-r.com/Manipulating_data/Summarizing_data # First run the entire summarySE function - from line 5 through line 38. summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE, conf.interval=.95, .drop=TRUE) { require(plyr) # New version of length which can handle NA's: if na.rm==T, don't count them length2 <- function (x, na.rm=FALSE) { if (na.rm) sum(!is.na(x)) else length(x) } # This does the summary. For each group's data frame, return a vector with # N, mean, and sd datac <- ddply(data, groupvars, .drop=.drop, .fun = function(xx, col) { c(N = length2(xx[[col]], na.rm=na.rm), mean = mean (xx[[col]], na.rm=na.rm), sd = sd (xx[[col]], na.rm=na.rm) ) }, measurevar ) # Rename the "mean" column datac <- rename(datac, c("mean" = measurevar)) datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean # Confidence interval multiplier for standard error # Calculate t-statistic for confidence interval: # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1 ciMult <- qt(conf.interval/2 + .5, datac$N-1) datac$ci <- datac$se * ciMult return(datac) } # Now import data from # http://jenkins.cos.ucf.edu/wordpress/wp-content/uploads/copter-data-F16.csv # and then specify appropriate details inside the summarySE parentheses below. # Here we are interested in Time for combinations of Design and Group within the data file summarySE(data, measurevar="Time", groupvars=c("Design", "Group"), na.rm=TRUE) # Now use print to make a data frame containing the results of summarySE. Beyond graphing this could be handy for a table later result <- print(summarySE(data, measurevar="Time", groupvars=c("Design", "Group"), na.rm=TRUE)) # To save the result table (e.g., as a csv file), you could use a write command (e.g., write.csv(result, "results.csv")) # Here we graph the result file but first we attach(result) # for convenience # Now we can plot those means and CIs using the package plotrix library(plotrix) # you may have to first install this package, which is handy for a variety of graphs # then make simple scatterplot of means +/- CIs plotCI(Design, Time, ci, main="95% CIs", ylim=c(3.0,7.0)) # Oops. You have multiple means and CIs per Design, because we calculated them per Group. # Recalculate per Design only to ignore among-Group differences. # Now repeat but using std. deviations and then std. errors for error bars in the plots # to plot all 3 graphs side by side, use this command first: par(mfrow=c(1,3)) plotCI(Design, Time, sd, main="SDs", ylim=c(3.0,7.0)) plotCI(Design, Time, se, main="SEs", ylim=c(3.0,7.0)) # Notice how appearances change with different error bars? # What do each of the error bars say about your data? # Which one most honestly represents mean Times? # Which one most honestly represents data distributions? # Which one most cynically manipulates readers' visual impressions of our data? # For plotting using ggplot2, see http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2) # or Box 5.2 on pg. 74 in Hector's book