-
Notifications
You must be signed in to change notification settings - Fork 53
/
README.Rmd
253 lines (167 loc) · 10.2 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# naniar <a href="https://naniar.njtierney.com/"><img src="man/figures/logo.png" align="right" height="138" /></a>
<!-- badges: start -->
[![R-CMD-check](https://github.com/njtierney/naniar/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/njtierney/naniar/actions/workflows/R-CMD-check.yaml)
[![Coverage Status](https://img.shields.io/codecov/c/github/njtierney/naniar/master.svg)](https://app.codecov.io/github/njtierney/naniar?branch=master)
[![CRAN Status Badge](https://www.r-pkg.org/badges/version/naniar)](https://cran.r-project.org/package=naniar)
[![CRAN Downloads Each Month](https://cranlogs.r-pkg.org/badges/naniar)](https://CRAN.R-project.org/package=naniar)
[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
<!-- badges: end -->
`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)](https://www.tandfonline.com/doi/abs/10.1080/01621459.1988.10478722) 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](https://naniar.njtierney.com/articles/naniar.html).
For a short primer on the data visualisation available in naniar, read the vignette [Gallery of Missing Data Visualisations](https://naniar.njtierney.com/articles/naniar-visualisation.html).
For full details of the package, including
# Installation
You can install naniar from CRAN:
```{r install-cran, eval = FALSE}
install.packages("naniar")
```
Or you can install the development version on github using `remotes`:
```{r install-github, eval = FALSE}
# install.packages("remotes")
remotes::install_github("njtierney/naniar")
```
# 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](http://ggobi.org/) and [manet](https://zbmath.org/software/3067), 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.
```{r regular-geom-point}
library(ggplot2)
ggplot(data = airquality,
aes(x = Ozone,
y = Solar.R)) +
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
```{r geom-miss-point}
library(naniar)
ggplot(data = airquality,
aes(x = Ozone,
y = Solar.R)) +
geom_miss_point()
```
`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.
```{r facet-by-month}
p1 <-
ggplot(data = airquality,
aes(x = Ozone,
y = Solar.R)) +
geom_miss_point() +
facet_wrap(~Month, ncol = 2) +
theme(legend.position = "bottom")
p1
```
# Data Structures
naniar provides a data structure for working with missing data, the shadow matrix [(Swayne and Buja, 1998)](https://www.researchgate.net/publication/2758672_Missing_Data_in_Interactive_High-Dimensional_Data_Visualization). 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.
```{r show-shadow}
head(airquality)
as_shadow(airquality)
```
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`:
```{r show-nabular}
bind_shadow(airquality)
nabular(airquality)
```
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:
```{r shadow-w-ggplot}
airquality %>%
bind_shadow() %>%
ggplot(aes(x = Temp,
fill = Ozone_NA)) +
geom_density(alpha = 0.5)
```
And even visualise imputations
```{r shadow-impute}
airquality %>%
bind_shadow() %>%
as.data.frame() %>%
simputation::impute_lm(Ozone ~ Temp + Solar.R) %>%
ggplot(aes(x = Solar.R,
y = Ozone,
colour = Ozone_NA)) +
geom_point()
```
Or perform an [upset](https://www.nature.com/articles/nmeth.3033) plot - to plot of the combinations of missingness across cases, using the `gg_miss_upset` function
```{r gg-miss-upset}
gg_miss_upset(airquality)
```
naniar 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:
```{r gg-miss-var}
gg_miss_var(airquality)
```
Or the number of missings in a given variable at a repeating span
```{r gg-miss-span}
gg_miss_span(pedestrian,
var = hourly_counts,
span_every = 1500)
```
You can read about all of the visualisations in naniar in the vignette [Gallery of missing data visualisations using naniar](https://naniar.njtierney.com/articles/naniar-visualisation.html).
naniar also provides handy helpers for calculating the number, proportion, and percentage of missing and complete observations:
```{r show-miss-helpers}
n_miss(airquality)
n_complete(airquality)
prop_miss(airquality)
prop_complete(airquality)
pct_miss(airquality)
pct_complete(airquality)
```
# 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 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.
```{r miss-var-case-summary}
miss_var_summary(airquality)
miss_case_summary(airquality)
```
You could also `group_by()` to work out the number of missings in each variable across the levels within it.
```{r group-by-var}
library(dplyr)
airquality %>%
group_by(Month) %>%
miss_var_summary()
```
You can read more about all of these functions in the vignette ["Getting Started with naniar"](https://naniar.njtierney.com/articles/naniar.html).
# Statistical tests of missingness
naniar provides `mcar_test()` for [Little's (1988)](https://www.tandfonline.com/doi/abs/10.1080/01621459.1988.10478722) 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:
```{r mcar-test}
mcar_test(airquality)
```
# Contributions
Please note that this project is released with a [Contributor Code of Conduct](https://naniar.njtierney.com/CONDUCT.html). 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](https://github.com/dicook) 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](https://github.com/milesmcbain). 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](https://github.com/ColinFay) 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](https://en.wikipedia.org/wiki/The_Chronicles_of_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, `NA`niar = 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.