View source: R/MeasurementMCat.R
MeasurementMCat | R Documentation |
Estimates a partial mediation model with multiple categorical indicator for the mediator and with an observed dependent variable using a mixture of Metropolis-Hastings and Gibbs sampling
MeasurementMCat(Data,Prior,R)
Data |
list(X, m_star, 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)
m^*_1 = M + U_{m^*_1}
\tilde{m}_1 = OrdProbit(m^*_1 ,C_{m_1})
m^*_2 = \lambda_{20} + M + U_{m^*_2}
\tilde{m}_2 = OrdProbit(m^*_2 ,C_{m_2})
...
m^*_K = \lambda_{K0} + M + U_{m^*_K}
\tilde{m}_K = OrdProbit(m^*_K ,C_{m_K})
\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_Y
\sim
Inv\chi^2(\nu,\nu S)
, where \nu=1
and S=3.
\lambda_{20},...,\lambda_{K0}
\sim
N(0,100)
\sigma^2_{m*_1}, ..., \sigma^2_{m*_K}
\sim
Inv\chi^2(\nu,\nu S)
C*_{m_1}, ..., C*_{m_K}
\sim
N(0,I)
Note: C*_{m_1}, ..., C*_{m_K}
are untransformed
cutoffs, which are then exponentially transformed to impose sign and order constraint on them. Subjective prior values for the error variances are \nu=1
, S=3.
Data = list(X, m_star, Y)
treatment variable vector
mediator indicators' matrix
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)
matrix of eq.1 coefficients' draws
matrix of eq.2 coefficients' draws
array of indicator coefficients' draws. Each slice is one draw, where rows represent the indicator equation and columns the coefficients. All Slope coefficients as well as intercept of the first equation are fixed to 1 and 0 respectively.
Matrix of indicator equations' coefficients' error variance draws
vector of eq.2 error variance draws
array of discretized mediator indicators' cutoff values.
Matrix of the augmented latent mediator
vector of means indexing MCMC draws of the direct effect (used in BFSD to compute Bayes factor)
vector of variance indexing MCMC draws of the direct effect (used in BFSD to compute Bayes factor)
set.seed(60)
SimMeasurementMCat = function(X, beta_M, cutoff_M, beta_Y, Sigma_Y, M_ind, lambda, ssq_m_star){
nobs = dim(X)[1]
m_star = m_tilde = matrix(double(nobs*M_ind), ncol = M_ind)
M = beta_M[1] + beta_M[2] * X + rnorm(nobs) #cbind(rep(1,nobs),X)%*%beta_M + rnorm(nobs)
for(i in 1: M_ind){
m_star[,i] = lambda[i] + M + sqrt(ssq_m_star[i])*rnorm(nobs);
m_tilde[,i] = cut(m_star[,i], br = cutoff_M[i,], right=TRUE, include.lowest = TRUE, labels = FALSE)
}
Y = beta_Y[1] + beta_Y[2] * M + beta_Y[3] * X + rnorm(nobs)*Sigma_Y
#cbind(rep(1,nobs),cbind(M,X))%*%beta_Y + rnorm(nobs)
return(list(Y = Y, M = M, m_tilde = m_tilde, X = X,
beta_M = beta_M, beta_Y = beta_Y,
lambda = lambda, ssq_m_star = ssq_m_star, m_star = m_star, cutoff_M = cutoff_M,
k_M=dim(cutoff_M)[2]-1, M_ind=M_ind))
}
M_ind = 2
Mcut = 8
nobs= 1000
X=as.matrix(runif(nobs,min=0, max=1))
beta_M = c(.5,1)
beta_Y = c(1, 2, 0)
Sigma_Y = 1^.5
ssq_m_star = c(.5,.7)
lambda = c(0,-.5) #the intercepts for the latent M indicators w. measurement
#error (first intercept should always be 0)
cutoff_M = matrix(c(-100, 0, 1.6, 2, 2.2, 3.3, 6, 100,
-100, 0, 1, 2, 3, 4, 5, 100) ,ncol= Mcut, byrow = TRUE)
DataMCat = SimMeasurementMCat(X, beta_M, cutoff_M, beta_Y, Sigma_Y, M_ind, lambda, ssq_m_star)
#estimation
Data = list(X=cbind(rep(1,length(DataMCat$X)),DataMCat$X), m_tilde=as.matrix(DataMCat$m_tilde),
Y= as.matrix(DataMCat$Y) ,k=Mcut-1, M_ind=dim(DataMCat$m_tilde)[2])
out = MeasurementMCat(Data=Data, R=10000)
#results
colMeans(out$beta_M)
colMeans(out$beta_Y)
apply(out$cutoff_M,c(1,2),FUN = mean)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.