---
title: "11.2.i Activity - Central Limit Theorem"
format: html
number-sections: true
embed-resources: true
execute:
  echo: true   # show code by default
  warning: false
  message: false
---

Be sure to run the following code chunk before continuing with the assignment.  

```{r}
library(tidyverse)
library(stat1080r)
```

We are going to use the `colleges` dataset from the `stat1080r` package for this assignment. Run the following code to make that dataset available in your environment. 

```{r}
colleges <- stat1080r::colleges
```

This dataset contains 831 American colleges that: 

- Offer four-year degree programs
- Have more than 2,000 students
- Have endowments larger than \$10 million

We are going to call this group the population of "large four-year schools". Note that this population does NOT represent ALL schools of higher education in the United States, but rather the "population of interest". We wish to explore the average endowment size of schools within this population. 

# Calculating the Population Parameter 

Rarely do we have access to all the observations in our population. 
Normally, we must take a much smaller sample of observations to get insight into our population. 
However, in this case, we are able to calculate the population parameter of interest (which we will call $\mu$), which is the average size of a university endowment. 
Please use a tidyverse `summarize()` statement to calculate the population average endowment size. 
Use the endowment size at the end of the fiscal year for this calculation.

```{r}
## pop_mu <- colleges |>
##   summarize(#...#) |>
##   pull(#...#)
##
## pop_mu
```

Note that the result is less than one, but it is NOT a proportion — it represents billions of dollars.
Thus, a value of 0.5 in this context does not mean a 50% chance of "yes", but rather means an endowment size of \$500 million. 

# Calculating the Standard Error 

Remember that the expected value of a sample average is equal to the population average. 
This means that the expected value of a sample size of `n` schools will be equal to `pop_mu`, regardless of the value of `n`. 

Unlike with proportions, where the standard error relies on the calculation $\sqrt{p*(1-p)/n}$, the standard error of a sample average (when the population standard deviation ($\sigma$) is known) is $\frac{\sigma}{\sqrt{n}}$. 
In other words, the standard error for our samples can be written in R as: 

```{r}
pop_sd <- colleges |>
  summarize(sd_endow = sd(endowment_end)) |>
  pull(sd_endow)

n <- 10 # replace with your sample size.

s_x <- pop_sd / sqrt(n) 
s_x
```

This in mind, please calculate the standard error of the average endowment size when the sample size is 100. 
How large is the standard error of a sample size of 100 relative to a sample size of 10? 
Calculate this as a percentage. 
Note that sample code is provided for this calculation, assuming you assign the standard error for a sample size of 100 to the variable `s_x_100`. 

```{r}
#...#

# Code to calculate the relative difference between the two standard errors. 
## (s_x_100 / s_x) * 100
```


# Sampling Distribution of the Sample Average Endowment 

The provided function takes a sample of 50 colleges without replacement from the `colleges` dataset and calculates a sample average endowment.
Using that function, please use the `replicate()` function to simulate 1,000 sample averages for sample sizes of 10, 100, and 200 respectively. 
The provided code will then let you compare histograms for the original observation and each of the sample averages. 

```{r}
# The argument n determines the number of colleges to sample. 
get_public_college <- function(n){
  colleges |>
    slice_sample(n = n) |> 
    summarize(mean_endow = mean(endowment_end)) |>
    pull(mean_endow)
}

set.seed(123) # set seed for reproducibility
## sample_avg_10 <- #...#
## sample_avg_100 <- #...#
## sample_avg_200 <- #...#

# Uncomment this code once you have completed lines 94-96. 
## data.frame(value = c(colleges |> pull(endowment_end), 
##                      sample_avg_10, 
##                      sample_avg_100,
##                      sample_avg_200),
##            name = c(rep("original", nrow(colleges)),
##                     rep("n = 10", length(sample_avg_10)),
##                     rep("n = 100", length(sample_avg_100)),
##                     rep("n = 200", length(sample_avg_200)))) |>
##   mutate(name = factor(name, levels = c("original", "n = 10", 
##                                         "n = 100", "n = 200"))) |>
##   ggplot(aes(x = value)) + 
##   geom_histogram(breaks = seq(0, 100, 0.1), color = "black") + 
##   coord_cartesian(xlim = c(0, 10)) + 
##   facet_wrap(~name, ncol = 1, scales = "free_y") 
```

Note that the x-axis scale has been restricted to omit some of the largest original values. 
For example, there are 15 colleges with endowments larger than 10 billion dollars that are not reflected in these histograms, with the largest endowment being \$50.75 billion. 

What do you notice about the shape of the histograms as the sample size increases? 
Please discuss this with a partner and/or write your answer below. 

*Replace this text with your answer. Optional if discussing in groups and required if working on your own.* 

# Endowment Sample Percentiles 

The following code calculates the 90th percentile endowment size for schools. 

```{r}
pop_q90 <- colleges |>
  summarize(q90_endow = quantile(endowment_end, 0.9)) |>
  pull(q90_endow) |>
  unname()

pop_q90
```

Calculate the proportion of sample averages from a sample size of 100 that exceed this 90th percentile (HINT: You should be using the variable `sample_avg_100` and the `mean()` function to answer this question). 

```{r}
#...#
```

# Normal Approximations 

Now repeat the probability calculation using the normal approximation. 
You should be using the expected value and standard error of a sample size of 100, along with `pnorm()` to answer this question. 

```{r}
#...#
```

Note that your answer highlights one of the issues with averages: when data are highly right-skewed, meaning that when you have a few observations with substantially higher values than the typical observation, the average value is not representative of the "typical" value. 
In this case, there is a fairly good chance that our sample average will be larger than 90% of the observed values in our dataset! 
Averages work best for data that are not so skewed. 