!------------------------------------------------------+
! Latent class analysis via Mx                         |
! marginal ml again                                    |
! data from http://www.people.vcu.edu/~nhenry/LSA50.htm|
! see                                                  |
! Latent Class Probs Item 1 Item 2 Item 3 Item 4       |
! .4243              .9240 .6276 .5704 .5125           |
! .5757              .4324 .1871 .1008 .0635           |
! Mx recovers:                                         |
! .4440    0.90736    0.61444    0.55828    0.50176    |
! .5560    0.42838    0.18218    .09394      .05627    |
!------------------------------------------------------+

#ngroups  2    ! number of groups in run
#define $nvar 5  ! Number of variables altogether, before selection
#define nclass 2
#define $nclass 2
#define nv 4    ! number of variables in model
#define ncov 0   ! number of covariates
#define nclassncov 0 ! nclass * ncov
#define ncnv 8  ! number of variables * number of classes
#define $allvar1 Armyrun Favatt squaredeal welfare freq
#define $covariates FHTTLPR1 FHTTLPR2
#define $variables Armyrun Favatt squaredeal welfare freq
#define $frequencies TRUE
#define $freqlabel freq
#define maxcat 1  ! Maximum score of any item
#define maxcatnc 2  ! Must be maxcat times nclass
!-------------------------------------------------------------------
Group 1: Fit the model
 Data Ninput=$nvar  Nmodel=$nclass
 ORdinal File=lazarsfeld.ord
 LAbels 
$allvar1 

!Select if sex = 0
 SElect 
#if ncov > 0
 $variables
 $covariates ;
 Definition $covariates ;
#else
 $variables  ;
#endif
#if $frequencies = TRUE
 Definition $freqlabel  ;
#end if
 
  Begin matrices;
    A full nclass 1
 #if ncov > 0
    P unit nv ncov
    K Full nv nclassncov  ! regressions of response probabilities on covariates 
    S full ncov 1   ! vector of observed covariates
    X full nclass ncov    ! For regression of class membership probs on covariates
 #else
    P unit nv 1
    K Full nv nclass     ! Not required except to make algebra work with no covariates
    D full 1 1      ! Not required except to make algebra work
    S full 1 1      ! Not required except to make algebra work
    X full nclass 1    ! For regression of class membership probs on covariates
 #endif
    E full 1 nv
    F unit maxcat 1
    G lower maxcat maxcat
    T full maxcatnc nv Free  ! thresholds, z-score metric for class probabilities
                             ! note that columns are thresholds within variables
    I iden nclass nclass 
    Q full 1 1               ! for frequency if used
    R iden nv nv
    U unit 1 1
    V unit nv 1
    W full nclass 1 free  ! class membership probabilities
  End Matrices;
 !
 ! Be kind to Mx, fix thresholds that are not going anywhere
 !
  Value 1 G 1 1 - G maxcat maxcat
 #if ncov > 0
  Specify S $covariates
 #endif
 #if $frequencies = TRUE
  Specify Q $freqlabel
 #end if
  Matrix W .2 .8

  Begin Algebra;
  End Algebra;

  Thresholds  (I@G)*T+(K*(I@S))' ;
  Covariance  R;
 #if $frequencies = TRUE
   Frequency Q;
 #end if
 #if ncov > 0
   Specify S $covariates
  #endif
  #if $frequencies = TRUE
   Specify Q $freqlabel
  #end if
   Matrix W .2 .8

   Begin Algebra;
   End Algebra;

   Thresholds  (I@G)*T+(K*(I@S))' ;
   Covariance  R;
  #if $frequencies = TRUE
    Frequency Q;
  #end if
  #if ncov > 0
   Weight      (W+X*S)@(\sum(W+X*S)~);  ! adjusted for covariates
  #else
   Weight      W ;
  #endif
   Option onecov
   Option func=1.e-9
  End Group;

  Constrain Un-regressed Weights to sum to 1
   Constraint
   Begin Matrices;
    W full nclass 1 = W1
    I unit 1 1
   End Matrices;

   Constraint I = \sum(W);
  End

  