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
22 #include <gsl/gsl_cdf.h>
23 #include <libpspp/message.h>
27 #include <libpspp/alloc.h>
28 #include <libpspp/str.h>
29 #include <data/case.h>
30 #include <data/dictionary.h>
31 #include <language/command.h>
32 #include <libpspp/compiler.h>
33 #include <language/lexer/lexer.h>
34 #include <libpspp/message.h>
35 #include <libpspp/magic.h>
36 #include <libpspp/misc.h>
37 #include <output/table.h>
38 #include <output/manager.h>
39 #include <data/value-labels.h>
40 #include <data/variable.h>
41 #include <procedure.h>
42 #include <libpspp/hash.h>
43 #include <data/casefile.h>
44 #include <math/factor-stats.h>
45 #include <math/moments.h>
46 #include <math/percentiles.h>
47 #include <output/charts/box-whisker.h>
48 #include <output/charts/cartesian.h>
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) msgid
55 #include <output/chart.h>
56 #include <output/charts/plot-hist.h>
57 #include <output/charts/plot-chart.h>
64 +missing=miss:pairwise/!listwise,
66 incl:include/!exclude;
67 +compare=cmp:variables/!groups;
70 +plot[plt_]=stemleaf,boxplot,npplot,:spreadlevel(*d:n),histogram,all,none;
72 +statistics[st_]=descriptives,:extreme(*d:n),all,none.
81 static struct cmd_examine cmd;
83 static struct variable **dependent_vars;
85 static size_t n_dependent_vars;
90 /* The independent variable */
91 struct variable *indep_var[2];
94 /* Hash table of factor stats indexed by 2 values */
95 struct hsh_table *fstats;
97 /* The hash table after it has been crunched */
98 struct factor_statistics **fs;
104 /* Linked list of factors */
105 static struct factor *factors=0;
107 static struct metrics *totals=0;
109 /* Parse the clause specifying the factors */
110 static int examine_parse_independent_vars(struct cmd_examine *cmd);
114 /* Output functions */
115 static void show_summary(struct variable **dependent_var, int n_dep_var,
116 const struct factor *f);
118 static void show_extremes(struct variable **dependent_var,
120 const struct factor *factor,
123 static void show_descriptives(struct variable **dependent_var,
125 struct factor *factor);
127 static void show_percentiles(struct variable **dependent_var,
129 struct factor *factor);
134 void np_plot(const struct metrics *m, const char *factorname);
137 void box_plot_group(const struct factor *fctr,
138 const struct variable **vars, int n_vars,
139 const struct variable *id
143 void box_plot_variables(const struct factor *fctr,
144 const struct variable **vars, int n_vars,
145 const struct variable *id
150 /* Per Split function */
151 static bool run_examine(const struct casefile *cf, void *cmd_);
153 static void output_examine(void);
156 void factor_calc(struct ccase *c, int case_no,
157 double weight, int case_missing);
160 /* Represent a factor as a string, so it can be
161 printed in a human readable fashion */
162 const char * factor_to_string(const struct factor *fctr,
163 struct factor_statistics *fs,
164 const struct variable *var);
167 /* Represent a factor as a string, so it can be
168 printed in a human readable fashion,
169 but sacrificing some readablility for the sake of brevity */
170 const char *factor_to_string_concise(const struct factor *fctr,
171 struct factor_statistics *fs);
176 /* Function to use for testing for missing values */
177 static is_missing_func *value_is_missing;
182 static subc_list_double percentile_list;
184 static enum pc_alg percentile_algorithm;
186 static short sbc_percentile;
194 subc_list_double_create(&percentile_list);
195 percentile_algorithm = PC_HAVERAGE;
197 if ( !parse_examine(&cmd) )
200 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
201 if (cmd.incl == XMN_INCLUDE )
202 value_is_missing = mv_is_value_system_missing;
204 value_is_missing = mv_is_value_missing;
206 if ( cmd.st_n == SYSMIS )
209 if ( ! cmd.sbc_cinterval)
210 cmd.n_cinterval[0] = 95.0;
212 /* If descriptives have been requested, make sure the
213 quartiles are calculated */
214 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES] )
216 subc_list_double_push(&percentile_list, 25);
217 subc_list_double_push(&percentile_list, 50);
218 subc_list_double_push(&percentile_list, 75);
221 ok = multipass_procedure_with_splits (run_examine, &cmd);
228 if ( dependent_vars )
229 free (dependent_vars);
232 struct factor *f = factors ;
235 struct factor *ff = f;
239 hsh_destroy ( ff->fstats ) ;
245 subc_list_double_destroy(&percentile_list);
247 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
252 /* Show all the appropriate tables */
258 /* Show totals if appropriate */
259 if ( ! cmd.sbc_nototal || factors == 0 )
261 show_summary(dependent_vars, n_dependent_vars, 0);
263 if ( cmd.sbc_statistics )
265 if ( cmd.a_statistics[XMN_ST_EXTREME])
266 show_extremes(dependent_vars, n_dependent_vars, 0, cmd.st_n);
268 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
269 show_descriptives(dependent_vars, n_dependent_vars, 0);
272 if ( sbc_percentile )
273 show_percentiles(dependent_vars, n_dependent_vars, 0);
278 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
280 for ( v = 0 ; v < n_dependent_vars; ++v )
281 np_plot(&totals[v], var_to_string(dependent_vars[v]));
284 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
286 if ( cmd.cmp == XMN_GROUPS )
288 box_plot_group(0, dependent_vars, n_dependent_vars,
292 box_plot_variables(0, dependent_vars, n_dependent_vars,
296 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
298 for ( v = 0 ; v < n_dependent_vars; ++v )
300 struct normal_curve normal;
302 normal.N = totals[v].n;
303 normal.mean = totals[v].mean;
304 normal.stddev = totals[v].stddev;
306 histogram_plot(totals[v].histogram,
307 var_to_string(dependent_vars[v]),
317 /* Show grouped statistics as appropriate */
321 show_summary(dependent_vars, n_dependent_vars, fctr);
323 if ( cmd.sbc_statistics )
325 if ( cmd.a_statistics[XMN_ST_EXTREME])
326 show_extremes(dependent_vars, n_dependent_vars, fctr, cmd.st_n);
328 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
329 show_descriptives(dependent_vars, n_dependent_vars, fctr);
332 if ( sbc_percentile )
333 show_percentiles(dependent_vars, n_dependent_vars, fctr);
340 struct factor_statistics **fs = fctr->fs ;
342 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
344 if ( cmd.cmp == XMN_VARIABLES )
345 box_plot_variables(fctr, dependent_vars, n_dependent_vars,
348 box_plot_group(fctr, dependent_vars, n_dependent_vars,
352 for ( v = 0 ; v < n_dependent_vars; ++v )
355 for ( fs = fctr->fs ; *fs ; ++fs )
357 const char *s = factor_to_string(fctr, *fs, dependent_vars[v]);
359 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
360 np_plot(&(*fs)->m[v], s);
362 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
364 struct normal_curve normal;
366 normal.N = (*fs)->m[v].n;
367 normal.mean = (*fs)->m[v].mean;
368 normal.stddev = (*fs)->m[v].stddev;
370 histogram_plot((*fs)->m[v].histogram,
374 } /* for ( fs .... */
376 } /* for ( v = 0 ..... */
386 /* Create a hash table of percentiles and their values from the list of
388 static struct hsh_table *
389 list_to_ptile_hash(const subc_list_double *l)
393 struct hsh_table *h ;
395 h = hsh_create(subc_list_double_count(l),
396 (hsh_compare_func *) ptile_compare,
397 (hsh_hash_func *) ptile_hash,
398 (hsh_free_func *) free,
402 for ( i = 0 ; i < subc_list_double_count(l) ; ++i )
404 struct percentile *p = xmalloc (sizeof *p);
406 p->p = subc_list_double_at(l,i);
417 /* Parse the PERCENTILES subcommand */
419 xmn_custom_percentiles(struct cmd_examine *p UNUSED)
427 while ( lex_is_number() )
429 subc_list_double_push(&percentile_list,lex_number());
439 if ( lex_match_id("HAVERAGE"))
440 percentile_algorithm = PC_HAVERAGE;
442 else if ( lex_match_id("WAVERAGE"))
443 percentile_algorithm = PC_WAVERAGE;
445 else if ( lex_match_id("ROUND"))
446 percentile_algorithm = PC_ROUND;
448 else if ( lex_match_id("EMPIRICAL"))
449 percentile_algorithm = PC_EMPIRICAL;
451 else if ( lex_match_id("AEMPIRICAL"))
452 percentile_algorithm = PC_AEMPIRICAL;
454 else if ( lex_match_id("NONE"))
455 percentile_algorithm = PC_NONE;
458 if ( 0 == subc_list_double_count(&percentile_list))
460 subc_list_double_push(&percentile_list, 5);
461 subc_list_double_push(&percentile_list, 10);
462 subc_list_double_push(&percentile_list, 25);
463 subc_list_double_push(&percentile_list, 50);
464 subc_list_double_push(&percentile_list, 75);
465 subc_list_double_push(&percentile_list, 90);
466 subc_list_double_push(&percentile_list, 95);
472 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
474 xmn_custom_total(struct cmd_examine *p)
476 if ( p->sbc_nototal )
478 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
486 xmn_custom_nototal(struct cmd_examine *p)
490 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
499 /* Parser for the variables sub command
500 Returns 1 on success */
502 xmn_custom_variables(struct cmd_examine *cmd )
506 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
512 if (!parse_variables (default_dict, &dependent_vars, &n_dependent_vars,
513 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
515 free (dependent_vars);
519 assert(n_dependent_vars);
521 totals = xnmalloc (n_dependent_vars, sizeof *totals);
523 if ( lex_match(T_BY))
526 success = examine_parse_independent_vars(cmd);
527 if ( success != 1 ) {
528 free (dependent_vars);
539 /* Parse the clause specifying the factors */
541 examine_parse_independent_vars(struct cmd_examine *cmd)
544 struct factor *sf = xmalloc (sizeof *sf);
546 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
554 sf->indep_var[0] = parse_variable();
555 sf->indep_var[1] = 0;
562 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
569 sf->indep_var[1] = parse_variable();
574 sf->fstats = hsh_create(4,
575 (hsh_compare_func *) factor_statistics_compare,
576 (hsh_hash_func *) factor_statistics_hash,
577 (hsh_free_func *) factor_statistics_free,
585 if ( token == '.' || token == '/' )
588 success = examine_parse_independent_vars(cmd);
599 void populate_percentiles(struct tab_table *tbl, int col, int row,
600 const struct metrics *m);
602 void populate_descriptives(struct tab_table *t, int col, int row,
603 const struct metrics *fs);
605 void populate_extremes(struct tab_table *t, int col, int row, int n,
606 const struct metrics *m);
608 void populate_summary(struct tab_table *t, int col, int row,
609 const struct metrics *m);
614 static int bad_weight_warn = 1;
617 /* Perform calculations for the sub factors */
619 factor_calc(struct ccase *c, int case_no, double weight, int case_missing)
622 struct factor *fctr = factors;
626 struct factor_statistics **foo ;
627 union value indep_vals[2] ;
629 indep_vals[0] = * case_data(c, fctr->indep_var[0]->fv);
631 if ( fctr->indep_var[1] )
632 indep_vals[1] = * case_data(c, fctr->indep_var[1]->fv);
634 indep_vals[1].f = SYSMIS;
636 assert(fctr->fstats);
638 foo = ( struct factor_statistics ** )
639 hsh_probe(fctr->fstats, (void *) &indep_vals);
644 *foo = create_factor_statistics(n_dependent_vars,
648 for ( v = 0 ; v < n_dependent_vars ; ++v )
650 metrics_precalc( &(*foo)->m[v] );
655 for ( v = 0 ; v < n_dependent_vars ; ++v )
657 const struct variable *var = dependent_vars[v];
658 const union value *val = case_data (c, var->fv);
660 if ( value_is_missing (&var->miss, val) || case_missing )
663 metrics_calc( &(*foo)->m[v], val, weight, case_no);
674 run_examine(const struct casefile *cf, void *cmd_ )
676 struct casereader *r;
680 const struct cmd_examine *cmd = (struct cmd_examine *) cmd_;
682 /* Make sure we haven't got rubbish left over from a
684 struct factor *fctr = factors;
687 struct factor *next = fctr->next;
689 hsh_clear(fctr->fstats);
698 for ( v = 0 ; v < n_dependent_vars ; ++v )
699 metrics_precalc(&totals[v]);
701 for(r = casefile_get_reader (cf);
702 casereader_read (r, &c) ;
706 const int case_no = casereader_cnum(r);
708 const double weight =
709 dict_get_case_weight(default_dict, &c, &bad_weight_warn);
711 if ( cmd->miss == XMN_LISTWISE )
713 for ( v = 0 ; v < n_dependent_vars ; ++v )
715 const struct variable *var = dependent_vars[v];
716 const union value *val = case_data (&c, var->fv);
718 if ( value_is_missing(&var->miss, val))
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) || case_missing )
732 metrics_calc(&totals[v], val, weight, case_no);
736 factor_calc(&c, case_no, weight, case_missing);
741 for ( v = 0 ; v < n_dependent_vars ; ++v)
746 struct hsh_iterator hi;
747 struct factor_statistics *fs;
749 for ( fs = hsh_first(fctr->fstats, &hi);
751 fs = hsh_next(fctr->fstats, &hi))
754 fs->m[v].ptile_hash = list_to_ptile_hash(&percentile_list);
755 fs->m[v].ptile_alg = percentile_algorithm;
756 metrics_postcalc(&fs->m[v]);
762 totals[v].ptile_hash = list_to_ptile_hash(&percentile_list);
763 totals[v].ptile_alg = percentile_algorithm;
764 metrics_postcalc(&totals[v]);
768 /* Make sure that the combination of factors are complete */
773 struct hsh_iterator hi;
774 struct hsh_iterator hi0;
775 struct hsh_iterator hi1;
776 struct factor_statistics *fs;
778 struct hsh_table *idh0=0;
779 struct hsh_table *idh1=0;
783 idh0 = hsh_create(4, (hsh_compare_func *) compare_values,
784 (hsh_hash_func *) hash_value,
787 idh1 = hsh_create(4, (hsh_compare_func *) compare_values,
788 (hsh_hash_func *) hash_value,
792 for ( fs = hsh_first(fctr->fstats, &hi);
794 fs = hsh_next(fctr->fstats, &hi))
796 hsh_insert(idh0,(void *) &fs->id[0]);
797 hsh_insert(idh1,(void *) &fs->id[1]);
800 /* Ensure that the factors combination is complete */
801 for ( val0 = hsh_first(idh0, &hi0);
803 val0 = hsh_next(idh0, &hi0))
805 for ( val1 = hsh_first(idh1, &hi1);
807 val1 = hsh_next(idh1, &hi1))
809 struct factor_statistics **ffs;
814 ffs = (struct factor_statistics **)
815 hsh_probe(fctr->fstats, (void *) &key );
819 (*ffs) = create_factor_statistics (n_dependent_vars,
821 for ( i = 0 ; i < n_dependent_vars ; ++i )
822 metrics_precalc( &(*ffs)->m[i]);
830 fctr->fs = (struct factor_statistics **) hsh_sort_copy(fctr->fstats);
841 for ( i = 0 ; i < n_dependent_vars ; ++i )
843 metrics_destroy(&totals[i]);
852 show_summary(struct variable **dependent_var, int n_dep_var,
853 const struct factor *fctr)
855 static const char *subtitle[]=
863 int heading_columns ;
865 const int heading_rows = 3;
866 struct tab_table *tbl;
874 n_factors = hsh_count(fctr->fstats);
875 n_rows = n_dep_var * n_factors ;
877 if ( fctr->indep_var[1] )
886 n_rows += heading_rows;
888 n_cols = heading_columns + 6;
890 tbl = tab_create (n_cols,n_rows,0);
891 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
893 tab_dim (tbl, tab_natural_dimensions);
895 /* Outline the box */
900 n_cols - 1, n_rows - 1);
902 /* Vertical lines for the data only */
907 n_cols - 1, n_rows - 1);
910 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
911 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
912 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
914 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
917 tab_title (tbl, 0, _("Case Processing Summary"));
920 tab_joint_text(tbl, heading_columns, 0,
922 TAB_CENTER | TAT_TITLE,
925 /* Remove lines ... */
932 for ( i = 0 ; i < 3 ; ++i )
934 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
937 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
940 tab_joint_text(tbl, heading_columns + i*2 , 1,
941 heading_columns + i*2 + 1, 1,
942 TAB_CENTER | TAT_TITLE,
945 tab_box (tbl, -1, -1,
947 heading_columns + i*2, 1,
948 heading_columns + i*2 + 1, 1);
953 /* Titles for the independent variables */
956 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
957 var_to_string(fctr->indep_var[0]));
959 if ( fctr->indep_var[1] )
961 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
962 var_to_string(fctr->indep_var[1]));
968 for ( i = 0 ; i < n_dep_var ; ++i )
972 n_factors = hsh_count(fctr->fstats);
976 tab_hline(tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
979 0, i * n_factors + heading_rows,
980 TAB_LEFT | TAT_TITLE,
981 var_to_string(dependent_var[i])
986 populate_summary(tbl, heading_columns,
987 (i * n_factors) + heading_rows,
993 struct factor_statistics **fs = fctr->fs;
998 static union value prev;
1000 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1001 fctr->indep_var[0]->width))
1005 (i * n_factors ) + count +
1007 TAB_LEFT | TAT_TITLE,
1008 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1011 if (fctr->indep_var[1] && count > 0 )
1012 tab_hline(tbl, TAL_1, 1, n_cols - 1,
1013 (i * n_factors ) + count + heading_rows);
1017 prev = (*fs)->id[0];
1020 if ( fctr->indep_var[1])
1023 (i * n_factors ) + count +
1025 TAB_LEFT | TAT_TITLE,
1026 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1029 populate_summary(tbl, heading_columns,
1030 (i * n_factors) + count
1045 populate_summary(struct tab_table *t, int col, int row,
1046 const struct metrics *m)
1049 const double total = m->n + m->n_missing ;
1051 tab_float(t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1052 tab_float(t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1053 tab_float(t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1057 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1058 100.0 * m->n / total );
1060 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1061 100.0 * m->n_missing / total );
1063 /* This seems a bit pointless !!! */
1064 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1065 100.0 * total / total );
1076 show_extremes(struct variable **dependent_var, int n_dep_var,
1077 const struct factor *fctr, int n_extremities)
1080 int heading_columns ;
1082 const int heading_rows = 1;
1083 struct tab_table *tbl;
1090 heading_columns = 2;
1091 n_factors = hsh_count(fctr->fstats);
1093 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1095 if ( fctr->indep_var[1] )
1096 heading_columns = 3;
1100 heading_columns = 1;
1101 n_rows = n_dep_var * 2 * n_extremities;
1104 n_rows += heading_rows;
1106 heading_columns += 2;
1107 n_cols = heading_columns + 2;
1109 tbl = tab_create (n_cols,n_rows,0);
1110 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1112 tab_dim (tbl, tab_natural_dimensions);
1114 /* Outline the box, No internal lines*/
1119 n_cols - 1, n_rows - 1);
1121 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1123 tab_title (tbl, 0, _("Extreme Values"));
1125 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1126 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1130 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1131 var_to_string(fctr->indep_var[0]));
1133 if ( fctr->indep_var[1] )
1134 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1135 var_to_string(fctr->indep_var[1]));
1138 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Value"));
1139 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Case Number"));
1141 for ( i = 0 ; i < n_dep_var ; ++i )
1145 tab_hline(tbl, TAL_1, 0, n_cols -1 ,
1146 i * 2 * n_extremities * n_factors + heading_rows);
1149 i * 2 * n_extremities * n_factors + heading_rows,
1150 TAB_LEFT | TAT_TITLE,
1151 var_to_string(dependent_var[i])
1156 populate_extremes(tbl, heading_columns - 2,
1157 i * 2 * n_extremities * n_factors + heading_rows,
1158 n_extremities, &totals[i]);
1162 struct factor_statistics **fs = fctr->fs;
1167 static union value prev ;
1169 const int row = heading_rows + ( 2 * n_extremities ) *
1170 ( ( i * n_factors ) + count );
1173 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1174 fctr->indep_var[0]->width))
1178 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1182 TAB_LEFT | TAT_TITLE,
1183 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1187 prev = (*fs)->id[0];
1189 if (fctr->indep_var[1] && count > 0 )
1190 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1192 if ( fctr->indep_var[1])
1193 tab_text (tbl, 2, row,
1194 TAB_LEFT | TAT_TITLE,
1195 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1198 populate_extremes(tbl, heading_columns - 2,
1213 /* Fill in the extremities table */
1215 populate_extremes(struct tab_table *t,
1216 int col, int row, int n, const struct metrics *m)
1222 tab_text(t, col, row,
1223 TAB_RIGHT | TAT_TITLE ,
1227 tab_text(t, col, row + n ,
1228 TAB_RIGHT | TAT_TITLE ,
1233 tab_hline(t, TAL_1, col, col + 3, row + n );
1235 for (extremity = 0; extremity < n ; ++extremity )
1238 tab_float(t, col + 1, row + extremity,
1240 extremity + 1, 8, 0);
1244 tab_float(t, col + 1, row + extremity + n,
1246 extremity + 1, 8, 0);
1252 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1255 const struct weighted_value *wv = m->wvp[idx];
1256 struct case_node *cn = wv->case_nos;
1259 for (j = 0 ; j < wv->w ; ++j )
1261 if ( extremity + j >= n )
1264 tab_float(t, col + 3, row + extremity + j + n,
1268 tab_float(t, col + 2, row + extremity + j + n,
1277 extremity += wv->w ;
1282 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1285 const struct weighted_value *wv = m->wvp[idx];
1286 struct case_node *cn = wv->case_nos;
1288 for (j = 0 ; j < wv->w ; ++j )
1290 if ( extremity + j >= n )
1293 tab_float(t, col + 3, row + extremity + j,
1297 tab_float(t, col + 2, row + extremity + j,
1306 extremity += wv->w ;
1311 /* Show the descriptives table */
1313 show_descriptives(struct variable **dependent_var,
1315 struct factor *fctr)
1318 int heading_columns ;
1320 const int n_stat_rows = 13;
1322 const int heading_rows = 1;
1324 struct tab_table *tbl;
1331 heading_columns = 4;
1332 n_factors = hsh_count(fctr->fstats);
1334 n_rows = n_dep_var * n_stat_rows * n_factors;
1336 if ( fctr->indep_var[1] )
1337 heading_columns = 5;
1341 heading_columns = 3;
1342 n_rows = n_dep_var * n_stat_rows;
1345 n_rows += heading_rows;
1347 n_cols = heading_columns + 2;
1350 tbl = tab_create (n_cols, n_rows, 0);
1352 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1354 tab_dim (tbl, tab_natural_dimensions);
1356 /* Outline the box and have no internal lines*/
1361 n_cols - 1, n_rows - 1);
1363 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1365 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1366 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1367 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1369 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Statistic"));
1370 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1372 tab_title (tbl, 0, _("Descriptives"));
1375 for ( i = 0 ; i < n_dep_var ; ++i )
1377 const int row = heading_rows + i * n_stat_rows * n_factors ;
1380 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1383 i * n_stat_rows * n_factors + heading_rows,
1384 TAB_LEFT | TAT_TITLE,
1385 var_to_string(dependent_var[i])
1391 struct factor_statistics **fs = fctr->fs;
1394 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1395 var_to_string(fctr->indep_var[0]));
1398 if ( fctr->indep_var[1])
1399 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1400 var_to_string(fctr->indep_var[1]));
1405 static union value prev ;
1407 const int row = heading_rows + n_stat_rows *
1408 ( ( i * n_factors ) + count );
1411 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1412 fctr->indep_var[0]->width))
1416 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1420 TAB_LEFT | TAT_TITLE,
1421 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1425 prev = (*fs)->id[0];
1427 if (fctr->indep_var[1] && count > 0 )
1428 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1430 if ( fctr->indep_var[1])
1431 tab_text (tbl, 2, row,
1432 TAB_LEFT | TAT_TITLE,
1433 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1436 populate_descriptives(tbl, heading_columns - 2,
1448 populate_descriptives(tbl, heading_columns - 2,
1449 i * n_stat_rows * n_factors + heading_rows,
1461 /* Fill in the descriptives data */
1463 populate_descriptives(struct tab_table *tbl, int col, int row,
1464 const struct metrics *m)
1467 const double t = gsl_cdf_tdist_Qinv(1 - cmd.n_cinterval[0]/100.0/2.0, \
1473 TAB_LEFT | TAT_TITLE,
1476 tab_float (tbl, col + 2,
1482 tab_float (tbl, col + 3,
1491 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1492 _("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1495 tab_text (tbl, col + 1,
1497 TAB_LEFT | TAT_TITLE,
1500 tab_float (tbl, col + 2,
1503 m->mean - t * m->se_mean,
1506 tab_text (tbl, col + 1,
1508 TAB_LEFT | TAT_TITLE,
1512 tab_float (tbl, col + 2,
1515 m->mean + t * m->se_mean,
1520 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1521 _("5%% Trimmed Mean"));
1523 tab_float (tbl, col + 2,
1531 TAB_LEFT | TAT_TITLE,
1535 struct percentile *p;
1538 p = hsh_find(m->ptile_hash, &d);
1543 tab_float (tbl, col + 2,
1553 TAB_LEFT | TAT_TITLE,
1556 tab_float (tbl, col + 2,
1565 TAB_LEFT | TAT_TITLE,
1566 _("Std. Deviation"));
1569 tab_float (tbl, col + 2,
1578 TAB_LEFT | TAT_TITLE,
1581 tab_float (tbl, col + 2,
1589 TAB_LEFT | TAT_TITLE,
1592 tab_float (tbl, col + 2,
1601 TAB_LEFT | TAT_TITLE,
1605 tab_float (tbl, col + 2,
1613 TAB_LEFT | TAT_TITLE,
1614 _("Interquartile Range"));
1617 struct percentile *p1;
1618 struct percentile *p2;
1621 p1 = hsh_find(m->ptile_hash, &d);
1624 p2 = hsh_find(m->ptile_hash, &d);
1629 tab_float (tbl, col + 2,
1640 TAB_LEFT | TAT_TITLE,
1644 tab_float (tbl, col + 2,
1650 /* stderr of skewness */
1651 tab_float (tbl, col + 3,
1660 TAB_LEFT | TAT_TITLE,
1664 tab_float (tbl, col + 2,
1670 /* stderr of kurtosis */
1671 tab_float (tbl, col + 3,
1683 box_plot_variables(const struct factor *fctr,
1684 const struct variable **vars, int n_vars,
1685 const struct variable *id)
1689 struct factor_statistics **fs ;
1693 box_plot_group(fctr, vars, n_vars, id);
1697 for ( fs = fctr->fs ; *fs ; ++fs )
1699 double y_min = DBL_MAX;
1700 double y_max = -DBL_MAX;
1701 struct chart *ch = chart_create();
1702 const char *s = factor_to_string(fctr, *fs, 0 );
1704 chart_write_title(ch, s);
1706 for ( i = 0 ; i < n_vars ; ++i )
1708 y_max = max(y_max, (*fs)->m[i].max);
1709 y_min = min(y_min, (*fs)->m[i].min);
1712 boxplot_draw_yscale(ch, y_max, y_min);
1714 for ( i = 0 ; i < n_vars ; ++i )
1717 const double box_width = (ch->data_right - ch->data_left)
1720 const double box_centre = ( i * 2 + 1) * box_width
1723 boxplot_draw_boxplot(ch,
1724 box_centre, box_width,
1726 var_to_string(vars[i]));
1738 /* Do a box plot, grouping all factors into one plot ;
1739 each dependent variable has its own plot.
1742 box_plot_group(const struct factor *fctr,
1743 const struct variable **vars,
1745 const struct variable *id UNUSED)
1750 for ( i = 0 ; i < n_vars ; ++i )
1752 struct factor_statistics **fs ;
1755 ch = chart_create();
1757 boxplot_draw_yscale(ch, totals[i].max, totals[i].min);
1763 for ( fs = fctr->fs ; *fs ; ++fs )
1766 chart_write_title(ch, _("Boxplot of %s vs. %s"),
1767 var_to_string(vars[i]), var_to_string(fctr->indep_var[0]) );
1769 for ( fs = fctr->fs ; *fs ; ++fs )
1772 const char *s = factor_to_string_concise(fctr, *fs);
1774 const double box_width = (ch->data_right - ch->data_left)
1775 / (n_factors * 2.0 ) ;
1777 const double box_centre = ( f++ * 2 + 1) * box_width
1780 boxplot_draw_boxplot(ch,
1781 box_centre, box_width,
1788 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1789 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1791 chart_write_title(ch, _("Boxplot"));
1793 boxplot_draw_boxplot(ch,
1794 box_centre, box_width,
1796 var_to_string(vars[i]) );
1805 /* Plot the normal and detrended normal plots for m
1806 Label the plots with factorname */
1808 np_plot(const struct metrics *m, const char *factorname)
1811 double yfirst=0, ylast=0;
1814 struct chart *np_chart;
1816 /* Detrended Normal Plot */
1817 struct chart *dnp_chart;
1819 /* The slope and intercept of the ideal normal probability line */
1820 const double slope = 1.0 / m->stddev;
1821 const double intercept = - m->mean / m->stddev;
1823 /* Cowardly refuse to plot an empty data set */
1824 if ( m->n_data == 0 )
1827 np_chart = chart_create();
1828 dnp_chart = chart_create();
1830 if ( !np_chart || ! dnp_chart )
1833 chart_write_title(np_chart, _("Normal Q-Q Plot of %s"), factorname);
1834 chart_write_xlabel(np_chart, _("Observed Value"));
1835 chart_write_ylabel(np_chart, _("Expected Normal"));
1838 chart_write_title(dnp_chart, _("Detrended Normal Q-Q Plot of %s"),
1840 chart_write_xlabel(dnp_chart, _("Observed Value"));
1841 chart_write_ylabel(dnp_chart, _("Dev from Normal"));
1843 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1844 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1848 /* Need to make sure that both the scatter plot and the ideal fit into the
1850 double x_lower = min(m->min, (yfirst - intercept) / slope) ;
1851 double x_upper = max(m->max, (ylast - intercept) / slope) ;
1852 double slack = (x_upper - x_lower) * 0.05 ;
1854 chart_write_xscale(np_chart, x_lower - slack, x_upper + slack, 5);
1856 chart_write_xscale(dnp_chart, m->min, m->max, 5);
1860 chart_write_yscale(np_chart, yfirst, ylast, 5);
1863 /* We have to cache the detrended data, beacause we need to
1864 find its limits before we can plot it */
1865 double *d_data = xnmalloc (m->n_data, sizeof *d_data);
1866 double d_max = -DBL_MAX;
1867 double d_min = DBL_MAX;
1868 for ( i = 0 ; i < m->n_data; ++i )
1870 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1872 chart_datum(np_chart, 0, m->wvp[i]->v.f, ns);
1874 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1876 if ( d_data[i] < d_min ) d_min = d_data[i];
1877 if ( d_data[i] > d_max ) d_max = d_data[i];
1879 chart_write_yscale(dnp_chart, d_min, d_max, 5);
1881 for ( i = 0 ; i < m->n_data; ++i )
1882 chart_datum(dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1887 chart_line(np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1888 chart_line(dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1890 chart_submit(np_chart);
1891 chart_submit(dnp_chart);
1897 /* Show the percentiles */
1899 show_percentiles(struct variable **dependent_var,
1901 struct factor *fctr)
1903 struct tab_table *tbl;
1909 struct hsh_table *ptiles ;
1911 int n_heading_columns;
1912 const int n_heading_rows = 2;
1913 const int n_stat_rows = 2;
1919 struct factor_statistics **fs = fctr->fs ;
1920 n_heading_columns = 3;
1921 n_factors = hsh_count(fctr->fstats);
1923 ptiles = (*fs)->m[0].ptile_hash;
1925 if ( fctr->indep_var[1] )
1926 n_heading_columns = 4;
1931 n_heading_columns = 2;
1933 ptiles = totals[0].ptile_hash;
1936 n_ptiles = hsh_count(ptiles);
1938 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1940 n_cols = n_heading_columns + n_ptiles ;
1942 tbl = tab_create (n_cols, n_rows, 0);
1944 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1946 tab_dim (tbl, tab_natural_dimensions);
1948 /* Outline the box and have no internal lines*/
1953 n_cols - 1, n_rows - 1);
1955 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
1957 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
1960 tab_title (tbl, 0, _("Percentiles"));
1963 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
1970 n_heading_columns - 1, n_rows - 1);
1976 n_heading_columns, n_heading_rows - 1,
1977 n_cols - 1, n_rows - 1);
1979 tab_joint_text(tbl, n_heading_columns + 1, 0,
1981 TAB_CENTER | TAT_TITLE ,
1986 /* Put in the percentile break points as headings */
1988 struct percentile **p = (struct percentile **) hsh_sort(ptiles);
1993 tab_float(tbl, n_heading_columns + i++ , 1,
2002 for ( i = 0 ; i < n_dep_var ; ++i )
2004 const int n_stat_rows = 2;
2005 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
2008 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
2011 i * n_stat_rows * n_factors + n_heading_rows,
2012 TAB_LEFT | TAT_TITLE,
2013 var_to_string(dependent_var[i])
2018 struct factor_statistics **fs = fctr->fs;
2021 tab_text (tbl, 1, n_heading_rows - 1,
2022 TAB_CENTER | TAT_TITLE,
2023 var_to_string(fctr->indep_var[0]));
2026 if ( fctr->indep_var[1])
2027 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2028 var_to_string(fctr->indep_var[1]));
2033 static union value prev ;
2035 const int row = n_heading_rows + n_stat_rows *
2036 ( ( i * n_factors ) + count );
2039 if ( 0 != compare_values(&prev, &(*fs)->id[0],
2040 fctr->indep_var[0]->width))
2044 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2048 TAB_LEFT | TAT_TITLE,
2049 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
2055 prev = (*fs)->id[0];
2057 if (fctr->indep_var[1] && count > 0 )
2058 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
2060 if ( fctr->indep_var[1])
2061 tab_text (tbl, 2, row,
2062 TAB_LEFT | TAT_TITLE,
2063 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
2067 populate_percentiles(tbl, n_heading_columns - 1,
2079 populate_percentiles(tbl, n_heading_columns - 1,
2080 i * n_stat_rows * n_factors + n_heading_rows,
2097 populate_percentiles(struct tab_table *tbl, int col, int row,
2098 const struct metrics *m)
2102 struct percentile **p = (struct percentile **) hsh_sort(m->ptile_hash);
2106 TAB_LEFT | TAT_TITLE,
2107 _("Tukey\'s Hinges")
2112 TAB_LEFT | TAT_TITLE,
2113 ptile_alg_desc[m->ptile_alg]
2120 tab_float(tbl, col + i + 1 , row,
2123 if ( (*p)->p == 25 )
2124 tab_float(tbl, col + i + 1 , row + 1,
2128 if ( (*p)->p == 50 )
2129 tab_float(tbl, col + i + 1 , row + 1,
2133 if ( (*p)->p == 75 )
2134 tab_float(tbl, col + i + 1 , row + 1,
2149 factor_to_string(const struct factor *fctr,
2150 struct factor_statistics *fs,
2151 const struct variable *var)
2154 static char buf1[100];
2160 sprintf(buf1, "%s (",var_to_string(var) );
2163 snprintf(buf2, 100, "%s = %s",
2164 var_to_string(fctr->indep_var[0]),
2165 value_to_string(&fs->id[0],fctr->indep_var[0]));
2169 if ( fctr->indep_var[1] )
2171 sprintf(buf2, "; %s = %s)",
2172 var_to_string(fctr->indep_var[1]),
2173 value_to_string(&fs->id[1],
2174 fctr->indep_var[1]));
2189 factor_to_string_concise(const struct factor *fctr,
2190 struct factor_statistics *fs)
2194 static char buf[100];
2198 snprintf(buf, 100, "%s",
2199 value_to_string(&fs->id[0], fctr->indep_var[0]));
2201 if ( fctr->indep_var[1] )
2203 sprintf(buf2, ",%s)", value_to_string(&fs->id[1], fctr->indep_var[1]) );