Attachment 'ranef_sim.R'
Download 1 set.seed(53)
2
3 ## generate some faked up dependent variables
4 nTrials <- 100
5 nSubjects <- 100
6 trials <- 1:nTrials
7 trial <- rep(trials, nSubjects)
8 ## 100 subjects
9 subjects <- 1:nSubjects
10 subject <- rep(subjects, rep(nTrials, nSubjects))
11 ## each with an iq score
12 iqs <- rnorm(nSubjects, mean=100, sd=15)
13 iq <- rep(iqs, rep(nTrials, nSubjects))
14 ## and with a random baseline
15 baselines.rand <- as.numeric(scale(rnorm(nSubjects, mean=0, sd=20), scale=F))
16 baseline.rand <- rep(baselines.rand, rep(nTrials, nSubjects))
17 ## or a baseline that varies with IQ
18 baselines.corr <- as.numeric(scale(rnorm(nSubjects, mean=0, sd=20) - iqs, scale=F))
19 baseline.corr <- rep(baselines.corr, rep(nTrials, nSubjects))
20 ## and a categorical variable
21 conditions <- rep(c(0, 1), 50)
22 condition <- rep(conditions, rep(100, 100))
23 ## and some random noise
24 noise <- rnorm(nTrials * nSubjects, mean=0, sd=5)
25
26
27 ## independent variables are a function of the dependent variables
28 log.RT.1 <-
29 ## speed up over trials
30 -.5 * log(trial) +
31 ## higher IQ is faster
32 -.1 * iq +
33 ## second condition is slower
34 10 * condition +
35 ## baseline for each subject
36 baseline.rand +
37 ## random noise
38 noise
39
40 log.RT.2 <-
41 ## speed up over trials
42 -.5 * log(trial) +
43 ## higher IQ is faster
44 -.1 * iq +
45 ## second condition is slower
46 10 * condition +
47 ## baseline for each subject
48 baseline.corr +
49 ## random noise
50 noise
51
52 d <- data.frame(subject, trial, iq, condition, log.RT.1, log.RT.2, baseline.rand, baseline.corr, noise)
53
54 ## library(lattice)
55 ## histogram(~ log.RT.1 | subject)
56 ## histogram(~ log.RT.2 | subject)
57
58 library(lme4)
59 lm.1 <- lm(log.RT.1 ~ iq + condition + log(trial), data = d)
60 lm.2 <- lm(log.RT.2 ~ iq + condition + log(trial), data = d)
61 lmer.1 <- lmer(log.RT.1 ~ iq + condition + log(trial) + (1 | subject), data = d)
62 lmer.2 <- lmer(log.RT.2 ~ iq + condition + log(trial) + (1 | subject), data = d)
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.You are not allowed to attach a file to this page.