library(metaSEM) library(readr) regression_model <- read_delim("Desktop/Regression.csv", ";", escape_double = FALSE, col_types = cols(Type_training = col_factor(levels = c("SP", "MP", "IM"))), na = "NA", trim_ws = TRUE) View(regression_model) ## Regression between both ES: MEP --> strength # 1. remove the missing values (these models cannot work with MV) and exclude # the 3 first columns RM_new <- regression_model[-8,4:9] ## A: asymmetric paths for regression coefficients ## and factor loadings A <- matrix(c(0, "0.1*beta1_2", 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0), ncol=4, nrow=4, byrow=TRUE) dimnames(A) <- list(c("f_strength","f_MEP", "y_strength","y_MEP"), c("f_strength","f_MEP", "y_strength","y_MEP")) A ## Convert it into OpenMx matrix A <- as.mxMatrix(A) # S: symmetric covariances and variances S <- mxMatrix(type="Symm", nrow=4, ncol=4, byrow=TRUE, free=c(TRUE, FALSE,TRUE, FALSE,FALSE,FALSE, FALSE,FALSE,FALSE,FALSE), values=c(0.1, 0,0.1, 0,0,0, 0,0,0,0), labels=c("tau2_1_1", NA,"tau2_2_2", NA,NA,"data.v_strength", NA,NA,"data.Cov_strength_MEP","data.v_MEP"), name = "S") ## F: select observed variables F <- matrix(c(0, 0, 1, 0, 0, 0, 0, 1), nrow = 2, ncol = 4, byrow = TRUE) dimnames(F) <- list(c("y_strength","y_MEP"), c("f_strength","f_MEP","y_strength", "y_MEP")) F F <-as.mxMatrix(F) ## M: intercepts or means (only intercepts of the latent are estimated ## the intercepts of the observed variables are set to 0) M <- matrix(c("0*beta1_0","0*beta2_0",0,0), nrow=1, ncol=4, byrow = TRUE) dimnames(M)[[2]] <- c("f_strength","f_MEP", "y_strength","y_MEP") M M <- as.mxMatrix(M) ## Formula for R2 R2 <- mxAlgebra(beta1_2^2*tau2_2_2/(beta1_2^2*tau2_2_2 + tau2_1_1), name="R2") ## Build the model reg <- mxModel("Regression", mxData(observed=RM_new, type="raw"), A, S, F, M, R2, mxCI("R2"), mxExpectationRAM(A="A", S="S", F="F", M="M", dimnames = c("f_strength","f_MEP", "y_strength","y_MEP")), mxFitFunctionML()) ## Run the analysis reg.fit <- mxRun(reg, intervals=TRUE, silent=TRUE) reg.fit@output$status[[1]]