* Prevent "more" messages from appearing
set more off 
* Control line length
set linesize 150 
 
********************************************************************************
******* BEGIN DATA MANIPULATION FOR CHAPTER 10a ALTERNATIVE TIME EXAMPLE *******
*******               CHANGE "filesave" to your directory                *******
********************************************************************************
 
* Defining global variable for file location to be replaced in code below
global filesave "C:\Dropbox\PilesOfVariance\Chapter10a\STATA" 
 
* Import chapter 10a stacked data and create centered predictors for analysis
use "$filesave\STATA_Chapter10a.dta", clear 
* Time in study
gen time = tvage-aget0 
gen timesq = time*time 
* Fixing 1 case rounded to 9
replace occasion=8 if (occasion==9) 
* Age (years since birth) variables
gen roundage = round(tvage,1) 
* Fixing 2 cases above 95
replace roundage=95 if (roundage==97) 
replace roundage=95 if (roundage==100) 
gen tvage84 = tvage-84 
gen tvage84sq = tvage84*tvage84 
gen aget084 = aget0-84 
* Years to death variables
gen roundytdeath = round(tvytdeath,1)+100 
* Fixing 1 case below original -15
replace roundytdeath=85 if (roundytdeath==84) 
gen tvytdeath7 = tvytdeath+7 
gen tvytdeath7sq = tvytdeath7*tvytdeath7 
gen ytdeatht07 = ytdeatht0+7 
label variable time "time: Years since Time 0" 
label variable timesq "timesq: Quadratic Years since Time 0" 
label variable roundage "roundage: Age Rounded to Nearest Year" 
label variable tvage84 "tvage84: Time-Varying Age (0=84 years)" 
label variable tvage84sq "tvage84sq: Quadratic Time-Varying Age (0=84 years)" 
label variable aget084 "aget084: Age at Time 0 (0=84 years)" 
label variable roundytdeath "roundytdeath: Years to Death Rounded to Nearest Year +100" 
label variable tvytdeath7 "tvytdeath7: Time-Varying Years to Death (0=-7 years)" 
label variable tvytdeath7sq "tvytdeath7sq: Quadratic Time-Varying Years to Death (0=-7 years)" 
label variable ytdeatht07 "ytdeatht07: Years to Death at Time 0 (0=-7 years)" 
 
* Subset sample to complete cases for all predictors
egen nummiss = rowmiss(tvage aget0 ytdeatht0 tvytdeath recall) 
drop if nummiss>0 
     
********************************************************************************
*******          BEGIN CHAPTER 10a ALTERNATIVE TIME MODELS               *******
********************************************************************************
 
* Save results to separate file
log using $filesave\STATA_Chapter10a_Output, replace name(STATA_Chapter10a) 
 
display as result "Chapter 10a: Descriptive Statistics for Time-Invariant Variables" 
preserve  
collapse aget0 ytdeatht0, by(personid) 
summarize aget0 ytdeatht0 
corr aget0 ytdeatht0 
restore  
 
display as result "Chapter 10a: Descriptive Statistics for Time-Varying Variables" 
summarize time tvage tvytdeath recall 
 
display as result "Ch 10a: Empty Means, Random Intercept Model for Prose Recall" 
mixed recall , ///
               || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      estat icc, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
 
display as result "Ch 10a: Saturated Means by Rounded Years in Study, Random Intercept Model for Prose Recall" 
mixed recall i.occasion, ///
               || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      contrast i.occasion,  
      margins  i.occasion,  
 
display as result "Ch 10a: Saturated Means by Rounded Age, Random Intercept Model for Prose Recall" 
mixed recall i.roundage, ///
               || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      contrast i.roundage,  
      margins  i.roundage,  
 
display as result "Ch 10a: Saturated Means by Rounded Years to Death, Random Intercept Model for Prose Recall" 
mixed recall i.roundytdeath, ///
               || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      contrast i.roundytdeath,  
      margins  i.roundytdeath,  
 
display as result "Ch 10a: Empty Means, Random Intercept Model for Years since Birth" 
mixed tvage , ///
              || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      estat icc, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
 
display as result "Ch 10a: Empty Means, Random Intercept Model for Years to Death" 
mixed tvytdeath , ///
                  || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      estat icc, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
 
display as result "Ch 10a: Empty Means, Random Intercept Model for Years in Study" 
mixed time , ///
             || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      estat icc, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
 
display as result "Ch 10a: Fixed Quadratic, Random Intercept Model using Years since Birth" 
mixed recall c.tvage84 c.tvage84#c.tvage84, ///
               || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      estimates store FitFQRIAge, 
 
display as result "Eq 10a.1: Random Linear Model using Years since Birth" 
mixed recall c.tvage84, ///
               || personid: tvage84, variance mle covariance(unstructured), 
      estat ic, n(207), 
 
