# Relationship Between Strikeouts and Home Runs

```
# set some default options for chunks
knitr::opts_chunk$set(
warning = FALSE, # avoid warnings and messages in the output
message = FALSE,
collapse = TRUE, # collapse all output into a single block
tidy = FALSE, # don't tidy our code-- assume we do it ourselves
fig.height = 5,
fig.width = 5
)
options(digits=4) # number of digits to display in output; can override with chunk option R.options=list(digits=)
par(mar=c(3,3,1,1)+.1)
set.seed(1234) # reproducibility
```

```
library(Lahman)
library(ggplot2)
library(dplyr)
library(car)
```

This vignette looks at the relationship between rate of strikeouts and home runs from the year 1950+. This question was inspired by Marchi and Albert (2016), "Analyzing Baseball Data in R."

There are many factors that must come together for a player to launch a home run. One of those factors is swing speed—against a 94-mph fastball, every 1-mph increase in swing speed extends distance about 8 feet (Coburn, 2009). If a batter hits ~50 home runs in a season, is it safe to assume that he’s swinging for the fences, and also more likely to strike out? Babe Ruth broke the record of most home runs in a season (60) and also struck out more than any other player (89). However, in 1971, Willie Stargell hit 48 home runs and struck out 154 times, while Henry Aaron hit 47 home runs and struck out 58 times, demonstrating that home runs and strikeouts do not always go hand in hand.

## The data files

Start with loading the files we will use here. We do some pre-processing to make them more convenient for the analyses done later.

### The `Batting`

data

The `Batting`

table contains batting data at the team level going back to 1871, with a separate observation from each year. This file is available using the newest v. `r packageVersion("Lahman")`

, of the `Lahman`

package. We use this to get everything we need for our analysis: at bats (AB) strikeouts (SO), and home runs (HR) for all teams since the year 1950+.

```
data("Batting", package="Lahman") # load the data
str(Batting) # take a look at the structure of the complete data set, as it is
```

We are only using part of the table, so we will filter the data set to include only the variables that we need.

We'll also create a new data frame that includes data from the year 1950+. The Batting table also has multiple listings for each year, so we'll collapse them using the summarize function.

Last, we will mutate the variables so that home runs and strikeouts are divided by at bat, to add new columns "SO rate" and "HR rate." This full data frame will be called FullBatting.

```
Batting <- Batting %>%
select(yearID, AB, SO, HR) %>% # select the variables that we need
group_by(yearID) %>% # group by year, so that each row is one year
summarise_each(funs(sum)) # we want the sum of AB, HR, and SO in the other rows
FullBatting<- Batting %>% # create a new variable that has SO rate and HR rate
filter(yearID >= 1950) %>% # select the years from 1900+
mutate(SO_rate = (SO/AB)*100, HR_rate = (HR/AB)*100) #add SO rate and HR rate as percentages to our data frame
some(FullBatting) # look at a set of random observations
```

`dim(FullBatting) # show the dimensions of the data frame`

##A first look at 'Batting'

What is the total number of strikeouts in our data set?

`sum(FullBatting$SO) # find the sum of strikeout column`

What is the average rate of strikeouts per at bat?

`mean(FullBatting$SO_rate) # find the mean of the strikeout rate column`

How many homeruns do we have in our data set?

`sum(FullBatting$HR) # find the sum of home run column`

What is the average rate of home runs per at bat?

`mean(FullBatting$HR_rate) # find the mean of the home run rate column`

Is there a relationship between strikeout rate and home run rate? According to our test, there is a significant correlation. The p-value is equal to .001, with df= 65. There is a .61 correlation between strikeout rate and home run rate.

```
corr <- cor.test(FullBatting$SO_rate, FullBatting$HR_rate)
corr # find the correlation between strikeout rate and home run rate
```

We can look at the totals for interpretation purposes. We see here that for every 6.14 strikeouts, home runs increase by 4.14.

```
Model_Totals <- lm(SO_rate~HR_rate, data=FullBatting)
summary(Model_Totals) # look at the model totals
```

Create a scatterplot in ggplot, using SO rate and HR rate.

```
plot <- ggplot(FullBatting, aes(x= SO_rate, y= HR_rate))+
geom_point()+
xlab("Strikeout Rate") +
ylab("Home Run Rate") +
ggtitle("Relationship Between Strikeouts and Home Runs")
plot + stat_smooth(method= "lm") ##stat_smooth fits the model and then we plot the linear regression model
```