tag:blogger.com,1999:blog-54080660566196988822024-03-05T09:31:07.977+01:00Rmayccahttp://www.blogger.com/profile/09567299420134652591noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-5408066056619698882.post-50457636548661002982016-11-07T23:14:00.006+01:002016-11-08T12:25:42.815+01:00Read NetLogo BehaviorSpace data in RI recently started to use the NetLogo, a platform for agent-based modelling. As I am a truly R lover, I miss the direct connection between importing the data produced from NetLogo BehaviorSpace to R platform to facilitate data analysis and plotting.<br />
<br />
Even really helpful, I found <i>R Marries NetLogo: Introduction to the RNetLogo Package</i> (Jan C. Thiele, 2014, https://www.jstatsoft.org/article/view/v058i02/v58i02.pdf) little too complicated for the beginners in R and NetLogo alone.<br />
<br />
<b>A complete beginners guide how to read and use NetLogo BehaviorSpace data in R</b><br />
<br />
Firstly, we need to export a data in <b>table output</b> from NetLogo BehaviorSpace.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk_1yoMa2C9QZLE_MfbiNvTK8ZGrlZ65si8JTrRir0luyYAbsne_3R3XAxeZ7IDTYnpg5Uri-4JGj_yqT439jHesHO__mtFQqQx6Zsf5vh0U9F_Oc9s947tnFqfMPOOVNC50ePkmUWUZDg/s1600/netlog_tabel_output.png" imageanchor="1"><img alt="NetLogo BehviorSpace Run options spreadsheet table output" border="0" height="133" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjk_1yoMa2C9QZLE_MfbiNvTK8ZGrlZ65si8JTrRir0luyYAbsne_3R3XAxeZ7IDTYnpg5Uri-4JGj_yqT439jHesHO__mtFQqQx6Zsf5vh0U9F_Oc9s947tnFqfMPOOVNC50ePkmUWUZDg/s320/netlog_tabel_output.png" title="NetLogo BehviorSpace Run options spreadsheet table output" width="320" /></a><br />
<br />
Sure, you can keep the Spreadsheet format as well, but in R, the Table output is more suitable.<br />
<br />
First, have a look of the structure of the BehaviorSpace Table output data.
Data consist of information about the executed NetLogo experiment and applied model (description....), and of the true table: consisted of table header (column names) and reporter values (data values).
The columns represent reporters' values over simulation run.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwHeEtElbybUfqu5AU5VeuHddHRJVP53Mkw9nnT9bikdpbGjrvwmew8qEzasT-Er507RsqOnHMytpKraFTRqjgGCvtz5DRDPDsJBrPsBaol7XPEIU3ghvrNnSwW-iR1Tej0dXoOMY2nNBQ/s1600/netlog_tale_structure.png" imageanchor="1"><img alt="NetLogo BehaviorSpace final table ouput structure" border="0" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwHeEtElbybUfqu5AU5VeuHddHRJVP53Mkw9nnT9bikdpbGjrvwmew8qEzasT-Er507RsqOnHMytpKraFTRqjgGCvtz5DRDPDsJBrPsBaol7XPEIU3ghvrNnSwW-iR1Tej0dXoOMY2nNBQ/s400/netlog_tale_structure.png" title="NetLogo BehaviorSpace final table ouput structure" width="400" /></a><br />
<br />
Now, we are ready to read NetLogo BehaviorSpace Data in R.<br />
<br />
Firstly, we need to identify the working directory, <span style="font-family: inherit;"> where your NetLogo table is stored</span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;"> # set working directory </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> setwd("c:/Users/Book/Desktop")</span><br />
<br />
Now, we need to load the table (<span style="font-family: "courier new" , "courier" , monospace;">read.table()</span>), skip the "description data" part of table (thus skip first 6 rows), and read table correctly by defining the columns by <span style="font-family: "courier new" , "courier" , monospace;">sep </span>and <span style="font-family: "courier new" , "courier" , monospace;">quote</span>.<br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"> # --------------------------------------------------------</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> # import .csv files by names</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> # skip the first columns but keep names of columns</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"> my.df<-read.table("Fire experiment-table.csv", </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> header = T, # set columns names true</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> sep = ",", # define the separator between columns</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> skip = 6, # skip first 6 rows </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> quote = "\"", # correct the column separator</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> </span><span style="font-family: "courier new" , "courier" , monospace;"> fill = TRUE ) # add blank fields if rows have unequal length</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: inherit;">Check if table has been read correctly</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"> head(my.df)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: inherit;">Plot the data as boxplot</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span><span style="font-family: "courier new" , "courier" , monospace;"> boxplot(burned.trees ~ density, </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> data = my.df, </span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> col = "lightgray",</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> main = "",</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> xlab = "density",</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ylab = "burned trees")</span><br />
<div>
</div>
<div>
</div>
<div>
Tadaaa !!!</div>
<div>
</div>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmjSlEOsOXBnaH1684UqUzAEOnZ3BEcE05dkKaYIzeEs6Mvt9VgozTmjn-iGXax-lSTU4ccA_Vew5lgUb8reAzAgjuHzKLSnsTboI8IvLE1mZEy5vqNOqTraoaFOcgXY0exa1Valp7IPpD/s1600/netlog_plot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmjSlEOsOXBnaH1684UqUzAEOnZ3BEcE05dkKaYIzeEs6Mvt9VgozTmjn-iGXax-lSTU4ccA_Vew5lgUb8reAzAgjuHzKLSnsTboI8IvLE1mZEy5vqNOqTraoaFOcgXY0exa1Valp7IPpD/s320/netlog_plot.png" width="319" /></a></div>
</div>
<br />mayccahttp://www.blogger.com/profile/09567299420134652591noreply@blogger.com1