Skip to content

Commit

Permalink
Can now deal with a single vector of input data (#364)
Browse files Browse the repository at this point in the history
* added handling of vector data #361

* styling
  • Loading branch information
osorensen authored Jan 26, 2024
1 parent a62545e commit ec9500c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
20 changes: 17 additions & 3 deletions R/setup_rank_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#' optional initial value of the rankings. If `rankings` has column names,
#' these are assumed to be the names of the items. `NA` values in rankings are
#' treated as missing data and automatically augmented; to change this
#' behavior, see the `na_action` argument to [set_model_options()].
#' behavior, see the `na_action` argument to [set_model_options()]. A vector
#' length `n_items` is silently converted to a matrix of length `1 x n_items`,
#' and names (if any), are used as column names.
#'
#' @param preferences A data frame with one row per pairwise comparison, and
#' columns `assessor`, `top_item`, and `bottom_item`. Each column contains the
Expand Down Expand Up @@ -138,10 +140,19 @@ setup_rank_data <- function(
if (na_action == "fail" && any(is.na(rankings))) {
stop("rankings matrix contains NA values")
}
if (!is.matrix(rankings)) {
rankings <- matrix(rankings,
nrow = 1,
dimnames = list(NULL, names(rankings))
)
}

if (na_action == "omit" && any(is.na(rankings))) {
keeps <- apply(rankings, 1, function(x) !any(is.na(x)))
print(paste("Omitting", sum(!keeps), "row(s) from rankings due to NA values"))
print(paste(
"Omitting", sum(!keeps),
"row(s) from rankings due to NA values"
))
rankings <- rankings[keeps, , drop = FALSE]
}
} else {
Expand All @@ -153,7 +164,10 @@ setup_rank_data <- function(
if (!is.null(observation_frequency)) {
validate_positive_vector(observation_frequency)
if (nrow(rankings) != length(observation_frequency)) {
stop("observation_frequency must be of same length as the number of rows in rankings")
stop(
"observation_frequency must be of same ",
"length as the number of rows in rankings"
)
}
} else {
observation_frequency <- rep(1, nrow(rankings))
Expand Down
4 changes: 3 additions & 1 deletion man/setup_rank_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/test-setup_rank_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ test_that("setup_rank_data works with rankings", {

dat <- setup_rank_data(rr, validate_rankings = FALSE)
expect_equal(dat$rankings, rr)

input1 <- potato_weighing[2, , drop = FALSE]
rownames(input1) <- NULL
input2 <- potato_weighing[2, ]
expect_equal(
setup_rank_data(input1),
setup_rank_data(input2)
)
})

test_that("setup_rank_data works for preferences", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-smc_update_correctness.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ test_that("update_mallows works for data one at a time", {
for (i in seq_len(200)) {
mod <- update_mallows(
model = mod,
new_data = setup_rank_data(sushi_rankings[i, , drop = FALSE])
new_data = setup_rank_data(sushi_rankings[i, ])
)
}
expect_equal(
Expand Down

0 comments on commit ec9500c

Please sign in to comment.