You are here

A residuals(model) function

8 posts / 0 new
Last post
fife's picture
Offline
Joined: 07/01/2010 - 03:02
A residuals(model) function

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.

tbates's picture
Offline
Joined: 07/31/2009 - 14:25
umx::residuals.MxModel

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
fife's picture
Offline
Joined: 07/01/2010 - 03:02
Great! Thanks for doing that.

Great! Thanks for doing that.

fife's picture
Offline
Joined: 07/01/2010 - 03:02
Have things changed??

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).

fife's picture
Offline
Joined: 07/01/2010 - 03:02
After a whole lot of

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. :)

fife's picture
Offline
Joined: 07/01/2010 - 03:02
Okay....I bit the bullet and

Okay....I bit the bullet and updated OpenMx. Now it works as it used to (i.e., without the [[2]]):

model$objective@info$expCov

jpritikin's picture
Offline
Joined: 05/24/2012 - 00:35
avoid objective and @

model$objective is deprecated. Also, use of @ is deprecated. Can you try model$expectation$UnfilteredExpCov ?

fife's picture
Offline
Joined: 07/01/2010 - 03:02
That doesn't work for me.

That doesn't work for me. Does it work on your end?