# Newbie question: how to do Bayesian parameter estimation with RStan?

**URL:** <https://discourse.mc-stan.org/t/newbie-question-how-to-do-bayesian-parameter-estimation-with-rstan/1462>\
**Category:** Modeling\
**Tags:** fitting-issues\
**Created:** [August 3, 2017, 1:39pm UTC](https://discourse.mc-stan.org/t/newbie-question-how-to-do-bayesian-parameter-estimation-with-rstan/1462 "2017-08-03T13:39:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![earlbellinger](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@earlbellinger](https://discourse.mc-stan.org/u/earlbellinger)\
**Post date:** [August 3, 2017, 1:39pm UTC](https://discourse.mc-stan.org/t/newbie-question-how-to-do-bayesian-parameter-estimation-with-rstan/1462/1 "2017-08-03T13:39:34Z")

</div>

I have a function

```
likelihood <- function(theta) { # theta is a list of N real numbers 
    # ... 
    # some black box here that depends on theta 
    # ... 
    return(value) 
}

```

I also priors (uniform and normal) on the different numbers in `theta`.

I want to estimate the posterior distribution of `theta` according to my likelihood function and the priors that I have set. I am under the impression that I can do this with RStan, although from reading the manual and trying out several tutorials, it is very unclear to me how.

I’m pretty new though and I’m sure this is a pretty naive question, but could someone please explain or point me to a relevant tutorial?

(For what it’s worth, I consider this to be pretty easily done with python’s `emcee` package)

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [August 3, 2017, 2:08pm UTC](https://discourse.mc-stan.org/t/newbie-question-how-to-do-bayesian-parameter-estimation-with-rstan/1462/2 "2017-08-03T14:08:22Z")

</div>

This (usually) isn’t too difficult, but you need to get familiar with the Stan language and the approach to estimation that is facilitated by Stan. Chapters 22 and 23 of the Stan manual discuss custom probability functions. Basically, you need to translate this R function to a Stan function. Then use `rstan::expose_stan_function` so that you can call it and make sure it matches your R implementation. You don’t have to combine everything into a single parameter vector `theta` like you would if you were calling `optim`; you can have multiple arguments to your function.

For the parameters that have priors, you just have to do

```
target += normal_lpdf(that_parameters | mean, sd);

```

For the parameters that have uniform priors (which is usually a bad idea), you don’t have to do anything to alter the posterior kernel (in log units).

---

<div class="post-metadata">

**Author:** ![Dirk\_Nachbar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dirk_nachbar/32/4271_2.png) [@Dirk\_Nachbar](https://discourse.mc-stan.org/u/Dirk_Nachbar)\
**Post date:** [August 3, 2017, 2:11pm UTC](https://discourse.mc-stan.org/t/newbie-question-how-to-do-bayesian-parameter-estimation-with-rstan/1462/3 "2017-08-03T14:11:29Z")

</div>

You will have to initialise your thetas, give them a prior and then express your likelihood in terms of distribution around equations/functional forms

> real theta[N];  
> real\<lower=0\> sigma;  
> theta ~ normal(0, 1);  
> y ~ normal(theta[1] … theta[N], sigma); // likelihood

If you want the actual LL check this [extract\_log\_lik: Extract pointwise log-likelihood from a Stan model in loo: Efficient Leave-One-Out Cross-Validation and WAIC for Bayesian Models](https://rdrr.io/cran/loo/man/extract_log_lik.html)
