Skip to contents

This function creates an Individual Based Model describing the population, events which can occur in the population, and the model parameters.

Usage

mk_model(
  characteristics = NULL,
  events,
  parameters = NULL,
  with_compilation = TRUE
)

Arguments

characteristics

List containing names and types of characteristics of individuals in the population. See get_characteristics.

events

List of events in the model. See mk_event_poisson, mk_event_inhomogeneous_poisson, mk_event_individual, and mk_event_interaction.

parameters

Model parameters. A list of parameters of the model.

with_compilation

(Optional) Logical parameter, TRUE by default. If FALSE the sourceCpp function is not called.

Value

model List containing the built model :

  • individual_type: Names and types (R and C++) of characteristics.

  • parameters_types: Names and types (R and C++) of model parameters.

  • events: List of events.

  • cpp_code: Output of C++ compilation.

Details

It builds the C++ model code and produces the function popsim_cpp which will be used for simulating the model. The function used to simulate a population from a model is popsim.

Examples

# \donttest{
params <- list("p_male"= 0.51,
              "birth_rate" = stepfun(c(15,40),c(0,0.05,0)),
              "death_rate" = gompertz(0.008,0.02))

death_event <- mk_event_individual(type = "death",
                                  intensity_code = "result = death_rate(age(I,t));")

birth_event <- mk_event_individual(type = 'birth',
                                  intensity_code = "if (I.male) result = 0;
                                    else result=birth_rate(age(I,t));",
                                  kernel_code = "newI.male = CUnif(0, 1) < p_male;")

model <- mk_model(characteristics = get_characteristics(population(EW_pop_14$sample)),
                 events = list(death_event,birth_event),
                 parameters = params)

summary(model)
#> Events description:
#> [[1]]	
#> Event class : individual 
#> Event type : death 
#> Event name : death
#> Intensity code : 'result = death_rate(age(I,t));' 
#> Kernel code : '' 
#> [[2]]	
#> Event class : individual 
#> Event type : birth 
#> Event name : birth
#> Intensity code : 'if (I.male) result = 0;
#>                                     else result=birth_rate(age(I,t));' 
#> Kernel code : 'newI.male = CUnif(0, 1) < p_male;' 
#> 
#> --------------------------------------- 
#> Individual description:
#> names:  birth death male 
#> R types:  double double logical 
#> C types:  double double bool 
#> --------------------------------------- 
#> R parameters available in C++ code:
#> names:  p_male birth_rate death_rate 
#> R types:  double closure closure 
#> C types:  double function_x function_x 
# }