Skip to content

Cryptic error in data.R if NULL passed as a variable #1126

@jpkrooney

Description

@jpkrooney

Describe the bug
If the data provided as a list includes a variable of type NULL, this won't be detected and the cryptic error as follows is generated:
Error in round(x) : non-numeric argument to mathematical function
(I had a bug in my own data prep code that meant NULL was being passed as a variable in the list without my awareness and because this error was so vague it took some time to track down the problem)

To Reproduce

library(cmdstanr)

file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan")
mod <- cmdstan_model(file)

# data including a NULL variable
stan_data <- list(N = NULL, y = c(0,1,0,0,0,0,0,0,0,1))

# Run MCMC using the 'sample' method
fit_mcmc <- mod$sample(data = stan_data)

Expected behavior
It would be preferable if an error reported "Variable 'N' was found to be NULL - numeric value expected.' - or something along those lines.

Operating system
Mac OsX 26.2

CmdStanR version number
‘0.9.0.9000’
CmdStan version 2.37

Additional context
On some occasions (though I could not make a reprex for this), the error also included the text:

Error in round(x) : non-numeric argument to mathematical function
Called from: is_wholenumber(data[[var_name]])

I searched for is_wholenumber in CmdStanR source code and found it only once in the process_data function at line 190 in the data.R file - so I believe this line is throwing the error at least some of the time if not all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions