<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Consistent models in DisMod-MR without many different types of data
In DisMod-II there was a requirement to have at least three different data types, corresponding to different parts of the compartmental model.  DisMod-MR can run a compartmental model with only two, or even one data type, but this requires expert priors to fill in the gaps.
This document provides an example of how a model for Parkinson's Disease might look with different subsets of data types.
Step1:  Consistent fit with all data
Let's start with a consistent fit of the simulated PD data.  This includes data on prevalence, incidence, and SMR, and the assumption that remission rate is zero.  All together this counts as four different data types in the DisMod-II accounting.
Step2:  Consistent fit without incidence
Now let's do it again with the incidence removed.  Since there is data on prevalence and SMR as well as the assumption that remission is zero, this counts as three data types, the minimum allowed for DisMod-II.
Step3:  Consistent fit without incidence or mortality
This uses only prevalence data and the assumption that there is no remission, so it is not valid in DisMod-II.  The Bayesian priors included by default in DisMod-MR make it possible, but the tradeoff between incidence and mortality is not informed by any data in this case.
Step4:  Consistent fit with only prevalence
Now without assumption of zero remission, DisMod-MR is going for a very underconstrained problem, and relies on the priors heavily.  However, the prevalence data is there, so the estimates of prevalence will not be changed much.
Step5:  Comparison of alternative models
Let's compare the distributions for all of these now.  You can see that the more data there is, the more concentrated the posterior distribution becomes.
Step6:  Consistent fit without prevalence
The really challenging case is without any prevalence data. DisMod-MR will go for it, but there will be a lot of uncertainty.
Step7:  Consistent fit with incidence only
DisMod-MR it will even go for it with only incidence.  But that is not ideal...
Step8:  Consistent fit without prevalence or incidence
DisMod-MR is not magic, however.  Without prevalence or incidence, it will not know how much PD there is!
<ASSISTANT_TASK:>
Python Code:
import matplotlib.pyplot as plt, numpy as np
import dismod_mr
models = {}
#iter=101; burn=0; thin=1  # use these settings to run faster
iter=10_000; burn=5_000; thin=5  # use these settings to make sure MCMC converges
Explanation: Consistent models in DisMod-MR without many different types of data
In DisMod-II there was a requirement to have at least three different data types, corresponding to different parts of the compartmental model.  DisMod-MR can run a compartmental model with only two, or even one data type, but this requires expert priors to fill in the gaps.
This document provides an example of how a model for Parkinson's Disease might look with different subsets of data types.
End of explanation
model = dismod_mr.load('pd_sim_data/')
model.keep(areas=['GBR'], sexes=['female', 'total'])
model.setup_model()
%time model.fit(iter=iter, burn=burn, thin=thin)
models['p, i, r, smr'] = model
model.plot()
models
Explanation: Consistent fit with all data
Let's start with a consistent fit of the simulated PD data.  This includes data on prevalence, incidence, and SMR, and the assumption that remission rate is zero.  All together this counts as four different data types in the DisMod-II accounting.
End of explanation
model = dismod_mr.load('pd_sim_data/')
model.keep(areas=['GBR'], sexes=['female', 'total'])
model.input_data = model.input_data[model.input_data.data_type != 'i']
print('kept %d rows' % len(model.input_data.index))
model.setup_model()
%time model.fit(iter=iter, burn=burn, thin=thin)
models['p, r, smr'] = model
model.plot()
Explanation: Consistent fit without incidence
Now let's do it again with the incidence removed.  Since there is data on prevalence and SMR as well as the assumption that remission is zero, this counts as three data types, the minimum allowed for DisMod-II.
End of explanation
model = dismod_mr.load('pd_sim_data/')
model.keep(areas=['GBR'], sexes=['female', 'total'])
model.input_data = model.input_data[model.input_data.data_type == 'p']
print('kept %d rows' % len(model.input_data.index))
model.setup_model()
%time model.fit(iter=iter, burn=burn, thin=thin)
# the above took 20 minutes in 2013
models['p, r'] = model
model.plot()
Explanation: Consistent fit without incidence or mortality
This uses only prevalence data and the assumption that there is no remission, so it is not valid in DisMod-II.  The Bayesian priors included by default in DisMod-MR make it possible, but the tradeoff between incidence and mortality is not informed by any data in this case.
End of explanation
model = dismod_mr.load('pd_sim_data/')
model.keep(areas=['GBR'], sexes=['female', 'total'])
model.input_data = model.input_data[model.input_data.data_type == 'p']
print('kept %d rows' % len(model.input_data.index))
model.set_level_bounds('r', 0., 1.)
model.set_level_value('r', age_before=0., age_after=101., value=0)
model.setup_model()
%time model.fit(iter=iter, burn=burn, thin=thin)
models['p'] = model
model.plot()
Explanation: Consistent fit with only prevalence
Now without assumption of zero remission, DisMod-MR is going for a very underconstrained problem, and relies on the priors heavily.  However, the prevalence data is there, so the estimates of prevalence will not be changed much.
End of explanation
for i, (label, model) in enumerate(models.items()):
    plt.hist(model.vars['p']['mu_age'].trace().mean(1), density=True, histtype='step',
         color=dismod_mr.plot.colors[i%4], linewidth=3, linestyle=['solid','dashed'][i//4],
         label=label)
plt.legend(loc=(1.1,.1))
plt.title('Posterior Distribution Comparison\nCrude Prevalence');
for i, (label, model) in enumerate(models.items()):
    plt.hist(model.vars['i']['mu_age'].trace().mean(1), density=True, histtype='step',
         color=dismod_mr.plot.colors[i%4], linewidth=3, linestyle=['solid','dashed'][i//4],
         label=label)
plt.legend(loc=(1.1,.1))
plt.title('Posterior Distribution Comparison\nCrude Incidence');
Explanation: Comparison of alternative models
Let's compare the distributions for all of these now.  You can see that the more data there is, the more concentrated the posterior distribution becomes.
End of explanation
model = dismod_mr.load('pd_sim_data/')
model.keep(areas=['GBR'], sexes=['female', 'total'])
model.input_data = model.input_data[model.input_data.data_type != 'p']
print('kept %d rows' % len(model.input_data.index))
model.setup_model()
%time model.fit(iter=iter, burn=burn, thin=thin)
models['i, r, smr'] = model
model.plot()
Explanation: Consistent fit without prevalence
The really challenging case is without any prevalence data. DisMod-MR will go for it, but there will be a lot of uncertainty.
End of explanation
model = dismod_mr.load('pd_sim_data/')
model.keep(areas=['GBR'], sexes=['female', 'total'])
model.input_data = model.input_data[model.input_data.data_type == 'i']
print('kept %d rows' % len(model.input_data.index))
model.set_level_bounds('r', 0., 1.)
model.setup_model()
%time model.fit(iter=iter, burn=burn, thin=thin)
models['i'] = model
model.plot()
Explanation: Consistent fit with incidence only
DisMod-MR it will even go for it with only incidence.  But that is not ideal...
End of explanation
model = dismod_mr.load('pd_sim_data/')
model.keep(areas=['GBR'], sexes=['female', 'total'])
model.input_data = model.input_data[model.input_data.data_type == 'smr']
print('kept %d rows' % len(model.input_data.index))
model.setup_model()
%time model.fit(iter=iter, burn=burn, thin=thin)
models['r, smr'] = model
model.plot()
for i, label in enumerate(['p', 'i, r, smr', 'i', 'r, smr']):
    try:
        plt.hist(models[label].vars['p']['mu_age'].trace().mean(1), density=False, histtype='step',
             color=dismod_mr.plot.colors[i%4], linewidth=3, linestyle=['solid','dashed'][i//4],
             label=label)
    except AttributeError as e:
        print(e)
plt.legend(loc=(1.1,.1))
plt.title('Posterior Distribution Comparison\nCrude Prevalence')
plt.axis(xmin=-.001);
!date
Explanation: Consistent fit without prevalence or incidence
DisMod-MR is not magic, however.  Without prevalence or incidence, it will not know how much PD there is!
End of explanation
<END_TASK>