Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 24 additions & 37 deletions R/reliablity_single_item.R
Original file line number Diff line number Diff line change
Expand Up @@ -598,47 +598,34 @@ calcAVE <- function(lambda.std, lV) {
}


getCompositeDenominator <- function(lV, parTable, cfa, scale.corrected) {
measr <- parTable[parTable$lhs == lV & parTable$op == "=~", ]
inds <- unique(measr$rhs)
mats <- lavaan::lavInspect(cfa, what = "coef")
if (scale.corrected) {
sum(as.vector(mats$lambda[inds, lV]))
} else {
length(inds) # rowMeans: divide sum by k
}
}
getCompositeRVCOV <- function(lVs, parTable, cfa, scale.corrected) {
matrices <- lavaan::lavInspect(cfa, what = "coef")
lambda <- matrices$lambda
theta <- matrices$theta

inds <- rownames(lambda)[apply(lambda, MARGIN = 1L, FUN = \(x) any(x != 0))]
lambda <- lambda[inds, lVs, drop = FALSE]
theta <- theta[inds, inds, drop = FALSE]

getCompositeRVCOV <- function(lVs, parTable, cfa, scale.corrected) {
# Build Theta blocks and denominators
mats <- lavaan::lavInspect(cfa, what = "coef")
thetaF <- mats$theta
I <- lambda
I[I!=0] <- 1

# item lists per LV
item_list <- lapply(lVs, function(lv)
unique(parTable[parTable$lhs == lv & parTable$op == "=~", "rhs"])
)
names(item_list) <- lVs
T <- t(I) %*% theta %*% I

# denominators D_f
denom <- vapply(lVs, function(lv)
getCompositeDenominator(lv, parTable, cfa, scale.corrected),
numeric(1)
)
FUN <- if (scale.corrected) sum else \(x) sum(x != 0)

p <- length(lVs)
resCov <- matrix(0, p, p, dimnames = list(lVs, lVs))

for (i in seq_len(p)) {
Ii <- item_list[[i]]
for (j in i:p) {
Ij <- item_list[[j]]
block_sum <- sum(thetaF[Ii, Ij, drop = FALSE])
val <- block_sum / (denom[i] * denom[j])
resCov[i, j] <- val
resCov[j, i] <- val
}
l <- apply(lambda, MARGIN = 2L, FUN = FUN)
l.inv <- 1 / l
l.inv[l <= 0] <- 1L
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve denominator sign when inverting loadings

Overwriting l.inv for all l <= 0 silently changes the scaling for any factor whose corrected denominator is negative (or exactly zero), so L.inv %*% T %*% L.inv no longer matches the intended per-factor normalization and can emit incorrect ~~ terms in generated syntax for models with negative/canceling loadings. The previous implementation divided by each factor’s actual denominator, so this guard should not collapse all non-positive values to 1.

Useful? React with 👍 / 👎.


if (length(l.inv) > 1) {
L.inv <- diag(l.inv)
dimnames(L.inv) <- list(names(l.inv), names(l.inv))

} else {
L.inv <- matrix(l.inv, nrow = 1, ncol = 1)
dimnames(L.inv) <- list(names(l.inv), names(l.inv))
}
resCov

L.inv %*% T %*% L.inv
}
Loading