Find the mode of a list
Invoke it this way:
my $mode = mode(1,2,3,3);
Or this way:
my $v1 = vector(1,2,3,3); my $mod = mode($v1);
And then either query the values or print them like so:
print "The mod of $v1: $mod\n"; my $mq = $mod->query; my $m0 = 0+$mod; # this will croak occasionally, see below
The mode of an array is not necessarily a scalar. The mode of this vector is a vector:
my $mod = mode(1,2,3); my $v2 = $mod->query;
print "hrm, there's three elements in this mode: $mod\n" if $mod->is_multimodal;
Create a 20 point \*(L"moving\*(R" mode like so:
use Statistics::Basic qw(:all nofill);
my $sth = $dbh->prepare("select col1 from data where something"); my $len = 20; my $mod = mode()->set_size($len);
$sth->execute or die $dbh->errstr; $sth->bind_columns( my $val ) or die $dbh->errstr;
while( $sth->fetch ) { $mod->insert( $val ); if( defined( my $m = $mod->query ) ) { print "Mode: $m\n"; }
print "Mode: $mod\n" if $mod->query_filled; }
The constructor takes a list of values, a single array ref, or a single Statistics::Basic::Vector as arguments. It returns a Statistics::Basic::Mode object. Note: normally you'd use the mean() constructor, rather than building these by hand using \*(C`new()\*(C'.
Statistics::Basic::Mode objects sometimes return Statistics::Basic::Vector objects instead of numbers. When \*(C`is_multimodal()\*(C' is true, the mode is a vector, not a scalar.
This module also inherits all the overloads and methods from Statistics::Basic::_OneVectorBase.
This object is overloaded. It tries to return an appropriate string for the calculation or the value of the computation in numeric context.
In boolean context, this object is always true (even when empty).
If evaluated as a string, Statistics::Basic::Mode will try to format a number (like any other Statistics::Basic object), but if the object \*(L"is_multimodal()\*(R", it will instead return a Statistics::Basic::Vector for stringification.
$x = mode(1,2,3); $y = mode(1,2,2);
print "$x, $y\n"; # prints: [1, 2, 3], 2
If evaluated as a number, a Statistics::Basic::Mode will raise an error when the object \*(L"is_multimodal()\*(R".
Paul Miller \*(C`<[email protected]>\*(C'
Copyright 2012 Paul Miller \*(-- Licensed under the \s-1LGPL\s0
perl\|(1), Statistics::Basic, Statistics::Basic::_OneVectorBase, Statistics::Basic::Vector