### Topics 

1. Point estimates
2. Sampling distribution of the sample mean
3. Simulation

A natural way to estimate features of a population, such as a population mean, is to use the corresponding summary statistic calculated from a sample drawn from the population; a sample mean is a point estimate of a population mean. If a different sample is drawn, the new sample mean would likely be different as a result of sampling variability. This lab explores the relationship between point estimates and population parameters through simulation.


### Background information

This lab uses data from the Youth Risk Behavioral Surveillance System (YRBSS), a yearly survey conducted by the US Centers for Disease Control to measure health-related activity in high-school aged youth. The dataset `yrbss` contains responses from the 13,572 participants in 2013 for a subset of the variables included in the complete survey data.

Variables in `yrbss` include:

- `age`: age in years
- `gender`: gender of participant, recorded as either `female` or `male`
- `grade`: grade in high school (9-12)
- `height`: height, in meters (1 m = 3.28 ft)
- `weight`: weight, in kilograms (1 kg = 2.2 lbs)

The CDC used the response from the 13,572 students to estimate the health behaviors of the target population: the 21.2 million high school aged students in the United States in 2013.

The goal in this lab is to observe the effect of sampling by treating the 13,572 individuals in `yrbss` as a target population and drawing random samples. How do point estimates of mean weight, $\overline{x}_{weight}$, calculated from random samples compare to the population parameter, $\mu_{weight}$?

### Install packages 

You will need `openintro` and `tidyverse` packages. We will also use the **infer** package for resampling.

```{r load-packages, message=FALSE}
if (!require("openintro")) {install.packages("openintro"); require("openintro")}
if (!require("tidyverse")) {install.packages("tidyverse"); require("tidyverse")}
if (!require("infer")) {install.packages("infer"); require("infer")}
library(openintro)
library(tidyverse)
library(infer)
```

### load the data 

Load the dataset `yrbss` available in the `openintro` package

```{r load-data}
# load the data

```

Inspect the data using the `View` command 

```{r, eval = FALSE}

```

Use `?sample` in the console to understand how the `sample` function works. Essentially, it returns a random sample of specified size from the specified data frame.

We use `set.seed(5011)` so that each time we run the chunk the same random sample will  be returned. This helps us correct any syntax errors as we write code and makes our code reproducible. 

### Exercise 1 Taking one sample

1. Run the following code to take a random sample of 10 individuals from `yrbss` and store the subset as `yrbss.sample`.

<!---
Set eval = TRUE for the results to display in the knitted html output
--->


```{r samp, eval = FALSE}
#set parameters
sample.size = 10

#obtain random sample of row numbers
set.seed(5011)
sample.rows = sample(1:nrow(yrbss), sample.size)

#create yrbss.sample
yrbss.sample = yrbss[sample.rows, ]
yrbss.sample
```

You can also use the `sample_n` command to sample from the population. Check help on this command by using `?sample_n` in the console. 

```{r samp1, eval = FALSE}
set.seed(5011)
samp1 <- yrbss %>%
  sample_n(10)
samp1
```

a) Which rows of `yrbss` were sampled from?


    
```{r}

``` 

    
    
    
b) How many individuals of each gender have been sampled?



```{r}

```

Hint: use `group_by` 
    
    
c) What is the mean age of the sampled students?


```{r}

```
Hint: use `summarise` 
<!---
Recall that to omit missing values before a summary statistic is computed, specify na.rm = TRUE.
--->

    
d) Calculate $\overline{x}_{weight}$ and $s_{weight}$, the mean and standard deviation of weight in the sample.


    
```{r mean_wt}

```
```{r sd_wt}

```

It is a good practice to avoid using function names as variables(column names). `yrbss.sample%>% summarise(sd = sd(weight, na.rm = T))` would  work, but could be confusing further down in your code file. Do you see why? 

Using `yrbss.sample%>% summarise(sd_wt = sd(weight, na.rm = T))` is better

<!---
Recall that to omit missing values before a summary statistic is computed, specify na.rm = TRUE.
--->
    
