# Setting Jeffreys’s prior on sigma

**URL:** https://discourse.mc-stan.org/t/setting-jeffreys-s-prior-on-sigma/15795
**Category:** brms
**Created:** [June 9, 2020, 2:36pm UTC](https://discourse.mc-stan.org/t/setting-jeffreys-s-prior-on-sigma/15795 "2020-06-09T14:36:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JAQuent](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jaquent/32/5763_2.png) [@JAQuent](https://discourse.mc-stan.org/u/JAQuent)
#### Post date: [June 9, 2020, 2:36pm UTC](https://discourse.mc-stan.org/t/setting-jeffreys-s-prior-on-sigma/15795/1 "2020-06-09T14:36:00Z")

</div>

Hey,  
I’d like to build this model ([Bayesian One-Sample T-Test (Stan)](https://cran.r-project.org/web/packages/bridgesampling/vignettes/bridgesampling_stan_ttest.html)) in brms. Is this possible?

I can’t figure out how to put the Jeffrey’s prior on sigma2 like this: `target += log(1/sigma2); // Jeffreys prior on sigma2`.

How can I add the equivalent prior on sigma?  
I’ve tried this

```no-highlight
priors <- c(prior('cauchy(0, 0.707)', class = "Intercept"),
            prior_string("target += sqrt(log(1/(sigma^sigma)))", check = FALSE))

```

but it didn’t work:

> model$prior  
> prior class coef group resp dpar nlpar bound  
> 1 cauchy(0, 0.707) Intercept  
> 2 student\_t(3, 0, 10) sigma  
> 3 target += sqrt(log(1/(sigma^sigma)))

Sorry if I get something horribly wrong.

---

<div class="post-metadata">

### Author: ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)
#### Post date: [June 12, 2020, 9:00pm UTC](https://discourse.mc-stan.org/t/setting-jeffreys-s-prior-on-sigma/15795/2 "2020-06-12T21:00:45Z")

</div>

`brms` expects priors to be already defined functions. You might need to use `stanvars` (check brms docs, if that’s not clear feel free to ask :-) ) to add a piece of Stan code defining a function representing the Jeffrey’s prior. Once a function like this is added to the model:

```stan
real jeffreys_lpdf(real x) {
  return log(1 / x);
}

```

You should be able to write `prior('jeffreys()', ...)`

Best of luck!

---

<div class="post-metadata">

### Author: ![JAQuent](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jaquent/32/5763_2.png) [@JAQuent](https://discourse.mc-stan.org/u/JAQuent)
#### Post date: [June 14, 2020, 5:20pm UTC](https://discourse.mc-stan.org/t/setting-jeffreys-s-prior-on-sigma/15795/3 "2020-06-14T17:20:57Z")

</div>

Hey,  
Thank you so much! This is what I tried:

```
# Generate data
df <- data.frame(y = rnorm(100, 0.5, 1))

# Create custom jeffrey prior
jeffreyPrior <- stanvar(block = 'functions', 
                        name = 'prior_sigma', 
                        scode = "real jeffreys_lpdf(real x) {
  return log(1 / x);
}")

# Make Stancode 
t_test_model <- make_stancode(y ~ 1, prior = priors, stanvars = jeffreyPrior, data = df)

```

However, I got a error message saying `PARSER EXPECTED: <probability function argument>`. More detail:

```
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'model3b6059dd15ab_file3b60e233cd4' at line 25, column 25
  -------------------------------------------------
    23: // priors including all constants
    24: target += cauchy_lpdf(Intercept | 0, 0.707);
    25: target += jeffreys_lpdf(sigma | )
                                ^
    26: - 1 * jeffreys_lccdf(0 | );
  -------------------------------------------------

PARSER EXPECTED: <probability function argument>
Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname, : 
  failed to parse Stan model 'file3b60e233cd4' due to the above error.

```

What am I doing wrong here?

---

<div class="post-metadata">

### Author: ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)
#### Post date: [June 15, 2020, 6:53am UTC](https://discourse.mc-stan.org/t/setting-jeffreys-s-prior-on-sigma/15795/4 "2020-06-15T06:53:07Z")

</div>

At this point, the probably easiest solution is probably

```
set_prior("", "sigma") + 
  set_prior("target += log(1 / sigma)", check = FALSE)

```
