A C E model fitted to the Heath (1989) data 
!     on alcohol consumption.
! Group 1.... Monozygotic twin pairs
! Anything after a '!' is a comment
DAta NGroups=2 NInput_variables=2 NObservations=171
LAbels Alc_t1 Alc_t2
CMatrix
1.28         
0.766 1.194
MATrices
A FUll 2 6
B SYmmetric 6 6
COvariance_model A*B*A' /
SPecification A
1 2 3 0 0 0
0 0 0 1 2 3
LAbels Col A
At1 Ct1 Et1 At2 Ct2 Et2
LAbels Row A
Pt1 Pt2
MAtrix B
1
0 1
0 0 1
1 0 0 1
0 1 0 0 1
0 0 0 0 0 1
LAbels Row B
At1 Ct1 Et1 At2 Ct2 Et2
LAbels Col B
At1 Ct1 Et1 At2 Ct2 Et2
OUtput RSiduals

Group 2.... Dizygotic twin pairs
DAta NInput_variables=2 NObservations=101
CMatrix
1.077 
0.463 0.962
LAbels Alc_t1 Alc_t2
MATrices
A FUll 2 6 =A(1)
B SYmm 6 6
COvariance_model A*B*A' /
MAtrix B
1
0 1
0 0 1
.5 0 0 1
0 1 0 0 1
0 0 0 0 0 1
LAbels row B
At1 Ct1 Et1 At2 Ct2 Et2
LAbels col B
At1 Ct1 Et1 At2 Ct2 Et2
STart .6 ALL
OUtput RSidual
Phenotypic interaction model, fit to Heath 1989 data. 
! Female MZ Twins
DAta NGroups=2 NInput_vars=2 NObservations=171
CMatrix
1.28
0.766 1.194
LAbels Alc_t1 Alc_t2
MATrices
G FUll 2 6
P SYmm 6 6
B FUll 2 2
I IDentity 2 2
COvariance_model (I-B)~*(G*P*G')*(I-B)~'/
LAbels row G
B4int_t1 B4int_t2
LAbels col G
At1 Ct1 Et1 At2 Ct2 Et2
SPecification G
1 2 3 0 0 0
0 0 0 1 2 3
LAbels row P
At1 Ct1 Et1 At2 Ct2 Et2
LAbels col P
At1 Ct1 Et1 At2 Ct2 Et2
MAtrix P
1
0 1
0 0 1
1 0 0 1
0 1 0 0 1
0 0 0 0 0 1
LAbels col B
B4int_t1 B4int_t2
LAbels row B
Alc_t1 Alc_t2
SPecification B
0 4
4 0
OUtput RS

Phenotypic interaction model, Heath alcohol data
! Female DZ twins
DAta NInput_vars=2 NObservations=101
CM SY
1.077 
0.463 0.962
MATrices
G FUll 2 6 =G(1)
P SYmm 6 6
B FUll 1 2      =b1
I IDentity 2 2
COvariance_model (I-B)~*(G*P*G')*(I-B)~'/
LAbels row P
At1 Ct1 Et1 At2 Ct2 Et2
LAbels col P
At1 Ct1 Et1 At2 Ct2 Et2
MAtrix P
1
0 1
0 0 1
.5 0 0 1
0 1 0 0 1
0 0 0 0 0 1
STart .6 g 1 1 - g(2,6)
STart .1 b 1 2 
BOundaries -.99 .99 4
BOundaries 0 5 1 2 3
OUtput RSiduals




Age correction Sex limitation model MZ FEMALES
DAta NGroups=5 NInput_vars=7 NObservations=43
CM FI=/home/ftp/pub/mx/examples/manual/drunkmzf.cov
SElect 1 2 5 /
MATrices
F FUll 3 7
P SYmm 7 7
MOdel F*P*F' /
SPecification F
1 2 3 0 0 0 4 
0 0 0 3 2 1 4
0 0 0 0 0 0 9
MA P
1
0 1
0 0 1
0 0 0 1
0 1 0 0 1
1 0 0 0 0 1
0 0 0 0 0 0 1
OUtput RSiduals 

Age correction Sex limitation model MZ MALES
DAta NInput_vars=7 NObservations=42
CM FI=/home/ftp/pub/mx/examples/manual/drunkmzm.cov
SElect 1 2 5 /
MATrices
P SYmm 7 7 = P1
M FUll 3 7
MO M*P*M' /
SPecification M
5 6 7 0 0 0 4
0 0 0 7 6 5 4
0 0 0 0 0 0 9
OUtput RSiduals 

Age correction Sex limitation model DZ FEMALES
DAta NObservations=44
CM FI=/home/ftp/pub/mx/examples/manual/drunkdzf.cov
SElect 1 2 5 /
MATrices
F FUll 3 7 =F1
P SYmm 7 7
MOdel F*P*F' /
MA P
1
0 1
0 0 1
0 0 0 1
0 1 0 0 1
.5 0 0 0 0 1
0 0 0 0 0 0 1
OUtput RSiduals 

Age correction Sex limitation model DZ MALES
DAta NObservations=38
CM FI=/home/ftp/pub/mx/examples/manual/drunkdzm.cov
SElect 1 2 5 /
MATrices
M FUll 3 7 =M2
P SYmm 7 7 =P3
MO M*P*M' /
OUtput RS

Age correction Sex limitation model DZ OPP SEX
DAta NObservations=39
CM FI=/home/ftp/pub/mx/examples/manual/drunkdzos.cov
SElect 1 2 5 /
MATrices
P SYmm 7 7
O FUll 3 7 
MO O*P*O' /
SPecification O
1 2 3 0 0 0 4 
0 0 0 7 6 5 4
0 0 0 0 0 0 9
MAtrix P
1
0 1
0 0 1
0 0 0 1
0 1 0 0 1
.5 0 0 0 0 1
0 0 0 0 0 0 1
!FRee P 6 1
!BOundary 0 0.5 P 6 1
BOundary 0 5 1 2 3 5 6 7
STart 2 ALL
STart .1 O 1 7
STart 1 O 3 7
OUtput RS




Simulated twin data.  Raw ML estimation
Data Ni=2 Ngroups=3 No=1000
Raw_data file=/home/ftp/pub/mx/examples/manual/mzasc.dat
Matrices
M Full 1 2 
R Stan 2 2 free
Mean M /
Covariance R /
Matrix M
0 0
Bound -.99 .99 R 1 2
OPtions rs
End

Dummy group to calculate expected cell proportions
Data Ni=2
CTable 2 2
0 0           ! It's full of zeros so it 
0 0           ! contributes zero to the function
Matrices
T Full 2 1
R Stan 2 2 =R1
Thresholds T /
Covaraince R /
Matrix T
1.282
1.282
Options rs
End

Calculate ascertainment correction
Data ni=0
Matrices
I iden 1 1
J izero 1 2
P full 2 2 = %P2
T full 1 1
Compute T*\ln(I-J*P*J') /
Matrix T
2000      ! twice the sample size of group 1
Options User-defined rs
End

Assortative mating: Phillips data, test that d 1 1 is zero
DAta NGroups=1 NInput_vars=12 NObservations=334
CM FI=/home/ftp/pub/mx/examples/manual/asmat.cov
MATrices
h SYmm 6 6 fr
w SYmm 6 6 fr
d FUll 6 6 fr
M2       h | h*d'*w_
     w*d*h | w         /
STart 1. h 1 1 h 2 2 h 3 3 h 4 4 h 5 5 h 6 6
STart 1. w 1 1 w 2 2 w 3 3 w 4 4 w 5 5 w 6 6
FIx  d 1 1
OUtput RSidual
Categorical data analysis.  PACE model
DAta Ninput=2 NGroup=3
CTable 2 2
11 8
8  72
MATrices
A fu 2 6
B fu 2 2
I id 2 2
P SYmm 6 6
T fu 2 1
THresholds T /
COvariances (I-B)~* A*P*A' *(I-B)~ /
SPecification A
1 2 3 0 0 0
0 0 0 1 2 3
STart .6 All
SPecification T
4 4
SPecification B
0 6
6 0
BOundary -.99 .99 6
MAtrix P
1
0 1
0 0 1
1 0 0 1
0 1 0 0 1
0 0 0 0 0 1
OUtput RSidual

Test of categorical data analysis.  DZ twins in ACE model
DAta Ninput=2
CTable 2 2
10 9
9 72
MATrices
A fu 2 6 =A1
B fu 2 2 =B1
I id 2 2 
P SYmm 6 6
T fu 2 1 =T1
THresholds T /
COvariances (I-B)~* A*P*A' *(I-B)~ /
MAtrix P
1
0 1
0 0 1
.5 0 0 1
0 1 0 0 1
0 0 0 0 0 1
OUtput RSidual

Constraint group to ensure a*a + c*c + e*e = 1
DAta COnstraint Ninput=1
MATrices
S FUll 1 3
I IDentity 1 1
COnstraint I - S*S'   /
SPecification S
1 2 3
OUtput NOne






Simple MX example file
DAta NGroups=1 NObservations=150 NInput_vars=2
CM
1.2
.8 1.3
MATrices
A FUll 2 1
D DIag 2 2
MOdel A*A' + D /
SPecification A
1 0
SPecification D
0 3
OUtput RSiduals MU
SPecification 1 A
1 2
OU
Principal components ABA' with constraints
!                    to keep A orthogonal
DAta NGroups=2 NInput_vars=3 NObservations=100
CM SY
   1.
  .6  .9
  .4  .2  .7
MATrices
A FUll 3 3
B DIag 3 3
MOdel A*B*A'/
SPecification A
1 2 3
4 5 6
7 8 9
SPecification B
10 11 12
STart 1.0 A(1,1) A(2,2) A(3,3) B(1,1) to B(3,3)
OUtput LS

Here is the constraint A*A'=I
DAta CON NInput_vars=3
MATrices
A FUll 3 3 = A(1) 
I IDentity 3 3
MOdel A*A'-I/
OUtput LS 
Phenotypic interaction PACE model, Heath 1989 data
! Demonstration of RAM specification and output
DATA NG=2 NInput=2 NObs=171
CM SY
1.280
0.766 1.194
MATRICES
s SY 8 8
i id 8 8
a FU 8 8
f zi 2 8
MODEL F*(I-A)~*S*(I-A)~'*F'/
MA s
1
0 1
0 0 1
1 0 0 1
0 1 0 0 1
0 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
label row a
a1 c1 e1 a2 c2 e2 p1 p2
label col a
a1 c1 e1 a2 c2 e2 p1 p2
label row s
a1 c1 e1 a2 c2 e2 p1 p2
label col s
a1 c1 e1 a2 c2 e2 p1 p2
specify a
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 2 3 0 0 0 0 4  ! This is where the parameters are
0 0 0 1 2 3 4 0
start .6 a 7 1 a 7 3
OU RS draw=mz.ram
Group 2.... DZ twins
DATA NI=2 NO=101
CM SY
1.077 
0.463 0.962
MATRICES
s SY 8 8
i id 8 8
a FU 8 8 = a1
f zi 2 8 
MODEL F*(I-A)~*S*(I-A)~'*F'/
ma s
1
0 1
0 0 1
.5 0 0 1
0 1 0 0 1
0 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
label row s
a1 c1 e1 a2 c2 e2 p1 p2
label col s
a1 c1 e1 a2 c2 e2 p1 p2
BOund -.99 .99 3
BOund 0 5 1 2
OU RS draw=dz.ram
!
! ML fitting to raw data simulated 
! with SAS, whose PROC COR COV gave:
!
! VARIABLE         N         MEAN        STANDARD 
!                                        DEVIATION
! P1            1000     0.00182388     0.98499439
! P2            1000    -0.98608262     1.40083917
! P3            1000     2.05400385     1.79139557
!
! COVARIANCE MATRIX
!                P1       P2       P3
! P1       0.970214 0.506058 0.620529
! P2       0.506058  1.96235 0.807754
! P3       0.620529 0.807754   3.2091
!
! Cholesky for covariance structure
!
ML example, calculation of likelihood for each observation.  
DAta NInput_vars=3 NObservations=1000 NGroups=1
RAw_data FIle=/home/ftp/pub/mx/examples/manual/mlped.raw
MATrices
M FUll  1 3 FRee
S LOwer 3 3 FRee
MEans M /
COvariances S*S' /
MAtrix_start_values S
1  
.6 .8  
.6 .0 .8
OUtput RM 
!
! ML fitting to raw variable length data.  
!
! Cholesky decomposition for the expected covariance matrix
!
! Also matrix expression for means, M1
!  - in this case just a simple vector with free parameters.
!
Variable pedigree size ML example.  
DAta NInput_vars=3 NObservations=1000 NGroups=1
VL FI=/home/ftp/pub/mx/examples/manual/unbalanced.raw
MATrices
M FUll 1 3 FRee
S LOwer 3 3 FRee
M1 M /
M2 S*S' /
STart 1 s 1 1 s 2 2 s 3 3
OUtput RM 

Simple MX example file
DAta NGroups=1 NObservations=150 NInput_variables=2
CMatrix
1.2
.8 1.3
MATRICES
A FUll 2 1
D DIag 2 2
COvariance_model A*A' + D /
SPecification A
1 2
SPecification D
0 3
OUtput
!
! Trivariate Cholesky 'Independent Pathways' model.   
!
! Additive Genetic factors are group 1 matrix H
! Random Environ. factors are group 2 matrix E
! Common Environ. factors are group 3 matrix C
!
! Data are Extraversion, Neuroticism and CESD Depression
!
This group JUST calculates, no fitting is done. 
! Genetic factors
DAta CAlculation NGroups=9
MATrices
H Lower 3 3 FRee
MOdel H*H'  /
OUtput

Title 2nd Group also for calculation 
! Specific Envt.
DAta CAlculation
MATrices
E Lower 3 3 FRee
MOdel E*E'  /
OUtput

Title 3rd Group also for calculation 
! Shared Envt.
DAta CAlculation
MATrices
C Lower 3 3 FRee
MOdel C*C'  /
OUtput

!
! Now get to the actual data, and use the results of 
!  groups 1, 2 and 3 (%E1 %E2 & %E3)
!
Unmatched twins
DAta NInput_vars=3 NObservations=449
CM SY
 .102216
-.021648 .0827805
-.005792 .0177622 .0138577
MAtrices
G SYmm 3 3 = %E1
E SYmm 3 3 = %E2
C SYmm 3 3 = %E3
MOdel (G + E + C) /
OUtput 

MZ twins with cotwins  Extraversion Neuroticism and Depression
DAta NInput_vars=6 no=456
cm
.0887349
-.018651  0.08036
-.001031 .0185154 .0139989
.0366848 -.008858 -.002873 0.097803
.0013777 .0291787 .0110424 -.014907 .0818947
-0.00122 .0103675 .0050655 -.005258 .0172896 0.013687
MAtrices
G SYmm 3 3 = %E1
E SYmm 3 3 = %E2
C SYmm 3 3 = %E3
MOdel
             (G + E + C| G + C _
                  G + C| G + E + C )
/
OUtput RSiduals 

!
!
DZ twins with cotwins  Extraversion Neuroticism and Depression
DAta NInput_vars=6 NObservations=357
CM
0.100808
-0.02249 .0785764
-.003347 .0173292 .0150047
-0.00471 .0025537 -.002045 0.102181
.0032861 .0081708 .0078504 -.023836 .0808707
-.001514 .0053548 .0036505 -.005488 .0164667 .0143083
MATrices 
G SYmm 3 3 = %E1
E SYmm 3 3 = %E2
C SYmm 3 3 = %E3
h DIag 1 1
MOdel 
!
! Covariance matrix bit.  Note H as diag matrix of .5's
! that models DZ genet cov.  By using the Kron operator,
! every element of G is multiplied by .5, in accordance with
! genetic theory that DZ twins share only half their genes.
!
      (G + E + C| H @ G + C _
       H @ G + C| G + E + C )
/
MA Half
.5
STart .5 ALL
BOundary -1 1 ALL
OUtput RSiduals 

Calculation of standardized solution - Additive Genetic
DAta ca
MATrices
A SYmm 3 3 =%e1
P SYmm 3 3 = %E4
MOdel A%P /
OUtput RS

Calculation of standardized solution - Shared Environment
DAta ca
MATrices
C SYmm 3 3 =%e3
P SYmm 3 3 = %E4
MOdel C%P /
OUtput RS

Calculation of standardized solution - Random Environment
DAta ca
MATrices
E SYmm 3 3 =%e2
P SYmm 3 3 = %E4
MOdel E%P /
OUtput RS
!
! Rose Fear data:  Social phobia on twins and parents
!
! Twins and parents: Genetic and cultural transmission model.
!                    P--P assortment
!                    Reduced P-C genetic correlation is 
!                              commented out group 7
!
Group 1 - Assortative mating constraints
DAta CAlculation NGroups=10
MATrices
A FUll 1 3
D STandardized 3 3
M FUll 1 1
COmpute (A*D)'*M*(A*D) /
LAbels Column A
A C E
LAbels Row A
P
SPecification A
1 2 3
MAtrix A
.7 .5 .5
LAbels Row D
A C E
LAbels Col D
A C E
SPecification D
4
0 0
SPecification M
5
OUtput

Group 2 - construct the overall covariance matrix of parents' 
!         latent variables
DAta CAlculate
MATrices
D STan 3 3 = D1
E FUll 3 3 = %E1
COmpute
  (D|E
  _E|D) /
LAbels row E
Ah Ch Eh
LAbels col E
Aw Cw Ew
OUtput

Group 3 - precalculate parents model
DAta CAlculate
MATrices
P SYmmetric 6 6 = %E2
G FUll 4 6
R DIagonal 4 4
COmpute G*P*G' + R/
! For reduced genetic correlation between parent & child model 
! parts commented out with ! are needed
SPecification G
1 2 3 0 0 0
0 0 0 1 2 3
0 0 0 0 0 0 !99 0 0 0 0 0
0 0 0 0 0 0 !0 0 0 99 0 0
LAbels row G
PH PW AH' AW'
LAbels column G
AH CH EH AW CW EW
VAlue 1 G 3 1 G 4 4
! SPecification r
! 0 0 100 100
OUtput

Group 4 - precalculate middle of model
DAta CAlculate
MATrices
P SYmmetric 4 4 = %E3
F FUll 5 4
S SYmmetric 5 5
COmpute F*P*F'+S /
LAbels row F
PH PW AC1 AC2 CT
LAbels col F
PH PW AH' AW'
SPecification F
 0 0 0 0
 0 0 0 0
 0 0 0 0
 0 0 0 0
 6 7 0 0
VAlue 1 F 1 1 F 2 2
VAlue .5 F 3 3  F 3 4  F 4 3  F 4 4
LAbels row S
PH PW AC1 AC2 CT
LAbels col S
PH PW AC1 AC2 CT
SPecification S
0
0 0
0 0 8
0 0 0 8
0 0 0 0 9
STart .5 S 3 3   S 4 4
STart 1 s 5 5
OUtput

G5 - Constraints on genetic environmental variance and 
!    covariance in children
DAta COnstraint NInput=2
MATrices
D STandardized 3 3 = D1  
!Correlation matrix of AH CH EH
H FUll 5 5 = %E4         
!Correlation matrix of PH PW AC1 AC2 CE
Y IZero 2 3              
!Filter matrix to get corr matrix AH CH
Z ZIden 2 5              
!Filter matrix to get corr matrix AC2 CE
COnstraint Y*D*Y' - Z*H*Z' /
OUtput NOne

G6 - Fourth constraint for standardization
DAta COnstraint NInput_vars=1
MATrices
H FUll 5 5 = %E4
Z IZ 1 5
I IDentity 1 1
COnstraint I - Z*H*Z' /
OUtput NOne

Group 7 new constraint for genetic transmission parameter
! The commented out parts of this group are needed if reduced 
! genetic correlation between parent and child model is to be 
! fitted, and the covariance statement should be commented out
DAta CAlculation ni=1
! Change the above to DAta COnstraint ni=1 for Rg model
MATrices
A SYmm 1 1
B SYmm 1 1
I IDen 1 1
COnstraint a /
! Delete the above line for Rg model and uncomment 
! the next five lines:
!constraint (a*a+b)-i /
!SPecification a
!99
!SPecification b
!100
OUtput None

Group 8 - Rose fear data: MZ twins & their parents
DAta NInput=4 NObservations=144
LAbels
Husband Wife MZ_Twin1 MZ_Twin2
CMatrix
.903
.113 1.1053
.2547 .0864 .9467
.1602 .2052 .4938 .8947
MATrices
C FUll 5 5 = %E4
L FUll 4 5
V DIagonal 4 4  !Matrix of standard deviations
E DIagonal 4 4  !Matrix to add random environment for children
COvariance_Model V*(L*C*L'+E*E)*V'/
SPecification V
10 10 10 10
MAtrix V
1 1 1 1
LAbels row V
PH PW PT1 PT2
LAbels column V
PH PW PT1 PT2
LAbels row E
Dummy Dummy Et1 Et2
LAbels column E
Dummy Dummy Et1 Et2
SPecification E
0 0 3 3
BOundary 1 5 v 1 1 v 2 2 v 3 3 V 4 4
LAbels row L
Ph Pw Pmzt1 Pmzt2
LAbels column L
Ph Pw At1 At2 Ct
SPecification L
0 0 0 0 0
0 0 0 0 0
0 0 1 0 2
0 0 1 0 2
VAlue 1 L 1 1 L 2 2
OUtput RSiduals

Group 9 - Rose social criticism DZ  twins & their parents
DAta NInput_vars=4 NObservations=106
LAbels
Husband Wife DZ_Twin1 DZ_Twin2
CM
1.0811
.2111 .9063
.1443 .2398 1.2079
.1001 .1551 .3246 1.0318
MATrices
C FUll 5 5 = %E4
L FUll 4 5
V DIag 4 4 = V8
E DIag 4 4 = E8
COvariances V*(L*C*L'+E*E)*V'/
SPecification L
0 0 0 0 0
0 0 0 0 0
0 0 1 0 2
0 0 0 1 2
VAlue 1 L 1 1 L 2 2
LAbels row E
Dummy Dummy Et1 Et2
LAbels column E
Dummy Dummy Et1 Et2
LAbels row L
Ph Pw Pdzt1 Pdzt2
LAbels column L
Ph Pw At1 At2 Ct
STart 1.5 v 1 1 v 2 2 v 3 3 V 4 4
BOundary .5 2 v 1 1 v 2 2 v 3 3 V 4 4
OUtput RSiduals IT=500

Group 10 - summary printout of parameter estimates
DAta CAlculation
MATrices
P FUll 1 10
COmpute P/
SPecification P
1 2 3 4 5 6 7 8 9 10
LAbels c p
a c e s mu zf zm res(a) res(c) vp
BOundary -.99 .99 4 5 6 7
BOundary 0 1 1 2 3 8 9
OUtput MUltiple
!
! Re-fit the model with Father-child and Mother-child 
!  cultural transmission set equal
!
EQuate f 4 5 1 f 4 5 2
OUtput
!
! User defined fit function - least squares
!
User defined function to fit to a correlation matrix 
!                     by least squares
DAta NInput=3 NGroupies=1
CMatrix SYmm
1
.2 1
.3 .4 1
MATrices
A SYmm 3 3 = %O1
B STan 3 3 FRee
MOdel \tr((a-b)*(a-b))/
OUtput USer RSidjools
