---
title: "2.4: Standard Errors"
output: 
  html:
    self-contained: TRUE
---

To get started, attach the tidyverse to your session and read the love island data set:

```{r, message = F}
library(tidyverse)
love <- read_csv("https://raw.githubusercontent.com/cobriant/320data/master/love.csv") %>%
  mutate(win = str_detect(outcome, "winner|runner up|third place"))
```

Then estimate the linear probability model: $$\text{Prob(win)}_i \sim \beta_0 + \beta_1 age_i + u_i$$

```{r}
love %>% 
  lm(win ~ age, data = .) %>%
  broom::tidy()
```

In assignment 2.3 you learned how to get the estimates for the coefficients $\hat{\beta_0} = -0.493$ and $\hat{\beta_1} = 0.0288$: they come from OLS formulas for the estimators of the parameters of the line of best fit.

In this assignment, we'll explore the other values in the table: the standard errors `std.error`, the test statistics `statistic`, and the p-values `p.value`.

## OLS Hypothesis Tests

<iframe width="759" height="527" src="https://www.youtube.com/embed/wEaqfK9OmRI" title="2.4.1 OLS Hypothesis Tests" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

<div style="background-color:#d2ff96; border-width:3px; border-style:solid; border-color:#d2ff96; padding: 1em;">
**Exercise 1:** The regression output shows $\hat{\beta_1} = 0.0288$ with a standard error of 0.0147. 

   a) Calculate the t-statistic for $\hat{\beta_1}$. Show your work.
   b) The sample size of $n = 96$ is large. Is the t-statistic greater than 1.96?
   c) Based on your answers to (a) and (b), do we reject $H_0: \beta_1 = 0$ at the 5% significance level? That is, does age have a statistically significant effect on the probability of winning at the 5% level?
   d) Compare your answers to the `lm()` results table. Did you get the same statistic? An equivalent way to do the hypothesis test is to look at the p-value from the regression output and compare it to the significance level. According to the p-value, does age have a statistically significant effect on the probability of winning at the 10% level? What about the 1% level?
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answers:* 

a) t-statistic: $\frac{\hat{\beta}_1}{SE} = $ 
b) 
c) 
d) 

</div>

## Assumptions for Standard Errors

For our standard errors and hypothesis tests to be valid, we need several key assumptions:

1. The linear model is an accurate model of the true data generating process
2. Exogeneity: $E[u_i|X] = 0$
3. Homoskedasticity: $Var(u_i|X) = \sigma^2_u$ (constant variance)
4. No Autocorrelation: $Cov(u_i, u_j|X) = 0$ for $i \neq j$, which could fail for time series data
5. Normality: Either $u$ is normally distributed, or the sample size is large enough for the Central Limit Theorem

The first three assumptions can be quite heroic depending on the application. We can use heteroskedasticity-robust standard errors to help mitigate the heteroskedasticity issue, but if the linear model is inaccurate or if exogeneity fails, OLS is biased and standard errors are wrong.

<div style="background-color:#d2ff96; border-width:3px; border-style:solid; border-color:#d2ff96; padding: 1em;">
**Exercise 2:** For each scenario below, identify which OLS assumption might be violated:

a) Analyzing house prices where expensive houses tend to have more variable prices
b) Time series data where this year's GDP is closely related to last year's GDP
c) A very small sample of only 10 observations
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 

a) 
b) 
c) 
</div>

## Formula for Standard Errors

Let's derive the formula for the standard error of $\hat{\beta_1}$ step by step.

<iframe width="759" height="527" src="https://www.youtube.com/embed/oajwvDwYSIM" title="2.4.2 Standard Error Derivation, part 1 of 4" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

<iframe width="759" height="527" src="https://www.youtube.com/embed/306ikTmHKTA" title="2.4.3 Standard Error Derivation, part 2 of 4" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

<iframe width="758" height="527" src="https://www.youtube.com/embed/eFrzcEYMnZA" title="2.4.4 Standard Error Derivation, part 3 of 4" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

<iframe width="759" height="527" src="https://www.youtube.com/embed/MutDKfWRWS0" title="2.4.5 Standard Error Derivation, part 4 of 4" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

<div style="background-color:#d2ff96; border-width:3px; border-style:solid; border-color:#d2ff96; padding: 1em;">
**Exercise 3:** Using the love island data, calculate using R:

a) $\sum_i (x_i - \bar{x})^2$ where x is age
b) $\sum_i e_i^2$ using the residuals
c) The standard error of $\hat{\beta_1}$ using the formula above
d) Verify your answer matches the regression output

```{r}
love %>%
  mutate(age_demean = age - mean(age))
```
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 
```{r}
# a <- love %>%
#   summarize(a = sum(___)) %>%
#   pull(a)

# b <- love %>% 
#   lm(___) %>%
#   residuals() %>%
#   .^2 %>%
#   ___()

# sqrt(___)

# love %>% 
#   lm(win ~ age, data = .) %>%
#   broom::tidy() %>%
#   select(std.error) %>%
#   slice(2)
```
</div>

## Standard Error Intuition

The formula for $Var(\hat{\beta_1})$ tells us three key things about precision:

1. More observations (larger n) → more precise estimates
2. More variance in x → more precise estimates
3. More variance in u → less precise estimates

<div style="background-color:#d2ff96; border-width:3px; border-style:solid; border-color:#d2ff96; padding: 1em;">
**Exercise 4:** Explain how we know these three key things about the precision of our estimates by referring to the formula for the standard error.
</div>

<div style="background-color:white; border-width:3px; border-style:solid; border-color:black; padding:1em;">
*Answer:* 

- n is in the denominator, so for large n, the standard error ___.
- the variance of u is in the numerator, so for large variance in u, the standard error ___.
- the variance of x is in the denominator, so for large variance in x, the standard error ___.
</div>
