knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

ABMR6 logo

Illustrating how to build agent-based modelling using R6.

GitHub GitHub R package version GitHub code size in bytes


Installation

To install this package, simply run the following code:

if (!require("remotes")) install.packages("remotes")
install_github("data-zoo-gang/ABMR6")

Usage

After installing this package, you can simply follow the steps below to run the simulations. Then explore the R6 classes defined in this package to see how things work (?simulation, ?world, ?population, ?moth).

library(ABMR6)
set.seed(123)
simu <- simulation$new(years = 200, N = 500, mutation_rate = 1e-2, period = 50)
simu$run()
simu$plot()

You can also run the following to explore the content of the objects in the middle of a simulation:

set.seed(123)
simu <- simulation$new(years = 200, N = 500, mutation_rate = 1e-2, period = 50)
simu$run(year_to_browse = 50)

Then, to explore the content of the running simulation in details, type:

## to display the running instance of the class simulation
self
## to display the running instance of the class world
self$world
## to display the running instance of the class population
self$population
## to display all agents of the simulation
self$population$individuals 
## to return proportion of black moths at the current time step
self$population$individuals$fetch_black_proportion()

Coda

The example treated here is inspired in part by the well known peppered moth evolution story (https://en.wikipedia.org/wiki/Peppered_moth_evolution). It considers that the environment is either black or white and that the colour switches regularly. The grey line on the plot shows the colour of the environment (top means black and bottom means white). The blue line shows how the allele frequency for the gene coding the black colour evolves through time. Evolution happens because of both drift and selection. The selection happens because I considered that moths (or butterflies in the files) have a lower fitness if they are not in an environment matching their colour (because, say, the predators spot them better).



data-zoo-gang/ABMR6 documentation built on Dec. 19, 2021, 8:12 p.m.