JonasMoss / univariateML

An R package for maximum likelihood estimation of univariate densities.
https://jonasmoss.github.io/univariateML/
Other
8 stars 6 forks source link

Better automated tests. #29

Closed JonasMoss closed 1 week ago

JonasMoss commented 5 years ago

Currently, most of the automated tests for the ml*** functions are copy-pasta. This has two downsides: 1.) It is hard to verify if each test is complete. 2.) I we want to implement more tests or change the object structure, we would have to modify 22 tests.

One test file that avoids this problem is test_input_checks.R. This goes through every ml*** file with just a couple of lines.

Most of the tests are straight-forward such as checking whether the input checks work, if the objects have all the needed attributes, etc. I propose the following pattern:

fun = function(x) eval(call("attr", match.call()[[1]], "type"))
attr(fun, "type") = "continuous"

Then

fun()
# > [1] "continuous"

These function attributes can then be used to do both testing and populating attributes of the univariateML objects. Take mlcauchy and consider

attr(mlcauchy, "model") = "Cauchy"
attr(mlcauchy, "name") = "mlcauchy"
attr(mlcauchy, "density") = "stats::dcauchy"
attr(mlcauchy, "parameters") = c("location", "scale")
attr(mlcauchy, "test_call") = "stats::dcauchy(n, 0, 1)"
attr(mlcauchy, "support") = c(-Inf, Inf)

This setup allows us to test a lot of things easily (currently untested), such as the existence of the appropriate d*** and r*** functions and whether the parameter names are correct. (They should be the first parameters after x.)

Potential problems: The attributes can be removed from the function mlcauchy, which will break the function in most scenarios. An alternative is to start each function with a variable such as name = quote(mlcauchy) which identifies the function instead of using match.call().

tripartio commented 1 month ago

I think adding attributes to the ml*** functions (https://github.com/JonasMoss/univariateML/issues/39) would make this much easier.

JonasMoss commented 1 week ago

Added automatic checks for testing the logLik. I don't think the remaining tests, such as tests for support, would be meaningful to automatize, as they are already handled by the "decorator" functon in conjunction with metadata. This is solved for now.