sim.data.multi {macc} | R Documentation |
Generate two/three-level simulation data
Description
This function generates a two/three-level dataset with given parameters.
Usage
sim.data.multi(Z.list, N, K = 1, Theta, Sigma,
Psi = diag(rep(1, 3)), Lambda = diag(rep(1, 3)))
Arguments
Z.list |
a list of data. When |
N |
an integer, indicates the number of subjects. |
K |
an integer, indicates the number of sessions of each subject. |
Theta |
a 2 by 2 matrix, containing the population level model coefficients. |
Sigma |
a 2 by 2 matrix, is the covariance matrix of the model errors in the single-level model. |
Psi |
the covariance matrix of the random effects in the mixed effects model of the model coefficients. This is used only when |
Lambda |
the covariance matrix of the model errors in the mixed effects model if |
Details
When K > 1
(three-level data), for the trials in each session of each subject, the single level mediation model is
where ,
,
,
, and
are vectors of length
.
Sigma
is the covariance matrix of (for simplicity,
Sigma
is the same across sessions and subjects). For coefficients ,
and
, we assume a mixed effects model. The random effects are from a trivariate normal distribution with mean zero and covariance
Psi
; and the model errors are from a trivariate normal distribution with mean zero and covariance Lambda
. For the fixed effects ,
and
, the values are specified in
Theta
with Theta[1,1] = A
, Theta[1,2] = C
and Theta[2,2] = B
.
When K = 1
(two-level data), the single-level model coefficients ,
and
are assumed to follow a trivariate linear regression model, where the population level coefficients are specified in
Theta
and the model errors are from a trivariate normal distribution with mean zero and covariance Lambda
.
See Section 5.2 of the reference for details.
Value
data |
a list of data. When |
A |
the value of |
B |
the value of |
C |
the value of |
type |
a character indicates the type of the dataset. When |
Author(s)
Yi Zhao, Brown University, yi_zhao@brown.edu; Xi Luo, Brown University, xi.rossi.luo@gmail.com
References
Zhao, Y., & Luo, X. (2014). Estimating Mediation Effects under Correlated Errors with an Application to fMRI. arXiv preprint arXiv:1410.7217.
Examples
###################################################
# Generate a two-level dataset
# covariance matrix of errors
delta<-0.5
Sigma<-matrix(c(1,2*delta,2*delta,4),2,2)
# model coefficients
A0<-0.5
B0<--1
C0<-0.5
Theta<-matrix(c(A0,0,C0,B0),2,2)
# number of subjects, and trials of each
set.seed(2000)
N<-50
K<-1
n<-matrix(NA,N,K)
for(i in 1:N)
{
n0<-rpois(1,100)
n[i,]<-rpois(K,n0)
}
# treatment assignment list
set.seed(100000)
Z.list<-list()
for(i in 1:N)
{
Z.list[[i]]<-rbinom(n[i,1],size=1,prob=0.5)
}
# Lambda
rho.AB=rho.AC=rho.BC<-0
Lambda<-matrix(0,3,3)
lambda2.alpha=lambda2.beta=lambda2.gamma<-0.5
Lambda[1,2]=Lambda[2,1]<-rho.AB*sqrt(lambda2.alpha*lambda2.beta)
Lambda[1,3]=Lambda[3,1]<-rho.AC*sqrt(lambda2.alpha*lambda2.gamma)
Lambda[2,3]=Lambda[3,2]<-rho.BC*sqrt(lambda2.beta*lambda2.gamma)
diag(Lambda)<-c(lambda2.alpha,lambda2.beta,lambda2.gamma)
# Data
set.seed(5000)
re.dat<-sim.data.multi(Z.list=Z.list,N=N,K=K,Theta=Theta,Sigma=Sigma,Lambda=Lambda)
data2<-re.dat$data
###################################################
###################################################
# Generate a three-level dataset
# covariance matrix of errors
delta<-0.5
Sigma<-matrix(c(1,2*delta,2*delta,4),2,2)
# model coefficients
A0<-0.5
B0<--1
C0<-0.5
Theta<-matrix(c(A0,0,C0,B0),2,2)
# number of subjects, and trials of each
set.seed(2000)
N<-50
K<-4
n<-matrix(NA,N,K)
for(i in 1:N)
{
n0<-rpois(1,100)
n[i,]<-rpois(K,n0)
}
# treatment assignment list
set.seed(100000)
Z.list<-list()
for(i in 1:N)
{
Z.list[[i]]<-list()
for(j in 1:K)
{
Z.list[[i]][[j]]<-rbinom(n[i,j],size=1,prob=0.5)
}
}
# Psi and Lambda
sigma2.alpha=sigma2.beta=sigma2.gamma<-0.5
theta2.alpha=theta2.beta=theta2.gamma<-0.5
Psi<-diag(c(sigma2.alpha,sigma2.beta,sigma2.gamma))
Lambda<-diag(c(theta2.alpha,theta2.beta,theta2.gamma))
# Data
set.seed(5000)
re.dat<-sim.data.multi(Z.list,N,K,Theta,Sigma,Psi,Lambda)
data3<-re.dat$data
###################################################