Last chance! 50% off unlimited learning
Sale ends in
This function relies on the lavaan package to use the Expectation Maximization (EM) algorithm to estimate the first and second moments (means and [co]variances) when there is missing data.
moments(data, ...)
A data frame or an object coercable to a data frame. The means and covariances of all variables are estimated.
Additional arguments passed on to the estimate.moments.EM
function in lavaan. Note this is not an exported function.
A list containing the esimates from the EM algorithm.
A named vector of the means.
The covariance matrix.
# NOT RUN {
# sample data
Xmiss <- as.matrix(iris[, -5])
# make 25% missing completely at random
set.seed(10)
Xmiss[sample(length(Xmiss), length(Xmiss) * .25)] <- NA
Xmiss <- as.data.frame(Xmiss)
# true means and covariance
colMeans(iris[, -5])
# covariance with n - 1 divisor
cov(iris[, -5])
# means and covariance matrix using list wise deletion
colMeans(na.omit(Xmiss))
cov(na.omit(Xmiss))
# means and covariance matrix using EM
moments(Xmiss)
# clean up
rm(Xmiss)
# }
Run the code above in your browser using DataLab