# Read in the csv file

kickData<-read.csv("kicks.csv", header=TRUE)
kickData
summary(kickData)

# Create function to calculate the descriptive statistics

generateStats<-function(x)
{
  # Create empty vector to add values to
  xStats<-NULL
  
  # Calculate the statistics we want 
  xStats<-c(xStats, mean(x, na.rm=TRUE))
  xStats<-c(xStats, median(x, na.rm=TRUE))
  xStats<-c(xStats, max(x, na.rm=TRUE))
  xStats<-c(xStats, min(x, na.rm=TRUE))
  xStats<-c(xStats, sd(x, na.rm=TRUE))
  
  # This Calculates the range
  xRange<-range(x, na.rm=TRUE)
  xStats<-c(xStats, xRange[2]-xRange[1])
  
  
  # Return our summary table at the end
  return(xStats)
}



# Create labels for our data frame which inclues the descriptive statistics

StatsLabels<-c("Mean", "Median", "Max", "Min", "Standard Deviation", "Range")


# Calculate descriptive statistics for each type of filled balls, using function created above

StatsAF<-generateStats(kickData$Air)
StatsHF<-generateStats(kickData$Helium)


# Create data frame from labels, $Air and $Helium data

kickSummary<-data.frame(Labels=StatsLabels,AirFilled=StatsAF, HeliumFilled=StatsHF)
kickSummary


# Create vectors for the two different samples

SampleAir<-kickData$Air
SampleHelium<-kickData$Helium


# Use Shapiro test to see if data is normal for both air filled and helium filled samples
shapiro.test(SampleAir) #p-value > 0.05 and so normally distributed
shapiro.test(SampleHelium) #p-value < 0.05 and so not normally distributed


# Create boxplot to show outliers for the Helium Filled samples and see if any for the Air

boxplot(SampleHelium, main="Helium") 
boxplot(SampleAir, main="Air") 


# Looks like there is one outlier for Air at 15/16 so filter this out of the data

SampleAir<-SampleAir[SampleAir > 16]
boxplot(SampleAir, main="Air")


# Check still normally distributed

shapiro.test(SampleAir) #p-value still > 0.05 and so normally distributed


# Outliers look like they are above 35 and below 20 for Helium so remove these from the data sample. Remove lower end first

SampleHelium<-SampleHelium[SampleHelium>= 20]
boxplot(SampleHelium, main="Helium")


# Remove top end outliers

SampleHelium<-SampleHelium[SampleHelium<= 35]
boxplot(SampleHelium, main="Helium")



# Check if normally distributed
shapiro.test(SampleHelium) #p-value now > 0.05 so normally distributed


# Perform t-test on the normally distributed samples
# H0: There is no difference between the distances travelled, i.e. both air filled and helium filled travel the same distance
# H1: There is a difference between the distances travelled, i.e. the ball filled with one type travels further than another


# Perform the t-test

t.test(SampleHelium, SampleAir)

# The calculated p-value for this is 0.09543. This is more than 0.05,
# this means we cannot reject the null hypothesis at the p=0.05 level.
# Therefore we accept the null hypothesis that the type of air that fills the ball has no difference on the distance travelled



# Create function which assigns all odd numbered rows to one vector and all even numbered rows to another

identifypararity<-function(y)
{
  #Create empty vectors to put the even and odd row data in
  evenKicks<-NULL
  oddKicks<-NULL
  
  for (i in seq(1:length(y[,1])))
  {
    if (y[i,1] %% 2 == 0) # checks if the row number is even and then outputs the data
    {
      evenKicks<-c(evenKicks,y[i,2])
      evenKicks<-c(evenKicks,y[i,3])
    }
    if (y[i,1] %% 2 != 0) # checks if the row number is odd and then outputs the data
      {
      oddKicks<-c(oddKicks,y[i,2])
      oddKicks<-c(oddKicks,y[i,3])
      }
  }

# Create summary table which assigns odd kicks to Neil and even to Jonny    
kickersummary<-data.frame(Neil = oddKicks,Jonny =evenKicks)
return(kickersummary)
}

# Run function on kickData

kickerData<-identifypararity(kickData)
JonnySample<-kickerData$Jonny
NeilSample<-kickerData$Neil


# Check that these are normally distributed using Shapiro test

shapiro.test(NeilSample)#p-value > 0.05 so normally distributed
shapiro.test(JonnySample)#p-value < 0.05 so not normally dsitributed


# Check if there are any outliers for Neil

boxplot(NeilSample, main="Neil")


# Remove the one outlier

NeilSample<-NeilSample[NeilSample>15]
boxplot(NeilSample, main="Neil")


# Iterate filter again

NeilSample<-NeilSample[NeilSample<35]
boxplot(NeilSample, main="Neil")


# Check data still normal

shapiro.test(NeilSample) #p-value > 0.05 so normally distributed


# Look for outliers

boxplot(JonnySample, main="Jonny")


# Outliers found so filter out samples < 18

JonnySample<-JonnySample[JonnySample>=18]
boxplot(JonnySample, main="Jonny")


# Still have outliers so iterate again

JonnySample<-JonnySample[JonnySample>21]
boxplot(JonnySample, main="Jonny")

JonnySample<-JonnySample[JonnySample<32]
boxplot(JonnySample, main="Jonny")


# No more outliers for check normal distribution again

shapiro.test(JonnySample)#p-value > 0.05 so normally dsitributed

# Now we can perform the t-test
# Ho:There is no difference between the two kickers
# H1: There is a significant difference between the two kickers

t.test(JonnySample, NeilSample)

# The calculated p-value for this is 0.3667. This is more than 0.05,
# this means we cannot reject the null hypothesis at the p=0.05 level.
# Therefore we accept the null hypothesis that the kicker has no difference on the distance travelled
