[R-sig-dyn-mod] simecol: neighbours

Thomas Petzoldt Thomas.Petzoldt at TU-Dresden.de
Thu Sep 13 21:32:02 CEST 2012


Hi Pablo,

I don't understand your code snippet, but the problem seems to be easy 
if you realize that there are 3 types of matrices:

1) the state matrix (your individuals) - x
2) the weight matrix (i.e. kernel), defining what is a neighbor - wdist
3) the output, i.e. neighbor matrix, that counts the (weighted)
    number of neighbors - nb


On 9/13/2012 7:40 PM, "Pablo García Díaz" wrote:
> Dear people in the forum,
>
> I'm begining to use simecol for modeling the spatial spread of some
> animals. First of all, I've a dispersal kernel to measure the distances of
> dispersal by animals.
> Then, I've a matrix in the same discrete scale as the dispersal kernels
> (this helps in the routines of calculations). However, I need to calculate
> not only the eight direct neighbours of each cell, but also other indirect
> neighbours and all of them independently (as one probability value applies
> to each one).
>
> For example, I need to know the number of occupied cells that are 2 cells
> away from the cell of interest and the number of occupied cells that are 3
> cells away from the cell of interest.


See example below.

> One can do this work using the neighbour function, but here comes the
> question, should I specify the weighted neighbour matrix for each position
> in the original matrix? This is, should I build a neigbour matrix for each
> of the positions in the original one? The code for this idea will be:

No, you just need to specify the weight matrix "in general" that is then 
automatically iterated over the grid.

Thomas


##---------------- Example for indirect neighbors ------------------
library(simecol)

## matrix with approx. 5% randomly initialized cells
set.seed(1243)
x <- matrix(runif(11^2) > 0.95, nrow=11, ncol=11)

## a weight matrix counting all cells that are 2 positions away from
## the center
wdist <- matrix(c(1, 1, 1, 1, 1,
                   1, 0, 0, 0, 1,
                   1, 0, 0, 0, 1,
                   1, 0, 0, 0, 1,
                   1, 1, 1, 1, 1), nrow=5)

## number neigbors with distance 2 for all cells
nb <- neighbors(x, 1, wdist)
View(nb)

par(mfrow=c(1,2))
image(x, main="individuals", col=c("wheat", "red"))
image(nb, main="neighbors", col=c("wheat", heat.colors(3)))

x[3, 6]  # cell is "on"
nb[3, 6] # and has 2 neighbors in distance 2

## a distance matrix with distance 3
wdist3 <- matrix(0, nrow=7, ncol=7)
wdist3[c(1, 7), ] <- wdist3[, c(1,7)] <- 1
View(wdist3)

## ... and so on



More information about the R-sig-dynamic-models mailing list