* Prevent "more" messages from appearing
set more off 
* Control line length
set linesize 150 
 
********************************************************************************
*******       BEGIN DATA MANIPULATION OF CHAPTER 7b CHANGE EXAMPLE       *******
*******               CHANGE "filesave" to your directory                *******
********************************************************************************
 
* Defining global variable for file location to be replaced in code below
global filesave "C:\Dropbox\PilesOfVariance\Chapter7b\STATA" 
 
* Import and stack chapter7b multivariate data
* List time-varying variables first, i(level2ID) j(newtimeID)
use "$filesave\STATA_Chapter7b.dta", clear 
reshape long age risky monitor, i(personid) j(occasion) 
label variable occasion "occasion: Occasion of Measurement (12-18)" 
label variable age "age: Exact Age at Occasion" 
label variable risky "risky: Risky Behavior at Occasion" 
label variable monitor "monitor: Monitoring at Occasion" 
 
* Center predictors for analysis in stacked data
gen agec18 = age - 18 
gen agec18sq = agec18 * agec18 
gen att4 = attitude12 - 4 
label variable agec18 "agec18: Exact Age (0=18)" 
label variable agec18sq "agec18sq: Exact Quadratic Age (0=18)" 
label variable att4 "att4: Age 12 Attitudes (0=4)" 
 
* Subset sample to complete cases for all eventual predictors in chapter 9
egen nummiss = rowmiss(agec18 att4 risky monitor) 
drop if nummiss>0 
 
********************************************************************************
*******                   BEGIN CHAPTER 7b CHANGE MODELS                 *******
********************************************************************************
 
* Save results to separate file
log using $filesave\STATA_Chapter7b_Output, replace name(STATA_Chapter7b) 
 
display as result "Chapter 7b: Descriptive Statistics for Time-Invariant Variables" 
preserve  
collapse attitude12, by(personid) 
summarize attitude12 
restore  
 
display as result "Chapter 7b: Descriptive Statistics for Time-Varying Variables" 
summarize age risky 
 
display as result "Ch 7b: Empty Means, Random Intercept Model" 
mixed risky , ///
              || personid: , variance mle covariance(unstructured), 
      estat ic, n(200), 
      estat icc, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
 
display as result "Ch 7b: Saturated Means by Rounded Occasion, Unstructured Variance Model" 
mixed risky i.occasion, ///
              || personid: , noconstant variance mle covariance(unstructured) ///
              residuals(unstructured,t(occasion)), 
      estat ic, n(200), 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
      contrast i.occasion,  
      margins  i.occasion,  
      margins  i.occasion, pwcompare(pveffects) 
 
display as result "Ch 7b: Fixed Linear Age, Random Intercept Model" 
mixed risky c.agec18, ///
              || personid: , variance mle covariance(unstructured), 
      estat ic, n(200), 
      * Intercept at Age=12
      lincom _cons*1 + c.agec18*-6 
      * Intercept at Age=13
      lincom _cons*1 + c.agec18*-5 
      * Intercept at Age=14
      lincom _cons*1 + c.agec18*-4 
      * Intercept at Age=15
      lincom _cons*1 + c.agec18*-3 
      * Intercept at Age=16
      lincom _cons*1 + c.agec18*-2 
      * Intercept at Age=17
      lincom _cons*1 + c.agec18*-1 
      * Intercept at Age=18
      lincom _cons*1 + c.agec18*0 
      estimates store FitFixLin, 
 
display as result "Ch 7b: Random Linear Age Model" 
mixed risky c.agec18, ///
              || personid: agec18, variance mle covariance(unstructured), 
      estat ic, n(200), 
      * Intercept at Age=12
      lincom _cons*1 + c.agec18*-6 
      * Intercept at Age=13
      lincom _cons*1 + c.agec18*-5 
      * Intercept at Age=14
      lincom _cons*1 + c.agec18*-4 
      * Intercept at Age=15
      lincom _cons*1 + c.agec18*-3 
      * Intercept at Age=16
      lincom _cons*1 + c.agec18*-2 
      * Intercept at Age=17
      lincom _cons*1 + c.agec18*-1 
      * Intercept at Age=18
      lincom _cons*1 + c.agec18*0 
      estimates store FitRandLin, 
      lrtest FitRandLin FitFixLin,  
 
