Skip to content

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

@richardbeare

Description

@richardbeare

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions