set more off
set linesize 150
global filesave "C:\Dropbox\PilesOfVariance\Chapter7b\STATA"
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"
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)"
egen nummiss = rowmiss(agec18 att4 risky monitor)
drop if nummiss>0
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),
lincom _cons*1 + c.agec18*-6
lincom _cons*1 + c.agec18*-5
lincom _cons*1 + c.agec18*-4
lincom _cons*1 + c.agec18*-3
lincom _cons*1 + c.agec18*-2
lincom _cons*1 + c.agec18*-1
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),
lincom _cons*1 + c.agec18*-6
lincom _cons*1 + c.agec18*-5
lincom _cons*1 + c.agec18*-4
lincom _cons*1 + c.agec18*-3
lincom _cons*1 + c.agec18*-2
lincom _cons*1 + c.agec18*-1
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),
lincom _cons*1 + c.agec18*-6 + c.agec18#c.agec18*36
lincom _cons*1 + c.agec18*-5 + c.agec18#c.agec18*25
lincom _cons*1 + c.agec18*-4 + c.agec18#c.agec18*16
lincom _cons*1 + c.agec18*-3 + c.agec18#c.agec18*9
lincom _cons*1 + c.agec18*-2 + c.agec18#c.agec18*4
lincom _cons*1 + c.agec18*-1 + c.agec18#c.agec18*1
lincom _cons*1 + c.agec18*0 + c.agec18#c.agec18*0
lincom c.agec18*1 + c.agec18#c.agec18*-12
lincom c.agec18*1 + c.agec18#c.agec18*-10
lincom c.agec18*1 + c.agec18#c.agec18*-8
lincom c.agec18*1 + c.agec18#c.agec18*-6
lincom c.agec18*1 + c.agec18#c.agec18*-4
lincom c.agec18*1 + c.agec18#c.agec18*-2
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),
lincom _cons*1 + c.agec18*-6 + c.agec18#c.agec18*36
lincom _cons*1 + c.agec18*-5 + c.agec18#c.agec18*25
lincom _cons*1 + c.agec18*-4 + c.agec18#c.agec18*16
lincom _cons*1 + c.agec18*-3 + c.agec18#c.agec18*9
lincom _cons*1 + c.agec18*-2 + c.agec18#c.agec18*4
lincom _cons*1 + c.agec18*-1 + c.agec18#c.agec18*1
lincom _cons*1 + c.agec18*0 + c.agec18#c.agec18*0
lincom c.agec18*1 + c.agec18#c.agec18*-12
lincom c.agec18*1 + c.agec18#c.agec18*-10
lincom c.agec18*1 + c.agec18#c.agec18*-8
lincom c.agec18*1 + c.agec18#c.agec18*-6
lincom c.agec18*1 + c.agec18#c.agec18*-4
lincom c.agec18*1 + c.agec18#c.agec18*-2
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
log close STATA_Chapter7b
log2html $filesave\STATA_Chapter7b_Output, replace