Getting Started with naniar

knitr::opts_chunk$set(fig.align = "center",
                      fig.width = 5,
                      fig.height = 4,
                      dpi = 100)


Missing values are ubiquitous in data and need to be carefully explored and handled in the initial stages of analysis. In this vignette we describe the tools in the package naniar for exploring missing data structures with minimal deviation from the common workflows of ggplot and tidy data (Wickham, 2014, Wickham, 2009).

Sometimes researchers or analysts will introduce or describe a mechanism for missingness. For example, they might explain that data from a weather station might have a malfunction when there are extreme weather events, and does not record temperature data when gusts speeds are high. This seems like a nice simple, logical explanation. However, like all good explanations, this one is simple, but the process to get there was probably not, and likely involved more time than you would have liked developing exploratory data analyses and models.

So when someone presents a really nice plot and a nice sensible explanation, the initial thought might be:

They worked it out themselves so quickly, so easy!

As if the problem was so easy to solve, they could accidentally solve it - they couldn't not solve it.

However, I think that if you manage to get that on the first go, that is more like turning around and throwing a rock into a lake and it landing in a cup in a boat. Unlikely.

With that thought in mind, this vignette aims to work with the following three questions, using the tools developed in naniar and another package, visdat. Namely, how do we:

  1. Start looking at missing data?
  2. Explore missingness mechanisms?
  3. Model missingness?

How do we start looking at missing data?

When you start with a dataset, you might do something where you look at the general summary, using functions such as:

These works really well when you've got a small amount of data, but when you have more data, you are generally limited by how much you can read.

So before you start looking at missing data, you'll need to look at the data, but what does that even mean?

The package visdat helps you get a handle on this. visdat provides a visualisation of an entire data frame at once, and was heavily inspired by csv-fingerprint, and functions like missmap, from Amelia.

There are two main functions in the visdat package:



vis_dat visualises the whole dataframe at once, and provides information about the class of the data input into R, as well as whether the data is missing or not.


The function vis_miss provides a summary of whether the data is missing or not. It also provides the amount of missings in each columns.


So here, Ozone and Solar.R have the most missing data, with Ozone having 24.2% missing data and Solar.R have 4.6%. The other variables do not have any missing data.

To read more about the functions available in visdat see the vignette "Using visdat"

Exploring missingness relationships

We can identify key variables that are missing using vis_miss, but for further exploration, we need to explore the relationship amongst the variables in this data:

Typically, when exploring this data, you might want to explore the variables Solar.R and Ozone, and so plot a scatterplot of solar radiation and ozone, doing something like this:

       aes(x = Solar.R, 
           y = Ozone)) + 

The problem with this is that ggplot does not handle missings be default, and removes the missing values. This makes them hard to explore. It also presents the strange question of "how do you visualise something that is not there?". One approach to visualising missing data comes from ggobi and manet, where we replace "NA" values with values 10% lower than the minimum value in that variable.

This process is performed and visualised for you with the geom_miss_point() ggplot2 geom. Here, we illustrate by exploring the relationship between Ozone and Solar radiation from the airquality dataset.

       aes(x = Solar.R, 
           y = Ozone)) + 


       aes(x = Solar.R, 
           y = Ozone)) + 

Being a proper ggplot geom, it supports all of the standard features of ggplot2, such as facets,

       aes(x = Solar.R, 
           y = Ozone)) + 
  geom_miss_point() + 

And different themes

       aes(x = Solar.R, 
           y = Ozone)) + 
  geom_miss_point() + 
  facet_wrap(~Month) + 

Visualising missings in variables

Another approach to visualising the missings in a dataset is to use the gg_miss_var plot:


The plots created with the gg_miss family all have a basic theme, but you can customise them, and add arguments like so:

gg_miss_var(airquality) + theme_bw() 

gg_miss_var(airquality) + labs(y = "Look at all the missing ones")

To add facets in these plots, you can use the facet argument:

gg_miss_var(airquality, facet = Month)

There are more visualisations available in naniar (each starting with gg_miss_) - you can see these in the "Gallery of Missing Data Visualisations" vignette..

It is important to note that for every visualisation of missing data in naniar, there is an accompanying function to get the dataframe of the plot out. This is important as the plot should not return a dataframe - but we also need to make the data available for use by the user so that it isn't locked into a plot. You can find these summary plots below, with miss_var_summary providing the dataframe that gg_miss_var() is based on.

Replacing existing values with NA

When you are dealing with missing values, you might want to replace values with a missing values (NA). This is useful in cases when you know the origin of the data and can be certain which values should be missing. For example, you might know that all values of "N/A", "N A", and "Not Available", or -99, or -1 are supposed to be missing.

naniar provides functions to specifically work on this type of problem using the function replace_with_na. This function is the compliment to tidyr::replace_na, which replaces an NA value with a specified value, whereas naniar::replace_with_na replaces a value with an NA:

You can read more about this in the vignette "Replacing values with NA"

Tidy Missing Data: The Shadow Matrix

Representing missing data structure in a tidy format is achieved using the shadow matrix, introduced in Swayne and Buja. 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". Although these may be represented as 1 and 0, respectively. This representation can be seen in the figure below, adding the suffix "_NA" to the variables. This structure can also be extended to allow for additional factor levels to be created. For example 0 indicates data presence, 1 indicates missing values, 2 indicates imputed value, and 3 might indicate a particular type or class of missingness, where reasons for missingness might be known or inferred. The data matrix can also be augmented to include the shadow matrix, which facilitates visualisation of univariate and bivariate missing data visualisations. Another format is to display it in long form, which facilitates heatmap style visualisations. This approach can be very helpful for giving an overview of which variables contain the most missingness. Methods can also be applied to rearrange rows and columns to find clusters, and identify other interesting features of the data that may have previously been hidden or unclear.


