data(abr1)
dat <- abr1$pos[,1970:1980]
cls <- factor(abr1$fact$class)
## fill zeros with NAs
dat <- mv.zene(dat)
## missing values summary
mv <- mv.stats(dat, grp=cls)
plot(mv$mv.grp.plot)
## fill NAs with mean
dat.mean <- mv.fill(dat,method="mean")
## fill NAs with median
dat.median <- mv.fill(dat,method="median")
## -----------------------------------------------------------------------
## fill NAs with user-defined methods: two examples given here.
## a.) Random imputation function:
rand <- function(x,...) sample(x[!is.na(x)], sum(is.na(x)), replace=TRUE)
## test this function:
(tmp <- dat[,1]) ## an vector with NAs
## get the randomised values for NAs
rand(tmp)
## fill NAs with method "rand"
dat.rand <- mv.fill(dat,method="rand")
## b.) "Low" imputation function:
"low" <- function(x, ...) {
max(mean(x,...) - 3 * sd(x,...), min(x, ...)/2)
}
## fill NAs with method "low"
dat.low <- mv.fill(dat, method="low")
## summary of imputed data set
df.summ(dat.mean)
Run the code above in your browser using DataLab