PartialMed: Gibbs Sampler for Partial Mediation Model

View source: R/PartialMed.R

PartialMedR Documentation

Gibbs Sampler for Partial Mediation Model

Description

Estimates a partial mediation model using series of Gibbs samplers

Usage

PartialMed(Data, Prior, R)

Arguments

Data

list(X, M, Y)

Prior

list(A_M,A_Y)

R

number of MCMC iterations, default = 10000

Details

Model

M = \beta_{0M} + X\beta_1 + U_M (eq.1)
Y = \beta_{0Y} + M\beta_2 + X\beta_3 + U_Y (eq.2)

Prior specification:

\beta_{0M} \sim N(0,100), \beta_{0Y} \sim N(0,100)
\beta_1 \sim N(0,100), \beta_2 \sim N(0,100), \beta_3 \sim N(0,1)
\sigma^2_M \sim Inv\chi^2(\nu,\nu S),\sigma^2_Y \sim Inv\chi^2(\nu,\nu S), where \nu=1 and S=3.

Argument Details

Data = list(X, M, Y)

X(N x 1)

treatment variable vector

M(N x 1)

mediator vector

Y(N x 1)

dependent variable vector

Prior = list(A_M,A_Y) [optional]

A_M

vector of coefficients' prior variances of eq.1, default = rep(100,2)

A_Y

vector of coefficients' prior variances of eq.2, default = c(100,100,1)

Value

a list containing

beta_M(R X 2)

matrix of eq.1 coefficients' posterior draws

beta_Y(R X 3)

matrix of eq.2 coefficients' posterior draws

ssq_M(R X 1)

vector of eq.1 error variance posterior draws

ssq_Y(R X 1)

vector of eq.2 error variance posterior draws

mu_draw

vector of means indexing MCMC draws of the direct effect (used in BFSD to compute Bayes factor)

var_draw

vector of variances indexing MCMC draws of the direct effect (used in BFSD to compute Bayes factor)

Examples

simPartialMed = function(beta_M,beta_Y, sigma_M, sigma_Y,N,X) {
  eps_M = rnorm(N)*sigma_M # generate errors for M (independent)
  eps_Y = rnorm(N)*sigma_Y # generate errors for Y (independent)
  M = beta_M[1] + beta_M[2] * X + eps_M # generate latent mediator M
  Y = beta_Y[1] + beta_Y[2] * M + beta_Y[3] * X + eps_Y # generate dependent variable
  list(X = X, M = M, Y = Y)
}

# Set up data generating parameters
N = 1000 # number of observations
sigma_M = .2^.5 # error std M
sigma_Y = .2^.5 # error std Y
beta_M = c(1, .3) # beta_0M and beta_1
beta_Y = c(1, .5, 0) # beta_0Y, beta_2, beta_3
X = rnorm(N,mean = 1,sd = 1)# generate random X
# Generate data based on parameters
Data = simPartialMed(beta_M,beta_Y,sigma_M,sigma_Y,N,X)

#Estimation
A_M = c(100,100); #prior variance for beta_0M, beta_1
A_Y = c(100,100,1) #prior variance for beta_0Y, beta_2, beta_3
R = 2000
out = PartialMed(Data=Data, Prior = list(A_M=A_M, A_Y=A_Y), R = R)

arashl1364/BFMediate documentation built on Oct. 11, 2023, 5:54 p.m.