library(OpenMx)
# Simulate some data
x=rnorm(1000, mean=0, sd=1)
y= 0.5*x + rnorm(1000, mean=0, sd=1)
tmpFrame <- data.frame(x, y)
tmpNames <- names(tmpFrame)
dir <- tempdir() # safe place to create files
mxOption(key="Checkpoint Directory", value=dir)
# Create a model that includes an expected covariance matrix,
# an expectation function, a fit function, and an observed covariance matrix
data <- mxData(cov(tmpFrame), type="cov", numObs = 1000)
expCov <- mxMatrix(type="Symm", nrow=2, ncol=2, values=c(.2,.1,.2), free=TRUE, name="expCov")
expFunction <- mxExpectationNormal(covariance="expCov", dimnames=tmpNames)
fitFunction <- mxFitFunctionML()
testModel <- mxModel(model="testModel", expCov, data, expFunction, fitFunction)
#Use mxRun to optimize the free parameters in the expected covariance matrix
modelOut <- mxRun(testModel)
modelOut$expCov
# Save the ending state of modelOut in a checkpoint file
mxSave(modelOut)
# Restore the saved model from the checkpoint file
modelSaved <- mxRestore(testModel)
modelSaved$expCov
Run the code above in your browser using DataLab