第 70 章 贝叶斯层级模型

library(tidyverse)
library(tidybayes)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

70.1 明尼苏达州房屋中氡的存在

radon <- readr::read_rds(here::here('demo_data', "radon.rds")) 
head(radon)
##   floor county  log_radon log_uranium
## 1     1 AITKIN 0.83290912  -0.6890476
## 2     0 AITKIN 0.83290912  -0.6890476
## 3     0 AITKIN 1.09861229  -0.6890476
## 4     0 AITKIN 0.09531018  -0.6890476
## 5     0  ANOKA 1.16315081  -0.8473129
## 6     0  ANOKA 0.95551145  -0.8473129

数据来源美国明尼苏达州85个县中房屋氡含量测量

  • log_radon 房屋氡含量 (log scale)
  • log_uranium 这个县放射性化学元素铀的等级 (log scale)
  • floor 房屋楼层 (0 = basement, 1 = first floor)
  • county 所在县 (factor)

70.2 任务

估计房屋中的氡含量。

70.2.1 可视化探索

df_n_county <- radon %>% 
  group_by(county) %>%
  summarise(
    n = n()
  ) 

df_n_county 
## # A tibble: 85 × 2
##    county        n
##    <fct>     <int>
##  1 AITKIN        4
##  2 ANOKA        52
##  3 BECKER        3
##  4 BELTRAMI      7
##  5 BENTON        4
##  6 BIGSTONE      3
##  7 BLUEEARTH    14
##  8 BROWN         4
##  9 CARLTON      10
## 10 CARVER        6
## # ℹ 75 more rows

统计每个县,样本量、氡含量均值、标准差、铀等级的均值、标准误

radon_county <- radon %>%
  group_by(county) %>%
  summarise(
    log_radon_mean = mean(log_radon),
    log_radon_sd   = sd(log_radon),
    log_uranium    = mean(log_uranium),
    n              = length(county)
  ) %>%
  mutate(log_radon_se = log_radon_sd / sqrt(n))

radon_county
## # A tibble: 85 × 6
##    county    log_radon_mean log_radon_sd log_uranium     n log_radon_se
##    <fct>              <dbl>        <dbl>       <dbl> <int>        <dbl>
##  1 AITKIN             0.715        0.432     -0.689      4       0.216 
##  2 ANOKA              0.891        0.718     -0.847     52       0.0995
##  3 BECKER             1.09         0.717     -0.113      3       0.414 
##  4 BELTRAMI           1.19         0.894     -0.593      7       0.338 
##  5 BENTON             1.28         0.415     -0.143      4       0.207 
##  6 BIGSTONE           1.54         0.504      0.387      3       0.291 
##  7 BLUEEARTH          1.93         0.542      0.272     14       0.145 
##  8 BROWN              1.65         0.595      0.278      4       0.298 
##  9 CARLTON            0.977        0.585     -0.332     10       0.185 
## 10 CARVER             1.22         1.90       0.0959     6       0.777 
## # ℹ 75 more rows
ggplot() +
  geom_boxplot(data = radon,
               mapping = aes(y = log_radon,
                             x = fct_reorder(county, log_radon, mean)),
               colour = "gray") +
  geom_point(data = radon,
             mapping = aes(y = log_radon,
                           x = fct_reorder(county, log_radon, mean)),
             colour = "gray") +
  geom_point(data = radon_county,
             mapping = aes(x = fct_reorder(county, log_radon_mean),
                           y = log_radon_mean),
             colour = "red") +
  coord_flip() +
  labs(y = "log(radon)", x = "")

70.2.2 pooling model

这是最简单的模型,该模型假定所有的房屋的氡含量来自同一个分布, 估计整体的均值和方差

\[ \begin{aligned}[t] y_i &\sim \operatorname{normal}(\mu, \sigma) \\ \mu &\sim \operatorname{normal}(0, 10) \\ \sigma &\sim \operatorname{exp}(1) \end{aligned} \] 这里我们指定 \(\mu\)\(\sigma\) 较弱的先验信息.

stan_program <- "
data {
  int N;
  vector[N] y;
}
parameters {
  real mu;
  real<lower=0> sigma;
}
model {
  mu ~ normal(0, 10);
  sigma ~ exponential(1);
  
  y ~ normal(mu, sigma);
}
"

stan_data <- list(
  N = nrow(radon),
  y = radon$log_radon
)

fit_pooling <- stan(model_code = stan_program, data = stan_data)

模型估计了均值和方差两个参数。

summary(fit_pooling)$summary
##               mean      se_mean         sd         2.5%          25%
## mu       1.2648705 0.0005215317 0.02789191    1.2099489    1.2458827
## sigma    0.8197429 0.0003222663 0.01915729    0.7826024    0.8069099
## lp__  -277.9589171 0.0248568462 1.05360358 -280.8510883 -278.3265917
##                50%          75%        97.5%    n_eff     Rhat
## mu       1.2654047    1.2836367    1.3196394 2860.191 1.000923
## sigma    0.8198766    0.8319114    0.8588467 3533.771 1.000211
## lp__  -277.6357605 -277.2189104 -276.9572760 1796.646 1.000153

70.2.3 no-pooling model

每个县都有独立的均值和方差,又叫 individual model

\[ \begin{aligned}[t] y_i &\sim \operatorname{normal}(\mu_{j[i]}, \sigma) \\ \mu_j &\sim \operatorname{normal}(0, 10) \\ \sigma &\sim \operatorname{exp}(1) \end{aligned} \] 其中, \(j[i]\) 表示观测\(i\)对应的所在县。

stan_program <- "
data {
  int<lower=1> N;                            
  int<lower=2> J;                     
  int<lower=1, upper=J> county[N]; 
  vector[N] y; 
}
parameters {
  vector[J] mu;
  real<lower=0> sigma;
}
model {
  mu ~ normal(0, 10);
  sigma ~ exponential(1);
  
  for(i in 1:N) {
    y[i] ~ normal(mu[county[i]], sigma);
  }
}
"

stan_data <- list(
  N      = nrow(radon),
  J      = length(unique(radon$county)),
  county = as.numeric(radon$county),
  y      = radon$log_radon
)

