1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * Remember that histograms, bar charts need mean, stddev.
31 #include "bitvector.h"
38 #include "algorithm.h"
45 #include "value-labels.h"
50 #include "debug-print.h"
55 format=cond:condense/onepage(*n:onepage_limit,"%s>=0")/!standard,
56 table:limit(n:limit,"%s>0")/notable/!table,
57 labels:!labels/nolabels,
58 sort:!avalue/dvalue/afreq/dfreq,
59 spaces:!single/double,
60 paging:newpage/!oldpage;
61 missing=miss:include/!exclude;
62 barchart(ba_)=:minimum(d:min),
64 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0");
65 histogram(hi_)=:minimum(d:min),
67 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
68 norm:!nonormal/normal,
69 incr:increment(d:inc,"%s>0");
70 hbar(hb_)=:minimum(d:min),
72 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
73 norm:!nonormal/normal,
74 incr:increment(d:inc,"%s>0");
78 statistics[st_]=1|mean,2|semean,3|median,4|mode,5|stddev,6|variance,
79 7|kurtosis,8|skewness,9|range,10|minimum,11|maximum,12|sum,
80 13|default,14|seskewness,15|sekurtosis,all,none.
85 /* Description of a statistic. */
88 int st_indx; /* Index into a_statistics[]. */
89 const char *s10; /* Identifying string. */
92 /* Table of statistics, indexed by dsc_*. */
93 static struct frq_info st_name[frq_n_stats + 1] =
95 {FRQ_ST_MEAN, N_("Mean")},
96 {FRQ_ST_SEMEAN, N_("S.E. Mean")},
97 {FRQ_ST_MEDIAN, N_("Median")},
98 {FRQ_ST_MODE, N_("Mode")},
99 {FRQ_ST_STDDEV, N_("Std Dev")},
100 {FRQ_ST_VARIANCE, N_("Variance")},
101 {FRQ_ST_KURTOSIS, N_("Kurtosis")},
102 {FRQ_ST_SEKURTOSIS, N_("S.E. Kurt")},
103 {FRQ_ST_SKEWNESS, N_("Skewness")},
104 {FRQ_ST_SESKEWNESS, N_("S.E. Skew")},
105 {FRQ_ST_RANGE, N_("Range")},
106 {FRQ_ST_MINIMUM, N_("Minimum")},
107 {FRQ_ST_MAXIMUM, N_("Maximum")},
108 {FRQ_ST_SUM, N_("Sum")},
112 /* Percentiles to calculate. */
116 double p; /* the %ile to be calculated */
117 double value; /* the %ile's value */
118 double x1; /* The datum value <= the percentile */
119 double x2; /* The datum value >= the percentile */
121 int flag2; /* Set to 1 if this percentile value has been found */
124 static struct percentile *percentiles;
125 static int n_percentiles;
127 static int implicit_50th ;
129 /* Groups of statistics. */
131 #define frq_default \
132 (BI (frq_mean) | BI (frq_stddev) | BI (frq_min) | BI (frq_max))
134 (BI (frq_sum) | BI(frq_min) | BI(frq_max) \
135 | BI(frq_mean) | BI(frq_semean) | BI(frq_stddev) \
136 | BI(frq_variance) | BI(frq_kurt) | BI(frq_sekurt) \
137 | BI(frq_skew) | BI(frq_seskew) | BI(frq_range) \
138 | BI(frq_range) | BI(frq_mode) | BI(frq_median))
140 /* Statistics; number of statistics. */
141 static unsigned long stats;
144 /* Types of graphs. */
147 GFT_NONE, /* Don't draw graphs. */
148 GFT_BAR, /* Draw bar charts. */
149 GFT_HIST, /* Draw histograms. */
150 GFT_HBAR /* Draw bar charts or histograms at our discretion. */
153 /* Parsed command. */
154 static struct cmd_frequencies cmd;
156 /* Summary of the barchart, histogram, and hbar subcommands. */
157 static int chart; /* NONE/BAR/HIST/HBAR. */
158 static double min, max; /* Minimum, maximum on y axis. */
159 static int format; /* FREQ/PERCENT: Scaling of y axis. */
160 static double scale, incr; /* FIXME */
161 static int normal; /* FIXME */
163 /* Variables for which to calculate statistics. */
164 static int n_variables;
165 static struct variable **v_variables;
167 /* Arenas used to store semi-permanent storage. */
168 static struct pool *int_pool; /* Integer mode. */
169 static struct pool *gen_pool; /* General mode. */
171 /* Easier access to a_statistics. */
172 #define stat cmd.a_statistics
174 static void determine_charts (void);
176 static void precalc (void *);
177 static int calc (struct ccase *, void *);
178 static void postcalc (void *);
180 static void postprocess_freq_tab (struct variable *);
181 static void dump_full (struct variable *);
182 static void dump_condensed (struct variable *);
183 static void dump_statistics (struct variable *, int show_varname);
184 static void cleanup_freq_tab (struct variable *);
186 static hsh_hash_func hash_value_numeric, hash_value_alpha;
187 static hsh_compare_func compare_value_numeric_a, compare_value_alpha_a;
188 static hsh_compare_func compare_value_numeric_d, compare_value_alpha_d;
189 static hsh_compare_func compare_freq_numeric_a, compare_freq_alpha_a;
190 static hsh_compare_func compare_freq_numeric_d, compare_freq_alpha_d;
192 /* Parser and outline. */
194 static int internal_cmd_frequencies (void);
197 cmd_frequencies (void)
201 int_pool = pool_create ();
202 result = internal_cmd_frequencies ();
203 pool_destroy (int_pool);
205 pool_destroy (gen_pool);
213 internal_cmd_frequencies (void)
223 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
224 dict_get_var(default_dict, i)->p.frq.used = 0;
226 if (!parse_frequencies (&cmd))
229 if (cmd.onepage_limit == NOT_LONG)
230 cmd.onepage_limit = 50;
232 /* Figure out statistics to calculate. */
234 if (stat[FRQ_ST_DEFAULT] || !cmd.sbc_statistics)
235 stats |= frq_default;
236 if (stat[FRQ_ST_ALL])
238 if (cmd.sort != FRQ_AVALUE && cmd.sort != FRQ_DVALUE)
239 stats &= ~frq_median;
240 for (i = 0; i < frq_n_stats; i++)
241 if (stat[st_name[i].st_indx])
242 stats |= BIT_INDEX (i);
243 if (stats & frq_kurt)
245 if (stats & frq_skew)
248 /* Calculate n_stats. */
250 for (i = 0; i < frq_n_stats; i++)
251 if ((stats & BIT_INDEX (i)))
256 if (chart != GFT_NONE || cmd.sbc_ntiles)
257 cmd.sort = FRQ_AVALUE;
260 procedure_with_splits (precalc, calc, postcalc, NULL);
265 /* Figure out which charts the user requested. */
267 determine_charts (void)
269 int count = (!!cmd.sbc_histogram) + (!!cmd.sbc_barchart) + (!!cmd.sbc_hbar);
279 msg (SW, _("At most one of BARCHART, HISTOGRAM, or HBAR should be "
280 "given. HBAR will be assumed. Argument values will be "
281 "given precedence increasing along the order given."));
283 else if (cmd.sbc_histogram)
285 else if (cmd.sbc_barchart)
296 if (cmd.sbc_barchart)
298 if (cmd.ba_min != SYSMIS)
300 if (cmd.ba_max != SYSMIS)
302 if (cmd.ba_scale == FRQ_FREQ)
307 else if (cmd.ba_scale == FRQ_PERCENT)
309 format = FRQ_PERCENT;
314 if (cmd.sbc_histogram)
316 if (cmd.hi_min != SYSMIS)
318 if (cmd.hi_max != SYSMIS)
320 if (cmd.hi_scale == FRQ_FREQ)
325 else if (cmd.hi_scale == FRQ_PERCENT)
327 format = FRQ_PERCENT;
332 if (cmd.hi_incr == FRQ_INCREMENT)
338 if (cmd.hb_min != SYSMIS)
340 if (cmd.hb_max != SYSMIS)
342 if (cmd.hb_scale == FRQ_FREQ)
347 else if (cmd.hb_scale == FRQ_PERCENT)
349 format = FRQ_PERCENT;
354 if (cmd.hb_incr == FRQ_INCREMENT)
358 if (min != SYSMIS && max != SYSMIS && min >= max)
360 msg (SE, _("MAX must be greater than or equal to MIN, if both are "
361 "specified. However, MIN was specified as %g and MAX as %g. "
362 "MIN and MAX will be ignored."), min, max);
367 /* Add data from case C to the frequency table. */
369 calc (struct ccase *c, void *aux UNUSED)
374 weight = dict_get_case_weight (default_dict, c);
376 for (i = 0; i < n_variables; i++)
378 struct variable *v = v_variables[i];
379 union value *val = &c->data[v->fv];
380 struct freq_tab *ft = &v->p.frq.tab;
382 switch (v->p.frq.tab.mode)
387 struct freq **fpp = (struct freq **) hsh_probe (ft->data, val);
393 struct freq *fp = *fpp = pool_alloc (gen_pool, sizeof *fp);
401 if (val->f == SYSMIS)
402 v->p.frq.tab.sysmis += weight;
403 else if (val->f > INT_MIN+1 && val->f < INT_MAX-1)
406 if (i >= v->p.frq.tab.min && i <= v->p.frq.tab.max)
407 v->p.frq.tab.vector[i - v->p.frq.tab.min] += weight;
410 v->p.frq.tab.out_of_range += weight;
419 /* Prepares each variable that is the target of FREQUENCIES by setting
420 up its hash table. */
422 precalc (void *aux UNUSED)
426 pool_destroy (gen_pool);
427 gen_pool = pool_create ();
429 for (i = 0; i < n_variables; i++)
431 struct variable *v = v_variables[i];
433 if (v->p.frq.tab.mode == FRQM_GENERAL)
436 hsh_compare_func *compare;
438 if (v->type == NUMERIC)
440 hash = hash_value_numeric;
441 compare = compare_value_numeric_a;
445 hash = hash_value_alpha;
446 compare = compare_value_alpha_a;
448 v->p.frq.tab.data = hsh_create (16, compare, hash, NULL, v);
454 for (j = (v->p.frq.tab.max - v->p.frq.tab.min); j >= 0; j--)
455 v->p.frq.tab.vector[j] = 0.0;
456 v->p.frq.tab.out_of_range = 0.0;
457 v->p.frq.tab.sysmis = 0.0;
462 /* Finishes up with the variables after frequencies have been
463 calculated. Displays statistics, percentiles, ... */
465 postcalc (void *aux UNUSED)
469 for (i = 0; i < n_variables; i++)
471 struct variable *v = v_variables[i];
473 int dumped_freq_tab = 1;
475 postprocess_freq_tab (v);
477 /* Frequencies tables. */
478 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
479 if (cmd.table == FRQ_TABLE
480 || (cmd.table == FRQ_LIMIT && n_categories <= cmd.limit))
490 if (n_categories > cmd.onepage_limit)
503 dump_statistics (v, !dumped_freq_tab);
505 cleanup_freq_tab (v);
509 /* Returns the comparison function that should be used for
510 sorting a frequency table by FRQ_SORT using VAR_TYPE
512 static hsh_compare_func *
513 get_freq_comparator (int frq_sort, int var_type)
515 /* Note that q2c generates tags beginning with 1000. */
516 switch (frq_sort | (var_type << 16))
518 case FRQ_AVALUE | (NUMERIC << 16): return compare_value_numeric_a;
519 case FRQ_AVALUE | (ALPHA << 16): return compare_value_alpha_a;
520 case FRQ_DVALUE | (NUMERIC << 16): return compare_value_numeric_d;
521 case FRQ_DVALUE | (ALPHA << 16): return compare_value_alpha_d;
522 case FRQ_AFREQ | (NUMERIC << 16): return compare_freq_numeric_a;
523 case FRQ_AFREQ | (ALPHA << 16): return compare_freq_alpha_a;
524 case FRQ_DFREQ | (NUMERIC << 16): return compare_freq_numeric_d;
525 case FRQ_DFREQ | (ALPHA << 16): return compare_freq_alpha_d;
532 /* Returns nonzero iff the value in struct freq F is non-missing
535 not_missing (const void *f_, void *v_)
537 const struct freq *f = f_;
538 struct variable *v = v_;
540 return !is_missing (&f->v, v);
543 /* Summarizes the frequency table data for variable V. */
545 postprocess_freq_tab (struct variable *v)
547 hsh_compare_func *compare;
551 struct freq *freqs, *f;
554 assert (v->p.frq.tab.mode == FRQM_GENERAL);
555 compare = get_freq_comparator (cmd.sort, v->type);
558 /* Extract data from hash table. */
559 count = hsh_count (ft->data);
560 data = hsh_data (ft->data);
562 /* Copy dereferenced data into freqs. */
563 freqs = xmalloc (count* sizeof *freqs);
564 for (i = 0; i < count; i++)
566 struct freq *f = data[i];
570 /* Put data into ft. */
572 ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, v);
573 ft->missing = freqs + ft->n_valid;
574 ft->n_missing = count - ft->n_valid;
577 sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare, v);
578 sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare, v);
580 /* Summary statistics. */
581 ft->valid_cases = 0.0;
582 for(i = 0 ; i < ft->n_valid ; ++i )
585 ft->valid_cases += f->c;
589 ft->total_cases = ft->valid_cases ;
590 for(i = 0 ; i < ft->n_missing ; ++i )
593 ft->total_cases += f->c;
598 /* Frees the frequency table for variable V. */
600 cleanup_freq_tab (struct variable *v)
602 assert (v->p.frq.tab.mode == FRQM_GENERAL);
603 free (v->p.frq.tab.valid);
604 hsh_destroy (v->p.frq.tab.data);
607 /* Parses the VARIABLES subcommand, adding to
608 {n_variables,v_variables}. */
610 frq_custom_variables (struct cmd_frequencies *cmd UNUSED)
613 int min = 0, max = 0;
615 int old_n_variables = n_variables;
619 if (token != T_ALL && (token != T_ID
620 || dict_lookup_var (default_dict, tokid) == NULL))
623 if (!parse_variables (default_dict, &v_variables, &n_variables,
624 PV_APPEND | PV_NO_SCRATCH))
627 for (i = old_n_variables; i < n_variables; i++)
628 v_variables[i]->p.frq.tab.mode = FRQM_GENERAL;
630 if (!lex_match ('('))
635 if (!lex_force_int ())
637 min = lex_integer ();
639 if (!lex_force_match (','))
641 if (!lex_force_int ())
643 max = lex_integer ();
645 if (!lex_force_match (')'))
649 msg (SE, _("Upper limit of integer mode value range must be "
650 "greater than lower limit."));
655 for (i = old_n_variables; i < n_variables; i++)
657 struct variable *v = v_variables[i];
659 if (v->p.frq.used != 0)
661 msg (SE, _("Variable %s specified multiple times on VARIABLES "
662 "subcommand."), v->name);
666 v->p.frq.used = 1; /* Used simply as a marker. */
668 v->p.frq.tab.valid = v->p.frq.tab.missing = NULL;
670 if (mode == FRQM_INTEGER)
672 if (v->type != NUMERIC)
674 msg (SE, _("Integer mode specified, but %s is not a numeric "
675 "variable."), v->name);
679 v->p.frq.tab.min = min;
680 v->p.frq.tab.max = max;
681 v->p.frq.tab.vector = pool_alloc (int_pool,
682 sizeof (struct freq) * (max - min + 1));
685 v->p.frq.tab.vector = NULL;
687 v->p.frq.n_groups = 0;
688 v->p.frq.groups = NULL;
693 /* Parses the GROUPED subcommand, setting the frq.{n_grouped,grouped}
694 fields of specified variables. */
696 frq_custom_grouped (struct cmd_frequencies *cmd UNUSED)
699 if ((token == T_ID && dict_lookup_var (default_dict, tokid) != NULL)
705 /* Max, current size of list; list itself. */
713 if (!parse_variables (default_dict, &v, &n,
714 PV_NO_DUPLICATE | PV_NUMERIC))
720 while (token == T_NUM)
725 dl = pool_realloc (int_pool, dl, ml * sizeof (double));
731 /* Note that nl might still be 0 and dl might still be
732 NULL. That's okay. */
733 if (!lex_match (')'))
736 msg (SE, _("`)' expected after GROUPED interval list."));
746 for (i = 0; i < n; i++)
748 if (v[i]->p.frq.used == 0)
749 msg (SE, _("Variables %s specified on GROUPED but not on "
750 "VARIABLES."), v[i]->name);
751 if (v[i]->p.frq.groups != NULL)
752 msg (SE, _("Variables %s specified multiple times on GROUPED "
753 "subcommand."), v[i]->name);
756 v[i]->p.frq.n_groups = nl;
757 v[i]->p.frq.groups = dl;
761 if (!lex_match ('/'))
763 if ((token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
774 /* Adds X to the list of percentiles, keeping the list in proper
777 add_percentile (double x)
781 for (i = 0; i < n_percentiles; i++)
782 if (x <= percentiles[i].p)
785 if (i >= n_percentiles || tokval != percentiles[i].p)
788 = pool_realloc (int_pool, percentiles,
789 (n_percentiles + 1) * sizeof (struct percentile ));
791 if (i < n_percentiles)
792 memmove (&percentiles[i + 1], &percentiles[i],
793 (n_percentiles - i) * sizeof (struct percentile) );
795 percentiles[i].p = x;
800 /* Parses the PERCENTILES subcommand, adding user-specified
801 percentiles to the list. */
803 frq_custom_percentiles (struct cmd_frequencies *cmd UNUSED)
808 msg (SE, _("Percentile list expected after PERCENTILES."));
814 if (tokval < 0 || tokval > 100)
816 msg (SE, _("Percentiles must be between 0 and 100."));
820 add_percentile (tokval / 100.0);
824 while (token == T_NUM);
828 /* Parses the NTILES subcommand, adding the percentiles that
829 correspond to the specified evenly-distributed ntiles. */
831 frq_custom_ntiles (struct cmd_frequencies *cmd UNUSED)
836 if (!lex_force_int ())
838 for (i = 1; i < lex_integer (); i++)
839 add_percentile (1.0 / lex_integer () * i);
844 /* Comparison functions. */
846 /* Hash of numeric values. */
848 hash_value_numeric (const void *value_, void *foo UNUSED)
850 const struct freq *value = value_;
851 return hsh_hash_double (value->v.f);
854 /* Hash of string values. */
856 hash_value_alpha (const void *value_, void *v_)
858 const struct freq *value = value_;
859 struct variable *v = v_;
861 return hsh_hash_bytes (value->v.s, v->width);
864 /* Ascending numeric compare of values. */
866 compare_value_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
868 const struct freq *a = a_;
869 const struct freq *b = b_;
873 else if (a->v.f < b->v.f)
879 /* Ascending string compare of values. */
881 compare_value_alpha_a (const void *a_, const void *b_, void *v_)
883 const struct freq *a = a_;
884 const struct freq *b = b_;
885 const struct variable *v = v_;
887 return memcmp (a->v.s, b->v.s, v->width);
890 /* Descending numeric compare of values. */
892 compare_value_numeric_d (const void *a, const void *b, void *foo UNUSED)
894 return -compare_value_numeric_a (a, b, foo);
897 /* Descending string compare of values. */
899 compare_value_alpha_d (const void *a, const void *b, void *v)
901 return -compare_value_alpha_a (a, b, v);
904 /* Ascending numeric compare of frequency;
905 secondary key on ascending numeric value. */
907 compare_freq_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
909 const struct freq *a = a_;
910 const struct freq *b = b_;
914 else if (a->c < b->c)
919 else if (a->v.f < b->v.f)
925 /* Ascending numeric compare of frequency;
926 secondary key on ascending string value. */
928 compare_freq_alpha_a (const void *a_, const void *b_, void *v_)
930 const struct freq *a = a_;
931 const struct freq *b = b_;
932 const struct variable *v = v_;
936 else if (a->c < b->c)
939 return memcmp (a->v.s, b->v.s, v->width);
942 /* Descending numeric compare of frequency;
943 secondary key on ascending numeric value. */
945 compare_freq_numeric_d (const void *a_, const void *b_, void *foo UNUSED)
947 const struct freq *a = a_;
948 const struct freq *b = b_;
952 else if (a->c < b->c)
957 else if (a->v.f < b->v.f)
963 /* Descending numeric compare of frequency;
964 secondary key on ascending string value. */
966 compare_freq_alpha_d (const void *a_, const void *b_, void *v_)
968 const struct freq *a = a_;
969 const struct freq *b = b_;
970 const struct variable *v = v_;
974 else if (a->c < b->c)
977 return memcmp (a->v.s, b->v.s, v->width);
980 /* Frequency table display. */
982 /* Sets the widths of all the columns and heights of all the rows in
983 table T for driver D. */
985 full_dim (struct tab_table *t, struct outp_driver *d)
987 int lab = cmd.labels == FRQ_LABELS;
991 t->w[0] = min (tab_natural_width (t, d, 0), d->prop_em_width * 15);
992 for (i = lab; i < lab + 5; i++)
993 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
994 for (i = 0; i < t->nr; i++)
995 t->h[i] = d->font_height;
998 /* Displays a full frequency table for variable V. */
1000 dump_full (struct variable * v)
1004 struct tab_table *t;
1006 double cum_total = 0.0;
1007 double cum_freq = 0.0;
1017 static struct init vec[] =
1019 {4, 0, N_("Valid")},
1021 {1, 1, N_("Value")},
1022 {2, 1, N_("Frequency")},
1023 {3, 1, N_("Percent")},
1024 {4, 1, N_("Percent")},
1025 {5, 1, N_("Percent")},
1033 int lab = cmd.labels == FRQ_LABELS;
1035 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
1036 t = tab_create (5 + lab, n_categories + 3, 0);
1037 tab_headers (t, 0, 0, 2, 0);
1038 tab_dim (t, full_dim);
1041 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value Label"));
1042 for (p = vec; p->s; p++)
1043 tab_text (t, p->c - (p->r ? !lab : 0), p->r,
1044 TAB_CENTER | TAT_TITLE, gettext (p->s));
1047 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1049 double percent, valid_percent;
1053 percent = f->c / v->p.frq.tab.total_cases * 100.0;
1054 valid_percent = f->c / v->p.frq.tab.valid_cases * 100.0;
1055 cum_total += valid_percent;
1059 const char *label = val_labs_find (v->val_labs, f->v);
1061 tab_text (t, 0, r, TAB_LEFT, label);
1064 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1065 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1066 tab_float (t, 2 + lab, r, TAB_NONE, percent, 5, 1);
1067 tab_float (t, 3 + lab, r, TAB_NONE, valid_percent, 5, 1);
1068 tab_float (t, 4 + lab, r, TAB_NONE, cum_total, 5, 1);
1071 for (; f < &v->p.frq.tab.valid[n_categories]; f++)
1077 const char *label = val_labs_find (v->val_labs, f->v);
1079 tab_text (t, 0, r, TAB_LEFT, label);
1082 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1083 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1084 tab_float (t, 2 + lab, r, TAB_NONE,
1085 f->c / v->p.frq.tab.total_cases * 100.0, 5, 1);
1086 tab_text (t, 3 + lab, r, TAB_NONE, _("Missing"));
1090 tab_box (t, TAL_1, TAL_1,
1091 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1093 tab_hline (t, TAL_2, 0, 4 + lab, 2);
1094 tab_hline (t, TAL_2, 0, 4 + lab, r);
1095 tab_joint_text (t, 0, r, 0 + lab, r, TAB_RIGHT | TAT_TITLE, _("Total"));
1096 tab_vline (t, TAL_0, 1, r, r);
1097 tab_float (t, 1 + lab, r, TAB_NONE, cum_freq, 8, 0);
1098 tab_float (t, 2 + lab, r, TAB_NONE, 100.0, 5, 1);
1099 tab_float (t, 3 + lab, r, TAB_NONE, 100.0, 5, 1);
1101 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1105 /* Sets the widths of all the columns and heights of all the rows in
1106 table T for driver D. */
1108 condensed_dim (struct tab_table *t, struct outp_driver *d)
1110 int cum_w = max (outp_string_width (d, _("Cum")),
1111 max (outp_string_width (d, _("Cum")),
1112 outp_string_width (d, "000")));
1116 for (i = 0; i < 2; i++)
1117 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1118 for (i = 2; i < 4; i++)
1120 for (i = 0; i < t->nr; i++)
1121 t->h[i] = d->font_height;
1124 /* Display condensed frequency table for variable V. */
1126 dump_condensed (struct variable * v)
1130 struct tab_table *t;
1132 double cum_total = 0.0;
1134 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
1135 t = tab_create (4, n_categories + 2, 0);
1137 tab_headers (t, 0, 0, 2, 0);
1138 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value"));
1139 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Freq"));
1140 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1141 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Cum"));
1142 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1143 tab_dim (t, condensed_dim);
1146 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1150 percent = f->c / v->p.frq.tab.total_cases * 100.0;
1151 cum_total += f->c / v->p.frq.tab.valid_cases * 100.0;
1153 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1154 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1155 tab_float (t, 2, r, TAB_NONE, percent, 3, 0);
1156 tab_float (t, 3, r, TAB_NONE, cum_total, 3, 0);
1159 for (; f < &v->p.frq.tab.valid[n_categories]; f++)
1161 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1162 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1163 tab_float (t, 2, r, TAB_NONE,
1164 f->c / v->p.frq.tab.total_cases * 100.0, 3, 0);
1168 tab_box (t, TAL_1, TAL_1,
1169 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1171 tab_hline (t, TAL_2, 0, 3, 2);
1172 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1173 tab_columns (t, SOM_COL_DOWN, 1);
1177 /* Statistical display. */
1179 /* Calculates all the pertinent statistics for variable V, putting
1180 them in array D[]. FIXME: This could be made much more optimal. */
1182 calc_stats (struct variable * v, double d[frq_n_stats])
1184 double W = v->p.frq.tab.valid_cases;
1193 double *median_value;
1195 /* Calculate percentiles. */
1197 /* If the 50th percentile was not explicitly requested then we must
1198 calculate it anyway --- it's the median */
1200 for (i = 0; i < n_percentiles; i++)
1202 if (percentiles[i].p == 0.5)
1204 median_value = &percentiles[i].value;
1209 if ( 0 == median_value )
1211 add_percentile (0.5);
1215 for (i = 0; i < n_percentiles; i++)
1217 percentiles[i].flag = 0;
1218 percentiles[i].flag2 = 0;
1222 for (idx = 0; idx < v->p.frq.tab.n_valid; ++idx)
1224 static double prev_value = SYSMIS;
1225 f = &v->p.frq.tab.valid[idx];
1227 for (i = 0; i < n_percentiles; i++)
1230 if ( percentiles[i].flag2 ) continue ;
1232 if ( get_algorithm() != COMPATIBLE )
1234 (v->p.frq.tab.valid_cases - 1) * percentiles[i].p;
1237 (v->p.frq.tab.valid_cases + 1) * percentiles[i].p - 1;
1239 if ( percentiles[i].flag )
1241 percentiles[i].x2 = f->v.f;
1242 percentiles[i].x1 = prev_value;
1243 percentiles[i].flag2 = 1;
1249 if ( f->c > 1 && rank - (f->c - 1) > tp )
1251 percentiles[i].x2 = percentiles[i].x1 = f->v.f;
1252 percentiles[i].flag2 = 1;
1256 percentiles[i].flag=1;
1262 prev_value = f->v.f;
1265 for (i = 0; i < n_percentiles; i++)
1267 /* Catches the case when p == 100% */
1268 if ( ! percentiles[i].flag2 )
1269 percentiles[i].x1 = percentiles[i].x2 = f->v.f;
1272 printf("percentile %d (p==%.2f); X1 = %g; X2 = %g\n",
1273 i,percentiles[i].p,percentiles[i].x1,percentiles[i].x2);
1277 for (i = 0; i < n_percentiles; i++)
1279 struct freq_tab *ft = &v->p.frq.tab;
1283 if ( get_algorithm() != COMPATIBLE )
1285 s = modf((ft->valid_cases - 1) * percentiles[i].p , &dummy);
1289 s = modf((ft->valid_cases + 1) * percentiles[i].p -1, &dummy);
1292 percentiles[i].value = percentiles[i].x1 +
1293 ( percentiles[i].x2 - percentiles[i].x1) * s ;
1295 if ( percentiles[i].p == 0.50)
1296 median_value = &percentiles[i].value;
1300 /* Calculate the mode. */
1303 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1305 if (most_often < f->c)
1310 else if (most_often == f->c)
1312 /* A duplicate mode is undefined.
1313 FIXME: keep track of *all* the modes. */
1318 /* Calculate moments. */
1319 m = moments_create (MOMENT_KURTOSIS);
1320 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1321 moments_pass_one (m, f->v.f, f->c);
1322 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1323 moments_pass_two (m, f->v.f, f->c);
1324 moments_calculate (m, NULL, &d[frq_mean], &d[frq_variance],
1325 &d[frq_skew], &d[frq_kurt]);
1326 moments_destroy (m);
1328 /* Formulas below are taken from _SPSS Statistical Algorithms_. */
1329 d[frq_min] = v->p.frq.tab.valid[0].v.f;
1330 d[frq_max] = v->p.frq.tab.valid[v->p.frq.tab.n_valid - 1].v.f;
1331 d[frq_mode] = X_mode;
1332 d[frq_range] = d[frq_max] - d[frq_min];
1333 d[frq_median] = *median_value;
1334 d[frq_sum] = d[frq_mean] * W;
1335 d[frq_stddev] = sqrt (d[frq_variance]);
1336 d[frq_semean] = d[frq_stddev] / sqrt (W);
1337 d[frq_seskew] = calc_seskew (W);
1338 d[frq_sekurt] = calc_sekurt (W);
1341 /* Displays a table of all the statistics requested for variable V. */
1343 dump_statistics (struct variable * v, int show_varname)
1345 double stat_value[frq_n_stats];
1346 struct tab_table *t;
1349 int n_explicit_percentiles = n_percentiles;
1351 if ( implicit_50th && n_percentiles > 0 )
1354 if (v->type == ALPHA)
1356 if (v->p.frq.tab.n_valid == 0)
1358 msg (SW, _("No valid data for variable %s; statistics not displayed."),
1362 calc_stats (v, stat_value);
1364 t = tab_create (3, n_stats + n_explicit_percentiles + 2, 0);
1365 tab_dim (t, tab_natural_dimensions);
1367 tab_box (t, TAL_1, TAL_1, -1, -1 , 0 , 0 , 2, tab_nr(t) - 1) ;
1370 tab_vline (t, TAL_1 , 2, 0, tab_nr(t) - 1);
1371 tab_vline (t, TAL_1 | TAL_SPACING , 1, 0, tab_nr(t) - 1 ) ;
1373 r=2; /* N missing and N valid are always dumped */
1375 for (i = 0; i < frq_n_stats; i++)
1376 if (stats & BIT_INDEX (i))
1378 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
1379 gettext (st_name[i].s10));
1380 tab_float (t, 2, r, TAB_NONE, stat_value[i], 11, 3);
1384 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("N"));
1385 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Valid"));
1386 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Missing"));
1388 tab_float(t, 2, 0, TAB_NONE, v->p.frq.tab.valid_cases, 11, 0);
1389 tab_float(t, 2, 1, TAB_NONE,
1390 v->p.frq.tab.total_cases - v->p.frq.tab.valid_cases, 11, 0);
1393 for (i = 0; i < n_explicit_percentiles; i++, r++)
1397 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Percentiles"));
1400 tab_float (t, 1, r, TAB_LEFT, percentiles[i].p * 100, 3, 0 );
1401 tab_float (t, 2, r, TAB_NONE, percentiles[i].value, 11, 3);
1405 tab_columns (t, SOM_COL_DOWN, 1);
1409 tab_title (t, 1, "%s: %s", v->name, v->label);
1411 tab_title (t, 0, v->name);
1414 tab_flags (t, SOMF_NO_TITLE);