! SCRIPT NAME   : ind3f.mx  (hm)
! GOAL          : Independent 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)	:
! Full model including three common genetic factors, one common shared environmental
! factor, one common non-shared environmental factor, 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 calculates genetic and environmental standardized path coefficients

#NGroups 4
#define nvar 5
#define nfac 1

G1: Define matrices
 Calculation
 Begin Matrices;
  X full nvar 3             ! common factor genetic path coefficients
  Y full nvar nfac          ! common factor shared environment path coefficients
  Z full nvar nfac Free     ! common factor nonshared environment path coefficients
  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
  M full  1 nvar Free       ! grand means 
 End Matrices;
  Specify X                 ! declare free parameters for 3 genetic common factors
!   G1  G2  G3
   100 110   0
   101 111   0
   102 112   0
   103   0 120
   104   0 121
  Drop T 1 5 5              ! fix genetic specific for last variable for identification
  Start .4 X 1 1 1 - X 1 nvar 1  Z 1 1 1 - Z 1 nvar 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
 Begin Algebra;
  A= X*X' + T*T';           ! additive genetic variance components
  C= Y*Y' + U*U';           ! shared environment variance components
  E= Z*Z' + V*V';           ! nonshared environment variance components
 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: Calculate Standardised Solution
 Calculation
 Matrices = Group 1
  I Iden nvar nvar
 End Matrices;
 Begin Algebra;
  R=A+C+E;                  ! total variance
  S=(\sqrt(I.R))~;          ! diagonal matrix of standard deviations
  P=S*X| S*Y| S*Z| \d2v(S*T)'| \d2v(S*U)'| \d2v(S*V)';  
   ! standardized estimates for common and specific factors
 End Algebra; 
  Labels Col P a1 a2 a3 c1 e1 as cs es
  Labels Row P cb gm wm rk cl
 Options NDecimals=4
End
