# Advice on using search\_terms in projpred

**URL:** <https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846>\
**Category:** Interfaces\
**Tags:** projpred\
**Created:** [June 4, 2021, 4:38am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846 "2021-06-04T04:38:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [June 4, 2021, 4:38am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846/1 "2021-06-04T04:38:35Z")

</div>

I am seeking advice on how to create an appropriate `search_terms` argument in `projpred` in order to ensure that when certain predictors are included certain other predictors are also included. I have not been able to find any formal documentation of exactly how `search_terms` works or its syntax, hence my question.

I have a Bernoulli family model of a binary outcome Y and there are continuous predictors `A1`, `A2`, `B1`, `B2`, `C1`, `C2`, and a binary factor `F` with two levels whose non-reference level is denoted `F1`. The model formula is `Y ~ (A1 + A2 + B1 + B2 + C1 + C2) * F`.  
I want to apply the following constrants:

1. `A1` and `A2` always come as a pair, as do `B1` and `B2`, as do `C1` and `C2`
2. If `A1 + A2` is included` (A1 + A2):F1` must also be included; same with `B1 + B2` and `(B1 + B2):F1`; same with `C1 + C2 and (C1 + C2):F1`

Is this the type of thing that is possible using `search_terms`? If so, how would these constraints be coded?

Many thanks in advance for any clues!

---

<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 11, 2021, 7:28am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846/2 "2021-06-11T07:28:15Z")

</div>

Unfortunately, I don’t know much about `projpred, but tagging @jpiironen who hopefully knows more and has time to answer.

---

<div class="post-metadata">

**Author:** ![jpiironen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jpiironen/32/9626_2.png) [@jpiironen](https://discourse.mc-stan.org/u/jpiironen)\
**Post date:** [June 11, 2021, 7:50am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846/3 "2021-06-11T07:50:54Z")

</div>

Hi, I’m no longer actively participating in projpred development. I’m tagging @AlejandroCatalina, hopefully he can answer this one.

---

<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:** [June 26, 2021, 8:24am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846/4 "2021-06-26T08:24:18Z")

</div>

Hi,

This is possible to do with `search_terms`, although a bit cumbersome to write, basically you have to think of `search_terms` as formula building elements, so that each member indicates a valid submodel. You can only explore submodels that are grown from at least one other member. Let me have an example:

Let’s assume, for the sake of the example, that we only have A1 + A2 + B1 from your problem, and we want to keep A1 and A2 always together. The search\_terms for this would be

```
search_tems <- c("1", "A1 + A2", "A1 + A2 + B1")

```

so that `B1` can only be included after including `A1 + A2`. In your example you are reusing terms a lot, so I would advise you to build search\_terms using `paste` and stored variables that are `t1 <- "A1 + A2", t2 <- "B1 + B2"`, etc. I see you have some interaction requirements as well, you can have your minimal building term be `(A1 + A2):F1` in your case as you want all of this included if `A1 + A2` is included.

Does this help at all?

Thanks for the question, this is an interesting one!

---

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [June 28, 2021, 4:41am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846/5 "2021-06-28T04:41:14Z")

</div>

Thanks, @AlejandroCatalina – the growing of terms makes sense. But, for the interaction, would having just `(A1 + A2):F1` in `search_terms` allow for the model to also have the lower order effects of `A1` and `A2` as well? You imply it would but I am struggling to understand the logic of the syntax used here because `:` usually refers only to the interaction and not the lower-order effects as well. It’s almost like `(A1 + A2):F1` in `search_terms` means `(A1 + A2)*F1` or `A1 + A2 + (A1 + A2):F1`, but maybe I am completely misunderstanding the syntax here.

---

<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:** [June 28, 2021, 5:32am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846/6 "2021-06-28T05:32:22Z")

</div>

You’re absolutely right, I made a mistake. You can indeed introduce A1+A2+(A1+A2):F as the building block.

---

<div class="post-metadata">

**Author:** ![andymilne](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andymilne/32/21046_2.png) [@andymilne](https://discourse.mc-stan.org/u/andymilne)\
**Post date:** [June 28, 2021, 6:26am UTC](https://discourse.mc-stan.org/t/advice-on-using-search-terms-in-projpred/22846/7 "2021-06-28T06:26:15Z")

</div>

Great – all makes sense now. Thank you.
