paul-buerkner / brms

brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
https://paul-buerkner.github.io/brms/
GNU General Public License v2.0
1.27k stars 182 forks source link

Inconsistency in `standata()` output between `resp_weights()` and `resp_trials()` #1542

Closed fweber144 closed 1 year ago

fweber144 commented 1 year ago

I noticed an inconsistency in the standata() output between resp_weights() and resp_trials() if the data variable containing the weights is missing:

library(brms)

# Gaussian with resp_weights():
Puromycin$wobs <- rep_len(5:3, nrow(Puromycin))
bfit_gau <- brm(
  formula = rate | weights(wobs) ~ conc + state,
  data = Puromycin,
  chains = 1,
  iter = 500,
  seed = 2052109,
  refresh = 0
)
Puromycin_new <- head(Puromycin, 3)
Puromycin_new$wobs <- NULL
sdata_gau <- standata(bfit_gau, newdata = Puromycin_new)
## --> Runs through and gives weights which are constantly 1:
all(sdata_gau$weights == 1)
## --> TRUE

# Binomial with resp_trials():
N_bin <- 100
data_bin <- data.frame(wobs = rep_len(5:3, N_bin))
set.seed(57346)
data_bin$success <- rbinom(N_bin, size = data_bin$wobs, prob = 0.4)
bfit_bin <- brm(
  formula = success | trials(wobs) ~ 1,
  data = data_bin,
  family = binomial(),
  chains = 1,
  iter = 500,
  seed = 2052109,
  refresh = 0
)
data_bin_new <- head(data_bin, 3)
data_bin_new$wobs <- NULL
sdata_bin <- standata(bfit_bin, newdata = data_bin_new)
## --> Error: The following variables can neither be found in 'data' nor in 'data2':
##     'wobs'

Perhaps this is intentioned or not avoidable, but I thought it would be better to report. I guess the error thrown in the resp_trials() case is the safer solution compared to the silently used vector of 1s.

paul-buerkner commented 1 year ago

Weights are largely ignored in post-processing as they are relevant only during inference (and log_lik computations). Thus, valid_newdata currently does not care if weights are provided or not. The number of trials in binomial models always affects predictions and thus needs to be present. Does this answer your question?

fweber144 commented 1 year ago

Ok, I was suspecting this to be rather a design issue, so I'm fine with leaving this as-is.

paul-buerkner commented 1 year ago

Okay. Thank you checking in about this!