Impute.data.frame {COINr} | R Documentation |

## Impute a data frame

### Description

Impute a data frame using any function, either column-wise, row-wise or by the whole data frame in one shot.

### Usage

```
## S3 method for class 'data.frame'
Impute(
x,
f_i = NULL,
f_i_para = NULL,
impute_by = "column",
normalise_first = NULL,
directions = NULL,
warn_on_NAs = TRUE,
...
)
```

### Arguments

`x` |
A data frame with only numeric columns. |

`f_i` |
A function to use for imputation. By default, imputation is performed by simply substituting
the mean of non- |

`f_i_para` |
Any additional parameters to pass to |

`impute_by` |
Specifies how to impute: if |

`normalise_first` |
Logical: if |

`directions` |
A vector of directions: either -1 or 1 to indicate the direction of each column
of |

`warn_on_NAs` |
Logical: if |

`...` |
arguments passed to or from other methods. |

### Details

This function only accepts data frames with all numeric columns. It imputes any `NA`

s in the data frame
by invoking the function `f_i`

and any optional arguments `f_i_para`

on each column at a time (if
`impute_by = "column"`

), or on each row at a time (if `impute_by = "row"`

), or by passing the entire
data frame to `f_i`

if `impute_by = "df"`

.

Clearly, the function `f_i`

needs to be able to accept with the data class passed to it - if
`impute_by`

is `"row"`

or `"column"`

this will be a numeric vector, or if `"df"`

it will be a data
frame. Moreover, this function should return a vector or data frame identical to the vector/data frame passed to
it except for `NA`

values, which can be replaced. The function `f_i`

is not required to replace *all* `NA`

values.

COINr has several built-in imputation functions of the form `i_*()`

for vectors which can be called by `Impute()`

. See the
online documentation for more details.

When imputing row-wise, prior normalisation of the data is recommended. This is because imputation
will use e.g. the mean of the unit values over all indicators (columns). If the indicators are on
very different scales, the result will likely make no sense. If the indicators are normalised first,
more sensible results can be obtained. There are two options to pre-normalise: first is by setting
`normalise_first = TRUE`

- this is anyway the default if `impute_by = "row"`

. In this case, you also
need to supply a vector of directions. The data will then be normalised using a min-max approach
before imputation, followed by the inverse operation to return the data to the original scales.

Another approach which gives more control is to simply run `Normalise()`

first, and work with the
normalised data from that point onwards. In that case it is better to set `normalise_first = FALSE`

,
since by default if `impute_by = "row"`

it will be set to `TRUE`

.

Checks are made on the format of the data returned by imputation functions, to ensure the
type and that non-`NA`

values have not been inadvertently altered. This latter check is allowed
a degree of tolerance for numerical precision, controlled by the `sfigs`

argument. This is because
if the data frame is normalised, and/or depending on the imputation function, there may be a very
small differences. By default `sfigs = 9`

, meaning that the non-`NA`

values pre and post-imputation
are compared to 9 significant figures.

### Value

An imputed data frame

### Examples

```
# a df of random numbers
X <- as.data.frame(matrix(runif(50), 10, 5))
# introduce NAs (2 in 3 of 5 cols)
X[sample(1:10, 2), 1] <- NA
X[sample(1:10, 2), 3] <- NA
X[sample(1:10, 2), 5] <- NA
# impute using column mean
Impute(X, f_i = "i_mean")
# impute using row median (no normalisation)
Impute(X, f_i = "i_median", impute_by = "row",
normalise_first = FALSE)
```

*COINr*version 1.1.14 Index]