Illustration of data structures for facilitating visualisation of missings and not missings

The shadow functions provide a way to keep track of missing values. The as_shadow function creates a dataframe with the same set of columns, but with the column names added a suffix _NA


bind_shadow attaches a shadow to the current dataframe, a format we call "nabular", a portmanteau of NA a tabular. You can also use nabular to do the same thing:

aq_shadow <- bind_shadow(airquality)
aq_nab <- nabular(airquality)



all.equal(aq_shadow, aq_nab)

Doing this provides a consistent syntax for referring to variables with missing values.

Nabular data provides a useful pattern to explore missing values, grouping by the missing/complete of one variable and looking at the mean and other summary values. Below we show the mean, sd, variance, and min and max values of Solar.R for when Ozone is present, and when it is missing.

airquality %>%
  bind_shadow() %>%
  group_by(Ozone_NA) %>%
  summarise_at(.vars = "Solar.R",
               .funs = c("mean", "sd", "var", "min", "max"),
               na.rm = TRUE)

Below, we can plot the distribution of Temperature, plotting for values of temperature when Ozone is missing, and when it is not missing.

       aes(x = Temp,
           colour = Ozone_NA)) + 

Binding the shadow here also has great benefits when combined with imputation.

Visualising imputed values

With the easy-to-use simputation package, we impute values for Ozone, then visualise the data:


airquality %>%
  impute_lm(Ozone ~ Temp + Wind) %>%
  ggplot(aes(x = Temp,
             y = Ozone)) + 

Note that we no longer get any errors regarding missing observations - because they are all imputed! But this comes at a cost: we also no longer have information about where the imputations are - they are now sort of invisible.

Using the shadow matrix to keep track of where the missings are, you can actually keep track of the imputations, by colouring by what was previously missing in Ozone.

aq_shadow %>%
  impute_lm(Ozone ~ Temp + Wind) %>%
  ggplot(aes(x = Temp,
             y = Ozone,
             colour = Ozone_NA)) + 

Numerical summaries of missing values

naniar also provide numerical summaries for missing data. Two convenient counters of complete values and missings are n_miss() and n_complete(). These work on both dataframes and vectors, similar to dplyr::n_distinct()




The syntax for the other numerical sumamries in naniar are miss_, and then case, or var to refer to cases or variables. There are then summary, table, run, span, and cumsum options to explore missing data.

miss_case_prop and miss_case_pct return numeric value describing the proportion or percent of missing values in the dataframe.


miss_case_summary() returns a numeric value that describes the number of missings in a given case (aka row), the percent of missings in that row.


miss_case_table() tabulates the number of missing values in a case / row. Below, this shows the number of missings in a case:


Similar to miss_case_pct() and miss_case_prop(), miss_var_pct() and miss_var_prop() returns the percent and proportion of variables that contain a missing value.


miss_var_summary() then returns the number of missing values in a variable, and the percent missing in that variable.


Finally, miss_var_table(). This describes the number of missings in a variable.


There are also summary functions for exploring missings that occur over a particular span or period of the dataset, or the number of missings in a single run:

miss_var_run() can be particularly useful in time series data, as it allows you to provide summaries for the number of missings or complete values in a single run. The function miss_var_run() provides a data.frame of the run length of missings and complete values. To explore this function we will use the built-in dataset, pedestrian, which contains hourly counts of pedestrians from four locations around Melbourne, Australia, from 2016.

To use miss_var_run(), you specify the variable that you want to explore the runs of missingness for, in this case, hourly_counts:


miss_var_span() is used to determine the number of missings over a specified repeating span of rows in variable of a dataframe. Similar to miss_var_run(), you specify the variable that you wish to explore, you then also specify the size of the span with the span_every argument.

              span_every = 100)

Using group_by() with naniar

Every miss_* summary function that returns a dataframe can be used with dplyr group_by(). For example, if you would like to look at the number of missing values for all variables of pedestrian data.

pedestrian %>% miss_var_summary()

We see that this is in hourly_counts. We can then explore this by month, and filder by the variable being hourly_counts, since it is the only one with missing values.

pedestrian %>%
 group_by(month) %>%
 miss_var_summary() %>%
 filter(variable == "hourly_counts")

Modelling missingness

The final question we proposed in this vignette was:

  1. Can we model missingness?

Sometimes it can be impractical to explore all of the variables that have missing data. One approach, however, is to model missing data using methods from Tierney et el. (2015).

Here, the approach is to predict the proportion of missingness in a given case, using all variables. There is a little helper function to add a column with the proportion of cases or rows missing - add_prop_miss(). This created a column named "prop_miss", which is the proportion of missing values in that row.

airquality %>%
  add_prop_miss() %>%

We can then use a model like decision trees to predict which variables and their values are important for predicting the proportion of missingness:


airquality %>%
  add_prop_miss() %>%
  rpart(prop_miss_all ~ ., data = .) %>%
  prp(type = 4, extra = 101, prefix = "Prop. Miss = ")

Here we can see that this produces quite a complex tree - this can be pruned back and the depth of the decision tree controlled.


The tools in naniar help us identify where missingness is, while maintaining a tidy workflow. We care about these mechanisms or these patterns because they can help us understand potential mechanisms, such as equipment failures, and then identify possible solutions based upon this evidence.

Future development

Thank you

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_with_na, miss_*_cumsum, and more.


Try the naniar package in your browser

Any scripts or data that you put into this service are public.

naniar documentation built on Sept. 10, 2018, 5:05 p.m.