Help with histograms

658 views
Skip to first unread message

N. Leticia Abrica-Jacinto

unread,
Mar 13, 2019, 12:08:13 PM3/13/19
to netlogo-users

Hi guys!

One question, let's suppose that a histogram of the form "histogram [vector] of turtles" is made, how can I call and operate each input of the histogram, in each step of tick?
that is, I want to calculate the scalar product from the list that gives the "histogram" command

Thank you

--

Tanto el mal razonamiento como el buen razonamiento son posibles; y en este hecho está el fundamento del lado práctico de la lógica.
C. Sanders Peirce




Alan Isaac

unread,
Mar 13, 2019, 12:50:33 PM3/13/19
to netlogo-users

N. Leticia Abrica-Jacinto

unread,
Mar 14, 2019, 1:18:48 PM3/14/19
to Alan Isaac, netlogo-users

Hi. 

I did not understand the previous recommendation, so I will be more detailed about it.

My agents (turtles) have an "opinion" (numerical value), which is updated according to certain equations. Then, to know the distribution of this opinion I calculate the histogram and I do it in this way (with which I have no problem):

 

to my-update-plots

  set-current-plot "Distribución de opinión"

  set-histogram-num-bars ceiling sqrt numero-de-agentes 

  histogram [opinion] of turtles

end 


Now, what I want to do is:

1º Calculate the proportion of agents in each cell (bin) of the histogram.

2º Do some arithmetic calculations with those values and report a number (num). This value (num) would be shown in another graph.


An attempt at this is the following:

to-report grupos

  set-histogram-num-bars ceiling sqrt numero-de-agentes

  let lista histogram [opinion] of turtles

  let proporcion map [i -> i / numero-de-agentes ] lista

  let cuadrados map [ x -> x * x ]  proporcion

  let suma-cuadrados reduce + cuadrados

  let num round (1 / suma-cuadrados)

  report num

end 


The code marks me an error: "LET expected 2 inputs" on the red line. I understand that "histogram" has "two lists" or something like that, and only that of the values is what interests me, but I do not know how to obtain it.

I appreciate your comments in advance.

Thank you   



El mié., 13 de mar. de 2019 a la(s) 11:08, N. Leticia Abrica-Jacinto (leticia...@gmail.com) escribió:
thakns!
I'll review it



--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/2582a83c-1cc3-c856-9204-b3c0dbbc774b%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Dra. Norma Leticia Abrica Jacinto
Tanto el mal razonamiento como el buen razonamiento son posibles; y en este hecho está el fundamento del lado práctico de la lógica.
C. Sanders Peirce




--
Dra. Norma Leticia Abrica Jacinto
Tanto el mal razonamiento como el buen razonamiento son posibles; y en este hecho está el fundamento del lado práctico de la lógica.
C. Sanders Peirce


Dale Frakes

unread,
Mar 15, 2019, 1:23:21 AM3/15/19
to netlog...@googlegroups.com
As a contrast, one of the nice things with a language like R is that you can easily get the counts/frequenices out of the bins from a histogram.  For example:

res <- hist(rnorm(100))
res$counts
[1]  1  2 17 27 34 16  2  1

res$density
[1] 0.01 0.02 0.17 0.27 0.34 0.16 0.02 0.01
(from:  https://stackoverflow.com/questions/7740503/getting-frequency-values-from-histogram-in-r)


Unfortunately, as far as I know, Netlogo doesn't yet offer that ability to directly do a histogram and get the values back out of it.  That said, it's not too difficult to do the calculations yourself.  Here's a bit of code that will do it (though not with extensive testing)... it creates 100 turtles, giving them a random opinion between 0 and 99, then has functions/reporters that will give you the counts and proportions of the opinions of the turtles in each bin (bin-size 10).

globals [
  bin-size   ; set this to 10 in 'setup'
]

turtles-own [
  opinion
]

to setup
  clear-all
  reset-ticks
 
  set bin-size 10
 
  create-turtles 100 [
    set opinion random 100
  ]
 
end

to go
 
  tick
end


to-report turtles-in-bin [ #bin-number ]
 
  let bin-bottom #bin-number * bin-size
  let bin-top (#bin-number + 1) * bin-size
 
  report count turtles with [ opinion >= bin-bottom and opinion < bin-top ]

end

to-report proportion-in-bin [ #bin-number ]
 
  let #bin-turtles turtles-in-bin #bin-number

  report #bin-turtles / count turtles

end
You can move the bin-number slider to pick which bin is shown in the reporters.  It looks something like this:



It's also a bit clunky, but you could use a foreach statement (http://ccl.northwestern.edu/netlogo/docs/dict/foreach.html) to iterate through your bins to get the values for each of them and put them in a list.  You might set up your list to iterate over using n-values like this:

show n-values 10 [ i -> i ]
[0 1 2 3 4 5 6 7 8 9]
http://ccl.northwestern.edu/netlogo/docs/dict/n-values.html

I hope that helps!

Dale

For more options, visit https://groups.google.com/d/optout.

-- 
Dale Frakes
Adjunct Instructor, PhD Candidate
PSU Systems Science
dfr...@pdx.edu - http://web.pdx.edu/~dfrakes/
bin-histogram.nlogo

Dale Frakes

unread,
Mar 15, 2019, 1:34:44 AM3/15/19
to netlog...@googlegroups.com
Just one last add on... here's a reporter that can be added to my code below that will generate a list of histogram bin-counts:

to-report histogram-list
 
  report n-values num-bins [ x -> turtles-in-bin x ]
 
end

Reply all
Reply to author
Forward
0 new messages