* Prevent "more" messages from appearing
set more off 
* Control line length
set linesize 150 
 
********************************************************************************
*******           BEGIN DATA MANIPULATION OF CHAPTER 5 EXAMPLE           *******
*****                 CHANGE "filesave" to your directory                *******
********************************************************************************
 
* Defining global variable for file location to be replaced in code below
global filesave "C:\Dropbox\PilesOfVariance\Chapter5\STATA" 
 
* Import chapter 5 stacked data and center time
use "$filesave\STATA_Chapter5.dta", clear 
gen time = wave - 1 
label variable time "time: Time in Study (0=1)" 
 
********************************************************************************
*******                     BEGIN CHAPTER 5 MODELS                       *******
********************************************************************************
 
* Save results to separate file
log using $filesave\STATA_Chapter5_Output, replace name(STATA_Chapter5) 
 
display as result "Chapter 5 Example: Means by Wave for outcome" 
tabulate wave, summarize(outcome) 
 
display as result "Eq 5.1: Empty Means, Random Intercept Model" 
mixed outcome , ///
                || personid: , variance reml covariance(unstructured) ///
                residuals(independent,t(wave)), 
      estat ic, n(25), 
      estat icc, 
      estat recovariance, relevel(personid), 
      estat recovariance, relevel(personid) correlation, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
 
display as result "Eq 5.3: Fixed Linear Time, Random Intercept Model" 
mixed outcome c.time, ///
                || personid: , variance reml covariance(unstructured) ///
                residuals(independent,t(wave)), 
      estat ic, n(25), 
      estat recovariance, relevel(personid), 
      estat recovariance, relevel(personid) correlation, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
      * Intercept at Time=0
      lincom _cons*1 + c.time*0 
      * Intercept at Time=1
      lincom _cons*1 + c.time*1 
      * Intercept at Time=2
      lincom _cons*1 + c.time*2 
      * Intercept at Time=3
      lincom _cons*1 + c.time*3 
      estimates store FitFixLin, 
 
display as result "Eq 5.5: Random Linear Time Model" 
mixed outcome c.time, ///
                || personid: time, variance reml covariance(unstructured) ///
                residuals(independent,t(wave)), 
      estat ic, n(25), 
      estat recovariance, relevel(personid), 
      estat recovariance, relevel(personid) correlation, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
      * Intercept at Time=0
      lincom _cons*1 + c.time*0 
      * Intercept at Time=1
      lincom _cons*1 + c.time*1 
      * Intercept at Time=2
      lincom _cons*1 + c.time*2 
      * Intercept at Time=3
      lincom _cons*1 + c.time*3 
      estimates store FitRandLin, 
      lrtest FitRandLin FitFixLin,  
 
display as result "Ch 5: Saturated Means, Unstructured Variance Model" 
display as result "ANSWER KEY for both sides of the model" 
mixed outcome i.wave, ///
                || personid: , noconstant variance reml covariance(unstructured) ///
                residuals(unstructured,t(wave)), 
      estat ic, n(25), 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
      contrast i.wave,  
      margins  i.wave,  
      margins  i.wave, pwcompare(pveffects) 
 
display as result "Ch 5: Random Linear Time Model with AR1 R Matrix" 
mixed outcome c.time, ///
                || personid: time, variance reml covariance(unstructured) ///
                residuals(ar1,t(wave)), 
      estat ic, n(25), 
      estat recovariance, relevel(personid), 
      estat recovariance, relevel(personid) correlation, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
      estimates store FitRandLinAR1, 
      lrtest FitRandLinAR1 FitRandLin,  
 
display as result "Ch 5: Random Linear Time Model with TOEP2 R Matrix" 
mixed outcome c.time, ///
                || personid: time, variance reml covariance(unstructured) ///
                residuals(toeplitz1,t(wave)), 
      estat ic, n(25), 
      estat recovariance, relevel(personid), 
      estat recovariance, relevel(personid) correlation, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
      estimates store FitRandLinTOEP2, 
      lrtest FitRandLinTOEP2 FitRandLin,  
  
****** END CHAPTER 5 MODELS ******
 
* Close log
log close STATA_Chapter5 
* Convert log to html using custom downloaded package
log2html $filesave\STATA_Chapter5_Output, replace