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 
   9 ## 100 subjects
  10 subjects <- 1:nSubjects
  11 subject <- rep(subjects, rep(nTrials, nSubjects))
  12 
  13 ## each with an iq score
  14 iqs <- rnorm(nSubjects, mean=100, sd=15)
  15 iq <- rep(iqs, rep(nTrials, nSubjects))
  16 
  17 ## and with a random baseline
  18 baselines.rand <- as.numeric(scale(rnorm(nSubjects, mean=0, sd=20), scale=F))
  19 baseline.rand <- rep(baselines.rand, rep(nTrials, nSubjects))
  20 
  21 ## each subject participates in both conditions
  22 conditions <- c(0,1)
  23 condition <- rep(conditions, nTrials * nSubjects / 2)
  24 
  25 ## and some random noise
  26 noise <- rnorm(nTrials * nSubjects, mean=0, sd=5)
  27 
  28 ## independent variables are a function of the dependent variables
  29 log.RT.1 <-
  30   ## speed up over trials
  31   -.5 * log(trial) +
  32   ## higher IQ is faster
  33   -.5 * iq +
  34   ## second condition is slower
  35   10 * condition + 
  36   ## baseline for each subject
  37   baseline.rand +
  38   ## random noise
  39   noise
  40 
  41 ## put it together into data frame
  42 d <- data.frame(subject, trial, iq, condition,
  43                 log.RT.1,               # log.RT.2,
  44                 baseline.rand,          # baseline.corr,
  45                 noise) 
  46 
  47 library(lme4)
  48 lm.1 <- lm(log.RT.1 ~
  49            iq + condition + log(trial),
  50            data = d)
  51 
  52 lmer.1 <- lmer(log.RT.1 ~
  53                iq + condition + log(trial) + (1 | subject),
  54                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.
  • [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