PartialMed | R Documentation |
Estimates a partial mediation model using series of Gibbs samplers
PartialMed(Data, Prior, R)
Data |
list(X, M, Y) |
Prior |
list(A_M,A_Y) |
R |
number of MCMC iterations, default = 10000 |
M = \beta_{0M} + X\beta_1 + U_M
(eq.1)
Y = \beta_{0Y} + M\beta_2 + X\beta_3 + U_Y
(eq.2)
\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.
Data = list(X, M, Y)
treatment variable vector
mediator vector
dependent variable vector
Prior = list(A_M,A_Y)
[optional]vector of coefficients' prior variances of eq.1, default = rep(100,2)
vector of coefficients' prior variances of eq.2, default = c(100,100,1)
a list containing
matrix of eq.1 coefficients' posterior draws
matrix of eq.2 coefficients' posterior draws
vector of eq.1 error variance posterior draws
vector of eq.2 error variance posterior draws
vector of means indexing MCMC draws of the direct effect (used in BFSD to compute Bayes factor)
vector of variances indexing MCMC draws of the direct effect (used in BFSD to compute Bayes factor)
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.