l-function.nlogo 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. ;; L function (with Monte-Carlo-Simulation) example of R-extension
  2. ;;
  3. ;;
  4. ;; by Jan C. Thiele
  5. ;; University of Goettingen, Germany
  6. ;; Department Ecoinformatics, Biometrics and Forest Growth
  7. ;; Buesgenweg 4
  8. ;; 37077 Goettingen
  9. ;; Germany
  10. ;;
  11. ;; Contact: jthiele@gwdg.de
  12. ;;
  13. ;; Copyright: 2011, J.C. Thiele
  14. extensions [r]
  15. to random_setup
  16. clear-all
  17. r:clear
  18. random-seed randomseed
  19. ;; load R package spatstat for spatial statistics
  20. r:eval "library(spatstat)"
  21. ;; create 80 turtles at random positions
  22. crt 80
  23. [
  24. set xcor random-xcor
  25. set ycor random-ycor
  26. ]
  27. end
  28. to random_setup2
  29. clear-all
  30. r:clear
  31. random-seed randomseed
  32. ;; load R package spatstat for spatial statistics
  33. r:eval "library(spatstat)"
  34. ask n-of 400 patches
  35. [
  36. sprout random 3
  37. [
  38. right random 360
  39. forward random 10
  40. ]
  41. ]
  42. end
  43. to cluster_setup
  44. clear-all
  45. r:clear
  46. random-seed randomseed
  47. ;; load R package spatstat for spatial statistics
  48. r:eval "library(spatstat)"
  49. ask n-of 10 patches
  50. [
  51. ask neighbors
  52. [
  53. sprout 3
  54. [
  55. right random 360
  56. forward random 2
  57. ]
  58. ]
  59. ]
  60. end
  61. to regular_setup
  62. clear-all
  63. r:clear
  64. random-seed randomseed
  65. ;; load R package spatstat for spatial statistics
  66. r:eval "library(spatstat)"
  67. ask patches with [pxcor mod 3 + pycor mod 2 = 0]
  68. [
  69. sprout 1
  70. ]
  71. ask n-of 30 turtles
  72. [ die ]
  73. end
  74. to go_with_L-caluclation
  75. ;; let the turtles walk around randomly
  76. if (let-them-walk)
  77. [
  78. ask turtles
  79. [
  80. right random 360
  81. forward random 10
  82. ]
  83. ]
  84. ;; send agent variables into an R data-frame
  85. (r:putagentdf "agentset" turtles "who" "xcor" "ycor")
  86. ;; create point pattern with vectors of x- and y-coordinates of turtles and the dimension of the window/world
  87. let revalstring (word "agppp <- ppp(agentset$xcor, agentset$ycor, c(" min-pxcor "," max-pxcor "), c(" min-pycor "," max-pycor ") )")
  88. r:eval revalstring
  89. ;; calculate L function
  90. r:eval "L <- Lest(agppp)"
  91. ;; get results from R
  92. let ripl_l r:get "L$iso"
  93. let r r:get "L$r"
  94. let theo r:get "L$theo"
  95. ;; combine results into a multidimensional list for plotting
  96. let ripl (map [ [?1 ?2 ?3] -> (list ?1 ?2 ?3) ] r ripl_l theo)
  97. ;; plot the results
  98. set-current-plot "L function"
  99. clear-plot
  100. foreach ripl
  101. [ [?1] ->
  102. set-current-plot "L function"
  103. set-current-plot-pen "obs"
  104. plotxy (item 0 ?1) (item 1 ?1)
  105. set-current-plot-pen "theo"
  106. plotxy (item 0 ?1) (item 2 ?1)
  107. ]
  108. end
  109. to go_with_L-confidencebands
  110. ;; let the turtles walk around randomly
  111. if (let-them-walk)
  112. [
  113. ask turtles
  114. [
  115. right random 360
  116. forward random 10
  117. ]
  118. ]
  119. ;; send agent variables into an R data-frame
  120. (r:putagentdf "agentset" turtles "who" "xcor" "ycor")
  121. ;; create point pattern with vectors of x- and y-coordinates of turtles and the dimension of the window/world
  122. let revalstring (word "agppp <- ppp(agentset$xcor, agentset$ycor, c(" min-pxcor "," max-pxcor "), c(" min-pycor "," max-pycor ") )")
  123. r:eval revalstring
  124. ;; calculate L function with confidence bands from 99 simulations
  125. r:eval "Lsim <- envelope(agppp, Lest)"
  126. ;; get results from R
  127. let r r:get "Lsim$r"
  128. let obs r:get "Lsim$obs"
  129. let theo r:get "Lsim$theo"
  130. let hi r:get "Lsim$hi"
  131. let lo r:get "Lsim$lo"
  132. ;; combine results into a multidimensional list for plotting
  133. let ripl (map [ [?1 ?2 ?3 ?4 ?5] -> (list ?1 ?2 ?3 ?4 ?5) ] r obs theo hi lo)
  134. ;; plot the results
  135. clear-plot
  136. foreach ripl
  137. [ [?1] ->
  138. set-current-plot "L function"
  139. set-current-plot-pen "obs"
  140. plotxy (item 0 ?1) (item 1 ?1)
  141. set-current-plot-pen "theo"
  142. plotxy (item 0 ?1) (item 2 ?1)
  143. set-current-plot-pen "high"
  144. plotxy (item 0 ?1) (item 3 ?1)
  145. set-current-plot-pen "low"
  146. plotxy (item 0 ?1) (item 4 ?1)
  147. ]
  148. end
  149. @#$#@#$#@
  150. GRAPHICS-WINDOW
  151. 829
  152. 10
  153. 1318
  154. 500
  155. -1
  156. -1
  157. 14.6
  158. 1
  159. 10
  160. 1
  161. 1
  162. 1
  163. 0
  164. 0
  165. 0
  166. 1
  167. -16
  168. 16
  169. -16
  170. 16
  171. 0
  172. 0
  173. 1
  174. ticks
  175. 30.0
  176. BUTTON
  177. 5
  178. 10
  179. 116
  180. 43
  181. NIL
  182. random_setup
  183. NIL
  184. 1
  185. T
  186. OBSERVER
  187. NIL
  188. S
  189. NIL
  190. NIL
  191. 1
  192. BUTTON
  193. 7
  194. 200
  195. 193
  196. 233
  197. NIL
  198. go_with_L-caluclation
  199. NIL
  200. 1
  201. T
  202. OBSERVER
  203. NIL
  204. G
  205. NIL
  206. NIL
  207. 1
  208. PLOT
  209. 200
  210. 10
  211. 823
  212. 523
  213. L function
  214. r
  215. L(r)
  216. 0.0
  217. 1.0
  218. 0.0
  219. 1.0
  220. true
  221. false
  222. "" ""
  223. PENS
  224. "obs" 1.0 0 -16777216 true "" ""
  225. "theo" 1.0 0 -2674135 true "" ""
  226. "high" 1.0 0 -10899396 true "" ""
  227. "low" 1.0 0 -13345367 true "" ""
  228. BUTTON
  229. 6
  230. 302
  231. 192
  232. 335
  233. NIL
  234. go_with_L-confidencebands
  235. NIL
  236. 1
  237. T
  238. OBSERVER
  239. NIL
  240. NIL
  241. NIL
  242. NIL
  243. 1
  244. BUTTON
  245. 5
  246. 82
  247. 116
  248. 115
  249. NIL
  250. cluster_setup
  251. NIL
  252. 1
  253. T
  254. OBSERVER
  255. NIL
  256. NIL
  257. NIL
  258. NIL
  259. 1
  260. BUTTON
  261. 5
  262. 118
  263. 118
  264. 151
  265. NIL
  266. regular_setup
  267. NIL
  268. 1
  269. T
  270. OBSERVER
  271. NIL
  272. NIL
  273. NIL
  274. NIL
  275. 1
  276. INPUTBOX
  277. 5
  278. 339
  279. 103
  280. 399
  281. randomseed
  282. 1234567.0
  283. 1
  284. 0
  285. Number
  286. BUTTON
  287. 5
  288. 46
  289. 116
  290. 79
  291. NIL
  292. random_setup2
  293. NIL
  294. 1
  295. T
  296. OBSERVER
  297. NIL
  298. NIL
  299. NIL
  300. NIL
  301. 1
  302. SWITCH
  303. 6
  304. 157
  305. 137
  306. 190
  307. let-them-walk
  308. let-them-walk
  309. 1
  310. 1
  311. -1000
  312. TEXTBOX
  313. 8
  314. 259
  315. 194
  316. 303
  317. Warning: Calculation of confidence envelopes takes a long time (99 Monte-Carlo-Simulations)
  318. 11
  319. 0.0
  320. 1
  321. @#$#@#$#@
  322. ## WHAT IS IT?
  323. This section could give a general understanding of what the model is trying to show or explain.
  324. ## HOW IT WORKS
  325. This section could explain what rules the agents use to create the overall behavior of the model.
  326. ## HOW TO USE IT
  327. This section could explain how to use the model, including a description of each of the items in the interface tab.
  328. ## THINGS TO NOTICE
  329. This section could give some ideas of things for the user to notice while running the model.
  330. ## THINGS TO TRY
  331. This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.
  332. ## EXTENDING THE MODEL
  333. This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc.
  334. ## NETLOGO FEATURES
  335. This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features.
  336. ## RELATED MODELS
  337. This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.
  338. ## CREDITS AND REFERENCES
  339. This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references.
  340. @#$#@#$#@
  341. default
  342. true
  343. 0
  344. Polygon -7500403 true true 150 5 40 250 150 205 260 250
  345. airplane
  346. true
  347. 0
  348. Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
  349. arrow
  350. true
  351. 0
  352. Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
  353. box
  354. false
  355. 0
  356. Polygon -7500403 true true 150 285 285 225 285 75 150 135
  357. Polygon -7500403 true true 150 135 15 75 150 15 285 75
  358. Polygon -7500403 true true 15 75 15 225 150 285 150 135
  359. Line -16777216 false 150 285 150 135
  360. Line -16777216 false 150 135 15 75
  361. Line -16777216 false 150 135 285 75
  362. bug
  363. true
  364. 0
  365. Circle -7500403 true true 96 182 108
  366. Circle -7500403 true true 110 127 80
  367. Circle -7500403 true true 110 75 80
  368. Line -7500403 true 150 100 80 30
  369. Line -7500403 true 150 100 220 30
  370. butterfly
  371. true
  372. 0
  373. Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
  374. Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
  375. Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
  376. Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
  377. Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
  378. Circle -16777216 true false 135 90 30
  379. Line -16777216 false 150 105 195 60
  380. Line -16777216 false 150 105 105 60
  381. car
  382. false
  383. 0
  384. Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
  385. Circle -16777216 true false 180 180 90
  386. Circle -16777216 true false 30 180 90
  387. Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
  388. Circle -7500403 true true 47 195 58
  389. Circle -7500403 true true 195 195 58
  390. circle
  391. false
  392. 0
  393. Circle -7500403 true true 0 0 300
  394. circle 2
  395. false
  396. 0
  397. Circle -7500403 true true 0 0 300
  398. Circle -16777216 true false 30 30 240
  399. cow
  400. false
  401. 0
  402. Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
  403. Polygon -7500403 true true 73 210 86 251 62 249 48 208
  404. Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
  405. cylinder
  406. false
  407. 0
  408. Circle -7500403 true true 0 0 300
  409. dot
  410. false
  411. 0
  412. Circle -7500403 true true 90 90 120
  413. face happy
  414. false
  415. 0
  416. Circle -7500403 true true 8 8 285
  417. Circle -16777216 true false 60 75 60
  418. Circle -16777216 true false 180 75 60
  419. Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
  420. face neutral
  421. false
  422. 0
  423. Circle -7500403 true true 8 7 285
  424. Circle -16777216 true false 60 75 60
  425. Circle -16777216 true false 180 75 60
  426. Rectangle -16777216 true false 60 195 240 225
  427. face sad
  428. false
  429. 0
  430. Circle -7500403 true true 8 8 285
  431. Circle -16777216 true false 60 75 60
  432. Circle -16777216 true false 180 75 60
  433. Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
  434. fish
  435. false
  436. 0
  437. Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
  438. Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
  439. Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
  440. Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
  441. Circle -16777216 true false 215 106 30
  442. flag
  443. false
  444. 0
  445. Rectangle -7500403 true true 60 15 75 300
  446. Polygon -7500403 true true 90 150 270 90 90 30
  447. Line -7500403 true 75 135 90 135
  448. Line -7500403 true 75 45 90 45
  449. flower
  450. false
  451. 0
  452. Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
  453. Circle -7500403 true true 85 132 38
  454. Circle -7500403 true true 130 147 38
  455. Circle -7500403 true true 192 85 38
  456. Circle -7500403 true true 85 40 38
  457. Circle -7500403 true true 177 40 38
  458. Circle -7500403 true true 177 132 38
  459. Circle -7500403 true true 70 85 38
  460. Circle -7500403 true true 130 25 38
  461. Circle -7500403 true true 96 51 108
  462. Circle -16777216 true false 113 68 74
  463. Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
  464. Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
  465. house
  466. false
  467. 0
  468. Rectangle -7500403 true true 45 120 255 285
  469. Rectangle -16777216 true false 120 210 180 285
  470. Polygon -7500403 true true 15 120 150 15 285 120
  471. Line -16777216 false 30 120 270 120
  472. leaf
  473. false
  474. 0
  475. Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
  476. Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
  477. line
  478. true
  479. 0
  480. Line -7500403 true 150 0 150 300
  481. line half
  482. true
  483. 0
  484. Line -7500403 true 150 0 150 150
  485. pentagon
  486. false
  487. 0
  488. Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
  489. person
  490. false
  491. 0
  492. Circle -7500403 true true 110 5 80
  493. Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
  494. Rectangle -7500403 true true 127 79 172 94
  495. Polygon -7500403 true true 195 90 240 150 225 180 165 105
  496. Polygon -7500403 true true 105 90 60 150 75 180 135 105
  497. plant
  498. false
  499. 0
  500. Rectangle -7500403 true true 135 90 165 300
  501. Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
  502. Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
  503. Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
  504. Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
  505. Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
  506. Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
  507. Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
  508. sheep
  509. false
  510. 0
  511. Rectangle -7500403 true true 151 225 180 285
  512. Rectangle -7500403 true true 47 225 75 285
  513. Rectangle -7500403 true true 15 75 210 225
  514. Circle -7500403 true true 135 75 150
  515. Circle -16777216 true false 165 76 116
  516. square
  517. false
  518. 0
  519. Rectangle -7500403 true true 30 30 270 270
  520. square 2
  521. false
  522. 0
  523. Rectangle -7500403 true true 30 30 270 270
  524. Rectangle -16777216 true false 60 60 240 240
  525. star
  526. false
  527. 0
  528. Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
  529. target
  530. false
  531. 0
  532. Circle -7500403 true true 0 0 300
  533. Circle -16777216 true false 30 30 240
  534. Circle -7500403 true true 60 60 180
  535. Circle -16777216 true false 90 90 120
  536. Circle -7500403 true true 120 120 60
  537. tree
  538. false
  539. 0
  540. Circle -7500403 true true 118 3 94
  541. Rectangle -6459832 true false 120 195 180 300
  542. Circle -7500403 true true 65 21 108
  543. Circle -7500403 true true 116 41 127
  544. Circle -7500403 true true 45 90 120
  545. Circle -7500403 true true 104 74 152
  546. triangle
  547. false
  548. 0
  549. Polygon -7500403 true true 150 30 15 255 285 255
  550. triangle 2
  551. false
  552. 0
  553. Polygon -7500403 true true 150 30 15 255 285 255
  554. Polygon -16777216 true false 151 99 225 223 75 224
  555. truck
  556. false
  557. 0
  558. Rectangle -7500403 true true 4 45 195 187
  559. Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
  560. Rectangle -1 true false 195 60 195 105
  561. Polygon -16777216 true false 238 112 252 141 219 141 218 112
  562. Circle -16777216 true false 234 174 42
  563. Rectangle -7500403 true true 181 185 214 194
  564. Circle -16777216 true false 144 174 42
  565. Circle -16777216 true false 24 174 42
  566. Circle -7500403 false true 24 174 42
  567. Circle -7500403 false true 144 174 42
  568. Circle -7500403 false true 234 174 42
  569. turtle
  570. true
  571. 0
  572. Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
  573. Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
  574. Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
  575. Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
  576. Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
  577. Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
  578. wheel
  579. false
  580. 0
  581. Circle -7500403 true true 3 3 294
  582. Circle -16777216 true false 30 30 240
  583. Line -7500403 true 150 285 150 15
  584. Line -7500403 true 15 150 285 150
  585. Circle -7500403 true true 120 120 60
  586. Line -7500403 true 216 40 79 269
  587. Line -7500403 true 40 84 269 221
  588. Line -7500403 true 40 216 269 79
  589. Line -7500403 true 84 40 221 269
  590. x
  591. false
  592. 0
  593. Polygon -7500403 true true 270 75 225 30 30 225 75 270
  594. Polygon -7500403 true true 30 75 75 30 270 225 225 270
  595. @#$#@#$#@
  596. NetLogo 6.0-BETA2
  597. @#$#@#$#@
  598. @#$#@#$#@
  599. @#$#@#$#@
  600. @#$#@#$#@
  601. @#$#@#$#@
  602. default
  603. 0.0
  604. -0.2 0 0.0 1.0
  605. 0.0 1 1.0 0.0
  606. 0.2 0 0.0 1.0
  607. link direction
  608. true
  609. 0
  610. Line -7500403 true 150 150 90 180
  611. Line -7500403 true 150 150 210 180
  612. @#$#@#$#@
  613. 0
  614. @#$#@#$#@