Package 'MrMediation'

Title: An R Package for Mendelian Randomization in the mediation setting
Description: A Bayesian framework for Mendelian Randomization in the mediation setting.
Authors: Jingshu Wang [aut], Ethan Lewis [aut], Zixuan Wu [aut, cre]
Maintainer: Zixuan Wu <[email protected]>
License: What license is it under?
Version: 1.0
Built: 2024-10-01 03:52:03 UTC
Source: https://github.com/remlapmot/MrMediation

Help Index


An R Package for Mendelian Randomization in the mediation setting

Description

A Bayesian framework for Mendelian Randomization in the mediation setting.

Details

The DESCRIPTION file:

Package: MrMediation
Type: Package
Title: An R Package for Mendelian Randomization in the mediation setting
Version: 1.0
Date: 2022-10-06
Authors@R: c(person("Jingshu", "Wang", email = "[email protected]", role = c("aut")), person("Ethan", "Lewis", email = "[email protected]", role = c("aut")), person("Zixuan", "Wu", email = "[email protected]", role = c("aut", "cre")))
Description: A Bayesian framework for Mendelian Randomization in the mediation setting.
License: What license is it under?
Imports: Rcpp (>= 1.0.9), coda, doParallel, foreach, ggplot2, stats, magic
LinkingTo: Rcpp, RcppArmadillo
RoxygenNote: 7.2.1
Encoding: UTF-8
Repository: https://mrcieu.r-universe.dev
RemoteUrl: https://github.com/remlapmot/MrMediation
RemoteRef: suggestions
RemoteSha: 779dfcc9d7305da5e859bc55d9f53960d0c2972a
Author: Jingshu Wang [aut], Ethan Lewis [aut], Zixuan Wu [aut, cre]
Maintainer: Zixuan Wu <[email protected]>

Index of help topics:

BayesMediation          BayesMediation
MrMediation-package     An R Package for Mendelian Randomization in the
                        mediation setting
gibbs_sampler           Perform Gibbs sampling for mediation MR without
                        correlation
gibbs_sampler_with_corr
                        Perform Gibbs sampling for mediation MR with
                        correlation
gibbs_wrapper           Gibbs sampler
indirect_effect         Indirrect effedt
summary_gibbs           Produce a summary table of the output from
                        gibbs_wrapper.
total_effect            Total effect
traceplot               Produce traceplot for the specified parameter
zero.centered.em        Perform EM Algorithm on Pleiotropy

~~ An overview of how to use the package, including the most important functions ~~

Author(s)

Jingshu Wang [aut], Ethan Lewis [aut], Zixuan Wu [aut, cre]

Maintainer: Zixuan Wu <[email protected]>

References

~~ Literature or other references for background information ~~

Examples

# simple examples of the most important functions ~~

BayesMediation

Description

Bayes approach for MR Mediation model.

Usage

BayesMediation(
  Gamma_hat,
  Sd_hat,
  init = "Random",
  iter = 6000,
  warmup = 3000,
  second = F,
  inv = FALSE,
  cor = NULL,
  Raw = T,
  total = F,
  indirect = F
)

Arguments

Gamma_hat

The estimated exposure and outcome effects gamma_hat

Sd_hat

The standarded errors of gamma_hat

init

Starting value for gibbs sampler. Either "Random" or "EM"

iter

The number of iterations for gibbs sampler

warmup

The nnumber of warmup periods

second

Whether to run the second stage. Default is False.

inv

When inv = False, we are estiming B in Gamma = (I + B) alpha; inv = True, we are estimating B in Gamma = B Gamma + alpha.

cor

The correlation matrix

Raw

Whether to include the unprocessed raw outputs. Default is F

total

Whether to include the total effects. Default is F

indirect

Whether to include the indirect effects. Default is F.

Value

A list with elements

summary_first
summary_second
total_effect_first

computed by first stage (included if total = T)

total_effect_second

computed by second stage (included if total = T and second = T)

indirect_effect_first

computed by first stage (included if indirect = T)

indirect_effect_second

computed by second stage (included if indirect = T and second = T)

raw_first
raw_second

Perform Gibbs sampling for mediation MR without correlation

Description

