matherealize / simdata

An R package for simulating data
https://matherealize.github.io/simdata/
7 stars 1 forks source link

check_and_infer #6

Closed AngelikaGeroldinger closed 3 years ago

AngelikaGeroldinger commented 3 years ago

In the current version, the check_and_infer option in simdesign was not so helpful for me, because it does not forward the error message from the simulate_data function. Switching check_and_infer off and putting the (erroneous) design into the simulate_data function immediately showed the problem. For me it would have been helpful, if simdesign forwards the errors obtained with the checking of the design.

matherealize commented 3 years ago

Did you have a specific example in mind?

I have tried to reproduce what you describe, is any of the three scenarios below relevant to your case? The current version (commit b52763868402cab9170c32cff85fdf463dd99147) now returns a more informative error message and the design object itself for further inspection, even if check_and_infer is set to TRUE. See the output in the comments of the snippet below.

null_simdesign <- function(...) {
    dsgn = simdesign(
        generator = function(x) NULL,
        ...
    )
    class(dsgn) = c("null_simdesign", class(dsgn))
    dsgn
}
sim_design = null_simdesign(check_and_infer=TRUE)
# Warning message:
# In simdesign(generator = function(x) NULL, ...) :
#     Simulation from design returned NULL. Please double check arguments. Returning the potentially faulty design object.

faulty_simdesign <- function(...) {
    dsgn = simdesign(
        generator = function(x) x / 0,
        ...
    )
    class(dsgn) = c("faulty_simdesign", class(dsgn))
    dsgn
}
sim_design = faulty_simdesign(check_and_infer=TRUE)
# Warning message:
#     In simdesign(generator = function(x) x/0, ...) :
#     Simulation from design returned unexpected number of observations (1 instead of 5). Please double check arguments. Returning the potentially faulty design object.

error_simdesign <- function(...) {
    dsgn = simdesign(
        generator = function(x) "" + 1,
        ...
    )
    class(dsgn) = c("error_simdesign", class(dsgn))
    dsgn
}
sim_design = error_simdesign(check_and_infer=TRUE)
# <simpleError in "" + 1: non-numeric argument to binary operator>
#     Warning message:
#     In simdesign(generator = function(x) "" + 1, ...) :
#     Unable to simulate from design. Please double check arguments. Returning the potentially faulty design object.
AngelikaGeroldinger commented 3 years ago

The modification looks very helpful! Unfortunately I cannot remember the circumstances, where I had this issue. I looked into my old codes, but could not reproduce it. I should have included an example when I wrote the issue. Thanks for taking it up!

matherealize commented 3 years ago

Then I'll be closing the issue for now. If you come across a similar situation again which is not solved with the current approach, feel free to re-open.