Attachment 'lmer.R'

Download

   1 library(languageR)
   2 
   3 ## ---------------------------------------------------
   4 # Lexical decision time data
   5 #
   6 # Q: To what extent are lexical decisions influenced by
   7 #    (a) properties of the participants, (b) words (items) 
   8 #    used in the experiment, (c) design properties of the 
   9 #    experiment, or (d) more general factors?
  10 ## ---------------------------------------------------
  11 data(lexdec)
  12 library(lme4, keep.source = FALSE)
  13 library(arm)
  14 
  15 lm.simp <- lm(RT ~ 1, data=lexdec)
  16 display(lm.simp)
  17 
  18 lm.1 <- lm(RT ~ meanWeight + Frequency, data=lexdec)
  19 summary(lm.1)
  20 
  21 
  22 lm.2 <- lm(RT ~ 1 + Length + meanWeight + Frequency + FamilySize + SynsetCount
  23 		   , data=lexdec)
  24 summary(lm.2)
  25 
  26 # comparison of models
  27 anova(lm.2, lm.1, lm.simp)
  28 
  29 ## ---------------------------------------------------
  30 # investigate the factors in the data set and build a good model 
  31 # ONLY USING LINGUISTIC FACTORS
  32 ## ---------------------------------------------------
  33 
  34 ## ---------------------------------------------------
  35 # Do subjects differ?
  36 ## ---------------------------------------------------
  37 
  38 ll.1 <- lmList(RT ~ 1 + meanWeight + Frequency | Subject, data=lexdec)
  39 str(ll.1)
  40 
  41 # a little function to calculate the standard error of a 
  42 # sample (in a vector)
  43 se <- function(x) {
  44 	var(x) / sqrt(length(x))
  45 }
  46 
  47 # coef and standard error of coef from each model
  48 paste("[", sapply(ll.1, coef), ", ", sapply(ll.1, se.coef), "]", sep="")
  49 
  50 # coef and standard error across model
  51 paste("[", mean(sapply(ll.1, coef)[2,]), ", ", se(sapply(ll.1, se.coef)[,2]), "]", sep="")
  52 
  53 
  54 # Trellis plot of individual participant coefficients, xyplot
  55 trellis.device(color=F)
  56 xyplot(RT ~ Frequency | Subject,
  57 	data=lexdec, 
  58 	main="By-subject LMs",
  59 	ylab="log reaction times",
  60 	xlab="",
  61 #	ylim= c(-200,200),
  62 	panel=function(x, y){
  63 		panel.xyplot(x, y, col=1)
  64 		panel.lmline(x, y, lty=4, col="blue", lwd=3)
  65 	}	
  66 )
  67 
  68 old.prompt = grid::grid.prompt(T)
  69 qqmath( ~ RT | Subject, data= lexdec, layout=c(4,4),
  70        prepanel = prepanel.qqmathline,
  71        panel = function(x, ...) {
  72           panel.qqmathline(x, col= 2, lty=1, ...)
  73           panel.qqmath(x, ...)
  74        }
  75 )
  76 grid::grid.prompt(old.prompt)
  77 
  78 xylowess.fnc(RT ~ meanWeight | Subject, data=lexdec)
  79 
  80 
  81 ## ---------------------------------------------------
  82 # multilevel (aka mixed) model
  83 ## ---------------------------------------------------
  84 
  85 lmer.1 <- lmer(RT ~ 1 + meanWeight + Frequency +
  86 		    (1 | Subject)
  87 		   , data=lexdec)
  88 summary(lmer.1)
  89 
  90 # the random effects
  91 ranef(lmer.1)
  92 var(ranef(lmer.1)[[1]])
  93 
  94 
  95 ## ---------------------------------------------------
  96 # R2
  97 ## ---------------------------------------------------
  98 summary(lm.1)
  99 cor(fitted(lm.1), lexdec$RT)^ 2
 100 
 101 cor(fitted(lmer.1), lexdec$RT) ^ 2
 102 
 103 ## ---------------------------------------------------
 104 # use R2 comparisons to investigate
 105 # item effects
 106 # design effects
 107 # effects of subject differences
 108 ## ---------------------------------------------------
 109 
 110 
 111 
 112 
 113 lexdec.lmer = lmer(RT ~ 1 + Correct + Trial + PrevType * meanWeight + 
 114    Frequency + NativeLanguage * Length + (1|Subject) + (1|Word), 
 115    data = lexdec)
 116 
 117 
 118 
 119 
 120 
 121 
 122 
 123 lexdec.lmer = lmer(RT ~ 1 + Correct + Trial + PrevType * meanWeight + 
 124    Frequency + NativeLanguage * Length + (1|Subject) + (1|Word), 
 125    data = lexdec)
 126 
 127 
 128 lexdec.lmer = lmer(RT ~ 1 + Correct + Trial + PrevType * meanWeight + 
 129    Frequency + NativeLanguage * Length + (1|Subject) + (1|Word), 
 130    data = lexdec)
 131 pvals.fnc(lexdec.lmer)$summary

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:55:33, 3.5 KB) [[attachment:lmer.R]]
  • [get | view] (2021-04-22 12:55:33, 1.4 KB) [[attachment:ranef_sim.R]]
 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