0

Yes, a similar question has been asked here NetLogo: histogram relative frequency, but as far as I know, no answer has been given. Am I allowed to re-ask it again? I would have added a comment under the question but I am not allowed to.

I would like to plot the relative frequency of patches with a specific colour against increasing distance from a turtle. What I've tried so far:

ask turtle [
  set-current-plot "plot 1"
  set-plot-y-range 0 1
  set-plot-pen-mode 1
  histogram [distance myself] of patches with [pcolor = red]]

This only gives me the absolute frequency. I want to plot the relative frequency of each patch colour against increasing distance from a turtle. So at distance = 1 away from turtle, how many patches out of the total no. of patches are red. I tried adding a

histogram [distance myself ] of patches with [ ] / count patches with [distance myself = 1] ; when x-axis = 1, and so for increasing x 

but there were certain syntax problems since the primitive histogram expects a list. I would think there is a simpler way to set the y-axis to (absolute occurrences/total no. of patches at distance x) so I looked through the Netlogo dictionary but could not find something that sets up the y-axis under plot set-up commands.

Would appreciate any advice with regards to this issue! Thank you for your time.

Community
  • 1
  • 1
lyh198
  • 61
  • 1
  • 6
  • Could you give an example? I'm not sure what you mean by plotting relative frequency of patches...You'll need to be a bit more specific. – mattsap Jun 01 '16 at 14:09
  • To fix your syntax issues: histogram [ (distance myself) / count patches with [distance myself = 1]] of patches with [pcolor = red] You may have a divide by 0 error though, which you may want to handle with an ifelse-value. – mattsap Jun 01 '16 at 14:10
  • Hey mattsap I have edited my qns to improve clarity. Thanks :) – lyh198 Jun 01 '16 at 15:24
  • I think your code and what you are trying to accomplish are two different things. Could you provide an example? It's still unclear to me what you mean. Do you mean you want to know the frequency of #red patches / #patches at distance? – mattsap Jun 02 '16 at 02:38
  • For example, at distance = 1 for netlogo landscape, there are a total of 8 patches (moore neighbourhood). Out of this, 7 are red patches. So 7/8 would be the relative frequency of red patches at distance = 1. At distance = 2, there are a total of 16 patches, out of which 12 are red. so rel. frequency would be 12/16. I would like plot the rel. frequencies of red patches on y-axis, against increasing distance = 1,2,3... on the x-axis. i hope it's clearer now? thanks for your help really appreciate it! – lyh198 Jun 02 '16 at 13:07
  • 1
    The `histogram` command won't do this job. You'll need to plot each bar individually. – Seth Tisue Jun 02 '16 at 15:42
  • Well, I think you just want to use a plot (like a scatter plot) rather than a histogram. A histogram counts how many times something appears in a list. A plot has xy pairs. In your case, it'd be (distance, proportion).I don't think you should use the word frequency since that has another technical meaning. – mattsap Jun 03 '16 at 17:22

1 Answers1

0

It sounds like you want to plot the proportion of red to total patches vs the distance. You wouldn't use a histogram for this...

Instead, you'd plot it.

You'll need to get the distance of the farthest away patch. Than for each distance calculate the proportion of red to total number of patches at that distance.

To make your life simpler, you may want to round the distances since a turtle could be off-center from the patch (i.e. if the turtle has xcor .5 rather than 0 or 1.)

to setup
  clear-all

  crt 1 [ setxy random-xcor random-ycor]
  ask patches [ set pcolor ifelse-value (random 100 < 30) [red][black]]

  ask turtle 0 
  [
    let max-distance round max [distance myself] of patches
    set-current-plot "example-plot"
    set-plot-x-range 0 max-distance
    set-plot-y-range 0 1
    foreach n-values  max-distance [?]
    [
     let percent-red (count patches with [ round (distance myself) = ? and pcolor = red]) / (count patches with [ round (distance myself)  = ?])
     plotxy ? percent-red
    ]

  ]

end
mattsap
  • 3,790
  • 1
  • 15
  • 36