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"
37 #include "algorithm.h"
45 #include "value-labels.h"
49 #include "debug-print.h"
54 format=cond:condense/onepage(*n:onepage_limit,"%s>=0")/!standard,
55 table:limit(n:limit,"%s>0")/notable/!table,
56 labels:!labels/nolabels,
57 sort:!avalue/dvalue/afreq/dfreq,
58 spaces:!single/double,
59 paging:newpage/!oldpage;
60 missing=miss:include/!exclude;
61 barchart(ba_)=:minimum(d:min),
63 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0");
64 histogram(hi_)=:minimum(d:min),
66 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
67 norm:!nonormal/normal,
68 incr:increment(d:inc,"%s>0");
69 hbar(hb_)=:minimum(d:min),
71 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
72 norm:!nonormal/normal,
73 incr:increment(d:inc,"%s>0");
77 statistics[st_]=1|mean,2|semean,3|median,4|mode,5|stddev,6|variance,
78 7|kurtosis,8|skewness,9|range,10|minimum,11|maximum,12|sum,
79 13|default,14|seskewness,15|sekurtosis,all,none.
84 /* Description of a statistic. */
87 int st_indx; /* Index into a_statistics[]. */
88 const char *s10; /* Identifying string. */
91 /* Table of statistics, indexed by dsc_*. */
92 static struct frq_info st_name[frq_n_stats + 1] =
94 {FRQ_ST_MEAN, N_("Mean")},
95 {FRQ_ST_SEMEAN, N_("S.E. Mean")},
96 {FRQ_ST_MEDIAN, N_("Median")},
97 {FRQ_ST_MODE, N_("Mode")},
98 {FRQ_ST_STDDEV, N_("Std Dev")},
99 {FRQ_ST_VARIANCE, N_("Variance")},
100 {FRQ_ST_KURTOSIS, N_("Kurtosis")},
101 {FRQ_ST_SEKURTOSIS, N_("S.E. Kurt")},
102 {FRQ_ST_SKEWNESS, N_("Skewness")},
103 {FRQ_ST_SESKEWNESS, N_("S.E. Skew")},
104 {FRQ_ST_RANGE, N_("Range")},
105 {FRQ_ST_MINIMUM, N_("Minimum")},
106 {FRQ_ST_MAXIMUM, N_("Maximum")},
107 {FRQ_ST_SUM, N_("Sum")},
111 /* Percentiles to calculate. */
112 static double *percentiles;
113 static double *percentile_values;
114 static int n_percentiles;
116 /* Groups of statistics. */
118 #define frq_default \
119 (BI (frq_mean) | BI (frq_stddev) | BI (frq_min) | BI (frq_max))
121 (BI (frq_sum) | BI(frq_min) | BI(frq_max) \
122 | BI(frq_mean) | BI(frq_semean) | BI(frq_stddev) \
123 | BI(frq_variance) | BI(frq_kurt) | BI(frq_sekurt) \
124 | BI(frq_skew) | BI(frq_seskew) | BI(frq_range) \
125 | BI(frq_range) | BI(frq_mode) | BI(frq_median))
127 /* Statistics; number of statistics. */
128 static unsigned long stats;
131 /* Types of graphs. */
134 GFT_NONE, /* Don't draw graphs. */
135 GFT_BAR, /* Draw bar charts. */
136 GFT_HIST, /* Draw histograms. */
137 GFT_HBAR /* Draw bar charts or histograms at our discretion. */
140 /* Parsed command. */
141 static struct cmd_frequencies cmd;
143 /* Summary of the barchart, histogram, and hbar subcommands. */
144 static int chart; /* NONE/BAR/HIST/HBAR. */
145 static double min, max; /* Minimum, maximum on y axis. */
146 static int format; /* FREQ/PERCENT: Scaling of y axis. */
147 static double scale, incr; /* FIXME */
148 static int normal; /* FIXME */
150 /* Variables for which to calculate statistics. */
151 static int n_variables;
152 static struct variable **v_variables;
154 /* Arenas used to store semi-permanent storage. */
155 static struct pool *int_pool; /* Integer mode. */
156 static struct pool *gen_pool; /* General mode. */
158 /* Easier access to a_statistics. */
159 #define stat cmd.a_statistics
161 static void determine_charts (void);
163 static void precalc (void *);
164 static int calc (struct ccase *, void *);
165 static void postcalc (void *);
167 static void postprocess_freq_tab (struct variable *);
168 static void dump_full (struct variable *);
169 static void dump_condensed (struct variable *);
170 static void dump_statistics (struct variable *, int show_varname);
171 static void cleanup_freq_tab (struct variable *);
173 static hsh_hash_func hash_value_numeric, hash_value_alpha;
174 static hsh_compare_func compare_value_numeric_a, compare_value_alpha_a;
175 static hsh_compare_func compare_value_numeric_d, compare_value_alpha_d;
176 static hsh_compare_func compare_freq_numeric_a, compare_freq_alpha_a;
177 static hsh_compare_func compare_freq_numeric_d, compare_freq_alpha_d;
179 /* Parser and outline. */
181 static int internal_cmd_frequencies (void);
184 cmd_frequencies (void)
188 int_pool = pool_create ();
189 result = internal_cmd_frequencies ();
190 pool_destroy (int_pool);
192 pool_destroy (gen_pool);
200 internal_cmd_frequencies (void)
205 percentile_values = NULL;
211 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
212 dict_get_var(default_dict, i)->p.frq.used = 0;
214 lex_match_id ("FREQUENCIES");
215 if (!parse_frequencies (&cmd))
218 if (cmd.onepage_limit == NOT_LONG)
219 cmd.onepage_limit = 50;
221 /* Figure out statistics to calculate. */
223 if (stat[FRQ_ST_DEFAULT] || !cmd.sbc_statistics)
224 stats |= frq_default;
225 if (stat[FRQ_ST_ALL])
227 if (cmd.sort != FRQ_AVALUE && cmd.sort != FRQ_DVALUE)
228 stats &= ~frq_median;
229 for (i = 0; i < frq_n_stats; i++)
230 if (stat[st_name[i].st_indx])
231 stats |= BIT_INDEX (i);
232 if (stats & frq_kurt)
234 if (stats & frq_skew)
237 /* Calculate n_stats. */
239 for (i = 0; i < frq_n_stats; i++)
240 if ((stats & BIT_INDEX (i)))
245 if (chart != GFT_NONE || cmd.sbc_ntiles)
246 cmd.sort = FRQ_AVALUE;
249 procedure_with_splits (precalc, calc, postcalc, NULL);
254 /* Figure out which charts the user requested. */
256 determine_charts (void)
258 int count = (!!cmd.sbc_histogram) + (!!cmd.sbc_barchart) + (!!cmd.sbc_hbar);
268 msg (SW, _("At most one of BARCHART, HISTOGRAM, or HBAR should be "
269 "given. HBAR will be assumed. Argument values will be "
270 "given precedence increasing along the order given."));
272 else if (cmd.sbc_histogram)
274 else if (cmd.sbc_barchart)
285 if (cmd.sbc_barchart)
287 if (cmd.ba_min != SYSMIS)
289 if (cmd.ba_max != SYSMIS)
291 if (cmd.ba_scale == FRQ_FREQ)
296 else if (cmd.ba_scale == FRQ_PERCENT)
298 format = FRQ_PERCENT;
303 if (cmd.sbc_histogram)
305 if (cmd.hi_min != SYSMIS)
307 if (cmd.hi_max != SYSMIS)
309 if (cmd.hi_scale == FRQ_FREQ)
314 else if (cmd.hi_scale == FRQ_PERCENT)
316 format = FRQ_PERCENT;
321 if (cmd.hi_incr == FRQ_INCREMENT)
327 if (cmd.hb_min != SYSMIS)
329 if (cmd.hb_max != SYSMIS)
331 if (cmd.hb_scale == FRQ_FREQ)
336 else if (cmd.hb_scale == FRQ_PERCENT)
338 format = FRQ_PERCENT;
343 if (cmd.hb_incr == FRQ_INCREMENT)
347 if (min != SYSMIS && max != SYSMIS && min >= max)
349 msg (SE, _("MAX must be greater than or equal to MIN, if both are "
350 "specified. However, MIN was specified as %g and MAX as %g. "
351 "MIN and MAX will be ignored."), min, max);
356 /* Add data from case C to the frequency table. */
358 calc (struct ccase *c, void *aux UNUSED)
363 weight = dict_get_case_weight (default_dict, c);
365 for (i = 0; i < n_variables; i++)
367 struct variable *v = v_variables[i];
368 union value *val = &c->data[v->fv];
369 struct freq_tab *ft = &v->p.frq.tab;
371 switch (v->p.frq.tab.mode)
376 struct freq **fpp = (struct freq **) hsh_probe (ft->data, val);
382 struct freq *fp = *fpp = pool_alloc (gen_pool, sizeof *fp);
390 if (val->f == SYSMIS)
391 v->p.frq.tab.sysmis += weight;
392 else if (val->f > INT_MIN+1 && val->f < INT_MAX-1)
395 if (i >= v->p.frq.tab.min && i <= v->p.frq.tab.max)
396 v->p.frq.tab.vector[i - v->p.frq.tab.min] += weight;
399 v->p.frq.tab.out_of_range += weight;
408 /* Prepares each variable that is the target of FREQUENCIES by setting
409 up its hash table. */
411 precalc (void *aux UNUSED)
415 pool_destroy (gen_pool);
416 gen_pool = pool_create ();
418 for (i = 0; i < n_variables; i++)
420 struct variable *v = v_variables[i];
422 if (v->p.frq.tab.mode == FRQM_GENERAL)
425 hsh_compare_func *compare;
427 if (v->type == NUMERIC)
429 hash = hash_value_numeric;
430 compare = compare_value_numeric_a;
434 hash = hash_value_alpha;
435 compare = compare_value_alpha_a;
437 v->p.frq.tab.data = hsh_create (16, compare, hash, NULL, v);
443 for (j = (v->p.frq.tab.max - v->p.frq.tab.min); j >= 0; j--)
444 v->p.frq.tab.vector[j] = 0.0;
445 v->p.frq.tab.out_of_range = 0.0;
446 v->p.frq.tab.sysmis = 0.0;
451 /* Finishes up with the variables after frequencies have been
452 calculated. Displays statistics, percentiles, ... */
454 postcalc (void *aux UNUSED)
458 for (i = 0; i < n_variables; i++)
460 struct variable *v = v_variables[i];
462 int dumped_freq_tab = 1;
464 postprocess_freq_tab (v);
466 /* Frequencies tables. */
467 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
468 if (cmd.table == FRQ_TABLE
469 || (cmd.table == FRQ_LIMIT && n_categories <= cmd.limit))
479 if (n_categories > cmd.onepage_limit)
492 dump_statistics (v, !dumped_freq_tab);
494 cleanup_freq_tab (v);
498 /* Returns the comparison function that should be used for
499 sorting a frequency table by FRQ_SORT using VAR_TYPE
501 static hsh_compare_func *
502 get_freq_comparator (int frq_sort, int var_type)
504 /* Note that q2c generates tags beginning with 1000. */
505 switch (frq_sort | (var_type << 16))
507 case FRQ_AVALUE | (NUMERIC << 16): return compare_value_numeric_a;
508 case FRQ_AVALUE | (ALPHA << 16): return compare_value_alpha_a;
509 case FRQ_DVALUE | (NUMERIC << 16): return compare_value_numeric_d;
510 case FRQ_DVALUE | (ALPHA << 16): return compare_value_alpha_d;
511 case FRQ_AFREQ | (NUMERIC << 16): return compare_freq_numeric_a;
512 case FRQ_AFREQ | (ALPHA << 16): return compare_freq_alpha_a;
513 case FRQ_DFREQ | (NUMERIC << 16): return compare_freq_numeric_d;
514 case FRQ_DFREQ | (ALPHA << 16): return compare_freq_alpha_d;
521 /* Returns nonzero iff the value in struct freq F is non-missing
524 not_missing (const void *f_, void *v_)
526 const struct freq *f = f_;
527 struct variable *v = v_;
529 return !is_missing (&f->v, v);
532 /* Summarizes the frequency table data for variable V. */
534 postprocess_freq_tab (struct variable *v)
536 hsh_compare_func *compare;
540 struct freq *freqs, *f;
543 assert (v->p.frq.tab.mode == FRQM_GENERAL);
544 compare = get_freq_comparator (cmd.sort, v->type);
547 /* Extract data from hash table. */
548 count = hsh_count (ft->data);
549 data = hsh_data (ft->data);
551 /* Copy dereferenced data into freqs. */
552 freqs = xmalloc (count* sizeof *freqs);
553 for (i = 0; i < count; i++)
555 struct freq *f = data[i];
559 /* Put data into ft. */
561 ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, v);
562 ft->missing = freqs + ft->n_valid;
563 ft->n_missing = count - ft->n_valid;
566 sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare, v);
567 sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare, v);
569 /* Summary statistics. */
570 ft->total_cases = ft->valid_cases = 0.0;
571 for (f = ft->valid; f < ft->valid + ft->n_valid; f++)
573 ft->total_cases += f->c;
575 if ((v->type != NUMERIC || f->v.f != SYSMIS)
576 && (cmd.miss != FRQ_EXCLUDE || !is_user_missing (&f->v, v)))
577 ft->valid_cases += f->c;
581 /* Frees the frequency table for variable V. */
583 cleanup_freq_tab (struct variable *v)
585 assert (v->p.frq.tab.mode == FRQM_GENERAL);
586 free (v->p.frq.tab.valid);
587 hsh_destroy (v->p.frq.tab.data);
590 /* Parses the VARIABLES subcommand, adding to
591 {n_variables,v_variables}. */
593 frq_custom_variables (struct cmd_frequencies *cmd UNUSED)
596 int min = 0, max = 0;
598 int old_n_variables = n_variables;
602 if (token != T_ALL && (token != T_ID
603 || dict_lookup_var (default_dict, tokid) == NULL))
606 if (!parse_variables (default_dict, &v_variables, &n_variables,
607 PV_APPEND | PV_NO_SCRATCH))
610 for (i = old_n_variables; i < n_variables; i++)
611 v_variables[i]->p.frq.tab.mode = FRQM_GENERAL;
613 if (!lex_match ('('))
618 if (!lex_force_int ())
620 min = lex_integer ();
622 if (!lex_force_match (','))
624 if (!lex_force_int ())
626 max = lex_integer ();
628 if (!lex_force_match (')'))
632 msg (SE, _("Upper limit of integer mode value range must be "
633 "greater than lower limit."));
638 for (i = old_n_variables; i < n_variables; i++)
640 struct variable *v = v_variables[i];
642 if (v->p.frq.used != 0)
644 msg (SE, _("Variable %s specified multiple times on VARIABLES "
645 "subcommand."), v->name);
649 v->p.frq.used = 1; /* Used simply as a marker. */
651 v->p.frq.tab.valid = v->p.frq.tab.missing = NULL;
653 if (mode == FRQM_INTEGER)
655 if (v->type != NUMERIC)
657 msg (SE, _("Integer mode specified, but %s is not a numeric "
658 "variable."), v->name);
662 v->p.frq.tab.min = min;
663 v->p.frq.tab.max = max;
664 v->p.frq.tab.vector = pool_alloc (int_pool,
665 sizeof (struct freq) * (max - min + 1));
668 v->p.frq.tab.vector = NULL;
670 v->p.frq.n_groups = 0;
671 v->p.frq.groups = NULL;
676 /* Parses the GROUPED subcommand, setting the frq.{n_grouped,grouped}
677 fields of specified variables. */
679 frq_custom_grouped (struct cmd_frequencies *cmd UNUSED)
682 if ((token == T_ID && dict_lookup_var (default_dict, tokid) != NULL)
688 /* Max, current size of list; list itself. */
696 if (!parse_variables (default_dict, &v, &n,
697 PV_NO_DUPLICATE | PV_NUMERIC))
703 while (token == T_NUM)
708 dl = pool_realloc (int_pool, dl, ml * sizeof (double));
714 /* Note that nl might still be 0 and dl might still be
715 NULL. That's okay. */
716 if (!lex_match (')'))
719 msg (SE, _("`)' expected after GROUPED interval list."));
729 for (i = 0; i < n; i++)
731 if (v[i]->p.frq.used == 0)
732 msg (SE, _("Variables %s specified on GROUPED but not on "
733 "VARIABLES."), v[i]->name);
734 if (v[i]->p.frq.groups != NULL)
735 msg (SE, _("Variables %s specified multiple times on GROUPED "
736 "subcommand."), v[i]->name);
739 v[i]->p.frq.n_groups = nl;
740 v[i]->p.frq.groups = dl;
744 if (!lex_match ('/'))
746 if ((token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
757 /* Adds X to the list of percentiles, keeping the list in proper
760 add_percentile (double x)
764 for (i = 0; i < n_percentiles; i++)
765 if (x <= percentiles[i])
767 if (i >= n_percentiles || tokval != percentiles[i])
770 = pool_realloc (int_pool, percentiles,
771 (n_percentiles + 1) * sizeof *percentiles);
773 = pool_realloc (int_pool, percentile_values,
774 (n_percentiles + 1) * sizeof *percentile_values);
775 if (i < n_percentiles)
777 memmove (&percentiles[i + 1], &percentiles[i],
778 (n_percentiles - i) * sizeof *percentiles);
779 memmove (&percentile_values[i + 1], &percentile_values[i],
780 (n_percentiles - i) * sizeof *percentile_values);
787 /* Parses the PERCENTILES subcommand, adding user-specified
788 percentiles to the list. */
790 frq_custom_percentiles (struct cmd_frequencies *cmd UNUSED)
795 msg (SE, _("Percentile list expected after PERCENTILES."));
801 if (tokval <= 0 || tokval >= 100)
803 msg (SE, _("Percentiles must be greater than "
804 "0 and less than 100."));
808 add_percentile (tokval / 100.0);
812 while (token == T_NUM);
816 /* Parses the NTILES subcommand, adding the percentiles that
817 correspond to the specified evenly-distributed ntiles. */
819 frq_custom_ntiles (struct cmd_frequencies *cmd UNUSED)
824 if (!lex_force_int ())
826 for (i = 1; i < lex_integer (); i++)
827 add_percentile (1.0 / lex_integer () * i);
832 /* Comparison functions. */
834 /* Hash of numeric values. */
836 hash_value_numeric (const void *value_, void *foo UNUSED)
838 const struct freq *value = value_;
839 return hsh_hash_double (value->v.f);
842 /* Hash of string values. */
844 hash_value_alpha (const void *value_, void *v_)
846 const struct freq *value = value_;
847 struct variable *v = v_;
849 return hsh_hash_bytes (value->v.s, v->width);
852 /* Ascending numeric compare of values. */
854 compare_value_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
856 const struct freq *a = a_;
857 const struct freq *b = b_;
861 else if (a->v.f < b->v.f)
867 /* Ascending string compare of values. */
869 compare_value_alpha_a (const void *a_, const void *b_, void *v_)
871 const struct freq *a = a_;
872 const struct freq *b = b_;
873 const struct variable *v = v_;
875 return memcmp (a->v.s, b->v.s, v->width);
878 /* Descending numeric compare of values. */
880 compare_value_numeric_d (const void *a, const void *b, void *foo UNUSED)
882 return -compare_value_numeric_a (a, b, foo);
885 /* Descending string compare of values. */
887 compare_value_alpha_d (const void *a, const void *b, void *v)
889 return -compare_value_alpha_a (a, b, v);
892 /* Ascending numeric compare of frequency;
893 secondary key on ascending numeric value. */
895 compare_freq_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
897 const struct freq *a = a_;
898 const struct freq *b = b_;
902 else if (a->c < b->c)
907 else if (a->v.f < b->v.f)
913 /* Ascending numeric compare of frequency;
914 secondary key on ascending string value. */
916 compare_freq_alpha_a (const void *a_, const void *b_, void *v_)
918 const struct freq *a = a_;
919 const struct freq *b = b_;
920 const struct variable *v = v_;
924 else if (a->c < b->c)
927 return memcmp (a->v.s, b->v.s, v->width);
930 /* Descending numeric compare of frequency;
931 secondary key on ascending numeric value. */
933 compare_freq_numeric_d (const void *a_, const void *b_, void *foo UNUSED)
935 const struct freq *a = a_;
936 const struct freq *b = b_;
940 else if (a->c < b->c)
945 else if (a->v.f < b->v.f)
951 /* Descending numeric compare of frequency;
952 secondary key on ascending string value. */
954 compare_freq_alpha_d (const void *a_, const void *b_, void *v_)
956 const struct freq *a = a_;
957 const struct freq *b = b_;
958 const struct variable *v = v_;
962 else if (a->c < b->c)
965 return memcmp (a->v.s, b->v.s, v->width);
968 /* Frequency table display. */
970 /* Sets the widths of all the columns and heights of all the rows in
971 table T for driver D. */
973 full_dim (struct tab_table *t, struct outp_driver *d)
975 int lab = cmd.labels == FRQ_LABELS;
979 t->w[0] = min (tab_natural_width (t, d, 0), d->prop_em_width * 15);
980 for (i = lab; i < lab + 5; i++)
981 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
982 for (i = 0; i < t->nr; i++)
983 t->h[i] = d->font_height;
986 /* Displays a full frequency table for variable V. */
988 dump_full (struct variable * v)
994 double cum_total = 0.0;
995 double cum_freq = 0.0;
1005 static struct init vec[] =
1007 {4, 0, N_("Valid")},
1009 {1, 1, N_("Value")},
1010 {2, 1, N_("Frequency")},
1011 {3, 1, N_("Percent")},
1012 {4, 1, N_("Percent")},
1013 {5, 1, N_("Percent")},
1021 int lab = cmd.labels == FRQ_LABELS;
1023 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
1024 t = tab_create (5 + lab, n_categories + 3, 0);
1025 tab_headers (t, 0, 0, 2, 0);
1026 tab_dim (t, full_dim);
1029 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value Label"));
1030 for (p = vec; p->s; p++)
1031 tab_text (t, p->c - (p->r ? !lab : 0), p->r,
1032 TAB_CENTER | TAT_TITLE, gettext (p->s));
1035 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1037 double percent, valid_percent;
1041 percent = f->c / v->p.frq.tab.total_cases * 100.0;
1042 valid_percent = f->c / v->p.frq.tab.valid_cases * 100.0;
1043 cum_total += valid_percent;
1047 const char *label = val_labs_find (v->val_labs, f->v);
1049 tab_text (t, 0, r, TAB_LEFT, label);
1052 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1053 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1054 tab_float (t, 2 + lab, r, TAB_NONE, percent, 5, 1);
1055 tab_float (t, 3 + lab, r, TAB_NONE, valid_percent, 5, 1);
1056 tab_float (t, 4 + lab, r, TAB_NONE, cum_total, 5, 1);
1059 for (; f < &v->p.frq.tab.valid[n_categories]; f++)
1065 const char *label = val_labs_find (v->val_labs, f->v);
1067 tab_text (t, 0, r, TAB_LEFT, label);
1070 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1071 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1072 tab_float (t, 2 + lab, r, TAB_NONE,
1073 f->c / v->p.frq.tab.total_cases * 100.0, 5, 1);
1074 tab_text (t, 3 + lab, r, TAB_NONE, _("Missing"));
1078 tab_box (t, TAL_1, TAL_1,
1079 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1081 tab_hline (t, TAL_2, 0, 4 + lab, 2);
1082 tab_hline (t, TAL_2, 0, 4 + lab, r);
1083 tab_joint_text (t, 0, r, 0 + lab, r, TAB_RIGHT | TAT_TITLE, _("Total"));
1084 tab_vline (t, TAL_0, 1, r, r);
1085 tab_float (t, 1 + lab, r, TAB_NONE, cum_freq, 8, 0);
1086 tab_float (t, 2 + lab, r, TAB_NONE, 100.0, 5, 1);
1087 tab_float (t, 3 + lab, r, TAB_NONE, 100.0, 5, 1);
1089 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1093 /* Sets the widths of all the columns and heights of all the rows in
1094 table T for driver D. */
1096 condensed_dim (struct tab_table *t, struct outp_driver *d)
1098 int cum_w = max (outp_string_width (d, _("Cum")),
1099 max (outp_string_width (d, _("Cum")),
1100 outp_string_width (d, "000")));
1104 for (i = 0; i < 2; i++)
1105 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1106 for (i = 2; i < 4; i++)
1108 for (i = 0; i < t->nr; i++)
1109 t->h[i] = d->font_height;
1112 /* Display condensed frequency table for variable V. */
1114 dump_condensed (struct variable * v)
1118 struct tab_table *t;
1120 double cum_total = 0.0;
1122 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
1123 t = tab_create (4, n_categories + 2, 0);
1125 tab_headers (t, 0, 0, 2, 0);
1126 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value"));
1127 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Freq"));
1128 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1129 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Cum"));
1130 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1131 tab_dim (t, condensed_dim);
1134 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1138 percent = f->c / v->p.frq.tab.total_cases * 100.0;
1139 cum_total += f->c / v->p.frq.tab.valid_cases * 100.0;
1141 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1142 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1143 tab_float (t, 2, r, TAB_NONE, percent, 3, 0);
1144 tab_float (t, 3, r, TAB_NONE, cum_total, 3, 0);
1147 for (; f < &v->p.frq.tab.valid[n_categories]; f++)
1149 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1150 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1151 tab_float (t, 2, r, TAB_NONE,
1152 f->c / v->p.frq.tab.total_cases * 100.0, 3, 0);
1156 tab_box (t, TAL_1, TAL_1,
1157 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1159 tab_hline (t, TAL_2, 0, 3, 2);
1160 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1161 tab_columns (t, SOM_COL_DOWN, 1);
1165 /* Statistical display. */
1167 /* Calculates all the pertinent statistics for variable V, putting
1168 them in array D[]. FIXME: This could be made much more optimal. */
1170 calc_stats (struct variable * v, double d[frq_n_stats])
1172 double W = v->p.frq.tab.valid_cases;
1173 double X_bar, X_mode, M2, M3, M4;
1179 double previous_value;
1181 /* Calculate the mean. */
1183 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1184 X_bar += f->v.f * f->c;
1187 /* Calculate percentiles. */
1189 previous_value = SYSMIS;
1190 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1193 for (; i < n_percentiles; i++)
1195 if (cum_total / v->p.frq.tab.valid_cases < percentiles[i])
1198 percentile_values[i] = previous_value;
1200 previous_value = f->v.f;
1203 /* Calculate the mode. */
1206 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1208 if (most_often < f->c)
1213 else if (most_often == f->c)
1215 /* A duplicate mode is undefined.
1216 FIXME: keep track of *all* the modes. */
1221 /* Calculate moments about the mean. */
1223 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1225 double dev = f->v.f - X_bar;
1235 /* Formulas below are taken from _SPSS Statistical Algorithms_. */
1236 d[frq_min] = v->p.frq.tab.valid[0].v.f;
1237 d[frq_max] = v->p.frq.tab.valid[v->p.frq.tab.n_valid - 1].v.f;
1238 d[frq_mode] = X_mode;
1239 d[frq_range] = d[frq_max] - d[frq_min];
1240 d[frq_median] = SYSMIS;
1241 d[frq_mean] = X_bar;
1242 d[frq_sum] = X_bar * W;
1243 d[frq_variance] = M2 / (W - 1);
1244 d[frq_stddev] = sqrt (d[frq_variance]);
1245 d[frq_semean] = d[frq_stddev] / sqrt (W);
1246 if (W >= 3.0 && d[frq_variance] > 0)
1248 double S = d[frq_stddev];
1249 d[frq_skew] = (W * M3 / ((W - 1.0) * (W - 2.0) * S * S * S));
1250 d[frq_seskew] = sqrt (6.0 * W * (W - 1.0)
1251 / ((W - 2.0) * (W + 1.0) * (W + 3.0)));
1255 d[frq_skew] = d[frq_seskew] = SYSMIS;
1257 if (W >= 4.0 && d[frq_variance] > 0)
1259 double S2 = d[frq_variance];
1260 double SE_g1 = d[frq_seskew];
1262 d[frq_kurt] = ((W * (W + 1.0) * M4 - 3.0 * M2 * M2 * (W - 1.0))
1263 / ((W - 1.0) * (W - 2.0) * (W - 3.0) * S2 * S2));
1264 d[frq_sekurt] = sqrt ((4.0 * (W * W - 1.0) * SE_g1 * SE_g1)
1265 / ((W - 3.0) * (W + 5.0)));
1269 d[frq_kurt] = d[frq_sekurt] = SYSMIS;
1273 /* Displays a table of all the statistics requested for variable V. */
1275 dump_statistics (struct variable * v, int show_varname)
1277 double stat_value[frq_n_stats];
1278 struct tab_table *t;
1281 if (v->type == ALPHA)
1283 if (v->p.frq.tab.n_valid == 0)
1285 msg (SW, _("No valid data for variable %s; statistics not displayed."),
1289 calc_stats (v, stat_value);
1291 t = tab_create (2, n_stats + n_percentiles, 0);
1292 tab_dim (t, tab_natural_dimensions);
1293 tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, n_stats - 1);
1294 for (i = r = 0; i < frq_n_stats; i++)
1295 if (stats & BIT_INDEX (i))
1297 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
1298 gettext (st_name[i].s10));
1299 tab_float (t, 1, r, TAB_NONE, stat_value[i], 11, 3);
1303 for (i = 0; i < n_percentiles; i++, r++)
1307 ds_init (gen_pool, &ds, 20);
1308 ds_printf (&ds, "%s %d", _("Percentile"), (int) (percentiles[i] * 100));
1310 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds.string);
1311 tab_float (t, 1, r, TAB_NONE, percentile_values[i], 11, 3);
1316 tab_columns (t, SOM_COL_DOWN, 1);
1320 tab_title (t, 1, "%s: %s", v->name, v->label);
1322 tab_title (t, 0, v->name);
1325 tab_flags (t, SOMF_NO_TITLE);