d <- read.csv("halfmile-2016-03-21.csv")
d$Diff <- d$After - d$Before
m8 <- lm(Diff ~ Treatment*Before + Age, d)
Anova(m8)
## Anova Table (Type II tests)
## 
## Response: Diff
##                  Sum Sq Df F value   Pr(>F)   
## Treatment         73.69  1  5.0546 0.030961 * 
## Before            15.87  1  1.0884 0.303986   
## Age              128.39  1  8.8064 0.005384 **
## Treatment:Before 154.80  1 10.6179 0.002494 **
## Residuals        510.26 35                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(m8)
## 
## Call:
## lm(formula = Diff ~ Treatment * Before + Age, data = d)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.0497 -1.9712  0.3687  2.9460  6.8119 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)   
## (Intercept)          1.15010    6.17025   0.186  0.85321   
## TreatmentTrt        17.80564    6.42093   2.773  0.00884 **
## Before               0.02870    0.01892   1.517  0.13830   
## Age                 -0.35832    0.12075  -2.968  0.00538 **
## TreatmentTrt:Before -0.08722    0.02677  -3.259  0.00249 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.818 on 35 degrees of freedom
## Multiple R-squared:  0.4693, Adjusted R-squared:  0.4087 
## F-statistic: 7.738 on 4 and 35 DF,  p-value: 0.000141
mean(d$Before)
## [1] 236.55
mean(d$Age)
## [1] 25.85
d$BeforeC <- d$Before - mean(d$Before)
d$AgeC <- d$Age - mean(d$Age)
m8b <- lm(Diff ~ Treatment*BeforeC + Age, d)
m8c <- lm(Diff ~ Treatment*BeforeC + AgeC, d)
printCoefmat(coef(summary(m8)), signif.legend = FALSE)
##                      Estimate Std. Error t value Pr(>|t|)   
## (Intercept)          1.150097   6.170253  0.1864 0.853212   
## TreatmentTrt        17.805645   6.420931  2.7731 0.008842 **
## Before               0.028697   0.018920  1.5168 0.138304   
## Age                 -0.358322   0.120747 -2.9676 0.005384 **
## TreatmentTrt:Before -0.087216   0.026766 -3.2585 0.002494 **
printCoefmat(coef(summary(m8b)), signif.legend = FALSE)
##                       Estimate Std. Error t value Pr(>|t|)   
## (Intercept)           7.938323   3.158703  2.5132 0.016722 * 
## TreatmentTrt         -2.825235   1.217876 -2.3198 0.026308 * 
## BeforeC               0.028697   0.018920  1.5168 0.138304   
## Age                  -0.358322   0.120747 -2.9676 0.005384 **
## TreatmentTrt:BeforeC -0.087216   0.026766 -3.2585 0.002494 **
printCoefmat(coef(summary(m8c)), signif.legend = FALSE)
##                       Estimate Std. Error t value Pr(>|t|)   
## (Intercept)          -1.324300   0.857556 -1.5443 0.131517   
## TreatmentTrt         -2.825235   1.217876 -2.3198 0.026308 * 
## BeforeC               0.028697   0.018920  1.5168 0.138304   
## AgeC                 -0.358322   0.120747 -2.9676 0.005384 **
## TreatmentTrt:BeforeC -0.087216   0.026766 -3.2585 0.002494 **
dx <- expand.grid(Sex="M", Age=mean(d$Age), Before=c(180, mean(d$Before), 300), Treatment=c("Control", "Trt"))
cbind(dx, predict(m8, newdata = dx, interval="confidence"))
##   Sex   Age Before Treatment        fit        lwr        upr
## 1   M 25.85 180.00   Control -2.9471038  -5.684291 -0.2099167
## 2   M 25.85 236.55   Control -1.3243003  -3.065231  0.4166300
## 3   M 25.85 300.00   Control  0.4965111  -2.546116  3.5391381
## 4   M 25.85 180.00       Trt -0.8402894  -3.667748  1.9871689
## 5   M 25.85 236.55       Trt -4.1495350  -5.890209 -2.4088609
## 6   M 25.85 300.00       Trt -7.8625613 -10.873878 -4.8512448
cbind(dx, predict(m8, newdata = dx, interval="prediction"))
##   Sex   Age Before Treatment        fit        lwr       upr
## 1   M 25.85 180.00   Control -2.9471038 -11.167607 5.2733997
## 2   M 25.85 236.55   Control -1.3243003  -9.268815 6.6202150
## 3   M 25.85 300.00   Control  0.4965111  -7.830677 8.8236993
## 4   M 25.85 180.00       Trt -0.8402894  -9.091290 7.4107108
## 5   M 25.85 236.55       Trt -4.1495350 -12.093994 3.7949241
## 6   M 25.85 300.00       Trt -7.8625613 -16.178360 0.4532376