2 PSPP - a program for statistical analysis.
3 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2014 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU 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, see <http://www.gnu.org/licenses/>.
21 #include <gsl/gsl_histogram.h>
24 #include "data/case.h"
25 #include "data/casegrouper.h"
26 #include "data/casereader.h"
27 #include "data/dataset.h"
28 #include "data/dictionary.h"
29 #include "data/format.h"
30 #include "data/missing-values.h"
31 #include "data/settings.h"
32 #include "data/value-labels.h"
33 #include "data/variable.h"
35 #include "language/dictionary/split-file.h"
37 #include "language/command.h"
38 #include "language/lexer/lexer.h"
39 #include "language/lexer/variable-parser.h"
40 #include "language/stats/freq.h"
42 #include "libpspp/array.h"
43 #include "libpspp/bit-vector.h"
44 #include "libpspp/compiler.h"
45 #include "libpspp/hmap.h"
46 #include "libpspp/message.h"
47 #include "libpspp/misc.h"
48 #include "libpspp/pool.h"
50 #include "math/histogram.h"
51 #include "math/moments.h"
52 #include "math/chart-geometry.h"
55 #include "output/chart-item.h"
56 #include "output/charts/piechart.h"
57 #include "output/charts/plot-hist.h"
58 #include "output/tab.h"
60 #include "gl/minmax.h"
61 #include "gl/xalloc.h"
64 #define _(msgid) gettext (msgid)
65 #define N_(msgid) msgid
67 /* Percentiles to calculate. */
71 double p; /* the %ile to be calculated */
72 double value; /* the %ile's value */
73 bool show; /* True to show this percentile in the statistics box. */
77 ptile_3way (const void *_p1, const void *_p2)
79 const struct percentile *p1 = _p1;
80 const struct percentile *p2 = _p2;
85 return (p1->p > p2->p);
109 /* Array indices for STATISTICS subcommand. */
129 /* Description of statistics. */
130 static const char *st_name[FRQ_ST_count] =
150 struct hmap data; /* Hash table for accumulating counts. */
151 struct freq *valid; /* Valid freqs. */
152 int n_valid; /* Number of total freqs. */
153 const struct dictionary *dict; /* Source of entries in the table. */
155 struct freq *missing; /* Missing freqs. */
156 int n_missing; /* Number of missing freqs. */
159 double total_cases; /* Sum of weights of all cases. */
160 double valid_cases; /* Sum of weights of valid cases. */
165 double x_min; /* X axis minimum value. */
166 double x_max; /* X axis maximum value. */
167 int y_scale; /* Y axis scale: FRQ_FREQ or FRQ_PERCENT. */
169 /* Histograms only. */
170 double y_max; /* Y axis maximum value. */
171 bool draw_normal; /* Whether to draw normal curve. */
173 /* Pie charts only. */
174 bool include_missing; /* Whether to include missing values. */
177 /* Per-variable frequency data. */
180 const struct variable *var;
182 /* Freqency table. */
183 struct freq_tab tab; /* Frequencies table to use. */
186 int n_groups; /* Number of groups. */
187 double *groups; /* Groups. */
190 double stat[FRQ_ST_count];
192 /* Variable attributes. */
200 struct var_freqs *vars;
203 /* Percentiles to calculate and possibly display. */
204 struct percentile *percentiles;
205 int n_percentiles, n_show_percentiles;
207 /* Frequency table display. */
208 int max_categories; /* Maximum categories to show. */
209 int sort; /* FRQ_AVALUE or FRQ_DVALUE
210 or FRQ_AFREQ or FRQ_DFREQ. */
212 /* Statistics; number of statistics. */
216 /* Histogram and pie chart settings. */
217 struct frq_chart *hist, *pie;
221 struct freq_compare_aux
227 bool ascending_value;
230 static void calc_stats (const struct var_freqs *vf, double d[FRQ_ST_count]);
232 static void do_piechart(const struct frq_chart *pie,
233 const struct variable *var,
234 const struct freq_tab *frq_tab);
236 static void dump_statistics (const struct frq_proc *frq,
237 const struct var_freqs *vf,
238 const struct variable *wv);
241 compare_freq (const void *a_, const void *b_, const void *aux_)
243 const struct freq_compare_aux *aux = aux_;
244 const struct freq *a = a_;
245 const struct freq *b = b_;
247 if (aux->by_freq && a->count != b->count)
249 int cmp = a->count > b->count ? 1 : -1;
250 return aux->ascending_freq ? cmp : -cmp;
254 int cmp = value_compare_3way (&a->value, &b->value, aux->width);
255 return aux->ascending_value ? cmp : -cmp;
259 /* Create a gsl_histogram from a freq_tab */
260 static struct histogram *
261 freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
262 const struct variable *var);
265 /* Displays a full frequency table for variable V. */
267 dump_freq_table (const struct var_freqs *vf, const struct variable *wv)
269 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
270 const struct freq_tab *ft = &vf->tab;
275 double cum_total = 0.0;
276 double cum_freq = 0.0;
278 static const char *headings[] = {
287 n_categories = ft->n_valid + ft->n_missing;
288 t = tab_create (6, n_categories + 2);
289 tab_set_format (t, RC_WEIGHT, wfmt);
290 tab_headers (t, 0, 0, 1, 0);
292 for (x = 0; x < 6; x++)
293 tab_text (t, x, 0, TAB_CENTER | TAT_TITLE, gettext (headings[x]));
296 for (f = ft->valid; f < ft->missing; f++)
299 double percent, valid_percent;
301 cum_freq += f->count;
303 percent = f->count / ft->total_cases * 100.0;
304 valid_percent = f->count / ft->valid_cases * 100.0;
305 cum_total += valid_percent;
307 label = var_lookup_value_label (vf->var, &f->value);
309 tab_text (t, 0, r, TAB_LEFT, label);
311 tab_value (t, 1, r, TAB_NONE, &f->value, vf->var, NULL);
312 tab_double (t, 2, r, TAB_NONE, f->count, NULL, RC_WEIGHT);
313 tab_double (t, 3, r, TAB_NONE, percent, NULL, RC_OTHER);
314 tab_double (t, 4, r, TAB_NONE, valid_percent, NULL, RC_OTHER);
315 tab_double (t, 5, r, TAB_NONE, cum_total, NULL, RC_OTHER);
318 for (; f < &ft->valid[n_categories]; f++)
322 cum_freq += f->count;
324 label = var_lookup_value_label (vf->var, &f->value);
326 tab_text (t, 0, r, TAB_LEFT, label);
328 tab_value (t, 1, r, TAB_NONE, &f->value, vf->var, NULL);
329 tab_double (t, 2, r, TAB_NONE, f->count, NULL, RC_WEIGHT);
330 tab_double (t, 3, r, TAB_NONE,
331 f->count / ft->total_cases * 100.0, NULL, RC_OTHER);
332 tab_text (t, 4, r, TAB_NONE, _("Missing"));
336 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, 5, r);
337 tab_hline (t, TAL_2, 0, 5, 1);
338 tab_hline (t, TAL_2, 0, 5, r);
339 tab_joint_text (t, 0, r, 1, r, TAB_RIGHT | TAT_TITLE, _("Total"));
340 tab_vline (t, TAL_0, 1, r, r);
341 tab_double (t, 2, r, TAB_NONE, cum_freq, NULL, RC_WEIGHT);
342 tab_double (t, 3, r, TAB_NONE, 100.0, &F_5_1, RC_OTHER);
343 tab_double (t, 4, r, TAB_NONE, 100.0, &F_5_1, RC_OTHER);
345 tab_title (t, "%s", var_to_string (vf->var));
349 /* Statistical display. */
352 calc_percentile (double p, double valid_cases, double x1, double x2)
356 s = (settings_get_algorithm () != COMPATIBLE
357 ? modf ((valid_cases - 1) * p, &dummy)
358 : modf ((valid_cases + 1) * p - 1, &dummy));
360 return x1 + (x2 - x1) * s;
363 /* Calculates all of the percentiles for VF within FRQ. */
365 calc_percentiles (const struct frq_proc *frq, const struct var_freqs *vf)
367 const struct freq_tab *ft = &vf->tab;
368 double W = ft->valid_cases;
369 const struct freq *f;
370 int percentile_idx = 0;
373 for (f = ft->valid; f < ft->missing; f++)
376 for (; percentile_idx < frq->n_percentiles; percentile_idx++)
378 struct percentile *pc = &frq->percentiles[percentile_idx];
381 tp = (settings_get_algorithm () == ENHANCED
383 : (W + 1) * pc->p - 1);
388 if (tp + 1 < rank || f + 1 >= ft->missing)
389 pc->value = f->value.f;
391 pc->value = calc_percentile (pc->p, W, f->value.f, f[1].value.f);
394 for (; percentile_idx < frq->n_percentiles; percentile_idx++)
396 struct percentile *pc = &frq->percentiles[percentile_idx];
397 pc->value = ft->valid[ft->n_valid - 1].value.f;
401 /* Returns true iff the value in struct freq F is non-missing
404 not_missing (const void *f_, const void *v_)
406 const struct freq *f = f_;
407 const struct variable *v = v_;
409 return !var_is_value_missing (v, &f->value, MV_ANY);
413 /* Summarizes the frequency table data for variable V. */
415 postprocess_freq_tab (const struct frq_proc *frq, struct var_freqs *vf)
417 struct freq_tab *ft = &vf->tab;
418 struct freq_compare_aux aux;
420 struct freq *freqs, *f;
423 /* Extract data from hash table. */
424 count = hmap_count (&ft->data);
425 freqs = freq_hmap_extract (&ft->data);
427 /* Put data into ft. */
429 ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, vf->var);
430 ft->missing = freqs + ft->n_valid;
431 ft->n_missing = count - ft->n_valid;
434 aux.by_freq = frq->sort == FRQ_AFREQ || frq->sort == FRQ_DFREQ;
435 aux.ascending_freq = frq->sort != FRQ_DFREQ;
436 aux.width = vf->width;
437 aux.ascending_value = frq->sort != FRQ_DVALUE;
438 sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare_freq, &aux);
439 sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare_freq, &aux);
441 /* Summary statistics. */
442 ft->valid_cases = 0.0;
443 for(i = 0 ; i < ft->n_valid ; ++i )
446 ft->valid_cases += f->count;
450 ft->total_cases = ft->valid_cases ;
451 for(i = 0 ; i < ft->n_missing ; ++i )
454 ft->total_cases += f->count;
459 /* Frees the frequency table for variable V. */
461 cleanup_freq_tab (struct var_freqs *vf)
463 free (vf->tab.valid);
464 freq_hmap_destroy (&vf->tab.data, vf->width);
467 /* Add data from case C to the frequency table. */
469 calc (struct frq_proc *frq, const struct ccase *c, const struct dataset *ds)
471 double weight = dict_get_case_weight (dataset_dict (ds), c, NULL);
474 for (i = 0; i < frq->n_vars; i++)
476 struct var_freqs *vf = &frq->vars[i];
477 const union value *value = case_data (c, vf->var);
478 size_t hash = value_hash (value, vf->width, 0);
481 f = freq_hmap_search (&vf->tab.data, value, vf->width, hash);
483 f = freq_hmap_insert (&vf->tab.data, value, vf->width, hash);
489 /* Prepares each variable that is the target of FREQUENCIES by setting
490 up its hash table. */
492 precalc (struct frq_proc *frq, struct casereader *input, struct dataset *ds)
497 c = casereader_peek (input, 0);
500 output_split_file_values (ds, c);
504 for (i = 0; i < frq->n_vars; i++)
505 hmap_init (&frq->vars[i].tab.data);
508 /* Finishes up with the variables after frequencies have been
509 calculated. Displays statistics, percentiles, ... */
511 postcalc (struct frq_proc *frq, const struct dataset *ds)
513 const struct dictionary *dict = dataset_dict (ds);
514 const struct variable *wv = dict_get_weight (dict);
517 for (i = 0; i < frq->n_vars; i++)
519 struct var_freqs *vf = &frq->vars[i];
521 postprocess_freq_tab (frq, vf);
523 /* Frequencies tables. */
524 if (vf->tab.n_valid + vf->tab.n_missing <= frq->max_categories)
525 dump_freq_table (vf, wv);
527 calc_percentiles (frq, vf);
531 dump_statistics (frq, vf, wv);
533 if (frq->hist && var_is_numeric (vf->var) && vf->tab.n_valid > 0)
535 double d[FRQ_ST_count];
536 struct histogram *histogram;
540 histogram = freq_tab_to_hist (frq, &vf->tab, vf->var);
544 chart_item_submit (histogram_chart_create (
545 histogram->gsl_hist, var_to_string(vf->var),
549 frq->hist->draw_normal));
551 statistic_destroy (&histogram->parent);
556 do_piechart(frq->pie, vf->var, &vf->tab);
558 cleanup_freq_tab (vf);
563 cmd_frequencies (struct lexer *lexer, struct dataset *ds)
567 const struct variable **vars;
569 bool sbc_barchart = false;
570 bool sbc_piechart = false;
571 bool sbc_histogram = false;
573 double pie_min = -DBL_MAX;
574 double pie_max = DBL_MAX;
575 bool pie_missing = false;
577 double hi_min = -DBL_MAX;
578 double hi_max = DBL_MAX;
579 int hi_scale = FRQ_FREQ;
580 int hi_freq = INT_MIN;
581 int hi_pcnt = INT_MIN;
582 int hi_norm = FRQ_NONORMAL;
584 frq.pool = pool_create ();
585 frq.sort = FRQ_AVALUE;
590 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
591 | BIT_INDEX (FRQ_ST_STDDEV)
592 | BIT_INDEX (FRQ_ST_MINIMUM)
593 | BIT_INDEX (FRQ_ST_MAXIMUM);
597 frq.max_categories = INT_MAX;
599 frq.percentiles = NULL;
600 frq.n_percentiles = 0;
601 frq.n_show_percentiles = 0;
607 /* Accept an optional, completely pointless "/VARIABLES=" */
608 lex_match (lexer, T_SLASH);
609 if (lex_match_id (lexer, "VARIABLES"))
611 if (! lex_force_match (lexer, T_EQUALS) )
615 if (!parse_variables_const (lexer, dataset_dict (ds),
621 frq.vars = xzalloc (frq.n_vars * sizeof (*frq.vars));
622 for (i = 0; i < frq.n_vars; ++i)
624 frq.vars[i].var = vars[i];
625 frq.vars[i].width = var_get_width (vars[i]);
628 while (lex_token (lexer) != T_ENDCMD)
630 lex_match (lexer, T_SLASH);
632 if (lex_match_id (lexer, "STATISTICS"))
634 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
635 | BIT_INDEX (FRQ_ST_STDDEV)
636 | BIT_INDEX (FRQ_ST_MINIMUM)
637 | BIT_INDEX (FRQ_ST_MAXIMUM);
641 if (lex_match (lexer, T_EQUALS))
647 while (lex_token (lexer) != T_ENDCMD
648 && lex_token (lexer) != T_SLASH)
650 if (lex_match_id (lexer, "DEFAULT"))
652 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
653 | BIT_INDEX (FRQ_ST_STDDEV)
654 | BIT_INDEX (FRQ_ST_MINIMUM)
655 | BIT_INDEX (FRQ_ST_MAXIMUM);
659 else if (lex_match_id (lexer, "MEAN"))
661 frq.stats |= BIT_INDEX (FRQ_ST_MEAN);
664 else if (lex_match_id (lexer, "SEMEAN"))
666 frq.stats |= BIT_INDEX (FRQ_ST_SEMEAN);
669 else if (lex_match_id (lexer, "MEDIAN"))
671 frq.stats |= BIT_INDEX (FRQ_ST_MEDIAN);
674 else if (lex_match_id (lexer, "MODE"))
676 frq.stats |= BIT_INDEX (FRQ_ST_MODE);
679 else if (lex_match_id (lexer, "STDDEV"))
681 frq.stats |= BIT_INDEX (FRQ_ST_STDDEV);
684 else if (lex_match_id (lexer, "VARIANCE"))
686 frq.stats |= BIT_INDEX (FRQ_ST_MEAN);
689 else if (lex_match_id (lexer, "KURTOSIS"))
691 frq.stats |= BIT_INDEX (FRQ_ST_KURTOSIS);
694 else if (lex_match_id (lexer, "SKEWNESS"))
696 frq.stats |= BIT_INDEX (FRQ_ST_SKEWNESS);
699 else if (lex_match_id (lexer, "RANGE"))
701 frq.stats |= BIT_INDEX (FRQ_ST_RANGE);
704 else if (lex_match_id (lexer, "MINIMUM"))
706 frq.stats |= BIT_INDEX (FRQ_ST_MINIMUM);
709 else if (lex_match_id (lexer, "MAXIMUM"))
711 frq.stats |= BIT_INDEX (FRQ_ST_MAXIMUM);
714 else if (lex_match_id (lexer, "SUM"))
716 frq.stats |= BIT_INDEX (FRQ_ST_SUM);
719 else if (lex_match_id (lexer, "SESKEWNESS"))
721 frq.stats |= BIT_INDEX (FRQ_ST_SESKEWNESS);
724 else if (lex_match_id (lexer, "SEKURTOSIS"))
726 frq.stats |= BIT_INDEX (FRQ_ST_SEKURTOSIS);
729 else if (lex_match_id (lexer, "NONE"))
734 else if (lex_match (lexer, T_ALL))
737 frq.n_stats = FRQ_ST_count;
741 lex_error (lexer, NULL);
746 else if (lex_match_id (lexer, "PERCENTILES"))
748 lex_match (lexer, T_EQUALS);
749 while (lex_token (lexer) != T_ENDCMD
750 && lex_token (lexer) != T_SLASH)
752 if (lex_force_num (lexer))
755 xrealloc (frq.percentiles,
756 (frq.n_percentiles + 1)
757 * sizeof (*frq.percentiles));
758 frq.percentiles[frq.n_percentiles].p = lex_number (lexer) / 100.0;
759 frq.percentiles[frq.n_percentiles].show = true;
762 frq.n_show_percentiles++;
766 lex_error (lexer, NULL);
769 lex_match (lexer, T_COMMA);
772 else if (lex_match_id (lexer, "FORMAT"))
774 lex_match (lexer, T_EQUALS);
775 while (lex_token (lexer) != T_ENDCMD
776 && lex_token (lexer) != T_SLASH)
778 if (lex_match_id (lexer, "TABLE"))
782 else if (lex_match_id (lexer, "NOTABLE"))
784 frq.max_categories = 0;
786 else if (lex_match_id (lexer, "AVALUE"))
788 frq.sort = FRQ_AVALUE;
790 else if (lex_match_id (lexer, "DVALUE"))
792 frq.sort = FRQ_DVALUE;
794 else if (lex_match_id (lexer, "AFREQ"))
796 frq.sort = FRQ_AFREQ;
798 else if (lex_match_id (lexer, "DFREQ"))
800 frq.sort = FRQ_DFREQ;
804 lex_error (lexer, NULL);
809 else if (lex_match_id (lexer, "NTILES"))
811 lex_match (lexer, T_EQUALS);
813 if (lex_force_int (lexer))
816 int n = lex_integer (lexer);
818 for (i = 0; i < n + 1; ++i)
821 xrealloc (frq.percentiles,
822 (frq.n_percentiles + 1)
823 * sizeof (*frq.percentiles));
824 frq.percentiles[frq.n_percentiles].p =
826 frq.percentiles[frq.n_percentiles].show = true;
829 frq.n_show_percentiles++;
834 lex_error (lexer, NULL);
838 else if (lex_match_id (lexer, "ALGORITHM"))
840 lex_match (lexer, T_EQUALS);
842 if (lex_match_id (lexer, "COMPATIBLE"))
844 settings_set_cmd_algorithm (COMPATIBLE);
846 else if (lex_match_id (lexer, "ENHANCED"))
848 settings_set_cmd_algorithm (ENHANCED);
852 lex_error (lexer, NULL);
856 else if (lex_match_id (lexer, "HISTOGRAM"))
858 lex_match (lexer, T_EQUALS);
859 sbc_histogram = true;
861 while (lex_token (lexer) != T_ENDCMD
862 && lex_token (lexer) != T_SLASH)
864 if (lex_match_id (lexer, "NORMAL"))
866 hi_norm = FRQ_NORMAL;
868 else if (lex_match_id (lexer, "NONORMAL"))
870 hi_norm = FRQ_NONORMAL;
872 else if (lex_match_id (lexer, "FREQ"))
875 if (lex_match (lexer, T_LPAREN))
877 if (lex_force_int (lexer))
879 hi_freq = lex_integer (lexer);
882 lex_error (lexer, _("Histogram frequency must be greater than zero."));
885 lex_force_match (lexer, T_RPAREN);
889 else if (lex_match_id (lexer, "PERCENT"))
891 hi_scale = FRQ_PERCENT;
892 if (lex_match (lexer, T_LPAREN))
894 if (lex_force_int (lexer))
896 hi_pcnt = lex_integer (lexer);
899 lex_error (lexer, _("Histogram percentage must be greater than zero."));
902 lex_force_match (lexer, T_RPAREN);
906 else if (lex_match_id (lexer, "MINIMUM"))
908 lex_force_match (lexer, T_LPAREN);
909 if (lex_force_num (lexer))
911 hi_min = lex_number (lexer);
914 lex_force_match (lexer, T_RPAREN);
916 else if (lex_match_id (lexer, "MAXIMUM"))
918 lex_force_match (lexer, T_LPAREN);
919 if (lex_force_num (lexer))
921 hi_max = lex_number (lexer);
924 lex_force_match (lexer, T_RPAREN);
928 lex_error (lexer, NULL);
933 else if (lex_match_id (lexer, "PIECHART"))
935 lex_match (lexer, T_EQUALS);
936 while (lex_token (lexer) != T_ENDCMD
937 && lex_token (lexer) != T_SLASH)
939 if (lex_match_id (lexer, "MINIMUM"))
941 lex_force_match (lexer, T_LPAREN);
942 if (lex_force_num (lexer))
944 pie_min = lex_number (lexer);
947 lex_force_match (lexer, T_RPAREN);
949 else if (lex_match_id (lexer, "MAXIMUM"))
951 lex_force_match (lexer, T_LPAREN);
952 if (lex_force_num (lexer))
954 pie_max = lex_number (lexer);
957 lex_force_match (lexer, T_RPAREN);
959 else if (lex_match_id (lexer, "MISSING"))
963 else if (lex_match_id (lexer, "NOMISSING"))
969 lex_error (lexer, NULL);
975 else if (lex_match_id (lexer, "MISSING"))
977 lex_match (lexer, T_EQUALS);
979 while (lex_token (lexer) != T_ENDCMD
980 && lex_token (lexer) != T_SLASH)
982 if (lex_match_id (lexer, "EXCLUDE"))
985 else if (lex_match_id (lexer, "INCLUDE"))
990 lex_error (lexer, NULL);
997 lex_error (lexer, NULL);
1002 if (frq.stats & BIT_INDEX (FRQ_ST_MEDIAN))
1005 xrealloc (frq.percentiles,
1006 (frq.n_percentiles + 1)
1007 * sizeof (*frq.percentiles));
1009 frq.percentiles[frq.n_percentiles].p = 0.50;
1010 frq.percentiles[frq.n_percentiles].show = true;
1012 frq.n_percentiles++;
1016 /* Figure out which charts the user requested. */
1020 msg (SW, _("Bar charts are not implemented."));
1024 struct frq_chart *hist;
1026 hist = frq.hist = xmalloc (sizeof *frq.hist);
1027 hist->x_min = hi_min;
1028 hist->x_max = hi_max;
1029 hist->y_scale = hi_scale;
1030 hist->y_max = hi_scale == FRQ_FREQ ? hi_freq : hi_pcnt;
1031 hist->draw_normal = hi_norm != FRQ_NONORMAL;
1032 hist->include_missing = false;
1034 if (hist->x_min != SYSMIS && hist->x_max != SYSMIS
1035 && hist->x_min >= hist->x_max)
1037 msg (SE, _("%s for histogram must be greater than or equal to %s, "
1038 "but %s was specified as %.15g and %s as %.15g. "
1039 "%s and %s will be ignored."),
1044 hist->x_min = hist->x_max = SYSMIS;
1048 xrealloc (frq.percentiles,
1049 (frq.n_percentiles + 2)
1050 * sizeof (*frq.percentiles));
1052 frq.percentiles[frq.n_percentiles].p = 0.25;
1053 frq.percentiles[frq.n_percentiles].show = false;
1055 frq.percentiles[frq.n_percentiles + 1].p = 0.75;
1056 frq.percentiles[frq.n_percentiles + 1].show = false;
1058 frq.n_percentiles+=2;
1063 struct frq_chart *pie;
1065 pie = frq.pie = xmalloc (sizeof *frq.pie);
1066 pie->x_min = pie_min;
1067 pie->x_max = pie_max;
1068 pie->include_missing = pie_missing;
1070 if (pie->x_min != SYSMIS && pie->x_max != SYSMIS
1071 && pie->x_min >= pie->x_max)
1073 msg (SE, _("%s for pie chart must be greater than or equal to %s, "
1074 "but %s was specified as %.15g and %s as %.15g. "
1075 "%s and %s will be ignored."),
1080 pie->x_min = pie->x_max = SYSMIS;
1087 double previous_p = -1;
1088 qsort (frq.percentiles, frq.n_percentiles,
1089 sizeof (*frq.percentiles),
1092 frq.n_show_percentiles = 0;
1093 for (i = o = 0; i < frq.n_percentiles; ++i)
1095 frq.percentiles[o].p = frq.percentiles[i].p;
1097 if (frq.percentiles[i].show)
1098 frq.percentiles[o].show = true;
1100 if (frq.percentiles[i].p != previous_p)
1102 if (frq.percentiles[i].show)
1103 frq.n_show_percentiles++;
1108 previous_p = frq.percentiles[i].p;
1111 frq.n_percentiles = o;
1115 struct casegrouper *grouper;
1116 struct casereader *group;
1119 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
1120 while (casegrouper_get_next_group (grouper, &group))
1123 precalc (&frq, group, ds);
1124 for (; (c = casereader_read (group)) != NULL; case_unref (c))
1126 postcalc (&frq, ds);
1128 ok = casegrouper_destroy (grouper);
1129 ok = proc_commit (ds) && ok;
1141 calculate_iqr (const struct frq_proc *frq)
1147 /* This cannot work unless the 25th and 75th percentile are calculated */
1148 assert (frq->n_percentiles >= 2);
1149 for (i = 0; i < frq->n_percentiles; i++)
1151 struct percentile *pc = &frq->percentiles[i];
1153 if (fabs (0.25 - pc->p) < DBL_EPSILON)
1155 else if (fabs (0.75 - pc->p) < DBL_EPSILON)
1159 return q1 == SYSMIS || q3 == SYSMIS ? SYSMIS : q3 - q1;
1163 chart_includes_value (const struct frq_chart *chart,
1164 const struct variable *var,
1165 const union value *value)
1167 if (!chart->include_missing && var_is_value_missing (var, value, MV_ANY))
1170 if (var_is_numeric (var)
1171 && ((chart->x_min != SYSMIS && value->f < chart->x_min)
1172 || (chart->x_max != SYSMIS && value->f > chart->x_max)))
1178 /* Create a gsl_histogram from a freq_tab */
1179 static struct histogram *
1180 freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
1181 const struct variable *var)
1183 double x_min, x_max, valid_freq;
1186 struct histogram *histogram;
1189 /* Find out the extremes of the x value, within the range to be included in
1190 the histogram, and sum the total frequency of those values. */
1194 for (i = 0; i < ft->n_valid; i++)
1196 const struct freq *f = &ft->valid[i];
1197 if (chart_includes_value (frq->hist, var, &f->value))
1199 x_min = MIN (x_min, f->value.f);
1200 x_max = MAX (x_max, f->value.f);
1201 valid_freq += f->count;
1206 iqr = calculate_iqr (frq);
1209 /* Freedman-Diaconis' choice of bin width. */
1210 bin_width = 2 * iqr / pow (valid_freq, 1.0 / 3.0);
1214 bin_width = (x_max - x_min) / (1 + log2 (valid_freq));
1216 histogram = histogram_create (bin_width, x_min, x_max);
1218 if ( histogram == NULL)
1221 for (i = 0; i < ft->n_valid; i++)
1223 const struct freq *f = &ft->valid[i];
1224 if (chart_includes_value (frq->hist, var, &f->value))
1225 histogram_add (histogram, f->value.f, f->count);
1232 add_slice (const struct frq_chart *pie, const struct freq *freq,
1233 const struct variable *var, struct slice *slice)
1235 if (chart_includes_value (pie, var, &freq->value))
1237 ds_init_empty (&slice->label);
1238 var_append_value_name (var, &freq->value, &slice->label);
1239 slice->magnitude = freq->count;
1246 /* Allocate an array of slices and fill them from the data in frq_tab
1247 n_slices will contain the number of slices allocated.
1248 The caller is responsible for freeing slices
1250 static struct slice *
1251 freq_tab_to_slice_array(const struct frq_chart *pie,
1252 const struct freq_tab *frq_tab,
1253 const struct variable *var,
1256 struct slice *slices;
1260 slices = xnmalloc (frq_tab->n_valid + frq_tab->n_missing, sizeof *slices);
1263 for (i = 0; i < frq_tab->n_valid; i++)
1264 n_slices += add_slice (pie, &frq_tab->valid[i], var, &slices[n_slices]);
1265 for (i = 0; i < frq_tab->n_missing; i++)
1266 n_slices += add_slice (pie, &frq_tab->missing[i], var, &slices[n_slices]);
1268 *n_slicesp = n_slices;
1274 do_piechart(const struct frq_chart *pie, const struct variable *var,
1275 const struct freq_tab *frq_tab)
1277 struct slice *slices;
1280 slices = freq_tab_to_slice_array (pie, frq_tab, var, &n_slices);
1283 msg (SW, _("Omitting pie chart for %s, which has only %d unique values."),
1284 var_get_name (var), n_slices);
1285 else if (n_slices > 50)
1286 msg (SW, _("Omitting pie chart for %s, which has over 50 unique values."),
1287 var_get_name (var));
1289 chart_item_submit (piechart_create (var_to_string(var), slices, n_slices));
1291 for (i = 0; i < n_slices; i++)
1292 ds_destroy (&slices[i].label);
1296 /* Calculates all the pertinent statistics for VF, putting them in array
1299 calc_stats (const struct var_freqs *vf, double d[FRQ_ST_count])
1301 const struct freq_tab *ft = &vf->tab;
1302 double W = ft->valid_cases;
1303 const struct freq *f;
1305 int most_often = -1;
1306 double X_mode = SYSMIS;
1308 /* Calculate the mode. */
1309 for (f = ft->valid; f < ft->missing; f++)
1311 if (most_often < f->count)
1313 most_often = f->count;
1314 X_mode = f->value.f;
1316 else if (most_often == f->count)
1318 /* A duplicate mode is undefined.
1319 FIXME: keep track of *all* the modes. */
1324 /* Calculate moments. */
1325 m = moments_create (MOMENT_KURTOSIS);
1326 for (f = ft->valid; f < ft->missing; f++)
1327 moments_pass_one (m, f->value.f, f->count);
1328 for (f = ft->valid; f < ft->missing; f++)
1329 moments_pass_two (m, f->value.f, f->count);
1330 moments_calculate (m, NULL, &d[FRQ_ST_MEAN], &d[FRQ_ST_VARIANCE],
1331 &d[FRQ_ST_SKEWNESS], &d[FRQ_ST_KURTOSIS]);
1332 moments_destroy (m);
1334 /* Formulae below are taken from _SPSS Statistical Algorithms_. */
1335 d[FRQ_ST_MINIMUM] = ft->valid[0].value.f;
1336 d[FRQ_ST_MAXIMUM] = ft->valid[ft->n_valid - 1].value.f;
1337 d[FRQ_ST_MODE] = X_mode;
1338 d[FRQ_ST_RANGE] = d[FRQ_ST_MAXIMUM] - d[FRQ_ST_MINIMUM];
1339 d[FRQ_ST_SUM] = d[FRQ_ST_MEAN] * W;
1340 d[FRQ_ST_STDDEV] = sqrt (d[FRQ_ST_VARIANCE]);
1341 d[FRQ_ST_SEMEAN] = d[FRQ_ST_STDDEV] / sqrt (W);
1342 d[FRQ_ST_SESKEWNESS] = calc_seskew (W);
1343 d[FRQ_ST_SEKURTOSIS] = calc_sekurt (W);
1346 /* Displays a table of all the statistics requested for variable V. */
1348 dump_statistics (const struct frq_proc *frq, const struct var_freqs *vf,
1349 const struct variable *wv)
1351 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
1352 const struct freq_tab *ft = &vf->tab;
1353 double stat_value[FRQ_ST_count];
1354 struct tab_table *t;
1355 int i, r = 2; /* N missing and N valid are always dumped */
1357 if (var_is_alpha (vf->var))
1360 calc_stats (vf, stat_value);
1362 t = tab_create (3, ((frq->stats & BIT_INDEX (FRQ_ST_MEDIAN)) ? frq->n_stats - 1 : frq->n_stats)
1363 + frq->n_show_percentiles + 2);
1365 tab_set_format (t, RC_WEIGHT, wfmt);
1366 tab_box (t, TAL_1, TAL_1, -1, -1 , 0 , 0 , 2, tab_nr(t) - 1) ;
1368 tab_vline (t, TAL_1 , 2, 0, tab_nr(t) - 1);
1369 tab_vline (t, TAL_GAP , 1, 0, tab_nr(t) - 1 ) ;
1371 for (i = 0; i < FRQ_ST_count; i++)
1373 if (FRQ_ST_MEDIAN == i)
1376 if (frq->stats & BIT_INDEX (i))
1378 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
1379 gettext (st_name[i]));
1381 if (vf->tab.n_valid <= 0 && r >= 2)
1382 tab_text (t, 2, r, 0, ".");
1384 tab_double (t, 2, r, TAB_NONE, stat_value[i], NULL, RC_OTHER);
1389 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("N"));
1390 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Valid"));
1391 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Missing"));
1393 tab_double (t, 2, 0, TAB_NONE, ft->valid_cases, NULL, RC_WEIGHT);
1394 tab_double (t, 2, 1, TAB_NONE, ft->total_cases - ft->valid_cases, NULL, RC_WEIGHT);
1396 for (i = 0; i < frq->n_percentiles; i++)
1398 const struct percentile *pc = &frq->percentiles[i];
1405 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Percentiles"));
1408 if (vf->tab.n_valid <= 0)
1410 tab_text (t, 2, r, 0, ".");
1416 tab_text (t, 1, r, TAB_LEFT, _("50 (Median)"));
1418 tab_double (t, 1, r, TAB_LEFT, pc->p * 100, NULL, RC_INTEGER);
1419 tab_double (t, 2, r, TAB_NONE, pc->value,
1420 var_get_print_format (vf->var), RC_OTHER);
1425 tab_title (t, "%s", var_to_string (vf->var));