You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在brms中无需重拟合模型即可使用bayes_factor且排除先验样本干扰?

Great question! Let's break this down and find a solution that doesn't require refitting your models.

First, clarify the two types of Bayes factors in brms

  • Savage-Dickey Bayes factors (for point null hypotheses like beta = 0): These do require prior samples, which is why you enabled saving them with save_pars = save_pars(all = TRUE) or similar.
  • Model-comparison Bayes factors (comparing two full models): These rely on estimating the marginal likelihood of each model, typically via bridge sampling—and they don't need prior samples at all.

Why you might be running into issues

Saving prior samples shouldn't inherently break bayes_factor() for model comparison, but sometimes the extra stored data can cause unexpected behavior in older brms versions, or you might be accidentally triggering the Savage-Dickey workflow instead of model comparison.

Fixes without refitting models

1. Remove prior samples from your model objects (temporarily)

You can create copies of your model objects and strip out the prior samples before calling bayes_factor(). This ensures the function focuses on model comparison via bridge sampling:

# Create copies of your models
model1_clean <- model1
model2_clean <- model2

# Remove the prior_samples element
model1_clean$prior_samples <- NULL
model2_clean$prior_samples <- NULL

# Calculate model-comparison Bayes factor
bf <- bayes_factor(model1_clean, model2_clean)

2. Calculate marginal likelihoods directly with bridge_sampler()

If you want more control, you can use the bridgesampling package (which brms uses under the hood) to estimate marginal likelihoods for each model, then compute the Bayes factor manually:

library(bridgesampling)

# Estimate marginal likelihood for each model
ml_model1 <- bridge_sampler(model1)
ml_model2 <- bridge_sampler(model2)

# Compute Bayes factor
bf <- bayes_factor(ml_model1, ml_model2)

This method ignores prior samples entirely, as it only uses the posterior draws to estimate the marginal likelihood.

A quick note on best practices

Since you prefer WAIC/LOO, remember that those methods focus on predictive performance, while Bayes factors focus on marginal likelihood (model fit to the data averaged over priors). Both have their use cases, so it's good you're offering both options to users!

内容的提问来源于stack exchange,提问作者BKV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:34:34