Genetic drift simulation

August 13, 2013

(This article was first published on R snippets, and kindly contributed to R-bloggers)

While preparing for the new teaching semester I have created an implementation of NetLogo GenDrift P local in GNU R.

The model works as follows. Initially a square grid having side size is randomly populated with n types of agents (denoted by colors).
Each agent randomly changes type (color) to one of its eight neighbors colors. In long run only one agent type remains due to random selection process (although it can take a long time if we set
side to a large value).

Here is the simulation code:

# initialize simulation
# side - square grid size
# n - number of agent types (from 2 to 6)
init <- function(side, n) {
matrix(, side * side, TRUE), side)

# plot simulation state
# s - simulation state

# n - initial number of agent types
do.plot <- function(s, n) {
print(levelplot(s, at = (0:n) + 0.5,
col.regions = 1 + (1:n),
 scales = list(draw = F), xlab = "", ylab = "",
colorkey = list(tick.number = n)))
# perform one step of simulation
# state - simulation state

sim.step <- function(state) {
updated <- FALSE
side <- dim(state)[1]
moves_x <- rep(-1:1, 3)[-5]
moves_y <- rep(-1:1, each = 3)[-5]
sequence <-
for (i in sequence) {
x <- 1 + (i - 1)%%side
y <- 1 + (i - 1)%/%side
ref <-, 1)
ref.x <- 1 + (x + moves_x[ref] - 1)%%side
ref.y <- 1 + (y + moves_y[ref] - 1)%%side
if (state[x, y] != state[ref.x, ref.y]) {
state[x, y] <- state[ref.x, ref.y]
updated <- TRUE
list(state, updated)

go <- function(size, n) {
s <- init(size, n)
updated <- TRUE
for (i in 1:4) {
do.plot(s, n)
i <- 0
while (updated) {
i <- i + 1
do.plot(s, n)
out <- sim.step(s)
s <- out[[1]]
updated <- out[[2]]
do.plot(s, n)
for (i in 1:4) {
do.plot(s, n)

animate <- FALSE # do we want to save animation?
side <- 15 # simulation grid will be side x side
n <- 5 # initial number of agent types

if (animate) {
ani.options(interval = 0.2)
saveGIF(go(side, n))
} else {
go(side, n)

We can select if we want to see the animation on the screen or save it to file via  animate option.
The animation the code generates is presented below:

To leave a comment for the author, please follow the link and comment on their blog: R snippets. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

plotly webpage

dominolab webpage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)