Regression analysis

Attachment | Size |
---|---|
Regression.csv | 1.52 KB |
Dear Community,
I’m trying to fit a regression model by regressing the true effect size y_strength on the true effect size y_MEP (see attached dataset). I use the following code to remove NA and unnecessary columns, as well as to specify the SEM in OpenMx:
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())
When I run the analysis, obtained the following error:
Error in value[rows[[i]], cols[[i]]] <- startValue :
incorrect number of subscripto n matrix
If a traceback the error, it gives:
> traceback()
5: FUN(X[[i]], ...)
4: lapply(flatModel@matrices, populateDefVarMatrix, flatModel)
3: populateDefInitialValues(flatModel)
2: runHelper(model, frontendStart, intervals, silent, suppressWarnings,
unsafe, checkpoint, useSocket, onlyFrontend, useOptimizer,
beginMessage)
1: mxRun(reg, intervals = TRUE, silent = TRUE)
I’m trying to figure out what’s going on but without success. Could you help me with this error please?
Thanks
Regression analysis
Juanfran
Log in or register to post comments
Hi Juanfran,
The issue is caused by the readr package, which returns a tibble. You may solve it using as.data.frame(RM_new).
Attached is the complete R code and output. I have also included an alternative approach, which may simplify the code.
Mike
Log in or register to post comments
Regression analysis
Log in or register to post comments