Skip to content

This NA handler ensures that those models that support the na.action parameter do not silently drop missing values. It wraps around stats::na.exclude() so that there is one prediction/residual for input row. To apply it globally, run options(na.action = na.warn).

Usage

na.warn(object)

Arguments

object

A data frame

Examples

df <- tibble::tibble(
  x = 1:10,
  y = c(5.1, 9.7, NA, 17.4, 21.2, 26.6, 27.9, NA, 36.3, 40.4)
)
# Default behaviour is to silently drop
m1 <- lm(y ~ x, data = df)
resid(m1)
#>           1           2           4           5           6           7 
#> -0.59214286  0.14500000  0.11928571  0.05642857  1.59357143 -0.96928571 
#>           9          10 
#> -0.29500000 -0.05785714 

# Use na.action = na.warn to warn
m2 <- lm(y ~ x, data = df, na.action = na.warn)
#> Warning: Dropping 2 rows with missing values
resid(m2)
#>           1           2           3           4           5           6 
#> -0.59214286  0.14500000          NA  0.11928571  0.05642857  1.59357143 
#>           7           8           9          10 
#> -0.96928571          NA -0.29500000 -0.05785714