moments | R Documentation |
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, ...)
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 |
A list containing the esimates from the EM algorithm.
mu |
A named vector of the means. |
sigma |
The covariance matrix. |
Suggested by Yves Rosseel author of the lavaan package on which this depends
SEMSummary
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.