$R^2$ for endogenous variables
Posted on

Forums
Hello,
What is the best way to compute $R^2$ for endogenous variables in OpenMx?
I wrote the function below, I get negative $R^2$ for exogenous (not endogenous) variable. Shouldn't it produce zero for exogenous variables?
Thanks in advance,
Davood
r2sq <- function(model = NULL) {
if (is.null(model))
stop("Please provide a model.")
maniVar <- model@manifestVars
df <- model@data@observed
df <- df[maniVar]
n <- nrow(df)
S <- cov(df)
res_mat <- model$S$values[maniVar, maniVar]
R2 <- 1 - diag(res_mat) / diag(S)
names(R2) <- maniVar
return(R2)
}
reproducible?
Log in or register to post comments