# [R] Spatial Voter Model

Martin Henry H. Stevens HStevens at MUOhio.edu
Fri Apr 16 22:35:15 CEST 2004

```Has anyone coded (in R) a spatial voter model with mutation (e.g.,
Kimura and Weiss 1964, Holley and Liggett 1975, Durrett and Levin
1996)? In principle, it is quite straightforward, but useful
simulations require many many iterations, making my "straightforward"
version too time intensive. I am happy to share my version (without
mutation, below), for what it is worth.

Thank you in advance,
Hank Stevens

# Voter model with no mutation in a square grid of size L^2
L = 50 #Dimension of square matrix (L=10^ or 10^4 would be nice...)
S = 2 # Number of species
loc=c(L,1:L,1) # possible neighbor locations
replacements = L^2 * 10 # iterations (hundreds * L^2  would be nice)
Lat <- matrix(sample(1:S,L^2, replace=TRUE),nrow=L) #Make an arena L X L
Lat0 <- Lat # Save original arena

# select random rows where number of reps = number of cells in arena
# and same for columns
ri <- sample(1:L, replacements, replace=TRUE)
rj <- sample(1:L, replacements, replace=TRUE)

#implement a voter model where each cell takes on the value of a
randomly selected individual around it.

for(i in 1:replacements) Lat[ri[i],rj[i]] <-
sample(c(Lat[loc[ri[i]],loc[rj[i]+1]],

Lat[loc[ri[i]+1],loc[rj[i]+2]],

Lat[loc[ri[i]+2],loc[rj[i]+1]],

Lat[loc[ri[i]+1],loc[rj[i]]]), 1)

# Calculate Simpon's Diversity (Nei genetic heterozygozity)
1 - sum( (table(Lat0)/sum(table(Lat0)))^2)
1 - sum( (table(Lat)/sum(table(Lat)))^2)
layout(matrix(1:2, nrow=1)) # plot arenas
plot(rep(1:L,each=L),rep(1:L,L),col=c(Lat0),pch=20)
plot(rep(1:L,each=L),rep(1:L,L),col=c(Lat),pch=20)

Dr. Martin Henry H. Stevens, Assistant Professor
338 Pearson Hall
Botany Department
Miami University
Oxford, OH 45056

Office: (513) 529-4206
Lab: (513) 529-4262
FAX: (513) 529-4243
http://www.cas.muohio.edu/botany/bot/henry.html
http://www.muohio.edu/ecology/
http://www.muohio.edu/botany/

```