#
A “minimal” reproducible example

First generate some sample data, `d`

.

```
library(tidyverse)
library(brms)
library(patchwork)
set.seed(1)
d <-
expand_grid(
id = factor(1:10),
time = 1:4
) %>%
mutate(
z = rep(rnorm(10, sd = 0.5), each = 4),
Y = 1.5 + 0.5 * time + z + rnorm(40, sd = 0.2),
)
ggplot(d, aes(time, Y, color = id)) +
geom_line() +
geom_point()
```

##
Model the “real” data (M1)

Fit the model to the data with `brm`

and show fixed and random estimates.

```
M1 <- brm(Y ~ 1 + time + (1 | id),
data = d, family = gaussian(),
iter = 1000)
plot_posterior_draws <- function(M) {
rand <- gather_draws(M, r_id[id,]) %>%
mutate(id = factor(id)) %>%
ggplot(aes(x = .value, y=id)) +
stat_halfeye() +
labs(title="Random")
fix <- gather_draws(M, b_Intercept, b_time) %>%
ggplot(aes(x = .value, y=.variable)) +
stat_halfeye() +
labs(title="Fixed")
rand / fix + plot_layout(heights = c(3,1))
}
plot_posterior_draws(M1)
```

##
Estimate random effects for hypothetical observations (M2)

I want to estimate random effects for hypothetical observations where I only have one observation in an individual.

First, generate hypothetical observations and give each a unique id (above 100) to distinguish it from real data.

```
hypothetical_data_for_predictions <-
expand_grid(time = 1:4, Y = 1:5) %>%
mutate(id = factor(row_number() + 100),
weight = 0)
```

Then, add this hypothetical data to the real data and fit the model again.

```
d_w_hypothetical <-
d %>%
mutate(weight = 1) %>%
bind_rows(hypothetical_data_for_predictions)
M2 <- brm(Y ~ 1 + time + (1 | id),
data = d_w_hypothetical, family = gaussian(),
iter = 1000)
plot_posterior_draws(M2)
```

Here we get estimates for the random effect for the hypothetical data (id 100:120), but adding the hypothetical data of course changes the rest of the model as well.

##
Attempt to avoid that hypothetical data changes the model (M3)

I attempted to use weights (1 for real data and 0 for hypothetical data) to avoid having the hypothetical data impact the model fit, but now random effects aren’t estimated either.

```
M3 <- brm(Y | weights(weight) ~ 1 + time + (1 | id),
data = d_w_hypothetical, family = gaussian(),
iter = 1000)
plot_posterior_draws(M3)
```

I also tried giving the hypothetical data a weight of 1e-6, but that gives a similar result to `M3`

.

Intuitively, I would think that only the relative weights within a group should impact the random effect estimate for that group.