1 /* PSPP - EXAMINE data for normality . -*-c-*-
3 Copyright (C) 2004 Free Software Foundation, Inc.
4 Author: John Darrington 2004
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <gsl/gsl_cdf.h>
24 #include <libpspp/message.h>
29 #include <data/case.h>
30 #include <data/casefile.h>
31 #include <data/dictionary.h>
32 #include <data/procedure.h>
33 #include <data/value-labels.h>
34 #include <data/variable.h>
35 #include <language/command.h>
36 #include <language/dictionary/split-file.h>
37 #include <language/lexer/lexer.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/compiler.h>
40 #include <libpspp/hash.h>
41 #include <libpspp/magic.h>
42 #include <libpspp/message.h>
43 #include <libpspp/misc.h>
44 #include <libpspp/str.h>
45 #include <math/factor-stats.h>
46 #include <math/moments.h>
47 #include <math/percentiles.h>
48 #include <output/charts/box-whisker.h>
49 #include <output/charts/cartesian.h>
50 #include <output/manager.h>
51 #include <output/table.h>
54 #define _(msgid) gettext (msgid)
55 #define N_(msgid) msgid
58 #include <output/chart.h>
59 #include <output/charts/plot-hist.h>
60 #include <output/charts/plot-chart.h>
67 +missing=miss:pairwise/!listwise,
69 incl:include/!exclude;
70 +compare=cmp:variables/!groups;
73 +plot[plt_]=stemleaf,boxplot,npplot,:spreadlevel(*d:n),histogram,all,none;
75 +statistics[st_]=descriptives,:extreme(*d:n),all,none.
84 static struct cmd_examine cmd;
86 static struct variable **dependent_vars;
88 static size_t n_dependent_vars;
93 /* The independent variable */
94 struct variable *indep_var[2];
97 /* Hash table of factor stats indexed by 2 values */
98 struct hsh_table *fstats;
100 /* The hash table after it has been crunched */
101 struct factor_statistics **fs;
107 /* Linked list of factors */
108 static struct factor *factors=0;
110 static struct metrics *totals=0;
112 /* Parse the clause specifying the factors */
113 static int examine_parse_independent_vars(struct cmd_examine *cmd);
117 /* Output functions */
118 static void show_summary(struct variable **dependent_var, int n_dep_var,
119 const struct factor *f);
121 static void show_extremes(struct variable **dependent_var,
123 const struct factor *factor,
126 static void show_descriptives(struct variable **dependent_var,
128 struct factor *factor);
130 static void show_percentiles(struct variable **dependent_var,
132 struct factor *factor);
137 void np_plot(const struct metrics *m, const char *factorname);
140 void box_plot_group(const struct factor *fctr,
141 const struct variable **vars, int n_vars,
142 const struct variable *id
146 void box_plot_variables(const struct factor *fctr,
147 const struct variable **vars, int n_vars,
148 const struct variable *id
153 /* Per Split function */
154 static bool run_examine(const struct ccase *,
155 const struct casefile *cf, void *cmd_);
157 static void output_examine(void);
160 void factor_calc(struct ccase *c, int case_no,
161 double weight, int case_missing);
164 /* Represent a factor as a string, so it can be
165 printed in a human readable fashion */
166 const char * factor_to_string(const struct factor *fctr,
167 struct factor_statistics *fs,
168 const struct variable *var);
171 /* Represent a factor as a string, so it can be
172 printed in a human readable fashion,
173 but sacrificing some readablility for the sake of brevity */
174 const char *factor_to_string_concise(const struct factor *fctr,
175 struct factor_statistics *fs);
180 /* Function to use for testing for missing values */
181 static is_missing_func *value_is_missing;
186 static subc_list_double percentile_list;
188 static enum pc_alg percentile_algorithm;
190 static short sbc_percentile;
198 subc_list_double_create(&percentile_list);
199 percentile_algorithm = PC_HAVERAGE;
201 if ( !parse_examine(&cmd, NULL) )
204 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
205 if (cmd.incl == XMN_INCLUDE )
206 value_is_missing = mv_is_value_system_missing;
208 value_is_missing = mv_is_value_missing;
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 ok = multipass_procedure_with_splits (run_examine, &cmd);
232 if ( dependent_vars )
233 free (dependent_vars);
236 struct factor *f = factors ;
239 struct factor *ff = f;
243 hsh_destroy ( ff->fstats ) ;
249 subc_list_double_destroy(&percentile_list);
251 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
256 /* Show all the appropriate tables */
262 /* Show totals if appropriate */
263 if ( ! cmd.sbc_nototal || factors == 0 )
265 show_summary(dependent_vars, n_dependent_vars, 0);
267 if ( cmd.sbc_statistics )
269 if ( cmd.a_statistics[XMN_ST_EXTREME])
270 show_extremes(dependent_vars, n_dependent_vars, 0, cmd.st_n);
272 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
273 show_descriptives(dependent_vars, n_dependent_vars, 0);
276 if ( sbc_percentile )
277 show_percentiles(dependent_vars, n_dependent_vars, 0);
282 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
284 for ( v = 0 ; v < n_dependent_vars; ++v )
285 np_plot(&totals[v], var_to_string(dependent_vars[v]));
288 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
290 if ( cmd.cmp == XMN_GROUPS )
292 box_plot_group (0, (const struct variable **) dependent_vars,
293 n_dependent_vars, cmd.v_id);
296 box_plot_variables (0,
297 (const struct variable **) dependent_vars,
298 n_dependent_vars, cmd.v_id);
301 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
303 for ( v = 0 ; v < n_dependent_vars; ++v )
305 struct normal_curve normal;
307 normal.N = totals[v].n;
308 normal.mean = totals[v].mean;
309 normal.stddev = totals[v].stddev;
311 histogram_plot(totals[v].histogram,
312 var_to_string(dependent_vars[v]),
322 /* Show grouped statistics as appropriate */
326 show_summary(dependent_vars, n_dependent_vars, fctr);
328 if ( cmd.sbc_statistics )
330 if ( cmd.a_statistics[XMN_ST_EXTREME])
331 show_extremes(dependent_vars, n_dependent_vars, fctr, cmd.st_n);
333 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
334 show_descriptives(dependent_vars, n_dependent_vars, fctr);
337 if ( sbc_percentile )
338 show_percentiles(dependent_vars, n_dependent_vars, fctr);
345 struct factor_statistics **fs = fctr->fs ;
347 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
349 if ( cmd.cmp == XMN_VARIABLES )
350 box_plot_variables (fctr,
351 (const struct variable **) dependent_vars,
352 n_dependent_vars, cmd.v_id);
354 box_plot_group (fctr,
355 (const struct variable **) dependent_vars,
356 n_dependent_vars, cmd.v_id);
359 for ( v = 0 ; v < n_dependent_vars; ++v )
362 for ( fs = fctr->fs ; *fs ; ++fs )
364 const char *s = factor_to_string(fctr, *fs, dependent_vars[v]);
366 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
367 np_plot(&(*fs)->m[v], s);
369 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
371 struct normal_curve normal;
373 normal.N = (*fs)->m[v].n;
374 normal.mean = (*fs)->m[v].mean;
375 normal.stddev = (*fs)->m[v].stddev;
377 histogram_plot((*fs)->m[v].histogram,
381 } /* for ( fs .... */
383 } /* for ( v = 0 ..... */
393 /* Create a hash table of percentiles and their values from the list of
395 static struct hsh_table *
396 list_to_ptile_hash(const subc_list_double *l)
400 struct hsh_table *h ;
402 h = hsh_create(subc_list_double_count(l),
403 (hsh_compare_func *) ptile_compare,
404 (hsh_hash_func *) ptile_hash,
405 (hsh_free_func *) free,
409 for ( i = 0 ; i < subc_list_double_count(l) ; ++i )
411 struct percentile *p = xmalloc (sizeof *p);
413 p->p = subc_list_double_at(l,i);
424 /* Parse the PERCENTILES subcommand */
426 xmn_custom_percentiles(struct cmd_examine *p UNUSED, void *aux UNUSED)
434 while ( lex_is_number() )
436 subc_list_double_push(&percentile_list,lex_number());
446 if ( lex_match_id("HAVERAGE"))
447 percentile_algorithm = PC_HAVERAGE;
449 else if ( lex_match_id("WAVERAGE"))
450 percentile_algorithm = PC_WAVERAGE;
452 else if ( lex_match_id("ROUND"))
453 percentile_algorithm = PC_ROUND;
455 else if ( lex_match_id("EMPIRICAL"))
456 percentile_algorithm = PC_EMPIRICAL;
458 else if ( lex_match_id("AEMPIRICAL"))
459 percentile_algorithm = PC_AEMPIRICAL;
461 else if ( lex_match_id("NONE"))
462 percentile_algorithm = PC_NONE;
465 if ( 0 == subc_list_double_count(&percentile_list))
467 subc_list_double_push(&percentile_list, 5);
468 subc_list_double_push(&percentile_list, 10);
469 subc_list_double_push(&percentile_list, 25);
470 subc_list_double_push(&percentile_list, 50);
471 subc_list_double_push(&percentile_list, 75);
472 subc_list_double_push(&percentile_list, 90);
473 subc_list_double_push(&percentile_list, 95);
479 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
481 xmn_custom_total(struct cmd_examine *p, void *aux UNUSED)
483 if ( p->sbc_nototal )
485 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
493 xmn_custom_nototal(struct cmd_examine *p, void *aux UNUSED)
497 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
506 /* Parser for the variables sub command
507 Returns 1 on success */
509 xmn_custom_variables(struct cmd_examine *cmd, void *aux UNUSED)
513 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
519 if (!parse_variables (default_dict, &dependent_vars, &n_dependent_vars,
520 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
522 free (dependent_vars);
526 assert(n_dependent_vars);
528 totals = xnmalloc (n_dependent_vars, sizeof *totals);
530 if ( lex_match(T_BY))
533 success = examine_parse_independent_vars(cmd);
534 if ( success != 1 ) {
535 free (dependent_vars);
546 /* Parse the clause specifying the factors */
548 examine_parse_independent_vars(struct cmd_examine *cmd)
551 struct factor *sf = xmalloc (sizeof *sf);
553 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
561 sf->indep_var[0] = parse_variable();
562 sf->indep_var[1] = 0;
569 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
576 sf->indep_var[1] = parse_variable();
581 sf->fstats = hsh_create(4,
582 (hsh_compare_func *) factor_statistics_compare,
583 (hsh_hash_func *) factor_statistics_hash,
584 (hsh_free_func *) factor_statistics_free,
592 if ( token == '.' || token == '/' )
595 success = examine_parse_independent_vars(cmd);
606 void populate_percentiles(struct tab_table *tbl, int col, int row,
607 const struct metrics *m);
609 void populate_descriptives(struct tab_table *t, int col, int row,
610 const struct metrics *fs);
612 void populate_extremes(struct tab_table *t, int col, int row, int n,
613 const struct metrics *m);
615 void populate_summary(struct tab_table *t, int col, int row,
616 const struct metrics *m);
621 static int bad_weight_warn = 1;
624 /* Perform calculations for the sub factors */
626 factor_calc(struct ccase *c, int case_no, double weight, int case_missing)
629 struct factor *fctr = factors;
633 struct factor_statistics **foo ;
634 union value indep_vals[2] ;
636 indep_vals[0] = * case_data(c, fctr->indep_var[0]->fv);
638 if ( fctr->indep_var[1] )
639 indep_vals[1] = * case_data(c, fctr->indep_var[1]->fv);
641 indep_vals[1].f = SYSMIS;
643 assert(fctr->fstats);
645 foo = ( struct factor_statistics ** )
646 hsh_probe(fctr->fstats, (void *) &indep_vals);
651 *foo = create_factor_statistics(n_dependent_vars,
655 for ( v = 0 ; v < n_dependent_vars ; ++v )
657 metrics_precalc( &(*foo)->m[v] );
662 for ( v = 0 ; v < n_dependent_vars ; ++v )
664 const struct variable *var = dependent_vars[v];
665 const union value *val = case_data (c, var->fv);
667 if ( value_is_missing (&var->miss, val) || case_missing )
670 metrics_calc( &(*foo)->m[v], val, weight, case_no);
681 run_examine(const struct ccase *first, const struct casefile *cf, void *cmd_ )
683 struct casereader *r;
687 const struct cmd_examine *cmd = (struct cmd_examine *) cmd_;
691 output_split_file_values (first);
693 /* Make sure we haven't got rubbish left over from a
698 struct factor *next = fctr->next;
700 hsh_clear(fctr->fstats);
709 for ( v = 0 ; v < n_dependent_vars ; ++v )
710 metrics_precalc(&totals[v]);
712 for(r = casefile_get_reader (cf);
713 casereader_read (r, &c) ;
717 const int case_no = casereader_cnum(r);
719 const double weight =
720 dict_get_case_weight(default_dict, &c, &bad_weight_warn);
722 if ( cmd->miss == XMN_LISTWISE )
724 for ( v = 0 ; v < n_dependent_vars ; ++v )
726 const struct variable *var = dependent_vars[v];
727 const union value *val = case_data (&c, var->fv);
729 if ( value_is_missing(&var->miss, val))
735 for ( v = 0 ; v < n_dependent_vars ; ++v )
737 const struct variable *var = dependent_vars[v];
738 const union value *val = case_data (&c, var->fv);
740 if ( value_is_missing(&var->miss, val) || case_missing )
743 metrics_calc(&totals[v], val, weight, case_no);
747 factor_calc(&c, case_no, weight, case_missing);
752 for ( v = 0 ; v < n_dependent_vars ; ++v)
757 struct hsh_iterator hi;
758 struct factor_statistics *fs;
760 for ( fs = hsh_first(fctr->fstats, &hi);
762 fs = hsh_next(fctr->fstats, &hi))
765 fs->m[v].ptile_hash = list_to_ptile_hash(&percentile_list);
766 fs->m[v].ptile_alg = percentile_algorithm;
767 metrics_postcalc(&fs->m[v]);
773 totals[v].ptile_hash = list_to_ptile_hash(&percentile_list);
774 totals[v].ptile_alg = percentile_algorithm;
775 metrics_postcalc(&totals[v]);
779 /* Make sure that the combination of factors are complete */
784 struct hsh_iterator hi;
785 struct hsh_iterator hi0;
786 struct hsh_iterator hi1;
787 struct factor_statistics *fs;
789 struct hsh_table *idh0=0;
790 struct hsh_table *idh1=0;
794 idh0 = hsh_create(4, (hsh_compare_func *) compare_values,
795 (hsh_hash_func *) hash_value,
798 idh1 = hsh_create(4, (hsh_compare_func *) compare_values,
799 (hsh_hash_func *) hash_value,
803 for ( fs = hsh_first(fctr->fstats, &hi);
805 fs = hsh_next(fctr->fstats, &hi))
807 hsh_insert(idh0,(void *) &fs->id[0]);
808 hsh_insert(idh1,(void *) &fs->id[1]);
811 /* Ensure that the factors combination is complete */
812 for ( val0 = hsh_first(idh0, &hi0);
814 val0 = hsh_next(idh0, &hi0))
816 for ( val1 = hsh_first(idh1, &hi1);
818 val1 = hsh_next(idh1, &hi1))
820 struct factor_statistics **ffs;
825 ffs = (struct factor_statistics **)
826 hsh_probe(fctr->fstats, (void *) &key );
830 (*ffs) = create_factor_statistics (n_dependent_vars,
832 for ( i = 0 ; i < n_dependent_vars ; ++i )
833 metrics_precalc( &(*ffs)->m[i]);
841 fctr->fs = (struct factor_statistics **) hsh_sort_copy(fctr->fstats);
852 for ( i = 0 ; i < n_dependent_vars ; ++i )
854 metrics_destroy(&totals[i]);
863 show_summary(struct variable **dependent_var, int n_dep_var,
864 const struct factor *fctr)
866 static const char *subtitle[]=
874 int heading_columns ;
876 const int heading_rows = 3;
877 struct tab_table *tbl;
885 n_factors = hsh_count(fctr->fstats);
886 n_rows = n_dep_var * n_factors ;
888 if ( fctr->indep_var[1] )
897 n_rows += heading_rows;
899 n_cols = heading_columns + 6;
901 tbl = tab_create (n_cols,n_rows,0);
902 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
904 tab_dim (tbl, tab_natural_dimensions);
906 /* Outline the box */
911 n_cols - 1, n_rows - 1);
913 /* Vertical lines for the data only */
918 n_cols - 1, n_rows - 1);
921 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
922 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
923 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
925 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
928 tab_title (tbl, _("Case Processing Summary"));
931 tab_joint_text(tbl, heading_columns, 0,
933 TAB_CENTER | TAT_TITLE,
936 /* Remove lines ... */
943 for ( i = 0 ; i < 3 ; ++i )
945 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
948 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
951 tab_joint_text(tbl, heading_columns + i*2 , 1,
952 heading_columns + i*2 + 1, 1,
953 TAB_CENTER | TAT_TITLE,
956 tab_box (tbl, -1, -1,
958 heading_columns + i*2, 1,
959 heading_columns + i*2 + 1, 1);
964 /* Titles for the independent variables */
967 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
968 var_to_string(fctr->indep_var[0]));
970 if ( fctr->indep_var[1] )
972 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
973 var_to_string(fctr->indep_var[1]));
979 for ( i = 0 ; i < n_dep_var ; ++i )
983 n_factors = hsh_count(fctr->fstats);
987 tab_hline(tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
990 0, i * n_factors + heading_rows,
991 TAB_LEFT | TAT_TITLE,
992 var_to_string(dependent_var[i])
997 populate_summary(tbl, heading_columns,
998 (i * n_factors) + heading_rows,
1004 struct factor_statistics **fs = fctr->fs;
1009 static union value prev;
1011 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1012 fctr->indep_var[0]->width))
1016 (i * n_factors ) + count +
1018 TAB_LEFT | TAT_TITLE,
1019 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1022 if (fctr->indep_var[1] && count > 0 )
1023 tab_hline(tbl, TAL_1, 1, n_cols - 1,
1024 (i * n_factors ) + count + heading_rows);
1028 prev = (*fs)->id[0];
1031 if ( fctr->indep_var[1])
1034 (i * n_factors ) + count +
1036 TAB_LEFT | TAT_TITLE,
1037 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1040 populate_summary(tbl, heading_columns,
1041 (i * n_factors) + count
1056 populate_summary(struct tab_table *t, int col, int row,
1057 const struct metrics *m)
1060 const double total = m->n + m->n_missing ;
1062 tab_float(t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1063 tab_float(t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1064 tab_float(t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1068 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1069 100.0 * m->n / total );
1071 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1072 100.0 * m->n_missing / total );
1074 /* This seems a bit pointless !!! */
1075 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1076 100.0 * total / total );
1087 show_extremes(struct variable **dependent_var, int n_dep_var,
1088 const struct factor *fctr, int n_extremities)
1091 int heading_columns ;
1093 const int heading_rows = 1;
1094 struct tab_table *tbl;
1101 heading_columns = 2;
1102 n_factors = hsh_count(fctr->fstats);
1104 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1106 if ( fctr->indep_var[1] )
1107 heading_columns = 3;
1111 heading_columns = 1;
1112 n_rows = n_dep_var * 2 * n_extremities;
1115 n_rows += heading_rows;
1117 heading_columns += 2;
1118 n_cols = heading_columns + 2;
1120 tbl = tab_create (n_cols,n_rows,0);
1121 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1123 tab_dim (tbl, tab_natural_dimensions);
1125 /* Outline the box, No internal lines*/
1130 n_cols - 1, n_rows - 1);
1132 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1134 tab_title (tbl, _("Extreme Values"));
1136 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1137 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1141 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1142 var_to_string(fctr->indep_var[0]));
1144 if ( fctr->indep_var[1] )
1145 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1146 var_to_string(fctr->indep_var[1]));
1149 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Value"));
1150 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Case Number"));
1152 for ( i = 0 ; i < n_dep_var ; ++i )
1156 tab_hline(tbl, TAL_1, 0, n_cols -1 ,
1157 i * 2 * n_extremities * n_factors + heading_rows);
1160 i * 2 * n_extremities * n_factors + heading_rows,
1161 TAB_LEFT | TAT_TITLE,
1162 var_to_string(dependent_var[i])
1167 populate_extremes(tbl, heading_columns - 2,
1168 i * 2 * n_extremities * n_factors + heading_rows,
1169 n_extremities, &totals[i]);
1173 struct factor_statistics **fs = fctr->fs;
1178 static union value prev ;
1180 const int row = heading_rows + ( 2 * n_extremities ) *
1181 ( ( i * n_factors ) + count );
1184 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1185 fctr->indep_var[0]->width))
1189 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1193 TAB_LEFT | TAT_TITLE,
1194 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1198 prev = (*fs)->id[0];
1200 if (fctr->indep_var[1] && count > 0 )
1201 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1203 if ( fctr->indep_var[1])
1204 tab_text (tbl, 2, row,
1205 TAB_LEFT | TAT_TITLE,
1206 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1209 populate_extremes(tbl, heading_columns - 2,
1224 /* Fill in the extremities table */
1226 populate_extremes(struct tab_table *t,
1227 int col, int row, int n, const struct metrics *m)
1233 tab_text(t, col, row,
1234 TAB_RIGHT | TAT_TITLE ,
1238 tab_text(t, col, row + n ,
1239 TAB_RIGHT | TAT_TITLE ,
1244 tab_hline(t, TAL_1, col, col + 3, row + n );
1246 for (extremity = 0; extremity < n ; ++extremity )
1249 tab_float(t, col + 1, row + extremity,
1251 extremity + 1, 8, 0);
1255 tab_float(t, col + 1, row + extremity + n,
1257 extremity + 1, 8, 0);
1263 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1266 const struct weighted_value *wv = m->wvp[idx];
1267 struct case_node *cn = wv->case_nos;
1270 for (j = 0 ; j < wv->w ; ++j )
1272 if ( extremity + j >= n )
1275 tab_float(t, col + 3, row + extremity + j + n,
1279 tab_float(t, col + 2, row + extremity + j + n,
1288 extremity += wv->w ;
1293 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1296 const struct weighted_value *wv = m->wvp[idx];
1297 struct case_node *cn = wv->case_nos;
1299 for (j = 0 ; j < wv->w ; ++j )
1301 if ( extremity + j >= n )
1304 tab_float(t, col + 3, row + extremity + j,
1308 tab_float(t, col + 2, row + extremity + j,
1317 extremity += wv->w ;
1322 /* Show the descriptives table */
1324 show_descriptives(struct variable **dependent_var,
1326 struct factor *fctr)
1329 int heading_columns ;
1331 const int n_stat_rows = 13;
1333 const int heading_rows = 1;
1335 struct tab_table *tbl;
1342 heading_columns = 4;
1343 n_factors = hsh_count(fctr->fstats);
1345 n_rows = n_dep_var * n_stat_rows * n_factors;
1347 if ( fctr->indep_var[1] )
1348 heading_columns = 5;
1352 heading_columns = 3;
1353 n_rows = n_dep_var * n_stat_rows;
1356 n_rows += heading_rows;
1358 n_cols = heading_columns + 2;
1361 tbl = tab_create (n_cols, n_rows, 0);
1363 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1365 tab_dim (tbl, tab_natural_dimensions);
1367 /* Outline the box and have no internal lines*/
1372 n_cols - 1, n_rows - 1);
1374 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1376 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1377 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1378 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1380 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Statistic"));
1381 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1383 tab_title (tbl, _("Descriptives"));
1386 for ( i = 0 ; i < n_dep_var ; ++i )
1388 const int row = heading_rows + i * n_stat_rows * n_factors ;
1391 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1394 i * n_stat_rows * n_factors + heading_rows,
1395 TAB_LEFT | TAT_TITLE,
1396 var_to_string(dependent_var[i])
1402 struct factor_statistics **fs = fctr->fs;
1405 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1406 var_to_string(fctr->indep_var[0]));
1409 if ( fctr->indep_var[1])
1410 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1411 var_to_string(fctr->indep_var[1]));
1416 static union value prev ;
1418 const int row = heading_rows + n_stat_rows *
1419 ( ( i * n_factors ) + count );
1422 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1423 fctr->indep_var[0]->width))
1427 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1431 TAB_LEFT | TAT_TITLE,
1432 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1436 prev = (*fs)->id[0];
1438 if (fctr->indep_var[1] && count > 0 )
1439 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1441 if ( fctr->indep_var[1])
1442 tab_text (tbl, 2, row,
1443 TAB_LEFT | TAT_TITLE,
1444 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1447 populate_descriptives(tbl, heading_columns - 2,
1459 populate_descriptives(tbl, heading_columns - 2,
1460 i * n_stat_rows * n_factors + heading_rows,
1472 /* Fill in the descriptives data */
1474 populate_descriptives(struct tab_table *tbl, int col, int row,
1475 const struct metrics *m)
1478 const double t = gsl_cdf_tdist_Qinv(1 - cmd.n_cinterval[0]/100.0/2.0, \
1484 TAB_LEFT | TAT_TITLE,
1487 tab_float (tbl, col + 2,
1493 tab_float (tbl, col + 3,
1502 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1503 _("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1506 tab_text (tbl, col + 1,
1508 TAB_LEFT | TAT_TITLE,
1511 tab_float (tbl, col + 2,
1514 m->mean - t * m->se_mean,
1517 tab_text (tbl, col + 1,
1519 TAB_LEFT | TAT_TITLE,
1523 tab_float (tbl, col + 2,
1526 m->mean + t * m->se_mean,
1531 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1532 _("5%% Trimmed Mean"));
1534 tab_float (tbl, col + 2,
1542 TAB_LEFT | TAT_TITLE,
1546 struct percentile *p;
1549 p = hsh_find(m->ptile_hash, &d);
1554 tab_float (tbl, col + 2,
1564 TAB_LEFT | TAT_TITLE,
1567 tab_float (tbl, col + 2,
1576 TAB_LEFT | TAT_TITLE,
1577 _("Std. Deviation"));
1580 tab_float (tbl, col + 2,
1589 TAB_LEFT | TAT_TITLE,
1592 tab_float (tbl, col + 2,
1600 TAB_LEFT | TAT_TITLE,
1603 tab_float (tbl, col + 2,
1612 TAB_LEFT | TAT_TITLE,
1616 tab_float (tbl, col + 2,
1624 TAB_LEFT | TAT_TITLE,
1625 _("Interquartile Range"));
1628 struct percentile *p1;
1629 struct percentile *p2;
1632 p1 = hsh_find(m->ptile_hash, &d);
1635 p2 = hsh_find(m->ptile_hash, &d);
1640 tab_float (tbl, col + 2,
1651 TAB_LEFT | TAT_TITLE,
1655 tab_float (tbl, col + 2,
1661 /* stderr of skewness */
1662 tab_float (tbl, col + 3,
1671 TAB_LEFT | TAT_TITLE,
1675 tab_float (tbl, col + 2,
1681 /* stderr of kurtosis */
1682 tab_float (tbl, col + 3,
1694 box_plot_variables(const struct factor *fctr,
1695 const struct variable **vars, int n_vars,
1696 const struct variable *id)
1700 struct factor_statistics **fs ;
1704 box_plot_group(fctr, vars, n_vars, id);
1708 for ( fs = fctr->fs ; *fs ; ++fs )
1710 double y_min = DBL_MAX;
1711 double y_max = -DBL_MAX;
1712 struct chart *ch = chart_create();
1713 const char *s = factor_to_string(fctr, *fs, 0 );
1715 chart_write_title(ch, s);
1717 for ( i = 0 ; i < n_vars ; ++i )
1719 y_max = max(y_max, (*fs)->m[i].max);
1720 y_min = min(y_min, (*fs)->m[i].min);
1723 boxplot_draw_yscale(ch, y_max, y_min);
1725 for ( i = 0 ; i < n_vars ; ++i )
1728 const double box_width = (ch->data_right - ch->data_left)
1731 const double box_centre = ( i * 2 + 1) * box_width
1734 boxplot_draw_boxplot(ch,
1735 box_centre, box_width,
1737 var_to_string(vars[i]));
1749 /* Do a box plot, grouping all factors into one plot ;
1750 each dependent variable has its own plot.
1753 box_plot_group(const struct factor *fctr,
1754 const struct variable **vars,
1756 const struct variable *id UNUSED)
1761 for ( i = 0 ; i < n_vars ; ++i )
1763 struct factor_statistics **fs ;
1766 ch = chart_create();
1768 boxplot_draw_yscale(ch, totals[i].max, totals[i].min);
1774 for ( fs = fctr->fs ; *fs ; ++fs )
1777 chart_write_title(ch, _("Boxplot of %s vs. %s"),
1778 var_to_string(vars[i]), var_to_string(fctr->indep_var[0]) );
1780 for ( fs = fctr->fs ; *fs ; ++fs )
1783 const char *s = factor_to_string_concise(fctr, *fs);
1785 const double box_width = (ch->data_right - ch->data_left)
1786 / (n_factors * 2.0 ) ;
1788 const double box_centre = ( f++ * 2 + 1) * box_width
1791 boxplot_draw_boxplot(ch,
1792 box_centre, box_width,
1799 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1800 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1802 chart_write_title(ch, _("Boxplot"));
1804 boxplot_draw_boxplot(ch,
1805 box_centre, box_width,
1807 var_to_string(vars[i]) );
1816 /* Plot the normal and detrended normal plots for m
1817 Label the plots with factorname */
1819 np_plot(const struct metrics *m, const char *factorname)
1822 double yfirst=0, ylast=0;
1825 struct chart *np_chart;
1827 /* Detrended Normal Plot */
1828 struct chart *dnp_chart;
1830 /* The slope and intercept of the ideal normal probability line */
1831 const double slope = 1.0 / m->stddev;
1832 const double intercept = - m->mean / m->stddev;
1834 /* Cowardly refuse to plot an empty data set */
1835 if ( m->n_data == 0 )
1838 np_chart = chart_create();
1839 dnp_chart = chart_create();
1841 if ( !np_chart || ! dnp_chart )
1844 chart_write_title(np_chart, _("Normal Q-Q Plot of %s"), factorname);
1845 chart_write_xlabel(np_chart, _("Observed Value"));
1846 chart_write_ylabel(np_chart, _("Expected Normal"));
1849 chart_write_title(dnp_chart, _("Detrended Normal Q-Q Plot of %s"),
1851 chart_write_xlabel(dnp_chart, _("Observed Value"));
1852 chart_write_ylabel(dnp_chart, _("Dev from Normal"));
1854 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1855 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1859 /* Need to make sure that both the scatter plot and the ideal fit into the
1861 double x_lower = min(m->min, (yfirst - intercept) / slope) ;
1862 double x_upper = max(m->max, (ylast - intercept) / slope) ;
1863 double slack = (x_upper - x_lower) * 0.05 ;
1865 chart_write_xscale(np_chart, x_lower - slack, x_upper + slack, 5);
1867 chart_write_xscale(dnp_chart, m->min, m->max, 5);
1871 chart_write_yscale(np_chart, yfirst, ylast, 5);
1874 /* We have to cache the detrended data, beacause we need to
1875 find its limits before we can plot it */
1876 double *d_data = xnmalloc (m->n_data, sizeof *d_data);
1877 double d_max = -DBL_MAX;
1878 double d_min = DBL_MAX;
1879 for ( i = 0 ; i < m->n_data; ++i )
1881 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1883 chart_datum(np_chart, 0, m->wvp[i]->v.f, ns);
1885 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1887 if ( d_data[i] < d_min ) d_min = d_data[i];
1888 if ( d_data[i] > d_max ) d_max = d_data[i];
1890 chart_write_yscale(dnp_chart, d_min, d_max, 5);
1892 for ( i = 0 ; i < m->n_data; ++i )
1893 chart_datum(dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1898 chart_line(np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1899 chart_line(dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1901 chart_submit(np_chart);
1902 chart_submit(dnp_chart);
1908 /* Show the percentiles */
1910 show_percentiles(struct variable **dependent_var,
1912 struct factor *fctr)
1914 struct tab_table *tbl;
1920 struct hsh_table *ptiles ;
1922 int n_heading_columns;
1923 const int n_heading_rows = 2;
1924 const int n_stat_rows = 2;
1930 struct factor_statistics **fs = fctr->fs ;
1931 n_heading_columns = 3;
1932 n_factors = hsh_count(fctr->fstats);
1934 ptiles = (*fs)->m[0].ptile_hash;
1936 if ( fctr->indep_var[1] )
1937 n_heading_columns = 4;
1942 n_heading_columns = 2;
1944 ptiles = totals[0].ptile_hash;
1947 n_ptiles = hsh_count(ptiles);
1949 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1951 n_cols = n_heading_columns + n_ptiles ;
1953 tbl = tab_create (n_cols, n_rows, 0);
1955 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1957 tab_dim (tbl, tab_natural_dimensions);
1959 /* Outline the box and have no internal lines*/
1964 n_cols - 1, n_rows - 1);
1966 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
1968 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
1971 tab_title (tbl, _("Percentiles"));
1974 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
1981 n_heading_columns - 1, n_rows - 1);
1987 n_heading_columns, n_heading_rows - 1,
1988 n_cols - 1, n_rows - 1);
1990 tab_joint_text(tbl, n_heading_columns + 1, 0,
1992 TAB_CENTER | TAT_TITLE ,
1997 /* Put in the percentile break points as headings */
1999 struct percentile **p = (struct percentile **) hsh_sort(ptiles);
2004 tab_float(tbl, n_heading_columns + i++ , 1,
2013 for ( i = 0 ; i < n_dep_var ; ++i )
2015 const int n_stat_rows = 2;
2016 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
2019 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
2022 i * n_stat_rows * n_factors + n_heading_rows,
2023 TAB_LEFT | TAT_TITLE,
2024 var_to_string(dependent_var[i])
2029 struct factor_statistics **fs = fctr->fs;
2032 tab_text (tbl, 1, n_heading_rows - 1,
2033 TAB_CENTER | TAT_TITLE,
2034 var_to_string(fctr->indep_var[0]));
2037 if ( fctr->indep_var[1])
2038 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2039 var_to_string(fctr->indep_var[1]));
2044 static union value prev ;
2046 const int row = n_heading_rows + n_stat_rows *
2047 ( ( i * n_factors ) + count );
2050 if ( 0 != compare_values(&prev, &(*fs)->id[0],
2051 fctr->indep_var[0]->width))
2055 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2059 TAB_LEFT | TAT_TITLE,
2060 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
2066 prev = (*fs)->id[0];
2068 if (fctr->indep_var[1] && count > 0 )
2069 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
2071 if ( fctr->indep_var[1])
2072 tab_text (tbl, 2, row,
2073 TAB_LEFT | TAT_TITLE,
2074 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
2078 populate_percentiles(tbl, n_heading_columns - 1,
2090 populate_percentiles(tbl, n_heading_columns - 1,
2091 i * n_stat_rows * n_factors + n_heading_rows,
2108 populate_percentiles(struct tab_table *tbl, int col, int row,
2109 const struct metrics *m)
2113 struct percentile **p = (struct percentile **) hsh_sort(m->ptile_hash);
2117 TAB_LEFT | TAT_TITLE,
2118 _("Tukey\'s Hinges")
2123 TAB_LEFT | TAT_TITLE,
2124 ptile_alg_desc[m->ptile_alg]
2131 tab_float(tbl, col + i + 1 , row,
2134 if ( (*p)->p == 25 )
2135 tab_float(tbl, col + i + 1 , row + 1,
2139 if ( (*p)->p == 50 )
2140 tab_float(tbl, col + i + 1 , row + 1,
2144 if ( (*p)->p == 75 )
2145 tab_float(tbl, col + i + 1 , row + 1,
2160 factor_to_string(const struct factor *fctr,
2161 struct factor_statistics *fs,
2162 const struct variable *var)
2165 static char buf1[100];
2171 sprintf(buf1, "%s (",var_to_string(var) );
2174 snprintf(buf2, 100, "%s = %s",
2175 var_to_string(fctr->indep_var[0]),
2176 value_to_string(&fs->id[0],fctr->indep_var[0]));
2180 if ( fctr->indep_var[1] )
2182 sprintf(buf2, "; %s = %s)",
2183 var_to_string(fctr->indep_var[1]),
2184 value_to_string(&fs->id[1],
2185 fctr->indep_var[1]));
2200 factor_to_string_concise(const struct factor *fctr,
2201 struct factor_statistics *fs)
2205 static char buf[100];
2209 snprintf(buf, 100, "%s",
2210 value_to_string(&fs->id[0], fctr->indep_var[0]));
2212 if ( fctr->indep_var[1] )
2214 sprintf(buf2, ",%s)", value_to_string(&fs->id[1], fctr->indep_var[1]) );