# Example with fictitious data
# Set the correlation matrix
mat.cor <- matrix(0, 4, 4)
mat.cor[lower.tri(mat.cor)] <- c(0.3, 0.5, 0.7, 0.8, 0.4, 0.8)
mat.cor <- mat.cor+t(mat.cor)
diag(mat.cor) <- 1
dimnames(mat.cor) <- list(c("x1","x2","y","z"), c("x1","x2","y","z"))
# generate data from multivariate normal distribution
library(mvtnorm)
data.all <- rmvnorm(n=100, mean=rep(0,4), sigma=mat.cor)
dimnames(data.all) <- list(1:100, c("x1","x2","y","z"))
# reproduce statistical matching framework
data.A <- data.all[1:50, 1:3] #z deleted
data.B <- data.all[51:100, c(1:2,4)] #y deleted
# ML estimation method under CIA ((rho_YZ|X=0));
# only parameter estimates (micro=FALSE)
mtc.1 <- mixed.mtc(data.rec=data.A, data.don=data.B,
match.vars=c("x1","x2"), y.rec="y", z.don="z")
# estimated vs. true correlation matrix
mtc.1$cor - mat.cor
# ML estimation method with partial correlation coefficient
# set equal to 0.5 (rho_YZ|X=0.5)
# only parameter estimates (micro=FALSE)
mtc.2 <- mixed.mtc(data.rec=data.A, data.don=data.B,
match.vars=c("x1","x2"), y.rec="y", z.don="z", rho.yz=0.5)
# estimated vs. true correlation matrix
mtc.2$cor - mat.cor
# ML estimation method with partial correlation coefficient
# set equal to 0.5 (rho_YZ|X=0.5)
# with imputation step (micro=TRUE)
mtc.3 <- mixed.mtc(data.rec=data.A, data.don=data.B,
match.vars=c("x1","x2"), y.rec="y", z.don="z", rho.yz=0.5,
micro=TRUE, constr.alg="lpSolve")
# estimated vs. true correlation matrix
mtc.3$cor - mat.cor
# first rows of data.rec filled in with z
head(mtc.3$filled.rec)
# Moriarity and Scheuren estimation method under CIA;
# only with parameter estimates (micro=FALSE)
mtc.4 <- mixed.mtc(data.rec=data.A, data.don=data.B,
match.vars=c("x1","x2"), y.rec="y", z.don="z", method="MS")
# estimated vs. true correlation matrix
mtc.4$cor - mat.cor
# Moriarity and Scheuren estimation method
# with correlation coefficient set equal to 0.2 (rho_YZ=0.2)
# only parameter estimates (micro=FALSE)
mtc.5 <- mixed.mtc(data.rec=data.A, data.don=data.B,
match.vars=c("x1","x2"), y.rec="y", z.don="z",
method="MS", rho.yz=0.2)
# the starting value of rho.yz and the value used
# in computations
mtc.5$rho.yz
# estimated vs. true correlation matrix
mtc.5$cor - mat.cor
# Moriarity and Scheuren estimation method
# with correlation coefficient set equal to 0.6 (rho_YZ=0.6)
# with imputation step (micro=TRUE)
mtc.6 <- mixed.mtc(data.rec=data.A, data.don=data.B,
match.vars=c("x1","x2"), y.rec="y", z.don="z", rho.yz=0.6,
method="MS", micro=TRUE, constr.alg="lpSolve")
# estimated vs. true correlation matrix
mtc.6$cor - mat.cor
# first rows of data.rec filled in with z imputed values
head(mtc.6$filled.rec)Run the code above in your browser using DataLab