---------------------------------------------- lines 27-246 of file: example/user/diabetes.py ---------------------------------------------- # {xrst_begin user_diabetes.py} # {xrst_spell # bmi # def # elif # exp # mtspecific # rel # } # {xrst_comment_ch #} # # An Example Fitting Simulated Diabetes Data # ########################################## # This example fits iota and chi using only prevalence and mtspecific data. # It is designed so that you are changed the setting documented below. # See :ref:`user_example@Run One Example` for instruction on running this # example. # # random_seed # *********** # This integer is used to seed the random number generator. # If it is zero, the current number of seconds on the system clock is used. # {xrst_code py} random_seed = 0 # {xrst_code} # # age_list, time_list # ******************* # These are the ages and times at which we simulate the data. # They are also the ages and times where we model iota and chi. # {xrst_code py} age_list = [ 0.0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ] time_list = [ 1980, 1990, 2000, 2010, 2020 ] # {xrst_code} # # number_nodes # ************ # This integer is the number of nodes. # If it is one, there are no child nodes (hence no random effects). # Otherwise, there are *number_nodes* - 1 child nodes. # {xrst_code py} number_nodes = 10 # {xrst_code} # # sim_std_random_effect # ********************* # If there are child nodes, the random effect for each node is simulated # using a Gaussian with this standard deviation. # For each child node, the same random effect is used for both iota and chi. # {xrst_code py} sim_std_random_effect = 0.2 # {xrst_code} # # sim_mulcov # ********** # This python dictionary contains the covariate multiplier values used # to simulate the data. # In this example, there is one covariate multiplier for each covariate, # so we use the covariate names to identify the multipliers: # {xrst_code py} sim_mulcov = { 'sex':0.4, 'bmi':0.02, 'ms_2000':0.3 } # {xrst_code} # # .. csv-table:: # :header-rows: 1 # # Name, Covariate Value, Affects # sex, (female=-0.5) (male=0.5), chi # bmi, body mass index, iota # ms_2000, 1 (0) if year is (is not) 2000, prevalence # # sim_no_effect_rate # ****************** # This is the value used to simulate the no effect rates; i.e., # the rates before the random effects and covariate effects are included. # The dismod_at model for iota, chi and omega are constant with respect to # age and time outside the grid limits for the corresponding rate. # The last age is not included in the iota age grid because there # is no prevalence data after it. Hence iota is constant for ages after # the next to last age. # The first age is not included in the chi age grid because prevalence # is zero at that age (and mtspecific does not inform chi). # Hence chi is constant for ages before the second age. # {xrst_code py} def sim_no_effect_rate(rate_name, age, time) : time = min( time_list[-1], max(time_list[0], age) ) age_exp = - abs(age - 50.0) / 100.00 time_exp = - abs(time - 2000.0) / 40.0 if rate_name == 'iota' : age = min( age_list[-2], max(age_list[0], age) ) rate = 0.001 * ( 1.0 + math.exp(age_exp) + math.exp(time_exp) ) elif rate_name == 'chi' : # chi is currently constant so age does not matter, but we set it here # incase you change that. age = min( age_list[-1], max(age_list[1], age) ) rate = 0.02 elif rate_name == 'omega' : # For this example, omega must be constant for all age and time # (the value of omega does not affect prevalence or mtspecific). rate = 0.01 else : rate = None assert False return rate # {xrst_code} # # model_meas_cv # ************* # The measurements are simulated without noise, but the model # has this level of measurement nose (express as a coefficient of variation): # {xrst_code py} model_meas_cv = 0.1 # {xrst_code} # # minimum_prevalence_std # ********************** # In this model, initial prevalence is zero. Hence a coefficient of variation # model for the measurement noise does not work at age zero. # We use a minimum prevalence standard deviation to avoid this problem: # {xrst_code py} minimum_prevalence_std = 1e-4 # {xrst_code} # # ode_step_size # ************* # This is the option table :ref:`option_table@Age Average Grid@ode_step_size` # value. # {xrst_code py} ode_step_size = 5.0 # {xrst_code} # # hold_out_max_fit # **************** # This is the hold out command :ref:`hold_out_command@max_fit` value. # It is used to hold out prevalence and mtspecific data. # {xrst_code py} hold_out_max_fit = 250 # {xrst_code} # # fixed_effect_rel_error_bnd # ************************** # This is a bound on the relative error in the fixed effects estimates. # If the relative error is greater that this for a fixed effect, # a message that identifies the fixed effect and the relative error is # printed and this program will exit with a non-zero error flag. # {xrst_code py} fixed_effect_rel_error_bnd = 0.1 # {xrst_code} # # Priors # ****** # # parent_rate_value_prior # ======================= # This prior is used for the value of iota and chi, for the parent node, # at each of the grid points in the corresponding smoothings. # Note that omega is constrained to the value used for it during simulation. # Also note that eta does not affect the density but it does affect the # :ref:`prior_table@eta@Scaling Fixed Effects` . # {xrst_code py} parent_rate_value_prior = { 'density': 'gaussian', 'mean': 0.01, 'std': 1.0, 'lower': 1e-6, 'upper': 1.0, 'eta': 1e-6, } # {xrst_code} # # child_rate_value_prior # ====================== # This prior is used for the log of iota and chi for the child nodes. # These are random effects and are constant w.r.t. age, time; i.e., # there is only one grid point (one variable) # for each node and each rate (iota and chi) . # {xrst_code py} child_rate_value_prior = { 'density': 'gaussian', 'mean': 0.0, 'std': 0.1, } # {xrst_code} # # difference_prior # ================ # This is the prior used for the forward difference (w.r.t. age and time) # of the value of iota and chi between grid points. # {xrst_code py} difference_prior = { 'density': 'log_gaussian', 'mean': 0.0, 'std': 0.4, 'eta': 1e-6, } # {xrst_code} # # mulcov_prior # ============ # This is the value prior used for each of the covariate multipliers. # These are fixed effects and are constant w.r.t age, time. # The mean does not affect the density and # is used as a starting point for the first optimization. # (The first optimization is just w.r.t the fixed effects and is used to get # a starting point for optimization w.r.t. both the fixed and random effects.) # {xrst_code py} mulcov_prior = { 'density': 'uniform', 'mean': 0.0, } # {xrst_code} # # Source Code # *********** # {xrst_literal # # BEGIN_SOURCE_CODE # # END_SOURCE_CODE # } # # {xrst_end user_diabetes.py}