|
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | 2 | <model version="NetLogo 7.0.0" snapToGrid="false"> |
3 | | - <code><![CDATA[;; Diversity example of R-extension |
4 | | -;; |
5 | | -;; |
6 | | -;; by Jan C. Thiele |
7 | | -;; University of Goettingen, Germany |
8 | | -;; Department Ecoinformatics, Biometrics and Forest Growth |
9 | | -;; Buesgenweg 4 |
10 | | -;; 37077 Goettingen |
11 | | -;; Germany |
12 | | -;; |
13 | | -;; Contact: jthiele@gwdg.de |
14 | | -;; |
15 | | -;; Copyright: 2011, J.C. Thiele |
16 | | -
|
17 | | -;; Converted to run with the SimpleR-Extension |
18 | | -
|
19 | | -extensions [sr] |
20 | | -
|
21 | | -;; one patch equals a plot of 1 ha |
22 | | -;; at each plot the number of trees will be counted |
23 | | -;; trees are individuals/turtles |
24 | | -
|
25 | | -breed [beeches beech] |
26 | | -breed [birches birch] |
27 | | -breed [hornbeams hornbeam] |
28 | | -breed [oaks oak] |
29 | | -breed [maples maple] |
30 | | -breed [ashs ash] |
31 | | -breed [poplars poplar] |
32 | | -
|
33 | | -patches-own |
34 | | -[ |
35 | | - beechcount |
36 | | - birchcount |
37 | | - hornbeamcount |
38 | | - oakcount |
39 | | - maplecount |
40 | | - ashcount |
41 | | - poplarcount |
42 | | - speccount |
43 | | - shannon |
44 | | - simpson |
45 | | - pielou |
46 | | -] |
47 | | -
|
48 | | -
|
49 | | -to setup |
50 | | - clear-all |
51 | | -
|
52 | | - ;; reset the R-workspace |
53 | | - sr:setup |
54 | | -
|
55 | | - ;; create the forest |
56 | | - set-default-shape turtles "tree" |
57 | | - create-beeches random 100 |
58 | | - [ |
59 | | - set xcor random-xcor |
60 | | - set ycor random-ycor |
61 | | - ] |
62 | | - create-birches random 100 |
63 | | - [ |
64 | | - set xcor random-xcor |
65 | | - set ycor random-ycor |
66 | | - ] |
67 | | - create-hornbeams random 100 |
68 | | - [ |
69 | | - set xcor random-xcor |
70 | | - set ycor random-ycor |
71 | | - ] |
72 | | - create-oaks random 100 |
73 | | - [ |
74 | | - set xcor random-xcor |
75 | | - set ycor random-ycor |
76 | | - ] |
77 | | - create-maples random 100 |
78 | | - [ |
79 | | - set xcor random-xcor |
80 | | - set ycor random-ycor |
81 | | - ] |
82 | | - create-ashs random 100 |
83 | | - [ |
84 | | - set xcor random-xcor |
85 | | - set ycor random-ycor |
86 | | - ] |
87 | | - create-poplars random 100 |
88 | | - [ |
89 | | - set xcor random-xcor |
90 | | - set ycor random-ycor |
91 | | - ] |
92 | | -
|
93 | | - let insp one-of patches with [count beeches-here > 0] |
94 | | - ;;inspect insp |
95 | | -
|
96 | | - reset-ticks |
97 | | -end |
98 | | -
|
99 | | -
|
100 | | -to calc-diversity |
101 | | - ;; calculate the number of individuals of each species for each patch |
102 | | - ask patches |
103 | | - [ |
104 | | - set beechcount count beeches-here |
105 | | - set birchcount count birches-here |
106 | | - set hornbeamcount count hornbeams-here |
107 | | - set oakcount count oaks-here |
108 | | - set maplecount count maples-here |
109 | | - set ashcount count ashs-here |
110 | | - set poplarcount count poplars-here |
111 | | - ] |
112 | | -
|
113 | | - ;; assign a new data.frame with patch-variables (the patches will be asigned in lines from upper left to lower right) |
114 | | - (sr:set-agent-data-frame "dfpatches" patches "pxcor" "pycor" "beechcount" "birchcount" "hornbeamcount" "oakcount" "maplecount" "ashcount" "poplarcount") |
115 | | -
|
116 | | - ;; create a data.frame without the coordinates |
117 | | - sr:run "df <- dfpatches[,3:7]" |
118 | | -
|
119 | | - ;; load R-library vegan (must be installed!) |
120 | | - sr:run "library(vegan)" |
121 | | -
|
122 | | - ;; calculate diversity |
123 | | - sr:run "spec <- specnumber(df)" |
124 | | - sr:run "shan <- diversity(df, 'shannon')" |
125 | | - ;; Pielou's evenness (J), if spec = 0, value of pielou will be NaN: |
126 | | - sr:run "pielou <- shan/log(spec)" |
127 | | - sr:run "simp <- diversity(df, 'simpson')" |
128 | | -
|
129 | | -
|
130 | | - ;; the following section is a little bit complicated |
131 | | - ;; it's done to save the calculated diversity values in the corresponding patch-variables |
132 | | - ;; to get just the diversity values as a list, call: print sr:runresult "shan" |
133 | | -
|
134 | | -
|
135 | | - ;; create lists with the coordinates |
136 | | - let xlist sr:runresult "dfpatches$pxcor" |
137 | | - let ylist sr:runresult "dfpatches$pycor" |
138 | | - let xylist (map [ [?1 ?2] -> list ?1 ?2 ] xlist ylist) |
139 | | -
|
140 | | - ;; save the diversity value into the corresponding patch-variables |
141 | | - let counter 1 |
142 | | - foreach xylist |
143 | | - [ ?1 -> |
144 | | - ask patch (item 0 ?1) (item 1 ?1) |
145 | | - [ |
146 | | - set speccount (sr:runresult (word "spec[" counter "]")) |
147 | | - set shannon (sr:runresult (word "shan[" counter "]")) |
148 | | - set simpson (sr:runresult (word "simp[" counter "]")) |
149 | | - set pielou (sr:runresult (word "pielou[" counter "]")) |
150 | | - ] |
151 | | - set counter counter + 1 |
152 | | - ] |
153 | | -
|
154 | | - set-histogram-num-bars 20 |
155 | | - set-current-plot "pielou" |
156 | | - histogram filter is-number? [pielou] of patches |
157 | | - set-current-plot "speccount" |
158 | | - histogram filter is-number? [speccount] of patches |
159 | | - set-current-plot "shannon" |
160 | | - histogram filter is-number? [shannon] of patches |
161 | | - set-current-plot "simpson" |
162 | | - histogram filter is-number? [simpson] of patches |
163 | | -
|
164 | | -end |
165 | | -
|
166 | | -
|
167 | | -to display-results |
168 | | - set-current-plot "pielou" |
169 | | - set-histogram-num-bars 25 |
170 | | - set-plot-pen-mode 1 |
171 | | - histogram filter is-number? [pielou] of patches |
172 | | - set-current-plot "speccount" |
173 | | - set-plot-pen-mode 1 |
174 | | - set-plot-x-range min filter is-number? [speccount] of patches max filter is-number? [speccount] of patches |
175 | | - set-histogram-num-bars (max filter is-number? [speccount] of patches) - (min filter is-number? [speccount] of patches) |
176 | | - histogram filter is-number? [speccount] of patches |
177 | | - set-current-plot "shannon" |
178 | | - set-plot-pen-mode 1 |
179 | | - set-histogram-num-bars 25 |
180 | | - histogram filter is-number? [shannon] of patches |
181 | | - set-current-plot "simpson" |
182 | | - set-plot-pen-mode 1 |
183 | | - set-histogram-num-bars 25 |
184 | | - histogram filter is-number? [simpson] of patches |
185 | | -end |
186 | | -
|
187 | | -
|
188 | | -; Public Domain: |
189 | | -; To the extent possible under law, Uri Wilensky has waived all |
190 | | -; copyright and related or neighboring rights to this model.]]></code> |
| 3 | + <code></code> |
191 | 4 | <widgets> |
192 | 5 | <view x="340" wrappingAllowedX="false" y="15" frameRate="30.0" minPycor="0" height="334" showTickCounter="true" patchSize="30.0" fontSize="10" wrappingAllowedY="false" width="334" tickCounterLabel="ticks" maxPycor="10" updateMode="1" maxPxcor="10" minPxcor="0"></view> |
193 | | - <plot x="123" autoPlotX="true" yMax="10.0" autoPlotY="true" yAxis="count" y="218" xMin="0.0" height="150" legend="false" xMax="1.0" yMin="0.0" width="200" xAxis="shannon" display="shannon"> |
194 | | - <setup> set-histogram-num-bars 10</setup> |
195 | | - <update></update> |
196 | | - <pen interval="1.0" mode="0" display="default" color="-16777216" legend="true"> |
197 | | - <setup></setup> |
198 | | - <update> histogram filter is-number? [shannon] of patches</update> |
199 | | - </pen> |
200 | | - </plot> |
201 | | - <plot x="123" autoPlotX="true" yMax="10.0" autoPlotY="true" yAxis="count" y="372" xMin="0.0" height="150" legend="false" xMax="1.0" yMin="0.0" width="200" xAxis="simpson" display="simpson"> |
202 | | - <setup> set-histogram-num-bars 20</setup> |
203 | | - <update></update> |
204 | | - <pen interval="1.0" mode="0" display="default" color="-16777216" legend="true"> |
205 | | - <setup></setup> |
206 | | - <update> histogram filter is-number? [simpson] of patches</update> |
207 | | - </pen> |
208 | | - </plot> |
209 | | - <plot x="123" autoPlotX="true" yMax="1.0" autoPlotY="true" yAxis="count" y="64" xMin="0.0" height="150" legend="false" xMax="1.0" yMin="0.0" width="200" xAxis="pielou" display="pielou"> |
210 | | - <setup>set-histogram-num-bars 30</setup> |
211 | | - <update></update> |
212 | | - <pen interval="1.0" mode="0" display="default" color="-16777216" legend="true"> |
213 | | - <setup></setup> |
214 | | - <update>histogram filter is-number? [pielou] of patches </update> |
215 | | - </pen> |
216 | | - </plot> |
217 | | - <plot x="330" autoPlotX="true" yMax="10.0" autoPlotY="true" yAxis="count" y="372" xMin="0.0" height="150" legend="false" xMax="1.0" yMin="0.0" width="200" xAxis="speccount" display="speccount"> |
218 | | - <setup> set-histogram-num-bars 20</setup> |
219 | | - <update></update> |
220 | | - <pen interval="1.0" mode="0" display="default" color="-16777216" legend="true"> |
221 | | - <setup></setup> |
222 | | - <update> histogram filter is-number? [speccount] of patches</update> |
223 | | - </pen> |
224 | | - </plot> |
225 | 6 | <note x="26" y="19" backgroundDark="0" fontSize="17" width="304" markdown="false" height="34" textColorDark="-1" textColorLight="-2674135" backgroundLight="0">R-Package "vegan" has to be installed</note> |
226 | | - <button x="28" y="64" height="35" disableUntilTicks="false" forever="false" kind="Observer" width="63">setup</button> |
227 | | - <button x="27" y="105" height="35" disableUntilTicks="true" forever="false" kind="Observer" width="64" display="run">calc-diversity |
228 | | -display-results</button> |
229 | 7 | </widgets> |
230 | 8 | <info><![CDATA[## WHAT IS IT? |
231 | 9 |
|
@@ -961,5 +739,5 @@ For more examples of the Simple R Extension, see the other example models in the |
961 | 739 | </indicator> |
962 | 740 | </shape> |
963 | 741 | </linkShapes> |
964 | | - <previewCommands>setup calc-diversity</previewCommands> |
| 742 | + <previewCommands>print "whee"</previewCommands> |
965 | 743 | </model> |
0 commit comments