Skip to content
Open
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions R/QC.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down
25 changes: 16 additions & 9 deletions R/designer.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -105,25 +108,29 @@ 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) {
stop("All the data can be done in one batch.", call. = FALSE)
}
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)
Expand Down
124 changes: 63 additions & 61 deletions R/entropy.R
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 11 additions & 10 deletions R/evaluate.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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))

}


Expand All @@ -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"
Expand All @@ -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)
}
Expand Down
Loading
Loading