Multilevel SEM: lavaan sets negative variances in (sample) covariance matrix to 0

63 views
Skip to first unread message

Julia Walther

unread,
Sep 11, 2023, 10:51:52 AM9/11/23
to lavaan
Hi,

I noticed that in mulitlevel SEM, in the estimate of the between-group covariance matrix (Sigma_B) negative variances and related covariances  are set to 0. However, I did not find any information on this procedure in the official sources. Did I miss any sources? What is the reason for this procedure? I put a minimal example below.

Thanks in advance!

Best,
Julia


#######################

library(lavaan)
library(Morpho)
set.seed(1499)

# set population model (unstructured bivariate model)
Sigma_W <- "y ~~ 1*y;
            x ~~ 1*x;
            y ~~ 0.5*x"
Sigma_B <- "y ~~ 0.2*y;
            x ~~ 0.2*x;
            y ~~ 0.1*x"

# set sample sizes
n <- 4 # cluster size
g <- 5 # number of clusters

data_W <- simulateData(Sigma_W, sample.nobs=n*g, model.type="lavaan")
data_B <- simulateData(Sigma_B, sample.nobs=g, model.type="lavaan")
groups <- rep(1:g, each=n) # grouping variable
data_T <- data_W # create final total data matrix (that we would sample) in the following
data_T[,] <- 0
for (j in unique(groups)){
  for (i in min(which(groups == j)):max(which(groups == j)))
    data_T[i,] <- data_W[i,] + data_B[j,]
}
data_T$groups <- as.factor(groups)
data_T$persons <- rep(1:n, g)


# estimate model (same as population model)
model <- '
level: 1
y ~~ y
x ~~ x
y ~~ x
level: 2
y ~~ y
x ~~ x
y ~~ x
'
fit <- sem(model, data = data_T, cluster = "groups")

# estimate (sample) covariance matrices (I rebuild lavaans estimation of the between-group level sample covariance matrices here, see also Muthen 1989, 1990, 1994 for the estimators)
S_pw <- covW(data=data_T[,1:2], groups=data_T[,3])
group_means <- attr(S_pw, "means")
attr(S_pw, "means") <- NULL
grand_means <- colMeans(data_T[,1:2], na.rm = TRUE)
datac <- t(t(group_means) - grand_means)
S_B <- lavaan:::lav_matrix_crossprod(datac * n, datac)/(g - 1)
Sigma_B <- (S_B - S_pw) / n

# compare the Sigma_B
Sigma_B # Muthen (1989, 1990, 1994)
fit@SampleStats@YLp[[1]][[2]][["Sigma.B"]] # lavaan's implementation of Muthen (1989, 1990, 1994)

# the variance of x, and the related covariance xy, are set to zero in lavaan
#######################

References:
Muthén, B. O. (1989). Latent variable modeling in heterogeneous populations. Psychometrika, 54(4), 557–585. https://doi.org/10.1007/BF02296397
Muthén, B. O. (1990). Mean and covariance structure analysis of hierarchical data. 88.
Muthén, B. O. (1994). Multilevel Covariance Structure Analysis. Sociological Methods & Research, 22(3), 376–398. https://doi.org/10.1177/0049124194022003006


Terrence Jorgensen

unread,
Sep 15, 2023, 7:05:40 AM9/15/23
to lavaan
I believe (hope) this paper has some information about the necessity of Sigma_B being positive definite (even the model-implied one in a more restrictive model), but that might depend on the estimator.


Terrence D. Jorgensen
Assistant Professor, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Julia Walther

unread,
Sep 16, 2023, 5:40:02 AM9/16/23
to lavaan
Thank you for your answer, Terrence! :)

I summarize what I found/inferred:

The default in multilevel SEM is the quasi-Newton algorithm (QN, p. 137), which I also used in my example. When θ_B (between-level residual variances which may be comparable with between-level variances in the unstructured model in my example) is negative, non-convergence might occur, but “lavaan does not restrict θ_B estimates to be positive when using QN” (p. 138). Thus, I assume that lavaans dealing with negative variances at the between-level have changed by now, and that negative variances are set to 0 to prevent non-convergence issues (similar to the procedure in MPLUS, see p. 138).

Further, I assume that this procedure has nothing to do with positive definiteness. When negative variances and related covariances are set to 0, this does not yield a positive definite covariance matrix. It only prevents a matrix that has only negative or negative and zero eigenvalues (i.e., is negative semi-definite). Thus, matrices can be positive definite, positive-semi definite and indefinite. Further, according to the paper only the EM algorithm requires a positive definite between-level model-implied covariance matrix (p. 138).

Best,
Julia
Reply all
Reply to author
Forward
0 new messages