it appears that within blav_Predict(), tmpres produces posterior predictions for the predictions set, e.g., to the following values [,4:10]:
Israel political donation P IWAH Authority Fairness Harm Ingroup
1 0.5 0.8333333 0.00 0.42857143 0.1944444 0.3666667 0.2592593 0.2142857 0.4333333
2 0.0 0.6666667 0.09 0.28571429 0.4444444 0.5000000 0.6296296 0.7142857 0.3000000
3 0.5 0.8333333 0.00 0.04761905 0.4722222 0.6666667 0.8148148 0.8214286 0.8000000
4 0.0 0.3333333 1.00 0.26190476 0.5555556 0.5666667 0.5925926 0.7857143 0.5000000
Whereas the values I set the predictors to in newdata were [,4:10] in the following data frame:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
[2,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
[3,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
[4,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
What I was hoping for was for a way to sample predictions once the predictors were set to the same values across the different groups in the multi-group SEM, to be able to calculate contrast effects and HDI for the group differences in predictions based on the model.
Hi!
yes, it appears that the error occurs, because there are no latent variables in the model, which makes the third dimension of the sims[,,i] array empty.
By modifying:
1) the "blav_fill_newdata()" function to check for the presence of latent variables with "any(grepl("^eta",
object@ParTable$lhs))":
if (lvs && any(grepl("^eta",
object@ParTable$lhs))) { newlvs <- samp_lvs(
object@external$mcmcout,
object@Model,
object@ParTable, smd, eeta = NULL, categorical = FALSE) lvsumm <- as.matrix(rstan::monitor(newlvs, print=FALSE)) cmatch <- match(colnames(
object@external$stansumm), colnames(lvsumm)) stansumm <-
object@external$stansumm lvcols <- grep("^eta", rownames(stansumm)) if (length(lvcols) > 0) stansumm <- stansumm[-lvcols, ]
object@external$stansumm <- rbind(stansumm, lvsumm[,cmatch])
object@external$stanlvs <- newlvs }
and 2) the "samp_lvs()" function to return an empty array, if there are no latent variables:
if (standata$w9use + standata$w9no == 0) { nsamps <- nrow(mcmcout) nchain <- length(mcmcout) return(array(dim = c(nsamps, nchain, 0)))}
the "blavPredict(Models_latent_M1[[2]], type = "yhat", newdata = new_data)" function call no longer produces the "sim[,,i] index out of boundary" error with my model.
However, one thing puzzles me:
for the new data argument, I supplied the grand mean of the predictors across all levels of the grouping variable (in four rows for four group levels).
Yet, when I inspect the samples generated by type = "yhat" and "ypred", then the predictors differ in their values across the rows.
A printout of
from blav_fill_newdata() shows that internal to it, the newdata is correctly formated with the same grandmean for the predictors (and here supplied NA values for the outcome variables to be predicted by the model):
List of 4
$ : num [1, 1:10] NA NA NA 0.217 0.5 ...
$ : num [1, 1:10] NA NA NA 0.217 0.5 ...
$ : num [1, 1:10] NA NA NA 0.217 0.5 ...
$ : num [1, 1:10] NA NA NA 0.217 0.5 ...
I then inspected:
and it confirmed that the first three columns of the outcome variables were set to zero and the following columns were correctly set to their grandmeans (as I had provided in the newdata argument):
new_dat_fill <- blav_fill_newdata(Models_latent_M1[[2]], new_data)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
[2,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
[3,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
[4,] 0 0 0 0.2168682 0.4997795 0.5253086 0.6957672 0.7243008 0.4376543 0.4845679
I am right now trying to locate the exact place within blavPredict() where these grandmean predictors get overwritten by different values of the predictors of each row (which I try to avoid so that I can calculate contrast effects for the predicted values across rows).
Best,
Niels