1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <gsl/gsl_cdf.h>
20 #include <libpspp/message.h>
25 #include <data/case.h>
26 #include <data/casegrouper.h>
27 #include <data/casereader.h>
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/value-labels.h>
31 #include <data/variable.h>
32 #include <language/command.h>
33 #include <language/dictionary/split-file.h>
34 #include <language/lexer/lexer.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/compiler.h>
37 #include <libpspp/hash.h>
38 #include <libpspp/magic.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/str.h>
42 #include <math/factor-stats.h>
43 #include <math/moments.h>
44 #include <math/percentiles.h>
45 #include <output/charts/box-whisker.h>
46 #include <output/charts/cartesian.h>
47 #include <output/manager.h>
48 #include <output/table.h>
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) msgid
57 #include <output/chart.h>
58 #include <output/charts/plot-hist.h>
59 #include <output/charts/plot-chart.h>
66 missing=miss:pairwise/!listwise,
68 incl:include/!exclude;
69 +compare=cmp:variables/!groups;
72 +plot[plt_]=stemleaf,boxplot,npplot,:spreadlevel(*d:n),histogram,all,none;
74 +statistics[st_]=descriptives,:extreme(*d:n),all,none.
83 static struct cmd_examine cmd;
85 static const struct variable **dependent_vars;
87 static size_t n_dependent_vars;
92 /* The independent variable */
93 struct variable *indep_var[2];
96 /* Hash table of factor stats indexed by 2 values */
97 struct hsh_table *fstats;
99 /* The hash table after it has been crunched */
100 struct factor_statistics **fs;
106 /* Linked list of factors */
107 static struct factor *factors = 0;
109 static struct metrics *totals = 0;
111 /* Parse the clause specifying the factors */
112 static int examine_parse_independent_vars (struct lexer *lexer, const struct dictionary *dict, struct cmd_examine *cmd);
116 /* Output functions */
117 static void show_summary (const struct variable **dependent_var, int n_dep_var,
118 const struct factor *f);
120 static void show_extremes (const struct variable **dependent_var,
122 const struct factor *factor,
125 static void show_descriptives (const struct variable **dependent_var,
127 struct factor *factor);
129 static void show_percentiles (const struct variable **dependent_var,
131 struct factor *factor);
136 void np_plot (const struct metrics *m, const char *factorname);
139 void box_plot_group (const struct factor *fctr,
140 const struct variable **vars, int n_vars,
141 const struct variable *id
145 void box_plot_variables (const struct factor *fctr,
146 const struct variable **vars, int n_vars,
147 const struct variable *id
152 /* Per Split function */
153 static void run_examine (struct cmd_examine *, struct casereader *,
156 static void output_examine (void);
159 void factor_calc (const struct ccase *c, int case_no,
160 double weight, int case_missing);
163 /* Represent a factor as a string, so it can be
164 printed in a human readable fashion */
165 const char * factor_to_string (const struct factor *fctr,
166 const struct factor_statistics *fs,
167 const struct variable *var);
170 /* Represent a factor as a string, so it can be
171 printed in a human readable fashion,
172 but sacrificing some readablility for the sake of brevity */
173 const char *factor_to_string_concise (const struct factor *fctr,
174 struct factor_statistics *fs);
179 /* Categories of missing values to exclude. */
180 static enum mv_class exclude_values;
184 static subc_list_double percentile_list;
186 static enum pc_alg percentile_algorithm;
188 static short sbc_percentile;
192 cmd_examine (struct lexer *lexer, struct dataset *ds)
194 struct casegrouper *grouper;
195 struct casereader *group;
198 subc_list_double_create (&percentile_list);
199 percentile_algorithm = PC_HAVERAGE;
201 if ( !parse_examine (lexer, ds, &cmd, NULL) )
203 subc_list_double_destroy (&percentile_list);
207 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
208 exclude_values = cmd.incl == XMN_INCLUDE ? MV_SYSTEM : MV_ANY;
210 if ( cmd.st_n == SYSMIS )
213 if ( ! cmd.sbc_cinterval)
214 cmd.n_cinterval[0] = 95.0;
216 /* If descriptives have been requested, make sure the
217 quartiles are calculated */
218 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES] )
220 subc_list_double_push (&percentile_list, 25);
221 subc_list_double_push (&percentile_list, 50);
222 subc_list_double_push (&percentile_list, 75);
225 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
226 while (casegrouper_get_next_group (grouper, &group))
227 run_examine (&cmd, group, ds);
228 ok = casegrouper_destroy (grouper);
229 ok = proc_commit (ds) && ok;
236 if ( dependent_vars )
237 free (dependent_vars);
240 struct factor *f = factors ;
243 struct factor *ff = f;
247 hsh_destroy ( ff->fstats ) ;
253 subc_list_double_destroy (&percentile_list);
255 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
260 /* Show all the appropriate tables */
262 output_examine (void)
266 /* Show totals if appropriate */
267 if ( ! cmd.sbc_nototal || factors == 0 )
269 show_summary (dependent_vars, n_dependent_vars, 0);
271 if ( cmd.sbc_statistics )
273 if ( cmd.a_statistics[XMN_ST_EXTREME])
274 show_extremes (dependent_vars, n_dependent_vars, 0, cmd.st_n);
276 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
277 show_descriptives (dependent_vars, n_dependent_vars, 0);
280 if ( sbc_percentile )
281 show_percentiles (dependent_vars, n_dependent_vars, 0);
286 if ( cmd.a_plot[XMN_PLT_STEMLEAF] )
287 msg (SW, _ ("%s is not currently supported."), "STEMLEAF");
289 if ( cmd.a_plot[XMN_PLT_SPREADLEVEL] )
290 msg (SW, _ ("%s is not currently supported."), "SPREADLEVEL");
292 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
294 for ( v = 0 ; v < n_dependent_vars; ++v )
295 np_plot (&totals[v], var_to_string (dependent_vars[v]));
298 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
300 if ( cmd.cmp == XMN_GROUPS )
302 box_plot_group (0, (const struct variable **) dependent_vars,
303 n_dependent_vars, cmd.v_id);
306 box_plot_variables (0,
307 (const struct variable **) dependent_vars,
308 n_dependent_vars, cmd.v_id);
311 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
313 for ( v = 0 ; v < n_dependent_vars; ++v )
315 struct normal_curve normal;
317 normal.N = totals[v].n;
318 normal.mean = totals[v].mean;
319 normal.stddev = totals[v].stddev;
321 histogram_plot (totals[v].histogram,
322 var_to_string (dependent_vars[v]),
332 /* Show grouped statistics as appropriate */
336 show_summary (dependent_vars, n_dependent_vars, fctr);
338 if ( cmd.sbc_statistics )
340 if ( cmd.a_statistics[XMN_ST_EXTREME])
341 show_extremes (dependent_vars, n_dependent_vars, fctr, cmd.st_n);
343 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
344 show_descriptives (dependent_vars, n_dependent_vars, fctr);
347 if ( sbc_percentile )
348 show_percentiles (dependent_vars, n_dependent_vars, fctr);
355 struct factor_statistics **fs = fctr->fs ;
357 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
359 if ( cmd.cmp == XMN_VARIABLES )
360 box_plot_variables (fctr,
361 (const struct variable **) dependent_vars,
362 n_dependent_vars, cmd.v_id);
364 box_plot_group (fctr,
365 (const struct variable **) dependent_vars,
366 n_dependent_vars, cmd.v_id);
369 for ( v = 0 ; v < n_dependent_vars; ++v )
372 for ( fs = fctr->fs ; *fs ; ++fs )
374 const char *s = factor_to_string (fctr, *fs, dependent_vars[v]);
376 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
377 np_plot (& (*fs)->m[v], s);
379 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
381 struct normal_curve normal;
383 normal.N = (*fs)->m[v].n;
384 normal.mean = (*fs)->m[v].mean;
385 normal.stddev = (*fs)->m[v].stddev;
387 histogram_plot ((*fs)->m[v].histogram,
391 } /* for ( fs .... */
393 } /* for ( v = 0 ..... */
403 /* Create a hash table of percentiles and their values from the list of
405 static struct hsh_table *
406 list_to_ptile_hash (const subc_list_double *l)
410 struct hsh_table *h ;
412 h = hsh_create (subc_list_double_count (l),
413 (hsh_compare_func *) ptile_compare,
414 (hsh_hash_func *) ptile_hash,
415 (hsh_free_func *) free,
419 for ( i = 0 ; i < subc_list_double_count (l) ; ++i )
421 struct percentile *p = xmalloc (sizeof *p);
423 p->p = subc_list_double_at (l,i);
434 /* Parse the PERCENTILES subcommand */
436 xmn_custom_percentiles (struct lexer *lexer, struct dataset *ds UNUSED,
437 struct cmd_examine *p UNUSED, void *aux UNUSED)
441 lex_match (lexer, '=');
443 lex_match (lexer, '(');
445 while ( lex_is_number (lexer) )
447 subc_list_double_push (&percentile_list, lex_number (lexer));
451 lex_match (lexer, ',') ;
453 lex_match (lexer, ')');
455 lex_match (lexer, '=');
457 if ( lex_match_id (lexer, "HAVERAGE"))
458 percentile_algorithm = PC_HAVERAGE;
460 else if ( lex_match_id (lexer, "WAVERAGE"))
461 percentile_algorithm = PC_WAVERAGE;
463 else if ( lex_match_id (lexer, "ROUND"))
464 percentile_algorithm = PC_ROUND;
466 else if ( lex_match_id (lexer, "EMPIRICAL"))
467 percentile_algorithm = PC_EMPIRICAL;
469 else if ( lex_match_id (lexer, "AEMPIRICAL"))
470 percentile_algorithm = PC_AEMPIRICAL;
472 else if ( lex_match_id (lexer, "NONE"))
473 percentile_algorithm = PC_NONE;
476 if ( 0 == subc_list_double_count (&percentile_list))
478 subc_list_double_push (&percentile_list, 5);
479 subc_list_double_push (&percentile_list, 10);
480 subc_list_double_push (&percentile_list, 25);
481 subc_list_double_push (&percentile_list, 50);
482 subc_list_double_push (&percentile_list, 75);
483 subc_list_double_push (&percentile_list, 90);
484 subc_list_double_push (&percentile_list, 95);
490 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
492 xmn_custom_total (struct lexer *lexer UNUSED, struct dataset *ds UNUSED, struct cmd_examine *p, void *aux UNUSED)
494 if ( p->sbc_nototal )
496 msg (SE, _ ("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
504 xmn_custom_nototal (struct lexer *lexer UNUSED, struct dataset *ds UNUSED,
505 struct cmd_examine *p, void *aux UNUSED)
509 msg (SE, _ ("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
518 /* Parser for the variables sub command
519 Returns 1 on success */
521 xmn_custom_variables (struct lexer *lexer, struct dataset *ds, struct cmd_examine *cmd, void *aux UNUSED)
523 const struct dictionary *dict = dataset_dict (ds);
524 lex_match (lexer, '=');
526 if ( (lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
527 && lex_token (lexer) != T_ALL)
532 if (!parse_variables_const (lexer, dict, &dependent_vars, &n_dependent_vars,
533 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
535 free (dependent_vars);
539 assert (n_dependent_vars);
541 totals = xnmalloc (n_dependent_vars, sizeof *totals);
543 if ( lex_match (lexer, T_BY))
546 success = examine_parse_independent_vars (lexer, dict, cmd);
547 if ( success != 1 ) {
548 free (dependent_vars);
559 /* Parse the clause specifying the factors */
561 examine_parse_independent_vars (struct lexer *lexer, const struct dictionary *dict, struct cmd_examine *cmd)
564 struct factor *sf = xmalloc (sizeof *sf);
566 if ( (lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
567 && lex_token (lexer) != T_ALL)
574 sf->indep_var[0] = parse_variable (lexer, dict);
575 sf->indep_var[1] = 0;
577 if ( lex_token (lexer) == T_BY )
580 lex_match (lexer, T_BY);
582 if ( (lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
583 && lex_token (lexer) != T_ALL)
589 sf->indep_var[1] = parse_variable (lexer, dict);
594 sf->fstats = hsh_create (4,
595 (hsh_compare_func *) factor_statistics_compare,
596 (hsh_hash_func *) factor_statistics_hash,
597 (hsh_free_func *) factor_statistics_free,
603 lex_match (lexer, ',');
605 if ( lex_token (lexer) == '.' || lex_token (lexer) == '/' )
608 success = examine_parse_independent_vars (lexer, dict, cmd);
619 void populate_percentiles (struct tab_table *tbl, int col, int row,
620 const struct metrics *m);
622 void populate_descriptives (struct tab_table *t, int col, int row,
623 const struct metrics *fs);
625 void populate_extremes (struct tab_table *t, int col, int row, int n,
626 const struct metrics *m);
628 void populate_summary (struct tab_table *t, int col, int row,
629 const struct metrics *m);
634 /* Perform calculations for the sub factors */
636 factor_calc (const struct ccase *c, int case_no, double weight,
640 struct factor *fctr = factors;
644 struct factor_statistics **foo ;
645 union value *indep_vals[2] ;
647 indep_vals[0] = value_dup (
648 case_data (c, fctr->indep_var[0]),
649 var_get_width (fctr->indep_var[0])
652 if ( fctr->indep_var[1] )
653 indep_vals[1] = value_dup (
654 case_data (c, fctr->indep_var[1]),
655 var_get_width (fctr->indep_var[1])
659 const union value sm = {SYSMIS};
660 indep_vals[1] = value_dup (&sm, 0);
663 assert (fctr->fstats);
665 foo = ( struct factor_statistics ** )
666 hsh_probe (fctr->fstats, (void *) &indep_vals);
671 *foo = create_factor_statistics (n_dependent_vars,
675 for ( v = 0 ; v < n_dependent_vars ; ++v )
677 metrics_precalc ( & (*foo)->m[v] );
683 free (indep_vals[0]);
684 free (indep_vals[1]);
687 for ( v = 0 ; v < n_dependent_vars ; ++v )
689 const struct variable *var = dependent_vars[v];
690 union value *val = value_dup (
695 if (case_missing || var_is_value_missing (var, val, exclude_values))
701 metrics_calc ( & (*foo)->m[v], val, weight, case_no);
711 run_examine (struct cmd_examine *cmd, struct casereader *input,
714 struct dictionary *dict = dataset_dict (ds);
722 if (!casereader_peek (input, 0, &c))
724 casereader_destroy (input);
727 output_split_file_values (ds, &c);
730 input = casereader_create_filter_weight (input, dict, NULL, NULL);
731 input = casereader_create_counter (input, &case_no, 0);
733 /* Make sure we haven't got rubbish left over from a
738 struct factor *next = fctr->next;
740 hsh_clear (fctr->fstats);
747 for ( v = 0 ; v < n_dependent_vars ; ++v )
748 metrics_precalc (&totals[v]);
750 for (; casereader_read (input, &c); case_destroy (&c))
752 int case_missing = 0;
753 const double weight = dict_get_case_weight (dict, &c, NULL);
755 if ( cmd->miss == XMN_LISTWISE )
757 for ( v = 0 ; v < n_dependent_vars ; ++v )
759 const struct variable *var = dependent_vars[v];
760 union value *val = value_dup (
765 if ( var_is_value_missing (var, val, exclude_values))
772 for ( v = 0 ; v < n_dependent_vars ; ++v )
774 const struct variable *var = dependent_vars[v];
775 union value *val = value_dup (
780 if ( var_is_value_missing (var, val, exclude_values)
787 metrics_calc (&totals[v], val, weight, case_no);
792 factor_calc (&c, case_no, weight, case_missing);
794 ok = casereader_destroy (input);
796 for ( v = 0 ; v < n_dependent_vars ; ++v)
801 struct hsh_iterator hi;
802 struct factor_statistics *fs;
804 for ( fs = hsh_first (fctr->fstats, &hi);
806 fs = hsh_next (fctr->fstats, &hi))
809 fs->m[v].ptile_hash = list_to_ptile_hash (&percentile_list);
810 fs->m[v].ptile_alg = percentile_algorithm;
811 metrics_postcalc (&fs->m[v]);
817 totals[v].ptile_hash = list_to_ptile_hash (&percentile_list);
818 totals[v].ptile_alg = percentile_algorithm;
819 metrics_postcalc (&totals[v]);
823 /* Make sure that the combination of factors are complete */
828 struct hsh_iterator hi;
829 struct hsh_iterator hi0;
830 struct hsh_iterator hi1;
831 struct factor_statistics *fs;
833 struct hsh_table *idh0=0;
834 struct hsh_table *idh1=0;
838 idh0 = hsh_create (4, (hsh_compare_func *) compare_values,
839 (hsh_hash_func *) hash_value,
842 idh1 = hsh_create (4, (hsh_compare_func *) compare_values,
843 (hsh_hash_func *) hash_value,
847 for ( fs = hsh_first (fctr->fstats, &hi);
849 fs = hsh_next (fctr->fstats, &hi))
851 hsh_insert (idh0, (void *) &fs->id[0]);
852 hsh_insert (idh1, (void *) &fs->id[1]);
855 /* Ensure that the factors combination is complete */
856 for ( val0 = hsh_first (idh0, &hi0);
858 val0 = hsh_next (idh0, &hi0))
860 for ( val1 = hsh_first (idh1, &hi1);
862 val1 = hsh_next (idh1, &hi1))
864 struct factor_statistics **ffs;
869 ffs = (struct factor_statistics **)
870 hsh_probe (fctr->fstats, (void *) &key );
874 (*ffs) = create_factor_statistics (n_dependent_vars,
876 for ( i = 0 ; i < n_dependent_vars ; ++i )
877 metrics_precalc ( & (*ffs)->m[i]);
885 fctr->fs = (struct factor_statistics **) hsh_sort_copy (fctr->fstats);
897 for ( i = 0 ; i < n_dependent_vars ; ++i )
899 metrics_destroy (&totals[i]);
906 show_summary (const struct variable **dependent_var, int n_dep_var,
907 const struct factor *fctr)
909 static const char *subtitle[]=
917 int heading_columns ;
919 const int heading_rows = 3;
920 struct tab_table *tbl;
928 n_factors = hsh_count (fctr->fstats);
929 n_rows = n_dep_var * n_factors ;
931 if ( fctr->indep_var[1] )
940 n_rows += heading_rows;
942 n_cols = heading_columns + 6;
944 tbl = tab_create (n_cols,n_rows,0);
945 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
947 tab_dim (tbl, tab_natural_dimensions);
949 /* Outline the box */
954 n_cols - 1, n_rows - 1);
956 /* Vertical lines for the data only */
961 n_cols - 1, n_rows - 1);
964 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
965 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
966 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
968 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
971 tab_title (tbl, _ ("Case Processing Summary"));
974 tab_joint_text (tbl, heading_columns, 0,
976 TAB_CENTER | TAT_TITLE,
979 /* Remove lines ... */
986 for ( i = 0 ; i < 3 ; ++i )
988 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
991 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
994 tab_joint_text (tbl, heading_columns + i*2 , 1,
995 heading_columns + i*2 + 1, 1,
996 TAB_CENTER | TAT_TITLE,
999 tab_box (tbl, -1, -1,
1001 heading_columns + i*2, 1,
1002 heading_columns + i*2 + 1, 1);
1007 /* Titles for the independent variables */
1010 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1011 var_to_string (fctr->indep_var[0]));
1013 if ( fctr->indep_var[1] )
1015 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1016 var_to_string (fctr->indep_var[1]));
1022 for ( i = 0 ; i < n_dep_var ; ++i )
1026 n_factors = hsh_count (fctr->fstats);
1030 tab_hline (tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
1033 0, i * n_factors + heading_rows,
1034 TAB_LEFT | TAT_TITLE,
1035 var_to_string (dependent_var[i])
1040 populate_summary (tbl, heading_columns,
1041 (i * n_factors) + heading_rows,
1047 struct factor_statistics **fs = fctr->fs;
1049 const union value *prev = NULL;
1054 0 != compare_values (prev, (*fs)->id[0],
1055 var_get_width (fctr->indep_var[0])))
1059 (i * n_factors ) + count +
1061 TAB_LEFT | TAT_TITLE,
1062 var_get_value_name (fctr->indep_var[0],
1066 if (fctr->indep_var[1] && count > 0 )
1067 tab_hline (tbl, TAL_1, 1, n_cols - 1,
1068 (i * n_factors ) + count + heading_rows);
1072 prev = (*fs)->id[0];
1075 if ( fctr->indep_var[1])
1078 (i * n_factors ) + count +
1080 TAB_LEFT | TAT_TITLE,
1081 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
1084 populate_summary (tbl, heading_columns,
1085 (i * n_factors) + count
1100 populate_summary (struct tab_table *t, int col, int row,
1101 const struct metrics *m)
1104 const double total = m->n + m->n_missing ;
1106 tab_float (t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1107 tab_float (t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1108 tab_float (t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1112 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1113 100.0 * m->n / total );
1115 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1116 100.0 * m->n_missing / total );
1118 /* This seems a bit pointless !!! */
1119 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1120 100.0 * total / total );
1131 show_extremes (const struct variable **dependent_var, int n_dep_var,
1132 const struct factor *fctr, int n_extremities)
1135 int heading_columns ;
1137 const int heading_rows = 1;
1138 struct tab_table *tbl;
1145 heading_columns = 2;
1146 n_factors = hsh_count (fctr->fstats);
1148 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1150 if ( fctr->indep_var[1] )
1151 heading_columns = 3;
1155 heading_columns = 1;
1156 n_rows = n_dep_var * 2 * n_extremities;
1159 n_rows += heading_rows;
1161 heading_columns += 2;
1162 n_cols = heading_columns + 2;
1164 tbl = tab_create (n_cols,n_rows,0);
1165 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1167 tab_dim (tbl, tab_natural_dimensions);
1169 /* Outline the box, No internal lines*/
1174 n_cols - 1, n_rows - 1);
1176 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1178 tab_title (tbl, _ ("Extreme Values"));
1180 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1181 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1185 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1186 var_to_string (fctr->indep_var[0]));
1188 if ( fctr->indep_var[1] )
1189 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1190 var_to_string (fctr->indep_var[1]));
1193 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _ ("Value"));
1194 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _ ("Case Number"));
1196 for ( i = 0 ; i < n_dep_var ; ++i )
1200 tab_hline (tbl, TAL_1, 0, n_cols -1 ,
1201 i * 2 * n_extremities * n_factors + heading_rows);
1204 i * 2 * n_extremities * n_factors + heading_rows,
1205 TAB_LEFT | TAT_TITLE,
1206 var_to_string (dependent_var[i])
1211 populate_extremes (tbl, heading_columns - 2,
1212 i * 2 * n_extremities * n_factors + heading_rows,
1213 n_extremities, &totals[i]);
1217 struct factor_statistics **fs = fctr->fs;
1219 const union value *prev = NULL;
1223 const int row = heading_rows + ( 2 * n_extremities ) *
1224 ( ( i * n_factors ) + count );
1227 if ( !prev || 0 != compare_values (prev, (*fs)->id[0],
1228 var_get_width (fctr->indep_var[0])))
1232 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1236 TAB_LEFT | TAT_TITLE,
1237 var_get_value_name (fctr->indep_var[0],
1242 prev = (*fs)->id[0];
1244 if (fctr->indep_var[1] && count > 0 )
1245 tab_hline (tbl, TAL_1, 2, n_cols - 1, row);
1247 if ( fctr->indep_var[1])
1248 tab_text (tbl, 2, row,
1249 TAB_LEFT | TAT_TITLE,
1250 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
1253 populate_extremes (tbl, heading_columns - 2,
1268 /* Fill in the extremities table */
1270 populate_extremes (struct tab_table *t,
1271 int col, int row, int n, const struct metrics *m)
1277 tab_text (t, col, row,
1278 TAB_RIGHT | TAT_TITLE ,
1282 tab_text (t, col, row + n ,
1283 TAB_RIGHT | TAT_TITLE ,
1288 tab_hline (t, TAL_1, col, col + 3, row + n );
1290 for (extremity = 0; extremity < n ; ++extremity )
1293 tab_float (t, col + 1, row + extremity,
1295 extremity + 1, 8, 0);
1299 tab_float (t, col + 1, row + extremity + n,
1301 extremity + 1, 8, 0);
1307 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1310 const struct weighted_value *wv = m->wvp[idx];
1311 struct case_node *cn = wv->case_nos;
1314 for (j = 0 ; j < wv->w ; ++j )
1316 if ( extremity + j >= n )
1319 tab_float (t, col + 3, row + extremity + j + n,
1323 tab_float (t, col + 2, row + extremity + j + n,
1332 extremity += wv->w ;
1337 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1340 const struct weighted_value *wv = m->wvp[idx];
1341 struct case_node *cn = wv->case_nos;
1343 for (j = 0 ; j < wv->w ; ++j )
1345 if ( extremity + j >= n )
1348 tab_float (t, col + 3, row + extremity + j,
1352 tab_float (t, col + 2, row + extremity + j,
1361 extremity += wv->w ;
1366 /* Show the descriptives table */
1368 show_descriptives (const struct variable **dependent_var,
1370 struct factor *fctr)
1373 int heading_columns ;
1375 const int n_stat_rows = 13;
1377 const int heading_rows = 1;
1379 struct tab_table *tbl;
1386 heading_columns = 4;
1387 n_factors = hsh_count (fctr->fstats);
1389 n_rows = n_dep_var * n_stat_rows * n_factors;
1391 if ( fctr->indep_var[1] )
1392 heading_columns = 5;
1396 heading_columns = 3;
1397 n_rows = n_dep_var * n_stat_rows;
1400 n_rows += heading_rows;
1402 n_cols = heading_columns + 2;
1405 tbl = tab_create (n_cols, n_rows, 0);
1407 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1409 tab_dim (tbl, tab_natural_dimensions);
1411 /* Outline the box and have no internal lines*/
1416 n_cols - 1, n_rows - 1);
1418 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1420 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1421 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1422 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1424 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _ ("Statistic"));
1425 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _ ("Std. Error"));
1427 tab_title (tbl, _ ("Descriptives"));
1430 for ( i = 0 ; i < n_dep_var ; ++i )
1432 const int row = heading_rows + i * n_stat_rows * n_factors ;
1435 tab_hline (tbl, TAL_1, 0, n_cols - 1, row );
1438 i * n_stat_rows * n_factors + heading_rows,
1439 TAB_LEFT | TAT_TITLE,
1440 var_to_string (dependent_var[i])
1446 const union value *prev = NULL;
1448 struct factor_statistics **fs = fctr->fs;
1451 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1452 var_to_string (fctr->indep_var[0]));
1455 if ( fctr->indep_var[1])
1456 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1457 var_to_string (fctr->indep_var[1]));
1461 const int row = heading_rows + n_stat_rows *
1462 ( ( i * n_factors ) + count );
1465 if ( !prev || 0 != compare_values (prev, (*fs)->id[0],
1466 var_get_width (fctr->indep_var[0])))
1470 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1474 TAB_LEFT | TAT_TITLE,
1475 var_get_value_name (fctr->indep_var[0],
1480 prev = (*fs)->id[0];
1482 if (fctr->indep_var[1] && count > 0 )
1483 tab_hline (tbl, TAL_1, 2, n_cols - 1, row);
1485 if ( fctr->indep_var[1])
1486 tab_text (tbl, 2, row,
1487 TAB_LEFT | TAT_TITLE,
1488 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
1491 populate_descriptives (tbl, heading_columns - 2,
1492 row, & (*fs)->m[i]);
1503 populate_descriptives (tbl, heading_columns - 2,
1504 i * n_stat_rows * n_factors + heading_rows,
1514 /* Fill in the descriptives data */
1516 populate_descriptives (struct tab_table *tbl, int col, int row,
1517 const struct metrics *m)
1519 const double t = gsl_cdf_tdist_Qinv ((1 - cmd.n_cinterval[0] / 100.0)/2.0,
1524 TAB_LEFT | TAT_TITLE,
1527 tab_float (tbl, col + 2,
1533 tab_float (tbl, col + 3,
1542 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1543 _ ("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1546 tab_text (tbl, col + 1,
1548 TAB_LEFT | TAT_TITLE,
1551 tab_float (tbl, col + 2,
1554 m->mean - t * m->se_mean,
1557 tab_text (tbl, col + 1,
1559 TAB_LEFT | TAT_TITLE,
1563 tab_float (tbl, col + 2,
1566 m->mean + t * m->se_mean,
1571 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1572 _ ("5%% Trimmed Mean"));
1574 tab_float (tbl, col + 2,
1582 TAB_LEFT | TAT_TITLE,
1586 struct percentile *p;
1589 p = hsh_find (m->ptile_hash, &d);
1594 tab_float (tbl, col + 2,
1604 TAB_LEFT | TAT_TITLE,
1607 tab_float (tbl, col + 2,
1616 TAB_LEFT | TAT_TITLE,
1617 _ ("Std. Deviation"));
1620 tab_float (tbl, col + 2,
1629 TAB_LEFT | TAT_TITLE,
1632 tab_float (tbl, col + 2,
1640 TAB_LEFT | TAT_TITLE,
1643 tab_float (tbl, col + 2,
1652 TAB_LEFT | TAT_TITLE,
1656 tab_float (tbl, col + 2,
1664 TAB_LEFT | TAT_TITLE,
1665 _ ("Interquartile Range"));
1668 struct percentile *p1;
1669 struct percentile *p2;
1672 p1 = hsh_find (m->ptile_hash, &d);
1675 p2 = hsh_find (m->ptile_hash, &d);
1680 tab_float (tbl, col + 2,
1691 TAB_LEFT | TAT_TITLE,
1695 tab_float (tbl, col + 2,
1701 /* stderr of skewness */
1702 tab_float (tbl, col + 3,
1711 TAB_LEFT | TAT_TITLE,
1715 tab_float (tbl, col + 2,
1721 /* stderr of kurtosis */
1722 tab_float (tbl, col + 3,
1734 box_plot_variables (const struct factor *fctr,
1735 const struct variable **vars, int n_vars,
1736 const struct variable *id)
1740 struct factor_statistics **fs ;
1744 box_plot_group (fctr, vars, n_vars, id);
1748 for ( fs = fctr->fs ; *fs ; ++fs )
1750 double y_min = DBL_MAX;
1751 double y_max = -DBL_MAX;
1752 struct chart *ch = chart_create ();
1753 const char *s = factor_to_string (fctr, *fs, 0 );
1755 chart_write_title (ch, s);
1757 for ( i = 0 ; i < n_vars ; ++i )
1759 y_max = MAX (y_max, (*fs)->m[i].max);
1760 y_min = MIN (y_min, (*fs)->m[i].min);
1763 boxplot_draw_yscale (ch, y_max, y_min);
1765 for ( i = 0 ; i < n_vars ; ++i )
1768 const double box_width = (ch->data_right - ch->data_left)
1771 const double box_centre = ( i * 2 + 1) * box_width
1774 boxplot_draw_boxplot (ch,
1775 box_centre, box_width,
1777 var_to_string (vars[i]));
1789 /* Do a box plot, grouping all factors into one plot ;
1790 each dependent variable has its own plot.
1793 box_plot_group (const struct factor *fctr,
1794 const struct variable **vars,
1796 const struct variable *id UNUSED)
1801 for ( i = 0 ; i < n_vars ; ++i )
1803 struct factor_statistics **fs ;
1806 ch = chart_create ();
1808 boxplot_draw_yscale (ch, totals[i].max, totals[i].min);
1814 for ( fs = fctr->fs ; *fs ; ++fs )
1817 chart_write_title (ch, _ ("Boxplot of %s vs. %s"),
1818 var_to_string (vars[i]), var_to_string (fctr->indep_var[0]) );
1820 for ( fs = fctr->fs ; *fs ; ++fs )
1823 const char *s = factor_to_string_concise (fctr, *fs);
1825 const double box_width = (ch->data_right - ch->data_left)
1826 / (n_factors * 2.0 ) ;
1828 const double box_centre = ( f++ * 2 + 1) * box_width
1831 boxplot_draw_boxplot (ch,
1832 box_centre, box_width,
1839 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1840 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1842 chart_write_title (ch, _ ("Boxplot"));
1844 boxplot_draw_boxplot (ch,
1845 box_centre, box_width,
1847 var_to_string (vars[i]) );
1856 /* Plot the normal and detrended normal plots for m
1857 Label the plots with factorname */
1859 np_plot (const struct metrics *m, const char *factorname)
1862 double yfirst=0, ylast=0;
1865 struct chart *np_chart;
1867 /* Detrended Normal Plot */
1868 struct chart *dnp_chart;
1870 /* The slope and intercept of the ideal normal probability line */
1871 const double slope = 1.0 / m->stddev;
1872 const double intercept = - m->mean / m->stddev;
1874 /* Cowardly refuse to plot an empty data set */
1875 if ( m->n_data == 0 )
1878 np_chart = chart_create ();
1879 dnp_chart = chart_create ();
1881 if ( !np_chart || ! dnp_chart )
1884 chart_write_title (np_chart, _ ("Normal Q-Q Plot of %s"), factorname);
1885 chart_write_xlabel (np_chart, _ ("Observed Value"));
1886 chart_write_ylabel (np_chart, _ ("Expected Normal"));
1889 chart_write_title (dnp_chart, _ ("Detrended Normal Q-Q Plot of %s"),
1891 chart_write_xlabel (dnp_chart, _ ("Observed Value"));
1892 chart_write_ylabel (dnp_chart, _ ("Dev from Normal"));
1894 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1895 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1899 /* Need to make sure that both the scatter plot and the ideal fit into the
1901 double x_lower = MIN (m->min, (yfirst - intercept) / slope) ;
1902 double x_upper = MAX (m->max, (ylast - intercept) / slope) ;
1903 double slack = (x_upper - x_lower) * 0.05 ;
1905 chart_write_xscale (np_chart, x_lower - slack, x_upper + slack, 5);
1907 chart_write_xscale (dnp_chart, m->min, m->max, 5);
1911 chart_write_yscale (np_chart, yfirst, ylast, 5);
1914 /* We have to cache the detrended data, beacause we need to
1915 find its limits before we can plot it */
1916 double *d_data = xnmalloc (m->n_data, sizeof *d_data);
1917 double d_max = -DBL_MAX;
1918 double d_min = DBL_MAX;
1919 for ( i = 0 ; i < m->n_data; ++i )
1921 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1923 chart_datum (np_chart, 0, m->wvp[i]->v.f, ns);
1925 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1927 if ( d_data[i] < d_min ) d_min = d_data[i];
1928 if ( d_data[i] > d_max ) d_max = d_data[i];
1930 chart_write_yscale (dnp_chart, d_min, d_max, 5);
1932 for ( i = 0 ; i < m->n_data; ++i )
1933 chart_datum (dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1938 chart_line (np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1939 chart_line (dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1941 chart_submit (np_chart);
1942 chart_submit (dnp_chart);
1948 /* Show the percentiles */
1950 show_percentiles (const struct variable **dependent_var,
1952 struct factor *fctr)
1954 struct tab_table *tbl;
1960 struct hsh_table *ptiles ;
1962 int n_heading_columns;
1963 const int n_heading_rows = 2;
1964 const int n_stat_rows = 2;
1970 struct factor_statistics **fs = fctr->fs ;
1971 n_heading_columns = 3;
1972 n_factors = hsh_count (fctr->fstats);
1974 ptiles = (*fs)->m[0].ptile_hash;
1976 if ( fctr->indep_var[1] )
1977 n_heading_columns = 4;
1982 n_heading_columns = 2;
1984 ptiles = totals[0].ptile_hash;
1987 n_ptiles = hsh_count (ptiles);
1989 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1991 n_cols = n_heading_columns + n_ptiles ;
1993 tbl = tab_create (n_cols, n_rows, 0);
1995 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1997 tab_dim (tbl, tab_natural_dimensions);
1999 /* Outline the box and have no internal lines*/
2004 n_cols - 1, n_rows - 1);
2006 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
2008 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
2011 tab_title (tbl, _ ("Percentiles"));
2014 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
2021 n_heading_columns - 1, n_rows - 1);
2027 n_heading_columns, n_heading_rows - 1,
2028 n_cols - 1, n_rows - 1);
2030 tab_joint_text (tbl, n_heading_columns + 1, 0,
2032 TAB_CENTER | TAT_TITLE ,
2037 /* Put in the percentile break points as headings */
2039 struct percentile **p = (struct percentile **) hsh_sort (ptiles);
2044 tab_float (tbl, n_heading_columns + i++ , 1,
2053 for ( i = 0 ; i < n_dep_var ; ++i )
2055 const int n_stat_rows = 2;
2056 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
2059 tab_hline (tbl, TAL_1, 0, n_cols - 1, row );
2062 i * n_stat_rows * n_factors + n_heading_rows,
2063 TAB_LEFT | TAT_TITLE,
2064 var_to_string (dependent_var[i])
2069 const union value *prev = NULL ;
2070 struct factor_statistics **fs = fctr->fs;
2073 tab_text (tbl, 1, n_heading_rows - 1,
2074 TAB_CENTER | TAT_TITLE,
2075 var_to_string (fctr->indep_var[0]));
2078 if ( fctr->indep_var[1])
2079 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2080 var_to_string (fctr->indep_var[1]));
2084 const int row = n_heading_rows + n_stat_rows *
2085 ( ( i * n_factors ) + count );
2088 if ( !prev || 0 != compare_values (prev, (*fs)->id[0],
2089 var_get_width (fctr->indep_var[0])))
2093 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2097 TAB_LEFT | TAT_TITLE,
2098 var_get_value_name (fctr->indep_var[0],
2105 prev = (*fs)->id[0];
2107 if (fctr->indep_var[1] && count > 0 )
2108 tab_hline (tbl, TAL_1, 2, n_cols - 1, row);
2110 if ( fctr->indep_var[1])
2111 tab_text (tbl, 2, row,
2112 TAB_LEFT | TAT_TITLE,
2113 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
2117 populate_percentiles (tbl, n_heading_columns - 1,
2118 row, & (*fs)->m[i]);
2129 populate_percentiles (tbl, n_heading_columns - 1,
2130 i * n_stat_rows * n_factors + n_heading_rows,
2147 populate_percentiles (struct tab_table *tbl, int col, int row,
2148 const struct metrics *m)
2152 struct percentile **p = (struct percentile **) hsh_sort (m->ptile_hash);
2156 TAB_LEFT | TAT_TITLE,
2157 _ ("Tukey\'s Hinges")
2162 TAB_LEFT | TAT_TITLE,
2163 ptile_alg_desc[m->ptile_alg]
2170 tab_float (tbl, col + i + 1 , row,
2173 if ( (*p)->p == 25 )
2174 tab_float (tbl, col + i + 1 , row + 1,
2178 if ( (*p)->p == 50 )
2179 tab_float (tbl, col + i + 1 , row + 1,
2183 if ( (*p)->p == 75 )
2184 tab_float (tbl, col + i + 1 , row + 1,
2199 factor_to_string (const struct factor *fctr,
2200 const struct factor_statistics *fs,
2201 const struct variable *var)
2204 static char buf1[100];
2210 sprintf (buf1, "%s (",var_to_string (var) );
2213 snprintf (buf2, 100, "%s = %s",
2214 var_to_string (fctr->indep_var[0]),
2215 var_get_value_name (fctr->indep_var[0], fs->id[0]));
2217 strcat (buf1, buf2);
2219 if ( fctr->indep_var[1] )
2221 sprintf (buf2, "; %s = %s)",
2222 var_to_string (fctr->indep_var[1]),
2223 var_get_value_name (fctr->indep_var[1], fs->id[1]));
2224 strcat (buf1, buf2);
2238 factor_to_string_concise (const struct factor *fctr,
2239 struct factor_statistics *fs)
2243 static char buf[100];
2247 snprintf (buf, 100, "%s",
2248 var_get_value_name (fctr->indep_var[0], fs->id[0]));
2250 if ( fctr->indep_var[1] )
2252 sprintf (buf2, ",%s)", var_get_value_name (fctr->indep_var[1],