02_fit_bayesian.R 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. library(brms)
  2. dat_rt <- readRDS("dat_rt.rds")
  3. n_cores <- 7
  4. seed <- 42
  5. n_iter <- 25000
  6. n_warmup <- 17500
  7. adapt_delta <- 0.99
  8. max_treedepth <- 11
  9. n_chains <- 5
  10. priors <- c(
  11. # FIXED EFFECTS
  12. set_prior("normal(0,7.5)", class = "b", coef = "Intercept"),
  13. set_prior("normal(0,2.5)", class = "b", coef = "cong_dev"),
  14. set_prior("normal(0,2.5)", class = "b", coef = "pred_norm"),
  15. set_prior("normal(0,2.5)", class = "b", coef = "cong_dev:pred_norm"),
  16. set_prior("normal(0,7.5)", class = "b", coef = "Intercept", dpar="sigma"),
  17. set_prior("normal(0,2.5)", class = "b", coef = "cong_dev", dpar="sigma"),
  18. set_prior("normal(0,2.5)", class = "b", coef = "pred_norm", dpar="sigma"),
  19. set_prior("normal(0,2.5)", class = "b", coef = "cong_dev:pred_norm", dpar="sigma"),
  20. set_prior("normal(0,7.5)", class = "b", coef = "Intercept", dpar="ndt"),
  21. set_prior("normal(0,2.5)", class = "b", coef = "cong_dev", dpar="ndt"),
  22. set_prior("normal(0,2.5)", class = "b", coef = "pred_norm", dpar="ndt"),
  23. set_prior("normal(0,2.5)", class = "b", coef = "cong_dev:pred_norm", dpar="ndt"),
  24. # STANDARD DEVIATIONS OF RANDOM EFFECT DISTRIBUTIONS
  25. set_prior("student_t(3, 0, 2)", class = "sd"),
  26. set_prior("student_t(3, 0, 2)", class = "sd", dpar="sigma"),
  27. set_prior("student_t(3, 0, 2)", class = "sd", dpar="ndt")
  28. )
  29. # Fit model
  30. m_bme <- brm(
  31. brmsformula(
  32. rt ~ 0 + Intercept + cong_dev * pred_norm +
  33. (cong_dev * pred_norm | subj_id) +
  34. (cong_dev | image) +
  35. (1 | string),
  36. sigma ~ 0 + Intercept + cong_dev * pred_norm +
  37. (cong_dev * pred_norm | subj_id) +
  38. (cong_dev | image) +
  39. (1 | string),
  40. ndt ~ 0 + Intercept + cong_dev * pred_norm +
  41. (cong_dev * pred_norm | subj_id) +
  42. (cong_dev | image) +
  43. (1 | string)
  44. ),
  45. data = dat_rt,
  46. family = shifted_lognormal(),
  47. prior = priors,
  48. inits = "0",
  49. iter = n_iter,
  50. warmup = n_warmup,
  51. chains = n_chains,
  52. control = list(
  53. adapt_delta = adapt_delta,
  54. max_treedepth = max_treedepth
  55. ),
  56. sample_prior = "no",
  57. silent = TRUE,
  58. cores = n_cores,
  59. seed = seed,
  60. thin = 1,
  61. file = file.path("mods", "m_bme.rds")
  62. )