---
title: "Week 4 Lecture 1 Predictive Modeling"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(gapminder)
```

### Predicting Life Expectancy

Suppose we want to predict life expectancy from the GDP per capita. As we saw, it makes sense to draw a straight line through the log-log plot of `lifeExp` vs. `gdpPercap`:

```{r}
gap.1982 <- gapminder %>% filter(year == 1982)
ggplot(gap.1982, mapping = aes(x = log(gdpPercap), y = log(lifeExp))) +
         geom_point() + 
         geom_smooth(method = "lm")
```
       
       
Let's now get the equation for the best line through the data:

```{r}
lm(log(lifeExp) ~ log(gdpPercap), data = gap.1982)
```

For example, for a country with a `gdpPercap` of $10,000, we would predict a `log(lifeExp)` of

```{r}
3.0527 + 0.1266 * log(10000)
```

The prediction for `lifeExp` will be

```{r}
exp(3.0527 + 0.1266 * log(10000))
```

We can get all the predictions using

```{r}
model <- lm(log(lifeExp) ~ log(gdpPercap), data = gap.1982)
predict(model, newdata = gap.1982)
```

We can also get the prediction for a `gdpPercap` of 1000 by creating our own data frame:
```{r}
new.df <- data.frame(gdpPercap = 10000)
exp(predict(model, newdata = new.df))
```

For `gap.1982`, we can compute the sum of squared errors as follows.

The errors (for predicting the log of the life expectancy) are

```{r}
log(gap.1982$lifeExp) - predict(model, newdata = gap.1982)
```

The sum of the squared errors is then

```{r}
sum((log(gap.1982$lifeExp) - predict(model, newdata = gap.1982))^2)
```

## Multiple linear regression

Let's now use both the `log(gdpPercap)` and the year to predict the log of the life expectancy. We'll use the full `gapminder` dataset -- otherwise using the year doesn't really make sense.

```{r}
model2 <- lm(log(lifeExp) ~ log(gdpPercap) + year, data = gapminder)
```

We'll also define the old model that ignores the year:

```{r}
model1 <- lm(log(lifeExp) ~ log(gdpPercap), data = gapminder)
```

Let's look at `model2`:

```{r}
model2
```

The implied equation is

$\log(\text{lifeExp}) \approx -4.054829 + 0.135050\log(\text{gdpPercap}) + 0.003543\text{year}$

What does that mean? Here's an example.

How is the predictions of $\log(\text{lifeExp})$ are going to differ between 1980 and 1981, if the gdpPercap is still the same? The predictions would differ by $0.003543\times(1981-1980) = 0.003543$ (Why?).

#### An aside on $\log$

$\log(ab) = \log(a) + \log(b)$. This means that if $\log(b)$ increases by 2, $ab$ increases by a factor of $\exp(2)$.

In our case, if year increases by 1, $0.003543\text{year}$ increases by $0.003543$, so the predicted $\log(\text{lifeExp})$ increases by $0.003543$, so the predicted `lifeExp` increases by a factor of $\exp(0.003543)$ (i.e., the new prediction is $\exp(0.003543)$ times the old one).




### Which model would have the smaller sum of squared errors?

We can compare the two models:

```{r}
model1 <- lm(log(lifeExp) ~ log(gdpPercap), data = gapminder)
model2 <- lm(log(lifeExp) ~ log(gdpPercap) + year, data = gapminder)
sum((log(gapminder$lifeExp) - predict(model1, newdata = gapminder))^2)
sum((log(gapminder$lifeExp) - predict(model2, newdata = gapminder))^2)
```

We expect `model2` to do a better job of predicting the life expectancy, since it can also take the year into account. (And if it didn't make sense to do so, it wouldn't -- it can always just multiply the log(gdpPercap) by 0 and get the same prediction as model 1, if that's the best that can be done.)

As we might expect, the sum of squared errors is larger for `model1` than for model2`.


### Predictions using categorical variables

Let's predict the life expectancy using continent and the year:

```{r}
model3 <- lm(lifeExp ~ continent + year, data = gapminder)
model3
```

For Americas in 1980, the prediction will be `-596.2613 + 15.7934 + 1980*0.3259`. For Africa in 1990, the prediction will be `-596.2613 + 0.3259*1990` (Note that we don't add a coefficient for Africa, since R chose to only use indicator variables for the other four continents).


