library(tidyverse)
library(gapminder) # install.packages("gapminder") if you do not have it
# Same data as the slides: countries in 2007, with log GDP per capita
g07 <- gapminder |> filter(year == 2007) |> mutate(lgdp = log(gdpPercap))
m1 <- lm(lifeExp ~ lgdp, data = g07)You Try Answers: Simple Linear Regression
POSC 8410
You Try
1. Regress lifeExp on unlogged gdpPercap. Compare \(R^2\) to the logged version. Which fits better, and why?
m0 <- lm(lifeExp ~ gdpPercap, data = g07)
c(unlogged = summary(m0)$r.squared,
logged = summary(m1)$r.squared) unlogged logged
0.4605827 0.6544490
The logged model fits better. Log GDP accounts for 65% of the variation in life expectancy; GDP in dollars accounts for 46%.
The reason is the shape of the relationship. Plot both versions:
g07 |>
pivot_longer(c(gdpPercap, lgdp), names_to = "scale", values_to = "x") |>
mutate(scale = if_else(scale == "gdpPercap",
"GDP per capita (dollars)", "log GDP per capita")) |>
ggplot(aes(x, lifeExp)) +
geom_point(alpha = .5, color = "grey35") +
geom_smooth(method = "lm", se = FALSE, color = "#522D80") +
facet_wrap(~ scale, scales = "free_x") +
labs(x = NULL, y = "Life expectancy") +
theme_minimal(base_size = 12)In dollars, life expectancy rises steeply among poor countries and then levels off. An extra $1,000 matters enormously in a country with $1,500 per person and barely at all in one with $40,000. A straight line cannot capture those dynamics, so it misses badly in both regions. Taking logs stretches out the poor countries and compresses the rich ones, which turns “each extra dollar matters less” into “each doubling matters the same,” which is closer to a straight line.
The residual standard error tells the same story: a typical miss of 8.9 years unlogged against 7.1 years logged.
This comparison of \(R^2\) is fair because both models predict the same outcome, lifeExp. You could not compare \(R^2\) between a model for lifeExp and one for log(lifeExp), because they would be explaining variation in different quantities.
2. Compute the slope by hand with cov and var. Does it match lm?
with(g07, cov(lgdp, lifeExp) / var(lgdp)) # by hand[1] 7.202802
coef(m1)["lgdp"] # from lm lgdp
7.202802
Yes, they match exactly. This is the formula from the slides, \(\hat{\beta}_1 = \widehat{\text{Cov}}(X, Y) / \widehat{\text{Var}}(X)\).
3. Verify the fitted line passes through \((\bar{x}, \bar{y})\).
Ask the model for its prediction at the mean of \(X\), and compare that with the mean of \(Y\):
predict(m1, newdata = tibble(lgdp = mean(g07$lgdp))) # fitted value at x-bar 1
67.00742
mean(g07$lifeExp) # y-bar[1] 67.00742
They are identical. This follows from the first least squares condition. Setting the derivative with respect to \(\beta_0\) to zero gave \(\bar{y} = \hat{\beta}_0 + \hat{\beta}_1 \bar{x}\), which says precisely that the point \((\bar{x}, \bar{y})\) lies on the line.
You can also check it with the coefficients directly:
coef(m1)[1] + coef(m1)[2] * mean(g07$lgdp) - mean(g07$lifeExp) # ~zero (Intercept)
-1.421085e-14
4. Center lgdp by subtracting its mean and refit. Which coefficient changed, and which did not?
g07 <- g07 |> mutate(lgdp_c = lgdp - mean(lgdp))
m1c <- lm(lifeExp ~ lgdp_c, data = g07)
rbind(original = coef(m1),
centered = coef(m1c)) (Intercept) lgdp
original 4.949612 7.202802
centered 67.007423 7.202802
The intercept changed; the slope stayed the same.
- The slope stays at 7.20. Subtracting a constant from \(X\) slides every point left by the same amount.
- The intercept becomes \(\bar{y}\) = 67.01. After centering, \(X = 0\) means “a country at the mean of log GDP.”
The old intercept, 4.95, was the prediction for a country with $1 per person, far outside the data. The new one is the prediction for a typical country. I