Introduction

This activity introduces basic R commands used in sports analytics. The goal is to practice using R as a calculator, creating objects, working with vectors, building simple data frames, and completing basic sports-related calculations.

1. R as a Calculator

R can be used to perform basic arithmetic operations.

# Addition
2 + 2
## [1] 4
# Subtraction
10 - 4
## [1] 6
# Multiplication
8 * 6
## [1] 48
# Division
45 / 5
## [1] 9
# Exponents
3^2
## [1] 9

2. Creating Objects

Objects allow us to save values and reuse them later.

runs_scored <- 763
runs_allowed <- 614

runs_scored
## [1] 763
runs_allowed
## [1] 614

3. Run Differential

Run differential is calculated by subtracting runs allowed from runs scored.

run_differential <- runs_scored - runs_allowed
run_differential
## [1] 149

The team scored 763 runs and allowed 614 runs, producing a run differential of 149.

4. Predicting Wins with a Linear Regression Model

During sports analytics, we can use a simple linear regression model to estimate wins based on run differential.

The model format is:

\[ \text{Predicted Wins} = \text{Intercept} + \text{Slope} \times \text{Run Differential} \]

For this activity, we use the lecture model:

\[ \text{Predicted Wins} = 80.8814 + 0.1058 \times \text{Run Differential} \]

intercept <- 80.8814
slope <- 0.1058

predicted_wins <- intercept + slope * run_differential
predicted_wins
## [1] 96.6456
# Rounded result
round(predicted_wins, 1)
## [1] 96.6
round(predicted_wins, 0)
## [1] 97

Based on this model, the team is expected to win approximately 96.6 games, or about 97 games when rounded to the nearest whole number.

5. Creating Vectors

A vector stores multiple values in one object.

teams <- c("Team A", "Team B", "Team C", "Team D")
wins <- c(97, 88, 81, 74)
losses <- c(65, 74, 81, 88)

teams
## [1] "Team A" "Team B" "Team C" "Team D"
wins
## [1] 97 88 81 74
losses
## [1] 65 74 81 88

6. Basic Vector Calculations

We can use vectors to calculate totals and winning percentages.

games_played <- wins + losses
winning_percentage <- wins / games_played

games_played
## [1] 162 162 162 162
winning_percentage
## [1] 0.5987654 0.5432099 0.5000000 0.4567901
round(winning_percentage, 3)
## [1] 0.599 0.543 0.500 0.457

7. Creating a Data Frame

A data frame is like a table. It can store several variables together.

baseball_standings <- data.frame(
  Team = teams,
  Wins = wins,
  Losses = losses,
  Games_Played = games_played,
  Winning_Percentage = round(winning_percentage, 3)
)

baseball_standings
##     Team Wins Losses Games_Played Winning_Percentage
## 1 Team A   97     65          162              0.599
## 2 Team B   88     74          162              0.543
## 3 Team C   81     81          162              0.500
## 4 Team D   74     88          162              0.457

8. Sorting Data

We can sort teams by wins or winning percentage.

# Sort by wins, highest to lowest
baseball_standings[order(-baseball_standings$Wins), ]
##     Team Wins Losses Games_Played Winning_Percentage
## 1 Team A   97     65          162              0.599
## 2 Team B   88     74          162              0.543
## 3 Team C   81     81          162              0.500
## 4 Team D   74     88          162              0.457
# Sort by winning percentage, highest to lowest
baseball_standings[order(-baseball_standings$Winning_Percentage), ]
##     Team Wins Losses Games_Played Winning_Percentage
## 1 Team A   97     65          162              0.599
## 2 Team B   88     74          162              0.543
## 3 Team C   81     81          162              0.500
## 4 Team D   74     88          162              0.457

9. Summary Statistics

R can quickly calculate descriptive statistics.

mean(wins)
## [1] 85
median(wins)
## [1] 84.5
max(wins)
## [1] 97
min(wins)
## [1] 74
range(wins)
## [1] 74 97
summary(wins)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   74.00   79.25   84.50   85.00   90.25   97.00

10. Simple Sports Analytics Example

The following example compares runs scored, runs allowed, run differential, and predicted wins for multiple teams.

team_stats <- data.frame(
  Team = c("Team A", "Team B", "Team C", "Team D", "Team E"),
  Runs_Scored = c(763, 720, 690, 650, 610),
  Runs_Allowed = c(614, 680, 705, 660, 740)
)

team_stats$Run_Differential <- team_stats$Runs_Scored - team_stats$Runs_Allowed
team_stats$Predicted_Wins <- intercept + slope * team_stats$Run_Differential
team_stats$Predicted_Wins_Rounded <- round(team_stats$Predicted_Wins, 1)

team_stats
##     Team Runs_Scored Runs_Allowed Run_Differential Predicted_Wins
## 1 Team A         763          614              149        96.6456
## 2 Team B         720          680               40        85.1134
## 3 Team C         690          705              -15        79.2944
## 4 Team D         650          660              -10        79.8234
## 5 Team E         610          740             -130        67.1274
##   Predicted_Wins_Rounded
## 1                   96.6
## 2                   85.1
## 3                   79.3
## 4                   79.8
## 5                   67.1

11. Plotting Runs Scored vs. Runs Allowed

Base R can create a simple scatter plot.

plot(
  team_stats$Runs_Allowed,
  team_stats$Runs_Scored,
  main = "Runs Scored vs. Runs Allowed",
  xlab = "Runs Allowed",
  ylab = "Runs Scored",
  pch = 19
)

text(
  team_stats$Runs_Allowed,
  team_stats$Runs_Scored,
  labels = team_stats$Team,
  pos = 4,
  cex = 0.8
)

12. Plotting Run Differential

A bar chart can show which teams performed better based on run differential.

barplot(
  team_stats$Run_Differential,
  names.arg = team_stats$Team,
  main = "Run Differential by Team",
  xlab = "Team",
  ylab = "Run Differential"
)

13. Final Answer for the Assignment Question

Question: If a baseball team scores 763 runs and allows 614 runs, how many games do we expect the team to win?

runs_scored <- 763
runs_allowed <- 614
run_differential <- runs_scored - runs_allowed
expected_wins <- 80.8814 + 0.1058 * run_differential

expected_wins
## [1] 96.6456
round(expected_wins, 1)
## [1] 96.6
round(expected_wins, 0)
## [1] 97

Final answer: The team is expected to win approximately 96.6 games, which rounds to 97 wins.

Conclusion

This activity demonstrates how R can be used for basic sports analytics. We practiced arithmetic, objects, vectors, data frames, sorting, summary statistics, plotting, and applying a simple regression equation to predict baseball wins.