Type: | Package |
Title: | Testing Infrastructure for Broom Model Generics |
Version: | 0.1.6 |
Description: | Provides a number of testthat tests that can be used to verify that tidy(), glance() and augment() methods meet consistent specifications. This allows methods for the same generic to be spread across multiple packages, since all of those packages can make the same guarantees to users about returned objects. |
License: | MIT + file LICENSE |
URL: | https://github.com/alexpghayes/modeltests |
BugReports: | https://github.com/alexpghayes/modeltests/issues |
Depends: | R (≥ 3.1) |
Imports: | dplyr (≥ 0.7.6), generics, purrr (≥ 0.2.5), testthat (≥ 2.0.0), tibble (≥ 3.0.0) |
Suggests: | covr |
Encoding: | UTF-8 |
LazyData: | true |
RoxygenNote: | 7.2.3 |
NeedsCompilation: | no |
Packaged: | 2024-05-03 17:35:53 UTC; alex |
Author: | Alex Hayes |
Maintainer: | Alex Hayes <alexpghayes@gmail.com> |
Repository: | CRAN |
Date/Publication: | 2024-05-03 17:50:02 UTC |
modeltests: Testing Infrastructure for Broom Model Generics
Description
Provides a number of testthat tests that can be used to verify that tidy(), glance() and augment() methods meet consistent specifications. This allows methods for the same generic to be spread across multiple packages, since all of those packages can make the same guarantees to users about returned objects.
Author(s)
Maintainer: Alex Hayes alexpghayes@gmail.com (ORCID)
Authors:
Simon Couch simonpatrickcouch@gmail.com
See Also
Useful links:
Report bugs at https://github.com/alexpghayes/modeltests/issues
Determine acceptable names for augment output
Description
Given a data frame (or tibble), and a model object, makes a character vector of acceptable columns names for augment output. This includes:
Any column names of the passed dataset
Any syntactically correct column names generated by calling
stats::model.frame()
on the object in question.
Usage
acceptable_augment_colnames(object, passed_data)
Arguments
object |
A model object. |
passed_data |
The dataset used to create the model object. |
Value
A vector of colnames that are acceptable in augment output.
Allowed argument names in tidiers
Description
Allowed argument names in tidiers
Usage
argument_glossary
Format
A tibble with 3 variables:
- method
One of "glance", "augment" or "tidy".
- argument
Character name of allowed argument name.
- description
Character description of argument use.
Examples
argument_glossary
Get copies of a dataset with various rowname behaviors
Description
Helper function for check_augment_data_specification()
. There should be no need
to ever use this directly in tests. Takes a dataset and returns a list
with three copies of the dataset. Optionally introduces NA
values into
the dataset. Useful for checking that tibbles, data frames, and data frames with
rownames are treated equivalently.
Usage
augment_data_helper(data, add_missing)
Arguments
data |
A data set as a |
add_missing |
Whether or not to set some values in |
Value
A list with three copies of data
:
-
tibble: the data in a
tibble::tibble()
. -
no_row: the data in a
data.frame()
without row names. -
row_nm: the data in a
data.frame
, with row names.
See Also
.row_names_info()
, rownames()
, tibble::rownames_to_column()
Check that tidying methods use allowed argument names
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Tests when strict = FALSE
:
None
Tests when strict = TRUE
:
-
tidy_method
has aconf.int
argument if it has aconf.level
argument. -
tidy_method
has aconf.level
argument if it has aconf.int
argument. -
conf.int
defaults toFALSE
when present. -
conf.level
defaults to '0.95“ when present. -
exponentiate
defaults toFALSE
when present. All arguments to
tidy_method
are listed in the argument_glossary.
Usage
check_arguments(tidy_method, strict = TRUE)
Arguments
tidy_method |
A tidying method. For example: |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Value
An invisible NULL
. This function should be called for side effects, not return values.
See Also
testthat, testthat::expect_true()
Check that augment behavior is consistent for dataframes and tibbles
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Uses augment_data_helper()
to create copies of the same dataset as
a tibble, data frame and dataframe with rownames. When add_missing = TRUE
these
datasets have missing values along the diagonal, and one row of entirely missing
values. Once the datasets have been generated, tests that:
-
augment(fit, data = generated_dataset)
passescheck_tibble()
for each generated dataset. Output of
augment(fit, data = generated_dataset)
is the same for all three generated datasets, except the data frame with rownames should also generate a.rownames
column that the tibble and nameless data frame do not.
Additional tests when test_newdata = TRUE
:
-
head(aug(model, newdata = data))
equalsaug(head(model, newdata = data))
. This commutativity check catches issues where the output ofpredict
changes for the same data point depending on the rest of the dataset.
Usage
check_augment_data_specification(aug, model, data, add_missing, test_newdata)
Arguments
aug |
An augment method. For example, |
model |
A fit model object to call the augment method on. |
data |
A data frame or tibble to use when testing |
add_missing |
Logical indicating whether or not missing data should be
introduced into the datasets generated with |
test_newdata |
Logical indicating whether the |
Value
An invisible NULL
. This function should be called for side effects, not return values.
Check an augment method
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Test when strict = FALSE
:
-
aug(model, data = data)
passescheck_tibble()
-
aug(model, newdata = newdata)
passescheck_tibble()
Additional tests when strict = TRUE
:
-
aug(model, data = data)
passescheck_augment_data_specification()
. -
aug(model, newdata = newdata)
passescheck_augment_data_specification()
. -
aug(model, newdata = newdata)
passescheck_augment_data_specification()
withadd_missing = TRUE
. If
aug
has anewdata
argument, thenewdata
argument takes precedence over adata
argument, i.e. callscheck_augment_newdata_precedence()
.-
aug
either gives an informative error or produces a reasonable tibble, i.e. callscheck_augment_no_data()
.
Note that it doesn't make sense to test that aug(model, data = data)
passes check_augment_data_specification()
with add_missing = TRUE
. This is
because the user is already guaranteeing that data
is the original dataset
used to create model
.
Usage
check_augment_function(aug, model, data = NULL, newdata = NULL, strict = TRUE)
Arguments
aug |
An augment method. For example, |
model |
A fit model object to call the augment method on. |
data |
A data frame or tibble to use when testing |
newdata |
A dataset to use to check the |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Value
An invisible NULL
. This function should be called for side effects, not return values.
Check that newdata argument has higher precedence than data argument
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Usage
check_augment_newdata_precedence(aug, model, data, strict = TRUE)
Arguments
aug |
An augment method. For example, |
model |
A fit model object to call the augment method on. |
data |
A data frame or tibble to use when testing |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Value
An invisible NULL
. This function should be called for side effects, not return values.
Check an augment method when no data or newdata is passed
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Test when strict = FALSE
:
None
Additional tests when strict = TRUE
:
-
aug(model)
either returns an informative error or produces output that passescheck_tibble()
. If the output passes
check_tibble
, will issue warning when:Augmented data is missing rows from original data.
Augmented data is missing columns from original data.
Original data has rownames but ugmented data is missing
.rownames
column.
Usage
check_augment_no_data(aug, model, passed_data, strict = TRUE)
Arguments
aug |
An augment method. For example, |
model |
A fit model object to call the augment method on. |
passed_data |
The dataset that |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Value
An invisible NULL
. This function should be called for side effects, not return values.
Check that tibble has expected dimensions.
Description
Check that tibble has expected dimensions.
Usage
check_dims(data, expected_rows = NULL, expected_cols = NULL)
Arguments
data |
A tibble or data frame. |
expected_rows |
Expected number of rows of tibble. |
expected_cols |
Expected number of columns of tibble. |
Examples
check_dims(iris, expected_rows = 150)
Check the output of a glance method
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Tests when strict = FALSE
:
Each item passed to
...
passescheck_tibble()
Each item passed to
...
has exactly 1 row.
Additional tests when strict = TRUE
:
Column names and order agree across all elements of
...
.
Usage
check_glance_outputs(..., strict = TRUE)
Arguments
... |
Outputs returned from calls to (the same) glance method. |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Value
An invisible NULL
. This function should be called for side effects, not return values.
See Also
Check the output of an augment method
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Test when strict = FALSE
:
-
au
passescheck_tibble()
. All column names present in
passed_data
are also present inau
.
Additional tests when strict = TRUE
:
If
passed_data
has rownames other than simple row numbers (i.e.paste(1:5)
),au
contains a column called.rownames
.
Usage
check_single_augment_output(au, passed_data, model = NULL, strict = TRUE)
Arguments
au |
Output from a call to |
passed_data |
Whichever of |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Value
An invisible NULL
. This function should be called for side effects, not return values.
Check the output of a tidying method
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
Tests when strict = FALSE
:
-
output
is a tibble.
Additional tests when strict = TRUE
:
-
columns
are listed in the column_glossary.
Usage
check_tibble(output, method, columns = colnames(output), strict = TRUE)
Arguments
output |
Object returned from |
method |
One of |
columns |
The names of the columns in the output data frame. Defaults
to the column names of |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Details
Do not call directly. Helper function used by check_tidy_output()
,
check_glance_outputs()
and check_augment_function()
.
Value
An invisible NULL
. This function should be called for side effects, not return values.
Check the output of a tidy method
Description
Call this function to perform tests. If a tests fails, an informative error will be thrown. Otherwise silent.
A thin wrapper around check_tibble()
.
Usage
check_tidy_output(td, strict = TRUE)
Arguments
td |
Output from a tidy method. |
strict |
Logical indicating whether the strict version of tests should be used. Defaults
to |
Value
An invisible NULL
. This function should be called for side effects, not return values.
Allowed column names in tidied tibbles
Description
Allowed column names in tidied tibbles
Usage
column_glossary
Format
A tibble with 4 variables:
- method
One of "glance", "augment" or "tidy".
- column
Character name of allowed output column.
- description
Character description of expected column contents.
Examples
column_glossary
Check whether or not a data-frame-like object has rownames
Description
Check whether or not a data-frame-like object has rownames
Usage
has_rownames(df)
Arguments
df |
A data frame |
Value
Logical indicating if df
has rownames. If df
is a tibble,
returns FALSE
. If df
is a data.frame, return FALSE
if the rownames
are simply row numbers. If the rownames are anything other than the
return row numbers, returns TRUE
.