Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract input checking and transforming from functions used in loops #278

Open
Bisaloo opened this issue Apr 22, 2024 · 0 comments
Open

Comments

@Bisaloo
Copy link
Member

Bisaloo commented Apr 22, 2024

Internal functions used in loops (via optim() ) include input checking which will have to run in every single iteration of the loop:

dist <- match.arg(dist)
cumulative <- switch(dist,
lnorm = stats::plnorm,
gamma = stats::pgamma,
weibull = stats::pweibull,
norm = stats::pnorm
)
density <- switch(dist,
lnorm = stats::dlnorm,
gamma = stats::dgamma,
weibull = stats::dweibull,
norm = stats::dnorm
)
param_names <- switch(dist,
lnorm = c("meanlog", "sdlog"),
gamma = c("shape", "scale"),
weibull = c("shape", "scale"),
norm = c("mean", "sd")
)

dist <- match.arg(dist)
cumulative <- switch(dist,
lnorm = stats::plnorm,
gamma = stats::pgamma,
weibull = stats::pweibull,
norm = stats::pnorm
)
param_names <- switch(dist,
lnorm = c("meanlog", "sdlog"),
gamma = c("shape", "scale"),
weibull = c("shape", "scale"),
norm = c("mean", "sd")

From a performance point of view, it would be much better to do this once before the loop is initiated. Since they are internal functions, we don't have to worry about checking their inputs.

It would also potentially reduce duplication across these two functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant