Learn R Programming

JWileymisc (version 1.4.1)

moments: Estimate the first and second moments

Description

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.

Usage

moments(data, ...)

Value

A list containing the esimates from the EM algorithm.

mu

A named vector of the means.

sigma

The covariance matrix.

Arguments

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.

Author

Suggested by Yves Rosseel author of the lavaan package on which this depends

See Also

SEMSummary

Examples

Run this code
# 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