stan-dev / rstan

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

sampling() fails if an unrecognized option is provided #681

Open mcol opened 5 years ago

mcol commented 5 years ago

Summary:

rstan::sampling doesn't seem to reject or ignore invalid options passed through the ... argument.

Description:

If an invalid option is given, it is not ignored but it causes an ugly failure:

> samples <- sampling(model, data=data.input,
                      chains=4, iter=1000, warmup=500, zzz=TRUE)

Error in FUN(X[[i]], ...) :
  trying to get slot "mode" from an object (class "try-error") that is not an S4 object
In addition: Warning message:
In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE,  :
  4 function calls resulted in an error

This came up because I have a function that takes a ... argument that gets passed to rstan::sampling, and I had a typo in one of my arguments (which didn't match and got passed to rstan).

Reproducible Steps:

This is enough to see the failure:

model <- stan_model(model_code="
parameters {
  real y;
}
model {
  y ~ normal(0,1);
}
")
samples <- sampling(model, zzz=TRUE)

Expected Output:

Unrecognized options should be ignored and sampling should proceed correctly. It seems that there's code in the optimizing method that checks recognizable names passed through the ... argument, but no such code exists in sampling.

> samples <- optimizing(model, zzz=TRUE)
Error: passing unknown arguments: zzz.

RStan Version:

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

R Version:

The version of R you are running (e.g., from R.version.string): 3.6.1

Operating System:

Your operating system (e.g., OS X 10.11.3): Ubuntu 18.04.3 LTS

jgabry commented 5 years ago

Yeah, I think it would be good to change that. I don’t think we should error out if it doesn’t recognize an argument. A warning seems sufficient.

JackCaster commented 2 years ago

I stumbled upon the same error. I passed the wrong argument to brms (priors instead of prior) and received the error:

Start sampling
Error in FUN(X[[i]], ...) : 
  trying to get slot "mode" from an object (class "try-error") that is not an S4 object 
In addition: Warning message:
In parallel::mclapply(1:chains, FUN = callFun, mc.preschedule = FALSE,  :
  4 function calls resulted in an error

Unfortunately, this error is not informative. Actually, it suggests a more serious problem related to sampling.

bob-carpenter commented 2 years ago

Thanks, @JackCaster---This seems like something that brms should catch. Pinging @paul-buerkner in case there isn't already a brms issue to catch errors due to unknown arguments.

mcol commented 2 years ago

Ideally rstan would sort it out, otherwise all interfaces will have to work around it in their code as I had to do here: https://github.com/mcol/hsstan/blob/167cc4e7585b4f3628ac1fdbac0650e75869f2fe/R/misc.R#L387-L408

bob-carpenter commented 2 years ago

Usually, interfaces take responsibility or checking their own arguments in order to provide interface-centric error messages and avoid user confusion from embedded messages from calls that should be cleanly encapsulated. For example, rather than waiting for the C++ compiler to fail, the Stan transpolar provides error messages in terms of the Stan code.