# Create and fit a model using mxMatrix, mxAlgebra,
# mxExpectationNormal, and mxFitFunctionML
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)
# Define the matrices
matrixS <- mxMatrix(type = "Full", nrow = 2, ncol = 2, values=c(1,0,0,1),
free=c(TRUE,FALSE,FALSE,TRUE), labels=c("Vx", NA, NA, "Vy"),
name = "S")
matrixA <- mxMatrix(type = "Full", nrow = 2, ncol = 2, values=c(0,1,0,0),
free=c(FALSE,TRUE,FALSE,FALSE), labels=c(NA, "b", NA, NA),
name = "A")
matrixF <- mxMatrix(type="Iden", nrow=2, ncol=2, name="F")
matrixM <- mxMatrix(type = "Full", nrow = 1, ncol = 2, values=c(0,0),
free=c(TRUE,TRUE), labels=c("Mx", "My"), name = "M")
# Define the expectation
expFunction <- mxExpectationRAM(M="M", dimnames = tmpNames)
# Choose a fit function
fitFunction <- mxFitFunctionML()
# Define the model
tmpModel <- mxModel(model="exampleRAMModel",
matrixA, matrixS, matrixF, matrixM,
expFunction, fitFunction,
mxData(observed=cov(tmpFrame), type="cov", numObs=nrow(tmpFrame),
means = colMeans(tmpFrame)))
# Fit the model and print a summary
tmpModelOut <- mxRun(tmpModel)
summary(tmpModelOut)
Run the code above in your browser using DataLab