-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from njtierney/add-impute-fixed-zero-261
Add impute_fixed, impute_zero, and impute_factor
- Loading branch information
Showing
15 changed files
with
528 additions
and
16 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,86 @@ | ||
#' 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 the `simputation` package, for example | ||
#' [simputation::impute_lm()]. | ||
#' | ||
#' @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 | ||
#' | ||
#' vec | ||
#' | ||
#' impute_factor(vec, "wat") | ||
#' | ||
#' library(dplyr) | ||
#' | ||
#' dat <- tibble( | ||
#' num = rnorm(10), | ||
#' int = rpois(10, 5), | ||
#' fct = factor(LETTERS[1:10]) | ||
#' ) %>% | ||
#' mutate( | ||
#' across( | ||
#' everything(), | ||
#' \(x) set_prop_miss(x, prop = 0.25) | ||
#' ) | ||
#' ) | ||
#' | ||
#' dat | ||
#' | ||
#' dat %>% | ||
#' nabular() %>% | ||
#' mutate( | ||
#' num = impute_fixed(num, -9999), | ||
#' int = impute_zero(int), | ||
#' fct = impute_factor(fct, "out") | ||
#' ) | ||
#' | ||
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,57 @@ | ||
#' 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 | ||
#' the `simputation` package, for example [simputation::impute_lm()]. | ||
#' | ||
#' @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 | ||
#' | ||
#' vec | ||
#' | ||
#' impute_fixed(vec, -999) | ||
#' | ||
#' library(dplyr) | ||
#' | ||
#' dat <- tibble( | ||
#' num = rnorm(10), | ||
#' int = rpois(10, 5), | ||
#' fct = factor(LETTERS[1:10]) | ||
#' ) %>% | ||
#' mutate( | ||
#' across( | ||
#' everything(), | ||
#' \(x) set_prop_miss(x, prop = 0.25) | ||
#' ) | ||
#' ) | ||
#' | ||
#' dat | ||
#' | ||
#' dat %>% | ||
#' nabular() %>% | ||
#' mutate( | ||
#' num = impute_fixed(num, -9999), | ||
#' int = impute_zero(int), | ||
#' fct = impute_factor(fct, "out") | ||
#' ) | ||
#' | ||
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,53 @@ | ||
#' 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 | ||
#' the `simputation` package, for example [simputation::impute_lm()]. | ||
#' | ||
#' @param x vector | ||
#' | ||
#' @return vector with a fixed values replaced | ||
#' @export | ||
#' | ||
#' @examples | ||
#' | ||
#' vec <- rnorm(10) | ||
#' | ||
#' vec[sample(1:10, 3)] <- NA | ||
#' | ||
#' vec | ||
#' | ||
#' impute_zero(vec) | ||
#' | ||
#' library(dplyr) | ||
#' | ||
#' library(dplyr) | ||
#' | ||
#' dat <- tibble( | ||
#' num = rnorm(10), | ||
#' int = rpois(10, 5), | ||
#' fct = factor(LETTERS[1:10]) | ||
#' ) %>% | ||
#' mutate( | ||
#' across( | ||
#' everything(), | ||
#' \(x) set_prop_miss(x, prop = 0.25) | ||
#' ) | ||
#' ) | ||
#' | ||
#' dat | ||
#' | ||
#' dat %>% | ||
#' nabular() %>% | ||
#' mutate( | ||
#' num = impute_fixed(num, -9999), | ||
#' int = impute_zero(int), | ||
#' fct = impute_factor(fct, "out") | ||
#' ) | ||
#' | ||
#' @rdname impute_zero | ||
impute_zero <- function(x){ | ||
|
||
impute_fixed(x = x, value = 0) | ||
|
||
} |
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
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.
Oops, something went wrong.