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_headers (t, 0, 0, 1, 0);
291 for (x = 0; x < 6; x++)
292 tab_text (t, x, 0, TAB_CENTER | TAT_TITLE, gettext (headings[x]));
295 for (f = ft->valid; f < ft->missing; f++)
298 double percent, valid_percent;
300 cum_freq += f->count;
302 percent = f->count / ft->total_cases * 100.0;
303 valid_percent = f->count / ft->valid_cases * 100.0;
304 cum_total += valid_percent;
306 label = var_lookup_value_label (vf->var, &f->value);
308 tab_text (t, 0, r, TAB_LEFT, label);
310 tab_value (t, 1, r, TAB_NONE, &f->value, vf->var, NULL);
311 tab_double (t, 2, r, TAB_NONE, f->count, wfmt);
312 tab_double (t, 3, r, TAB_NONE, percent, NULL);
313 tab_double (t, 4, r, TAB_NONE, valid_percent, NULL);
314 tab_double (t, 5, r, TAB_NONE, cum_total, NULL);
317 for (; f < &ft->valid[n_categories]; f++)
321 cum_freq += f->count;
323 label = var_lookup_value_label (vf->var, &f->value);
325 tab_text (t, 0, r, TAB_LEFT, label);
327 tab_value (t, 1, r, TAB_NONE, &f->value, vf->var, NULL);
328 tab_double (t, 2, r, TAB_NONE, f->count, wfmt);
329 tab_double (t, 3, r, TAB_NONE,
330 f->count / ft->total_cases * 100.0, NULL);
331 tab_text (t, 4, r, TAB_NONE, _("Missing"));
335 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, 5, r);
336 tab_hline (t, TAL_2, 0, 5, 1);
337 tab_hline (t, TAL_2, 0, 5, r);
338 tab_joint_text (t, 0, r, 1, r, TAB_RIGHT | TAT_TITLE, _("Total"));
339 tab_vline (t, TAL_0, 1, r, r);
340 tab_double (t, 2, r, TAB_NONE, cum_freq, wfmt);
341 tab_fixed (t, 3, r, TAB_NONE, 100.0, 5, 1);
342 tab_fixed (t, 4, r, TAB_NONE, 100.0, 5, 1);
344 tab_title (t, "%s", var_to_string (vf->var));
348 /* Statistical display. */
351 calc_percentile (double p, double valid_cases, double x1, double x2)
355 s = (settings_get_algorithm () != COMPATIBLE
356 ? modf ((valid_cases - 1) * p, &dummy)
357 : modf ((valid_cases + 1) * p - 1, &dummy));
359 return x1 + (x2 - x1) * s;
362 /* Calculates all of the percentiles for VF within FRQ. */
364 calc_percentiles (const struct frq_proc *frq, const struct var_freqs *vf)
366 const struct freq_tab *ft = &vf->tab;
367 double W = ft->valid_cases;
368 const struct freq *f;
372 assert (ft->n_valid > 0);
376 for (f = ft->valid; f < ft->missing; f++)
379 for (; percentile_idx < frq->n_percentiles; percentile_idx++)
381 struct percentile *pc = &frq->percentiles[percentile_idx];
384 tp = (settings_get_algorithm () == ENHANCED
386 : (W + 1) * pc->p - 1);
391 if (tp + 1 < rank || f + 1 >= ft->missing)
392 pc->value = f->value.f;
394 pc->value = calc_percentile (pc->p, W, f->value.f, f[1].value.f);
397 for (; percentile_idx < frq->n_percentiles; percentile_idx++)
399 struct percentile *pc = &frq->percentiles[percentile_idx];
400 pc->value = ft->valid[ft->n_valid - 1].value.f;
404 /* Returns true iff the value in struct freq F is non-missing
407 not_missing (const void *f_, const void *v_)
409 const struct freq *f = f_;
410 const struct variable *v = v_;
412 return !var_is_value_missing (v, &f->value, MV_ANY);
416 /* Summarizes the frequency table data for variable V. */
418 postprocess_freq_tab (const struct frq_proc *frq, struct var_freqs *vf)
420 struct freq_tab *ft = &vf->tab;
421 struct freq_compare_aux aux;
423 struct freq *freqs, *f;
426 /* Extract data from hash table. */
427 count = hmap_count (&ft->data);
428 freqs = freq_hmap_extract (&ft->data);
430 /* Put data into ft. */
432 ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, vf->var);
433 ft->missing = freqs + ft->n_valid;
434 ft->n_missing = count - ft->n_valid;
437 aux.by_freq = frq->sort == FRQ_AFREQ || frq->sort == FRQ_DFREQ;
438 aux.ascending_freq = frq->sort != FRQ_DFREQ;
439 aux.width = vf->width;
440 aux.ascending_value = frq->sort != FRQ_DVALUE;
441 sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare_freq, &aux);
442 sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare_freq, &aux);
444 /* Summary statistics. */
445 ft->valid_cases = 0.0;
446 for(i = 0 ; i < ft->n_valid ; ++i )
449 ft->valid_cases += f->count;
453 ft->total_cases = ft->valid_cases ;
454 for(i = 0 ; i < ft->n_missing ; ++i )
457 ft->total_cases += f->count;
462 /* Frees the frequency table for variable V. */
464 cleanup_freq_tab (struct var_freqs *vf)
466 free (vf->tab.valid);
467 freq_hmap_destroy (&vf->tab.data, vf->width);
470 /* Add data from case C to the frequency table. */
472 calc (struct frq_proc *frq, const struct ccase *c, const struct dataset *ds)
474 double weight = dict_get_case_weight (dataset_dict (ds), c, NULL);
477 for (i = 0; i < frq->n_vars; i++)
479 struct var_freqs *vf = &frq->vars[i];
480 const union value *value = case_data (c, vf->var);
481 size_t hash = value_hash (value, vf->width, 0);
484 f = freq_hmap_search (&vf->tab.data, value, vf->width, hash);
486 f = freq_hmap_insert (&vf->tab.data, value, vf->width, hash);
492 /* Prepares each variable that is the target of FREQUENCIES by setting
493 up its hash table. */
495 precalc (struct frq_proc *frq, struct casereader *input, struct dataset *ds)
500 c = casereader_peek (input, 0);
503 output_split_file_values (ds, c);
507 for (i = 0; i < frq->n_vars; i++)
508 hmap_init (&frq->vars[i].tab.data);
511 /* Finishes up with the variables after frequencies have been
512 calculated. Displays statistics, percentiles, ... */
514 postcalc (struct frq_proc *frq, const struct dataset *ds)
516 const struct dictionary *dict = dataset_dict (ds);
517 const struct variable *wv = dict_get_weight (dict);
520 for (i = 0; i < frq->n_vars; i++)
522 struct var_freqs *vf = &frq->vars[i];
524 postprocess_freq_tab (frq, vf);
526 /* Frequencies tables. */
527 if (vf->tab.n_valid + vf->tab.n_missing <= frq->max_categories)
528 dump_freq_table (vf, wv);
530 calc_percentiles (frq, vf);
534 dump_statistics (frq, vf, wv);
536 if (frq->hist && var_is_numeric (vf->var) && vf->tab.n_valid > 0)
538 double d[FRQ_ST_count];
539 struct histogram *histogram;
543 histogram = freq_tab_to_hist (frq, &vf->tab, vf->var);
547 chart_item_submit (histogram_chart_create (
548 histogram->gsl_hist, var_to_string(vf->var),
552 frq->hist->draw_normal));
554 statistic_destroy (&histogram->parent);
559 do_piechart(frq->pie, vf->var, &vf->tab);
561 cleanup_freq_tab (vf);
566 cmd_frequencies (struct lexer *lexer, struct dataset *ds)
570 const struct variable **vars;
572 bool sbc_barchart = false;
573 bool sbc_piechart = false;
574 bool sbc_histogram = false;
576 double pie_min = -DBL_MAX;
577 double pie_max = DBL_MAX;
578 bool pie_missing = false;
580 double hi_min = -DBL_MAX;
581 double hi_max = DBL_MAX;
582 int hi_scale = FRQ_FREQ;
583 int hi_freq = INT_MIN;
584 int hi_pcnt = INT_MIN;
585 int hi_norm = FRQ_NONORMAL;
587 frq.pool = pool_create ();
588 frq.sort = FRQ_AVALUE;
593 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
594 | BIT_INDEX (FRQ_ST_STDDEV)
595 | BIT_INDEX (FRQ_ST_MINIMUM)
596 | BIT_INDEX (FRQ_ST_MAXIMUM);
600 frq.max_categories = INT_MAX;
602 frq.percentiles = NULL;
603 frq.n_percentiles = 0;
604 frq.n_show_percentiles = 0;
610 /* Accept an optional, completely pointless "/VARIABLES=" */
611 lex_match (lexer, T_SLASH);
612 if (lex_match_id (lexer, "VARIABLES"))
614 if (! lex_force_match (lexer, T_EQUALS) )
618 if (!parse_variables_const (lexer, dataset_dict (ds),
624 frq.vars = xzalloc (frq.n_vars * sizeof (*frq.vars));
625 for (i = 0; i < frq.n_vars; ++i)
627 frq.vars[i].var = vars[i];
630 while (lex_token (lexer) != T_ENDCMD)
632 lex_match (lexer, T_SLASH);
634 if (lex_match_id (lexer, "STATISTICS"))
636 lex_match (lexer, T_EQUALS);
641 while (lex_token (lexer) != T_ENDCMD
642 && lex_token (lexer) != T_SLASH)
644 if (lex_match_id (lexer, "DEFAULT"))
646 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
647 | BIT_INDEX (FRQ_ST_STDDEV)
648 | BIT_INDEX (FRQ_ST_MINIMUM)
649 | BIT_INDEX (FRQ_ST_MAXIMUM);
653 else if (lex_match_id (lexer, "MEAN"))
655 frq.stats |= BIT_INDEX (FRQ_ST_MEAN);
658 else if (lex_match_id (lexer, "SEMEAN"))
660 frq.stats |= BIT_INDEX (FRQ_ST_SEMEAN);
663 else if (lex_match_id (lexer, "MEDIAN"))
665 frq.stats |= BIT_INDEX (FRQ_ST_MEDIAN);
668 else if (lex_match_id (lexer, "MODE"))
670 frq.stats |= BIT_INDEX (FRQ_ST_MODE);
673 else if (lex_match_id (lexer, "STDDEV"))
675 frq.stats |= BIT_INDEX (FRQ_ST_STDDEV);
678 else if (lex_match_id (lexer, "VARIANCE"))
680 frq.stats |= BIT_INDEX (FRQ_ST_MEAN);
683 else if (lex_match_id (lexer, "KURTOSIS"))
685 frq.stats |= BIT_INDEX (FRQ_ST_KURTOSIS);
688 else if (lex_match_id (lexer, "SKEWNESS"))
690 frq.stats |= BIT_INDEX (FRQ_ST_SKEWNESS);
693 else if (lex_match_id (lexer, "RANGE"))
695 frq.stats |= BIT_INDEX (FRQ_ST_RANGE);
698 else if (lex_match_id (lexer, "MINIMUM"))
700 frq.stats |= BIT_INDEX (FRQ_ST_MINIMUM);
703 else if (lex_match_id (lexer, "MAXIMUM"))
705 frq.stats |= BIT_INDEX (FRQ_ST_MAXIMUM);
708 else if (lex_match_id (lexer, "SUM"))
710 frq.stats |= BIT_INDEX (FRQ_ST_SUM);
713 else if (lex_match_id (lexer, "SESKEWNESS"))
715 frq.stats |= BIT_INDEX (FRQ_ST_SESKEWNESS);
718 else if (lex_match_id (lexer, "SEKURTOSIS"))
720 frq.stats |= BIT_INDEX (FRQ_ST_SEKURTOSIS);
723 else if (lex_match_id (lexer, "NONE"))
728 else if (lex_match (lexer, T_ALL))
731 frq.n_stats = FRQ_ST_count;
735 lex_error (lexer, NULL);
740 else if (lex_match_id (lexer, "PERCENTILES"))
742 lex_match (lexer, T_EQUALS);
743 while (lex_token (lexer) != T_ENDCMD
744 && lex_token (lexer) != T_SLASH)
746 if (lex_force_num (lexer))
749 xrealloc (frq.percentiles,
750 (frq.n_percentiles + 1)
751 * sizeof (*frq.percentiles));
752 frq.percentiles[frq.n_percentiles].p = lex_number (lexer) / 100.0;
753 frq.percentiles[frq.n_percentiles].show = true;
756 frq.n_show_percentiles++;
760 lex_error (lexer, NULL);
765 else if (lex_match_id (lexer, "FORMAT"))
767 lex_match (lexer, T_EQUALS);
768 while (lex_token (lexer) != T_ENDCMD
769 && lex_token (lexer) != T_SLASH)
771 if (lex_match_id (lexer, "TABLE"))
775 else if (lex_match_id (lexer, "NOTABLE"))
777 frq.max_categories = 0;
779 else if (lex_match_id (lexer, "AVALUE"))
781 frq.sort = FRQ_AVALUE;
783 else if (lex_match_id (lexer, "DVALUE"))
785 frq.sort = FRQ_DVALUE;
787 else if (lex_match_id (lexer, "AFREQ"))
789 frq.sort = FRQ_AFREQ;
791 else if (lex_match_id (lexer, "DFREQ"))
793 frq.sort = FRQ_DFREQ;
797 lex_error (lexer, NULL);
802 else if (lex_match_id (lexer, "NTILES"))
804 lex_match (lexer, T_EQUALS);
806 if (lex_force_int (lexer))
809 int n = lex_integer (lexer);
811 for (i = 0; i < n + 1; ++i)
814 xrealloc (frq.percentiles,
815 (frq.n_percentiles + 1)
816 * sizeof (*frq.percentiles));
817 frq.percentiles[frq.n_percentiles].p =
819 frq.percentiles[frq.n_percentiles].show = true;
822 frq.n_show_percentiles++;
827 lex_error (lexer, NULL);
831 else if (lex_match_id (lexer, "ALGORITHM"))
833 lex_match (lexer, T_EQUALS);
835 if (lex_match_id (lexer, "COMPATIBLE"))
837 settings_set_cmd_algorithm (COMPATIBLE);
839 else if (lex_match_id (lexer, "ENHANCED"))
841 settings_set_cmd_algorithm (ENHANCED);
845 lex_error (lexer, NULL);
849 else if (lex_match_id (lexer, "HISTOGRAM"))
851 lex_match (lexer, T_EQUALS);
852 sbc_histogram = true;
854 while (lex_token (lexer) != T_ENDCMD
855 && lex_token (lexer) != T_SLASH)
857 if (lex_match_id (lexer, "NORMAL"))
859 hi_norm = FRQ_NORMAL;
861 else if (lex_match_id (lexer, "NONORMAL"))
863 hi_norm = FRQ_NONORMAL;
865 else if (lex_match_id (lexer, "FREQ"))
868 if (lex_match (lexer, T_LPAREN))
870 if (lex_force_int (lexer))
872 hi_freq = lex_integer (lexer);
875 lex_error (lexer, _("Histogram frequency must be greater than zero."));
878 lex_force_match (lexer, T_RPAREN);
882 else if (lex_match_id (lexer, "PERCENT"))
884 hi_scale = FRQ_PERCENT;
885 if (lex_match (lexer, T_LPAREN))
887 if (lex_force_int (lexer))
889 hi_pcnt = lex_integer (lexer);
892 lex_error (lexer, _("Histogram percentaage must be greater than zero."));
895 lex_force_match (lexer, T_RPAREN);
899 else if (lex_match_id (lexer, "MINIMUM"))
901 lex_force_match (lexer, T_LPAREN);
902 if (lex_force_num (lexer))
904 hi_min = lex_number (lexer);
907 lex_force_match (lexer, T_RPAREN);
909 else if (lex_match_id (lexer, "MAXIMUM"))
911 lex_force_match (lexer, T_LPAREN);
912 if (lex_force_num (lexer))
914 hi_max = lex_number (lexer);
917 lex_force_match (lexer, T_RPAREN);
921 lex_error (lexer, NULL);
926 else if (lex_match_id (lexer, "PIECHART"))
928 lex_match (lexer, T_EQUALS);
929 while (lex_token (lexer) != T_ENDCMD
930 && lex_token (lexer) != T_SLASH)
932 if (lex_match_id (lexer, "MINIMUM"))
934 lex_force_match (lexer, T_LPAREN);
935 if (lex_force_num (lexer))
937 pie_min = lex_number (lexer);
940 lex_force_match (lexer, T_RPAREN);
942 else if (lex_match_id (lexer, "MAXIMUM"))
944 lex_force_match (lexer, T_LPAREN);
945 if (lex_force_num (lexer))
947 pie_max = lex_number (lexer);
950 lex_force_match (lexer, T_RPAREN);
952 else if (lex_match_id (lexer, "MISSING"))
956 else if (lex_match_id (lexer, "NOMISSING"))
962 lex_error (lexer, NULL);
968 else if (lex_match_id (lexer, "MISSING"))
970 lex_match (lexer, T_EQUALS);
972 while (lex_token (lexer) != T_ENDCMD
973 && lex_token (lexer) != T_SLASH)
975 if (lex_match_id (lexer, "EXCLUDE"))
978 else if (lex_match_id (lexer, "INCLUDE"))
983 lex_error (lexer, NULL);
990 lex_error (lexer, NULL);
995 if (frq.stats & FRQ_ST_MEDIAN)
998 xrealloc (frq.percentiles,
999 (frq.n_percentiles + 1)
1000 * sizeof (*frq.percentiles));
1002 frq.percentiles[frq.n_percentiles].p = 0.50;
1003 frq.percentiles[frq.n_percentiles].show = true;
1005 frq.n_percentiles++;
1009 /* Figure out which charts the user requested. */
1013 msg (SW, _("Bar charts are not implemented."));
1017 struct frq_chart *hist;
1019 hist = frq.hist = xmalloc (sizeof *frq.hist);
1020 hist->x_min = hi_min;
1021 hist->x_max = hi_max;
1022 hist->y_scale = hi_scale;
1023 hist->y_max = hi_scale == FRQ_FREQ ? hi_freq : hi_pcnt;
1024 hist->draw_normal = hi_norm != FRQ_NONORMAL;
1025 hist->include_missing = false;
1027 if (hist->x_min != SYSMIS && hist->x_max != SYSMIS
1028 && hist->x_min >= hist->x_max)
1030 msg (SE, _("%s for histogram must be greater than or equal to %s, "
1031 "but %s was specified as %.15g and %s as %.15g. "
1032 "%s and %s will be ignored."),
1037 hist->x_min = hist->x_max = SYSMIS;
1041 xrealloc (frq.percentiles,
1042 (frq.n_percentiles + 2)
1043 * sizeof (*frq.percentiles));
1045 frq.percentiles[frq.n_percentiles].p = 0.25;
1046 frq.percentiles[frq.n_percentiles].show = false;
1048 frq.percentiles[frq.n_percentiles + 1].p = 0.75;
1049 frq.percentiles[frq.n_percentiles + 1].show = false;
1051 frq.n_percentiles+=2;
1056 struct frq_chart *pie;
1058 pie = frq.pie = xmalloc (sizeof *frq.pie);
1059 pie->x_min = pie_min;
1060 pie->x_max = pie_max;
1061 pie->include_missing = pie_missing;
1063 if (pie->x_min != SYSMIS && pie->x_max != SYSMIS
1064 && pie->x_min >= pie->x_max)
1066 msg (SE, _("%s for pie chart must be greater than or equal to %s, "
1067 "but %s was specified as %.15g and %s as %.15g. "
1068 "%s and %s will be ignored."),
1073 pie->x_min = pie->x_max = SYSMIS;
1080 double previous_p = -1;
1081 qsort (frq.percentiles, frq.n_percentiles,
1082 sizeof (*frq.percentiles),
1085 frq.n_show_percentiles = 0;
1086 for (i = o = 0; i < frq.n_percentiles; ++i)
1088 frq.percentiles[o].p = frq.percentiles[i].p;
1090 if (frq.percentiles[i].show)
1091 frq.percentiles[o].show = true;
1093 if (frq.percentiles[i].p != previous_p)
1095 if (frq.percentiles[i].show)
1096 frq.n_show_percentiles++;
1101 previous_p = frq.percentiles[i].p;
1104 frq.n_percentiles = o;
1108 struct casegrouper *grouper;
1109 struct casereader *group;
1112 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
1113 while (casegrouper_get_next_group (grouper, &group))
1116 precalc (&frq, group, ds);
1117 for (; (c = casereader_read (group)) != NULL; case_unref (c))
1119 postcalc (&frq, ds);
1121 ok = casegrouper_destroy (grouper);
1122 ok = proc_commit (ds) && ok;
1134 calculate_iqr (const struct frq_proc *frq)
1140 /* This cannot work unless the 25th and 75th percentile are calculated */
1141 assert (frq->n_percentiles >= 2);
1142 for (i = 0; i < frq->n_percentiles; i++)
1144 struct percentile *pc = &frq->percentiles[i];
1146 if (fabs (0.25 - pc->p) < DBL_EPSILON)
1148 else if (fabs (0.75 - pc->p) < DBL_EPSILON)
1152 return q1 == SYSMIS || q3 == SYSMIS ? SYSMIS : q3 - q1;
1156 chart_includes_value (const struct frq_chart *chart,
1157 const struct variable *var,
1158 const union value *value)
1160 if (!chart->include_missing && var_is_value_missing (var, value, MV_ANY))
1163 if (var_is_numeric (var)
1164 && ((chart->x_min != SYSMIS && value->f < chart->x_min)
1165 || (chart->x_max != SYSMIS && value->f > chart->x_max)))
1171 /* Create a gsl_histogram from a freq_tab */
1172 static struct histogram *
1173 freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
1174 const struct variable *var)
1176 double x_min, x_max, valid_freq;
1179 struct histogram *histogram;
1182 /* Find out the extremes of the x value, within the range to be included in
1183 the histogram, and sum the total frequency of those values. */
1187 for (i = 0; i < ft->n_valid; i++)
1189 const struct freq *f = &ft->valid[i];
1190 if (chart_includes_value (frq->hist, var, &f->value))
1192 x_min = MIN (x_min, f->value.f);
1193 x_max = MAX (x_max, f->value.f);
1194 valid_freq += f->count;
1199 iqr = calculate_iqr (frq);
1202 /* Freedman-Diaconis' choice of bin width. */
1203 bin_width = 2 * iqr / pow (valid_freq, 1.0 / 3.0);
1207 bin_width = (x_max - x_min) / (1 + log2 (valid_freq));
1209 histogram = histogram_create (bin_width, x_min, x_max);
1211 if ( histogram == NULL)
1214 for (i = 0; i < ft->n_valid; i++)
1216 const struct freq *f = &ft->valid[i];
1217 if (chart_includes_value (frq->hist, var, &f->value))
1218 histogram_add (histogram, f->value.f, f->count);
1225 add_slice (const struct frq_chart *pie, const struct freq *freq,
1226 const struct variable *var, struct slice *slice)
1228 if (chart_includes_value (pie, var, &freq->value))
1230 ds_init_empty (&slice->label);
1231 var_append_value_name (var, &freq->value, &slice->label);
1232 slice->magnitude = freq->count;
1239 /* Allocate an array of slices and fill them from the data in frq_tab
1240 n_slices will contain the number of slices allocated.
1241 The caller is responsible for freeing slices
1243 static struct slice *
1244 freq_tab_to_slice_array(const struct frq_chart *pie,
1245 const struct freq_tab *frq_tab,
1246 const struct variable *var,
1249 struct slice *slices;
1253 slices = xnmalloc (frq_tab->n_valid + frq_tab->n_missing, sizeof *slices);
1256 for (i = 0; i < frq_tab->n_valid; i++)
1257 n_slices += add_slice (pie, &frq_tab->valid[i], var, &slices[n_slices]);
1258 for (i = 0; i < frq_tab->n_missing; i++)
1259 n_slices += add_slice (pie, &frq_tab->missing[i], var, &slices[n_slices]);
1261 *n_slicesp = n_slices;
1267 do_piechart(const struct frq_chart *pie, const struct variable *var,
1268 const struct freq_tab *frq_tab)
1270 struct slice *slices;
1273 slices = freq_tab_to_slice_array (pie, frq_tab, var, &n_slices);
1276 msg (SW, _("Omitting pie chart for %s, which has only %d unique values."),
1277 var_get_name (var), n_slices);
1278 else if (n_slices > 50)
1279 msg (SW, _("Omitting pie chart for %s, which has over 50 unique values."),
1280 var_get_name (var));
1282 chart_item_submit (piechart_create (var_to_string(var), slices, n_slices));
1284 for (i = 0; i < n_slices; i++)
1285 ds_destroy (&slices[i].label);
1289 /* Calculates all the pertinent statistics for VF, putting them in array
1292 calc_stats (const struct var_freqs *vf, double d[FRQ_ST_count])
1294 const struct freq_tab *ft = &vf->tab;
1295 double W = ft->valid_cases;
1296 const struct freq *f;
1301 assert (ft->n_valid > 0);
1303 /* Calculate the mode. */
1306 for (f = ft->valid; f < ft->missing; f++)
1308 if (most_often < f->count)
1310 most_often = f->count;
1311 X_mode = f->value.f;
1313 else if (most_often == f->count)
1315 /* A duplicate mode is undefined.
1316 FIXME: keep track of *all* the modes. */
1321 /* Calculate moments. */
1322 m = moments_create (MOMENT_KURTOSIS);
1323 for (f = ft->valid; f < ft->missing; f++)
1324 moments_pass_one (m, f->value.f, f->count);
1325 for (f = ft->valid; f < ft->missing; f++)
1326 moments_pass_two (m, f->value.f, f->count);
1327 moments_calculate (m, NULL, &d[FRQ_ST_MEAN], &d[FRQ_ST_VARIANCE],
1328 &d[FRQ_ST_SKEWNESS], &d[FRQ_ST_KURTOSIS]);
1329 moments_destroy (m);
1331 /* Formulas below are taken from _SPSS Statistical Algorithms_. */
1332 d[FRQ_ST_MINIMUM] = ft->valid[0].value.f;
1333 d[FRQ_ST_MAXIMUM] = ft->valid[ft->n_valid - 1].value.f;
1334 d[FRQ_ST_MODE] = X_mode;
1335 d[FRQ_ST_RANGE] = d[FRQ_ST_MAXIMUM] - d[FRQ_ST_MINIMUM];
1336 d[FRQ_ST_SUM] = d[FRQ_ST_MEAN] * W;
1337 d[FRQ_ST_STDDEV] = sqrt (d[FRQ_ST_VARIANCE]);
1338 d[FRQ_ST_SEMEAN] = d[FRQ_ST_STDDEV] / sqrt (W);
1339 d[FRQ_ST_SESKEWNESS] = calc_seskew (W);
1340 d[FRQ_ST_SEKURTOSIS] = calc_sekurt (W);
1343 /* Displays a table of all the statistics requested for variable V. */
1345 dump_statistics (const struct frq_proc *frq, const struct var_freqs *vf,
1346 const struct variable *wv)
1348 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : &F_8_0;
1349 const struct freq_tab *ft = &vf->tab;
1350 double stat_value[FRQ_ST_count];
1351 struct tab_table *t;
1354 if (var_is_alpha (vf->var))
1357 if (ft->n_valid == 0)
1359 msg (SW, _("No valid data for variable %s; statistics not displayed."),
1360 var_get_name (vf->var));
1363 calc_stats (vf, stat_value);
1365 t = tab_create (3, ((frq->stats & FRQ_ST_MEDIAN) ? frq->n_stats - 1 : frq->n_stats)
1366 + frq->n_show_percentiles + 2);
1368 tab_box (t, TAL_1, TAL_1, -1, -1 , 0 , 0 , 2, tab_nr(t) - 1) ;
1371 tab_vline (t, TAL_1 , 2, 0, tab_nr(t) - 1);
1372 tab_vline (t, TAL_GAP , 1, 0, tab_nr(t) - 1 ) ;
1374 r = 2; /* N missing and N valid are always dumped */
1376 for (i = 0; i < FRQ_ST_count; i++)
1378 if (FRQ_ST_MEDIAN == i)
1381 if (frq->stats & BIT_INDEX (i))
1383 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
1384 gettext (st_name[i]));
1385 tab_double (t, 2, r, TAB_NONE, stat_value[i], NULL);
1390 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("N"));
1391 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Valid"));
1392 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Missing"));
1394 tab_double (t, 2, 0, TAB_NONE, ft->valid_cases, wfmt);
1395 tab_double (t, 2, 1, TAB_NONE, ft->total_cases - ft->valid_cases, wfmt);
1397 for (i = 0; i < frq->n_percentiles; i++)
1399 const struct percentile *pc = &frq->percentiles[i];
1406 tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Percentiles"));
1410 tab_text (t, 1, r, TAB_LEFT, _("50 (Median)"));
1412 tab_fixed (t, 1, r, TAB_LEFT, pc->p * 100, 3, 0);
1413 tab_double (t, 2, r, TAB_NONE, pc->value,
1414 var_get_print_format (vf->var));
1418 tab_title (t, "%s", var_to_string (vf->var));