Lobz / facilitation

A R/cpp framework for facilitation IBMs
1 stars 2 forks source link

abiogenesis? Or species-identity swap? while using proceed #49

Closed Lobz closed 6 years ago

Lobz commented 6 years ago

Weird bug in two species simulation using proceed after one of the species was extinct. I believe that id of the individuals was swapped in the proceed, or maybe when simulation started with species 1 and 2 extinct, species 3 and 4 became 1 and 2 in he simulation.

id_swap

Lobz commented 6 years ago

I tested it in master with this code:

p<-data.frame(D=c(5,1,2,1),G=c(2,0,2,0),R=c(0,3,0,3),radius=c(0,2,0,2))
m0 <- community(2,c(2,2),p,dispersal=10,init=rep(10,4),interactionsD=c(0,0,0,0,0,-1,0,-1,0,0,0,0,0,-1,0,-1))
m1<-proceed(m0,10)
abundance.matrix(m1)
m2<-proceed(m1,10)
abundance.matrix(m2)
m3<-proceed(m2,10)
abundance.matrix(m3)

the swap occurs with the last proceed.

Lobz commented 6 years ago

Identified the error as a factor level change in simulatin m2.

> levels(m2$init$sp)
[1] "1" "2" "3" "4"
> levels(m2$data$sp)
[1] "3" "4" "1" "2"

The sp levels in init are correct, but in data the levels 3 and 4 are actually numeric 1 and 2 (which are therefore read by Rcpp code as 1 and 2, I imagine). Probably an error in the Rcpp::DataFrame construction.

Lobz commented 6 years ago

I'm getting that we probably shouldn't be using factors at all from the factor doc

In particular,
     ‘as.numeric’ applied to a factor is meaningless, and may happen by
     implicit coercion.  To transform a factor ‘f’ to approximately its
     original numeric values, ‘as.numeric(levels(f))[f]’ is recommended
     and slightly more efficient than ‘as.numeric(as.character(f))’