mjskay / ggdist

Visualizations of distributions and uncertainty

Home Page:https://mjskay.github.io/ggdist/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error with rvar and curve_interval

mjskay opened this issue · comments

Issue with rvar and curve_interval to investigate...


Originally posted by @mattansb in vincentarelbundock/marginaleffects#539 (comment):

    This should also work, I think, but I'm getting an error? @mjskay, do you know what I'm doing wrong?
mod <- brm(Species ~ ., 
           data = iris,
           family = categorical(), 
           backend = "cmdstanr", cores = 4)


predictions(mod, newdata = datagrid(Sepal.length = 4:5)) |>
  posteriordraws("rvar") |> 
  curve_interval(rvar, .along = c("Sepal.length", "group"))
#> Error in apply(draws, 2, halfspace_depth) : 
#>   dim(X) must have a positive length
#> In addition: Warning message:
#> Some of the variable names are missing from the model data: Sepal.length

This should be fixed now --- let me know if there are any other issues you run into with curve_interval / rvar. Thanks!

Thanks!

library(brms)
library(marginaleffects)
library(ggdist)
library(ggplot2)

mod <- brm(Species ~ ., 
  data = iris,
  family = categorical(), 
  backend = "cmdstanr", cores = 4, iter = 300
)

predictions(mod, newdata = datagrid(Sepal.Width = seq(2, 4.5, len = 50))) |> 
  posteriordraws(shape = "rvar") |> 
  curve_interval(rvar, .along = c("Sepal.Width", "group")) |> 
  ggplot(aes(Sepal.Width, estimate, color = group)) + 
  facet_grid(~group) + 
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high, fill = group), 
              color = NA, alpha = 0.4) + 
  geom_line()

image

looks right (ish? bit of a weird model...) though to actually use the curve_interval output you'd want rvar, .lower, and .upper instead of estimate, conf.low, and conf.high in the ggplot spec

Ah right.

(Yes, the model is crap - just playing around with functions 🤓)