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/compiler.h>
36 #include <libpspp/hash.h>
37 #include <libpspp/message.h>
38 #include <libpspp/misc.h>
39 #include <libpspp/str.h>
40 #include <math/factor-stats.h>
41 #include <math/moments.h>
42 #include <math/percentiles.h>
43 #include <output/charts/box-whisker.h>
44 #include <output/charts/cartesian.h>
45 #include <output/manager.h>
46 #include <output/table.h>
52 #define _(msgid) gettext (msgid)
53 #define N_(msgid) msgid
56 #include <output/chart.h>
57 #include <output/charts/plot-hist.h>
58 #include <output/charts/plot-chart.h>
65 missing=miss:pairwise/!listwise,
67 incl:include/!exclude;
68 +compare=cmp:variables/!groups;
71 +plot[plt_]=stemleaf,boxplot,npplot,:spreadlevel(*d:n),histogram,all,none;
73 +statistics[st_]=descriptives,:extreme(*d:n),all,none.
82 static struct cmd_examine cmd;
84 static const struct variable **dependent_vars;
86 static size_t n_dependent_vars;
91 /* The independent variable */
92 struct variable *indep_var[2];
95 /* Hash table of factor stats indexed by 2 values */
96 struct hsh_table *fstats;
98 /* The hash table after it has been crunched */
99 struct factor_statistics **fs;
105 /* Linked list of factors */
106 static struct factor *factors = 0;
108 static struct metrics *totals = 0;
110 /* Parse the clause specifying the factors */
111 static int examine_parse_independent_vars (struct lexer *lexer, const struct dictionary *dict, struct cmd_examine *cmd);
115 /* Output functions */
116 static void show_summary (const struct variable **dependent_var, int n_dep_var,
117 const struct factor *f);
119 static void show_extremes (const struct variable **dependent_var,
121 const struct factor *factor,
124 static void show_descriptives (const struct variable **dependent_var,
126 struct factor *factor);
128 static void show_percentiles (const struct variable **dependent_var,
130 struct factor *factor);
135 void np_plot (const struct metrics *m, const char *factorname);
138 void box_plot_group (const struct factor *fctr,
139 const struct variable **vars, int n_vars,
140 const struct variable *id
144 void box_plot_variables (const struct factor *fctr,
145 const struct variable **vars, int n_vars,
146 const struct variable *id
151 /* Per Split function */
152 static void run_examine (struct cmd_examine *, struct casereader *,
155 static void output_examine (void);
158 void factor_calc (const struct ccase *c, int case_no,
159 double weight, int case_missing);
162 /* Represent a factor as a string, so it can be
163 printed in a human readable fashion */
164 const char * factor_to_string (const struct factor *fctr,
165 const struct factor_statistics *fs,
166 const struct variable *var);
169 /* Represent a factor as a string, so it can be
170 printed in a human readable fashion,
171 but sacrificing some readablility for the sake of brevity */
172 const char *factor_to_string_concise (const struct factor *fctr,
173 struct factor_statistics *fs);
178 /* Categories of missing values to exclude. */
179 static enum mv_class exclude_values;
183 static subc_list_double percentile_list;
185 static enum pc_alg percentile_algorithm;
187 static short sbc_percentile;
191 cmd_examine (struct lexer *lexer, struct dataset *ds)
193 struct casegrouper *grouper;
194 struct casereader *group;
197 subc_list_double_create (&percentile_list);
198 percentile_algorithm = PC_HAVERAGE;
200 if ( !parse_examine (lexer, ds, &cmd, NULL) )
202 subc_list_double_destroy (&percentile_list);
206 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
207 exclude_values = cmd.incl == XMN_INCLUDE ? MV_SYSTEM : MV_ANY;
209 if ( cmd.st_n == SYSMIS )
212 if ( ! cmd.sbc_cinterval)
213 cmd.n_cinterval[0] = 95.0;
215 /* If descriptives have been requested, make sure the
216 quartiles are calculated */
217 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES] )
219 subc_list_double_push (&percentile_list, 25);
220 subc_list_double_push (&percentile_list, 50);
221 subc_list_double_push (&percentile_list, 75);
224 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
225 while (casegrouper_get_next_group (grouper, &group))
226 run_examine (&cmd, group, ds);
227 ok = casegrouper_destroy (grouper);
228 ok = proc_commit (ds) && ok;
235 if ( dependent_vars )
236 free (dependent_vars);
239 struct factor *f = factors ;
242 struct factor *ff = f;
246 hsh_destroy ( ff->fstats ) ;
252 subc_list_double_destroy (&percentile_list);
254 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
259 /* Show all the appropriate tables */
261 output_examine (void)
265 /* Show totals if appropriate */
266 if ( ! cmd.sbc_nototal || factors == 0 )
268 show_summary (dependent_vars, n_dependent_vars, 0);
270 if ( cmd.sbc_statistics )
272 if ( cmd.a_statistics[XMN_ST_EXTREME])
273 show_extremes (dependent_vars, n_dependent_vars, 0, cmd.st_n);
275 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
276 show_descriptives (dependent_vars, n_dependent_vars, 0);
279 if ( sbc_percentile )
280 show_percentiles (dependent_vars, n_dependent_vars, 0);
285 if ( cmd.a_plot[XMN_PLT_STEMLEAF] )
286 msg (SW, _ ("%s is not currently supported."), "STEMLEAF");
288 if ( cmd.a_plot[XMN_PLT_SPREADLEVEL] )
289 msg (SW, _ ("%s is not currently supported."), "SPREADLEVEL");
291 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
293 for ( v = 0 ; v < n_dependent_vars; ++v )
294 np_plot (&totals[v], var_to_string (dependent_vars[v]));
297 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
299 if ( cmd.cmp == XMN_GROUPS )
301 box_plot_group (0, (const struct variable **) dependent_vars,
302 n_dependent_vars, cmd.v_id);
305 box_plot_variables (0,
306 (const struct variable **) dependent_vars,
307 n_dependent_vars, cmd.v_id);
310 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
312 for ( v = 0 ; v < n_dependent_vars; ++v )
314 struct normal_curve normal;
316 normal.N = totals[v].n;
317 normal.mean = totals[v].mean;
318 normal.stddev = totals[v].stddev;
320 histogram_plot (totals[v].histogram,
321 var_to_string (dependent_vars[v]),
331 /* Show grouped statistics as appropriate */
335 show_summary (dependent_vars, n_dependent_vars, fctr);
337 if ( cmd.sbc_statistics )
339 if ( cmd.a_statistics[XMN_ST_EXTREME])
340 show_extremes (dependent_vars, n_dependent_vars, fctr, cmd.st_n);
342 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
343 show_descriptives (dependent_vars, n_dependent_vars, fctr);
346 if ( sbc_percentile )
347 show_percentiles (dependent_vars, n_dependent_vars, fctr);
354 struct factor_statistics **fs = fctr->fs ;
356 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
358 if ( cmd.cmp == XMN_VARIABLES )
359 box_plot_variables (fctr,
360 (const struct variable **) dependent_vars,
361 n_dependent_vars, cmd.v_id);
363 box_plot_group (fctr,
364 (const struct variable **) dependent_vars,
365 n_dependent_vars, cmd.v_id);
368 for ( v = 0 ; v < n_dependent_vars; ++v )
371 for ( fs = fctr->fs ; *fs ; ++fs )
373 const char *s = factor_to_string (fctr, *fs, dependent_vars[v]);
375 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
376 np_plot (& (*fs)->m[v], s);
378 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
380 struct normal_curve normal;
382 normal.N = (*fs)->m[v].n;
383 normal.mean = (*fs)->m[v].mean;
384 normal.stddev = (*fs)->m[v].stddev;
386 histogram_plot ((*fs)->m[v].histogram,
390 } /* for ( fs .... */
392 } /* for ( v = 0 ..... */
402 /* Create a hash table of percentiles and their values from the list of
404 static struct hsh_table *
405 list_to_ptile_hash (const subc_list_double *l)
409 struct hsh_table *h ;
411 h = hsh_create (subc_list_double_count (l),
412 (hsh_compare_func *) ptile_compare,
413 (hsh_hash_func *) ptile_hash,
414 (hsh_free_func *) free,
418 for ( i = 0 ; i < subc_list_double_count (l) ; ++i )
420 struct percentile *p = xmalloc (sizeof *p);
422 p->p = subc_list_double_at (l,i);
433 /* Parse the PERCENTILES subcommand */
435 xmn_custom_percentiles (struct lexer *lexer, struct dataset *ds UNUSED,
436 struct cmd_examine *p UNUSED, void *aux UNUSED)
440 lex_match (lexer, '=');
442 lex_match (lexer, '(');
444 while ( lex_is_number (lexer) )
446 subc_list_double_push (&percentile_list, lex_number (lexer));
450 lex_match (lexer, ',') ;
452 lex_match (lexer, ')');
454 lex_match (lexer, '=');
456 if ( lex_match_id (lexer, "HAVERAGE"))
457 percentile_algorithm = PC_HAVERAGE;
459 else if ( lex_match_id (lexer, "WAVERAGE"))
460 percentile_algorithm = PC_WAVERAGE;
462 else if ( lex_match_id (lexer, "ROUND"))
463 percentile_algorithm = PC_ROUND;
465 else if ( lex_match_id (lexer, "EMPIRICAL"))
466 percentile_algorithm = PC_EMPIRICAL;
468 else if ( lex_match_id (lexer, "AEMPIRICAL"))
469 percentile_algorithm = PC_AEMPIRICAL;
471 else if ( lex_match_id (lexer, "NONE"))
472 percentile_algorithm = PC_NONE;
475 if ( 0 == subc_list_double_count (&percentile_list))
477 subc_list_double_push (&percentile_list, 5);
478 subc_list_double_push (&percentile_list, 10);
479 subc_list_double_push (&percentile_list, 25);
480 subc_list_double_push (&percentile_list, 50);
481 subc_list_double_push (&percentile_list, 75);
482 subc_list_double_push (&percentile_list, 90);
483 subc_list_double_push (&percentile_list, 95);
489 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
491 xmn_custom_total (struct lexer *lexer UNUSED, struct dataset *ds UNUSED, struct cmd_examine *p, void *aux UNUSED)
493 if ( p->sbc_nototal )
495 msg (SE, _ ("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
503 xmn_custom_nototal (struct lexer *lexer UNUSED, struct dataset *ds UNUSED,
504 struct cmd_examine *p, void *aux UNUSED)
508 msg (SE, _ ("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
517 /* Parser for the variables sub command
518 Returns 1 on success */
520 xmn_custom_variables (struct lexer *lexer, struct dataset *ds, struct cmd_examine *cmd, void *aux UNUSED)
522 const struct dictionary *dict = dataset_dict (ds);
523 lex_match (lexer, '=');
525 if ( (lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
526 && lex_token (lexer) != T_ALL)
531 if (!parse_variables_const (lexer, dict, &dependent_vars, &n_dependent_vars,
532 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
534 free (dependent_vars);
538 assert (n_dependent_vars);
540 totals = xnmalloc (n_dependent_vars, sizeof *totals);
542 if ( lex_match (lexer, T_BY))
545 success = examine_parse_independent_vars (lexer, dict, cmd);
546 if ( success != 1 ) {
547 free (dependent_vars);
558 /* Parse the clause specifying the factors */
560 examine_parse_independent_vars (struct lexer *lexer, const struct dictionary *dict, struct cmd_examine *cmd)
563 struct factor *sf = xmalloc (sizeof *sf);
565 if ( (lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
566 && lex_token (lexer) != T_ALL)
573 sf->indep_var[0] = parse_variable (lexer, dict);
574 sf->indep_var[1] = 0;
576 if ( lex_token (lexer) == T_BY )
579 lex_match (lexer, T_BY);
581 if ( (lex_token (lexer) != T_ID || dict_lookup_var (dict, lex_tokid (lexer)) == NULL)
582 && lex_token (lexer) != T_ALL)
588 sf->indep_var[1] = parse_variable (lexer, dict);
593 sf->fstats = hsh_create (4,
594 (hsh_compare_func *) factor_statistics_compare,
595 (hsh_hash_func *) factor_statistics_hash,
596 (hsh_free_func *) factor_statistics_free,
602 lex_match (lexer, ',');
604 if ( lex_token (lexer) == '.' || lex_token (lexer) == '/' )
607 success = examine_parse_independent_vars (lexer, dict, cmd);
618 void populate_percentiles (struct tab_table *tbl, int col, int row,
619 const struct metrics *m);
621 void populate_descriptives (struct tab_table *t, int col, int row,
622 const struct metrics *fs);
624 void populate_extremes (struct tab_table *t, int col, int row, int n,
625 const struct metrics *m);
627 void populate_summary (struct tab_table *t, int col, int row,
628 const struct metrics *m);
633 /* Perform calculations for the sub factors */
635 factor_calc (const struct ccase *c, int case_no, double weight,
639 struct factor *fctr = factors;
643 struct factor_statistics **foo ;
644 union value *indep_vals[2] ;
646 indep_vals[0] = value_dup (
647 case_data (c, fctr->indep_var[0]),
648 var_get_width (fctr->indep_var[0])
651 if ( fctr->indep_var[1] )
652 indep_vals[1] = value_dup (
653 case_data (c, fctr->indep_var[1]),
654 var_get_width (fctr->indep_var[1])
658 const union value sm = {SYSMIS};
659 indep_vals[1] = value_dup (&sm, 0);
662 assert (fctr->fstats);
664 foo = ( struct factor_statistics ** )
665 hsh_probe (fctr->fstats, (void *) &indep_vals);
670 *foo = create_factor_statistics (n_dependent_vars,
674 for ( v = 0 ; v < n_dependent_vars ; ++v )
676 metrics_precalc ( & (*foo)->m[v] );
682 free (indep_vals[0]);
683 free (indep_vals[1]);
686 for ( v = 0 ; v < n_dependent_vars ; ++v )
688 const struct variable *var = dependent_vars[v];
689 union value *val = value_dup (
694 if (case_missing || var_is_value_missing (var, val, exclude_values))
700 metrics_calc ( & (*foo)->m[v], val, weight, case_no);
710 run_examine (struct cmd_examine *cmd, struct casereader *input,
713 struct dictionary *dict = dataset_dict (ds);
721 if (!casereader_peek (input, 0, &c))
723 casereader_destroy (input);
726 output_split_file_values (ds, &c);
729 input = casereader_create_filter_weight (input, dict, NULL, NULL);
730 input = casereader_create_counter (input, &case_no, 0);
732 /* Make sure we haven't got rubbish left over from a
737 struct factor *next = fctr->next;
739 hsh_clear (fctr->fstats);
746 for ( v = 0 ; v < n_dependent_vars ; ++v )
747 metrics_precalc (&totals[v]);
749 for (; casereader_read (input, &c); case_destroy (&c))
751 int case_missing = 0;
752 const double weight = dict_get_case_weight (dict, &c, NULL);
754 if ( cmd->miss == XMN_LISTWISE )
756 for ( v = 0 ; v < n_dependent_vars ; ++v )
758 const struct variable *var = dependent_vars[v];
759 union value *val = value_dup (
764 if ( var_is_value_missing (var, val, exclude_values))
771 for ( v = 0 ; v < n_dependent_vars ; ++v )
773 const struct variable *var = dependent_vars[v];
774 union value *val = value_dup (
779 if ( var_is_value_missing (var, val, exclude_values)
786 metrics_calc (&totals[v], val, weight, case_no);
791 factor_calc (&c, case_no, weight, case_missing);
793 ok = casereader_destroy (input);
795 for ( v = 0 ; v < n_dependent_vars ; ++v)
800 struct hsh_iterator hi;
801 struct factor_statistics *fs;
803 for ( fs = hsh_first (fctr->fstats, &hi);
805 fs = hsh_next (fctr->fstats, &hi))
808 fs->m[v].ptile_hash = list_to_ptile_hash (&percentile_list);
809 fs->m[v].ptile_alg = percentile_algorithm;
810 metrics_postcalc (&fs->m[v]);
816 totals[v].ptile_hash = list_to_ptile_hash (&percentile_list);
817 totals[v].ptile_alg = percentile_algorithm;
818 metrics_postcalc (&totals[v]);
822 /* Make sure that the combination of factors are complete */
827 struct hsh_iterator hi;
828 struct hsh_iterator hi0;
829 struct hsh_iterator hi1;
830 struct factor_statistics *fs;
832 struct hsh_table *idh0=0;
833 struct hsh_table *idh1=0;
837 idh0 = hsh_create (4, (hsh_compare_func *) compare_values,
838 (hsh_hash_func *) hash_value,
841 idh1 = hsh_create (4, (hsh_compare_func *) compare_values,
842 (hsh_hash_func *) hash_value,
846 for ( fs = hsh_first (fctr->fstats, &hi);
848 fs = hsh_next (fctr->fstats, &hi))
850 hsh_insert (idh0, (void *) &fs->id[0]);
851 hsh_insert (idh1, (void *) &fs->id[1]);
854 /* Ensure that the factors combination is complete */
855 for ( val0 = hsh_first (idh0, &hi0);
857 val0 = hsh_next (idh0, &hi0))
859 for ( val1 = hsh_first (idh1, &hi1);
861 val1 = hsh_next (idh1, &hi1))
863 struct factor_statistics **ffs;
868 ffs = (struct factor_statistics **)
869 hsh_probe (fctr->fstats, (void *) &key );
873 (*ffs) = create_factor_statistics (n_dependent_vars,
875 for ( i = 0 ; i < n_dependent_vars ; ++i )
876 metrics_precalc ( & (*ffs)->m[i]);
884 fctr->fs = (struct factor_statistics **) hsh_sort_copy (fctr->fstats);
896 for ( i = 0 ; i < n_dependent_vars ; ++i )
898 metrics_destroy (&totals[i]);
905 show_summary (const struct variable **dependent_var, int n_dep_var,
906 const struct factor *fctr)
908 static const char *subtitle[]=
916 int heading_columns ;
918 const int heading_rows = 3;
919 struct tab_table *tbl;
927 n_factors = hsh_count (fctr->fstats);
928 n_rows = n_dep_var * n_factors ;
930 if ( fctr->indep_var[1] )
939 n_rows += heading_rows;
941 n_cols = heading_columns + 6;
943 tbl = tab_create (n_cols,n_rows,0);
944 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
946 tab_dim (tbl, tab_natural_dimensions);
948 /* Outline the box */
953 n_cols - 1, n_rows - 1);
955 /* Vertical lines for the data only */
960 n_cols - 1, n_rows - 1);
963 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
964 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
965 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
967 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
970 tab_title (tbl, _ ("Case Processing Summary"));
973 tab_joint_text (tbl, heading_columns, 0,
975 TAB_CENTER | TAT_TITLE,
978 /* Remove lines ... */
985 for ( i = 0 ; i < 3 ; ++i )
987 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
990 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
993 tab_joint_text (tbl, heading_columns + i*2 , 1,
994 heading_columns + i*2 + 1, 1,
995 TAB_CENTER | TAT_TITLE,
998 tab_box (tbl, -1, -1,
1000 heading_columns + i*2, 1,
1001 heading_columns + i*2 + 1, 1);
1006 /* Titles for the independent variables */
1009 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1010 var_to_string (fctr->indep_var[0]));
1012 if ( fctr->indep_var[1] )
1014 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1015 var_to_string (fctr->indep_var[1]));
1021 for ( i = 0 ; i < n_dep_var ; ++i )
1025 n_factors = hsh_count (fctr->fstats);
1029 tab_hline (tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
1032 0, i * n_factors + heading_rows,
1033 TAB_LEFT | TAT_TITLE,
1034 var_to_string (dependent_var[i])
1039 populate_summary (tbl, heading_columns,
1040 (i * n_factors) + heading_rows,
1046 struct factor_statistics **fs = fctr->fs;
1048 const union value *prev = NULL;
1053 0 != compare_values (prev, (*fs)->id[0],
1054 var_get_width (fctr->indep_var[0])))
1058 (i * n_factors ) + count +
1060 TAB_LEFT | TAT_TITLE,
1061 var_get_value_name (fctr->indep_var[0],
1065 if (fctr->indep_var[1] && count > 0 )
1066 tab_hline (tbl, TAL_1, 1, n_cols - 1,
1067 (i * n_factors ) + count + heading_rows);
1071 prev = (*fs)->id[0];
1074 if ( fctr->indep_var[1])
1077 (i * n_factors ) + count +
1079 TAB_LEFT | TAT_TITLE,
1080 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
1083 populate_summary (tbl, heading_columns,
1084 (i * n_factors) + count
1099 populate_summary (struct tab_table *t, int col, int row,
1100 const struct metrics *m)
1103 const double total = m->n + m->n_missing ;
1105 tab_float (t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1106 tab_float (t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1107 tab_float (t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1111 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1112 100.0 * m->n / total );
1114 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1115 100.0 * m->n_missing / total );
1117 /* This seems a bit pointless !!! */
1118 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1119 100.0 * total / total );
1130 show_extremes (const struct variable **dependent_var, int n_dep_var,
1131 const struct factor *fctr, int n_extremities)
1134 int heading_columns ;
1136 const int heading_rows = 1;
1137 struct tab_table *tbl;
1144 heading_columns = 2;
1145 n_factors = hsh_count (fctr->fstats);
1147 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1149 if ( fctr->indep_var[1] )
1150 heading_columns = 3;
1154 heading_columns = 1;
1155 n_rows = n_dep_var * 2 * n_extremities;
1158 n_rows += heading_rows;
1160 heading_columns += 2;
1161 n_cols = heading_columns + 2;
1163 tbl = tab_create (n_cols,n_rows,0);
1164 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1166 tab_dim (tbl, tab_natural_dimensions);
1168 /* Outline the box, No internal lines*/
1173 n_cols - 1, n_rows - 1);
1175 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1177 tab_title (tbl, _ ("Extreme Values"));
1179 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1180 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1184 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1185 var_to_string (fctr->indep_var[0]));
1187 if ( fctr->indep_var[1] )
1188 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1189 var_to_string (fctr->indep_var[1]));
1192 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _ ("Value"));
1193 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _ ("Case Number"));
1195 for ( i = 0 ; i < n_dep_var ; ++i )
1199 tab_hline (tbl, TAL_1, 0, n_cols -1 ,
1200 i * 2 * n_extremities * n_factors + heading_rows);
1203 i * 2 * n_extremities * n_factors + heading_rows,
1204 TAB_LEFT | TAT_TITLE,
1205 var_to_string (dependent_var[i])
1210 populate_extremes (tbl, heading_columns - 2,
1211 i * 2 * n_extremities * n_factors + heading_rows,
1212 n_extremities, &totals[i]);
1216 struct factor_statistics **fs = fctr->fs;
1218 const union value *prev = NULL;
1222 const int row = heading_rows + ( 2 * n_extremities ) *
1223 ( ( i * n_factors ) + count );
1226 if ( !prev || 0 != compare_values (prev, (*fs)->id[0],
1227 var_get_width (fctr->indep_var[0])))
1231 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1235 TAB_LEFT | TAT_TITLE,
1236 var_get_value_name (fctr->indep_var[0],
1241 prev = (*fs)->id[0];
1243 if (fctr->indep_var[1] && count > 0 )
1244 tab_hline (tbl, TAL_1, 2, n_cols - 1, row);
1246 if ( fctr->indep_var[1])
1247 tab_text (tbl, 2, row,
1248 TAB_LEFT | TAT_TITLE,
1249 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
1252 populate_extremes (tbl, heading_columns - 2,
1267 /* Fill in the extremities table */
1269 populate_extremes (struct tab_table *t,
1270 int col, int row, int n, const struct metrics *m)
1276 tab_text (t, col, row,
1277 TAB_RIGHT | TAT_TITLE ,
1281 tab_text (t, col, row + n ,
1282 TAB_RIGHT | TAT_TITLE ,
1287 tab_hline (t, TAL_1, col, col + 3, row + n );
1289 for (extremity = 0; extremity < n ; ++extremity )
1292 tab_float (t, col + 1, row + extremity,
1294 extremity + 1, 8, 0);
1298 tab_float (t, col + 1, row + extremity + n,
1300 extremity + 1, 8, 0);
1306 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1309 const struct weighted_value *wv = m->wvp[idx];
1310 struct case_node *cn = wv->case_nos;
1313 for (j = 0 ; j < wv->w ; ++j )
1315 if ( extremity + j >= n )
1318 tab_float (t, col + 3, row + extremity + j + n,
1322 tab_float (t, col + 2, row + extremity + j + n,
1331 extremity += wv->w ;
1336 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1339 const struct weighted_value *wv = m->wvp[idx];
1340 struct case_node *cn = wv->case_nos;
1342 for (j = 0 ; j < wv->w ; ++j )
1344 if ( extremity + j >= n )
1347 tab_float (t, col + 3, row + extremity + j,
1351 tab_float (t, col + 2, row + extremity + j,
1360 extremity += wv->w ;
1365 /* Show the descriptives table */
1367 show_descriptives (const struct variable **dependent_var,
1369 struct factor *fctr)
1372 int heading_columns ;
1374 const int n_stat_rows = 13;
1376 const int heading_rows = 1;
1378 struct tab_table *tbl;
1385 heading_columns = 4;
1386 n_factors = hsh_count (fctr->fstats);
1388 n_rows = n_dep_var * n_stat_rows * n_factors;
1390 if ( fctr->indep_var[1] )
1391 heading_columns = 5;
1395 heading_columns = 3;
1396 n_rows = n_dep_var * n_stat_rows;
1399 n_rows += heading_rows;
1401 n_cols = heading_columns + 2;
1404 tbl = tab_create (n_cols, n_rows, 0);
1406 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1408 tab_dim (tbl, tab_natural_dimensions);
1410 /* Outline the box and have no internal lines*/
1415 n_cols - 1, n_rows - 1);
1417 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1419 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1420 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1421 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1423 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _ ("Statistic"));
1424 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _ ("Std. Error"));
1426 tab_title (tbl, _ ("Descriptives"));
1429 for ( i = 0 ; i < n_dep_var ; ++i )
1431 const int row = heading_rows + i * n_stat_rows * n_factors ;
1434 tab_hline (tbl, TAL_1, 0, n_cols - 1, row );
1437 i * n_stat_rows * n_factors + heading_rows,
1438 TAB_LEFT | TAT_TITLE,
1439 var_to_string (dependent_var[i])
1445 const union value *prev = NULL;
1447 struct factor_statistics **fs = fctr->fs;
1450 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1451 var_to_string (fctr->indep_var[0]));
1454 if ( fctr->indep_var[1])
1455 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1456 var_to_string (fctr->indep_var[1]));
1460 const int row = heading_rows + n_stat_rows *
1461 ( ( i * n_factors ) + count );
1464 if ( !prev || 0 != compare_values (prev, (*fs)->id[0],
1465 var_get_width (fctr->indep_var[0])))
1469 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1473 TAB_LEFT | TAT_TITLE,
1474 var_get_value_name (fctr->indep_var[0],
1479 prev = (*fs)->id[0];
1481 if (fctr->indep_var[1] && count > 0 )
1482 tab_hline (tbl, TAL_1, 2, n_cols - 1, row);
1484 if ( fctr->indep_var[1])
1485 tab_text (tbl, 2, row,
1486 TAB_LEFT | TAT_TITLE,
1487 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
1490 populate_descriptives (tbl, heading_columns - 2,
1491 row, & (*fs)->m[i]);
1502 populate_descriptives (tbl, heading_columns - 2,
1503 i * n_stat_rows * n_factors + heading_rows,
1513 /* Fill in the descriptives data */
1515 populate_descriptives (struct tab_table *tbl, int col, int row,
1516 const struct metrics *m)
1518 const double t = gsl_cdf_tdist_Qinv ((1 - cmd.n_cinterval[0] / 100.0)/2.0,
1523 TAB_LEFT | TAT_TITLE,
1526 tab_float (tbl, col + 2,
1532 tab_float (tbl, col + 3,
1541 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1542 _ ("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1545 tab_text (tbl, col + 1,
1547 TAB_LEFT | TAT_TITLE,
1550 tab_float (tbl, col + 2,
1553 m->mean - t * m->se_mean,
1556 tab_text (tbl, col + 1,
1558 TAB_LEFT | TAT_TITLE,
1562 tab_float (tbl, col + 2,
1565 m->mean + t * m->se_mean,
1570 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1571 _ ("5%% Trimmed Mean"));
1573 tab_float (tbl, col + 2,
1581 TAB_LEFT | TAT_TITLE,
1585 struct percentile *p;
1588 p = hsh_find (m->ptile_hash, &d);
1593 tab_float (tbl, col + 2,
1603 TAB_LEFT | TAT_TITLE,
1606 tab_float (tbl, col + 2,
1615 TAB_LEFT | TAT_TITLE,
1616 _ ("Std. Deviation"));
1619 tab_float (tbl, col + 2,
1628 TAB_LEFT | TAT_TITLE,
1631 tab_float (tbl, col + 2,
1639 TAB_LEFT | TAT_TITLE,
1642 tab_float (tbl, col + 2,
1651 TAB_LEFT | TAT_TITLE,
1655 tab_float (tbl, col + 2,
1663 TAB_LEFT | TAT_TITLE,
1664 _ ("Interquartile Range"));
1667 struct percentile *p1;
1668 struct percentile *p2;
1671 p1 = hsh_find (m->ptile_hash, &d);
1674 p2 = hsh_find (m->ptile_hash, &d);
1679 tab_float (tbl, col + 2,
1690 TAB_LEFT | TAT_TITLE,
1694 tab_float (tbl, col + 2,
1700 /* stderr of skewness */
1701 tab_float (tbl, col + 3,
1710 TAB_LEFT | TAT_TITLE,
1714 tab_float (tbl, col + 2,
1720 /* stderr of kurtosis */
1721 tab_float (tbl, col + 3,
1733 box_plot_variables (const struct factor *fctr,
1734 const struct variable **vars, int n_vars,
1735 const struct variable *id)
1739 struct factor_statistics **fs ;
1743 box_plot_group (fctr, vars, n_vars, id);
1747 for ( fs = fctr->fs ; *fs ; ++fs )
1749 double y_min = DBL_MAX;
1750 double y_max = -DBL_MAX;
1751 struct chart *ch = chart_create ();
1752 const char *s = factor_to_string (fctr, *fs, 0 );
1754 chart_write_title (ch, s);
1756 for ( i = 0 ; i < n_vars ; ++i )
1758 y_max = MAX (y_max, (*fs)->m[i].max);
1759 y_min = MIN (y_min, (*fs)->m[i].min);
1762 boxplot_draw_yscale (ch, y_max, y_min);
1764 for ( i = 0 ; i < n_vars ; ++i )
1767 const double box_width = (ch->data_right - ch->data_left)
1770 const double box_centre = ( i * 2 + 1) * box_width
1773 boxplot_draw_boxplot (ch,
1774 box_centre, box_width,
1776 var_to_string (vars[i]));
1788 /* Do a box plot, grouping all factors into one plot ;
1789 each dependent variable has its own plot.
1792 box_plot_group (const struct factor *fctr,
1793 const struct variable **vars,
1795 const struct variable *id UNUSED)
1800 for ( i = 0 ; i < n_vars ; ++i )
1802 struct factor_statistics **fs ;
1805 ch = chart_create ();
1807 boxplot_draw_yscale (ch, totals[i].max, totals[i].min);
1813 for ( fs = fctr->fs ; *fs ; ++fs )
1816 chart_write_title (ch, _ ("Boxplot of %s vs. %s"),
1817 var_to_string (vars[i]), var_to_string (fctr->indep_var[0]) );
1819 for ( fs = fctr->fs ; *fs ; ++fs )
1822 const char *s = factor_to_string_concise (fctr, *fs);
1824 const double box_width = (ch->data_right - ch->data_left)
1825 / (n_factors * 2.0 ) ;
1827 const double box_centre = ( f++ * 2 + 1) * box_width
1830 boxplot_draw_boxplot (ch,
1831 box_centre, box_width,
1838 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1839 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1841 chart_write_title (ch, _ ("Boxplot"));
1843 boxplot_draw_boxplot (ch,
1844 box_centre, box_width,
1846 var_to_string (vars[i]) );
1855 /* Plot the normal and detrended normal plots for m
1856 Label the plots with factorname */
1858 np_plot (const struct metrics *m, const char *factorname)
1861 double yfirst=0, ylast=0;
1864 struct chart *np_chart;
1866 /* Detrended Normal Plot */
1867 struct chart *dnp_chart;
1869 /* The slope and intercept of the ideal normal probability line */
1870 const double slope = 1.0 / m->stddev;
1871 const double intercept = - m->mean / m->stddev;
1873 /* Cowardly refuse to plot an empty data set */
1874 if ( m->n_data == 0 )
1877 np_chart = chart_create ();
1878 dnp_chart = chart_create ();
1880 if ( !np_chart || ! dnp_chart )
1883 chart_write_title (np_chart, _ ("Normal Q-Q Plot of %s"), factorname);
1884 chart_write_xlabel (np_chart, _ ("Observed Value"));
1885 chart_write_ylabel (np_chart, _ ("Expected Normal"));
1888 chart_write_title (dnp_chart, _ ("Detrended Normal Q-Q Plot of %s"),
1890 chart_write_xlabel (dnp_chart, _ ("Observed Value"));
1891 chart_write_ylabel (dnp_chart, _ ("Dev from Normal"));
1893 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1894 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1898 /* Need to make sure that both the scatter plot and the ideal fit into the
1900 double x_lower = MIN (m->min, (yfirst - intercept) / slope) ;
1901 double x_upper = MAX (m->max, (ylast - intercept) / slope) ;
1902 double slack = (x_upper - x_lower) * 0.05 ;
1904 chart_write_xscale (np_chart, x_lower - slack, x_upper + slack, 5);
1906 chart_write_xscale (dnp_chart, m->min, m->max, 5);
1910 chart_write_yscale (np_chart, yfirst, ylast, 5);
1913 /* We have to cache the detrended data, beacause we need to
1914 find its limits before we can plot it */
1915 double *d_data = xnmalloc (m->n_data, sizeof *d_data);
1916 double d_max = -DBL_MAX;
1917 double d_min = DBL_MAX;
1918 for ( i = 0 ; i < m->n_data; ++i )
1920 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1922 chart_datum (np_chart, 0, m->wvp[i]->v.f, ns);
1924 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1926 if ( d_data[i] < d_min ) d_min = d_data[i];
1927 if ( d_data[i] > d_max ) d_max = d_data[i];
1929 chart_write_yscale (dnp_chart, d_min, d_max, 5);
1931 for ( i = 0 ; i < m->n_data; ++i )
1932 chart_datum (dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1937 chart_line (np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1938 chart_line (dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1940 chart_submit (np_chart);
1941 chart_submit (dnp_chart);
1947 /* Show the percentiles */
1949 show_percentiles (const struct variable **dependent_var,
1951 struct factor *fctr)
1953 struct tab_table *tbl;
1959 struct hsh_table *ptiles ;
1961 int n_heading_columns;
1962 const int n_heading_rows = 2;
1963 const int n_stat_rows = 2;
1969 struct factor_statistics **fs = fctr->fs ;
1970 n_heading_columns = 3;
1971 n_factors = hsh_count (fctr->fstats);
1973 ptiles = (*fs)->m[0].ptile_hash;
1975 if ( fctr->indep_var[1] )
1976 n_heading_columns = 4;
1981 n_heading_columns = 2;
1983 ptiles = totals[0].ptile_hash;
1986 n_ptiles = hsh_count (ptiles);
1988 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1990 n_cols = n_heading_columns + n_ptiles ;
1992 tbl = tab_create (n_cols, n_rows, 0);
1994 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1996 tab_dim (tbl, tab_natural_dimensions);
1998 /* Outline the box and have no internal lines*/
2003 n_cols - 1, n_rows - 1);
2005 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
2007 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
2010 tab_title (tbl, _ ("Percentiles"));
2013 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
2020 n_heading_columns - 1, n_rows - 1);
2026 n_heading_columns, n_heading_rows - 1,
2027 n_cols - 1, n_rows - 1);
2029 tab_joint_text (tbl, n_heading_columns + 1, 0,
2031 TAB_CENTER | TAT_TITLE ,
2036 /* Put in the percentile break points as headings */
2038 struct percentile **p = (struct percentile **) hsh_sort (ptiles);
2043 tab_float (tbl, n_heading_columns + i++ , 1,
2052 for ( i = 0 ; i < n_dep_var ; ++i )
2054 const int n_stat_rows = 2;
2055 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
2058 tab_hline (tbl, TAL_1, 0, n_cols - 1, row );
2061 i * n_stat_rows * n_factors + n_heading_rows,
2062 TAB_LEFT | TAT_TITLE,
2063 var_to_string (dependent_var[i])
2068 const union value *prev = NULL ;
2069 struct factor_statistics **fs = fctr->fs;
2072 tab_text (tbl, 1, n_heading_rows - 1,
2073 TAB_CENTER | TAT_TITLE,
2074 var_to_string (fctr->indep_var[0]));
2077 if ( fctr->indep_var[1])
2078 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2079 var_to_string (fctr->indep_var[1]));
2083 const int row = n_heading_rows + n_stat_rows *
2084 ( ( i * n_factors ) + count );
2087 if ( !prev || 0 != compare_values (prev, (*fs)->id[0],
2088 var_get_width (fctr->indep_var[0])))
2092 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2096 TAB_LEFT | TAT_TITLE,
2097 var_get_value_name (fctr->indep_var[0],
2104 prev = (*fs)->id[0];
2106 if (fctr->indep_var[1] && count > 0 )
2107 tab_hline (tbl, TAL_1, 2, n_cols - 1, row);
2109 if ( fctr->indep_var[1])
2110 tab_text (tbl, 2, row,
2111 TAB_LEFT | TAT_TITLE,
2112 var_get_value_name (fctr->indep_var[1], (*fs)->id[1])
2116 populate_percentiles (tbl, n_heading_columns - 1,
2117 row, & (*fs)->m[i]);
2128 populate_percentiles (tbl, n_heading_columns - 1,
2129 i * n_stat_rows * n_factors + n_heading_rows,
2146 populate_percentiles (struct tab_table *tbl, int col, int row,
2147 const struct metrics *m)
2151 struct percentile **p = (struct percentile **) hsh_sort (m->ptile_hash);
2155 TAB_LEFT | TAT_TITLE,
2156 _ ("Tukey\'s Hinges")
2161 TAB_LEFT | TAT_TITLE,
2162 ptile_alg_desc[m->ptile_alg]
2169 tab_float (tbl, col + i + 1 , row,
2172 if ( (*p)->p == 25 )
2173 tab_float (tbl, col + i + 1 , row + 1,
2177 if ( (*p)->p == 50 )
2178 tab_float (tbl, col + i + 1 , row + 1,
2182 if ( (*p)->p == 75 )
2183 tab_float (tbl, col + i + 1 , row + 1,
2198 factor_to_string (const struct factor *fctr,
2199 const struct factor_statistics *fs,
2200 const struct variable *var)
2203 static char buf1[100];
2209 sprintf (buf1, "%s (",var_to_string (var) );
2212 snprintf (buf2, 100, "%s = %s",
2213 var_to_string (fctr->indep_var[0]),
2214 var_get_value_name (fctr->indep_var[0], fs->id[0]));
2216 strcat (buf1, buf2);
2218 if ( fctr->indep_var[1] )
2220 sprintf (buf2, "; %s = %s)",
2221 var_to_string (fctr->indep_var[1]),
2222 var_get_value_name (fctr->indep_var[1], fs->id[1]));
2223 strcat (buf1, buf2);
2237 factor_to_string_concise (const struct factor *fctr,
2238 struct factor_statistics *fs)
2242 static char buf[100];
2246 snprintf (buf, 100, "%s",
2247 var_get_value_name (fctr->indep_var[0], fs->id[0]));
2249 if ( fctr->indep_var[1] )
2251 sprintf (buf2, ",%s)", var_get_value_name (fctr->indep_var[1],