You are here

$R^2$ for endogenous variables

2 posts / 0 new
Last post
dtofighi's picture
Joined: 10/12/2009 - 14:55
$R^2$ for endogenous variables


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,


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
AdminRobK's picture
Joined: 01/24/2014 - 12:15

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.