fit_no_pooling <- stan(model_code = stan_program, data = stan_data)
summary(fit_no_pooling)$summary
##                mean      se_mean         sd          2.5%          25%
## mu[1]     0.7102281 0.0052372533 0.39746605 -8.562233e-02    0.4473147
## mu[2]     0.8879961 0.0014067058 0.10628010  6.760031e-01    0.8160872
## mu[3]     1.0882640 0.0065850206 0.45076863  2.073126e-01    0.7882002
## mu[4]     1.1907543 0.0043159614 0.28907221  6.116509e-01    0.9973324
## mu[5]     1.2845097 0.0051584670 0.39020035  5.119509e-01    1.0235373
## mu[6]     1.5349632 0.0069152785 0.44956177  6.503407e-01    1.2308065
## mu[7]     1.9233315 0.0027098894 0.20385244  1.534799e+00    1.7831451
## mu[8]     1.6522359 0.0052367226 0.38581310  8.763013e-01    1.3952936
## mu[9]     0.9761394 0.0032701902 0.24011818  5.076284e-01    0.8063581
## mu[10]    1.2265250 0.0044868476 0.31354896  6.126223e-01    1.0185804
## mu[11]    1.4235609 0.0047564697 0.34009161  7.652808e-01    1.1978834
## mu[12]    1.7513258 0.0047347859 0.38180196  1.007964e+00    1.4931843
## mu[13]    1.0785303 0.0044394110 0.31582034  4.580286e-01    0.8687181
## mu[14]    1.8139592 0.0029466719 0.20825027  1.401183e+00    1.6732879
## mu[15]    1.0249794 0.0049353155 0.37720676  2.899878e-01    0.7683932
## mu[16]    0.7229141 0.0069980637 0.54315199 -3.286089e-01    0.3551150
## mu[17]    0.7475452 0.0051337167 0.38921343 -3.593451e-02    0.4855614
## mu[18]    0.9846711 0.0028882144 0.21666882  5.693172e-01    0.8323940
## mu[19]    1.3280318 0.0013451390 0.09784114  1.138076e+00    1.2624299
## mu[20]    1.8142360 0.0068823460 0.45570934  9.406269e-01    1.5075895
## mu[21]    1.6665901 0.0035670076 0.24859618  1.186818e+00    1.5004582
## mu[22]    0.6605330 0.0042467744 0.32152351  2.445141e-02    0.4527695
## mu[23]    1.0634955 0.0078068776 0.54460001  3.992087e-03    0.7004814
## mu[24]    1.9571644 0.0035569050 0.25838602  1.454342e+00    1.7860924
## mu[25]    1.8626001 0.0028250188 0.20322262  1.452687e+00    1.7245460
## mu[26]    1.3202027 0.0010202114 0.07381467  1.176463e+00    1.2706909
## mu[27]    1.5576784 0.0040322904 0.31861675  9.251201e-01    1.3511192
## mu[28]    0.8707364 0.0048081832 0.34524393  1.907432e-01    0.6359569
## mu[29]    1.0877071 0.0063215824 0.45101408  1.954840e-01    0.7865018
## mu[30]    0.9668417 0.0033111276 0.23215021  4.972084e-01    0.8168690
## mu[31]    2.0322105 0.0048886251 0.35192661  1.340106e+00    1.7915333
## mu[32]    1.2640321 0.0054020010 0.38864890  5.140842e-01    1.0050564
## mu[33]    2.0694110 0.0051707452 0.38133665  1.317942e+00    1.8097455
## mu[34]    1.1557314 0.0063321208 0.44626490  2.678986e-01    0.8531634
## mu[35]    0.4759037 0.0039596737 0.28818147 -9.721717e-02    0.2906393
## mu[36]    2.5922928 0.0075406046 0.54412197  1.518573e+00    2.2306075
## mu[37]    0.4122966 0.0038097147 0.26212868 -9.743268e-02    0.2320982
## mu[38]    1.5258147 0.0052704348 0.38257325  7.999594e-01    1.2620896
## mu[39]    1.6232845 0.0051767108 0.35211844  9.238562e-01    1.3907508
## mu[40]    2.1380683 0.0054392423 0.38372439  1.393175e+00    1.8809007
## mu[41]    1.9004238 0.0036254246 0.26503590  1.376367e+00    1.7233898
## mu[42]    1.3474057 0.0109117854 0.73584353 -7.680579e-02    0.8426540
## mu[43]    1.2582571 0.0035518796 0.25415139  7.664978e-01    1.0866101
## mu[44]    1.0086260 0.0041976007 0.29177498  4.418412e-01    0.8082380
## mu[45]    1.1102826 0.0030918678 0.21900479  6.768904e-01    0.9611857
## mu[46]    1.2534435 0.0047404832 0.33592720  5.968573e-01    1.0234263
## mu[47]    0.6241228 0.0074090430 0.53181826 -4.305870e-01    0.2550359
## mu[48]    1.1078221 0.0033675056 0.25690966  5.979456e-01    0.9343569
## mu[49]    1.6195858 0.0029342919 0.21344318  1.199882e+00    1.4786782
## mu[50]    2.4864278 0.0111956762 0.79427228  8.799377e-01    1.9445598
## mu[51]    2.1615075 0.0050842048 0.37593836  1.430685e+00    1.9135756
## mu[52]    1.9353474 0.0061300877 0.44804619  1.065377e+00    1.6204829
## mu[53]    1.0559782 0.0063302329 0.44809623  1.903658e-01    0.7565460
## mu[54]    1.2527237 0.0020671663 0.15597263  9.384507e-01    1.1469279
## mu[55]    1.3786292 0.0038270235 0.27549837  8.522523e-01    1.1901746
## mu[56]    0.7336197 0.0060105938 0.44398304 -1.534896e-01    0.4412740
## mu[57]    0.6931695 0.0043071193 0.30630411  1.046131e-01    0.4882893
## mu[58]    1.7008430 0.0055392449 0.39120357  9.392564e-01    1.4283128
## mu[59]    1.3947946 0.0052458223 0.38874128  6.379986e-01    1.1354205
## mu[60]    1.3086582 0.0081252665 0.53030112  2.794425e-01    0.9517949
## mu[61]    1.1281792 0.0021004655 0.13575755  8.666748e-01    1.0355293
## mu[62]    1.8618329 0.0047296742 0.34772484  1.181745e+00    1.6272365
## mu[63]    1.4666080 0.0061331446 0.44051439  6.238725e-01    1.1674353
## mu[64]    1.8044179 0.0034576936 0.23318523  1.357019e+00    1.6483019
## mu[65]    1.3365554 0.0068995443 0.52341861  2.658436e-01    1.0035532
## mu[66]    1.2888505 0.0026190560 0.20469962  8.935963e-01    1.1505366
## mu[67]    1.6099600 0.0029730044 0.20785483  1.194712e+00    1.4731162
## mu[68]    1.1217948 0.0036931329 0.27356149  5.909118e-01    0.9355937
## mu[69]    1.2685883 0.0050461894 0.36514908  5.417730e-01    1.0233080
## mu[70]    0.8281681 0.0010128048 0.07210317  6.902686e-01    0.7779085
## mu[71]    1.4119600 0.0021732818 0.15311395  1.102542e+00    1.3126431
## mu[72]    1.6059006 0.0035012944 0.24989313  1.118306e+00    1.4383236
## mu[73]    1.8044508 0.0073961345 0.53320736  7.463978e-01    1.4439606
## mu[74]    1.0218552 0.0052947573 0.36628831  3.254795e-01    0.7761861
## mu[75]    1.5059425 0.0059914317 0.43923351  6.607712e-01    1.2017799
## mu[76]    1.8398316 0.0050770167 0.37319810  1.109100e+00    1.5866827
## mu[77]    1.7360913 0.0041372986 0.28606551  1.182268e+00    1.5405152
## mu[78]    1.0393296 0.0046197109 0.34210709  3.507465e-01    0.8134819
## mu[79]    0.5301785 0.0052605638 0.39063289 -2.220643e-01    0.2578896
## mu[80]    1.2886355 0.0014604219 0.11073824  1.068640e+00    1.2143839
## mu[81]    2.2404035 0.0065646429 0.45773323  1.351962e+00    1.9350978
## mu[82]    2.2336720 0.0110240558 0.76027955  7.443827e-01    1.7303229
## mu[83]    1.4935696 0.0028707638 0.21377919  1.084719e+00    1.3453707
## mu[84]    1.6128595 0.0029735546 0.20779040  1.197459e+00    1.4764978
## mu[85]    1.2217269 0.0071981406 0.53611971  1.762531e-01    0.8556420
## sigma     0.7673386 0.0002794168 0.01907036  7.316965e-01    0.7538393
## lp__   -217.6688170 0.1782193526 7.09460376 -2.323055e+02 -222.1550310
##                 50%          75%        97.5%    n_eff      Rhat
## mu[1]     0.7104945    0.9847795    1.4796444 5759.608 0.9994211
## mu[2]     0.8869731    0.9591745    1.0937704 5708.176 0.9997852
## mu[3]     1.0918103    1.3806825    1.9669293 4685.901 0.9999646
## mu[4]     1.1880373    1.3929600    1.7603848 4485.983 0.9996183
## mu[5]     1.2797278    1.5513882    2.0473226 5721.817 0.9999846
## mu[6]     1.5380614    1.8332893    2.4259460 4226.291 1.0011288
## mu[7]     1.9241888    2.0618267    2.3328780 5658.857 1.0001068
## mu[8]     1.6481938    1.9170364    2.4079248 5427.937 0.9992071
## mu[9]     0.9735121    1.1391354    1.4439216 5391.431 0.9994766
## mu[10]    1.2258000    1.4337172    1.8345071 4883.465 0.9998651
## mu[11]    1.4180848    1.6547494    2.0926062 5112.371 0.9992111
## mu[12]    1.7521597    2.0050044    2.5039842 6502.429 0.9992633
## mu[13]    1.0807130    1.2918436    1.6999479 5060.920 0.9991332
## mu[14]    1.8131122    1.9522390    2.2234262 4994.679 0.9996435
## mu[15]    1.0240681    1.2797681    1.7728313 5841.564 0.9992350
## mu[16]    0.7282358    1.0805540    1.7895215 6024.028 0.9998475
## mu[17]    0.7521424    1.0086231    1.5075821 5747.935 0.9993141
## mu[18]    0.9820686    1.1321497    1.4181809 5627.740 0.9997708
## mu[19]    1.3269253    1.3930391    1.5252363 5290.646 0.9996780
## mu[20]    1.8144080    2.1165882    2.7147030 4384.326 0.9998382
## mu[21]    1.6671654    1.8331291    2.1499363 4857.142 0.9999792
## mu[22]    0.6619321    0.8714278    1.2881674 5732.012 0.9997474
## mu[23]    1.0557678    1.4294541    2.1334555 4866.319 0.9994674
## mu[24]    1.9572158    2.1286620    2.4682109 5277.078 1.0010838
## mu[25]    1.8656547    2.0031776    2.2467665 5174.894 0.9997194
## mu[26]    1.3201217    1.3697230    1.4703725 5234.859 1.0010927
## mu[27]    1.5571246    1.7663021    2.1970115 6243.579 0.9994621
## mu[28]    0.8709221    1.0994428    1.5500089 5155.729 0.9997824
## mu[29]    1.0893937    1.3887654    1.9777529 5090.127 0.9995341
## mu[30]    0.9645854    1.1255377    1.4175400 4915.711 0.9998218
## mu[31]    2.0371416    2.2756527    2.7215379 5182.398 1.0003250
## mu[32]    1.2629727    1.5162938    2.0491196 5176.135 0.9992757
## mu[33]    2.0657631    2.3332609    2.8139849 5438.897 0.9994133
## mu[34]    1.1604451    1.4615739    2.0065526 4966.919 0.9998011
## mu[35]    0.4766516    0.6616156    1.0618081 5296.797 0.9994115
## mu[36]    2.5932028    2.9613089    3.6664554 5206.911 0.9996905
## mu[37]    0.4158555    0.5882675    0.9292967 4734.173 1.0006043
## mu[38]    1.5175816    1.7827755    2.2983308 5269.099 0.9995597
## mu[39]    1.6250678    1.8510458    2.3158593 4626.683 0.9999252
## mu[40]    2.1391047    2.3929543    2.8948609 4976.935 1.0002834
## mu[41]    1.9033105    2.0793635    2.4250060 5344.310 0.9995403
## mu[42]    1.3477827    1.8529042    2.7772296 4547.569 0.9998741
## mu[43]    1.2544855    1.4353573    1.7517407 5119.983 0.9995517
## mu[44]    1.0119102    1.2014459    1.5762207 4831.632 0.9992527
## mu[45]    1.1103477    1.2570460    1.5543138 5017.246 0.9998394
## mu[46]    1.2555786    1.4855625    1.9147366 5021.635 0.9996903
## mu[47]    0.6300522    0.9880093    1.6433680 5152.313 0.9999010
## mu[48]    1.1104897    1.2784609    1.6172714 5820.285 0.9990674
## mu[49]    1.6187340    1.7615153    2.0370319 5291.246 0.9996056
## mu[50]    2.4795667    3.0369555    4.0323720 5033.130 0.9993847
## mu[51]    2.1615250    2.4153302    2.8954881 5467.480 0.9993554
## mu[52]    1.9373423    2.2495368    2.8226650 5342.102 1.0004513
## mu[53]    1.0612748    1.3529580    1.9461049 5010.755 1.0003840
## mu[54]    1.2550703    1.3580708    1.5546298 5693.063 0.9995955
## mu[55]    1.3739507    1.5627293    1.9175355 5182.219 0.9995236
## mu[56]    0.7326126    1.0361603    1.5716687 5456.297 0.9997436
## mu[57]    0.6929797    0.8995471    1.2943597 5057.453 1.0003098
## mu[58]    1.6965030    1.9673631    2.4531736 4987.748 0.9996922
## mu[59]    1.3935096    1.6530787    2.1807781 5491.540 0.9995229
## mu[60]    1.3064981    1.6661356    2.3372386 4259.610 0.9994474
## mu[61]    1.1269041    1.2201668    1.3951591 4177.312 1.0001917
## mu[62]    1.8650606    2.1001328    2.5380556 5405.165 1.0000716
## mu[63]    1.4547813    1.7634189    2.3338514 5158.861 0.9996034
## mu[64]    1.8034328    1.9646250    2.2582409 4548.090 1.0005226
## mu[65]    1.3232838    1.6758949    2.3928390 5755.161 1.0002474
## mu[66]    1.2872787    1.4261179    1.6974246 6108.639 0.9994740
## mu[67]    1.6074964    1.7508355    2.0116309 4887.977 0.9997619
## mu[68]    1.1213777    1.3072504    1.6464764 5486.811 0.9995822
## mu[69]    1.2655578    1.5171240    1.9937313 5236.165 1.0001208
## mu[70]    0.8263535    0.8772136    0.9726447 5068.240 1.0002380
## mu[71]    1.4117050    1.5173229    1.7037469 4963.607 1.0007141
## mu[72]    1.6099132    1.7693583    2.0954182 5093.911 0.9997535
## mu[73]    1.7911447    2.1748936    2.8257815 5197.359 0.9994840
## mu[74]    1.0177488    1.2669327    1.7390951 4785.794 0.9995969
## mu[75]    1.5044475    1.8066360    2.3724783 5374.396 0.9997485
## mu[76]    1.8422538    2.0909232    2.5630840 5403.332 0.9996254
## mu[77]    1.7361210    1.9301749    2.3019558 4780.764 1.0004296
## mu[78]    1.0477639    1.2671975    1.6937840 5483.964 1.0009945
## mu[79]    0.5241083    0.7977090    1.2889539 5514.080 0.9999812
## mu[80]    1.2885462    1.3634653    1.5000135 5749.613 0.9993348
## mu[81]    2.2337085    2.5443309    3.1431362 4861.862 0.9997871
## mu[82]    2.2334173    2.7275068    3.7388409 4756.241 0.9996835
## mu[83]    1.4868301    1.6421098    1.9241626 5545.439 0.9993294
## mu[84]    1.6137527    1.7506795    2.0262397 4883.139 0.9997881
## mu[85]    1.2221858    1.5813139    2.2839771 5547.316 0.9995661
## sigma     0.7670999    0.7800618    0.8056710 4658.142 0.9995385
## lp__   -217.3323174 -212.7460121 -204.5471892 1584.698 1.0013902

有多少县,就有多少个模型,每个模型有一个 \(\mu\),参数\(\sigma\)是共同的。需要注意的是,每组之间彼此独立的,没有共享信息。

70.2.4 partially pooled model

和 “no-pooling model” 模型一样,每个县都有自己的均值,但是,这些县彼此会分享信息,一个县获取的信息可以帮助我们估计其它县的均值。

  • 模型同时考虑各个类别数据中的信息以及整个群体中的信息
  • 怎么叫共享信息?参数来自同一个分布
  • 怎么做到的呢?通过先验

\[ \begin{aligned}[t] y_i &\sim \operatorname{normal}(\mu_{j[i]}, \sigma) \\ \mu_j &\sim \operatorname{normal}(\gamma, \tau) \\ \gamma &\sim \operatorname{normal}(0, 5) \\ \tau &\sim \operatorname{exp}(1) \end{aligned} \]

每个县的氡含量均值\(\mu_j\)都服从均值为 \(\gamma\)、标准差为 \(\tau\) 的正态分布。但先验分布中的参数 \(\gamma\)\(\tau\) 都各自有自己的先验分布,即参数的参数, 通常称之为超参数,这就是多层模型中”层”的来历,\(\mu_j\) 是第一层参数,\(\gamma\) 是第二层参数。

  • \(\gamma\)\(\tau\) 的先验称为 超先验分布
  • 超参数是多层模型的标志。
stan_program <- "
data {
  int<lower=1> N;                            
  int<lower=2> J;                     
  int<lower=1, upper=J> county[N]; 
  vector[N] y; 
}
parameters {
  vector[J] mu;
  real mu_a;
  real<lower=0> sigma_y;
  real<lower=0> sigma_a;
}
model {
  mu_a ~ normal(0, 5);
  sigma_a ~ exponential(1);
  sigma_y ~ exponential(1);
  
  mu ~ normal(mu_a, sigma_a);
  
  for(i in 1:N) {
    y[i] ~ normal(mu[county[i]], sigma_y);
  }
}
"

stan_data <- list(
  N      = nrow(radon),
  J      = length(unique(radon$county)),
  county = as.numeric(radon$county),
  y      = radon$log_radon
)

