I simulated lognormal data, where the variation on the log scale is constant which I believe represents the lognormal family assumptions in brms.
When I simulated this data I recovered all parameters almost exactly, but when plotting the y obs by average yobs - yrep I see some trend. Is this normal?
I then used my model on my actual data and got the same trend in this plot equivalent to the “error_scatter_avg” in the pp_check. Should it be like this or is something wrong?
Or is this plot more informative:
ppc_error_scatter_avg(
y = log(data2$y),
yrep = post_pred,
x = colMeans(log_epred) # Use the expected values (no noise) here
)
)
I know that posterior_predredict does the values on the raw scale so I used posterior_linpred which is the log_y and added the noise manually
# residual plot: log scale posterior predictive draws
posterior_draws <- as_draws_df(m1) |> as.data.frame() # posterior draws
log_epred <- posterior_linpred(m1, re.form = NULL)
post_pred <- log_epred
post_pred[] <- NA
# add noise
for( i in 1:nrow(post_pred)) {
sd_group1 <- exp(posterior_draws$b_sigma_group1[i])
names(sd_group1) <- "group1"
sd_group2 <- exp(posterior_draws$b_sigma_group2[i])
names(sd_group2) <- "group2"
indx <- data2$group
sd <- c(sd_group1 , sd_group2)[indx ]
mu_ <- log_epred[i,]
# alpha & beta
post_pred[i,] <- rnorm(n = length(mu_), mean = mu_, sd = sd)
}
ppc_error_scatter_avg(
y = log(data2$y),
yrep = post_pred
)
Shouldn’t a lognormal model in brms produce homoscedastic residuals on the log scale, given its assumption of Gaussian errors, or is some heteroscedastic trend expected?



