Title: | Bayes Factors for Hierarchical Linear Models with Continuous Predictors |
---|---|
Description: | Runs hierarchical linear Bayesian models. Samples from the posterior distributions of model parameters in JAGS (Just Another Gibbs Sampler; Plummer, 2003, <doi:10.1.1.13.3406>). Computes Bayes factors for group parameters of interest with the Savage-Dickey density ratio (Wetzels, Raaijmakers, Jakab, Wagenmakers, 2009, <doi:10.3758/PBR.16.4.752>). |
Authors: | Mirko Thalmann [aut, cre], Marcel Niklaus [aut], Klaus Oberauer [ths], John Kruschke [ctb] |
Maintainer: | Mirko Thalmann <[email protected]> |
License: | GPL (>=2) |
Version: | 0.1.3 |
Built: | 2024-11-05 03:41:51 UTC |
Source: | https://github.com/mirkoth/bayesrs |
Example data set used for showing functionality of modelrun
. The examples give the code used for simulating the data set.
bayesrsdata
bayesrsdata
A data.frame with 1200 rows and 4 variables
## Not run: require(MASS) nsubj <- 40 # number of participants nobs <- 30 # number of observations per cell ncont <- 1 # number of continuous IVs ncat <- 1 # number of categorical IVs ntrials <- nobs * ncont * ncat #total number of trials per subject xcont <- seq(1,5,1) # values of continuous IV xcont.mc <- xcont-mean(xcont) # mean-centered values of continuous IV xcat <- c(-.5,.5) # Simple coded categorical IV eff.size.cont <- c(0.3) # effect size of continuous IV eff.size.cat <- c(0.8) # effect size of categorical IV eff.size.interaction <- c(0) # effect size of interaction correlation.predictors <- 0.5 # correlation between b<-subject slopes of the two predictors intercept <- 0 # grand intercept error.sd <- 1 # standard deviation of error term #------------------------- # Create Simulated Data - #------------------------- # correlation between by-subject continuous slope, and by-subject categorical slope subj.cont1.cat1.corr <- mvrnorm(n = nsubj, mu = c(eff.size.cont,eff.size.cat), Sigma = matrix(data = c(1,correlation.predictors, correlation.predictors,1), nrow = 2, ncol = 2, byrow = TRUE), empirical = TRUE) b.cont.subj <- data.frame(subject = 1:nsubj, vals = subj.cont1.cat1.corr[,1]) b.cat.subj <- data.frame(subject = 1:nsubj, vals = subj.cont1.cat1.corr[,2]) b.subj.rand <- data.frame(subject = 1:nsubj, vals = rnorm(n = nsubj, mean = 0, sd = 1)) b.ia.subj <- data.frame(subject = 1:nsubj, vals = rnorm(n = nsubj, mean = eff.size.interaction, sd = 1)) # generate according to lin reg formula bayesrsdata <- data.frame(subject = rep(1:nsubj, each = ntrials), x.time = rep(xcont, each = ntrials/5), x.domain= rep(xcat, each = ntrials/10)) bayesrsdata$y <- 0 for (i in 1:nrow(bayesrsdata)){ bayesrsdata$y[i] <- b.subj.rand$vals[bayesrsdata$subject[i]==b.subj.rand$subject] + bayesrsdata$x.time[i] * (eff.size.cont+b.cont.subj$vals[bayesrsdata$subject[i]== b.cont.subj$subject]) + bayesrsdata$x.domain[i] * (eff.size.cat+b.cat.subj$vals[bayesrsdata$subject[i]== b.cat.subj$subject]) + bayesrsdata$x.time[i] * bayesrsdata$x.domain[i] * (eff.size.interaction+b.ia.subj$vals[bayesrsdata$subj[i]==b.ia.subj$subject]) } # add measurement error bayesrsdata$y <- bayesrsdata$y + rnorm(n = nrow(bayesrsdata), mean = 0, sd = 1) # create final data set recvars <- which(names(bayesrsdata) %in% c("subject", "item", "x.domain")) bayesrsdata[,recvars] <- lapply(bayesrsdata[,recvars], as.factor) save(bayesrsdata, file= "bayesrsdata.rda") ## End(Not run)
## Not run: require(MASS) nsubj <- 40 # number of participants nobs <- 30 # number of observations per cell ncont <- 1 # number of continuous IVs ncat <- 1 # number of categorical IVs ntrials <- nobs * ncont * ncat #total number of trials per subject xcont <- seq(1,5,1) # values of continuous IV xcont.mc <- xcont-mean(xcont) # mean-centered values of continuous IV xcat <- c(-.5,.5) # Simple coded categorical IV eff.size.cont <- c(0.3) # effect size of continuous IV eff.size.cat <- c(0.8) # effect size of categorical IV eff.size.interaction <- c(0) # effect size of interaction correlation.predictors <- 0.5 # correlation between b<-subject slopes of the two predictors intercept <- 0 # grand intercept error.sd <- 1 # standard deviation of error term #------------------------- # Create Simulated Data - #------------------------- # correlation between by-subject continuous slope, and by-subject categorical slope subj.cont1.cat1.corr <- mvrnorm(n = nsubj, mu = c(eff.size.cont,eff.size.cat), Sigma = matrix(data = c(1,correlation.predictors, correlation.predictors,1), nrow = 2, ncol = 2, byrow = TRUE), empirical = TRUE) b.cont.subj <- data.frame(subject = 1:nsubj, vals = subj.cont1.cat1.corr[,1]) b.cat.subj <- data.frame(subject = 1:nsubj, vals = subj.cont1.cat1.corr[,2]) b.subj.rand <- data.frame(subject = 1:nsubj, vals = rnorm(n = nsubj, mean = 0, sd = 1)) b.ia.subj <- data.frame(subject = 1:nsubj, vals = rnorm(n = nsubj, mean = eff.size.interaction, sd = 1)) # generate according to lin reg formula bayesrsdata <- data.frame(subject = rep(1:nsubj, each = ntrials), x.time = rep(xcont, each = ntrials/5), x.domain= rep(xcat, each = ntrials/10)) bayesrsdata$y <- 0 for (i in 1:nrow(bayesrsdata)){ bayesrsdata$y[i] <- b.subj.rand$vals[bayesrsdata$subject[i]==b.subj.rand$subject] + bayesrsdata$x.time[i] * (eff.size.cont+b.cont.subj$vals[bayesrsdata$subject[i]== b.cont.subj$subject]) + bayesrsdata$x.domain[i] * (eff.size.cat+b.cat.subj$vals[bayesrsdata$subject[i]== b.cat.subj$subject]) + bayesrsdata$x.time[i] * bayesrsdata$x.domain[i] * (eff.size.interaction+b.ia.subj$vals[bayesrsdata$subj[i]==b.ia.subj$subject]) } # add measurement error bayesrsdata$y <- bayesrsdata$y + rnorm(n = nrow(bayesrsdata), mean = 0, sd = 1) # create final data set recvars <- which(names(bayesrsdata) %in% c("subject", "item", "x.domain")) bayesrsdata[,recvars] <- lapply(bayesrsdata[,recvars], as.factor) save(bayesrsdata, file= "bayesrsdata.rda") ## End(Not run)
Computes Bayes Factors for hierarchical linear models including continuous predictors using the Savage-Dickey density ratio
modelrun(data, dv, dat.str, randvar.ia = NULL, corstr = NULL, nadapt = NULL, nburn = NULL, nsteps = NULL, checkconv = NULL, mcmc.save.indiv = NULL, plot.post = NULL, dic = NULL, path = NULL)
modelrun(data, dv, dat.str, randvar.ia = NULL, corstr = NULL, nadapt = NULL, nburn = NULL, nsteps = NULL, checkconv = NULL, mcmc.save.indiv = NULL, plot.post = NULL, dic = NULL, path = NULL)
data |
a |
dv |
|
dat.str |
a |
randvar.ia |
a |
corstr |
a |
nadapt |
number of MCMC steps to adapt the sampler (2000 by default). |
nburn |
number of MCMC steps to burn in the sampler (2000 by default). |
nsteps |
number of saved MCMC steps in all chains (100'000 by default). |
checkconv |
indicates that convergence statistics of the main model parameters should be returned in the console and that figures of the chains should be plotted when set to 1 (0 by default). |
mcmc.save.indiv |
indicates that the chains should be saved in a |
plot.post |
indicates that the 95 percent highest-density interval of the posterior of the group parameters should be plotted as a figure with the corresponding Bayes Factors when set to 1 (0 by default). |
dic |
indicates that the deviation information criterion (Spiegelhalter, Best, Carlin, & Linde, 2002) should be computed for a given model when set to 1 (0 by default). |
path |
defines directory where model is saved as .txt file and model name. Is set to file.path(tempdir(), "model.txt") by default. |
The argument corstr
can be used to model correlations between (a) pairs of predictors and (b) more than two predictors. When both is done within the same random variable, a predictor can only appear in (a) or (b).
modelrun
z-standardizes the dependent variable and the continuous independent variables. To obtain the posteriors in the original scale they have to be retransformed.
Bayes Factors are computed with the Savage-Dickey density ratio. We use the normal approximation (e.g., Wetzels, Raaijmakers, Jakab, & Wagenmakers, 2009) to estimate the density of the posterior.
returns a list with components:
bf
: a data.frame
object with the Bayes Factor estimates of the group parameters (aka fixed effects).
mcmcdf
: a data.frame
object with the saved MCMC chains.
dic
: DIC of the fitted model.
Thalmann, M., Niklaus, M. Part of this package uses code from John Kruschke.
Spiegelhalter, D. J., Best, N. G., Carlin, B. P., & van der Linde, A. (2002). Bayesian measures of model complexity and fit. Journal of the Royal Statistical Society: Series B (Statistical Methodology), 64(4), 583.
Wetzels, R., Raaijmakers, J. G. W., Jakab, E., & Wagenmakers, E.-J. (2009). How to quantify support for and against the null hypothesis: A flexible WinBUGS implementation of a default Bayesian t test. Psychonomic Bulletin & Review, 16(4), 752-760. https://doi.org/10.3758/PBR.16.4.752
data(bayesrsdata) #load data ## ----------------------------------------------------------------- ## Example 1: Estimation of Bayes Factors from a continuous ## independent variable (IV) with random slopes ## - repeated measures for each participant ## - continuous variable with 5 values: x.time ## ------------------------------------------------------------------ ## JAGS Sampler Settings # ----------------- # nr of adaptation, burn-in, and saved mcmc steps only for exemplary use nadapt = 2000 # number of adaptation steps nburn = 2000 # number of burn-in samples mcmcstep = 100000 # number of saved mcmc samples, min. should be 100'000 # Define model structure; dat.str <- data.frame(iv = c("x.time"), type = c("cont"), subject = c(1)) # name of random variable (here 'subject') needs to match data frame # Run modelrun function out <- modelrun(data = bayesrsdata, dv = "y", dat.str = dat.str, nadapt = nadapt, nburn = nburn, nsteps = mcmcstep, checkconv = 0) # Obtain Bayes factor bf <- out[[1]] bf ## ----------------------------------------------------------------- ## Example 2: Estimation of Bayes Factors from a continuous ## independent variable with random slopes that ## are correlated with the random slopes of a categorical variable. ## - Repeated measures for each participant ## - a continuous IV with 5 values: x.time ## - a categorical variable with 2 levels: x.domain ## ------------------------------------------------------------------ ## JAGS Sampler Settings # nr of adaptation, burn-in, and saved mcmc steps only for exemplary use # ----------------- nadapt = 2000 # number of adaptation steps nburn = 2000 # number of burn-in samples mcmcstep = 100000 # number of saved mcmc samples, min. should be 100'000 # Define model structure; # order of IVs: continuous variable(s) needs to go first dat.str <- data.frame(iv = c("x.time", "x.domain"), type = c("cont", "cat"), subject = c(1,1)) # name of random variable (here 'subject') needs to match data frame # Define random effect structure on interaction for each random variable ias.subject <- matrix(0, nrow=nrow(dat.str), ncol = nrow(dat.str)) ias.subject[c(2)] <- 1 randvar.ia <- list(ias.subject) # Define correlation structure between predictors within a random variable cor.subject <- matrix(0, nrow=nrow(dat.str)+1, ncol = nrow(dat.str)+1) cor.subject[c(2,3,6)] <- 1 corstr <- list(cor.subject) # Run modelrun function out <- modelrun(data = bayesrsdata, dv = "y", dat.str = dat.str, randvar.ia = randvar.ia, nadapt = nadapt, nburn = nburn, nsteps = mcmcstep, checkconv = 0, mcmc.save.indiv = 1, corstr = corstr) # Obtain Bayes factors for continous main effect, # categorical main effect, and their interaction bf <- out[[1]] bf
data(bayesrsdata) #load data ## ----------------------------------------------------------------- ## Example 1: Estimation of Bayes Factors from a continuous ## independent variable (IV) with random slopes ## - repeated measures for each participant ## - continuous variable with 5 values: x.time ## ------------------------------------------------------------------ ## JAGS Sampler Settings # ----------------- # nr of adaptation, burn-in, and saved mcmc steps only for exemplary use nadapt = 2000 # number of adaptation steps nburn = 2000 # number of burn-in samples mcmcstep = 100000 # number of saved mcmc samples, min. should be 100'000 # Define model structure; dat.str <- data.frame(iv = c("x.time"), type = c("cont"), subject = c(1)) # name of random variable (here 'subject') needs to match data frame # Run modelrun function out <- modelrun(data = bayesrsdata, dv = "y", dat.str = dat.str, nadapt = nadapt, nburn = nburn, nsteps = mcmcstep, checkconv = 0) # Obtain Bayes factor bf <- out[[1]] bf ## ----------------------------------------------------------------- ## Example 2: Estimation of Bayes Factors from a continuous ## independent variable with random slopes that ## are correlated with the random slopes of a categorical variable. ## - Repeated measures for each participant ## - a continuous IV with 5 values: x.time ## - a categorical variable with 2 levels: x.domain ## ------------------------------------------------------------------ ## JAGS Sampler Settings # nr of adaptation, burn-in, and saved mcmc steps only for exemplary use # ----------------- nadapt = 2000 # number of adaptation steps nburn = 2000 # number of burn-in samples mcmcstep = 100000 # number of saved mcmc samples, min. should be 100'000 # Define model structure; # order of IVs: continuous variable(s) needs to go first dat.str <- data.frame(iv = c("x.time", "x.domain"), type = c("cont", "cat"), subject = c(1,1)) # name of random variable (here 'subject') needs to match data frame # Define random effect structure on interaction for each random variable ias.subject <- matrix(0, nrow=nrow(dat.str), ncol = nrow(dat.str)) ias.subject[c(2)] <- 1 randvar.ia <- list(ias.subject) # Define correlation structure between predictors within a random variable cor.subject <- matrix(0, nrow=nrow(dat.str)+1, ncol = nrow(dat.str)+1) cor.subject[c(2,3,6)] <- 1 corstr <- list(cor.subject) # Run modelrun function out <- modelrun(data = bayesrsdata, dv = "y", dat.str = dat.str, randvar.ia = randvar.ia, nadapt = nadapt, nburn = nburn, nsteps = mcmcstep, checkconv = 0, mcmc.save.indiv = 1, corstr = corstr) # Obtain Bayes factors for continous main effect, # categorical main effect, and their interaction bf <- out[[1]] bf