Imputation the Tidyverse Way

Functions and methods for imputing missing values (NA) in tables and list patterned after the tidyverse approach of 'dplyr' and 'rlang'; works with data.tables as well.


Comprehensive Library For Handling Missing Values

License CRAN Downloads

tidyimpute is tidtverse/dplyr compliant toolkit for imputing missing values (NA) values in list-like and table-like structures including data.tables. It had two goals: 1) extend existing na.* functions from the stats packages and 2) provide dplyr/tidyverse compliant methods for tables and lists.

This package is based on the handy na.tools package which provides tools for working with missing values in vectors.

Feature List

  • Over 80 functions for imputi missing values (See [#Function List] below.)
  • dplyr/tidyverse compliant inteface:
    • impute_* family of functions for table- or list-based imputations.
    • impute_*_at, impute_*_all and impute_*_if functions
  • Uses the *na.tools package to ensure
    • Type/class and length-safe replacement. (tidyimport will never change produce an object with a different length/nrow or type/class of its target.)
  • General imputation methods
    • Generic imputation: impute, impute_at, impute_all, impute_if
  • Specialized imputation methods
    • Common imputations for:
      • constants: 0, -Inf, Inf
      • univariate, commutative summary functions: mean, median, max, min, zero
      • (Coming Soon) univariate, non-commutive/ordered/time-series data: loess, locf, locb
      • (Coming Soon) model-based imputation
    • Support for recursive (lists and table-like structures)
    • Support for tibble
    • Support for data.table
  • Four extensible types of imputations

Upcoming features

  • recall/track which values have been replaced
  • by-group calculations
  • Time-series/ordered/non-commutative methods
  • Model-based imputation
    • Model-based + by-groups

Installation

Github (Development Version)

library(devtools)
install_github( "decisionpatterns/tidyimport")

CRAN

R> install.packages("tidyimpute")

Coming Soon ...

  • Impute by model
  • Memorable imputing

Function List

There are four types of imputation methods. They are distinguished by how the replacement values are calculated. Each is described below as well as describing each of the methods used.

Constants

In "constant" imputation methods, missing values are replaced by an a priori selected constant value. The vector containingmissing values is not used to calculate the replacement value. These take the form: na.fun(x, ...)

  • impute_zero - 0
  • impute_inf / impute_neginf - Inf/-Inf
  • impute_constant - Impute with a constant

Univariate

(Impute using function(s) of the target variable; When imputing in a table this is also called column-based imputation since the values used to derive the imputed come from the single column alone.)

In "univariate" replacement methods, values are calculated using only the target vector, ie the one containing the missing values. The functions for performing the imputation are nominally univariate summary functions. Generally, the ordering of the vector does not affect imputed values. In general, one value is used to replace all missing values (NA) for a variable.

  • impute_max - maximum
  • impute_minimum - minumum
  • impute_mean - mean
  • impute_median - median value
  • impute_quantile - quantile value
  • impute_sample - randomly sampled value via bootstrap.

Ordered Univariate (Coming Soon)

(Impute using function(s) of the target variable. Variable ordering relevant. This is a super class of the previous column-based imputation.)

In "ordered univariate" methods, replacement valuse are calculated from the vector that is assumed to be ordered. These types are very often used with time-series data. (Many of these functions are taken from or patterned after functions in the zoo package.)

  • impute_loess - loess smoother, assumes values are ordered
  • impute_locf - last observation carried forward, assumes ordered
  • impute_nocb - next observation carried backwards, assumes ordered

Multivariate (Coming Soon)

(Impute with multiple variables from the same observation. In tables, this is also called row-based imputation because imputed values derive from other measurement for the same observation. )

In "Multivariate" imputation, any value from the same row (observation) can be used to derive the replacement value. This is generally implemented as a model traing from the data with var ~ ...

  • impute_fit,impute_predict - use a model
  • impute_by_group - use by-group imputation

Generalized (Coming Soon)

(Impute with column and rows.)

Future:

  • unimpute/impute_restore - restore NAs to the vector; remembering replacement
  • impute_toggle - toggle between NA and replacement values

Examples

tbl <- data.frame( col_1 = letters[1:3], col_2=c(1,NA_real_,3), col_3=3:1)
 
impute( tbl, 2) 
impute_mean( tbl )

News

Version

  • Add NA_explicit_ as an exported constant for explicit categorical values.
  • Convert man to use markdown.

Version 0.7.3 (2018-01-22)

  • Fix na_replace (and na_explicit) to add levels for values if they do not already exist.
  • Add tests
  • Fix documentation

Version 0.7.0 (2017-08-22)

  • Add na_explicit and na_implicit

Version 0.6.2

  • na_replace: revert from using ifelse because of edge cases
  • add zzz.R
  • add NEWS.md
  • add tests for na_replace

Version 0.6.1

  • na_replace now uses ifelse and prevent recycling value

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("tidyimpute")

0.1.0 by Christopher Brown, a year ago


https://github.com/decisionpatterns/tidyimpute


Report a bug at https://github.com/decisionpatterns/tidyimpute/issues


Browse source code at https://github.com/cran/tidyimpute


Authors: Christopher Brown [aut, cre] , Decision Patterns [cph]


Documentation:   PDF Manual  


Task views: Missing Data


GPL-3 | file LICENSE license


Imports methods, dplyr, rlang, na.tools

Suggests testthat, data.table, magrittr


See at CRAN