load(file="/Users/dleibel1/Box Sync/R Data/DanThesisFinal.rdata")
library(lmerTest)
## Loading required package: Matrix
## Loading required package: lme4
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
library(zStat)
library(zUtil)

##Model 5: Final model (Significant Interactions Retained)
mm5 = lmer(PhyPerfSingleMN~Age + Sex + EF + Race + BMI + PovStat + WRATtotal + (1|HNDid) + Age*Race + Age*EF ,data=DanThesisFinal,REML=F)
summary(mm5)
## Linear mixed model fit by maximum likelihood t-tests use Satterthwaite
##   approximations to degrees of freedom [lmerMod]
## Formula: 
## PhyPerfSingleMN ~ Age + Sex + EF + Race + BMI + PovStat + WRATtotal +  
##     (1 | HNDid) + Age * Race + Age * EF
##    Data: DanThesisFinal
## 
##      AIC      BIC   logLik deviance df.resid 
##   8520.5   8584.9  -4248.2   8496.5     1579 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.1365 -0.0659  0.1569  0.3788  2.6634 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  HNDid    (Intercept) 4.814    2.194   
##  Residual             7.990    2.827   
## Number of obs: 1591, groups:  HNDid, 1112
## 
## Fixed effects:
##                 Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)    3.053e+01  8.405e-01  7.741e+02  36.327  < 2e-16 ***
## Age           -1.552e-01  1.843e-02  9.181e+02  -8.417  < 2e-16 ***
## SexMen         1.370e-01  2.005e-01  6.855e+02   0.683 0.494642    
## EF             1.560e-01  4.542e-02  1.321e+03   3.435 0.000611 ***
## RaceAfrAm      7.627e-01  2.152e-01  7.245e+02   3.544 0.000419 ***
## BMI           -1.215e-01  1.464e-02  8.185e+02  -8.301 4.44e-16 ***
## PovStatBelow  -3.531e-01  2.172e-01  7.169e+02  -1.626 0.104465    
## WRATtotal      2.120e-02  1.526e-02  7.717e+02   1.389 0.165241    
## Age:RaceAfrAm  6.831e-02  2.316e-02  8.904e+02   2.950 0.003260 ** 
## Age:EF         1.220e-02  4.557e-03  1.276e+03   2.677 0.007515 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Age    SexMen EF     RcAfrA BMI    PvSttB WRATtt Ag:RAA
## Age          0.052                                                        
## SexMen      -0.194 -0.060                                                 
## EF           0.223 -0.001 -0.002                                          
## RaceAfrAm   -0.312 -0.108 -0.020  0.136                                   
## BMI         -0.502  0.024  0.142  0.036 -0.030                            
## PovStatBelw -0.275  0.049  0.049  0.076 -0.043  0.100                     
## WRATtotal   -0.819 -0.048  0.017 -0.361  0.228 -0.028  0.172              
## Age:RcAfrAm -0.064 -0.797  0.038  0.028  0.128  0.003  0.035  0.045       
## Age:EF      -0.020 -0.309  0.050  0.228  0.058 -0.026 -0.041  0.031  0.264
##OLD zHAT: Plot Age*Race interaction in Final Model
pAge = seq(-19,20)

hatPhyPerfSingleMN = zMixHat(DanThesisFinal, mm5, vary = "Age=pAge, Race=zQ(AfrAm,White)",fixedCov=c("BMI","Sex","EF","PovStat","WRATtotal"))

with(hatPhyPerfSingleMN[hatPhyPerfSingleMN$Race == 'AfrAm',],plot(pAge,hat, lty=1, col = "red", type = "l",ylim = c(24,32), ylab = "Time(Sec)", xlab = "Age"))
with(hatPhyPerfSingleMN[hatPhyPerfSingleMN$Race == "White",], lines(pAge,hat, lty=2, col = "black"))

axis(1,at=c(-20,-15,-10,-5,0,5,10,15,20),labels = c("30","35","40","45","50","55","60","65","70"))
legend(-20,32, zQ(AfrAm,White), lty=(1:2), col = "black",cex=.75,bty="n")

##NEW zHAT: Plot Age*Race interaction in Final Model
plotData = zHat(DanThesisFinal, mm5, xAxis='Age', factors=('Race'))

par(las=1, lwd=2)

plotData$AgeUn = plotData$Age + 50

with(plotData[plotData$Race=='AfrAm',], plot (AgeUn, Hat, lty =1, typ='l', ylim=c(22,32), ylab='Single Leg Stand', xlab='Age'))
with(plotData[plotData$Race=='White',], lines(AgeUn, Hat, lty =2))

legend(55, 32, c('African American','White'), lty=1:2)