

NetLogo displays of random walks using step length 1 (left) and Cauchy-distributed step-lengths from R (right)

By using the r:get command, a value is returned which is then used to move the agent forward. 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).

To do this, we edit the go procedure in the Random Walk example so that it looks like this: to go NetLogo’s native random number generators don’t include Cauchy distributions, but we can get them from R. 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. 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). What if we want to bring values back from R? Another command, r:get, will do this. NetLogo displays and R histograms for %-similar-wanted 40% (top) and 75% (bottom)
Netlogo lput code#
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. 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). Once this is done, we can convert that list to a vector called dist in R by using the r:put command. 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. Making use of NetLogo’s lists works well because it is transferable to R’s vector class. The r:put command assigns a value (in this case, a list) to an R variable. The r:eval command will send a value to R without expecting any value to return to NetLogo. There are two basic commands used here that will send things from NetLogo to R. R:eval "hist(dist, xlab=\"Distance\",main=\"Distance to Nearest Neighbor of Other Color\")" Set d lput (distance (min-one-of (turtles with of myself ]) )) d This can be done with the following code: to get-distance-to-other 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. r:clearģ) Now we need something for R to do. If you haven’t used any extensions for NetLogo, it’s worth having a look at the NetLogo user manual.Ģ) Add some code to the setup procedure which will clear any existing data in R and send any plot commands to a separate window. 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). This will connect the model to the R extension. What we want to do in this example is collect some information about the population and send it to R for plotting.ġ) Add the code this to the top of the code page. 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. With the R-extension properly installed, let’s try and add it to some existing models in the NetLogo library.įirst we’ll use NetLogo’s Segregation Model, which is based on Thomas Schelling’s famous model of segregation. If you want to be using R to plot from NetLogo, you’ll need to have the JavaGD package installed as well. 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). Step-by-step instructions are included in the download. Installing the extension is not as simple as dropping the files into the extension folder, but it’s worth the trouble.
Netlogo lput install#
I heartily recommend this online course (if you can’t wait for the course to start, you can watch the videos on YouTube here).įirst things first, you’ll need to have NetLogo installed, and download and install the extension. If you don’t have familiarity with R but wish you did, check out the resources on our tutorials page. Thiele and Volker Grimm, the extension allows you to connect NetLogo directly to the beloved stats package, R. 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.
