# Compute Bayes factor from cmdstan output using bridgesampling package (error message)

**URL:** <https://discourse.mc-stan.org/t/compute-bayes-factor-from-cmdstan-output-using-bridgesampling-package-error-message/21915>\
**Category:** Modeling\
**Tags:** cmdstan, bayes-factor, techniques\
**Created:** [April 16, 2021, 8:01am UTC](https://discourse.mc-stan.org/t/compute-bayes-factor-from-cmdstan-output-using-bridgesampling-package-error-message/21915 "2021-04-16T08:01:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scijens](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/scijens/32/9695_2.png) [@scijens](https://discourse.mc-stan.org/u/scijens)\
**Post date:** [April 16, 2021, 8:01am UTC](https://discourse.mc-stan.org/t/compute-bayes-factor-from-cmdstan-output-using-bridgesampling-package-error-message/21915/1 "2021-04-16T08:01:05Z")

</div>

Hi everyone,

I ran several computationally intense models (~2-3 weeks/model; so it’s not desirable to re-run these models) with CmdStan and want to compute the Bayes factor for these models. I came across the [bridgesampling package](https://cran.r-project.org/web/packages/bridgesampling/index.html). I imported the .csv output files from CmdStan to R using :

```
files <- c("output_1.csv", "output_2.csv")
out = read_stan_csv(files)

```

When I run

```
library(bridgesampling)
bridge_sampler(out)

```

I get the following error message:

```
Error in .local(object, ...) : 
  the model object is not created or not valid

```

How can I get around this error message? Is there any workaround to compute the Bayes factor without rerunning the models in RStan?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [April 16, 2021, 8:07am UTC](https://discourse.mc-stan.org/t/compute-bayes-factor-from-cmdstan-output-using-bridgesampling-package-error-message/21915/2 "2021-04-16T08:07:44Z")

</div>

bridgesampling isn’t compatible with `cmdstan` unfortunately, there’s more discussion and a possible workaround on their github: [bridgesampling on cmdstanr · Issue #27 · quentingronau/bridgesampling · GitHub](https://github.com/quentingronau/bridgesampling/issues/27)

---

<div class="post-metadata">

**Author:** ![scijens](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/scijens/32/9695_2.png) [@scijens](https://discourse.mc-stan.org/u/scijens)\
**Post date:** [April 16, 2021, 8:16am UTC](https://discourse.mc-stan.org/t/compute-bayes-factor-from-cmdstan-output-using-bridgesampling-package-error-message/21915/3 "2021-04-16T08:16:29Z")

</div>

Not the desired answer, but a very helpful one, thanks a lot!

---

<div class="post-metadata">

**Author:** ![scijens](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/scijens/32/9695_2.png) [@scijens](https://discourse.mc-stan.org/u/scijens)\
**Post date:** [April 16, 2021, 11:31am UTC](https://discourse.mc-stan.org/t/compute-bayes-factor-from-cmdstan-output-using-bridgesampling-package-error-message/21915/4 "2021-04-16T11:31:04Z")

</div>

Update: With the workaround suggested (create an empty Stanfit object), the bridge\_sampler command was running for an hour. Then it outputs the following:

```
> bridge_sampler(out, new_mod)
Iteration: 1
Iteration: 2
Iteration: 1
Error in jj[2,] : Index out of bounds

1: Infinite value in iterative scheme, returning NA.
Try rerunning with more samples.
2: logml could not be estimated within maxiter, rerunning with adjusted starting value.
Estimate might be more variable than usual.

```

Here is my code:

```
# Load packages

library(rstan)
library(bridgesampling)

# Load posterior output from the cmdstan estimation

files <- c("output_1.csv")
out = read_stan_csv(files)

data_stan = rstan::read_rdump("full_data.R");

new_mod <- stan(file = "hmm.stan", data = data_stan, iter = 0)

bridge_sampler(out, new_mod)

```

Any ideas what’s happening?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [April 16, 2021, 11:50am UTC](https://discourse.mc-stan.org/t/compute-bayes-factor-from-cmdstan-output-using-bridgesampling-package-error-message/21915/5 "2021-04-16T11:50:00Z")

</div>

Try the development version of `bridgesampling`:

```r
devtools::install_github("quentingronau/bridgesampling@master")

```

If that also doesn’t work then you’ll need to open an issue on the bridgesampling github
