# Error in extract\_log\_lik from the loo package

**URL:** <https://discourse.mc-stan.org/t/error-in-extract-log-lik-from-the-loo-package/23169>\
**Category:** RStan\
**Tags:** loo, rstan\
**Created:** [June 24, 2021, 11:37am UTC](https://discourse.mc-stan.org/t/error-in-extract-log-lik-from-the-loo-package/23169 "2021-06-24T11:37:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isa\_Nazar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/isa_nazar/32/9376_2.png) [@Isa\_Nazar](https://discourse.mc-stan.org/u/Isa_Nazar)\
**Post date:** [June 24, 2021, 11:37am UTC](https://discourse.mc-stan.org/t/error-in-extract-log-lik-from-the-loo-package/23169/1 "2021-06-24T11:37:15Z")

</div>

Dear all,  
I ran my model in the RSTAN as follow.  
fit.total06 ← stan(“dependencymodel.stan”,data = da,iter=iter, warmup=warmup, cores =2,chains = 2, thin = 1, init = “random”, control = list(c(adapt\_delta = 0.99,max\_treedepth = 15)))

Now, When I want to calculate the loo and WAIC from the loo package, I get the following error.  
extract\_log\_lik (fit.total06, parameter\_name = “log\_lik”, merge\_chains = TRUE)

Error is as below:  
“”  
Error in extract\_log\_lik(fit.total06, parameter\_name = “log\_lik”, merge\_chains = TRUE) :  
could not find function “extract\_log\_lik”  
“”  
Could someone help me to solve this problem?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [June 24, 2021, 4:18pm UTC](https://discourse.mc-stan.org/t/error-in-extract-log-lik-from-the-loo-package/23169/2 "2021-06-24T16:18:52Z")

</div>

> [@Isa\_Nazar](#):
>
> could not find function “extract\_log\_lik”

Is the loo package loaded via `library(loo)`? Alternatively you can do `loo::extract_log_lik()`.

---

<div class="post-metadata">

**Author:** ![Isa\_Nazar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/isa_nazar/32/9376_2.png) [@Isa\_Nazar](https://discourse.mc-stan.org/u/Isa_Nazar)\
**Post date:** [June 24, 2021, 4:42pm UTC](https://discourse.mc-stan.org/t/error-in-extract-log-lik-from-the-loo-package/23169/3 "2021-06-24T16:42:25Z")

</div>

Thanks for your reply. First, I installed the loo package from CRAN and loaded it via `library(loo)`. But, I encountered the following error again.

“”  
Error in check\_pars(allpars, pars) : no parameter log\_lik  
“”  
Thank you!

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [June 24, 2021, 5:51pm UTC](https://discourse.mc-stan.org/t/error-in-extract-log-lik-from-the-loo-package/23169/4 "2021-06-24T17:51:41Z")

</div>

`extract_log_lik(fit.total06, parameter_name = "log_lik", merge_chains = TRUE)` will only work if you have a variable in your model called `log_lik`. If it’s not called `log_lik` then you can change the name in the `parameter_name` argument, but it needs to be able to find the log likelihood values computed in the Stan program. For more info on this check out this vignette in the loo package, which demonstrates how to write a Stan program compatible with loo:

> **[Writing Stan programs for use with the loo package](https://mc-stan.org/loo/articles/loo2-with-rstan.html)**
>
> loo

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Isa\_Nazar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/isa_nazar/32/9376_2.png) [@Isa\_Nazar](https://discourse.mc-stan.org/u/Isa_Nazar)\
**Post date:** [June 25, 2021, 1:34pm UTC](https://discourse.mc-stan.org/t/error-in-extract-log-lik-from-the-loo-package/23169/5 "2021-06-25T13:34:26Z")

</div>

Thanks for your detailed explanation and help.
