naniar provides principled, tidy ways to summarise, visualise, and
manipulate missing data with minimal deviations from the workflows in
ggplot2 and tidy data. It does this by providing:
Shadow matrices, a tidy data structure for missing data:
bind_shadow() and nabular()
Shorthand summaries for missing data:
n_miss() and n_complete()
pct_miss()and pct_complete()
Numerical summaries of missing data in variables and cases:
miss_var_summary() and miss_var_table()
miss_case_summary(), miss_case_table()
Statistical tests of missingness:
mcar_test() for Little’s
(1988)
missing completely at random (MCAR) test
Visualisation for missing data:
geom_miss_point()
gg_miss_var()
gg_miss_case()
gg_miss_fct()
For more details on the workflow and theory underpinning naniar, read
the vignette Getting started with
naniar.
Visualising missing data might sound a little strange - how do you
visualise something that is not there? One approach to visualising
missing data comes from ggobi and
manet, which replaces NA values
with values 10% lower than the minimum value in that variable. This
visualisation is provided with the geom_miss_point() ggplot2 geom,
which we illustrate by exploring the relationship between Ozone and
Solar radiation from the airquality dataset.
library(ggplot2)
ggplot(data = airquality,
aes(x = Ozone,
y = Solar.R)) +
geom_point()
#> Warning: Removed 42 rows containing missing values or values outside the scale range
#> (`geom_point()`).
ggplot2 does not handle these missing values, and we get a warning
message about the missing values.
We can instead use geom_miss_point() to display the missing data
geom_miss_point() has shifted the missing values to now be 10% below
the minimum value. The missing values are a different colour so that
missingness becomes pre-attentive. As it is a ggplot2 geom, it supports
features like faceting and other ggplot features.
naniar provides a data structure for working with missing data, the
shadow matrix (Swayne and Buja,
1998).
The shadow matrix is the same dimension as the data, and consists of
binary indicators of missingness of data values, where missing is
represented as “NA”, and not missing is represented as “!NA”, and
variable names are kep the same, with the added suffix “_NA” to the
variables.
Binding the shadow data to the data you help keep better track of the
missing values. This format is called “nabular”, a portmanteau of NA
and tabular. You can bind the shadow to the data using bind_shadow
or nabular:
Using the nabular format helps you manage where missing values are in
your dataset and make it easy to do visualisations where you split by
missingness:
naniar provides numerical summaries of missing data, that follow a
consistent rule that uses a syntax begining with miss_. Summaries
focussing on variables or a single selected variable, start with
miss_var_, and summaries for cases (the initial collected row order of
the data), they start with miss_case_. All of these functions that
return dataframes also work with dplyr’s group_by().
For example, we can look at the number and percent of missings in each
case and variable with miss_var_summary(), and miss_case_summary(),
which both return output ordered by the number of missing values.
naniar provides mcar_test() for Little’s
(1988)
statistical test for missing completely at random (MCAR) data. The null
hypothesis in this test is that the data is MCAR, and the test statistic
is a chi-squared value. Given the high statistic value and low p-value,
we can conclude that the airquality data is not missing completely at
random:
Please note that this project is released with a Contributor Code of
Conduct. By participating in
this project you agree to abide by its terms.
Future Work
Extend the geom_miss_* family to include categorical variables,
Bivariate plots: scatterplots, density overlays
SQL translation for databases
Big Data tools (sparklyr, sparklingwater)
Work well with other imputation engines / processes
Provide tools for assessing goodness of fit for classical approaches
of MCAR, MAR, and MNAR (graphical inference from nullabor package)
Acknowledgements
Firstly, thanks to Di Cook for giving the
initial inspiration for the package and laying down the rich theory and
literature that the work in naniar is built upon. Naming credit (once
again!) goes to Miles McBain. Among
various other things, Miles also worked out how to overload the missing
data and make it work as a geom. Thanks also to Colin
Fay for helping me understand tidy
evaluation and for features such as replace_to_na, miss_*_cumsum,
and more.
A note on the name
naniar was previously named ggmissing and initially provided a ggplot
geom and some other visualisations. ggmissing was changed to naniar
to reflect the fact that this package is going to be bigger in scope,
and is not just related to ggplot2. Specifically, the package is
designed to provide a suite of tools for generating visualisations of
missing values and imputations, manipulate, and summarise missing data.
…But why naniar?
Well, I think it is useful to think of missing values in data being like
this other dimension, perhaps like C.S. Lewis’s
Narnia - a
different world, hidden away. You go inside, and sometimes it seems like
you’ve spent no time in there but time has passed very quickly, or the
opposite. Also, NAniar = na in r, and if you so desire, naniar may
sound like “noneoya” in an nz/aussie accent. Full credit to @MilesMcbain
for the name, and @Hadley for the rearranged spelling.
naniar
naniarprovides principled, tidy ways to summarise, visualise, and manipulate missing data with minimal deviations from the workflows in ggplot2 and tidy data. It does this by providing:bind_shadow()andnabular()n_miss()andn_complete()pct_miss()andpct_complete()miss_var_summary()andmiss_var_table()miss_case_summary(),miss_case_table()mcar_test()for Little’s (1988) missing completely at random (MCAR) testgeom_miss_point()gg_miss_var()gg_miss_case()gg_miss_fct()For more details on the workflow and theory underpinning naniar, read the vignette Getting started with naniar.
For a short primer on the data visualisation available in naniar, read the vignette Gallery of Missing Data Visualisations.
For full details of the package, including
Installation
You can install naniar from CRAN:
Or you can install the development version on github using
remotes:A short overview of naniar
Visualising missing data might sound a little strange - how do you visualise something that is not there? One approach to visualising missing data comes from ggobi and manet, which replaces
NAvalues with values 10% lower than the minimum value in that variable. This visualisation is provided with thegeom_miss_point()ggplot2 geom, which we illustrate by exploring the relationship between Ozone and Solar radiation from the airquality dataset.ggplot2 does not handle these missing values, and we get a warning message about the missing values.
We can instead use
geom_miss_point()to display the missing datageom_miss_point()has shifted the missing values to now be 10% below the minimum value. The missing values are a different colour so that missingness becomes pre-attentive. As it is a ggplot2 geom, it supports features like faceting and other ggplot features.Data Structures
naniar provides a data structure for working with missing data, the shadow matrix (Swayne and Buja, 1998). The shadow matrix is the same dimension as the data, and consists of binary indicators of missingness of data values, where missing is represented as “NA”, and not missing is represented as “!NA”, and variable names are kep the same, with the added suffix “_NA” to the variables.
Binding the shadow data to the data you help keep better track of the missing values. This format is called “nabular”, a portmanteau of
NAandtabular. You can bind the shadow to the data usingbind_shadowornabular:Using the nabular format helps you manage where missing values are in your dataset and make it easy to do visualisations where you split by missingness:
And even visualise imputations
Or perform an upset plot - to plot of the combinations of missingness across cases, using the
gg_miss_upsetfunctionnaniar does this while following consistent principles that are easy to read, thanks to the tools of the tidyverse.
naniar also provides handy visualations for each variable:
Or the number of missings in a given variable at a repeating span
You can read about all of the visualisations in naniar in the vignette Gallery of missing data visualisations using naniar.
naniar also provides handy helpers for calculating the number, proportion, and percentage of missing and complete observations:
Numerical summaries for missing data
naniar provides numerical summaries of missing data, that follow a consistent rule that uses a syntax begining with
miss_. Summaries focussing on variables or a single selected variable, start withmiss_var_, and summaries for cases (the initial collected row order of the data), they start withmiss_case_. All of these functions that return dataframes also work with dplyr’sgroup_by().For example, we can look at the number and percent of missings in each case and variable with
miss_var_summary(), andmiss_case_summary(), which both return output ordered by the number of missing values.You could also
group_by()to work out the number of missings in each variable across the levels within it.You can read more about all of these functions in the vignette “Getting Started with naniar”.
Statistical tests of missingness
naniar provides
mcar_test()for Little’s (1988) statistical test for missing completely at random (MCAR) data. The null hypothesis in this test is that the data is MCAR, and the test statistic is a chi-squared value. Given the high statistic value and low p-value, we can conclude that theairqualitydata is not missing completely at random:Contributions
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Future Work
geom_miss_*family to include categorical variables, Bivariate plots: scatterplots, density overlaysnullaborpackage)Acknowledgements
Firstly, thanks to Di Cook for giving the initial inspiration for the package and laying down the rich theory and literature that the work in naniar is built upon. Naming credit (once again!) goes to Miles McBain. Among various other things, Miles also worked out how to overload the missing data and make it work as a geom. Thanks also to Colin Fay for helping me understand tidy evaluation and for features such as
replace_to_na,miss_*_cumsum, and more.A note on the name
naniar was previously named
ggmissingand initially provided a ggplot geom and some other visualisations.ggmissingwas changed tonaniarto reflect the fact that this package is going to be bigger in scope, and is not just related toggplot2. Specifically, the package is designed to provide a suite of tools for generating visualisations of missing values and imputations, manipulate, and summarise missing data.Well, I think it is useful to think of missing values in data being like this other dimension, perhaps like C.S. Lewis’s Narnia - a different world, hidden away. You go inside, and sometimes it seems like you’ve spent no time in there but time has passed very quickly, or the opposite. Also,
NAniar = na in r, and if you so desire, naniar may sound like “noneoya” in an nz/aussie accent. Full credit to @MilesMcbain for the name, and @Hadley for the rearranged spelling.