# Why do the posterior predictions from fitted model differ from predictions from the same data as "newdata"

**URL:** <https://discourse.mc-stan.org/t/why-do-the-posterior-predictions-from-fitted-model-differ-from-predictions-from-the-same-data-as-newdata/35259>\
**Category:** Modeling\
**Tags:** brms\
**Created:** [May 26, 2024, 7:54am UTC](https://discourse.mc-stan.org/t/why-do-the-posterior-predictions-from-fitted-model-differ-from-predictions-from-the-same-data-as-newdata/35259 "2024-05-26T07:54:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitangus](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@pitangus](https://discourse.mc-stan.org/u/pitangus)\
**Post date:** [May 26, 2024, 7:54am UTC](https://discourse.mc-stan.org/t/why-do-the-posterior-predictions-from-fitted-model-differ-from-predictions-from-the-same-data-as-newdata/35259/1 "2024-05-26T07:54:20Z")

</div>

I fitted stimulated count data with the brms code given below. Then I made two predictions with the tidybayes function "add\_predicted\_draws: first the posterior predictions directly from the fitted model and second using the original fitted data as “newdata”.  
The predicted summed, means and standard deviations of the counts were different and I can’t work out why.  
I thought of comparing the posterior predictions to new data with different covariate values, but if they predict by a different process then they won’t be legitimate comparisons

```r
library("brms")
library(readr)
library(dplyr)
library(bayesplot)
library(tidybayes)
library(boot)
library(stats)
library(ggplot2)
library(tidyr)
library("RcppParallel")
library(priorsense)
library(cmdstanr)

# fit data
zifamily = zero_inflated_poisson(link = "log", link_zi = "logit")

zip_prior <- c(prior(normal(0,8), class = b),
			   prior(lkj(1), class = cor),
			   prior(student_t(3, 0, 10), class = sd),
			   prior(logistic(0,1), class = Intercept, dpar = zi),
			   prior(student_t(3, 0, 10), class = sd, dpar = zi))
sp_formula = bf(count ~ 0 + Intercept + crop01 + crop02 + crop03 + crop04 + crop05 + (1 || days) + (1 || site) + (1 | g1 | species), zi ~ (1 | g1 | species))

fit <-
  brm(data = my_data,
      family = zifamily,
      formula = sp_formula,
      prior = zip_prior,
      iter = 2000, warmup = 1000, thin = 4, chains = 4, cores = 4,
  	control = list(adapt_delta = .99),
  	threads = threading(8, grainsize = 100),
      seed = 999,
  	  # refresh = 0,
  	backend = "cmdstanr",
  	sample_prior = FALSE
  	)

# predictions

apd = add_predicted_draws(fit$data, fit, ndraws = 1, value = 'pcount')

pred <- add_predicted_draws(fit,
					newdata = newdata, 
					ndraws = 1,
					value = "new_prediction",
					seed = 999,
					summary = FALSE,
					re_formula = ~ 0 + Intercept + crop01 + crop02 + crop03 + crop04 + crop05 + (1||sday) + (1||site) + (1|g1|species),
					allow_new_levels = TRUE)

pred_df = tibble(site = pred$site, species = pred$species, 
					   days = pred$days, data_pred = apd$pcount, 
					   predicted = pred$new_prediction)

```
