# Is there an easier way to integrate projpred with stanfit object?

**URL:** https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065
**Category:** General
**Tags:** projpred
**Created:** [July 29, 2020, 3:29am UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065 "2020-07-29T03:29:51Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Jiahao\_Liu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jiahao_liu/32/5691_2.png) [@Jiahao\_Liu](https://discourse.mc-stan.org/u/Jiahao_Liu)
#### Post date: [July 29, 2020, 3:29am UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/1 "2020-07-29T03:29:51Z")

</div>

I just found it hard to integrate the projpred with stanfit object. My major obstacle is using the cv\_varsel function in the k fold scenario. So according to the description, I need to specify the cvfun parameter in the init\_refmodel function. But I don’t know how to specify the function just according to the description here [https://mc-stan.org/projpred/reference/init\_refmodel.html](https://mc-stan.org/projpred/reference/init_refmodel.html)

I went to see the source code of init\_refmodel, their cvfun function is defined based on the object created by rstanarm or brms. Can anyone provide me an example to get to the k fold predictive projection method from an stanfit object created by RStan?

---

<div class="post-metadata">

### Author: ![torkar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/torkar/32/1355_2.png) [@torkar](https://discourse.mc-stan.org/u/torkar)
#### Post date: [August 4, 2020, 9:26am UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/2 "2020-08-04T09:26:07Z")

</div>

Welcome to the community!

I haven’t used `projpred` with `Stan` as-is (only with `brms`). I’m sure @avehtari can provide an answer :)

---

<div class="post-metadata">

### Author: ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)
#### Post date: [August 4, 2020, 9:46am UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/3 "2020-08-04T09:46:15Z")

</div>

Does Custom reference models section in [projpred quick start](https://mc-stan.org/projpred/articles/quickstart.html) help?

---

<div class="post-metadata">

### Author: ![Jiahao\_Liu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jiahao_liu/32/5691_2.png) [@Jiahao\_Liu](https://discourse.mc-stan.org/u/Jiahao_Liu)
#### Post date: [August 4, 2020, 1:24pm UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/4 "2020-08-04T13:24:01Z")

</div>

Nope. The reference model listed there is working under the loo scenario, but I want to get a custome reference model working under the k fold scenario. So according the the description of the [init\_refmodel](https://mc-stan.org/projpred/reference/init_refmodel.html) function, I need to write a cvfun inside the init\_refmodel function. But just based on the description of cvfun, it is kind of hard to write the funciton without any examples.

---

<div class="post-metadata">

### Author: ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)
#### Post date: [August 4, 2020, 1:48pm UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/5 "2020-08-04T13:48:17Z")

</div>

ping @AlejandroCatalina

---

<div class="post-metadata">

### Author: ![mcol](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mcol/32/5598_2.png) [@mcol](https://discourse.mc-stan.org/u/mcol)
#### Post date: [August 4, 2020, 2:10pm UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/6 "2020-08-04T14:10:36Z")

</div>

There are two examples in the projpred source code, that use `init_refmodel()` to define reference models for `rstanarm` and `brms` models (see for example [https://github.com/stan-dev/projpred/blob/181606a416471f8145976e8b048d5213b9fb6ab3/R/refmodel.R#L116](https://github.com/stan-dev/projpred/blob/181606a416471f8145976e8b048d5213b9fb6ab3/R/refmodel.R#L116)). By following those functions, you’ll see how to set up the various parameters expected by `init_refmodel()`.

Having said that, I agree that documentation could be improved on that front! :)

---

<div class="post-metadata">

### Author: ![Jiahao\_Liu](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jiahao_liu/32/5691_2.png) [@Jiahao\_Liu](https://discourse.mc-stan.org/u/Jiahao_Liu)
#### Post date: [August 4, 2020, 11:45pm UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/7 "2020-08-04T23:45:23Z")

</div>

> [@Jiahao\_Liu](#):
>
> Nope. The reference model listed there is working under the loo scenario, but I want to get a custome reference model working under the k fold scenario. So according the the description of the [init\_refmodel](https://mc-stan.org/projpred/reference/init_refmodel.html) function, I need to write a cvfun inside the init\_refmodel function. But just based on the description of cvfun, it is kind of hard to write the funciton without any examples.

Thanks! That really helps!

---

<div class="post-metadata">

### Author: ![AlejandroCatalina](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alejandrocatalina/32/6093_2.png) [@AlejandroCatalina](https://discourse.mc-stan.org/u/AlejandroCatalina)
#### Post date: [August 5, 2020, 6:47am UTC](https://discourse.mc-stan.org/t/is-there-an-easier-way-to-integrate-projpred-with-stanfit-object/17065/8 "2020-08-05T06:47:04Z")

</div>

Hi, sorry for the late reply!

I got the documentation message and will try to improve it!

Then, to the question at hand, the key reason why projpred is not so straightforward for a general stanfit object is because we don’t know anything about it basically, so we can’t have a general predfun or anything, and the same goes for cvfun. As @mcol said, there are some examples in the code where these functions are implemented for some models, and that helps (thanks for the link). I’ll try to improve the documentation so it’s clearer.

In the case of cvfun, you have to provide a function that builds a model per fold and returns the mean prediction for that fold (predfun) and its dispersion parameter. If you built your model with k-fold in mind you probably already built different fits per fold and can provide cvfits argument instead (basically each cvfit is a fold-refmodel). Anyway, I’ll try to improve the documentation and provide more examples.

Hope this clarifies the issue a bit more!
