2 PSPP - a program for statistical analysis.
3 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2014, 2015 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"
49 #include "math/histogram.h"
50 #include "math/moments.h"
51 #include "math/chart-geometry.h"
54 #include "output/charts/barchart.h"
55 #include "output/charts/piechart.h"
56 #include "output/charts/plot-hist.h"
57 #include "output/pivot-table.h"
59 #include "gl/minmax.h"
60 #include "gl/xalloc.h"
63 #define _(msgid) gettext (msgid)
64 #define N_(msgid) msgid
66 /* Percentiles to calculate. */
70 double p; /* the %ile to be calculated */
71 double value; /* the %ile's value */
72 bool show; /* True to show this percentile in the statistics box. */
76 ptile_3way (const void *_p1, const void *_p2)
78 const struct percentile *p1 = _p1;
79 const struct percentile *p2 = _p2;
86 if (p1->show > p2->show)
89 return (p1->show < p2->show);
92 return (p1->p > p2->p);
116 /* Array indices for STATISTICS subcommand. */
136 /* Description of statistics. */
137 static const char *st_name[FRQ_ST_count] =
157 struct hmap data; /* Hash table for accumulating counts. */
158 struct freq *valid; /* Valid freqs. */
159 int n_valid; /* Number of total freqs. */
160 const struct dictionary *dict; /* Source of entries in the table. */
162 struct freq *missing; /* Missing freqs. */
163 int n_missing; /* Number of missing freqs. */
166 double total_cases; /* Sum of weights of all cases. */
167 double valid_cases; /* Sum of weights of valid cases. */
172 double x_min; /* X axis minimum value. */
173 double x_max; /* X axis maximum value. */
174 int y_scale; /* Y axis scale: FRQ_FREQ or FRQ_PERCENT. */
176 /* Histograms only. */
177 double y_max; /* Y axis maximum value. */
178 bool draw_normal; /* Whether to draw normal curve. */
180 /* Pie charts only. */
181 bool include_missing; /* Whether to include missing values. */
184 /* Per-variable frequency data. */
187 const struct variable *var;
189 /* Freqency table. */
190 struct freq_tab tab; /* Frequencies table to use. */
193 double stat[FRQ_ST_count];
195 /* Variable attributes. */
203 struct var_freqs *vars;
206 /* Percentiles to calculate and possibly display. */
207 struct percentile *percentiles;
208 const struct percentile *median;
211 /* Frequency table display. */
212 long int max_categories; /* Maximum categories to show. */
213 int sort; /* FRQ_AVALUE or FRQ_DVALUE
214 or FRQ_AFREQ or FRQ_DFREQ. */
216 /* Statistics; number of statistics. */
220 /* Histogram and pie chart settings. */
221 struct frq_chart *hist, *pie, *bar;
227 struct freq_compare_aux
233 bool ascending_value;
236 static void calc_stats (const struct frq_proc *,
237 const struct var_freqs *, double d[FRQ_ST_count]);
239 static void do_piechart(const struct frq_chart *pie,
240 const struct variable *var,
241 const struct freq_tab *frq_tab);
243 static void do_barchart(const struct frq_chart *bar,
244 const struct variable **var,
245 const struct freq_tab *frq_tab);
247 static void dump_statistics (const struct frq_proc *frq,
248 const struct variable *wv);
251 compare_freq (const void *a_, const void *b_, const void *aux_)
253 const struct freq_compare_aux *aux = aux_;
254 const struct freq *a = a_;
255 const struct freq *b = b_;
257 if (aux->by_freq && a->count != b->count)
259 int cmp = a->count > b->count ? 1 : -1;
260 return aux->ascending_freq ? cmp : -cmp;
264 int cmp = value_compare_3way (a->values, b->values, aux->width);
265 return aux->ascending_value ? cmp : -cmp;
269 /* Create a gsl_histogram from a freq_tab */
270 static struct histogram *
271 freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
272 const struct variable *var);
275 put_freq_row (struct pivot_table *table, int var_idx,
276 double frequency, double percent,
277 double valid_percent, double cum_percent)
279 double entries[] = { frequency, percent, valid_percent, cum_percent };
280 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
281 if (entries[i] != SYSMIS)
282 pivot_table_put2 (table, i, var_idx,
283 pivot_value_new_number (entries[i]));
286 /* Displays a full frequency table for variable V. */
288 dump_freq_table (const struct var_freqs *vf, const struct variable *wv)
290 const struct freq_tab *ft = &vf->tab;
292 struct pivot_table *table = pivot_table_create__ (pivot_value_new_variable (
293 vf->var), "Frequencies");
294 pivot_table_set_weight_var (table, wv);
296 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
297 N_("Frequency"), PIVOT_RC_COUNT,
298 N_("Percent"), PIVOT_RC_PERCENT,
299 N_("Valid Percent"), PIVOT_RC_PERCENT,
300 N_("Cumulative Percent"), PIVOT_RC_PERCENT);
302 struct pivot_dimension *variable = pivot_dimension_create__ (
303 table, PIVOT_AXIS_ROW, pivot_value_new_variable (vf->var));
305 double cum_freq = 0.0;
306 double cum_percent = 0.0;
307 struct pivot_category *valid = NULL;
308 for (const struct freq *f = ft->valid; f < ft->missing; f++)
310 cum_freq += f->count;
311 double valid_percent = f->count / ft->valid_cases * 100.0;
312 cum_percent += valid_percent;
315 valid = pivot_category_create_group (variable->root, N_("Valid"));
316 int var_idx = pivot_category_create_leaf (
317 valid, pivot_value_new_var_value (vf->var, &f->values[0]));
318 put_freq_row (table, var_idx, f->count,
319 f->count / ft->total_cases * 100.0,
320 valid_percent, cum_percent);
323 struct pivot_category *missing = NULL;
324 size_t n_categories = ft->n_valid + ft->n_missing;
325 for (const struct freq *f = ft->missing; f < &ft->valid[n_categories]; f++)
327 cum_freq += f->count;
330 missing = pivot_category_create_group (variable->root, N_("Missing"));
331 int var_idx = pivot_category_create_leaf (
332 missing, pivot_value_new_var_value (vf->var, &f->values[0]));
333 put_freq_row (table, var_idx, f->count,
334 f->count / ft->total_cases * 100.0, SYSMIS, SYSMIS);
337 int var_idx = pivot_category_create_leaf (
338 variable->root, pivot_value_new_text (N_("Total")));
339 put_freq_row (table, var_idx, cum_freq, cum_percent, SYSMIS, SYSMIS);
341 pivot_table_submit (table);
344 /* Statistical display. */
347 calc_percentile (double p, double valid_cases, double x1, double x2)
351 s = (settings_get_algorithm () != COMPATIBLE
352 ? modf ((valid_cases - 1) * p, &dummy)
353 : modf ((valid_cases + 1) * p - 1, &dummy));
355 return x1 + (x2 - x1) * s;
358 /* Calculates all of the percentiles for VF within FRQ. */
360 calc_percentiles (const struct frq_proc *frq, const struct var_freqs *vf)
362 const struct freq_tab *ft = &vf->tab;
363 double W = ft->valid_cases;
364 const struct freq *f;
365 int percentile_idx = 0;
368 for (f = ft->valid; f < ft->missing; f++)
371 for (; percentile_idx < frq->n_percentiles; percentile_idx++)
373 struct percentile *pc = &frq->percentiles[percentile_idx];
376 tp = (settings_get_algorithm () == ENHANCED
378 : (W + 1) * pc->p - 1);
383 if (tp + 1 < rank || f + 1 >= ft->missing)
384 pc->value = f->values[0].f;
386 pc->value = calc_percentile (pc->p, W, f->values[0].f, f[1].values[0].f);
389 for (; percentile_idx < frq->n_percentiles; percentile_idx++)
391 struct percentile *pc = &frq->percentiles[percentile_idx];
392 pc->value = (ft->n_valid > 0
393 ? ft->valid[ft->n_valid - 1].values[0].f
398 /* Returns true iff the value in struct freq F is non-missing
401 not_missing (const void *f_, const void *v_)
403 const struct freq *f = f_;
404 const struct variable *v = v_;
406 return !var_is_value_missing (v, f->values);
410 /* Summarizes the frequency table data for variable V. */
412 postprocess_freq_tab (const struct frq_proc *frq, struct var_freqs *vf)
414 struct freq_tab *ft = &vf->tab;
415 struct freq_compare_aux aux;
417 struct freq *freqs, *f;
420 /* Extract data from hash table. */
421 count = hmap_count (&ft->data);
422 freqs = freq_hmap_extract (&ft->data);
424 /* Put data into ft. */
426 ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, vf->var);
427 ft->missing = freqs + ft->n_valid;
428 ft->n_missing = count - ft->n_valid;
431 aux.by_freq = frq->sort == FRQ_AFREQ || frq->sort == FRQ_DFREQ;
432 aux.ascending_freq = frq->sort != FRQ_DFREQ;
433 aux.width = vf->width;
434 aux.ascending_value = frq->sort != FRQ_DVALUE;
435 sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare_freq, &aux);
436 sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare_freq, &aux);
438 /* Summary statistics. */
439 ft->valid_cases = 0.0;
440 for(i = 0 ; i < ft->n_valid ; ++i)
443 ft->valid_cases += f->count;
447 ft->total_cases = ft->valid_cases ;
448 for(i = 0 ; i < ft->n_missing ; ++i)
451 ft->total_cases += f->count;
456 /* Frees the frequency table for variable V. */
458 cleanup_freq_tab (struct var_freqs *vf)
460 free (vf->tab.valid);
461 freq_hmap_destroy (&vf->tab.data, vf->width);
464 /* Add data from case C to the frequency table. */
466 calc (struct frq_proc *frq, const struct ccase *c, const struct dataset *ds)
468 double weight = dict_get_case_weight (dataset_dict (ds), c, &frq->warn);
471 for (i = 0; i < frq->n_vars; i++)
473 struct var_freqs *vf = &frq->vars[i];
474 const union value *value = case_data (c, vf->var);
475 size_t hash = value_hash (value, vf->width, 0);
478 f = freq_hmap_search (&vf->tab.data, value, vf->width, hash);
480 f = freq_hmap_insert (&vf->tab.data, value, vf->width, hash);
486 /* Prepares each variable that is the target of FREQUENCIES by setting
487 up its hash table. */
489 precalc (struct frq_proc *frq, struct casereader *input, struct dataset *ds)
494 c = casereader_peek (input, 0);
497 output_split_file_values (ds, c);
501 for (i = 0; i < frq->n_vars; i++)
502 hmap_init (&frq->vars[i].tab.data);
505 /* Finishes up with the variables after frequencies have been
506 calculated. Displays statistics, percentiles, ... */
508 postcalc (struct frq_proc *frq, const struct dataset *ds)
510 const struct dictionary *dict = dataset_dict (ds);
511 const struct variable *wv = dict_get_weight (dict);
514 for (i = 0; i < frq->n_vars; i++)
516 struct var_freqs *vf = &frq->vars[i];
517 postprocess_freq_tab (frq, vf);
518 calc_percentiles (frq, vf);
522 dump_statistics (frq, wv);
524 for (i = 0; i < frq->n_vars; i++)
526 struct var_freqs *vf = &frq->vars[i];
528 /* Frequencies tables. */
529 if (vf->tab.n_valid + vf->tab.n_missing <= frq->max_categories)
530 dump_freq_table (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;
538 calc_stats (frq, vf, d);
540 histogram = freq_tab_to_hist (frq, &vf->tab, vf->var);
544 chart_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);
559 do_barchart(frq->bar, &vf->var, &vf->tab);
561 cleanup_freq_tab (vf);
566 cmd_frequencies (struct lexer *lexer, struct dataset *ds)
570 const struct variable **vars = NULL;
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 = true;
580 double bar_min = -DBL_MAX;
581 double bar_max = DBL_MAX;
582 bool bar_freq = true;
584 double hi_min = -DBL_MAX;
585 double hi_max = DBL_MAX;
586 int hi_scale = FRQ_FREQ;
587 int hi_freq = INT_MIN;
588 int hi_pcnt = INT_MIN;
589 int hi_norm = FRQ_NONORMAL;
591 frq.sort = FRQ_AVALUE;
596 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
597 | BIT_INDEX (FRQ_ST_STDDEV)
598 | BIT_INDEX (FRQ_ST_MINIMUM)
599 | BIT_INDEX (FRQ_ST_MAXIMUM);
603 frq.max_categories = LONG_MAX;
605 frq.percentiles = NULL;
606 frq.n_percentiles = 0;
614 /* Accept an optional, completely pointless "/VARIABLES=" */
615 lex_match (lexer, T_SLASH);
616 if (lex_match_id (lexer, "VARIABLES"))
618 if (! lex_force_match (lexer, T_EQUALS))
622 if (!parse_variables_const (lexer, dataset_dict (ds),
628 frq.vars = xcalloc (frq.n_vars, sizeof (*frq.vars));
629 for (i = 0; i < frq.n_vars; ++i)
631 frq.vars[i].var = vars[i];
632 frq.vars[i].width = var_get_width (vars[i]);
635 while (lex_token (lexer) != T_ENDCMD)
637 lex_match (lexer, T_SLASH);
639 if (lex_match_id (lexer, "STATISTICS"))
641 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
642 | BIT_INDEX (FRQ_ST_STDDEV)
643 | BIT_INDEX (FRQ_ST_MINIMUM)
644 | BIT_INDEX (FRQ_ST_MAXIMUM);
648 if (lex_match (lexer, T_EQUALS))
654 while (lex_token (lexer) != T_ENDCMD
655 && lex_token (lexer) != T_SLASH)
657 if (lex_match_id (lexer, "DEFAULT"))
659 frq.stats = BIT_INDEX (FRQ_ST_MEAN)
660 | BIT_INDEX (FRQ_ST_STDDEV)
661 | BIT_INDEX (FRQ_ST_MINIMUM)
662 | BIT_INDEX (FRQ_ST_MAXIMUM);
666 else if (lex_match_id (lexer, "MEAN"))
668 frq.stats |= BIT_INDEX (FRQ_ST_MEAN);
671 else if (lex_match_id (lexer, "SEMEAN"))
673 frq.stats |= BIT_INDEX (FRQ_ST_SEMEAN);
676 else if (lex_match_id (lexer, "MEDIAN"))
678 frq.stats |= BIT_INDEX (FRQ_ST_MEDIAN);
681 else if (lex_match_id (lexer, "MODE"))
683 frq.stats |= BIT_INDEX (FRQ_ST_MODE);
686 else if (lex_match_id (lexer, "STDDEV"))
688 frq.stats |= BIT_INDEX (FRQ_ST_STDDEV);
691 else if (lex_match_id (lexer, "VARIANCE"))
693 frq.stats |= BIT_INDEX (FRQ_ST_VARIANCE);
696 else if (lex_match_id (lexer, "KURTOSIS"))
698 frq.stats |= BIT_INDEX (FRQ_ST_KURTOSIS);
701 else if (lex_match_id (lexer, "SKEWNESS"))
703 frq.stats |= BIT_INDEX (FRQ_ST_SKEWNESS);
706 else if (lex_match_id (lexer, "RANGE"))
708 frq.stats |= BIT_INDEX (FRQ_ST_RANGE);
711 else if (lex_match_id (lexer, "MINIMUM"))
713 frq.stats |= BIT_INDEX (FRQ_ST_MINIMUM);
716 else if (lex_match_id (lexer, "MAXIMUM"))
718 frq.stats |= BIT_INDEX (FRQ_ST_MAXIMUM);
721 else if (lex_match_id (lexer, "SUM"))
723 frq.stats |= BIT_INDEX (FRQ_ST_SUM);
726 else if (lex_match_id (lexer, "SESKEWNESS"))
728 frq.stats |= BIT_INDEX (FRQ_ST_SESKEWNESS);
731 else if (lex_match_id (lexer, "SEKURTOSIS"))
733 frq.stats |= BIT_INDEX (FRQ_ST_SEKURTOSIS);
736 else if (lex_match_id (lexer, "NONE"))
741 else if (lex_match (lexer, T_ALL))
744 frq.n_stats = FRQ_ST_count;
748 lex_error (lexer, NULL);
753 else if (lex_match_id (lexer, "PERCENTILES"))
755 lex_match (lexer, T_EQUALS);
756 while (lex_token (lexer) != T_ENDCMD
757 && lex_token (lexer) != T_SLASH)
759 if (lex_force_num (lexer))
762 xrealloc (frq.percentiles,
763 (frq.n_percentiles + 1)
764 * sizeof (*frq.percentiles));
765 frq.percentiles[frq.n_percentiles].p = lex_number (lexer) / 100.0;
766 frq.percentiles[frq.n_percentiles].show = true;
772 lex_error (lexer, NULL);
775 lex_match (lexer, T_COMMA);
778 else if (lex_match_id (lexer, "FORMAT"))
780 lex_match (lexer, T_EQUALS);
781 while (lex_token (lexer) != T_ENDCMD
782 && lex_token (lexer) != T_SLASH)
784 if (lex_match_id (lexer, "TABLE"))
787 else if (lex_match_id (lexer, "NOTABLE"))
789 frq.max_categories = 0;
791 else if (lex_match_id (lexer, "LIMIT"))
793 if (!lex_force_match (lexer, T_LPAREN)
794 || !lex_force_int_range (lexer, "LIMIT", 0, INT_MAX))
797 frq.max_categories = lex_integer (lexer);
800 if (!lex_force_match (lexer, T_RPAREN))
803 else if (lex_match_id (lexer, "AVALUE"))
805 frq.sort = FRQ_AVALUE;
807 else if (lex_match_id (lexer, "DVALUE"))
809 frq.sort = FRQ_DVALUE;
811 else if (lex_match_id (lexer, "AFREQ"))
813 frq.sort = FRQ_AFREQ;
815 else if (lex_match_id (lexer, "DFREQ"))
817 frq.sort = FRQ_DFREQ;
821 lex_error (lexer, NULL);
826 else if (lex_match_id (lexer, "NTILES"))
828 lex_match (lexer, T_EQUALS);
830 if (lex_force_int_range (lexer, "NTILES", 0, INT_MAX))
832 int n = lex_integer (lexer);
834 for (int i = 0; i < n + 1; ++i)
837 xrealloc (frq.percentiles,
838 (frq.n_percentiles + 1)
839 * sizeof (*frq.percentiles));
840 frq.percentiles[frq.n_percentiles].p =
842 frq.percentiles[frq.n_percentiles].show = true;
849 lex_error (lexer, NULL);
853 else if (lex_match_id (lexer, "ALGORITHM"))
855 lex_match (lexer, T_EQUALS);
857 if (lex_match_id (lexer, "COMPATIBLE"))
859 settings_set_cmd_algorithm (COMPATIBLE);
861 else if (lex_match_id (lexer, "ENHANCED"))
863 settings_set_cmd_algorithm (ENHANCED);
867 lex_error (lexer, NULL);
871 else if (lex_match_id (lexer, "HISTOGRAM"))
873 lex_match (lexer, T_EQUALS);
874 sbc_histogram = true;
876 while (lex_token (lexer) != T_ENDCMD
877 && lex_token (lexer) != T_SLASH)
879 if (lex_match_id (lexer, "NORMAL"))
881 hi_norm = FRQ_NORMAL;
883 else if (lex_match_id (lexer, "NONORMAL"))
885 hi_norm = FRQ_NONORMAL;
887 else if (lex_match_id (lexer, "FREQ"))
890 if (lex_match (lexer, T_LPAREN))
892 if (lex_force_int_range (lexer, "FREQ", 1, INT_MAX))
894 hi_freq = lex_integer (lexer);
896 if (! lex_force_match (lexer, T_RPAREN))
901 else if (lex_match_id (lexer, "PERCENT"))
903 hi_scale = FRQ_PERCENT;
904 if (lex_match (lexer, T_LPAREN))
906 if (lex_force_int_range (lexer, "PERCENT", 1, INT_MAX))
908 hi_pcnt = lex_integer (lexer);
910 if (! lex_force_match (lexer, T_RPAREN))
915 else if (lex_match_id (lexer, "MINIMUM"))
917 if (! lex_force_match (lexer, T_LPAREN))
919 if (lex_force_num (lexer))
921 hi_min = lex_number (lexer);
924 if (! lex_force_match (lexer, T_RPAREN))
927 else if (lex_match_id (lexer, "MAXIMUM"))
929 if (! lex_force_match (lexer, T_LPAREN))
931 if (lex_force_num (lexer))
933 hi_max = lex_number (lexer);
936 if (! lex_force_match (lexer, T_RPAREN))
941 lex_error (lexer, NULL);
946 else if (lex_match_id (lexer, "PIECHART"))
948 lex_match (lexer, T_EQUALS);
949 while (lex_token (lexer) != T_ENDCMD
950 && lex_token (lexer) != T_SLASH)
952 if (lex_match_id (lexer, "MINIMUM"))
954 if (! lex_force_match (lexer, T_LPAREN))
956 if (lex_force_num (lexer))
958 pie_min = lex_number (lexer);
961 if (! lex_force_match (lexer, T_RPAREN))
964 else if (lex_match_id (lexer, "MAXIMUM"))
966 if (! lex_force_match (lexer, T_LPAREN))
968 if (lex_force_num (lexer))
970 pie_max = lex_number (lexer);
973 if (! lex_force_match (lexer, T_RPAREN))
976 else if (lex_match_id (lexer, "MISSING"))
980 else if (lex_match_id (lexer, "NOMISSING"))
986 lex_error (lexer, NULL);
992 else if (lex_match_id (lexer, "BARCHART"))
994 lex_match (lexer, T_EQUALS);
995 while (lex_token (lexer) != T_ENDCMD
996 && lex_token (lexer) != T_SLASH)
998 if (lex_match_id (lexer, "MINIMUM"))
1000 if (! lex_force_match (lexer, T_LPAREN))
1002 if (lex_force_num (lexer))
1004 bar_min = lex_number (lexer);
1007 if (! lex_force_match (lexer, T_RPAREN))
1010 else if (lex_match_id (lexer, "MAXIMUM"))
1012 if (! lex_force_match (lexer, T_LPAREN))
1014 if (lex_force_num (lexer))
1016 bar_max = lex_number (lexer);
1019 if (! lex_force_match (lexer, T_RPAREN))
1022 else if (lex_match_id (lexer, "FREQ"))
1024 if (lex_match (lexer, T_LPAREN))
1026 if (lex_force_num (lexer))
1031 if (! lex_force_match (lexer, T_RPAREN))
1036 else if (lex_match_id (lexer, "PERCENT"))
1038 if (lex_match (lexer, T_LPAREN))
1040 if (lex_force_num (lexer))
1045 if (! lex_force_match (lexer, T_RPAREN))
1052 lex_error (lexer, NULL);
1056 sbc_barchart = true;
1058 else if (lex_match_id (lexer, "MISSING"))
1060 lex_match (lexer, T_EQUALS);
1062 while (lex_token (lexer) != T_ENDCMD
1063 && lex_token (lexer) != T_SLASH)
1065 if (lex_match_id (lexer, "EXCLUDE"))
1068 else if (lex_match_id (lexer, "INCLUDE"))
1073 lex_error (lexer, NULL);
1078 else if (lex_match_id (lexer, "ORDER"))
1080 lex_match (lexer, T_EQUALS);
1081 if (!lex_match_id (lexer, "ANALYSIS"))
1082 lex_match_id (lexer, "VARIABLE");
1086 lex_error (lexer, NULL);
1091 if (frq.stats & BIT_INDEX (FRQ_ST_MEDIAN))
1094 xrealloc (frq.percentiles,
1095 (frq.n_percentiles + 1)
1096 * sizeof (*frq.percentiles));
1098 frq.percentiles[frq.n_percentiles].p = 0.50;
1099 frq.percentiles[frq.n_percentiles].show = false;
1101 frq.n_percentiles++;
1105 /* Figure out which charts the user requested. */
1110 struct frq_chart *hist;
1112 hist = frq.hist = xmalloc (sizeof *frq.hist);
1113 hist->x_min = hi_min;
1114 hist->x_max = hi_max;
1115 hist->y_scale = hi_scale;
1116 hist->y_max = hi_scale == FRQ_FREQ ? hi_freq : hi_pcnt;
1117 hist->draw_normal = hi_norm != FRQ_NONORMAL;
1118 hist->include_missing = false;
1120 if (hist->x_min != SYSMIS && hist->x_max != SYSMIS
1121 && hist->x_min >= hist->x_max)
1123 msg (SE, _("%s for histogram must be greater than or equal to %s, "
1124 "but %s was specified as %.15g and %s as %.15g. "
1125 "%s and %s will be ignored."),
1130 hist->x_min = hist->x_max = SYSMIS;
1134 xrealloc (frq.percentiles,
1135 (frq.n_percentiles + 2)
1136 * sizeof (*frq.percentiles));
1138 frq.percentiles[frq.n_percentiles].p = 0.25;
1139 frq.percentiles[frq.n_percentiles].show = false;
1141 frq.percentiles[frq.n_percentiles + 1].p = 0.75;
1142 frq.percentiles[frq.n_percentiles + 1].show = false;
1144 frq.n_percentiles+=2;
1149 frq.bar = xmalloc (sizeof *frq.bar);
1150 frq.bar->x_min = bar_min;
1151 frq.bar->x_max = bar_max;
1152 frq.bar->include_missing = false;
1153 frq.bar->y_scale = bar_freq ? FRQ_FREQ : FRQ_PERCENT;
1158 struct frq_chart *pie;
1160 pie = frq.pie = xmalloc (sizeof *frq.pie);
1161 pie->x_min = pie_min;
1162 pie->x_max = pie_max;
1163 pie->include_missing = pie_missing;
1165 if (pie->x_min != SYSMIS && pie->x_max != SYSMIS
1166 && pie->x_min >= pie->x_max)
1168 msg (SE, _("%s for pie chart must be greater than or equal to %s, "
1169 "but %s was specified as %.15g and %s as %.15g. "
1170 "%s and %s will be ignored."),
1175 pie->x_min = pie->x_max = SYSMIS;
1182 double previous_p = -1;
1183 qsort (frq.percentiles, frq.n_percentiles,
1184 sizeof (*frq.percentiles),
1187 for (i = o = 0; i < frq.n_percentiles; ++i)
1189 if (frq.percentiles[i].p != previous_p)
1191 frq.percentiles[o].p = frq.percentiles[i].p;
1192 frq.percentiles[o].show = frq.percentiles[i].show;
1195 else if (frq.percentiles[i].show &&
1196 !frq.percentiles[o].show)
1198 frq.percentiles[o].show = true;
1200 previous_p = frq.percentiles[i].p;
1203 frq.n_percentiles = o;
1206 for (i = 0; i < frq.n_percentiles; i++)
1207 if (frq.percentiles[i].p == 0.5)
1209 frq.median = &frq.percentiles[i];
1215 struct casegrouper *grouper;
1216 struct casereader *group;
1219 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
1220 while (casegrouper_get_next_group (grouper, &group))
1223 precalc (&frq, group, ds);
1225 for (; (c = casereader_read (group)) != NULL; case_unref (c))
1227 postcalc (&frq, ds);
1228 casereader_destroy (group);
1230 ok = casegrouper_destroy (grouper);
1231 ok = proc_commit (ds) && ok;
1240 free (frq.percentiles);
1251 free (frq.percentiles);
1257 calculate_iqr (const struct frq_proc *frq)
1263 /* This cannot work unless the 25th and 75th percentile are calculated */
1264 assert (frq->n_percentiles >= 2);
1265 for (i = 0; i < frq->n_percentiles; i++)
1267 struct percentile *pc = &frq->percentiles[i];
1269 if (fabs (0.25 - pc->p) < DBL_EPSILON)
1271 else if (fabs (0.75 - pc->p) < DBL_EPSILON)
1275 return q1 == SYSMIS || q3 == SYSMIS ? SYSMIS : q3 - q1;
1279 chart_includes_value (const struct frq_chart *chart,
1280 const struct variable *var,
1281 const union value *value)
1283 if (!chart->include_missing && var_is_value_missing (var, value))
1286 if (var_is_numeric (var)
1287 && ((chart->x_min != SYSMIS && value->f < chart->x_min)
1288 || (chart->x_max != SYSMIS && value->f > chart->x_max)))
1294 /* Create a gsl_histogram from a freq_tab */
1295 static struct histogram *
1296 freq_tab_to_hist (const struct frq_proc *frq, const struct freq_tab *ft,
1297 const struct variable *var)
1299 double x_min, x_max, valid_freq;
1302 struct histogram *histogram;
1305 /* Find out the extremes of the x value, within the range to be included in
1306 the histogram, and sum the total frequency of those values. */
1310 for (i = 0; i < ft->n_valid; i++)
1312 const struct freq *f = &ft->valid[i];
1313 if (chart_includes_value (frq->hist, var, f->values))
1315 x_min = MIN (x_min, f->values[0].f);
1316 x_max = MAX (x_max, f->values[0].f);
1317 valid_freq += f->count;
1321 if (valid_freq <= 0)
1324 iqr = calculate_iqr (frq);
1327 /* Freedman-Diaconis' choice of bin width. */
1328 bin_width = 2 * iqr / pow (valid_freq, 1.0 / 3.0);
1332 bin_width = (x_max - x_min) / (1 + log2 (valid_freq));
1334 histogram = histogram_create (bin_width, x_min, x_max);
1336 if (histogram == NULL)
1339 for (i = 0; i < ft->n_valid; i++)
1341 const struct freq *f = &ft->valid[i];
1342 if (chart_includes_value (frq->hist, var, f->values))
1343 histogram_add (histogram, f->values[0].f, f->count);
1350 /* Allocate an array of struct freqs and fill them from the data in FRQ_TAB,
1351 according to the parameters of CATCHART
1352 N_SLICES will contain the number of slices allocated.
1353 The caller is responsible for freeing slices
1355 static struct freq *
1356 pick_cat_counts (const struct frq_chart *catchart,
1357 const struct freq_tab *frq_tab,
1362 struct freq *slices = xnmalloc (frq_tab->n_valid + frq_tab->n_missing, sizeof *slices);
1364 for (i = 0; i < frq_tab->n_valid; i++)
1366 const struct freq *f = &frq_tab->valid[i];
1367 if (f->count > catchart->x_max)
1370 if (f->count < catchart->x_min)
1373 slices[n_slices] = *f;
1378 if (catchart->include_missing)
1380 for (i = 0; i < frq_tab->n_missing; i++)
1382 const struct freq *f = &frq_tab->missing[i];
1383 slices[n_slices].count += f->count;
1386 slices[n_slices].values[0] = f->values[0];
1389 if (frq_tab->n_missing > 0)
1393 *n_slicesp = n_slices;
1398 /* Allocate an array of struct freqs and fill them from the data in FRQ_TAB,
1399 according to the parameters of CATCHART
1400 N_SLICES will contain the number of slices allocated.
1401 The caller is responsible for freeing slices
1403 static struct freq **
1404 pick_cat_counts_ptr (const struct frq_chart *catchart,
1405 const struct freq_tab *frq_tab,
1410 struct freq **slices = xnmalloc (frq_tab->n_valid + frq_tab->n_missing, sizeof *slices);
1412 for (i = 0; i < frq_tab->n_valid; i++)
1414 struct freq *f = &frq_tab->valid[i];
1415 if (f->count > catchart->x_max)
1418 if (f->count < catchart->x_min)
1421 slices[n_slices] = f;
1426 if (catchart->include_missing)
1428 for (i = 0; i < frq_tab->n_missing; i++)
1430 const struct freq *f = &frq_tab->missing[i];
1433 slices[n_slices] = xmalloc (sizeof (struct freq));
1434 slices[n_slices]->values[0] = f->values[0];
1437 slices[n_slices]->count += f->count;
1442 *n_slicesp = n_slices;
1449 do_piechart(const struct frq_chart *pie, const struct variable *var,
1450 const struct freq_tab *frq_tab)
1453 struct freq *slices = pick_cat_counts (pie, frq_tab, &n_slices);
1456 msg (SW, _("Omitting pie chart for %s, which has only %d unique values."),
1457 var_get_name (var), n_slices);
1458 else if (n_slices > 50)
1459 msg (SW, _("Omitting pie chart for %s, which has over 50 unique values."),
1460 var_get_name (var));
1462 chart_submit (piechart_create (var, slices, n_slices));
1469 do_barchart(const struct frq_chart *bar, const struct variable **var,
1470 const struct freq_tab *frq_tab)
1473 struct freq **slices = pick_cat_counts_ptr (bar, frq_tab, &n_slices);
1476 msg (SW, _("Omitting bar chart, which has no values."));
1478 chart_submit (barchart_create (var, 1,
1479 (bar->y_scale == FRQ_FREQ) ? _("Count") : _("Percent"),
1480 (bar->y_scale == FRQ_PERCENT),
1486 /* Calculates all the pertinent statistics for VF, putting them in array
1489 calc_stats (const struct frq_proc *frq, const struct var_freqs *vf,
1490 double d[FRQ_ST_count])
1492 const struct freq_tab *ft = &vf->tab;
1493 double W = ft->valid_cases;
1494 const struct freq *f;
1496 int most_often = -1;
1497 double X_mode = SYSMIS;
1499 /* Calculate the mode. */
1500 for (f = ft->valid; f < ft->missing; f++)
1502 if (most_often < f->count)
1504 most_often = f->count;
1505 X_mode = f->values[0].f;
1507 else if (most_often == f->count)
1509 /* A duplicate mode is undefined.
1510 FIXME: keep track of *all* the modes. */
1515 /* Calculate moments. */
1516 m = moments_create (MOMENT_KURTOSIS);
1517 for (f = ft->valid; f < ft->missing; f++)
1518 moments_pass_one (m, f->values[0].f, f->count);
1519 for (f = ft->valid; f < ft->missing; f++)
1520 moments_pass_two (m, f->values[0].f, f->count);
1521 moments_calculate (m, NULL, &d[FRQ_ST_MEAN], &d[FRQ_ST_VARIANCE],
1522 &d[FRQ_ST_SKEWNESS], &d[FRQ_ST_KURTOSIS]);
1523 moments_destroy (m);
1525 /* Formulae below are taken from _SPSS Statistical Algorithms_. */
1526 if (ft->n_valid > 0)
1528 d[FRQ_ST_MINIMUM] = ft->valid[0].values[0].f;
1529 d[FRQ_ST_MAXIMUM] = ft->valid[ft->n_valid - 1].values[0].f;
1530 d[FRQ_ST_RANGE] = d[FRQ_ST_MAXIMUM] - d[FRQ_ST_MINIMUM];
1534 d[FRQ_ST_MINIMUM] = SYSMIS;
1535 d[FRQ_ST_MAXIMUM] = SYSMIS;
1536 d[FRQ_ST_RANGE] = SYSMIS;
1538 d[FRQ_ST_MODE] = X_mode;
1539 d[FRQ_ST_SUM] = d[FRQ_ST_MEAN] * W;
1540 d[FRQ_ST_STDDEV] = sqrt (d[FRQ_ST_VARIANCE]);
1541 d[FRQ_ST_SEMEAN] = d[FRQ_ST_STDDEV] / sqrt (W);
1542 d[FRQ_ST_SESKEWNESS] = calc_seskew (W);
1543 d[FRQ_ST_SEKURTOSIS] = calc_sekurt (W);
1544 d[FRQ_ST_MEDIAN] = frq->median ? frq->median->value : SYSMIS;
1548 all_string_variables (const struct frq_proc *frq)
1550 for (size_t i = 0; i < frq->n_vars; i++)
1551 if (var_is_numeric (frq->vars[i].var))
1557 /* Displays a table of all the statistics requested. */
1559 dump_statistics (const struct frq_proc *frq, const struct variable *wv)
1561 if (all_string_variables (frq))
1564 struct pivot_table *table = pivot_table_create (N_("Statistics"));
1565 pivot_table_set_weight_var (table, wv);
1567 struct pivot_dimension *variables
1568 = pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Variables"));
1570 struct pivot_dimension *statistics = pivot_dimension_create (
1571 table, PIVOT_AXIS_ROW, N_("Statistics"));
1572 struct pivot_category *n = pivot_category_create_group (
1573 statistics->root, N_("N"));
1574 pivot_category_create_leaves (n,
1575 N_("Valid"), PIVOT_RC_COUNT,
1576 N_("Missing"), PIVOT_RC_COUNT);
1577 for (int i = 0; i < FRQ_ST_count; i++)
1578 if (frq->stats & BIT_INDEX (i))
1579 pivot_category_create_leaf (statistics->root,
1580 pivot_value_new_text (st_name[i]));
1581 struct pivot_category *percentiles = NULL;
1582 for (size_t i = 0; i < frq->n_percentiles; i++)
1584 const struct percentile *pc = &frq->percentiles[i];
1590 percentiles = pivot_category_create_group (
1591 statistics->root, N_("Percentiles"));
1592 pivot_category_create_leaf (percentiles, pivot_value_new_integer (
1596 for (size_t i = 0; i < frq->n_vars; i++)
1598 struct var_freqs *vf = &frq->vars[i];
1599 if (var_is_alpha (vf->var))
1602 const struct freq_tab *ft = &vf->tab;
1604 int var_idx = pivot_category_create_leaf (
1605 variables->root, pivot_value_new_variable (vf->var));
1608 pivot_table_put2 (table, var_idx, row++,
1609 pivot_value_new_number (ft->valid_cases));
1610 pivot_table_put2 (table, var_idx, row++,
1611 pivot_value_new_number (
1612 ft->total_cases - ft->valid_cases));
1614 double stat_values[FRQ_ST_count];
1615 calc_stats (frq, vf, stat_values);
1616 for (int j = 0; j < FRQ_ST_count; j++)
1618 if (!(frq->stats & BIT_INDEX (j)))
1621 union value v = { .f = vf->tab.n_valid ? stat_values[j] : SYSMIS };
1622 struct pivot_value *pv
1623 = (j == FRQ_ST_MODE || j == FRQ_ST_MINIMUM || j == FRQ_ST_MAXIMUM
1624 ? pivot_value_new_var_value (vf->var, &v)
1625 : pivot_value_new_number (v.f));
1626 pivot_table_put2 (table, var_idx, row++, pv);
1629 for (size_t j = 0; j < frq->n_percentiles; j++)
1631 const struct percentile *pc = &frq->percentiles[j];
1635 union value v = { .f = vf->tab.n_valid ? pc->value : SYSMIS };
1636 pivot_table_put2 (table, var_idx, row++,
1637 pivot_value_new_var_value (vf->var, &v));
1641 pivot_table_submit (table);