TITLE:
  Ch 5: Random Linear Time Model with AR1 R Matrix

DATA:
  FILE = MPLUS_Chapter5.csv;
  FORMAT = FREE;

DATA LONGTOWIDE:
  LONG = outcome;
  WIDE = outcome1-outcome4;
  IDVARIABLE = PersonID;
  REPETITION = wave (1-4);

DEFINE:
  time = wave - 1;

VARIABLE:
  NAMES = PersonID wave outcome;
  USEVARIABLES = outcome1-outcome4;
  MISSING = ALL (-9999);

ANALYSIS:
  ESTIMATOR = ML;
  PROCESSORS = 4;

OUTPUT:
  STDYX;

MODEL:
  RandInt BY outcome1-outcome4@1;
  RandLin BY outcome1@0 outcome2@1 outcome3@2 outcome4@3;
  RandInt RandLin (intvar linvar);
  RandInt WITH RandLin;
  [outcome1-outcome4@0 RandInt RandLin];
  outcome1-outcome4 (resvar);
  outcome1-outcome3 PWITH outcome2-outcome4 (rescov1-rescov3);
  outcome1-outcome2 PWITH outcome3-outcome4 (rescov4-rescov5);
  outcome1          PWITH outcome4          (rescov6)        ;

MODEL CONSTRAINT:
  NEW(AR1cor);
! Residual Covariance for Outcome 1 and 2
  rescov1 = AR1cor**1*SQRT(resvar)*SQRT(resvar);
! Residual Covariance for Outcome 2 and 3
  rescov2 = AR1cor**1*SQRT(resvar)*SQRT(resvar);
! Residual Covariance for Outcome 3 and 4
  rescov3 = AR1cor**1*SQRT(resvar)*SQRT(resvar);
! Residual Covariance for Outcome 1 and 3
  rescov4 = AR1cor**2*SQRT(resvar)*SQRT(resvar);
! Residual Covariance for Outcome 2 and 4
  rescov5 = AR1cor**2*SQRT(resvar)*SQRT(resvar);
! Residual Covariance for Outcome 1 and 4
  rescov6 = AR1cor**3*SQRT(resvar)*SQRT(resvar);