Model is that Y = (I+B)A + noise

Usage

gibbs_sampler(
  Y,
  Sd_hat,
  N,
  B,
  sigma,
  sigma1,
  sigma0,
  p,
  A,
  Z,
  alpha_B,
  beta_B,
  alpha_0,
  alpha_1,
  beta_0,
  beta_1,
  a,
  b
)

Arguments

Y

KxP arma::matrix of GWAS marginal associations

Sd_hat

KxP arma::matrix of GWAS std errors

N

number of samples to obtain

B

initial value of B arma::matrix

sigma

Initial value of prior std deviation for values of B

sigma1

K-arma::vector of Initial values of prior slab SDs for each trait's pleiotropy

sigma0

K-arma::vector of Initial values of prior splike SDs for each trait's pleiotropy

p

K-arma::vector of initial values for p, which is slab probability for mixture

A

KxP arma::matrix of initial values for A (ignored)

Z

KxP arma::matrix of initial values for Z (ignored), which is latent varaible indicating if A[i,j] comes from slab

alpha_B

Shape parameter of InvGamma prior for sigma (for B)

beta_B

Scale parameter of InvGamma prior for sigma (for B)

alpha_0

K-arma::vector Shape parameter of InvGamma prior for sigma0 (Spike)

alpha_1

K-arma::vector Shape parameter of InvGamma prior for sigma0 (Slab)

beta_0

K-arma::vector Scale parameter of InvGamma prior for sigma0 (Spike)

beta_1

K-arma::vector Scale parameter of InvGamma prior for sigma0 (Slab)

a

K-arma::vector First parameter of beta prior for p

b

K-arma::vector Second parameter of beta prior for p


Perform Gibbs sampling for mediation MR with correlation

Description

Model is that Y = (I+B)A + noise

Usage

gibbs_sampler_with_corr(
  Y,
  Sd_hat,
  trait_corr,
  N,
  B,
  sigma,
  sigma1,
  sigma0,
  p,
  A,
  Z,
  alpha_B,
  beta_B,
  alpha_0,
  alpha_1,
  beta_0,
  beta_1,
  a,
  b,
  Lambda3,
  Lambda_inv3
)

Arguments

Y

KxP arma::matrix of GWAS marginal associations

Sd_hat

KxP arma::matrix of GWAS std errors

trait_corr

KxK arma::matrix of correlation of noises.

N

number of samples to obtain

B

initial value of B arma::matrix

sigma

Initial value of prior std deviation for values of B

sigma1

K-arma::vector of Initial values of prior slab SDs for each trait's pleiotropy

sigma0

K-arma::vector of Initial values of prior splike SDs for each trait's pleiotropy

p

K-arma::vector of initial values for p, which is slab probability for mixture

A

KxP arma::matrix of initial values for A (ignored)

Z

KxP arma::matrix of initial values for Z (ignored), which is latent varaible indicating if A[i,j] comes from slab

alpha_B

Shape parameter of InvGamma prior for sigma (for B)

beta_B

Scale parameter of InvGamma prior for sigma (for B)

alpha_0

K-arma::vector Shape parameter of InvGamma prior for sigma0 (Spike)

alpha_1

K-arma::vector Shape parameter of InvGamma prior for sigma0 (Slab)

beta_0

K-arma::vector Scale parameter of InvGamma prior for sigma0 (Spike)

beta_1

K-arma::vector Scale parameter of InvGamma prior for sigma0 (Slab)

a

K-arma::vector First parameter of beta prior for p

b

K-arma::vector Second parameter of beta prior for p

Lambda3

Precomputed covariance matrix

Lambda_inv3

Precomputed inverse covariance matrix


Gibbs sampler

Description

Given the observed effect and standard error, run gibbs sampler to draw samples from the posterior density

Usage

gibbs_wrapper(
  N,
  warmup,
  chains = 4,
  Gamma_hat,
  Sd_hat,
  sigma,
  sigma_1,
  sigma_0,
  p,
  parallel = TRUE,
  init = "Random",
  ratio = 10,
  cor_mat = NULL,
  Lambda = NULL,
  Lambda_inv = NULL
)

Arguments

N

Number of total iterations

warmup

