Subject: Package descriptive, working with frequency lists
From: Jaime Villate
Date: Tue, 11 Dec 2012 12:30:50 +0000
On Tue, Dec 11, 2012 at 12:36 PM, Andreas Goebel <a-goebel at gmx.de
<mailto:a-goebel at gmx.de>> wrote:
>
> Hi,
>
> this is my first post here, so hello from me. I?m a math teacher at a
> grammar school in germany.
>
> My students have to work with frequency lists, like this:
>
> Size 178 179 180 181 182
> count 2 4 5 5 3
>
> While the package "descriptive" provides functions for computing mean,
> standard deviation and so on for lists, it does not do so for
> frequency
> lists. So my students would have to enter the list like this:
> 178 178 179 179 179 179 ....
>
> which is not really an option.
>
> To solve this, I have tried a bit with the descriptive package and
> have
> introduced a function frequ_mean like this:
>
> /* UNIVARIATE DESCRIPTIVE STATISTICS */
>
> /* Arithmetic mean for frequency list */
> frequ_mean(x) := block([i,mean, total],
> mean:0, total:0,
> for i:1 thru length(x) do( block(
> mean:mean+x[i][1]*x[i][2],
> total:total+x[i][2]
> ) ),
> mean/total
> )$
> /* Arithmetic mean */
>
Hi,
Welcome. Unlike Fotis, I think you made the right decision by putting
values and frequencies in a list
[[178,2],[179,4],...]
Your main concern should not be making the programming task easier for
the teacher but making the program better for the students. Entering
values and frequencies in separate lists they would often forget to
write one frequency or write them in the wrong order without noticing.
I have several improvements to propose:
1- give initial values to the local variables in the block when they are
defined.
2- variables used in for loops, such as i in your code, are
automatically local; there is no need to declare them at the beginning
of the block.
3- use the form "for element in list" rather than "for index:1 thru ..."
4- try to avoid misleading variable names: "mean" is not really the mean
but the accumulated sum.
5- there is no need to use another block inside the for loop (unless you
wanted to define extra variables local for just the for loop).
With those improvements, your code would look like this:
frequ_mean(x) := block([sum:0, total:0],
for p in x do
(sum: sum+p[1]*p[2],
total: total+p[2]),
sum/total)$
> So my questions are:
> - does this make sense?
>
sure.
>
> - should there be some checks to avoid errors, for instance if the
> list
> has the wrong form?
>
It depends. If you want your students to see the code and understand how
it is done, I'd suggest to leave it like that. If you want them to load
it and use it as a black box, then add as many checks as you can.
> - who maintains the descriptive package?
>
Mario Rodriguez. He should be reading your message and replying soon.
> Whould he / she like to
> implement functions for frequency lists himself, or should I do
> that and
> submit them (and if so, how?)?
>
let's wait to hear from him. He's usually very active so I guess he will
go ahead and improve his package very quickly.
> - I would also need to draw histograms for data like that, but have no
> idea at the moment how to achieve this
>
Here is a suggestion:
histogram(data):=
block([xmin:first(data)[1]-1,xmax:last(data)[1]+1,ymax:0,h:[]],
for p in data do
(h:cons(['discrete,[p[1]-1/2,p[1]-1/2,p[1]+1/2,p[1]+1/2],[0,p[2],p[2],0]],h),
if p[2] > ymax then ymax: p[2]),
plot2d (h,['x,xmin,xmax],['y,0,ymax+1],[xlabel,"value"],[ylabel,"count"],
[legend,false],[color,blue]))$
histogram([[178,2],[179,4],[180,5],[181,5],[182,3]]);
I'm assuming that your data are always equally spaced by units, and
[[178,2],[186,3]]
would mean that there are no elements with values 179 through 185.
More general histograms would need some changes in that code (and I bet
with you that Mario will show a nicer solution using his draw package :) )
Best regards,
Jaime