e) Calculate $\mu_{weight}$, the mean weight in the `yrbss` population.




```{r}

```

    
### Exercise 2. Take another sample 

Take a new random sample of size 10 from `yrbss`, changing the seed to be the four digits representing your birth month and day (e.g., `1028} for October 28).

```{r }

```


a) Run the code below to get the five summary statistics for the weights in `yrbss.sample`. 


```{r eval=FALSE}
summary(yrbss.sample$weight)
```
```{r eval=FALSE}
yrbss.sample %>% 
  summarize( Min = min(weight,na.rm = T),
             Q1 = quantile(weight, .25, na.rm = T),
             Avg_weight = mean(weight,na.rm = T), 
             Q3 = quantile(weight, .75, na.rm = T),
             Max = max(weight,na.rm = T)
             )
```

See what happens if  you do not include `na.rm=T`. Are there any missing values in your sample?


b) What is $\overline{x}_{weight}$, as calculated from your sample? Does it differ from $\overline{x}_{weight}$ as calculated in part d) of the previous question? How do these point estimates compare to the population mean $\mu_{weight}$?



Not surprisingly, every time you take another random sample, you might get a different sample mean.
It's useful to get a sense of just how much variability you should expect when estimating the population mean this way.
The distribution of sample means, called the *sampling distribution (of the sample mean)*, can help you understand this variability.
In this lab, because you have access to the population, you can build up the sampling distribution for the sample mean by repeating the above steps many times.


### Exercise 3 Taking many samples



Run the following code to take 1,000 random samples of size 10 from population `yrbss`. For each sample, the code calculates mean weight for participants in the sample and stores the resulting mean value in `sample_means10` and also draws the histogram of weights in these 1000 samples. 

Note that we specify that `replace = TRUE` since sampling distributions are constructed by sampling with replacement.

```{r iterate10, eval=FALSE}
#set parameters
sample.size = 10
replicates = 1000

set.seed(5011)
sample_means10 <- yrbss %>%
                    rep_sample_n(size = sample.size, reps = replicates, replace = TRUE) %>% summarise(mean_wt = mean(weight, na.rm = T)) 

#create histogram of sample means
hist(sample_means10$mean_wt, xlim = c(54, 87)) #xlim keeps the axis scale fixed

#draw a blue line at the mean of sample means
abline(v = mean(sample_means10$mean_wt, na.rm=TRUE), col = "blue")

#draw a red line at the population mean weight in yrbss
abline(v = mean(yrbss$weight, na.rm = TRUE), col = "red")
```

The idea behind the `rep_sample_n` function is *repetition*.
Earlier, you took a single sample of size `n` (10) from the population of all people in the population.
With this new function, you can repeat this sampling procedure `reps` times in order to build a distribution of a series of sample statistics, which is called the **sampling distribution**.

Note that in practice one rarely gets to build true sampling distributions, because one rarely has access to data from the entire population.

Without the `rep_sample_n` function, this would be painful.

    a) Describe the distribution of sample means.
    

_Note: The histogram shows a visual approximation of the theoretical sampling distribution of $\overline{X}$ with $n = 10$_
    
    
    b) Explore the effect of larger sample sizes by re-running the code for sample sizes of 25, 100, and 300. Describe how the distribution of sample means changes as sample size increases.
    



    
    
    c) Recall that the goal of inference is to learn about characteristics of a target population through the information obtained by taking one sample. What is the advantage of a larger sample size?
    

    
    d) From what you have observed in this exercise about the relationship between a point estimate for the mean $\overline{x}$ and the population mean ($\mu$), evaluate the following statement:
    
        "Since the mean weight of the 13,572 sampled high school students in the 2013 YRBSS survey is 67.91 kg, it is possible to definitively conclude that the mean weight of the 21.2 million high school students in the US in 2013 is also 67.91 kg."
        



------------------------------------------------------------------------

This lab is modified from the Open Intro Biostatistics lab at openintro.org
