A residuals(model) function

Posted on
No user picture. fife Joined: 07/01/2010
I think the subject says it, but I think it's awkward to have to type:

cov2cor(data.matrix(data)) - cov2cor(model$objective@info$expCov)

It seems like it would be simple to have a class (is that the term?) for the residuals function that operates on an mxobject. I often find myself looking at the residual covariance matrix to see where misfit lies.

Replied on Wed, 04/30/2014 - 14:29
Picture of user. tbates Joined: 07/31/2009

the umx helper package (now) has an S3 function implementing residuals()

Let me know how it works for you

library(devtools)
install_github("tbates/umx")
library(umx)
data(demoOneFactor)
latents = c("g")
manifests = names(demoOneFactor)
m1 <- mxModel("One Factor", type = "RAM",
manifestVars = manifests, latentVars = latents,
mxPath(from = latents, to = manifests),
mxPath(from = manifests, arrows = 2),
mxPath(from = latents, arrows = 2, free = F, values = 1.0),
mxData(cov(demoOneFactor), type = "cov", numObs = 500)
)
m1 = umxRun(m1, setLabels = T, setValues = T)
residuals(m1)
residuals(m1, digits = 3)
residuals(m1, digits = 3, suppress = .005)
a = residuals(m1);
a

Replied on Tue, 09/30/2014 - 16:10
No user picture. fife Joined: 07/01/2010

Hi all,

My previous code for extracting residuals quit working. I remembered starting this topic and it seems that tbates's code no longer works either. Has OpenMx changed (again) how one calls the expected covariance matrix? If so, how do I access it? Is there a way to call it that won't change in the future? (I've got a package that depends on that and would hate to have to change it with each OpenMx update).

Replied on Tue, 09/30/2014 - 16:36
No user picture. fife Joined: 07/01/2010

In reply to by fife

After a whole lot of fiddling, I figured out how you can extract the expected covariance matrix:

model$objective[[2]]@info$expCov

So, we've got an S3 object (expCov) within an S4 object (info) within a list ([[2]]) within an S3 object (objective). Weird. By the way, I'm on R 3.1.1 and OpenMx_999.0.0-3473. I'm not sure if this is the most recent version, but I'm afraid I'll break everything if I update. :)