From f2b6cc1c5a475e2bb7244e99a94dbdaccff4a86d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Fri, 7 Aug 2026 11:45:36 +0200 Subject: [PATCH 1/2] Fix cran note --- tests/testthat/test-inspect.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-inspect.R b/tests/testthat/test-inspect.R index 3534ef5..5dfbcd0 100644 --- a/tests/testthat/test-inspect.R +++ b/tests/testthat/test-inspect.R @@ -21,7 +21,7 @@ test_that("inspect works with replicates", { survey$sample <- seq_len(nrow(survey)) batches <- inspect(index, survey[-nas, ]) t_samples <- table(batches$sample) - expect_equal(table(t_samples), structure(c(`1` = 230L, `4` = 5L), .Dim = 2L, .Dimnames = list( + expect_equal(table(t_samples), structure(c(`1` = 230L, `4` = 5L), dim = 2L, dimnames = list( t_samples = c("1", "4")), class = "table")) # TODO check order is right for the samples From 9783af2cf1d0ecf12848e6b12bf41fa7923f2850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs?= Date: Fri, 7 Aug 2026 13:05:17 +0200 Subject: [PATCH 2/2] Update style and documentation --- DESCRIPTION | 2 +- R/QC.R | 19 ++-- R/designer.R | 25 +++-- R/entropy.R | 124 +++++++++++++------------ R/evaluate.R | 21 +++-- R/evaluate_category.R | 43 ++++++--- R/evaluate_num.R | 26 ++++-- R/follow_up.R | 58 +++++++----- R/indexing.R | 26 +++--- R/optimum.R | 28 +++--- R/reporting.R | 27 ++++-- R/spatial.R | 35 +++---- R/utils.R | 28 +++--- README.Rmd | 2 +- man/check_data.Rd | 2 +- man/check_index.Rd | 6 +- man/compare_index.Rd | 6 +- man/design.Rd | 6 +- man/distribution.Rd | 6 +- man/evaluate_entropy.Rd | 28 +++--- man/evaluate_independence.Rd | 28 +++--- man/evaluate_index.Rd | 16 ++-- man/evaluate_mad.Rd | 30 +++--- man/evaluate_mean.Rd | 30 +++--- man/evaluate_na.Rd | 30 +++--- man/evaluate_orig.Rd | 16 ++-- man/evaluate_sd.Rd | 30 +++--- man/extreme_cases.Rd | 6 +- man/follow_up2.Rd | 4 +- man/inspect.Rd | 6 +- man/replicates.Rd | 6 +- man/spatial.Rd | 6 +- man/valid_followup.Rd | 2 +- tests/spelling.R | 5 +- tests/testthat/test-batch_names.R | 2 +- tests/testthat/test-check_data.R | 35 +++---- tests/testthat/test-compare_index.R | 26 +++--- tests/testthat/test-consistent_index.R | 6 +- tests/testthat/test-design.R | 85 +++++++++-------- tests/testthat/test-distribution.R | 6 +- tests/testthat/test-evaluate_entropy.R | 22 +++-- tests/testthat/test-evaluate_index.R | 28 +++--- tests/testthat/test-extreme_cases.R | 41 ++++---- tests/testthat/test-follow_up.R | 14 ++- tests/testthat/test-indexing.R | 26 ++++-- tests/testthat/test-insert.R | 28 +++--- tests/testthat/test-inspect.R | 37 +++++--- tests/testthat/test-optimum.R | 6 +- tests/testthat/test-replicates.R | 12 ++- tests/testthat/test-simplify2matrix.R | 10 +- tests/testthat/test-spatial.R | 54 +++++++---- vignettes/experdesign.Rmd | 22 +++-- 52 files changed, 681 insertions(+), 512 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6cdeb2a..96f3b87 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,4 +35,4 @@ Config/testthat/edition: 3 Encoding: UTF-8 Language: en-US Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +Config/roxygen2/version: 8.0.0 diff --git a/R/QC.R b/R/QC.R index 56fd736..2981d00 100644 --- a/R/QC.R +++ b/R/QC.R @@ -8,14 +8,15 @@ #' @seealso [optimum()] #' @export #' @examples -#' metadata <- expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50), -#' sex = c("Male","Female")) +#' metadata <- expand.grid( +#' height = seq(60, 80, 5), weight = seq(100, 300, 50), +#' sex = c("Male", "Female") +#' ) #' sel <- extreme_cases(metadata, 10) #' # We can see that it selected both Female and Males and wide range of height #' # and weight: #' metadata[sel, ] extreme_cases <- function(pheno, size, omit = NULL, iterations = 500) { - # Calculate batches pheno_o <- omit(pheno, omit) num <- is_num(pheno_o) @@ -38,7 +39,7 @@ extreme_cases <- function(pheno, size, omit = NULL, iterations = 500) { # Evaluate the differences between the subsets and the originals differences <- drop(abs(sweep(subsets, c(1, 2), original_pheno))) differences <- differences[-c(1, 4), ] - differences["entropy", ] <- differences["entropy", ]/0.5 + differences["entropy", ] <- differences["entropy", ] / 0.5 # Maximize the entropy and the dispersion optimize <- sum(colSums(differences)) @@ -66,7 +67,6 @@ extreme_cases <- function(pheno, size, omit = NULL, iterations = 500) { #' QC_samples <- qcSubset(index, 10) #' QC_samplesBatch <- qcSubset(index, 10, TRUE) qcSubset <- function(index, size, each = FALSE) { - if (!is_logical(each)) { stop("each should be either TRUE or FALSE") } @@ -101,8 +101,10 @@ qcSubset <- function(index, size, each = FALSE) { #' @export #' @examples #' index <- create_subset(50, 24) -#' metadata <- expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50), -#' sex = c("Male","Female")) +#' metadata <- expand.grid( +#' height = seq(60, 80, 5), weight = seq(100, 300, 50), +#' sex = c("Male", "Female") +#' ) #' check_index(metadata, index) check_index <- function(pheno, index, omit = NULL) { batches <- length(index) @@ -114,14 +116,13 @@ check_index <- function(pheno, index, omit = NULL) { num <- is_num(pheno_o) eval_n <- evaluations(num) original_pheno <- .evaluate_orig(pheno_o, num) - original_pheno["na", ] <- original_pheno["na", ]/batches + original_pheno["na", ] <- original_pheno["na", ] / batches .check_index(index, pheno_o, num, eval_n, original_pheno) } .check_index <- function(index, pheno_o, num, eval_n, eval_orig) { - subsets <- .evaluate_index(index, pheno_o, num) # Evaluate the differences between the subsets and the originals differences <- abs(sweep(subsets, c(1, 2), eval_orig)) diff --git a/R/designer.R b/R/designer.R index 981f5a8..4343c92 100644 --- a/R/designer.R +++ b/R/designer.R @@ -15,8 +15,10 @@ #' @export #' @examples #' data(survey, package = "MASS") -#' index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, -#' iterations = 10) +#' index <- design(survey[, c("Sex", "Smoke", "Age")], +#' size_subset = 50, +#' iterations = 10 +#' ) #' index design <- function(pheno, size_subset, omit = NULL, iterations = 500, name = "SubSet") { @@ -52,7 +54,8 @@ design <- function(pheno, size_subset, omit = NULL, iterations = 500, if (length(size_subset) == 1 && !valid_sizes(size_data, size_subset, batches)) { stop("Please provide a higher number of batches or more samples per batch.", - call. = FALSE) + call. = FALSE + ) } pheno_o <- omit(pheno, omit) @@ -62,7 +65,7 @@ design <- function(pheno, size_subset, omit = NULL, iterations = 500, num <- is_num(pheno_o) original_pheno <- .evaluate_orig(pheno_o, num) - original_pheno["na", ] <- original_pheno["na", ]/batches + original_pheno["na", ] <- original_pheno["na", ] / batches # Find the numeric values dates <- vapply(pheno_o, is_date, logical(1L)) @@ -105,17 +108,19 @@ design <- function(pheno, size_subset, omit = NULL, iterations = 500, #' @seealso [design()], [extreme_cases()]. #' @export #' @examples -#' samples <- data.frame(L = letters[1:25], Age = rnorm(25), -#' type = sample(LETTERS[1:5], 25, TRUE)) +#' samples <- data.frame( +#' L = letters[1:25], Age = rnorm(25), +#' type = sample(LETTERS[1:5], 25, TRUE) +#' ) #' index <- replicates(samples, 5, controls = 2, omit = "L", iterations = 10) #' head(index) replicates <- function(pheno, size_subset, controls, omit = NULL, - iterations = 500){ + iterations = 500) { stopifnot(is_numeric(size_subset) && length(size_subset) == 1) stopifnot(length(dim(pheno)) == 2) stopifnot(is_numeric(iterations)) stopifnot("There should be a positive integer of controls" = is.numeric(controls) && length(controls) && as.integer(controls) == controls && - is.finite(controls) && !is.na(controls) && controls > 0) + is.finite(controls) && !is.na(controls) && controls > 0) size_data <- nrow(pheno) if (size_subset >= size_data) { @@ -123,7 +128,9 @@ replicates <- function(pheno, size_subset, controls, omit = NULL, } if (size_subset < controls) { stop("The controls are technical controls for the batches.\n\t", - "They cannot be above the number of samples per batch.", call. = FALSE) + "They cannot be above the number of samples per batch.", + call. = FALSE + ) } size_subset <- size_subset - controls values <- extreme_cases(pheno = pheno, size = controls, omit = omit) diff --git a/R/entropy.R b/R/entropy.R index c8e7996..78799f7 100644 --- a/R/entropy.R +++ b/R/entropy.R @@ -1,61 +1,63 @@ -#' Calculates the entropy -#' -#' Calculates the entropy of a category. It uses the amount of categories to -#' scale between 0 and 1. -#' @param x A character or vector with two or more categories -#' @return The numeric value of the Shannon entropy scaled between 0 and 1. -#' @note It omits the `NA` if present. -#' @export -#' @examples -#' entropy(c("H", "T", "H", "T")) -#' entropy(c("H", "T", "H", "T", "H", "H", "H")) -#' entropy(c("H", "T", "H", "T", "H", "H", NA)) -#' entropy(c("H", "T", "H", "T", "H", "H")) -#' entropy(c("H", "H", "H", "H", "H", "H", NA)) -entropy <- function(x){ - x <- x[!is.na(x)] - n <- length(unique(x)) - if (n == 1L) { - return(0L) - } - prob <- table(x)/length(x) - - sum(prob*log(prob, n)) -} - -#' Evaluate the dispersion of NAs -#' -#' Looks how are `NA` distributed in each subset -#' @param i list of numeric indices of the data.frame -#' @param pheno Data.frame -#' @return The optimum value to reduce -#' @family functions to evaluate samples -#' @family functions to evaluate categories -#' @family functions to evaluate numbers -#' @export -#' @examples -#' samples <- 10 -#' m <- matrix(rnorm(samples), nrow = samples) -#' m[sample(seq_len(samples), size = 5), ] <- NA # Some NA -#' i <- create_subset(samples, 3, 4) # random subsets -#' evaluate_na(i, m) -evaluate_na <- function(i, pheno) { - stopifnot(sum(lengths(i)) == nrow(pheno)) - orig_nas <- colSums(is.na(pheno)) - - orig_nas <- orig_nas/length(i) - out <- lapply(i, function(x){colSums(is.na(pheno[x, , drop = FALSE]))}) - nas <- t(simplify2matrix(out)) - evaluate_helper(nas, orig_nas) -} - - -# Function like simplify2array but it always return a matrix -simplify2matrix <- function(l) { - stopifnot(length(unique(lengths(l))) == 1) - u <- unlist(l) - - m <- matrix(u, ncol = length(l)) - colnames(m) <- names(l) - rownames(m) <- names(l[[1]]) - m -} +#' Calculates the entropy +#' +#' Calculates the entropy of a category. It uses the amount of categories to +#' scale between 0 and 1. +#' @param x A character or vector with two or more categories +#' @return The numeric value of the Shannon entropy scaled between 0 and 1. +#' @note It omits the `NA` if present. +#' @export +#' @examples +#' entropy(c("H", "T", "H", "T")) +#' entropy(c("H", "T", "H", "T", "H", "H", "H")) +#' entropy(c("H", "T", "H", "T", "H", "H", NA)) +#' entropy(c("H", "T", "H", "T", "H", "H")) +#' entropy(c("H", "H", "H", "H", "H", "H", NA)) +entropy <- function(x) { + x <- x[!is.na(x)] + n <- length(unique(x)) + if (n == 1L) { + return(0L) + } + prob <- table(x) / length(x) + -sum(prob * log(prob, n)) +} + +#' Evaluate the dispersion of NAs +#' +#' Looks how are `NA` distributed in each subset +#' @param i list of numeric indices of the data.frame +#' @param pheno Data.frame +#' @return The optimum value to reduce +#' @family functions to evaluate samples +#' @family functions to evaluate categories +#' @family functions to evaluate numbers +#' @export +#' @examples +#' samples <- 10 +#' m <- matrix(rnorm(samples), nrow = samples) +#' m[sample(seq_len(samples), size = 5), ] <- NA # Some NA +#' i <- create_subset(samples, 3, 4) # random subsets +#' evaluate_na(i, m) +evaluate_na <- function(i, pheno) { + stopifnot(sum(lengths(i)) == nrow(pheno)) + orig_nas <- colSums(is.na(pheno)) + + orig_nas <- orig_nas / length(i) + out <- lapply(i, function(x) { + colSums(is.na(pheno[x, , drop = FALSE])) + }) + nas <- t(simplify2matrix(out)) + evaluate_helper(nas, orig_nas) +} + + +# Function like simplify2array but it always return a matrix +simplify2matrix <- function(l) { + stopifnot(length(unique(lengths(l))) == 1) + u <- unlist(l) + + m <- matrix(u, ncol = length(l)) + colnames(m) <- names(l) + rownames(m) <- names(l[[1]]) + m +} diff --git a/R/evaluate.R b/R/evaluate.R index d247256..d782c7e 100644 --- a/R/evaluate.R +++ b/R/evaluate.R @@ -1,6 +1,6 @@ # A function to calculate the difference between a matrix and the original # dataset -evaluate_helper <- function(x, original_x){ +evaluate_helper <- function(x, original_x) { stopifnot(ncol(x) == length(original_x)) out <- sweep(x, 2, original_x, "-") colMeans(abs(out), na.rm = TRUE) @@ -66,7 +66,6 @@ evaluate_index <- function(i, pheno) { warning("There might be some problems with the data use check_data().", call. = FALSE) } .evaluate_index(i, pheno, is_num(pheno)) - } @@ -80,15 +79,15 @@ evaluate_index <- function(i, pheno) { out <- sapply(i, ev_subset, pheno = pheno, numeric = num, diff = d, simplify = "array") - dimnames(out) <- list("stat" = rownames(d), - "variables" = variables, - "subgroups" = names(i)) + dimnames(out) <- list( + "stat" = rownames(d), + "variables" = variables, + "subgroups" = names(i) + ) out - } -ev_subset <- function(x, pheno, numeric, diff){ - +ev_subset <- function(x, pheno, numeric, diff) { subset_na <- colSums(is.na(pheno[x, , drop = FALSE])) subset_na <- t(as.matrix(subset_na)) rownames(subset_na) <- "na" @@ -101,9 +100,11 @@ ev_subset <- function(x, pheno, numeric, diff){ # pheno_num <- pheno[x, numeric, drop = FALSE] subset_num <- apply(pheno_num, 2, function(y) { - c("sd" = sd(y, na.rm = TRUE), + c( + "sd" = sd(y, na.rm = TRUE), "mean" = mean(y, na.rm = TRUE), - "mad" = mad(y, na.rm = TRUE)) + "mad" = mad(y, na.rm = TRUE) + ) }) diff1 <- insert(diff1, subset_num) } diff --git a/R/evaluate_category.R b/R/evaluate_category.R index 17de4e5..3aa1aa5 100644 --- a/R/evaluate_category.R +++ b/R/evaluate_category.R @@ -12,17 +12,20 @@ #' @export #' @examples #' data(survey, package = "MASS") -#' index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, -#' iterations = 10) +#' index <- design(survey[, c("Sex", "Smoke", "Age")], +#' size_subset = 50, +#' iterations = 10 +#' ) #' # Note that numeric columns will be omitted: #' evaluate_entropy(index, survey[, c("Sex", "Smoke", "Age")]) evaluate_entropy <- function(i, pheno) { - num <- is_num(pheno) stopifnot(sum(!num) >= 1) pheno_o <- droplevels(pheno[, !num, drop = FALSE]) # Calculate the entropy for the categorical values - original_e <- vapply(pheno_o, function(x){length(unique(x))/length(x)}, numeric(1L)) + original_e <- vapply(pheno_o, function(x) { + length(unique(x)) / length(x) + }, numeric(1L)) # Remove those that are different in each sample (Hopefully just the name of the sample) remove_e <- original_e == 1 .evaluate_cat(i, pheno_o, remove_e) @@ -30,7 +33,7 @@ evaluate_entropy <- function(i, pheno) { .evaluate_cat <- function(i, pheno, remove_e) { # Calculate the entropy for the subsets - out_e <- lapply(i, function(x){ + out_e <- lapply(i, function(x) { vapply(pheno[x, , drop = FALSE], entropy, numeric(1L)) }) out_e <- t(simplify2matrix(out_e)) @@ -53,8 +56,10 @@ evaluate_entropy <- function(i, pheno) { #' @export #' @examples #' data(survey, package = "MASS") -#' index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, -#' iterations = 10) +#' index <- design(survey[, c("Sex", "Smoke", "Age")], +#' size_subset = 50, +#' iterations = 10 +#' ) #' # Note that numeric columns will be omitted: #' evaluate_independence(index, survey[, c("Sex", "Smoke", "Age")]) evaluate_independence <- function(i, pheno) { @@ -87,7 +92,7 @@ evaluate_independence <- function(i, pheno) { #' rdata2 <- rbind(rdata, rdata) #' check_data(rdata2) #' \donttest{ -#' #Different warnings +#' # Different warnings #' check_data(rdata) #' check_data(rdata[-c(1, 3), ]) #' data(survey, package = "MASS") @@ -107,7 +112,9 @@ check_data <- function(pheno, omit = NULL, na.omit = FALSE) { if (sum(num + cat) != ncol(pheno)) { if (verbose) { warning("There are some columns of unidentified type. ", - "Only accepts numeric or categorical values in a data.frame.", call. = FALSE) + "Only accepts numeric or categorical values in a data.frame.", + call. = FALSE + ) } data_status <- FALSE } @@ -116,7 +123,8 @@ check_data <- function(pheno, omit = NULL, na.omit = FALSE) { if (sum(cat) == 0) { if (verbose) { warning("No categorical values were found; numeric values are not checked here.", - call. = FALSE) + call. = FALSE + ) } return(data_status) } @@ -130,7 +138,8 @@ check_data <- function(pheno, omit = NULL, na.omit = FALSE) { if (any(pairwise_colusion)) { if (isTRUE(verbose)) { warning("Two categorical variables don't have all combinations.", - call. = FALSE) + call. = FALSE + ) } data_status <- FALSE } @@ -138,7 +147,9 @@ check_data <- function(pheno, omit = NULL, na.omit = FALSE) { pheno_o <- droplevels(pheno[, cat, drop = FALSE]) - nas <- lapply(pheno_o, function(x){which(is.na(x))}) + nas <- lapply(pheno_o, function(x) { + which(is.na(x)) + }) if (any(lengths(nas) >= 1)) { if (verbose) { warning("Some values are missing.", call. = FALSE) @@ -151,7 +162,9 @@ check_data <- function(pheno, omit = NULL, na.omit = FALSE) { if (any(lengths(l_unique) == nrow(pheno_o))) { if (verbose) { warning("There is a variable with as many categories as samples. ", - "Are these the sample names?", call. = FALSE) + "Are these the sample names?", + call. = FALSE + ) } } if (sum(lengths(l_unique) == nrow(pheno_o)) > 1) { @@ -161,7 +174,9 @@ check_data <- function(pheno, omit = NULL, na.omit = FALSE) { data_status <- FALSE } - if (any(vapply(l_unique, function(x) {any(x == 1)}, FUN.VALUE = logical(1L)))) { + if (any(vapply(l_unique, function(x) { + any(x == 1) + }, FUN.VALUE = logical(1L)))) { if (verbose) { warning("There is a category with just one sample.", call. = FALSE) } diff --git a/R/evaluate_num.R b/R/evaluate_num.R index 8b2457e..19700b6 100644 --- a/R/evaluate_num.R +++ b/R/evaluate_num.R @@ -11,11 +11,13 @@ #' @export #' @examples #' data(survey, package = "MASS") -#' index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, -#' iterations = 10) +#' index <- design(survey[, c("Sex", "Smoke", "Age")], +#' size_subset = 50, +#' iterations = 10 +#' ) #' # Note that categorical columns will be omitted: #' evaluate_sd(index, survey[, c("Sex", "Smoke", "Age")]) -evaluate_sd <- function(i, pheno){ +evaluate_sd <- function(i, pheno) { stopifnot(sum(lengths(i)) == nrow(pheno)) # Distribution of sd num <- is_num(pheno) @@ -23,7 +25,8 @@ evaluate_sd <- function(i, pheno){ original_sd <- apply(pheno_o, 2, sd, na.rm = TRUE) i <- batch_names(i) sd_group <- apply(pheno_o, 2, function(x) { - tapply(x, i, sd, na.rm = TRUE, default = 0L)}) + tapply(x, i, sd, na.rm = TRUE, default = 0L) + }) evaluate_helper(sd_group, original_sd) } @@ -38,8 +41,10 @@ evaluate_sd <- function(i, pheno){ #' @export #' @examples #' data(survey, package = "MASS") -#' index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, -#' iterations = 10) +#' index <- design(survey[, c("Sex", "Smoke", "Age")], +#' size_subset = 50, +#' iterations = 10 +#' ) #' # Note that categorical columns will be omitted: #' evaluate_mean(index, survey[, c("Sex", "Smoke", "Age")]) evaluate_mean <- function(i, pheno) { @@ -68,8 +73,10 @@ evaluate_mean <- function(i, pheno) { #' @export #' @examples #' data(survey, package = "MASS") -#' index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, -#' iterations = 10) +#' index <- design(survey[, c("Sex", "Smoke", "Age")], +#' size_subset = 50, +#' iterations = 10 +#' ) #' # Note that categorical columns will be omitted: #' evaluate_mad(index, survey[, c("Sex", "Smoke", "Age")]) evaluate_mad <- function(i, pheno) { @@ -82,7 +89,6 @@ evaluate_mad <- function(i, pheno) { i <- batch_names(i) mad_group <- apply(pheno_o, 2, function(x) { tapply(x, i, mad, na.rm = TRUE, default = 0L) - }) + }) evaluate_helper(mad_group, original_mad) } - diff --git a/R/follow_up.R b/R/follow_up.R index 049b28a..3dbef63 100644 --- a/R/follow_up.R +++ b/R/follow_up.R @@ -39,12 +39,15 @@ follow_up <- function(original, follow_up, size_subset, omit = NULL, original[[old_new]] <- "old" follow_up[[old_new]] <- "new" full <- rbind(original[, mc], follow_up[, mc]) - full_b <- rbind(original[, c(match_columns, old_new)], - follow_up[, c(match_columns, old_new)]) + full_b <- rbind( + original[, c(match_columns, old_new)], + follow_up[, c(match_columns, old_new)] + ) .check_followup(full, full_b, - new_data = follow_up[, match_columns], - old_data = original[, match_columns]) + new_data = follow_up[, match_columns], + old_data = original[, match_columns] + ) d <- .design(full_b, size_subset = size_subset, iterations = iterations, check = FALSE) inspect(d, full_b, omit = omit) @@ -75,7 +78,7 @@ follow_up <- function(original, follow_up, size_subset, omit = NULL, #' index1 <- design(survey1, size_subset = 50, iterations = 10) #' r_survey <- inspect(index1, survey1) #' # Create the second batch with "new" students -#' survey2 <- survey[seq(from = first_batch_n +1, to = nrow(survey)), variables] +#' survey2 <- survey[seq(from = first_batch_n + 1, to = nrow(survey)), variables] #' survey2$batch <- NA #' # Prepare the follow up #' all_classroom <- rbind(r_survey, survey2) @@ -87,8 +90,10 @@ follow_up2 <- function(all_data, batch_column = "batch", ...) { } if (!anyNA(all_data[[batch_column]])) { - stop("Seems that there is no new data: All the ", batch_column, - " column is already filled up.") + stop( + "Seems that there is no new data: All the ", batch_column, + " column is already filled up." + ) } new_data <- all_data[is.na(all_data[[batch_column]]), ] @@ -101,7 +106,7 @@ follow_up2 <- function(all_data, batch_column = "batch", ...) { args$size_subset <- max(lengths(old_index)) } if (!"iterations" %in% names(args)) { - #Set the same as the default for design + # Set the same as the default for design args$iterations <- formals(design)$iterations } if (!"omit" %in% names(args)) { @@ -119,20 +124,26 @@ follow_up2 <- function(all_data, batch_column = "batch", ...) { all_data2[is.na(all_data2[[batch_column]]), batch_column] <- "new" num <- is_num(all_data) - n_unique <- sapply(all_data[, !num], function(x){length(unique(x))}) + n_unique <- sapply(all_data[, !num], function(x) { + length(unique(x)) + }) which_s <- n_unique == nrow(all_data) if (sum(which_s) > 1) { warning("Multiple samples were identified") } colnames <- colnames(all_data) - .check_followup(all_data[, setdiff(colnames, args$omit)], - all_data2[, setdiff(colnames, omit)], - new_data[, setdiff(colnames, args$omit)], - old_data[, setdiff(colnames, args$omit)]) + .check_followup( + all_data[, setdiff(colnames, args$omit)], + all_data2[, setdiff(colnames, omit)], + new_data[, setdiff(colnames, args$omit)], + old_data[, setdiff(colnames, args$omit)] + ) - new_index <- .design(new_data, size_subset = args$size_subset, omit = args$omit, - iterations = args$iterations, name = args$name, check = FALSE) + new_index <- .design(new_data, + size_subset = args$size_subset, omit = args$omit, + iterations = args$iterations, name = args$name, check = FALSE + ) w_new <- which(is.na(all_data[[batch_column]])) w_old <- which(!is.na(all_data[[batch_column]])) @@ -163,7 +174,7 @@ follow_up2 <- function(all_data, batch_column = "batch", ...) { #' survey2 <- survey[119:nrow(survey), ] #' valid_followup(survey1, survey2) #' survey$batch <- NA -#' survey$batch[1:118] <- "old" +#' survey$batch[1:118] <- "old" #' valid_followup(all_data = survey) valid_followup <- function(old_data = NULL, new_data = NULL, all_data = NULL, omit = NULL, column = "batch") { @@ -186,8 +197,10 @@ valid_followup <- function(old_data = NULL, new_data = NULL, all_data = NULL, all_data <- rbind(old_data[, mc], new_data[, mc]) all_data2 <- rbind(old_data[, mc], new_data[, mc]) - all_data2[[column]] <- c(rep("old", nrow(old_data)), - rep("new", nrow(new_data))) + all_data2[[column]] <- c( + rep("old", nrow(old_data)), + rep("new", nrow(new_data)) + ) } else if (!is.null(all_data) && valid_column) { all_data <- all_data[, setdiff(colnames(all_data), omit)] all_data2 <- all_data @@ -203,7 +216,6 @@ valid_followup <- function(old_data = NULL, new_data = NULL, all_data = NULL, } .check_followup <- function(all_data, all_data_batch, new_data, old_data, verbose = TRUE) { - # Check all data but omitting batch name check_all <- .check_data(all_data, verbose = FALSE) # Check all data but knowing that there is an old and new category @@ -221,19 +233,21 @@ valid_followup <- function(old_data = NULL, new_data = NULL, all_data = NULL, if (check_all && !check_cmbn) { if (verbose) { warning("There are some problems with the addition of the new samples.", - call. = FALSE) + call. = FALSE + ) } ok <- FALSE } if (!check_cmbn) { if (verbose) { warning("There are some problems with the new samples and the batches.", - call. = FALSE) + call. = FALSE + ) } ok <- FALSE } if (!check_new) { - if (verbose ) { + if (verbose) { warning("There are some problems with the new data.", call. = FALSE) } ok <- FALSE diff --git a/R/indexing.R b/R/indexing.R index f6c1cf7..2b55fd9 100644 --- a/R/indexing.R +++ b/R/indexing.R @@ -13,7 +13,6 @@ #' @examples #' index <- create_subset(100, 50, 2) create_subset <- function(size_data, size_subset = NULL, n = NULL, name = "SubSet") { - if (is.null(size_subset) && is.null(n)) { stop("Either size.subset or n should numeric") } @@ -44,24 +43,28 @@ create_index <- function(size_data, size_batches, n, name = "SubSet") { # Shuffle sample within index to improve positioning create_index4index <- function(index, name) { - - m <- matrix(data = NA, nrow = length(name), ncol = length(index), - dimnames = list(name, names(index))) - # Assign each row number from each batch to a position: + m <- matrix( + data = NA, nrow = length(name), ncol = length(index), + dimnames = list(name, names(index)) + ) + # Assign each row number from each batch to a position: for (batch in seq_along(index)) { positions <- sample(index[[batch]]) rows <- sample(seq_along(positions)) m[rows, batch] <- positions } # Transform to a list omitting the empty values - index_out <- apply(m, 1, function(x){x[!is.na(x)]}, simplify = FALSE) + index_out <- apply(m, 1, function(x) { + x[!is.na(x)] + }, simplify = FALSE) index_out[lengths(index_out) != 0] } id2batch_names <- function(name, n) { if (length(name) != 1L && length(name) != n) { stop("Provide a single character or a vector the same size of the batches.", - call. = FALSE) + call. = FALSE + ) } if (length(name) == 1L) { name <- paste0(name, seq_len(n)) @@ -142,8 +145,10 @@ batch_names <- function(i) { #' @examples #' index1 <- create_subset(50, 24) #' index2 <- batch_names(create_subset(50, 24)) -#' metadata <- expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50), -#' sex = c("Male","Female")) +#' metadata <- expand.grid( +#' height = seq(60, 80, 5), weight = seq(100, 300, 50), +#' sex = c("Male", "Female") +#' ) #' compare_index(metadata, index1, index2) compare_index <- function(pheno, index1, index2) { if (is.character(index1) && length(index1) == nrow(pheno)) { @@ -181,13 +186,12 @@ compare_index <- function(pheno, index1, index2) { eval_n <- evaluations(num) original_pheno <- .evaluate_orig(pheno, num) - original_pheno["na", ] <- original_pheno["na", ]/batches + original_pheno["na", ] <- original_pheno["na", ] / batches ci1 <- .check_index(index1, pheno, num, eval_n, original_pheno) ci2 <- .check_index(index2, pheno, num, eval_n, original_pheno) ci1 - ci2 - } apply_index <- function(pheno, index, name = "old_rows") { diff --git a/R/optimum.R b/R/optimum.R index 031c316..c464e30 100644 --- a/R/optimum.R +++ b/R/optimum.R @@ -32,7 +32,7 @@ NULL optimum_batches <- function(size_data, size_subset) { check_number(size_data, "size_data") check_number(size_subset, "size_subset") - ceiling(size_data/size_subset) + ceiling(size_data / size_subset) } #' @export @@ -40,7 +40,7 @@ optimum_batches <- function(size_data, size_subset) { optimum_subset <- function(size_data, batches) { check_number(size_data, "size_data") check_number(batches, "batches") - ceiling(size_data/batches) + ceiling(size_data / batches) } @@ -51,18 +51,21 @@ sizes_batches <- function(size_data, size_subset, batches) { check_number(size_data, "size_data") check_number(size_subset, "size_subset") check_number(batches, "batches") - if (size_subset*batches < size_data) { + if (size_subset * batches < size_data) { stop("batches or size_subset is too small to fit all the samples.", - call. = FALSE) + call. = FALSE + ) } - if (sum(size_subset*seq_len(batches) > size_data) > 1) { + if (sum(size_subset * seq_len(batches) > size_data) > 1) { stop("batches or size_subset could be reduced.", - call. = FALSE) + call. = FALSE + ) } if (!valid_sizes(size_data, size_subset, batches)) { stop("Please provide a higher number of batches or more samples per batch.", - call. = FALSE) + call. = FALSE + ) } out <- internal_batches(size_data, size_subset, batches) out <- unname(out) @@ -74,7 +77,7 @@ sizes_batches <- function(size_data, size_subset, batches) { internal_batches <- function(size_data, size_subset, batches) { # If the data fits exactly there is no need for further calculations - if (size_subset*batches == size_data) { + if (size_subset * batches == size_data) { return(rep(size_subset, times = batches)) } @@ -83,7 +86,7 @@ internal_batches <- function(size_data, size_subset, batches) { } # If there are no remaining samples to allocate that's it - if (size_subset*batches == size_data) { + if (size_subset * batches == size_data) { return(rep(size_subset, times = batches)) } # If not all samples can be allocated return the maximum number of samples @@ -93,7 +96,7 @@ internal_batches <- function(size_data, size_subset, batches) { } # Calculate the minimum number of samples per batch - remaining <- size_data - max_batch_size*batches + remaining <- size_data - max_batch_size * batches # Pre-allocate the max size of each batch out <- rep(max_batch_size, batches) @@ -103,11 +106,10 @@ internal_batches <- function(size_data, size_subset, batches) { } # Calculate how many samples must be removed - samples_to_remove_per_batch <- ceiling(abs(remaining)/batches) + samples_to_remove_per_batch <- ceiling(abs(remaining) / batches) # Calculate how many batches have less samples - batches_to_remove_samples <- abs(remaining)/samples_to_remove_per_batch + batches_to_remove_samples <- abs(remaining) / samples_to_remove_per_batch # Apply it:`` out[1:batches_to_remove_samples] <- out[1:batches_to_remove_samples] - samples_to_remove_per_batch sort(out, decreasing = TRUE) } - diff --git a/R/reporting.R b/R/reporting.R index 4bbf82b..fb9e940 100644 --- a/R/reporting.R +++ b/R/reporting.R @@ -10,19 +10,20 @@ #' @examples #' data(survey, package = "MASS") #' columns <- c("Sex", "Age", "Smoke") -#' index <- design(pheno = survey[, columns], size_subset = 70, -#' iterations = 10) +#' index <- design( +#' pheno = survey[, columns], size_subset = 70, +#' iterations = 10 +#' ) #' batches <- inspect(index, survey[, columns]) #' head(batches) inspect <- function(i, pheno, omit = NULL, index_name = "batch") { - consistent_index(i, pheno) batch <- batch_names(translate_index(i)) # duplicate rows according to the index pheno <- apply_index(pheno, i) # Remove old rows (only needed to inspect changes) - pheno[ , "old_rows"] <- NULL + pheno[, "old_rows"] <- NULL stopifnot("Samples do not match" = length(i) == length(table(batch))) @@ -49,27 +50,33 @@ inspect <- function(i, pheno, omit = NULL, index_name = "batch") { #' data(survey, package = "MASS") #' columns <- c("Sex", "Age", "Smoke") #' nas <- c(137, 70) # Omit rows with NA to avoid warnings in design -#' index <- design(pheno = survey[-nas, columns], size_subset = 70, -#' iterations = 10) +#' index <- design( +#' pheno = survey[-nas, columns], size_subset = 70, +#' iterations = 10 +#' ) #' batches <- inspect(index, survey[-nas, columns]) #' distribution(batches, "Sex") #' distribution(batches, "Smoke") -distribution <- function(report, column){ +distribution <- function(report, column) { stopifnot(length(column) == 1) nBatch <- length(unique(report$batch)) distr <- table(report[[column]], report$batch) nCategory <- table(report[[column]]) - batchesCategory <- apply(distr, 1, function(x){sum(x != 0)}) + batchesCategory <- apply(distr, 1, function(x) { + sum(x != 0) + }) # Samples which are not on all batches and in less than the number of samples # per category interesting <- batchesCategory != nBatch & batchesCategory < nCategory if (any(interesting)) { - warning(column, ": ", sum(interesting), - " categories not totally distributed in all batches") + warning( + column, ": ", sum(interesting), + " categories not totally distributed in all batches" + ) return(FALSE) } TRUE diff --git a/R/spatial.R b/R/spatial.R index d9190bf..34f8beb 100644 --- a/R/spatial.R +++ b/R/spatial.R @@ -12,21 +12,23 @@ #' @export #' @examples #' data(survey, package = "MASS") -#' index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, -#' iterations = 10) +#' index <- design(survey[, c("Sex", "Smoke", "Age")], +#' size_subset = 50, +#' iterations = 10 +#' ) #' index2 <- spatial(index, survey[, c("Sex", "Smoke", "Age")], iterations = 10) #' head(index2) spatial <- function(index, pheno, omit = NULL, remove_positions = NULL, rows = LETTERS[1:5], - columns = 1:10, iterations = 500) { - + columns = 1:10, iterations = 500) { stopifnot(length(dim(pheno)) == 2) stopifnot(is_numeric(iterations)) position <- handle_positions(rows, columns, remove_positions) if (length(position) < max(lengths(index))) { stop("The size for the batch is smaller than the samples it must contain.", - "\n\tPlease check the rows and columns or how you created the index.", - call. = FALSE) + "\n\tPlease check the rows and columns or how you created the index.", + call. = FALSE + ) } opt <- Inf @@ -42,7 +44,7 @@ spatial <- function(index, pheno, omit = NULL, remove_positions = NULL, rows = L num <- is_num(pheno_o) original_pheno <- .evaluate_orig(pheno_o, num) - original_pheno["na", ] <- original_pheno["na", ]/batches + original_pheno["na", ] <- original_pheno["na", ] / batches # Find the numeric values dates <- vapply(pheno_o, is_date, logical(1L)) @@ -53,7 +55,6 @@ spatial <- function(index, pheno, omit = NULL, remove_positions = NULL, rows = L eval_n <- evaluations(num) for (j in seq_len(iterations)) { - i <- create_index4index(i2, name = position) meanDiff <- .check_index(i, pheno_o, num, eval_n, original_pheno) # Minimize the value @@ -68,8 +69,9 @@ spatial <- function(index, pheno, omit = NULL, remove_positions = NULL, rows = L if (any(lengths(val) > length(index))) { stop("The spatial distribution is impossible:", - "It allocated more sample to the previous index than possible.", - call. = FALSE) + "It allocated more sample to the previous index than possible.", + call. = FALSE + ) } # Return positions ordered by row and column m <- match(position_name(rows, columns)$name, names(val)) @@ -77,7 +79,6 @@ spatial <- function(index, pheno, omit = NULL, remove_positions = NULL, rows = L } handle_positions <- function(rows, columns, remove_positions) { - if (is.null(rows) || length(rows) == 0) { stop("Please provide at least one row.", call. = FALSE) } @@ -94,17 +95,19 @@ handle_positions <- function(rows, columns, remove_positions) { if (mix_positions) { warning("There is a mix of specific positions and rows or columns.") } - p <- positions[k_position & k_rows & k_columns, ,drop = FALSE] + p <- positions[k_position & k_rows & k_columns, , drop = FALSE] if (nrow(p) == 0L) { stop("No position is left. Did you remove too many positions?", - call. = FALSE) + call. = FALSE + ) } if (length(remove_positions) > (nrow(positions) - nrow(p))) { stop("Unrecognized position to remove.", - "\n\tCheck that it is a combination of rows and columns: A1, A3, or full rows and columns ...", - call. = FALSE) + "\n\tCheck that it is a combination of rows and columns: A1, A3, or full rows and columns ...", + call. = FALSE + ) } p$name -} \ No newline at end of file +} diff --git a/R/utils.R b/R/utils.R index 973ed3a..fb2002d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -13,7 +13,7 @@ is_date <- function(y) { methods::is(y, "Date") || inherits(y, "POSIXt") } -f_check <- function(y){ +f_check <- function(y) { is_char <- is.character(y) || is.factor(y) is_char || is_date(y) } @@ -30,13 +30,15 @@ is_cat <- function(x, ...) { # Other #### -omit <- function(pheno, omit){ +omit <- function(pheno, omit) { # Omit columns if (!is.null(omit)) { col_diff <- setdiff(omit, colnames(pheno)) if (length(col_diff) != 0L) { - warning("Columns to omit were not present: ", - paste(col_diff, collapse = ", "), ".") + warning( + "Columns to omit were not present: ", + paste(col_diff, collapse = ", "), "." + ) } pheno[, !colnames(pheno) %in% omit, drop = FALSE] } else { @@ -99,7 +101,7 @@ empty_res <- function(pheno, num) { column <- colnames(pheno) } if (sum(!num) > 1) { - ncol <- ncol +1 + ncol <- ncol + 1 column <- c(column, "mix_cat") } @@ -110,16 +112,16 @@ empty_res <- function(pheno, num) { diff } -valid_sizes <- function(size_data, size_subset, batches){ +valid_sizes <- function(size_data, size_subset, batches) { n_batch_max <- optimum_batches(size_data, size_subset) size_batch_max <- optimum_subset(size_data, batches) - if (size_subset >= size_batch_max && batches >= n_batch_max && size_subset*batches >= size_data) { + if (size_subset >= size_batch_max && batches >= n_batch_max && size_subset * batches >= size_data) { return(TRUE) } FALSE } -is_logical <- function(x){ +is_logical <- function(x) { isTRUE(x) || isFALSE(x) } @@ -138,13 +140,15 @@ mean_difference <- function(differences, subset_ind, eval_n) { apply(differences, 3, function(x, eval, indep) { x <- rbind(x, "independence" = 0) x <- insert(x, indep, name = "independence") - colSums(x, na.rm = TRUE)/eval + colSums(x, na.rm = TRUE) / eval }, eval = eval_n, indep = subset_ind) } release_bullets <- function() { - c("Update codemeta.json with: `codemetar::write_codemeta()`", - "Run: cffr::cff_write()") + c( + "Update codemeta.json with: `codemetar::write_codemeta()`", + "Run: cffr::cff_write()" + ) } # Numbers are evaluated 4 times: mean, sd, mad, na. @@ -192,4 +196,4 @@ consistent_index <- function(i, pheno) { stop("Index is shorter than the data provided.", call. = FALSE) } TRUE -} \ No newline at end of file +} diff --git a/README.Rmd b/README.Rmd index 98d2605..daa8c4d 100644 --- a/README.Rmd +++ b/README.Rmd @@ -61,7 +61,7 @@ We can use the survey dataset for the examples: ```{r show} library("experDesign") -data(survey, package = "MASS") +data(survey, package = "MASS") head(survey) ``` diff --git a/man/check_data.Rd b/man/check_data.Rd index 81649d4..0460f58 100644 --- a/man/check_data.Rd +++ b/man/check_data.Rd @@ -25,7 +25,7 @@ rdata <- expand.grid(sex = c("M", "F"), class = c("lower", "median", "high")) rdata2 <- rbind(rdata, rdata) check_data(rdata2) \donttest{ -#Different warnings +# Different warnings check_data(rdata) check_data(rdata[-c(1, 3), ]) data(survey, package = "MASS") diff --git a/man/check_index.Rd b/man/check_index.Rd index 62c27ad..f27a5bc 100644 --- a/man/check_index.Rd +++ b/man/check_index.Rd @@ -25,8 +25,10 @@ distribution, so it is a better randomization. } \examples{ index <- create_subset(50, 24) -metadata <- expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50), - sex = c("Male","Female")) +metadata <- expand.grid( + height = seq(60, 80, 5), weight = seq(100, 300, 50), + sex = c("Male", "Female") +) check_index(metadata, index) } \seealso{ diff --git a/man/compare_index.Rd b/man/compare_index.Rd index 5ddeaf6..68b739c 100644 --- a/man/compare_index.Rd +++ b/man/compare_index.Rd @@ -22,8 +22,10 @@ Compare the distribution of samples with two different batches. \examples{ index1 <- create_subset(50, 24) index2 <- batch_names(create_subset(50, 24)) -metadata <- expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50), - sex = c("Male","Female")) +metadata <- expand.grid( + height = seq(60, 80, 5), weight = seq(100, 300, 50), + sex = c("Male", "Female") +) compare_index(metadata, index1, index2) } \seealso{ diff --git a/man/design.Rd b/man/design.Rd index 1b2f58b..0ae611f 100644 --- a/man/design.Rd +++ b/man/design.Rd @@ -28,8 +28,10 @@ categorical data. } \examples{ data(survey, package = "MASS") -index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10) +index <- design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 +) index } \seealso{ diff --git a/man/distribution.Rd b/man/distribution.Rd index 41c5e89..6c9112e 100644 --- a/man/distribution.Rd +++ b/man/distribution.Rd @@ -23,8 +23,10 @@ Aimed for categorical variables. data(survey, package = "MASS") columns <- c("Sex", "Age", "Smoke") nas <- c(137, 70) # Omit rows with NA to avoid warnings in design -index <- design(pheno = survey[-nas, columns], size_subset = 70, - iterations = 10) +index <- design( + pheno = survey[-nas, columns], size_subset = 70, + iterations = 10 +) batches <- inspect(index, survey[-nas, columns]) distribution(batches, "Sex") distribution(batches, "Smoke") diff --git a/man/evaluate_entropy.Rd b/man/evaluate_entropy.Rd index 3d37836..fe5bed3 100644 --- a/man/evaluate_entropy.Rd +++ b/man/evaluate_entropy.Rd @@ -22,24 +22,26 @@ and thus omitted. } \examples{ data(survey, package = "MASS") -index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10) +index <- design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 +) # Note that numeric columns will be omitted: evaluate_entropy(index, survey[, c("Sex", "Smoke", "Age")]) } \seealso{ -Other functions to evaluate samples: -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_index}()}, -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_orig}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate samples: +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_index]{evaluate_index()}}, +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_orig]{evaluate_orig()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} -Other functions to evaluate categories: -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_na}()} +Other functions to evaluate categories: +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_na]{evaluate_na()}} } \concept{functions to evaluate categories} \concept{functions to evaluate samples} diff --git a/man/evaluate_independence.Rd b/man/evaluate_independence.Rd index b4e6edf..5f02621 100644 --- a/man/evaluate_independence.Rd +++ b/man/evaluate_independence.Rd @@ -20,24 +20,26 @@ Looks the independence between the categories and the batches. } \examples{ data(survey, package = "MASS") -index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10) +index <- design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 +) # Note that numeric columns will be omitted: evaluate_independence(index, survey[, c("Sex", "Smoke", "Age")]) } \seealso{ -Other functions to evaluate samples: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_index}()}, -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_orig}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate samples: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_index]{evaluate_index()}}, +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_orig]{evaluate_orig()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} -Other functions to evaluate categories: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_na}()} +Other functions to evaluate categories: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_na]{evaluate_na()}} } \concept{functions to evaluate categories} \concept{functions to evaluate samples} diff --git a/man/evaluate_index.Rd b/man/evaluate_index.Rd index e1b6e4a..7fe8b9a 100644 --- a/man/evaluate_index.Rd +++ b/man/evaluate_index.Rd @@ -28,13 +28,13 @@ ev_index["entropy", , ] \seealso{ If you have already an index you can use \code{\link[=use_index]{use_index()}}. -Other functions to evaluate samples: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_orig}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate samples: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_orig]{evaluate_orig()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} } \concept{functions to evaluate samples} diff --git a/man/evaluate_mad.Rd b/man/evaluate_mad.Rd index 51ee98a..a5e5355 100644 --- a/man/evaluate_mad.Rd +++ b/man/evaluate_mad.Rd @@ -20,25 +20,27 @@ Looks for the median absolute deviation values in each subgroup. } \examples{ data(survey, package = "MASS") -index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10) +index <- design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 +) # Note that categorical columns will be omitted: evaluate_mad(index, survey[, c("Sex", "Smoke", "Age")]) } \seealso{ -Other functions to evaluate samples: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_index}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_orig}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate samples: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_index]{evaluate_index()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_orig]{evaluate_orig()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} -Other functions to evaluate numbers: -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate numbers: +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} } \concept{functions to evaluate numbers} \concept{functions to evaluate samples} diff --git a/man/evaluate_mean.Rd b/man/evaluate_mean.Rd index 85202f2..a39d47b 100644 --- a/man/evaluate_mean.Rd +++ b/man/evaluate_mean.Rd @@ -19,25 +19,27 @@ Looks for the mean of the numeric values } \examples{ data(survey, package = "MASS") -index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10) +index <- design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 +) # Note that categorical columns will be omitted: evaluate_mean(index, survey[, c("Sex", "Smoke", "Age")]) } \seealso{ -Other functions to evaluate samples: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_index}()}, -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_orig}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate samples: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_index]{evaluate_index()}}, +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_orig]{evaluate_orig()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} -Other functions to evaluate numbers: -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate numbers: +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} } \concept{functions to evaluate numbers} \concept{functions to evaluate samples} diff --git a/man/evaluate_na.Rd b/man/evaluate_na.Rd index 59f981e..0375b00 100644 --- a/man/evaluate_na.Rd +++ b/man/evaluate_na.Rd @@ -25,23 +25,23 @@ i <- create_subset(samples, 3, 4) # random subsets evaluate_na(i, m) } \seealso{ -Other functions to evaluate samples: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_index}()}, -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_orig}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate samples: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_index]{evaluate_index()}}, +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_orig]{evaluate_orig()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} -Other functions to evaluate categories: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_independence}()} +Other functions to evaluate categories: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_independence]{evaluate_independence()}} -Other functions to evaluate numbers: -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate numbers: +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} } \concept{functions to evaluate categories} \concept{functions to evaluate numbers} diff --git a/man/evaluate_orig.Rd b/man/evaluate_orig.Rd index 79dd91e..eacd261 100644 --- a/man/evaluate_orig.Rd +++ b/man/evaluate_orig.Rd @@ -22,13 +22,13 @@ data(survey, package = "MASS") evaluate_orig(survey[, c("Sex", "Age", "Smoke")]) } \seealso{ -Other functions to evaluate samples: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_index}()}, -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_sd}()} +Other functions to evaluate samples: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_index]{evaluate_index()}}, +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_sd]{evaluate_sd()}} } \concept{functions to evaluate samples} diff --git a/man/evaluate_sd.Rd b/man/evaluate_sd.Rd index ba76716..81ae99e 100644 --- a/man/evaluate_sd.Rd +++ b/man/evaluate_sd.Rd @@ -20,25 +20,27 @@ Looks for the standard deviation of the numeric values } \examples{ data(survey, package = "MASS") -index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10) +index <- design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 +) # Note that categorical columns will be omitted: evaluate_sd(index, survey[, c("Sex", "Smoke", "Age")]) } \seealso{ -Other functions to evaluate samples: -\code{\link{evaluate_entropy}()}, -\code{\link{evaluate_independence}()}, -\code{\link{evaluate_index}()}, -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()}, -\code{\link{evaluate_orig}()} +Other functions to evaluate samples: +\code{\link[=evaluate_entropy]{evaluate_entropy()}}, +\code{\link[=evaluate_independence]{evaluate_independence()}}, +\code{\link[=evaluate_index]{evaluate_index()}}, +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}}, +\code{\link[=evaluate_orig]{evaluate_orig()}} -Other functions to evaluate numbers: -\code{\link{evaluate_mad}()}, -\code{\link{evaluate_mean}()}, -\code{\link{evaluate_na}()} +Other functions to evaluate numbers: +\code{\link[=evaluate_mad]{evaluate_mad()}}, +\code{\link[=evaluate_mean]{evaluate_mean()}}, +\code{\link[=evaluate_na]{evaluate_na()}} } \concept{functions to evaluate numbers} \concept{functions to evaluate samples} diff --git a/man/extreme_cases.Rd b/man/extreme_cases.Rd index ddbd387..5598e05 100644 --- a/man/extreme_cases.Rd +++ b/man/extreme_cases.Rd @@ -22,8 +22,10 @@ A vector with the number of the rows that are selected. Subset some samples that are mostly different. } \examples{ -metadata <- expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50), - sex = c("Male","Female")) +metadata <- expand.grid( + height = seq(60, 80, 5), weight = seq(100, 300, 50), + sex = c("Male", "Female") +) sel <- extreme_cases(metadata, 10) # We can see that it selected both Female and Males and wide range of height # and weight: diff --git a/man/follow_up2.Rd b/man/follow_up2.Rd index c6814d9..4146d46 100644 --- a/man/follow_up2.Rd +++ b/man/follow_up2.Rd @@ -14,7 +14,7 @@ Each row is a sample.} If NA it is interpreted as a new data, if not empty it is considered a batch.} \item{...}{ - Arguments passed on to \code{\link[=design]{design}} + Arguments passed on to \code{\link{design}} \describe{ \item{\code{size_subset}}{Numeric value of the number of sample per batch.} \item{\code{omit}}{Name of the columns of the \code{pheno} that will be omitted.} @@ -43,7 +43,7 @@ survey1 <- survey[seq_len(first_batch_n), variables] index1 <- design(survey1, size_subset = 50, iterations = 10) r_survey <- inspect(index1, survey1) # Create the second batch with "new" students -survey2 <- survey[seq(from = first_batch_n +1, to = nrow(survey)), variables] +survey2 <- survey[seq(from = first_batch_n + 1, to = nrow(survey)), variables] survey2$batch <- NA # Prepare the follow up all_classroom <- rbind(r_survey, survey2) diff --git a/man/inspect.Rd b/man/inspect.Rd index a289b2c..5cbeda6 100644 --- a/man/inspect.Rd +++ b/man/inspect.Rd @@ -24,8 +24,10 @@ Given the index and the data of the samples append the batch assignment \examples{ data(survey, package = "MASS") columns <- c("Sex", "Age", "Smoke") -index <- design(pheno = survey[, columns], size_subset = 70, - iterations = 10) +index <- design( + pheno = survey[, columns], size_subset = 70, + iterations = 10 +) batches <- inspect(index, survey[, columns]) head(batches) } diff --git a/man/replicates.Rd b/man/replicates.Rd index 2325ecc..e5c6b09 100644 --- a/man/replicates.Rd +++ b/man/replicates.Rd @@ -30,8 +30,10 @@ It uses the most different samples as controls as defined with \code{\link[=extr To control for variance replicates are important, see for example \url{https://www.nature.com/articles/nmeth.3091}. } \examples{ -samples <- data.frame(L = letters[1:25], Age = rnorm(25), - type = sample(LETTERS[1:5], 25, TRUE)) +samples <- data.frame( + L = letters[1:25], Age = rnorm(25), + type = sample(LETTERS[1:5], 25, TRUE) +) index <- replicates(samples, 5, controls = 2, omit = "L", iterations = 10) head(index) } diff --git a/man/spatial.Rd b/man/spatial.Rd index 849591a..63611b4 100644 --- a/man/spatial.Rd +++ b/man/spatial.Rd @@ -39,8 +39,10 @@ a plate with a grid scheme. } \examples{ data(survey, package = "MASS") -index <- design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10) +index <- design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 +) index2 <- spatial(index, survey[, c("Sex", "Smoke", "Age")], iterations = 10) head(index2) } diff --git a/man/valid_followup.Rd b/man/valid_followup.Rd index abfc2a2..c733d2c 100644 --- a/man/valid_followup.Rd +++ b/man/valid_followup.Rd @@ -40,7 +40,7 @@ survey1 <- survey[1:118, ] survey2 <- survey[119:nrow(survey), ] valid_followup(survey1, survey2) survey$batch <- NA -survey$batch[1:118] <- "old" +survey$batch[1:118] <- "old" valid_followup(all_data = survey) } \seealso{ diff --git a/tests/spelling.R b/tests/spelling.R index b9fa045..c2600f1 100644 --- a/tests/spelling.R +++ b/tests/spelling.R @@ -1,2 +1,3 @@ -if (requireNamespace('spelling', quietly = TRUE)) - spelling::spell_check_test(vignettes = TRUE, error = FALSE, skip_on_cran = TRUE) +if (requireNamespace("spelling", quietly = TRUE)) { + spelling::spell_check_test(vignettes = TRUE, error = FALSE, skip_on_cran = TRUE) +} diff --git a/tests/testthat/test-batch_names.R b/tests/testthat/test-batch_names.R index e4d3d62..b7b13d7 100644 --- a/tests/testthat/test-batch_names.R +++ b/tests/testthat/test-batch_names.R @@ -6,7 +6,7 @@ test_that("batch_names works", { expect_true(all(batch[index[[2]]] == names(index)[2])) }) -test_that("batch_names and use_index are the opposite" ,{ +test_that("batch_names and use_index are the opposite", { plates <- c("P1", "P2", "P1", "P2", "P2", "P3", "P1", "P3", "P1", "P1") expect_equal(batch_names(use_index(plates)), plates) }) diff --git a/tests/testthat/test-check_data.R b/tests/testthat/test-check_data.R index 0c61497..b981042 100644 --- a/tests/testthat/test-check_data.R +++ b/tests/testthat/test-check_data.R @@ -1,16 +1,19 @@ -test_that("check_data works", { - - data <- expand.grid(sex = c("M", "F"), class = c("lower", "median", "high")) - expect_warning(out <- check_data(data), " i.e. just one sample.") - expect_false(out) - - expect_warning( - expect_warning( - expect_warning(out <- check_data(data[-c(1, 3), ]), - "don't have all combinations"), - " category with just one sample"), - "no replicates; i.e. just one sample") - expect_false(out) - data2 <- rbind(data, data) - expect_true(expect_warning(check_data(data2), NA)) -}) +test_that("check_data works", { + data <- expand.grid(sex = c("M", "F"), class = c("lower", "median", "high")) + expect_warning(out <- check_data(data), " i.e. just one sample.") + expect_false(out) + + expect_warning( + expect_warning( + expect_warning( + out <- check_data(data[-c(1, 3), ]), + "don't have all combinations" + ), + " category with just one sample" + ), + "no replicates; i.e. just one sample" + ) + expect_false(out) + data2 <- rbind(data, data) + expect_true(expect_warning(check_data(data2), NA)) +}) diff --git a/tests/testthat/test-compare_index.R b/tests/testthat/test-compare_index.R index fd9403b..67440fe 100644 --- a/tests/testthat/test-compare_index.R +++ b/tests/testthat/test-compare_index.R @@ -1,12 +1,14 @@ -test_that("compare_index works", { - index1 <- create_subset(50, 24) - index2 <- batch_names(create_subset(50, 24)) - metadata <- expand.grid(height = seq(60, 80, 5), weight = seq(100, 300, 50), - sex = c("Male","Female")) - ci <- compare_index(metadata, index1, index2) - expect_equal(dim(ci), c(3, 3)) - - metadata$index <- batch_names(index1) - ci <- compare_index(metadata, "index", index2) - expect_equal(dim(ci), c(3, 3)) -}) +test_that("compare_index works", { + index1 <- create_subset(50, 24) + index2 <- batch_names(create_subset(50, 24)) + metadata <- expand.grid( + height = seq(60, 80, 5), weight = seq(100, 300, 50), + sex = c("Male", "Female") + ) + ci <- compare_index(metadata, index1, index2) + expect_equal(dim(ci), c(3, 3)) + + metadata$index <- batch_names(index1) + ci <- compare_index(metadata, "index", index2) + expect_equal(dim(ci), c(3, 3)) +}) diff --git a/tests/testthat/test-consistent_index.R b/tests/testthat/test-consistent_index.R index b30f92e..44b7108 100644 --- a/tests/testthat/test-consistent_index.R +++ b/tests/testthat/test-consistent_index.R @@ -1,7 +1,9 @@ test_that("consitent_index works", { data(survey, package = "MASS") - index <- expect_warning(design(survey[, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 10)) + index <- expect_warning(design(survey[, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 10 + )) # Test error on index larger than data: # FIXME: what with replicates? expect_error(consistent_index(index, survey[1:40, columns])) diff --git a/tests/testthat/test-design.R b/tests/testthat/test-design.R index 0c0bd78..a85416b 100644 --- a/tests/testthat/test-design.R +++ b/tests/testthat/test-design.R @@ -1,37 +1,48 @@ -test_that("design works", { - set.seed(44) - samples <- data.frame(L = letters[1:25], Age = rnorm(25), - type = sample(LETTERS[1:5], 25, TRUE)) - index <- design(samples, omit = "L", 5, iterations = 10) - expect_length(index, 5L) - expect_equal(lengths(index), c("SubSet1" = 5L, - "SubSet2" = 5L, - "SubSet3" = 5L, - "SubSet4" = 5L, - "SubSet5" = 5L)) -}) - - -test_that("design works with previous batches", { - set.seed(55) - samples <- data.frame(L = letters[1:25], Age = rnorm(25), - type = sample(LETTERS[1:5], 25, TRUE)) - i0 <- c("A" = 13, "B" = 12) - expect_warning(i <- design(samples, size_subset = i0, iterations = 10), - "some problems") - expect_equal(lengths(i), i0) - - # Unamed size_subsets but multiple names - i1 <- i0 - names(i1) <- NULL - expect_warning(i <- design(samples, size_subset = i1, - iterations = 10, name = names(i0))) - expect_equal(lengths(i), i0) - - # Unamed size_subsets but multiple names - i1 <- i0 - names(i1) <- NULL - expect_error(expect_warning(design(samples, size_subset = i1, - iterations = 10, name = LETTERS[1:3]))) - -}) +test_that("design works", { + set.seed(44) + samples <- data.frame( + L = letters[1:25], Age = rnorm(25), + type = sample(LETTERS[1:5], 25, TRUE) + ) + index <- design(samples, omit = "L", 5, iterations = 10) + expect_length(index, 5L) + expect_equal(lengths(index), c( + "SubSet1" = 5L, + "SubSet2" = 5L, + "SubSet3" = 5L, + "SubSet4" = 5L, + "SubSet5" = 5L + )) +}) + + +test_that("design works with previous batches", { + set.seed(55) + samples <- data.frame( + L = letters[1:25], Age = rnorm(25), + type = sample(LETTERS[1:5], 25, TRUE) + ) + i0 <- c("A" = 13, "B" = 12) + expect_warning( + i <- design(samples, size_subset = i0, iterations = 10), + "some problems" + ) + expect_equal(lengths(i), i0) + + # Unamed size_subsets but multiple names + i1 <- i0 + names(i1) <- NULL + expect_warning(i <- design(samples, + size_subset = i1, + iterations = 10, name = names(i0) + )) + expect_equal(lengths(i), i0) + + # Unamed size_subsets but multiple names + i1 <- i0 + names(i1) <- NULL + expect_error(expect_warning(design(samples, + size_subset = i1, + iterations = 10, name = LETTERS[1:3] + ))) +}) diff --git a/tests/testthat/test-distribution.R b/tests/testthat/test-distribution.R index 317aaa9..2989352 100644 --- a/tests/testthat/test-distribution.R +++ b/tests/testthat/test-distribution.R @@ -3,8 +3,10 @@ test_that("distribution works", { set.seed(4565) columns <- c("Sex", "Age", "Smoke") nas <- c(137, 70) - index <- design(pheno = survey[-nas, columns], size_subset = 70, - iterations = 10) + index <- design( + pheno = survey[-nas, columns], size_subset = 70, + iterations = 10 + ) batches <- inspect(index, survey[-nas, columns]) expect_true(distribution(batches, "Sex")) expect_true(distribution(batches, "Smoke")) diff --git a/tests/testthat/test-evaluate_entropy.R b/tests/testthat/test-evaluate_entropy.R index 97f31fa..5a3bab5 100644 --- a/tests/testthat/test-evaluate_entropy.R +++ b/tests/testthat/test-evaluate_entropy.R @@ -1,10 +1,12 @@ -test_that("evaluate_entropy works", { - data(survey, package = "MASS") - nas <- c(137, 70) - index <- design(survey[-nas, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 50) - # Note that numeric columns will be omitted: - out <- evaluate_entropy(index, survey[, c("Sex", "Smoke", "Age")]) - expect_length(out, 2L) - expect_equal(names(out), c("Sex", "Smoke")) -}) +test_that("evaluate_entropy works", { + data(survey, package = "MASS") + nas <- c(137, 70) + index <- design(survey[-nas, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 50 + ) + # Note that numeric columns will be omitted: + out <- evaluate_entropy(index, survey[, c("Sex", "Smoke", "Age")]) + expect_length(out, 2L) + expect_equal(names(out), c("Sex", "Smoke")) +}) diff --git a/tests/testthat/test-evaluate_index.R b/tests/testthat/test-evaluate_index.R index ecaf6de..19d980b 100644 --- a/tests/testthat/test-evaluate_index.R +++ b/tests/testthat/test-evaluate_index.R @@ -1,12 +1,16 @@ -test_that("works" ,{ - set.seed(43) - samples <- data.frame(L = letters[1:25], Age = rnorm(25), - type = sample(LETTERS[1:5], 25, TRUE)) - i <- create_subset(25, 5, 5) - arr <- evaluate_index(i, samples[, -1]) - expect_equal(arr["entropy", "Age",], c("SubSet1" = 0, - "SubSet2" = 0, - "SubSet3" = 0, - "SubSet4" = 0, - "SubSet5" = 0)) -}) +test_that("works", { + set.seed(43) + samples <- data.frame( + L = letters[1:25], Age = rnorm(25), + type = sample(LETTERS[1:5], 25, TRUE) + ) + i <- create_subset(25, 5, 5) + arr <- evaluate_index(i, samples[, -1]) + expect_equal(arr["entropy", "Age", ], c( + "SubSet1" = 0, + "SubSet2" = 0, + "SubSet3" = 0, + "SubSet4" = 0, + "SubSet5" = 0 + )) +}) diff --git a/tests/testthat/test-extreme_cases.R b/tests/testthat/test-extreme_cases.R index 4ee4d11..c325b9c 100644 --- a/tests/testthat/test-extreme_cases.R +++ b/tests/testthat/test-extreme_cases.R @@ -1,20 +1,21 @@ -test_that("extreme_cases works", { - set.seed(456) - surv <- MASS::survey - samples <- extreme_cases(surv, size = 119, iterations = 10) - expect_length(samples, 119L) -}) - -test_that("check_index works", { - set.seed(456) - surv <- MASS::survey - nas <- c(137, 70) - index <- create_subset(size_data = nrow(surv)-length(nas), n = 2) - out <- check_index(surv[-nas, c("Sex", "Smoke", "Age")], index) - expect_equal(dim(out), c(4L, 2L)) - - i2 <- design(surv[-nas, c("Sex", "Smoke", "Age")], - size_subset = 119, iterations = 100) - o2 <- check_index(surv[-nas, c("Sex", "Smoke", "Age")], i2) - expect_true(sum(out) >= sum(o2)) -}) +test_that("extreme_cases works", { + set.seed(456) + surv <- MASS::survey + samples <- extreme_cases(surv, size = 119, iterations = 10) + expect_length(samples, 119L) +}) + +test_that("check_index works", { + set.seed(456) + surv <- MASS::survey + nas <- c(137, 70) + index <- create_subset(size_data = nrow(surv) - length(nas), n = 2) + out <- check_index(surv[-nas, c("Sex", "Smoke", "Age")], index) + expect_equal(dim(out), c(4L, 2L)) + + i2 <- design(surv[-nas, c("Sex", "Smoke", "Age")], + size_subset = 119, iterations = 100 + ) + o2 <- check_index(surv[-nas, c("Sex", "Smoke", "Age")], i2) + expect_true(sum(out) >= sum(o2)) +}) diff --git a/tests/testthat/test-follow_up.R b/tests/testthat/test-follow_up.R index 3ce6085..b26376c 100644 --- a/tests/testthat/test-follow_up.R +++ b/tests/testthat/test-follow_up.R @@ -26,10 +26,14 @@ test_that("follow_up2 works", { expect_warning( expect_warning( fu1 <- follow_up2(survey, iterations = 10), - "with the data."), - "with the new samples"), - "some problems with the new data."), - "some problems with the old data.") + "with the data." + ), + "with the new samples" + ), + "some problems with the new data." + ), + "some problems with the old data." + ) expect_type(fu1, "character") # old vs new with confounding effects survey$batch <- ifelse(survey$Clap %in% "Right", "old", NA) @@ -91,7 +95,7 @@ test_that("valid_followup works", { ) expect_false(out) survey$batch <- NA - survey$batch[1:118] <- "old" + survey$batch[1:118] <- "old" expect_warning( expect_warning( diff --git a/tests/testthat/test-indexing.R b/tests/testthat/test-indexing.R index 229e12f..8928953 100644 --- a/tests/testthat/test-indexing.R +++ b/tests/testthat/test-indexing.R @@ -2,8 +2,10 @@ test_that("sizes_batches works", { out <- sizes_batches(size_data = 237, size_subset = 60, batches = 4) expect_equal(out, c(60, 59, 59, 59)) - expect_error(sizes_batches(size_data = 237, size_subset = 59, batches = 4), - "batches or size_subset is too small to fit all the samples.") + expect_error( + sizes_batches(size_data = 237, size_subset = 59, batches = 4), + "batches or size_subset is too small to fit all the samples." + ) }) test_that("create_index4index works", { @@ -26,11 +28,13 @@ test_that("translate_index works", { test_that("translate_index works with duplicated output", { new_rows <- 1:60 - old_rows <- c(3L, 9L, 10L, 16L, 17L, 20L, 27L, 28L, 30L, 31L, 32L, 33L, 34L, - 35L, 40L, 41L, 42L, 44L, 45L, 48L, 49L, 4L, 5L, 7L, 8L, 9L, 10L, - 13L, 15L, 18L, 21L, 22L, 25L, 26L, 32L, 33L, 34L, 36L, 37L, 38L, - 39L, 47L, 1L, 2L, 6L, 9L, 10L, 11L, 12L, 14L, 19L, 23L, 24L, - 29L, 32L, 33L, 34L, 43L, 46L, 50L) + old_rows <- c( + 3L, 9L, 10L, 16L, 17L, 20L, 27L, 28L, 30L, 31L, 32L, 33L, 34L, + 35L, 40L, 41L, 42L, 44L, 45L, 48L, 49L, 4L, 5L, 7L, 8L, 9L, 10L, + 13L, 15L, 18L, 21L, 22L, 25L, 26L, 32L, 33L, 34L, 36L, 37L, 38L, + 39L, 47L, 1L, 2L, 6L, 9L, 10L, 11L, 12L, 14L, 19L, 23L, 24L, + 29L, 32L, 33L, 34L, 43L, 46L, 50L + ) i <- list( A1 = c(3L, 4L, 1L), B1 = c(9L, 39L, 34L), @@ -47,7 +51,7 @@ test_that("translate_index works with duplicated output", { A3 = 34L, B3 = c(35L, 18L, 12L), C3 = c(40L, 21L, 14L), - D3 = c(41L,22L, 19L), + D3 = c(41L, 22L, 19L), E3 = c(42L, 25L, 23L), F3 = c(44L, 26L, 24L), A4 = c(45L, 32L), @@ -66,8 +70,10 @@ test_that("translate_index works with duplicated output", { old_rows[2] <- 46 new_rows <- seq_len(45) ti <- translate_index(index, old_rows, new_rows) - expect_equal(length(unlist(index, FALSE, FALSE)), - length(unlist(ti, FALSE, FALSE))) + expect_equal( + length(unlist(index, FALSE, FALSE)), + length(unlist(ti, FALSE, FALSE)) + ) }) test_that("translate_index works with normal index", { diff --git a/tests/testthat/test-insert.R b/tests/testthat/test-insert.R index f68ec4b..311455c 100644 --- a/tests/testthat/test-insert.R +++ b/tests/testthat/test-insert.R @@ -1,12 +1,16 @@ -test_that("insert works", { - m <- matrix(0, ncol = 2, nrow = 2, - dimnames = list(LETTERS[1:2], letters[1:2])) - vec <- c("a" = 1) - i <- insert(m, vec, "A") - expect_equal(i["A", "a"], 1L) - m2 <- matrix(1, ncol = 2, nrow = 1, - dimnames = list(LETTERS[1], letters[1:2])) - i <- insert(m, m2, "A") - expect_equal(i["A", "a"], 1L) - expect_equal(i["A", "b"], 1L) -}) +test_that("insert works", { + m <- matrix(0, + ncol = 2, nrow = 2, + dimnames = list(LETTERS[1:2], letters[1:2]) + ) + vec <- c("a" = 1) + i <- insert(m, vec, "A") + expect_equal(i["A", "a"], 1L) + m2 <- matrix(1, + ncol = 2, nrow = 1, + dimnames = list(LETTERS[1], letters[1:2]) + ) + i <- insert(m, m2, "A") + expect_equal(i["A", "a"], 1L) + expect_equal(i["A", "b"], 1L) +}) diff --git a/tests/testthat/test-inspect.R b/tests/testthat/test-inspect.R index 5dfbcd0..8acf655 100644 --- a/tests/testthat/test-inspect.R +++ b/tests/testthat/test-inspect.R @@ -2,8 +2,10 @@ test_that("inspect works", { data(survey, package = "MASS") columns <- c("Sex", "Age", "Smoke") nas <- c(137, 70) - index <- design(pheno = survey[-nas, columns], size_subset = 70, - iterations = 10) + index <- design( + pheno = survey[-nas, columns], size_subset = 70, + iterations = 10 + ) batches <- inspect(index, survey[-nas, columns]) expect_true("batch" %in% colnames(batches)) @@ -16,13 +18,16 @@ test_that("inspect works with replicates", { data(survey, package = "MASS") nas <- c(137, 70) columns <- c("Sex", "Age", "Smoke") - index <- replicates(pheno = survey[-nas, columns], size_subset = 70, controls = 5, - iterations = 10) + index <- replicates( + pheno = survey[-nas, columns], size_subset = 70, controls = 5, + iterations = 10 + ) survey$sample <- seq_len(nrow(survey)) batches <- inspect(index, survey[-nas, ]) t_samples <- table(batches$sample) expect_equal(table(t_samples), structure(c(`1` = 230L, `4` = 5L), dim = 2L, dimnames = list( - t_samples = c("1", "4")), class = "table")) + t_samples = c("1", "4") + ), class = "table")) # TODO check order is right for the samples k <- batches$sample %in% names(t_samples[t_samples == 4]) @@ -33,23 +38,29 @@ test_that("inspect with translate_index", { data(survey, package = "MASS") nas <- c(137, 70) columns <- c("Sex", "Age", "Smoke") - index1 <- replicates(survey[-nas, columns], size_subset = 50, - iterations = 25, controls = 15) + index1 <- replicates(survey[-nas, columns], + size_subset = 50, + iterations = 25, controls = 15 + ) index2 <- spatial(index1, survey[-nas, columns], iterations = 25) i1 <- inspect(index1, survey[-nas, columns]) i2 <- inspect(index2, i1, index_name = "spatial") expect_true(all(table(i2$batch, i2$spatial) <= 1)) }) - test_that("Warning on duplidate names", { +test_that("Warning on duplidate names", { data(survey, package = "MASS") columns <- c("Sex", "Age", "Smoke") - expect_warning(index <- design(pheno = survey[, columns], size_subset = 70, - iterations = 10)) + expect_warning(index <- design( + pheno = survey[, columns], size_subset = 70, + iterations = 10 + )) batches <- inspect(index, survey[, columns]) - expect_warning(index2 <- design(pheno = batches, size_subset = 70, - iterations = 10)) + expect_warning(index2 <- design( + pheno = batches, size_subset = 70, + iterations = 10 + )) expect_warning(inspect(index2, batches)) expect_no_warning(inspect(index2, batches, index_name = "batch2")) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test-optimum.R b/tests/testthat/test-optimum.R index 70e75e7..5d9e9dd 100644 --- a/tests/testthat/test-optimum.R +++ b/tests/testthat/test-optimum.R @@ -14,8 +14,10 @@ test_that("sizes_batches", { expect_length(sizes_batches(size_data, size_batch, batches), batches) expect_true(all(sizes_batches(size_data, size_batch, batches) <= size_batch)) batches <- 1 - expect_error(sizes_batches(size_data, size_batch, batches), - "'batches' must be a single number bigger than 1.") + expect_error( + sizes_batches(size_data, size_batch, batches), + "'batches' must be a single number bigger than 1." + ) }) test_that("internal_batches", { diff --git a/tests/testthat/test-replicates.R b/tests/testthat/test-replicates.R index 657416a..8a7cc2d 100644 --- a/tests/testthat/test-replicates.R +++ b/tests/testthat/test-replicates.R @@ -1,7 +1,9 @@ test_that("replicates works", { set.seed(45) - samples <- data.frame(L = letters[1:25], Age = rnorm(25), - type = sample(LETTERS[1:5], 25, TRUE)) + samples <- data.frame( + L = letters[1:25], Age = rnorm(25), + type = sample(LETTERS[1:5], 25, TRUE) + ) index <- replicates(samples, 5, omit = "L", controls = 2, iterations = 10) expect_length(index, 9L) @@ -13,6 +15,8 @@ test_that("Setting 0 replicates is an error", { data(survey, package = "MASS") set.seed(4568) nas <- c(137, 70) - expect_error(replicates(survey[-nas, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 25, controls = 0)) + expect_error(replicates(survey[-nas, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 25, controls = 0 + )) }) diff --git a/tests/testthat/test-simplify2matrix.R b/tests/testthat/test-simplify2matrix.R index ddfdcff..d3fcc03 100644 --- a/tests/testthat/test-simplify2matrix.R +++ b/tests/testthat/test-simplify2matrix.R @@ -1,5 +1,5 @@ -test_that("simplify2matrix works",{ - l <- list("a" = 1, "b" = 1) - m <- simplify2matrix(l) - expect_true(is(m, "matrix")) -}) +test_that("simplify2matrix works", { + l <- list("a" = 1, "b" = 1) + m <- simplify2matrix(l) + expect_true(is(m, "matrix")) +}) diff --git a/tests/testthat/test-spatial.R b/tests/testthat/test-spatial.R index 78a5c64..fc7454d 100644 --- a/tests/testthat/test-spatial.R +++ b/tests/testthat/test-spatial.R @@ -2,12 +2,15 @@ test_that("spatial works", { data(survey, package = "MASS") set.seed(21892) nas <- c(137, 70) - index <- design(survey[-nas, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 25) + index <- design(survey[-nas, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 25 + ) index2 <- spatial(index, survey[-nas, c("Sex", "Smoke", "Age")], - rows = LETTERS[1:9], - columns = 1:12, iterations = 25) - expect_lte(length(index2), 9*12) + rows = LETTERS[1:9], + columns = 1:12, iterations = 25 + ) + expect_lte(length(index2), 9 * 12) expect_true(all(names(index2) %in% position_name(rows = LETTERS[1:9], 1:12)$name)) expect_false(any(table(batch_names(index), batch_names(index2)) > 1)) }) @@ -16,8 +19,10 @@ test_that("spatial works with replicates", { data(survey, package = "MASS") set.seed(4568) nas <- c(137, 70) - index <- replicates(survey[-nas, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 25, controls = 15) + index <- replicates(survey[-nas, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 25, controls = 15 + ) index2 <- spatial(index, survey[-nas, c("Sex", "Smoke", "Age")], iterations = 25) expect_lte(length(index2), 50) expect_false(any(is.na(unlist(index2)))) @@ -31,8 +36,11 @@ test_that("spatial don't duplicate samples", { nas <- c(137, 70) index <- create_subset(NROW(survey[-nas, ]), size_subset = 50) index2 <- spatial(index, survey[-nas, c("Sex", "Smoke", "Age")], iterations = 25) - uniqueness <- vapply(index2, function(x){length(unique(x)) == length(x)}, - FUN.VALUE = logical(1L)) + uniqueness <- vapply(index2, function(x) { + length(unique(x)) == length(x) + }, + FUN.VALUE = logical(1L) + ) # Avoid placing the a sample twice in a position expect_false(any(!uniqueness)) # Avoid placing a sample in the same well in the same subset @@ -43,12 +51,15 @@ test_that("Removing rows works", { data(survey, package = "MASS") set.seed(21829) nas <- c(137, 70) - index <- design(survey[-nas, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 25) + index <- design(survey[-nas, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 25 + ) index2 <- spatial(index, survey[-nas, c("Sex", "Smoke", "Age")], - rows = LETTERS[1:9], remove_positions = "B", - columns = 1:12, iterations = 25) - expect_lte(length(index2), 9*12) + rows = LETTERS[1:9], remove_positions = "B", + columns = 1:12, iterations = 25 + ) + expect_lte(length(index2), 9 * 12) expect_true(all(names(index2) %in% position_name(rows = LETTERS[1:9], 1:12)$name)) expect_false(any(table(batch_names(index), batch_names(index2)) > 1)) }) @@ -57,12 +68,15 @@ test_that("Removing columns works", { data(survey, package = "MASS") set.seed(21289) nas <- c(137, 70) - index <- design(survey[-nas, c("Sex", "Smoke", "Age")], size_subset = 50, - iterations = 25) + index <- design(survey[-nas, c("Sex", "Smoke", "Age")], + size_subset = 50, + iterations = 25 + ) index2 <- spatial(index, survey[-nas, c("Sex", "Smoke", "Age")], - rows = LETTERS[1:9], remove_positions = "A", - columns = 1:12, iterations = 25) - expect_lte(length(index2), 9*12) + rows = LETTERS[1:9], remove_positions = "A", + columns = 1:12, iterations = 25 + ) + expect_lte(length(index2), 9 * 12) expect_true(all(names(index2) %in% position_name(rows = LETTERS[1:9], 1:12)$name)) expect_false(any(table(batch_names(index), batch_names(index2)) > 1)) }) @@ -74,4 +88,4 @@ test_that("position_name works", { expect_equal(pn$row, rep(LETTERS[1:2], each = 2)) expect_equal(pn$column, rep(1:2, length.out = 4)) expect_equal(pn$name, as.factor(paste0(rep(LETTERS[1:2], each = 2), rep(1:2, length.out = 4)))) -}) \ No newline at end of file +}) diff --git a/vignettes/experdesign.Rmd b/vignettes/experdesign.Rmd index 4977020..4b496ed 100644 --- a/vignettes/experdesign.Rmd +++ b/vignettes/experdesign.Rmd @@ -80,9 +80,11 @@ Question in [Bioinformatics.SE](https://bioinformatics.stackexchange.com/q/4765/ Imagine you have some samples already collected and you want to distributed them in batches: ```{r experDesign_setup} library("experDesign") -metadata <- expand.grid(height = seq(60, 80, 5), - weight = seq(100, 300, 50), - sex = c("Male","Female")) +metadata <- expand.grid( + height = seq(60, 80, 5), + weight = seq(100, 300, 50), + sex = c("Male", "Female") +) head(metadata, 15) ``` @@ -178,9 +180,11 @@ but let's create an unbalanced dataset to check it. ```{r unbalanced} n <- 99 samples <- 100 -unbalanced <- data.frame(Classroom = rep(c("A", "B"), each = samples/2), - Sex = c(rep("M", n), rep("F", samples-n)), - Age = rnorm(samples, mean = 25, sd = 3)) +unbalanced <- data.frame( + Classroom = rep(c("A", "B"), each = samples / 2), + Sex = c(rep("M", n), rep("F", samples - n)), + Age = rnorm(samples, mean = 25, sd = 3) +) table(unbalanced[, 1:2]) ``` @@ -206,7 +210,7 @@ If you need a subset with the samples that are more diverse you can use the following function: ```{r QC} -data(survey, package = "MASS") +data(survey, package = "MASS") head(survey) samples <- extreme_cases(survey, size = 10) survey[samples, ] @@ -238,7 +242,7 @@ head(survey[, keep]) # Set a seed for reproducibility # Looking for groups at most of 70 samples. -index <- create_subset(nrow(survey), size_subset = 70) +index <- create_subset(nrow(survey), size_subset = 70) index ``` @@ -260,7 +264,7 @@ Then it compares to the previous index ```{r check_index2} score_index2 <- check_index(survey[, keep], index2) -sum(rowMeans(abs(score_index2-score_index1))) +sum(rowMeans(abs(score_index2 - score_index1))) ``` If this score is lower than the previous one the new index is kept.