moments: Estimate the first and second moments

View source: R/descriptives.R

momentsR Documentation

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, ...)

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.

Value

A list containing the esimates from the EM algorithm.

mu

A named vector of the means.

sigma

The covariance matrix.

Author(s)

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

See Also

SEMSummary

Examples

# 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)

JWileymisc documentation built on Oct. 5, 2023, 5:06 p.m.