Attachment 'gillespie-script.R'

Download

   1 library(languageR)
   2 ################ Reads in Data #############################
   3 d<-read.table("fakedata.txt", header=TRUE)
   4 #renames factors
   5 d$IV1<-ifelse(d$IV1==1, "silent", "noise")
   6 d$IV2<-ifelse(d$IV2==2, "word", ifelse(d$IV2==3, "legal", "illegal")) 
   7 
   8 d$NoiseCond<-as.factor(d$IV1)
   9 d$WordCond<-as.factor(d$IV2)
  10 d$WordCond<-as.factor(d$WordCond)
  11 d$NoiseCond<-as.factor(d$NoiseCond)
  12 d$Freq<-as.numeric(d$Freq)
  13 
  14 
  15 ########### Sets up Treatment Coding ############################
  16 
  17 # 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...
  18 
  19 d$WordCond.Treatment<-d$WordCond
  20 
  21 d$WordCond.Treatment<-factor(d$WordCond.Treatment, levels=c("word","legal","illegal")) #reorders levels to put "word" in baseline position (1st in list)
  22 
  23 # 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.
  24 
  25 #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)
  26 #This says set the contrasts to treatment coding with 3 levels, with the 3rd level being the base condition
  27 
  28 
  29 lin.Treatment<-lmer(RT ~ WordCond.Treatment + (1|Subject) + (1|Item), data=d) #linear model
  30 
  31 
  32 ########### Sets up Effects Coding ############################
  33 
  34 d$WordCond.Effects<-d$WordCond
  35 
  36 # 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
  37 
  38 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
  39 
  40 #The basic command to create effects coding variables is contr.sum(n), where n = the number of levels of your factor.
  41 
  42 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
  43 
  44 
  45 
  46 ########### Sets up Helmert Coding ############################
  47 
  48 ##Regression style###
  49 
  50 
  51 # Specifically, let's make each Ci equal the difference between the means we're testing.
  52 d$WordCond.Helm.Reg<-d$WordCond
  53 
  54 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) 
  55 
  56 
  57 lin.Helm.Reg<-lmer(RT ~ WordCond.Helm.Reg + (1|Subject) + (1|Item), data=d) # This model allows for directly interpretable Bs - difference between conditions
  58 
  59 ########### Sets up Polynomial Coding ############################
  60 
  61 d$WordCond.Poly<-d$WordCond
  62 
  63 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)
  64 
  65 #Orthogonal coding scheme, but interpretations of Cis are different because you're not testing for differences among group means
  66 
  67 lin.Poly<-lmer(RT ~ WordCond.Poly + (1|Subject) + (1|Item), data=d)
  68 
  69 
  70 # 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.
  71 
  72 
  73 ##########Comparing models #################################
  74 
  75 p.Treatment<-pvals.fnc(lin.Treatment,1000)
  76 a.Treatment<-aovlmer.fnc(lin.Treatment, p.Treatment, noMCMC=TRUE)
  77 
  78 p.Effects<-pvals.fnc(lin.Effects,1000)
  79 a.Effects<-aovlmer.fnc(lin.Effects, p.Effects, noMCMC=TRUE)
  80 
  81 p.Helmert<-pvals.fnc(lin.Helm.Reg,1000)
  82 a.Helmert<-aovlmer.fnc(lin.Helm.Reg, p.Helmert, noMCMC=TRUE)
  83 
  84 p.Poly<-pvals.fnc(lin.Poly,1000)
  85 a.Poly<-aovlmer.fnc(lin.Poly, p.Poly, noMCMC=TRUE)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-04-22 12:56:04, 10.9 KB) [[attachment:Day1.R]]
  • [get | view] (2021-04-22 12:56:04, 1697.7 KB) [[attachment:Day1.pdf]]
  • [get | view] (2021-04-22 12:56:04, 2625.1 KB) [[attachment:Day2.pdf]]
  • [get | view] (2021-04-22 12:56:04, 2261.1 KB) [[attachment:Groningen11.pdf]]
  • [get | view] (2021-04-22 12:56:05, 63.3 KB) [[attachment:PluralComparisonMontreal.csv]]
  • [get | view] (2021-04-22 12:56:04, 10.9 KB) [[attachment:day1-script.R]]
  • [get | view] (2021-04-22 12:56:04, 23.0 KB) [[attachment:fakedata.txt]]
  • [get | view] (2021-04-22 12:56:04, 3.8 KB) [[attachment:gillespie-script.R]]
  • [get | view] (2021-04-22 12:56:04, 624.2 KB) [[attachment:gillespie-tutorial.pdf]]
  • [get | view] (2021-04-22 12:56:04, 1.4 KB) [[attachment:graff-script.R]]
  • [get | view] (2021-04-22 12:56:04, 514.0 KB) [[attachment:graff-tutorial.pdf]]
  • [get | view] (2021-04-22 12:56:05, 6860.5 KB) [[attachment:lecture1McGill.pdf]]
  • [get | view] (2021-04-22 12:56:05, 2614.6 KB) [[attachment:lecture2McGill.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

MoinMoin Appliance - Powered by TurnKey Linux