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., 59 Temple Place - Suite 330, Boston, MA
22 #include <gsl/gsl_cdf.h>
30 #include "dictionary.h"
38 #include "value-labels.h"
43 #include "factor_stats.h"
45 #include "percentiles.h"
55 +missing=miss:pairwise/!listwise,
57 incl:include/!exclude;
58 +compare=cmp:variables/!groups;
61 +plot[plt_]=stemleaf,boxplot,npplot,:spreadlevel(*d:n),histogram,all,none;
63 +statistics[st_]=descriptives,:extreme(*d:n),all,none.
72 static struct cmd_examine cmd;
74 static struct variable **dependent_vars;
76 static int n_dependent_vars;
81 /* The independent variable */
82 struct variable *indep_var[2];
85 /* Hash table of factor stats indexed by 2 values */
86 struct hsh_table *fstats;
88 /* The hash table after it has been crunched */
89 struct factor_statistics **fs;
95 /* Linked list of factors */
96 static struct factor *factors=0;
98 static struct metrics *totals=0;
100 /* Parse the clause specifying the factors */
101 static int examine_parse_independent_vars(struct cmd_examine *cmd);
105 /* Output functions */
106 static void show_summary(struct variable **dependent_var, int n_dep_var,
107 const struct factor *f);
109 static void show_extremes(struct variable **dependent_var,
111 const struct factor *factor,
114 static void show_descriptives(struct variable **dependent_var,
116 struct factor *factor);
118 static void show_percentiles(struct variable **dependent_var,
120 struct factor *factor);
125 void np_plot(const struct metrics *m, const char *factorname);
128 void box_plot_group(const struct factor *fctr,
129 const struct variable **vars, int n_vars,
130 const struct variable *id
134 void box_plot_variables(const struct factor *fctr,
135 const struct variable **vars, int n_vars,
136 const struct variable *id
141 /* Per Split function */
142 static void run_examine(const struct casefile *cf, void *cmd_);
144 static void output_examine(void);
147 void factor_calc(struct ccase *c, int case_no,
148 double weight, int case_missing);
151 /* Represent a factor as a string, so it can be
152 printed in a human readable fashion */
153 const char * factor_to_string(const struct factor *fctr,
154 struct factor_statistics *fs,
155 const struct variable *var);
158 /* Represent a factor as a string, so it can be
159 printed in a human readable fashion,
160 but sacrificing some readablility for the sake of brevity */
161 const char *factor_to_string_concise(const struct factor *fctr,
162 struct factor_statistics *fs);
167 /* Function to use for testing for missing values */
168 static is_missing_func value_is_missing;
173 static subc_list_double percentile_list;
175 static enum pc_alg percentile_algorithm;
177 static short sbc_percentile;
184 subc_list_double_create(&percentile_list);
185 percentile_algorithm = PC_HAVERAGE;
187 if ( !parse_examine(&cmd) )
190 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
191 if (cmd.incl == XMN_INCLUDE )
192 value_is_missing = is_system_missing;
194 value_is_missing = is_missing;
196 if ( cmd.st_n == SYSMIS )
199 if ( ! cmd.sbc_cinterval)
200 cmd.n_cinterval[0] = 95.0;
202 /* If descriptives have been requested, make sure the
203 quartiles are calculated */
204 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES] )
206 subc_list_double_push(&percentile_list, 25);
207 subc_list_double_push(&percentile_list, 50);
208 subc_list_double_push(&percentile_list, 75);
211 multipass_procedure_with_splits (run_examine, &cmd);
216 if ( dependent_vars )
217 free (dependent_vars);
219 subc_list_double_destroy(&percentile_list);
226 /* Show all the appropriate tables */
232 /* Show totals if appropriate */
233 if ( ! cmd.sbc_nototal || factors == 0 )
235 show_summary(dependent_vars, n_dependent_vars, 0);
237 if ( cmd.sbc_statistics )
239 if ( cmd.a_statistics[XMN_ST_EXTREME])
240 show_extremes(dependent_vars, n_dependent_vars, 0, cmd.st_n);
242 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
243 show_descriptives(dependent_vars, n_dependent_vars, 0);
246 if ( sbc_percentile )
247 show_percentiles(dependent_vars, n_dependent_vars, 0);
252 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
254 for ( v = 0 ; v < n_dependent_vars; ++v )
255 np_plot(&totals[v], var_to_string(dependent_vars[v]));
258 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
260 if ( cmd.cmp == XMN_GROUPS )
262 box_plot_group(0, dependent_vars, n_dependent_vars,
266 box_plot_variables(0, dependent_vars, n_dependent_vars,
270 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
272 for ( v = 0 ; v < n_dependent_vars; ++v )
274 struct normal_curve normal;
276 normal.N = totals[v].n;
277 normal.mean = totals[v].mean;
278 normal.stddev = totals[v].stddev;
280 histogram_plot(totals[v].histogram,
281 var_to_string(dependent_vars[v]),
291 /* Show grouped statistics as appropriate */
295 show_summary(dependent_vars, n_dependent_vars, fctr);
297 if ( cmd.sbc_statistics )
299 if ( cmd.a_statistics[XMN_ST_EXTREME])
300 show_extremes(dependent_vars, n_dependent_vars, fctr, cmd.st_n);
302 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
303 show_descriptives(dependent_vars, n_dependent_vars, fctr);
306 if ( sbc_percentile )
307 show_percentiles(dependent_vars, n_dependent_vars, fctr);
314 struct factor_statistics **fs = fctr->fs ;
316 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
318 if ( cmd.cmp == XMN_VARIABLES )
319 box_plot_variables(fctr, dependent_vars, n_dependent_vars,
322 box_plot_group(fctr, dependent_vars, n_dependent_vars,
326 for ( v = 0 ; v < n_dependent_vars; ++v )
329 for ( fs = fctr->fs ; *fs ; ++fs )
331 const char *s = factor_to_string(fctr, *fs, dependent_vars[v]);
333 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
334 np_plot(&(*fs)->m[v], s);
336 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
338 struct normal_curve normal;
340 normal.N = (*fs)->m[v].n;
341 normal.mean = (*fs)->m[v].mean;
342 normal.stddev = (*fs)->m[v].stddev;
344 histogram_plot((*fs)->m[v].histogram,
348 } /* for ( fs .... */
350 } /* for ( v = 0 ..... */
360 static struct hsh_table *
361 list_to_ptile_hash(const subc_list_double *l)
365 struct hsh_table *h ;
367 h = hsh_create(subc_list_double_count(l),
368 (hsh_compare_func *) ptile_compare,
369 (hsh_hash_func *) ptile_hash,
370 (hsh_free_func *) free,
374 for ( i = 0 ; i < subc_list_double_count(l) ; ++i )
376 struct percentile *p = xmalloc (sizeof (struct percentile));
378 p->p = subc_list_double_at(l,i);
388 /* Parse the PERCENTILES subcommand */
390 xmn_custom_percentiles(struct cmd_examine *p UNUSED)
398 while ( lex_double_p() )
400 subc_list_double_push(&percentile_list,lex_double());
410 if ( lex_match_id("HAVERAGE"))
411 percentile_algorithm = PC_HAVERAGE;
413 else if ( lex_match_id("WAVERAGE"))
414 percentile_algorithm = PC_WAVERAGE;
416 else if ( lex_match_id("ROUND"))
417 percentile_algorithm = PC_ROUND;
419 else if ( lex_match_id("EMPIRICAL"))
420 percentile_algorithm = PC_EMPIRICAL;
422 else if ( lex_match_id("AEMPIRICAL"))
423 percentile_algorithm = PC_AEMPIRICAL;
425 else if ( lex_match_id("NONE"))
426 percentile_algorithm = PC_NONE;
429 if ( 0 == subc_list_double_count(&percentile_list))
431 subc_list_double_push(&percentile_list, 5);
432 subc_list_double_push(&percentile_list, 10);
433 subc_list_double_push(&percentile_list, 25);
434 subc_list_double_push(&percentile_list, 50);
435 subc_list_double_push(&percentile_list, 75);
436 subc_list_double_push(&percentile_list, 90);
437 subc_list_double_push(&percentile_list, 95);
443 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
445 xmn_custom_total(struct cmd_examine *p)
447 if ( p->sbc_nototal )
449 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
457 xmn_custom_nototal(struct cmd_examine *p)
461 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
470 /* Parser for the variables sub command */
472 xmn_custom_variables(struct cmd_examine *cmd )
477 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
481 if (!parse_variables (default_dict, &dependent_vars, &n_dependent_vars,
482 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
484 free (dependent_vars);
488 assert(n_dependent_vars);
490 totals = xmalloc( sizeof(struct metrics) * n_dependent_vars);
491 memset ( totals, 0, sizeof(struct metrics) * n_dependent_vars);
493 if ( lex_match(T_BY))
495 return examine_parse_independent_vars(cmd);
503 /* Parse the clause specifying the factors */
505 examine_parse_independent_vars(struct cmd_examine *cmd)
508 struct factor *sf = xmalloc(sizeof(struct factor));
510 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
515 sf->indep_var[0] = parse_variable();
516 sf->indep_var[1] = 0;
523 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
527 sf->indep_var[1] = parse_variable();
532 sf->fstats = hsh_create(4,
533 (hsh_compare_func *) factor_statistics_compare,
534 (hsh_hash_func *) factor_statistics_hash,
535 (hsh_free_func *) factor_statistics_free,
543 if ( token == '.' || token == '/' )
546 return examine_parse_independent_vars(cmd);
552 void populate_percentiles(struct tab_table *tbl, int col, int row,
553 const struct metrics *m);
555 void populate_descriptives(struct tab_table *t, int col, int row,
556 const struct metrics *fs);
558 void populate_extremes(struct tab_table *t, int col, int row, int n,
559 const struct metrics *m);
561 void populate_summary(struct tab_table *t, int col, int row,
562 const struct metrics *m);
567 static int bad_weight_warn = 1;
570 /* Perform calculations for the sub factors */
572 factor_calc(struct ccase *c, int case_no, double weight, int case_missing)
575 struct factor *fctr = factors;
579 union value indep_vals[2] ;
581 indep_vals[0] = * case_data(c, fctr->indep_var[0]->fv);
583 if ( fctr->indep_var[1] )
584 indep_vals[1] = * case_data(c, fctr->indep_var[1]->fv);
586 indep_vals[1].f = SYSMIS;
588 assert(fctr->fstats);
590 struct factor_statistics **foo = ( struct factor_statistics ** )
591 hsh_probe(fctr->fstats, (void *) &indep_vals);
596 *foo = create_factor_statistics(n_dependent_vars,
600 for ( v = 0 ; v < n_dependent_vars ; ++v )
602 metrics_precalc( &(*foo)->m[v] );
607 for ( v = 0 ; v < n_dependent_vars ; ++v )
609 const struct variable *var = dependent_vars[v];
610 const union value *val = case_data (c, var->fv);
612 if ( value_is_missing(val,var) || case_missing )
615 metrics_calc( &(*foo)->m[v], val, weight, case_no);
629 run_examine(const struct casefile *cf, void *cmd_ )
631 struct casereader *r;
635 const struct cmd_examine *cmd = (struct cmd_examine *) cmd_;
637 /* Make sure we haven't got rubbish left over from a
640 struct factor *fctr = factors;
643 struct factor *next = fctr->next;
645 hsh_clear(fctr->fstats);
654 for ( v = 0 ; v < n_dependent_vars ; ++v )
655 metrics_precalc(&totals[v]);
657 for(r = casefile_get_reader (cf);
658 casereader_read (r, &c) ;
662 const int case_no = casereader_cnum(r);
664 const double weight =
665 dict_get_case_weight(default_dict, &c, &bad_weight_warn);
667 if ( cmd->miss == XMN_LISTWISE )
669 for ( v = 0 ; v < n_dependent_vars ; ++v )
671 const struct variable *var = dependent_vars[v];
672 const union value *val = case_data (&c, var->fv);
674 if ( value_is_missing(val,var))
680 for ( v = 0 ; v < n_dependent_vars ; ++v )
682 const struct variable *var = dependent_vars[v];
683 const union value *val = case_data (&c, var->fv);
685 if ( value_is_missing(val,var) || case_missing )
688 metrics_calc(&totals[v], val, weight, case_no);
692 factor_calc(&c, case_no, weight, case_missing);
697 for ( v = 0 ; v < n_dependent_vars ; ++v)
702 struct hsh_iterator hi;
703 struct factor_statistics *fs;
705 for ( fs = hsh_first(fctr->fstats, &hi);
707 fs = hsh_next(fctr->fstats, &hi))
710 fs->m[v].ptile_hash = list_to_ptile_hash(&percentile_list);
711 fs->m[v].ptile_alg = percentile_algorithm;
712 metrics_postcalc(&fs->m[v]);
718 totals[v].ptile_hash = list_to_ptile_hash(&percentile_list);
719 totals[v].ptile_alg = percentile_algorithm;
720 metrics_postcalc(&totals[v]);
724 /* Make sure that the combination of factors are complete */
729 struct hsh_iterator hi;
730 struct hsh_iterator hi0;
731 struct hsh_iterator hi1;
732 struct factor_statistics *fs;
734 struct hsh_table *idh0=0;
735 struct hsh_table *idh1=0;
739 idh0 = hsh_create(4, (hsh_compare_func *) compare_values,
740 (hsh_hash_func *) hash_value,
743 idh1 = hsh_create(4, (hsh_compare_func *) compare_values,
744 (hsh_hash_func *) hash_value,
748 for ( fs = hsh_first(fctr->fstats, &hi);
750 fs = hsh_next(fctr->fstats, &hi))
752 hsh_insert(idh0,(void *) &fs->id[0]);
753 hsh_insert(idh1,(void *) &fs->id[1]);
756 /* Ensure that the factors combination is complete */
757 for ( val0 = hsh_first(idh0, &hi0);
759 val0 = hsh_next(idh0, &hi0))
761 for ( val1 = hsh_first(idh1, &hi1);
763 val1 = hsh_next(idh1, &hi1))
765 struct factor_statistics **ffs;
770 ffs = (struct factor_statistics **)
771 hsh_probe(fctr->fstats, (void *) &key );
775 (*ffs) = create_factor_statistics (n_dependent_vars,
777 for ( i = 0 ; i < n_dependent_vars ; ++i )
778 metrics_precalc( &(*ffs)->m[i]);
786 fctr->fs = (struct factor_statistics **) hsh_sort_copy(fctr->fstats);
797 for ( i = 0 ; i < n_dependent_vars ; ++i )
798 metrics_destroy(&totals[i]);
805 show_summary(struct variable **dependent_var, int n_dep_var,
806 const struct factor *fctr)
808 static const char *subtitle[]=
816 int heading_columns ;
818 const int heading_rows = 3;
819 struct tab_table *tbl;
827 n_factors = hsh_count(fctr->fstats);
828 n_rows = n_dep_var * n_factors ;
830 if ( fctr->indep_var[1] )
839 n_rows += heading_rows;
841 n_cols = heading_columns + 6;
843 tbl = tab_create (n_cols,n_rows,0);
844 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
846 tab_dim (tbl, tab_natural_dimensions);
848 /* Outline the box */
853 n_cols - 1, n_rows - 1);
855 /* Vertical lines for the data only */
860 n_cols - 1, n_rows - 1);
863 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
864 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
865 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
867 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
870 tab_title (tbl, 0, _("Case Processing Summary"));
873 tab_joint_text(tbl, heading_columns, 0,
875 TAB_CENTER | TAT_TITLE,
878 /* Remove lines ... */
885 for ( i = 0 ; i < 3 ; ++i )
887 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
890 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
893 tab_joint_text(tbl, heading_columns + i*2 , 1,
894 heading_columns + i*2 + 1, 1,
895 TAB_CENTER | TAT_TITLE,
898 tab_box (tbl, -1, -1,
900 heading_columns + i*2, 1,
901 heading_columns + i*2 + 1, 1);
906 /* Titles for the independent variables */
909 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
910 var_to_string(fctr->indep_var[0]));
912 if ( fctr->indep_var[1] )
914 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
915 var_to_string(fctr->indep_var[1]));
921 for ( i = 0 ; i < n_dep_var ; ++i )
925 n_factors = hsh_count(fctr->fstats);
929 tab_hline(tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
932 0, i * n_factors + heading_rows,
933 TAB_LEFT | TAT_TITLE,
934 var_to_string(dependent_var[i])
939 populate_summary(tbl, heading_columns,
940 (i * n_factors) + heading_rows,
946 struct factor_statistics **fs = fctr->fs;
951 static union value prev;
953 if ( 0 != compare_values(&prev, &(*fs)->id[0],
954 fctr->indep_var[0]->width))
958 (i * n_factors ) + count +
960 TAB_LEFT | TAT_TITLE,
961 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
964 if (fctr->indep_var[1] && count > 0 )
965 tab_hline(tbl, TAL_1, 1, n_cols - 1,
966 (i * n_factors ) + count + heading_rows);
973 if ( fctr->indep_var[1])
976 (i * n_factors ) + count +
978 TAB_LEFT | TAT_TITLE,
979 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
982 populate_summary(tbl, heading_columns,
983 (i * n_factors) + count
998 populate_summary(struct tab_table *t, int col, int row,
999 const struct metrics *m)
1002 const double total = m->n + m->n_missing ;
1004 tab_float(t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1005 tab_float(t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1006 tab_float(t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1010 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1011 100.0 * m->n / total );
1013 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1014 100.0 * m->n_missing / total );
1016 /* This seems a bit pointless !!! */
1017 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1018 100.0 * total / total );
1029 show_extremes(struct variable **dependent_var, int n_dep_var,
1030 const struct factor *fctr, int n_extremities)
1033 int heading_columns ;
1035 const int heading_rows = 1;
1036 struct tab_table *tbl;
1043 heading_columns = 2;
1044 n_factors = hsh_count(fctr->fstats);
1046 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1048 if ( fctr->indep_var[1] )
1049 heading_columns = 3;
1053 heading_columns = 1;
1054 n_rows = n_dep_var * 2 * n_extremities;
1057 n_rows += heading_rows;
1059 heading_columns += 2;
1060 n_cols = heading_columns + 2;
1062 tbl = tab_create (n_cols,n_rows,0);
1063 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1065 tab_dim (tbl, tab_natural_dimensions);
1067 /* Outline the box, No internal lines*/
1072 n_cols - 1, n_rows - 1);
1074 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1076 tab_title (tbl, 0, _("Extreme Values"));
1078 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1079 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1083 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1084 var_to_string(fctr->indep_var[0]));
1086 if ( fctr->indep_var[1] )
1087 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1088 var_to_string(fctr->indep_var[1]));
1091 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Value"));
1092 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Case Number"));
1094 for ( i = 0 ; i < n_dep_var ; ++i )
1098 tab_hline(tbl, TAL_1, 0, n_cols -1 ,
1099 i * 2 * n_extremities * n_factors + heading_rows);
1102 i * 2 * n_extremities * n_factors + heading_rows,
1103 TAB_LEFT | TAT_TITLE,
1104 var_to_string(dependent_var[i])
1109 populate_extremes(tbl, heading_columns - 2,
1110 i * 2 * n_extremities * n_factors + heading_rows,
1111 n_extremities, &totals[i]);
1115 struct factor_statistics **fs = fctr->fs;
1120 static union value prev ;
1122 const int row = heading_rows + ( 2 * n_extremities ) *
1123 ( ( i * n_factors ) + count );
1126 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1127 fctr->indep_var[0]->width))
1131 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1135 TAB_LEFT | TAT_TITLE,
1136 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1140 prev = (*fs)->id[0];
1142 if (fctr->indep_var[1] && count > 0 )
1143 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1145 if ( fctr->indep_var[1])
1146 tab_text (tbl, 2, row,
1147 TAB_LEFT | TAT_TITLE,
1148 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1151 populate_extremes(tbl, heading_columns - 2,
1166 /* Fill in the extremities table */
1168 populate_extremes(struct tab_table *t,
1169 int col, int row, int n, const struct metrics *m)
1175 tab_text(t, col, row,
1176 TAB_RIGHT | TAT_TITLE ,
1180 tab_text(t, col, row + n ,
1181 TAB_RIGHT | TAT_TITLE ,
1186 tab_hline(t, TAL_1, col, col + 3, row + n );
1188 for (extremity = 0; extremity < n ; ++extremity )
1191 tab_float(t, col + 1, row + extremity,
1193 extremity + 1, 8, 0);
1197 tab_float(t, col + 1, row + extremity + n,
1199 extremity + 1, 8, 0);
1205 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1208 const struct weighted_value *wv = m->wvp[idx];
1209 struct case_node *cn = wv->case_nos;
1212 for (j = 0 ; j < wv->w ; ++j )
1214 if ( extremity + j >= n )
1217 tab_float(t, col + 3, row + extremity + j + n,
1221 tab_float(t, col + 2, row + extremity + j + n,
1230 extremity += wv->w ;
1235 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1238 const struct weighted_value *wv = m->wvp[idx];
1239 struct case_node *cn = wv->case_nos;
1241 for (j = 0 ; j < wv->w ; ++j )
1243 if ( extremity + j >= n )
1246 tab_float(t, col + 3, row + extremity + j,
1250 tab_float(t, col + 2, row + extremity + j,
1259 extremity += wv->w ;
1264 /* Show the descriptives table */
1266 show_descriptives(struct variable **dependent_var,
1268 struct factor *fctr)
1271 int heading_columns ;
1273 const int n_stat_rows = 13;
1275 const int heading_rows = 1;
1277 struct tab_table *tbl;
1284 heading_columns = 4;
1285 n_factors = hsh_count(fctr->fstats);
1287 n_rows = n_dep_var * n_stat_rows * n_factors;
1289 if ( fctr->indep_var[1] )
1290 heading_columns = 5;
1294 heading_columns = 3;
1295 n_rows = n_dep_var * n_stat_rows;
1298 n_rows += heading_rows;
1300 n_cols = heading_columns + 2;
1303 tbl = tab_create (n_cols, n_rows, 0);
1305 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1307 tab_dim (tbl, tab_natural_dimensions);
1309 /* Outline the box and have no internal lines*/
1314 n_cols - 1, n_rows - 1);
1316 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1318 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1319 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1320 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1322 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Statistic"));
1323 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1325 tab_title (tbl, 0, _("Descriptives"));
1328 for ( i = 0 ; i < n_dep_var ; ++i )
1330 const int row = heading_rows + i * n_stat_rows * n_factors ;
1333 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1336 i * n_stat_rows * n_factors + heading_rows,
1337 TAB_LEFT | TAT_TITLE,
1338 var_to_string(dependent_var[i])
1344 struct factor_statistics **fs = fctr->fs;
1347 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1348 var_to_string(fctr->indep_var[0]));
1351 if ( fctr->indep_var[1])
1352 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1353 var_to_string(fctr->indep_var[1]));
1358 static union value prev ;
1360 const int row = heading_rows + n_stat_rows *
1361 ( ( i * n_factors ) + count );
1364 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1365 fctr->indep_var[0]->width))
1369 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1373 TAB_LEFT | TAT_TITLE,
1374 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1378 prev = (*fs)->id[0];
1380 if (fctr->indep_var[1] && count > 0 )
1381 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1383 if ( fctr->indep_var[1])
1384 tab_text (tbl, 2, row,
1385 TAB_LEFT | TAT_TITLE,
1386 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1389 populate_descriptives(tbl, heading_columns - 2,
1401 populate_descriptives(tbl, heading_columns - 2,
1402 i * n_stat_rows * n_factors + heading_rows,
1414 /* Fill in the descriptives data */
1416 populate_descriptives(struct tab_table *tbl, int col, int row,
1417 const struct metrics *m)
1420 const double t = gsl_cdf_tdist_Qinv(1 - cmd.n_cinterval[0]/100.0/2.0, \
1426 TAB_LEFT | TAT_TITLE,
1429 tab_float (tbl, col + 2,
1435 tab_float (tbl, col + 3,
1444 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1445 _("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1448 tab_text (tbl, col + 1,
1450 TAB_LEFT | TAT_TITLE,
1453 tab_float (tbl, col + 2,
1456 m->mean - t * m->se_mean,
1459 tab_text (tbl, col + 1,
1461 TAB_LEFT | TAT_TITLE,
1465 tab_float (tbl, col + 2,
1468 m->mean + t * m->se_mean,
1473 TAB_LEFT | TAT_TITLE,
1474 _("5% Trimmed Mean"));
1476 tab_float (tbl, col + 2,
1484 TAB_LEFT | TAT_TITLE,
1488 struct percentile *p;
1491 p = hsh_find(m->ptile_hash, &d);
1495 tab_float (tbl, col + 2,
1504 TAB_LEFT | TAT_TITLE,
1507 tab_float (tbl, col + 2,
1516 TAB_LEFT | TAT_TITLE,
1517 _("Std. Deviation"));
1520 tab_float (tbl, col + 2,
1529 TAB_LEFT | TAT_TITLE,
1532 tab_float (tbl, col + 2,
1540 TAB_LEFT | TAT_TITLE,
1543 tab_float (tbl, col + 2,
1552 TAB_LEFT | TAT_TITLE,
1556 tab_float (tbl, col + 2,
1564 TAB_LEFT | TAT_TITLE,
1565 _("Interquartile Range"));
1568 struct percentile *p1;
1569 struct percentile *p2;
1572 p1 = hsh_find(m->ptile_hash, &d);
1575 p2 = hsh_find(m->ptile_hash, &d);
1580 tab_float (tbl, col + 2,
1591 TAB_LEFT | TAT_TITLE,
1595 tab_float (tbl, col + 2,
1601 /* stderr of skewness */
1602 tab_float (tbl, col + 3,
1611 TAB_LEFT | TAT_TITLE,
1615 tab_float (tbl, col + 2,
1621 /* stderr of kurtosis */
1622 tab_float (tbl, col + 3,
1634 box_plot_variables(const struct factor *fctr,
1635 const struct variable **vars, int n_vars,
1636 const struct variable *id)
1640 struct factor_statistics **fs ;
1644 box_plot_group(fctr, vars, n_vars, id);
1648 for ( fs = fctr->fs ; *fs ; ++fs )
1650 double y_min = DBL_MAX;
1651 double y_max = -DBL_MAX;
1654 ch = chart_create();
1656 const char *s = factor_to_string(fctr, *fs, 0 );
1658 chart_write_title(ch, s);
1660 for ( i = 0 ; i < n_vars ; ++i )
1662 y_max = max(y_max, (*fs)->m[i].max);
1663 y_min = min(y_min, (*fs)->m[i].min);
1666 boxplot_draw_yscale(ch, y_max, y_min);
1668 for ( i = 0 ; i < n_vars ; ++i )
1671 const double box_width = (ch->data_right - ch->data_left)
1674 const double box_centre = ( i * 2 + 1) * box_width
1677 boxplot_draw_boxplot(ch,
1678 box_centre, box_width,
1680 var_to_string(vars[i]));
1692 /* Do a box plot, grouping all factors into one plot ;
1693 each dependent variable has its own plot.
1696 box_plot_group(const struct factor *fctr,
1697 const struct variable **vars,
1699 const struct variable *id UNUSED)
1704 for ( i = 0 ; i < n_vars ; ++i )
1706 struct factor_statistics **fs ;
1709 ch = chart_create();
1711 boxplot_draw_yscale(ch, totals[i].max, totals[i].min);
1717 for ( fs = fctr->fs ; *fs ; ++fs )
1720 chart_write_title(ch, _("Boxplot of %s vs. %s"),
1721 var_to_string(vars[i]), var_to_string(fctr->indep_var[0]) );
1723 for ( fs = fctr->fs ; *fs ; ++fs )
1726 const char *s = factor_to_string_concise(fctr, *fs);
1728 const double box_width = (ch->data_right - ch->data_left)
1729 / (n_factors * 2.0 ) ;
1731 const double box_centre = ( f++ * 2 + 1) * box_width
1734 boxplot_draw_boxplot(ch,
1735 box_centre, box_width,
1742 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1743 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1745 chart_write_title(ch, _("Boxplot"));
1747 boxplot_draw_boxplot(ch,
1748 box_centre, box_width,
1750 var_to_string(vars[i]) );
1759 /* Plot the normal and detrended normal plots for m
1760 Label the plots with factorname */
1762 np_plot(const struct metrics *m, const char *factorname)
1765 double yfirst=0, ylast=0;
1768 struct chart *np_chart;
1770 /* Detrended Normal Plot */
1771 struct chart *dnp_chart;
1773 /* The slope and intercept of the ideal normal probability line */
1774 const double slope = 1.0 / m->stddev;
1775 const double intercept = - m->mean / m->stddev;
1777 /* Cowardly refuse to plot an empty data set */
1778 if ( m->n_data == 0 )
1781 np_chart = chart_create();
1782 dnp_chart = chart_create();
1784 if ( !np_chart || ! dnp_chart )
1787 chart_write_title(np_chart, _("Normal Q-Q Plot of %s"), factorname);
1788 chart_write_xlabel(np_chart, _("Observed Value"));
1789 chart_write_ylabel(np_chart, _("Expected Normal"));
1792 chart_write_title(dnp_chart, _("Detrended Normal Q-Q Plot of %s"),
1794 chart_write_xlabel(dnp_chart, _("Observed Value"));
1795 chart_write_ylabel(dnp_chart, _("Dev from Normal"));
1797 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1798 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1802 /* Need to make sure that both the scatter plot and the ideal fit into the
1804 double x_lower = min(m->min, (yfirst - intercept) / slope) ;
1805 double x_upper = max(m->max, (ylast - intercept) / slope) ;
1806 double slack = (x_upper - x_lower) * 0.05 ;
1808 chart_write_xscale(np_chart, x_lower - slack, x_upper + slack, 5);
1810 chart_write_xscale(dnp_chart, m->min, m->max, 5);
1814 chart_write_yscale(np_chart, yfirst, ylast, 5);
1817 /* We have to cache the detrended data, beacause we need to
1818 find its limits before we can plot it */
1820 d_data = xmalloc (m->n_data * sizeof(double));
1821 double d_max = -DBL_MAX;
1822 double d_min = DBL_MAX;
1823 for ( i = 0 ; i < m->n_data; ++i )
1825 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1827 chart_datum(np_chart, 0, m->wvp[i]->v.f, ns);
1829 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1831 if ( d_data[i] < d_min ) d_min = d_data[i];
1832 if ( d_data[i] > d_max ) d_max = d_data[i];
1834 chart_write_yscale(dnp_chart, d_min, d_max, 5);
1836 for ( i = 0 ; i < m->n_data; ++i )
1837 chart_datum(dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1842 chart_line(np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1843 chart_line(dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1845 chart_submit(np_chart);
1846 chart_submit(dnp_chart);
1852 /* Show the percentiles */
1854 show_percentiles(struct variable **dependent_var,
1856 struct factor *fctr)
1858 struct tab_table *tbl;
1864 struct hsh_table *ptiles ;
1866 int n_heading_columns;
1867 const int n_heading_rows = 2;
1868 const int n_stat_rows = 2;
1874 struct factor_statistics **fs = fctr->fs ;
1875 n_heading_columns = 3;
1876 n_factors = hsh_count(fctr->fstats);
1878 ptiles = (*fs)->m[0].ptile_hash;
1880 if ( fctr->indep_var[1] )
1881 n_heading_columns = 4;
1886 n_heading_columns = 2;
1888 ptiles = totals[0].ptile_hash;
1891 n_ptiles = hsh_count(ptiles);
1893 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1895 n_cols = n_heading_columns + n_ptiles ;
1897 tbl = tab_create (n_cols, n_rows, 0);
1899 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1901 tab_dim (tbl, tab_natural_dimensions);
1903 /* Outline the box and have no internal lines*/
1908 n_cols - 1, n_rows - 1);
1910 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
1912 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
1915 tab_title (tbl, 0, _("Percentiles"));
1918 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
1925 n_heading_columns - 1, n_rows - 1);
1931 n_heading_columns, n_heading_rows - 1,
1932 n_cols - 1, n_rows - 1);
1934 tab_joint_text(tbl, n_heading_columns + 1, 0,
1936 TAB_CENTER | TAT_TITLE ,
1941 /* Put in the percentile break points as headings */
1943 struct percentile **p = (struct percentile **) hsh_sort(ptiles);
1948 tab_float(tbl, n_heading_columns + i++ , 1,
1957 for ( i = 0 ; i < n_dep_var ; ++i )
1959 const int n_stat_rows = 2;
1960 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
1963 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1966 i * n_stat_rows * n_factors + n_heading_rows,
1967 TAB_LEFT | TAT_TITLE,
1968 var_to_string(dependent_var[i])
1973 struct factor_statistics **fs = fctr->fs;
1976 tab_text (tbl, 1, n_heading_rows - 1,
1977 TAB_CENTER | TAT_TITLE,
1978 var_to_string(fctr->indep_var[0]));
1981 if ( fctr->indep_var[1])
1982 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
1983 var_to_string(fctr->indep_var[1]));
1988 static union value prev ;
1990 const int row = n_heading_rows + n_stat_rows *
1991 ( ( i * n_factors ) + count );
1994 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1995 fctr->indep_var[0]->width))
1999 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2003 TAB_LEFT | TAT_TITLE,
2004 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
2010 prev = (*fs)->id[0];
2012 if (fctr->indep_var[1] && count > 0 )
2013 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
2015 if ( fctr->indep_var[1])
2016 tab_text (tbl, 2, row,
2017 TAB_LEFT | TAT_TITLE,
2018 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
2022 populate_percentiles(tbl, n_heading_columns - 1,
2034 populate_percentiles(tbl, n_heading_columns - 1,
2035 i * n_stat_rows * n_factors + n_heading_rows,
2052 populate_percentiles(struct tab_table *tbl, int col, int row,
2053 const struct metrics *m)
2057 struct percentile **p = (struct percentile **) hsh_sort(m->ptile_hash);
2061 TAB_LEFT | TAT_TITLE,
2062 _("Tukey\'s Hinges")
2067 TAB_LEFT | TAT_TITLE,
2068 ptile_alg_desc[m->ptile_alg]
2075 tab_float(tbl, col + i + 1 , row,
2078 if ( (*p)->p == 25 )
2079 tab_float(tbl, col + i + 1 , row + 1,
2083 if ( (*p)->p == 50 )
2084 tab_float(tbl, col + i + 1 , row + 1,
2088 if ( (*p)->p == 75 )
2089 tab_float(tbl, col + i + 1 , row + 1,
2104 factor_to_string(const struct factor *fctr,
2105 struct factor_statistics *fs,
2106 const struct variable *var)
2109 static char buf1[100];
2115 sprintf(buf1, "%s (",var_to_string(var) );
2118 snprintf(buf2, 100, "%s = %s",
2119 var_to_string(fctr->indep_var[0]),
2120 value_to_string(&fs->id[0],fctr->indep_var[0]));
2124 if ( fctr->indep_var[1] )
2126 sprintf(buf2, "; %s = %s)",
2127 var_to_string(fctr->indep_var[1]),
2128 value_to_string(&fs->id[1],
2129 fctr->indep_var[1]));
2144 factor_to_string_concise(const struct factor *fctr,
2145 struct factor_statistics *fs)
2149 static char buf[100];
2153 snprintf(buf, 100, "%s",
2154 value_to_string(&fs->id[0], fctr->indep_var[0]));
2156 if ( fctr->indep_var[1] )
2158 sprintf(buf2, ",%s)", value_to_string(&fs->id[1], fctr->indep_var[1]) );