Aimee Jones
2012-Jan-08 08:14 UTC
[R] Adding a migration element to a deterministic spatial model
Hi all, I've been working with a friend's model that is a spatial model consisting of 4 patches. She uses the code found below to add migration between the patches for the three species of concern. When I run a script incorporating this code, all four patches run independently without migration occuring. When I type m1[1,2] into the workspace to see if it will return a figure, it returns, error, object m1 not found. However, if I type the code below directly into the workspace, it finds and returns m1[1,2] (or any other value). I have tried to find other examples of code that add a migration element to a spatial model but have not had any success. I need help figuring out why m#[i,j] consistently returns a 0 value (or at least appears to) despite the code instructing R to return different values under set circumstances. thanks for any help you are able to give, yours sincerely, Aimee with(as.list(c(y,p)), { dydt <- rep(0,NP*16) # Initialize m1,m2,m3 m1 <- matrix(0,NP,NP) m2 <- matrix(0,NP,NP) m3 <- matrix(0,NP,NP) # Only nearest neighbors have nonzero travel rates between each other for (i in 1:NP) { for (j in 1:NP) { if ((i%%sqrt(NP)) == 0 & i!=j) { if (j == i+sqrt(NP) | j == i-1 | j == i-sqrt(NP)) { m1[j,i] <- 0.1 m1[i,j] <- 0.1 m2[j,i] <- 0.25 m2[i,j] <- 0.25 m3[j,i] <- 0.1 m3[i,j] <- 0.1 } } if (i%%sqrt(NP) == 1) { if (j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP)) { m1[j,i] <- 0.1 m1[i,j] <- 0.1 m2[j,i] <- 0.25 m2[i,j] <- 0.25 m3[j,i] <- 0.1 m3[i,j] <- 0.1 } } if (i%%sqrt(NP)!=0 & i%%sqrt(NP)!=1 & i!=j) { if (j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP)| j == i-1) { m1[j,i] <- 0.1 m1[i,j] <- 0.1 m2[j,i] <- 0.25 m2[i,j] <- 0.25 m3[j,i] <- 0.1 m3[i,j] <- 0.1 } } } } # Initialize the summaries of incoming movements to a patch s <- rep(0,NP*16) for (i in 1:NP) { z <- (i-1)*16 for (k in 3:6) { for (j in 1:NP) { zz <- (j-1)*16 s[z+k] <- s[z+k] + m1[i,j]*y[zz+k] } } for (k in 7:11) { for (j in 1:NP) { zz <- (j-1)*16 s[z+k] <- s[z+k] + m2[i,j]*y[zz+k] } } for (k in 13:16) { for (j in 1:NP) { zz <- (j-1)*16 s[z+k] <- s[z+k] + m3[i,j]*y[zz+k] } }