---
title: "Session 7 — Residuals"
author: "Prof. Dae-Jin Lee (daelee@faculty.ie.edu)"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(tidyverse)
ci <- read_csv("data/country_indicators.csv")
```

```{r}
fit <- lm(life_expectancy ~ gdp_per_capita, data = ci)

ci |>
  mutate(yhat = fitted(fit), uhat = resid(fit)) |>
  select(country, gdp_per_capita, life_expectancy, yhat, uhat) |>
  arrange(desc(abs(uhat))) |>
  head(8)

ci |>
  mutate(yhat = fitted(fit), uhat = resid(fit)) |>
  ggplot(aes(yhat, uhat)) +
  geom_hline(yintercept = 0) +
  geom_point() +
  labs(x = "Fitted LE", y = "Residual")
```
