Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors retrieving models when stan_glm is run inside a function #311

Open
richardbeare opened this issue Oct 27, 2023 · 0 comments
Open

Comments

@richardbeare
Copy link

Hi,
We're having issues plotting model statistics when investigating a set of models. The problems are scattered between see and bayestestR.

Here's an example of the problem. In our case we want the 3 diagnostic plots returned from a single function. I've separated them for illustration purposes.

Simple first pass version:

library(tidyverse)
library(rstanarm)
library(bayestestR)
library(palmerpenguins)

data(penguins)
bayesStuffA <- function(df, thevar) {
  f <- as.formula(paste(thevar, "~ species + sex + island"))
  m1 <- stan_glm(f, data=df, refresh=0)
  PD <- p_direction(m1)
  pl1 <- plot(PD)
  return(list(plot_direction = pl1))
}

bayesStuffB <- function(df, thevar) {
  f <- as.formula(paste(thevar, "~ species + sex + island"))
  m1 <- stan_glm(f, data=df, refresh=0)
  P <- describe_posterior(m1)
  pl1 <- plot(P)
  return(list(plot_post = pl1))
}


bayesStuffC <- function(df, thevar) {
  f <- as.formula(paste(thevar, "~ species + sex + island"))
  m1 <- stan_glm(f, data=df, refresh=0)
  PR <- rope(m1)
  pl1 <- plot(PR)
  return(list(plot_rope = pl1))
}

Errors as follows:

g1 <- bayesStuffA(penguins, "bill_length_mm")
Error: Failed at retrieving data :( Please provide original model or data through the `data` argument

g2 <- bayesStuffB(penguins, "bill_length_mm")
Warning message:
Could not find model-object. Try ' plot(estimate_density(model))' instead. 

g3 <- bayesStuffC(penguins, "bill_length_mm")
Error: Failed at retrieving data :( Please provide original model or data through the `data` argument

Now, following the advice of the error messages:

bayesStuffA <- function(df, thevar) {
  f <- as.formula(paste(thevar, "~ species + sex + island"))
  m1 <- stan_glm(f, data=df, refresh=0)
  PD <- p_direction(m1)
  pl1 <- plot(PD, data=m1)
  return(list(plot_direction = pl1))
}

g1 <- bayesStuffA(penguins, "bill_length_mm")
Error: Failed at retrieving data :( Please provide original model or data through the `data` argument

This error is attributable to here, as there's no check of the data argument.

I'll stop with the code, but you get the idea. The rope code in bayesStuffC can be fixed by passing a model argument to plot. bayestestR::plot.describe_posterior, used in bayesStuffB, doesn't have a data argument but needs something equivalent.

Also note that the current setup is potentially dangerous - it is easy to accidentally leave a model in the global environment with the same name as one created in the function and the plot functions will silently use that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant