#### Approach 3 - MODEL 1 #### Estimate a Random-Effects pooled correlation matrix with with those variables present in the research model #### Fit the Random-Effects stage 2 model using this pooled correlation matrix ### Step 1: Import Libraries library("OpenMx") library("lavaan") library("metaSEM") library("semPlot") ### Step 2: Load Data. ## DATA FILE FOR MODEL 1 data3 <- read.csv("/Users/srikanthparameswaran/Desktop/TSSEM Project 2/Approach 3/Model 1 Data.csv") ### Step 3: Setup the correlation matrices nvar <- 6 varnames <- c("X","A","B","D","E","F") labels <- list(varnames,varnames) cordat <- list() for (i in 1:nrow(data3)){ cordat[[i]] <- vec2symMat(as.matrix(data3[i,2:16]),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. 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) pooled <- vec2symMat(coef(random_stage1, select="fixed"), diag = FALSE) dimnames(pooled)<- list(varnames,varnames) pooled ### Step 8: Random Effects Stage 2 ### Step 8a: Model setup in Lavaan. Converting Lavaan to RAM model1 <- 'X ~ A2X*A + B2X*B + D2X*D + E2X*E + F2X*F' plot(model1, col="yellow") RAM1 <- lavaan2RAM(model1, obs.variables=c("X","A","B","D","E","F")) RAM1 RAM1\$S ## Step 8b: All variances except the DVs were set to 1. 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 RAM1 ### Step 8c: Random Effects Stage 2 Model estimation random_stage2 <- tssem2(random_stage1, Amatrix=RAM1\$A, Smatrix=RAM1\$S ) summary(random_stage2) my.plot1 <- meta2semPlot(random_stage2) semPaths(my.plot1, whatLabels="est", layout = "tree2", sizeMan=12, edge.label.cex=2, color="yellow", edge.color = "black", weighted=FALSE) (Smatrix <- diag(mxEval(Smatrix, random_stage2\$mx.fit))) 1 - Smatrix