Unable to exclude warmup iterations
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 1.1k
- Forks
- 266
- Avg merge
- 2h 56m
- Merged PRs (30d)
- 1
Description
Summary:
Fitting a model I specified save_warmup = FALSE but on loading the stanfit CSV files they appear to still be there. Also inc_warmup = FALSE doesn't appear to make a difference in a as.data.frame.stanfit() or extract() call.
Description:
I'm using rstan version 2.18.2. I am not certain but I'm pretty sure that I didn't have this issue in the last version I was using (2.17.*).
I fit a model using the stan() function with 300 warmup iterations and 200 sampling iterations, and included the setting save_warmup = FALSE.
model_fit <- stan(
file.path("stan_models", "general_nls_model.stan"),
data = stan_data,
chains = 8,
iter = 500,
warmup = 300,
control = lst(max_treedepth = as.integer(script_options$`max-treedepth` %||% 10),
adapt_delta = as.numeric(script_options$`adapt-delta` %||% 0.8)),
include = TRUE,
pars = param_to_save,
sample_file = if (script_options$`debug-output` %||% TRUE) NULL else file.path("stanfit", str_c(output_name, ".csv")),
init = init_list,
save_warmup = FALSE)
When I load the csv files (I have 8) using read_stan_csv() I get the following warning message:
Warning message:
In read_stan_csv(.) :
the number of iterations after warmup found (500) does not match iter/warmup/thin from CSV comments (200)
Taking a look at the stanfit CSV I'm seeing the following header:
# Sample generated by Stan
# stan_version_major=2
# stan_version_minor=18
# stan_version_patch=1
# init=user
# enable_random_init=1
# seed=2062663890
# chain_id=1
# iter=500
# warmup=300
# save_warmup=0
# thin=1
# refresh=50
# stepsize=1
# stepsize_jitter=0
# adapt_engaged=1
# adapt_gamma=0.05
# adapt_delta=0.8
# adapt_kappa=0.75
# adapt_t0=10
# max_treedepth=10
# sampler_t=NUTS(diag_e)
# sample_file=stanfit/nls_model_1_1.csv
# append_samples=0
Reproducible Steps:
Here's a simpler repro of the bug:
library(rstan)
fit <- stan(model_code = "parameters { real x; } model { x ~ normal(0, 1); }", iter = 500, warmup = 300, save_warmup = FALSE, chains = 1, sample_file = "test.csv")
dim(as.data.frame(fit)) # Should only have 200 iterations
fit2 <- read_stan_csv("test_1.csv")
dim(as.data.frame(fit2)) # Should only have 200 iterations
dim(as.data.frame(fit2, inc_warmup = FALSE)) # Should only have 200 iterations
str(extract(fit2, inc_warmup = FALSE)) # Should only have 200 iterations
Current Output:
> library(rstan)
Loading required package: ggplot2
Registered S3 methods overwritten by 'ggplot2':
method from
[.quosures rlang
c.quosures rlang
print.quosures rlang
Loading required package: StanHeaders
rstan (Version 2.18.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
> fit <- stan(model_code = "parameters { real x; } model { x ~ normal(0, 1); }", iter = 500, warmup = 300, save_warmup = FALSE, chains = 1, sample_file = "test.csv")
SAMPLING FOR MODEL 'd4901852a59d5abb2b87f654d5e8d0a7' NOW (CHAIN 1).
Chain 1:
Chain 1: Gradient evaluation took 8e-06 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.08 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1:
Chain 1:
Chain 1: Iteration: 1 / 500 [ 0%] (Warmup)
Chain 1: Iteration: 50 / 500 [ 10%] (Warmup)
Chain 1: Iteration: 100 / 500 [ 20%] (Warmup)
Chain 1: Iteration: 150 / 500 [ 30%] (Warmup)
Chain 1: Iteration: 200 / 500 [ 40%] (Warmup)
Chain 1: Iteration: 250 / 500 [ 50%] (Warmup)
Chain 1: Iteration: 300 / 500 [ 60%] (Warmup)
Chain 1: Iteration: 301 / 500 [ 60%] (Sampling)
Chain 1: Iteration: 350 / 500 [ 70%] (Sampling)
Chain 1: Iteration: 400 / 500 [ 80%] (Sampling)
Chain 1: Iteration: 450 / 500 [ 90%] (Sampling)
Chain 1: Iteration: 500 / 500 [100%] (Sampling)
Chain 1:
Chain 1: Elapsed Time: 0.002715 seconds (Warm-up)
Chain 1: 0.002436 seconds (Sampling)
Chain 1: 0.005151 seconds (Total)
Chain 1:
So far the number of iterations is correct
> dim(as.data.frame(fit))
[1] 200 2
But when loading from the CSV file, I get a warning message and the number of iterations is wrong. Aslo, inc_warmup = FALSE doesn't seem to work for as.data.frame.stanfit().
> fit2 <- read_stan_csv("test_1.csv")
Warning message:
In read_stan_csv("test_1.csv") :
the number of iterations after warmup found (500) does not match iter/warmup/thin from CSV comments (200)
> dim(as.data.frame(fit2))
[1] 500 2
> dim(as.data.frame(fit2, inc_warmup = FALSE))
Error in .local(object, ...) :
formal argument "inc_warmup" matched by multiple actual arguments
> str(extract(fit2, inc_warmup = FALSE))
List of 2
$ x : num [1:500(1d)] -1.22 0 0 0.529 1.482 ...
..- attr(*, "dimnames")=List of 1
.. ..$ iterations: NULL
$ lp__: num [1:500(1d)] -0.745 0 0 -0.14 -1.098 ...
..- attr(*, "dimnames")=List of 1
.. ..$ iterations: NULL
Expected Output:
- No warning message
- Same number of iterations (200) after loading
stanfitobject from CSV file inc_warmupoption should work foras.data.frame.stanfit().
RStan Version:
2.18.2
R Version:
R version 3.6.0 (2019-04-26)
Operating System:
Distributor ID: Ubuntu
Description: Pop!_OS 19.04
Release: 19.04
Codename: disco
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the read_stan_csv(), as.data.frame.stanfit(), and extract() entry points using the minimal reproducible example in the issue. Compare the in-memory stan() result with the CSV-loaded result and verify that warmup rows are excluded without warnings or duplicate inc_warmup arguments; the expected result is 200 iterations in each case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100