Example 2: Home Range Analysis with NetLogo and R


see an example output here


extensions [r]

...

to calc-homerange
  ;; load R package adehabitat
  r:eval "library(adehabitat)"

  ;; create an empty data-frame
  r:eval "turtles <- data.frame()"
  
  ;; merge the Name-, X- and Y-lists of all animals to one data-frame
  ask animals
  [
    (r:putdataframe "turtle" "X" X "Y" Y)
    r:eval (word "turtle <- data.frame(turtle, Name = '" Name "')")
    r:eval "turtles <- rbind(turtles, turtle)"
  ]
   
  ;; split the data-frame into coordinates and factor variable 
  r:eval "xy <- turtles[,c('X','Y')]"
  r:eval "id <- turtles$Name" 

  ;; calculate homerange
  r:eval "homerange <- mcp(xy, id)"

  ...
end 

to mark-homeranges
  ...
  ask animals
  [  
    pen-up

    ;; get the points of the homerange polygon for the current animal
    r:eval (word "temp <- subset(homerange, ID=='"Name"')")
    let tempX r:get "temp$X"
    let tempY r:get "temp$Y"
    let tempXY (map [list ?1 ?2] tempX tempY)
    ...
  ]
end

to plot-area
  ...
  let precstart 20
  let precincre 5

  ;; calculate the size of the homerange depending on homerange level 
  r:eval (word "area <- mcp.area(xy, id, unout='m2', percent = seq(" 
     precstart ",100, by = " precincre "), plotit=FALSE)")
  ...
end