matrix vs path specification

9 replies [Last post]
ebejer's picture
Offline
Joined: 03/18/2010

Hi,

I'm learning to use OpenMx and have been working through some simple models. The estimates I get differ between matrix and path specification when I use the same data, and model. I also get different values for the same models depending on whether I use covariances or raw data for parameter estimates. Below are the path and matrix style specification that I have found to differ. Are the models different, or is expected that they will differ slightly? Are estimates expected to differ slightly between using covariances and raw data for estimation? Sorry if this is a really silly question.

j

p.s. what is the filter (identity?) matrix used for in the matrix model?

The models and data are straight out of OpenMx documentation;

data(myRegData)

names(myRegData)

simpleDataRaw<-myRegDataRaw[,c("x","y")]

myRegDataCov<-matrix(
c(0.808,-0.110, 0.089, 0.361,
-0.110, 1.116, 0.539, 0.289,
0.089, 0.539, 0.933, 0.312,
0.361, 0.289, 0.312, 0.836), nrow=4, dimnames=list(c("w","x","y","z"),
c("w","x","y","z"))
)

simpleDataCov<-myRegDataCov[c("x","y"),c("x","y")]

myRegDataMeans<-c(2.582, 0.054, 2.574, 4.061)

simpleDataMeans<-myRegDataMeans[c(2,3)]

## I will specify the model

require(OpenMx)

uniRegModel<-mxModel("simple regression path specification",
type="RAM",
mxData(observed=simpleDataRaw, type="raw"),
manifestVars=c("x","y"),
mxPath(from=c("x","y"), arrows=2, free=TRUE, values=c(1,1),
labels=c("varx","residual")),
mxPath(from="x", to="y", arrows=1, free=TRUE, values=1, labels="beta1"),
mxPath(from="one", to=c("x","y"), arrows=1, free=TRUE, values=c(1,1),
labels=c("meanx", "beta0"))
)
uniRegModelFit<-mxRun(uniRegModel)

uniRegModelFit@output

uniRegModelm<-mxModel("simple regression matrix specification",
mxData(observed=simpleDataRaw, type="raw"),
mxMatrix(type="Full", nrow=2, ncol=2, free=c(rep(F,4)),
values=c(0,0,1,0), labels=c(NA, NA,"beta1", NA),
byrow=TRUE, name="A"),
mxMatrix(type="Symm", nrow=2, ncol=2, values=c(1,0,0,1),
free=c(T,F,F,T), labels=c("varx", NA,NA, "residual"),
byrow=TRUE, name="S"),
mxMatrix(type="Iden", nrow=2, ncol=2, name="F", dimnames=list(
c("x","y"),c("x","y"))),
mxMatrix(type="Full", nrow=1, ncol=2, free=c(T,T), values=c(0,0),
labels=c("meanx","beta0"), name="M"),
mxRAMObjective("A","S","F","M"))

uniRegFitm<-mxRun(uniRegModelm)

uniRegFitm@output

ebejer's picture
Offline
Joined: 03/18/2010
Hi, I have tried to run the

Hi,

I have tried to run the growth curve model, and have found that the standard errors I get are quite larger and the covariance tends to exceed the variance.

I have played around with the parameter values though can't seem to find reasonable estimates. I'm not sure what I need to look for.

Any suggestions will be greatly appreciated. I'll attach the script and output below.

Kind regards,

j

data(myLongitudinalData)
myLongitudinalCov<-cov(myLongitudinalData)
myLongitudinalMeans<-mean(myLongitudinalData)

growthCurveModelmr<-mxModel("Linear growth curve model matrixRaw specification",
type="RAM",
mxData(observed=myLongitudinalData, type="raw"),

mxMatrix(type="Full", nrow=7, ncol=7, free=c(rep(c(F,F,F,F,F),7), c(F,F,F,F,F,T,T,F,F,F,F,F,T,T)), values=c(rep(c(0,0,0,0,0), 7), c(50,50,50,50,50,40,25,0,10,100,1000,10000,25,70)), labels=c(rep(c(NA,NA,NA,NA,NA),7),
c("i1","i2","i3","i4","i5","vari","cov",NA,NA,NA,NA,NA,"cov","vars")), name="A"),

mxMatrix(type="Symm", nrow=7, ncol=7, values=c(rbind(rep(10,7), diag(7, x=10))[2:8,1:5], c(0,0,0,0,0,0,0), c(0,0,0,0,0,0,0)),
free=c(T,F,F,F,F,F,F,
F,T,F,F,F,F,F,
F,F,T,F,F,F,F,
F,F,F,T,F,F,F,
F,F,F,F,T,F,F,
F,F,F,F,F,F,F,
F,F,F,F,F,F,F), labels=c("residual", NA, NA, NA, NA, NA, NA, NA,"residual", NA, NA, NA, NA, NA,
NA,NA, "residual", NA, NA, NA, NA,
NA,NA, NA,"residual",NA, NA, NA,
NA,NA, NA, NA,"residual",NA, NA,
NA,NA, NA, NA, NA, NA, NA,
NA,NA, NA, NA, NA, NA, NA), byrow=TRUE, name="S"),

mxMatrix(type="Full", nrow=5, ncol=7, values=cbind(rep(1,7), diag(7)) [1:5,2:8], free=FALSE, byrow=TRUE, dimnames=list(NULL,
c("x1","x2","x3","x4","x5","intercept","slope")), name="F"),

mxMatrix(type="Full", nrow=1, ncol=7, free=c(F,F,F,F,F,T,T), values=c(10,12,14,16,18,35,41),
labels=c(NA,NA,NA,NA,NA,"meani","means"), name="M"),
mxRAMObjective("A","S","F","M")
)

