Getting Started with R,Part 2 Let us continue getting started with R as we start discussing important statistical concepts in Sports Analytics.
Case-scenario 1 This is the fourth season of outfielder Luis Robert with the Chicago White Socks. If during the first three seasons he hit 11, 13, and 12 home runs, how many does he need on this season for his overall average to be at least 20?
Solution Given that x1=11,x2=13,x3=12
we want to find x4 such that the mean (average) number of home-runs is x¯>=20
Notice that in this case n=4 .
According to the information above: 20×4=11+13+12+x4
so when x4=61 , the home-runs average will be 20.
# Home-runs so far
HR_before <- c(11, 13, 12)
# Average Number of Home-runs per season wanted
wanted_HR <- 20
# Number of seasons
n_seasons <- 4
# Needed Home-runs on season 4
x_4 <- n_seasons*wanted_HR - sum(HR_before)
# Minimum number of Home-runs needed by Robert
x_4
## [1] 44
According to the calculations above, Robert must hit 44 home-runs or better on this season to get an average number of home-runs per season of at least 20.
We could confirm this, by using the function mean() in R
# Robert's performance
Robert_HRs <- c(11, 13, 12,44)
# Find mean
mean(Robert_HRs)
## [1] 20
# Find standard deviation
sd(Robert_HRs)
## [1] 16.02082
# Find the maximum number of home-runs during the four seasons period
max(Robert_HRs)
## [1] 44
# Find the minimum number of home-runs during the four seasons period
min(Robert_HRs)
## [1] 11
We can also use the summary() function to find basic statistics, including the median!
summary(Robert_HRs)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 11.00 11.75 12.50 20.00 20.75 44.00
#Question 1 Now, you must complete the problem below which represents a similar case scenario. You may use the steps that we executed in Case-scenario 1 as a template for your solution.
This is the sixth season of outfielder Juan Soto in the majors. If during the first five seasons he received 79, 108,41,145, and 135 walks, how many does he need on this season for his overall number of walks per season to be at least 100?
walks_bef <- c(79, 108,41,145,135)
walks_wanted <- 100
n_seasons_JS <- 6
x_6 <- n_seasons_JS * walks_wanted - sum(walks_bef)
x_6
## [1] 92
Juan Soto needs 92 walks for his overall walks per season to be 100. Lets make sure below.
#Juan Soto Perfromance
Juan_Soto_walks <- c(79, 108,41,145,135,92)
# Find mean
mean(Juan_Soto_walks)
## [1] 100
Perfect!!!
min(Juan_Soto_walks) # minimum
## [1] 41
max(Juan_Soto_walks) #maximum
## [1] 145
mean(Juan_Soto_walks) #mean
## [1] 100
sd(Juan_Soto_walks) #standard deviation
## [1] 38.20995
summary(Juan_Soto_walks)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 41.00 82.25 100.00 100.00 128.25 145.00
#Case-scenario 2 The average salary of 10 baseball players is 72,000 dollars a week and the average salary of 4 soccer players is 84,000. Find the mean salary of all 14 professional players.
Solution We can easily find the joined mean by adding both mean and dividing by the total number of people.
Let n1=10 denote the number of baseball players, and y1=72000 their mean salary. Let n2=4 the number of soccer players and y2=84000 their mean salary. Then the mean salary of all 16 individuals is: n1x1+n2x2n1+n2
We can compute this in R as follows:
n_1 <- 10
n_2 <- 4
y_1 <- 72000
y_2 <- 84000
# Mean salary overall
salary_ave <- (n_1*y_1 + n_2*y_2)/(n_1+n_2)
salary_ave
## [1] 75428.57
In Average players will make a Salary of $75428.57 a week.
#Question 2 The average salary of 7 basketball players is 102,000 dollars a week and the average salary of 9 NFL players is 91,000. Find the mean salary of all 16 professional players.
getwd()
## [1] "C:/Users/17862/Documents/SPORTS_ANALYTICS/Intro_to_R"
n_1 <- 7
n_2 <- 9
y_1 <- 102000
y_2 <- 91000
# Mean salary overall
salary_ave <- (n_1*y_1 + n_2*y_2)/(n_1+n_2)
salary_ave
## [1] 95812.5
In average players will make $95812.5 per week.
mean(salary_ave)
## [1] 95812.5
#Case-scenario 3 The frequency distribution below lists the number of active players in the Barclays Premier League and the time left in their contract.
Years Number of players 6 28 5 72 4 201 3 109 2 56 1 34
Find the mean,the median and the standard deviation.
What percentage of the data lies within one standard deviation of the mean?
What percentage of the data lies within two standard deviations of the mean?
What percent of the data lies within three standard deviations of the mean?
Draw a histogram to illustrate the data.
#Solution The allcontracts.csv file contains all the players’ contracts length. We can read this file in R using the read.csv() function.
#creating a contract_length dataframe from a csv file
contract_length <- read.table("allcontracts.csv", header = TRUE, sep = ",")
contract_years <- contract_length$years
contract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1 5 3
## [482] 2 4 1 3 3 1 5 5 4 4 2 1 2 4 5 4 6 6
To find the mean and the standard deviation
# Mean
contracts_mean <- mean(contract_years)
contracts_mean
## [1] 3.458918
The average of years left is 3.46
# Median
contracts_median <- median(contract_years)
contracts_median
## [1] 3
The median is 3 years
# Find number of observations
contracts_n <- length(contract_years)
contracts_n
## [1] 499
The numbers of observations are 499
# Find standard deviation
contracts_sd <- sd(contract_years)
contracts_sd
## [1] 1.69686
How spread are the contracts? 1.7 years
What percentage of the data lies within one standard deviation of the mean?
contracts_w1sd <- sum((contract_years - contracts_mean)/contracts_sd < 1)/ contracts_n
# Percentage of observation within one standard deviation of the mean
contracts_w1sd
## [1] 0.8416834
84% of the data lies within one standard deviation of the mean
## Difference from empirical
contracts_w1sd - 0.68
## [1] 0.1616834
The difference between actual and empirical is 16%
What percentage of the data lies within two standard deviations of the mean?
## Within 2 sd
contracts_w2sd <- sum((contract_years - contracts_mean)/ contracts_sd < 2)/contracts_n
contracts_w2sd
## [1] 1
100% of the data lies within two standard deviation of the mean
## Difference from empirical
contracts_w2sd - 0.95
## [1] 0.05
The difference between actual and empirical is 5%
What percent of the data lies within three standard deviations of the mean?
## Within 3 sd
contracts_w3sd <- sum((contract_years - contracts_mean)/ contracts_sd < 3)/contracts_n
contracts_w3sd
## [1] 1
100% of the data lies within three standard deviation of the mean
## Difference from empirical
contracts_w3sd - 0.9973
## [1] 0.0027
The difference between actual and empirical is 2.7%
# Create histogram
hist(contract_years,xlab = "Years Left in Contract",col = "light green",border = "red", xlim = c(0,8), ylim = c(0,225),
breaks = 5)
#Question 3 Use the skills learned in case scenario number 3 on one the following data sets. You may choose only one dataset. They are both available in Canvas. (doubles_hit.csv or triples_hit.csv)
#creating the dataframe from a csv file
length_hits <- read.table("C:/Users/17862/Documents/SPORTS_ANALYTICS/doubles_hit.csv", header = TRUE, sep = ",")
hits <- length_hits$doubles_hit
hits
## [1] 37 4 6 7 9 25 18 11 8 13 15 1 30 30 6 23 14 26 33 23 34 32 9 4 23
## [26] 34 19 29 15 27 18 35 7 7 19 4 38 2 16 15 26 15 3 19 24 33 34 33 38 29
## [51] 19 18 7 7 30 15 31 12 17 21 11 9 35 1 27 27 27 10 35 34 13 5 40 40 11
## [76] 40 29 23 37 22 29 15 24 25 40 14 1 47 49 45 42 40 40 46 41 42 43 45 48 46
#MEan
hits_mean <- mean(hits)
hits_mean
## [1] 23.55
# Median
hits_median <- median(hits)
hits_median
## [1] 23.5
# Find number of observations
hits_n <- length(hits)
hits_n
## [1] 100
# Find standard deviation
hits_sd <- sd(hits)
hits_sd
## [1] 13.37371
hits_w1sd <- sum((hits - hits_mean)/hits_sd < 1)/ hits_n
# Percentage of observation within one standard deviation of the mean
hits_w1sd
## [1] 0.79
## Difference from empirical
hits_w1sd - 0.68
## [1] 0.11
#within 2 sd
hits_w2sd <- sum((hits - hits_mean)/ hits_sd < 2)/hits_n
hits_w2sd
## [1] 1
## Difference from empirical
hits_w2sd - 0.95
## [1] 0.05
## Within 3 sd
hits_w3sd <- sum((hits - hits_mean)/ hits_sd < 3)/hits_n
hits_w3sd
## [1] 1
## Difference from empirical
hits_w3sd - 0.9973
## [1] 0.0027
# Create histogram
hist(hits,main = "Histogram of Double Hits",xlab = "Double Hits" , col = "light blue")