---
title: "Analyzing Golf Ball Carry Distance"
author: "Tyler Wiederich"
date: "Fall 2022"
output:
  word_document: default
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
options(continue = " ")
knitr::opts_chunk$set(prompt = TRUE, 
                      comment = NA, 
                      background = "white",
                      tidy = TRUE, 
                      tidy.opts = list(width.cutoff = 60, blank = FALSE))
```

# Introduction

We will use methods learned recently to analyze golf ball carry distances. Our overall goal is to determine if a less expensive type of golf ball will perform differently than a more expensive type of golf ball. The performance measure is the carry distance of the golf ball (distance ball goes in the air) after being hit by a driver club. To achieve our goal, we will examine the following:   

* Golfers prefer a golf ball that covers the most distance on average. Comparing the mean carry distance across golf ball types can help a golfer make an informed decision as to which ball they should use.

* Golfers prefer a golf ball that is consistent in the distance that it covers. Comparing the variances for the golf ball types can help a golfer make an informed decision as to which ball they should use.

The data in this study was collected by Mark Crossfield, who is a golf YouTuber extraordinaire. He used a launch monitor to determine carry distance of a large number of golf balls hit. The corresponding data that he collected is in the Carry Distance.csv file.

```{r}
# make sure you have this file in the directory of your .rmd file
carry_distance <- read.csv("Carry Distance.csv")

# let's look at the data
head(carry_distance)

```
**Questions**

  * If we want to compare the mean carry distances of two golf ball types, do we have independent samples or dependent samples?  
  
  * What is the population here?

Let's examine a plot comparing the carry distances for the five different golf balls used by Mark.


```{r}
# side-by-side box plots for the carry distances
par(mfrow = c(1,1))

# col = NA removes gray background in box
boxplot(formula = Carry.Distance ~ Ball, 
        data = carry_distance, 
        main = "Box and dot plot", 
        ylab = "Carry distance (yards)",
        xlab = "Ball type", 
        pars = list(outpch=NA), 
        col = NA) 

stripchart(x = carry_distance$Carry.Distance ~ carry_distance$Ball, 
           lwd = 2, 
           col = "red",
           method = "jitter", 
           vertical = TRUE, 
           pch = 1, 
           add = TRUE)
```

**Question** - Do you think there are differences among the means or variances? Remember, a plot like this is meant to give an initial impression of the data relative to the research hypotheses. Focus on shifts of points in addition to the variability, and focus on the location of the medians. 

# Compare mean carry distances

We will focus on the mean carry distances of the Soft Feel and the Z Star golf balls. Soft Feel golf balls cost around 20 dollars, while Z Star golf balls cost around 40 dollars. If there is no significant difference between the mean carry distance of these two types, then a golfer may prefer the Soft Feel due to their lower price.  

Let's first construct the confidence interval for $\mu_1 - \mu_2$, where $\mu_1$ ($\mu_2$) represents the population mean carry distance for the Soft Feel (Z Star) ball. We will use $\alpha = 0.05$.

We know that the $(1-\alpha)100\%$ CI for $\mu_1 - \mu_2$ with $\sigma_{1}^2$ and $\sigma_{2}^2$ unknown and possibly unequal is given by $$\bar{y_1} - \bar{y_2} - t_{\frac{\alpha}{2},v}\sqrt{\frac{s_{1}^2}{n_1}+\frac{s_{2}^2}{n_2}} < \mu_1 - \mu_2 < \bar{y_1} - \bar{y_2} + t_{\frac{\alpha}{2},v}\sqrt{\frac{s_{1}^2}{n_1}+\frac{s_{2}^2}{n_2}}$$ where $t_{\frac{\alpha}{2},v}$ is the $1-\frac{\alpha}{2}$ quantile from a t distribution with $$v = \frac{\left(\frac{s_{1}^2}{n_1}+\frac{s_{2}^2}{n_2}\right)^{2}}{\frac{\left(\frac{s_{1}^2}{n_1}\right)^2}{n_1 -1} + \frac{\left(\frac{s_{2}^2}{n_2}\right)^2}{n_2 -1}}$$ degrees of freedom. 

**Question** - Why does our assumption of unequal $\sigma_{1}^2$ and $\sigma_{2}^2$ make sense? 


```{r}
alpha <- 0.05
#Let 1 = Soft feel and 2 = Z star

save.mean <- aggregate(x = Carry.Distance ~ Ball, 
                       data = carry_distance, 
                       FUN = mean)

ybar1 <- save.mean$Carry.Distance[3]
ybar2 <- save.mean$Carry.Distance[4]

save.var <- aggregate(x = Carry.Distance ~ Ball, 
                      data = carry_distance, 
                      FUN = var)

s.sq1 <- save.var$Carry.Distance[3]
s.sq2 <- save.var$Carry.Distance[4]

save.n <- aggregate(x = Carry.Distance ~ Ball, 
                    data = carry_distance, 
                    FUN = length)

n1 <- save.n$Carry.Distance[3]
n2 <- save.n$Carry.Distance[4]

