# Two-steps estimation in poisson model

**URL:** <https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383>\
**Category:** Modeling\
**Tags:** rstan, fitting-issues\
**Created:** [May 8, 2021, 4:00pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383 "2021-05-08T16:00:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin\_roshani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/amin_roshani/32/11582_2.png) [@amin\_roshani](https://discourse.mc-stan.org/u/amin_roshani)\
**Post date:** [May 8, 2021, 4:00pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/1 "2021-05-08T16:00:43Z")

</div>

I want to fitting the Poisson model like below

D\_{x,t} \sim Poisson(m\_{x,t}),\\ \log m\_{x,t} = B\_xK\_t + b\_{x}k\_t ~. \qquad (1)

I have to implement a _ **two-step** _ structure for estimating the parameters.

1- parameters B\_x and K\_t are estimated using the following model.

d\_{x,t} \sim Poisson(g\_{x,t}),\\ \log g\_{x,t} = B\_xK\_t .

2- the estimated values of \hat{B}\_x and \hat{K}\_t are substituted in Equation (1) and the other parameters (b\_x , k\_t) are estimated. Can I use codes like the following in the model block?

```stan
model {
for(x in 1:A){
   for(t in 1:n){
     dxt[x,t] ~ poisson(exp( B[x] * K[t] ));
     Dxt[x,t] ~ poisson(exp( B[x] * K[t] + b[x] * k[t]));
}}
} 

```

I can not estimate the parameters in one step and I must do it in two steps.

---

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [May 9, 2021, 8:39pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/2 "2021-05-09T20:39:46Z")

</div>

> [@amin\_roshani](#):
>
> I can not estimate the parameters in one step and I must do it in two steps.

What do you mean with this?

---

<div class="post-metadata">

**Author:** ![amin\_roshani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/amin_roshani/32/11582_2.png) [@amin\_roshani](https://discourse.mc-stan.org/u/amin_roshani)\
**Post date:** [May 9, 2021, 8:43pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/3 "2021-05-09T20:43:07Z")

</div>

I want to emphasize that the idea of doing work in one step is not possible for me.

---

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [May 9, 2021, 8:52pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/4 "2021-05-09T20:52:12Z")

</div>

How would it look doing it in one step? How in two steps? In three?

---

<div class="post-metadata">

**Author:** ![amin\_roshani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/amin_roshani/32/11582_2.png) [@amin\_roshani](https://discourse.mc-stan.org/u/amin_roshani)\
**Post date:** [May 9, 2021, 9:03pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/5 "2021-05-09T21:03:59Z")

</div>

For example, if we want to do in one step:

```
model {
for(x in 1:A){
   for(t in 1:n){
     Dxt[x,t] ~ poisson(exp( B[x] * K[t] + b[x] * k[t]));
}}
}

```

But I want to first estimate B\_x and K\_t using

d\_{x,t} \sim Poisson(g\_{x,t}),\\ \log (g\_{x,t}) = B\_x K\_t,

and then use their estimated values \hat{B}\_x and \hat{K}\_t as constant values in

D\_{x,t} \sim Poisson(m\_{x,t}),\\ \log (m\_{x,t}) = \hat{B}\_x \hat{K}\_t + b\_x k\_t,

to estimate the other parameters b\_x and k\_t.

---

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [May 9, 2021, 9:12pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/6 "2021-05-09T21:12:48Z")

</div>

And why do you want the first parameters to be constant in the “second step”?

---

<div class="post-metadata">

**Author:** ![amin\_roshani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/amin_roshani/32/11582_2.png) [@amin\_roshani](https://discourse.mc-stan.org/u/amin_roshani)\
**Post date:** [May 9, 2021, 9:19pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/7 "2021-05-09T21:19:56Z")

</div>

Because, without the two step strategy, to ensure convergence, further constraints may need to be imposed; for example, the parameters in the B\_x , K\_t and the b\_x, k\_t are all orthogonal to one another.

---

<div class="post-metadata">

**Author:** ![Funko\_Unko](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/funko_unko/32/10786_2.png) [@Funko\_Unko](https://discourse.mc-stan.org/u/Funko_Unko)\
**Post date:** [May 10, 2021, 8:59am UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/8 "2021-05-10T08:59:35Z")

</div>

If you need further constraints (e.g. to get rid of “spurious” modes), maybe a better approach would be to encode them into the parameters in the stan model?

> [@amin\_roshani](#):
>
> ```stan
> model {
> for(x in 1:A){
> for(t in 1:n){
> dxt[x,t] ~ poisson(exp( B[x] * K[t] ));
> Dxt[x,t] ~ poisson(exp( B[x] * K[t] + b[x] * k[t]));
> }}
> } 
> 
> ```

This e.g. should be possible in principle, but you may get dependence between the parameters.

---

<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:** [May 10, 2021, 10:30am UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/9 "2021-05-10T10:30:28Z")

</div>

Just to clarify a bit what I believe @Funko_Unko is aiming at:

You can definitely have multiple likelihood terms in a single model and there can be - in principle - almost any dependency structure among them. In practice, some models are problematic for Stan (and other samplers), but that’s a different question.

Also, you can use `y ~ poisson_log(lambda)` instead of `y ~ poisson(exp(lambda))` for increased efficiency and numerical stability.

Best of luck with your model!

---

<div class="post-metadata">

**Author:** ![sonicking](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sonicking](https://discourse.mc-stan.org/u/sonicking)\
**Post date:** [May 11, 2021, 2:00am UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/10 "2021-05-11T02:00:08Z")

</div>

What results are you getting when you do the “one step” estimation?

---

<div class="post-metadata">

**Author:** ![amin\_roshani](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/amin_roshani/32/11582_2.png) [@amin\_roshani](https://discourse.mc-stan.org/u/amin_roshani)\
**Post date:** [May 11, 2021, 10:42am UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/11 "2021-05-11T10:42:09Z")

</div>

It is done very very slowly.

---

<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:** [May 11, 2021, 12:50pm UTC](https://discourse.mc-stan.org/t/two-steps-estimation-in-poisson-model/22383/12 "2021-05-11T12:50:53Z")

</div>

Hi,  
I think we are missing a lot of background to be able to help you effectively. Could you share the full Stan model? Could you also share at least a subset of the data? What do the data represent? How big is the dataset? What is the scientific/real-world question you are trying to answer?

It is generally our experience that when a model is very slow, it is often because there is some problem with the model, so before thinking about doing any sort of two step process, I would check if the model makes sense for your problem.

It is also usually better for us if instead of saying something is “not possible” to be specific and say why do you believe it is not possible, what did you try? What did you expect to happen? And what did happen instead?

Best of luck with your model!