fit_partial_pooling <- stan(model_code = stan_program, data = stan_data)
summary(fit_partial_pooling)$summary
##                 mean      se_mean         sd         2.5%          25%
## mu[1]      1.1084097 0.0031081165 0.24060087    0.6303011    0.9508302
## mu[2]      0.9450575 0.0013475712 0.10339071    0.7433346    0.8741160
## mu[3]      1.2717704 0.0027884812 0.24927733    0.7867879    1.1022398
## mu[4]      1.2736759 0.0023077119 0.20769081    0.8596611    1.1305216
## mu[5]      1.3234032 0.0027132721 0.23420580    0.8729979    1.1677823
## mu[6]      1.4075719 0.0027665132 0.24870724    0.9252210    1.2428811
## mu[7]      1.7406398 0.0024489031 0.17559019    1.4087052    1.6194460
## mu[8]      1.4626025 0.0030320282 0.24489912    0.9801245    1.3049055
## mu[9]      1.1249570 0.0023957654 0.19040825    0.7479457    0.9955339
## mu[10]     1.2848871 0.0025048487 0.21741182    0.8539388    1.1409645
## mu[11]     1.3852742 0.0026100444 0.22543990    0.9434696    1.2293096
## mu[12]     1.5061957 0.0027741109 0.23825752    1.0465163    1.3445244
## mu[13]     1.2275044 0.0025616822 0.21766809    0.8145285    1.0799403
## mu[14]     1.6595746 0.0022222466 0.17362505    1.3307863    1.5381041
## mu[15]     1.2206647 0.0026609452 0.23829773    0.7523013    1.0614768
## mu[16]     1.2044767 0.0029765110 0.27175609    0.6697289    1.0240399
## mu[17]     1.1229205 0.0030690233 0.24913780    0.6041515    0.9635835
## mu[18]     1.1194638 0.0022419223 0.18396948    0.7635975    0.9923661
## mu[19]     1.3306430 0.0011280749 0.09350621    1.1496730    1.2661231
## mu[20]     1.4991115 0.0030716519 0.24742736    1.0310134    1.3336007
## mu[21]     1.5367547 0.0023197706 0.19652059    1.1518894    1.3997366
## mu[22]     1.0259162 0.0028694928 0.23028368    0.5646807    0.8734896
## mu[23]     1.2872281 0.0030130750 0.26305912    0.7686607    1.1116337
## mu[24]     1.6998061 0.0026920862 0.20266336    1.3058390    1.5668371
## mu[25]     1.6987418 0.0024676486 0.17136889    1.3582196    1.5835377
## mu[26]     1.3227316 0.0008689561 0.07150034    1.1776430    1.2756592
## mu[27]     1.4504790 0.0024731445 0.22112748    1.0136073    1.3027154
## mu[28]     1.1356421 0.0027468452 0.23087771    0.6763061    0.9824189
## mu[29]     1.2714399 0.0027156391 0.24970588    0.7689937    1.1066899
## mu[30]     1.1151932 0.0020199915 0.18226151    0.7632167    0.9947194
## mu[31]     1.6499409 0.0034018140 0.24040787    1.2027026    1.4871254
## mu[32]     1.3136976 0.0028256062 0.23465862    0.8531251    1.1568867
## mu[33]     1.6268729 0.0033024190 0.24134883    1.1765446    1.4574877
## mu[34]     1.2896807 0.0026260702 0.24858083    0.7948018    1.1272285
## mu[35]     0.8964984 0.0029328330 0.22065185    0.4411538    0.7559838
## mu[36]     1.6513310 0.0035115591 0.28088427    1.1352804    1.4653745
## mu[37]     0.8098652 0.0029318537 0.21066208    0.3919079    0.6705600
## mu[38]     1.4205333 0.0029216064 0.22949599    0.9734380    1.2726107
## mu[39]     1.4664953 0.0025410622 0.23189374    1.0232956    1.3052568
## mu[40]     1.6537988 0.0033529805 0.24945648    1.1709104    1.4833785
## mu[41]     1.6484800 0.0026080958 0.21179480    1.2417868    1.5047071
## mu[42]     1.3546495 0.0031310680 0.28975954    0.7642863    1.1664047
## mu[43]     1.2960342 0.0021925404 0.19874955    0.9141612    1.1601260
## mu[44]     1.1771211 0.0023450934 0.20335116    0.7749813    1.0370906
## mu[45]     1.1909438 0.0020592835 0.17819685    0.8404829    1.0702950
## mu[46]     1.3036117 0.0026578850 0.23455633    0.8354640    1.1440064
## mu[47]     1.1765884 0.0033048868 0.26943373    0.6430813    0.9959328
## mu[48]     1.2094601 0.0020649328 0.19455545    0.8165176    1.0768322
## mu[49]     1.5297237 0.0020070264 0.17832040    1.1812129    1.4090134
## mu[50]     1.5041126 0.0036083310 0.28744636    0.9401003    1.3125738
## mu[51]     1.6661378 0.0035599319 0.23996910    1.2183687    1.4997989
## mu[52]     1.5368641 0.0031398596 0.25359095    1.0411684    1.3708124
## mu[53]     1.2569541 0.0030009183 0.24767698    0.7626649    1.0921831
## mu[54]     1.2748831 0.0015553870 0.14137361    0.9910852    1.1792916
## mu[55]     1.3697212 0.0024166694 0.20362970    0.9762653    1.2288922
## mu[56]     1.1546667 0.0030082357 0.24426284    0.6683294    0.9933597
## mu[57]     1.0416396 0.0027847617 0.22898227    0.5746120    0.8909629
## mu[58]     1.4784601 0.0030322299 0.24413051    1.0044698    1.3176143
## mu[59]     1.3633894 0.0030575770 0.24194076    0.8825282    1.2054204
## mu[60]     1.3383092 0.0031078867 0.27363666    0.8016936    1.1562806
## mu[61]     1.1668776 0.0014321974 0.12615682    0.9213661    1.0807301
## mu[62]     1.5716926 0.0028122961 0.22953094    1.1392952    1.4134942
## mu[63]     1.3812329 0.0026802498 0.25244884    0.8864999    1.2081973
## mu[64]     1.6306558 0.0021994300 0.18591666    1.2662753    1.5059411
## mu[65]     1.3488445 0.0028648817 0.26884156    0.8231664    1.1698666
## mu[66]     1.3059936 0.0020022572 0.17266949    0.9734341    1.1915084
## mu[67]     1.5225410 0.0023649853 0.17406350    1.1774614    1.4046757
## mu[68]     1.2259948 0.0024301862 0.20301958    0.8229721    1.0921685
## mu[69]     1.3216701 0.0028125968 0.24018607    0.8542927    1.1568585
## mu[70]     0.8589878 0.0009169858 0.07002820    0.7177547    0.8136742
## mu[71]     1.3971295 0.0016041080 0.13590345    1.1352712    1.3077034
## mu[72]     1.5011196 0.0022827765 0.19286497    1.1314313    1.3747782
## mu[73]     1.4606933 0.0033471381 0.27210975    0.9395088    1.2783641
## mu[74]     1.2266641 0.0025570549 0.23129564    0.7506740    1.0698377
## mu[75]     1.3939941 0.0028916241 0.24807180    0.9152214    1.2269920
## mu[76]     1.5388389 0.0031623381 0.24799422    1.0678073    1.3659047
## mu[77]     1.5556031 0.0027089639 0.21494868    1.1469020    1.4057152
## mu[78]     1.2222426 0.0028279068 0.23525615    0.7517612    1.0707774
## mu[79]     1.0373341 0.0033868568 0.24243530    0.5476955    0.8778477
## mu[80]     1.2982671 0.0010937532 0.10291947    1.0952994    1.2272563
## mu[81]     1.6316501 0.0034560498 0.25980053    1.1339546    1.4496138
## mu[82]     1.4746789 0.0036039969 0.28511983    0.9270741    1.2827919
## mu[83]     1.4483523 0.0021673523 0.17212901    1.1147562    1.3309268
## mu[84]     1.5222110 0.0023291264 0.17547953    1.1784026    1.4054971
## mu[85]     1.3164488 0.0028886750 0.26843637    0.7922286    1.1319468
## mu_a       1.3503144 0.0007314467 0.04795069    1.2563785    1.3183838
## sigma_y    0.7674156 0.0002488359 0.01875271    0.7319439    0.7548186
## sigma_a    0.3028885 0.0013765791 0.04697524    0.2191734    0.2694903
## lp__    -157.7138483 0.3354473594 9.58122238 -176.6915211 -164.1803754
##                  50%          75%        97.5%     n_eff      Rhat
## mu[1]      1.1122954    1.2690169    1.5771764 5992.3866 0.9993266
## mu[2]      0.9457923    1.0153845    1.1424840 5886.5325 0.9996637
## mu[3]      1.2801095    1.4425215    1.7574459 7991.5333 0.9995747
## mu[4]      1.2767987    1.4171862    1.6709795 8099.7453 0.9993844
## mu[5]      1.3217271    1.4792899    1.7826411 7450.8972 0.9998490
## mu[6]      1.4061056    1.5702258    1.8979235 8081.8610 0.9993518
## mu[7]      1.7389380    1.8592091    2.0802458 5141.1151 0.9999898
## mu[8]      1.4595738    1.6206148    1.9541018 6523.9103 0.9996129
## mu[9]      1.1235294    1.2573311    1.4963141 6316.5935 0.9992793
## mu[10]     1.2873514    1.4273824    1.7109876 7533.6128 0.9993783
## mu[11]     1.3844900    1.5335465    1.8226319 7460.4626 0.9992098
## mu[12]     1.5046479    1.6640086    1.9755281 7376.4193 0.9993307
## mu[13]     1.2256441    1.3730444    1.6607657 7220.0306 0.9992641
## mu[14]     1.6581402    1.7769122    2.0119008 6104.3623 0.9992024
## mu[15]     1.2242031    1.3799609    1.6778876 8019.8813 0.9991564
## mu[16]     1.2032806    1.3877674    1.7378392 8335.7291 0.9994562
## mu[17]     1.1245713    1.2851490    1.6054131 6589.9004 0.9999045
## mu[18]     1.1222375    1.2461171    1.4696331 6733.6484 0.9992764
## mu[19]     1.3315400    1.3956922    1.5135268 6870.7640 1.0003840
## mu[20]     1.4933475    1.6648926    2.0034493 6488.6067 0.9992286
## mu[21]     1.5355249    1.6668734    1.9276083 7176.7204 0.9999623
## mu[22]     1.0309578    1.1819612    1.4723472 6440.4474 0.9998907
## mu[23]     1.2879734    1.4647287    1.7922002 7622.3142 0.9993139
## mu[24]     1.6992175    1.8324394    2.0975229 5667.2523 0.9994168
## mu[25]     1.6979376    1.8142054    2.0400888 4822.7789 0.9994465
## mu[26]     1.3229862    1.3699134    1.4613441 6770.4959 0.9994536
## mu[27]     1.4454651    1.6002669    1.8931962 7994.4105 0.9997066
## mu[28]     1.1426499    1.2898275    1.5825358 7064.7312 1.0002163
## mu[29]     1.2731047    1.4439204    1.7428034 8454.9969 0.9996843
## mu[30]     1.1157631    1.2331525    1.4647330 8141.2452 0.9993767
## mu[31]     1.6444262    1.8081110    2.1341509 4994.3185 0.9994449
## mu[32]     1.3119601    1.4711528    1.7747903 6896.8334 0.9992576
## mu[33]     1.6217263    1.7845780    2.1233482 5341.0426 0.9995869
## mu[34]     1.2959900    1.4563018    1.7763623 8960.3010 0.9990510
## mu[35]     0.9008154    1.0501834    1.3117502 5660.3134 0.9993095
## mu[36]     1.6456005    1.8347086    2.2227131 6398.1568 0.9998117
## mu[37]     0.8129931    0.9559390    1.1987334 5162.8342 0.9993425
## mu[38]     1.4205746    1.5667655    1.8847472 6170.3071 0.9995504
## mu[39]     1.4700406    1.6189906    1.9300095 8328.1291 0.9993559
## mu[40]     1.6496013    1.8240619    2.1426446 5535.1260 1.0003420
## mu[41]     1.6452140    1.7922436    2.0749544 6594.5251 0.9992021
## mu[42]     1.3541069    1.5424070    1.9119786 8564.2723 0.9996764
## mu[43]     1.2969034    1.4319925    1.6736156 8217.0723 0.9997623
## mu[44]     1.1811435    1.3120129    1.5722875 7519.2239 0.9998434
## mu[45]     1.1928671    1.3106827    1.5442513 7488.0332 0.9994536
## mu[46]     1.3101327    1.4609030    1.7578466 7787.9279 0.9993502
## mu[47]     1.1821782    1.3599207    1.6930701 6646.4656 0.9997660
## mu[48]     1.2092117    1.3390639    1.5946455 8877.1784 0.9994800
## mu[49]     1.5284692    1.6490920    1.8796104 7893.9778 0.9999364
## mu[50]     1.5017914    1.6887381    2.0870259 6346.0119 0.9991088
## mu[51]     1.6640936    1.8291993    2.1527581 4543.8842 0.9997986
## mu[52]     1.5296555    1.7049699    2.0594901 6522.9951 0.9994294
## mu[53]     1.2553924    1.4235989    1.7398266 6811.8168 0.9993383
## mu[54]     1.2752268    1.3700736    1.5543345 8261.5161 0.9996061
## mu[55]     1.3690474    1.5098458    1.7617188 7099.8271 0.9996028
## mu[56]     1.1588658    1.3147636    1.6356037 6593.1212 0.9994784
## mu[57]     1.0469475    1.2025334    1.4715052 6761.2598 0.9993354
## mu[58]     1.4748220    1.6413019    1.9743942 6482.1619 0.9992823
## mu[59]     1.3619997    1.5233835    1.8439957 6261.2823 0.9998367
## mu[60]     1.3394542    1.5225007    1.8623667 7752.0792 0.9991352
## mu[61]     1.1676222    1.2531585    1.4102341 7759.1777 0.9997426
## mu[62]     1.5690924    1.7226368    2.0264454 6661.3212 0.9995500
## mu[63]     1.3772951    1.5515939    1.8882086 8871.4827 0.9993289
## mu[64]     1.6285832    1.7556093    1.9989379 7145.2319 0.9996486
## mu[65]     1.3448057    1.5245950    1.8988129 8806.0152 0.9998843
## mu[66]     1.3037688    1.4212892    1.6430145 7436.8919 0.9996459
## mu[67]     1.5221444    1.6395988    1.8700785 5416.9964 1.0003312
## mu[68]     1.2268063    1.3627961    1.6222378 6979.0569 0.9992313
## mu[69]     1.3186360    1.4866100    1.8009393 7292.5711 0.9993519
## mu[70]     0.8593464    0.9037434    1.0011889 5832.0433 0.9999353
## mu[71]     1.3973049    1.4894151    1.6638424 7177.8401 0.9996263
## mu[72]     1.4953451    1.6307353    1.8794483 7138.0555 0.9992408
## mu[73]     1.4562429    1.6356669    2.0089372 6609.0790 1.0004529
## mu[74]     1.2289191    1.3909436    1.6737296 8181.9114 0.9991446
## mu[75]     1.3904320    1.5641072    1.8807771 7359.8868 0.9995104
## mu[76]     1.5336787    1.7006009    2.0443620 6149.8781 1.0001208
## mu[77]     1.5534190    1.6956001    1.9854102 6295.9771 0.9991148
## mu[78]     1.2275924    1.3792875    1.6699779 6920.7280 0.9995299
## mu[79]     1.0425418    1.2025620    1.4927602 5123.8693 1.0001928
## mu[80]     1.3000062    1.3694426    1.5022114 8854.3445 0.9992527
## mu[81]     1.6238384    1.8035967    2.1434413 5650.9322 1.0001997
## mu[82]     1.4727950    1.6611511    2.0341499 6258.7275 0.9993069
## mu[83]     1.4484795    1.5669512    1.7842118 6307.3822 0.9994626
## mu[84]     1.5222154    1.6389520    1.8724023 5676.3191 0.9999284
## mu[85]     1.3166099    1.4966687    1.8513674 8635.4573 0.9997583
## mu_a       1.3499510    1.3829318    1.4437952 4297.5842 1.0006525
## sigma_y    0.7672437    0.7795480    0.8051034 5679.3917 0.9995405
## sigma_a    0.3002997    0.3336296    0.4015116 1164.4896 1.0007051
## lp__    -157.7957095 -151.1305719 -139.6007551  815.8176 1.0017382

