Fitted correlation/covariance matrix (or residual matrix)

6 replies [Last post]
DavidCross's picture
Offline
Joined: 06/27/2011

I am curious if there is a straightforward way to obtain either the fitted covariance/correlation matrix or the residual matrix?

TIA

neale's picture
Offline
Joined: 07/31/2009
Yes

If one uses the RAM objective it isn't super straightforward, but supposing one uses the example from the homepage, and does something like:
oneFactorRun <- mxRun(oneFactor)
then
attr(oneFactorRun@output$algebras$`One Factor.objective`,"expCov")

[,1] [,2] [,3] [,4] [,5]
[1,] 0.1985440 0.2000301 0.2292526 0.2791080 0.3162323
[2,] 0.2000301 0.2916945 0.2907340 0.3539597 0.4010401
[3,] 0.2292526 0.2907340 0.3740347 0.4056700 0.4596284
[4,] 0.2791080 0.3539597 0.4056700 0.5332778 0.5595835
[5,] 0.3162323 0.4010401 0.4596284 0.5595835 0.6703011

I have to admit I was an attr() virgin until I tried to answer this question. It is because the objective has several bits - the objective function and the expected covariance matrix. The residuals are pretty easy to get now:

cov(demoOneFactor) - attr(oneFactorRun@output$algebras$`One Factor.objective`,"expCov")

So in this instance it's yes-ish. With missing data it's more of a problem; the general solution would be to fit a saturated model (different parameter for every variance & covariance and a different mean for every variable) and then to take the difference between these MLE's and the model-implied ones.

DavidCross's picture
Offline
Joined: 06/27/2011
Thanks - and one more question!

Thanks so much ... this helps, a lot!

But there is a more general question, and that is: "Is there documentation for all (or nearly all, or the most commonly used) @output functions? I have looked through the documentation, and the website, and haven't seen such a thing.

Cheers

mspiegel's picture
Offline
Joined: 07/31/2009
Digging through the

Digging through the model@output is usually unnecessary (although sometimes necessary). In this case you should be able to do oneFactorRun$objective@expCov. In OpenMx 1.2, this syntax will change to something like oneFactorRun$objective@info$expCov. We're still playing around with the exact notation for OpenMx 1.2, the goal is to remove the proliferation of S4 slots in OpenMx objects. (S4 slots are the thingies accessed using '@')

BTW the attributes() method is working because it appears that R implements S4 slots as attributes. I don't really understand the mechanisms going on. A less mysterious approach is to use slotNames(oneFactorRun$objective) in order to see what S4 slots have been declared.

Steve's picture
Offline
Joined: 07/30/2009
Expected covariance in a RAM model in 1.2

It appears that oneFactorRun$objective@expCov no longer works in 1.2. I'm wondering what the new recommended alternative might be.

mspiegel's picture
Offline
Joined: 07/31/2009
Try

Try oneFactorRun$objective@info$expCov.

neale's picture
Offline
Joined: 07/31/2009
Documentation pending

Grappling with model output is not well documented, for several reasons. One is that the @ parts of model output are somewhat in a state of flux, so that we weren't sure if @ syntax was going to be superseded by $ syntax uniformly. A second is that it's not been a high priority. Third, it is typically possible to "drill down" using R to inspect objects within objects. For example, with the oneFactorRun object I can just see what it contains:

> oneFactorRun
MxModel 'One Factor'
type : RAM
@matrices : 'A', 'S', and 'F'
@algebras :
@constraints :
@intervals :
@latentVars : 'G'
@manifestVars : 'x1', 'x2', 'x3', 'x4', and 'x5'
@data : 5 x 5
@data means : NA
@data type: 'cov'
@submodels :
@objective : MxRAMObjective
@independent : FALSE
@options :
@output : TRUE

And then have a look at the matrices it contains:

oneFactorRun@matrices
$A
FullMatrix 'A'

@labels: No labels assigned.

@values
x1 x2 x3 x4 x5 G
x1 0 0 0 0 0 0.3971521
x2 0 0 0 0 0 0.5036611
x3 0 0 0 0 0 0.5772413
x4 0 0 0 0 0 0.7027736
x5 0 0 0 0 0 0.7962499
G 0 0 0 0 0 0.0000000

@free
x1 x2 x3 x4 x5 G
x1 FALSE FALSE FALSE FALSE FALSE TRUE
x2 FALSE FALSE FALSE FALSE FALSE TRUE
x3 FALSE FALSE FALSE FALSE FALSE TRUE
x4 FALSE FALSE FALSE FALSE FALSE TRUE
x5 FALSE FALSE FALSE FALSE FALSE TRUE
G FALSE FALSE FALSE FALSE FALSE FALSE

@lbound: No lower bounds assigned.

@ubound: No upper bounds assigned.

...
< same sort of thing for S and F deleted for brevity>

Or I could just look at one of them directly with
>oneFactorRun@matrices$A
or more succinctly
>oneFactorRun$A
or just the values thereof

> oneFactorRun$A@values
x1 x2 x3 x4 x5 G
x1 0 0 0 0 0 0.3971521
x2 0 0 0 0 0 0.5036611
x3 0 0 0 0 0 0.5772413
x4 0 0 0 0 0 0.7027736
x5 0 0 0 0 0 0.7962499
G 0 0 0 0 0 0.0000000

or just an element of this matrix
> oneFactorRun$A@values[1,6]
[1] 0.3971521

Now I might want to, say set this element of this matrix to zero and stop it being a free parameter and run it again (copying the result to a new object first):
oneFactorFixGx1 <- oneFactorFix
oneFactorFixGx1$A@values[1,6] <- 0
oneFactorFixGx1$A@free[1,6] <- FALSE
oneFactorFixGx1Run <- mxRun(oneFactorFixGx1)