如何使用pymc3计算风险比?

时间:2019-07-08 04:45:08

标签: pymc3

我想在调查中为数据的风险比率建模,但是我很难将问题归结为pymc3。风险比是某项风险暴露的概率与无风险概率之比。

据我了解,风险比为:(暴露组的发生率)/(未暴露组的发生率)

我尝试按照下面的示例代码所示解决问题:

import pymc3

exposed_num_observations = 25
exposed_observed = 11
notExposed_num_observations = 853
notExposed_observed = 202

with pm.Model() as rr_mdoel:
    # Prior
    p_exposed = pm.Uniform("p_exposed", 0 ,1)
    p_notExposed = pm.Uniform("p_notExposed", 0 ,1)
    sd = pm.Uniform("sd", 0,5)

    # Likelihood
    exposed = pm.Binomial("exposed", 
                      p=p_exposed, 
                      n=exposed_num_observations,  
                      observed=exposed_observed)
    notExposed = pm.Binomial("notExposed", 
                             p=p_notExposed, 
                             n=notExposed_num_observations,  
                             observed=notExposed_observed)

    risk_ratio = pm.Normal("risk_ratio", mu=(p_exposed/p_notExposed), sd=sd)

    # Inference Run/ Markov chain Monte Carlo 
    trace = pm.sample(10000, chains=2, tune=1000)

我希望risk_ratio始终大于0,并且我希望sd适合某个值。结果(例如az.plot_posterior(trace, round_to=3)所示)显示risk_ratio的一些负值,而sd仍然是均匀的。

0 个答案:

没有答案