
library(languageR)
################ Reads in Data #############################
d<-read.table("fakedata.txt", header=TRUE)
#renames factors
d$IV1<-ifelse(d$IV1==1, "silent", "noise")
d$IV2<-ifelse(d$IV2==2, "word", ifelse(d$IV2==3, "legal", "illegal")) 

d$NoiseCond<-as.factor(d$IV1)
d$WordCond<-as.factor(d$IV2)
d$WordCond<-as.factor(d$WordCond)
d$NoiseCond<-as.factor(d$NoiseCond)
d$Freq<-as.numeric(d$Freq)


########### Sets up Treatment Coding ############################

# R automatically assigns levels alphabetically, this isn't always what you'll want, so you can reassign the order of the levels as shown below...

d$WordCond.Treatment<-d$WordCond

d$WordCond.Treatment<-factor(d$WordCond.Treatment, levels=c("word","legal","illegal")) #reorders levels to put "word" in baseline position (1st in list)

# R's default is to set coding scheme to Treatment, so here you don't need to do anything else now that the levels are ordered appropriately.

#More generally, if you just want to specify which level is the baseline you can do the following:  contrasts(d$WordCond.Treatment)<-contr.treatment(3, base=3)
#This says set the contrasts to treatment coding with 3 levels, with the 3rd level being the base condition


lin.Treatment<-lmer(RT ~ WordCond.Treatment + (1|Subject) + (1|Item), data=d) #linear model


########### Sets up Effects Coding ############################

d$WordCond.Effects<-d$WordCond

# If you want to make your outputs more readable (easier to figure out what each Ci is doing), then you can manually create the contrast matrix

contrasts(d$WordCond.Effects)<-cbind("illegal"= c(1, 0, -1),  "legal"= c(0, 1, -1))  #renames Cis to give indication of what is being tested... C1 = half difference between illegal and word, C2= half the difference between legal and word

#The basic command to create effects coding variables is contr.sum(n), where n = the number of levels of your factor.

lin.Effects<-lmer(RT ~ WordCond.Effects + (1|Subject) + (1|Item), data=d) #output is exactly the same as before, but now the levels of output give description of contrasts



########### Sets up Helmert Coding ############################

##Regression style###


# Specifically, let's make each Ci equal the difference between the means we're testing.
d$WordCond.Helm.Reg<-d$WordCond

contrasts(d$WordCond.Helm.Reg)<-cbind("leg.vs.ill"= c(-.5, .5, 0),"word.vs.nons"=c (-(1/3), -(1/3), (2/3)) )  #renames Cis to give indication of what is being tested... C1 = illegal vs. legal, C2= word vs.nonwords(mean of other two levels) 


lin.Helm.Reg<-lmer(RT ~ WordCond.Helm.Reg + (1|Subject) + (1|Item), data=d) # This model allows for directly interpretable Bs - difference between conditions

########### Sets up Polynomial Coding ############################

d$WordCond.Poly<-d$WordCond

contrasts(d$WordCond.Poly)<-contr.poly(3) #This specifies that you want to do polynomial coding (tests linear and quadratic components for this example with 3-levels)

#Orthogonal coding scheme, but interpretations of Cis are different because you're not testing for differences among group means

lin.Poly<-lmer(RT ~ WordCond.Poly + (1|Subject) + (1|Item), data=d)


# Output shows .L for linear component and .Q for quadratic.  If the .L coeff is significant, then the regression requires a linear component, if the .Q coeff is significant, the effect has a quadratic component.


##########Comparing models #################################

p.Treatment<-pvals.fnc(lin.Treatment,1000)
a.Treatment<-aovlmer.fnc(lin.Treatment, p.Treatment, noMCMC=TRUE)

p.Effects<-pvals.fnc(lin.Effects,1000)
a.Effects<-aovlmer.fnc(lin.Effects, p.Effects, noMCMC=TRUE)

p.Helmert<-pvals.fnc(lin.Helm.Reg,1000)
a.Helmert<-aovlmer.fnc(lin.Helm.Reg, p.Helmert, noMCMC=TRUE)

p.Poly<-pvals.fnc(lin.Poly,1000)
a.Poly<-aovlmer.fnc(lin.Poly, p.Poly, noMCMC=TRUE)