ASKurz / Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed

The bookdown version lives here:
https://bookdown.org/content/4857/
Creative Commons Zero v1.0 Universal
125 stars 38 forks source link

4.4.3.1 posterior_samples deprecation #36

Closed gserapio closed 2 years ago

gserapio commented 3 years ago

First, thanks for sharing this incredible resource! It's exactly what I've been looking for and I'm certain it will benefit many more people to come.

The following code is provided at 4.4.3.1 to display the variance/covariance matrix with sigma included (I'll try to provide a quick repex):

library(rethinking)
library(brms)
library(tidyverse)

data("Howell1"); d <- Howell1; d2 <- d[d$age >= 18,]

d2 <-
  d2 %>% 
  mutate(weight_c = weight - mean(weight))

b4.3 <- 
  brm(data = d2, 
      family = gaussian,
      height ~ 1 + weight_c,
      prior = c(prior(normal(178, 20), class = Intercept),
                prior(lognormal(0, 1), class = b),
                prior(uniform(0, 50), class = sigma)),
      iter = 28000, warmup = 27000, chains = 4, cores = 4,
      seed = 4,
      file = "fits/b04.03")

posterior_samples(b4.3) %>%
  select(-lp__) %>%
  cov() %>%
  round(digits = 3)
#> Warning: Method 'posterior_samples' is deprecated. Please see ?as_draws for
#> recommended alternatives.
#>             b_Intercept b_weight_c sigma
#> b_Intercept       0.076      0.000 0.000
#> b_weight_c        0.000      0.002 0.000
#> sigma             0.000      0.000 0.036

Created on 2021-09-07 by the reprex package (v2.0.1)

Given this warning, would it help to update with code to use as_draws()? I was thinking something like this:

as_draws_df(b4.3) %>%
  select(-lp__) %>%
  cov() %>%
  round(digits = 3)

But it would be nice to more cleanly drop the hidden reserved variables (chain, iteration, draw):

as_draws_df(b4.3) %>%
  select(b_Intercept, b_weight_c, sigma) %>%
  cov() %>%
  round(digits = 3)

Though this ends with the warning: Warning message: Dropping 'draws_df' class as required metadata was removed.

ASKurz commented 3 years ago

Hey @gserapio. Yep, I updated brms last week and I noticed the depreciation notice, too. It looks like Paul's switch to a more posterior-oriented workflow is going to result in a lot of code updates for my ebooks. Since nothing appears to have broken, yet, these changes are lower on my priority list. They'll probably make it into the next version update, but that update won't likely be out anytime soon.

gserapio commented 3 years ago

Hey @gserapio. Yep, I updated brms last week and I noticed the depreciation notice, too. It looks like Paul's switch to a more posterior-oriented workflow is going to result in a lot of code updates for my ebooks. Since nothing appears to have broken, yet, these changes are lower on my priority list. They'll probably make it into the next version update, but that update won't likely be out anytime soon.

Sounds good. Thanks again for your work!