improving doc for posterior_linpred with new levels #956
Closed
Description
It seems that posterior_linpred samples new levels of random effects only once per draw even if the new level varies and as such should be re-drawn multiple times.
Here is a minimal example:
library(brms)
library(mvtnorm)
library(dplyr)
set.seed(425435)
R <- matrix(c(1, 0.1, 0.1, 1), 2, 2, byrow=TRUE)
R
J <- 20
group_draw <- matrix(rmvnorm(J, sigma=R), ncol=2, dimnames=list(NULL, list("a", "b"))) %>%
as.data.frame() %>%
mutate(id=1:J)
group_draw
fake <- expand.grid(id=1:J, x=seq(0,1,length=11)) %>%
left_join(group_draw) %>%
arrange(id, x) %>%
mutate(y_hat= a + b * x, y = rnorm(n(), y_hat), id=factor(id))
head(fake)
fake
form <- bf(y ~ 1 + x + (1+x|id), center=FALSE)
get_prior(form, data=fake, family=gaussian)
model_prior <- prior(normal(0, 2), class=b, coef=Intercept) +
prior(normal(0, 1), class=b) +
prior(normal(0, 1), class=sd, coef=Intercept, group=id) +
prior(normal(0, 0.5), class=sd, coef=x, group=id)
fit <- brm(form, data=fake, prior=model_prior, family=gaussian, cores=4)
fit
df <- data.frame(id=factor((J+1) : (2*J)), x=c(0))
df
plin1 <- posterior_linpred(fit, newdata=df, allow_new_levels=TRUE)
plin2 <- posterior_linpred(fit, newdata=df, allow_new_levels=TRUE)
plin1[1,]
plin2[1,]The output is:
> plin1[1,]
[1] 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684
[8] 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684
[15] 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684 0.1073684
> plin2[2,]
[1] -0.1205381 -0.1205381 -0.1205381 -0.1205381 -0.1205381 -0.1205381
[7] -0.1205381 -0.1205381 -0.1205381 -0.1205381 -0.1205381 -0.1205381
[13] -0.1205381 -0.1205381 -0.1205381 -0.1205381 -0.1205381 -0.1205381
[19] -0.1205381 -0.1205381So the random effect which varies by id is kept constant for an entire draw, but that's just not right as the id varies across the data set which I wish to simulate.
Is this a user error or a bug in brms?
The above code ran with brms 2.13.0, but it is the same misery in 2.10.0
As the new data set as many new id's I would expect that I get a newly drawn random effect for each row of my data set.