#### Example of a 2-group analysis with correlation matrices #### One common correlation matrix Rho is estimated #### Diagonal matrices D are used to account for differences in scaling library(OpenMx) library(MASS) set.seed(12345) V <- matrix(c(12,8,7, 8,10,6, 7,6,15),3,3) x1 <- mvrnorm(100,c(0,0,0),V) x2 <- mvrnorm(100,c(0,0,0),V) cov1 <- cov(x1) cov2 <- cov(x2) cor1 <- round(cov2cor(cov1),4) cor2 <- round(cov2cor(cov2),4) varnames <- c("v1","v2","v3") dimnames(cor1) <- dimnames(cor2) <- list(varnames,varnames) cor1 cor2 data1 <- mxData(observed = cor1, type = 'cov', numObs = 100) data2 <- mxData(observed = cor2, type = 'cov', numObs = 100) rho <- mxMatrix( type = "Symm", free = c(FALSE, TRUE,FALSE, TRUE,TRUE,FALSE), values = c(1, .1,1, .1,.1,1), labels = c("r11", "r21","r22", "r31","r32","r33"), nrow = 3, ncol = 3, byrow = TRUE, name = "Rho") D <- mxMatrix( type = "Diag", free = rep(TRUE,3), values = 1, nrow = 3, ncol = 3, byrow = TRUE, name = "D") sigma <- mxAlgebra(expression = D%*%Rho%*%D, name = "sigma") exp <- mxExpectationNormal(covariance="sigma",dimnames = varnames) fit <- mxFitFunctionML() model1 <- mxModel('model1',data1,rho,D,sigma,exp,fit) model2 <- mxModel('model2',data2,rho,D,sigma,exp,fit) multifit <- mxFitFunctionMultigroup(paste(paste(rep("model",2),1:2,sep=""),"fitfunction",sep=".")) overallmodel <- mxModel('Multigroup',model1,model2,multifit) # Run model out <- mxRun(overallmodel) summary(out) # Check matrices # Rho has 1's on diagonal rho <- out$model1$Rho$values rho # D's are bigger or smaller than 1 D1 <- out$model1$D$values D2 <- out$model2$D$values D1 D2 # sigma 1 and sigma 2 do not necessarily have 1's on diagonal sigma1 <- out$model1$sigma$result sigma2 <- out$model2$sigma$result