The chapter began with the problem of overfitting, a universal phenomenon by which models with more parameters fit a sample better, even when the additional parameters are meaningless. Two common tools were introduced to address overfitting: regularizing priors and estimates of out-of-sample accuracy (WAIC and PSIS). Regularizing priors reduce overfitting during estimation, and WAIC and PSIS help estimate the degree of overfitting. Practical functions compare in the rethinking package were introduced to help analyze collections of models fit to the same data. If you are after causal estimates, then these tools will mislead you. So models must be designed through some other method, not selected on the basis of out-of-sample predictive accuracy. But any causal estimate will still overfit the sample. So you always have to worry about overfitting, measuring it with WAIC/PSIS and reducing it with regularization.
Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Make sure to include plots if the question requests them.
Finally, upon completion, name your final output .html
file as: YourName_ANLY505-Year-Semester.html and publish
the assignment to your R Pubs account and submit the link to Canvas.
Each question is worth 5 points.
7-1. When comparing models with an information criterion, why must all models be fit to exactly the same observations? What would happen to the information criterion values, if the models were fit to different numbers of observations? Perform some simulations.
library(ggplot2)
library(tidyverse)
library(rethinking)
data("Howell1")
d <- Howell1[complete.cases(Howell1), ]
d_500 <- d[sample(1:nrow(d), size = 500, replace = FALSE), ]
d_400 <- d[sample(1:nrow(d), size = 400, replace = FALSE), ]
d_300 <- d[sample(1:nrow(d), size = 300, replace = FALSE), ]
m_500 <- map(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b * log(weight)
),
data = d_500,
start = list(a = mean(d_500$height), b = 0, sigma = sd(d_500$height))
)
m_400 <- map(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b * log(weight)
),
data = d_400,
start = list(a = mean(d_400$height), b = 0, sigma = sd(d_400$height))
)
m_300 <- map(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b * log(weight)
),
data = d_300,
start = list(a = mean(d_300$height), b = 0, sigma = sd(d_300$height))
)
(model.compare <- compare(m_500, m_400, m_300))
## WAIC SE dWAIC dSE pWAIC weight
## m_300 1819.522 29.92014 0.0000 NA 3.523231 1.000000e+00
## m_400 2437.525 31.02349 618.0024 48.16707 3.247745 6.345770e-135
## m_500 3068.705 35.15393 1249.1832 51.87483 3.094406 5.537421e-272
7-2. What happens to the effective number of parameters, as measured by PSIS or WAIC, as a prior becomes more concentrated? Why? Perform some simulations.
data("Howell1")
d <- Howell1[complete.cases(Howell1), ]
d$height.log <- log(d$height)
d$height.log.z <- (d$height.log - mean(d$height.log)) / sd(d$height.log)
d$weight.log <- log(d$weight)
d$weight.log.z <- (d$weight.log - mean(d$weight.log)) / sd(d$weight.log)
m_wide <- map(
alist(
height.log.z ~ dnorm(mu, sigma),
mu <- a + b * weight.log.z,
a ~ dnorm(0, 10),
b ~ dnorm(1, 10),
sigma ~ dunif(0, 10)
),
data = d
)
m_narrow <- map(
alist(
height.log.z ~ dnorm(mu, sigma),
mu <- a + b * weight.log.z,
a ~ dnorm(0, 0.10),
b ~ dnorm(1, 0.10),
sigma ~ dunif(0, 1)
),
data = d
)
WAIC(m_wide, refresh = 0)
## WAIC lppd penalty std_err
## 1 -102.8707 55.60379 4.168465 36.6166
#The pwaic decreases as the priors become more concentrated.
7-3. Consider three fictional Polynesian islands. On each there is a Royal Ornithologist charged by the king with surveying the bird population. They have each found the following proportions of 5 important bird species:
| Island | Species A | Species B | Species C | Species D | Species E |
| 1 | 0.2 | 0.2 | 0.2 | 0.2 | 0.2 |
| 2 | 0.8 | 0.1 | 0.05 | 0.025 | 0.025 |
| 3 | 0.05 | 0.15 | 0.7 | 0.05 | 0.05 |
Notice that each row sums to 1, all the birds. This problem has two parts. It is not computationally complicated. But it is conceptually tricky. First, compute the entropy of each island’s bird distribution. Interpret these entropy values. Second, use each island’s bird distribution to predict the other two. This means to compute the KL divergence of each island from the others, treating each island as if it were a statistical model of the other islands. You should end up with 6 different KL divergence values. Which island predicts the others best? Why?
island1 <- c(0.2, 0.2, 0.2, 0.2, 0.2)
island2 <- c(0.8, 0.1, 0.05, 0.025, 0.025)
island3 <- c(0.05, 0.15, 0.7, 0.05, 0.05)
entropy1 <- -sum(island1 * log(island1))
entropy2 <- -sum(island2 * log(island2))
entropy3 <- -sum(island3 * log(island3))
entropy1
## [1] 1.609438
entropy2
## [1] 0.7430039
entropy3
## [1] 0.9836003
DKL <- function(p,q) sum( p*(log(p)-log(q)) )
Dm <- matrix( NA , nrow=3 , ncol=3 )
Dm[1,1] <- DKL (island1, island1)
Dm[1,2] <- DKL (island1, island2)
Dm[1,3] <- DKL (island1, island3)
Dm[2,1] <- DKL (island2, island1)
Dm[2,2] <- DKL (island2, island2)
Dm[2,3] <- DKL (island2, island3)
Dm[3,1] <- DKL (island3, island1)
Dm[3,2] <- DKL (island3, island2)
Dm[3,3] <- DKL (island3, island3)
Dm
## [,1] [,2] [,3]
## [1,] 0.0000000 0.9704061 0.6387604
## [2,] 0.8664340 0.0000000 2.0109142
## [3,] 0.6258376 1.8388452 0.0000000
# As the results suggest, island 1 predicts other island the most.
7-4. Recall the marriage, age, and happiness collider bias example from Chapter 6. Run models m6.9 and m6.10 again (page 178). Compare these two models using WAIC (or PSIS, they will produce identical results). Which model is expected to make better predictions? Which model provides the correct causal inference about the influence of age on happiness? Can you explain why the answers to these two questions disagree?
d <- sim_happiness(seed=990, N_years=1000)
d <- d %>%
as_tibble() %>%
filter(age > 17) %>%
mutate(age = (age - 18)/ (65 -18)) %>%
mutate(mid = married +1)
m6_9 <- alist(
happiness ~ dnorm( mu , sigma ),
mu <- a[mid] + bA*age,
a[mid] ~ dnorm( 0 , 1 ),
bA ~ dnorm( 0 , 2 ),
sigma ~ dexp(1)) %>%
quap(data = d)
m6_10 <- alist(
happiness ~ dnorm( mu , sigma ),
mu <- a + bA*age,
a ~ dnorm( 0 , 1 ),
bA ~ dnorm( 0 , 2 ),
sigma ~ dexp(1)) %>%
quap(data = d)
c(m6_9, m6_10) %>%
map_dfr(WAIC) %>%
add_column(model = c("m6_9", "m6_10"), .before = 1)
## model WAIC lppd penalty std_err
## 1 m6_9 2764.015 -1378.383 3.624173 36.89099
## 2 m6_10 3102.073 -1548.605 2.431971 27.79481
7-5. Revisit the urban fox data, data(foxes), from the previous chapter’s practice problems. Use WAIC or PSIS based model comparison on five different models, each using weight as the outcome, and containing these sets of predictor variables:
Can you explain the relative differences in WAIC scores, using the fox DAG from the previous chapter? Be sure to pay attention to the standard error of the score differences (dSE).
data("foxes")
foxes_q <- foxes %>%
as_tibble() %>%
mutate(across(-group, standardize))
m1 <- alist(
weight ~ dnorm(mu, sigma),
mu <- a + Bf*avgfood + Bg*groupsize + Ba*area,
a ~ dnorm(0, 0.2),
c(Bf, Bg, Ba) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = foxes_q)
m2 <- alist(
weight ~ dnorm(mu, sigma),
mu <- a + Bf*avgfood + Bg*groupsize,
a ~ dnorm(0, 0.2),
c(Bf, Bg) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = foxes_q)
m3 <- alist(
weight ~ dnorm(mu, sigma),
mu <- a + Bg*groupsize + Ba*area,
a ~ dnorm(0, 0.2),
c(Bg, Ba) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = foxes_q)
m4 <- alist(
weight ~ dnorm(mu, sigma),
mu <- Bf*avgfood,
a ~ dnorm(0, 0.2),
Bf ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = foxes_q)
m5 <- alist(
weight ~ dnorm(mu, sigma),
mu <- a + Ba*area,
a ~ dnorm(0, 0.2),
Ba ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = foxes_q)
compare(m1, m2, m3, m4, m5)
## WAIC SE dWAIC dSE pWAIC weight
## m1 323.2988 16.41026 0.0000000 NA 4.858114 0.405967083
## m2 323.7937 15.99188 0.4949447 3.683576 3.670268 0.316967650
## m3 324.1221 15.87261 0.8232912 2.803876 3.839407 0.268977170
## m4 331.7604 13.88174 8.4615844 7.213152 1.575466 0.005903113
## m5 333.7481 13.73632 10.4493276 7.294025 2.659469 0.002184984
# The results show that mnodels 4 and 5 look different from rest of the models.