Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve verify_data function #138

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -46,4 +46,4 @@ Config/testthat/edition: 3
Copyright: 'ggmice' authors
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
2 changes: 1 addition & 1 deletion R/ggmice.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
ggmice <- function(data = NULL,
mapping = ggplot2::aes()) {
# validate inputs
verify_data(data, df = TRUE, imp = TRUE)
verify_data(data, classes = c("data.frame", "mids"))
if (is.null(mapping$x) && is.null(mapping$y)) {
cli::cli_abort(
c(
Expand Down Expand Up @@ -107,8 +107,8 @@
.imp = 0,
.id = rownames(data$data),
data$data
)[!miss_xy,],

Check warning on line 110 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=110,col=20,[commas_linter] Commas should always have a space after.
data.frame(.where = "imputed", mice::complete(data, action = "long"))[where_xy,]

Check warning on line 111 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=111,col=88,[commas_linter] Commas should always have a space after.
),
.where = factor(
.where,
Expand All @@ -133,7 +133,7 @@

# edit plot to display missing values on the axes
if (is.data.frame(data) &&
!is.null(mapping$x) && !is.null(mapping$y)) {

Check warning on line 136 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=136,col=6,[indentation_linter] Indentation should be 8 spaces but is 6 spaces.
gg <- gg +
ggplot2::coord_cartesian(clip = "off")
if (!is.null(mapping$x)) {
Expand Down Expand Up @@ -187,14 +187,14 @@
mapping_out <- mapping_text
}
if ((mice::is.mids(data) &&
mapping_text %in% c(".id", ".imp", ".where"))) {

Check warning on line 190 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=190,col=7,[indentation_linter] Indentation should be 9 spaces but is 7 spaces.
mapping_out <- NULL
}
if (!is.null(mapping_in) &&
mapping_text %nin% c(vrbs, ".id", ".imp", ".where")) {

Check warning on line 194 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=194,col=6,[indentation_linter] Indentation should be 8 spaces but is 6 spaces.
mapping_out <- vrbs[stringr::str_detect(mapping_text, vrbs)]
if (identical(mapping_out, character(0)) ||
inherits(try(dplyr::mutate(mapping_data,

Check warning on line 197 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=197,col=8,[indentation_linter] Indentation should be 10 spaces but is 8 spaces.
!!rlang::parse_quo(mapping_text, env = rlang::current_env())),
silent = TRUE)
,
Expand Down
11 changes: 3 additions & 8 deletions R/plot_corr.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@
if (is.matrix(data) && ncol(data) > 1) {
data <- as.data.frame(data)
}
verify_data(data = data, df = TRUE)
verify_data(data = data, classes = "data.frame")
vrb <- substitute(vrb)
if (vrb != "all" && length(vrb) < 2) {
vrb <- vrb_to_cols(vrb, data)
if (length(vrb) < 2) {
cli::cli_abort("The number of variables should be two or more to compute correlations.")
}
if (vrb[1] == "all") {
vrb <- names(data)
} else {
data <- dplyr::select(data, {{vrb}})
vrb <- names(data)
}
# check if any column is constant
constants <- apply(data, MARGIN = 2, function(x) {
all(is.na(x)) || max(x, na.rm = TRUE) == min(x, na.rm = TRUE)
Expand Down Expand Up @@ -91,7 +86,7 @@
y = "Variable to impute",
fill = "Correlation*
",
caption = "*pairwise complete observations"

Check warning on line 89 in R/plot_corr.R

View workflow job for this annotation

GitHub Actions / lint

file=R/plot_corr.R,line=89,col=6,[indentation_linter] Indentation should be 10 spaces but is 6 spaces.
)
} else {
gg <- gg +
Expand Down
10 changes: 3 additions & 7 deletions R/plot_flux.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ plot_flux <-
vrb = "all",
label = TRUE,
caption = TRUE) {
verify_data(data, df = TRUE)
verify_data(data, classes = "data.frame")
vrb <- substitute(vrb)
if (vrb != "all" && length(vrb) < 2) {
vrb <- vrb_to_cols(vrb, data)
if (length(vrb) < 2) {
cli::cli_abort("The number of variables should be two or more to compute flux.")
}
if (vrb[1] == "all") {
vrb <- names(data)
} else {
vrb <- names(dplyr::select(data, {{vrb}}))
}
# plot in and outflux
flx <- mice::flux(data[, vrb])[, c("influx", "outflux")]
gg <-
Expand Down
10 changes: 3 additions & 7 deletions R/plot_pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ plot_pattern <-
if (is.matrix(data) && ncol(data) > 1) {
data <- as.data.frame(data)
}
verify_data(data, df = TRUE)
verify_data(data, classes = "data.frame")
vrb <- substitute(vrb)
if (vrb != "all" && length(vrb) < 2) {
vrb <- vrb_to_cols(vrb, data)
if (length(vrb) < 2) {
cli::cli_abort("The number of variables should be two or more to compute missing data patterns.")
}
if (vrb[1] == "all") {
vrb <- names(data)
} else {
vrb <- names(dplyr::select(as.data.frame(data), {{vrb}}))
}
if (".x" %in% vrb || ".y" %in% vrb) {
cli::cli_abort(
c(
Expand Down
8 changes: 2 additions & 6 deletions R/plot_pred.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plot_pred <-
label = TRUE,
square = TRUE,
rotate = FALSE) {
verify_data(data, pred = TRUE)
verify_data(data, classes = "matrix")
p <- nrow(data)
if (!is.null(method) && is.character(method)) {
if (length(method) == 1) {
Expand All @@ -38,11 +38,7 @@ plot_pred <-
cli::cli_abort("Method should be NULL or a character string or vector (of length 1 or `ncol(data)`).")
}
vrb <- substitute(vrb)
if (vrb[1] == "all") {
vrb <- names(data)
} else {
vrb <- names(dplyr::select(as.data.frame(data), {{vrb}}))
}
vrb <- vrb_to_cols(vrb, data)
vrbs <- row.names(data)
long <- data.frame(
vrb = 1:p,
Expand Down
4 changes: 2 additions & 2 deletions R/plot_trace.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' plot_trace(imp)
#' @export
plot_trace <- function(data, vrb = "all") {
verify_data(data, imp = TRUE)
verify_data(data, classes = "mids")
if (is.null(data$chainMean) && is.null(data$chainVar)) {
cli::cli_abort("No convergence diagnostics found", call. = FALSE)
}
Expand All @@ -20,9 +20,9 @@ plot_trace <- function(data, vrb = "all") {
sm <- sqrt(data$chainVar)

# select variable to plot from list of imputed variables
vrb <- substitute(vrb)
varlist <-
names(data$imp)[apply(!(is.nan(mn) | is.na(mn)), 1, all)]
vrb <- substitute(vrb)
if (as.character(vrb)[1] == "all") {
vrb <- varlist
} else {
Expand Down
96 changes: 28 additions & 68 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,42 @@
#' Utils function to validate data argument inputs
#'
#' @param data The input supplied to the 'data' argument.
#' @param df Logical indicating whether 'data.frame' inputs are permitted.
#' @param imp Logical indicating whether 'mids' inputs are permitted.
#' @param pred Logical indicating whether predictor matrix inputs are permitted.
#' @param classes String or character vector specifying which of the data types `data.frame`, `mids`, and/or `matrix` are allowed.
#'
#' @return Either nothing or an error.
#'
#' @keywords internal
#' @noRd
verify_data <- function(data,
df = FALSE,
imp = FALSE,
pred = FALSE) {
if (df && !imp) {
if (!(is.data.frame(data) || is.matrix(data))) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'data.frame' or 'matrix'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
classes) {
if(!rlang::inherits_any(data, classes)){

Check warning on line 33 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=33,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 33 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=33,col=42,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 33 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=33,col=42,[paren_body_linter] There should be a space between a right parenthesis and a body expression.
classes_format <- purrr::map(classes, function(x) paste0("`", x, "`")) %>% unlist() # format type names to be used in function
cli::cli_abort(c(
"!" = "The {.arg data} argument requires an object of class {stringr::str_flatten_comma({classes_format}, \", or \")}.",
"i" = "Input object is of class `{class(data)}`"
),
call. = FALSE)
}
if (df && imp) {
if (!(is.data.frame(data) ||
is.matrix(data) || mice::is.mids(data))) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'data.frame', 'matrix', or 'mids'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
}
if (imp && !df) {
if (!mice::is.mids(data)) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'mids'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
}
if (pred) {
if (!is.matrix(data)) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'matrix'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
if (dim(data)[1] != dim(data)[2]) {
cli::cli_abort(
c(
"The 'data' argument requires a square predictor matrix.",
"i" = "Input object has {dim(data)[1]} rows and {dim(data)[2]} columns."
),
call. = FALSE
)
}
if (is.null(rownames(data)) || is.null(colnames(data)) ||
!all.equal(rownames(data), colnames(data))) {
cli::cli_warn(
c(
"The 'data' argument expects a square predictor matrix with equal row and column names.",
"i" = "Try using `mice::make.predictorMatrix()` or `mice::quickpred()`."
),
call. = FALSE
)
}
}

#' Utils function to process variable vector as character vector
#'
#' @param vrb The input supplied to the `vrb` argument.
#' @param data The input supplied to the `data` argument.
#'
#' @return String with variable names
#'
#' @keywords internal
#' @noRd
vrb_to_cols <- function(vrb, data){
if (vrb[1] == "all") {
vrb <- colnames(data)
} else {
vrb <- as.data.frame(data) %>%
dplyr::select({{vrb}}) %>%
colnames()
}
return(vrb)
}

# suppress undefined global functions or variables note
Expand Down
Loading