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"
39 #include "algorithm.h"
46 #include "value-labels.h"
52 #include "debug-print.h"
57 format=cond:condense/onepage(*n:onepage_limit,"%s>=0")/!standard,
58 table:limit(n:limit,"%s>0")/notable/!table,
59 labels:!labels/nolabels,
60 sort:!avalue/dvalue/afreq/dfreq,
61 spaces:!single/double,
62 paging:newpage/!oldpage;
63 missing=miss:include/!exclude;
64 barchart(ba_)=:minimum(d:min),
66 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0");
67 piechart(pie_)=:minimum(d:min),
69 missing:missing/!nomissing;
70 histogram(hi_)=: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");
75 hbar(hb_)=:minimum(d:min),
77 scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
78 norm:!nonormal/normal,
79 incr:increment(d:inc,"%s>0");
82 +percentiles = double list;
83 statistics[st_]=1|mean,2|semean,3|median,4|mode,5|stddev,6|variance,
84 7|kurtosis,8|skewness,9|range,10|minimum,11|maximum,12|sum,
85 13|default,14|seskewness,15|sekurtosis,all,none.
90 /* Description of a statistic. */
93 int st_indx; /* Index into a_statistics[]. */
94 const char *s10; /* Identifying string. */
97 /* Table of statistics, indexed by dsc_*. */
98 static struct frq_info st_name[frq_n_stats + 1] =
100 {FRQ_ST_MEAN, N_("Mean")},
101 {FRQ_ST_SEMEAN, N_("S.E. Mean")},
102 {FRQ_ST_MEDIAN, N_("Median")},
103 {FRQ_ST_MODE, N_("Mode")},
104 {FRQ_ST_STDDEV, N_("Std Dev")},
105 {FRQ_ST_VARIANCE, N_("Variance")},
106 {FRQ_ST_KURTOSIS, N_("Kurtosis")},
107 {FRQ_ST_SEKURTOSIS, N_("S.E. Kurt")},
108 {FRQ_ST_SKEWNESS, N_("Skewness")},
109 {FRQ_ST_SESKEWNESS, N_("S.E. Skew")},
110 {FRQ_ST_RANGE, N_("Range")},
111 {FRQ_ST_MINIMUM, N_("Minimum")},
112 {FRQ_ST_MAXIMUM, N_("Maximum")},
113 {FRQ_ST_SUM, N_("Sum")},
117 /* Percentiles to calculate. */
121 double p; /* the %ile to be calculated */
122 double value; /* the %ile's value */
123 double x1; /* The datum value <= the percentile */
124 double x2; /* The datum value >= the percentile */
126 int flag2; /* Set to 1 if this percentile value has been found */
130 static void add_percentile (double x) ;
133 static struct percentile *percentiles;
134 static int n_percentiles;
136 static int implicit_50th ;
138 /* Groups of statistics. */
140 #define frq_default \
141 (BI (frq_mean) | BI (frq_stddev) | BI (frq_min) | BI (frq_max))
143 (BI (frq_sum) | BI(frq_min) | BI(frq_max) \
144 | BI(frq_mean) | BI(frq_semean) | BI(frq_stddev) \
145 | BI(frq_variance) | BI(frq_kurt) | BI(frq_sekurt) \
146 | BI(frq_skew) | BI(frq_seskew) | BI(frq_range) \
147 | BI(frq_range) | BI(frq_mode) | BI(frq_median))
149 /* Statistics; number of statistics. */
150 static unsigned long stats;
153 /* Types of graphs. */
156 GFT_NONE, /* Don't draw graphs. */
157 GFT_BAR, /* Draw bar charts. */
158 GFT_HIST, /* Draw histograms. */
159 GFT_PIE, /* Draw piechart */
160 GFT_HBAR /* Draw bar charts or histograms at our discretion. */
163 /* Parsed command. */
164 static struct cmd_frequencies cmd;
166 /* Summary of the barchart, histogram, and hbar subcommands. */
167 /* FIXME: These should not be mututally exclusive */
168 static int chart; /* NONE/BAR/HIST/HBAR/PIE. */
169 static double min, max; /* Minimum, maximum on y axis. */
170 static int format; /* FREQ/PERCENT: Scaling of y axis. */
171 static double scale, incr; /* FIXME */
172 static int normal; /* FIXME */
174 /* Variables for which to calculate statistics. */
175 static int n_variables;
176 static struct variable **v_variables;
178 /* Arenas used to store semi-permanent storage. */
179 static struct pool *int_pool; /* Integer mode. */
180 static struct pool *gen_pool; /* General mode. */
182 /* Easier access to a_statistics. */
183 #define stat cmd.a_statistics
185 static void determine_charts (void);
187 static void calc_stats (struct variable * v, double d[frq_n_stats]);
189 static void precalc (void *);
190 static int calc (struct ccase *, void *);
191 static void postcalc (void *);
193 static void postprocess_freq_tab (struct variable *);
194 static void dump_full (struct variable *);
195 static void dump_condensed (struct variable *);
196 static void dump_statistics (struct variable *, int show_varname);
197 static void cleanup_freq_tab (struct variable *);
199 static hsh_hash_func hash_value_numeric, hash_value_alpha;
200 static hsh_compare_func compare_value_numeric_a, compare_value_alpha_a;
201 static hsh_compare_func compare_value_numeric_d, compare_value_alpha_d;
202 static hsh_compare_func compare_freq_numeric_a, compare_freq_alpha_a;
203 static hsh_compare_func compare_freq_numeric_d, compare_freq_alpha_d;
205 /* Parser and outline. */
207 static int internal_cmd_frequencies (void);
210 cmd_frequencies (void)
214 int_pool = pool_create ();
215 result = internal_cmd_frequencies ();
216 pool_destroy (int_pool);
218 pool_destroy (gen_pool);
226 internal_cmd_frequencies (void)
236 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
237 dict_get_var(default_dict, i)->p.frq.used = 0;
239 if (!parse_frequencies (&cmd))
242 if (cmd.onepage_limit == NOT_LONG)
243 cmd.onepage_limit = 50;
245 /* Figure out statistics to calculate. */
247 if (stat[FRQ_ST_DEFAULT] || !cmd.sbc_statistics)
248 stats |= frq_default;
249 if (stat[FRQ_ST_ALL])
251 if (cmd.sort != FRQ_AVALUE && cmd.sort != FRQ_DVALUE)
252 stats &= ~frq_median;
253 for (i = 0; i < frq_n_stats; i++)
254 if (stat[st_name[i].st_indx])
255 stats |= BIT_INDEX (i);
256 if (stats & frq_kurt)
258 if (stats & frq_skew)
261 /* Calculate n_stats. */
263 for (i = 0; i < frq_n_stats; i++)
264 if ((stats & BIT_INDEX (i)))
269 if (chart != GFT_NONE || cmd.sbc_ntiles)
270 cmd.sort = FRQ_AVALUE;
272 /* Work out what percentiles need to be calculated */
273 if ( cmd.sbc_percentiles )
275 for ( i = 0 ; i < MAXLISTS ; ++i )
278 subc_list_double *ptl_list = &cmd.dl_percentiles[i];
279 for ( pl = 0 ; pl < subc_list_double_count(ptl_list); ++pl)
280 add_percentile(subc_list_double_at(ptl_list,pl) / 100.0 );
283 if ( cmd.sbc_ntiles )
285 for ( i = 0 ; i < cmd.sbc_ntiles ; ++i )
288 for (j = 0; j <= cmd.n_ntiles[i]; ++j )
289 add_percentile(j / (double) cmd.n_ntiles[i]);
295 procedure_with_splits (precalc, calc, postcalc, NULL);
300 /* Figure out which charts the user requested. */
302 determine_charts (void)
304 int count = (!!cmd.sbc_histogram) + (!!cmd.sbc_barchart) +
305 (!!cmd.sbc_hbar) + (!!cmd.sbc_piechart);
315 msg (SW, _("At most one of BARCHART, HISTOGRAM, or HBAR should be "
316 "given. HBAR will be assumed. Argument values will be "
317 "given precedence increasing along the order given."));
319 else if (cmd.sbc_histogram)
321 else if (cmd.sbc_barchart)
323 else if (cmd.sbc_piechart)
334 if (cmd.sbc_barchart)
336 if (cmd.ba_min != SYSMIS)
338 if (cmd.ba_max != SYSMIS)
340 if (cmd.ba_scale == FRQ_FREQ)
345 else if (cmd.ba_scale == FRQ_PERCENT)
347 format = FRQ_PERCENT;
352 if (cmd.sbc_histogram)
354 if (cmd.hi_min != SYSMIS)
356 if (cmd.hi_max != SYSMIS)
358 if (cmd.hi_scale == FRQ_FREQ)
363 else if (cmd.hi_scale == FRQ_PERCENT)
365 format = FRQ_PERCENT;
368 if (cmd.hi_norm != FRQ_NONORMAL )
370 if (cmd.hi_incr == FRQ_INCREMENT)
376 if (cmd.hb_min != SYSMIS)
378 if (cmd.hb_max != SYSMIS)
380 if (cmd.hb_scale == FRQ_FREQ)
385 else if (cmd.hb_scale == FRQ_PERCENT)
387 format = FRQ_PERCENT;
392 if (cmd.hb_incr == FRQ_INCREMENT)
396 if (min != SYSMIS && max != SYSMIS && min >= max)
398 msg (SE, _("MAX must be greater than or equal to MIN, if both are "
399 "specified. However, MIN was specified as %g and MAX as %g. "
400 "MIN and MAX will be ignored."), min, max);
405 /* Add data from case C to the frequency table. */
407 calc (struct ccase *c, void *aux UNUSED)
413 weight = dict_get_case_weight (default_dict, c, &bad_warn);
415 for (i = 0; i < n_variables; i++)
417 struct variable *v = v_variables[i];
418 const union value *val = case_data (c, v->fv);
419 struct freq_tab *ft = &v->p.frq.tab;
421 switch (v->p.frq.tab.mode)
427 struct freq **fpp = (struct freq **) hsh_probe (ft->data, val);
433 struct freq *fp = *fpp = pool_alloc (gen_pool, sizeof *fp);
441 if (val->f == SYSMIS)
442 v->p.frq.tab.sysmis += weight;
443 else if (val->f > INT_MIN+1 && val->f < INT_MAX-1)
446 if (i >= v->p.frq.tab.min && i <= v->p.frq.tab.max)
447 v->p.frq.tab.vector[i - v->p.frq.tab.min] += weight;
450 v->p.frq.tab.out_of_range += weight;
459 /* Prepares each variable that is the target of FREQUENCIES by setting
460 up its hash table. */
462 precalc (void *aux UNUSED)
466 pool_destroy (gen_pool);
467 gen_pool = pool_create ();
469 for (i = 0; i < n_variables; i++)
471 struct variable *v = v_variables[i];
473 if (v->p.frq.tab.mode == FRQM_GENERAL)
476 hsh_compare_func *compare;
478 if (v->type == NUMERIC)
480 hash = hash_value_numeric;
481 compare = compare_value_numeric_a;
485 hash = hash_value_alpha;
486 compare = compare_value_alpha_a;
488 v->p.frq.tab.data = hsh_create (16, compare, hash, NULL, v);
494 for (j = (v->p.frq.tab.max - v->p.frq.tab.min); j >= 0; j--)
495 v->p.frq.tab.vector[j] = 0.0;
496 v->p.frq.tab.out_of_range = 0.0;
497 v->p.frq.tab.sysmis = 0.0;
502 /* Finishes up with the variables after frequencies have been
503 calculated. Displays statistics, percentiles, ... */
505 postcalc (void *aux UNUSED)
509 for (i = 0; i < n_variables; i++)
511 struct variable *v = v_variables[i];
513 int dumped_freq_tab = 1;
515 postprocess_freq_tab (v);
517 /* Frequencies tables. */
518 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
519 if (cmd.table == FRQ_TABLE
520 || (cmd.table == FRQ_LIMIT && n_categories <= cmd.limit))
530 if (n_categories > cmd.onepage_limit)
543 dump_statistics (v, !dumped_freq_tab);
546 if ( chart == GFT_HIST)
549 double d[frq_n_stats];
550 struct frequencies_proc *frq = &v->p.frq;
552 struct normal_curve norm;
553 norm.N = frq->tab.total_cases ;
556 norm.mean = d[frq_mean];
557 norm.stddev = d[frq_stddev];
559 chart_initialise(&ch);
560 draw_histogram(&ch, v_variables[i], "HISTOGRAM",&norm,normal);
565 if ( chart == GFT_PIE)
569 chart_initialise(&ch);
571 draw_piechart(&ch, v_variables[i]);
577 cleanup_freq_tab (v);
582 /* Returns the comparison function that should be used for
583 sorting a frequency table by FRQ_SORT using VAR_TYPE
585 static hsh_compare_func *
586 get_freq_comparator (int frq_sort, int var_type)
588 /* Note that q2c generates tags beginning with 1000. */
589 switch (frq_sort | (var_type << 16))
591 case FRQ_AVALUE | (NUMERIC << 16): return compare_value_numeric_a;
592 case FRQ_AVALUE | (ALPHA << 16): return compare_value_alpha_a;
593 case FRQ_DVALUE | (NUMERIC << 16): return compare_value_numeric_d;
594 case FRQ_DVALUE | (ALPHA << 16): return compare_value_alpha_d;
595 case FRQ_AFREQ | (NUMERIC << 16): return compare_freq_numeric_a;
596 case FRQ_AFREQ | (ALPHA << 16): return compare_freq_alpha_a;
597 case FRQ_DFREQ | (NUMERIC << 16): return compare_freq_numeric_d;
598 case FRQ_DFREQ | (ALPHA << 16): return compare_freq_alpha_d;
605 /* Returns nonzero iff the value in struct freq F is non-missing
608 not_missing (const void *f_, void *v_)
610 const struct freq *f = f_;
611 struct variable *v = v_;
613 return !is_missing (&f->v, v);
616 /* Summarizes the frequency table data for variable V. */
618 postprocess_freq_tab (struct variable *v)
620 hsh_compare_func *compare;
624 struct freq *freqs, *f;
627 assert (v->p.frq.tab.mode == FRQM_GENERAL);
628 compare = get_freq_comparator (cmd.sort, v->type);
631 /* Extract data from hash table. */
632 count = hsh_count (ft->data);
633 data = hsh_data (ft->data);
635 /* Copy dereferenced data into freqs. */
636 freqs = xmalloc (count * sizeof *freqs);
637 for (i = 0; i < count; i++)
639 struct freq *f = data[i];
643 /* Put data into ft. */
645 ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, v);
646 ft->missing = freqs + ft->n_valid;
647 ft->n_missing = count - ft->n_valid;
650 sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare, v);
651 sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare, v);
653 /* Summary statistics. */
654 ft->valid_cases = 0.0;
655 for(i = 0 ; i < ft->n_valid ; ++i )
658 ft->valid_cases += f->c;
662 ft->total_cases = ft->valid_cases ;
663 for(i = 0 ; i < ft->n_missing ; ++i )
666 ft->total_cases += f->c;
671 /* Frees the frequency table for variable V. */
673 cleanup_freq_tab (struct variable *v)
675 assert (v->p.frq.tab.mode == FRQM_GENERAL);
676 free (v->p.frq.tab.valid);
677 hsh_destroy (v->p.frq.tab.data);
680 /* Parses the VARIABLES subcommand, adding to
681 {n_variables,v_variables}. */
683 frq_custom_variables (struct cmd_frequencies *cmd UNUSED)
686 int min = 0, max = 0;
688 int old_n_variables = n_variables;
692 if (token != T_ALL && (token != T_ID
693 || dict_lookup_var (default_dict, tokid) == NULL))
696 if (!parse_variables (default_dict, &v_variables, &n_variables,
697 PV_APPEND | PV_NO_SCRATCH))
700 for (i = old_n_variables; i < n_variables; i++)
701 v_variables[i]->p.frq.tab.mode = FRQM_GENERAL;
703 if (!lex_match ('('))
708 if (!lex_force_int ())
710 min = lex_integer ();
712 if (!lex_force_match (','))
714 if (!lex_force_int ())
716 max = lex_integer ();
718 if (!lex_force_match (')'))
722 msg (SE, _("Upper limit of integer mode value range must be "
723 "greater than lower limit."));
728 for (i = old_n_variables; i < n_variables; i++)
730 struct variable *v = v_variables[i];
732 if (v->p.frq.used != 0)
734 msg (SE, _("Variable %s specified multiple times on VARIABLES "
735 "subcommand."), v->name);
739 v->p.frq.used = 1; /* Used simply as a marker. */
741 v->p.frq.tab.valid = v->p.frq.tab.missing = NULL;
743 if (mode == FRQM_INTEGER)
745 if (v->type != NUMERIC)
747 msg (SE, _("Integer mode specified, but %s is not a numeric "
748 "variable."), v->name);
752 v->p.frq.tab.min = min;
753 v->p.frq.tab.max = max;
754 v->p.frq.tab.vector = pool_alloc (int_pool,
755 sizeof (struct freq) * (max - min + 1));
758 v->p.frq.tab.vector = NULL;
760 v->p.frq.n_groups = 0;
761 v->p.frq.groups = NULL;
766 /* Parses the GROUPED subcommand, setting the frq.{n_grouped,grouped}
767 fields of specified variables. */
769 frq_custom_grouped (struct cmd_frequencies *cmd UNUSED)
772 if ((token == T_ID && dict_lookup_var (default_dict, tokid) != NULL)
778 /* Max, current size of list; list itself. */
786 if (!parse_variables (default_dict, &v, &n,
787 PV_NO_DUPLICATE | PV_NUMERIC))
793 while (token == T_NUM)
798 dl = pool_realloc (int_pool, dl, ml * sizeof (double));
804 /* Note that nl might still be 0 and dl might still be
805 NULL. That's okay. */
806 if (!lex_match (')'))
809 msg (SE, _("`)' expected after GROUPED interval list."));
819 for (i = 0; i < n; i++)
821 if (v[i]->p.frq.used == 0)
822 msg (SE, _("Variables %s specified on GROUPED but not on "
823 "VARIABLES."), v[i]->name);
824 if (v[i]->p.frq.groups != NULL)
825 msg (SE, _("Variables %s specified multiple times on GROUPED "
826 "subcommand."), v[i]->name);
829 v[i]->p.frq.n_groups = nl;
830 v[i]->p.frq.groups = dl;
834 if (!lex_match ('/'))
836 if ((token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
847 /* Adds X to the list of percentiles, keeping the list in proper
850 add_percentile (double x)
854 for (i = 0; i < n_percentiles; i++)
856 /* Do nothing if it's already in the list */
857 if ( fabs(x - percentiles[i].p) < DBL_EPSILON )
860 if (x < percentiles[i].p)
864 if (i >= n_percentiles || tokval != percentiles[i].p)
867 = pool_realloc (int_pool, percentiles,
868 (n_percentiles + 1) * sizeof (struct percentile ));
870 if (i < n_percentiles)
871 memmove (&percentiles[i + 1], &percentiles[i],
872 (n_percentiles - i) * sizeof (struct percentile) );
874 percentiles[i].p = x;
879 /* Comparison functions. */
881 /* Hash of numeric values. */
883 hash_value_numeric (const void *value_, void *foo UNUSED)
885 const struct freq *value = value_;
886 return hsh_hash_double (value->v.f);
889 /* Hash of string values. */
891 hash_value_alpha (const void *value_, void *v_)
893 const struct freq *value = value_;
894 struct variable *v = v_;
896 return hsh_hash_bytes (value->v.s, v->width);
899 /* Ascending numeric compare of values. */
901 compare_value_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
903 const struct freq *a = a_;
904 const struct freq *b = b_;
908 else if (a->v.f < b->v.f)
914 /* Ascending string compare of values. */
916 compare_value_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_;
922 return memcmp (a->v.s, b->v.s, v->width);
925 /* Descending numeric compare of values. */
927 compare_value_numeric_d (const void *a, const void *b, void *foo UNUSED)
929 return -compare_value_numeric_a (a, b, foo);
932 /* Descending string compare of values. */
934 compare_value_alpha_d (const void *a, const void *b, void *v)
936 return -compare_value_alpha_a (a, b, v);
939 /* Ascending numeric compare of frequency;
940 secondary key on ascending numeric value. */
942 compare_freq_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
944 const struct freq *a = a_;
945 const struct freq *b = b_;
949 else if (a->c < b->c)
954 else if (a->v.f < b->v.f)
960 /* Ascending numeric compare of frequency;
961 secondary key on ascending string value. */
963 compare_freq_alpha_a (const void *a_, const void *b_, void *v_)
965 const struct freq *a = a_;
966 const struct freq *b = b_;
967 const struct variable *v = v_;
971 else if (a->c < b->c)
974 return memcmp (a->v.s, b->v.s, v->width);
977 /* Descending numeric compare of frequency;
978 secondary key on ascending numeric value. */
980 compare_freq_numeric_d (const void *a_, const void *b_, void *foo UNUSED)
982 const struct freq *a = a_;
983 const struct freq *b = b_;
987 else if (a->c < b->c)
992 else if (a->v.f < b->v.f)
998 /* Descending numeric compare of frequency;
999 secondary key on ascending string value. */
1001 compare_freq_alpha_d (const void *a_, const void *b_, void *v_)
1003 const struct freq *a = a_;
1004 const struct freq *b = b_;
1005 const struct variable *v = v_;
1009 else if (a->c < b->c)
1012 return memcmp (a->v.s, b->v.s, v->width);
1015 /* Frequency table display. */
1017 /* Sets the widths of all the columns and heights of all the rows in
1018 table T for driver D. */
1020 full_dim (struct tab_table *t, struct outp_driver *d)
1022 int lab = cmd.labels == FRQ_LABELS;
1026 t->w[0] = min (tab_natural_width (t, d, 0), d->prop_em_width * 15);
1027 for (i = lab; i < lab + 5; i++)
1028 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1029 for (i = 0; i < t->nr; i++)
1030 t->h[i] = d->font_height;
1033 /* Displays a full frequency table for variable V. */
1035 dump_full (struct variable * v)
1039 struct tab_table *t;
1041 double cum_total = 0.0;
1042 double cum_freq = 0.0;
1052 static struct init vec[] =
1054 {4, 0, N_("Valid")},
1056 {1, 1, N_("Value")},
1057 {2, 1, N_("Frequency")},
1058 {3, 1, N_("Percent")},
1059 {4, 1, N_("Percent")},
1060 {5, 1, N_("Percent")},
1068 int lab = cmd.labels == FRQ_LABELS;
1070 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
1071 t = tab_create (5 + lab, n_categories + 3, 0);
1072 tab_headers (t, 0, 0, 2, 0);
1073 tab_dim (t, full_dim);
1076 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value Label"));
1077 for (p = vec; p->s; p++)
1078 tab_text (t, p->c - (p->r ? !lab : 0), p->r,
1079 TAB_CENTER | TAT_TITLE, gettext (p->s));
1082 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1084 double percent, valid_percent;
1088 percent = f->c / v->p.frq.tab.total_cases * 100.0;
1089 valid_percent = f->c / v->p.frq.tab.valid_cases * 100.0;
1090 cum_total += valid_percent;
1094 const char *label = val_labs_find (v->val_labs, f->v);
1096 tab_text (t, 0, r, TAB_LEFT, label);
1099 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1100 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1101 tab_float (t, 2 + lab, r, TAB_NONE, percent, 5, 1);
1102 tab_float (t, 3 + lab, r, TAB_NONE, valid_percent, 5, 1);
1103 tab_float (t, 4 + lab, r, TAB_NONE, cum_total, 5, 1);
1106 for (; f < &v->p.frq.tab.valid[n_categories]; f++)
1112 const char *label = val_labs_find (v->val_labs, f->v);
1114 tab_text (t, 0, r, TAB_LEFT, label);
1117 tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1118 tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1119 tab_float (t, 2 + lab, r, TAB_NONE,
1120 f->c / v->p.frq.tab.total_cases * 100.0, 5, 1);
1121 tab_text (t, 3 + lab, r, TAB_NONE, _("Missing"));
1125 tab_box (t, TAL_1, TAL_1,
1126 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1128 tab_hline (t, TAL_2, 0, 4 + lab, 2);
1129 tab_hline (t, TAL_2, 0, 4 + lab, r);
1130 tab_joint_text (t, 0, r, 0 + lab, r, TAB_RIGHT | TAT_TITLE, _("Total"));
1131 tab_vline (t, TAL_0, 1, r, r);
1132 tab_float (t, 1 + lab, r, TAB_NONE, cum_freq, 8, 0);
1133 tab_float (t, 2 + lab, r, TAB_NONE, 100.0, 5, 1);
1134 tab_float (t, 3 + lab, r, TAB_NONE, 100.0, 5, 1);
1136 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1141 /* Sets the widths of all the columns and heights of all the rows in
1142 table T for driver D. */
1144 condensed_dim (struct tab_table *t, struct outp_driver *d)
1146 int cum_w = max (outp_string_width (d, _("Cum")),
1147 max (outp_string_width (d, _("Cum")),
1148 outp_string_width (d, "000")));
1152 for (i = 0; i < 2; i++)
1153 t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1154 for (i = 2; i < 4; i++)
1156 for (i = 0; i < t->nr; i++)
1157 t->h[i] = d->font_height;
1160 /* Display condensed frequency table for variable V. */
1162 dump_condensed (struct variable * v)
1166 struct tab_table *t;
1168 double cum_total = 0.0;
1170 n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing;
1171 t = tab_create (4, n_categories + 2, 0);
1173 tab_headers (t, 0, 0, 2, 0);
1174 tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value"));
1175 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Freq"));
1176 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1177 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Cum"));
1178 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1179 tab_dim (t, condensed_dim);
1182 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1186 percent = f->c / v->p.frq.tab.total_cases * 100.0;
1187 cum_total += f->c / v->p.frq.tab.valid_cases * 100.0;
1189 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1190 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1191 tab_float (t, 2, r, TAB_NONE, percent, 3, 0);
1192 tab_float (t, 3, r, TAB_NONE, cum_total, 3, 0);
1195 for (; f < &v->p.frq.tab.valid[n_categories]; f++)
1197 tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1198 tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1199 tab_float (t, 2, r, TAB_NONE,
1200 f->c / v->p.frq.tab.total_cases * 100.0, 3, 0);
1204 tab_box (t, TAL_1, TAL_1,
1205 cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1207 tab_hline (t, TAL_2, 0, 3, 2);
1208 tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1209 tab_columns (t, SOM_COL_DOWN, 1);
1213 /* Statistical display. */
1215 /* Calculates all the pertinent statistics for variable V, putting
1216 them in array D[]. FIXME: This could be made much more optimal. */
1218 calc_stats (struct variable * v, double d[frq_n_stats])
1220 double W = v->p.frq.tab.valid_cases;
1229 double *median_value;
1231 /* Calculate percentiles. */
1233 /* If the 50th percentile was not explicitly requested then we must
1234 calculate it anyway --- it's the median */
1236 for (i = 0; i < n_percentiles; i++)
1238 if (percentiles[i].p == 0.5)
1240 median_value = &percentiles[i].value;
1245 if ( 0 == median_value )
1247 add_percentile (0.5);
1251 for (i = 0; i < n_percentiles; i++)
1253 percentiles[i].flag = 0;
1254 percentiles[i].flag2 = 0;
1258 for (idx = 0; idx < v->p.frq.tab.n_valid; ++idx)
1260 static double prev_value = SYSMIS;
1261 f = &v->p.frq.tab.valid[idx];
1263 for (i = 0; i < n_percentiles; i++)
1266 if ( percentiles[i].flag2 ) continue ;
1268 if ( get_algorithm() != COMPATIBLE )
1270 (v->p.frq.tab.valid_cases - 1) * percentiles[i].p;
1273 (v->p.frq.tab.valid_cases + 1) * percentiles[i].p - 1;
1275 if ( percentiles[i].flag )
1277 percentiles[i].x2 = f->v.f;
1278 percentiles[i].x1 = prev_value;
1279 percentiles[i].flag2 = 1;
1285 if ( f->c > 1 && rank - (f->c - 1) > tp )
1287 percentiles[i].x2 = percentiles[i].x1 = f->v.f;
1288 percentiles[i].flag2 = 1;
1292 percentiles[i].flag=1;
1298 prev_value = f->v.f;
1301 for (i = 0; i < n_percentiles; i++)
1303 /* Catches the case when p == 100% */
1304 if ( ! percentiles[i].flag2 )
1305 percentiles[i].x1 = percentiles[i].x2 = f->v.f;
1308 printf("percentile %d (p==%.2f); X1 = %g; X2 = %g\n",
1309 i,percentiles[i].p,percentiles[i].x1,percentiles[i].x2);
1313 for (i = 0; i < n_percentiles; i++)
1315 struct freq_tab *ft = &v->p.frq.tab;
1319 if ( get_algorithm() != COMPATIBLE )
1321 s = modf((ft->valid_cases - 1) * percentiles[i].p , &dummy);
1325 s = modf((ft->valid_cases + 1) * percentiles[i].p -1, &dummy);
1328 percentiles[i].value = percentiles[i].x1 +
1329 ( percentiles[i].x2 - percentiles[i].x1) * s ;
1331 if ( percentiles[i].p == 0.50)
1332 median_value = &percentiles[i].value;
1336 /* Calculate the mode. */
1339 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1341 if (most_often < f->c)
1346 else if (most_often == f->c)
1348 /* A duplicate mode is undefined.
1349 FIXME: keep track of *all* the modes. */
1354 /* Calculate moments. */
1355 m = moments_create (MOMENT_KURTOSIS);
1356 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1357 moments_pass_one (m, f->v.f, f->c);
1358 for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++)
1359 moments_pass_two (m, f->v.f, f->c);
1360 moments_calculate (m, NULL, &d[frq_mean], &d[frq_variance],
1361 &d[frq_skew], &d[frq_kurt]);
1362 moments_destroy (m);
1364 /* Formulas below are taken from _SPSS Statistical Algorithms_. */
1365 d[frq_min] = v->p.frq.tab.valid[0].v.f;
1366 d[frq_max] = v->p.frq.tab.valid[v->p.frq.tab.n_valid - 1].v.f;
1367 d[frq_mode] = X_mode;
1368 d[frq_range] = d[frq_max] - d[frq_min];
1369 d[frq_median] = *median_value;
1370 d[frq_sum] = d[frq_mean] * W;
1371 d[frq_stddev] = sqrt (d[frq_variance]);
1372 d[frq_semean] = d[frq_stddev] / sqrt (W);
1373 d[frq_seskew] = calc_seskew (W);
1374 d[frq_sekurt] = calc_sekurt (W);
1377 /* Displays a table of all the statistics requested for variable V. */
1379 dump_statistics (struct variable * v, int show_varname)
1381 double stat_value[frq_n_stats];
1382 struct tab_table *t;
1385 int n_explicit_percentiles = n_percentiles;
1387 if ( implicit_50th && n_percentiles > 0 )
1390 if (v->type == ALPHA)
1392 if (v->p.frq.tab.n_valid == 0)
1394 msg (SW, _("No valid data for variable %s; statistics not displayed."),
1398 calc_stats (v, stat_value);
1400 t = tab_create (3, n_stats + n_explicit_percentiles + 2, 0);
1401 tab_dim (t, tab_natural_dimensions);
1403 tab_box (t, TAL_1, TAL_1, -1, -1 , 0 , 0 , 2, tab_nr(t) - 1) ;
1406 tab_vline (t, TAL_1 , 2, 0, tab_nr(t) - 1);
1407 tab_vline (t, TAL_1 | TAL_SPACING , 1, 0, tab_nr(t) - 1 ) ;
1409 r=2; /* N missing and N valid are always dumped */
1411 for (i = 0; i < frq_n_stats; i++)
1412 if (stats & BIT_INDEX (i))
1414 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
1415 gettext (st_name[i].s10));
1416 tab_float (t, 2, r, TAB_NONE, stat_value[i], 11, 3);
1420 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("N"));
1421 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Valid"));
1422 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Missing"));
1424 tab_float(t, 2, 0, TAB_NONE, v->p.frq.tab.valid_cases, 11, 0);
1425 tab_float(t, 2, 1, TAB_NONE,
1426 v->p.frq.tab.total_cases - v->p.frq.tab.valid_cases, 11, 0);
1429 for (i = 0; i < n_explicit_percentiles; i++, r++)
1433 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Percentiles"));
1436 tab_float (t, 1, r, TAB_LEFT, percentiles[i].p * 100, 3, 0 );
1437 tab_float (t, 2, r, TAB_NONE, percentiles[i].value, 11, 3);
1441 tab_columns (t, SOM_COL_DOWN, 1);
1445 tab_title (t, 1, "%s: %s", v->name, v->label);
1447 tab_title (t, 0, v->name);
1450 tab_flags (t, SOMF_NO_TITLE);