Learn R Programming

macc (version 1.0.1)

sim.data.single: Generate single-level simulation data

Description

This function generates a single-level dataset with given parameters.

Usage

sim.data.single(Z, Theta, Sigma)

Arguments

Z

a vector of treatment/exposure assignment.

Theta

a 2 by 2 matrix, containing the model coefficients.

Sigma

a 2 by 2 matrix, is the covariance matrix of the model errors.

Value

The function returns a dataframe with variables Z, M and R.

Details

The single level mediation model is $$M=ZA+E_1,$$ $$R=ZC+MB+E_2.$$ Theta[1,1] = A, Theta[1,2] = C and Theta[2,2] = B; Sigma is the covariance matrix of \((E_1,E_2)\).

References

Zhao, Y., & Luo, X. (2014). Estimating Mediation Effects under Correlated Errors with an Application to fMRI. arXiv preprint arXiv:1410.7217.

Examples

Run this code
# NOT RUN {
  ###################################################
  # Generate a single-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 trials
  n<-100

  # generate a treatment assignment vector
  set.seed(100)
  Z<-matrix(rbinom(n,size=1,0.5),n,1)

  # Data
  set.seed(5000)
  data.single<-sim.data.single(Z,Theta,Sigma)
  ###################################################
# }

Run the code above in your browser using DataLab