第 67 章 贝叶斯logistic-binomial模型

library(tidyverse)
library(tidybayes)
library(rstan)

rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
theme_set(bayesplot::theme_default())

67.1 企鹅案例

筛选出物种为”Gentoo”的企鹅,并构建gender变量,male 对应1,female对应0

library(palmerpenguins)
gentoo <- penguins %>%
  filter(species == "Gentoo", !is.na(sex)) %>% 
  mutate(gender = if_else(sex == "male", 1, 0))
gentoo
## # A tibble: 119 × 9
##    species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
##    <fct>   <fct>           <dbl>         <dbl>             <int>       <int>
##  1 Gentoo  Biscoe           46.1          13.2               211        4500
##  2 Gentoo  Biscoe           50            16.3               230        5700
##  3 Gentoo  Biscoe           48.7          14.1               210        4450
##  4 Gentoo  Biscoe           50            15.2               218        5700
##  5 Gentoo  Biscoe           47.6          14.5               215        5400
##  6 Gentoo  Biscoe           46.5          13.5               210        4550
##  7 Gentoo  Biscoe           45.4          14.6               211        4800
##  8 Gentoo  Biscoe           46.7          15.3               219        5200
##  9 Gentoo  Biscoe           43.3          13.4               209        4400
## 10 Gentoo  Biscoe           46.8          15.4               215        5150
## # ℹ 109 more rows
## # ℹ 3 more variables: sex <fct>, year <int>, gender <dbl>

67.1.1 dotplots

借鉴ggdist的Logit dotplots 的画法,画出dotplot

gentoo %>%
  ggplot(aes(x = body_mass_g, y = sex, side = ifelse(sex == "male", "bottom", "top"))) +
  geom_dots(scale = 0.5) +
  ggtitle(
    "geom_dots(scale = 0.5)",
    'aes(side = ifelse(sex == "male", "bottom", "top"))'
  )

\[ \begin{align*} y_i & = \text{bernoulli}( p_i) \\ p_i & =\text{logit}^{-1}(X_i \beta) \end{align*} \]

67.1.2 bayesian logit模型

stan_program <- "
data {
  int<lower=0> N;
  vector[N] x;
  int<lower=0,upper=1> y[N];
  int<lower=0> M;
  vector[M] new_x;  
}
parameters {
  real alpha;
  real beta;
}
model {
  // more efficient and arithmetically stable
  y ~ bernoulli_logit(alpha + beta * x);
}
generated quantities {
  vector[M] y_epred; 
  vector[M] mu = alpha + beta * new_x;

  for(i in 1:M) {
    y_epred[i] = inv_logit(mu[i]);
  }
   
}
"

newdata <- data.frame(
    body_mass_g = seq(min(gentoo$body_mass_g), max(gentoo$body_mass_g), length.out = 100)
   ) 


stan_data <- list(
  N = nrow(gentoo),
  y = gentoo$gender, 
  x = gentoo$body_mass_g,
  M = nrow(newdata),
  new_x = newdata$body_mass_g
)

m <- stan(model_code = stan_program, data = stan_data)
fit <- m %>%
  tidybayes::gather_draws(y_epred[i]) %>%
  ggdist::mean_qi(.value)
fit
## # A tibble: 100 × 8
##        i .variable    .value        .lower   .upper .width .point .interval
##    <int> <chr>         <dbl>         <dbl>    <dbl>  <dbl> <chr>  <chr>    
##  1     1 y_epred   0.0000275 0.00000000277 0.000233   0.95 mean   qi       
##  2     2 y_epred   0.0000333 0.00000000428 0.000281   0.95 mean   qi       
##  3     3 y_epred   0.0000403 0.00000000660 0.000339   0.95 mean   qi       
##  4     4 y_epred   0.0000488 0.0000000101  0.000406   0.95 mean   qi       
##  5     5 y_epred   0.0000593 0.0000000155  0.000485   0.95 mean   qi       
##  6     6 y_epred   0.0000720 0.0000000237  0.000583   0.95 mean   qi       
##  7     7 y_epred   0.0000877 0.0000000366  0.000699   0.95 mean   qi       
##  8     8 y_epred   0.000107  0.0000000556  0.000834   0.95 mean   qi       
##  9     9 y_epred   0.000130  0.0000000857  0.000991   0.95 mean   qi       
## 10    10 y_epred   0.000159  0.000000132   0.00119    0.95 mean   qi       
## # ℹ 90 more rows

