Title: | Individual Based Model Population Simulation |
---|---|
Description: | Simulation of the random evolution of heterogeneous populations using stochastic Individual-Based Models (IBMs) <doi:10.48550/arXiv.2303.06183>. The package enables users to simulate population evolution, in which individuals are characterized by their age and some characteristics, and the population is modified by different types of events, including births/arrivals, death/exit events, or changes of characteristics. The frequency at which an event can occur to an individual can depend on their age and characteristics, but also on the characteristics of other individuals (interactions). Such models have a wide range of applications. For instance, IBMs can be used for simulating the evolution of a heterogeneous insurance portfolio with selection or for validating mortality forecasts. This package overcomes the limitations of time-consuming IBMs simulations by implementing new efficient algorithms based on thinning methods, which are compiled using the 'Rcpp' package while providing a user-friendly interface. |
Authors: | Daphné Giorgi [aut, cre], Sarah Kaakai [aut], Vincent Lemaire [aut] |
Maintainer: | Daphné Giorgi <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.0 |
Built: | 2025-02-08 05:39:02 UTC |
Source: | https://github.com/daphnegiorgi/ibmpopsim |
Simulation of the random evolution of heterogeneous populations using stochastic Individual-Based Models (IBMs) doi:10.48550/arXiv.2303.06183. The package enables users to simulate population evolution, in which individuals are characterized by their age and some characteristics, and the population is modified by different types of events, including births/arrivals, death/exit events, or changes of characteristics. The frequency at which an event can occur to an individual can depend on their age and characteristics, but also on the characteristics of other individuals (interactions). Such models have a wide range of applications. For instance, IBMs can be used for simulating the evolution of a heterogeneous insurance portfolio with selection or for validating mortality forecasts. This package overcomes the limitations of time-consuming IBMs simulations by implementing new efficient algorithms based on thinning methods, which are compiled using the 'Rcpp' package while providing a user-friendly interface.
Maintainer: Daphné Giorgi [email protected]
Authors:
Sarah Kaakai [email protected]
Vincent Lemaire [email protected]
Useful links:
Report bugs at https://github.com/DaphneGiorgi/IBMPopSim/issues
Generic method for add_characteristic
add_characteristic(x, name, value = NA)
add_characteristic(x, name, value = NA)
x |
An object. |
name |
Name of the characteristic to add. |
value |
Value of the characteristic. By default |
Add characteristic to a population
## S3 method for class 'population' add_characteristic(x, name, value = NA)
## S3 method for class 'population' add_characteristic(x, name, value = NA)
x |
Object of population class representing a population. |
name |
Name of the characteristic to add. |
value |
Value of the characteristic. By default |
Generic method for age_pyramid
age_pyramid(object, time = 0, ages = c(0:110, Inf), ...)
age_pyramid(object, time = 0, ages = c(0:110, Inf), ...)
object |
Population. |
time |
The age pyramid is computed at instant |
ages |
(Optional) A numeric vector of distinct positive values composing age groups. Must be in increasing order. |
... |
Additional parameters |
An object of class pyramid
containing the age pyramid of a population at instant time
.
Reduce a population containing all individuals (with some characteristics) to an age-groups data frame (preserving characteristics). The function computes the number of individuals at time
in each age group [ages[i],ages[i+1][
, for i
in {1,...,N-1}
.
## S3 method for class 'population' age_pyramid(object, time = 0, ages = c(0:110, Inf), ...)
## S3 method for class 'population' age_pyramid(object, time = 0, ages = c(0:110, Inf), ...)
object |
Object of |
time |
The age pyramid is computed at instant |
ages |
(Optional) A numeric vector of distinct positive values composing age groups. Must be in increasing order. |
... |
Additional parameters |
An object of class pyramid
containing the age pyramid of the given population at instant time
.
age_pyramid(population(EW_pop_14$sample), time = 0) age_pyramid(population(EW_popIMD_14$sample), time = 0, ages = seq(0, 120, by=2))
age_pyramid(population(EW_pop_14$sample), time = 0) age_pyramid(population(EW_popIMD_14$sample), time = 0, ages = seq(0, 120, by=2))
Generic method for age_pyramids
age_pyramids(object, time = 0, ages = c(0:110, Inf))
age_pyramids(object, time = 0, ages = c(0:110, Inf))
object |
Population. |
time |
The age pyramid is computed at instant |
ages |
(Optional) A numeric vector of distinct positive values composing age groups. Must be in increasing order. |
Vectorial version in time of the function age_pyramid.population
. Not compatible with IBMs including swap events.
## S3 method for class 'population' age_pyramids(object, time = 0, ages = c(0:110, Inf))
## S3 method for class 'population' age_pyramids(object, time = 0, ages = c(0:110, Inf))
object |
Object of |
time |
The age pyramid is computed at instant |
ages |
(Optional) A numeric vector of distinct positive values composing age groups. Must be in increasing order. |
For convenience. This is a just a lapply
call of age_pyramid.population
on the vector time
.
Verifies that the intensity contains the string 'result'.
check_intensity_code(code)
check_intensity_code(code)
code |
String containing the intensity code. |
Verifies that the interaction contains the string 'result'.
check_interaction_code(code)
check_interaction_code(code)
code |
String containing the interaction code. |
Verifies the kernel code.
check_kernel_code(code)
check_kernel_code(code)
code |
String containing the kernel code. |
A function to check the compatibility between characteristics and events
compatibility_chars_events(characteristics, events)
compatibility_chars_events(characteristics, events)
characteristics |
List of characteristics |
events |
List of events |
A function to check the compatibility between a population and a model
compatibility_pop_model(pop, model)
compatibility_pop_model(pop, model)
pop |
An object of class population |
model |
An Individual Based Model created with the mk_model function |
Creates a death table from a population object.
For each i=1..N-1
and j=1..M
, the number of individuals with age at last birthday in [ages[i],ages[i+1])
and died in [times[j],times[j+1])
is computed.
death_table(pop, ages, period)
death_table(pop, ages, period)
pop |
Object of class |
ages |
A vector of size |
period |
A vector of size |
The function computes the number of death in each time interval [times[j],times[j+1])
, j=1..M
.
A death table matrix.
dth_table <- death_table(population(EW_pop_out), 0:101, 0:11)
dth_table <- death_table(population(EW_pop_out), 0:101, 0:11)
EW 2014 population and death rates by age and gender (Source: Office for National Statistics, reference number 006518).
Female birth rates by age of the mother (Source: Office for National Statistics birth summary tables).
EW_pop_14
EW_pop_14
A list containing:
age_pyramid
Data frame containing EW age pyramid for year 2014, by gender and single year of age (0-115).
rates
A list containing three data frames:
birth
Birth rates data frame, by age of mother and 5 years age groups.
death_male
Male death rates data frame, by single year of age (0-90+).
death_female
Female death rates dataframe, by single year of age (0-90+).
sample
Population dataframe composed of 100 000 individuals, sampled from age_pyramid
.
Example of "human population" data frame after 100 years of simulation, based on a sample of England and Wales 2014 population and demographic rates.
EW_pop_out
EW_pop_out
Data frame containing a population structured by age and gender,
simulated with an initial population of 100 000 individuals sampled from EW_pop_14$age_pyramid
over 100 years, with birth and death events.
EW population, death rates by age, gender and IMD for year 2014 (Source: Office for National Statistics, reference number 006518).
EW_popIMD_14
EW_popIMD_14
A list containing:
age_pyramid
Data frame containing EW age pyramid for year 2014, by gender, IMD and single year of age (0-115).
Individuals in the age class 90+ are distributed in the single year of age classes as in the EW population.
death_rates
List containing 4 fields:
male
Male death rates data frame, by IMD and single year of age (0-90+).
female
Female death rates dataframe, by IMD and single year of age (0-90+).
sample
Population dataframe composed of 100 000 individuals, sampled from age_pyramid
.
Obtained with
EWdata_hmd <- hmd.mx(country = "GBRTENW", username = ... , password = ...,label = "England and Wales")
EWdata_hmd
EWdata_hmd
An object of class demogdata
of length 7.
Returns the Central Exposure-to-Risk for given ages groups and time period. The central Exposure-to-risk is computed as the sum of the time spent by individuals in a given age group over a given period, where age is the age at last birthday.
exposure_table(pop, ages, period)
exposure_table(pop, ages, period)
pop |
Object of class |
ages |
A vector of size |
period |
A vector of size |
The function computes the central exposure-to-risk in each time interval [t[j],t[j+1])
, j=1..M
, and age groups.
An exposure matrix
ex_table <- exposure_table(population(EW_pop_out),0:101,0:11)
ex_table <- exposure_table(population(EW_pop_out),0:101,0:11)
Generic method for get_characteristics
get_characteristics(object, ...)
get_characteristics(object, ...)
object |
An object. |
... |
Additional parameters. |
Returns names and C types of the characteristics (other than birth and death) of the individuals in a population, from a population data frame.
## S3 method for class 'population' get_characteristics(object, ...)
## S3 method for class 'population' get_characteristics(object, ...)
object |
Object of population class representing a population. |
... |
additional arguments. |
Named vector composed of characteristics names and C types. If the population has no characteristics, which means that it has only the birth
and death
columns, this returns NULL
.
get_characteristics(population(EW_pop_14$sample))
get_characteristics(population(EW_pop_14$sample))
The intensity function (or hazard function) for the Gompertz-Makeham law of mortality distribution is defined as
with .
gompertz(alpha, beta, lambda = 0)
gompertz(alpha, beta, lambda = 0)
alpha |
Non-negative real parameter. |
beta |
Non-negative real parameter. |
lambda |
Non-negative real parameter. |
A C++ version of this function is available. See vignette('IBMPopSim_cpp')
for more details.
Function which associates x
to .
https://en.wikipedia.org/wiki/Gompertz%E2%80%93Makeham_law_of_mortality
Return a function performing the linear interpolation.
linfun(x, y, yleft = y[1], yright = y[length(y)])
linfun(x, y, yleft = y[1], yright = y[length(y)])
x , y
|
Numeric vectors giving the coordinates of the points to be interpolated. |
yleft |
The value to be returned when input |
yright |
The value to be returned when input |
A C++ version of this function is available. See vignette('IBMPopSim_cpp')
for more details.
Objet of class linfun
and function
which is an approxfun
function with method = 'linear'
.
stepfun
.Returns the maximum of a function of class stepfun
.
## S3 method for class 'stepfun' max(..., na.rm = FALSE)
## S3 method for class 'stepfun' max(..., na.rm = FALSE)
... |
argument of class |
na.rm |
a logical indicating whether missing values should be removed |
The maximum of the step function.
A function returning a merged dataframe from a list of population dataframes with id.
merge_pop_withid(pop_df_list, chars_tracked = NULL)
merge_pop_withid(pop_df_list, chars_tracked = NULL)
pop_df_list |
A list of population dataframe where the first three columns of each dataframe are id, birth and death. |
chars_tracked |
A vector of characteristics to be tracked over time. |
A dataframe composed of all individuals with their characteristics at each simulation time.
Creates an event with intensity of class individual (without interactions). When the event occurs, something happens to an individual I
in the population.
The created event must be used with mk_model
.
mk_event_individual(type, name, intensity_code, kernel_code = "")
mk_event_individual(type, name, intensity_code, kernel_code = "")
type |
Must be one of |
name |
(Optional) If not specified, the name given to the event is its type. |
intensity_code |
String containing some C++ code describing the intensity function. See details. |
kernel_code |
String containing some C++ code describing the event action. Optional for |
The type
argument is one of the following
'birth'
By default, a new individual newI
is created, with the same characteristics of the parent I
and birth date equal to the current time. Optional code can be precised in kernel_code
.
'death'
By default, the individual I
dies. Optional code can be precised in kernel_code
.
'entry'
A new individual newI
is added to the population, and its characteristics have to be defined by the user in the entry kernel_code
.
'exit'
An individual I
exits from the population. Optional code can be precised in kernel_code
.
'swap'
The user can change the characteristics of the selected individual I
. This requires kernel_code
.
'custom'
None of the above types, the user defines kernel_code
that can act on the selected individual I
and on the population pop
.
The intensity_code
argument is a string containing some C++ code describing the event intensity for individual I
at time t
. The intensity value must be stored in the variable result
.
Some of available variables in the C++ code are: t
(the current time), I
(the current individual selected for the event), the name of the model parameters (some variables, or functions, see mk_model
).
See vignette('IBMPopSim_Cpp')
for more details.
The kernel_code
argument is a string containing some C++ code which describing the action of the event. Some of available variables in the C++ code are: t
(the current time), pop
(the current population), I
(the current individual selected for the event), newI
(the new individual if 'birth'
or 'entry'
event), the name of the model parameters (some variables, or functions, see mk_model
).
See vignette('IBMPopSim')
for more details.
An S3 object of class event
of type individual.
mk_model
, mk_event_poisson
, mk_event_inhomogeneous_poisson
, and mk_event_interaction
.
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", name = "my_death_event", 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;")
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", name = "my_death_event", 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;")
The function mk_event_inhomogeneous_poisson
is used to create an event with intensity type inhomogeneous Poisson (time dependent intensity which does not depend on population).
When the event occurs, something happens in the population.
The created event must be used with mk_model
.
mk_event_inhomogeneous_poisson(type, name, intensity_code, kernel_code = "")
mk_event_inhomogeneous_poisson(type, name, intensity_code, kernel_code = "")
type |
Must be one of |
name |
(Optional) If not specified, the name given to the event is its type. |
intensity_code |
String containing some C++ code describing the intensity function. See details. |
kernel_code |
String containing some C++ code describing the event action. Optional for |
The type
argument is one of the following
'birth'
By default, a new individual newI
is created, with the same characteristics of the parent I
and birth date equal to the current time. Optional code can be precised in kernel_code
.
'death'
By default, the individual I
dies. Optional code can be precised in kernel_code
.
'entry'
A new individual newI
is added to the population, and its characteristics have to be defined by the user in the entry kernel_code
.
'exit'
An individual I
exits from the population. Optional code can be precised in kernel_code
.
'swap'
The user can change the characteristics of the selected individual I
. This requires kernel_code
.
'custom'
None of the above types, the user defines kernel_code
that can act on the selected individual I
and on the population pop
.
The intensity_code
argument is a string containing some C++ code describing the event intensity for individual I
at time t
. The intensity value must be stored in the variable result
.
Some of available variables in the C++ code are: t
(the current time), I
(the current individual selected for the event), the name of the model parameters (some variables, or functions, see mk_model
).
See vignette('IBMPopSim_Cpp')
for more details.
The kernel_code
argument is a string containing some C++ code which describing the action of the event. Some of available variables in the C++ code are: t
(the current time), pop
(the current population), I
(the current individual selected for the event), newI
(the new individual if 'birth'
or 'entry'
event), the name of the model parameters (some variables, or functions, see mk_model
).
See vignette('IBMPopSim')
for more details.
An S3 object of class event
of type inhomogeneous Poisson.
mk_model
, mk_event_poisson
, mk_event_individual
, mk_event_interaction
.
Creates an event whose intensity depends on an individual and interactions with the population. When the event occurs, something happens to an individual I
in the population. The intensity of the event can depend on time, the characteristics of I and other individuals in the population, and can be written as
where is called the interaction function.
The created event must be used with
mk_model
.
mk_event_interaction( type, name, interaction_code, kernel_code = "", interaction_type = "random" )
mk_event_interaction( type, name, interaction_code, kernel_code = "", interaction_type = "random" )
type |
Must be one of |
name |
(Optional) If not specified, the name given to the event is its type. |
interaction_code |
String containing some C++ code describing the interaction function. See details. |
kernel_code |
String containing some C++ code describing the event action. Optional for |
interaction_type |
(Optional) Either |
The type
argument is one of the following
'birth'
By default, a new individual newI
is created, with the same characteristics of the parent I
and birth date equal to the current time. Optional code can be precised in kernel_code
.
'death'
By default, the individual I
dies. Optional code can be precised in kernel_code
.
'entry'
A new individual newI
is added to the population, and its characteristics have to be defined by the user in the entry kernel_code
.
'exit'
An individual I
exits from the population. Optional code can be precised in kernel_code
.
'swap'
The user can change the characteristics of the selected individual I
. This requires kernel_code
.
'custom'
None of the above types, the user defines kernel_code
that can act on the selected individual I
and on the population pop
.
The interaction_code
argument is a string containing some C++ code describing the event interaction function $U$ at time t
. The interaction value must be stored in the variable result
.
Some of available variables in the C++ code are: t
(the current time), I
(the current individual selected for the event), J
(another individual if interaction_type
is 'random'
), the name of the model parameters (some variables, or functions, see mk_model
).
See vignette('IBMPopSim_Cpp')
for more details.
The kernel_code
argument is a string containing some C++ code which describing the action of the event. Some of available variables in the C++ code are: t
(the current time), pop
(the current population), I
(the current individual selected for the event), newI
(the new individual if 'birth'
or 'entry'
event), the name of the model parameters (some variables, or functions, see mk_model
).
See vignette('IBMPopSim')
for more details.
An S3 object of class event
of type interaction.
mk_model
, mk_event_poisson
, mk_event_inhomogeneous_poisson
, mk_event_individual
.
death_interaction_code<- " result = max(J.size -I.size,0);" event <- mk_event_interaction(type="death", interaction_code = death_interaction_code)
death_interaction_code<- " result = max(J.size -I.size,0);" event <- mk_event_interaction(type="death", interaction_code = death_interaction_code)
The function mk_event_poisson
is used to create an event with intensity of type Poisson (constant intensity which does not depend on population or time).
When the event occurs, something happens in the population.
The created event must be used with mk_model
.
mk_event_poisson(type, name, intensity, kernel_code = "")
mk_event_poisson(type, name, intensity, kernel_code = "")
type |
Must be one of |
name |
(Optional) If not specified, the name given to the event is its type. |
intensity |
String containing some constant positive value, or name of a parameter which is a constant positive value. |
kernel_code |
String containing some C++ code describing the event action. Optional for |
The type
argument is one of the following
'birth'
By default, a new individual newI
is created, with the same characteristics of the parent I
and birth date equal to the current time. Optional code can be precised in kernel_code
.
'death'
By default, the individual I
dies. Optional code can be precised in kernel_code
.
'entry'
A new individual newI
is added to the population, and its characteristics have to be defined by the user in the entry kernel_code
.
'exit'
An individual I
exits from the population. Optional code can be precised in kernel_code
.
'swap'
The user can change the characteristics of the selected individual I
. This requires kernel_code
.
'custom'
None of the above types, the user defines kernel_code
that can act on the selected individual I
and on the population pop
.
The kernel_code
argument is a string containing some C++ code which describing the action of the event. Some of available variables in the C++ code are: t
(the current time), pop
(the current population), I
(the current individual selected for the event), newI
(the new individual if 'birth'
or 'entry'
event), the name of the model parameters (some variables, or functions, see mk_model
).
See vignette('IBMPopSim')
for more details.
An S3 object of class event
of type Poisson.
mk_model
, mk_event_inhomogeneous_poisson
, mk_event_individual
, mk_event_interaction
.
birth <- mk_event_poisson('birth', intensity = 10) params <- list(beta = 10) death <- mk_event_poisson('death', intensity = 'beta') # name of one parameter mk_model(events = list(birth, death), parameters = params)
birth <- mk_event_poisson('birth', intensity = 10) params <- list(beta = 10) death <- mk_event_poisson('death', intensity = 'beta') # name of one parameter mk_model(events = list(birth, death), parameters = params)
This function creates an Individual Based Model describing the population, events which can occur in the population, and the model parameters.
mk_model( characteristics = NULL, events, parameters = NULL, with_compilation = TRUE )
mk_model( characteristics = NULL, events, parameters = NULL, with_compilation = TRUE )
characteristics |
List containing names and types of characteristics of individuals in the population. See |
events |
List of events in the model. See |
parameters |
Model parameters. A list of parameters of the model. |
with_compilation |
(Optional) Logical parameter, |
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
.
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.
popsim
, mk_event_poisson
, mk_event_individual
, mk_event_interaction
.
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)
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)
Given the vectors (breaks[1],...,breaks[n])
and the list of IBMPopSim
compatible
functions funs = (f[0],f[1],...,f[n])
(one value more!), piecewise_x(breaks, funs)
returns
the function
piecewise_x(breaks, funs)
piecewise_x(breaks, funs)
breaks |
Numeric vector giving the breaks of functions given in |
funs |
List of functions. |
A C++ version of this function is available. See vignette('IBMPopSim_cpp')
for more details.
Piecewise function built with the given intervals and functions.
dr <- with(EW_pop_14$rates, stepfun(x=death_male[,"age"], y=c(0,death_male[,"value"]))) # before age 80 the stepfun and after age 80 the gompertz function f <- piecewise_x(80, list(dr, gompertz(0.00006, 0.085))) x <- seq(40:120) plot(x, sapply(x, f))
dr <- with(EW_pop_14$rates, stepfun(x=death_male[,"age"], y=c(0,death_male[,"value"]))) # before age 80 the stepfun and after age 80 the gompertz function f <- piecewise_x(80, list(dr, gompertz(0.00006, 0.085))) x <- seq(40:120) plot(x, sapply(x, f))
Given the vectors (breaks[1],...,breaks[n])
and the list of IBMPopSim
compatible
functions funs = (f[0],f[1],...,f[n])
(one value more!), piecewise_xy(breaks, funs)
returns
the function
piecewise_xy(breaks, funs)
piecewise_xy(breaks, funs)
breaks |
Numeric vector giving the breaks of functions given in |
funs |
List of functions. |
A C++ version of this function is available. See vignette('IBMPopSim_cpp')
for more details.
Piecewise bivariate function built with the given intervals and functions.
time_dep_function <- piecewise_xy(c(5), list(gompertz(0.1, 0.005), gompertz(0.08, 0.005))) time_dep_function(0, 65) # death intensity at time 0 and age 65.
time_dep_function <- piecewise_xy(c(5), list(gompertz(0.1, 0.005), gompertz(0.08, 0.005))) time_dep_function(0, 65) # death intensity at time 0 and age 65.
Plot an age pyramid from age pyramid data frame with possibly several characteristics.
## S3 method for class 'population' plot( x, group_colors = NULL, group_legend = "Group", age_breaks = NULL, value_breaks = NULL, ... )
## S3 method for class 'population' plot( x, group_colors = NULL, group_legend = "Group", age_breaks = NULL, value_breaks = NULL, ... )
x |
Object of class population. |
group_colors |
(Optional) Named character vector. |
group_legend |
(Optional) Legend title name. By default set to |
age_breaks |
(Optional) An ordered vector of indexes of vector |
value_breaks |
(Optional) Breaks for the axis of values. |
... |
Additional arguments |
Plot of age pyramid.
plot.pyramid
, age_pyramid.population
.
plot(population(EW_pop_14$sample), time = 0)
plot(population(EW_pop_14$sample), time = 0)
Plot an age pyramid from age pyramid data frame with possibly several characteristics.
## S3 method for class 'pyramid' plot( x, group_colors = NULL, group_legend = "Group", age_breaks = NULL, value_breaks = NULL, ... )
## S3 method for class 'pyramid' plot( x, group_colors = NULL, group_legend = "Group", age_breaks = NULL, value_breaks = NULL, ... )
x |
Object of class (Optional) For plotting an age pyramid composed of several subgroups, the population data frame must contain a column named |
group_colors |
(Optional) Named character vector. |
group_legend |
(Optional) Legend title name. By default set to |
age_breaks |
(Optional) An ordered vector of indexes of vector |
value_breaks |
(Optional) Breaks for the axis of values. |
... |
Additional parameters |
Plot of the age pyramid.
plot.pyramid(subset(pyramid(EW_pop_14$age_pyramid), as.numeric(age) <= 110)) library(colorspace) pyr_IMD <- subset(pyramid(EW_popIMD_14$age_pyramid), as.numeric(age) <= 110) pyr_IMD$group_name <- with(pyr_IMD, ifelse(male, paste('Males - IMD', IMD), paste('Females - IMD', IMD))) colors <- c(sequential_hcl(n=5, palette = "Magenta"), sequential_hcl(n=5, palette = "Teal")) names(colors) <- c(paste('Females - IMD', 1:5), paste('Males - IMD', 1:5)) # note that you must have setequal(names(colors), pyr_IMD$group_name) is TRUE plot.pyramid(pyr_IMD, colors) # age pyramids at different times library(gganimate) pyrs = age_pyramids(population(EW_popIMD_14$sample), time = 1:10) plot.pyramid(pyrs) + transition_time(time) + labs(title = "Time: {frame_time}")
plot.pyramid(subset(pyramid(EW_pop_14$age_pyramid), as.numeric(age) <= 110)) library(colorspace) pyr_IMD <- subset(pyramid(EW_popIMD_14$age_pyramid), as.numeric(age) <= 110) pyr_IMD$group_name <- with(pyr_IMD, ifelse(male, paste('Males - IMD', IMD), paste('Females - IMD', IMD))) colors <- c(sequential_hcl(n=5, palette = "Magenta"), sequential_hcl(n=5, palette = "Teal")) names(colors) <- c(paste('Females - IMD', 1:5), paste('Males - IMD', 1:5)) # note that you must have setequal(names(colors), pyr_IMD$group_name) is TRUE plot.pyramid(pyr_IMD, colors) # age pyramids at different times library(gganimate) pyrs = age_pyramids(population(EW_popIMD_14$sample), time = 1:10) plot.pyramid(pyrs) + transition_time(time) + labs(title = "Time: {frame_time}")
Generic method for popsample
popsample(age_pyramid, size, age_max = 120, time = 0)
popsample(age_pyramid, size, age_max = 120, time = 0)
age_pyramid |
Age pyramid. |
size |
A non-negative integer giving the number of individuals in population. |
age_max |
(Optional) A non-negative numeric which replace (if exists) the |
time |
(Optional) The age pyramid is computed at instant |
Object of population
class representing a data frame of size size
containing a population of individuals.
Sample a population from an age pyramid (at a given time).
## S3 method for class 'pyramid' popsample(age_pyramid, size, age_max = 120, time = 0)
## S3 method for class 'pyramid' popsample(age_pyramid, size, age_max = 120, time = 0)
age_pyramid |
Object of |
size |
A non-negative integer giving the number of individuals in population. |
age_max |
(Optional) A non-negative numeric which replace (if exists) the |
time |
(Optional) The age pyramid is computed at instant |
Object of population
class representing a data frame of size size
containing a population of individuals.
pop_sample_1e4 <- popsample(pyramid(EW_pop_14$age_pyramid), size = 1e4)
pop_sample_1e4 <- popsample(pyramid(EW_pop_14$age_pyramid), size = 1e4)
This function simulates the random evolution of an IBM.
popsim( model, initial_population, events_bounds, parameters = NULL, age_max = Inf, time, multithreading = FALSE, num_threads = NULL, clean_step = NULL, clean_ratio = 0.1, seed = NULL, verbose = FALSE )
popsim( model, initial_population, events_bounds, parameters = NULL, age_max = Inf, time, multithreading = FALSE, num_threads = NULL, clean_step = NULL, clean_ratio = 0.1, seed = NULL, verbose = FALSE )
model |
Model resulting from a call to the function |
initial_population |
Object of population class representing the initial population. |
events_bounds |
Named vector of events bounds, with names corresponding to events names. |
parameters |
List of model parameters. |
age_max |
Maximum age of individuals in the population ( |
time |
Final time (Numeric). Can be of length 1 or a vector of simulation discretized times. |
multithreading |
Logical for multithread activation, |
num_threads |
(Optional) Number of threads used for multithreading. Set by default to the number of concurrent threads supported by the available hardware implementation. |
clean_step |
(Optional) Optional parameter for improving simulation time. Time step for removing dead (or exited) individuals from the population. By default, equal to age_max. |
clean_ratio |
(Optional) Optional parameter for improving simulation time. 0.1 by default. |
seed |
(Optional) Random generator seed, random by default. |
verbose |
(Optional) Activate verbose output, FALSE by default. |
List composed of
Simulation inputs (initial population, parameters value, multithreading...)
Simulation logs (algorithm duration, accepted/rejected events...).
If time
is of length 1, population is an object of type population containing of all individuals who lived in the population in the time interval [0,time]
. If time
is a vector (time[1], ..., time[n]
), population
is a list of n
objects of type population, each representing the state of the population at time time[i]
, for i = 1,..., n
.
init_size <- 100000 pop_df <- data.frame(birth = rep(0, init_size), death = NA) pop <- population(pop_df) birth = mk_event_poisson(type = 'birth', intensity = 'lambda') death = mk_event_poisson(type = 'death', intensity = 'mu') params = list('lambda' = 100, 'mu' = 100) birth_death <- mk_model(events = list(birth, death), parameters = params) sim_out <- popsim(model = birth_death, initial_population = pop, events_bounds = c('birth' = params$lambda, 'death' = params$mu), parameters = params, time = 10)
init_size <- 100000 pop_df <- data.frame(birth = rep(0, init_size), death = NA) pop <- population(pop_df) birth = mk_event_poisson(type = 'birth', intensity = 'lambda') death = mk_event_poisson(type = 'death', intensity = 'mu') params = list('lambda' = 100, 'mu' = 100) birth_death <- mk_model(events = list(birth, death), parameters = params) sim_out <- popsim(model = birth_death, initial_population = pop, events_bounds = c('birth' = params$lambda, 'death' = params$mu), parameters = params, time = 10)
Data frame containing a population, with at least a birth and a death column, and eventually some other characteristics
population(x, entry = FALSE, out = FALSE, id = FALSE)
population(x, entry = FALSE, out = FALSE, id = FALSE)
x |
Data frame or list of data frames, containing at least a birth and a death column |
entry |
Boolean flag. By default set to FALSE. If set to TRUE the population must contain a column of numerical values named "entry", If the column doesn't exist a column named "entry" is added to the data frame with all values set to NA. |
out |
Boolean flag. By default set to FALSE. If set to TRUE the population must contain a column of boolean values named "out", If the column doesn't exist a column named "out" is added to the data frame with all the values set to FALSE. |
id |
Boolean flag. By default set to FALSE. If set to TRUE the population must contain a column of integer distinct values named "id". If the column doesn't exist a column named "id" is added to the data frame with values seq(1, nrow(x)). |
Given data frame augmented of the "population" class. If a list of data frames is given, the column names should contain the string "id" and the list corresponds to the evolution of a population at different times. The constructor then returns the last population observed in the list (corresponding to the final state of the population).
Generic method for population_alive
population_alive(object, t, a1 = 0, a2 = Inf, ...)
population_alive(object, t, a1 = 0, a2 = Inf, ...)
object |
A population. |
t |
A numeric indicating the time at which alive individuals are observed. |
a1 |
0 by default. Lower bound for age. |
a2 |
Inf by default. Upper bound for age. |
... |
Additional params. |
All individuals alive at time t
and of age in [a1,a2)
.
Returns a population of individuals alive.
## S3 method for class 'population' population_alive(object, t, a1 = 0, a2 = Inf, ...)
## S3 method for class 'population' population_alive(object, t, a1 = 0, a2 = Inf, ...)
object |
A population data frame containing at least a column |
t |
A numeric indicating the time. |
a1 |
0 by default. All individuals of age over |
a2 |
Inf by default. All individuals of age below |
... |
Additional params. |
The function returns a population data frame containing all individuals alive at time t
and of age in [a1,a2)
.
print
method for class "event" giving a short description of an event.
## S3 method for class 'event' print(x, ...)
## S3 method for class 'event' print(x, ...)
x |
Argument of class |
... |
Additional arguments affecting the summary produced. |
print
method for class model
.
## S3 method for class 'model' print(x, ...)
## S3 method for class 'model' print(x, ...)
x |
argument of class |
... |
additional arguments affecting the summary produced. |
Print a population
## S3 method for class 'population' print(x, ...)
## S3 method for class 'population' print(x, ...)
x |
Object of population class representing a population. |
... |
Additional arguments |
Print the population
Data frame containing an age pyramid, with at least an age
and a value
column,
and eventually some other characteristics. If a male
column is present, it must be a logical vector,
if a group
column is present, it must be a vector of type character.
pyramid(x)
pyramid(x)
x |
Data frame, containing at least an |
Given data frame augmented of the "age_pyramid" class.
Given the vectors (x[1],...,x[n])
and (y[0],y[1],...,y[n])
(one value more!), stepfun(x, y)
returns an interpolating step function, say f_n
. This is the cadlag version (right = FALSE
) of the stepfun
function from package stats
. The step function value f_n(t)
equals to the constant y[k-1]
for t
in [x[k-1], x[k])
so that
with and
.
stepfun(x, y)
stepfun(x, y)
x |
Numeric vector giving the knots or jump locations of the step function. Must be sorted with unique values. |
y |
Numeric vector one longer than x, giving the heights of the function values between the c |
This function is defined for documentation purposes only. See stepfun
and approxfun
.
A C++ version of this function is available. See vignette('IBMPopSim_cpp')
for more details.
Objet of class stepfun
with option right = FALSE
(cadlag function).
plot.stepfun
and max.stepfun
.
summary
method for class event
giving a detailed description of an event.
## S3 method for class 'event' summary(object, ...)
## S3 method for class 'event' summary(object, ...)
object |
Argument of class |
... |
Additional arguments affecting the summary produced. |
Summary of the logs of a simulation
## S3 method for class 'logs' summary(object, ...)
## S3 method for class 'logs' summary(object, ...)
object |
Logs of the output of a call to popsim function |
... |
Additional arguments affecting the summary produced |
Print column names and number of individuals
summary
method for class model
.
## S3 method for class 'model' summary(object, ...)
## S3 method for class 'model' summary(object, ...)
object |
argument of class |
... |
additional arguments affecting the summary produced. |
Summary of a population with column names and number of individuals
## S3 method for class 'population' summary(object, ...)
## S3 method for class 'population' summary(object, ...)
object |
Object of population class representing a population. |
... |
Additional arguments affecting the summary produced |
Print column names and number of individuals
Summary of a simulation output
## S3 method for class 'simulation_output' summary(object, ...)
## S3 method for class 'simulation_output' summary(object, ...)
object |
Output of a call to popsim function |
... |
Additional arguments affecting the summary produced |
Summary of population(s) and the logs
Toy parameters for IBMPopSim-human_popIMD vignette example.
toy_params
toy_params
A list containing:
birth
A list of 3 numeric vectors (alpha
, beta
, TFR_weights
) for creating birth intensity with the Weibull probability density function.
swap
A List of one numeric vector and two data frames (ages
, intensities
and distribution
) for creating the swap intensity and kernel functions.
The Weibull (density) function is defined as
with .
weibull(k, lambda = 1)
weibull(k, lambda = 1)
k |
Shape parameter, a positive real number. |
lambda |
Scale parameter, a positive real number, defaults to 1. |
A C++ version of this function is available. See vignette('IBMPopSim_cpp')
for more details.
The Weibull density function dweibull
with shape parameter k
and scale parameter lambda
, see dweibull
.
https://en.wikipedia.org/wiki/Weibull_distribution