rstan

How to avoid "Dropping 'draws_df' class as required metadata was removed" warning?


When doing certain operations on a draws_df data frame the class gets dropped leading to a warning; see example below. How can that warning be avoided? (What I have in mind is either a function/parameter in posterior, or perhaps a better way of doing what my example does without using pivot_longer.)

library(tidyverse)
library(posterior)

example_draws() |> 
  as_draws_df() |> 
  pivot_longer(!.draw) |> 
  filter(str_starts(name, 'theta')) |> 
  mutate(cumulative = cumsum(value), .by = .draw)
# Warning message:
# Dropping 'draws_df' class as required metadata was removed.

Solution

  • The warning can be avoided by converting the draws in "df" format into a tibble. For example, the code below issues a warning, but if the indicated line is uncommented then it goes away.

    library(tidyverse)
    library(posterior)
    
    example_draws() |> 
      as_draws_df() |> 
      # as_tibble() |> # uncomment this line to avoid warning
      pivot_longer(!starts_with('.'))