两个图画在一起

fit %>% 
  bind_cols(newdata) %>% 
  ggplot(aes(x = body_mass_g)) +
  geom_dots(
    data = gentoo,
    aes(y = gender, side = ifelse(sex == "male", "bottom", "top")),
    scale = 0.4
  ) +
  geom_lineribbon(
    aes(y = .value, ymin = .lower, ymax = .upper), 
    alpha = 1/4, 
    fill = "#08306b"
  ) +
  labs(
    title = "logit dotplot: stat_dots() with stat_lineribbon()",
    subtitle = 'aes(side = ifelse(sex == "male", "bottom", "top"))',
    x = "Body mass (g) of Gentoo penguins",
    y = "Pr(sex = male)"
  )

67.2 篮球案例

我们模拟100个选手每人投篮20次,假定命中概率是身高的线性函数,案例来源chap15.3 of [Regression and Other Stories] (page270).

n <- 100

data <-
  tibble(size   = 20,
         height = rnorm(n, mean = 72, sd = 3)) %>% 
  mutate(y = rbinom(n, size = size, p = 0.4 + 0.1 * (height - 72) / 3))

head(data)
## # A tibble: 6 × 3
##    size height     y
##   <dbl>  <dbl> <int>
## 1    20   74.2    13
## 2    20   72.0    12
## 3    20   70.7     4
## 4    20   68.7     6
## 5    20   78.4    13
## 6    20   72.4     6

67.2.1 常规做法

fit_glm <- glm(
  cbind(y, 20-y) ~ height, family = binomial(link = "logit"),
  data = data
)
fit_glm
## 
## Call:  glm(formula = cbind(y, 20 - y) ~ height, family = binomial(link = "logit"), 
##     data = data)
## 
## Coefficients:
## (Intercept)       height  
##    -10.9493       0.1465  
## 
## Degrees of Freedom: 99 Total (i.e. Null);  98 Residual
## Null Deviance:       163.9 
## Residual Deviance: 84.39     AIC: 424.3

67.2.2 stan 代码

\[ \begin{align*} y_i & = \text{Binomial}(n_i, p_i) \\ p_i & =\text{logit}^{-1}(X_i \beta) \end{align*} \]

stan_program <- "
data {
  int<lower=0> N;
  int<lower=0> K;
  matrix[N, K] X;
  int<lower=0> y[N];
  int trials[N];
}
parameters {
  vector[K] beta;
}
model {
  
  for(i in 1:N) {
    target += binomial_logit_lpmf(y[i] | trials[i], X[i] * beta);
  }
  
}
"


stan_data <- data %>%
  tidybayes::compose_data(
    N      = n,
    K      = 2,
    y      = y,
    trials = size,
    X      = model.matrix(~ 1 + height)
  )

fit <- stan(model_code = stan_program, data = stan_data)
fit
## Inference for Stan model: anon_model.
## 4 chains, each with iter=2000; warmup=1000; thin=1; 
## post-warmup draws per chain=1000, total post-warmup draws=4000.
## 
##            mean se_mean   sd    2.5%     25%     50%     75%   97.5% n_eff Rhat
## beta[1]  -11.03    0.05 1.16  -13.34  -11.81  -11.05  -10.26   -8.72   519    1
## beta[2]    0.15    0.00 0.02    0.12    0.14    0.15    0.16    0.18   517    1
## lp__    -211.12    0.03 0.99 -213.77 -211.47 -210.81 -210.43 -210.19   800    1
## 
## Samples were drawn using NUTS(diag_e) at Tue Jul 18 20:14:17 2023.
## For each parameter, n_eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor on split chains (at 
## convergence, Rhat=1).
## Warning in rm(gentoo, fit, m, stan_data, stan_program, data, fit_glm, fit, :
## object 'fit' not found