display as result "Eq 7b.8: Fixed Quadratic, Random Linear Age Model" 
mixed risky c.agec18 c.agec18#c.agec18, ///
              || personid: agec18, variance mle covariance(unstructured), 
      estat ic, n(200), 
      * Intercept at Age=12
      lincom _cons*1 + c.agec18*-6 + c.agec18#c.agec18*36 
      * Intercept at Age=13
      lincom _cons*1 + c.agec18*-5 + c.agec18#c.agec18*25 
      * Intercept at Age=14
      lincom _cons*1 + c.agec18*-4 + c.agec18#c.agec18*16 
      * Intercept at Age=15
      lincom _cons*1 + c.agec18*-3 + c.agec18#c.agec18*9 
      * Intercept at Age=16
      lincom _cons*1 + c.agec18*-2 + c.agec18#c.agec18*4 
      * Intercept at Age=17
      lincom _cons*1 + c.agec18*-1 + c.agec18#c.agec18*1 
      * Intercept at Age=18
      lincom _cons*1 + c.agec18*0  + c.agec18#c.agec18*0 
      * Linear Slope at Age=12
      lincom c.agec18*1 + c.agec18#c.agec18*-12 
      * Linear Slope at Age=13
      lincom c.agec18*1 + c.agec18#c.agec18*-10 
      * Linear Slope at Age=14
      lincom c.agec18*1 + c.agec18#c.agec18*-8 
      * Linear Slope at Age=15
      lincom c.agec18*1 + c.agec18#c.agec18*-6 
      * Linear Slope at Age=16
      lincom c.agec18*1 + c.agec18#c.agec18*-4 
      * Linear Slope at Age=17
      lincom c.agec18*1 + c.agec18#c.agec18*-2 
      * Linear Slope at Age=18
      lincom c.agec18*1 + c.agec18#c.agec18*0 
      estimates store FitFixQuad, 
      predict PredAge, xb, 
corr risky PredAge 
 
display as result "Ch 7b: Random Quadratic Age Model" 
mixed risky c.agec18 c.agec18#c.agec18, ///
              || personid: agec18 agec18sq, variance mle covariance(unstructured), 
      estat ic, n(200), 
      * Intercept at Age=12
      lincom _cons*1 + c.agec18*-6 + c.agec18#c.agec18*36 
      * Intercept at Age=13
      lincom _cons*1 + c.agec18*-5 + c.agec18#c.agec18*25 
      * Intercept at Age=14
      lincom _cons*1 + c.agec18*-4 + c.agec18#c.agec18*16 
      * Intercept at Age=15
      lincom _cons*1 + c.agec18*-3 + c.agec18#c.agec18*9 
      * Intercept at Age=16
      lincom _cons*1 + c.agec18*-2 + c.agec18#c.agec18*4 
      * Intercept at Age=17
      lincom _cons*1 + c.agec18*-1 + c.agec18#c.agec18*1 
      * Intercept at Age=18
      lincom _cons*1 + c.agec18*0  + c.agec18#c.agec18*0 
      * Linear Slope at Age=12
      lincom c.agec18*1 + c.agec18#c.agec18*-12 
      * Linear Slope at Age=13
      lincom c.agec18*1 + c.agec18#c.agec18*-10 
      * Linear Slope at Age=14
      lincom c.agec18*1 + c.agec18#c.agec18*-8 
      * Linear Slope at Age=15
      lincom c.agec18*1 + c.agec18#c.agec18*-6 
      * Linear Slope at Age=16
      lincom c.agec18*1 + c.agec18#c.agec18*-4 
      * Linear Slope at Age=17
      lincom c.agec18*1 + c.agec18#c.agec18*-2 
      * Linear Slope at Age=18
      lincom c.agec18*1 + c.agec18#c.agec18*0 
      estimates store FitRandQuad, 
      lrtest FitRandQuad FitFixQuad,  
 
display as result "Ch 7b: Fixed Cubic, Random Linear Age Model" 
mixed risky c.agec18 c.agec18#c.agec18 c.agec18#c.agec18#c.agec18, ///
              || personid: agec18, variance mle covariance(unstructured), 
      estat ic, n(200), 
 
display as result "Ch 7b: Fixed Quadratic, Random Linear Age Model" 
display as result "Attitudes Predicting Intercept" 
mixed risky c.agec18 c.agec18#c.agec18 ///
            c.att4, ///
              || personid: agec18, variance mle covariance(unstructured), 
      estat ic, n(200), 
      predict PredAttInt, xb, 
corr risky PredAttInt 
 
display as result "Ch 7b: Fixed Quadratic, Random Linear Age Model" 
display as result "Attitudes Predicting Linear Age Slope" 
mixed risky c.agec18 c.agec18#c.agec18 ///
            c.att4 c.agec18#c.att4, ///
              || personid: agec18, variance mle covariance(unstructured), 
      estat ic, n(200), 
      predict PredAttLin, xb, 
      margins, at (c.agec18=(-6(1)0) c.att4=(-2(3)1)) vsquish, 
corr risky PredAttLin 
 
display as result "Eq 7b.9: Fixed Quadratic, Random Linear Age Model" 
display as result "Attitudes Predicting Quadratic Age Slope" 
mixed risky c.agec18 c.agec18#c.agec18 ///
            c.att4 c.agec18#c.att4 c.agec18#c.agec18#c.att4, ///
              || personid: agec18, variance mle covariance(unstructured), 
      estat ic, n(200), 
      predict PredAttQuad, xb, 
corr risky PredAttQuad 
  
****** END CHAPTER 7b MODELS ******
 
* Close log
log close STATA_Chapter7b 
* Convert log to html using custom downloaded package
log2html $filesave\STATA_Chapter7b_Output, replace