# Lab Exercise 4 Solutions #cardat.csv dat<-read.csv(file.choose()) head(dat) names(dat) # 1 library(lawstat) #Homogeneity of Variance tapply(dat$purc,dat$type,var) lawstat::levene.test(dat$purc,dat$type) #Normality hist(dat$purc[dat$type=="car"]) hist(dat$purc[dat$type=="truck"]) hist(dat$purc[dat$type=="van"]) boxplot(dat$purc~dat$type) qqnorm(dat$purc[dat$type=="car"]) qqnorm(dat$purc[dat$type=="truck"]) qqnorm(dat$purc[dat$type=="van"]) tapply(dat$purc,dat$type,shapiro.test) #Welch F Test oneway.test(purc~type, data=dat) # Assumptions are reasonably met, # but just in case #Welch on Trimmed Means library(WRS2) t1way(purc~type, tr=.2, data=dat) # 2 pairwise.t.test(dat$twoyr, dat$type, p.adj="holm") # 3 long_dat<-reshape(data=dat, direction="long", timevar="time",varying=1:3, v.names='shape',sep="") library(ez) long_dat$time<-factor(long_dat$time) ezANOVA(wid=id,within=time,dv=shape, data=long_dat) library(nlme) mod1<-lme(shape~time, random=~1|id, data=long_dat) anova(mod1) ## shows how to get p values pvals<-c() pvals[1]<-t.test(dat$purc,dat$oneyr, paired=TRUE)$p.value pvals[2]<-t.test(dat$purc,dat$twoyr, paired=TRUE)$p.value pvals[3]<-t.test(dat$oneyr,dat$twoyr, paired=TRUE)$p.value pvals p.adjust(p=pvals,method='holm') # 4 interaction.plot(x.factor=dat$sex, trace.factor=dat$type,response=dat$twoyr) mod1<-lm(twoyr~sex*type, data=dat) anova(mod1) # 5 library(ez) ezANOVA(data=long_dat,wid=id,dv=shape,within=time,between=sex) interaction.plot(long_dat$time,long_dat$sex,long_dat$shape) #Interaction Contrasts ezANOVA(data=subset(long_dat,time==1|time==2),wid=id,dv=shape,within=time,between=sex) ezANOVA(data=subset(long_dat,time==1|time==3),wid=id,dv=shape,within=time,between=sex) ezANOVA(data=subset(long_dat,time==2|time==3),wid=id,dv=shape,within=time,between=sex) library(nlme) mod2<-lme(shape~time*sex, random=~1|id, data=long_dat) anova(mod2)