You are here

$R^2$ for endogenous variables

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

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

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.