Learn R Programming

multimedia

multimedia is an R package for multimodal mediation analysis of microbiome data. It streamlines data curation, mediation and outcome model specification, and statistical inference for direct and indirect effects. By defining causal relationships across data modalities, it can support principled data integration. You can read more about the package in our preprint:

H. Jiang, X. Miao, M. W. Thairu, M. Beebe, D. W. Grupe, R. J. Davidson, J. Handelsman, and K. Sankaran (2024). multimedia: Multimodal Mediation Analysis of Microbiome Data.

The preprint describes the scientific context and interpretation for two of the vignettes in this package. One gives a multi-omics analysis of IBD, and the other describes how to simultaneously model 16S profiles and survey responses in a mindfulness intervention study.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("krisrs1128/multimedia")

Example

Here is a simple example of estimating direct and indirect effects. The data are randomly generated (no real effects), but we imagine that the ASV columns mediate the relationship between treatment and PHQ. This is mimics the possibility that the microbiome (ASV = Amplicon Sequence Variant) mediates the relationship between a treatment and depression (PHQ = Patient Health Questionnaire). You can find more details in the random.Rmd vignette.

The original data here are a SummarizedExperiment. The package can also take phyloseq objects and data.frames.

library(multimedia)
demo_joy()

Next, we specify which columns are the treatment, mediators, and outcomes. Notice that we can use tidyselect syntax to match multiple columns.

exper <- mediation_data(demo_joy(), "PHQ", "treatment", starts_with("ASV"))
exper

Next, we fit all mediation analysis components and estimate effects. By default, the package uses linear models – see the vignettes for examples using sparse regression, random forests, and bayesian hierarchical models instead.

model <- multimedia(exper) |>
    estimate(exper)
model

In any mediation analysis, there are several types of effects that could be interesting, each corresponding to different ways of traveling from the treatment to the outcome in the mediation analysis causal graph. In the block below, direct_effect captures treatment effects that bypass the microbiome; indirect_effect are effects that are mediated by ASV relative abundances. Since this example uses a linear model, the effects are identical for the two indirect settings.

direct_effect(model, exper)
indirect_overall(model, exper)

The package also includes helpers to visualize and perform inference on these effects. For example,

boot <- bootstrap(model, exper, c(direct = direct_effect))

library(ggplot2)
ggplot(boot$direct) +
    geom_histogram(aes(direct_effect), bins = 20) +
    scale_y_continuous(expand = c(0, 0)) +
    theme_classic() +
    labs(x = "Direct Effect", y = "Frequency", title = "Bootstrap Distribution")

If we want to use a different type of model, we can just modify the original multimedia specification. Below we use a sparse regression model, which correctly recovers that the direct effects are 0.

multimedia(exper, glmnet_model(lambda = .1)) |>
    estimate(exper) |>
    direct_effect()

Help

We welcome questions and comments about the package either through github or email.

sessionInfo()

Copy Link

Version

Install

install.packages('multimedia')

Monthly Downloads

179

Version

0.2.0

License

CC0

Issues

Pull Requests

Stars

Forks

Maintainer

Kris Sankaran

Last Published

September 18th, 2024

Functions in multimedia (0.2.0)

estimate

Estimate a Mediation Model
indirect_overall

Overall Indirect Effect
null_contrast

Compare Effects from Experimental vs. Null Mediation Data
mediators,mediation_data-method

Access to @mediators in Mediation Data
plot_mediators

Visualize Indirect Effects
parallelize

Parallelize Estimation across Responses
nrow,mediation_data-method

How many samples in the mediation dataset?
mediators,multimedia-method

Names of Mediators in a Multimedia Object
mediators<-,mediation_data-method

Set the Mediators in a Mediation Data Object
mediators<-

Set Mediators
lnm_model

Logistic Normal Multinomial Model
lnm_sampler

Sample from the Logistic Normal Multinomial
outcomes,multimedia-method

Names of Outcomes in a Multimedia Object
outcomes<-,mediation_data-method

Set the Outcomes in a Mediation Data Object
lm_sampler

Sample a Linear Model
lm_model

Linear Model across Responses
pretreatments,mediation_data-method

Pretreatments in a Mediation Data Object
mediators

Access Class-Specific Mediators
mindfulness

Mindfulness Dataset
mediation_data

mediation_data Constructor
nullify

Nullify Active Edges
indirect_pathwise

Indirect Effects via Single Mediation Paths
rf_model

Random Forest Model
treatments,mediation_data-method

Treatments in a Mediation Data Object
treatments<-

Set Treatments This is an setter method for treatments in an S4 object, usually of class mediation_data.
predict_across

Predict a Subset of Responses
treatment_profile-class

Define a Treatment Profile
outcome_model

Access the Outcome Model in a Multimedia Object
pretreatments<-,mediation_data-method

Set the Pretreatments in a Mediation Data Object
n_mediators

Number of Mediators in a Multimedia Object
sub_formula

Helper to Modify Formulas
n_outcomes

Number of Outcomes in a Multimedia Object
rf_sampler

Sample from a Random Forest Model
pretreatments<-

Set Pretreatments This is an setter method for pretreatments in an S4 object, usually of class mediation_data.
[,mediation_data,ANY,ANY,ANY-method

Subset a mediation dataset
mediation_models

Accessor for Outcome Models
treatments,multimedia-method

Names of Treatments in a Multimedia Object
outcomes,mediation_data-method

Outcomes Data in a Mediation Data Object
model-class

Representation of an Outcome or Mediation Model
multimedia

multimedia Constructor
outcome_models

Accessor for Outcome Models
sensitivity_pathwise

Sensitivity Analysis for Pathwise Indirect Effects
sensitivity

Sensitivity Analysis for Overall Indirect Effect
sensitivity_perturb

Sensitivity to User-Specified Perturbations
sample,multimedia-method

Sample New Mediator/Outcome Data
treatments

Access Treatments
plot_sensitivity

Generic Sensitivity Plot
treatments<-,mediation_data-method

Set the Treatments in a Mediation Data Object
predict,multimedia-method

Predictions from a Multimedia Class
retrieve_names

Variables in a Multimedia Object
outcomes

Access Outcomes
spline_fun

Generate Random Spline
outcomes<-

Set Outcomes This is an setter method for outcomes in an S4 object, usually of class mediation_data.
setup_profile

Define a treatment_profile object
pretreatments

Access Pretreatments
ansi_aware_handler

Pretty Printing
bind_mediation

Convert mediation_data to a single data.frame
contrast_samples

Difference between Samples at Contrasting Profiles
demo_joy

A Demo Dataset (Random)
demo_spline

A Demo Dataset (Spline)
direct_effect

Direct Effects from Estimated Model
effect_summary

Average Effects across j
bootstrap

Bootstrap Distribution for Estimators
glmnet_sampler

Sample from a 'Glmnet' Package Model
exper_df

Convert a Summarized Experiment to a data.frame
contrast_predictions

Estimate the Difference between Profiles
edges,multimedia-method

Access Mediation Model DAG
glmnet_model

Regularized 'Glmnet' Model across Responses
brms_model

Bayesian Regression Model across Responses
edges

Graphical Structure for Mediation Objects
fdr_summary

Calibration using Synthetic Nulls
brms_sampler

Sample from a Bayesian Regression Model
estimator,model-method

Accessor for Estimators
estimator

Accessor for Model Estimators