70.2.5 对比三个模型

对比三个模型的结果

overall_mean <- broom.mixed::tidyMCMC(fit_pooling) %>% 
  filter(term == "mu") %>% 
  pull(estimate)



df_no_pooling <- fit_no_pooling %>% 
  tidybayes::gather_draws(mu[i]) %>%
  tidybayes::mean_hdi() %>% 
  ungroup() %>% 
  mutate(
    type = "no_pooling"
  ) %>% 
  select(type, .value) %>% 
  bind_cols(df_n_county)



df_partial_pooling <- fit_partial_pooling %>% 
  tidybayes::gather_draws(mu[i]) %>%
  tidybayes::mean_hdi() %>% 
  ungroup() %>% 
  mutate(
    type = "partial_pooling"
  ) %>% 
  select(type, .value) %>% 
  bind_cols(df_n_county)


bind_rows(df_no_pooling, df_partial_pooling) %>% 
  ggplot(
    aes(x = n, y = .value, color = type)
  ) +
  geom_point(size = 3) +
  geom_hline(yintercept = overall_mean) +
  scale_x_log10()
  • 层级模型可以实现不同分组之间的信息交换
  • 分组的均值向整体的均值靠拢(收缩)
  • 分组的样本量越小,收缩效应越明显

用我们四川火锅记住他们。

70.3 增加预测变量

70.3.1 增加楼层floor作为预测变量

\[ \begin{aligned} y_i &\sim N(\mu_i, \sigma^2) \\ \mu_i &= \alpha_{j[i]} + \beta~\mathtt{floor}_i \\ \alpha_j &\sim \operatorname{normal}(\gamma, \tau) \\ \beta &\sim \operatorname{normal}(0, 2.5)\\ \gamma &\sim \operatorname{normal}(0, 10) \\ \tau &\sim \operatorname{exp}(1) \\ \end{aligned} \] 不同的县有不同的截距,但有共同的\(\beta\),因此被称为变化的截距

stan_program <- "
data {
  int<lower=1> N;                            
  int<lower=2> J;                     
  int<lower=1, upper=J> county[N]; 
  vector[N] x; 
  vector[N] y; 
}
parameters {
  vector[J] alpha;
  real beta;
  real gamma;
  real<lower=0> sigma_y;
  real<lower=0> sigma_a;
}
model {
  vector[N] mu;
  for(i in 1:N) {
    mu[i] = alpha[county[i]] + beta * x[i];
  }
  
  for(i in 1:N) {
    y[i] ~ normal(mu[i], sigma_y);
  }
  
  alpha ~ normal(gamma, sigma_a);
  gamma ~ normal(0, 10);
  beta ~ normal(0, 2.5);
  sigma_a ~ exponential(1);
  sigma_y ~ exponential(1);

}
"

stan_data <- list(
  N      = nrow(radon),
  J      = length(unique(radon$county)),
  county = as.numeric(radon$county),
  x      = radon$floor,
  y      = radon$log_radon
)

