# Golf ball distance data


######################################################################
#Read in data

  gb <- read.csv(file = "c:\\data\\stat705\\GolfBalls.csv")
  gb

  gb$Person <- factor(gb$Person)
  head(gb)
  class(gb$Person)

  #Reshape data
  gb2 <- rbind(data.frame(Person = gb$Person, Type = "TitleistB", Distance = gb$TitleistB),
    data.frame(Person = gb$Person, Type = "Maxfli", Distance = gb$Maxfli),
    data.frame(Person = gb$Person, Type = "TitleistP", Distance = gb$TitleistP),
    data.frame(Person = gb$Person, Type = "TopFlite", Distance = gb$TopFlite))
  head(gb2)
  tail(gb2)

  #Another way
  gb.temp <- stack(x = gb, select = c(TitleistB, Maxfli, TitleistP, TopFlite))
  gb.temp$Person <- rep(x = 1:20, times = 4)
  gb.temp
  
  #The reshape() function would work too
  
  
######################################################################
# Plots

  #Not adjusted for block
  boxplot(formula = Distance ~ Type, data = gb2, main = "Box and dot plot", ylab = "Distance",
      xlab = "Golf ball", pars = list(outpch=NA), col = NA) #Not helpful here due to small sample size for each treatment level
  stripchart(x = gb2$Distance ~ gb2$Type, lwd = 2, col = "red",
      method = "jitter", vertical = TRUE, pch = 1, add = TRUE)

  # Adjusted for block
  distance.block.adj <- aggregate(x = Distance ~ Person, data = gb2, FUN = mean)
  distance.block.adj
  gb3 <- merge(x = gb2, y = distance.block.adj, by.x = "Person", by.y = "Person",
    suffixes = c("",".person")) 
  head(gb3)
  tail(gb3)
  gb3$Distance.adj <- gb3$Distance - gb3$Distance.person
  head(gb3)
  
  boxplot(formula = Distance.adj ~ Type, data = gb3, main = "Box and dot plot", ylab = "Distance adjusted for person",
      xlab = "Golf ball", pars = list(outpch=NA), col = NA)
  stripchart(x = gb3$Distance.adj ~ gb3$Type, lwd = 2, col = "red",
      method = "jitter", vertical = TRUE, pch = 1, add = TRUE)


######################################################################
# aov() approach with agricolae

  #Main calculations
  mod.fit <- aov(formula = Distance ~ Type + Person, data = gb2)
  # mod.fit <- aov(formula = Distance ~ Type + factor(Person), data = gb2) # If Person was not changed to a factor class type
  summary(mod.fit)
  TukeyHSD(x = mod.fit, conf.level = 0.95, which = "Type")
   
  #Additional calculations
  qf(p = 0.95, df1 = 3, df2 = 57)
  1 - pf(q = 6.3621, df1 = 3, df2 = 57) 

  aggregate(x = Distance.adj ~ Type, data = gb3, FUN = mean) #Compare to "diff" column in TukeyHSD
 
 
  # What if the data was analyzed instead as a CRD?
  mod.fit.CRD <- aov(formula = Distance ~ Type, data = gb2)
  summary(mod.fit.CRD)
 
  # What if the data was analyzed instead as a CRD using the block adjusted data?
  mod.fit.adjust <- aov(formula = Distance.adj ~ Type, data = gb3)
  summary(mod.fit.adjust)
 
 
  # agricolae package - use for LSD and Bonferoni, http://cran.r-project.org/web/packages/agricolae/index.html
  options(width = 60)  # Helpful to avoid printing too wide for notes
  library(package = agricolae)
  save.LSD <- LSD.test(y = mod.fit, trt = "Type", alpha = 0.05, group = FALSE, p.adj = "none")  #LSD
  save.LSD
  save.Bon <- LSD.test(y = mod.fit, trt = "Type", alpha = 0.05, group = FALSE, p.adj = "bonferroni") #Bonferroni
  save.Bon

  # Examine what happes without group = FALSE! A crude plot summarizing the results is produced
 
  # Multiple comparisons with pairwise.t.test - gives incorrect answers for RCB
  pairwise.t.test(x = gb2$Distance, g = gb2$Type, p.adjust.method = "none",
     alternative = "two.sided", paired = TRUE)


################################################################################
# Linear mixed effects model

  # Estimate model with random effect for block
  library(package = lme4)
  mod.fit.lmer <- lmer(formula = Distance ~ Type + (1 | Person), data = gb2)
  summary(mod.fit.lmer)

  anova(mod.fit.lmer)

  library(package = emmeans)
  calc.est <- emmeans(mod.fit.lmer, specs = ~ Type)
  # calc.est <- emmeans(mod.fit.lmer, specs = "Type")
  # calc.est
  test.info <- contrast(object = calc.est, method = "pairwise", adjust = "none")
  test.info
  ci.info <- confint(object = test.info, adjust = "none", level = 0.95)
  ci.info

  test.info <- contrast(object = calc.est, method = "pairwise", adjust = "Bonferroni")
  test.info
  ci.info <- confint(object = test.info, adjust = "Bonferroni", level = 0.95)
  ci.info

  test.info <- contrast(object = calc.est, method = "pairwise", adjust = "Tukey")
  test.info
  ci.info <- confint(object = test.info, adjust = "Tukey", level = 0.95)
  ci.info


  # Another way to estimate model with random effect for block
  library(package = nlme)
  mod.fit.lme <- lme(Distance ~ Type, random = ~1|Person, data = gb2)
  summary(mod.fit.lme)
  anova(mod.fit.lme)

  library(package = emmeans)
  calc.est <- emmeans(mod.fit.lme, specs = "Type")
  calc.est
  test.info <- contrast(object = calc.est, method = "pairwise", adjust = "none")
  test.info
  ci.info <- confint(object = test.info, adjust = "none", level = 0.95)
  ci.info


  # What happens if use the aov() model fit?
  calc.est <- emmeans(mod.fit, specs = ~ Type)
  test.info <- contrast(object = calc.est, method = "pairwise", adjust = "none")
  test.info
  ci.info <- confint(object = test.info, adjust = "none", level = 0.95)
  ci.info



















       
#