logLik in path model

49 views
Skip to first unread message

Sun Beom Kwon

unread,
Mar 2, 2024, 12:21:05 AM3/2/24
to lavaan
Hi,

I am having trouble with reproducing the log likelihood of a saturated path model.

Here is the code I used to fit a very simple saturated path model, and to extract the log-likelihood:

model_temp <- '
x1 ~ x2 + x3
'
fit_temp <- sem(model = model_temp, data = data)
summary(fit_temp)
S <- lavInspect(fit_temp, "obs")$cov # sample covariance matrix
Sigma <- lavInspect(fit_temp, "cov.ov") # model implied covariance matrix

logLik(fit_temp)

- n / 2 * (nrow(S) * log(2 * pi) + log(det(Sigma)) + nrow(S))

- n / 2 * log(det(Sigma)) - n / 2 * sum(diag(S %*% solve(Sigma))) - n / 2 * nrow(S) * log(2 * pi)


I was expecting to get exactly the same value from logLik(fit_temp) and the manual way of calculating the log-likelihood. Is lavaan calculating the log-likelihood differently for a saturated path model?


Shu Fai Cheung (張樹輝)

unread,
Mar 2, 2024, 12:54:44 AM3/2/24
to lavaan
I believe it is due to the fixed.x option, which is TRUE by default. Here is how logLik works (for multivariate normal with no missing data) with fixed.x = TRUE:


You can try this:

fit_temp <- sem(model = model_temp, data = data, fixed.x = FALSE)

The values should then be the same.

-- Shu Fai

Reply all
Reply to author
Forward
0 new messages