Learn R Programming

SciencesPo (version 1.3.9)

randomImput: Simple Random Imputation

Description

Performs random imputation in a vector containing missing values.

Usage

randomImput(x)

Arguments

x
a vector whose missing values (NA) is to be replaced.

encoding

UTF-8

Details

Indeed a very simple but somewhat limited approach is to impute missing values from observed ones chosen at random with replacement (MCAR), assuming that $$p(R|Z_{obs}, Z_{mis}) = p(R|\phi)$$. Sampling with replacement is important since it continues to favor values with higher incidence (preserving the MCAR empirical distribution). It may also be combined with apply for matrix imputation drills, but keep in mind that it is experimental (actually, I wrote this for teaching purposes).

Examples

Run this code
x <- c(1,2,NA,4,5,NA)
randomImput(x)

if (interactive()) {
n = 100
mat <- matrix(ncol=3, nrow=n)
for(i in 1:n){
mu = mean(randomImput(x))
med = median(randomImput(x))
mod = Mode(randomImput(x))
mat[i,] <- c(mu, med, mod[1])
}
print(mat)
}

Run the code above in your browser using DataLab