#### Approach 1 - Estimate ONE BIG (with all effect sizes) Random-Effects Pooled Correlation Matrix. #### Use the BIG Pooled correlation matrices and #### estimate models 1, 2, and 3. #### All models have all variables(by freely correlating the variables with each other). #### Adapted from https://openmx.ssri.psu.edu/sites/default/files/Test.pdf ### Step 1: Import Libraries library("OpenMx") library("lavaan") library("metaSEM") library("semPlot") ### Step 2: Load Data. data3 <- read.csv("/Users/srikanthparameswaran/Desktop/TSSEM Project 2/Approach 1/Approach 1 Data.csv") ### Step 3: Setup the correlation matrices nvar <- 9 varnames <- c("X","Y","A","B","C","M","D","E","F") labels <- list(varnames,varnames) cordat <- list() for (i in 1:nrow(data3)){ cordat[[i]] <- vec2symMat(as.matrix(data3[i,2:37]),diag = FALSE) dimnames(cordat[[i]]) <- labels} data3\$data<-cordat ### Step 4: Put NA on diagonal if variable is missing for (i in 1:length(data3\$data)){ for (j in 1:nrow(data3\$data[[i]])){ if (sum(is.na(data3\$data[[i]][j,]))==nvar-1) {data3\$data[[i]][j,j] <- NA} }} ### Step 5: Put NA on diagonal for variable with least correlations for (i in 1:length(data3\$data)){ for (j in 1:nrow(data3\$data[[i]])){ for (k in 1:nvar){ if (is.na(data3\$data[[i]][j,k])==TRUE &is.na(data3\$data[[i]][j,j])!=TRUE &is.na(data3\$data[[i]][k,k])!=TRUE){ if(sum(is.na(data3\$data[[i]])[j,])>sum(is.na(data3\$data[[i]])[k,])) {data3\$data[[i]][k,k] = NA} if(sum(is.na(data3\$data[[i]])[j,])<=sum(is.na(data3\$data[[i]])[k,])) {data3\$data[[i]][j,j] = NA} }}}} # Display processed data data3\$data # Assess missingness pattern.na(data3\$data, show.na = FALSE) pattern.n(data3\$data, data3\$Corr_Sample) ### Step 6: Check Positive-Definiteness. Everything should be TRUE. is.pd(data3\$data) length(is.pd(data3\$data)) ### Step 7: Random-effects Stage 1 estimation random_stage1<- tssem1(data3\$data, data3\$Corr_Sample, method="REM", RE.type="Diag") summary(random_stage1) vec2symMat(coef(random_stage1, select="fixed"), diag = FALSE) ### Step 8: Creating variables for Random-effects Stage 2 estimation pooled <- vec2symMat(coef(random_stage1, select="fixed"), diag = FALSE) dimnames(pooled)<- list(varnames,varnames) pooled aCov <- vcov(random_stage1, select="fixed") aCov n2 <- random_stage1\$total.n n2 ### Step 9: Model 1 ## Step 9a: Model setup in Lavaan. Converting Lavaan to RAM model1 <- 'X ~ 0*Y + A2X*A + 0*M + B2X*B + 0*C + D2X*D + E2X*E + F2X*F' plot(model1, col="yellow") RAM1 <- lavaan2RAM(model1, obs.variables=c("X","Y","A","B","C","M","D","E","F")) RAM1 ## Step 9b: All variances except the DVs were set to 1. RAM1\$S RAM1\$S[2,2] <- 1 RAM1\$S[3,3] <- 1 RAM1\$S[4,4] <- 1 RAM1\$S[5,5] <- 1 RAM1\$S[6,6] <- 1 RAM1\$S[7,7] <- 1 RAM1\$S[8,8] <- 1 RAM1\$S[9,9] <- 1 ## Step 9c: Correlating Y, C, M with the dependent variable X RAM1\$S[1,2] <- "0*XWITHY" RAM1\$S[2,1] <- "0*XWITHY" RAM1\$S[1,5] <- "0*XWITHC" RAM1\$S[5,1] <- "0*XWITHC" RAM1\$S[1,6] <- "0*XWITHM" RAM1\$S[6,1] <- "0*XWITHM" RAM1\$S RAM1 ## Step 9d: Model estimation fit1 <- wls(Cov=pooled, aCov=aCov, n= n2, RAM=RAM1, diag.constraints = TRUE) summary(fit1) my.plot1 <- meta2semPlot(fit1) semPaths(my.plot1, whatLabels="est", layout = "tree2", sizeMan=12, edge.label.cex=2, color="yellow", edge.color = "black", weighted=FALSE) (Smatrix <- diag(mxEval(Smatrix, fit1\$mx.fit))) 1 - Smatrix ### Step 10: Model 2 ## Step 10a: Model setup in Lavaan. Converting Lavaan to RAM model2 <- 'X ~ 0*Y + A2X*A + M2X*M + B2X*B + C2X*C + D2X*D + E2X*E + F2X*F M ~ A2M*A + B2M*B' plot(model2, col="yellow") RAM2 <- lavaan2RAM(model2, obs.variables=c("X","Y","A","B","C","M","D","E","F")) RAM2 ## Step 10b: All variances except the DVs were set to 1. RAM2\$S RAM2\$S[2,2] <- 1 RAM2\$S[3,3] <- 1 RAM2\$S[4,4] <- 1 RAM2\$S[5,5] <- 1 RAM2\$S[7,7] <- 1 RAM2\$S[8,8] <- 1 RAM2\$S[9,9] <- 1 ## Step 10c: Correlating Y with the dependent variable X RAM2\$S[1,2] <- "0*XWITHY" RAM2\$S[2,1] <- "0*XWITHY" ## Step 18c: Correlating Y with the mediator variable M RAM2\$S[2,6] <- "0*MWITHY" RAM2\$S[6,2] <- "0*MWITHY" RAM2\$S RAM2 ## Step 10d: Model estimation fit2 <- wls(Cov=pooled, aCov=aCov, n= n2, RAM=RAM2, diag.constraints = TRUE) summary(fit2) fit2 <-rerun(fit2) summary(fit2) my.plot2 <- meta2semPlot(fit2) semPaths(my.plot2, whatLabels="est", layout = "tree2", sizeMan=12, edge.label.cex=2, color="yellow", edge.color = "black", weighted=FALSE) (Smatrix <- diag(mxEval(Smatrix, fit2\$mx.fit))) 1 - Smatrix ### Step 11: Model 3 ## Step 11a: Model setup in Lavaan. Converting Lavaan to RAM model3 <- 'X ~ A2X*A + M2X*M + B2X*B + C2X*C + D2X*D + E2X*E + F2X*F M ~ A2M*A + B2M*B Y ~ A2Y*A + M2Y*M + B2Y*B + C2Y*C + D2Y*D + E2Y*E + F2Y*F + X2Y*X' plot(model3, col="yellow") RAM3 <- lavaan2RAM(model3, obs.variables=c("X","Y","A","B","C","M","D","E","F")) RAM3 ## Step 11b: All variances except the DVs were set to 1. RAM3\$S RAM3\$S[3,3] <- 1 RAM3\$S[4,4] <- 1 RAM3\$S[5,5] <- 1 RAM3\$S[7,7] <- 1 RAM3\$S[8,8] <- 1 RAM3\$S[9,9] <- 1 RAM3\$S RAM3 ## Step 11c: Not applicable. ## Step 11d: Model estimation fit3 <- wls(Cov=pooled, aCov=aCov, n= n2, RAM=RAM3, diag.constraints = TRUE) summary(fit3) fit3 <-rerun(fit3) summary(fit3) my.plot3 <- meta2semPlot(fit3) semPaths(my.plot3, whatLabels="est", layout = "tree2", sizeMan=12, edge.label.cex=2, color="yellow", edge.color = "black", weighted=FALSE) (Smatrix <- diag(mxEval(Smatrix, fit3\$mx.fit))) 1 - Smatrix