# Imputation Method IRMI

#### Wolfgang Rannetbauer

## Overview

In addition to Model based Imputation Methods (see `vignette("modelImp")`) the `VIM` package also presents an iterative imputation method.

This vignette showcases the function `irmi()`. IRMI is short for Iterative Robust Model-based Imputation. This method can be used to generate imputations for several variables in a dataset.

Basically `irmi()` mimics the functionality of IVEWARE (Raghunathan et al., 2001), but there are several improvements with respect to the stability of the initialized values, or the robustness of the imputed values. In contrast to other imputation methods, the IRMI algorithm does not require at least one fully observed variable. In each step of the iteration, one variable is used as a response variable and the remaining variables serve as the regressors. Thus the “whole” multivariate information will be used for imputation in the response variable. For more details, please see IRMI Imputation.

## Data

The following example demonstrates the functionality of `irmi()` using a subset of `sleep`. The columns have been selected deliberately to include some interactions between the missing values.

    library(VIM)
    dataset <- sleep[, c("Dream", "NonD", "BodyWgt", "Span")]
    dataset$BodyWgt <- log(dataset$BodyWgt)
    dataset$Span <- log(dataset$Span)
    aggr(dataset)

The plot indicates several missing values in `Dream`, `NonD`, and `Span.`

    sapply(dataset, function(x) sum(is.na(x)))
    #>   Dream    NonD BodyWgt    Span
    #>      12      14       0       4

## Imputing multiple variables

The call of the function is straightforward and the algorithm usually converges in a few iterations.

    imp_irmi <- irmi(dataset)
    #>       NonD    BodyWgt       Span       NonD    BodyWgt       Span
    #>  2.1000000 -5.2983174  0.6931472 17.9000000  8.8029735  4.6051702
    #>      Dream    BodyWgt       Span      Dream    BodyWgt       Span
    #>  0.0000000 -5.2983174  0.6931472  6.6000000  8.8029735  4.6051702
    #>     Dream      NonD   BodyWgt     Dream      NonD   BodyWgt
    #>  0.000000  2.100000 -5.298317  6.600000 17.900000  8.802973

    aggr(imp_irmi, delimiter = "_imp")

We can see that `irmi()` imputed all missing values for all variables in our dataset.

### Diagnosing the results

As we can see in the next plot, for imputing missing values in `NonD` `Bodygt` plays an important role. The original data structure of `NonD` and `BodyWgt` is preserved by the `irmi()` imputation method.

    imp_irmi[, c("NonD", "BodyWgt", "NonD_imp")] |>
      marginplot(delimiter = "_imp")

The same is true for the data structure of `Span` and `BodyWgt`.

    imp_irmi[, c("Span", "BodyWgt", "Span_imp")] |>
      marginplot(delimiter = "_imp")

## Performance of method

In order to validate the performance of `irmi()` and to highlight the ability to impute different datatypes the `iris` dataset is used. Firstly, some values are randomly set to `NA`.

    data(iris)
    df <- iris
    colnames(df) <- c("S.Length", "S.Width", "P.Length", "P.Width", "Species")
    # randomly produce some missing values in the data
    set.seed(1)
    nbr_missing <- 50
    y <- data.frame(row = sample(nrow(iris), size = nbr_missing, replace = TRUE),
                    col = sample(ncol(iris), size = nbr_missing, replace = TRUE))
    y <- y[!duplicated(y), ]
    df[as.matrix(y)] <- NA

    aggr(df)

    sapply(df, function(x) sum(is.na(x)))
    #> S.Length  S.Width P.Length  P.Width  Species
    #>       10        9        8       10       12

We can see that there are missings in all variables and some observations reveal missing values on several points.

    imp_irmi <- irmi(df)
    #>  S.Width P.Length  P.Width  S.Width P.Length  P.Width
    #>      2.0      1.0      0.1      4.4      6.9      2.5
    #> S.Length P.Length  P.Width S.Length P.Length  P.Width
    #>      4.3      1.0      0.1      7.9      6.9      2.5
    #> S.Length  S.Width  P.Width S.Length  S.Width  P.Width
    #>      4.3      2.0      0.1      7.9      4.4      2.5
    #> S.Length  S.Width P.Length S.Length  S.Width P.Length
    #>      4.3      2.0      1.0      7.9      4.4      6.9
    #> S.Length  S.Width P.Length  P.Width S.Length  S.Width P.Length  P.Width
    #>      4.3      2.0      1.0      0.1      7.9      4.4      6.9      2.5

    aggr(imp_irmi, delimiter = "imp")

The plot indicates that all missing values have been imputed by the IRMI algorithm. The following table displays the rounded first five results of the imputation for all variables.