Learn R Programming

filling (version 0.2.4)

fill.simple: Imputation by Simple Rules

Description

One of the most simplest ways to fill in the missing entries is to apply any simple rule for each variable. In this example, we provide 3 options, "mean", "median", and "random". It assumes that every column has at least one non-missing entries in that for each column, the rule is applied from the subset of non-missing values.

Usage

fill.simple(A, method = c("mean", "median", "random"))

Value

a named list containing

X

an \((n\times p)\) matrix after completion.

Arguments

A

an \((n\times p)\) partially observed matrix.

method

simple rule to fill in the missing entries in a columnwise manner.

References

gelman_data_2007filling

Examples

Run this code
## load image data of 'lena128'
data(lena128)

## transform 5% of entries into missing
A <- aux.rndmissing(lena128, x=0.05)

## apply all three methods#'
fill1 <- fill.simple(A, method="mean")
fill2 <- fill.simple(A, method="median")
fill3 <- fill.simple(A, method="random")

## visualize
opar <- par(no.readonly=TRUE)
par(mfrow=c(2,2), pty="s")
image(A, col=gray((0:100)/100), axes=FALSE, main="original")
image(fill1$X, col=gray((0:100)/100), axes=FALSE, main="method:mean")
image(fill2$X, col=gray((0:100)/100), axes=FALSE, main="method:median")
image(fill3$X, col=gray((0:100)/100), axes=FALSE, main="method:random")
par(opar)

Run the code above in your browser using DataLab