From 360a2f3d08842333922fe921316553e55e8394c3 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Thu, 18 Dec 2003 04:25:59 +0000 Subject: [PATCH] Added a calculation of mode to FREQUENCIES --- src/ChangeLog | 3 +++ src/frequencies.q | 28 +++++++++++++++++++++++----- tests/command/weight.sh | 6 +++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 92e0b769..0beccd0f 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,6 @@ +Thu Dec 18 12:27:03 WAST 2003 John Darrington + * added a calculation of the mode to FREQUENCIES + Wed Dec 17 12:53:01 WAST 2003 John Darrington * moved (un)defs of DEBUGGING to config.h diff --git a/src/frequencies.q b/src/frequencies.q index 0ad5cf9d..477eee07 100644 --- a/src/frequencies.q +++ b/src/frequencies.q @@ -1057,13 +1057,31 @@ static void calc_stats (struct variable * v, double d[frq_n_stats]) { double W = v->p.frq.tab.valid_cases; - double X_bar, M2, M3, M4; + double X_bar, X_mode, M2, M3, M4; struct freq *f; + int most_often; - /* Calculate the mean. */ + /* Calculate the mean and mode */ X_bar = 0.0; + most_often = -1; + X_mode = SYSMIS; for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++) - X_bar += f->v.f * f->c; + { + /* mean */ + X_bar += f->v.f * f->c; + + /* mode */ + if (most_often < f->c ) + { + most_often=f->c; + X_mode= f->v.f; + } + else if ( most_often == f->c ) + { + /* if there are 2 values , then mode is undefined */ + X_mode=SYSMIS; + } + } X_bar /= W; /* Calculate moments about the mean. */ @@ -1083,9 +1101,9 @@ calc_stats (struct variable * v, double d[frq_n_stats]) /* Formulas below are taken from _SPSS Statistical Algorithms_. */ d[frq_min] = v->p.frq.tab.valid[0].v.f; d[frq_max] = v->p.frq.tab.missing[-1].v.f; - d[frq_mode] = 0.0; + d[frq_mode] = X_mode; d[frq_range] = d[frq_max] - d[frq_min]; - d[frq_median] = 0.0; + d[frq_median] = SYSMIS; d[frq_mean] = X_bar; d[frq_sum] = X_bar * W; d[frq_variance] = M2 / (W - 1); diff --git a/tests/command/weight.sh b/tests/command/weight.sh index 35bbaa77..6446c344 100755 --- a/tests/command/weight.sh +++ b/tests/command/weight.sh @@ -1,6 +1,6 @@ #!/bin/sh -# This program tests .... +# This program tests the WEIGHT command TEMPDIR=/tmp/pspp-tst-$$ @@ -147,8 +147,8 @@ diff -B -b $TEMPDIR/pspp.list - <