---
title: "Worksheet 10"
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 gain experience with hypothesis tests for two means using parametric hypothesis tests.  




$\\$





<!-- 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 
 
```







**Question 1a**: In the last worksheet, you assessed 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). To do this you used a *permutation test* in which you created a null distribution by combing the data from the two leagues, shuffling the data, splitting into two random groups, recomputing the statistic of interest on these shuffled groups, and then repeating this process 10,000 times. 

Let's repeat this process now using parametric methods (i.e., a t-test) in which the null distrubtion is given by a t-distribution (rather than shuffling the data 10,000 times). To start, state again the null hypothesis in symbols. 


**Answer:** 




$\\$






**Exercise 1b**: A vector containing the number of runs scored by each team scored in for each league is given in the R chunk below (as was also done in worksheet 9). Use this data to then calculate the following statistics: 

1) Calculate the difference in the mean number of runs scored in these two leagues and store this difference in a variable called obs.diff. 

2) Calculate the sample sizes $n_{AL}$ and $n_{NL}$ for the AL and NL teams and store them in variables called num.teams.AL and num.teams.NL. 

3) Calculate the standard deviation $s_{AL}$, $s_{NL}$ for the teams in the AL and in the NL, and store the results in variables sd.AL and sd.NL. 

4) Calculate the pooled sample standard deviation which is given by:  $\sqrt{\frac{s_{AL}^2}{n_{AL}} + \frac{s_{NL}^2}{n_{NL}}}$  (to see this equation more clearly, knit the document to a pdf and look at the equation) and save it in a variable called pooled.sd. 

5) Finally, calculate the t-statistic which is the the observed difference (obs.diff) divided by the pooled standard deviation (pooled.sd) and save it to a variabled called t.statistic. Report what the value of the t-statistic is. 


```{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 1c**:  As discussed in class, a t-distribution also has one parameter called "the degrees of freedom". If we set the degrees of freedom correctly, then the t-distribution we use will be an appropriate null distribution for a t-statistic calculated above. One (conservative) estimate of the degrees of freedom is to find the sample with the fewest number of points in it, and subtract one from this sample size. Report what the sample size is for both the AL and NL samples, and report what the degrees of freedom are here. 


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



 
```

**Answer:** 





$\\$






**Exercise 1d**: Now that we have the t-statistic and the degrees of freedom, we can easily calculate a p-value using the pt() function which gives us the probability of getting a statistic as or more extreme than the observed t-statistic from a t-distribution with the specified degrees of freedom. In R, we can use the pt() function as follows to get a p-value:  pt(t.statistic, degrees.of.freedom, lower.tail = FALSE). Run this function and get the p-value for this data. What conclusion would you draw, and how does this p-value compare to the p-value found using the permutaiton tests in question 2 of worksheet 9? 

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




```

**Answer:** 






$\\$






**Exercise 1e**: R conveniently also has a built in funciton to do a t-test which is called t.test(). We can run the full t-test using this function as: t.test(sample.one, sample.two, alternative = "greater"). Use the t.test() function to compare the AL and NL team runs from 2013. What is the t-statistic, degrees of freedom (df) and p-value returned by the t.test function? Does this funciton give similar p-value to the t-test run above in exercise 1d? From looking at the output of the t-test, do you have any ideas why it might give slightly different results? 
```{r message=FALSE, warning=FALSE, tidy=TRUE} 




```

**Answer:** 







$\\$






**Question 2:**  For this question we will assess whether there was there a change in the mean height of a baseball player who played in the earlier years compared to baseball players who played more recently - i.e., if there were infinitely many baseball players, would the players who played today be taller than players who played in the past? Let's test whether the infinite potential population of players born after 1960 is taller (on average) than the population born before 1960 by doing the following steps: 

1) State the null and alternative hypotheses in symbols and in words

2) Write down the observed statistic of interest 

3) Use the appropriate test to compare the mean heights for those born before vs after 1960 and report the p-value. 

4) Is there evidence that players who played after 1960 are taller than players who played before 1960? 

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

heights.before.1960 <- filter(Master, birthYear < 1960)$height
heights.after.1960 <- filter(Master, birthYear > 1960)$height





```




**Answer:** 

1) 




2) 




3) 




4) 