fit_intercept_partial <- stan(model_code = stan_program, data = stan_data)
summary(fit_intercept_partial)$summary
##                   mean      se_mean         sd         2.5%          25%
## alpha[1]     1.2253595 0.0031349786 0.24948424    0.7275480    1.0605371
## alpha[2]     0.9810239 0.0011624098 0.09552055    0.7944613    0.9184936
## alpha[3]     1.5055523 0.0028623219 0.26104231    0.9875449    1.3348424
## alpha[4]     1.5345793 0.0026043076 0.21616788    1.0956299    1.3912246
## alpha[5]     1.4732604 0.0027765939 0.23737434    1.0028969    1.3161273
## alpha[6]     1.5081851 0.0029886648 0.25588109    1.0003186    1.3323714
## alpha[7]     1.8743178 0.0023186098 0.17143533    1.5333894    1.7579332
## alpha[8]     1.7067874 0.0030001593 0.24046697    1.2547277    1.5406262
## alpha[9]     1.1947810 0.0024438214 0.19235874    0.8096286    1.0655495
## alpha[10]    1.5226042 0.0025163120 0.22100398    1.0893845    1.3741522
## alpha[11]    1.4606131 0.0027245356 0.22214096    1.0277910    1.3085287
## alpha[12]    1.6084497 0.0027044228 0.24407328    1.1358932    1.4432797
## alpha[13]    1.2748870 0.0027232509 0.22676506    0.8143976    1.1264089
## alpha[14]    1.8564862 0.0021137333 0.16364016    1.5361029    1.7470814
## alpha[15]    1.4300135 0.0028231027 0.23965291    0.9428906    1.2721858
## alpha[16]    1.2750751 0.0033813874 0.27558544    0.7292171    1.0897030
## alpha[17]    1.3870212 0.0027942344 0.23791296    0.9107281    1.2283613
## alpha[18]    1.2609722 0.0022059247 0.17650981    0.9215523    1.1426884
## alpha[19]    1.3797592 0.0010950297 0.08868494    1.2028861    1.3198307
## alpha[20]    1.6094186 0.0030734850 0.25361820    1.1212622    1.4441969
## alpha[21]    1.6540765 0.0023429082 0.19794059    1.2600069    1.5237692
## alpha[22]    1.1124283 0.0029368274 0.22924631    0.6641916    0.9626498
## alpha[23]    1.4689004 0.0033625438 0.28070512    0.8977475    1.2905261
## alpha[24]    1.8837917 0.0026055898 0.19781071    1.4959077    1.7463532
## alpha[25]    1.8326066 0.0020718053 0.16748915    1.5077638    1.7208107
## alpha[26]    1.3949890 0.0008467816 0.07385359    1.2459093    1.3457149
## alpha[27]    1.6452864 0.0026838384 0.21382915    1.2274730    1.5000976
## alpha[28]    1.3765997 0.0025817593 0.23393745    0.9203361    1.2188568
## alpha[29]    1.3411419 0.0028983764 0.25916517    0.8148836    1.1711518
## alpha[30]    1.1425622 0.0022513982 0.17460230    0.8004576    1.0211954
## alpha[31]    1.7589059 0.0028079699 0.23402528    1.3037749    1.5966613
## alpha[32]    1.3944994 0.0027091914 0.23634658    0.9349874    1.2371606
## alpha[33]    1.7489508 0.0028771090 0.24279506    1.2844597    1.5841813
## alpha[34]    1.5339653 0.0028906306 0.25151559    1.0358852    1.3637245
## alpha[35]    1.1275376 0.0028084947 0.21727448    0.6894268    0.9830685
## alpha[36]    1.8950371 0.0040854610 0.29036082    1.3622655    1.6979987
## alpha[37]    0.8570442 0.0031716753 0.20736104    0.4522754    0.7156116
## alpha[38]    1.6450072 0.0028320516 0.23637647    1.1914926    1.4846720
## alpha[39]    1.6236588 0.0026765435 0.22264829    1.1983281    1.4743276
## alpha[40]    1.8549183 0.0031607621 0.25274544    1.3669403    1.6847465
## alpha[41]    1.7834502 0.0025659180 0.21105417    1.3709577    1.6448450
## alpha[42]    1.4762175 0.0035225467 0.29488681    0.8807338    1.2825599
## alpha[43]    1.5751920 0.0022685080 0.19475093    1.1998506    1.4417524
## alpha[44]    1.2673923 0.0027993849 0.21546520    0.8454602    1.1228072
## alpha[45]    1.3621190 0.0020192976 0.17514130    1.0286456    1.2423918
## alpha[46]    1.3710712 0.0029656801 0.23404545    0.9151093    1.2111608
## alpha[47]    1.3414904 0.0030700236 0.27454742    0.7962551    1.1628099
## alpha[48]    1.2957838 0.0023684741 0.19494473    0.9228262    1.1668844
## alpha[49]    1.6548266 0.0020336953 0.17485322    1.3099277    1.5364161
## alpha[50]    1.6559682 0.0034148128 0.29976602    1.0642015    1.4609043
## alpha[51]    1.7847685 0.0032229080 0.24934015    1.2949544    1.6169266
## alpha[52]    1.6596146 0.0029050725 0.25400599    1.1535656    1.4946759
## alpha[53]    1.4057082 0.0027745917 0.25588234    0.9067935    1.2275390
## alpha[54]    1.3672563 0.0017649479 0.14106188    1.0911222    1.2715900
## alpha[55]    1.5767831 0.0022972889 0.20497746    1.1738086    1.4412638
## alpha[56]    1.3791295 0.0027800927 0.25639533    0.8690078    1.2093369
## alpha[57]    1.1238226 0.0029304330 0.22137521    0.6931982    0.9719436
## alpha[58]    1.6644312 0.0029349908 0.24135264    1.1939521    1.4981836
## alpha[59]    1.5945163 0.0027549421 0.24767128    1.1147852    1.4224660
## alpha[60]    1.4377266 0.0032756535 0.27472901    0.8955201    1.2548075
## alpha[61]    1.2350321 0.0015552232 0.12564861    0.9831787    1.1549748
## alpha[62]    1.7367182 0.0029088158 0.23225547    1.2927070    1.5786551
## alpha[63]    1.5607642 0.0028582141 0.24960799    1.0695431    1.3916569
## alpha[64]    1.7446559 0.0021322543 0.18210402    1.4018753    1.6205410
## alpha[65]    1.4509537 0.0030826806 0.28413235    0.8851802    1.2607909
## alpha[66]    1.6176671 0.0021046662 0.17070986    1.2852280    1.5039620
## alpha[67]    1.7226216 0.0021910936 0.17222761    1.3889535    1.6048886
## alpha[68]    1.2685228 0.0023904576 0.20563456    0.8646097    1.1314938
## alpha[69]    1.3939323 0.0028892463 0.23794423    0.9185173    1.2392242
## alpha[70]    0.9435360 0.0008428266 0.06486792    0.8119202    0.8997147
## alpha[71]    1.5114931 0.0016012358 0.13470415    1.2457639    1.4209407
## alpha[72]    1.5639161 0.0022649151 0.19079302    1.1934639    1.4364760
## alpha[73]    1.5764408 0.0032535994 0.27958614    1.0260077    1.3924012
## alpha[74]    1.2897208 0.0027952218 0.24083449    0.8138872    1.1307607
## alpha[75]    1.5807199 0.0029943380 0.26001806    1.0781176    1.3963953
## alpha[76]    1.7225764 0.0030318683 0.24658837    1.2475494    1.5610194
## alpha[77]    1.6901026 0.0023873614 0.20988864    1.2852271    1.5462302
## alpha[78]    1.4037466 0.0024669616 0.22979159    0.9584463    1.2475604
## alpha[79]    1.1466515 0.0032317904 0.25245303    0.6447416    0.9760041
## alpha[80]    1.3760777 0.0011870554 0.10489989    1.1646802    1.3072691
## alpha[81]    1.9261490 0.0036454626 0.26480298    1.4216657    1.7475330
## alpha[82]    1.6172577 0.0038406748 0.29356574    1.0560573    1.4217625
## alpha[83]    1.6070866 0.0020415003 0.17182506    1.2664474    1.4918271
## alpha[84]    1.6144788 0.0021027534 0.17258776    1.2835120    1.4928643
## alpha[85]    1.4185033 0.0029888840 0.27676221    0.8705873    1.2342730
## beta        -0.6623648 0.0009093930 0.06679569   -0.7935569   -0.7065409
## gamma        1.4920739 0.0007872434 0.05061468    1.3932400    1.4572731
## sigma_y      0.7269327 0.0002107433 0.01692890    0.6942472    0.7152909
## sigma_a      0.3213818 0.0012240634 0.04530957    0.2411180    0.2893991
## lp__      -113.1551496 0.2981632941 8.90554569 -131.3180968 -119.1165036
##                    50%          75%       97.5%     n_eff      Rhat
## alpha[1]     1.2279284    1.3864017   1.7000298 6333.1109 0.9994901
## alpha[2]     0.9798771    1.0435752   1.1672022 6752.6635 1.0000233
## alpha[3]     1.5092366    1.6782183   2.0098157 8317.3475 0.9997371
## alpha[4]     1.5313667    1.6783596   1.9533211 6889.6595 0.9992025
## alpha[5]     1.4712373    1.6323951   1.9385647 7308.7449 0.9992455
## alpha[6]     1.5085638    1.6782017   2.0205352 7330.3037 0.9993166
## alpha[7]     1.8720805    1.9899555   2.2100188 5466.9528 1.0000874
## alpha[8]     1.7011025    1.8627453   2.1948638 6424.2471 0.9992849
## alpha[9]     1.1966803    1.3242141   1.5756415 6195.6221 0.9996715
## alpha[10]    1.5263844    1.6717139   1.9651068 7713.8503 0.9993437
## alpha[11]    1.4583205    1.6086431   1.8953730 6647.7136 0.9992616
## alpha[12]    1.6042856    1.7678151   2.1056905 8145.0041 0.9992740
## alpha[13]    1.2776283    1.4231201   1.7234474 6933.8905 0.9996260
## alpha[14]    1.8570113    1.9650278   2.1878730 5993.4844 0.9999163
## alpha[15]    1.4313625    1.5900803   1.9049468 7206.2954 0.9994343
## alpha[16]    1.2791765    1.4621541   1.7994694 6642.3641 0.9998631
## alpha[17]    1.3855708    1.5477301   1.8532742 7249.5413 0.9992511
## alpha[18]    1.2601841    1.3862314   1.6106666 6402.5995 0.9996301
## alpha[19]    1.3812002    1.4381097   1.5491656 6559.1549 0.9994454
## alpha[20]    1.6065743    1.7780149   2.1182933 6809.2397 1.0000996
## alpha[21]    1.6562953    1.7839794   2.0487515 7137.7142 0.9992523
## alpha[22]    1.1095495    1.2646479   1.5556203 6093.2339 0.9996619
## alpha[23]    1.4655305    1.6546295   2.0358618 6968.9085 0.9993983
## alpha[24]    1.8809344    2.0206447   2.2761840 5763.5162 0.9995372
## alpha[25]    1.8309993    1.9468983   2.1556023 6535.4503 0.9992666
## alpha[26]    1.3956565    1.4466686   1.5359149 7606.7726 1.0004816
## alpha[27]    1.6398157    1.7861366   2.0846794 6347.7684 0.9996471
## alpha[28]    1.3719875    1.5401606   1.8212229 8210.4696 0.9995834
## alpha[29]    1.3434023    1.5139829   1.8484631 7995.4644 0.9998306
## alpha[30]    1.1416462    1.2635199   1.4750548 6014.4417 1.0000444
## alpha[31]    1.7566401    1.9186718   2.2183490 6946.0936 0.9994379
## alpha[32]    1.3939644    1.5538746   1.8611918 7610.6064 0.9993483
## alpha[33]    1.7456617    1.9109727   2.2466004 7121.4276 0.9992019
## alpha[34]    1.5321826    1.7068058   2.0344074 7570.8499 0.9998732
## alpha[35]    1.1291329    1.2710767   1.5432165 5985.0836 0.9994287
## alpha[36]    1.8864300    2.0796670   2.4952800 5051.1922 0.9996748
## alpha[37]    0.8614382    0.9956166   1.2644484 4274.4170 0.9993312
## alpha[38]    1.6381391    1.8041144   2.1200381 6966.3638 0.9994790
## alpha[39]    1.6264648    1.7757701   2.0450621 6919.7453 0.9997341
## alpha[40]    1.8531590    2.0176267   2.3543580 6394.1532 0.9996025
## alpha[41]    1.7800437    1.9207270   2.2033671 6765.5384 0.9998297
## alpha[42]    1.4818607    1.6661544   2.0646448 7008.0498 0.9993853
## alpha[43]    1.5738323    1.7063022   1.9601772 7370.1862 0.9993563
## alpha[44]    1.2701463    1.4114907   1.6863300 5924.1907 0.9996406
## alpha[45]    1.3619716    1.4819492   1.7070468 7522.7469 0.9997994
## alpha[46]    1.3714378    1.5333191   1.8170011 6228.0456 0.9996056
## alpha[47]    1.3470912    1.5179282   1.8861857 7997.4461 0.9996293
## alpha[48]    1.2938967    1.4252110   1.6816753 6774.6321 0.9996719
## alpha[49]    1.6558778    1.7717103   1.9962240 7392.2301 0.9991101
## alpha[50]    1.6534177    1.8472343   2.2681816 7706.0352 0.9995123
## alpha[51]    1.7838036    1.9547404   2.2631512 5985.3369 0.9994620
## alpha[52]    1.6552960    1.8301235   2.1698085 7644.9376 0.9991387
## alpha[53]    1.4051031    1.5814268   1.9033071 8505.1595 0.9994046
## alpha[54]    1.3691407    1.4614940   1.6424096 6387.8623 0.9997053
## alpha[55]    1.5761523    1.7129853   1.9843865 7961.2453 0.9995902
## alpha[56]    1.3768383    1.5519571   1.8819726 8505.5355 0.9997772
## alpha[57]    1.1246056    1.2735038   1.5608942 5706.8223 1.0001692
## alpha[58]    1.6609631    1.8227991   2.1536362 6762.2405 0.9994181
## alpha[59]    1.5956475    1.7629253   2.0694197 8082.1394 0.9997699
## alpha[60]    1.4341653    1.6245282   1.9795062 7034.1744 0.9994704
## alpha[61]    1.2379167    1.3193547   1.4832084 6527.2459 0.9995957
## alpha[62]    1.7336698    1.8919409   2.1988668 6375.2830 0.9991998
## alpha[63]    1.5581858    1.7234367   2.0552368 7626.5384 0.9992914
## alpha[64]    1.7431355    1.8666352   2.0978668 7293.9211 0.9994934
## alpha[65]    1.4524144    1.6350439   2.0062496 8495.4092 0.9991466
## alpha[66]    1.6181518    1.7336943   1.9536508 6578.8613 1.0000080
## alpha[67]    1.7239697    1.8380555   2.0628255 6178.5089 0.9994188
## alpha[68]    1.2694989    1.4084337   1.6613005 7399.9727 0.9992869
## alpha[69]    1.3920774    1.5502779   1.8670746 6782.3658 0.9998827
## alpha[70]    0.9451017    0.9863459   1.0734100 5923.5682 0.9993476
## alpha[71]    1.5109445    1.6048811   1.7758043 7077.0360 0.9991404
## alpha[72]    1.5633848    1.6894112   1.9413150 7096.1225 0.9993415
## alpha[73]    1.5759716    1.7614327   2.1226997 7384.1942 0.9996093
## alpha[74]    1.2917319    1.4503624   1.7526460 7423.4333 0.9993974
## alpha[75]    1.5833216    1.7621626   2.0896714 7540.5907 0.9996483
## alpha[76]    1.7200100    1.8822002   2.1988899 6614.9187 0.9996690
## alpha[77]    1.6889967    1.8326834   2.1161838 7729.3244 0.9994520
## alpha[78]    1.4035490    1.5614726   1.8519235 8676.4781 0.9992113
## alpha[79]    1.1497060    1.3223493   1.6142843 6102.0364 0.9990623
## alpha[80]    1.3761588    1.4489289   1.5776003 7809.2277 0.9994819
## alpha[81]    1.9224432    2.0987766   2.4805861 5276.4332 1.0002672
## alpha[82]    1.6121870    1.8016634   2.2277889 5842.4591 0.9994398
## alpha[83]    1.6092139    1.7213661   1.9460541 7083.9281 0.9994766
## alpha[84]    1.6125475    1.7308274   1.9621729 6736.6393 1.0005635
## alpha[85]    1.4214965    1.5990452   1.9767727 8574.2369 0.9991484
## beta        -0.6615357   -0.6179761  -0.5321654 5395.0277 0.9993255
## gamma        1.4914139    1.5252839   1.5944249 4133.6610 0.9997812
## sigma_y      0.7267046    0.7380043   0.7610607 6452.8289 0.9993803
## sigma_a      0.3193202    0.3501351   0.4172410 1370.1624 1.0027054
## lp__      -112.9006659 -107.0058332 -96.1731596  892.0983 1.0028669

70.3.2 截距中增加预测因子

相当于在第二层参数中增加预测因子

\[ \begin{aligned} y_i &\sim N(\mu_i, ~\sigma) \\ \mu_i &= \alpha_{j[i]} + \beta~\mathtt{floor}_i \\ \alpha_j &\sim \operatorname{normal}(\gamma_0 + \gamma_1~u_j, ~\tau) \\ \beta &\sim \operatorname{normal}(0, 1)\\ \gamma_0 &\sim \operatorname{normal}(0, 2.5)\\ \gamma_1 &\sim \operatorname{normal}(0, 2.5)\\ \tau &\sim \operatorname{exp}(1) \\ \end{aligned} \]

stan_program <- "
data {
  int<lower=0> N;
  vector[N] y;             
  int<lower=0, upper=1> x[N];             
  int<lower=2> J;                     
  int<lower=1, upper=J> county[N]; 
  vector[J] u; 
}
parameters {
  vector[J] alpha;
  real beta;
  real gamma0;
  real gamma1;
  real<lower=0> sigma_a;
  real<lower=0> sigma_y;
}
model {
  vector[N] mu;

  for(i in 1:N) {
    mu[i] = alpha[county[i]] + x[i] * beta;
  }
  
  for(j in 1:J) {
    alpha[j] ~ normal(gamma0 + gamma1 * u[j], sigma_a);
  }
  
  y ~ normal(mu, sigma_y);

  beta ~ normal(0, 1);
  gamma0 ~ normal(0, 2.5);
  gamma1 ~ normal(0, 2.5);
  sigma_a ~ exponential(1);
  sigma_y ~ exponential(1);

}


"

stan_data <- list(
  N      = nrow(radon),
  J      = length(unique(radon$county)),
  county = as.numeric(radon$county),
  x      = radon$floor,
  y      = radon$log_radon,
  u      = unique(radon$log_uranium)
)



fit_intercept_partial_2 <- stan(model_code = stan_program, data = stan_data)
summary(fit_intercept_partial_2, c("beta", "gamma0", "gamma1", "sigma_y", "sigma_a"))$summary
##               mean      se_mean         sd       2.5%        25%        50%
## beta    -0.6452604 0.0010398952 0.06652071 -0.7757324 -0.6900582 -0.6455907
## gamma0   1.4909118 0.0008616731 0.04340030  1.4061917  1.4619970  1.4898379
## gamma1   0.5853796 0.0019706713 0.11146044  0.3690098  0.5091248  0.5854970
## sigma_y  0.7251119 0.0002221356 0.01748984  0.6916058  0.7130007  0.7247674
## sigma_a  0.2444068 0.0013643687 0.03985162  0.1725122  0.2167089  0.2417834
##                75%      97.5%     n_eff      Rhat
## beta    -0.6002769 -0.5128438 4091.9902 0.9994524
## gamma0   1.5200546  1.5779120 2536.8828 1.0000491
## gamma1   0.6591015  0.8049398 3198.9919 1.0002695
## sigma_y  0.7369797  0.7607401 6199.1940 0.9997433
## sigma_a  0.2710905  0.3262400  853.1559 1.0031821

beta怎么解释? - 负号,说明楼上比楼下氡含量低

70.3.3 变化的截距和斜率

之前模型假定,不管哪个县,所有的房屋一楼和二楼的氡含量的差别是一样的(beta系数是不变的),现在,我们将模型进一步扩展,假定一楼和二楼的氡含量的差别不是固定不变的,而是随县变化的,也就说不同县的房屋,一二楼氡含量差别是不同的。

写出变化的截距和斜率模型的数学表达式

