set more off
set linesize 150
global filesave "C:\Dropbox\PilesOfVariance\Chapter2\STATA"
use "$filesave\STATA_Chapter2.dta", clear
gen age80 = age - 80
gen age85 = age - 85
gen age90 = age - 90
gen grip6 = grip - 6
gen grip9 = grip - 9
gen grip12 = grip - 12
gen sexwm = sexmw
recode sexwm 1=0 if sexmw==1
recode sexwm 0=1 if sexmw==0
gen demnf=0
gen demnc=0
gen demfn=0
gen demfc=0
gen demcn=0
gen demcf=0
replace demnf=0 if demgroup==1
replace demnc=0 if demgroup==1
replace demfn=1 if demgroup==1
replace demfc=0 if demgroup==1
replace demcn=1 if demgroup==1
replace demcf=0 if demgroup==1
replace demnf=1 if demgroup==2
replace demnc=0 if demgroup==2
replace demfn=0 if demgroup==2
replace demfc=0 if demgroup==2
replace demcn=0 if demgroup==2
replace demcf=1 if demgroup==2
replace demnf=0 if demgroup==3
replace demnc=1 if demgroup==3
replace demfn=0 if demgroup==3
replace demfc=1 if demgroup==3
replace demcn=0 if demgroup==3
replace demcf=0 if demgroup==3
label define fdemgoup 1 "1None" 2 "2Future" 3 "3Current"
label values demgroup fdemgroup
label variable age80 "age80: Age in Years (0=80)"
label variable age85 "age85: Age in Years (0=85)"
label variable age90 "age85: Age in Years (0=90)"
label variable grip6 "grip6: Grip Strength in Pounds (0=6)"
label variable grip9 "grip9: Grip Strength in Pounds (0=9)"
label variable grip12 "grip12: Grip Strength in Pounds (0=12)"
label variable sexwm "sexwm: Sex (0=Women, 1=Men)"
label variable demnf "demnf: Dementia Contrast for None=0 vs Future=1"
label variable demnc "demnc: Dementia Contrast for None=0 vs Current=1"
label variable demfn "demfn: Dementia Contrast for Future=0 vs None=1"
label variable demfc "demfc: Dementia Contrast for Future=0 vs Current=1"
label variable demcn "demcn: Dementia Contrast for Current=0 vs None=1"
label variable demcf "demcf: Dementia Contrast for Current=0 vs Future=1"
log using $filesave\STATA_Chapter2_Output, replace name(STATA_Chapter2)
display as result "Chapter 2: Descriptive Statistics for Example Variables"
summarize age grip cognition
tabulate sexmw demgroup
corr age grip sexmw cognition
display as result "Eq 2.3: Empty Means Model"
mixed cognition ,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.4: Adding Age (0=85)"
mixed cognition c.age85,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.4: Adding Original Age Instead"
mixed cognition c.age,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.6: Adding Grip (0=9)"
mixed cognition c.age85 c.grip9,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
test (c.age85=0) (c.grip9=0)
display as result "Eq 2.7: Adding Sex (0=M, 1=W)"
mixed cognition c.age85 c.grip9 c.sexmw,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
test (c.age85=0) (c.grip9=0) (c.sexmw=0)
display as result "Eq 2.7: Adding Sex (1=M 0=W)"
mixed cognition c.age85 c.grip9 c.sexwm,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.8: Adding Dementia Group"
display as result "Using Manual Group Contrasts so Reference=None"
mixed cognition c.age85 c.grip9 c.sexmw c.demnf c.demnc,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
test (c.age85=0) (c.grip9=0) (c.sexmw=0) (c.demnf=0) (c.demnc=0)
test (c.demnf=0) (c.demnc=0)
lincom c.demnf*-1 + c.demnc*1
display as result "Eq 2.8: Adding Dementia Group"
display as result "Categorical Predictor for Dementia Group"
mixed cognition c.age85 c.grip9 c.sexmw i.demgroup,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
contrast i.demgroup,
margins i.demgroup, at(c.age85=0 c.grip9=0 c.sexmw=0)
margins i.demgroup, pwcompare(pveffects)
display as result "Eq 2.9: Adding Age by Grip Interaction"
display as result "Age (0=85), Grip (0=9)"
mixed cognition c.age85 c.grip9 c.sexmw c.demnf c.demnc
c.age85#c.grip9,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
estat vce,
test (c.age85=0) (c.grip9=0) (c.sexmw=0) (c.demnf=0) (c.demnc=0) (c.age85#c.grip9=0)
lincom c.demnf*-1 + c.demnc*1
lincom c.age85*1 + c.age85#c.grip9*-3
lincom c.age85*1 + c.age85#c.grip9*0
lincom c.age85*1 + c.age85#c.grip9*3
lincom c.grip9*1 + c.age85#c.grip9*-5
lincom c.grip9*1 + c.age85#c.grip9*0
lincom c.grip9*1 + c.age85#c.grip9*5
lincom _cons*1 + c.age85*-5 + c.grip9*3 + c.age85#c.grip9*-15
lincom _cons*1 + c.age85*0 + c.grip9*3 + c.age85#c.grip9*0
lincom _cons*1 + c.age85*5 + c.grip9*3 + c.age85#c.grip9*15
lincom _cons*1 + c.age85*-5 + c.grip9*0 + c.age85#c.grip9*0
lincom _cons*1 + c.age85*0 + c.grip9*0 + c.age85#c.grip9*0
lincom _cons*1 + c.age85*5 + c.grip9*0 + c.age85#c.grip9*0
lincom _cons*1 + c.age85*-5 + c.grip9*-3 + c.age85#c.grip9*15
lincom _cons*1 + c.age85*0 + c.grip9*-3 + c.age85#c.grip9*0
lincom _cons*1 + c.age85*5 + c.grip9*-3 + c.age85#c.grip9*-15
margins, at (c.age85=(-5(5)5) c.grip9=(-3(3)3) c.sexmw=0 c.demnf=0 c.demnc=0) vsquish,
display as result "Eq 2.9: Adding Age by Grip Interaction"
display as result "Age (0=80), Grip (0=12)"
mixed cognition c.age80 c.grip12 c.sexmw c.demnf c.demnc
c.age80#c.grip12,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
lincom c.demnf*-1 + c.demnc*1
display as result "Eq 2.9: Adding Age by Grip Interaction"
display as result "Age (0=90), Grip (0=6)"
mixed cognition c.age90 c.grip6 c.sexmw c.demnf c.demnc
c.age90#c.grip6,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
lincom c.demnf*-1 + c.demnc*1
display as result "Eq 2.13: Adding Sex by Dementia Interaction"
display as result "Sex (0=Men), Dementia (0=None)"
mixed cognition c.age85 c.grip9 c.sexmw c.demnf c.demnc
c.age85#c.grip9 c.sexmw#c.demnf c.sexmw#c.demnc,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
estat vce,
test (c.age85=0) (c.grip9=0) (c.sexmw=0) (c.demnf=0) (c.demnc=0) (c.age85#c.grip9=0) (c.sexmw#c.demnf=0) (c.sexmw#c.demnc=0)
test (c.sexmw#c.demnf=0) (c.sexmw#c.demnc=0)
lincom _cons*1 + c.sexmw*0 + c.demnf*0 + c.demnc*0 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom _cons*1 + c.sexmw*1 + c.demnf*0 + c.demnc*0 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom _cons*1 + c.sexmw*0 + c.demnf*1 + c.demnc*0 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom _cons*1 + c.sexmw*1 + c.demnf*1 + c.demnc*0 + c.sexmw#c.demnf*1 + c.sexmw#c.demnc*0
lincom _cons*1 + c.sexmw*0 + c.demnf*0 + c.demnc*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom _cons*1 + c.sexmw*1 + c.demnf*0 + c.demnc*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*1
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*1 + c.sexmw#demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#demnc*1
lincom c.demnf*1 + c.sexmw#c.demnf*0
lincom c.demnf*1 + c.sexmw#c.demnf*1
lincom c.demnc*1 + c.sexmw#c.demnc*0
lincom c.demnc*1 + c.sexmw#c.demnc*1
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*-1 + c.sexmw#c.demnc*1
lincom c.sexmw#c.demnf*1
lincom c.sexmw#c.demnc*1
lincom c.sexmw#c.demnf*-1 + c.sexmw#c.demnc*1
display as result "Eq 2.13: Adding Sex by Dementia Interaction"
display as result "Sex (0=Women), Dementia (0=None)"
mixed cognition c.age85 c.grip9 c.sexwm c.demnf c.demnc
c.age85#c.grip9 sexwm#c.demnf c.sexwm#c.demnc,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.15: Adding Sex by Dementia Interaction"
display as result "Sex (0=Men), Dementia (0=Future)"
mixed cognition c.age85 c.grip9 c.sexmw c.demfn c.demfc
c.age85#c.grip9 c.sexmw#c.demfn c.sexmw#c.demfc,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.15: Adding Sex by Dementia Interaction"
display as result "Sex (0=Women), Dementia (0=Future)"
mixed cognition c.age85 c.grip9 c.sexwm c.demfn c.demfc
c.age85#c.grip9 c.sexwm#c.demfn c.sexwm#c.demfc,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.16: Adding Sex by Dementia Interaction"
display as result "Sex (0=Men), Dementia (0=Current)"
mixed cognition c.age85 c.grip9 c.sexmw c.demcn c.demcf
c.age85#c.grip9 c.sexmw#c.demcn c.sexmw#c.demcf,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.16: Adding Sex by Dementia Interaction"
display as result "Sex (0=Women), Dementia (0=Current)"
mixed cognition c.age85 c.grip9 c.sexwm c.demcn c.demcf
c.age85#c.grip9 c.sexwm#c.demcn c.sexwm#c.demcf,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
display as result "Eq 2.16: Adding Sex by Dementia Interaction"
display as result "Categorical Sex and Dementia"
mixed cognition c.age85 c.grip9 i.sexmw i.demgroup
c.age85#c.grip9 i.sexmw#i.demgroup,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
contrast i.sexmw#i.demgroup,
margins i.sexmw#i.demgroup, at(c.age85=0 c.grip9=0)
margins i.sexmw#i.demgroup, pwcompare(pveffects)
margins i.sexmw@i.demgroup, at(c.age85=0 c.grip9=0)
margins i.demgroup@i.sexmw, at(c.age85=0 c.grip9=0)
contrast {i.sexmw#i.demgroup -1 1 0 1 -1 0}
contrast {i.sexmw#i.demgroup -1 0 1 1 0 -1}
contrast {i.sexmw#i.demgroup 0 -1 1 0 1 -1}
display as result "Eq 2.17: Adding Sex by Age and Sex by Grip Interactions"
display as result "Age (0=85), Grip (0=9), Sex (0=Men), Dementia (0=None)"
mixed cognition c.age85 c.grip9 c.sexmw c.demnf c.demnc
c.age85#c.grip9 c.sexmw#c.demnf c.sexmw#c.demnc
c.age85#c.sexmw c.grip9#c.sexmw,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
lincom c.age85*1 + c.age85#c.sexmw*0
lincom c.age85*1 + c.age85#c.sexmw*1
lincom c.grip9*1 + c.grip9#c.sexmw*0
lincom c.grip9*1 + c.grip9#c.sexmw*1
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*1 + c.sexmw#c.demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*1
lincom c.demnf*1 + c.demnc*0 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.demnf*0 + c.demnc*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.demnf*1 + c.demnc*0 + c.sexmw#c.demnf*1 + c.sexmw#c.demnc*0
lincom c.demnf*0 + c.demnc*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*1
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*-1 + c.sexmw#c.demnc*1
lincom c.demnf#c.sexmw*1 + c.demnc#c.sexmw*0
lincom c.demnf#c.sexmw*0 + c.demnc#c.sexmw*1
lincom c.demnf#c.sexmw*-1 + c.demnc#c.sexmw*1
display as result "Eq 2.18: Adding Sex by Age by Grip Three-Way Interaction"
display as result "Age (0=85), Grip (0=9), Sex (0=Men), Dementia (0=None)"
mixed cognition c.age85 c.grip9 c.sexmw c.demnf c.demnc
c.age85#c.grip9 c.sexmw#c.demnf c.sexmw#c.demnc
c.age85#c.sexmw c.grip9#c.sexmw c.age85#c.grip9#c.sexmw,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
lincom c.age85*1 + c.age85#c.sexmw*0
lincom c.age85*1 + c.age85#c.sexmw*1
lincom c.grip9*1 + c.grip9#c.sexmw*0
lincom c.grip9*1 + c.grip9#c.sexmw*1
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*1 + c.sexmw#c.demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*1
lincom c.demnf*1 + c.demnc*0 + c.sexmw#c.demnf*0 + sexmw#c.demnc*0
lincom c.demnf*0 + c.demnc*1 + c.sexmw#c.demnf*0 + sexmw#c.demnc*0
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*0 + sexmw#c.demnc*0
lincom c.demnf*1 + c.demnc*0 + c.sexmw#c.demnf*1 + sexmw#c.demnc*0
lincom c.demnf*0 + c.demnc*1 + c.sexmw#c.demnf*0 + sexmw#c.demnc*1
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*-1 + sexmw#c.demnc*1
lincom c.demnf#c.sexmw*1 + c.demnc#c.sexmw*0
lincom c.demnf#c.sexmw*0 + c.demnc#c.sexmw*1
lincom c.demnf#c.sexmw*-1 + c.demnc#c.sexmw*1
lincom c.age85#c.grip9*1 + c.age85#c.grip9#c.sexmw*0
lincom c.age85#c.grip9*1 + c.age85#c.grip9#c.sexmw*1
margins, at (c.age85=(-5(5)5) c.grip9=(-3(3)3) c.sexmw=(0(1)1) c.demnf=0 c.demnc=0) vsquish,
display as result "Eq 2.13: Final Reported Model"
display as result "Age (0=85), Grip (0=9), Sex (0=Men), Dementia (0=None)"
mixed cognition c.age85 c.grip9 c.sexmw c.demnf c.demnc
c.age85#c.grip9 c.sexmw#c.demnf c.sexmw#c.demnc,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
test (c.age85=0) (c.grip9=0) (c.sexmw=0) (c.demnf=0) (c.demnc=0) (c.age85#c.grip9=0) (c.sexmw#c.demnf=0) (c.sexmw#c.demnc=0)
test (c.sexmw#c.demnf=0) (c.sexmw#c.demnc=0)
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*1 + c.sexmw#c.demnc*0
lincom c.sexmw*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*1
lincom c.demnf*1 + c.sexmw#c.demnf*0
lincom c.demnf*1 + c.sexmw#c.demnf*1
lincom c.demnc*1 + c.sexmw#c.demnc*0
lincom c.demnc*1 + c.sexmw#c.demnc*1
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*0 + c.sexmw#c.demnc*0
lincom c.demnf*-1 + c.demnc*1 + c.sexmw#c.demnf*-1 + c.sexmw#c.demnc*1
lincom c.sexmw#c.demnf*1
lincom c.sexmw#c.demnc*1
lincom c.sexmw#c.demnf*-1 + c.sexmw#c.demnc*1
display as result "Eq 2.13: Final Reported Model"
display as result "Categorical Variables for Sex and Dementia"
mixed cognition c.age85 c.grip9 i.sexmw i.demgroup
c.age85#c.grip9 i.sexmw#i.demgroup,
|| personid: , noconstant variance reml covariance(unstructured),
estat ic, n(550),
contrast i.sexmw#i.demgroup,
margins i.sexmw#i.demgroup, at(c.age85=0 c.grip9=0)
margins i.sexmw#i.demgroup, pwcompare(pveffects)
margins i.sexmw@i.demgroup, at(c.age85=0 c.grip9=0)
margins i.demgroup@i.sexmw, at(c.age85=0 c.grip9=0)
log close STATA_Chapter2
log2html $filesave\STATA_Chapter2_Output, replace