# # ############### T-test and Analysis of Variance ############### # # This script assumes you have worked through all the previous notes from # the web page and you have downloaded, installed, and updated all available # R packages. # Load the following libraries if you have not already; if an additional # library is needed, it will be listed in the script and loaded. library(foreign) library(Rcmdr) # Start by importing the data from the web, assigning the name 'example1'. example1 <- read.spss("http://www.unt.edu/rss/class/Jon/R_SC/Module3/ExampleData1.sav", use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE) attach(example1) summary(example1) names(example1) ############################################################################## ############ T-tests ############### ### Single sample t-tests with Recall1; specifying one- and two-tailed tests against # various values of the population mean. t.test(Recall1, alternative='two.sided', mu=0.0, conf.level=.95) t.test(Recall1, alternative='less', mu=15.0, conf.level=.95) t.test(Recall1, alternative='greater', mu=11.5, conf.level=.95) ### Dependent or paired samples t-tests. # Is the difference between Recall1 and Recall2 less than zero? t.test(Recall1, Recall2, alternative='less', conf.level=.95, paired=TRUE) # Is the difference between Recall1 and Recall2 greater than zero? t.test(Recall1, Recall2, alternative='greater', conf.level=.95, paired=TRUE) ### Independent samples t-tests. # Two-tailed test of Skittles vs. No candy on Recall1 WITHOUT assuming # homogeneity of variances (Welch t-test). t.test(Recall1~Candy, alternative='two.sided', conf.level=.95, var.equal=FALSE, data=example1) # Boxplot of Recall1 by groups of Candy. boxplot(Recall1~Candy, ylab="Recall1", xlab="Candy", data=example1) # One-tailed test of males vs. females on Recall1 ASSUMING equal variances. t.test(Recall1~Sex, alternative='less', conf.level=.95, var.equal=TRUE, data=example1) ### Robust t-test. # First create an object (called 'x1' here) to show each group of Candy # on Recall1. x1 <- split(Recall1, Candy) x1 # Load required library(WRS). # If not installed, it can be found only on R-Forge. # To install this package directly within R type: install.packages("WRS", repos="http://R-Forge.R-project.org") library(WRS) # Robust t-test (Yuen bootstrapped t-test); 600 bootstrapped resamples; one # tailed test (side=T). yuenbt(x1$Skittles, x1$None, tr=.0, alpha=.05, nboot=600, side=T) # With 20% trimmed means. yuenbt(x1$Skittles,x1$None,tr=.2,alpha=.05,nboot=600,side=T) ### Cohen's d effect size (and confidence interval for Cohen's d) for the ### difference between two means. # Load required library(MBESS) library(MBESS) # Calculate the Cohen's d (also known as Standardized Measure of Difference). smd(x1$Skittles, x1$None) # To get the confidence interval for Cohen's d; we need the Noncentrality # Parameter (generally the observed t-statistic from the t-test). Here we are # using the NCP from the Yuen bootstrapped and trimmed t-test just above. ci.smd(ncp = -6.3, n.1 = 27, n.2 = 27, conf.level = .95) # To test the assumption of homogeneity of variances; the first line simply # reports the variances of each group, the second runs the 'Levene's test'. tapply(example1$Recall1, example1$Candy, var, na.rm=TRUE) var.test(Recall1 ~ Candy, alternative='two.sided', conf.level=.95, data=example1) ############################################################################## ############### ANOVA ############### ### Oneway ANOVA comparing three types of distraction on ability to Recall. library(multcomp, pos=4) library(abind, pos=4) # Conduct the Analysis of Variance (aov). AnovaModel.1 <- aov(Recall1 ~ Distraction, data=example1) summary(AnovaModel.1) # Summary for types of distraction. numSummary(Recall1 , groups=Distraction,statistics=c("mean", "sd")) # Pairwise comparisons: Tukey. .Pairs <- glht(AnovaModel.1, linfct = mcp(Distraction = "Tukey")) summary(.Pairs) confint(.Pairs) # confidence intervals cld(.Pairs) # compact letter display # Plotting the means with tail displays. old.oma <- par(oma=c(0,5,0,0)) plot(confint(.Pairs)) par(old.oma) remove(.Pairs) ### Factorial ANOVA comparing 3 types of Distraction and 2 types of Candy on ### ability to Recall. # Conduct the ANOVA. This can be 'read' as: Linear Model of Recall predicted # by Candy and Distraction. AnovaModel.2 <- (lm(Recall1 ~ Candy*Distraction, data=example1)) # Summary of the ANOVA Model 2. Anova(AnovaModel.2) # Individual cell/condition summaries (mean, sd, n). tapply(Recall1, list(Candy=Candy,Distraction=Distraction), mean, na.rm=TRUE) tapply(Recall1, list(Candy=Candy,Distraction=Distraction), sd, na.rm=TRUE) tapply(Recall1, list(Candy=Candy,Distraction=Distraction), function(x) sum(!is.na(x))) # Plot of means from ANOVA Model 2. plotMeans(Recall1, Distraction, Candy, error.bars="se") #### SEE ALSO ### # (1): http://www.unt.edu/benchmarks/archives/2003/august03/rss.htm # (2): http://www.unt.edu/benchmarks/archives/2002/april02/rss.htm # END: December 2010.