Number of warm-up iterations.

chains

Number of Chains.

Gamma_hat

The observed effects

Sd_hat

The standard deviation of Gamma_hat

sigma

The prior mean for sigma

sigma_1

The prior mean for sigma_1

sigma_0

The prior mean for sigma_0

p

The prior mean for p

parallel

Indicating whether to parallelly run all the chains. Default is True.

init

Method of Initialization. Must be one of "EM" or "Random". Default is "Random"

ratio

The ratio of initial sigma1 and sigma0 when the initialization method is "Random". Default is 10.

cor_mat

The correlation matrix for noise. Default is null (no correlation)

Lambda

The block diagonal matrix such that each block is the covariance matrix of the noise for a SNP. Default is NULL (not provided)

Lambda_inv

The block diagonal matrix such that each block is the inverse of covariance matrix of the noise for a SNP. Default is NULL (not provided).

Value

A list of which the elements are outputs from each chain. Each of these outputs is a list with attributes

B
p
sigma1
sigma0
pi
sigma

Indirrect effedt

Description

Estimate the indirrect effect of an exposure on the outcome

Usage

indirect_effect(results, K, warmup = 3000)

Arguments

results

results from gibbs_wrapper

K

number of phenotypes

warump

The length of warmup period. Default is 3000

Value

A matrix of indirect effect and its quantiles.


Produce a summary table of the output from gibbs_wrapper.

Description

Produce a summary table of the output from gibbs_wrapper.

Usage

summary_gibbs(result, pars, K, T = 1000, inv = FALSE)

Arguments

result

The output of gibbs_wrapper or gibbs_wrapper_cor

pars

A vector of parameters to be included in the summary table

K

The number of mediation layers

T

The warmup period. Default is 1000.

inv

When inv is TRUE, the function shows summary statistics for B in Gamma = B Gamma + alpha, otherwise it shows summary statistics for B in Gamma = (I + B) alpha. Default is False

Value

A table where each row corresponds to a parameter. It has columns:

mean
variance
sd
2.5%
50%
97.5%
ESS
R_hat

of convergence


Total effect

Description

Estimate the total effect of an exposure on the outcome

Usage

total_effect(results, K, warmup = 3000)

Arguments

results

results from gibbs_wrapper

K

number of phenotypes

warump

The length of warmup period. Default is 3000

Value

A matrix of total effect and its quantiles.


Produce traceplot for the specified parameter

Description

Produce traceplot for the specified parameter

Usage

traceplot(results, par, ind, chains = -1, ylim = NA, T = 1000)

Arguments

results

The output of gibbs_wrapper.

ind

The index of the parameter. For "B" it is a two-vector. For "sigma1", "sigma0" and "pi" it is a single number. For "sigma" ind is ignored.

chains

The indices of chains to be included in the plot. Default is -1, which uses all the chains.

ylim

The range of y-axis in the final plot

T

The number of warmup period.

pars

A parameter to be ploted. Must be one of "B", "sigma", "sigma1", "sigma0" and "pi".

Value

A traceplot of a scalar parameter after warmup-period


Perform EM Algorithm on Pleiotropy

Description

Fit a two-component zero-mean Gaussian mixture on a sequence of observed values

Usage

zero.centered.em(
  values,
  std.errors,
  initial_slab_prob = 0.05,
  initial_sigma1_squared = 0.1,
  initial_sigma2_squared = 1e-04,
  iters = 100,
  optimizer = "Brent",
  show_outputs = FALSE
)

Arguments

values

A vector of observations.

initial_slab_prob

The initial value for the proportion of slab (the Gaussian component with higher variance). Default is .05.

initial_sigma1_squared

The initial value for the variance of the slab component. Default is .1.

initial_sigma2_squared

The initial value for the variance of the spike component. Default is .0001

iters

The number of iterations of EM algorithm. Default is 100

optimizer

The method used in Maximization Step. Must be one of "Brent", "Nelder-Mead" and "L-BFGS-B". Default is "Brent".

show_outputs

Whether to included a plot of the fitted density and the smoothed data density. Default is FALSE.

std.error

The standard error of noise of observations.

Value

A list of elements

S1
S2
Pi