Skip to content

Commit

Permalink
Merge pull request #593 from gerkovink/ampute_fix
Browse files Browse the repository at this point in the history
ampute() now preserves the data structure
  • Loading branch information
stefvanbuuren authored Oct 6, 2023
2 parents da91bfc + aa2f538 commit 954e6c6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions R/ampute.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ ampute <- function(data, prop = 0.5, patterns = NULL, freq = NULL,
if (is.null(data)) {
stop("Argument data is missing, with no default", call. = FALSE)
}
data.in <- data # preserve an original set to inject the NA's in later
data <- check.dataform(data)
if (anyNA(data)) {
stop("Data cannot contain NAs", call. = FALSE)
Expand All @@ -218,7 +219,7 @@ ampute <- function(data, prop = 0.5, patterns = NULL, freq = NULL,
data <- data.frame(data)
if (any(vapply(data, Negate(is.numeric), logical(1))) && mech != "MCAR") {
data <- as.data.frame(sapply(data, as.numeric))
warning("Data is made numeric because the calculation of weights requires numeric data",
warning("Data is made numeric internally, because the calculation of weights requires numeric data",
call. = FALSE
)
}
Expand Down Expand Up @@ -454,7 +455,7 @@ ampute <- function(data, prop = 0.5, patterns = NULL, freq = NULL,
names(patterns.new) <- names(data)
names(weights) <- names(data)
call <- match.call()
missing.data <- data.frame(missing.data)
data.in[is.na(data.frame(missing.data))] <- NA
result <- list(
call = call,
prop = prop,
Expand All @@ -466,7 +467,7 @@ ampute <- function(data, prop = 0.5, patterns = NULL, freq = NULL,
std = std,
type = type,
odds = odds,
amp = missing.data,
amp = data.in,
cand = P - 1,
scores = scores,
data = as.data.frame(data)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-ampute.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ test_that("function works around unusual arguments", {
# when data is categorical and mech != mcar, warning is expected
expect_warning(
ampute(data = nasty.data),
"Data is made numeric because the calculation of weights requires numeric data"
"Data is made numeric internally, because the calculation of weights requires numeric data"
)
# when data is categorical and mech = mcar, function can continue
expect_warning(ampute(data = nasty.data, mech = "MCAR"), NA)
Expand Down

0 comments on commit 954e6c6

Please sign in to comment.