# Help with Hypothesis Testing

**URL:** <https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043>\
**Category:** Modeling\
**Created:** [May 11, 2020, 3:33pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043 "2020-05-11T15:33:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordan\_Howell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jordan_howell/32/4786_2.png) [@Jordan\_Howell](https://discourse.mc-stan.org/u/Jordan_Howell)\
**Post date:** [May 11, 2020, 3:33pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/1 "2020-05-11T15:33:41Z")

</div>

Hello. I have ran a simple uni-variate model and would like to do the Bayesian equivalent to a hypothesis test.

The model is below but at a high level, I have a dependent variable ‘claim count’ and predictor ‘repair\_letter’. I have fit the model. I am asking guidance on showing the difference in claim count based on if an observation gets a repair letter (variable denoted by a 1 or 0)

* * *

```stan
#model code
model_cod = """
data {
int<lower=0> N;
int<lower=0> clmcnt[N];
vector[N] repair_flag;
}
parameters {
real beta;
}
model {
beta ~ normal(0,1);
clmcnt~poisson_log(repair_flag*beta);
}
"""
sm = StanModel(model_code = model_cod)

data1 = {'N': df_join.shape[0], 'repair_flag':df_join['repair_flag'].values
         , 'clmcnt':df_join['clmcnt'].values}
sm_fit = sm.sampling(data = data1, init = .3, chains = 3, iter = 5000, warmup = 1000)

print(sm_fit)

```

Print

```
       mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
beta -2.81 3.1e-4 0.02 -2.85 -2.82 -2.81 -2.8 -2.77 4067 1.0
lp__ -5.5e6 0.01 0.72 -5.5e6 -5.5e6 -5.5e6 -5.5e6 -5.5e6 4742 1.0

```

* * *

Any guidance on analyzing and visualizing would be great.

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [May 11, 2020, 3:35pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/2 "2020-05-11T15:35:15Z")

</div>

Code your `repair_flag` variable as `-.5` and `+.5`, then you can interpret the beta parameter as the difference. See [this lecture](https://www.youtube.com/watch?v=qY4nT4LMtpM&list=PLu77iLvsj_GNmWqDdX-26kZ56dCZqkzBO&index=12).

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [May 11, 2020, 3:43pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/3 "2020-05-11T15:43:29Z")

</div>

Actually, are you sure you have your model coded as you intend? with `repair_flag` as a 0/1 coded variable, you’re conveying perfect certainty that the entries coded with 0 have an alpha of 1 (`log(0)`). I suspect that you want an intercept in your model as well.

---

<div class="post-metadata">

**Author:** ![Jordan\_Howell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jordan_howell/32/4786_2.png) [@Jordan\_Howell](https://discourse.mc-stan.org/u/Jordan_Howell)\
**Post date:** [May 11, 2020, 3:44pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/4 "2020-05-11T15:44:56Z")

</div>

ooo. Didn’t think about that. Could you point me to sampling from a posterior as well?

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [May 11, 2020, 3:48pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/5 "2020-05-11T15:48:40Z")

</div>

> [@Jordan\_Howell](#):
>
> Could you point me to sampling from a posterior as well?

Sorry, I don’t know what you mean.

Here’s your model with an intercept:

```stan
data {
    int<lower=0> N;
    int<lower=0> clmcnt[N];
    vector[N] repair_flag;
}
parameters {
    real intercept;
    real beta;
}
model {
    intercept ~ normal(0,1);
    beta ~ normal(0,1);
    clmcnt ~ poisson_log( intercept + repair_flag*beta );
}

```

If you code your repair flag with -.5/+.5, beta will be the difference between conditions and intercept will be the average across conditions. Be sure to modify the priors to convey your actual beliefs.

---

<div class="post-metadata">

**Author:** ![Jordan\_Howell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jordan_howell/32/4786_2.png) [@Jordan\_Howell](https://discourse.mc-stan.org/u/Jordan_Howell)\
**Post date:** [May 11, 2020, 3:52pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/6 "2020-05-11T15:52:51Z")

</div>

Thank you. I think i’m trying to do what generated quantities does. Does this look correct?

```
generated quantities {
vector[N_new] y_new;
for (n in 1:N_new)
    y_new[n] = poisson_rng(alpha[n] + repair_flag*beta[n])
}

```

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [May 11, 2020, 4:11pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/7 "2020-05-11T16:11:20Z")

</div>

> [@Jordan\_Howell](#):
>
> Thank you. I think i’m trying to do what generated quantities does. Does this look correct?

This will generate new count observations given the posterior.

---

<div class="post-metadata">

**Author:** ![Jordan\_Howell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jordan_howell/32/4786_2.png) [@Jordan\_Howell](https://discourse.mc-stan.org/u/Jordan_Howell)\
**Post date:** [May 11, 2020, 4:28pm UTC](https://discourse.mc-stan.org/t/help-with-hypothesis-testing/15043/8 "2020-05-11T16:28:57Z")

</div>

Thanks!
