Guest User

Untitled

a guest
Mar 18th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. library("MASS")
  2. library("lme4")
  3. set.seed(123)
  4.  
  5. simulate_data <- function(n_items = 60, n_subj = 27, n_subj_item_rep = 30,
  6. mu = 308,
  7. contrast1 = c(2/3, -1/3, -1/3), contrast2 = c(-1/3, 2/3, -1/3),
  8. effect1 = 12, effect2 = -4.5,
  9. subj_interc_sd = 43,
  10. subj_slope1_sd = 17,
  11. subj_slope2_sd = 112,
  12. subj_cor_s1_i = 0.4,
  13. subj_cor_s2_i = -0.65,
  14. subj_cor_s1_s2 = -0.55,
  15. item_interc_sd = 18,
  16. item_slope1_sd = 6,
  17. item_slope2_sd = 6,
  18. item_cor_s1_i = 0.4,
  19. item_cor_s2_i = 0.3,
  20. item_cor_s1_s2 = -0.7,
  21. subj_item_interc_sd = 20,
  22. residual_sd = 180) {
  23.  
  24. # generate design
  25. dat <- expand.grid(item = seq_len(n_items), subject = seq_len(n_subj))
  26. dat$item_group <- dat$item %% 3 + 1
  27. dat$subject_group <- dat$subject %% 3 + 1
  28. dat$condition <- (dat$item_group + dat$subject_group) %% 3 + 1
  29.  
  30. # generate appropriate contrasts
  31. # R requires the generalized inverse of the contrast matrix
  32. contrast_mat <- ginv(rbind(contrast1, contrast2))
  33. dat$contrast1 <- contrast_mat[, 1][dat$condition]
  34. dat$contrast2 <- contrast_mat[, 2][dat$condition]
  35.  
  36. # fixed grand mean
  37. dat$mean_response <- mu
  38.  
  39. # condition effects
  40. dat$contrast_effect1 <- effect1 * dat$contrast1
  41. dat$contrast_effect2 <- effect2 * dat$contrast2
  42.  
  43. # random effects subjects: intercept, slope1, slope2
  44. subject_effect <- mvrnorm(n_subj,
  45. mu = c(0, 0, 0),
  46. Sigma = matrix(c(subj_interc_sd^2,
  47. subj_cor_s1_i * subj_slope1_sd * subj_interc_sd,
  48. subj_cor_s2_i * subj_slope2_sd * subj_interc_sd,
  49. subj_cor_s1_i * subj_slope1_sd * subj_interc_sd,
  50. subj_slope1_sd^2,
  51. subj_cor_s1_s2 * subj_slope1_sd * subj_slope2_sd,
  52. subj_cor_s2_i * subj_slope2_sd * subj_interc_sd,
  53. subj_cor_s1_s2 * subj_slope1_sd * subj_slope2_sd,
  54. subj_slope2_sd^2), nrow = 3),
  55. empirical = TRUE) # mu & sigma as empirical (not population!) parameters for testing
  56.  
  57. dat$subject_intercept <- subject_effect[dat$subject, 1]
  58. dat$subject_slope1 <- subject_effect[dat$subject, 2] * dat$contrast1
  59. dat$subject_slope2 <- subject_effect[dat$subject, 3] * dat$contrast2
  60.  
  61. # random effects items: intercept, slope1, slope2
  62. item_effect <- mvrnorm(n_items,
  63. mu = c(0, 0, 0),
  64. Sigma = matrix(c(item_interc_sd^2,
  65. item_cor_s1_i * item_slope1_sd * item_interc_sd,
  66. item_cor_s2_i * item_slope2_sd * item_interc_sd,
  67. item_cor_s1_i * item_slope1_sd * item_interc_sd,
  68. item_slope1_sd^2,
  69. item_cor_s1_s2 * item_slope1_sd * item_slope2_sd,
  70. item_cor_s2_i * item_slope2_sd * item_interc_sd,
  71. item_cor_s1_s2 * item_slope1_sd * item_slope2_sd,
  72. item_slope2_sd^2), nrow = 3),
  73. empirical = TRUE)
  74.  
  75. dat$item_intercept <- item_effect[dat$item, 1]
  76. dat$item_slope1 <- item_effect[dat$item, 2] * dat$contrast1
  77. dat$item_slope2 <- item_effect[dat$item, 3] * dat$contrast2
  78.  
  79. # random effects subject-item combinations
  80. dat$subj_item_intercept <- 0
  81. if (n_subj_item_rep > 1) { # if more than 1 observations of the same subject-item combination
  82. dat$subj_item_intercept <- mvrnorm(n_subj * n_items, mu = 0, Sigma = subj_item_interc_sd^2, empirical = TRUE)[, 1]
  83. }
  84.  
  85. # generate subject-item replications according to number of observations of the same subject-item combination
  86. dat <- do.call("rbind", replicate(n_subj_item_rep, dat, simplify = FALSE))
  87.  
  88. # residual variation
  89. dat$residual_sd <- mvrnorm(nrow(dat), mu = 0, Sigma = residual_sd^2, empirical = TRUE)[, 1]
  90.  
  91. # calculate response
  92. dat$response <- dat$mean_response + dat$contrast_effect1 + dat$contrast_effect2 +
  93. dat$item_intercept + dat$item_slope1 + dat$item_slope2 +
  94. dat$subject_intercept + dat$subject_slope1 + dat$subject_slope2 +
  95. dat$subj_item_intercept +
  96. dat$residual_sd
  97.  
  98. # convert categorical variables to factors
  99. dat[, 1:5] <- lapply(dat[, 1:5], factor)
  100.  
  101. # set specified contrasts
  102. contrasts(dat$condition) <- contrast_mat
  103.  
  104. dat
  105. }
  106.  
  107. d <- simulate_data()
  108.  
  109. # if more than 1 observations of the same subject-item combination ..
  110. summary(lmer(response ~ contrast1 + contrast2 + (contrast1 + contrast2|item) +
  111. (contrast1 + contrast2|subject) + (1|subject:item), d))
  112. # .. otherwise
  113. # summary(lmer(response ~ contrast1 + contrast2 + (contrast1 + contrast2|item) +
  114. # (contrast1 + contrast2|subject), d))
  115.  
  116. Linear mixed model fit by REML ['lmerMod']
  117. Formula: response ~ contrast1 + contrast2 + (contrast1 + contrast2 | item) +
  118. (contrast1 + contrast2 | subject) + (1 | subject:item)
  119. Data: d
  120.  
  121. REML criterion at convergence: 643678
  122.  
  123. Scaled residuals:
  124. Min 1Q Median 3Q Max
  125. -4.1695 -0.6709 -0.0006 0.6728 4.2132
  126.  
  127. Random effects:
  128. Groups Name Variance Std.Dev. Corr
  129. subject:item (Intercept) 423.44 20.578
  130. item (Intercept) 337.69 18.376
  131. contrast1 36.02 6.002 0.38
  132. contrast2 66.85 8.176 0.11 -0.88
  133. subject (Intercept) 1819.74 42.658
  134. contrast1 270.89 16.459 0.45
  135. contrast2 12677.59 112.595 -0.65 -0.60
  136. Residual 32431.73 180.088
  137. Number of obs: 48600, groups: subject:item, 1620; item, 60; subject, 27
  138.  
  139. Fixed effects:
  140. Estimate Std. Error t value
  141. (Intercept) 308.000 8.600 35.82
  142. contrast1 13.771 3.534 3.90
  143. contrast2 -4.708 21.737 -0.22
  144.  
  145. Correlation of Fixed Effects:
  146. (Intr) cntrs1
  147. contrast1 0.410
  148. contrast2 -0.619 -0.557
Add Comment
Please, Sign In to add comment