@@ -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
0 commit comments