├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── NAMESPACE ├── NEWS.md ├── R ├── bmlm-package.R ├── data.R ├── mlm.R ├── mlm_plot.R ├── stanmodels.R ├── utils.R └── zzz.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── bmlm.Rproj ├── configure ├── configure.win ├── cran-comments.md ├── data ├── BLch9.rda └── MEC2010.rda ├── docs ├── 404.html ├── articles │ ├── FAQ │ │ └── FAQ.html │ ├── Spaghetti-Plots │ │ ├── Spaghetti-Plots.html │ │ └── Spaghetti-Plots_files │ │ │ └── figure-html │ │ │ ├── unnamed-chunk-5-1.png │ │ │ └── unnamed-chunk-6-1.png │ ├── bmlm-blch9 │ │ ├── bmlm-blch9.html │ │ ├── bmlm-blch9_files │ │ │ └── figure-html │ │ │ │ ├── unnamed-chunk-10-1.png │ │ │ │ ├── unnamed-chunk-11-1.png │ │ │ │ ├── unnamed-chunk-11-2.png │ │ │ │ ├── unnamed-chunk-11-3.png │ │ │ │ ├── unnamed-chunk-12-1.png │ │ │ │ ├── unnamed-chunk-13-1.png │ │ │ │ ├── unnamed-chunk-14-1.png │ │ │ │ ├── unnamed-chunk-16-1.png │ │ │ │ ├── unnamed-chunk-17-1.png │ │ │ │ ├── unnamed-chunk-18-1.png │ │ │ │ ├── unnamed-chunk-22-1.png │ │ │ │ ├── unnamed-chunk-23-1.png │ │ │ │ ├── unnamed-chunk-25-1.png │ │ │ │ └── unnamed-chunk-26-1.png │ │ └── table.png │ ├── index.html │ └── ms │ │ ├── bmlm-ms.pdf │ │ └── bmlm-ms_files │ │ └── figure-latex │ │ ├── example1-path-plot-1.pdf │ │ ├── example1-path-plot-1.png │ │ ├── example1-pme-plot-1.pdf │ │ ├── example1-pme-plot-1.png │ │ ├── example1-pop-hist-1.pdf │ │ ├── example1-pop-hist-1.png │ │ ├── example1-pop-violin-1.pdf │ │ ├── example1-pop-violin-1.png │ │ ├── example1-subj-coefs-1.pdf │ │ ├── example1-subj-coefs-1.png │ │ ├── lkj-prior-example-1.pdf │ │ ├── lkj-prior-example-1.png │ │ ├── spaghetti-plot-b-binary-y-1.pdf │ │ ├── spaghetti-plot-b-binary-y-1.png │ │ ├── spaghetti-plot-binary-y-1.pdf │ │ ├── spaghetti-plot-binary-y-1.png │ │ ├── template-mediation-plot-1.pdf │ │ └── template-mediation-plot-1.png ├── authors.html ├── bootstrap-toc.css ├── bootstrap-toc.js ├── docsearch.css ├── docsearch.js ├── index.html ├── jquery.sticky-kit.min.js ├── link.svg ├── news │ └── index.html ├── pkgdown.css ├── pkgdown.js ├── pkgdown.yml └── reference │ ├── BLch9.html │ ├── MEC2010.html │ ├── bmlm-package.html │ ├── bmlm.html │ ├── index.html │ ├── isolate.html │ ├── mlm.html │ ├── mlm_pars_plot.html │ ├── mlm_path_plot-1.png │ ├── mlm_path_plot.html │ ├── mlm_spaghetti_plot.html │ ├── mlm_summary.html │ └── tab2doc.html ├── inst ├── CITATION ├── chunks │ └── license.stan ├── include │ └── stan_meta_header.hpp └── stan │ ├── bmlm.stan │ ├── bmlm_binary_y.stan │ └── chunks │ ├── common_functions.stan │ ├── data.stan │ ├── generated_quantities.stan │ ├── model.stan │ ├── parameters.stan │ ├── transformed_data.stan │ └── transformed_parameters.stan ├── man ├── BLch9.Rd ├── MEC2010.Rd ├── bmlm-package.Rd ├── isolate.Rd ├── mlm.Rd ├── mlm_pars_plot.Rd ├── mlm_path_plot.Rd ├── mlm_spaghetti_plot.Rd └── mlm_summary.Rd ├── src └── init.cpp └── vignettes ├── FAQ ├── FAQ.Rmd └── bibliography.bib ├── Spaghetti-Plots └── Spaghetti-Plots.Rmd ├── bmlm-blch9 ├── apa.csl ├── bibliography.bib └── bmlm-blch9.Rmd └── ms ├── bibliography.bib ├── bmlm-ms.Rmd └── includes ├── compare-bmlm-mplus.rda └── multilevel-mediation.pdf /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^CRAN-RELEASE$ 2 | ^.*\.Rproj$ 3 | ^\.Rproj\.user$ 4 | ^README\.Rmd$ 5 | ^README-.*\.png$ 6 | ^docs$ 7 | ^_pkgdown\.yml$ 8 | ^cran-comments\.md$ 9 | ^\cleanup* 10 | ^.*\.o$ 11 | ^vignettes/* 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | inst/doc 6 | src/* 7 | !src/init.cpp 8 | data-raw/ 9 | *_cache/ 10 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: bmlm 2 | Title: Bayesian Multilevel Mediation 3 | Version: 1.3.15 4 | Date: 2023-09-25 5 | Authors@R: person(given = "Matti", 6 | family = "Vuorre", 7 | email = "m.j.vuorre@tilburguniversity.edu", 8 | role = c("aut", "cre"), 9 | comment = c(ORCID = "0000-0001-5052-066X")) 10 | Description: Easy estimation of Bayesian multilevel mediation models with Stan. 11 | URL: https://github.com/mvuorre/bmlm/ 12 | BugReports: https://github.com/mvuorre/bmlm/issues/ 13 | License: GPL (>= 3) 14 | LazyData: true 15 | NeedsCompilation: yes 16 | Depends: 17 | R (>= 3.4.0), 18 | Rcpp (>= 0.12.0) 19 | Imports: 20 | ggplot2 (>= 2.0.0), 21 | methods, 22 | RcppParallel (>= 5.0.1), 23 | rstan (>= 2.26.0), 24 | rstantools (>= 2.3.1.1) 25 | Suggests: 26 | qgraph, 27 | knitr, 28 | rmarkdown, 29 | reshape2, 30 | dplyr 31 | LinkingTo: 32 | BH (>= 1.66.0), 33 | Rcpp (>= 0.12.0), 34 | RcppEigen (>= 0.3.3.3.0), 35 | RcppParallel (>= 5.0.1), 36 | rstan (>= 2.26.0), 37 | StanHeaders (>= 2.26.0) 38 | RoxygenNote: 7.2.3 39 | Encoding: UTF-8 40 | SystemRequirements: GNU make 41 | Biarch: true 42 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(isolate) 4 | export(mlm) 5 | export(mlm_pars_plot) 6 | export(mlm_path_plot) 7 | export(mlm_spaghetti_plot) 8 | export(mlm_summary) 9 | import(Rcpp) 10 | import(ggplot2) 11 | import(methods) 12 | import(rstan) 13 | importFrom(rstan,sampling) 14 | importFrom(stats,as.formula) 15 | importFrom(stats,model.matrix) 16 | importFrom(stats,quantile) 17 | useDynLib(bmlm, .registration = TRUE) 18 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # bmlm 1.3.15 2 | 3 | Minor update: Use new Stan array syntax thanks to Andrew Johnson. 4 | 5 | # bmlm 1.3.14 6 | 7 | Minor fix: Let rstantools generate Makevars 8 | 9 | # bmlm 1.3.13 10 | 11 | Minor housekeeping: 12 | 13 | - Updated compiler flags thanks to @jgabry 14 | - Updated contact info 15 | 16 | # bmlm 1.3.12 17 | 18 | Updated compiler flags for new version of RStan thanks to Andrew Johnson. 19 | 20 | # bmlm 1.3.11 21 | 22 | Fix package for staged installation 23 | 24 | # bmlm 1.3.9 25 | 26 | * Fix NOTE about methods package 27 | 28 | # bmlm 1.3.8 29 | 30 | * Update to C++14, thanks to Ben Goodrich. 31 | 32 | # bmlm 1.3.7 33 | 34 | * Removed `tab2doc()`, package no longer needs archived ReporteRs package. 35 | 36 | # bmlm 1.3.6 37 | 38 | * Deprecate `tab2doc()` because required package ReporteRs is archived. 39 | 40 | # bmlm 1.3.5 41 | 42 | * Fix (harmless) constructor error message 43 | 44 | # bmlm 1.3.4 45 | 46 | * Minor cleaning of Stan code 47 | * Fix typos in documentation 48 | 49 | # bmlm 1.3.3 50 | 51 | * Change the label '%me' to 'pme' (for proportion mediated effect) in output of mlm_path_plot(..., text = TRUE). 52 | 53 | # bmlm 1.3.2 54 | 55 | * Add options to `mlm_spaghetti_plot()` to allow jittering and adjusting size of the error bars. 56 | 57 | # bmlm 1.3.1 58 | 59 | * `mlm_spaghetti_plot()` now has argument `mx` which can be set to `mx = "data"` to plot the spaghetti plot of the M - Y relationship (b path) such that the X values are from data, and not fitted values from the X - M model (a path). The argument defaults to `mx = "fitted"`, such that the X axis values of the M - Y spaghetti plot are fitted values. 60 | 61 | # bmlm 1.3.0 62 | 63 | * New function `mlm_spaghetti_plot()` for visualizing model-fitted values for paths a (X->M regression) and b (M->Y regression) 64 | 65 | # bmlm 1.2.10 66 | 67 | * Default priors are now $Normal(0, 1000)$ for regression coefficients, and $Cauchy(0, 50)$ for group-level SDs 68 | * `mlm_summary()` now gives only population level parameters by default, and group-level parameters when `pars = "random"` 69 | * Renamed the mediated effect parameter to *me* to distinguish it from the product of *a* and *b* (similarly for group-level *u_me*) 70 | * `mlm_path_plot()` now draws a template if no model is entered (i.e. `template` argument is deprecated) 71 | * `mlm_path_plot()` now by default also shows SDs of group-level effects. This behavior can be turned off by specifying `random = FALSE` 72 | * The fitted model object doesn't contain the whole covariance matrix anymore, but now contains the group-level intercepts 73 | * New example data set included in package: `MEC2010` 74 | * Posterior standard deviation is now referred to as SE in `mlm_summary()` 75 | 76 | 77 | 78 | # bmlm 1.2.9 79 | 80 | Removed sigma_y from being modeled when binary_y = TRUE. 81 | 82 | # bmlm 1.2.1 83 | 84 | Removed posterior probabilities from default outputs. 85 | 86 | Added type = "violin" as option for plotting coefficients with mlm_pars_plot(). 87 | 88 | # bmlm 1.2.0 89 | 90 | Users may now change each individual regression parameter's prior, instead of classes of priors. 91 | 92 | Users may now change the shape parameter of the LKJ prior. 93 | 94 | # bmlm 1.1.1 95 | 96 | Coefficient plots now reorder parameter estimates, if user has requested varying effects. 97 | 98 | Path plot now by default does not scale the edges. 99 | 100 | # bmlm 1.1.0 101 | 102 | ## Major update 103 | 104 | bmlm now uses pre-compiled C++ code for the Stan models, which eliminates the need to compile a model each time `mlm()` is run. This significantly speeds up model estimation. 105 | 106 | ## Minor update 107 | 108 | The Stan code used by `mlm()` is now built from separate chunks, allowing more flexible and robust model development. 109 | 110 | # bmlm 1.0.0 111 | 112 | Initial release to CRAN. 113 | -------------------------------------------------------------------------------- /R/bmlm-package.R: -------------------------------------------------------------------------------- 1 | #' bmlm: Easy estimation of Bayesian multilevel mediation models with Stan. 2 | #' 3 | #' See \url{https://mvuorre.github.io/bmlm/} for a short tutorial. 4 | #' 5 | #' @docType package 6 | #' @name bmlm-package 7 | #' @aliases bmlm 8 | #' @useDynLib bmlm, .registration = TRUE 9 | #' @import methods 10 | #' @import Rcpp 11 | #' @importFrom rstan sampling 12 | NULL 13 | -------------------------------------------------------------------------------- /R/data.R: -------------------------------------------------------------------------------- 1 | #' Relationship between work stressors, work dissatisfaction, 2 | #' and relationship dissatisfaction. 3 | #' 4 | #' @description Simulated data from Intensive Longitudinal Methods: 5 | #' An Introduction to Diary and Experience Sampling Research. 6 | #' (Bolger, & Laurenceau, 2013, chapter 9; 7 | #' \url{http://www.intensivelongitudinal.com/index.html}). 8 | #' 9 | #' @docType data 10 | #' 11 | #' @usage data(BLch9) 12 | #' 13 | #' @format A data frame with 2100 rows and 8 variables: 14 | #' \describe{ 15 | #' \item{id}{ID of study participant} 16 | #' \item{time}{Time} 17 | #' \item{fwkstrs}{Number of work stressors} 18 | #' \item{fwkdis}{Work dissatisfaction rating} 19 | #' \item{freldis}{Relationship dissatisfaction} 20 | #' \item{x}{Subject-mean deviated number of work stressors} 21 | #' \item{m}{Subject-mean deviated work dissatisfaction rating} 22 | #' \item{y}{Subject-mean deviated relationship dissatisfaction} 23 | #' } 24 | #' 25 | #' @source \url{http://www.intensivelongitudinal.com/datasets.html} 26 | "BLch9" 27 | 28 | #' Judgments of performance in a video game 29 | #' 30 | #' @description Data from an experiment where participants rated their 31 | #' performance in a video game in two conditions. 32 | #' (Experiment 1 in Metcalfe, Eich, & Castel, 2010; 33 | #' \url{https://www.sciencedirect.com/science/article/pii/S0010027710001113}). 34 | #' 35 | #' @docType data 36 | #' 37 | #' @usage data(MEC2010) 38 | #' 39 | #' @format A data frame with 344 rows and 4 variables: 40 | #' \describe{ 41 | #' \item{subj}{Subject id number.} 42 | #' \item{lag}{Lag condition (0 = no lag, 1 = 250ms lag).} 43 | #' \item{hr}{Hit rate.} 44 | #' \item{jop}{Judgment of Performance.} 45 | #' } 46 | #' 47 | #' @source Metcalfe, J., Eich, T. S., & Castel, A. D. (2010). 48 | #' Metacognition of agency across the lifespan. 49 | #' Cognition, 116(2), 267-282. 50 | #' \doi{10.1016/j.cognition.2010.05.009} 51 | "MEC2010" 52 | -------------------------------------------------------------------------------- /R/mlm.R: -------------------------------------------------------------------------------- 1 | #' Estimate a multilevel mediation model 2 | #' 3 | #' Estimates a Bayesian multilevel mediation model using Stan. 4 | #' 5 | #' @param d A \code{data.frame} or a \code{data_frame}. 6 | #' @param id Column of participant IDs in \code{data}. 7 | #' @param x Column of X values in \code{data}. 8 | #' @param m Column of M values in \code{data}. 9 | #' @param y Column of Y values in \code{data}. 10 | #' @param priors A list of named values to be used as the prior scale 11 | #' parameters. See details. 12 | #' @param binary_y Set to TRUE if y is binary and should be modelled 13 | #' with logistic regression. Defaults to FALSE (y treated as continuous.) 14 | #' This feature is experimental. 15 | #' @param ... Other optional parameters passed to \code{rstan::stan()}. 16 | #' 17 | #' @return An object of S4 class stanfit, with all its available methods. 18 | #' 19 | #' @author Matti Vuorre \email{mv2521@columbia.edu} 20 | #' 21 | #' @details Draw samples from the joint posterior distribution of a 22 | #' multilevel mediation model using Stan. 23 | #' 24 | #' \subsection{Priors}{ 25 | #' 26 | #' Users may pass a list of named values for the \code{priors} argument. 27 | #' The values will be used to define the scale parameter of the 28 | #' respective prior distributions. 29 | #' This list may specify some or all of the following parameters: 30 | #' 31 | #' \describe{ 32 | #' \item{dy, dm}{Regression intercepts (for Y and M as outcomes, respectively.)} 33 | #' \item{a, b, cp}{Regression slopes.} 34 | #' \item{tau_x}{Varying effects SDs for above parameters (e.g replace x with a.)} 35 | #' \item{lkj_shape}{Shape parameter for the LKJ prior.} 36 | #' } 37 | #' See examples for specifying the following: Gaussian distributions with SD = 10 38 | #' as priors for the intercepts, Gaussians with SD = 2 for the slopes, 39 | #' Half-Cauchy distributions with scale parameters 1 for the varying effects 40 | #' SDs, and an LKJ prior of 2. 41 | #' } 42 | #' 43 | #' @examples 44 | #' \dontrun{ 45 | #' ## Run example from Bolger and Laurenceau (2013) 46 | #' data(BLch9) 47 | #' fit <- mlm(BLch9) 48 | #' mlm_summary(fit) 49 | #' 50 | #' ### With priors 51 | #' Priors <- list(dy = 10, dm = 10, a = 2, b = 2, cp = 2, 52 | #' tau_dy = 1, tau_dm = 1, tau_a = 1, tau_b = 1, tau_cp = 1, 53 | #' lkj_shape = 2) 54 | #' fit <- mlm(BLch9, priors = Priors) 55 | #' } 56 | #' 57 | #' @import rstan 58 | #' @export 59 | mlm <- function(d = NULL, id = "id", x = "x", m = "m", y = "y", 60 | priors = NULL, 61 | binary_y = FALSE, 62 | ...) { 63 | 64 | # Check for data 65 | if (is.null(d)) stop("No data entered") 66 | if (class(d)[1] == "tbl_df") d <- as.data.frame(d) # Allow tibbles 67 | 68 | # Check priors 69 | default_priors <- list( 70 | dm = 1000, tau_dm = 50, 71 | dy = 1000, tau_dy = 50, 72 | a = 1000, tau_a = 50, 73 | b = 1000, tau_b = 50, 74 | cp = 1000, tau_cp = 50, 75 | lkj_shape = 1 76 | ) 77 | if (is.null(priors$dm)) priors$dm <- default_priors$dm 78 | if (is.null(priors$dy)) priors$dy <- default_priors$dy 79 | if (is.null(priors$a)) priors$a <- default_priors$a 80 | if (is.null(priors$b)) priors$b <- default_priors$b 81 | if (is.null(priors$cp)) priors$cp <- default_priors$cp 82 | if (is.null(priors$tau_dm)) priors$tau_dm <- default_priors$tau_dm 83 | if (is.null(priors$tau_dy)) priors$tau_dy <- default_priors$tau_dy 84 | if (is.null(priors$tau_a)) priors$tau_a <- default_priors$tau_a 85 | if (is.null(priors$tau_b)) priors$tau_b <- default_priors$tau_b 86 | if (is.null(priors$tau_cp)) priors$tau_cp <- default_priors$tau_cp 87 | if (is.null(priors$lkj_shape)) priors$lkj_shape <- default_priors$lkj_shape 88 | names(priors) <- lapply(names(priors), function(x) paste0("prior_", x)) 89 | 90 | # Create a data list for Stan 91 | ld <- list() 92 | ld$id = as.integer(as.factor(as.character(d[,id]))) # Sequential IDs 93 | ld$X = d[,x] 94 | ld$M = d[,m] 95 | ld$Y = d[,y] 96 | ld$J <- length(unique(ld$id)) 97 | ld$N <- nrow(d) 98 | ld <- append(ld, priors) 99 | 100 | # Choose model 101 | if (binary_y) { 102 | # model_file <- system.file("stan/bmlm_binary_y.stan", package="bmlm") 103 | model_s <- stanmodels$bmlm_binary_y 104 | } else { 105 | # model_file <- system.file("stan/bmlm.stan", package="bmlm") 106 | model_s <- stanmodels$bmlm 107 | } 108 | 109 | # Sample from model 110 | message("Estimating model, please wait.") 111 | fit <- rstan::sampling( 112 | object = model_s, 113 | data = ld, 114 | pars = c("U", "z_U", "L_Omega", "Tau", "Sigma"), 115 | include = FALSE, 116 | ...) 117 | 118 | return(fit) 119 | } 120 | -------------------------------------------------------------------------------- /R/stanmodels.R: -------------------------------------------------------------------------------- 1 | # Generated by rstantools. Do not edit by hand. 2 | 3 | # names of stan models 4 | stanmodels <- c("bmlm", "bmlm_binary_y") 5 | 6 | # load each stan module 7 | Rcpp::loadModule("stan_fit4bmlm_mod", what = TRUE) 8 | Rcpp::loadModule("stan_fit4bmlm_binary_y_mod", what = TRUE) 9 | 10 | # instantiate each stanmodel object 11 | stanmodels <- sapply(stanmodels, function(model_name) { 12 | # create C++ code for stan model 13 | stan_file <- if(dir.exists("stan")) "stan" else file.path("inst", "stan") 14 | stan_file <- file.path(stan_file, paste0(model_name, ".stan")) 15 | stanfit <- rstan::stanc_builder(stan_file, 16 | allow_undefined = TRUE, 17 | obfuscate_model_name = FALSE) 18 | stanfit$model_cpp <- list(model_cppname = stanfit$model_name, 19 | model_cppcode = stanfit$cppcode) 20 | # create stanmodel object 21 | methods::new(Class = "stanmodel", 22 | model_name = stanfit$model_name, 23 | model_code = stanfit$model_code, 24 | model_cpp = stanfit$model_cpp, 25 | mk_cppmodule = function(x) get(paste0("rstantools_model_", model_name))) 26 | }) 27 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | #' Print a summary of the estimated multilevel mediation model 2 | #' 3 | #' Prints the estimated parameters (numerical summaries of the marginal 4 | #' posterior distributions). 5 | #' 6 | #' @param mod A \code{stanfit} object obtained from \code{mlm()} 7 | #' @param level "Confidence" level; Defines the limits of the credible intervals. 8 | #' Defaults to .95 (i.e. displays 95\% CIs.) 9 | #' @param pars Parameters to summarize. Defaults to main average-level 10 | #' parameters. See Details for more information. 11 | #' @param digits How many decimal points to display in the output. Defaults to 2. 12 | #' 13 | #' @return A \code{data.frame} summarizing the estimated multilevel 14 | #' mediation model: 15 | #' \describe{ 16 | #' \item{Parameter}{Name of parameter} 17 | #' \item{Mean}{Mean of parameter's posterior distribution.} 18 | #' \item{Median}{Median of parameter's posterior distribution.} 19 | #' \item{SE}{Standard deviation of parameter's posterior distribution.} 20 | #' \item{ci_lwr}{The lower limit of Credible Intervals.} 21 | #' \item{ci_upr}{The upper limit of Credible Intervals.} 22 | #' \item{n_eff}{Number of efficient samples.} 23 | #' \item{Rhat}{Should be 1.00.} 24 | #'} 25 | #' 26 | #' @details After estimating a model (drawing samples from the joint posterior 27 | #' probability distribution) with \code{mlm()}, show the estimated results 28 | #' by using \code{mlm_summary(fit)}, where \code{fit} is an object containing 29 | #' the fitted model. 30 | #' 31 | #' The function shows, for each parameter specified with \code{pars}, 32 | #' the posterior mean, and limits of the Credible Interval as specified 33 | #' by \code{level}. For example, \code{level = .91} shows a 34 | #' 91\% Credible Interval, which summarizes the central 91\% mass of 35 | #' the marginal posterior distribution. 36 | #' 37 | #' \subsection{Parameters}{ 38 | #' By default, \code{mlm()} estimates and returns a large number of parameters, 39 | #' including the varying effects, and their associated standard deviations. 40 | #' However, \code{mlm_summay()} by default only displays a subset of the 41 | #' estimated parameters: 42 | #' 43 | #' \describe{ 44 | #' \item{a}{Regression slope of the X -> M relationship.} 45 | #' \item{b}{Regression slope of the M -> Y relationship.} 46 | #' \item{cp}{Regression slope of the X -> Y relationship. (Direct effect.)} 47 | #' \item{me}{Mediated effect (\eqn{a * b + \sigma_{{a_j}{b_j}}}).} 48 | #' \item{c}{Total effect of X on Y. ( \eqn{cp + me} )} 49 | #' \item{pme}{Percent mediated effect.} 50 | #'} 51 | #' The user may specify \code{pars = NULL} to display all estimated parameters. 52 | #' Other options include e.g. \code{pars = "tau"} to display the varying 53 | #' effects' standard deviations. To display all the group-level parameters 54 | #' (also known as random effects) only, specify \code{pars = "random"}. 55 | #' With this argument, \code{mlm_summary()} prints the following parameters: 56 | #' 57 | #' \describe{ 58 | #' \item{tau_a}{Standard deviation of subject-level \code{a_j}s.} 59 | #' \item{tau_b}{Standard deviation of subject-level \code{b_j}s.} 60 | #' \item{tau_cp}{Standard deviation of subject-level \code{c\'_j}s.} 61 | #' \item{covab}{Estimated covariance of \code{a_j} and \code{b_j}s.} 62 | #' \item{corrab}{Estimated correlation of \code{a_j} and \code{b_j}s.} 63 | #'} 64 | #' 65 | #' To learn more about the additional parameters, refer to the Stan code 66 | #' (\code{cat(get_stancode(fit))}). 67 | #'} 68 | #' 69 | #' @author Matti Vuorre \email{mv2521@columbia.edu} 70 | #' 71 | #' @export 72 | mlm_summary <- function( 73 | mod = NULL, 74 | level = .95, 75 | pars = c("a", "b", "cp", "me", "c", "pme"), 76 | digits = 2 77 | ){ 78 | 79 | # Check that mod is a Stanfit object 80 | if (!is(mod, "stanfit")) stop("Model is not a stanfit object.") 81 | 82 | # Choose which parameters to display 83 | if (is.null(pars)) pars <- mod@sim$pars_oi # Return all parameters 84 | if (any(pars == "random")) pars <- c( 85 | "tau_a", "tau_b", "tau_cp", "covab", "corrab" 86 | ) 87 | 88 | # Obtain model summary from Stanfit 89 | lower_ci <- .5 - (level/2) 90 | upper_ci <- .5 + (level/2) 91 | 92 | mod_sum <- rstan::summary(object = mod, 93 | probs = c(lower_ci, .5, upper_ci), 94 | pars = pars)$summary[,-2] 95 | # Clean and get post. probs 96 | if (is.null(dim(mod_sum))) { # If only one param entered 97 | mod_sum <- data.frame(t(mod_sum)) 98 | mod_sum <- data.frame(t(apply(mod_sum, 2, round, digits = digits))) 99 | Names <- pars 100 | } else { 101 | mod_sum <- data.frame(mod_sum) 102 | mod_sum <- data.frame(apply(mod_sum, 2, round, digits = digits)) 103 | Names <- row.names(mod_sum) 104 | } 105 | mod_sum$n_eff <- floor(mod_sum$n_eff) 106 | mod_sum$Parameter <- Names 107 | mod_sum <- mod_sum[,c(8,1,2,4,3,5,6,7)] 108 | names(mod_sum) <- c("Parameter", "Mean", "SE", "Median", 109 | paste0(lower_ci*100, "%"), paste0(upper_ci*100, "%"), 110 | "n_eff", "Rhat") 111 | row.names(mod_sum) <- NULL 112 | return(mod_sum) 113 | } 114 | 115 | #' Create isolated within- (and optionally between-) person variables. 116 | #' 117 | #' Creates variables that represent pure within- and between-person predictors. 118 | #' 119 | #' @param d A \code{data.frame}. 120 | #' @param by A vector of values in \code{d} by which the data is clustered. 121 | #' i.e. a vector of unique participant IDs. 122 | #' @param value Names of columns in \code{d} to isolate. Multiple values can be 123 | #' given by \code{value = c("var1", "var2", "var3")} 124 | #' @param z Should the created values be standardized (defaults to FALSE). 125 | #' @param which Which component to return. "within" (default) returns 126 | #' within-person deviations only; "between" returns between-person means only; 127 | #' "both" returns both. 128 | #' 129 | #' @return A \code{data.frame} with additional columns for the within- and 130 | #' between-person variables. The new columns are labelled _cw for 131 | #' centered-within and _cb for centered-between. 132 | #' 133 | #' @author Matti Vuorre \email{mv2521@columbia.edu} 134 | #' 135 | #' @examples 136 | #' # Create within-person deviations of work stressors in BLch9. 137 | #' data(BLch9) 138 | #' BLch9 <- isolate(BLch9, by = "id", value = "fwkstrs") 139 | #' head(BLch9) # Now has new column for within-person work stressors. 140 | #' 141 | #' @export 142 | isolate <- function(d = NULL, by = NULL, value = NULL, 143 | z = FALSE, which = "within"){ 144 | for (val in value){ 145 | oldnames <- names(d) 146 | d$c <- as.numeric(scale(d[,val], scale = z)) # Mean centered (or Zd) 147 | d <- within(d, 148 | {cb = stats::ave( 149 | c, d[,by], 150 | FUN = function(x) mean(x, na.rm = T)) 151 | }) 152 | d$cw <- d$c - d$cb # Within-person 153 | names(d) <- c(oldnames, 154 | paste0(val, "_c"), 155 | paste0(val, "_cb"), 156 | paste0(val, "_cw")) 157 | N <- dim(d)[2] 158 | if (which == "within") d <- d[,-c(N-1, N-2)] 159 | else if (which == "between") d <- d[,-c(N, N-2)] 160 | else d <- d[,-(N-2)] 161 | } 162 | return(d) 163 | } 164 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onLoad <- function(libname, pkgname) { 2 | if (!("methods" %in% .packages())) attachNamespace("methods") 3 | modules <- paste0("stan_fit4", names(stanmodels), "_mod") 4 | for (m in modules) loadModule(m, what = TRUE) 5 | } 6 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "bmlm: R package for Bayesian multilevel mediation" 3 | output: github_document 4 | --- 5 | 6 | 7 | 8 | ```{r, echo = FALSE} 9 | knitr::opts_chunk$set( 10 | collapse = TRUE, 11 | comment = "#>", 12 | fig.path = "README-" 13 | ) 14 | ``` 15 | 16 | # bmlm 17 | 18 | [![CRAN version](https://www.r-pkg.org/badges/version/bmlm)](https://www.r-pkg.org/pkg/bmlm) 19 | [![download-badge](https://cranlogs.r-pkg.org/badges/bmlm)](https://cran.r-project.org/package=bmlm) 20 | 21 | **bmlm** is an R package providing convenient methods for Bayesian estimation of multilevel mediation models using [Stan](https://mc-stan.org/). 22 | 23 | The package's source code is hosted on [GitHub](https://github.com/mvuorre/bmlm/). More information can be found on the [bmlm's website](https://mvuorre.github.io/bmlm/). 24 | 25 | # Install 26 | 27 | ## Requirements 28 | 29 | Please ensure you have the latest version of R installed. Windows users may need to install RTools, and OS X users may need to install XCode (see . 30 | 31 | ## Install from CRAN 32 | 33 | To install the latest stable version of bmlm from CRAN, run 34 | 35 | ```r 36 | install.packages("bmlm") 37 | ``` 38 | 39 | ## Install from GitHub 40 | 41 | Sometimes the stable version on CRAN is not the latest version of bmlm. bmlm is developed on GitHub, and users may obtain the latest (development) version from GitHub directly. 42 | 43 | The latest development version of bmlm requires [remotes](https://cran.r-project.org/package=remotes) for installation. If you don't have the remotes package installed in R, first run this line: 44 | 45 | ```r 46 | install.packages("remotes") 47 | ``` 48 | 49 | Then proceed to install bmlm from GitHub: 50 | 51 | ```r 52 | remotes::install_github("mvuorre/bmlm") 53 | ``` 54 | 55 | # Information 56 | 57 | Please contact the author of the package for questions and suggestions. I recommend creating a new issue on GitHub. 58 | 59 | # Citation 60 | 61 | If you use this software, please cite it: 62 | 63 | ```{r} 64 | citation("bmlm") 65 | ``` 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bmlm: R package for Bayesian multilevel mediation 2 | ================ 3 | 4 | 5 | 6 | # bmlm 7 | 8 | [![CRAN 9 | version](https://www.r-pkg.org/badges/version/bmlm)](https://www.r-pkg.org/pkg/bmlm) 10 | [![download-badge](https://cranlogs.r-pkg.org/badges/bmlm)](https://cran.r-project.org/package=bmlm) 11 | 12 | **bmlm** is an R package providing convenient methods for Bayesian 13 | estimation of multilevel mediation models using 14 | [Stan](https://mc-stan.org/). 15 | 16 | The package’s source code is hosted on 17 | [GitHub](https://github.com/mvuorre/bmlm/). More information can be 18 | found on the [bmlm’s website](https://mvuorre.github.io/bmlm/). 19 | 20 | # Install 21 | 22 | ## Requirements 23 | 24 | Please ensure you have the latest version of R installed. Windows users 25 | may need to install RTools, and OS X users may need to install XCode 26 | (see . 27 | 28 | ## Install from CRAN 29 | 30 | To install the latest stable version of bmlm from CRAN, run 31 | 32 | ``` r 33 | install.packages("bmlm") 34 | ``` 35 | 36 | ## Install from GitHub 37 | 38 | Sometimes the stable version on CRAN is not the latest version of bmlm. 39 | bmlm is developed on GitHub, and users may obtain the latest 40 | (development) version from GitHub directly. 41 | 42 | The latest development version of bmlm requires 43 | [remotes](https://cran.r-project.org/package=remotes) for installation. 44 | If you don’t have the remotes package installed in R, first run this 45 | line: 46 | 47 | ``` r 48 | install.packages("remotes") 49 | ``` 50 | 51 | Then proceed to install bmlm from GitHub: 52 | 53 | ``` r 54 | remotes::install_github("mvuorre/bmlm") 55 | ``` 56 | 57 | # Information 58 | 59 | Please contact the author of the package for questions and suggestions. 60 | I recommend creating a new issue on GitHub. 61 | 62 | # Citation 63 | 64 | If you use this software, please cite it: 65 | 66 | ``` r 67 | citation("bmlm") 68 | #> To cite package 'bmlm' in publications use: 69 | #> 70 | #> Vuorre M, Bolger N (2018). "Within-subject mediation analysis for 71 | #> experimental data in cognitive psychology and neuroscience." 72 | #> _Behavior Research Methods_. doi:10.3758/s13428-017-0980-9 73 | #> . 74 | #> 75 | #> Vuorre M (2023). _bmlm: Bayesian Multilevel Mediation_. R package 76 | #> version 1.3.15, . 77 | #> 78 | #> To see these entries in BibTeX format, use 'print(, 79 | #> bibtex=TRUE)', 'toBibtex(.)', or set 80 | #> 'options(citation.bibtex.max=999)'. 81 | ``` 82 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | template: 2 | params: 3 | bootswatch: simplex 4 | -------------------------------------------------------------------------------- /bmlm.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: No 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source --preclean 21 | -------------------------------------------------------------------------------- /configure: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # Generated by rstantools. Do not edit by hand. 4 | 5 | "${R_HOME}/bin/Rscript" -e "rstantools::rstan_config()" 6 | -------------------------------------------------------------------------------- /configure.win: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # Generated by rstantools. Do not edit by hand. 4 | 5 | "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "rstantools::rstan_config()" 6 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | 3 | - Local MacOS Ventura, R 4.3.1 4 | - Remote Fedora Linux, R-devel, clang, gfortran 5 | - winbuilder 6 | 7 | ## R CMD check results 8 | 9 | ❯ checking dependencies in R code ... NOTE 10 | Namespaces in Imports field not imported from: 11 | 'RcppParallel' 'rstantools' 12 | All declared Imports should be used. 13 | 14 | - Required by dependency rstan. 15 | 16 | ❯ checking for GNU extensions in Makefiles ... NOTE 17 | GNU make is a SystemRequirements. 18 | 19 | - Required by dependency rstan. 20 | 21 | # Reverse dependencies 22 | 23 | N/A 24 | -------------------------------------------------------------------------------- /data/BLch9.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/data/BLch9.rda -------------------------------------------------------------------------------- /data/MEC2010.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/data/MEC2010.rda -------------------------------------------------------------------------------- /docs/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Page not found (404) • bmlm 9 | 10 | 11 | 12 | 13 | 14 | 15 | 19 | 20 | 21 | 22 | 23 |
24 |
82 | 83 | 84 | 85 | 86 |
87 |
88 | 91 | 92 | Content not found. Please use links in the navbar. 93 | 94 |
95 | 96 | 100 | 101 |
102 | 103 | 104 | 105 |
109 | 110 |
111 |

112 |

Site built with pkgdown 2.1.1.

113 |
114 | 115 |
116 |
117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /docs/articles/Spaghetti-Plots/Spaghetti-Plots_files/figure-html/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/Spaghetti-Plots/Spaghetti-Plots_files/figure-html/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /docs/articles/Spaghetti-Plots/Spaghetti-Plots_files/figure-html/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/Spaghetti-Plots/Spaghetti-Plots_files/figure-html/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-10-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-10-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-11-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-11-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-11-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-11-2.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-11-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-11-3.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-12-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-12-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-13-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-13-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-14-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-14-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-16-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-16-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-17-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-17-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-18-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-18-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-22-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-22-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-23-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-23-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-25-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-25-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-26-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/bmlm-blch9_files/figure-html/unnamed-chunk-26-1.png -------------------------------------------------------------------------------- /docs/articles/bmlm-blch9/table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/bmlm-blch9/table.png -------------------------------------------------------------------------------- /docs/articles/index.html: -------------------------------------------------------------------------------- 1 | 2 | Articles • bmlm 6 | 7 | 8 |
9 |
59 | 60 | 61 | 62 |
83 | 84 | 85 |
88 | 89 |
90 |

Site built with pkgdown 2.0.7.

91 |
92 | 93 |
94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-path-plot-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-path-plot-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-path-plot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-path-plot-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-pme-plot-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-pme-plot-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-pme-plot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-pme-plot-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-hist-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-hist-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-hist-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-hist-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-violin-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-violin-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-violin-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-pop-violin-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-subj-coefs-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-subj-coefs-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/example1-subj-coefs-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/example1-subj-coefs-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/lkj-prior-example-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/lkj-prior-example-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/lkj-prior-example-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/lkj-prior-example-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-b-binary-y-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-b-binary-y-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-b-binary-y-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-b-binary-y-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-binary-y-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-binary-y-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-binary-y-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/spaghetti-plot-binary-y-1.png -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/template-mediation-plot-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/template-mediation-plot-1.pdf -------------------------------------------------------------------------------- /docs/articles/ms/bmlm-ms_files/figure-latex/template-mediation-plot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/articles/ms/bmlm-ms_files/figure-latex/template-mediation-plot-1.png -------------------------------------------------------------------------------- /docs/authors.html: -------------------------------------------------------------------------------- 1 | 2 | Authors and Citation • bmlm 6 | 7 | 8 |
9 |
59 | 60 | 61 | 62 |
63 |
64 |
65 | 68 | 69 | 70 |
  • 71 |

    Matti Vuorre. Author, maintainer. 72 |

    73 |
  • 74 |
75 |
76 |
77 |

Citation

78 | Source: inst/CITATION 79 |
80 |
81 | 82 | 83 |

Vuorre M, Bolger N (2018). 84 | “Within-subject mediation analysis for experimental data in cognitive psychology and neuroscience.” 85 | Behavior Research Methods. 86 | doi:10.3758/s13428-017-0980-9. 87 |

88 |
@Article{,
 89 |   title = {Within-subject mediation analysis for experimental data in cognitive psychology and neuroscience},
 90 |   author = {Matti Vuorre and Niall Bolger},
 91 |   journal = {Behavior Research Methods},
 92 |   year = {2018},
 93 |   doi = {10.3758/s13428-017-0980-9},
 94 | }
95 |

Vuorre M (2023). 96 | bmlm: Bayesian Multilevel Mediation. 97 | R package version 1.3.15, https://CRAN.R-project.org/package=bmlm. 98 |

99 |
@Manual{,
100 |   title = {bmlm: Bayesian Multilevel Mediation},
101 |   author = {Matti Vuorre},
102 |   year = {2023},
103 |   note = {R package version 1.3.15},
104 |   url = {https://CRAN.R-project.org/package=bmlm},
105 | }
106 | 107 |
108 | 109 |
110 | 111 | 112 | 113 |
116 | 117 |
118 |

Site built with pkgdown 2.1.1.

119 |
120 | 121 |
122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /docs/bootstrap-toc.css: -------------------------------------------------------------------------------- 1 | /*! 2 | * Bootstrap Table of Contents v0.4.1 (http://afeld.github.io/bootstrap-toc/) 3 | * Copyright 2015 Aidan Feldman 4 | * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ 5 | 6 | /* modified from https://github.com/twbs/bootstrap/blob/94b4076dd2efba9af71f0b18d4ee4b163aa9e0dd/docs/assets/css/src/docs.css#L548-L601 */ 7 | 8 | /* All levels of nav */ 9 | nav[data-toggle='toc'] .nav > li > a { 10 | display: block; 11 | padding: 4px 20px; 12 | font-size: 13px; 13 | font-weight: 500; 14 | color: #767676; 15 | } 16 | nav[data-toggle='toc'] .nav > li > a:hover, 17 | nav[data-toggle='toc'] .nav > li > a:focus { 18 | padding-left: 19px; 19 | color: #563d7c; 20 | text-decoration: none; 21 | background-color: transparent; 22 | border-left: 1px solid #563d7c; 23 | } 24 | nav[data-toggle='toc'] .nav > .active > a, 25 | nav[data-toggle='toc'] .nav > .active:hover > a, 26 | nav[data-toggle='toc'] .nav > .active:focus > a { 27 | padding-left: 18px; 28 | font-weight: bold; 29 | color: #563d7c; 30 | background-color: transparent; 31 | border-left: 2px solid #563d7c; 32 | } 33 | 34 | /* Nav: second level (shown on .active) */ 35 | nav[data-toggle='toc'] .nav .nav { 36 | display: none; /* Hide by default, but at >768px, show it */ 37 | padding-bottom: 10px; 38 | } 39 | nav[data-toggle='toc'] .nav .nav > li > a { 40 | padding-top: 1px; 41 | padding-bottom: 1px; 42 | padding-left: 30px; 43 | font-size: 12px; 44 | font-weight: normal; 45 | } 46 | nav[data-toggle='toc'] .nav .nav > li > a:hover, 47 | nav[data-toggle='toc'] .nav .nav > li > a:focus { 48 | padding-left: 29px; 49 | } 50 | nav[data-toggle='toc'] .nav .nav > .active > a, 51 | nav[data-toggle='toc'] .nav .nav > .active:hover > a, 52 | nav[data-toggle='toc'] .nav .nav > .active:focus > a { 53 | padding-left: 28px; 54 | font-weight: 500; 55 | } 56 | 57 | /* from https://github.com/twbs/bootstrap/blob/e38f066d8c203c3e032da0ff23cd2d6098ee2dd6/docs/assets/css/src/docs.css#L631-L634 */ 58 | nav[data-toggle='toc'] .nav > .active > ul { 59 | display: block; 60 | } 61 | -------------------------------------------------------------------------------- /docs/bootstrap-toc.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Bootstrap Table of Contents v0.4.1 (http://afeld.github.io/bootstrap-toc/) 3 | * Copyright 2015 Aidan Feldman 4 | * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ 5 | (function() { 6 | 'use strict'; 7 | 8 | window.Toc = { 9 | helpers: { 10 | // return all matching elements in the set, or their descendants 11 | findOrFilter: function($el, selector) { 12 | // http://danielnouri.org/notes/2011/03/14/a-jquery-find-that-also-finds-the-root-element/ 13 | // http://stackoverflow.com/a/12731439/358804 14 | var $descendants = $el.find(selector); 15 | return $el.filter(selector).add($descendants).filter(':not([data-toc-skip])'); 16 | }, 17 | 18 | generateUniqueIdBase: function(el) { 19 | var text = $(el).text(); 20 | var anchor = text.trim().toLowerCase().replace(/[^A-Za-z0-9]+/g, '-'); 21 | return anchor || el.tagName.toLowerCase(); 22 | }, 23 | 24 | generateUniqueId: function(el) { 25 | var anchorBase = this.generateUniqueIdBase(el); 26 | for (var i = 0; ; i++) { 27 | var anchor = anchorBase; 28 | if (i > 0) { 29 | // add suffix 30 | anchor += '-' + i; 31 | } 32 | // check if ID already exists 33 | if (!document.getElementById(anchor)) { 34 | return anchor; 35 | } 36 | } 37 | }, 38 | 39 | generateAnchor: function(el) { 40 | if (el.id) { 41 | return el.id; 42 | } else { 43 | var anchor = this.generateUniqueId(el); 44 | el.id = anchor; 45 | return anchor; 46 | } 47 | }, 48 | 49 | createNavList: function() { 50 | return $(''); 51 | }, 52 | 53 | createChildNavList: function($parent) { 54 | var $childList = this.createNavList(); 55 | $parent.append($childList); 56 | return $childList; 57 | }, 58 | 59 | generateNavEl: function(anchor, text) { 60 | var $a = $(''); 61 | $a.attr('href', '#' + anchor); 62 | $a.text(text); 63 | var $li = $('
  • '); 64 | $li.append($a); 65 | return $li; 66 | }, 67 | 68 | generateNavItem: function(headingEl) { 69 | var anchor = this.generateAnchor(headingEl); 70 | var $heading = $(headingEl); 71 | var text = $heading.data('toc-text') || $heading.text(); 72 | return this.generateNavEl(anchor, text); 73 | }, 74 | 75 | // Find the first heading level (`

    `, then `

    `, etc.) that has more than one element. Defaults to 1 (for `

    `). 76 | getTopLevel: function($scope) { 77 | for (var i = 1; i <= 6; i++) { 78 | var $headings = this.findOrFilter($scope, 'h' + i); 79 | if ($headings.length > 1) { 80 | return i; 81 | } 82 | } 83 | 84 | return 1; 85 | }, 86 | 87 | // returns the elements for the top level, and the next below it 88 | getHeadings: function($scope, topLevel) { 89 | var topSelector = 'h' + topLevel; 90 | 91 | var secondaryLevel = topLevel + 1; 92 | var secondarySelector = 'h' + secondaryLevel; 93 | 94 | return this.findOrFilter($scope, topSelector + ',' + secondarySelector); 95 | }, 96 | 97 | getNavLevel: function(el) { 98 | return parseInt(el.tagName.charAt(1), 10); 99 | }, 100 | 101 | populateNav: function($topContext, topLevel, $headings) { 102 | var $context = $topContext; 103 | var $prevNav; 104 | 105 | var helpers = this; 106 | $headings.each(function(i, el) { 107 | var $newNav = helpers.generateNavItem(el); 108 | var navLevel = helpers.getNavLevel(el); 109 | 110 | // determine the proper $context 111 | if (navLevel === topLevel) { 112 | // use top level 113 | $context = $topContext; 114 | } else if ($prevNav && $context === $topContext) { 115 | // create a new level of the tree and switch to it 116 | $context = helpers.createChildNavList($prevNav); 117 | } // else use the current $context 118 | 119 | $context.append($newNav); 120 | 121 | $prevNav = $newNav; 122 | }); 123 | }, 124 | 125 | parseOps: function(arg) { 126 | var opts; 127 | if (arg.jquery) { 128 | opts = { 129 | $nav: arg 130 | }; 131 | } else { 132 | opts = arg; 133 | } 134 | opts.$scope = opts.$scope || $(document.body); 135 | return opts; 136 | } 137 | }, 138 | 139 | // accepts a jQuery object, or an options object 140 | init: function(opts) { 141 | opts = this.helpers.parseOps(opts); 142 | 143 | // ensure that the data attribute is in place for styling 144 | opts.$nav.attr('data-toggle', 'toc'); 145 | 146 | var $topContext = this.helpers.createChildNavList(opts.$nav); 147 | var topLevel = this.helpers.getTopLevel(opts.$scope); 148 | var $headings = this.helpers.getHeadings(opts.$scope, topLevel); 149 | this.helpers.populateNav($topContext, topLevel, $headings); 150 | } 151 | }; 152 | 153 | $(function() { 154 | $('nav[data-toggle="toc"]').each(function(i, el) { 155 | var $nav = $(el); 156 | Toc.init($nav); 157 | }); 158 | }); 159 | })(); 160 | -------------------------------------------------------------------------------- /docs/docsearch.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | // register a handler to move the focus to the search bar 4 | // upon pressing shift + "/" (i.e. "?") 5 | $(document).on('keydown', function(e) { 6 | if (e.shiftKey && e.keyCode == 191) { 7 | e.preventDefault(); 8 | $("#search-input").focus(); 9 | } 10 | }); 11 | 12 | $(document).ready(function() { 13 | // do keyword highlighting 14 | /* modified from https://jsfiddle.net/julmot/bL6bb5oo/ */ 15 | var mark = function() { 16 | 17 | var referrer = document.URL ; 18 | var paramKey = "q" ; 19 | 20 | if (referrer.indexOf("?") !== -1) { 21 | var qs = referrer.substr(referrer.indexOf('?') + 1); 22 | var qs_noanchor = qs.split('#')[0]; 23 | var qsa = qs_noanchor.split('&'); 24 | var keyword = ""; 25 | 26 | for (var i = 0; i < qsa.length; i++) { 27 | var currentParam = qsa[i].split('='); 28 | 29 | if (currentParam.length !== 2) { 30 | continue; 31 | } 32 | 33 | if (currentParam[0] == paramKey) { 34 | keyword = decodeURIComponent(currentParam[1].replace(/\+/g, "%20")); 35 | } 36 | } 37 | 38 | if (keyword !== "") { 39 | $(".contents").unmark({ 40 | done: function() { 41 | $(".contents").mark(keyword); 42 | } 43 | }); 44 | } 45 | } 46 | }; 47 | 48 | mark(); 49 | }); 50 | }); 51 | 52 | /* Search term highlighting ------------------------------*/ 53 | 54 | function matchedWords(hit) { 55 | var words = []; 56 | 57 | var hierarchy = hit._highlightResult.hierarchy; 58 | // loop to fetch from lvl0, lvl1, etc. 59 | for (var idx in hierarchy) { 60 | words = words.concat(hierarchy[idx].matchedWords); 61 | } 62 | 63 | var content = hit._highlightResult.content; 64 | if (content) { 65 | words = words.concat(content.matchedWords); 66 | } 67 | 68 | // return unique words 69 | var words_uniq = [...new Set(words)]; 70 | return words_uniq; 71 | } 72 | 73 | function updateHitURL(hit) { 74 | 75 | var words = matchedWords(hit); 76 | var url = ""; 77 | 78 | if (hit.anchor) { 79 | url = hit.url_without_anchor + '?q=' + escape(words.join(" ")) + '#' + hit.anchor; 80 | } else { 81 | url = hit.url + '?q=' + escape(words.join(" ")); 82 | } 83 | 84 | return url; 85 | } 86 | -------------------------------------------------------------------------------- /docs/jquery.sticky-kit.min.js: -------------------------------------------------------------------------------- 1 | /* Sticky-kit v1.1.2 | WTFPL | Leaf Corcoran 2015 | */ 2 | /* 3 | Source: https://github.com/leafo/sticky-kit 4 | License: MIT 5 | */ 6 | (function(){var b,f;b=this.jQuery||window.jQuery;f=b(window);b.fn.stick_in_parent=function(d){var A,w,J,n,B,K,p,q,k,E,t;null==d&&(d={});t=d.sticky_class;B=d.inner_scrolling;E=d.recalc_every;k=d.parent;q=d.offset_top;p=d.spacer;w=d.bottoming;null==q&&(q=0);null==k&&(k=void 0);null==B&&(B=!0);null==t&&(t="is_stuck");A=b(document);null==w&&(w=!0);J=function(a,d,n,C,F,u,r,G){var v,H,m,D,I,c,g,x,y,z,h,l;if(!a.data("sticky_kit")){a.data("sticky_kit",!0);I=A.height();g=a.parent();null!=k&&(g=g.closest(k)); 7 | if(!g.length)throw"failed to find stick parent";v=m=!1;(h=null!=p?p&&a.closest(p):b("
    "))&&h.css("position",a.css("position"));x=function(){var c,f,e;if(!G&&(I=A.height(),c=parseInt(g.css("border-top-width"),10),f=parseInt(g.css("padding-top"),10),d=parseInt(g.css("padding-bottom"),10),n=g.offset().top+c+f,C=g.height(),m&&(v=m=!1,null==p&&(a.insertAfter(h),h.detach()),a.css({position:"",top:"",width:"",bottom:""}).removeClass(t),e=!0),F=a.offset().top-(parseInt(a.css("margin-top"),10)||0)-q, 8 | u=a.outerHeight(!0),r=a.css("float"),h&&h.css({width:a.outerWidth(!0),height:u,display:a.css("display"),"vertical-align":a.css("vertical-align"),"float":r}),e))return l()};x();if(u!==C)return D=void 0,c=q,z=E,l=function(){var b,l,e,k;if(!G&&(e=!1,null!=z&&(--z,0>=z&&(z=E,x(),e=!0)),e||A.height()===I||x(),e=f.scrollTop(),null!=D&&(l=e-D),D=e,m?(w&&(k=e+u+c>C+n,v&&!k&&(v=!1,a.css({position:"fixed",bottom:"",top:c}).trigger("sticky_kit:unbottom"))),eb&&!v&&(c-=l,c=Math.max(b-u,c),c=Math.min(q,c),m&&a.css({top:c+"px"})))):e>F&&(m=!0,b={position:"fixed",top:c},b.width="border-box"===a.css("box-sizing")?a.outerWidth()+"px":a.width()+"px",a.css(b).addClass(t),null==p&&(a.after(h),"left"!==r&&"right"!==r||h.append(a)),a.trigger("sticky_kit:stick")),m&&w&&(null==k&&(k=e+u+c>C+n),!v&&k)))return v=!0,"static"===g.css("position")&&g.css({position:"relative"}), 10 | a.css({position:"absolute",bottom:d,top:"auto"}).trigger("sticky_kit:bottom")},y=function(){x();return l()},H=function(){G=!0;f.off("touchmove",l);f.off("scroll",l);f.off("resize",y);b(document.body).off("sticky_kit:recalc",y);a.off("sticky_kit:detach",H);a.removeData("sticky_kit");a.css({position:"",bottom:"",top:"",width:""});g.position("position","");if(m)return null==p&&("left"!==r&&"right"!==r||a.insertAfter(h),h.remove()),a.removeClass(t)},f.on("touchmove",l),f.on("scroll",l),f.on("resize", 11 | y),b(document.body).on("sticky_kit:recalc",y),a.on("sticky_kit:detach",H),setTimeout(l,0)}};n=0;for(K=this.length;n 2 | 3 | 5 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /docs/pkgdown.css: -------------------------------------------------------------------------------- 1 | /* Sticky footer */ 2 | 3 | /** 4 | * Basic idea: https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ 5 | * Details: https://github.com/philipwalton/solved-by-flexbox/blob/master/assets/css/components/site.css 6 | * 7 | * .Site -> body > .container 8 | * .Site-content -> body > .container .row 9 | * .footer -> footer 10 | * 11 | * Key idea seems to be to ensure that .container and __all its parents__ 12 | * have height set to 100% 13 | * 14 | */ 15 | 16 | html, body { 17 | height: 100%; 18 | } 19 | 20 | body { 21 | position: relative; 22 | } 23 | 24 | body > .container { 25 | display: flex; 26 | height: 100%; 27 | flex-direction: column; 28 | } 29 | 30 | body > .container .row { 31 | flex: 1 0 auto; 32 | } 33 | 34 | footer { 35 | margin-top: 45px; 36 | padding: 35px 0 36px; 37 | border-top: 1px solid #e5e5e5; 38 | color: #666; 39 | display: flex; 40 | flex-shrink: 0; 41 | } 42 | footer p { 43 | margin-bottom: 0; 44 | } 45 | footer div { 46 | flex: 1; 47 | } 48 | footer .pkgdown { 49 | text-align: right; 50 | } 51 | footer p { 52 | margin-bottom: 0; 53 | } 54 | 55 | img.icon { 56 | float: right; 57 | } 58 | 59 | /* Ensure in-page images don't run outside their container */ 60 | .contents img { 61 | max-width: 100%; 62 | height: auto; 63 | } 64 | 65 | /* Fix bug in bootstrap (only seen in firefox) */ 66 | summary { 67 | display: list-item; 68 | } 69 | 70 | /* Typographic tweaking ---------------------------------*/ 71 | 72 | .contents .page-header { 73 | margin-top: calc(-60px + 1em); 74 | } 75 | 76 | dd { 77 | margin-left: 3em; 78 | } 79 | 80 | /* Section anchors ---------------------------------*/ 81 | 82 | a.anchor { 83 | display: none; 84 | margin-left: 5px; 85 | width: 20px; 86 | height: 20px; 87 | 88 | background-image: url(./link.svg); 89 | background-repeat: no-repeat; 90 | background-size: 20px 20px; 91 | background-position: center center; 92 | } 93 | 94 | h1:hover .anchor, 95 | h2:hover .anchor, 96 | h3:hover .anchor, 97 | h4:hover .anchor, 98 | h5:hover .anchor, 99 | h6:hover .anchor { 100 | display: inline-block; 101 | } 102 | 103 | /* Fixes for fixed navbar --------------------------*/ 104 | 105 | .contents h1, .contents h2, .contents h3, .contents h4 { 106 | padding-top: 60px; 107 | margin-top: -40px; 108 | } 109 | 110 | /* Navbar submenu --------------------------*/ 111 | 112 | .dropdown-submenu { 113 | position: relative; 114 | } 115 | 116 | .dropdown-submenu>.dropdown-menu { 117 | top: 0; 118 | left: 100%; 119 | margin-top: -6px; 120 | margin-left: -1px; 121 | border-radius: 0 6px 6px 6px; 122 | } 123 | 124 | .dropdown-submenu:hover>.dropdown-menu { 125 | display: block; 126 | } 127 | 128 | .dropdown-submenu>a:after { 129 | display: block; 130 | content: " "; 131 | float: right; 132 | width: 0; 133 | height: 0; 134 | border-color: transparent; 135 | border-style: solid; 136 | border-width: 5px 0 5px 5px; 137 | border-left-color: #cccccc; 138 | margin-top: 5px; 139 | margin-right: -10px; 140 | } 141 | 142 | .dropdown-submenu:hover>a:after { 143 | border-left-color: #ffffff; 144 | } 145 | 146 | .dropdown-submenu.pull-left { 147 | float: none; 148 | } 149 | 150 | .dropdown-submenu.pull-left>.dropdown-menu { 151 | left: -100%; 152 | margin-left: 10px; 153 | border-radius: 6px 0 6px 6px; 154 | } 155 | 156 | /* Sidebar --------------------------*/ 157 | 158 | #pkgdown-sidebar { 159 | margin-top: 30px; 160 | position: -webkit-sticky; 161 | position: sticky; 162 | top: 70px; 163 | } 164 | 165 | #pkgdown-sidebar h2 { 166 | font-size: 1.5em; 167 | margin-top: 1em; 168 | } 169 | 170 | #pkgdown-sidebar h2:first-child { 171 | margin-top: 0; 172 | } 173 | 174 | #pkgdown-sidebar .list-unstyled li { 175 | margin-bottom: 0.5em; 176 | } 177 | 178 | /* bootstrap-toc tweaks ------------------------------------------------------*/ 179 | 180 | /* All levels of nav */ 181 | 182 | nav[data-toggle='toc'] .nav > li > a { 183 | padding: 4px 20px 4px 6px; 184 | font-size: 1.5rem; 185 | font-weight: 400; 186 | color: inherit; 187 | } 188 | 189 | nav[data-toggle='toc'] .nav > li > a:hover, 190 | nav[data-toggle='toc'] .nav > li > a:focus { 191 | padding-left: 5px; 192 | color: inherit; 193 | border-left: 1px solid #878787; 194 | } 195 | 196 | nav[data-toggle='toc'] .nav > .active > a, 197 | nav[data-toggle='toc'] .nav > .active:hover > a, 198 | nav[data-toggle='toc'] .nav > .active:focus > a { 199 | padding-left: 5px; 200 | font-size: 1.5rem; 201 | font-weight: 400; 202 | color: inherit; 203 | border-left: 2px solid #878787; 204 | } 205 | 206 | /* Nav: second level (shown on .active) */ 207 | 208 | nav[data-toggle='toc'] .nav .nav { 209 | display: none; /* Hide by default, but at >768px, show it */ 210 | padding-bottom: 10px; 211 | } 212 | 213 | nav[data-toggle='toc'] .nav .nav > li > a { 214 | padding-left: 16px; 215 | font-size: 1.35rem; 216 | } 217 | 218 | nav[data-toggle='toc'] .nav .nav > li > a:hover, 219 | nav[data-toggle='toc'] .nav .nav > li > a:focus { 220 | padding-left: 15px; 221 | } 222 | 223 | nav[data-toggle='toc'] .nav .nav > .active > a, 224 | nav[data-toggle='toc'] .nav .nav > .active:hover > a, 225 | nav[data-toggle='toc'] .nav .nav > .active:focus > a { 226 | padding-left: 15px; 227 | font-weight: 500; 228 | font-size: 1.35rem; 229 | } 230 | 231 | /* orcid ------------------------------------------------------------------- */ 232 | 233 | .orcid { 234 | font-size: 16px; 235 | color: #A6CE39; 236 | /* margins are required by official ORCID trademark and display guidelines */ 237 | margin-left:4px; 238 | margin-right:4px; 239 | vertical-align: middle; 240 | } 241 | 242 | /* Reference index & topics ----------------------------------------------- */ 243 | 244 | .ref-index th {font-weight: normal;} 245 | 246 | .ref-index td {vertical-align: top; min-width: 100px} 247 | .ref-index .icon {width: 40px;} 248 | .ref-index .alias {width: 40%;} 249 | .ref-index-icons .alias {width: calc(40% - 40px);} 250 | .ref-index .title {width: 60%;} 251 | 252 | .ref-arguments th {text-align: right; padding-right: 10px;} 253 | .ref-arguments th, .ref-arguments td {vertical-align: top; min-width: 100px} 254 | .ref-arguments .name {width: 20%;} 255 | .ref-arguments .desc {width: 80%;} 256 | 257 | /* Nice scrolling for wide elements --------------------------------------- */ 258 | 259 | table { 260 | display: block; 261 | overflow: auto; 262 | } 263 | 264 | /* Syntax highlighting ---------------------------------------------------- */ 265 | 266 | pre, code, pre code { 267 | background-color: #f8f8f8; 268 | color: #333; 269 | } 270 | pre, pre code { 271 | white-space: pre-wrap; 272 | word-break: break-all; 273 | overflow-wrap: break-word; 274 | } 275 | 276 | pre { 277 | border: 1px solid #eee; 278 | } 279 | 280 | pre .img, pre .r-plt { 281 | margin: 5px 0; 282 | } 283 | 284 | pre .img img, pre .r-plt img { 285 | background-color: #fff; 286 | } 287 | 288 | code a, pre a { 289 | color: #375f84; 290 | } 291 | 292 | a.sourceLine:hover { 293 | text-decoration: none; 294 | } 295 | 296 | .fl {color: #1514b5;} 297 | .fu {color: #000000;} /* function */ 298 | .ch,.st {color: #036a07;} /* string */ 299 | .kw {color: #264D66;} /* keyword */ 300 | .co {color: #888888;} /* comment */ 301 | 302 | .error {font-weight: bolder;} 303 | .warning {font-weight: bolder;} 304 | 305 | /* Clipboard --------------------------*/ 306 | 307 | .hasCopyButton { 308 | position: relative; 309 | } 310 | 311 | .btn-copy-ex { 312 | position: absolute; 313 | right: 0; 314 | top: 0; 315 | visibility: hidden; 316 | } 317 | 318 | .hasCopyButton:hover button.btn-copy-ex { 319 | visibility: visible; 320 | } 321 | 322 | /* headroom.js ------------------------ */ 323 | 324 | .headroom { 325 | will-change: transform; 326 | transition: transform 200ms linear; 327 | } 328 | .headroom--pinned { 329 | transform: translateY(0%); 330 | } 331 | .headroom--unpinned { 332 | transform: translateY(-100%); 333 | } 334 | 335 | /* mark.js ----------------------------*/ 336 | 337 | mark { 338 | background-color: rgba(255, 255, 51, 0.5); 339 | border-bottom: 2px solid rgba(255, 153, 51, 0.3); 340 | padding: 1px; 341 | } 342 | 343 | /* vertical spacing after htmlwidgets */ 344 | .html-widget { 345 | margin-bottom: 10px; 346 | } 347 | 348 | /* fontawesome ------------------------ */ 349 | 350 | .fab { 351 | font-family: "Font Awesome 5 Brands" !important; 352 | } 353 | 354 | /* don't display links in code chunks when printing */ 355 | /* source: https://stackoverflow.com/a/10781533 */ 356 | @media print { 357 | code a:link:after, code a:visited:after { 358 | content: ""; 359 | } 360 | } 361 | 362 | /* Section anchors --------------------------------- 363 | Added in pandoc 2.11: https://github.com/jgm/pandoc-templates/commit/9904bf71 364 | */ 365 | 366 | div.csl-bib-body { } 367 | div.csl-entry { 368 | clear: both; 369 | } 370 | .hanging-indent div.csl-entry { 371 | margin-left:2em; 372 | text-indent:-2em; 373 | } 374 | div.csl-left-margin { 375 | min-width:2em; 376 | float:left; 377 | } 378 | div.csl-right-inline { 379 | margin-left:2em; 380 | padding-left:1em; 381 | } 382 | div.csl-indent { 383 | margin-left: 2em; 384 | } 385 | -------------------------------------------------------------------------------- /docs/pkgdown.js: -------------------------------------------------------------------------------- 1 | /* http://gregfranko.com/blog/jquery-best-practices/ */ 2 | (function($) { 3 | $(function() { 4 | 5 | $('.navbar-fixed-top').headroom(); 6 | 7 | $('body').css('padding-top', $('.navbar').height() + 10); 8 | $(window).resize(function(){ 9 | $('body').css('padding-top', $('.navbar').height() + 10); 10 | }); 11 | 12 | $('[data-toggle="tooltip"]').tooltip(); 13 | 14 | var cur_path = paths(location.pathname); 15 | var links = $("#navbar ul li a"); 16 | var max_length = -1; 17 | var pos = -1; 18 | for (var i = 0; i < links.length; i++) { 19 | if (links[i].getAttribute("href") === "#") 20 | continue; 21 | // Ignore external links 22 | if (links[i].host !== location.host) 23 | continue; 24 | 25 | var nav_path = paths(links[i].pathname); 26 | 27 | var length = prefix_length(nav_path, cur_path); 28 | if (length > max_length) { 29 | max_length = length; 30 | pos = i; 31 | } 32 | } 33 | 34 | // Add class to parent
  • , and enclosing
  • if in dropdown 35 | if (pos >= 0) { 36 | var menu_anchor = $(links[pos]); 37 | menu_anchor.parent().addClass("active"); 38 | menu_anchor.closest("li.dropdown").addClass("active"); 39 | } 40 | }); 41 | 42 | function paths(pathname) { 43 | var pieces = pathname.split("/"); 44 | pieces.shift(); // always starts with / 45 | 46 | var end = pieces[pieces.length - 1]; 47 | if (end === "index.html" || end === "") 48 | pieces.pop(); 49 | return(pieces); 50 | } 51 | 52 | // Returns -1 if not found 53 | function prefix_length(needle, haystack) { 54 | if (needle.length > haystack.length) 55 | return(-1); 56 | 57 | // Special case for length-0 haystack, since for loop won't run 58 | if (haystack.length === 0) { 59 | return(needle.length === 0 ? 0 : -1); 60 | } 61 | 62 | for (var i = 0; i < haystack.length; i++) { 63 | if (needle[i] != haystack[i]) 64 | return(i); 65 | } 66 | 67 | return(haystack.length); 68 | } 69 | 70 | /* Clipboard --------------------------*/ 71 | 72 | function changeTooltipMessage(element, msg) { 73 | var tooltipOriginalTitle=element.getAttribute('data-original-title'); 74 | element.setAttribute('data-original-title', msg); 75 | $(element).tooltip('show'); 76 | element.setAttribute('data-original-title', tooltipOriginalTitle); 77 | } 78 | 79 | if(ClipboardJS.isSupported()) { 80 | $(document).ready(function() { 81 | var copyButton = ""; 82 | 83 | $("div.sourceCode").addClass("hasCopyButton"); 84 | 85 | // Insert copy buttons: 86 | $(copyButton).prependTo(".hasCopyButton"); 87 | 88 | // Initialize tooltips: 89 | $('.btn-copy-ex').tooltip({container: 'body'}); 90 | 91 | // Initialize clipboard: 92 | var clipboardBtnCopies = new ClipboardJS('[data-clipboard-copy]', { 93 | text: function(trigger) { 94 | return trigger.parentNode.textContent.replace(/\n#>[^\n]*/g, ""); 95 | } 96 | }); 97 | 98 | clipboardBtnCopies.on('success', function(e) { 99 | changeTooltipMessage(e.trigger, 'Copied!'); 100 | e.clearSelection(); 101 | }); 102 | 103 | clipboardBtnCopies.on('error', function() { 104 | changeTooltipMessage(e.trigger,'Press Ctrl+C or Command+C to copy'); 105 | }); 106 | }); 107 | } 108 | })(window.jQuery || window.$) 109 | -------------------------------------------------------------------------------- /docs/pkgdown.yml: -------------------------------------------------------------------------------- 1 | pandoc: 3.1.7 2 | pkgdown: 2.0.7 3 | pkgdown_sha: ~ 4 | articles: 5 | FAQ: FAQ/FAQ.html 6 | Spaghetti-Plots: Spaghetti-Plots/Spaghetti-Plots.html 7 | bmlm-blch9: bmlm-blch9/bmlm-blch9.html 8 | bmlm-ms: ms/bmlm-ms.pdf 9 | last_built: 2023-09-26T07:41Z 10 | 11 | -------------------------------------------------------------------------------- /docs/reference/BLch9.html: -------------------------------------------------------------------------------- 1 | 2 | Relationship between work stressors, work dissatisfaction, 3 | and relationship dissatisfaction. — BLch9 • bmlm 11 | 12 | 13 |
    14 |
    64 | 65 | 66 | 67 |
    68 |
    69 | 75 | 76 |
    77 |

    Simulated data from Intensive Longitudinal Methods: 78 | An Introduction to Diary and Experience Sampling Research. 79 | (Bolger, & Laurenceau, 2013, chapter 9; 80 | http://www.intensivelongitudinal.com/index.html).

    81 |
    82 | 83 |
    84 |
    data(BLch9)
    85 |
    86 | 87 |
    88 |

    Format

    89 |

    A data frame with 2100 rows and 8 variables:

    id
    90 |

    ID of study participant

    91 | 92 |
    time
    93 |

    Time

    94 | 95 |
    fwkstrs
    96 |

    Number of work stressors

    97 | 98 |
    fwkdis
    99 |

    Work dissatisfaction rating

    100 | 101 |
    freldis
    102 |

    Relationship dissatisfaction

    103 | 104 |
    x
    105 |

    Subject-mean deviated number of work stressors

    106 | 107 |
    m
    108 |

    Subject-mean deviated work dissatisfaction rating

    109 | 110 |
    y
    111 |

    Subject-mean deviated relationship dissatisfaction

    112 | 113 | 114 |
    115 | 119 | 120 |
    121 | 124 |
    125 | 126 | 127 |
    130 | 131 |
    132 |

    Site built with pkgdown 2.0.7.

    133 |
    134 | 135 |
    136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /docs/reference/MEC2010.html: -------------------------------------------------------------------------------- 1 | 2 | Judgments of performance in a video game — MEC2010 • bmlm 9 | 10 | 11 |
    12 |
    62 | 63 | 64 | 65 |
    66 |
    67 | 72 | 73 |
    74 |

    Data from an experiment where participants rated their 75 | performance in a video game in two conditions. 76 | (Experiment 1 in Metcalfe, Eich, & Castel, 2010; 77 | https://www.sciencedirect.com/science/article/pii/S0010027710001113).

    78 |
    79 | 80 |
    81 |
    data(MEC2010)
    82 |
    83 | 84 |
    85 |

    Format

    86 |

    A data frame with 344 rows and 4 variables:

    subj
    87 |

    Subject id number.

    88 | 89 |
    lag
    90 |

    Lag condition (0 = no lag, 1 = 250ms lag).

    91 | 92 |
    hr
    93 |

    Hit rate.

    94 | 95 |
    jop
    96 |

    Judgment of Performance.

    97 | 98 | 99 |
    100 |
    101 |

    Source

    102 |

    Metcalfe, J., Eich, T. S., & Castel, A. D. (2010). 103 | Metacognition of agency across the lifespan. 104 | Cognition, 116(2), 267-282. 105 | doi:10.1016/j.cognition.2010.05.009

    106 |
    107 | 108 |
    109 | 112 |
    113 | 114 | 115 |
    118 | 119 |
    120 |

    Site built with pkgdown 2.0.7.

    121 |
    122 | 123 |
    124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /docs/reference/bmlm-package.html: -------------------------------------------------------------------------------- 1 | 2 | bmlm: Easy estimation of Bayesian multilevel mediation models with Stan. — bmlm-package • bmlm 6 | 7 | 8 |
    9 |
    59 | 60 | 61 | 62 |
    63 |
    64 | 69 | 70 |
    71 |

    See https://mvuorre.github.io/bmlm/ for a short tutorial.

    72 |
    73 | 74 | 75 | 76 |
    77 | 80 |
    81 | 82 | 83 |
    86 | 87 |
    88 |

    Site built with pkgdown 2.0.7.

    89 |
    90 | 91 |
    92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /docs/reference/bmlm.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | bmlm: Easy estimation of Bayesian multilevel mediation models with Stan. — bmlm • bmlm 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 48 | 49 | 50 | 51 | 52 | 53 |
    54 |
    55 | 119 | 120 | 121 |
    122 | 123 |
    124 |
    125 | 130 | 131 |
    132 | 133 |

    See https://mvuorre.github.io/bmlm/ for a short tutorial.

    134 | 135 |
    136 | 137 | 138 | 139 |
    140 | 146 |
    147 | 148 |
    149 | 152 | 153 |
    154 |

    Site built with pkgdown 1.3.0.

    155 |
    156 |
    157 |
    158 | 159 | 160 | 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /docs/reference/index.html: -------------------------------------------------------------------------------- 1 | 2 | Function reference • bmlm 6 | 7 | 8 |
    9 |
    59 | 60 | 61 | 62 |
    63 |
    64 | 67 | 68 | 72 | 75 | 77 | 80 | 81 | 84 | 85 | 88 | 89 | 92 | 93 | 96 | 97 | 100 | 101 | 104 | 105 | 108 | 109 |
    69 |

    All functions

    70 |

    71 |
    73 |

    BLch9

    74 |

    Relationship between work stressors, work dissatisfaction, 76 | and relationship dissatisfaction.

    78 |

    MEC2010

    79 |

    Judgments of performance in a video game

    82 |

    bmlm-package bmlm

    83 |

    bmlm: Easy estimation of Bayesian multilevel mediation models with Stan.

    86 |

    isolate()

    87 |

    Create isolated within- (and optionally between-) person variables.

    90 |

    mlm()

    91 |

    Estimate a multilevel mediation model

    94 |

    mlm_pars_plot()

    95 |

    Plot estimated parameters of multilevel mediation model

    98 |

    mlm_path_plot()

    99 |

    Plot bmlm's mediation model as a path diagram

    102 |

    mlm_spaghetti_plot()

    103 |

    Plot fitted values of M and Y from multilevel mediation model

    106 |

    mlm_summary()

    107 |

    Print a summary of the estimated multilevel mediation model

    110 | 111 | 114 |
    115 | 116 | 117 |
    120 | 121 |
    122 |

    Site built with pkgdown 2.0.7.

    123 |
    124 | 125 |
    126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /docs/reference/isolate.html: -------------------------------------------------------------------------------- 1 | 2 | Create isolated within- (and optionally between-) person variables. — isolate • bmlm 6 | 7 | 8 |
    9 |
    59 | 60 | 61 | 62 |
    63 |
    64 | 69 | 70 |
    71 |

    Creates variables that represent pure within- and between-person predictors.

    72 |
    73 | 74 |
    75 |
    isolate(d = NULL, by = NULL, value = NULL, z = FALSE, which = "within")
    76 |
    77 | 78 |
    79 |

    Arguments

    80 |
    d
    81 |

    A data.frame.

    82 | 83 | 84 |
    by
    85 |

    A vector of values in d by which the data is clustered. 86 | i.e. a vector of unique participant IDs.

    87 | 88 | 89 |
    value
    90 |

    Names of columns in d to isolate. Multiple values can be 91 | given by value = c("var1", "var2", "var3")

    92 | 93 | 94 |
    z
    95 |

    Should the created values be standardized (defaults to FALSE).

    96 | 97 | 98 |
    which
    99 |

    Which component to return. "within" (default) returns 100 | within-person deviations only; "between" returns between-person means only; 101 | "both" returns both.

    102 | 103 |
    104 |
    105 |

    Value

    106 | 107 | 108 |

    A data.frame with additional columns for the within- and 109 | between-person variables. The new columns are labelled _cw for 110 | centered-within and _cb for centered-between.

    111 |
    112 |
    113 |

    Author

    114 |

    Matti Vuorre mv2521@columbia.edu

    115 |
    116 | 117 |
    118 |

    Examples

    119 |
    # Create within-person deviations of work stressors in BLch9.
    120 | data(BLch9)
    121 | BLch9 <- isolate(BLch9, by = "id", value = "fwkstrs")
    122 | head(BLch9)  # Now has new column for within-person work stressors.
    123 | #>    id time fwkstrs   fwkdis  freldis          x          m          y
    124 | #> 1 101    1       3 5.590119 3.034483  0.3333333  0.9828781 -1.4420726
    125 | #> 2 101    2       3 5.535224 4.620690  0.3333333  0.9279833  0.1441343
    126 | #> 3 101    3       3 3.888381 2.850575  0.3333333 -0.7188603 -1.6259807
    127 | #> 4 101    4       4 5.352242 6.398467  1.3333333  0.7450007  1.9219121
    128 | #> 5 101    5       1 4.483074 2.544061 -1.6666667 -0.1241668 -1.9324941
    129 | #> 6 101    6       2 3.339433 5.164751 -0.6666667 -1.2678081  0.6881956
    130 | #>   fwkstrs_cw
    131 | #> 1  0.3333333
    132 | #> 2  0.3333333
    133 | #> 3  0.3333333
    134 | #> 4  1.3333333
    135 | #> 5 -1.6666667
    136 | #> 6 -0.6666667
    137 | 
    138 | 
    139 |
    140 |
    141 | 144 |
    145 | 146 | 147 |
    150 | 151 |
    152 |

    Site built with pkgdown 2.0.7.

    153 |
    154 | 155 |
    156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /docs/reference/mlm_pars_plot.html: -------------------------------------------------------------------------------- 1 | 2 | Plot estimated parameters of multilevel mediation model — mlm_pars_plot • bmlm 6 | 7 | 8 |
    9 |
    59 | 60 | 61 | 62 |
    63 |
    64 | 69 | 70 |
    71 |

    Plot the model's estimated parameters as histograms or a coefficient plot.

    72 |
    73 | 74 |
    75 |
    mlm_pars_plot(
     76 |   mod = NULL,
     77 |   type = "hist",
     78 |   color = "black",
     79 |   p_shape = 15,
     80 |   p_size = 1.2,
     81 |   level = 0.95,
     82 |   nrow = 3,
     83 |   pars = c("a", "b", "cp", "covab", "me", "c", "pme")
     84 | )
    85 |
    86 | 87 |
    88 |

    Arguments

    89 |
    mod
    90 |

    A Stanfit model estimated with mlm().

    91 | 92 | 93 |
    type
    94 |

    Type of the plot, hist, coef, or violin.

    95 | 96 | 97 |
    color
    98 |

    Color (and fill) for plots.

    99 | 100 | 101 |
    p_shape
    102 |

    Shape of points for coefplot.

    103 | 104 | 105 |
    p_size
    106 |

    Size of points for coefplot.

    107 | 108 | 109 |
    level
    110 |

    X level for Credible Intervals. (Defaults to .95.)

    111 | 112 | 113 |
    nrow
    114 |

    Number of rows for multiple histograms.

    115 | 116 | 117 |
    pars
    118 |

    Which parameters to plot.

    119 | 120 |
    121 |
    122 |

    Value

    123 | 124 | 125 |

    A ggplot2 object.

    126 |
    127 |
    128 |

    Details

    129 |

    The point estimate for the coefficient plot is the posterior mean.

    130 |
    131 |
    132 |

    Author

    133 |

    Matti Vuorre mv2521@columbia.edu

    134 |
    135 | 136 |
    137 | 140 |
    141 | 142 | 143 |
    146 | 147 |
    148 |

    Site built with pkgdown 2.0.7.

    149 |
    150 | 151 |
    152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /docs/reference/mlm_path_plot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/docs/reference/mlm_path_plot-1.png -------------------------------------------------------------------------------- /docs/reference/mlm_path_plot.html: -------------------------------------------------------------------------------- 1 | 2 | Plot bmlm's mediation model as a path diagram — mlm_path_plot • bmlm 6 | 7 | 8 |
    9 |
    59 | 60 | 61 | 62 |
    63 |
    64 | 69 | 70 |
    71 |

    Plots a path diagram for an estimated multilevel mediation model.

    72 |
    73 | 74 |
    75 |
    mlm_path_plot(
     76 |   mod = NULL,
     77 |   xlab = "X",
     78 |   ylab = "Y",
     79 |   mlab = "M",
     80 |   level = 0.95,
     81 |   random = TRUE,
     82 |   text = FALSE,
     83 |   id = NULL,
     84 |   digits = 2,
     85 |   ...
     86 | )
    87 |
    88 | 89 |
    90 |

    Arguments

    91 |
    mod
    92 |

    A Stanfit model estimated with mlm().

    93 | 94 | 95 |
    xlab
    96 |

    Label for X

    97 | 98 | 99 |
    ylab
    100 |

    Label for Y

    101 | 102 | 103 |
    mlab
    104 |

    Label for M

    105 | 106 | 107 |
    level
    108 |

    "Confidence" level for credible intervals. (Defaults to .95.)

    109 | 110 | 111 |
    random
    112 |

    Should the "random" effects SDs be displayed? (Default = TRUE)

    113 | 114 | 115 |
    text
    116 |

    Should additional parameter values be displayed? 117 | (Defaults to FALSE.)

    118 | 119 | 120 |
    id
    121 |

    Plot an individual-level path diagram by specifying ID number.

    122 | 123 | 124 |
    digits
    125 |

    Number of significant digits to show on graph. (Default = 2.)

    126 | 127 | 128 |
    ...
    129 |

    Other arguments passed on to qgraph::qgraph().

    130 | 131 |
    132 |
    133 |

    Value

    134 | 135 | 136 |

    A qgraph object.

    137 |
    138 |
    139 |

    Details

    140 |

    Plots a path diagram of the mediation model, 141 | with estimated parameter values and credible intervals. Can also 142 | be used to draw a template diagram of the mediation model by not 143 | specifying input to the mod argument.

    144 |

    To modify various settings of the underlying qgraph object, see 145 | qgraph.

    146 |
    147 |
    148 |

    Author

    149 |

    Matti Vuorre mv2521@columbia.edu

    150 |
    151 | 152 |
    153 |

    Examples

    154 |
    # Draw a template path diagram of the mediation model
    155 | mlm_path_plot()
    156 | 
    157 | 
    158 | 
    159 |
    160 |
    161 | 164 |
    165 | 166 | 167 |
    170 | 171 |
    172 |

    Site built with pkgdown 2.0.7.

    173 |
    174 | 175 |
    176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /docs/reference/mlm_spaghetti_plot.html: -------------------------------------------------------------------------------- 1 | 2 | Plot fitted values of M and Y from multilevel mediation model — mlm_spaghetti_plot • bmlm 7 | 8 | 9 |
    10 |
    60 | 61 | 62 | 63 |
    64 |
    65 | 70 | 71 |
    72 |

    Plot population-level fitted values and X 73 | values, for M and Y.

    74 |
    75 | 76 |
    77 |
    mlm_spaghetti_plot(
     78 |   mod = NULL,
     79 |   d = NULL,
     80 |   id = "id",
     81 |   x = "x",
     82 |   m = "m",
     83 |   y = "y",
     84 |   level = 0.95,
     85 |   n = 12,
     86 |   binary_y = FALSE,
     87 |   mx = "fitted",
     88 |   fixed = TRUE,
     89 |   random = TRUE,
     90 |   h_jitter = 0,
     91 |   v_jitter = 0,
     92 |   bar_width = 0.2,
     93 |   bar_size = 0.75,
     94 |   n_samples = NA
     95 | )
    96 |
    97 | 98 |
    99 |

    Arguments

    100 |
    mod
    101 |

    A multilevel mediation model estimated with mlm().

    102 | 103 | 104 |
    d
    105 |

    A data.frame or a data_frame used in fitting model.

    106 | 107 | 108 |
    id
    109 |

    Name of id variable (identifying subjects) in data (d).

    110 | 111 | 112 |
    x
    113 |

    Name of X variable in data.

    114 | 115 | 116 |
    m
    117 |

    Name of M variable in data.

    118 | 119 | 120 |
    y
    121 |

    Name of Y variable in data.

    122 | 123 | 124 |
    level
    125 |

    X level for Credible Intervals. (Defaults to .95.)

    126 | 127 | 128 |
    n
    129 |

    Number of points along X to evaluate fitted values on. 130 | See details.

    131 | 132 | 133 |
    binary_y
    134 |

    Set to TRUE if the outcome variable (Y) is 0/1.

    135 | 136 | 137 |
    mx
    138 |

    Should the X axis of the M-Y figure be "fitted" values, 139 | or "data" values. Defaults to "fitted".

    140 | 141 | 142 |
    fixed
    143 |

    Should the population-level ("fixed") fitted values be shown?

    144 | 145 | 146 |
    random
    147 |

    Should the subject-level ("random") fitted values be shown?

    148 | 149 | 150 |
    h_jitter
    151 |

    Horizontal jitter of points. Defaults to 0.

    152 | 153 | 154 |
    v_jitter
    155 |

    Vertical jitter of points. Defaults to 0.

    156 | 157 | 158 |
    bar_width
    159 |

    Width of the error bars. Defaults to 0.2.

    160 | 161 | 162 |
    bar_size
    163 |

    Thickness of the error bars. Defaults to 0.75.

    164 | 165 | 166 |
    n_samples
    167 |

    Number of MCMC samples to use in calculating fitted values. 168 | See details.

    169 | 170 |
    171 |
    172 |

    Value

    173 | 174 | 175 |

    A list of two ggplot2 objects.

    176 |
    177 |
    178 |

    Details

    179 |

    If n = 2, the fitted values will be represented as points 180 | with X 181 | line with a Confidence Ribbon instead. 182 | If a very large model is fitted with a large number of MCMC iterations, 183 | the function might take a long time to run. In these cases, users can set 184 | n_samples to a smaller value (e.g. 1000), in which case the fitted 185 | values (and the CIs) will be based on a random subset of n_samples 186 | MCMC samples. The default value is NA, meaning that all MCMC samples are 187 | used.

    188 |
    189 |
    190 |

    Author

    191 |

    Matti Vuorre mv2521@columbia.edu

    192 |
    193 | 194 |
    195 | 198 |
    199 | 200 | 201 |
    204 | 205 |
    206 |

    Site built with pkgdown 2.0.7.

    207 |
    208 | 209 |
    210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | -------------------------------------------------------------------------------- /docs/reference/mlm_summary.html: -------------------------------------------------------------------------------- 1 | 2 | Print a summary of the estimated multilevel mediation model — mlm_summary • bmlm 7 | 8 | 9 |
    10 |
    60 | 61 | 62 | 63 |
    64 |
    65 | 70 | 71 |
    72 |

    Prints the estimated parameters (numerical summaries of the marginal 73 | posterior distributions).

    74 |
    75 | 76 |
    77 |
    mlm_summary(
     78 |   mod = NULL,
     79 |   level = 0.95,
     80 |   pars = c("a", "b", "cp", "me", "c", "pme"),
     81 |   digits = 2
     82 | )
    83 |
    84 | 85 |
    86 |

    Arguments

    87 |
    mod
    88 |

    A stanfit object obtained from mlm()

    89 | 90 | 91 |
    level
    92 |

    "Confidence" level; Defines the limits of the credible intervals. 93 | Defaults to .95 (i.e. displays 95% CIs.)

    94 | 95 | 96 |
    pars
    97 |

    Parameters to summarize. Defaults to main average-level 98 | parameters. See Details for more information.

    99 | 100 | 101 |
    digits
    102 |

    How many decimal points to display in the output. Defaults to 2.

    103 | 104 |
    105 |
    106 |

    Value

    107 | 108 | 109 |

    A data.frame summarizing the estimated multilevel 110 | mediation model:

    Parameter
    111 |

    Name of parameter

    112 | 113 |
    Mean
    114 |

    Mean of parameter's posterior distribution.

    115 | 116 |
    Median
    117 |

    Median of parameter's posterior distribution.

    118 | 119 |
    SE
    120 |

    Standard deviation of parameter's posterior distribution.

    121 | 122 |
    ci_lwr
    123 |

    The lower limit of Credible Intervals.

    124 | 125 |
    ci_upr
    126 |

    The upper limit of Credible Intervals.

    127 | 128 |
    n_eff
    129 |

    Number of efficient samples.

    130 | 131 |
    Rhat
    132 |

    Should be 1.00.

    133 | 134 | 135 |
    136 |
    137 |

    Details

    138 |

    After estimating a model (drawing samples from the joint posterior 139 | probability distribution) with mlm(), show the estimated results 140 | by using mlm_summary(fit), where fit is an object containing 141 | the fitted model.

    142 |

    The function shows, for each parameter specified with pars, 143 | the posterior mean, and limits of the Credible Interval as specified 144 | by level. For example, level = .91 shows a 145 | 91% Credible Interval, which summarizes the central 91% mass of 146 | the marginal posterior distribution.

    147 |
    148 |

    Parameters

    149 |

    By default, mlm() estimates and returns a large number of parameters, 150 | including the varying effects, and their associated standard deviations. 151 | However, mlm_summay() by default only displays a subset of the 152 | estimated parameters:

    153 |
    a
    154 |

    Regression slope of the X -> M relationship.

    155 | 156 |
    b
    157 |

    Regression slope of the M -> Y relationship.

    158 | 159 |
    cp
    160 |

    Regression slope of the X -> Y relationship. (Direct effect.)

    161 | 162 |
    me
    163 |

    Mediated effect (\(a * b + \sigma_{{a_j}{b_j}}\)).

    164 | 165 |
    c
    166 |

    Total effect of X on Y. ( \(cp + me\) )

    167 | 168 |
    pme
    169 |

    Percent mediated effect.

    170 | 171 | 172 |

    The user may specify pars = NULL to display all estimated parameters. 173 | Other options include e.g. pars = "tau" to display the varying 174 | effects' standard deviations. To display all the group-level parameters 175 | (also known as random effects) only, specify pars = "random". 176 | With this argument, mlm_summary() prints the following parameters:

    177 |
    tau_a
    178 |

    Standard deviation of subject-level a_js.

    179 | 180 |
    tau_b
    181 |

    Standard deviation of subject-level b_js.

    182 | 183 |
    tau_cp
    184 |

    Standard deviation of subject-level c\'_js.

    185 | 186 |
    covab
    187 |

    Estimated covariance of a_j and b_js.

    188 | 189 |
    corrab
    190 |

    Estimated correlation of a_j and b_js.

    191 | 192 | 193 |

    To learn more about the additional parameters, refer to the Stan code 194 | (cat(get_stancode(fit))).

    195 |
    196 | 197 |
    198 |
    199 |

    Author

    200 |

    Matti Vuorre mv2521@columbia.edu

    201 |
    202 | 203 |
    204 | 207 |
    208 | 209 | 210 |
    213 | 214 |
    215 |

    Site built with pkgdown 2.0.7.

    216 |
    217 | 218 |
    219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | -------------------------------------------------------------------------------- /docs/reference/tab2doc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Create a word document from a summary table. — tab2doc • bmlm 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 44 | 45 | 46 | 47 | 48 | 49 |
    50 |
    51 | 114 | 115 | 116 |
    117 | 118 |
    119 |
    120 | 125 | 126 |
    127 | 128 |

    Saves a Word document to the current working directory.

    129 | 130 |
    131 | 132 |
    tab2doc(d = NULL, name = NULL)
    133 | 134 |

    Arguments

    135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 |
    d

    A data.frame.

    name

    Name of file to create. Defaults to "Table.docx".

    146 | 147 |

    Value

    148 | 149 |

    Saves a word document in the current working directory.

    150 | 151 |

    Details

    152 | 153 |

    Requires the ReporteRs R package, which is no longer maintained. 154 | Copy-pasting individual values is error-prone. Use tab2doc() 155 | to create a word document containing a summary table.

    156 | 157 | 158 |

    Examples

    159 |
    # NOT RUN {
    160 | tab2doc(mlm_summary(fit), name = "Fit_summary")
    161 | # }
    162 |
    163 |
    164 | 181 |
    182 | 183 |
    184 | 187 | 188 |
    189 |

    Site built with pkgdown.

    190 |
    191 | 192 |
    193 |
    194 | 195 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | c( 2 | bibentry( 3 | bibtype = "Article", 4 | title = "Within-subject mediation analysis for experimental data in cognitive psychology and neuroscience", 5 | author = c( 6 | person( 7 | given = "Matti", 8 | family = "Vuorre", 9 | email = "m.j.vuorre@tilburguniversity.edu", 10 | role = "aut", 11 | comment = c(ORCID = "0000-0001-5052-066X") 12 | ), 13 | person( 14 | given = "Niall", 15 | family = "Bolger", 16 | email = "nb2229@columbia.edu", 17 | role = "aut" 18 | ) 19 | ), 20 | journal = "Behavior Research Methods", 21 | year = 2018, 22 | doi = "10.3758/s13428-017-0980-9" 23 | ), 24 | bibentry( 25 | bibtype = "Manual", 26 | title = "bmlm: Bayesian Multilevel Mediation", 27 | author = c( 28 | person( 29 | given = "Matti", 30 | family = "Vuorre", 31 | email = "m.j.vuorre@tilburguniversity.edu", 32 | role = c("aut", "cre"), 33 | comment = c(ORCID = "0000-0001-5052-066X") 34 | ) 35 | ), 36 | year = "2023", 37 | note = "R package version 1.3.15", 38 | url = "https://CRAN.R-project.org/package=bmlm" 39 | ) 40 | ) 41 | -------------------------------------------------------------------------------- /inst/chunks/license.stan: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/inst/chunks/license.stan -------------------------------------------------------------------------------- /inst/include/stan_meta_header.hpp: -------------------------------------------------------------------------------- 1 | // Insert all #include statements here -------------------------------------------------------------------------------- /inst/stan/bmlm.stan: -------------------------------------------------------------------------------- 1 | // Stan code for multilevel mediation model 2 | 3 | data { 4 | #include /chunks/data.stan 5 | vector[N] Y; // Continuous outcome 6 | } 7 | transformed data{ 8 | #include /chunks/transformed_data.stan 9 | } 10 | parameters{ 11 | #include /chunks/parameters.stan 12 | real sigma_y; // Residual 13 | } 14 | transformed parameters { 15 | #include /chunks/transformed_parameters.stan 16 | } 17 | model { 18 | #include /chunks/model.stan 19 | // Data model 20 | Y ~ normal(mu_y, sigma_y); 21 | M ~ normal(mu_m, sigma_m); 22 | } 23 | generated quantities{ 24 | #include /chunks/generated_quantities.stan 25 | } 26 | -------------------------------------------------------------------------------- /inst/stan/bmlm_binary_y.stan: -------------------------------------------------------------------------------- 1 | // Stan code for multilevel mediation model 2 | 3 | data { 4 | #include chunks/data.stan 5 | array[N] int Y; // Dichotomous outcome 6 | } 7 | transformed data{ 8 | #include chunks/transformed_data.stan 9 | } 10 | parameters{ 11 | #include chunks/parameters.stan 12 | } 13 | transformed parameters { 14 | #include chunks/transformed_parameters.stan 15 | } 16 | model { 17 | #include chunks/model.stan 18 | // Data model 19 | Y ~ bernoulli_logit(mu_y); 20 | M ~ normal(mu_m, sigma_m); 21 | } 22 | generated quantities{ 23 | #include chunks/generated_quantities.stan 24 | } 25 | -------------------------------------------------------------------------------- /inst/stan/chunks/common_functions.stan: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/inst/stan/chunks/common_functions.stan -------------------------------------------------------------------------------- /inst/stan/chunks/data.stan: -------------------------------------------------------------------------------- 1 | int N; // Number of observations 2 | int J; // Number of participants 3 | array[N] int id; // Participant IDs 4 | vector[N] X; // Manipulated variable 5 | vector[N] M; // Mediator 6 | // Priors 7 | real prior_dm; 8 | real prior_dy; 9 | real prior_a; 10 | real prior_b; 11 | real prior_cp; 12 | real prior_tau_dm; 13 | real prior_tau_dy; 14 | real prior_tau_a; 15 | real prior_tau_b; 16 | real prior_tau_cp; 17 | real prior_lkj_shape; 18 | -------------------------------------------------------------------------------- /inst/stan/chunks/generated_quantities.stan: -------------------------------------------------------------------------------- 1 | matrix[K, K] Omega; // Correlation matrix 2 | matrix[K, K] Sigma; // Covariance matrix 3 | 4 | // Average mediation parameters 5 | real covab; // a-b covariance 6 | real corrab; // a-b correlation 7 | real me; // Mediated effect 8 | real c; // Total effect 9 | real pme; // % mediated effect 10 | 11 | // Person-specific mediation parameters 12 | vector[J] u_a; 13 | vector[J] u_b; 14 | vector[J] u_cp; 15 | vector[J] u_dy; 16 | vector[J] u_dm; 17 | vector[J] u_c; 18 | vector[J] u_me; 19 | vector[J] u_pme; 20 | 21 | // Re-named tau parameters for easy output 22 | real tau_cp; 23 | real tau_b; 24 | real tau_a; 25 | real tau_dy; 26 | real tau_dm; 27 | 28 | tau_cp = Tau[1]; 29 | tau_b = Tau[2]; 30 | tau_a = Tau[3]; 31 | tau_dy = Tau[4]; 32 | tau_dm = Tau[5]; 33 | 34 | Omega = L_Omega * L_Omega'; 35 | Sigma = quad_form_diag(Omega, Tau); 36 | 37 | covab = Sigma[3,2]; 38 | corrab = Omega[3,2]; 39 | me = a*b + covab; 40 | c = cp + me; 41 | pme = me / c; 42 | 43 | for (j in 1:J) { 44 | u_a[j] = a + U[j, 3]; 45 | u_b[j] = b + U[j, 2]; 46 | u_me[j] = (a + U[j, 3]) * (b + U[j, 2]); 47 | u_cp[j] = cp + U[j, 1]; 48 | u_dy[j] = dy + U[j, 4]; 49 | u_dm[j] = dm + U[j, 5]; 50 | u_c[j] = u_cp[j] + u_me[j]; 51 | u_pme[j] = u_me[j] / u_c[j]; 52 | } 53 | -------------------------------------------------------------------------------- /inst/stan/chunks/model.stan: -------------------------------------------------------------------------------- 1 | // Means of linear models 2 | vector[N] mu_y; 3 | vector[N] mu_m; 4 | // Regression parameter priors 5 | dy ~ normal(0, prior_dy); 6 | dm ~ normal(0, prior_dm); 7 | a ~ normal(0, prior_a); 8 | b ~ normal(0, prior_b); 9 | cp ~ normal(0, prior_cp); 10 | // SDs and correlation matrix 11 | Tau[1] ~ cauchy(0, prior_tau_cp); // u_cp 12 | Tau[2] ~ cauchy(0, prior_tau_b); // u_b 13 | Tau[3] ~ cauchy(0, prior_tau_a); // u_a 14 | Tau[4] ~ cauchy(0, prior_tau_dy); // u_intercept_y 15 | Tau[5] ~ cauchy(0, prior_tau_dm); // u_intercept_m 16 | L_Omega ~ lkj_corr_cholesky(prior_lkj_shape); 17 | // Allow vectorized sampling of varying effects via stdzd z_U 18 | to_vector(z_U) ~ normal(0, 1); 19 | 20 | // Regressions 21 | for (n in 1:N){ 22 | mu_y[n] = (cp + U[id[n], 1]) * X[n] + 23 | (b + U[id[n], 2]) * M[n] + 24 | (dy + U[id[n], 4]); 25 | mu_m[n] = (a + U[id[n], 3]) * X[n] + 26 | (dm + U[id[n], 5]); 27 | } 28 | -------------------------------------------------------------------------------- /inst/stan/chunks/parameters.stan: -------------------------------------------------------------------------------- 1 | // Regression Y on X and M 2 | real dy; // Intercept 3 | real cp; // X to Y effect 4 | real b; // M to Y effect 5 | // Regression M on X 6 | real dm; // Intercept 7 | real a; // X to M effect 8 | real sigma_m; // Residual 9 | 10 | // Correlation matrix and SDs of participant-level varying effects 11 | cholesky_factor_corr[K] L_Omega; 12 | vector[K] Tau; 13 | 14 | // Standardized varying effects 15 | matrix[K, J] z_U; 16 | -------------------------------------------------------------------------------- /inst/stan/chunks/transformed_data.stan: -------------------------------------------------------------------------------- 1 | int K; // Number of predictors 2 | K = 5; 3 | -------------------------------------------------------------------------------- /inst/stan/chunks/transformed_parameters.stan: -------------------------------------------------------------------------------- 1 | // Participant-level varying effects 2 | matrix[J, K] U; 3 | U = (diag_pre_multiply(Tau, L_Omega) * z_U)'; 4 | -------------------------------------------------------------------------------- /man/BLch9.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{BLch9} 5 | \alias{BLch9} 6 | \title{Relationship between work stressors, work dissatisfaction, 7 | and relationship dissatisfaction.} 8 | \format{ 9 | A data frame with 2100 rows and 8 variables: 10 | \describe{ 11 | \item{id}{ID of study participant} 12 | \item{time}{Time} 13 | \item{fwkstrs}{Number of work stressors} 14 | \item{fwkdis}{Work dissatisfaction rating} 15 | \item{freldis}{Relationship dissatisfaction} 16 | \item{x}{Subject-mean deviated number of work stressors} 17 | \item{m}{Subject-mean deviated work dissatisfaction rating} 18 | \item{y}{Subject-mean deviated relationship dissatisfaction} 19 | } 20 | } 21 | \source{ 22 | \url{http://www.intensivelongitudinal.com/datasets.html} 23 | } 24 | \usage{ 25 | data(BLch9) 26 | } 27 | \description{ 28 | Simulated data from Intensive Longitudinal Methods: 29 | An Introduction to Diary and Experience Sampling Research. 30 | (Bolger, & Laurenceau, 2013, chapter 9; 31 | \url{http://www.intensivelongitudinal.com/index.html}). 32 | } 33 | \keyword{datasets} 34 | -------------------------------------------------------------------------------- /man/MEC2010.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{MEC2010} 5 | \alias{MEC2010} 6 | \title{Judgments of performance in a video game} 7 | \format{ 8 | A data frame with 344 rows and 4 variables: 9 | \describe{ 10 | \item{subj}{Subject id number.} 11 | \item{lag}{Lag condition (0 = no lag, 1 = 250ms lag).} 12 | \item{hr}{Hit rate.} 13 | \item{jop}{Judgment of Performance.} 14 | } 15 | } 16 | \source{ 17 | Metcalfe, J., Eich, T. S., & Castel, A. D. (2010). 18 | Metacognition of agency across the lifespan. 19 | Cognition, 116(2), 267-282. 20 | \doi{10.1016/j.cognition.2010.05.009} 21 | } 22 | \usage{ 23 | data(MEC2010) 24 | } 25 | \description{ 26 | Data from an experiment where participants rated their 27 | performance in a video game in two conditions. 28 | (Experiment 1 in Metcalfe, Eich, & Castel, 2010; 29 | \url{https://www.sciencedirect.com/science/article/pii/S0010027710001113}). 30 | } 31 | \keyword{datasets} 32 | -------------------------------------------------------------------------------- /man/bmlm-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bmlm-package.R 3 | \docType{package} 4 | \name{bmlm-package} 5 | \alias{bmlm-package} 6 | \alias{bmlm} 7 | \title{bmlm: Easy estimation of Bayesian multilevel mediation models with Stan.} 8 | \description{ 9 | See \url{https://mvuorre.github.io/bmlm/} for a short tutorial. 10 | } 11 | -------------------------------------------------------------------------------- /man/isolate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{isolate} 4 | \alias{isolate} 5 | \title{Create isolated within- (and optionally between-) person variables.} 6 | \usage{ 7 | isolate(d = NULL, by = NULL, value = NULL, z = FALSE, which = "within") 8 | } 9 | \arguments{ 10 | \item{d}{A \code{data.frame}.} 11 | 12 | \item{by}{A vector of values in \code{d} by which the data is clustered. 13 | i.e. a vector of unique participant IDs.} 14 | 15 | \item{value}{Names of columns in \code{d} to isolate. Multiple values can be 16 | given by \code{value = c("var1", "var2", "var3")}} 17 | 18 | \item{z}{Should the created values be standardized (defaults to FALSE).} 19 | 20 | \item{which}{Which component to return. "within" (default) returns 21 | within-person deviations only; "between" returns between-person means only; 22 | "both" returns both.} 23 | } 24 | \value{ 25 | A \code{data.frame} with additional columns for the within- and 26 | between-person variables. The new columns are labelled _cw for 27 | centered-within and _cb for centered-between. 28 | } 29 | \description{ 30 | Creates variables that represent pure within- and between-person predictors. 31 | } 32 | \examples{ 33 | # Create within-person deviations of work stressors in BLch9. 34 | data(BLch9) 35 | BLch9 <- isolate(BLch9, by = "id", value = "fwkstrs") 36 | head(BLch9) # Now has new column for within-person work stressors. 37 | 38 | } 39 | \author{ 40 | Matti Vuorre \email{mv2521@columbia.edu} 41 | } 42 | -------------------------------------------------------------------------------- /man/mlm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mlm.R 3 | \name{mlm} 4 | \alias{mlm} 5 | \title{Estimate a multilevel mediation model} 6 | \usage{ 7 | mlm( 8 | d = NULL, 9 | id = "id", 10 | x = "x", 11 | m = "m", 12 | y = "y", 13 | priors = NULL, 14 | binary_y = FALSE, 15 | ... 16 | ) 17 | } 18 | \arguments{ 19 | \item{d}{A \code{data.frame} or a \code{data_frame}.} 20 | 21 | \item{id}{Column of participant IDs in \code{data}.} 22 | 23 | \item{x}{Column of X values in \code{data}.} 24 | 25 | \item{m}{Column of M values in \code{data}.} 26 | 27 | \item{y}{Column of Y values in \code{data}.} 28 | 29 | \item{priors}{A list of named values to be used as the prior scale 30 | parameters. See details.} 31 | 32 | \item{binary_y}{Set to TRUE if y is binary and should be modelled 33 | with logistic regression. Defaults to FALSE (y treated as continuous.) 34 | This feature is experimental.} 35 | 36 | \item{...}{Other optional parameters passed to \code{rstan::stan()}.} 37 | } 38 | \value{ 39 | An object of S4 class stanfit, with all its available methods. 40 | } 41 | \description{ 42 | Estimates a Bayesian multilevel mediation model using Stan. 43 | } 44 | \details{ 45 | Draw samples from the joint posterior distribution of a 46 | multilevel mediation model using Stan. 47 | 48 | \subsection{Priors}{ 49 | 50 | Users may pass a list of named values for the \code{priors} argument. 51 | The values will be used to define the scale parameter of the 52 | respective prior distributions. 53 | This list may specify some or all of the following parameters: 54 | 55 | \describe{ 56 | \item{dy, dm}{Regression intercepts (for Y and M as outcomes, respectively.)} 57 | \item{a, b, cp}{Regression slopes.} 58 | \item{tau_x}{Varying effects SDs for above parameters (e.g replace x with a.)} 59 | \item{lkj_shape}{Shape parameter for the LKJ prior.} 60 | } 61 | See examples for specifying the following: Gaussian distributions with SD = 10 62 | as priors for the intercepts, Gaussians with SD = 2 for the slopes, 63 | Half-Cauchy distributions with scale parameters 1 for the varying effects 64 | SDs, and an LKJ prior of 2. 65 | } 66 | } 67 | \examples{ 68 | \dontrun{ 69 | ## Run example from Bolger and Laurenceau (2013) 70 | data(BLch9) 71 | fit <- mlm(BLch9) 72 | mlm_summary(fit) 73 | 74 | ### With priors 75 | Priors <- list(dy = 10, dm = 10, a = 2, b = 2, cp = 2, 76 | tau_dy = 1, tau_dm = 1, tau_a = 1, tau_b = 1, tau_cp = 1, 77 | lkj_shape = 2) 78 | fit <- mlm(BLch9, priors = Priors) 79 | } 80 | 81 | } 82 | \author{ 83 | Matti Vuorre \email{mv2521@columbia.edu} 84 | } 85 | -------------------------------------------------------------------------------- /man/mlm_pars_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mlm_plot.R 3 | \name{mlm_pars_plot} 4 | \alias{mlm_pars_plot} 5 | \title{Plot estimated parameters of multilevel mediation model} 6 | \usage{ 7 | mlm_pars_plot( 8 | mod = NULL, 9 | type = "hist", 10 | color = "black", 11 | p_shape = 15, 12 | p_size = 1.2, 13 | level = 0.95, 14 | nrow = 3, 15 | pars = c("a", "b", "cp", "covab", "me", "c", "pme") 16 | ) 17 | } 18 | \arguments{ 19 | \item{mod}{A Stanfit model estimated with \code{mlm()}.} 20 | 21 | \item{type}{Type of the plot, \code{hist}, \code{coef}, or \code{violin}.} 22 | 23 | \item{color}{Color (and fill) for plots.} 24 | 25 | \item{p_shape}{Shape of points for coefplot.} 26 | 27 | \item{p_size}{Size of points for coefplot.} 28 | 29 | \item{level}{X level for Credible Intervals. (Defaults to .95.)} 30 | 31 | \item{nrow}{Number of rows for multiple histograms.} 32 | 33 | \item{pars}{Which parameters to plot.} 34 | } 35 | \value{ 36 | A ggplot2 object. 37 | } 38 | \description{ 39 | Plot the model's estimated parameters as histograms or a coefficient plot. 40 | } 41 | \details{ 42 | The point estimate for the coefficient plot is the posterior mean. 43 | } 44 | \author{ 45 | Matti Vuorre \email{mv2521@columbia.edu} 46 | } 47 | -------------------------------------------------------------------------------- /man/mlm_path_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mlm_plot.R 3 | \name{mlm_path_plot} 4 | \alias{mlm_path_plot} 5 | \title{Plot \code{bmlm}'s mediation model as a path diagram} 6 | \usage{ 7 | mlm_path_plot( 8 | mod = NULL, 9 | xlab = "X", 10 | ylab = "Y", 11 | mlab = "M", 12 | level = 0.95, 13 | random = TRUE, 14 | text = FALSE, 15 | id = NULL, 16 | digits = 2, 17 | ... 18 | ) 19 | } 20 | \arguments{ 21 | \item{mod}{A Stanfit model estimated with \code{mlm()}.} 22 | 23 | \item{xlab}{Label for X} 24 | 25 | \item{ylab}{Label for Y} 26 | 27 | \item{mlab}{Label for M} 28 | 29 | \item{level}{"Confidence" level for credible intervals. (Defaults to .95.)} 30 | 31 | \item{random}{Should the "random" effects SDs be displayed? (Default = TRUE)} 32 | 33 | \item{text}{Should additional parameter values be displayed? 34 | (Defaults to FALSE.)} 35 | 36 | \item{id}{Plot an individual-level path diagram by specifying ID number.} 37 | 38 | \item{digits}{Number of significant digits to show on graph. (Default = 2.)} 39 | 40 | \item{...}{Other arguments passed on to \code{qgraph::qgraph()}.} 41 | } 42 | \value{ 43 | A qgraph object. 44 | } 45 | \description{ 46 | Plots a path diagram for an estimated multilevel mediation model. 47 | } 48 | \details{ 49 | Plots a path diagram of the mediation model, 50 | with estimated parameter values and credible intervals. Can also 51 | be used to draw a template diagram of the mediation model by not 52 | specifying input to the \code{mod} argument. 53 | 54 | To modify various settings of the underlying qgraph object, see 55 | \code{\link[qgraph]{qgraph}}. 56 | } 57 | \examples{ 58 | # Draw a template path diagram of the mediation model 59 | mlm_path_plot() 60 | 61 | } 62 | \author{ 63 | Matti Vuorre \email{mv2521@columbia.edu} 64 | } 65 | -------------------------------------------------------------------------------- /man/mlm_spaghetti_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mlm_plot.R 3 | \name{mlm_spaghetti_plot} 4 | \alias{mlm_spaghetti_plot} 5 | \title{Plot fitted values of M and Y from multilevel mediation model} 6 | \usage{ 7 | mlm_spaghetti_plot( 8 | mod = NULL, 9 | d = NULL, 10 | id = "id", 11 | x = "x", 12 | m = "m", 13 | y = "y", 14 | level = 0.95, 15 | n = 12, 16 | binary_y = FALSE, 17 | mx = "fitted", 18 | fixed = TRUE, 19 | random = TRUE, 20 | h_jitter = 0, 21 | v_jitter = 0, 22 | bar_width = 0.2, 23 | bar_size = 0.75, 24 | n_samples = NA 25 | ) 26 | } 27 | \arguments{ 28 | \item{mod}{A multilevel mediation model estimated with \code{mlm()}.} 29 | 30 | \item{d}{A \code{data.frame} or a \code{data_frame} used in fitting model.} 31 | 32 | \item{id}{Name of id variable (identifying subjects) in data (\code{d}).} 33 | 34 | \item{x}{Name of X variable in \code{data}.} 35 | 36 | \item{m}{Name of M variable in \code{data}.} 37 | 38 | \item{y}{Name of Y variable in \code{data}.} 39 | 40 | \item{level}{X level for Credible Intervals. (Defaults to .95.)} 41 | 42 | \item{n}{Number of points along X to evaluate fitted values on. 43 | See details.} 44 | 45 | \item{binary_y}{Set to TRUE if the outcome variable (Y) is 0/1.} 46 | 47 | \item{mx}{Should the X axis of the M-Y figure be "fitted" values, 48 | or "data" values. Defaults to "fitted".} 49 | 50 | \item{fixed}{Should the population-level ("fixed") fitted values be shown?} 51 | 52 | \item{random}{Should the subject-level ("random") fitted values be shown?} 53 | 54 | \item{h_jitter}{Horizontal jitter of points. Defaults to 0.} 55 | 56 | \item{v_jitter}{Vertical jitter of points. Defaults to 0.} 57 | 58 | \item{bar_width}{Width of the error bars. Defaults to 0.2.} 59 | 60 | \item{bar_size}{Thickness of the error bars. Defaults to 0.75.} 61 | 62 | \item{n_samples}{Number of MCMC samples to use in calculating fitted values. 63 | See details.} 64 | } 65 | \value{ 66 | A list of two ggplot2 objects. 67 | } 68 | \description{ 69 | Plot population-level fitted values and X% CI, and subject level fitted 70 | values, for M and Y. 71 | } 72 | \details{ 73 | If \code{n = 2}, the fitted values will be represented as points 74 | with X% CIs as "error bars". If \code{n > 2}, the representation will be a 75 | line with a Confidence Ribbon instead. 76 | If a very large model is fitted with a large number of MCMC iterations, 77 | the function might take a long time to run. In these cases, users can set 78 | \code{n_samples} to a smaller value (e.g. 1000), in which case the fitted 79 | values (and the CIs) will be based on a random subset of \code{n_samples} 80 | MCMC samples. The default value is NA, meaning that all MCMC samples are 81 | used. 82 | } 83 | \author{ 84 | Matti Vuorre \email{mv2521@columbia.edu} 85 | } 86 | -------------------------------------------------------------------------------- /man/mlm_summary.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils.R 3 | \name{mlm_summary} 4 | \alias{mlm_summary} 5 | \title{Print a summary of the estimated multilevel mediation model} 6 | \usage{ 7 | mlm_summary( 8 | mod = NULL, 9 | level = 0.95, 10 | pars = c("a", "b", "cp", "me", "c", "pme"), 11 | digits = 2 12 | ) 13 | } 14 | \arguments{ 15 | \item{mod}{A \code{stanfit} object obtained from \code{mlm()}} 16 | 17 | \item{level}{"Confidence" level; Defines the limits of the credible intervals. 18 | Defaults to .95 (i.e. displays 95\% CIs.)} 19 | 20 | \item{pars}{Parameters to summarize. Defaults to main average-level 21 | parameters. See Details for more information.} 22 | 23 | \item{digits}{How many decimal points to display in the output. Defaults to 2.} 24 | } 25 | \value{ 26 | A \code{data.frame} summarizing the estimated multilevel 27 | mediation model: 28 | \describe{ 29 | \item{Parameter}{Name of parameter} 30 | \item{Mean}{Mean of parameter's posterior distribution.} 31 | \item{Median}{Median of parameter's posterior distribution.} 32 | \item{SE}{Standard deviation of parameter's posterior distribution.} 33 | \item{ci_lwr}{The lower limit of Credible Intervals.} 34 | \item{ci_upr}{The upper limit of Credible Intervals.} 35 | \item{n_eff}{Number of efficient samples.} 36 | \item{Rhat}{Should be 1.00.} 37 | } 38 | } 39 | \description{ 40 | Prints the estimated parameters (numerical summaries of the marginal 41 | posterior distributions). 42 | } 43 | \details{ 44 | After estimating a model (drawing samples from the joint posterior 45 | probability distribution) with \code{mlm()}, show the estimated results 46 | by using \code{mlm_summary(fit)}, where \code{fit} is an object containing 47 | the fitted model. 48 | 49 | The function shows, for each parameter specified with \code{pars}, 50 | the posterior mean, and limits of the Credible Interval as specified 51 | by \code{level}. For example, \code{level = .91} shows a 52 | 91\% Credible Interval, which summarizes the central 91\% mass of 53 | the marginal posterior distribution. 54 | 55 | \subsection{Parameters}{ 56 | By default, \code{mlm()} estimates and returns a large number of parameters, 57 | including the varying effects, and their associated standard deviations. 58 | However, \code{mlm_summay()} by default only displays a subset of the 59 | estimated parameters: 60 | 61 | \describe{ 62 | \item{a}{Regression slope of the X -> M relationship.} 63 | \item{b}{Regression slope of the M -> Y relationship.} 64 | \item{cp}{Regression slope of the X -> Y relationship. (Direct effect.)} 65 | \item{me}{Mediated effect (\eqn{a * b + \sigma_{{a_j}{b_j}}}).} 66 | \item{c}{Total effect of X on Y. ( \eqn{cp + me} )} 67 | \item{pme}{Percent mediated effect.} 68 | } 69 | The user may specify \code{pars = NULL} to display all estimated parameters. 70 | Other options include e.g. \code{pars = "tau"} to display the varying 71 | effects' standard deviations. To display all the group-level parameters 72 | (also known as random effects) only, specify \code{pars = "random"}. 73 | With this argument, \code{mlm_summary()} prints the following parameters: 74 | 75 | \describe{ 76 | \item{tau_a}{Standard deviation of subject-level \code{a_j}s.} 77 | \item{tau_b}{Standard deviation of subject-level \code{b_j}s.} 78 | \item{tau_cp}{Standard deviation of subject-level \code{c\'_j}s.} 79 | \item{covab}{Estimated covariance of \code{a_j} and \code{b_j}s.} 80 | \item{corrab}{Estimated correlation of \code{a_j} and \code{b_j}s.} 81 | } 82 | 83 | To learn more about the additional parameters, refer to the Stan code 84 | (\code{cat(get_stancode(fit))}). 85 | } 86 | } 87 | \author{ 88 | Matti Vuorre \email{mv2521@columbia.edu} 89 | } 90 | -------------------------------------------------------------------------------- /src/init.cpp: -------------------------------------------------------------------------------- 1 | // This file is part of RStanArm 2 | // Copyright (C) 2017 Trustees of Columbia University 3 | // 4 | // RStan is free software; you can redistribute it and/or 5 | // modify it under the terms of the GNU General Public License 6 | // as published by the Free Software Foundation; either version 3 7 | // of the License, or (at your option) any later version. 8 | // 9 | // RStan is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License 15 | // along with this program; if not, write to the Free Software 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 | 18 | /* 19 | * To register the functions implemented in C++, see 20 | * http://cran.r-project.org/doc/manuals/R-exts.html#Registering-native-routines 21 | * 22 | * But it seems not to work as it is supposed to be in that 23 | * they are still working if not registered. 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | static const R_CallMethodDef CallEntries[] = { 32 | {NULL, NULL, 0} 33 | }; 34 | 35 | void attribute_visible R_init_bmlm(DllInfo *dll) { 36 | // next line is necessary to avoid a NOTE from R CMD check 37 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 38 | R_useDynamicSymbols(dll, TRUE); // necessary for .onLoad() to work 39 | } 40 | -------------------------------------------------------------------------------- /vignettes/FAQ/FAQ.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Frequently Asked Questions" 3 | author: "Matti Vuorre" 4 | date: "`r Sys.Date()`" 5 | output: 6 | rmarkdown::html_document: 7 | df_print: kable 8 | bibliography: bibliography.bib 9 | --- 10 | 11 | ```{r setup, include = FALSE} 12 | library(knitr) 13 | library(bmlm) 14 | opts_chunk$set(message = F, warning = F) 15 | ``` 16 | 17 | # Subject-specific parameters and effects 18 | 19 | ## How to identify specific subjects' effects? 20 | 21 | When plotting subject specific effects using `mlm_pars_plot()` or `mlm_spaghetti_plot()`, individual subjects' effects or fitted values are represented as points or lines. Sometimes it might be of interest to identify specific subjects with lines / points in the figures. 22 | 23 | One solution to identifying specific subjects in the estimated model is to inspect the numerical values of the estimated parameters: All subject-specific effects' posterior samples are saved in the estimated model object as *u_x*, where *x* is the parameter (one of *a*, *b*, *cp* (c'), *c*, *me*, or *pme*). 24 | 25 | For example, let's focus on the subject-specific *a* parameters. Let's identify the 6 subjects with the lowest values for this parameter. First, we'll fit a model using the included `BLch9` data set: 26 | 27 | ```{r subject-effects-estimate, cache = T} 28 | library(bmlm) 29 | head(BLch9) 30 | fit <- mlm(BLch9, x="x", m="m", y="y", cores = 4, iter = 500) 31 | ``` 32 | 33 | Once the model has been estimated, you can find the *u_a* parameters with `mlm_summary(fit, pars = "u_a")`. However, this would return a row of information for each subject in the data. What we would like to do here is to arrange the estimated *u_a* parameters in an (ascending) order of their magnitudes (calculated from posterior means). To do that, we'll use the `arrange()` function [@wickham_dplyr:_2016] to arrange the data frame returned by `mlm_summary()` 34 | 35 | ```{r subject-effects-2} 36 | library(dplyr) 37 | u_a <- mlm_summary(fit, pars = "u_a") 38 | head( arrange(u_a, Mean) ) 39 | ``` 40 | 41 | Notice that we used the `head()` function to return the first six rows of the resulting data frame. The subject numbers are in square brackets: ID `r substr(arrange(u_a, Mean)[1,1], 5, 6)` has the lowest *u_a* parameter value. To find the individuals with the highest *u_a* values, wrap `Mean` in `desc()` (this arranges the data frame on descending values of Mean). 42 | 43 | ```{r} 44 | head( arrange(u_a, desc(Mean)) ) 45 | ``` 46 | 47 | **Important**: To use this method accurately, please ensure that the subjects' ID numbers in your data are sequential integers from 1 to however many subjects there are in your sample. The underlying Stan MCMC sampler requires sequential integer subject IDs, and if your IDs are something else (letters, non-sequential integers, etc.) `mlm()` will coerce the subject numbers to 1:N sequential integers before fitting the model. 48 | 49 | Thanks to Xiao Hu for asking this question. 50 | 51 | # References 52 | -------------------------------------------------------------------------------- /vignettes/FAQ/bibliography.bib: -------------------------------------------------------------------------------- 1 | 2 | @book{wickham_dplyr:_2016, 3 | title = {dplyr: A Grammar of Data Manipulation}, 4 | url = {http://CRAN.R-project.org/package=dplyr}, 5 | author = {Wickham, Hadley and Francois, Romain}, 6 | date = {2016}, 7 | note = {00009 8 | R package version 0.5.0} 9 | } 10 | 11 | @software{wickham_tidyverse:_2016, 12 | title = {tidyverse: Easily Install and Load 'Tidyverse' Packages}, 13 | url = {https://CRAN.R-project.org/package=tidyverse}, 14 | author = {Wickham, Hadley}, 15 | date = {2016}, 16 | note = {00001 17 | R package version 1.0.0} 18 | } -------------------------------------------------------------------------------- /vignettes/Spaghetti-Plots/Spaghetti-Plots.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "A Note on bmlm's Spaghetti Plots" 3 | author: "Matti Vuorre" 4 | date: "`r Sys.Date()`" 5 | output: 6 | rmarkdown::html_document: 7 | df_print: kable 8 | --- 9 | 10 | ```{r, message = F} 11 | library(tidyverse) 12 | library(bmlm) 13 | ``` 14 | 15 | Consider a mediation model in which Y is binary: 16 | 17 | ```{r, echo=F} 18 | BLch9_biny <- group_by(BLch9, id) %>% 19 | select(id, x, m, y) %>% 20 | mutate(biny = as.integer(y > quantile(y, .5))) %>% 21 | ungroup() 22 | head(BLch9_biny) 23 | ``` 24 | 25 | ```{r, echo=T, results="hide", message=F, cache = T} 26 | fit_biny <- mlm(d = BLch9_biny, 27 | id = "id", 28 | y = "biny", binary_y = TRUE, 29 | cores=4, iter = 500) 30 | ``` 31 | 32 | ```{r} 33 | mlm_summary(fit_biny) %>% 34 | knitr::kable() 35 | ``` 36 | 37 | The `mlm_spaghetti_plot()` function produces figures in which the horizontal axis values of the M-Y graph (right panel, below) correspond to the vertical axis values of the X-M graph (left panel, below): 38 | 39 | ```{r} 40 | pasta <- mlm_spaghetti_plot(fit_biny, BLch9_biny, 41 | id="id", x="x", m="m", y="biny", 42 | binary_y=T) 43 | gridExtra::grid.arrange( 44 | pasta[[1]], 45 | pasta[[2]] + coord_cartesian(ylim = 0:1), 46 | nrow = 1) 47 | ``` 48 | 49 | It is important to note that the fitted values (lines) on the right panel of this figure are therefore based on the variation of the fitted values in the left panel, and not on the observed values of `m`. This behavior usually makes sense and is the default option for `mlm_spaghetti_plot()`. However, sometimes users may want to plot the M-Y relationship's (b path's) fitted values with the observed data values of M on the horizontal axis instead. 50 | 51 | To do this, set `mx = "data"`: 52 | 53 | ```{r} 54 | pasta2 <- mlm_spaghetti_plot(fit_biny, BLch9_biny, 55 | id="id", x="x", m="m", y="biny", 56 | binary_y=T, mx = "data") 57 | gridExtra::grid.arrange( 58 | pasta2[[1]], 59 | pasta2[[2]] + coord_cartesian(ylim = 0:1), 60 | nrow = 1) 61 | ``` 62 | 63 | As result, the horizontal axis variability for the b path figure (right panel) is much greater. However, the two panels are no longer directly comparable (vertical axis in left panel doesn't match horizontal axis of right panel) or interpretable as the model's implied mediated effect path. 64 | 65 | Thanks for reading. Please notify the package's developer on [GitHub](https://github.com/mvuorre/bmlm) if you have further questions or comments. 66 | -------------------------------------------------------------------------------- /vignettes/ms/includes/compare-bmlm-mplus.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/vignettes/ms/includes/compare-bmlm-mplus.rda -------------------------------------------------------------------------------- /vignettes/ms/includes/multilevel-mediation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvuorre/bmlm/97feb82146a8b211aebe45b70042ee02e22f148c/vignettes/ms/includes/multilevel-mediation.pdf --------------------------------------------------------------------------------