! SCRIPT NAME   : comp.mx  (hm)
! GOAL          : Common pathway model to determine underlying (ACE) factor structure 
! DATA          : continuous
! INPUT         : raw data
! UNI/BI/MULTI  : multi
! DATA-GROUPS   : MZ DZ
! MEANS MODEL   : grand means
! VARIANCE COVARIANCE MODEL(S)	: one common latent factor (F) influenced by three latent
! factors A, C, E (genetic, shared and non-shared environment), and for each variable specific 
! genetic, shared and non-shared environmental effects. 

! Downloading Mx software: http://views.vcu.edu/mx
! Mx script's library: http://www.psy.vu.nl/mxbib
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! Group 1 sets up matrices for variance components
! Group 2 fits model to MZ data
! Group 3 fits model to DZ data
! Group 4 constraint
! Group 5 calculates genetic and environmental standardized path coefficients

#NGroups 5
#define nvar 5
#define nfac 1

G1: Define matrices
 Calculation
 Begin Matrices;
  X full nfac nfac Free     ! latent factor genetic path coefficient
  Y full nfac nfac          ! latent factor shared environment path coefficient
  Z full nfac nfac Free     ! latent factor nonshared environment path coefficient
  T diag nvar nvar Free     ! variable specific genetic path coefficients
  U diag nvar nvar          ! variable specific shared environment path coefficients
  V diag nvar nvar Free     ! variable specific residual path coefficients
  F full nvar nfac Free     ! loadings of variables on latent factor 
  M full  1 nvar Free       ! grand means 
 End Matrices;
  Start .4 F 1 1 1 - F 1 nvar 1
  Start .6 X 1 1 1 Z 1 1 
  Start .2 T 1 1 1 - T 1 nvar nvar
  Start .5 V 1 1 1 - V 1 nvar nvar
  Matrix M 8 8 8 8 8 8
 Begin Algebra;
  A= F&(X*X') + T*T';      ! genetic variance components
  C= F&(Y*Y') + U*U';      ! shared environment variance components
  E= F&(Z*Z') + V*V';      ! nonshared environment variance components
  L= X*X' + Y*Y' + Z*Z';   ! variance of latent factor
 End Algebra;
 Option No_Output
End

G2: MZ twins
#include mri-iq-mz.dat
 Begin Matrices = Group 1;
 Means M | M ;
 Covariance
  A+C+E | A+C 	_
  A+C	   | A+C+E ;
 Option Rsiduals
End

G3: DZ twins
#include mri-iq-dz.dat
 Begin Matrices = Group 1;
  H full 1 1
 End Matrices;
  Matrix H .5
 Means M | M ;
 Covariance
  A+C+E | H@A+C	_
  H@A+C | A+C+E ;
 Option Rsiduals
End

G4: Constrain variance of latent factor to 1
 Constraint
 Begin Matrices;
  L computed =L1
  I unit 1 1
 End Matrices;
 Constraint L = I ;
End

G5: Calculate Standardised Solution
 Calculation
 Matrices = Group 1
  D Iden nvar nvar
 End Matrices;
 Begin Algebra;
  R=A+C+E;               ! total variance
  S=(\sqrt(D.R))~;       ! diagonal matrix of standard deviations
  P=S*F| \d2v(S*T)'| \d2v(S*U)'| \d2v(S*V)';   ! standardized estimates
  W= X*X' | Y*Y'| Z*Z';  ! standardized estimates for latent phenotype
 End Algebra;
 Label Col P LP as cs es
 Label Row P cb gm wm rk cl
 Label Col W a^2 c^2 e^2
 Label Row W LP
 Options NDecimals=4
End