\[ \begin{aligned}[t] y_i &\sim \operatorname{Normal}(\mu_i, \sigma_y) \\ \mu_i &= \alpha_{j[i]} + \beta_{j[i]}~\mathtt{floor}_i \\ \begin{bmatrix} \alpha_j \\ \beta_j \end{bmatrix} & \sim \operatorname{MVNormal} \left( \begin{bmatrix} \gamma_0^{\alpha} + \gamma_1^{\alpha} ~ u_j \\ \gamma_0^{\beta} + \gamma_1^{\beta} ~ u_j \\ \end{bmatrix}, ~\mathbf S \right) \\ \mathbf S & = \begin{bmatrix} \sigma_\alpha & 0 \\ 0 & \sigma_\beta \end{bmatrix} \mathbf R \begin{bmatrix} \sigma_\alpha & 0 \\ 0 & \sigma_\beta \end{bmatrix} \\ & = \begin{bmatrix} \sigma_\alpha & 0 \\ 0 & \sigma_\beta \end{bmatrix} \begin{bmatrix} 1 & \rho \\ \rho & 1 \end{bmatrix} \begin{bmatrix} \sigma_\alpha & 0 \\ 0 & \sigma_\beta \end{bmatrix} \\ \gamma_a & \sim \operatorname{Normal}(0, 4) \\ \gamma_b & \sim \operatorname{Normal}(0, 4) \\ \sigma & \sim \operatorname{Exponential}(1) \\ \sigma_\alpha & \sim \operatorname{Exponential}(1) \\ \sigma_\beta & \sim \operatorname{Exponential}(1) \\ \mathbf R & \sim \operatorname{LKJcorr}(2) \end{aligned} \]

  • 模型表达式中 \(\alpha_j\)\(\beta_j\) 不是直接给先验,而是给的层级先验。

  • \(\alpha_j\)\(\beta_j\) 也可能存在关联,常见的有,多元正态分布(Multivariate Gaussian Distribution)

\[ \begin{aligned}[t] \begin{bmatrix} \alpha_j \\ \beta_j \end{bmatrix} &\sim \operatorname{MVNormal}\left(\begin{bmatrix}\gamma_{\alpha} \\ \gamma_{\beta} \end{bmatrix}, \mathbf S\right) \\ \end{aligned} \]

70.3.4 协方差矩阵(covariance matrix)

MASS::mvrnorm(n, mu, Sigma)产生多元高斯分布的随机数,每组随机变量高度相关。 比如,人的身高服从正态分布,人的体重也服从正态分布,同时身高和体重又存在强烈的关联。

  • n: 随机样本的大小
  • mu: 多元高斯分布的均值向量
  • Sigma: 协方差矩阵,主要这里是大写的S (Sigma),提醒我们它是一个矩阵,不是一个数值
a       <- 3.5
b       <- -1
sigma_a <- 1
sigma_b <- 0.5
rho     <- -0.7
mu      <- c(a, b)
cov_ab  <- sigma_a * sigma_b * rho 
sigma   <- matrix(c(sigma_a^2, cov_ab, 
                    cov_ab, sigma_b^2), ncol = 2)
sigma
##       [,1]  [,2]
## [1,]  1.00 -0.35
## [2,] -0.35  0.25
d <- MASS::mvrnorm(1000, mu = mu, Sigma = sigma) %>%
  data.frame() %>%
  set_names("group_a", "group_b")
head(d)
##    group_a    group_b
## 1 4.106095 -1.7055920
## 2 2.069552 -0.6133952
## 3 3.712972 -2.3481713
## 4 3.771514 -0.8929372
## 5 2.197660 -0.7306239
## 6 3.760032 -1.0116445
d %>%
  ggplot(aes(x = group_a)) +
  geom_density(
    color = "transparent",
    fill = "dodgerblue3",
    alpha = 1 / 2
  ) +
  stat_function(
    fun = dnorm,
    args = list(mean = 3.5, sd = 1),
    linetype = 2
  )
d %>%
  ggplot(aes(x = group_b)) +
  geom_density(
    color = "transparent",
    fill = "dodgerblue3",
    alpha = 1 / 2
  ) +
  stat_function(
    fun = dnorm,
    args = list(mean = -1, sd = 0.5),
    linetype = 2
  )
d %>%
  ggplot(aes(x = group_a, y = group_b)) +
  geom_point() +
  stat_ellipse(type = "norm", level = 0.95)

70.3.5 回到模型

在stan中要给协方差矩阵指定一个先验,Priors for Covariances

stan_program <- "
data {
  int<lower=0> N;
  vector[N] y;             
  int<lower=0, upper=1> x[N];             
  int<lower=2> J;                     
  int<lower=1, upper=J> county[N]; 
  vector[J] u; 
}
parameters {
  vector[J] alpha;
  vector[J] beta;
  vector[2] gamma_a;
  vector[2] gamma_b;
  
  real<lower=0> sigma;
  vector<lower=0>[2] tau;
  corr_matrix[2] Rho;
}
transformed parameters {
  vector[2] YY[J];

  for (j in 1:J) {
    YY[j] = [alpha[j], beta[j]]';
  }
}
model {
  vector[N] mu;
  vector[2] MU[J];
  
  sigma ~ exponential(1);
  tau ~ exponential(1);
  Rho ~ lkj_corr(2);
  gamma_a ~ normal(0, 2);
  gamma_b ~ normal(0, 2);
  
  for(i in 1:N) {
    mu[i] = alpha[county[i]] + beta[county[i]] * x[i];  
  }
  
  for(j in 1:J) {
    MU[j, 1] = gamma_a[1] + gamma_a[2] * u[j];
    MU[j, 2] = gamma_b[1] + gamma_b[2] * u[j];
  }
 

  target += multi_normal_lpdf(YY | MU, quad_form_diag(Rho, tau));
  
  y ~ normal(mu, sigma); 
}
"


stan_data <- list(
  N      = nrow(radon),
  J      = length(unique(radon$county)),
  county = as.numeric(radon$county),
  x      = radon$floor,
  y      = radon$log_radon,
  u      = unique(radon$log_uranium)
)


