stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.04k stars 265 forks source link

stan_ac issue when ggplot2 not loaded #1049

Open jfrench opened 1 year ago

jfrench commented 1 year ago

Summary:

The stan_ac function doesn't work properly if the ggplot2 package isn't loaded.

Reproducible Steps:

Reproducible example:

library(rstan)
scode <- "
parameters {
  real y[2]; 
} 
model {
  y[1] ~ normal(0, 1);
  y[2] ~ double_exponential(0, 2);
} 
"
fit1 <- stan(model_code = scode, iter = 100, verbose = FALSE) 
stan_ac(fit1)

Current Output:

> stan_ac(fit1)
Warning messages:
1: Computation failed in `stat_summary()`
Caused by error in `get()`:
! object 'mean_se' of mode 'function' was not found 
2: Computation failed in `stat_summary()`
Caused by error in `get()`:
! object 'mean_se' of mode 'function' was not found 

Plot is produced but empty.

Running the following code produces the expected result

library(ggplot2)
stan_ac(fit1)

Changing line 20 (?) of the function's code to dots$fun.data <- ggplot2::mean_se seems to correct the issue.

RStan Version:

The version of RStan you are running (e.g., from packageVersion("rstan"))

2.26.15

R Version:

4.2.2 (2022-10-31 ucrt)

Operating System:

Windows 10 x64 (build 19044)

jgabry commented 1 year ago

Thanks for reporting. I'll take a look. I recommend also checking out the mcmc_acf and mcmc_acf_bar functions in our bayesplot package. Those are more recent versions of similar plots. (We may deprecate the plotting functions in rstan in the future and rely on bayesplot instead.)