# Credible intervals with Capybaras, R and Stan (rstan and cmdstanr interfaces)

*R and Shiny Training: If you find this blog to be interesting, please note that I offer personalized and group-based training sessions that may be reserved through Buy me a Coffee. Additionally, I provide training services in the Spanish language and am available to discuss means by which I may contribute to your Shiny project.*

# Motivation

Matsuura’s excellent book Bayesian Statistical Modeling with Stan, R, and Python uses `cmdstanr`

for all the coding examples. You can access the codes from GitHub.

Because I am reading that book, I searched online how to obtain a credible interval with `cmdstanr`

, but I did not find anything with open access about it. Therefore, I decided to write this post to show how to do it with both interfaces.

# Problem

We have a dataset about 100 capybaras (*hydrochoerus hydrochaeris*). Each capybara has 2 babies, one in each season, with each season being “birth1” and “birth2”. The data is:

In this data, the codification is “female = 0” and “male = 1”.

```
<- data.frame(
baby_capybaras birth1 = c(1,0,0,0,1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,
0,1,0,1,1,1,0,1,0,1,1,0,1,0,0,1,1,0,1,0,0,0,0,0,0,0,1,1,0,1,0,0,
1,0,0,0,1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,1,0,1,1,0,1,0,1,1,1,0,
1,1,1,1),
birth2 = c(0,1,0,1,0,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,0,1,1,1,0,1,1,
1,0,1,0,0,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,
0,1,1,0,1,1,1,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0,1,1,0,0,0,1,1,1,
0,0,0,0)
)
```

Which is the probability that a capybara has a male baby if the first baby is a female?

# Solution

We can calculate posterior beliefs about the conditional probability by using Stan. Before doing computation, let’s assume the births are dependent and that the prior probability is uniform.

The Stan for code for this problem is:

```
// Input
data {
int<lower=0> N1;
int<lower=0> N2;
int<lower=0> n1;
int<lower=0> n2;
}
// Parameters
parameters {
vector<lower=0, upper=1>[2] p;
}
// Model
model {
1]);
n1 ~ binomial(N1, p[2]);
n2 ~ binomial(N2, p[ }
```

From R we can either use `rstan`

or `cmdstanr`

. The first one has more documentation, but the second one is faster and compatible with the latest Stan version. I will show how to use both.

## Credible interval with rstan

With `rstan`

we do the following:

```
# install.packages("rstan")
library(rstan)
<- stan_model("baby-capybaras.stan")
mod_rstan
<- list(
baby_capybaras_list N1 = sum(baby_capybaras$birth1 == 0),
N2 = sum(baby_capybaras$birth1 == 1),
n1 = sum(baby_capybaras$birth2[baby_capybaras$birth1 == 0]),
n2 = sum(baby_capybaras$birth2[baby_capybaras$birth1 == 1])
)
# the seed is for reproducibility
# see https://www.independent.co.uk/life-style/history/42-the-answer-to-life-the-universe-and-everything-2205734.html
<- sampling(
fit_rstan
mod_rstan,data = baby_capybaras_list,
seed = 42,
chains = 4,
refresh = 500,
warmup = 1000,
iter = 10000
)
fit_rstan
for Stan model: baby-capybaras.
Inference 4 chains, each with iter=10000; warmup=1000; thin=1;
-warmup draws per chain=9000, total post-warmup draws=36000.
post
2.5% 25% 50% 75% 97.5% n_eff Rhat
mean se_mean sd 1] 0.78 0.00 0.06 0.66 0.75 0.79 0.83 0.89 31038 1
p[2] 0.42 0.00 0.07 0.29 0.37 0.41 0.46 0.55 31870 1
p[-63.57 0.01 1.01 -66.29 -63.96 -63.26 -62.85 -62.59 16559 1
lp__
NUTS(diag_e) at Mon Jul 3 14:40:25 2023.
Samples were drawn using
For each parameter, n_eff is a crude measure of effective sample size,chains (at
and Rhat is the potential scale reduction factor on split Rhat=1). convergence,
```

This implies that the probability of a male birth if the first birth is a female is 0.78.

For the credible interval I will create a separate vector for the percentiles. The reason for this is that I typed `0.0025`

instead of `0.025`

to verify the computation with `rstan`

by doing the same with `cmdstanr`

.

My typing error made me spend more than one hour checking my code and trying to figure out why the results were different, until I realised I had this:

```
quantile(extract(fit_rstan, pars = "p")$p[, 1], probs = c(0.025, 0.975))
# versus
quantile(fit_cmdstanr$draws("p", format = "matrix")[, "p[1]"],
probs = c(0.0025, 0.975))
# please note the additional 0 in the second case
```

The code for the 95% credible interval with `rstan`

is:

```
# percentiles 0% + alpha/2 and 100% - alpha/2
# in this case 2.5% and 97.5%
<- 0.05
alpha <- c(0, 1) + (c(1, -1) * (alpha / 2))
percentiles
quantile(extract(fit_rstan, pars = "p")$p[, 1], probs = percentiles)
2.5% 97.5%
0.6629666 0.8853788
```

## Credible interval with cmdstanr

With `cmdstanr`

we do the following:

```
# install.packages("cmdstanr",
# repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
library(cmdstanr)
# run once and only once after installing the package
# check_cmdstan_toolchain()
# install_cmdstan(cores = 4)
<- cmdstan_model("baby-capybaras.stan")
mod_cmdstanr
<- mod_cmdstanr$sample(
fit_cmdstanr data = baby_capybaras_list,
seed = 42,
chains = 4,
refresh = 500,
iter_warmup = 1000,
iter_sampling = 10000
)
fit_cmdstanr
variable mean median sd mad q5 q95 rhat ess_bulk ess_tail-63.58 -63.27 1.01 0.74 -65.60 -62.61 1.00 18852 24730
lp__ 1] 0.78 0.79 0.06 0.06 0.68 0.87 1.00 36314 26137
p[2] 0.42 0.41 0.07 0.07 0.31 0.53 1.00 39442 28938 p[
```

The code for the 95% credible interval with `cmdstanr`

is:

```
quantile(fit_cmdstanr$draws("p", format = "matrix")[, "p[1]"],
probs = percentiles)
2.5% 97.5%
0.6623710 0.8853289
```

These results are equivalent to the previous part. There is a small difference for the credible interval, but it is due to the different implementations used by `rstan`

and `cmdstanr`

, and the implemented steps such as rounding.