display as result "Eq 10a.1: Fixed Quadratic, Random Linear Model using Years since Birth" 
mixed recall c.tvage84 c.tvage84#c.tvage84, ///
               || personid: tvage84, variance mle covariance(unstructured), 
      estat ic, n(207), 
      estimates store FitFQRLAge, 
      lrtest FitFQRLAge FitFQRIAge,  
      predict PredFQRLAge, xb, 
      margins, at (c.tvage84=(-2(2)10)) vsquish, 
corr recall PredFQRLAge 
 
display as result "Ch 10a: Fixed Quadratic, Random Intercept Model using Years to Death" 
mixed recall c.tvytdeath7 c.tvytdeath7#c.tvytdeath7, ///
               || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      estimates store FitFQRIYTD, 
 
display as result "Eq 10a.1: Fixed Quadratic, Random Linear Model using Years to Death" 
mixed recall c.tvytdeath7 c.tvytdeath7#c.tvytdeath7, ///
               || personid: tvytdeath7, variance mle covariance(unstructured), 
      estat ic, n(207), 
      estimates store FitFQRLYTD, 
      lrtest FitFQRLYTD FitFQRIYTD,  
      predict PredFQRLYTD, xb, 
      margins, at (c.tvytdeath7=(-4(2)6)) vsquish, 
corr recall PredFQRLYTD 
 
display as result "Ch 10a: Fixed Quadratic, Random Intercept Model using Years in Study" 
mixed recall c.time c.time#c.time, ///
               || personid: , variance mle covariance(unstructured), 
      estat ic, n(207), 
      estimates store FitFQRIYIS, 
 
display as result "Eq 10a.1: Fixed Quadratic, Random Linear Model using Years in Study" 
mixed recall c.time c.time#c.time, ///
               || personid: time, variance mle covariance(unstructured), 
      estat ic, n(207), 
      estimates store FitFQRLYIS, 
      lrtest FitFQRLYIS FitFQRIYIS,  
      predict PredFQRLYIS, xb, 
      margins, at (c.time=(0(2)8)) vsquish, 
corr recall PredFQRLYIS 
 
