Brms: How to extract posterior draws of a function following a Gaussian process prior

Hello, this may be a very silly question but I try to get the posterior draws of a function that is modeled using a Gaussian process prior. I tried getting the draws via the ‘as.data.frame’ function, but the scale of the draws seems to be off (which is probably because the draws are on a z-scale?) but also the function shape seems to be somewhat off. The example below comes form the brms documentation

set.seed(123)
dat <- mgcv::gamSim(1, n = 50, scale = 2)
# fit a simple GP model
fit1 <- brm(y ~ gp(x2), dat, chains = 2)
me1 <- conditional_effects(fit1, ndraws = 100, spaghetti = TRUE)
plot(me1, ask = FALSE, points = TRUE)

The plot shows posterior realizations of f(x2) as blue lines but I (thus) wonder how to get these draws for the observed grid of x2. They also don’t seem to be in the ‘me1’ object.

Thanks in advance your help. In case I overlooked this from the documentation, my apologies.

You could also try tidybayes approach Extracting and visualizing tidy draws from brms models • tidybayes. The example uses linear models, but add_epred_draws works for gp and spline models, too. I have used this approach with many models + posteriors successfully

Indeed this seems useful. Thanks.

I checked this and it seems to work. However, the model that I actually work with also has a linear term so that I can separately model the linear and the nonlinear effect of x2:

set.seed(123)
dat <- mgcv::gamSim(1, n = 50, scale = 2)
# fit a simple GP model
fit1 <- brm(y ~ x2  + gp(x2), dat, chains = 2)

In this case, \code{add_epred_draws} provides draws of the combined linear and nonlinear effect of x2 it seems. And I don’t know how to correct for the linear term such that I also take the posterior dependency into account of the linear and nonlinear effect. So perhaps it’s not possible using brms but I should just use Stan.