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 [@Wickham2014; @Wickham2009].
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:
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?
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
There are two main functions in the
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.
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"
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:
library(ggplot2) ggplot(airquality, aes(x = Solar.R, y = Ozone)) + geom_point()
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
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.
ggplot(airquality, aes(x = Solar.R, y = Ozone)) + geom_point() library(naniar) ggplot(airquality, aes(x = Solar.R, y = Ozone)) + geom_miss_point()
Being a proper ggplot geom, it supports all of the standard features of ggplot2, such as facets,
ggplot(airquality, aes(x = Solar.R, y = Ozone)) + geom_miss_point() + facet_wrap(~Month)
And different themes
ggplot(airquality, aes(x = Solar.R, y = Ozone)) + geom_miss_point() + facet_wrap(~Month) + theme_dark()
Another approach to visualising the missings in a dataset is to use the
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
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.
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
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:
tidyr::replace_na: Missing values turns into a value (NA --> -99)
naniar::replace_with_na: Value becomes a missing value (-99 --> NA)
You can read more about this in the vignette "Replacing values with NA"
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
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
bind_shadow argument attaches a shadow to the current dataframe
aq_shadow <- bind_shadow(airquality) library(dplyr) glimpse(aq_shadow)
Doing this provides a consistent syntax for referring to variables with missing values.
bind_shadow() also 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.
ggplot(aq_shadow, aes(x = Temp, colour = Ozone_NA)) + geom_density()
Binding the shadow here also has great benefits when combined with imputation.
With the easy-to-use
simputation package, we impute values for Ozone, then visualise the data:
library(simputation) library(dplyr) airquality %>% impute_lm(Ozone ~ Temp + Wind) %>% ggplot(aes(x = Temp, y = Ozone)) + geom_point()
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)) + geom_point()
naniar also provide numerical summaries for missing data. Two convenient counters of complete values and missings are
n_complete. These work on both dataframes and vectors, similar to
dplyr::n_distinct(airquality) dplyr::n_distinct(airquality$Ozone) n_miss(airquality) n_miss(airquality$Ozone) n_complete(airquality) n_complete(airquality$Ozone)
The syntax for the other numerical sumamries in
miss_, and then
var to refer to cases or variables. There are then
cumsum options to explore missing data.
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:
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.
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.
miss_var_run, you specify the variable that you want to explore the runs of missingness for, in this case,
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
miss_var_span(pedestrian, hourly_counts, span_every = 100)
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")
The final question we proposed in this vignette was:
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() %>% head()
We can then use a model like decision trees to predict which variables and their values are important for predicting the proportion of missingness:
library(rpart) library(rpart.plot) airquality %>% add_prop_miss() %>% rpart(prop_miss_all ~ ., data = .) %>% prp(type = 4, extra = 101, prefix = "Prop. Miss = ") # library(visdat) # vis_miss(messy_airquality)
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.
naniarwork with big data tools like
geom_miss_family to include:
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
miss_*_cumsum, and more.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.