-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement impute_zero, impute_factor, and the flexible impute_fixed - r…
…esolves #261
- Loading branch information
Showing
12 changed files
with
362 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#' Impute a factor value into a vector with missing values | ||
#' | ||
#' For imputing fixed factor levels. It adds the new imputed value to the end | ||
#' of the levels of the vector. We generally recommend to impute using other | ||
#' model based approaches. See [impute_lm()] and friends. | ||
#' | ||
#' @param x vector | ||
#' @param value factor to impute | ||
#' | ||
#' @return vector with a factor values replaced | ||
#' @export | ||
#' @name impute_factor | ||
#' | ||
#' @examples | ||
#' | ||
#' vec <- factor(LETTERS[1:10]) | ||
#' | ||
#' vec[sample(1:10, 3)] <- NA | ||
#' | ||
#' impute_factor(vec, "wat") | ||
#' | ||
impute_factor <- function(x, value) UseMethod("impute_factor") | ||
|
||
#' @export | ||
#' @rdname impute_factor | ||
impute_factor.default <- function(x, value){ | ||
vctrs::vec_assert(x, ptype = character()) | ||
} | ||
|
||
#' @export | ||
#' @rdname impute_factor | ||
impute_factor.factor <- function(x, value){ | ||
|
||
x <- forcats::fct_expand(x, value) | ||
|
||
x[is.na(x)] <- factor(value) | ||
|
||
x | ||
} | ||
|
||
#' @export | ||
#' @rdname impute_factor | ||
impute_factor.character <- function(x, value){ | ||
|
||
x <- forcats::fct_expand(x, value) | ||
|
||
x[is.na(x)] <- factor(value) | ||
|
||
x | ||
} | ||
|
||
#' @export | ||
#' @rdname impute_factor | ||
impute_factor.shade <- function(x, value){ | ||
|
||
#do nothing | ||
x | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#' Impute a fixed value into a vector with missing values | ||
#' | ||
#' This can be useful if you are imputing specific values, however we would | ||
#' generally recommend to impute using other model based approaches. See | ||
#' [impute_lm()] and friends. | ||
#' | ||
#' @param x vector | ||
#' @param value value to impute | ||
#' | ||
#' @return vector with a fixed values replaced | ||
#' @export | ||
#' @name impute_fixed | ||
#' | ||
#' @examples | ||
#' | ||
#' vec <- rnorm(10) | ||
#' | ||
#' vec[sample(1:10, 3)] <- NA | ||
#' | ||
#' impute_fixed(vec, -999) | ||
#' | ||
impute_fixed <- function(x, value) UseMethod("impute_fixed") | ||
|
||
#' @export | ||
#' @rdname impute_fixed | ||
impute_fixed.default <- function(x, value){ | ||
|
||
x[is.na(x)] <- value | ||
|
||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#' Impute zero into a vector with missing values | ||
#' | ||
#' This can be useful if you are imputing specific values, however we would | ||
#' generally recommend to impute using other model based approaches. See | ||
#' [impute_lm()] and friends. | ||
#' | ||
#' @param x vector | ||
#' | ||
#' @return vector with a fixed values replaced | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' vec <- rnorm(10) | ||
#' | ||
#' vec[sample(1:10, 3)] <- NA | ||
#' | ||
#' impute_zero(vec) | ||
#' @rdname impute_zero | ||
impute_zero <- function(x){ | ||
|
||
impute_fixed(x = x, value = 0) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
library(dplyr) | ||
vec <- factor(x = c(NA, LETTERS[1:4])) | ||
|
||
vec2 <- factor(x = c("wat", LETTERS[1:4])) | ||
|
||
chick <- chickwts %>% | ||
mutate( | ||
feed = set_prop_miss(feed, prop = 0.2) | ||
) | ||
chick_shadow <- nabular(chick) | ||
|
||
test_that("impute_factor works", { | ||
expect_equal(impute_factor(vec, "wat"), vec2) | ||
}) | ||
|
||
## impute_factor_across -------------------------------------------------------- | ||
test_that("impute_factor works with across", { | ||
expect_false( | ||
mutate(chick, | ||
across(where(is.factor), \(x) impute_factor(x, "wat"))) %>% | ||
all_na() | ||
) | ||
}) | ||
|
||
test_that("impute_factor works with across and nabular", { | ||
expect_false( | ||
mutate(chick_shadow, | ||
across(where(is.factor), \(x) impute_factor(x, "wat"))) %>% | ||
all_na() | ||
) | ||
}) | ||
|
||
test_that("impute_factor retains proper shadow values when used with across", { | ||
expect_equal( | ||
unbind_data( | ||
mutate(chick_shadow, | ||
across(where(is.factor), \(x) impute_factor(x, "wat"))) | ||
), | ||
unbind_data( | ||
chick_shadow | ||
) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
vec <- rnorm(10) | ||
|
||
vec[sample(1:10, 3)] <- NA | ||
|
||
fixed_val <- -99 | ||
|
||
vec2 <- vec | ||
|
||
vec2[is.na(vec)] <- fixed_val | ||
|
||
aq_shadow <- nabular(airquality) | ||
|
||
test_that("impute_fixed works", { | ||
expect_equal(impute_fixed(vec, -99), vec2) | ||
}) | ||
|
||
## impute_fixed_across -------------------------------------------------------- | ||
library(dplyr) | ||
test_that("impute_fixed works with across", { | ||
expect_false( | ||
mutate(airquality, | ||
across(where(is.numeric), \(x) impute_fixed(x, -99))) %>% | ||
all_na() | ||
) | ||
}) | ||
|
||
test_that("impute_fixed works with across and nabular", { | ||
expect_false( | ||
mutate(aq_shadow, | ||
across(where(is.numeric), \(x) impute_fixed(x, -99))) %>% | ||
all_na() | ||
) | ||
}) | ||
|
||
test_that("impute_fixed retains proper shadow values when used with across", { | ||
expect_equal( | ||
unbind_data( | ||
mutate(aq_shadow, | ||
across(where(is.numeric), \(x) impute_fixed(x, -99))) | ||
), | ||
unbind_data( | ||
aq_shadow | ||
) | ||
) | ||
}) |
Oops, something went wrong.