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

84 catch empty factor levels early on #85

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions R/abn-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ check.valid.data <- function(data.df = NULL, data.dists = NULL, group.var = NULL
stop("The data contains missing values! These must be removed.")
}

## check factor variables for empty levels
for (i in 1:dim(data.df)[2]) {
if (is.factor(data.df[[i]])) {
# check if there are levels with no observations
if (sum(table(data.df[[i]], useNA = "no") == 0) > 0) {
stop(paste0("The factor variable ", names(data.df)[i], " has levels with no observations! These must be removed."))
}
}
}

## check that distributions are in a list
if (!is.list(data.dists)) {
stop("data.dist must be a list")
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-abn-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ test_that("Data is checked properly", {

expect_equal(check.valid.data(data.df = df, data.dists = dists, group.var = NULL),
list(gaus=3, bin=1, pois=2, mult=4))

# test empty factor levels
df$D <- factor(df$D, levels=c("x", "y", "z", "w"))
expect_error(check.valid.data(data.df = df, data.dists = dists, group.var = NULL))
})

test_that("DAG is checked properly", {
Expand Down