* Stop syntax and large titles from printing to output.
SET PRINTBACK=NONE HEADER=NO.

***********************************************************************************.
*******   BEGIN DATA MANIPULATION OF CHAPTER 10b THREE-LEVEL TIME EXAMPLE   *******.
*******               CHANGE "filesave" to your directory                   *******.
***********************************************************************************.

* Define location of files used in code below.
FILE HANDLE filesave /NAME = "C:\Dropbox\PilesOfVariance\Chapter10b\SPSS".

* Import chapter 10b stacked data. 
GET FILE = "filesave/SPSS_Chapter10b.sav".
DATASET NAME Chapter10b WINDOW=FRONT.

* Piecewise slopes for session.
RECODE session (1=-1) (2 THRU HI=0) INTO slope12.
RECODE session (LO THRU 2=0) (3=1) (4=2) (5=3) (6=4) INTO slope26.
* Linear slope for burst.
COMPUTE burst1 = burst-1.
* Difference between early and later bursts.
DOIF (burst=1) OR (burst=2).
COMPUTE b1or2=1.
END IF.
DOIF (burst>2).
COMPUTE b1or2=0.
END IF.
VARIABLE LABELS
slope12 "slope12: Initial Slope (Session 1-2)"
slope26 "slope26: Later Slope (Session 2-6)"
burst1 "burst1: Measurement Burst (0=1)"
b1or2 "b1or2: Is Burst 1 or 2 (0=no, 1=yes)".

* Subset sample to complete cases for all predictors.
SELECT IF NVALID(session, burst)=2.
EXECUTE.

* Creating 'fake people' to show predicted session*burst means.
* Each row is a fake person for which to create a predicted outcome.
DATA LIST FREE /
PersonID burst burst1 b1or2 session slope12 slope26.
BEGIN DATA.
-99 1 0 1 1 -1 0
-99 1 0 1 2  0 0
-99 1 0 1 3  0 1
-99 1 0 1 4  0 2
-99 1 0 1 5  0 3
-99 1 0 1 6  0 4
-99 2 1 1 1 -1 0
-99 2 1 1 2  0 0
-99 2 1 1 3  0 1
-99 2 1 1 4  0 2
-99 2 1 1 5  0 3
-99 2 1 1 6  0 4
-99 3 2 0 1 -1 0
-99 3 2 0 2  0 0
-99 3 2 0 3  0 1
-99 3 2 0 4  0 2
-99 3 2 0 5  0 3
-99 3 2 0 6  0 4
-99 4 3 0 1 -1 0
-99 4 3 0 2  0 0
-99 4 3 0 3  0 1
-99 4 3 0 4  0 2
-99 4 3 0 5  0 3
-99 4 3 0 6  0 4
-99 5 4 0 1 -1 0
-99 5 4 0 2  0 0
-99 5 4 0 3  0 1
-99 5 4 0 4  0 2
-99 5 4 0 5  0 3
-99 5 4 0 6  0 4
END DATA.
DATASET NAME FakeMeans.
* Merge with real data.
ADD FILES FILE=Chapter10b /FILE=FakeMeans.
DATASET NAME PlotMeans.
SORT CASES BY PersonID.
DATASET CLOSE FakeMeans.

***********************************************************************************.
*******              BEGIN CHAPTER 10b THREE-LEVEL TIME MODELS              *******.
***********************************************************************************.

* Open output directory.
OUTPUT NAME SPSS_Chapter10b_Output.

DATASET ACTIVATE Chapter10b.
ECHO "Chapter 10b: Descriptive Statistics for Time-Varying Variables".
SUMMARIZE
   /TABLES = symptoms posaff
   /FORMAT = NOLIST TOTAL 
   /CELLS  = COUNT MEAN STDDEV MIN MAX.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Empty Means, Single-Level Model for the Variance for Symptoms;'.