growthCurveFitmr<-mxRun(growthCurveModelmr)

summary(growthCurveFitmr)

data:
$`Linear growth curve model matrixRaw specification.data`
x1 x2 x3 x4 x5
Min. : 2.372 Min. : 2.632 Min. : 3.323 Min. : 4.569 Min. : 6.617
1st Qu.: 8.143 1st Qu.:10.004 1st Qu.:11.632 1st Qu.:12.962 1st Qu.:14.766
Median :10.011 Median :11.754 Median :13.617 Median :15.153 Median :17.061
Mean : 9.864 Mean :11.812 Mean :13.612 Mean :15.317 Mean :17.178
3rd Qu.:11.598 3rd Qu.:13.627 3rd Qu.:15.559 3rd Qu.:17.415 3rd Qu.:19.684
Max. :16.909 Max. :20.019 Max. :21.659 Max. :25.283 Max. :29.693

free parameters:
name matrix row col Estimate Std.Error
1 vari A 6 6 7819.58305 6.024839e+03
2 cov A 7 6 42218.27256 4.736609e+04
3 vars A 7 7 -43090.02422 8.007324e+04
4 residual S 1 1 9.60953 2.717991e-01
5 meani M 1 6 53.21884 3.270244e+01
6 means M 1 7 273.10311 3.020402e+02

observed statistics: 2500
estimated parameters: 6
degrees of freedom: 2494
-2 log likelihood: 12751.58
saturated -2 log likelihood: NA
number of observations: 500
chi-square: NA
p: NA
AIC (Mx): 7763.582
BIC (Mx): -1373.826
adjusted BIC:
RMSEA: NA
timestamp: 2010-10-15 09:04:07
frontend time: 0.2136118 secs
backend time: 0.3100319 secs
independent submodels time: 5.507469e-05 secs
wall clock time: 0.5236988 secs
cpu time: 0.5236988 secs
openmx version number: 0.9.2-1446

Ryne's picture
Offline
Joined: 07/31/2009
There are some errors in the

There are some errors in the model you specified. Most prominently, you placed the latent variances and covariances in the A matrix rather than the S matrix. This is the main reason for your problematic results. Parameters "vari", "cov" and "vars" should be in elements 6,6, 7,6 and 7,7 of the S matrix. I'm a little surprised that this model didn't return at least a status 1 error; the "vari", "cov" and "vars" parameters don't impact the expected covariance matrix, so they should create a flat spot in the likelihood space that would throw an error.

I'm also curious as to why you fixed the manifest intercepts (means) at the values 10, 12, 14, 16 and 18, especially given the exponential pattern of your slope loadings.

ebejer's picture
Offline
Joined: 03/18/2010
Thanks Ryne, yes that's a

Thanks Ryne, yes that's a very interesting question :)

ebejer's picture
Offline
Joined: 03/18/2010
Thankyou very much. j

Thankyou very much.

j

ebejer's picture
Offline
Joined: 03/18/2010
Hi, I have hit another snag.

Hi,

I have hit another snag. When I run the matrix model with covariance data, and the model contains more than one variable, I get the following error message (this has happened for the multiple and multivariate regression models as well);

oneFactorFitmc<-mxRun(oneFactorModelmc)
Running one factor matrix model
Error: The M matrix associated with the RAM objective function in model 'one factor matrix model' does not contain dimnames.

summary(oneFactorFitmc)
Error: object 'oneFactorFitmc' not found
Error in summary(oneFactorFitmc) :
error in evaluating the argument 'object' in selecting a method for function 'summary'

I have tried several different things to try to make the model run, though I am really only guessing. Following is the basic model I use;

