---
title: "Worksheet 9"
output:
  pdf_document: default
  html_notebook: default
---



## Author: Enter name of the author here

## Discussants: Enter the names of other people who you discussed this problem set with


$\\$


The goal of this worksheet is to practice running hypothesis tests for two proportions and two means using randomization methods.  



$\\$




<!-- This R chunk sets some parameters that will be used in the rest of the document. -->
```{r message=FALSE, warning=FALSE, tidy=TRUE, echo = FALSE}

 library(knitr)
 
 # makes sure the code is wrapped to fit when it creats a pdf
 opts_chunk$set(tidy.opts=list(width.cutoff=60)) 
 
 set.seed(1)  # set the random number generator to always give the same sequence of random numbers

 require("Lahman") # you need to run this every time you use the Lahman database
 require("dplyr")  # use the dplyr functions to manipulate data 
 
```



**Exercise 1a**:  In this exercise you will run a hypothesis test to assess whether the home run ability of David Ortiz is higher than the home run ability of Derek Jeter. To measure their home run performance, we will look at the proporiton of plate appearances that they hit home runs (i.e., HR/PA). Over David Ortiz's career, David hit 541 home runs in 10091 plate appearances. Over Derek's Jeter's career, Derek hit 260 home runs in 12602 plate appearances.  Start by stating the null and alternative hypothesis in words and in symbols to test whether David Ortiz has a higher home run rate ability than Derek Jeter. Hint: some of the following symbols might be useful: $H_0$, $\pi_{Ortiz}$, $\hat{p}_{Jeter}$, etc.


**Answer:**  





$\\$





**Exercise 1b**: Now calculate the observed statistic of interest for the difference in home run proportions. Report David Ortiz's and Derek Jeter's observed home run rates (i.e., proportions), and also what the difference in their rates is.  Based on just looking at their home run rates, do you have any guess as to whether their home run rate abilities are different? 
```{r message=FALSE, warning=FALSE, tidy=TRUE}




```

**Answer:** 





$\\$




**Exercise 1c**:  Next create a null distribution that would occur if the null hypothesis was true. We can do this using the following steps: First compute the combined home run rate of Ortiz and Jeter that you would expect if they had the same ability. Then for 10,000 simulations, generate how many home runs would be expected for the 10091 plate appearances for David Ortiz using this combined homerun rate, and normalize this by the 10091 plate appearances Dave Ortize had to get 10,000 simulated home run rates (hint: use the rbinom() function). Similarly, repeat this process for the 12602 plate appearances for Derek Jeter. Then substract the 10,000 simulations of Ortiz's data from the 10,000 simulations of Jeter's data to get a null distribution of simulated expected differences in home run rates between these players *if the null hypothesis was true*. Hint: see class slides if you are stuck (but also think about what the null distribution is telling you). Bonus: plot the null distribution below as a histogram.

```{r message=FALSE, warning=FALSE, tidy=TRUE}



 
```





$\\$




**Exercise 1d**: Now calculate the p-value which is the proportion of values in the null distribution that is greater than or equal to the observed difference in home run rates. Does this p-value give evidence that David Ortiz's home run rate is higher than Derek Jeter's? Explain your answer. 
 
 
```{r message=FALSE, warning=FALSE, tidy=TRUE} 



 
```


**Answer:** 




$\\$






**Question 2a**: Let’s assess whether the mean number of runs that American League teams (AL) score is greater than the mean number of runs that National League teams (NL) score using data from the 2013 season (i.e., if there were infinitely many teams in the American and National League, would the mean number of runs that AL teams scored be higher than the mean number of runs NL teams scored). Start by stating the null hypothesis in words and using symbols. 



**Answer:** 




$\\$





**Exercise 2b**: A vector containing the number of runs scored by each team scored in for each league is calculated in the R chunk below. Use this data to calculate the mean number of runs scored in the AL and NL, and the difference in the mean number of runs scored in these two leagues, and report these numbers. Also create a variable that has the number of teams in the AL, and a variable that has the number of teams in the NL.   
```{r message=FALSE, warning=FALSE, tidy=TRUE} 

teams.2013.AL.runs <- filter(Teams, yearID == 2013, lgID == "AL")$R
teams.2013.NL.runs <- filter(Teams, yearID == 2013, lgID == "NL")$R





```


**Answer:** 




$\\$





**Exercise 2c**:  Now create a null distribution for the difference in means runs between the leagues that you would typically see if null hypothesis was true. To do this first combined the data from both leagues into a single vector called combined.team.runs using the c() function. Then use a for loop to repeat this process 10,000 times:  

1) Shuffle the order of the values in this vector using the sample() function and save the shuffled data in a variable called shuffled.team.runs.  

2) Take the first 15 teams from the shuffled.team.runs vector and assign them to a variable shuff.AL, and the last 15 teams and assign them to a variable shuff.NL.

3) Take the mean of the shuffled AL teams assign them to a vector called mean.AL.shuff, and take the mean of the shuffled NL teams assign them to a vector called mean.NL.shuff.

4) Take the difference of these shuffled means which will give you one point in the null distribution, and repeating this process 10,000 times in the for loop will give you a full distribution. 

Again, look at the code in the lecture slides if you get stuck. Also, plot a histogram of the null distribution. 

```{r message=FALSE, warning=FALSE, tidy=TRUE}



 
```





$\\$





**Exercise 2d**: Now calculate the p-value which is the proportion of values in the null distribution that is as great or greater than the observed difference in runs scored between the AL and NL. Does this p-value give evidence that the AL scores more runs than the NL? 
 
```{r message=FALSE, warning=FALSE, tidy=TRUE} 




```

**Answer:**  






$\\$