fit_slope_partial <- stan(model_code = stan_program, data = stan_data)
summary(fit_slope_partial, c("alpha"))$summary
##                mean     se_mean         sd      2.5%       25%       50%
## alpha[1]  0.9485857 0.003201862 0.22127021 0.5056961 0.8045684 0.9464021
## alpha[2]  0.9196250 0.001460353 0.09323424 0.7367808 0.8573361 0.9209711
## alpha[3]  1.4305227 0.003394390 0.22540973 1.0041332 1.2812836 1.4274692
## alpha[4]  1.2402123 0.003526593 0.20754611 0.8522981 1.0996431 1.2274336
## alpha[5]  1.4023365 0.002655671 0.21112494 0.9873945 1.2621464 1.4002079
## alpha[6]  1.7009605 0.003008240 0.21810026 1.2726828 1.5564595 1.7013075
## alpha[7]  1.8692580 0.002548541 0.15594938 1.5728665 1.7630466 1.8679354
## alpha[8]  1.7896086 0.003553438 0.21927033 1.3637364 1.6436027 1.7863478
## alpha[9]  1.1255340 0.002418434 0.16792399 0.7933823 1.0087571 1.1271670
## alpha[10] 1.5759148 0.003124468 0.19706674 1.1962884 1.4431988 1.5768011
## alpha[11] 1.2029378 0.002945933 0.20969470 0.7932556 1.0667015 1.2006592
## alpha[12] 1.7000912 0.002762496 0.20253945 1.3103630 1.5648890 1.6931876
## alpha[13] 1.0259464 0.002661313 0.19475814 0.6377635 0.8956724 1.0256049
## alpha[14] 1.9277268 0.002979326 0.15991930 1.6149674 1.8179727 1.9268206
## alpha[15] 1.4119358 0.002991556 0.21658782 0.9831530 1.2723158 1.4108443
## alpha[16] 1.0594757 0.003448858 0.22875501 0.5955149 0.9120977 1.0651593
## alpha[17] 1.6501381 0.003953324 0.21830003 1.2195423 1.5064273 1.6475111
## alpha[18] 1.0582953 0.002643359 0.16902573 0.7170066 0.9468024 1.0573036
## alpha[19] 1.3893659 0.001270459 0.08637285 1.2233660 1.3302512 1.3883650
## alpha[20] 1.7149859 0.002927814 0.21754665 1.2892858 1.5698986 1.7126428
## alpha[21] 1.6724232 0.002407532 0.17591451 1.3277869 1.5535803 1.6729864
## alpha[22] 1.3271907 0.003437574 0.20142151 0.9117362 1.1971244 1.3304520
## alpha[23] 1.7216194 0.003294320 0.23106503 1.2597988 1.5669272 1.7260377
## alpha[24] 1.8812820 0.002967651 0.17993976 1.5307442 1.7658628 1.8754204
## alpha[25] 1.8090786 0.002445085 0.15653969 1.5121255 1.7036669 1.8074955
## alpha[26] 1.3973632 0.001075166 0.06984851 1.2578808 1.3514755 1.3969072
## alpha[27] 1.8313561 0.003107676 0.19330508 1.4627063 1.6998629 1.8310240
## alpha[28] 1.1856483 0.002886492 0.20674097 0.7754743 1.0477219 1.1875839
## alpha[29] 1.0069468 0.003434578 0.23001078 0.5687910 0.8520322 1.0025202
## alpha[30] 1.0022842 0.002318336 0.16486999 0.6840539 0.8913405 1.0012058
## alpha[31] 1.8217637 0.002909109 0.19797559 1.4538653 1.6842437 1.8141125
## alpha[32] 1.3936796 0.002493900 0.19862938 1.0062378 1.2599978 1.3925207
## alpha[33] 1.7229616 0.003266636 0.21014349 1.3283003 1.5799112 1.7176145
## alpha[34] 1.5230532 0.003488952 0.22782653 1.0790789 1.3730411 1.5199123
## alpha[35] 0.7646484 0.003645950 0.21228986 0.3360479 0.6229008 0.7696224
## alpha[36] 1.9195297 0.003898970 0.24202619 1.4631387 1.7598335 1.9096097
## alpha[37] 0.7323369 0.003012812 0.18309784 0.3553514 0.6132745 0.7380863
## alpha[38] 1.2221226 0.003738425 0.22342104 0.7983971 1.0686459 1.2215125
## alpha[39] 1.6711193 0.003099456 0.19907145 1.2924514 1.5382885 1.6684578
## alpha[40] 1.9736662 0.003625402 0.21525943 1.5781671 1.8277727 1.9657799
## alpha[41] 1.8579548 0.002985642 0.18238234 1.5032895 1.7392923 1.8591104
## alpha[42] 1.5712177 0.003490062 0.24393860 1.0926753 1.4128772 1.5701360
## alpha[43] 1.7266521 0.007073845 0.20387845 1.3612303 1.5847170 1.7154103
## alpha[44] 1.3391735 0.002539689 0.18617876 0.9755123 1.2149504 1.3382477
## alpha[45] 1.4627446 0.002480657 0.17014576 1.1177052 1.3500993 1.4639976
## alpha[46] 1.4145408 0.002815072 0.20064027 1.0241935 1.2748476 1.4179530
## alpha[47] 1.2882327 0.003378599 0.23223997 0.8319285 1.1369270 1.2873638
## alpha[48] 1.2923771 0.002347879 0.17295997 0.9493864 1.1779821 1.2938361
## alpha[49] 1.7307030 0.002545991 0.15813071 1.4249886 1.6239160 1.7292909
## alpha[50] 1.8423993 0.003484435 0.23842763 1.3771592 1.6817731 1.8417545
## alpha[51] 1.8257335 0.003364185 0.21001578 1.4087449 1.6893060 1.8218711
## alpha[52] 1.8138331 0.003387386 0.21798274 1.3977305 1.6653419 1.8126370
## alpha[53] 1.5829266 0.003074397 0.22222130 1.1418732 1.4372833 1.5871107
## alpha[54] 1.4547713 0.001975237 0.13353488 1.1945659 1.3630950 1.4561092
## alpha[55] 1.4522050 0.002809668 0.19027949 1.0907795 1.3223355 1.4498008
## alpha[56] 1.3919289 0.003393043 0.23042986 0.9206087 1.2415737 1.3900413
## alpha[57] 1.1422393 0.002845615 0.18918255 0.7517573 1.0190589 1.1452757
## alpha[58] 1.8434254 0.003074870 0.21146535 1.4408655 1.6980453 1.8454330
## alpha[59] 1.7265386 0.003635135 0.21701611 1.3131281 1.5855884 1.7209879
## alpha[60] 1.6111580 0.003443286 0.22287303 1.1620506 1.4609110 1.6101535
## alpha[61] 1.1653287 0.001590805 0.11484688 0.9382232 1.0904011 1.1650632
## alpha[62] 1.8097757 0.003187521 0.20478252 1.4005092 1.6762595 1.8110029
## alpha[63] 1.7285050 0.003241851 0.21528481 1.3074052 1.5784829 1.7280405
## alpha[64] 1.7605778 0.002619082 0.16632560 1.4352331 1.6474215 1.7587183
## alpha[65] 1.7569678 0.003432346 0.23425737 1.2878242 1.6066911 1.7629980
## alpha[66] 1.4824280 0.002895226 0.17108284 1.1521526 1.3664697 1.4827994
## alpha[67] 1.4603594 0.003271784 0.17064392 1.1278982 1.3467043 1.4559051
## alpha[68] 1.3388443 0.002443013 0.17500021 0.9794749 1.2206377 1.3428540
## alpha[69] 1.0989652 0.003173102 0.21013535 0.6876524 0.9639804 1.0998895
## alpha[70] 0.9717896 0.001359045 0.07324072 0.8283938 0.9215583 0.9720588
## alpha[71] 1.5449350 0.001920762 0.12750106 1.2926845 1.4601985 1.5468064
## alpha[72] 1.6386628 0.002440803 0.17169818 1.2926751 1.5247012 1.6408200
## alpha[73] 1.8158915 0.003508705 0.23038545 1.3824582 1.6592115 1.8130564
## alpha[74] 1.4920789 0.003253550 0.20903033 1.0511166 1.3584350 1.4979913
## alpha[75] 1.5033303 0.003226265 0.21940305 1.0754159 1.3629887 1.5010869
## alpha[76] 1.9072641 0.003179838 0.20742233 1.5142213 1.7655025 1.9030983
## alpha[77] 1.7166919 0.003107124 0.18848400 1.3504138 1.5905990 1.7118861
## alpha[78] 1.1033970 0.003151375 0.20741084 0.6858810 0.9688855 1.0991259
## alpha[79] 1.3615401 0.003534562 0.21372290 0.9289004 1.2224945 1.3670589
## alpha[80] 1.3737863 0.001496856 0.09964062 1.1779105 1.3063940 1.3747668
## alpha[81] 1.8602679 0.003909025 0.23026261 1.4320716 1.7054901 1.8529363
## alpha[82] 1.7219101 0.003504594 0.23891516 1.2585437 1.5612930 1.7182977
## alpha[83] 1.7860448 0.003284362 0.17062203 1.4550838 1.6705763 1.7860103
## alpha[84] 1.5676663 0.002281959 0.15531131 1.2730579 1.4606346 1.5634927
## alpha[85] 1.6357712 0.003170700 0.22857980 1.1676800 1.4869464 1.6405394
##                 75%    97.5%     n_eff      Rhat
## alpha[1]  1.0953875 1.391108 4775.7389 0.9997832
## alpha[2]  0.9822724 1.099621 4076.0117 0.9999417
## alpha[3]  1.5745060 1.873965 4409.8292 0.9999382
## alpha[4]  1.3752822 1.665380 3463.5266 0.9998790
## alpha[5]  1.5358671 1.823896 6320.1948 0.9998829
## alpha[6]  1.8481694 2.127000 5256.3875 0.9992806
## alpha[7]  1.9764461 2.176442 3744.4144 1.0016948
## alpha[8]  1.9319467 2.216418 3807.6965 0.9996758
## alpha[9]  1.2416966 1.445683 4821.2221 0.9994303
## alpha[10] 1.7090956 1.976665 3978.0891 0.9995597
## alpha[11] 1.3385761 1.629068 5066.7479 0.9993610
## alpha[12] 1.8356105 2.110604 5375.4654 0.9994290
## alpha[13] 1.1567834 1.396629 5355.4900 0.9999291
## alpha[14] 2.0367919 2.244385 2881.1492 1.0000993
## alpha[15] 1.5515419 1.841525 5241.7196 1.0006071
## alpha[16] 1.2135294 1.487868 4399.3715 1.0002958
## alpha[17] 1.7949494 2.086886 3049.1786 1.0002538
## alpha[18] 1.1680398 1.399757 4088.7757 1.0000725
## alpha[19] 1.4479976 1.560148 4622.0349 0.9992822
## alpha[20] 1.8612247 2.147051 5521.0009 0.9996739
## alpha[21] 1.7914972 2.009616 5338.9912 0.9992717
## alpha[22] 1.4685077 1.716434 3433.2678 1.0007140
## alpha[23] 1.8768337 2.168372 4919.6812 0.9999983
## alpha[24] 2.0005261 2.239869 3676.4496 1.0003064
## alpha[25] 1.9131896 2.126451 4098.8404 1.0008703
## alpha[26] 1.4437072 1.540268 4220.4912 0.9997468
## alpha[27] 1.9624546 2.212182 3869.1457 1.0011933
## alpha[28] 1.3186391 1.588899 5129.9428 0.9997558
## alpha[29] 1.1606225 1.469468 4484.8674 1.0002404
## alpha[30] 1.1140135 1.328834 5057.4356 0.9994148
## alpha[31] 1.9512630 2.228920 4631.3050 0.9999337
## alpha[32] 1.5264598 1.791878 6343.4973 0.9998085
## alpha[33] 1.8603471 2.153573 4138.3811 1.0004863
## alpha[34] 1.6670704 1.979983 4264.0141 0.9997341
## alpha[35] 0.9068832 1.162017 3390.2925 1.0002460
## alpha[36] 2.0724763 2.413496 3853.2292 1.0000801
## alpha[37] 0.8551660 1.078644 3693.3664 1.0006359
## alpha[38] 1.3667057 1.672325 3571.6660 1.0001837
## alpha[39] 1.8037013 2.068559 4125.2178 1.0003299
## alpha[40] 2.1063812 2.439879 3525.4296 1.0008885
## alpha[41] 1.9791443 2.228009 3731.5582 1.0006638
## alpha[42] 1.7302193 2.046582 4885.3391 1.0003340
## alpha[43] 1.8554846 2.166042  830.6757 1.0019830
## alpha[44] 1.4643957 1.701715 5374.0180 0.9996486
## alpha[45] 1.5772492 1.799905 4704.4508 0.9999513
## alpha[46] 1.5523328 1.804726 5079.9227 0.9992668
## alpha[47] 1.4391825 1.749816 4724.9884 0.9995298
## alpha[48] 1.4111121 1.626055 5426.7487 1.0000891
## alpha[49] 1.8346723 2.041734 3857.6146 0.9999291
## alpha[50] 2.0005326 2.320473 4682.1829 0.9994486
## alpha[51] 1.9610879 2.242818 3897.1225 1.0009723
## alpha[52] 1.9576413 2.258114 4141.0925 0.9994889
## alpha[53] 1.7317194 2.022661 5224.5810 0.9999280
## alpha[54] 1.5447507 1.714274 4570.3665 0.9995635
## alpha[55] 1.5829758 1.825161 4586.4214 1.0007723
## alpha[56] 1.5399347 1.856795 4612.1006 0.9998590
## alpha[57] 1.2693900 1.502370 4419.8747 0.9999347
## alpha[58] 1.9807852 2.267009 4729.6049 0.9996622
## alpha[59] 1.8653786 2.170790 3564.0424 1.0004787
## alpha[60] 1.7597809 2.047750 4189.5653 0.9993510
## alpha[61] 1.2410987 1.394953 5211.9983 1.0004405
## alpha[62] 1.9446630 2.211543 4127.4304 1.0006116
## alpha[63] 1.8782238 2.140203 4410.0219 1.0015475
## alpha[64] 1.8741183 2.091342 4032.9233 1.0009510
## alpha[65] 1.9130042 2.211486 4658.0540 0.9995607
## alpha[66] 1.5959516 1.827310 3491.7897 1.0001894
## alpha[67] 1.5703683 1.811487 2720.2727 1.0005809
## alpha[68] 1.4577526 1.665119 5131.2782 0.9998648
## alpha[69] 1.2401830 1.507617 4385.6108 0.9993405
## alpha[70] 1.0231192 1.112810 2904.2734 1.0010123
## alpha[71] 1.6293048 1.801736 4406.3670 0.9993744
## alpha[72] 1.7538943 1.966655 4948.4134 1.0010566
## alpha[73] 1.9679177 2.277640 4311.3803 0.9995677
## alpha[74] 1.6319985 1.883531 4127.6571 0.9992034
## alpha[75] 1.6474410 1.933883 4624.7178 0.9993309
## alpha[76] 2.0423616 2.325008 4255.0137 0.9993103
## alpha[77] 1.8384127 2.098204 3679.8637 1.0005331
## alpha[78] 1.2379193 1.515212 4331.7442 0.9999613
## alpha[79] 1.5041763 1.759578 3656.2091 1.0002290
## alpha[80] 1.4413973 1.566072 4431.1116 0.9995841
## alpha[81] 2.0107603 2.333306 3469.8434 1.0000701
## alpha[82] 1.8821553 2.211199 4647.4193 0.9994501
## alpha[83] 1.8978045 2.130012 2698.7847 1.0006446
## alpha[84] 1.6695407 1.882372 4632.2349 0.9996432
## alpha[85] 1.7913879 2.081435 5197.1517 0.9994517
summary(fit_slope_partial, c("beta"))$summary
##                mean     se_mean        sd       2.5%        25%        50%
## beta[1]  -0.2974790 0.006669563 0.3051832 -0.9114797 -0.4823822 -0.3042058
## beta[2]  -0.4919876 0.014859180 0.2918734 -1.1362669 -0.6774336 -0.4702246
## beta[3]  -0.5821937 0.004657163 0.2641337 -1.1087596 -0.7375090 -0.5868099
## beta[4]  -0.4127450 0.004924461 0.2543936 -0.9414251 -0.5668956 -0.4071555
## beta[5]  -0.5409691 0.004966566 0.2759610 -1.0921209 -0.7038386 -0.5561706
## beta[6]  -0.8092573 0.005904078 0.3122332 -1.4235772 -0.9954144 -0.8170037
## beta[7]  -0.5269566 0.019674488 0.3115003 -1.0452370 -0.7477068 -0.5636189
## beta[8]  -0.7380857 0.005180854 0.2746566 -1.2715770 -0.9105647 -0.7476462
## beta[9]  -0.3335398 0.010641580 0.3002733 -0.8480973 -0.5295502 -0.3718247
## beta[10] -0.7597588 0.005190574 0.2456947 -1.2867627 -0.9034058 -0.7480565
## beta[11] -0.3840336 0.006217504 0.3383254 -1.0612100 -0.5753378 -0.3831756
## beta[12] -0.7675016 0.005928549 0.3139625 -1.4044398 -0.9477753 -0.7678653
## beta[13] -0.3191141 0.007250852 0.3516002 -1.0135276 -0.5215254 -0.3239035
## beta[14] -0.8409201 0.005855967 0.2420444 -1.3485550 -0.9875650 -0.8389608
## beta[15] -0.6119671 0.004176843 0.2626269 -1.1438942 -0.7663832 -0.6148170
## beta[16] -0.4107326 0.005920069 0.3257652 -1.0699720 -0.5967329 -0.4153249
## beta[17] -0.9410567 0.009016928 0.2706921 -1.5435567 -1.1056965 -0.9138354
## beta[18] -0.2751584 0.006045381 0.2622296 -0.7701347 -0.4492207 -0.2892631
## beta[19] -0.7247800 0.004468576 0.2232759 -1.1980130 -0.8590546 -0.7103188
## beta[20] -0.7626317 0.005879086 0.3242200 -1.4448083 -0.9482948 -0.7579696
## beta[21] -0.6759075 0.004786715 0.2823590 -1.2285693 -0.8449401 -0.6849748
## beta[22] -0.7750843 0.005541595 0.2843813 -1.3432240 -0.9404723 -0.7790272
## beta[23] -0.8142072 0.005298617 0.2891304 -1.3969223 -0.9931353 -0.8180062
## beta[24] -0.7228173 0.005455002 0.2691975 -1.2626551 -0.8822155 -0.7264248
## beta[25] -0.5070891 0.019217075 0.2942119 -0.9969706 -0.7168371 -0.5355082
## beta[26] -0.7045105 0.004030456 0.1748519 -1.0713128 -0.8173816 -0.6966455
## beta[27] -0.8116515 0.006433518 0.2836839 -1.3731826 -0.9875985 -0.8247931
## beta[28] -0.4505136 0.004346600 0.2561481 -0.9557997 -0.6130356 -0.4547818
## beta[29] -0.3152085 0.007116184 0.3502536 -1.0168845 -0.5225489 -0.3165321
## beta[30] -0.3511362 0.006591278 0.3340842 -1.0187120 -0.5519409 -0.3523791
## beta[31] -0.7916455 0.006012244 0.3089549 -1.4283505 -0.9660869 -0.7930705
## beta[32] -0.6191550 0.004940510 0.3124300 -1.2630302 -0.7893036 -0.6192258
## beta[33] -0.7065350 0.005771677 0.3055758 -1.3409635 -0.8768652 -0.7050421
## beta[34] -0.6707417 0.004426376 0.2686169 -1.2380734 -0.8260064 -0.6577397
## beta[35] -0.2321739 0.006647641 0.2803040 -0.7710013 -0.4118809 -0.2354750
## beta[36] -0.6214640 0.014744004 0.3258231 -1.1886354 -0.8330133 -0.6572018
## beta[37] -0.3179723 0.008106486 0.3174608 -0.9413850 -0.5174004 -0.3257776
## beta[38] -0.2677009 0.007420642 0.2965547 -0.8374281 -0.4545592 -0.2828802
## beta[39] -0.6657171 0.006736405 0.2876843 -1.2078809 -0.8455834 -0.6843648
## beta[40] -0.8337366 0.006845240 0.3051754 -1.4739350 -1.0161482 -0.8356741
## beta[41] -0.7033767 0.010176280 0.3056645 -1.2517611 -0.9016842 -0.7272266
## beta[42] -0.7004553 0.005163023 0.3129389 -1.3609449 -0.8723812 -0.7034417
## beta[43] -1.0486999 0.019425603 0.2930801 -1.7226452 -1.2265867 -1.0099322
## beta[44] -0.7967551 0.009133460 0.2869384 -1.4531815 -0.9554248 -0.7621935
## beta[45] -0.8298948 0.006007167 0.2437289 -1.3580680 -0.9754772 -0.8132214
## beta[46] -0.6413360 0.005033162 0.3079097 -1.2583958 -0.8129848 -0.6453882
## beta[47] -0.7192948 0.010948695 0.3031097 -1.3969719 -0.8936856 -0.6854597
## beta[48] -0.5269121 0.005807752 0.2733586 -1.0510629 -0.6910160 -0.5483336
## beta[49] -0.9186089 0.009901679 0.2820497 -1.5560927 -1.0799081 -0.8887112
## beta[50] -0.8261902 0.005918529 0.3235579 -1.4868224 -1.0059474 -0.8288264
## beta[51] -0.7734154 0.006427791 0.3142404 -1.4254767 -0.9517577 -0.7745124
## beta[52] -0.8209085 0.005847200 0.3182350 -1.4510734 -1.0073239 -0.8277720
## beta[53] -0.7869805 0.004460211 0.2799477 -1.3756368 -0.9507313 -0.7806912
## beta[54] -0.9049648 0.009400536 0.2578442 -1.4737655 -1.0598150 -0.8798096
## beta[55] -0.4824666 0.005071871 0.2512172 -0.9737859 -0.6409538 -0.4965609
## beta[56] -0.7389678 0.007524774 0.2849825 -1.3662454 -0.9024193 -0.7151073
## beta[57] -0.5937024 0.004859748 0.2876798 -1.1849877 -0.7589764 -0.5910800
## beta[58] -0.7826653 0.008381032 0.3013156 -1.3575573 -0.9729122 -0.8000502
## beta[59] -0.8339167 0.005979799 0.2757148 -1.4325721 -0.9940466 -0.8121842
## beta[60] -0.7577541 0.005717738 0.3220528 -1.3889788 -0.9441428 -0.7652176
## beta[61] -0.2780393 0.011909665 0.2705272 -0.7389345 -0.4608578 -0.3103389
## beta[62] -0.5293128 0.024043044 0.3574532 -1.0796268 -0.7758505 -0.5914331
## beta[63] -0.6912735 0.010228800 0.3097158 -1.2513980 -0.8911622 -0.7195921
## beta[64] -0.7479466 0.005767696 0.2800597 -1.3226881 -0.9122466 -0.7441430
## beta[65] -0.8655129 0.006480115 0.3257633 -1.5313357 -1.0559127 -0.8696401
## beta[66] -0.5006911 0.004061259 0.2167056 -0.9152148 -0.6348328 -0.5043784
## beta[67] -0.2196905 0.015487686 0.2801390 -0.7128097 -0.4123166 -0.2514030
## beta[68] -0.6532340 0.005262819 0.3041707 -1.2733745 -0.8217675 -0.6657519
## beta[69] -0.3467039 0.006738048 0.3382406 -1.0484967 -0.5437137 -0.3424991
## beta[70] -0.6376136 0.005691249 0.1784821 -0.9711100 -0.7591190 -0.6453012
## beta[71] -0.7767956 0.004766695 0.2249172 -1.2466372 -0.9150638 -0.7668297
## beta[72] -0.7531244 0.005899009 0.3146015 -1.3782140 -0.9345486 -0.7616713
## beta[73] -0.8497306 0.006773812 0.3347097 -1.5206233 -1.0459243 -0.8585821
## beta[74] -0.7609234 0.005584744 0.3195240 -1.4177136 -0.9446863 -0.7714594
## beta[75] -0.4958398 0.008868779 0.2929014 -1.0172349 -0.6857591 -0.5287377
## beta[76] -0.8660138 0.006680451 0.3051946 -1.4594310 -1.0557144 -0.8738191
## beta[77] -0.8298603 0.008187787 0.3029109 -1.5013166 -0.9951016 -0.8025581
## beta[78] -0.4150848 0.006667414 0.2917578 -1.0467899 -0.5881587 -0.3960531
## beta[79] -0.8024247 0.005547236 0.2929321 -1.4291666 -0.9751783 -0.7937564
## beta[80] -0.7368338 0.008390437 0.2229473 -1.2166450 -0.8775044 -0.7163868
## beta[81] -0.5344239 0.017266635 0.2979241 -1.0456433 -0.7366513 -0.5655028
## beta[82] -0.7538237 0.005374370 0.3151661 -1.3978565 -0.9353623 -0.7521702
## beta[83] -1.1386753 0.017200878 0.3012679 -1.8113693 -1.3259842 -1.1027073
## beta[84] -0.6223675 0.004899650 0.2730415 -1.2030984 -0.7791503 -0.6163391
## beta[85] -0.7934573 0.005468683 0.3279542 -1.4641024 -0.9776916 -0.7981338
##                  75%        97.5%     n_eff      Rhat
## beta[1]  -0.12416273  0.366509773 2093.7571 1.0003587
## beta[2]  -0.29051388  0.022505573  385.8329 1.0108070
## beta[3]  -0.43260874 -0.020352894 3216.6567 1.0013758
## beta[4]  -0.25335577  0.090366025 2668.6706 0.9991645
## beta[5]  -0.38427795  0.064714470 3087.3293 1.0020571
## beta[6]  -0.62892857 -0.144228530 2796.7519 1.0010941
## beta[7]  -0.33874397  0.169485408  250.6745 1.0172413
## beta[8]  -0.57775164 -0.144729264 2810.4598 1.0019244
## beta[9]  -0.17239132  0.344251024  796.1980 1.0087784
## beta[10] -0.60530162 -0.287291707 2240.5823 0.9998031
## beta[11] -0.19218299  0.316831875 2960.9898 1.0004837
## beta[12] -0.58978112 -0.091200731 2804.5200 1.0004132
## beta[13] -0.10991678  0.415459826 2351.3647 0.9992597
## beta[14] -0.68246432 -0.382531862 1708.4134 0.9996131
## beta[15] -0.46043630 -0.080030396 3953.5025 1.0015143
## beta[16] -0.22853228  0.291920431 3027.9995 0.9999532
## beta[17] -0.75847015 -0.467580459  901.2264 1.0040108
## beta[18] -0.11425407  0.288012680 1881.5518 1.0021723
## beta[19] -0.57750828 -0.309990439 2496.5789 1.0014261
## beta[20] -0.58376805 -0.080131569 3041.3057 0.9999879
## beta[21] -0.51244985 -0.082844031 3479.5894 1.0005296
## beta[22] -0.60986371 -0.187635964 2633.4946 0.9995684
## beta[23] -0.63892252 -0.202838511 2977.5727 0.9995882
## beta[24] -0.56514210 -0.167483638 2435.2979 1.0003331
## beta[25] -0.33512315  0.161991105  234.3937 1.0193225
## beta[26] -0.58687924 -0.373586793 1882.0552 1.0023225
## beta[27] -0.64108999 -0.215432943 1944.3405 1.0034870
## beta[28] -0.30113888  0.087666444 3472.8239 0.9998967
## beta[29] -0.10751137  0.388748263 2422.5399 0.9991031
## beta[30] -0.15330642  0.360166834 2569.0498 0.9993153
## beta[31] -0.61412950 -0.151089847 2640.6871 1.0002358
## beta[32] -0.45383633  0.059812191 3999.0947 1.0004580
## beta[33] -0.52895783 -0.079703289 2803.0692 1.0009959
## beta[34] -0.50965831 -0.140316873 3682.7316 0.9996424
## beta[35] -0.05738729  0.332302245 1777.9654 1.0000562
## beta[36] -0.43741969  0.098696794  488.3518 1.0096689
## beta[37] -0.12924451  0.336515959 1533.6098 1.0015731
## beta[38] -0.09435263  0.372176751 1597.0800 1.0028942
## beta[39] -0.50029169 -0.043101803 1823.7952 1.0029463
## beta[40] -0.65114097 -0.212502846 1987.5664 1.0010783
## beta[41] -0.52444901 -0.030079734  902.2191 1.0054809
## beta[42] -0.52618488 -0.059490482 3673.7605 0.9998714
## beta[43] -0.83032602 -0.586962997  227.6269 1.0182032
## beta[44] -0.61501979 -0.276016856  986.9765 1.0056226
## beta[45] -0.67141313 -0.382862940 1646.1691 1.0023844
## beta[46] -0.47924799  0.006898942 3742.5271 1.0005422
## beta[47] -0.51613844 -0.186537502  766.4342 1.0044711
## beta[48] -0.37011502  0.062398320 2215.3856 1.0023510
## beta[49] -0.72752803 -0.430639609  811.3976 1.0047367
## beta[50] -0.63972777 -0.157826840 2988.6603 1.0012337
## beta[51] -0.58962816 -0.118730798 2390.0141 0.9999034
## beta[52] -0.63222145 -0.182128804 2962.1020 0.9999680
## beta[53] -0.61986139 -0.216618782 3939.5166 0.9997041
## beta[54] -0.73271264 -0.431010034  752.3319 1.0073202
## beta[55] -0.33079557  0.042785776 2453.3656 1.0024730
## beta[56] -0.55884880 -0.213183059 1434.3314 1.0038319
## beta[57] -0.42329282 -0.015290264 3504.2196 1.0005580
## beta[58] -0.60537138 -0.121589099 1292.5521 1.0040704
## beta[59] -0.65917149 -0.305775234 2125.9203 1.0008889
## beta[60] -0.58034735 -0.051518209 3172.5282 1.0006966
## beta[61] -0.11644069  0.309688218  515.9682 1.0104184
## beta[62] -0.33126443  0.311857697  221.0342 1.0178437
## beta[63] -0.51379856  0.013842999  916.8060 1.0054316
## beta[64] -0.58071956 -0.188364101 2357.7429 0.9999332
## beta[65] -0.67645128 -0.185330847 2527.1967 0.9998931
## beta[66] -0.37042826 -0.049050974 2847.2059 1.0008073
## beta[67] -0.04805829  0.405956802  327.1703 1.0140715
## beta[68] -0.48970711 -0.007921149 3340.3968 1.0020060
## beta[69] -0.14703780  0.330977342 2519.9001 0.9992956
## beta[70] -0.51802702 -0.271576297  983.4990 1.0050218
## beta[71] -0.63181433 -0.344115367 2226.4390 1.0004591
## beta[72] -0.57323752 -0.086665650 2844.2218 1.0006489
## beta[73] -0.66648386 -0.140448326 2441.5760 0.9995838
## beta[74] -0.58792422 -0.056239893 3273.4117 1.0016168
## beta[75] -0.33494489  0.154121555 1090.7246 1.0042964
## beta[76] -0.68424718 -0.238993080 2087.0949 1.0001565
## beta[77] -0.64490214 -0.263439044 1368.6642 1.0015698
## beta[78] -0.22886102  0.133809027 1914.8292 1.0018489
## beta[79] -0.62015364 -0.231891476 2788.5645 1.0008997
## beta[80] -0.58714254 -0.328976843  706.0498 1.0052644
## beta[81] -0.36218219  0.135688804  297.7116 1.0154500
## beta[82] -0.57666986 -0.107162846 3438.9346 1.0002590
## beta[83] -0.92457027 -0.645182455  306.7641 1.0102974
## beta[84] -0.46057513 -0.095617821 3105.4678 1.0010878
## beta[85] -0.62105724 -0.084668937 3596.3404 0.9995785
summary(fit_slope_partial, c("sigma"))$summary
##            mean     se_mean        sd      2.5%       25%       50%       75%
## sigma 0.7168493 0.000396972 0.0175195 0.6836641 0.7051074 0.7166247 0.7282287
##           97.5%    n_eff     Rhat
## sigma 0.7536974 1947.707 1.001728
summary(fit_slope_partial, c("gamma_a", "gamma_b"))$summary
##                  mean     se_mean         sd       2.5%        25%        50%
## gamma_a[1]  1.4924838 0.001117302 0.04389493  1.4083780  1.4628053  1.4918695
## gamma_a[2]  0.6869849 0.002739377 0.11496147  0.4655327  0.6107307  0.6867139
## gamma_b[1] -0.6429961 0.002709503 0.08323914 -0.8062148 -0.6984874 -0.6442659
## gamma_b[2] -0.4417057 0.007893296 0.22437006 -0.8888901 -0.5923388 -0.4397335
##                   75%        97.5%     n_eff     Rhat
## gamma_a[1]  1.5209251  1.579982518 1543.4308 1.004581
## gamma_a[2]  0.7633894  0.913936387 1761.1687 1.000393
## gamma_b[1] -0.5898831 -0.478284204  943.7910 1.005254
## gamma_b[2] -0.2877130 -0.004933068  808.0031 1.000573
rstan::traceplot(fit_slope_partial, pars = c("sigma"))