display as result "Eq 10a.2: Fixed Quadratic, Random Linear Model using Years since Birth" 
display as result "Controlling for Birth Cohort" 
mixed recall c.tvage84 c.tvage84#c.tvage84 ///
             c.aget084 c.aget084#c.aget084 c.tvage84#c.aget084, ///
               || personid: tvage84, variance mle covariance(unstructured), 
      estat ic, n(207), 
      * Multivariate Test of Birth Cohort Contextual Effects
      test (c.aget084=0) (c.aget084#c.aget084=0) (c.tvage84#c.aget084=0) 
      * Contextual Linear Birth Cohort on Intercept
      lincom c.aget084*1 
      * Contextual Quadratic Birth Cohort on Intercept
      lincom c.aget084#c.aget084*1 
      * Contextual Linear Birth Cohort on Linear Slope
      lincom c.tvage84#c.aget084*1 
      * Total Linear Birth Cohort on Intercept
      lincom c.aget084*1 + c.tvage84*1 
      * Total Quadratic Birth Cohort on Intercept
      lincom c.aget084#c.aget084*1 + c.tvage84#c.aget084*1 + c.tvage84#c.tvage84*1 
      * Total Linear Birth Cohort on Linear Slope
      lincom c.tvage84#c.aget084*1 + c.tvage84#c.tvage84*2 
      estimates store FitCohAge, 
      lrtest FitCohAge FitFQRLAge,  
      predict PredCohAge, xb, 
      margins, at (c.tvage84=(-2(2)10) c.aget084=(-4(4)4)) vsquish, 
corr recall PredCohAge 
 
display as result "Eq 10a.2: Fixed Quadratic, Random Linear Model using Years in Study" 
display as result "Controlling for Birth Cohort" 
mixed recall c.time c.time#c.time ///
             c.aget084 c.aget084#c.aget084 c.time#c.aget084, ///
               || personid: time, variance mle covariance(unstructured), 
      estat ic, n(207), 
      * Multivariate Test of Birth Cohort Total Effects
      test (c.aget084=0) (c.aget084#c.aget084=0) (c.time#c.aget084=0) 
      * Contextual Linear Birth Cohort on Intercept
      lincom c.aget084*1 + c.time*-1 
      * Contextual Quadratic Birth Cohort on Intercept
      lincom c.aget084#c.aget084*1 + c.time#c.aget084*-1 + c.time#c.time*1 
      * Contextual Linear Birth Cohort on Linear Slope
      lincom c.time#c.aget084*1 + c.time#c.time*-2 
      * Total Linear Birth Cohort on Intercept
      lincom c.aget084*1 
      * Total Quadratic Birth Cohort on Intercept
      lincom c.aget084#c.aget084*1 
      * Total Linear Birth Cohort on Linear Slope
      lincom c.time#c.aget084*1 
      estimates store FitCohAgeYIS, 
      lrtest FitCohAgeYIS FitFQRLYIS,  
      predict PredCohAgeYIS, xb, 
      margins, at (c.time=(0(2)8) c.aget084=(-4(4)4)) vsquish, 
corr recall PredCohAgeYIS 
 
display as result "Eq 10a.2: Fixed Quadratic, Random Linear Model using Years to Death" 
display as result "Controlling for Death Cohort" 
mixed recall c.tvytdeath7 c.tvytdeath7#c.tvytdeath7 ///
             c.ytdeatht07 c.ytdeatht07#c.ytdeatht07 c.tvytdeath7#c.ytdeatht07, ///
               || personid: tvytdeath7, variance mle covariance(unstructured), 
      estat ic, n(207), 
      * Multivariate Test of Death Cohort Contextual Effects
      test (c.ytdeatht07=0) (c.ytdeatht07#c.ytdeatht07=0) (c.tvytdeath7#c.ytdeatht07=0) 
      * Contextual Linear Death Cohort on Intercept
      lincom c.ytdeatht07*1 
      * Contextual Quadratic Death Cohort on Intercept
      lincom c.ytdeatht07#c.ytdeatht07*1 
      * Contextual Linear Death Cohort on Linear Slope
      lincom c.tvytdeath7#c.ytdeatht07*1 
      * Total Linear Death Cohort on Intercept
      lincom c.ytdeatht07*1 + c.tvytdeath7*1 
      * Total Quadratic Death Cohort on Intercept
      lincom c.ytdeatht07#c.ytdeatht07*1 + c.tvytdeath7#c.ytdeatht07*1 + c.tvytdeath7#c.tvytdeath7*1 
      * Total Linear Death Cohort on Linear Slope
      lincom c.tvytdeath7#c.ytdeatht07*1 + c.tvytdeath7#c.tvytdeath7*2 
      estimates store FitCohYTD, 
      lrtest FitCohYTD FitFQRLYTD,  
      predict PredCohYTD, xb, 
      margins, at (c.tvytdeath7=(-4(2)6) c.ytdeatht07=(-4(4)4)) vsquish, 
corr recall PredCohYTD 
 
display as result "Eq 10a.2: Fixed Quadratic, Random Linear Model using Years in Study" 
display as result "Controlling for Death Cohort" 
mixed recall c.time c.time#c.time ///
             c.ytdeatht07 c.ytdeatht07#c.ytdeatht07 c.time#c.ytdeatht07, ///
               || personid: time, variance mle covariance(unstructured), 
      estat ic, n(207), 
      * Multivariate Test of Death Cohort Total Effects
      test (c.ytdeatht07=0) (c.ytdeatht07#c.ytdeatht07=0) (c.time#c.ytdeatht07=0) 
      * Contextual Linear Birth Cohort on Intercept
      lincom c.ytdeatht07*1 + c.time*-1 
      * Contextual Quadratic Birth Cohort on Intercept
      lincom c.ytdeatht07#c.ytdeatht07*1 + c.time#c.ytdeatht07*-1 + c.time#c.time*1 
      * Contextual Linear Birth Cohort on Linear Slope
      lincom c.time#c.ytdeatht07*1 + c.time#c.time*-2 
      * Total Linear Birth Cohort on Intercept
      lincom c.ytdeatht07*1 
      * Total Quadratic Birth Cohort on Intercept
      lincom c.ytdeatht07#c.ytdeatht07*1 
      * Total Linear Birth Cohort on Linear Slope
      lincom c.time#c.ytdeatht07*1 
      estimates store FitCohYTDYIS, 
      lrtest FitCohYTDYIS FitFQRLYIS,  
      predict PredCohYTDYIS, xb, 
      margins, at (c.time=(0(2)8) c.ytdeatht07=(-4(4)0)) vsquish, 
corr recall PredCohYTDYIS 
 
display as result "Eq 10a.4: Fixed Quadratic, Random Linear Model using Years in Study" 
display as result "Controlling for Birth Cohort and Death Cohort" 
mixed recall c.time c.time#c.time ///
             c.aget084 c.aget084#c.aget084 c.time#c.aget084 ///
             c.ytdeatht07 c.ytdeatht07#c.ytdeatht07 c.time#c.ytdeatht07, /// 
               || personid: time, variance mle covariance(unstructured), 
      estat ic, n(207), 
      predict PredBothYIS, xb, 
      margins, at (c.time=(0(2)8) c.aget084=(-4(4)4) c.ytdeatht07=(-4(4)4)) vsquish, 
corr recall PredBothYIS 
  
****** END CHAPTER 10a MODELS ******
 
* Close log
log close STATA_Chapter10a 
* Convert log to html using custom downloaded package
log2html $filesave\STATA_Chapter10a_Output, replace