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);
218 if ( dependent_vars )
219 free (dependent_vars);
222 struct factor *f = factors ;
225 struct factor *ff = f;
229 hsh_destroy ( ff->fstats ) ;
234 subc_list_double_destroy(&percentile_list);
241 /* Show all the appropriate tables */
247 /* Show totals if appropriate */
248 if ( ! cmd.sbc_nototal || factors == 0 )
250 show_summary(dependent_vars, n_dependent_vars, 0);
252 if ( cmd.sbc_statistics )
254 if ( cmd.a_statistics[XMN_ST_EXTREME])
255 show_extremes(dependent_vars, n_dependent_vars, 0, cmd.st_n);
257 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
258 show_descriptives(dependent_vars, n_dependent_vars, 0);
261 if ( sbc_percentile )
262 show_percentiles(dependent_vars, n_dependent_vars, 0);
267 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
269 for ( v = 0 ; v < n_dependent_vars; ++v )
270 np_plot(&totals[v], var_to_string(dependent_vars[v]));
273 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
275 if ( cmd.cmp == XMN_GROUPS )
277 box_plot_group(0, dependent_vars, n_dependent_vars,
281 box_plot_variables(0, dependent_vars, n_dependent_vars,
285 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
287 for ( v = 0 ; v < n_dependent_vars; ++v )
289 struct normal_curve normal;
291 normal.N = totals[v].n;
292 normal.mean = totals[v].mean;
293 normal.stddev = totals[v].stddev;
295 histogram_plot(totals[v].histogram,
296 var_to_string(dependent_vars[v]),
306 /* Show grouped statistics as appropriate */
310 show_summary(dependent_vars, n_dependent_vars, fctr);
312 if ( cmd.sbc_statistics )
314 if ( cmd.a_statistics[XMN_ST_EXTREME])
315 show_extremes(dependent_vars, n_dependent_vars, fctr, cmd.st_n);
317 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
318 show_descriptives(dependent_vars, n_dependent_vars, fctr);
321 if ( sbc_percentile )
322 show_percentiles(dependent_vars, n_dependent_vars, fctr);
329 struct factor_statistics **fs = fctr->fs ;
331 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
333 if ( cmd.cmp == XMN_VARIABLES )
334 box_plot_variables(fctr, dependent_vars, n_dependent_vars,
337 box_plot_group(fctr, dependent_vars, n_dependent_vars,
341 for ( v = 0 ; v < n_dependent_vars; ++v )
344 for ( fs = fctr->fs ; *fs ; ++fs )
346 const char *s = factor_to_string(fctr, *fs, dependent_vars[v]);
348 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
349 np_plot(&(*fs)->m[v], s);
351 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
353 struct normal_curve normal;
355 normal.N = (*fs)->m[v].n;
356 normal.mean = (*fs)->m[v].mean;
357 normal.stddev = (*fs)->m[v].stddev;
359 histogram_plot((*fs)->m[v].histogram,
363 } /* for ( fs .... */
365 } /* for ( v = 0 ..... */
375 /* Create a hash table of percentiles and their values from the list of
377 static struct hsh_table *
378 list_to_ptile_hash(const subc_list_double *l)
382 struct hsh_table *h ;
384 h = hsh_create(subc_list_double_count(l),
385 (hsh_compare_func *) ptile_compare,
386 (hsh_hash_func *) ptile_hash,
387 (hsh_free_func *) free,
391 for ( i = 0 ; i < subc_list_double_count(l) ; ++i )
393 struct percentile *p = xmalloc (sizeof (struct percentile));
395 p->p = subc_list_double_at(l,i);
406 /* Parse the PERCENTILES subcommand */
408 xmn_custom_percentiles(struct cmd_examine *p UNUSED)
416 while ( lex_is_number() )
418 subc_list_double_push(&percentile_list,lex_number());
428 if ( lex_match_id("HAVERAGE"))
429 percentile_algorithm = PC_HAVERAGE;
431 else if ( lex_match_id("WAVERAGE"))
432 percentile_algorithm = PC_WAVERAGE;
434 else if ( lex_match_id("ROUND"))
435 percentile_algorithm = PC_ROUND;
437 else if ( lex_match_id("EMPIRICAL"))
438 percentile_algorithm = PC_EMPIRICAL;
440 else if ( lex_match_id("AEMPIRICAL"))
441 percentile_algorithm = PC_AEMPIRICAL;
443 else if ( lex_match_id("NONE"))
444 percentile_algorithm = PC_NONE;
447 if ( 0 == subc_list_double_count(&percentile_list))
449 subc_list_double_push(&percentile_list, 5);
450 subc_list_double_push(&percentile_list, 10);
451 subc_list_double_push(&percentile_list, 25);
452 subc_list_double_push(&percentile_list, 50);
453 subc_list_double_push(&percentile_list, 75);
454 subc_list_double_push(&percentile_list, 90);
455 subc_list_double_push(&percentile_list, 95);
461 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
463 xmn_custom_total(struct cmd_examine *p)
465 if ( p->sbc_nototal )
467 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
475 xmn_custom_nototal(struct cmd_examine *p)
479 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
488 /* Parser for the variables sub command
489 Returns 1 on success */
491 xmn_custom_variables(struct cmd_examine *cmd )
495 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
501 if (!parse_variables (default_dict, &dependent_vars, &n_dependent_vars,
502 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
504 free (dependent_vars);
508 assert(n_dependent_vars);
510 totals = xmalloc( sizeof(struct metrics) * n_dependent_vars);
512 if ( lex_match(T_BY))
515 success = examine_parse_independent_vars(cmd);
516 if ( success != 1 ) {
517 free (dependent_vars);
528 /* Parse the clause specifying the factors */
530 examine_parse_independent_vars(struct cmd_examine *cmd)
533 struct factor *sf = xmalloc(sizeof(struct factor));
535 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
543 sf->indep_var[0] = parse_variable();
544 sf->indep_var[1] = 0;
551 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
558 sf->indep_var[1] = parse_variable();
563 sf->fstats = hsh_create(4,
564 (hsh_compare_func *) factor_statistics_compare,
565 (hsh_hash_func *) factor_statistics_hash,
566 (hsh_free_func *) factor_statistics_free,
574 if ( token == '.' || token == '/' )
577 success = examine_parse_independent_vars(cmd);
588 void populate_percentiles(struct tab_table *tbl, int col, int row,
589 const struct metrics *m);
591 void populate_descriptives(struct tab_table *t, int col, int row,
592 const struct metrics *fs);
594 void populate_extremes(struct tab_table *t, int col, int row, int n,
595 const struct metrics *m);
597 void populate_summary(struct tab_table *t, int col, int row,
598 const struct metrics *m);
603 static int bad_weight_warn = 1;
606 /* Perform calculations for the sub factors */
608 factor_calc(struct ccase *c, int case_no, double weight, int case_missing)
611 struct factor *fctr = factors;
615 union value indep_vals[2] ;
617 indep_vals[0] = * case_data(c, fctr->indep_var[0]->fv);
619 if ( fctr->indep_var[1] )
620 indep_vals[1] = * case_data(c, fctr->indep_var[1]->fv);
622 indep_vals[1].f = SYSMIS;
624 assert(fctr->fstats);
626 struct factor_statistics **foo = ( struct factor_statistics ** )
627 hsh_probe(fctr->fstats, (void *) &indep_vals);
632 *foo = create_factor_statistics(n_dependent_vars,
636 for ( v = 0 ; v < n_dependent_vars ; ++v )
638 metrics_precalc( &(*foo)->m[v] );
643 for ( v = 0 ; v < n_dependent_vars ; ++v )
645 const struct variable *var = dependent_vars[v];
646 const union value *val = case_data (c, var->fv);
648 if ( value_is_missing(val,var) || case_missing )
651 metrics_calc( &(*foo)->m[v], val, weight, case_no);
665 run_examine(const struct casefile *cf, void *cmd_ )
667 struct casereader *r;
671 const struct cmd_examine *cmd = (struct cmd_examine *) cmd_;
673 /* Make sure we haven't got rubbish left over from a
676 struct factor *fctr = factors;
679 struct factor *next = fctr->next;
681 hsh_clear(fctr->fstats);
690 for ( v = 0 ; v < n_dependent_vars ; ++v )
691 metrics_precalc(&totals[v]);
693 for(r = casefile_get_reader (cf);
694 casereader_read (r, &c) ;
698 const int case_no = casereader_cnum(r);
700 const double weight =
701 dict_get_case_weight(default_dict, &c, &bad_weight_warn);
703 if ( cmd->miss == XMN_LISTWISE )
705 for ( v = 0 ; v < n_dependent_vars ; ++v )
707 const struct variable *var = dependent_vars[v];
708 const union value *val = case_data (&c, var->fv);
710 if ( value_is_missing(val,var))
716 for ( v = 0 ; v < n_dependent_vars ; ++v )
718 const struct variable *var = dependent_vars[v];
719 const union value *val = case_data (&c, var->fv);
721 if ( value_is_missing(val,var) || case_missing )
724 metrics_calc(&totals[v], val, weight, case_no);
728 factor_calc(&c, case_no, weight, case_missing);
733 for ( v = 0 ; v < n_dependent_vars ; ++v)
738 struct hsh_iterator hi;
739 struct factor_statistics *fs;
741 for ( fs = hsh_first(fctr->fstats, &hi);
743 fs = hsh_next(fctr->fstats, &hi))
746 fs->m[v].ptile_hash = list_to_ptile_hash(&percentile_list);
747 fs->m[v].ptile_alg = percentile_algorithm;
748 metrics_postcalc(&fs->m[v]);
754 totals[v].ptile_hash = list_to_ptile_hash(&percentile_list);
755 totals[v].ptile_alg = percentile_algorithm;
756 metrics_postcalc(&totals[v]);
760 /* Make sure that the combination of factors are complete */
765 struct hsh_iterator hi;
766 struct hsh_iterator hi0;
767 struct hsh_iterator hi1;
768 struct factor_statistics *fs;
770 struct hsh_table *idh0=0;
771 struct hsh_table *idh1=0;
775 idh0 = hsh_create(4, (hsh_compare_func *) compare_values,
776 (hsh_hash_func *) hash_value,
779 idh1 = hsh_create(4, (hsh_compare_func *) compare_values,
780 (hsh_hash_func *) hash_value,
784 for ( fs = hsh_first(fctr->fstats, &hi);
786 fs = hsh_next(fctr->fstats, &hi))
788 hsh_insert(idh0,(void *) &fs->id[0]);
789 hsh_insert(idh1,(void *) &fs->id[1]);
792 /* Ensure that the factors combination is complete */
793 for ( val0 = hsh_first(idh0, &hi0);
795 val0 = hsh_next(idh0, &hi0))
797 for ( val1 = hsh_first(idh1, &hi1);
799 val1 = hsh_next(idh1, &hi1))
801 struct factor_statistics **ffs;
806 ffs = (struct factor_statistics **)
807 hsh_probe(fctr->fstats, (void *) &key );
811 (*ffs) = create_factor_statistics (n_dependent_vars,
813 for ( i = 0 ; i < n_dependent_vars ; ++i )
814 metrics_precalc( &(*ffs)->m[i]);
822 fctr->fs = (struct factor_statistics **) hsh_sort_copy(fctr->fstats);
833 for ( i = 0 ; i < n_dependent_vars ; ++i )
835 metrics_destroy(&totals[i]);
843 show_summary(struct variable **dependent_var, int n_dep_var,
844 const struct factor *fctr)
846 static const char *subtitle[]=
854 int heading_columns ;
856 const int heading_rows = 3;
857 struct tab_table *tbl;
865 n_factors = hsh_count(fctr->fstats);
866 n_rows = n_dep_var * n_factors ;
868 if ( fctr->indep_var[1] )
877 n_rows += heading_rows;
879 n_cols = heading_columns + 6;
881 tbl = tab_create (n_cols,n_rows,0);
882 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
884 tab_dim (tbl, tab_natural_dimensions);
886 /* Outline the box */
891 n_cols - 1, n_rows - 1);
893 /* Vertical lines for the data only */
898 n_cols - 1, n_rows - 1);
901 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
902 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
903 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
905 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
908 tab_title (tbl, 0, _("Case Processing Summary"));
911 tab_joint_text(tbl, heading_columns, 0,
913 TAB_CENTER | TAT_TITLE,
916 /* Remove lines ... */
923 for ( i = 0 ; i < 3 ; ++i )
925 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
928 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
931 tab_joint_text(tbl, heading_columns + i*2 , 1,
932 heading_columns + i*2 + 1, 1,
933 TAB_CENTER | TAT_TITLE,
936 tab_box (tbl, -1, -1,
938 heading_columns + i*2, 1,
939 heading_columns + i*2 + 1, 1);
944 /* Titles for the independent variables */
947 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
948 var_to_string(fctr->indep_var[0]));
950 if ( fctr->indep_var[1] )
952 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
953 var_to_string(fctr->indep_var[1]));
959 for ( i = 0 ; i < n_dep_var ; ++i )
963 n_factors = hsh_count(fctr->fstats);
967 tab_hline(tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
970 0, i * n_factors + heading_rows,
971 TAB_LEFT | TAT_TITLE,
972 var_to_string(dependent_var[i])
977 populate_summary(tbl, heading_columns,
978 (i * n_factors) + heading_rows,
984 struct factor_statistics **fs = fctr->fs;
989 static union value prev;
991 if ( 0 != compare_values(&prev, &(*fs)->id[0],
992 fctr->indep_var[0]->width))
996 (i * n_factors ) + count +
998 TAB_LEFT | TAT_TITLE,
999 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1002 if (fctr->indep_var[1] && count > 0 )
1003 tab_hline(tbl, TAL_1, 1, n_cols - 1,
1004 (i * n_factors ) + count + heading_rows);
1008 prev = (*fs)->id[0];
1011 if ( fctr->indep_var[1])
1014 (i * n_factors ) + count +
1016 TAB_LEFT | TAT_TITLE,
1017 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1020 populate_summary(tbl, heading_columns,
1021 (i * n_factors) + count
1036 populate_summary(struct tab_table *t, int col, int row,
1037 const struct metrics *m)
1040 const double total = m->n + m->n_missing ;
1042 tab_float(t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1043 tab_float(t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1044 tab_float(t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1048 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1049 100.0 * m->n / total );
1051 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1052 100.0 * m->n_missing / total );
1054 /* This seems a bit pointless !!! */
1055 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1056 100.0 * total / total );
1067 show_extremes(struct variable **dependent_var, int n_dep_var,
1068 const struct factor *fctr, int n_extremities)
1071 int heading_columns ;
1073 const int heading_rows = 1;
1074 struct tab_table *tbl;
1081 heading_columns = 2;
1082 n_factors = hsh_count(fctr->fstats);
1084 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1086 if ( fctr->indep_var[1] )
1087 heading_columns = 3;
1091 heading_columns = 1;
1092 n_rows = n_dep_var * 2 * n_extremities;
1095 n_rows += heading_rows;
1097 heading_columns += 2;
1098 n_cols = heading_columns + 2;
1100 tbl = tab_create (n_cols,n_rows,0);
1101 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1103 tab_dim (tbl, tab_natural_dimensions);
1105 /* Outline the box, No internal lines*/
1110 n_cols - 1, n_rows - 1);
1112 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1114 tab_title (tbl, 0, _("Extreme Values"));
1116 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1117 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1121 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1122 var_to_string(fctr->indep_var[0]));
1124 if ( fctr->indep_var[1] )
1125 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1126 var_to_string(fctr->indep_var[1]));
1129 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Value"));
1130 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Case Number"));
1132 for ( i = 0 ; i < n_dep_var ; ++i )
1136 tab_hline(tbl, TAL_1, 0, n_cols -1 ,
1137 i * 2 * n_extremities * n_factors + heading_rows);
1140 i * 2 * n_extremities * n_factors + heading_rows,
1141 TAB_LEFT | TAT_TITLE,
1142 var_to_string(dependent_var[i])
1147 populate_extremes(tbl, heading_columns - 2,
1148 i * 2 * n_extremities * n_factors + heading_rows,
1149 n_extremities, &totals[i]);
1153 struct factor_statistics **fs = fctr->fs;
1158 static union value prev ;
1160 const int row = heading_rows + ( 2 * n_extremities ) *
1161 ( ( i * n_factors ) + count );
1164 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1165 fctr->indep_var[0]->width))
1169 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1173 TAB_LEFT | TAT_TITLE,
1174 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1178 prev = (*fs)->id[0];
1180 if (fctr->indep_var[1] && count > 0 )
1181 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1183 if ( fctr->indep_var[1])
1184 tab_text (tbl, 2, row,
1185 TAB_LEFT | TAT_TITLE,
1186 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1189 populate_extremes(tbl, heading_columns - 2,
1204 /* Fill in the extremities table */
1206 populate_extremes(struct tab_table *t,
1207 int col, int row, int n, const struct metrics *m)
1213 tab_text(t, col, row,
1214 TAB_RIGHT | TAT_TITLE ,
1218 tab_text(t, col, row + n ,
1219 TAB_RIGHT | TAT_TITLE ,
1224 tab_hline(t, TAL_1, col, col + 3, row + n );
1226 for (extremity = 0; extremity < n ; ++extremity )
1229 tab_float(t, col + 1, row + extremity,
1231 extremity + 1, 8, 0);
1235 tab_float(t, col + 1, row + extremity + n,
1237 extremity + 1, 8, 0);
1243 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1246 const struct weighted_value *wv = m->wvp[idx];
1247 struct case_node *cn = wv->case_nos;
1250 for (j = 0 ; j < wv->w ; ++j )
1252 if ( extremity + j >= n )
1255 tab_float(t, col + 3, row + extremity + j + n,
1259 tab_float(t, col + 2, row + extremity + j + n,
1268 extremity += wv->w ;
1273 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1276 const struct weighted_value *wv = m->wvp[idx];
1277 struct case_node *cn = wv->case_nos;
1279 for (j = 0 ; j < wv->w ; ++j )
1281 if ( extremity + j >= n )
1284 tab_float(t, col + 3, row + extremity + j,
1288 tab_float(t, col + 2, row + extremity + j,
1297 extremity += wv->w ;
1302 /* Show the descriptives table */
1304 show_descriptives(struct variable **dependent_var,
1306 struct factor *fctr)
1309 int heading_columns ;
1311 const int n_stat_rows = 13;
1313 const int heading_rows = 1;
1315 struct tab_table *tbl;
1322 heading_columns = 4;
1323 n_factors = hsh_count(fctr->fstats);
1325 n_rows = n_dep_var * n_stat_rows * n_factors;
1327 if ( fctr->indep_var[1] )
1328 heading_columns = 5;
1332 heading_columns = 3;
1333 n_rows = n_dep_var * n_stat_rows;
1336 n_rows += heading_rows;
1338 n_cols = heading_columns + 2;
1341 tbl = tab_create (n_cols, n_rows, 0);
1343 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1345 tab_dim (tbl, tab_natural_dimensions);
1347 /* Outline the box and have no internal lines*/
1352 n_cols - 1, n_rows - 1);
1354 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1356 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1357 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1358 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1360 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Statistic"));
1361 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1363 tab_title (tbl, 0, _("Descriptives"));
1366 for ( i = 0 ; i < n_dep_var ; ++i )
1368 const int row = heading_rows + i * n_stat_rows * n_factors ;
1371 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1374 i * n_stat_rows * n_factors + heading_rows,
1375 TAB_LEFT | TAT_TITLE,
1376 var_to_string(dependent_var[i])
1382 struct factor_statistics **fs = fctr->fs;
1385 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1386 var_to_string(fctr->indep_var[0]));
1389 if ( fctr->indep_var[1])
1390 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1391 var_to_string(fctr->indep_var[1]));
1396 static union value prev ;
1398 const int row = heading_rows + n_stat_rows *
1399 ( ( i * n_factors ) + count );
1402 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1403 fctr->indep_var[0]->width))
1407 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1411 TAB_LEFT | TAT_TITLE,
1412 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1416 prev = (*fs)->id[0];
1418 if (fctr->indep_var[1] && count > 0 )
1419 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1421 if ( fctr->indep_var[1])
1422 tab_text (tbl, 2, row,
1423 TAB_LEFT | TAT_TITLE,
1424 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1427 populate_descriptives(tbl, heading_columns - 2,
1439 populate_descriptives(tbl, heading_columns - 2,
1440 i * n_stat_rows * n_factors + heading_rows,
1452 /* Fill in the descriptives data */
1454 populate_descriptives(struct tab_table *tbl, int col, int row,
1455 const struct metrics *m)
1458 const double t = gsl_cdf_tdist_Qinv(1 - cmd.n_cinterval[0]/100.0/2.0, \
1464 TAB_LEFT | TAT_TITLE,
1467 tab_float (tbl, col + 2,
1473 tab_float (tbl, col + 3,
1482 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1483 _("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1486 tab_text (tbl, col + 1,
1488 TAB_LEFT | TAT_TITLE,
1491 tab_float (tbl, col + 2,
1494 m->mean - t * m->se_mean,
1497 tab_text (tbl, col + 1,
1499 TAB_LEFT | TAT_TITLE,
1503 tab_float (tbl, col + 2,
1506 m->mean + t * m->se_mean,
1511 TAB_LEFT | TAT_TITLE,
1512 _("5% Trimmed Mean"));
1514 tab_float (tbl, col + 2,
1522 TAB_LEFT | TAT_TITLE,
1526 struct percentile *p;
1529 p = hsh_find(m->ptile_hash, &d);
1534 tab_float (tbl, col + 2,
1544 TAB_LEFT | TAT_TITLE,
1547 tab_float (tbl, col + 2,
1556 TAB_LEFT | TAT_TITLE,
1557 _("Std. Deviation"));
1560 tab_float (tbl, col + 2,
1569 TAB_LEFT | TAT_TITLE,
1572 tab_float (tbl, col + 2,
1580 TAB_LEFT | TAT_TITLE,
1583 tab_float (tbl, col + 2,
1592 TAB_LEFT | TAT_TITLE,
1596 tab_float (tbl, col + 2,
1604 TAB_LEFT | TAT_TITLE,
1605 _("Interquartile Range"));
1608 struct percentile *p1;
1609 struct percentile *p2;
1612 p1 = hsh_find(m->ptile_hash, &d);
1615 p2 = hsh_find(m->ptile_hash, &d);
1620 tab_float (tbl, col + 2,
1631 TAB_LEFT | TAT_TITLE,
1635 tab_float (tbl, col + 2,
1641 /* stderr of skewness */
1642 tab_float (tbl, col + 3,
1651 TAB_LEFT | TAT_TITLE,
1655 tab_float (tbl, col + 2,
1661 /* stderr of kurtosis */
1662 tab_float (tbl, col + 3,
1674 box_plot_variables(const struct factor *fctr,
1675 const struct variable **vars, int n_vars,
1676 const struct variable *id)
1680 struct factor_statistics **fs ;
1684 box_plot_group(fctr, vars, n_vars, id);
1688 for ( fs = fctr->fs ; *fs ; ++fs )
1690 double y_min = DBL_MAX;
1691 double y_max = -DBL_MAX;
1694 ch = chart_create();
1696 const char *s = factor_to_string(fctr, *fs, 0 );
1698 chart_write_title(ch, s);
1700 for ( i = 0 ; i < n_vars ; ++i )
1702 y_max = max(y_max, (*fs)->m[i].max);
1703 y_min = min(y_min, (*fs)->m[i].min);
1706 boxplot_draw_yscale(ch, y_max, y_min);
1708 for ( i = 0 ; i < n_vars ; ++i )
1711 const double box_width = (ch->data_right - ch->data_left)
1714 const double box_centre = ( i * 2 + 1) * box_width
1717 boxplot_draw_boxplot(ch,
1718 box_centre, box_width,
1720 var_to_string(vars[i]));
1732 /* Do a box plot, grouping all factors into one plot ;
1733 each dependent variable has its own plot.
1736 box_plot_group(const struct factor *fctr,
1737 const struct variable **vars,
1739 const struct variable *id UNUSED)
1744 for ( i = 0 ; i < n_vars ; ++i )
1746 struct factor_statistics **fs ;
1749 ch = chart_create();
1751 boxplot_draw_yscale(ch, totals[i].max, totals[i].min);
1757 for ( fs = fctr->fs ; *fs ; ++fs )
1760 chart_write_title(ch, _("Boxplot of %s vs. %s"),
1761 var_to_string(vars[i]), var_to_string(fctr->indep_var[0]) );
1763 for ( fs = fctr->fs ; *fs ; ++fs )
1766 const char *s = factor_to_string_concise(fctr, *fs);
1768 const double box_width = (ch->data_right - ch->data_left)
1769 / (n_factors * 2.0 ) ;
1771 const double box_centre = ( f++ * 2 + 1) * box_width
1774 boxplot_draw_boxplot(ch,
1775 box_centre, box_width,
1782 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1783 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1785 chart_write_title(ch, _("Boxplot"));
1787 boxplot_draw_boxplot(ch,
1788 box_centre, box_width,
1790 var_to_string(vars[i]) );
1799 /* Plot the normal and detrended normal plots for m
1800 Label the plots with factorname */
1802 np_plot(const struct metrics *m, const char *factorname)
1805 double yfirst=0, ylast=0;
1808 struct chart *np_chart;
1810 /* Detrended Normal Plot */
1811 struct chart *dnp_chart;
1813 /* The slope and intercept of the ideal normal probability line */
1814 const double slope = 1.0 / m->stddev;
1815 const double intercept = - m->mean / m->stddev;
1817 /* Cowardly refuse to plot an empty data set */
1818 if ( m->n_data == 0 )
1821 np_chart = chart_create();
1822 dnp_chart = chart_create();
1824 if ( !np_chart || ! dnp_chart )
1827 chart_write_title(np_chart, _("Normal Q-Q Plot of %s"), factorname);
1828 chart_write_xlabel(np_chart, _("Observed Value"));
1829 chart_write_ylabel(np_chart, _("Expected Normal"));
1832 chart_write_title(dnp_chart, _("Detrended Normal Q-Q Plot of %s"),
1834 chart_write_xlabel(dnp_chart, _("Observed Value"));
1835 chart_write_ylabel(dnp_chart, _("Dev from Normal"));
1837 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1838 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1842 /* Need to make sure that both the scatter plot and the ideal fit into the
1844 double x_lower = min(m->min, (yfirst - intercept) / slope) ;
1845 double x_upper = max(m->max, (ylast - intercept) / slope) ;
1846 double slack = (x_upper - x_lower) * 0.05 ;
1848 chart_write_xscale(np_chart, x_lower - slack, x_upper + slack, 5);
1850 chart_write_xscale(dnp_chart, m->min, m->max, 5);
1854 chart_write_yscale(np_chart, yfirst, ylast, 5);
1857 /* We have to cache the detrended data, beacause we need to
1858 find its limits before we can plot it */
1860 d_data = xmalloc (m->n_data * sizeof(double));
1861 double d_max = -DBL_MAX;
1862 double d_min = DBL_MAX;
1863 for ( i = 0 ; i < m->n_data; ++i )
1865 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1867 chart_datum(np_chart, 0, m->wvp[i]->v.f, ns);
1869 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1871 if ( d_data[i] < d_min ) d_min = d_data[i];
1872 if ( d_data[i] > d_max ) d_max = d_data[i];
1874 chart_write_yscale(dnp_chart, d_min, d_max, 5);
1876 for ( i = 0 ; i < m->n_data; ++i )
1877 chart_datum(dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1882 chart_line(np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1883 chart_line(dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1885 chart_submit(np_chart);
1886 chart_submit(dnp_chart);
1892 /* Show the percentiles */
1894 show_percentiles(struct variable **dependent_var,
1896 struct factor *fctr)
1898 struct tab_table *tbl;
1904 struct hsh_table *ptiles ;
1906 int n_heading_columns;
1907 const int n_heading_rows = 2;
1908 const int n_stat_rows = 2;
1914 struct factor_statistics **fs = fctr->fs ;
1915 n_heading_columns = 3;
1916 n_factors = hsh_count(fctr->fstats);
1918 ptiles = (*fs)->m[0].ptile_hash;
1920 if ( fctr->indep_var[1] )
1921 n_heading_columns = 4;
1926 n_heading_columns = 2;
1928 ptiles = totals[0].ptile_hash;
1931 n_ptiles = hsh_count(ptiles);
1933 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1935 n_cols = n_heading_columns + n_ptiles ;
1937 tbl = tab_create (n_cols, n_rows, 0);
1939 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1941 tab_dim (tbl, tab_natural_dimensions);
1943 /* Outline the box and have no internal lines*/
1948 n_cols - 1, n_rows - 1);
1950 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
1952 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
1955 tab_title (tbl, 0, _("Percentiles"));
1958 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
1965 n_heading_columns - 1, n_rows - 1);
1971 n_heading_columns, n_heading_rows - 1,
1972 n_cols - 1, n_rows - 1);
1974 tab_joint_text(tbl, n_heading_columns + 1, 0,
1976 TAB_CENTER | TAT_TITLE ,
1981 /* Put in the percentile break points as headings */
1983 struct percentile **p = (struct percentile **) hsh_sort(ptiles);
1988 tab_float(tbl, n_heading_columns + i++ , 1,
1997 for ( i = 0 ; i < n_dep_var ; ++i )
1999 const int n_stat_rows = 2;
2000 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
2003 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
2006 i * n_stat_rows * n_factors + n_heading_rows,
2007 TAB_LEFT | TAT_TITLE,
2008 var_to_string(dependent_var[i])
2013 struct factor_statistics **fs = fctr->fs;
2016 tab_text (tbl, 1, n_heading_rows - 1,
2017 TAB_CENTER | TAT_TITLE,
2018 var_to_string(fctr->indep_var[0]));
2021 if ( fctr->indep_var[1])
2022 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2023 var_to_string(fctr->indep_var[1]));
2028 static union value prev ;
2030 const int row = n_heading_rows + n_stat_rows *
2031 ( ( i * n_factors ) + count );
2034 if ( 0 != compare_values(&prev, &(*fs)->id[0],
2035 fctr->indep_var[0]->width))
2039 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2043 TAB_LEFT | TAT_TITLE,
2044 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
2050 prev = (*fs)->id[0];
2052 if (fctr->indep_var[1] && count > 0 )
2053 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
2055 if ( fctr->indep_var[1])
2056 tab_text (tbl, 2, row,
2057 TAB_LEFT | TAT_TITLE,
2058 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
2062 populate_percentiles(tbl, n_heading_columns - 1,
2074 populate_percentiles(tbl, n_heading_columns - 1,
2075 i * n_stat_rows * n_factors + n_heading_rows,
2092 populate_percentiles(struct tab_table *tbl, int col, int row,
2093 const struct metrics *m)
2097 struct percentile **p = (struct percentile **) hsh_sort(m->ptile_hash);
2101 TAB_LEFT | TAT_TITLE,
2102 _("Tukey\'s Hinges")
2107 TAB_LEFT | TAT_TITLE,
2108 ptile_alg_desc[m->ptile_alg]
2115 tab_float(tbl, col + i + 1 , row,
2118 if ( (*p)->p == 25 )
2119 tab_float(tbl, col + i + 1 , row + 1,
2123 if ( (*p)->p == 50 )
2124 tab_float(tbl, col + i + 1 , row + 1,
2128 if ( (*p)->p == 75 )
2129 tab_float(tbl, col + i + 1 , row + 1,
2144 factor_to_string(const struct factor *fctr,
2145 struct factor_statistics *fs,
2146 const struct variable *var)
2149 static char buf1[100];
2155 sprintf(buf1, "%s (",var_to_string(var) );
2158 snprintf(buf2, 100, "%s = %s",
2159 var_to_string(fctr->indep_var[0]),
2160 value_to_string(&fs->id[0],fctr->indep_var[0]));
2164 if ( fctr->indep_var[1] )
2166 sprintf(buf2, "; %s = %s)",
2167 var_to_string(fctr->indep_var[1]),
2168 value_to_string(&fs->id[1],
2169 fctr->indep_var[1]));
2184 factor_to_string_concise(const struct factor *fctr,
2185 struct factor_statistics *fs)
2189 static char buf[100];
2193 snprintf(buf, 100, "%s",
2194 value_to_string(&fs->id[0], fctr->indep_var[0]));
2196 if ( fctr->indep_var[1] )
2198 sprintf(buf2, ",%s)", value_to_string(&fs->id[1], fctr->indep_var[1]) );