#Variance unequal
nu <- (s.sq1/n1 + s.sq2/n2)^2 / ( (s.sq1/n1)^2 / (n1 - 1) +  (s.sq2/n2)^2 / (n2 - 1)) 

data.frame(ybar1, ybar2, s.sq1, s.sq2, n1, n2, nu, t.quant = qt(p = 1 - alpha/2, df = nu))

lower <- ybar1 - ybar2 - qt(p = 1 - alpha/2, df = nu) * sqrt(s.sq1/n1 + s.sq2/n2) 

upper <- ybar1 - ybar2 + qt(p = 1 - alpha/2, df = nu) * 
    sqrt(s.sq1/n1 + s.sq2/n2) 

data.frame(lower, upper)

# easier way to verify the calculations
t.test(formula = Carry.Distance ~ Ball, 
       data = carry_distance[carry_distance$Ball == "Soft Feel" | 
                               carry_distance$Ball == "Z Star",],
       var.equal = FALSE, 
       conf.level = 0.95)

# clever way to check ball type
t.test(formula = Carry.Distance ~ Ball, 
       data = carry_distance[carry_distance$Ball %in% c("Soft Feel", "Z Star"),],
       var.equal = FALSE, 
       conf.level = 0.95)
```
From our calculations, we find that the $95 \%$ CI for $\mu_1 - \mu_2$ is (-6.03, 8.88). Because 0 is inside this interval, we *do not* have sufficient evidence to indicate a difference between the mean carry distances for the two types of golf balls.

\newpage

**Exploration** - Please conduct a similar analysis for Q Star and Z Star XV. Interpret your results in context of the golf problem. 

**Questions to take home** 

* Interpret the confidence intervals calculated.
* How can your conclusion above help a golfer save money? 
* Using the confidence intervals calculated, conduct the following hypothesis test and interpret the results.

$$\begin{aligned}H_{o}: \mu_1 - \mu_2 = 0 \\ H_{a}: \mu_1 - \mu_2 \neq 0\end{aligned}$$ 

* How many possible pairwise comparisons can be made for the golf ball data (all 5 types)? Suppose you are conducting a hypothesis test for each possible pairwise comparison and using a level of $\alpha$ for the type I error rate in each test. How does the probability of making at least one type I error for all multiple comparisons compare with $\alpha$?
* What are the consequences of a type I and type II error in this hypothesis test?
* Do you think the selected $\alpha$ value is appropriate? Would you consider a higher or lower $\alpha$ if you were conducting the experiment yourself? Why?

\newpage

# Compare carry distance variability

Next, we are going to examine the consistency among the carry distances for the Soft Feel and Z Star golf balls. This will be done by performing inferences for the ratio of variances.  

We know that a $(1-\alpha)100\%$ CI for the ratio of variances is given by $$\frac{s_{1}^2}{s_{2}^2}F_{1-\frac{\alpha}{2},v_2,v_1} < \frac{\sigma_{1}^2}{\sigma_{2}^2} < \frac{s_{1}^2}{s_{2}^2}F_{\frac{\alpha}{2},v_2,v_1}$$ where $v_1 = n_1 - 1$ and $v_2 = n_2 - 1$. 

```{r}
alpha <- 0.05

data.frame(n1, n2)

qf(p = alpha/2, df1 = n2 - 1, df2 = n1 - 1)

qf(p = 1 - alpha/2, df1 = n2 - 1, df2 = n1 - 1)

lower <- s.sq1/s.sq2 * qf(p = alpha/2, df1 = n2 - 1, df2 
    = n1 - 1)

upper <- s.sq1/s.sq2 * qf(p = 1 - alpha/2, df1 = n2 - 1, 
    df2 = n1 - 1)

data.frame(lower, upper)

# easier way to verify the calculations
soft.feel = carry_distance[carry_distance$Ball == "Soft Feel",]$Carry.Distance
z.star = carry_distance[carry_distance$Ball == "Z Star",]$Carry.Distance
var.test(x = soft.feel, y = z.star,conf.level = 0.95)
```

From our calculations, we find that the $95 \%$ CI for $\frac{\sigma_{1}^2}{\sigma_{2}^2}$ is (0.08, 1.61). Because 1 is inside this interval, we *do not* have sufficient evidence to indicate that the ratio of variances for the carry distances is different from 1.

\newpage

**Exploration** - Please conduct a similar analysis for the golf balls Q Star and Z Star XV. Interpret your results in context of the golf problem. 

**Questions to take home**

* Interpret the confidence intervals calculated.
* Using the confidence intervals calculated, conduct the following hypothesis test and interpret the results. 

$$\begin{aligned}H_{o}: \frac{\sigma_{1}^2}{\sigma_{2}^2} = 1 \\ H_{a}: \frac{\sigma_{1}^2}{\sigma_{2}^2} \neq 1\end{aligned}$$ 

* What are the consequences of a type I and type II error in this hypothesis test?
* Do you think the selected $\alpha$ value is appropriate? Would you consider a higher or lower $\alpha$ if you were conducting the experiment yourself? Why?