This week 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(rethinking)
data(Howell1)
set.seed(6)
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 1862.175 27.91431 0.0000 NA 3.437152 1.000000e+00
## m_400 2419.639 33.94881 557.4644 46.34780 3.442396 8.874615e-122
## m_500 3054.604 35.32283 1192.4289 53.17046 3.257117 1.167789e-259
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 (at least 4).
data <- Howell1[complete.cases(Howell1), ]
data$height_std <- (log(data$height) - mean (log(data$height))) / sd(log(data$height))
data$weight_std <- (log(data$weight) - mean (log(data$weight))) / sd(log(data$weight))
m1 <- map(
alist(
height_std ~ dnorm(mu, sigma),
mu <- a + b * weight_std,
a ~ dnorm(0, 10),
b ~ dnorm(1, 10),
sigma ~ dunif(0, 10)
),
data = data
)
m2 <- map(
alist(
height_std ~ dnorm(mu, sigma),
mu <- a + b * weight_std,
a ~ dnorm(0, 1),
b ~ dnorm(1, 1),
sigma ~ dunif(0, 1)
),
data = data
)
WAIC(m1, refresh = 0)
## WAIC lppd penalty std_err
## 1 -102.7631 55.62747 4.24594 36.5361
WAIC(m2, refresh = 0)
## WAIC lppd penalty std_err
## 1 -102.7109 55.62624 4.270777 36.46584
PSIS(m1)
## PSIS lppd penalty std_err
## 1 -102.5858 51.29288 4.363906 36.50849
PSIS(m2)
## PSIS lppd penalty std_err
## 1 -102.656 51.32801 4.299804 36.57027
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:
| height | weight | age | male |
|---|---|---|---|
| 151.7650 | 47.825606 | 63.00 | 1 |
| 139.7000 | 36.485807 | 63.00 | 0 |
| 136.5250 | 31.864838 | 65.00 | 0 |
| 156.8450 | 53.041914 | 41.00 | 1 |
| 145.4150 | 41.276872 | 51.00 | 0 |
| 163.8300 | 62.992589 | 35.00 | 1 |
| 149.2250 | 38.243476 | 32.00 | 0 |
| 168.9100 | 55.479971 | 27.00 | 1 |
| 147.9550 | 34.869885 | 19.00 | 0 |
| 165.1000 | 54.487739 | 54.00 | 1 |
| 154.3050 | 49.895120 | 47.00 | 0 |
| 151.1300 | 41.220173 | 66.00 | 1 |
| 144.7800 | 36.032215 | 73.00 | 0 |
| 149.9000 | 47.700000 | 20.00 | 0 |
| 150.4950 | 33.849303 | 65.30 | 0 |
| 163.1950 | 48.562694 | 36.00 | 1 |
| 157.4800 | 42.325803 | 44.00 | 1 |
| 143.9418 | 38.356873 | 31.00 | 0 |
| 121.9200 | 19.617854 | 12.00 | 1 |
| 105.4100 | 13.947954 | 8.00 | 0 |
| 86.3600 | 10.489315 | 6.50 | 0 |
| 161.2900 | 48.987936 | 39.00 | 1 |
| 156.2100 | 42.722696 | 29.00 | 0 |
| 129.5400 | 23.586784 | 13.00 | 1 |
| 109.2200 | 15.989118 | 7.00 | 0 |
| 146.4000 | 35.493574 | 56.00 | 1 |
| 148.5900 | 37.903281 | 45.00 | 0 |
| 147.3200 | 35.465224 | 19.00 | 0 |
| 137.1600 | 27.328918 | 17.00 | 1 |
| 125.7300 | 22.679600 | 16.00 | 0 |
| 114.3000 | 17.860185 | 11.00 | 1 |
| 147.9550 | 40.312989 | 29.00 | 1 |
| 161.9250 | 55.111428 | 30.00 | 1 |
| 146.0500 | 37.506388 | 24.00 | 0 |
| 146.0500 | 38.498621 | 35.00 | 0 |
| 152.7048 | 46.606578 | 33.00 | 0 |
| 142.8750 | 38.838815 | 27.00 | 0 |
| 142.8750 | 35.578623 | 32.00 | 0 |
| 147.9550 | 47.400364 | 36.00 | 0 |
| 160.6550 | 47.882306 | 24.00 | 1 |
| 151.7650 | 49.413179 | 30.00 | 1 |
| 162.8648 | 49.384829 | 24.00 | 1 |
| 171.4500 | 56.557252 | 52.00 | 1 |
| 147.3200 | 39.122310 | 42.00 | 0 |
| 147.9550 | 49.895120 | 19.00 | 0 |
| 144.7800 | 28.803092 | 17.00 | 0 |
| 121.9200 | 20.411640 | 8.00 | 1 |
| 128.9050 | 23.359988 | 12.00 | 0 |
| 97.7900 | 13.267566 | 5.00 | 0 |
| 154.3050 | 41.248522 | 55.00 | 1 |
| 143.5100 | 38.555320 | 43.00 | 0 |
| 146.7000 | 42.400000 | 20.00 | 1 |
| 157.4800 | 44.650463 | 18.00 | 1 |
| 127.0000 | 22.010552 | 13.00 | 1 |
| 110.4900 | 15.422128 | 9.00 | 0 |
| 97.7900 | 12.757275 | 5.00 | 0 |
| 165.7350 | 58.598416 | 42.00 | 1 |
| 152.4000 | 46.719976 | 44.00 | 0 |
| 141.6050 | 44.225220 | 60.00 | 0 |
| 158.8000 | 50.900000 | 20.00 | 0 |
| 155.5750 | 54.317642 | 37.00 | 0 |
| 164.4650 | 45.897841 | 50.00 | 1 |
| 151.7650 | 48.024053 | 50.00 | 0 |
| 161.2900 | 52.219779 | 31.00 | 1 |
| 154.3050 | 47.627160 | 25.00 | 0 |
| 145.4150 | 45.642695 | 23.00 | 0 |
| 145.4150 | 42.410852 | 52.00 | 0 |
| 152.4000 | 36.485807 | 79.30 | 1 |
| 163.8300 | 55.933563 | 35.00 | 1 |
| 144.1450 | 37.194544 | 27.00 | 0 |
| 129.5400 | 24.550667 | 13.00 | 1 |
| 129.5400 | 25.627948 | 14.00 | 0 |
| 153.6700 | 48.307548 | 38.00 | 1 |
| 142.8750 | 37.336292 | 39.00 | 0 |
| 146.0500 | 29.596878 | 12.00 | 0 |
| 167.0050 | 47.173568 | 30.00 | 1 |
| 158.4198 | 47.286966 | 24.00 | 0 |
| 91.4400 | 12.927372 | 0.60 | 1 |
| 165.7350 | 57.549485 | 51.00 | 1 |
| 149.8600 | 37.931631 | 46.00 | 0 |
| 147.9550 | 41.900561 | 17.00 | 0 |
| 137.7950 | 27.584063 | 12.00 | 0 |
| 154.9400 | 47.201918 | 22.00 | 0 |
| 160.9598 | 43.204638 | 29.00 | 1 |
| 161.9250 | 50.263663 | 38.00 | 1 |
| 147.9550 | 39.377456 | 30.00 | 0 |
| 113.6650 | 17.463292 | 6.00 | 1 |
| 159.3850 | 50.689000 | 45.00 | 1 |
| 148.5900 | 39.434154 | 47.00 | 0 |
| 136.5250 | 36.287360 | 79.00 | 0 |
| 158.1150 | 46.266384 | 45.00 | 1 |
| 144.7800 | 42.269104 | 54.00 | 0 |
| 156.8450 | 47.627160 | 31.00 | 1 |
| 179.0700 | 55.706767 | 23.00 | 1 |
| 118.7450 | 18.824068 | 9.00 | 0 |
| 170.1800 | 48.562694 | 41.00 | 1 |
| 146.0500 | 42.807745 | 23.00 | 0 |
| 147.3200 | 35.068331 | 36.00 | 0 |
| 113.0300 | 17.888534 | 5.00 | 1 |
| 162.5600 | 56.755699 | 30.00 | 0 |
| 133.9850 | 27.442316 | 12.00 | 1 |
| 152.4000 | 51.255896 | 34.00 | 0 |
| 160.0200 | 47.230267 | 44.00 | 1 |
| 149.8600 | 40.936678 | 43.00 | 0 |
| 142.8750 | 32.715323 | 73.30 | 0 |
| 167.0050 | 57.067543 | 38.00 | 1 |
| 159.3850 | 42.977842 | 43.00 | 1 |
| 154.9400 | 39.944446 | 33.00 | 0 |
| 148.5900 | 32.460178 | 16.00 | 0 |
| 111.1250 | 17.123098 | 11.00 | 1 |
| 111.7600 | 16.499409 | 6.00 | 1 |
| 162.5600 | 45.954540 | 35.00 | 1 |
| 152.4000 | 41.106775 | 29.00 | 0 |
| 124.4600 | 18.257078 | 12.00 | 0 |
| 111.7600 | 15.081934 | 9.00 | 1 |
| 86.3600 | 11.481547 | 7.60 | 1 |
| 170.1800 | 47.598810 | 58.00 | 1 |
| 146.0500 | 37.506388 | 53.00 | 0 |
| 159.3850 | 45.019006 | 51.00 | 1 |
| 151.1300 | 42.269104 | 48.00 | 0 |
| 160.6550 | 54.856282 | 29.00 | 1 |
| 169.5450 | 53.523856 | 41.00 | 1 |
| 158.7500 | 52.191429 | 81.75 | 1 |
| 74.2950 | 9.752228 | 1.00 | 1 |
| 149.8600 | 42.410852 | 35.00 | 0 |
| 153.0350 | 49.583275 | 46.00 | 0 |
| 96.5200 | 13.097469 | 5.00 | 1 |
| 161.9250 | 41.730464 | 29.00 | 1 |
| 162.5600 | 56.018612 | 42.00 | 1 |
| 149.2250 | 42.155707 | 27.00 | 0 |
| 116.8400 | 19.391058 | 8.00 | 0 |
| 100.0760 | 15.081934 | 6.00 | 1 |
| 163.1950 | 53.098613 | 22.00 | 1 |
| 161.9250 | 50.235314 | 43.00 | 1 |
| 145.4150 | 42.524250 | 53.00 | 0 |
| 163.1950 | 49.101334 | 43.00 | 1 |
| 151.1300 | 38.498621 | 41.00 | 0 |
| 150.4950 | 49.810071 | 50.00 | 0 |
| 141.6050 | 29.313383 | 15.00 | 1 |
| 170.8150 | 59.760746 | 33.00 | 1 |
| 91.4400 | 11.708343 | 3.00 | 0 |
| 157.4800 | 47.939005 | 62.00 | 1 |
| 152.4000 | 39.292407 | 49.00 | 0 |
| 149.2250 | 38.130077 | 17.00 | 1 |
| 129.5400 | 21.999212 | 12.00 | 0 |
| 147.3200 | 36.882700 | 22.00 | 0 |
| 145.4150 | 42.127357 | 29.00 | 0 |
| 121.9200 | 19.787951 | 8.00 | 0 |
| 113.6650 | 16.782904 | 5.00 | 1 |
| 157.4800 | 44.565414 | 33.00 | 1 |
| 154.3050 | 47.853956 | 34.00 | 0 |
| 120.6500 | 21.177076 | 12.00 | 0 |
| 115.6000 | 18.900000 | 7.00 | 1 |
| 167.0050 | 55.196477 | 42.00 | 1 |
| 142.8750 | 32.998818 | 40.00 | 0 |
| 152.4000 | 40.879979 | 27.00 | 0 |
| 96.5200 | 13.267566 | 3.00 | 0 |
| 160.0000 | 51.200000 | 25.00 | 1 |
| 159.3850 | 49.044635 | 29.00 | 1 |
| 149.8600 | 53.438808 | 45.00 | 0 |
| 160.6550 | 54.090846 | 26.00 | 1 |
| 160.6550 | 55.366574 | 45.00 | 1 |
| 149.2250 | 42.240755 | 45.00 | 0 |
| 125.0950 | 22.367756 | 11.00 | 0 |
| 140.9700 | 40.936678 | 85.60 | 0 |
| 154.9400 | 49.696674 | 26.00 | 1 |
| 141.6050 | 44.338618 | 24.00 | 0 |
| 160.0200 | 45.954540 | 57.00 | 1 |
| 150.1648 | 41.957260 | 22.00 | 0 |
| 155.5750 | 51.482692 | 24.00 | 0 |
| 103.5050 | 12.757275 | 6.00 | 0 |
| 94.6150 | 13.012420 | 4.00 | 0 |
| 156.2100 | 44.111822 | 21.00 | 0 |
| 153.0350 | 32.205032 | 79.00 | 0 |
| 167.0050 | 56.755699 | 50.00 | 1 |
| 149.8600 | 52.673371 | 40.00 | 0 |
| 147.9550 | 36.485807 | 64.00 | 0 |
| 159.3850 | 48.846188 | 32.00 | 1 |
| 161.9250 | 56.954146 | 38.70 | 1 |
| 155.5750 | 42.099007 | 26.00 | 0 |
| 159.3850 | 50.178615 | 63.00 | 1 |
| 146.6850 | 46.549879 | 62.00 | 0 |
| 172.7200 | 61.801910 | 22.00 | 1 |
| 166.3700 | 48.987936 | 41.00 | 1 |
| 141.6050 | 31.524644 | 19.00 | 1 |
| 142.8750 | 32.205032 | 17.00 | 0 |
| 133.3500 | 23.756881 | 14.00 | 0 |
| 127.6350 | 24.408919 | 9.00 | 1 |
| 119.3800 | 21.517270 | 7.00 | 1 |
| 151.7650 | 35.295127 | 74.00 | 0 |
| 156.8450 | 45.642695 | 41.00 | 1 |
| 148.5900 | 43.885026 | 33.00 | 0 |
| 157.4800 | 45.557646 | 53.00 | 0 |
| 149.8600 | 39.008912 | 18.00 | 0 |
| 147.9550 | 41.163474 | 37.00 | 0 |
| 102.2350 | 13.125818 | 6.00 | 0 |
| 153.0350 | 45.245802 | 61.00 | 0 |
| 160.6550 | 53.637254 | 44.00 | 1 |
| 149.2250 | 52.304828 | 35.00 | 0 |
| 114.3000 | 18.342126 | 7.00 | 1 |
| 100.9650 | 13.749507 | 4.00 | 1 |
| 138.4300 | 39.093961 | 23.00 | 0 |
| 91.4400 | 12.530479 | 4.00 | 1 |
| 162.5600 | 45.699394 | 55.00 | 1 |
| 149.2250 | 40.398038 | 53.00 | 0 |
| 158.7500 | 51.482692 | 59.00 | 1 |
| 149.8600 | 38.668718 | 57.00 | 0 |
| 158.1150 | 39.235708 | 35.00 | 1 |
| 156.2100 | 44.338618 | 29.00 | 0 |
| 148.5900 | 39.519203 | 62.00 | 1 |
| 143.5100 | 31.071052 | 18.00 | 0 |
| 154.3050 | 46.776675 | 51.00 | 0 |
| 131.4450 | 22.509503 | 14.00 | 0 |
| 157.4800 | 40.624834 | 19.00 | 1 |
| 157.4800 | 50.178615 | 42.00 | 1 |
| 154.3050 | 41.276872 | 25.00 | 0 |
| 107.9500 | 17.576690 | 6.00 | 1 |
| 168.2750 | 54.600000 | 41.00 | 1 |
| 145.4150 | 44.990657 | 37.00 | 0 |
| 147.9550 | 44.735511 | 16.00 | 0 |
| 100.9650 | 14.401546 | 5.00 | 1 |
| 113.0300 | 19.050864 | 9.00 | 1 |
| 149.2250 | 35.805419 | 82.00 | 1 |
| 154.9400 | 45.217453 | 28.00 | 1 |
| 162.5600 | 48.109102 | 50.00 | 1 |
| 156.8450 | 45.671045 | 43.00 | 0 |
| 123.1900 | 20.808533 | 8.00 | 1 |
| 161.0106 | 48.420946 | 31.00 | 1 |
| 144.7800 | 41.191823 | 67.00 | 0 |
| 143.5100 | 38.413573 | 39.00 | 0 |
| 149.2250 | 42.127357 | 18.00 | 0 |
| 110.4900 | 17.661738 | 11.00 | 0 |
| 149.8600 | 38.243476 | 48.00 | 0 |
| 165.7350 | 48.335898 | 30.00 | 1 |
| 144.1450 | 38.923864 | 64.00 | 0 |
| 157.4800 | 40.029494 | 72.00 | 1 |
| 154.3050 | 50.206964 | 68.00 | 0 |
| 163.8300 | 54.289293 | 44.00 | 1 |
| 156.2100 | 45.600000 | 43.00 | 0 |
| 153.6700 | 40.766581 | 16.00 | 0 |
| 134.6200 | 27.130471 | 13.00 | 0 |
| 144.1450 | 39.434154 | 34.00 | 0 |
| 114.3000 | 20.496689 | 10.00 | 0 |
| 162.5600 | 43.204638 | 62.00 | 1 |
| 146.0500 | 31.864838 | 44.00 | 0 |
| 120.6500 | 20.893581 | 11.00 | 1 |
| 154.9400 | 45.444249 | 31.00 | 1 |
| 144.7800 | 38.045029 | 29.00 | 0 |
| 106.6800 | 15.989118 | 8.00 | 0 |
| 146.6850 | 36.088913 | 62.00 | 0 |
| 152.4000 | 40.879979 | 67.00 | 0 |
| 163.8300 | 47.910655 | 57.00 | 1 |
| 165.7350 | 47.712209 | 32.00 | 1 |
| 156.2100 | 46.379782 | 24.00 | 0 |
| 152.4000 | 41.163474 | 77.00 | 1 |
| 140.3350 | 36.599204 | 62.00 | 0 |
| 158.1150 | 43.091240 | 17.00 | 1 |
| 163.1950 | 48.137451 | 67.00 | 1 |
| 151.1300 | 36.712603 | 70.00 | 0 |
| 171.1198 | 56.557252 | 37.00 | 1 |
| 149.8600 | 38.697068 | 58.00 | 0 |
| 163.8300 | 47.485413 | 35.00 | 1 |
| 141.6050 | 36.202312 | 30.00 | 0 |
| 93.9800 | 14.288148 | 5.00 | 0 |
| 149.2250 | 41.276872 | 26.00 | 0 |
| 105.4100 | 15.223681 | 5.00 | 0 |
| 146.0500 | 44.763860 | 21.00 | 0 |
| 161.2900 | 50.433760 | 41.00 | 1 |
| 162.5600 | 55.281525 | 46.00 | 1 |
| 145.4150 | 37.931631 | 49.00 | 0 |
| 145.4150 | 35.493574 | 15.00 | 1 |
| 170.8150 | 58.456669 | 28.00 | 1 |
| 127.0000 | 21.488921 | 12.00 | 0 |
| 159.3850 | 44.423667 | 83.00 | 0 |
| 159.4000 | 44.400000 | 54.00 | 1 |
| 153.6700 | 44.565414 | 54.00 | 0 |
| 160.0200 | 44.622113 | 68.00 | 1 |
| 150.4950 | 40.483086 | 68.00 | 0 |
| 149.2250 | 44.083472 | 56.00 | 0 |
| 127.0000 | 24.408919 | 15.00 | 0 |
| 142.8750 | 34.416293 | 57.00 | 0 |
| 142.1130 | 32.772022 | 22.00 | 0 |
| 147.3200 | 35.947166 | 40.00 | 0 |
| 162.5600 | 49.554900 | 19.00 | 1 |
| 164.4650 | 53.183662 | 41.00 | 1 |
| 160.0200 | 37.081146 | 75.90 | 1 |
| 153.6700 | 40.511435 | 73.90 | 0 |
| 167.0050 | 50.603857 | 49.00 | 1 |
| 151.1300 | 43.970075 | 26.00 | 1 |
| 147.9550 | 33.792604 | 17.00 | 0 |
| 125.3998 | 21.375523 | 13.00 | 0 |
| 111.1250 | 16.669506 | 8.00 | 0 |
| 153.0350 | 49.890000 | 88.00 | 1 |
| 139.0650 | 33.594158 | 68.00 | 0 |
| 152.4000 | 43.856676 | 33.00 | 1 |
| 154.9400 | 48.137451 | 26.00 | 0 |
| 147.9550 | 42.751046 | 56.00 | 0 |
| 143.5100 | 34.841535 | 16.00 | 1 |
| 117.9830 | 24.097075 | 13.00 | 0 |
| 144.1450 | 33.906002 | 34.00 | 0 |
| 92.7100 | 12.076887 | 5.00 | 0 |
| 147.9550 | 41.276872 | 17.00 | 0 |
| 155.5750 | 39.717650 | 74.00 | 1 |
| 150.4950 | 35.947166 | 69.00 | 0 |
| 155.5750 | 50.915702 | 50.00 | 1 |
| 154.3050 | 45.756093 | 44.00 | 0 |
| 130.6068 | 25.259404 | 15.00 | 0 |
| 101.6000 | 15.337079 | 5.00 | 0 |
| 157.4800 | 49.214732 | 18.00 | 0 |
| 168.9100 | 58.825212 | 41.00 | 1 |
| 150.4950 | 43.459784 | 27.00 | 0 |
| 111.7600 | 17.831836 | 8.90 | 1 |
| 160.0200 | 51.964633 | 38.00 | 1 |
| 167.6400 | 50.688906 | 57.00 | 1 |
| 144.1450 | 34.246196 | 64.50 | 0 |
| 145.4150 | 39.377456 | 42.00 | 0 |
| 160.0200 | 59.562300 | 24.00 | 1 |
| 147.3200 | 40.312989 | 16.00 | 1 |
| 164.4650 | 52.163080 | 71.00 | 1 |
| 153.0350 | 39.972795 | 49.50 | 0 |
| 149.2250 | 43.941725 | 33.00 | 1 |
| 160.0200 | 54.601137 | 28.00 | 0 |
| 149.2250 | 45.075705 | 47.00 | 0 |
| 85.0900 | 11.453198 | 3.00 | 1 |
| 84.4550 | 11.765042 | 1.00 | 1 |
| 59.6138 | 5.896696 | 1.00 | 0 |
| 92.7100 | 12.105237 | 3.00 | 1 |
| 111.1250 | 18.313777 | 6.00 | 0 |
| 90.8050 | 11.368149 | 5.00 | 0 |
| 153.6700 | 41.333571 | 27.00 | 0 |
| 99.6950 | 16.244263 | 5.00 | 0 |
| 62.4840 | 6.803880 | 1.00 | 0 |
| 81.9150 | 11.878440 | 2.00 | 1 |
| 96.5200 | 14.968536 | 2.00 | 0 |
| 80.0100 | 9.865626 | 1.00 | 1 |
| 150.4950 | 41.900561 | 55.00 | 0 |
| 151.7650 | 42.524000 | 83.40 | 1 |
| 140.6398 | 28.859791 | 12.00 | 1 |
| 88.2650 | 12.785625 | 2.00 | 0 |
| 158.1150 | 43.147939 | 63.00 | 1 |
| 149.2250 | 40.823280 | 52.00 | 0 |
| 151.7650 | 42.864444 | 49.00 | 1 |
| 154.9400 | 46.209685 | 31.00 | 0 |
| 123.8250 | 20.581737 | 9.00 | 0 |
| 104.1400 | 15.875720 | 6.00 | 0 |
| 161.2900 | 47.853956 | 35.00 | 1 |
| 148.5900 | 42.524250 | 35.00 | 0 |
| 97.1550 | 17.066399 | 7.00 | 0 |
| 93.3450 | 13.182517 | 5.00 | 1 |
| 160.6550 | 48.505994 | 24.00 | 1 |
| 157.4800 | 45.869491 | 41.00 | 1 |
| 167.0050 | 52.900167 | 32.00 | 1 |
| 157.4800 | 47.570461 | 43.00 | 1 |
| 91.4400 | 12.927372 | 6.00 | 0 |
| 60.4520 | 5.669900 | 1.00 | 1 |
| 137.1600 | 28.916490 | 15.00 | 1 |
| 152.4000 | 43.544832 | 63.00 | 0 |
| 152.4000 | 43.431434 | 21.00 | 0 |
| 81.2800 | 11.509897 | 1.00 | 1 |
| 109.2200 | 11.708343 | 2.00 | 0 |
| 71.1200 | 7.540967 | 1.00 | 1 |
| 89.2048 | 12.700576 | 3.00 | 0 |
| 67.3100 | 7.200773 | 1.00 | 0 |
| 85.0900 | 12.360382 | 1.00 | 1 |
| 69.8500 | 7.796112 | 1.00 | 0 |
| 161.9250 | 53.212012 | 55.00 | 0 |
| 152.4000 | 44.678812 | 38.00 | 0 |
| 88.9000 | 12.558829 | 3.00 | 1 |
| 90.1700 | 12.700576 | 3.00 | 1 |
| 71.7550 | 7.370870 | 1.00 | 0 |
| 83.8200 | 9.213587 | 1.00 | 0 |
| 159.3850 | 47.201918 | 28.00 | 1 |
| 142.2400 | 28.632995 | 16.00 | 0 |
| 142.2400 | 31.666391 | 36.00 | 0 |
| 168.9100 | 56.443855 | 38.00 | 1 |
| 123.1900 | 20.014747 | 12.00 | 1 |
| 74.9300 | 8.504850 | 1.00 | 1 |
| 74.2950 | 8.306404 | 1.00 | 0 |
| 90.8050 | 11.623295 | 3.00 | 0 |
| 160.0200 | 55.791816 | 48.00 | 1 |
| 67.9450 | 7.966209 | 1.00 | 0 |
| 135.8900 | 27.215520 | 15.00 | 0 |
| 158.1150 | 47.485413 | 45.00 | 1 |
| 85.0900 | 10.801160 | 3.00 | 1 |
| 93.3450 | 14.004653 | 3.00 | 0 |
| 152.4000 | 45.160753 | 38.00 | 0 |
| 155.5750 | 45.529297 | 21.00 | 0 |
| 154.3050 | 48.874538 | 50.00 | 0 |
| 156.8450 | 46.578229 | 41.00 | 1 |
| 120.0150 | 20.128145 | 13.00 | 0 |
| 114.3000 | 18.143680 | 8.00 | 1 |
| 83.8200 | 10.914558 | 3.00 | 1 |
| 156.2100 | 43.885026 | 30.00 | 0 |
| 137.1600 | 27.158821 | 12.00 | 1 |
| 114.3000 | 19.050864 | 7.00 | 1 |
| 93.9800 | 13.834556 | 4.00 | 0 |
| 168.2750 | 56.046962 | 21.00 | 1 |
| 147.9550 | 40.086193 | 38.00 | 0 |
| 139.7000 | 26.563482 | 15.00 | 1 |
| 157.4800 | 50.802304 | 19.00 | 0 |
| 76.2000 | 9.213587 | 1.00 | 1 |
| 66.0400 | 7.569317 | 1.00 | 1 |
| 160.7000 | 46.300000 | 31.00 | 1 |
| 114.3000 | 19.419407 | 8.00 | 0 |
| 146.0500 | 37.903281 | 16.00 | 1 |
| 161.2900 | 49.356479 | 21.00 | 1 |
| 69.8500 | 7.314171 | 0.00 | 0 |
| 133.9850 | 28.151053 | 13.00 | 1 |
| 67.9450 | 7.824462 | 0.00 | 1 |
| 150.4950 | 44.111822 | 50.00 | 0 |
| 163.1950 | 51.029100 | 39.00 | 1 |
| 148.5900 | 40.766581 | 44.00 | 1 |
| 148.5900 | 37.563088 | 36.00 | 0 |
| 161.9250 | 51.596090 | 36.00 | 1 |
| 153.6700 | 44.820560 | 18.00 | 0 |
| 68.5800 | 8.022908 | 0.00 | 0 |
| 151.1300 | 43.403084 | 58.00 | 0 |
| 163.8300 | 46.719976 | 58.00 | 1 |
| 153.0350 | 39.547553 | 33.00 | 0 |
| 151.7650 | 34.784836 | 21.50 | 0 |
| 132.0800 | 22.792998 | 11.00 | 1 |
| 156.2100 | 39.292407 | 26.00 | 1 |
| 140.3350 | 37.449689 | 22.00 | 0 |
| 158.7500 | 48.676091 | 28.00 | 1 |
| 142.8750 | 35.606972 | 42.00 | 0 |
| 84.4550 | 9.383684 | 2.00 | 1 |
| 151.9428 | 43.714929 | 21.00 | 1 |
| 161.2900 | 48.194150 | 19.00 | 1 |
| 127.9906 | 29.852024 | 13.00 | 1 |
| 160.9852 | 50.972401 | 48.00 | 1 |
| 144.7800 | 43.998424 | 46.00 | 0 |
| 132.0800 | 28.292801 | 11.00 | 1 |
| 117.9830 | 20.354941 | 8.00 | 1 |
| 160.0200 | 48.194150 | 25.00 | 1 |
| 154.9400 | 39.179009 | 16.00 | 1 |
| 160.9852 | 46.691626 | 51.00 | 1 |
| 165.9890 | 56.415505 | 25.00 | 1 |
| 157.9880 | 48.591043 | 28.00 | 1 |
| 154.9400 | 48.222499 | 26.00 | 0 |
| 97.9932 | 13.295915 | 5.00 | 1 |
| 64.1350 | 6.662133 | 1.00 | 0 |
| 160.6550 | 47.485413 | 54.00 | 1 |
| 147.3200 | 35.550273 | 66.00 | 0 |
| 146.7000 | 36.600000 | 20.00 | 0 |
| 147.3200 | 48.959587 | 25.00 | 0 |
| 172.9994 | 51.255896 | 38.00 | 1 |
| 158.1150 | 46.521529 | 51.00 | 1 |
| 147.3200 | 36.967748 | 48.00 | 0 |
| 124.9934 | 25.117657 | 13.00 | 1 |
| 106.0450 | 16.272613 | 6.00 | 1 |
| 165.9890 | 48.647742 | 27.00 | 1 |
| 149.8600 | 38.045029 | 22.00 | 0 |
| 76.2000 | 8.504850 | 1.00 | 0 |
| 161.9250 | 47.286966 | 60.00 | 1 |
| 140.0048 | 28.349500 | 15.00 | 0 |
| 66.6750 | 8.136306 | 0.00 | 0 |
| 62.8650 | 7.200773 | 0.00 | 1 |
| 163.8300 | 55.394923 | 43.00 | 1 |
| 147.9550 | 32.488527 | 12.00 | 1 |
| 160.0200 | 54.204244 | 27.00 | 1 |
| 154.9400 | 48.477645 | 30.00 | 1 |
| 152.4000 | 43.062891 | 29.00 | 0 |
| 62.2300 | 7.257472 | 0.00 | 0 |
| 146.0500 | 34.189497 | 23.00 | 0 |
| 151.9936 | 49.951819 | 30.00 | 0 |
| 157.4800 | 41.305222 | 17.00 | 1 |
| 55.8800 | 4.847765 | 0.00 | 0 |
| 60.9600 | 6.236890 | 0.00 | 1 |
| 151.7650 | 44.338618 | 41.00 | 0 |
| 144.7800 | 33.452410 | 42.00 | 0 |
| 118.1100 | 16.896302 | 7.00 | 0 |
| 78.1050 | 8.221355 | 3.00 | 0 |
| 160.6550 | 47.286966 | 43.00 | 1 |
| 151.1300 | 46.124637 | 35.00 | 0 |
| 121.9200 | 20.184844 | 10.00 | 0 |
| 92.7100 | 12.757275 | 3.00 | 1 |
| 153.6700 | 47.400364 | 75.50 | 1 |
| 147.3200 | 40.851630 | 64.00 | 0 |
| 139.7000 | 50.348712 | 38.00 | 1 |
| 157.4800 | 45.132404 | 24.20 | 0 |
| 91.4400 | 11.623295 | 4.00 | 0 |
| 154.9400 | 42.240755 | 26.00 | 1 |
| 143.5100 | 41.645415 | 19.00 | 0 |
| 83.1850 | 9.156889 | 2.00 | 1 |
| 158.1150 | 45.217453 | 43.00 | 1 |
| 147.3200 | 51.255896 | 38.00 | 0 |
| 123.8250 | 21.205426 | 10.00 | 1 |
| 88.9000 | 11.594945 | 3.00 | 1 |
| 160.0200 | 49.271431 | 23.00 | 1 |
| 137.1600 | 27.952607 | 16.00 | 0 |
| 165.1000 | 51.199197 | 49.00 | 1 |
| 154.9400 | 43.856676 | 41.00 | 0 |
| 111.1250 | 17.690088 | 6.00 | 1 |
| 153.6700 | 35.521923 | 23.00 | 0 |
| 145.4150 | 34.246196 | 14.00 | 0 |
| 141.6050 | 42.885420 | 43.00 | 0 |
| 144.7800 | 32.545226 | 15.00 | 0 |
| 163.8300 | 46.776675 | 21.00 | 1 |
| 161.2900 | 41.872211 | 24.00 | 1 |
| 154.9000 | 38.200000 | 20.00 | 1 |
| 161.3000 | 43.300000 | 20.00 | 1 |
| 170.1800 | 53.637254 | 34.00 | 1 |
| 149.8600 | 42.977842 | 29.00 | 0 |
| 123.8250 | 21.545620 | 11.00 | 1 |
| 85.0900 | 11.424848 | 3.00 | 0 |
| 160.6550 | 39.774349 | 65.00 | 1 |
| 154.9400 | 43.346385 | 46.00 | 0 |
| 106.0450 | 15.478827 | 8.00 | 0 |
| 126.3650 | 21.914164 | 15.00 | 1 |
| 166.3700 | 52.673371 | 43.00 | 1 |
| 148.2852 | 38.441922 | 39.00 | 0 |
| 124.4600 | 19.277660 | 12.00 | 0 |
| 89.5350 | 11.113004 | 3.00 | 1 |
| 101.6000 | 13.494362 | 4.00 | 0 |
| 151.7650 | 42.807745 | 43.00 | 0 |
| 148.5900 | 35.890467 | 70.00 | 0 |
| 153.6700 | 44.225220 | 26.00 | 0 |
| 53.9750 | 4.252425 | 0.00 | 0 |
| 146.6850 | 38.073378 | 48.00 | 0 |
| 56.5150 | 5.159609 | 0.00 | 0 |
| 100.9650 | 14.316498 | 5.00 | 1 |
| 121.9200 | 23.218241 | 8.00 | 1 |
| 81.5848 | 10.659412 | 3.00 | 0 |
| 154.9400 | 44.111822 | 44.00 | 1 |
| 156.2100 | 44.026773 | 33.00 | 0 |
| 132.7150 | 24.975910 | 15.00 | 1 |
| 125.0950 | 22.594552 | 12.00 | 0 |
| 101.6000 | 14.344847 | 5.00 | 0 |
| 160.6550 | 47.882306 | 41.00 | 1 |
| 146.0500 | 39.405805 | 37.40 | 0 |
| 132.7150 | 24.777463 | 13.00 | 0 |
| 87.6300 | 10.659412 | 6.00 | 0 |
| 156.2100 | 41.050076 | 53.00 | 1 |
| 152.4000 | 40.823280 | 49.00 | 0 |
| 162.5600 | 47.031821 | 27.00 | 0 |
| 114.9350 | 17.519991 | 7.00 | 1 |
| 67.9450 | 7.229122 | 1.00 | 0 |
| 142.8750 | 34.246196 | 31.00 | 0 |
| 76.8350 | 8.022908 | 1.00 | 1 |
| 145.4150 | 31.127751 | 17.00 | 1 |
| 162.5600 | 52.163080 | 31.00 | 1 |
| 156.2100 | 54.062497 | 21.00 | 0 |
| 71.1200 | 8.051258 | 0.00 | 1 |
| 158.7500 | 52.531624 | 68.00 | 1 |
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
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 = 1977, N_years = 1000)
d2 <- d[d$age > 17, ] # only adults
d2$A <- (d2$age - 18) / (65 - 18)
d2$mid <- d2$married + 1
m6.9 <- quap(
alist(
happiness ~ dnorm(mu, sigma),
mu <- a[mid] + bA * A,
a[mid] ~ dnorm(0, 1),
bA ~ dnorm(0, 2),
sigma ~ dexp(1)
),
data = d2
)
m6.10 <- quap(
alist(
happiness ~ dnorm(mu, sigma),
mu <- a + bA * A,
a ~ dnorm(0, 1),
bA ~ dnorm(0, 2),
sigma ~ dexp(1)
),
data = d2
)
compare(m6.9, m6.10)
## WAIC SE dWAIC dSE pWAIC weight
## m6.9 2713.971 37.54465 0.0000 NA 3.738532 1.000000e+00
## m6.10 3101.906 27.74379 387.9347 35.40032 2.340445 5.768312e-85
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$area <- scale(foxes$area)
foxes$avgfood <- scale(foxes$avgfood)
foxes$weight <- scale(foxes$weight)
foxes$groupsize <- scale(foxes$groupsize)
m1 <- quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + bFood * avgfood + bGroup * groupsize + bArea * area,
a ~ dnorm(0, .2),
c(bFood, bGroup, bArea) ~ dnorm(0, 5),
sigma ~ dexp(1)
),
data = foxes
)
m2 <- quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + bFood * avgfood + bGroup * groupsize,
a ~ dnorm(0, .2),
c(bFood, bGroup) ~ dnorm(0, 5),
sigma ~ dexp(1)
),
data = foxes
)
m3 <- quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + bGroup * groupsize + bArea * area,
a ~ dnorm(0, .2),
c(bGroup, bArea) ~ dnorm(0, 5),
sigma ~ dexp(1)
),
data = foxes
)
m4 <- quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + bFood * avgfood,
a ~ dnorm(0, .2),
bFood ~ dnorm(0, 5),
sigma ~ dexp(1)
),
data = foxes
)
m5 <- quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + bArea * area,
a ~ dnorm(0, .2),
bArea ~ dnorm(0, 5),
sigma ~ dexp(1)
),
data = foxes
)
compare(m1, m2, m3, m4, m5)
## WAIC SE dWAIC dSE pWAIC weight
## m1 323.2326 16.93241 0.0000000 NA 5.169070 0.416340180
## m2 323.8576 16.81225 0.6250217 3.887568 4.088241 0.304597680
## m3 324.0666 16.18771 0.8339840 4.211866 3.945774 0.274379088
## m4 333.4412 13.84628 10.2085563 8.696885 2.418544 0.002527484
## m5 333.7595 13.74596 10.5268978 8.758170 2.664246 0.002155567