To check all is well, type
You should see a message: Loading required package: OpenMx
N.B. There have been some teething problems with the latest version of OpenMx; if the 'require' command gives an error message, you may need to reinstall R and try again. If all else fails, the very helpful people at the OpenMx forum will be able to assist if you describe your problem and send details of the console error message.
Before considering a script for optimisation, a brief introduction is needed to R syntax for model specification in OpenMx. To access OpenMx commands, type:
The Console window automatically displays the matrix, A, that you have created. Unlike the matrices created with the R matrix command, this matrix has properties as well as numerical values. These additional properties are listed on the Console, preceded by the symbol @. For this matrix they are boringly blank, but in future applications they will be explained further, and you will see how they can be assigned values. 'Full' is the default kind of matrix where values are specified for all rows and columns. We will encounter other matrix types later on.
Models in OpenMx
In OpenMx, you first specify a model, and then run it. The model specifies a set of matrices that will act as input to the model, and computational operations that are to be performed, as well as other constraints. Here is a very simple model that specifies two matrices and then adds them together. This is a pretty pointless thing to do in OpenMx, as it is much simpler to do it with regular R commands. However, it is useful for introducing you to the format of OpenMx models and specifications.
The final print statement indicates that just part of the model output is shown, the numerical result of the computation, which is referred to as algebras. If you type myrun on the Console, you will see the full model output. Note that once again there are lots of lines beginning with the @ symbol, most of which are NULL. These are all properties of the model output. The complex structure of models and their outputs can be confusing, but will become clearer as we work through more examples.
This example is just to give you a feel for the format of an mxModel; in practice, you would not use OpenMx for simple matrix manipulation: rather it is used in optimization. The next script shows an OpenMx in this more usual role. It is performing the same task as the Likelihood_3var script in the previous section: estimating the correlations between three variables from the attitudes dataset. But instead of laboriously computing the log likelihood for each possible value to find a minimum, it uses the powerful optimisation methods of OpenMx to find a solution. But first, a brief digression, on packages.
Finding and installing packages in R
In general, we will be working in future with models of covariances rather than correlations, but so we can compare outputs with the Likelihood_3var script, we will display results as correlations. To simplify our task, we will load another package that has a command for converting a covariance matrix into a correlation matrix.
I found a relevant package simply by Googling "convert covariance to correlation in R". This led me to the bayesm package. Like OpenMx, this does not come as a default package in R, so you have to load it. But this is made very easy. At the Console, type:
You may be asked for your CRAN mirror again, but by now you should not be fazed by that.
Right! Now we're ready to run the script, Find3corr_OpenMx
require(bayesm) #l You'll need to have installed this package, see text
# If you want to give different starting values for different variables,
# then values can be specified as a vector, e.g. c(60,55,50)
# Chol is multiplied by its transpose to give a square matrix: see text
print("expected correlations under optimal fit")
ecor=matrix(nmat(my_ecov),ncol=3) #nmat function from bayesm
|Figure 3: Models (dark squares) within models (grey squares) within a model (white square)|
- A matrix of observed data; this is specified using the command mxData
- Matrices which specify how the expected covariances are to be derived, these are defined with the command mxMatrix. The optimization procedure will start by estimating –LL for start values provided by the user – these are given as 'values'. The other point to note about an mxMatrix is that we can distinguish elements that we want to have estimated by the program (where Free=TRUE), and those we want to keep fixed at the start value. We will encounter cases where Free=FALSE later on. For the present, note that we want to estimate the whole covariance matrix, and so Free=TRUE for all elements of the expected covariance matrix.
- Alebraic expressions which specify operations to be carried out on matrices, defined with mxAlgebra, and explained for this script more fully below
- An expression specifying the optimization method to be used, here defined as mxFIMLObjective; the details of this are not visible to the user, but it specifies a procedure for comparing likelihoods for obtained and expected values of the covariances in order to minimize the –LL function. All OpenMx functions which are termed 'Objective' are functions that are used to achieve optimization.
The mxAlgebra section of this script illustrates one important method that is commonly used in optimization routines, i.e. the Cholesky decomposition. As we saw in the previous script, if you allow a square covariance matrix to have different values estimated for it, there is a danger that the resulting matrix will not be suitable for the matrix operations that are used in estimating likelihood. In formal terms, it is not 'positive definite' (which means that it has one or more negative eigenvalues). This can be avoided if, instead of estimating the expected covariance matrix directly, we estimate a lower triangular matrix, which, when multiplied by its transpose, yields the matrix of expected covariances. This is known as a Cholesky decomposition. You may encounter the Cholesky decomposition in structural equation models where it can be used to perform a process analogous to stepwise multiple regression, but its more general use in modelling is just as a computational fix to constrain the kinds of matrix that can be estimated.