Have you been working on NetLogo models, but wish you could be making prettier plots? Wish you could be doing data analysis on the fly? Have mathematical calculations that are tripping up your code? Stef’s post the other day on the R turtle graphics reminded me about a tool I use all the time: the R-extension for NetLogo. Put together by Jan C. Thiele and Volker Grimm, the extension allows you to connect NetLogo directly to the beloved stats package, R . If you don’t have familiarity with R but wish you did, check out the resources on our tutorials page. I heartily recommend this online course (if you can’t wait for the course to start, you can watch the videos on YouTube here).
First things first, you’ll need to have NetLogo installed, and download and install the extension. Installing the extension is not as simple as dropping the files into the extension folder, but it’s worth the trouble. Step-by-step instructions are included in the download. Basically, the things to watch out for are making sure that your environmental variables are set up correctly, and making sure that the Java and R you’re running are the same version (32 vs 64 bit). If you want to be using R to plot from NetLogo, you’ll need to have the JavaGD package installed as well.
With the R-extension properly installed, let’s try and add it to some existing models in the NetLogo library.
Example 1: Segregation Model
First we’ll use NetLogo’s Segregation Model, which is based on Thomas Schelling’s famous model of segregation. Agents in this model determine how happy they are based on a preference for neighbors of a similar type, and will try to move elsewhere if they are unhappy. What we want to do in this example is collect some information about the population and send it to R for plotting.
1) Add the code this to the top of the code page.
extensions [ r ]
This will connect the model to the R extension. Mind that this code will not work if the R extension is not installed correctly (most likely, NetLogo will simply close when you try to run it or check it). If you haven’t used any extensions for NetLogo, it’s worth having a look at the NetLogo user manual.
2) Add some code to the setup procedure which will clear any existing data in R and send any plot commands to a separate window.
3) Now we need something for R to do. Let’s say we want to evaluate the average distance from a turtle of one color to a turtle of the other color and plot those values as a histogram. This can be done with the following code:
to get-distance-to-other let d  ask turtles [ set d lput (distance (min-one-of (turtles with [ color != [ color ] of myself ]) [ distance myself ])) d ] r:put "dist" d r:eval "hist(dist, xlab=\"Distance\",main=\"Distance to Nearest Neighbor of Other Color\")" end
There are two basic commands used here that will send things from NetLogo to R. The r:eval command will send a value to R without expecting any value to return to NetLogo. The r:put command assigns a value (in this case, a list) to an R variable. Making use of NetLogo’s lists works well because it is transferable to R’s vector class. Here, we’ve created an empty list, d, and asked all the agents to calculate the distance to their nearest neighbor of a different color, and then add it to the end of the list using the lput command. Once this is done, we can convert that list to a vector called dist in R by using the r:put command. Then we just ask R to make a histogram from the vector values using r:eval, and add in our own nifty labels (Note: be sure to use \” when using quotations within an R command sent from NetLogo). Now you can add this command to a button, enter it into the command line, or embed it elsewhere in your code whenever you want this histogram to appear. Voilà.
NetLogo displays and R histograms for %-similar-wanted 40% (top) and 75% (bottom)
Example 2: Random Walks
What if we want to bring values back from R? Another command, r:get, will do this. We’ll use the Random Walk example in the NetLogo library, which is just a model of an agent picking a random direction and taking a step (for an archaeological example of this, see Brantingham’s 2003 paper on raw material procurement). Lets say that, rather than have the step length be the default value of 1, we want to pick step lengths from a Cauchy distribution. NetLogo’s native random number generators don’t include Cauchy distributions, but we can get them from R.
To do this, we edit the go procedure in the Random Walk example so that it looks like this:
to go ask turtles [ rt random 360 forward r:get "rcauchy(1)" ] tick end
Here, we’ve simply replaced the step length of 1 with a random number R has drawn from a Cauchy distribution using the default parameters (x0 = 0, ϒ = 1). By using the r:get command, a value is returned which is then used to move the agent forward.
NetLogo displays of random walks using step length 1 (left) and Cauchy-distributed step-lengths from R (right)
Once you’re comfortable working between NetLogo and R, the sky’s the limit. Any package that works with your current version of R can be used by NetLogo. I’ve found many that are useful for data analysis during model-building. For example, I tend to use spatstat for spatial analysis, allowing me to run cluster analyses on agents and get output immediately. To add a package to your current project, make sure the package is installed on your system and then just add this code to your setup routine:
r:eval “library(package name)”
Hopefully what’s clear is that this is just the beginning: by adding R functionality to NetLogo, you’ve got a powerful set of tools at your disposal for both model-building and data analysis. Good times!
Thiele JC, Grimm V (2010). NetLogo meets R: Linking agent-based models with a toolbox for their analysis. Environmental Modelling and Software 25(8): 972 – 974. [DOI: 10.1016/j.envsoft.2010.02.008].
Featured image: some code used for a grand evil scheme of world domination (or possibly to parse CSV files), written using R-Studio.