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

add plot_pred compatibility with mids object and add test #132

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
14 changes: 12 additions & 2 deletions R/plot_pred.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Plot the predictor matrix of an imputation model
#'
#' @param data A predictor matrix for `mice`, typically generated with [mice::make.predictorMatrix] or [mice::quickpred].
#' @param data A predictor matrix for `mice`, typically generated with [mice::make.predictorMatrix] or [mice::quickpred], or an object of class [`mice::mids`].

Check warning on line 3 in R/plot_pred.R

View workflow job for this annotation

GitHub Actions / lint

file=R/plot_pred.R,line=3,col=151,[line_length_linter] Lines should not be more than 150 characters. This line is 159 characters.
#' @param vrb String, vector, or unquoted expression with variable name(s), default is "all".
#' @param method Character string or vector with imputation methods.
#' @param label Logical indicating whether predictor matrix values should be displayed.
Expand All @@ -20,7 +20,17 @@
label = TRUE,
square = TRUE,
rotate = FALSE) {
verify_data(data, pred = TRUE)
verify_data(data, pred = TRUE, imp = TRUE)
if (mice::is.mids(data)) {
if (!is.null(method)) {
cli::cli_warn(c(
"!" = "`Method` is ignored when `data` is of class `mids`.",
hanneoberman marked this conversation as resolved.
Show resolved Hide resolved
"i" = "The `method` vector from the `mids` object will be used."
))
}
method <- data$method
data <- data$predictorMatrix
}
p <- nrow(data)
if (!is.null(method) && is.character(method)) {
if (length(method) == 1) {
Expand Down
15 changes: 13 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#'
#' @keywords internal
#' @noRd
verify_data <- function(data,

Check warning on line 38 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=38,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 30, this has 40.
df = FALSE,
imp = FALSE,
pred = FALSE) {
Expand All @@ -52,7 +52,7 @@
}
if (df && imp) {
if (!(is.data.frame(data) ||
is.matrix(data) || mice::is.mids(data))) {

Check warning on line 55 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=55,col=10,[indentation_linter] Indentation should be 12 spaces but is 10 spaces.
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'data.frame', 'matrix', or 'mids'.",
Expand All @@ -62,7 +62,7 @@
)
}
}
if (imp && !df) {
if (imp && !df && !pred) {
if (!mice::is.mids(data)) {
cli::cli_abort(
c(
Expand All @@ -73,7 +73,18 @@
)
}
}
if (pred) {
if (imp && pred){

Check warning on line 76 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

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

Check warning on line 76 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=76,col=19,[paren_body_linter] There should be a space between a right parenthesis and a body expression.
if (!(is.matrix(data) || mice::is.mids(data))) {
cli::cli_abort(
c(
"The 'data' argument requires an object of class 'matrix', or 'mids'.",
"i" = "Input object is of class {class(data)}."
),
call. = FALSE
)
}
}
if (pred && !imp) {
if (!is.matrix(data)) {
cli::cli_abort(
c(
Expand Down
2 changes: 1 addition & 1 deletion man/plot_pred.Rd

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

2 changes: 2 additions & 0 deletions tests/testthat/test-plot_pred.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# create test objects
dat <- mice::nhanes
pred <- mice::quickpred(dat)
imp <- mice::mice(dat, printFlag = FALSE)

# tests
test_that("plot_pred creates ggplot object", {
Expand All @@ -18,6 +19,7 @@ test_that("plot_pred creates ggplot object", {
expect_s3_class(plot_pred(rbind(
cbind(pred, "with space" = 0), "with space" = 0
)), "ggplot")
expect_s3_class(plot_pred(imp, vrb = c("age", "bmi")), "ggplot")
})

test_that("plot_pred with incorrect argument(s)", {
Expand Down
Loading