na.pattern: Missing Data Pattern

View source: R/na.pattern.R

na.patternR Documentation

Missing Data Pattern

Description

This function computes a summary of missing data patterns, i.e., number ( cases with a specific missing data pattern and plots the missing data patterns.

Usage

na.pattern(data, ..., order = FALSE, n.pattern = NULL, digits = 2, as.na = NULL,
           plot = FALSE, geom = c("all", "square", "rotate"), filename = NULL,
           width = NA, height = NA,  dpi = 600, write = NULL, append = TRUE,
           check = TRUE, output = TRUE)

Arguments

data

a data frame with incomplete data, where missing values are coded as NA.

...

an expression indicating the variable names in data e.g., na.pattern(dat, x1, x2, x3). Note that the operators +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

order

logical: if TRUE, variables are ordered from left to right in increasing order of missing values.

n.pattern

an integer value indicating the minimum number of cases sharing a missing data pattern to be included in the result table and the plot, e.g., specifying n.pattern = 5 excludes missing data patters with less than 5 cases.

digits

an integer value indicating the number of decimal places to be used for displaying percentages.

as.na

a numeric vector indicating user-defined missing values, i.e. these values are converted to NA before conducting the analysis.

plot

logical: if TRUE, missing data pattern is plotted.

geom

a character vector indicating which plot elements to draw, i.e., "all" for drawing all plot elements, "square" the plot tiles are squares to mimic the md.pattern function in the package mice, and "rotate" for rotating variable name labels 90 degrees.

filename

a character string indicating the filename argument (default is "NA_Pattern.pdf") including the file extension for the ggsave function. Note that one of ".eps", ".ps", ".tex", ".pdf" (default), ".jpeg", ".tiff", ".png", ".bmp", ".svg" or ".wmf" needs to be specified as file extension in the file argument.

width

a numeric value indicating the width argument (default is the size of the current graphics device) for the ggsave function.

height

a numeric value indicating the height argument (default is the size of the current graphics device) for the ggsave function.

dpi

a numeric value indicating the dpi argument (default is 600) for the ggsave function.

write

a character string naming a file for writing the output into either a text file with file extension ".txt" (e.g., "Output.txt") or Excel file with file extension ".xlsx" (e.g., "Output.xlsx"). If the file name does not contain any file extension, an Excel file will be written.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

logical: if TRUE (default), argument specification is checked.

output

logical: if TRUE (default), output is shown.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

data

data frame with variables used in the analysis

args

specification of function arguments

result

result table

plot

ggplot2 object for plotting the results

pattern

a numeric vector indicating the missing data pattern for each case

Note

The code for plotting missing data patterns is based on the plot_pattern function in the ggmice package by Hanne Oberman.

Author(s)

Takuya Yanagida

References

Enders, C. K. (2022). Applied missing data analysis (2nd ed.). The Guilford Press.

Graham, J. W. (2009). Missing data analysis: Making it work in the real world. Annual Review of Psychology, 60, 549-576. https://doi.org/10.1146/annurev.psych.58.110405.085530

Oberman, H. (2023). ggmice: Visualizations for 'mice' with 'ggplot2'. R package version 0.1.0. https://doi.org/10.32614/CRAN.package.ggmice

van Buuren, S. (2018). Flexible imputation of missing data (2nd ed.). Chapman & Hall.

See Also

write.result, as.na, na.as, na.auxiliary, na.coverage, na.descript, na.indicator, na.prop, na.test

Examples

## Not run: 
#————————————————————————————————————————————————————————————————————————————
# Missing Data Pattern

# Example 1a: Compute a summary of missing data patterns
dat.pattern <- na.pattern(airquality)

# Example 1b: Exclude missing data patterns with less than 3 cases
na.pattern(airquality, plot = TRUE, n.pattern = 3)

# Example 1c: Vector of missing data pattern for each case
dat.pattern$pattern

# Example 1d: Data frame without cases with missing data pattern 2
airquality[dat.pattern$pattern != 2, ]

#————————————————————————————————————————————————————————————————————————————
# Plot

# Example 2a: Plot a summary of missing data patterns
na.pattern(airquality, plot = TRUE)

# Example 2b: Plot results using the plot() function, use additional arguments
# see Details in the help page of the function plot.misty.object
object <- na.pattern(airquality)
plot(object, tile.alpha = 0.8, scale.fill.col = c("red2", "darkgreen"))

#————————————————————————————————————————————————————————————————————————————
# Write Results

# Example 3a: Write Results into a text file
na.pattern(airquality, write = "NA_Pattern.xlsx")

# Example 3b: Write Results into a Excel file
na.pattern(airquality, write = "NA_Pattern.xlsx")

# Example 3c: Write Results into a text file
na.pattern(airquality, plot = TRUE, filename = "NA_Pattern.png", width = 8, height = 7)
## End(Not run)

misty documentation built on Nov. 24, 2026, 5:06 p.m.

Related to na.pattern in misty...