User Tools

Site Tools


en:data:simul-spatial:script

Script creating spatially structured community data (using library neutral.vp)

# install.packages("packfor", repos="http://R-Forge.R-project.org") # install packfor from r-forge (only for the last version of R)
# install.packages(c('vegan', 'permute'))
install.packages ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/neutral.vp.3.1.0/neutral.vp_1.0-0.zip', repos = NULL, type = 'win.binary') # install compiled neutral.vp library from this website (compiled for R version 2.15)
 
library (packfor)
library (neutral.vp)
library (ade4)
 
topo <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/topography_100x100.txt')  # reads topo data, which are the template for environmental characteristics
 
# NOTE: the simulation may be time demanding!
 
# the first model - dispersal rate is low (u = 0.2)
neut.topo <- neut.simulate(M = 100, K = 500, S = 50, m = 0.005, b = 0.505, d = 0.500, 
                      u = 0.2, sel = 990, time = 500, cycles = 3, 
                      habitat = topo, fitness = rep(1:10, length = 50))
 
# the second model - dispersal rate is high (u = 0.7)
neut.topo.disp <- neut.simulate(M = 100, K = 500, S = 50, m = 0.005, b = 0.505, d = 0.500, 
                      u = 0.7, sel = 990, time = 500, cycles = 3, 
                      habitat = topo, fitness = rep(1:10, length = 50))
 

You may apply PCA on simulated data (after Hellinger transformation) to figure out whether the simulation was successful. Sample scores on the first axis can be projected as a landscape:

par (mfrow = c (2,3))
index.cut <- sim.cut (neut.topo, fact = 1)
image (matrix (rda (decostand (census (neut.topo, snap = 2)[index.cut,], 'hell'))$CA$u[,1], ncol = 100), main = 'Low dispersal, cycle 2')
image (matrix (rda (decostand (census (neut.topo, snap = 3)[index.cut,], 'hell'))$CA$u[,1], ncol = 100), main = 'Low dispersal, cycle 3')
image (matrix (rda (decostand (census (neut.topo, snap = 4)[index.cut,], 'hell'))$CA$u[,1], ncol = 100), main = 'Low dispersal, cycle 4')
image (matrix (rda (decostand (census (neut.topo.disp, snap = 2)[index.cut,], 'hell'))$CA$u[,1], ncol = 100), main = 'High dispersal, cycle 2')
image (matrix (rda (decostand (census (neut.topo.disp, snap = 3)[index.cut,], 'hell'))$CA$u[,1], ncol = 100), main = 'High dispersal, cycle 3')
image (matrix (rda (decostand (census (neut.topo.disp, snap = 4)[index.cut,], 'hell'))$CA$u[,1], ncol = 100), main = 'High dispersal, cycle 4')
 

The following script prepares matrix of species x samples, geographical coordinates and environmental variable:

index.cut.8 <- sim.cut (neut.topo, fact = 8)
low.disp.spe <- census (neut.topo)[index.cut.8,]
high.disp.spe <- census (neut.topo.disp)[index.cut.8,]
 
env <- as.vector (as.matrix (topo))[index.cut.8]
coord <- neut.topo$coo[index.cut.8,c('X', 'Y')]
 
write.table (low.disp.spe, 'low-disp-spe.txt', sep = '\t')
write.table (high.disp.spe, 'high-disp-spe.txt', sep = '\t')
write.table (env, 'env-spatial-model.txt', sep = '\t')
write.table (coord, 'coord-spatial-model.txt', sep = '\t')
 

Example of use - calculation of PCNM

# read data
low.disp.spe <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/low-disp-spe.txt')
high.disp.spe <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/high-disp-spe.txt')
env <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/env-spatial-model.txt')
coord <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/coord-spatial-model.txt')
 
 
#install.packages("PCNM", repos="http://R-Forge.R-project.org")  # works ONLY FOR THE LATEST R VERSION
library (PCNM)
neut.pcnm <- PCNM (dist (coord))
neut.pcnm_n <- neut.pcnm$vectors[, which(neut.pcnm$Moran_I$Positive)]
 
vp.low <- varpart (decostand (low.disp.spe, 'hell'), env, neut.pcnm_n)
vp.high <- varpart (decostand (high.disp.spe, 'hell'), env, neut.pcnm_n)
 
par (mfrow = c(1,2))
plot (vp.low)
title (main = 'Low dispersal')
plot (vp.high)
title (main = 'High dispersal')
 

en/data/simul-spatial/script.txt · Last modified: 2018/04/09 15:23 (external edit)