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"
33 #include "dictionary.h"
40 #include "algorithm.h"
47 #include "value-labels.h"
54 #include "debug-print.h"
59 format=cond:condense/onepage(*n:onepage_limit,"%s>=0")/!standard,
60 table:limit(n:limit,"%s>0")/notable/!table,
61 labels:!labels/nolabels,
62 sort:!avalue/dvalue/afreq/dfreq,
63 spaces:!single/double,
64 paging:newpage/!oldpage;
65 missing=miss:include/!exclude;
66 barchart(ba_)=:minimum(d:min),
68 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0");
69 piechart(pie_)=:minimum(d:min),
71 missing:missing/!nomissing;
72 histogram(hi_)=:minimum(d:min),
74 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
75 norm:!nonormal/normal,
76 incr:increment(d:inc,"%s>0");
77 hbar(hb_)=:minimum(d:min),
79 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
80 norm:!nonormal/normal,
81 incr:increment(d:inc,"%s>0");
84 +percentiles = double list;
85 statistics[st_]=1|mean,2|semean,3|median,4|mode,5|stddev,6|variance,
86 7|kurtosis,8|skewness,9|range,10|minimum,11|maximum,12|sum,
87 13|default,14|seskewness,15|sekurtosis,all,none.
95 frq_mean = 0, frq_semean, frq_median, frq_mode, frq_stddev, frq_variance,
96 frq_kurt, frq_sekurt, frq_skew, frq_seskew, frq_range, frq_min, frq_max,
100 /* Description of a statistic. */
103 int st_indx; /* Index into a_statistics[]. */
104 const char *s10; /* Identifying string. */
107 /* Table of statistics, indexed by dsc_*. */
108 static struct frq_info st_name[frq_n_stats + 1] =
110 {FRQ_ST_MEAN, N_("Mean")},
111 {FRQ_ST_SEMEAN, N_("S.E. Mean")},
112 {FRQ_ST_MEDIAN, N_("Median")},
113 {FRQ_ST_MODE, N_("Mode")},
114 {FRQ_ST_STDDEV, N_("Std Dev")},
115 {FRQ_ST_VARIANCE, N_("Variance")},
116 {FRQ_ST_KURTOSIS, N_("Kurtosis")},
117 {FRQ_ST_SEKURTOSIS, N_("S.E. Kurt")},
118 {FRQ_ST_SKEWNESS, N_("Skewness")},
119 {FRQ_ST_SESKEWNESS, N_("S.E. Skew")},
120 {FRQ_ST_RANGE, N_("Range")},
121 {FRQ_ST_MINIMUM, N_("Minimum")},
122 {FRQ_ST_MAXIMUM, N_("Maximum")},
123 {FRQ_ST_SUM, N_("Sum")},
127 /* Percentiles to calculate. */
131 double p; /* the %ile to be calculated */
132 double value; /* the %ile's value */
133 double x1; /* The datum value <= the percentile */
134 double x2; /* The datum value >= the percentile */
136 int flag2; /* Set to 1 if this percentile value has been found */
140 static void add_percentile (double x) ;
142 static struct percentile *percentiles;
143 static int n_percentiles;
145 static int implicit_50th ;
147 /* Groups of statistics. */
149 #define frq_default \
150 (BI (frq_mean) | BI (frq_stddev) | BI (frq_min) | BI (frq_max))
152 (BI (frq_sum) | BI(frq_min) | BI(frq_max) \
153 | BI(frq_mean) | BI(frq_semean) | BI(frq_stddev) \
154 | BI(frq_variance) | BI(frq_kurt) | BI(frq_sekurt) \
155 | BI(frq_skew) | BI(frq_seskew) | BI(frq_range) \
156 | BI(frq_range) | BI(frq_mode) | BI(frq_median))
158 /* Statistics; number of statistics. */
159 static unsigned long stats;
162 /* Types of graphs. */
165 GFT_NONE, /* Don't draw graphs. */
166 GFT_BAR, /* Draw bar charts. */
167 GFT_HIST, /* Draw histograms. */
168 GFT_PIE, /* Draw piechart */
169 GFT_HBAR /* Draw bar charts or histograms at our discretion. */
172 /* Parsed command. */
173 static struct cmd_frequencies cmd;
175 /* Summary of the barchart, histogram, and hbar subcommands. */
176 /* FIXME: These should not be mututally exclusive */
177 static int chart; /* NONE/BAR/HIST/HBAR/PIE. */
178 static double min, max; /* Minimum, maximum on y axis. */
179 static int format; /* FREQ/PERCENT: Scaling of y axis. */
180 static double scale, incr; /* FIXME */
181 static int normal; /* FIXME */
183 /* Variables for which to calculate statistics. */
184 static int n_variables;
185 static struct variable **v_variables;
187 /* Arenas used to store semi-permanent storage. */
188 static struct pool *int_pool; /* Integer mode. */
189 static struct pool *gen_pool; /* General mode. */
191 /* Per-variable frequency data. */
194 /* Freqency table. */
195 struct freq_tab tab; /* Frequencies table to use. */
198 int n_groups; /* Number of groups. */
199 double *groups; /* Groups. */
202 double stat[frq_n_stats];
205 static inline struct var_freqs *
206 get_var_freqs (struct variable *v)
209 assert (v->aux != NULL);
213 static void determine_charts (void);
215 static void calc_stats (struct variable *v, double d[frq_n_stats]);
217 static void precalc (void *);
218 static int calc (struct ccase *, void *);
219 static void postcalc (void *);
221 static void postprocess_freq_tab (struct variable *);
222 static void dump_full (struct variable *);
223 static void dump_condensed (struct variable *);
224 static void dump_statistics (struct variable *, int show_varname);
225 static void cleanup_freq_tab (struct variable *);
227 static hsh_hash_func hash_value_numeric, hash_value_alpha;
228 static hsh_compare_func compare_value_numeric_a, compare_value_alpha_a;
229 static hsh_compare_func compare_value_numeric_d, compare_value_alpha_d;
230 static hsh_compare_func compare_freq_numeric_a, compare_freq_alpha_a;
231 static hsh_compare_func compare_freq_numeric_d, compare_freq_alpha_d;
233 /* Parser and outline. */
235 static int internal_cmd_frequencies (void);
238 cmd_frequencies (void)
242 int_pool = pool_create ();
243 result = internal_cmd_frequencies ();
244 pool_destroy (int_pool);
246 pool_destroy (gen_pool);
254 internal_cmd_frequencies (void)
264 if (!parse_frequencies (&cmd))
267 if (cmd.onepage_limit == NOT_LONG)
268 cmd.onepage_limit = 50;
270 /* Figure out statistics to calculate. */
272 if (cmd.a_statistics[FRQ_ST_DEFAULT] || !cmd.sbc_statistics)
273 stats |= frq_default;
274 if (cmd.a_statistics[FRQ_ST_ALL])
276 if (cmd.sort != FRQ_AVALUE && cmd.sort != FRQ_DVALUE)
277 stats &= ~frq_median;
278 for (i = 0; i < frq_n_stats; i++)
279 if (cmd.a_statistics[st_name[i].st_indx])
280 stats |= BIT_INDEX (i);
281 if (stats & frq_kurt)
283 if (stats & frq_skew)
286 /* Calculate n_stats. */
288 for (i = 0; i < frq_n_stats; i++)
289 if ((stats & BIT_INDEX (i)))
294 if (chart != GFT_NONE || cmd.sbc_ntiles)
295 cmd.sort = FRQ_AVALUE;
297 /* Work out what percentiles need to be calculated */
298 if ( cmd.sbc_percentiles )
300 for ( i = 0 ; i < MAXLISTS ; ++i )
303 subc_list_double *ptl_list = &cmd.dl_percentiles[i];
304 for ( pl = 0 ; pl < subc_list_double_count(ptl_list); ++pl)
305 add_percentile(subc_list_double_at(ptl_list,pl) / 100.0 );
308 if ( cmd.sbc_ntiles )
310 for ( i = 0 ; i < cmd.sbc_ntiles ; ++i )
313 for (j = 0; j <= cmd.n_ntiles[i]; ++j )
314 add_percentile(j / (double) cmd.n_ntiles[i]);
320 procedure_with_splits (precalc, calc, postcalc, NULL);
322 free_frequencies(&cmd);
327 /* Figure out which charts the user requested. */
329 determine_charts (void)
331 int count = (!!cmd.sbc_histogram) + (!!cmd.sbc_barchart) +
332 (!!cmd.sbc_hbar) + (!!cmd.sbc_piechart);
342 msg (SW, _("At most one of BARCHART, HISTOGRAM, or HBAR should be "
343 "given. HBAR will be assumed. Argument values will be "
344 "given precedence increasing along the order given."));
346 else if (cmd.sbc_histogram)
348 else if (cmd.sbc_barchart)
350 else if (cmd.sbc_piechart)
361 if (cmd.sbc_barchart)
363 if (cmd.ba_min != SYSMIS)
365 if (cmd.ba_max != SYSMIS)
367 if (cmd.ba_scale == FRQ_FREQ)
372 else if (cmd.ba_scale == FRQ_PERCENT)
374 format = FRQ_PERCENT;
379 if (cmd.sbc_histogram)
381 if (cmd.hi_min != SYSMIS)
383 if (cmd.hi_max != SYSMIS)
385 if (cmd.hi_scale == FRQ_FREQ)
390 else if (cmd.hi_scale == FRQ_PERCENT)
392 format = FRQ_PERCENT;
395 if (cmd.hi_norm != FRQ_NONORMAL )
397 if (cmd.hi_incr == FRQ_INCREMENT)
403 if (cmd.hb_min != SYSMIS)
405 if (cmd.hb_max != SYSMIS)
407 if (cmd.hb_scale == FRQ_FREQ)
412 else if (cmd.hb_scale == FRQ_PERCENT)
414 format = FRQ_PERCENT;
419 if (cmd.hb_incr == FRQ_INCREMENT)
423 if (min != SYSMIS && max != SYSMIS && min >= max)
425 msg (SE, _("MAX must be greater than or equal to MIN, if both are "
426 "specified. However, MIN was specified as %g and MAX as %g. "
427 "MIN and MAX will be ignored."), min, max);
432 /* Add data from case C to the frequency table. */
434 calc (struct ccase *c, void *aux UNUSED)
440 weight = dict_get_case_weight (default_dict, c, &bad_warn);
442 for (i = 0; i < n_variables; i++)
444 struct variable *v = v_variables[i];
445 const union value *val = case_data (c, v->fv);
446 struct freq_tab *ft = &get_var_freqs (v)->tab;
454 struct freq **fpp = (struct freq **) hsh_probe (ft->data, val);
460 struct freq *fp = *fpp = pool_alloc (gen_pool, sizeof *fp);
468 if (val->f == SYSMIS)
469 ft->sysmis += weight;
470 else if (val->f > INT_MIN+1 && val->f < INT_MAX-1)
473 if (i >= ft->min && i <= ft->max)
474 ft->vector[i - ft->min] += weight;
477 ft->out_of_range += weight;
486 /* Prepares each variable that is the target of FREQUENCIES by setting
487 up its hash table. */
489 precalc (void *aux UNUSED)
493 pool_destroy (gen_pool);
494 gen_pool = pool_create ();
496 for (i = 0; i < n_variables; i++)
498 struct variable *v = v_variables[i];
499 struct freq_tab *ft = &get_var_freqs (v)->tab;
501 if (ft->mode == FRQM_GENERAL)
504 hsh_compare_func *compare;
506 if (v->type == NUMERIC)
508 hash = hash_value_numeric;
509 compare = compare_value_numeric_a;
513 hash = hash_value_alpha;
514 compare = compare_value_alpha_a;
516 ft->data = hsh_create (16, compare, hash, NULL, v);
522 for (j = (ft->max - ft->min); j >= 0; j--)
524 ft->out_of_range = 0.0;
530 /* Finishes up with the variables after frequencies have been
531 calculated. Displays statistics, percentiles, ... */
533 postcalc (void *aux UNUSED)
537 for (i = 0; i < n_variables; i++)
539 struct variable *v = v_variables[i];
540 struct var_freqs *vf = get_var_freqs (v);
541 struct freq_tab *ft = &vf->tab;
543 int dumped_freq_tab = 1;
545 postprocess_freq_tab (v);
547 /* Frequencies tables. */
548 n_categories = ft->n_valid + ft->n_missing;
549 if (cmd.table == FRQ_TABLE
550 || (cmd.table == FRQ_LIMIT && n_categories <= cmd.limit))
560 if (n_categories > cmd.onepage_limit)
573 dump_statistics (v, !dumped_freq_tab);
576 if ( chart == GFT_HIST)
579 double d[frq_n_stats];
581 struct normal_curve norm;
582 norm.N = vf->tab.total_cases;
585 norm.mean = d[frq_mean];
586 norm.stddev = d[frq_stddev];
588 chart_initialise(&ch);
589 draw_histogram(&ch, v_variables[i], ft, "HISTOGRAM",&norm,normal);
594 if ( chart == GFT_PIE)
598 chart_initialise(&ch);
600 draw_piechart(&ch, v_variables[i], ft);
606 cleanup_freq_tab (v);
611 /* Returns the comparison function that should be used for
612 sorting a frequency table by FRQ_SORT using VAR_TYPE
614 static hsh_compare_func *
615 get_freq_comparator (int frq_sort, int var_type)
617 /* Note that q2c generates tags beginning with 1000. */
618 switch (frq_sort | (var_type << 16))
620 case FRQ_AVALUE | (NUMERIC << 16): return compare_value_numeric_a;
621 case FRQ_AVALUE | (ALPHA << 16): return compare_value_alpha_a;
622 case FRQ_DVALUE | (NUMERIC << 16): return compare_value_numeric_d;
623 case FRQ_DVALUE | (ALPHA << 16): return compare_value_alpha_d;
624 case FRQ_AFREQ | (NUMERIC << 16): return compare_freq_numeric_a;
625 case FRQ_AFREQ | (ALPHA << 16): return compare_freq_alpha_a;
626 case FRQ_DFREQ | (NUMERIC << 16): return compare_freq_numeric_d;
627 case FRQ_DFREQ | (ALPHA << 16): return compare_freq_alpha_d;
634 /* Returns nonzero iff the value in struct freq F is non-missing
637 not_missing (const void *f_, void *v_)
639 const struct freq *f = f_;
640 struct variable *v = v_;
642 return !is_missing (&f->v, v);
645 /* Summarizes the frequency table data for variable V. */
647 postprocess_freq_tab (struct variable *v)
649 hsh_compare_func *compare;
653 struct freq *freqs, *f;
656 ft = &get_var_freqs (v)->tab;
657 assert (ft->mode == FRQM_GENERAL);
658 compare = get_freq_comparator (cmd.sort, v->type);
660 /* Extract data from hash table. */
661 count = hsh_count (ft->data);
662 data = hsh_data (ft->data);
664 /* Copy dereferenced data into freqs. */
665 freqs = xmalloc (count * sizeof *freqs);
666 for (i = 0; i < count; i++)
668 struct freq *f = data[i];
672 /* Put data into ft. */
674 ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, v);
675 ft->missing = freqs + ft->n_valid;
676 ft->n_missing = count - ft->n_valid;
679 sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare, v);
680 sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare, v);
682 /* Summary statistics. */
683 ft->valid_cases = 0.0;
684 for(i = 0 ; i < ft->n_valid ; ++i )
687 ft->valid_cases += f->c;
691 ft->total_cases = ft->valid_cases ;
692 for(i = 0 ; i < ft->n_missing ; ++i )
695 ft->total_cases += f->c;
700 /* Frees the frequency table for variable V. */
702 cleanup_freq_tab (struct variable *v)
704 struct freq_tab *ft = &get_var_freqs (v)->tab;
705 assert (ft->mode == FRQM_GENERAL);
707 hsh_destroy (ft->data);
710 /* Parses the VARIABLES subcommand, adding to
711 {n_variables,v_variables}. */
713 frq_custom_variables (struct cmd_frequencies *cmd UNUSED)
716 int min = 0, max = 0;
718 int old_n_variables = n_variables;
722 if (token != T_ALL && (token != T_ID
723 || dict_lookup_var (default_dict, tokid) == NULL))
726 if (!parse_variables (default_dict, &v_variables, &n_variables,
727 PV_APPEND | PV_NO_SCRATCH))
730 if (!lex_match ('('))
735 if (!lex_force_int ())
737 min = lex_integer ();
739 if (!lex_force_match (','))
741 if (!lex_force_int ())
743 max = lex_integer ();
745 if (!lex_force_match (')'))
749 msg (SE, _("Upper limit of integer mode value range must be "
750 "greater than lower limit."));
755 for (i = old_n_variables; i < n_variables; i++)
757 struct variable *v = v_variables[i];
758 struct var_freqs *vf;
762 msg (SE, _("Variable %s specified multiple times on VARIABLES "
763 "subcommand."), v->name);
766 if (mode == FRQM_INTEGER && v->type != NUMERIC)
768 msg (SE, _("Integer mode specified, but %s is not a numeric "
769 "variable."), v->name);
773 vf = var_attach_aux (v, xmalloc (sizeof *vf), var_dtor_free);
775 vf->tab.valid = vf->tab.missing = NULL;
776 if (mode == FRQM_INTEGER)
780 vf->tab.vector = pool_alloc (int_pool,
781 sizeof (struct freq) * (max - min + 1));
784 vf->tab.vector = NULL;
791 /* Parses the GROUPED subcommand, setting the n_grouped, grouped
792 fields of specified variables. */
794 frq_custom_grouped (struct cmd_frequencies *cmd UNUSED)
797 if ((token == T_ID && dict_lookup_var (default_dict, tokid) != NULL)
803 /* Max, current size of list; list itself. */
811 if (!parse_variables (default_dict, &v, &n,
812 PV_NO_DUPLICATE | PV_NUMERIC))
818 while (token == T_NUM)
823 dl = pool_realloc (int_pool, dl, ml * sizeof (double));
829 /* Note that nl might still be 0 and dl might still be
830 NULL. That's okay. */
831 if (!lex_match (')'))
834 msg (SE, _("`)' expected after GROUPED interval list."));
844 for (i = 0; i < n; i++)
845 if (v[i]->aux == NULL)
846 msg (SE, _("Variables %s specified on GROUPED but not on "
847 "VARIABLES."), v[i]->name);
850 struct var_freqs *vf = get_var_freqs (v[i]);
852 if (vf->groups != NULL)
853 msg (SE, _("Variables %s specified multiple times on GROUPED "
854 "subcommand."), v[i]->name);
862 if (!lex_match ('/'))
864 if ((token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
875 /* Adds X to the list of percentiles, keeping the list in proper
878 add_percentile (double x)
882 for (i = 0; i < n_percentiles; i++)
884 /* Do nothing if it's already in the list */
885 if ( fabs(x - percentiles[i].p) < DBL_EPSILON )
888 if (x < percentiles[i].p)
892 if (i >= n_percentiles || tokval != percentiles[i].p)
895 = pool_realloc (int_pool, percentiles,
896 (n_percentiles + 1) * sizeof (struct percentile ));
898 if (i < n_percentiles)
899 memmove (&percentiles[i + 1], &percentiles[i],
900 (n_percentiles - i) * sizeof (struct percentile) );
902 percentiles[i].p = x;
907 /* Comparison functions. */
909 /* Hash of numeric values. */
911 hash_value_numeric (const void *value_, void *foo UNUSED)
913 const struct freq *value = value_;
914 return hsh_hash_double (value->v.f);
917 /* Hash of string values. */
919 hash_value_alpha (const void *value_, void *v_)
921 const struct freq *value = value_;
922 struct variable *v = v_;
924 return hsh_hash_bytes (value->v.s, v->width);
927 /* Ascending numeric compare of values. */
929 compare_value_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
931 const struct freq *a = a_;
932 const struct freq *b = b_;
936 else if (a->v.f < b->v.f)
942 /* Ascending string compare of values. */
944 compare_value_alpha_a (const void *a_, const void *b_, void *v_)
946 const struct freq *a = a_;
947 const struct freq *b = b_;
948 const struct variable *v = v_;
950 return memcmp (a->v.s, b->v.s, v->width);
953 /* Descending numeric compare of values. */
955 compare_value_numeric_d (const void *a, const void *b, void *foo UNUSED)
957 return -compare_value_numeric_a (a, b, foo);
960 /* Descending string compare of values. */
962 compare_value_alpha_d (const void *a, const void *b, void *v)
964 return -compare_value_alpha_a (a, b, v);
967 /* Ascending numeric compare of frequency;
968 secondary key on ascending numeric value. */
970 compare_freq_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
972 const struct freq *a = a_;
973 const struct freq *b = b_;
977 else if (a->c < b->c)
982 else if (a->v.f < b->v.f)
988 /* Ascending numeric compare of frequency;
989 secondary key on ascending string value. */
991 compare_freq_alpha_a (const void *a_, const void *b_, void *v_)
993 const struct freq *a = a_;
994 const struct freq *b = b_;
995 const struct variable *v = v_;
999 else if (a->c < b->c)
1002 return memcmp (a->v.s, b->v.s, v->width);
1005 /* Descending numeric compare of frequency;
1006 secondary key on ascending numeric value. */
1008 compare_freq_numeric_d (const void *a_, const void *b_, void *foo UNUSED)
1010 const struct freq *a = a_;
1011 const struct freq *b = b_;
1015 else if (a->c < b->c)
1018 if (a->v.f > b->v.f)
1020 else if (a->v.f < b->v.f)
1026 /* Descending numeric compare of frequency;
1027 secondary key on ascending string value. */
1029 compare_freq_alpha_d (const void *a_, const void *b_, void *v_)
1031 const struct freq *a = a_;
1032 const struct freq *b = b_;
1033 const struct variable *v = v_;
1037 else if (a->c < b->c)
1040 return memcmp (a->v.s, b->v.s, v->width);
1043 /* Frequency table display. */
1045 /* Sets the widths of all the columns and heights of all the rows in
1046 table T for driver D. */
1048 full_dim (struct tab_table *t, struct outp_driver *d)
1050 int lab = cmd.labels == FRQ_LABELS;
1054 t->w[0] = min (tab_natural_width (t, d, 0), d->prop_em_width * 15);
1055 for (i = lab; i < lab + 5; i++)
1056 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1057 for (i = 0; i < t->nr; i++)
1058 t->h[i] = d->font_height;
1061 /* Displays a full frequency table for variable V. */
1063 dump_full (struct variable *v)
1066 struct freq_tab *ft;
1068 struct tab_table *t;
1070 double cum_total = 0.0;
1071 double cum_freq = 0.0;
1081 static struct init vec[] =
1083 {4, 0, N_("Valid")},
1085 {1, 1, N_("Value")},
1086 {2, 1, N_("Frequency")},
1087 {3, 1, N_("Percent")},
1088 {4, 1, N_("Percent")},
1089 {5, 1, N_("Percent")},
1097 int lab = cmd.labels == FRQ_LABELS;
1099 ft = &get_var_freqs (v)->tab;
1100 n_categories = ft->n_valid + ft->n_missing;
1101 t = tab_create (5 + lab, n_categories + 3, 0);
1102 tab_headers (t, 0, 0, 2, 0);
1103 tab_dim (t, full_dim);
1106 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value Label"));
1107 for (p = vec; p->s; p++)
1108 tab_text (t, p->c - (p->r ? !lab : 0), p->r,
1109 TAB_CENTER | TAT_TITLE, gettext (p->s));
1112 for (f = ft->valid; f < ft->missing; f++)
1114 double percent, valid_percent;
1118 percent = f->c / ft->total_cases * 100.0;
1119 valid_percent = f->c / ft->valid_cases * 100.0;
1120 cum_total += valid_percent;
1124 const char *label = val_labs_find (v->val_labs, f->v);
1126 tab_text (t, 0, r, TAB_LEFT, label);
1129 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1130 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1131 tab_float (t, 2 + lab, r, TAB_NONE, percent, 5, 1);
1132 tab_float (t, 3 + lab, r, TAB_NONE, valid_percent, 5, 1);
1133 tab_float (t, 4 + lab, r, TAB_NONE, cum_total, 5, 1);
1136 for (; f < &ft->valid[n_categories]; f++)
1142 const char *label = val_labs_find (v->val_labs, f->v);
1144 tab_text (t, 0, r, TAB_LEFT, label);
1147 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1148 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1149 tab_float (t, 2 + lab, r, TAB_NONE,
1150 f->c / ft->total_cases * 100.0, 5, 1);
1151 tab_text (t, 3 + lab, r, TAB_NONE, _("Missing"));
1155 tab_box (t, TAL_1, TAL_1,
1156 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1158 tab_hline (t, TAL_2, 0, 4 + lab, 2);
1159 tab_hline (t, TAL_2, 0, 4 + lab, r);
1160 tab_joint_text (t, 0, r, 0 + lab, r, TAB_RIGHT | TAT_TITLE, _("Total"));
1161 tab_vline (t, TAL_0, 1, r, r);
1162 tab_float (t, 1 + lab, r, TAB_NONE, cum_freq, 8, 0);
1163 tab_float (t, 2 + lab, r, TAB_NONE, 100.0, 5, 1);
1164 tab_float (t, 3 + lab, r, TAB_NONE, 100.0, 5, 1);
1166 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1171 /* Sets the widths of all the columns and heights of all the rows in
1172 table T for driver D. */
1174 condensed_dim (struct tab_table *t, struct outp_driver *d)
1176 int cum_w = max (outp_string_width (d, _("Cum")),
1177 max (outp_string_width (d, _("Cum")),
1178 outp_string_width (d, "000")));
1182 for (i = 0; i < 2; i++)
1183 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1184 for (i = 2; i < 4; i++)
1186 for (i = 0; i < t->nr; i++)
1187 t->h[i] = d->font_height;
1190 /* Display condensed frequency table for variable V. */
1192 dump_condensed (struct variable *v)
1195 struct freq_tab *ft;
1197 struct tab_table *t;
1199 double cum_total = 0.0;
1201 ft = &get_var_freqs (v)->tab;
1202 n_categories = ft->n_valid + ft->n_missing;
1203 t = tab_create (4, n_categories + 2, 0);
1205 tab_headers (t, 0, 0, 2, 0);
1206 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value"));
1207 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Freq"));
1208 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1209 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Cum"));
1210 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1211 tab_dim (t, condensed_dim);
1214 for (f = ft->valid; f < ft->missing; f++)
1218 percent = f->c / ft->total_cases * 100.0;
1219 cum_total += f->c / ft->valid_cases * 100.0;
1221 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1222 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1223 tab_float (t, 2, r, TAB_NONE, percent, 3, 0);
1224 tab_float (t, 3, r, TAB_NONE, cum_total, 3, 0);
1227 for (; f < &ft->valid[n_categories]; f++)
1229 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1230 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1231 tab_float (t, 2, r, TAB_NONE,
1232 f->c / ft->total_cases * 100.0, 3, 0);
1236 tab_box (t, TAL_1, TAL_1,
1237 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1239 tab_hline (t, TAL_2, 0, 3, 2);
1240 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1241 tab_columns (t, SOM_COL_DOWN, 1);
1245 /* Statistical display. */
1247 /* Calculates all the pertinent statistics for variable V, putting
1248 them in array D[]. FIXME: This could be made much more optimal. */
1250 calc_stats (struct variable *v, double d[frq_n_stats])
1252 struct freq_tab *ft = &get_var_freqs (v)->tab;
1253 double W = ft->valid_cases;
1262 double *median_value;
1264 /* Calculate percentiles. */
1266 /* If the 50th percentile was not explicitly requested then we must
1267 calculate it anyway --- it's the median */
1269 for (i = 0; i < n_percentiles; i++)
1271 if (percentiles[i].p == 0.5)
1273 median_value = &percentiles[i].value;
1278 if ( 0 == median_value )
1280 add_percentile (0.5);
1284 for (i = 0; i < n_percentiles; i++)
1286 percentiles[i].flag = 0;
1287 percentiles[i].flag2 = 0;
1291 for (idx = 0; idx < ft->n_valid; ++idx)
1293 static double prev_value = SYSMIS;
1294 f = &ft->valid[idx];
1296 for (i = 0; i < n_percentiles; i++)
1299 if ( percentiles[i].flag2 ) continue ;
1301 if ( get_algorithm() != COMPATIBLE )
1303 (ft->valid_cases - 1) * percentiles[i].p;
1306 (ft->valid_cases + 1) * percentiles[i].p - 1;
1308 if ( percentiles[i].flag )
1310 percentiles[i].x2 = f->v.f;
1311 percentiles[i].x1 = prev_value;
1312 percentiles[i].flag2 = 1;
1318 if ( f->c > 1 && rank - (f->c - 1) > tp )
1320 percentiles[i].x2 = percentiles[i].x1 = f->v.f;
1321 percentiles[i].flag2 = 1;
1325 percentiles[i].flag=1;
1331 prev_value = f->v.f;
1334 for (i = 0; i < n_percentiles; i++)
1336 /* Catches the case when p == 100% */
1337 if ( ! percentiles[i].flag2 )
1338 percentiles[i].x1 = percentiles[i].x2 = f->v.f;
1341 printf("percentile %d (p==%.2f); X1 = %g; X2 = %g\n",
1342 i,percentiles[i].p,percentiles[i].x1,percentiles[i].x2);
1346 for (i = 0; i < n_percentiles; i++)
1348 struct freq_tab *ft = &get_var_freqs (v)->tab;
1352 if ( get_algorithm() != COMPATIBLE )
1354 s = modf((ft->valid_cases - 1) * percentiles[i].p , &dummy);
1358 s = modf((ft->valid_cases + 1) * percentiles[i].p -1, &dummy);
1361 percentiles[i].value = percentiles[i].x1 +
1362 ( percentiles[i].x2 - percentiles[i].x1) * s ;
1364 if ( percentiles[i].p == 0.50)
1365 median_value = &percentiles[i].value;
1369 /* Calculate the mode. */
1372 for (f = ft->valid; f < ft->missing; f++)
1374 if (most_often < f->c)
1379 else if (most_often == f->c)
1381 /* A duplicate mode is undefined.
1382 FIXME: keep track of *all* the modes. */
1387 /* Calculate moments. */
1388 m = moments_create (MOMENT_KURTOSIS);
1389 for (f = ft->valid; f < ft->missing; f++)
1390 moments_pass_one (m, f->v.f, f->c);
1391 for (f = ft->valid; f < ft->missing; f++)
1392 moments_pass_two (m, f->v.f, f->c);
1393 moments_calculate (m, NULL, &d[frq_mean], &d[frq_variance],
1394 &d[frq_skew], &d[frq_kurt]);
1395 moments_destroy (m);
1397 /* Formulas below are taken from _SPSS Statistical Algorithms_. */
1398 d[frq_min] = ft->valid[0].v.f;
1399 d[frq_max] = ft->valid[ft->n_valid - 1].v.f;
1400 d[frq_mode] = X_mode;
1401 d[frq_range] = d[frq_max] - d[frq_min];
1402 d[frq_median] = *median_value;
1403 d[frq_sum] = d[frq_mean] * W;
1404 d[frq_stddev] = sqrt (d[frq_variance]);
1405 d[frq_semean] = d[frq_stddev] / sqrt (W);
1406 d[frq_seskew] = calc_seskew (W);
1407 d[frq_sekurt] = calc_sekurt (W);
1410 /* Displays a table of all the statistics requested for variable V. */
1412 dump_statistics (struct variable *v, int show_varname)
1414 struct freq_tab *ft;
1415 double stat_value[frq_n_stats];
1416 struct tab_table *t;
1419 int n_explicit_percentiles = n_percentiles;
1421 if ( implicit_50th && n_percentiles > 0 )
1424 if (v->type == ALPHA)
1426 ft = &get_var_freqs (v)->tab;
1427 if (ft->n_valid == 0)
1429 msg (SW, _("No valid data for variable %s; statistics not displayed."),
1433 calc_stats (v, stat_value);
1435 t = tab_create (3, n_stats + n_explicit_percentiles + 2, 0);
1436 tab_dim (t, tab_natural_dimensions);
1438 tab_box (t, TAL_1, TAL_1, -1, -1 , 0 , 0 , 2, tab_nr(t) - 1) ;
1441 tab_vline (t, TAL_1 , 2, 0, tab_nr(t) - 1);
1442 tab_vline (t, TAL_1 | TAL_SPACING , 1, 0, tab_nr(t) - 1 ) ;
1444 r=2; /* N missing and N valid are always dumped */
1446 for (i = 0; i < frq_n_stats; i++)
1447 if (stats & BIT_INDEX (i))
1449 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
1450 gettext (st_name[i].s10));
1451 tab_float (t, 2, r, TAB_NONE, stat_value[i], 11, 3);
1455 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("N"));
1456 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Valid"));
1457 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Missing"));
1459 tab_float(t, 2, 0, TAB_NONE, ft->valid_cases, 11, 0);
1460 tab_float(t, 2, 1, TAB_NONE, ft->total_cases - ft->valid_cases, 11, 0);
1463 for (i = 0; i < n_explicit_percentiles; i++, r++)
1467 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Percentiles"));
1470 tab_float (t, 1, r, TAB_LEFT, percentiles[i].p * 100, 3, 0 );
1471 tab_float (t, 2, r, TAB_NONE, percentiles[i].value, 11, 3);
1475 tab_columns (t, SOM_COL_DOWN, 1);
1479 tab_title (t, 1, "%s: %s", v->name, v->label);
1481 tab_title (t, 0, v->name);
1484 tab_flags (t, SOMF_NO_TITLE);