## code on page 1
# read the csv file comprising the carry distance data
# make sure you have this file in your working directory first
carry_distance <- read.csv("Carry Distance.csv")
# let's look at the data
head(carry_distance)
# let's make side by side boxplots 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)
## code on page 3
alpha <- 0.05
#Let 1 = Soft feel and 2 = Z star
save.mean <- aggregate(formula = Carry.Distance ~ Ball, data = carry_distance, FUN = mean)
ybar1 <- save.mean$Carry.Distance[3]
ybar2 <- save.mean$Carry.Distance[4]
save.var <- aggregate(formula = 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(formula = 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 %in% c("Soft Feel", "Z Star"),],var.equal = FALSE, conf.level = 0.95)
### for the part of the code "data = carry_distance[carry_distance$Ball %in% c("Soft Feel", "Z Star")" in the line 49
### you can also use "data = carry_distance[carry_distance$Ball == "Soft Feel" | carry_distance$Ball == "Z Star"),]"
### let's explore what this part of the code "data = carry_distance[(carry_distance$Ball == "Soft Feel" | carry_distance$Ball == "Z Star"),]" does by breaking down the code
### carry_distance$Ball == "Soft Feel" will just give a vector of True and False corresponding to when the Ball Type was actually Soft Feel
carry_distance$Ball == "Soft Feel"
### carry_distance$Ball == "Z Star" will just give a vector of True and False corresponding to when the Ball Type was actually Z Star
carry_distance$Ball == "Z Star"
### carry_distance[carry_distance$Ball == "Soft Feel" | carry_distance$Ball == "Z Star"),] will then give True and False corresponding
### to those rows which are either Soft Feel or Z Star
carry_distance$Ball == "Soft Feel" | carry_distance$Ball == "Z Star"
### finally carry_distance[(carry_distance$Ball == "Soft Feel" | carry_distance$Ball == "Z Star"),] will give those rows of the carry_distance
### dataset that correspond to Ball Type Soft Feel or Z Star
carry_distance[(carry_distance$Ball == "Soft Feel" | carry_distance$Ball == "Z Star"),]
## code on page 4 and 5
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
var.test(x = carry_distance[carry_distance$Ball == "Soft Feel",]$Carry.Distance, y = carry_distance[carry_distance$Ball == "Z Star",]$Carry.Distance,conf.level = 0.95)