RNETLOGO: an R package for running and exploring individual‐based models implemented in NETLOGO
Correspondence site: http://www.respond2articles.com/MEE/
Summary
1. NetLogo is a free software platform for implementing individual‐based and agent‐based models. However, NetLogo’s support of systematic design, performance and analysis of simulation experiments is limited. The statistics software R includes such support.
2. RNetLogo is an R package that links R and NetLogo: any NetLogo program can be controlled and run from R and model results can be transferred back to R for statistical analyses. RNetLogo includes 16 functions, which are explained and demonstrated in the user manual and tutorial. The design of RNetLogo was inspired by a similar link between Mathematica and NetLogo.
3. RNetLogo is a powerful tool for making individual‐based modelling more efficient and less ad hoc. It links two fast growing user communities and constitutes a new interface for integrating descriptive statistical analyses and individual‐based modelling.
Introduction
Individual‐based models (IBMs) are simulation models that explicitly represent individual organisms and how they interact with each other and their environment (Grimm & Railsback 2005; Thiele, Kurth & Grimm 2011). IBMs are an established and widely used tool in ecology and evolution (DeAngelis & Mooij 2005). A remaining challenge, however, is that analyses of many IBMs are still more or less superficial (Grimm 1999; Grimm & Railsback 2005; Lorscheid, Heine & Meyer 2011). Much more could be learned from IBMs if they would be embedded in a rigorous framework for designing simulation experiments (Oh et al. 2009), storing simulation results in a systematic way and using statistical toolboxes for analysing these results.
RNetLogo is designed for this purpose. It is a package for the free statistics software R (R Development Core Team 2011) which allows running and analysing IBMs that are implemented in NetLogo (Wilensky 1999), a free software platform for implementing individual‐based or agent‐based models. Both R and NetLogo are increasingly used in their fields, slowly but surely turning into standard software platforms which are also the basis for training the next generation of researchers (see, e.g., Bolker 2008; Railsback & Grimm 2011).
Linking NetLogo with R is therefore desirable. One such link already exists: the R extension of NetLogo (Thiele & Grimm 2010). It allows calling any R command from a NetLogo program. It is mainly designed for using R commands to support the implementation of IBMs, or their sub models. For example, if a population model of a territorial animal requires, while the model is running, to calculate home range sizes based on the animals’ movement, existing R packages for calculating home range sizes can be used (e.g., Calenge 2006). Or, if random numbers are needed from probability distributions which are not provided by NetLogo, they can easily be imported from R. There are, however, good reasons for linking R and NetLogo also the other way round, i.e. to call NetLogo programs and commands from R: the R user community is much larger than the NetLogo user community so it makes sense to provide a tool that starts with R; the R extension can, for technical reasons, be cumbersome to install whereas RNetLogo is as easy to load as any other R package; RNetLogo can be used to create self‐documented simulation experiments and reports using Sweave (Leisch 2002), SWord (Baier 2009) or odfWeave (Kuhn 2010); RNetLogo opens a way to integrate NetLogo simulation into spreadsheets using RExel (Heidberger & Neuwirth 2009), ROOo (Drexel 2011) or R4Calc (Gryc 2008).
RNetLogo is not designed for using R within NetLogo programs but for running and exploring simulation experiments of a given NetLogo program. R is already widely used to analyse file output of simulation models, including those implemented in NetLogo. However, a seamless integration of both tools would facilitate the combined use of R and NetLogo. Such a seamless integration was already the reason for linking Mathematica (Wolfram Research 2010) and NetLogo (Bakshy & Wilensky 2007), which was designed to make use of the Mathematica tools for ‘advanced import capabilities, statistical functions, data visualization and document creation. With the NetLogo‐Mathematica link, you can run all of these tools side‐by‐side with NetLogo’ (Bakshy & Wilensky 2007), RNetLogo’s scope is virtually the same: all these tools support systematic and comprehensive analyses of model behaviour.
NetLogo itself includes a flexible tool for performing experiments on models, BehaviorSpace (Shargel & Wilensky 2002), which is routinely used by NetLogo users. However, links to Mathematica or R provide direct access to a wide array of additional ready‐to‐use powerful tools which go beyond BehaviorSpace’s scope.
For an overview of RNetLogo, its main functions are listed in Table 1. Basic usage examples of the different functions are shown in the examples section of the manual pages and are included in the examples/code_samples folder of the package. In the following we briefly describe typical example applications. These and further examples are included in the tutorial and the examples/applications folder of the package.
| RNetLogo function | The function’s scope |
|---|---|
| NLStart | Initializes the connection to NetLogo, creates an object storing the instance |
| NLLoadModel | Loads a NetLogo model into an NetLogo instance |
| NLCommand | Executes the submitted string in NetLogo |
| NLDoCommand, NLDoCommandWhile | Repeated execution of the submitted string. In the first case with a fixed number of repetitions, in the second case while a condition remains true within NetLogo. |
| NLReport | Send the result of a NetLogo reporter back to R. Results can be String, Number, Boolean or NetLogo lists which are transformed to appropriate R data types. |
| NLDoReport, NLDoReportWhile | Repeated execution of a NetLogo command combined with a reporter. The result of the report is sent back to R after every execution of the NetLogo command. In the first case with a fixed number of repetitions, in the second case while a condition remains true within NetLogo. |
| NLGetAgentSet | An easy‐to‐use way to access variables of an agent or an agentset. An agent is a NetLogo turtle, breed, patch or link. An agentset is a collection of agents. Results can be transformed to R lists or R data.frames. |
Examples
Exploring models
Simulation experiments can be defined, run and evaluated using RNetLogo for any existing NetLogo program (Fig. 1). For this, first model and simulation parameters are set and then the central ‘go’ procedure, which contains the schedule of an IBM, is run for a given number of time steps or repetitions. Simulation output can be conveniently stored in R data.frames, lists or matrices and then processed for visualization and statistical analyses. Running simulation experiments via R has the advantage that modellers more easily and directly adopt the perspective of experimentalists, which will facilitate more thorough model analyses than with homespun designs programmed in NetLogo. R contains a large number of powerful packages that can be used for analysing simulation models (see for example the R function and package listings of Groemping (2011) for Design of Experiments, Simpson (2011) for the analysis of ecological and environmental data, Montana (2011) for statistical genetics, Bivand (2011) for the analysis of spatial data, Allignol & Latouche (2011) for the analysis of survival data or Hyndman & Zeileis (2011) for the analysis of time series data).

