* Prevent "more" messages from appearing
set more off 
* Control line length
set linesize 150 
 
********************************************************************************
*******    BEGIN DATA MANIPULATION FOR CHAPTER 7a FLUCTUATION EXAMPLE    *******
*******               CHANGE "filesave" to your directory                *******
********************************************************************************
 
* Defining global variable for file location to be replaced in code below
global filesave "C:\Dropbox\PilesOfVariance\Chapter7a\STATA" 
 
* Import chapter 7a stacked data and create centered predictors for analysis
use "$filesave\STATA_Chapter7a.dta", clear 
gen studyday1 = studyday - 1 
gen dayofweek1 = dayofweek - 1 
gen age80 = baseage - 80 
label variable studyday1 "studyday1: Day of Study (0=1)" 
label variable dayofweek1 "dayofweek1: Day of Week (0=1)" 
label variable age80 "age80: Baseline Age (0=80)" 
 
* Subset sample to complete cases for all eventual predictors in chapter 8
egen nummiss = rowmiss(women baseage symptoms mood stressor mood) 
drop if nummiss>0 
* Subset sample to study days within a two-week period
drop if studyday>14 
     
********************************************************************************
*******               BEGIN CHAPTER 7a FLUCTUATION MODELS                *******
*******  NOTE: HETEROGENEOUS VARIANCE MODELS ARE NOT POSSIBLE IN STATA   *******
********************************************************************************
 
* Save results to separate file
log using $filesave\STATA_Chapter7a_Output, replace name(STATA_Chapter7a) 
 
display as result "Chapter 7a: Descriptive Statistics for Time-Invariant Variables" 
preserve  
collapse women baseage, by(personid) 
summarize women baseage 
restore  
 
display as result "Chapter 7a: Descriptive Statistics for Time-Varying Variables" 
summarize symptoms 
 
display as result "Eq 7a.3: Empty Means, Random Intercept Model" 
mixed symptoms , ///
                 || personid: , variance mle covariance(unstructured), 
      estat ic, n(105), 
      estat icc, 
      estat wcorrelation, covariance, 
      estat wcorrelation, 
      estimates store FitEmpty, 
 
display as result "Ch 7a: Testing Saturated Means by Day of Study" 
display as result "Random Intercept Only" 
mixed symptoms i.studyday, ///
                 || personid: , variance mle covariance(unstructured), 
      estat ic, n(105), 
      contrast i.studyday,  
      margins  i.studyday,  
      margins  i.studyday, pwcompare(pveffects) 
 
display as result "Ch 7a: Testing Fixed Linear Effect of Day of Study" 
display as result "Random Intercept Only" 
mixed symptoms c.studyday1, ///
                 || personid: , variance mle covariance(unstructured), 
      estat ic, n(105), 
      estimates store FitFixDayofStudy, 
 
display as result "Ch 7a: Testing Random Linear Effect of Day of Study" 
mixed symptoms c.studyday1, ///
                 || personid: studyday1, variance mle covariance(unstructured), 
      estat ic, n(105), 
      estimates store FitRandDayofStudy, 
      lrtest FitRandDayofStudy FitFixDayofStudy,  
 
display as result "Ch 7a: Testing Saturated Means by Day of Week" 
display as result "Random Intercept Only" 
mixed symptoms i.dayofweek, ///
                 || personid: , variance mle covariance(unstructured), 
      estat ic, n(105), 
      contrast i.dayofweek,  
      margins  i.dayofweek,  
      margins  i.dayofweek, pwcompare(pveffects) 
 
display as result "Ch 7a: Testing Fixed Effect of Weekend" 
display as result "Random Intercept Only" 
mixed symptoms c.weekend, ///
                 || personid: , variance mle covariance(unstructured), 
      estat ic, n(105), 
      estimates store FitFixWeekend, 
 
display as result "Ch 7a: Testing Random Effect of Weekend" 
mixed symptoms c.weekend, ///
                 || personid: weekend, variance mle covariance(unstructured), 
      estat ic, n(105), 
      estimates store FitRandWeekend, 
      lrtest FitRandWeekend FitFixWeekend,  
 
display as result "Eq 7a.4: Adding Sex and Age to the Model for the Means" 
mixed symptoms c.women c.age80 c.women#c.age80, ///
                 || personid: , variance mle covariance(unstructured), 
      estat ic, n(105), 
      estat vce, 
      * Multivariate Test of Fixed Effects
      test (c.women=0) (c.age80=0) (c.women#c.age80=0) 
      * Age Slope for Men
      lincom c.age80*1 + c.women#c.age80*0 
      * Age Slope for Women
      lincom c.age80*1 + c.women#c.age80*1 
      estimates store FitSexAge, 
      lrtest FitSexAge FitEmpty,  
      predict PredSexAge, xb, 
corr symptoms PredSexAge 
  
****** END CHAPTER 7a MODELS ******
 
* Close log
log close STATA_Chapter7a 
* Convert log to html using custom downloaded package
log2html $filesave\STATA_Chapter7a_Output, replace