### 17.3 ### 
  ### A.###
    ### Linear combination i and iii are contrasts because the coefficients sum to zero.###
  ### B.### 
    ### 1.###
      ### L = Ȳ1  + 3Ȳ2 - 4Ȳ3 (For the estimates changes the 𝝻 to Ȳ.), s2 {L} = 26MSE/n (for the variance apply the         formula found in the slides.) ###
      ### L = .3Ȳ1  + .5Ȳ2  +  .1Ȳ3  +  .1Ȳ4  s2 {L} = .36MSE/n ### 
      ### L = (Ȳ1 + Ȳ2 + Ȳ3 )/ 3 - Ȳ4 s2 {L} = 26MSE/n ###
### Set up for 17.16 ###
library(DescTools)
setwd("C:/Users/lhomm/OneDrive/Documents/R")
Dat<- read.table("C:/Users/lhomm/OneDrive/Documents/R/Design2 Data.txt",header=FALSE)
colnames(Dat) = c("Offer", "Age_Group", "Count")
head(Dat)
##   Offer Age_Group Count
## 1    23     Young     1
## 2    25     Young     2
## 3    21     Young     3
## 4    22     Young     4
## 5    21     Young     5
## 6    22     Young     6
Dat$Age_Group = factor(Dat$Age_Group)
tapply(X= Dat$Offer, INDEX = list(Dat$Age_Group), FUN = mean)
##  Elderly   Middle    Young 
## 21.41667 27.75000 21.50000
### 17.16 ###
  ### A. ###
    ### Ȳ3  - 2Ȳ2 + Ȳ1  = 21.4167 - 2(27.75) + 21.5 = -12.5833 s{L} = 1.1158 the t-value for alpha/2=.995 and ni - r     = 33 is 2.733 the Ci is then  -12.5833 ∓ 2.733(1.1158) = -15.632 ≤ L ≥ -9.534 ### 
  ### B. ###
    Dat_ANOVA <- aov(Offer ~ Age_Group, data = Dat)
    Dat_ANOVA$

    Scheffe <- PostHocTest(Dat_ANOVA, method = "scheffe", conf.level = .9)
    
    D1 <- 6.3333 ### the diff for middle-elderly for Scheffe method ###
    D2 <- -6.25 ### the diff for young-middle for Scheffe method ###

    Bonferroni <- PostHocTest(Dat_ANOVA, method = "bonferroni", conf.level = .9)
    Bonferroni
## 
##   Posthoc multiple comparisons of means : Bonferroni 
##     90% family-wise confidence level
## 
## $Age_Group
##                       diff    lwr.ci    upr.ci    pval    
## Middle-Elderly  6.33333333  4.902639  7.764027 7.4e-11 ***
## Young-Elderly   0.08333333 -1.347361  1.514027  1.0000    
## Young-Middle   -6.25000000 -7.680694 -4.819306 1.0e-10 ***
## 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    Tukey <- PostHocTest(Dat_ANOVA, method = "hsd", conf.level = .9)
    Tukey
## 
##   Posthoc multiple comparisons of means : Tukey HSD 
##     90% family-wise confidence level
## 
## $Age_Group
##                       diff    lwr.ci    upr.ci    pval    
## Middle-Elderly  6.33333333  4.963842  7.702825 7.4e-11 ***
## Young-Elderly   0.08333333 -1.286158  1.452825  0.9908    
## Young-Middle   -6.25000000 -7.619492 -4.880508 1.0e-10 ***
## 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ### CI for contrast 
    r <- 3 ### Three Groups: young, middle, elderly. ###
    L <- -12.5833 ### From part A. ###
    SE <- 1.1158 ### From part A. ###
    F_val <- 2.47 ### From table with alpha = .9, degrees of freedom from the numerator = 2, degrees of freedom from      the denominator = 33 ###
    F_Val_adj <- sqrt((r-1) * F_val)
    SE_F_val_adj <- SE * F_Val_adj
    Lower_L_Ci <- -12.5833 -  SE_F_val_adj
    Lower_L_Ci
## [1] -15.06329
    Upper_L_CI <- -12.5833 + SE_F_val_adj
    Upper_L_CI
## [1] -10.10331
    L_CI <- c(-15.06329, -10.10331)
    L_CI
## [1] -15.06329 -10.10331
    ### The results of the three pair-wise comparison methods are consistent. The P-values for the middle-elderly and     the young-middle comparisons are all small and are significant at alpha levels .1, .05, and .01 in all tests. And     the CIs for middle-elderly and young-middle all do not contain zero. Conversely the p-values for young-elderly        are large across all comparisons and are not significant at alphas .1, .05 and .01. Additionally the CIs for          young-elderly contain zero for all three comparison methods. All three tests even the most conservative,              Bonferroni method suggest that there are significant differences between the cash offers for cars made to middle      aged people and elderly people, and between middle aged and young people, but not between young and elderly           people. ###