R Console (on the left) with loaded RNetLogo package and a NetLogo (on the upper right) instance started in interactive mode with Graphical User Interface. The Fire model (Wilensky 1997) was loaded from within R and the model output (percentage of burned forest patches) has been evaluated over different initial forest density values from 1% to 100% (R plot window A). The critical range between 30% and 60% of forest density is then evaluated with repeated simulations to take into account stochasticity in the model, with a step width of 5% and 10 replications (R plot window B) and with a step width of 1% and 20 replications (R plot window C). The full code for this analysis is shown in red in the R Console (29 lines of code).
Linking to analytical models
For many ecological and evolutionary questions, it can be helpful to compare output from simulation models to analytical approximations. By using R packages for solving analytical models, for example Ryacas (Goedman et al. 2010), this can be performed directly via RNetLogo.
Visualization
R comes with all kinds of graphics packages which facilitate visual analyses of model output. In particular, it can be used to overcome the limitation of NetLogo that only one instance of the model world can be displayed. If a model’s grid cells and individuals have several state variables, which usually is the case, it is helpful to visualize them simultaneously in separate panels, plus any summary statistics of interest. If in addition such visualizations for a given time are stored for entire simulations, modellers can ‘slide’ forth and back through the output of a simulation run, thereby developing a better understanding of spatio‐temporal dynamics and how they are related to aggregated output variables. The RNetLogo tutorial includes such an example.
Databases
For complex models, exploratory simulations can create so much output data that it can be difficult to store them in a logical, consistent and easily accessible way. Especially the small, file‐based SQLLite (Hipp 2011) database enables the modeller to save model results together with model metadata (like inputs such as parameter sets) in a single file. R comes with all kinds of database management packages, which can be used by the individual‐based modeller via RNetLogo. This later allows for the use of database query language (SQL) to extract the right data needed for further analyses.
Conclusions
RNetLogo links two ‘worlds’ and related software platforms. R is a free and open source software and has turned into a standard tool, which is clearly indicated by the growing number of R textbooks (Crawley 2007; Bolker 2008; Dalgaard 2008). In RNetLogo, R represents the world of statistics, including design of experiments, visualizations and all kinds of statistical inference. R can also be used for implementing simple simulations (e.g., Petzoldt & Rinke 2007) but does not provide specific support for making model development and simulation efficient. NetLogo was originally developed as a teaching tool, but is increasingly used for research. In RNetLogo, it represents the world of individual‐based and agent‐based modelling, which has considerably matured over the last 10 years but still has not yet established a culture of systematic design and analysis of simulation experiments.
RNetLogo is easy to install and use and thereby opens R and NetLogo users’ access to each others’ world and software platform. This, we hope, will lead to more rigorous model analyses and, thereby, to making better use of individual‐ and agent‐based models for answering theoretical and applied questions in ecology and evolution.
The RNetLogo package is available on CRAN (http://cran.r‐project.org/web/packages/RNetLogo) and R‐Forge (http://rnetlogo.r‐forge.r‐project.org). This package includes the aforementioned user manual and tutorial. See the Data S1 here for installation and quick start usage instructions.
Acknowledgements
The authors would like to thank three anonymous reviewers for their useful comments on an earlier version of the manuscript.
References
Citing Literature
Number of times cited according to CrossRef: 42
- Martin Zoričak, Denis Horváth, Vladimír Gazda, Oto Hudec, Spatial evolution of industries modelled by cellular automata, Journal of Business Research, 10.1016/j.jbusres.2019.12.043, (2020).
- Diego Bogado Tomasiello, Mariana Giannotti, Flávia F. Feitosa, ACCESS: An agent-based model to explore job accessibility inequalities, Computers, Environment and Urban Systems, 10.1016/j.compenvurbsys.2020.101462, 81, (101462), (2020).
- Mariano R. Recio, Alexander Singer, Petter Wabakken, Håkan Sand, Agent-based models predict patterns and identify constraints of large carnivore recolonizations, a case study of wolves in Scandinavia, Biological Conservation, 10.1016/j.biocon.2020.108752, 251, (108752), (2020).
- Dr Robin Boyd, Dr Nicola Walker, Dr Kieran Hyder, Dr Robert Thorpe, Dr Shovonlal Roy, Professor Richard Sibly, SEASIM-NEAM: a Spatially-Explicit Agent-based SIMulator of NorthEast Atlantic Mackerel population dynamics, MethodsX, 10.1016/j.mex.2020.101044, (101044), (2020).
- Andrew P. Nield, Ran Nathan, Neal J. Enright, Philip G. Ladd, George L. W. Perry, The spatial complexity of seed movement: Animal‐generated seed dispersal patterns in fragmented landscapes revealed by animal movement models, Journal of Ecology, 10.1111/1365-2745.13287, 108, 2, (687-701), (2019).
- Marc Jaxa-Rozen, Jan Kwakkel, Martin Bloemendal, A coupled simulation architecture for agent-based/geohydrological modelling with NetLogo and MODFLOW, Environmental Modelling & Software, 10.1016/j.envsoft.2019.01.020, (2019).
- Marco Pangallo, Jean-Pierre Nadal, Annick Vignes, Residential income segregation: A behavioral model of the housing market, Journal of Economic Behavior & Organization, 10.1016/j.jebo.2019.01.010, 159, (15-35), (2019).
- Sarah Bauduin, Eliot J. B. McIntire, Alex M. Chubaty, NetLogoR: a package to build and run spatially explicit agent‐based models in R, Ecography, 10.1111/ecog.04516, 42, 11, (1841-1849), (2019).
- Jan Salecker, Marco Sciaini, Katrin M. Meyer, Kerstin Wiegand, The nlrx r package: A next‐generation framework for reproducible NetLogo model analyses, Methods in Ecology and Evolution, 10.1111/2041-210X.13286, 10, 11, (1854-1863), (2019).
- Neil H. Carter, Simon A. Levin, Volker Grimm, Effects of human‐induced prey depletion on large carnivores in protected areas: Lessons from modeling tiger populations in stylized spatial scenarios, Ecology and Evolution, 10.1002/ece3.5632, 9, 19, (11298-11313), (2019).
- Lorenzo Quaglietta, Miguel Porto, Adam T. Ford, Simulating animal movements to predict wildlife-vehicle collisions: illustrating an application of the novel R package SiMRiv, European Journal of Wildlife Research, 10.1007/s10344-019-1333-z, 65, 6, (2019).
- Etienne Delay, Cyril Piou, Mutual aid: When does resource scarcity favour group cooperation?, Ecological Complexity, 10.1016/j.ecocom.2019.100790, 40, (100790), (2019).
- Xi-Yin Zhou, Spatial explicit management for the water sustainability of coupled human and natural systems, Environmental Pollution, 10.1016/j.envpol.2019.05.020, (2019).
- Pablo Araujo-Granda, Anna Gras, Marta Ginovart, Vincent Moulton, INDISIM-Denitrification, an individual-based model for study the denitrification process, Journal of Industrial Microbiology & Biotechnology, 10.1007/s10295-019-02245-8, (2019).
- Carlos M. Lemos, Ross J. Gore, Laurence Lessard-Phillips, F. LeRon Shults, A network agent-based model of ethnocentrism and intergroup cooperation, Quality & Quantity, 10.1007/s11135-019-00856-y, (2019).
- Carlos M. Lemos, Carlos M. Lemos, ABM of Civil Violence: ODD Description, Agent-Based Modeling of Social Conflict, 10.1007/978-3-319-67050-8_5, (51-63), (2018).
- Craig E. Simpkins, Todd E. Dennis, Thomas R. Etherington, George L.W. Perry, Assessing the performance of common landscape connectivity metrics using a virtual ecologist approach, Ecological Modelling, 10.1016/j.ecolmodel.2017.11.001, 367, (13-23), (2018).
- Victoria L. Boult, Tristan Quaife, Vicki Fishlock, Cynthia J. Moss, Phyllis C. Lee, Richard M. Sibly, Individual-based modelling of elephant population dynamics using remote sensing to estimate food availability, Ecological Modelling, 10.1016/j.ecolmodel.2018.09.010, 387, (187-195), (2018).
- Robert Fletcher, Marie-Josée Fortin, Robert Fletcher, Marie-Josée Fortin, Population Dynamics in Space, Spatial Ecology and Conservation Modeling, 10.1007/978-3-030-01989-1, (369-417), (2018).
- Robert Fletcher, Marie-Josée Fortin, Robert Fletcher, Marie-Josée Fortin, What Have We Learned? Looking Back and Pressing Forward, Spatial Ecology and Conservation Modeling, 10.1007/978-3-030-01989-1, (475-487), (2018).
- Alireza Mostafizi, Haizhong Wang, Dan Cox, Shangjia Dong, An agent-based vertical evacuation model for a near-field tsunami: Choice behavior, logical shelter locations, and life safety, International Journal of Disaster Risk Reduction, 10.1016/j.ijdrr.2018.12.018, (2018).
- Christian E. Vincenot, How new concepts become universal scientific approaches: insights from citation network analysis of agent-based complex systems science, Proceedings of the Royal Society B: Biological Sciences, 10.1098/rspb.2017.2360, 285, 1874, (20172360), (2018).
- Rongxu Qiu, Wei Xu, John Zhang, Karl Staenz, Modelling and Simulating Urban Residential Land Development in Jiading New City, Shanghai, Applied Spatial Analysis and Policy, 10.1007/s12061-017-9244-4, 11, 4, (753-777), (2017).
- Rongxu Qiu, Wei Xu, John Zhang, Karl Staenz, Modeling and simulating industrial land-use evolution in Shanghai, China, Journal of Geographical Systems, 10.1007/s10109-017-0258-x, 20, 1, (57-83), (2017).
- Matthew Malishev, C. Michael Bull, Michael R. Kearney, An individual‐based model of ectotherm movement integrating metabolic and microclimatic constraints, Methods in Ecology and Evolution, 10.1111/2041-210X.12909, 9, 3, (472-489), (2017).
- R.A.C. van der Veen, K.H. Kisjes, I. Nikolic, Exploring policy impacts for servicising in product-based markets: A generic agent-based model, Journal of Cleaner Production, 10.1016/j.jclepro.2017.01.016, 145, (1-13), (2017).
- Daniel Ciganda, Francisco Villavicencio, Feedback Mechanisms in the Postponement of Fertility in Spain, Agent-Based Modelling in Population Studies, 10.1007/978-3-319-32283-4_14, (405-435), (2017).
- Yong Liu, Industrial pollution resulting in mass incidents: Urban residents' behavior and conflict mitigation, Journal of Cleaner Production, 10.1016/j.jclepro.2017.08.125, 166, (1253-1264), (2017).
- Marco Pangallo, Jean-Pierre Nadal, annick vignes, Residential Income Segregation: A Behavioral Model of the Housing Market, SSRN Electronic Journal, 10.2139/ssrn.3084090, (2017).
- Alireza Mostafizi, Haizhong Wang, Dan Cox, Lori A. Cramer, Shangjia Dong, Agent-based tsunami evacuation modeling of unplanned network disruptions for evidence-driven resource allocation and retrofitting strategies, Natural Hazards, 10.1007/s11069-017-2927-y, 88, 3, (1347-1372), (2017).
- Giovanni Dalmasso, Paula Andrea Marin Zapata, Nathan Ryan Brady, Anne Hamacher-Brady, Agent-Based Modeling of Mitochondria Links Sub-Cellular Dynamics to Cellular Homeostasis and Heterogeneity, PLOS ONE, 10.1371/journal.pone.0168198, 12, 1, (e0168198), (2017).
- Elske van der Vaart, Alice S.A. Johnston, Richard M. Sibly, Predicting how many animals will be where: How to build, calibrate and evaluate individual-based models, Ecological Modelling, 10.1016/j.ecolmodel.2015.08.012, 326, (113-123), (2016).
- Shannon C. Roberts, John D. Lee, Using Agent-Based Modeling to Predict the Diffusion of Safe Teenage Driving Behavior Through an Online Social Network, Proceedings of the Human Factors and Ergonomics Society Annual Meeting, 10.1177/1071181312561478, 56, 1, (2271-2275), (2016).
- Monica Patrascu, Andreea Ion, Vlad Constantinescu, undefined, 2015 14th International Conference on ITS Telecommunications (ITST), 10.1109/ITST.2015.7377399, (50-54), (2015).
- Elske van der Vaart, Mark A. Beaumont, Alice S.A. Johnston, Richard M. Sibly, Calibration and evaluation of individual-based models using Approximate Bayesian Computation, Ecological Modelling, 10.1016/j.ecolmodel.2015.05.020, 312, (182-190), (2015).
- L. Sáiz-Bárcena, J. I. Díez-Pérez, M. A. Manzanedo del Campo, R. Del Olmo Martínez, Information Enclosing Knowledge Networks: A Study of Social Relations, Enhancing Synergies in a Collaborative Environment, 10.1007/978-3-319-14078-0_36, (315-321), (2015).
- Sabrina Mazzoni, Rune Halvorsen, Vegar Bakkestuen, MIAT: Modular R-wrappers for flexible implementation of MaxEnt distribution modelling, Ecological Informatics, 10.1016/j.ecoinf.2015.07.001, 30, (215-221), (2015).
- Christoph S Börlin, Verena Lang, Anne Hamacher-Brady, Nathan R Brady, Agent-based modeling of autophagy reveals emergent regulatory behavior of spatio-temporal autophagy dynamics, Cell Communication and Signaling, 10.1186/s12964-014-0056-8, 12, 1, (2014).
- Béatrice M. Frank, Philippe V. Baret, Simulating brown trout demogenetics in a river/nursery brook system: The individual-based model DemGenTrout, Ecological Modelling, 10.1016/j.ecolmodel.2012.09.017, 248, (184-202), (2013).
- Bogumił Kamiński, Przemysław Szufel, Verification of Models in Agent Based Computational Economics — Lessons from Software Engineering, Perspectives in Business Informatics Research, 10.1007/978-3-642-40823-6_15, (185-199), (2013).
- François Rebaudo, Arnaud Rouzic, Stéphane Dupas, Jean‐François Silvain, Myriam Harry, Olivier Dangles, SimAdapt: an individual‐based genetic model for simulating landscape management impacts on populations, Methods in Ecology and Evolution, 10.1111/2041-210X.12041, 4, 6, (595-600), (2013).
- Ernesto Wandeler, Wolfgang Haid, Lothar Thiele, Performance Analysis of Distributed Embedded Systems, Embedded Systems Handbook, Second Edition, 10.1201/9781439807637.ch10, (10-1-10-17), (2009).