ECHO 'Independent Observations'.
MIXED symptoms BY PersonID burst
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    =
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Empty Means, Two-Level Model for the Variance for Symptoms;'.
ECHO 'Sessions Within Burst*Persons'.
MIXED symptoms BY PersonID burst
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    =
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.5: Empty Means, Three-Level Model for the Variance for Symptoms;'.
ECHO 'Level-1 Sessions Within Level-2 Bursts Within Level-3 Persons'.
MIXED symptoms BY PersonID burst
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    =
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Empty Means, Single-Level Model for the Variance for Positive Affect;'.
ECHO 'Independent Observations'.
MIXED posaff BY PersonID burst
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    =
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Empty Means, Two-Level Model for the Variance for Positive Affect;'.
ECHO 'Sessions Within Burst*Persons'.
MIXED posaff BY PersonID burst
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    =
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.5: Empty Means, Three-Level Model for the Variance for Positive Affect;'.
ECHO 'Level-1 Sessions Within Level-2 Bursts Within Level-3 Persons'.
MIXED posaff BY PersonID burst
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    =
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.6: Saturated Means for Burst by Session;'.
ECHO 'Three-Level Model for the Variance for Symptoms'.
MIXED symptoms BY PersonID burst session
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    = session burst session*burst
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
     /EMMEANS  = TABLES(session)
     /EMMEANS  = TABLES(burst)
     /EMMEANS  = TABLES(session*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.7: Piecewise Session Slopes by Observed Burst;'.
ECHO 'Three-Level Model for the Variance for Symptoms'.
MIXED symptoms BY PersonID burst WITH slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    = slope12 slope26 burst
                 slope12*burst slope26*burst
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
     /TEST     = 'Session 2 at Burst 1'     intercept 1 burst 1 0 0 0 0
     /TEST     = 'Session 2 at Burst 2'     intercept 1 burst 0 1 0 0 0
     /TEST     = 'Session 2 at Burst 3'     intercept 1 burst 0 0 1 0 0
     /TEST     = 'Session 2 at Burst 4'     intercept 1 burst 0 0 0 1 0
     /TEST     = 'Session 2 at Burst 5'     intercept 1 burst 0 0 0 0 1
     /TEST     = 'Slope12 at Burst 1'       slope12 1 slope12*burst 1 0 0 0 0
     /TEST     = 'Slope12 at Burst 2'       slope12 1 slope12*burst 0 1 0 0 0
     /TEST     = 'Slope12 at Burst 3'       slope12 1 slope12*burst 0 0 1 0 0
     /TEST     = 'Slope12 at Burst 4'       slope12 1 slope12*burst 0 0 0 1 0
     /TEST     = 'Slope12 at Burst 5'       slope12 1 slope12*burst 0 0 0 0 1
     /TEST     = 'Slope26 at Burst 1'       slope26 1 slope26*burst 1 0 0 0 0
     /TEST     = 'Slope26 at Burst 2'       slope26 1 slope26*burst 0 1 0 0 0
     /TEST     = 'Slope26 at Burst 3'       slope26 1 slope26*burst 0 0 1 0 0
     /TEST     = 'Slope26 at Burst 4'       slope26 1 slope26*burst 0 0 0 1 0
     /TEST     = 'Slope26 at Burst 5'       slope26 1 slope26*burst 0 0 0 0 1
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.8: Piecewise Session Slopes by Quadratic Burst;'.
ECHO 'Three-Level Model for the Variance for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope12*burst1 slope26*burst1
                 slope12*burst1*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
     /TEST     = 'Slope12 at Burst 1'       slope12 1 slope12*burst1 0 slope12*burst1*burst1 0
     /TEST     = 'Slope12 at Burst 2'       slope12 1 slope12*burst1 1 slope12*burst1*burst1 1
     /TEST     = 'Slope12 at Burst 3'       slope12 1 slope12*burst1 2 slope12*burst1*burst1 4
     /TEST     = 'Slope12 at Burst 4'       slope12 1 slope12*burst1 3 slope12*burst1*burst1 9
     /TEST     = 'Slope12 at Burst 5'       slope12 1 slope12*burst1 4 slope12*burst1*burst1 16
     /TEST     = 'Slope26 at Burst 1'       slope26 1 slope26*burst1 0 slope26*burst1*burst1 0
     /TEST     = 'Slope26 at Burst 2'       slope26 1 slope26*burst1 1 slope26*burst1*burst1 1
     /TEST     = 'Slope26 at Burst 3'       slope26 1 slope26*burst1 2 slope26*burst1*burst1 4
     /TEST     = 'Slope26 at Burst 4'       slope26 1 slope26*burst1 3 slope26*burst1*burst1 9
     /TEST     = 'Slope26 at Burst 5'       slope26 1 slope26*burst1 4 slope26*burst1*burst1 16
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.9: Piecewise Session Slopes (Slope26 by Quadratic Burst Only);'.
ECHO 'Three-Level Model for the Variance for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
     /TEST     = 'Slope26 at Burst 1'       slope26 1 slope26*burst1 0 slope26*burst1*burst1 0
     /TEST     = 'Slope26 at Burst 2'       slope26 1 slope26*burst1 1 slope26*burst1*burst1 1
     /TEST     = 'Slope26 at Burst 3'       slope26 1 slope26*burst1 2 slope26*burst1*burst1 4
     /TEST     = 'Slope26 at Burst 4'       slope26 1 slope26*burst1 3 slope26*burst1*burst1 9
     /TEST     = 'Slope26 at Burst 5'       slope26 1 slope26*burst1 4 slope26*burst1*burst1 16
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes (Slope26 by Quadratic Burst Only);'.
ECHO 'Add Random Linear Burst Slope across Persons for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT burst1 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes (Slope26 by Quadratic Burst Only);'.
ECHO 'Add Random Quadratic Burst Slope across Persons for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT burst1 burst1*burst1 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes (Slope26 by Quadratic Burst Only);'.
ECHO 'Add Random Linear Slope12 Across Bursts for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT burst1 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes (Slope26 by Quadratic Burst Only);'.
ECHO 'Add Random Linear Slope12 Across Persons for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT burst1 slope12 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 | COVTYPE(UN) SUBJECT(PersonID*burst)
.

* Estimate model on data with fake people to make predictions.
DATASET ACTIVATE PlotMeans WINDOW=FRONT.
ECHO 'Eq 10b.10: Piecewise Session Slopes (Slope26 by Quadratic Burst Only);'.
ECHO 'Add Random Linear Slope26 Across Bursts for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT burst1 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 slope26 | COVTYPE(UN) SUBJECT(PersonID*burst)
     /TEST     = 'Slope26 at Burst 1'       slope26 1 slope26*burst1 0 slope26*burst1*burst1 0
     /TEST     = 'Slope26 at Burst 2'       slope26 1 slope26*burst1 1 slope26*burst1*burst1 1
     /TEST     = 'Slope26 at Burst 3'       slope26 1 slope26*burst1 2 slope26*burst1*burst1 4
     /TEST     = 'Slope26 at Burst 4'       slope26 1 slope26*burst1 3 slope26*burst1*burst1 9
     /TEST     = 'Slope26 at Burst 5'       slope26 1 slope26*burst1 4 slope26*burst1*burst1 16
     /SAVE     = FIXPRED(PredUncS)
.
CORRELATIONS symptoms PredUncS.
ECHO 'Predicted Outcomes for Fake People will be in PlotMeans dataset'.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes (Slope26 by Quadratic Burst Only);'.
ECHO 'Add Random Linear Slope26 Across Persons for Symptoms'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT burst1 slope26 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 slope26 | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Final Unconditional Model for Symptoms;'.
ECHO 'Remove Level-2 Random Effects Variances and Covariances'.
MIXED symptoms BY PersonID burst WITH burst1 slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 burst1*burst1 slope12 slope26
                 slope26*burst1 slope26*burst1*burst1
     /RANDOM   = INTERCEPT burst1 | COVTYPE(UN) SUBJECT(PersonID)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.6: Saturated Means for Burst by Session;'.
ECHO 'Three-Level Model for the Variance for Positive Affect'.
MIXED posaff BY PersonID burst session
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    = session burst session*burst
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
     /EMMEANS  = TABLES(session)
     /EMMEANS  = TABLES(burst)
     /EMMEANS  = TABLES(session*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.7: Piecewise Session Slopes by Observed Burst;'.
ECHO 'Three-Level Model for the Variance for Positive Affect'.
MIXED posaff BY PersonID burst WITH slope12 slope26
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    = slope12 slope26 burst
                 slope12*burst slope26*burst
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
     /TEST     = 'Session 2 at Burst 1'     intercept 1 burst 1 0 0 0 0
     /TEST     = 'Session 2 at Burst 2'     intercept 1 burst 0 1 0 0 0
     /TEST     = 'Session 2 at Burst 3'     intercept 1 burst 0 0 1 0 0
     /TEST     = 'Session 2 at Burst 4'     intercept 1 burst 0 0 0 1 0
     /TEST     = 'Session 2 at Burst 5'     intercept 1 burst 0 0 0 0 1
     /TEST     = 'Slope12 at Burst 1'       slope12 1 slope12*burst 1 0 0 0 0
     /TEST     = 'Slope12 at Burst 2'       slope12 1 slope12*burst 0 1 0 0 0
     /TEST     = 'Slope12 at Burst 3'       slope12 1 slope12*burst 0 0 1 0 0
     /TEST     = 'Slope12 at Burst 4'       slope12 1 slope12*burst 0 0 0 1 0
     /TEST     = 'Slope12 at Burst 5'       slope12 1 slope12*burst 0 0 0 0 1
     /TEST     = 'Slope26 at Burst 1'       slope26 1 slope26*burst 1 0 0 0 0
     /TEST     = 'Slope26 at Burst 2'       slope26 1 slope26*burst 0 1 0 0 0
     /TEST     = 'Slope26 at Burst 3'       slope26 1 slope26*burst 0 0 1 0 0
     /TEST     = 'Slope26 at Burst 4'       slope26 1 slope26*burst 0 0 0 1 0
     /TEST     = 'Slope26 at Burst 5'       slope26 1 slope26*burst 0 0 0 0 1
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Eq 10b.11and12: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Three-Level Model for the Variance for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
     /TEST     = 'Slope12 at Burst 1 or 2' slope12 1 slope12*b1or2 1
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Add Random Linear Burst across Persons for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT burst1 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Add Fixed and Random Quadratic Burst across Persons for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
                 burst1*burst1
     /RANDOM   = INTERCEPT burst1 burst1*burst1 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Add Random Linear Slope12 across Bursts for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT burst1 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Add Random Linear Slope12 across Persons for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT burst1 slope12 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Add Random Slope12*b1or2 across Persons for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT burst1 slope12 slope12*b1or2 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 | COVTYPE(UN) SUBJECT(PersonID*burst)
.

DATASET ACTIVATE Chapter10b WINDOW=FRONT.
ECHO 'Ch 10b: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Add Random Linear Slope26 across Bursts for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT burst1 slope12 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 slope26 | COVTYPE(UN) SUBJECT(PersonID*burst)
.

* Estimate model on data with fake people to make predictions.
DATASET ACTIVATE PlotMeans WINDOW=FRONT.
ECHO 'Eq 10b.13: Piecewise Session Slopes, Linear Burst, Slope12 by Burst1or2;'.
ECHO 'Add Random Linear Slope26 across Persons for Positive Affect'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT burst1 slope12 slope26 | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = INTERCEPT slope12 slope26 | COVTYPE(UN) SUBJECT(PersonID*burst)
     /TEST     = 'Slope12 at Burst 1 or 2' slope12 1 slope12*b1or2 1
     /SAVE     = FIXPRED(PredUncP)
.
CORRELATIONS posaff PredUncP.
ECHO 'Predicted Outcomes for Fake People will be in PlotMeans dataset'.

DATASET ACTIVATE PlotMeans WINDOW=FRONT.
ECHO 'Ch 10b: Final Unconditional Model for Positive Affect;'.
ECHO 'Removing Level-2 Random Effects Variances and Covariances'.
MIXED posaff BY PersonID burst WITH burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G
     /FIXED    = burst1 slope12 slope12*b1or2 slope26
     /RANDOM   = INTERCEPT burst1 slope12 slope26 | COVTYPE(UN) SUBJECT(PersonID)
.

* Create double-stacked dataset for multivariate analysis.
DATASET ACTIVATE Chapter10b.
DATASET COPY Chapter10bdoublestacked.
DATASET ACTIVATE Chapter10bdoublestacked.
VARSTOCASES
   /MAKE outcome FROM symptoms posaff
   /INDEX = DV (2)
   /KEEP = ALL.
VALUE LABELS DV 1 "1symptoms" 2 "2posaff".
* Create dummy codes to serve as intercepts.
DOIF (DV=1).
COMPUTE dvS=1.
COMPUTE dvP=0.
END IF.
DOIF (DV=2).
COMPUTE dvS=0.
COMPUTE dvP=1.
END IF.
VARIABLE LABELS
DV "DV: Categorical indicator for which DV the row is for"
dvS "dvS: Intercept 1=symptoms, 0=posaff"
dvP "dvP: Intercept 0=symptoms, 1=posaff"
outcome "outcome: Combined outcome variable column".
EXECUTE.

DATASET ACTIVATE Chapter10bdoublestacked WINDOW=FRONT.
ECHO 'Ch 10b: Multivariate Model of Symptoms and Positive Affect;'.
ECHO 'Tricking Univariate Software'.
MIXED outcome BY PersonID burst session DV WITH dvS dvP burst1 slope12 slope26 b1or2
     /METHOD   = ML
     /PRINT    = SOLUTION TESTCOV G R
     /FIXED    = dvS dvP dvS*burst1 dvS*burst1*burst1 dvS*slope12 dvS*slope26
                 dvS*slope26*burst1 dvS*slope26*burst1*burst1
                 dvP*burst1 dvP*slope12 dvP*slope12*b1or2 dvP*slope26 | NOINT
     /RANDOM   = dvS dvS*burst1 dvP dvP*burst1 dvP*slope12 dvP*slope26   | COVTYPE(UN) SUBJECT(PersonID)
     /RANDOM   = dvS dvS*slope12 dvS*slope26 dvP dvP*slope12 dvP*slope26 | COVTYPE(UN) SUBJECT(PersonID*burst)
     /REPEATED = DV | COVTYPE(UN) SUBJECT(PersonID*burst*session)
.

****** END CHAPTER 10b MODELS ******.

* Close output directory.
OUTPUT EXPORT NAME=SPSS_Chapter10b_Output
     /CONTENTS EXPORT=VISIBLE LAYERS=VISIBLE MODELVIEWS=VISIBLE
     /HTML DOCUMENTFILE='C:\Dropbox\PilesOfVariance\Chapter10b\SPSS\SPSS_Chapter10b_Output.html'
           IMAGEFORMAT=PNG STYLING=YES.