$R^2$ for endogenous variables

Posted on
Picture of user. dtofighi Joined: 10/12/2009
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)
}
Replied on Mon, 09/09/2019 - 17:14
Picture of user. AdminRobK Joined: 01/24/2014

Could you provide a minimal reproducible instance of negative R²s? Every reasonable RAM example script I've tried has given reasonable results. Note also that the R² for exogenous variables won't be exactly zero even in the simplest cases, since `cov()` does its calculation with N-1 in the denominator, but the normal-theory ML variance estimator uses N.