Skip to content

Commit 23db309

Browse files
committed
fix ordering of two-level lv samples in situations where the rows of the dataset are not arranged by cluster
1 parent d4808bd commit 23db309

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: blavaan
22
Title: Bayesian Latent Variable Analysis
3-
Version: 0.5-9.1376
3+
Version: 0.5-9.1377
44
Authors@R: c(person(given = "Edgar", family = "Merkle",
55
role = c("aut", "cre"),
66
email = "merklee@missouri.edu",

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
## Bugs/glitches:
66
* For some ordinal models with no latent variables, information criteria are incorrect (inflated effective number of parameters).
77

8+
* For two-level datasets where rows are not ordered by cluster, latent variable predictions may not be ordered as expected.
9+
810
# Version 0.5-8
911
## New features
1012
* This release contains minor improvements and bug fixes.

R/lav_export_stanmarg.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,12 +1256,13 @@ lav2standata <- function(lavobject, dosam = FALSE) {
12561256
cidx[[g]] <- cidx[[g]] + max(cidx[[(g - 1)]])
12571257
}
12581258
}
1259-
cidx <- unlist(cidx)
1259+
cidx <- unlist(cidx) ## FIXME assumes group 2 observations come after group 1 observations
12601260
}
12611261
mean_d_full <- rowsum.default(as.matrix(dat$YX), cidx) / dat$cluster_size
12621262

12631263
tmpYX <- split.data.frame(dat$YX, cidx)
12641264
dat$YX <- do.call("rbind", tmpYX)
1265+
dat$orig_id <- unlist(split(1:nrow(dat$YX), cidx))
12651266
dat$log_lik_x_full <- llx_2l(Lp[[1]], dat$YX, mean_d_full, cidx)
12661267
dat$mean_d_full <- lapply(1:nrow(mean_d_full), function(i) mean_d_full[i, dat$between_idx])
12671268

R/lvgqs.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ samp_lvs_2lev <- function(mcobj, lavmodel, lavsamplestats, lavdata, lavpartable,
303303
between.idx <- Lp$between.idx[[2]]
304304

305305
if(length(between.idx) > 0L){
306-
YX.B[, between.idx] <- stanorig$YX[!duplicated(Lp$cluster.idx[[2]]), between.idx]
306+
YX.B[, between.idx] <- stanorig$YX[order(standata$orig_id),][!duplicated(Lp$cluster.idx[[2]]), between.idx] #stanorig$YX[!duplicated(Lp$cluster.idx[[2]]), between.idx]
307307
}
308308

309309
## manipulations to reuse existing lvgqs code
@@ -326,6 +326,7 @@ samp_lvs_2lev <- function(mcobj, lavmodel, lavsamplestats, lavdata, lavpartable,
326326

327327
## now level 1
328328
standata <- stanorig
329+
## the YX matrix has been ordered by cluster already:
329330
clusidx <- rep(1:length(standata$cluster_size), standata$cluster_size)
330331
standata$YX <- with(standata, YX[, between_idx[(N_between + 1):p_tilde]]) - clusmns[clusidx,]
331332
modmat1 <- modmats[2 * (1:standata$Ng) - 2 + 1]
@@ -346,10 +347,14 @@ samp_lvs_2lev <- function(mcobj, lavmodel, lavsamplestats, lavdata, lavpartable,
346347
}
347348

348349
etasamps <- do.call(funcall, loop.args)
349-
350350
etaout <- vector("list", 2)
351+
idmap <- standata$orig_id ## to put the lvs back in their original order
351352
for (i in 1:2) {
352-
tmpeta <- lapply(etasamps, function(x) x[[i]])
353+
if (i == 1) {
354+
tmpeta <- lapply(etasamps, function(x) x[[i]][, order(idmap), ])
355+
} else if (i == 2) {
356+
tmpeta <- lapply(etasamps, function(x) x[[i]])
357+
}
353358
tmpN <- ifelse(i==1, standata$Ntot, sum(standata$nclus[,2]))
354359
tmpw9 <- ifelse(i==1, standata$w9use + standata$w9no, standata$w9use_c + standata$w9no_c)
355360

R/stanmarg_data.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ stanmarg_data <- function(YX = NULL, S = NULL, YXo = NULL, N, Ng, grpnum, # data
275275
Ndum = NULL, dum_ov_idx = NULL, dum_lv_idx = NULL, # for bsam
276276
Ndum_x = NULL, dum_ov_x_idx = NULL, dum_lv_x_idx = NULL,
277277
measnblk = NULL, measblkse = NULL, measorder = NULL, measrevord = NULL,
278-
ngh = NULL, ghnode = NULL, ghwt = NULL,
278+
ngh = NULL, ghnode = NULL, ghwt = NULL, orig_id = NULL,
279279
...) {
280280

281281
dat <- list()
@@ -324,6 +324,7 @@ stanmarg_data <- function(YX = NULL, S = NULL, YXo = NULL, N, Ng, grpnum, # data
324324
dat$ngh <- ngh
325325
dat$ghnode <- ghnode
326326
dat$ghwt <- ghwt
327+
dat$orig_id <- orig_id
327328

328329
dat$use_suff <- 1L
329330
if (ord | multilev) dat$use_suff <- 0L

0 commit comments

Comments
 (0)