oneFactorModelmc<-mxModel("one factor matrix model",
type="RAM",
mxData(observed=oneFactorCov, type="cov", means=oneFactorMeans, numObs=500),
mxMatrix(type="Full", nrow=7, ncol=7, values=rbind(matrix(rep(rep(c (0,0,0,0,0,0),7))), cbind(rep(c(1,1,1,1,1,1,0)))), free=rbind(matrix(
rep(rep(c(F,F,F,F,F,F),7))), cbind(rep(c(F,T,T,T,T,T,F)))),
labels=rbind(matrix(rep(rep(c(NA,NA,NA,NA,NA,NA), 7))), cbind(rep
(c("l1","l2","l3","l4","l5","l6",NA)))), byrow=TRUE, name="A"),
mxMatrix(type="Symm", nrow=7, ncol=7, values=cbind(rep(0,7), diag(7))[,2:8],
free=c(T,F,F,F,F,F,F,
F,T,F,F,F,F,F,
F,F,T,F,F,F,F,
F,F,F,T,F,F,F,
F,F,F,F,T,F,F,
F,F,F,F,F,T,F,
F,F,F,F,F,F,T), labels=c("e1",NA, NA, NA, NA, NA, NA, NA,"e2",NA,NA, NA, NA, NA,
NA,NA,"e3",NA, NA, NA, NA,
NA,NA, NA,"e4", NA, NA, NA,
NA,NA, NA, NA,"e5", NA, NA,
NA,NA, NA, NA, NA,"e6", NA,
NA,NA, NA, NA, NA, NA,"e7"), byrow=TRUE, name="S"),
mxMatrix(type="Full", nrow=6, ncol=7, free=FALSE, values=cbind(rep(0,7),
diag(7))[1:6,2:8], byrow=TRUE, name="F",
dimnames=list(NULL, c("x1","x2","x3","x4","x5","x6","f1"))),
mxMatrix(type="Full", nrow=1, ncol=7, free=c(T,T,T,T,T,T,F), values=c(1,1,1,1,1,1,0),
labels=c("meanx1","meanx2","meanx3","meanx4","meanx5","meanx6",NA), name="M"),
mxRAMObjective("A","S","F","M")
)

oneFactorFitmc<-mxRun(oneFactorModelmc)

summary(oneFactorFitmc)

__________

I thought it might have something to do with the objective function?

j

tbates's picture
Offline
Joined: 07/31/2009
As the error message says,

As the error message says, the M matrix needs to have dimnames so that the objective knows how to map this matrix onto the data (i.e., the M matrix needs to have dimensions named to match the covariance matrix variables. You can see this in the F matrix already done correctly

mxMatrix(type="Full", nrow=1, ncol=7,
free =c(T,T,T,T,T,T,F),
values=c(1,1,1,1,1,1,0),
labels=c("meanx1","meanx2","meanx3","meanx4","meanx5","meanx6",NA),
dimnames=list(NULL, c("x1","x2","x3","x4","x5","x6","f1")),
name="M"
),

You might be making things a bit more obscure than they need to be while learning: In particular, in matrix mode, large RAM models can be hard to read compared to their path specification.

I think you might be using functions like rep() unnecessarily. When learning, I would write everything out to avoid errors.

For instance
rep(c(1,1,1,1,1,1,0))
is just
c(1,1,1,1,1,1,0)

and, without specifying rows and columns in the matrix, this is just a vector of 42 0s
matrix(rep(rep(c(0,0,0,0,0,0),7)))

ebejer's picture
Offline
Joined: 03/18/2010
Thanks Tim, I was looking for

Thanks Tim, I was looking for the answer in all the wrong places! Yes, I have been playing around with the rep() function.

j

neale's picture
Offline
Joined: 07/31/2009
The A matrix in the

The A matrix in the uniRegModelm specification is missing a free parameter in element 2,1. That the models are specified differently is easily seen using summary(uniRegFitm) and summary(uniRegModelFit). Replacing

mxMatrix(type="Full", nrow=2, ncol=2, free=c(rep(F,4)),

with
mxMatrix(type="Full", nrow=2, ncol=2, free=c(F,F,T,F),

yields identical results.

The F matrix is a filter matrix to extract the covariance matrix of the observed variables from that of the result of
solve(I-A) %&% S
since this typically yields the covariance matrix of both the observed and the latent variables. In this case, there just aren't any explicit latent variables [welcome home simple regression, the lack of latent variables means that we don't end up with a cripplingly cryptic model specification :)].

mspiegel's picture
Offline
Joined: 07/31/2009
We've seen several other

We've seen several other users test out the OpenMx library by creating the same model in path and matrix forms. So here's a hint that may help several people: path style models are stored in matrix format in OpenMx. So you can write your model in path style, and then inspect the matrices by using:

> model$A
> model$S
> model$F

Assuming the variable that stores your model is named 'model'.