Saturday, March 27, 2010

Conference on Cultural Evolution in Spatially Structured Populations

Wow. This year is really amazing for me here at UCL.
We had the Institute of Archaeology Monday Seminars titled "Contemporary Roles for Spatial Analysis in Archaeology" , which was a huge success with many interesting papers from a very wide perspective (from ABM to Time GIS, passing through 3D models).
We organised the UK Chapter of Computer and Quantitative Applications in Archaeology, again here at IoA in UCL. The conference was really in an enjoyable atmosphere with a wide range of papers from Network Analysis to Multinomial Logistic Regression and a session entitled "Modeling, GIS and Spatial Analysis".
Now, the AHRC funded Centre for the Evolution of Cultural Diversity has just made its call for papers for a three day conference on Cultural Evolution in Spatially Structured Populations . Notice that my blog is titled Evovling Spaces, and the conference is on Cultural Evolution in Spatially Structured Populations, so you can understand how much I can be excited about this. There are already a lot of confirmed speakers, and I can see already the themes unfolding from that: Maximum Entropy Models (e.g. Tim Evans, Alan Wilson), Spatial Diffusion Models (Anne Kandler, James Steele), Niche Construction Theory (Kevin Laland) , Scaling (Alex Bentley) and Agent Based Models (Luke Premo, Tim Kohler)... just to mention some of them...
I can't wait for September!!!!



Thursday, March 25, 2010

NetLogo & R Extension

I'm really a heavy user of R and was so much before starting to do any agent based models. So the first thing I was looking in any software package for ABM was some automated link to R (much like spgrass6 for GRASS and R for GIS). 
I thought  Repast Simphony was the way to go, since the website claims about capabilities to work along with R, but then I was disappointed to find out that it was only storing the output in a data.frame class object (and besides it does not work on a  Mac…). Then after switching (at this stage almost completely) to NetLogo, I found this awesome extension, currently in beta stage (and alas, still not working on yet on a Mac…) but as far as I've seen it works perfectly fine. 
The NetLogo-R-Extension developed by Jan C. Thiele and Volker Grimm (one of the authors of the ABM textbook of the previous post) links seamlessly NetLogo to R through the rJava package. This means that you can do much more than exporting your results in a data.frame class object: you can call R while your simulation is running!!!! So for instance you can plot the L-function of your agents' spatial distribution on the fly while the simulation is running (see the picture below). But this is just the tip of the iceberg! Since you can virtually call any command in R while running your model, you can save plots in any folder, link to Databases (I haven't tried yet, but I guess it's possible), and virtually do any analysis you would like to do and store it in a .Rdata file!!!



Example of a NetLogo ABM with continuos plot of the L function (along with the Confidence Envelope)

Wednesday, March 10, 2010

Importing Raster Files in NetLogo without the GIS Extension

I've been working mainly on abstract ABM, and I really don't need all that bells and whistles, and besides I've had some problems when trying to handle the relation between the real coordinate system and NetLogo own system. What I want is to use a GIS (GRASS in my case) to create artificial landscape with some given statistical or geostatistical proprieties that I can import in NetLogo. There is only one problem for this: NetLogo identifies the integer coordinates at its centroid and not on the bottom-left corner. This means that a world with max-pxcor and max-pycor set to 1 have actually four cells, with the coordinate of their centroids at 0,0; 0,1; 1,1; and 1,0. Thus in reality the absolute extent of the world is -0.5 to +1.5, with the actual cell size maintained to 1. Now if you create a map in GRASS with cellsize 1 and extent max x and max y at 1 you will have only 1 cell.   


So here's my solution:
0)Set up a GRASS region with 0,0 origin on the bottom-left.


1)Create a raster model in GRASS and import in R as a SpatialGridDataFrame (via maptools or spgrass6; you can of course also create a model in R directly)


2)Export as text file using the following code (where "data" is the SpatialGridDataFrame):


#Extrapolate the coordinates of the cell centroid

x<-coordinates(data)
#Shift the coordinate by half the cell size
x<-x-(data@grid@cellsize[1]/2)
#convert matrix to data.frame
x<-as.data.frame(x)
#export the data value (data@data) as the third column of the data.frame
x[,3]<-data@data
#write the output into a text file with tab separator, without putting any column name
write.table(x,"data.txt",sep="\t",col.names=FALSE,row.names=FALSE)



If you look at the .txt you'll see something like this:

0    49    120
1    49    110 
...

3)Read the text file in NetLogo. The procedure is adapted from what Railsback and Grimm have written in their forthcoming book on ABM which is simply GREAT and I advise to everybody.


to import-rast [filename]
file-open filename
  while [not file-at-end?]
  [
    let next-X file-read
    let next-Y file-read
    let next-Z file-read
     
    ask patch next-X next-Y [set mapValue next-Z ]
  ]
    
file-close

end

so I use this in my setup procedure putting something like:

import-rast "mapA.txt"

with mapValue being a numerical property of the patch which I am interested in. Be sure also to set the NetLogo world with origins at the bottom-left, and use as maxpxcor and maxpycor the maxX and maxY of your Raster minus the cellsize (e.g. for a 50 x 50 Raster map with cellsize 1, maxpxcor and maxpycor should be 49). Oddly, if your world is to small, there will be no error message but just a portion of the raster model presented in NetLogo.
Again this workflow is fine as long as your model is abstract. You can of course use this for real world raster data, but you need to shift the entire data so that you have your origin on your bottom-left, or you can use the GIS-Extension.