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);
221 subc_list_double_destroy(&percentile_list);
228 /* Show all the appropriate tables */
234 /* Show totals if appropriate */
235 if ( ! cmd.sbc_nototal || factors == 0 )
237 show_summary(dependent_vars, n_dependent_vars, 0);
239 if ( cmd.sbc_statistics )
241 if ( cmd.a_statistics[XMN_ST_EXTREME])
242 show_extremes(dependent_vars, n_dependent_vars, 0, cmd.st_n);
244 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
245 show_descriptives(dependent_vars, n_dependent_vars, 0);
248 if ( sbc_percentile )
249 show_percentiles(dependent_vars, n_dependent_vars, 0);
254 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
256 for ( v = 0 ; v < n_dependent_vars; ++v )
257 np_plot(&totals[v], var_to_string(dependent_vars[v]));
260 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
262 if ( cmd.cmp == XMN_GROUPS )
264 box_plot_group(0, dependent_vars, n_dependent_vars,
268 box_plot_variables(0, dependent_vars, n_dependent_vars,
272 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
274 for ( v = 0 ; v < n_dependent_vars; ++v )
276 struct normal_curve normal;
278 normal.N = totals[v].n;
279 normal.mean = totals[v].mean;
280 normal.stddev = totals[v].stddev;
282 histogram_plot(totals[v].histogram,
283 var_to_string(dependent_vars[v]),
293 /* Show grouped statistics as appropriate */
297 show_summary(dependent_vars, n_dependent_vars, fctr);
299 if ( cmd.sbc_statistics )
301 if ( cmd.a_statistics[XMN_ST_EXTREME])
302 show_extremes(dependent_vars, n_dependent_vars, fctr, cmd.st_n);
304 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
305 show_descriptives(dependent_vars, n_dependent_vars, fctr);
308 if ( sbc_percentile )
309 show_percentiles(dependent_vars, n_dependent_vars, fctr);
316 struct factor_statistics **fs = fctr->fs ;
318 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
320 if ( cmd.cmp == XMN_VARIABLES )
321 box_plot_variables(fctr, dependent_vars, n_dependent_vars,
324 box_plot_group(fctr, dependent_vars, n_dependent_vars,
328 for ( v = 0 ; v < n_dependent_vars; ++v )
331 for ( fs = fctr->fs ; *fs ; ++fs )
333 const char *s = factor_to_string(fctr, *fs, dependent_vars[v]);
335 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
336 np_plot(&(*fs)->m[v], s);
338 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
340 struct normal_curve normal;
342 normal.N = (*fs)->m[v].n;
343 normal.mean = (*fs)->m[v].mean;
344 normal.stddev = (*fs)->m[v].stddev;
346 histogram_plot((*fs)->m[v].histogram,
350 } /* for ( fs .... */
352 } /* for ( v = 0 ..... */
362 static struct hsh_table *
363 list_to_ptile_hash(const subc_list_double *l)
367 struct hsh_table *h ;
369 h = hsh_create(subc_list_double_count(l),
370 (hsh_compare_func *) ptile_compare,
371 (hsh_hash_func *) ptile_hash,
372 (hsh_free_func *) free,
376 for ( i = 0 ; i < subc_list_double_count(l) ; ++i )
378 struct percentile *p = xmalloc (sizeof (struct percentile));
380 p->p = subc_list_double_at(l,i);
390 /* Parse the PERCENTILES subcommand */
392 xmn_custom_percentiles(struct cmd_examine *p UNUSED)
400 while ( lex_double_p() )
402 subc_list_double_push(&percentile_list,lex_double());
412 if ( lex_match_id("HAVERAGE"))
413 percentile_algorithm = PC_HAVERAGE;
415 else if ( lex_match_id("WAVERAGE"))
416 percentile_algorithm = PC_WAVERAGE;
418 else if ( lex_match_id("ROUND"))
419 percentile_algorithm = PC_ROUND;
421 else if ( lex_match_id("EMPIRICAL"))
422 percentile_algorithm = PC_EMPIRICAL;
424 else if ( lex_match_id("AEMPIRICAL"))
425 percentile_algorithm = PC_AEMPIRICAL;
427 else if ( lex_match_id("NONE"))
428 percentile_algorithm = PC_NONE;
431 if ( 0 == subc_list_double_count(&percentile_list))
433 subc_list_double_push(&percentile_list, 5);
434 subc_list_double_push(&percentile_list, 10);
435 subc_list_double_push(&percentile_list, 25);
436 subc_list_double_push(&percentile_list, 50);
437 subc_list_double_push(&percentile_list, 75);
438 subc_list_double_push(&percentile_list, 90);
439 subc_list_double_push(&percentile_list, 95);
445 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
447 xmn_custom_total(struct cmd_examine *p)
449 if ( p->sbc_nototal )
451 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
459 xmn_custom_nototal(struct cmd_examine *p)
463 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
472 /* Parser for the variables sub command
473 Returns 1 on success */
475 xmn_custom_variables(struct cmd_examine *cmd )
479 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
485 if (!parse_variables (default_dict, &dependent_vars, &n_dependent_vars,
486 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
488 free (dependent_vars);
492 assert(n_dependent_vars);
494 totals = xmalloc( sizeof(struct metrics) * n_dependent_vars);
496 if ( lex_match(T_BY))
499 success = examine_parse_independent_vars(cmd);
500 if ( success != 1 ) {
501 free (dependent_vars);
512 /* Parse the clause specifying the factors */
514 examine_parse_independent_vars(struct cmd_examine *cmd)
517 struct factor *sf = xmalloc(sizeof(struct factor));
519 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
527 sf->indep_var[0] = parse_variable();
528 sf->indep_var[1] = 0;
535 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
542 sf->indep_var[1] = parse_variable();
547 sf->fstats = hsh_create(4,
548 (hsh_compare_func *) factor_statistics_compare,
549 (hsh_hash_func *) factor_statistics_hash,
550 (hsh_free_func *) factor_statistics_free,
558 if ( token == '.' || token == '/' )
561 success = examine_parse_independent_vars(cmd);
572 void populate_percentiles(struct tab_table *tbl, int col, int row,
573 const struct metrics *m);
575 void populate_descriptives(struct tab_table *t, int col, int row,
576 const struct metrics *fs);
578 void populate_extremes(struct tab_table *t, int col, int row, int n,
579 const struct metrics *m);
581 void populate_summary(struct tab_table *t, int col, int row,
582 const struct metrics *m);
587 static int bad_weight_warn = 1;
590 /* Perform calculations for the sub factors */
592 factor_calc(struct ccase *c, int case_no, double weight, int case_missing)
595 struct factor *fctr = factors;
599 union value indep_vals[2] ;
601 indep_vals[0] = * case_data(c, fctr->indep_var[0]->fv);
603 if ( fctr->indep_var[1] )
604 indep_vals[1] = * case_data(c, fctr->indep_var[1]->fv);
606 indep_vals[1].f = SYSMIS;
608 assert(fctr->fstats);
610 struct factor_statistics **foo = ( struct factor_statistics ** )
611 hsh_probe(fctr->fstats, (void *) &indep_vals);
616 *foo = create_factor_statistics(n_dependent_vars,
620 for ( v = 0 ; v < n_dependent_vars ; ++v )
622 metrics_precalc( &(*foo)->m[v] );
627 for ( v = 0 ; v < n_dependent_vars ; ++v )
629 const struct variable *var = dependent_vars[v];
630 const union value *val = case_data (c, var->fv);
632 if ( value_is_missing(val,var) || case_missing )
635 metrics_calc( &(*foo)->m[v], val, weight, case_no);
649 run_examine(const struct casefile *cf, void *cmd_ )
651 struct casereader *r;
655 const struct cmd_examine *cmd = (struct cmd_examine *) cmd_;
657 /* Make sure we haven't got rubbish left over from a
660 struct factor *fctr = factors;
663 struct factor *next = fctr->next;
665 hsh_clear(fctr->fstats);
674 for ( v = 0 ; v < n_dependent_vars ; ++v )
675 metrics_precalc(&totals[v]);
677 for(r = casefile_get_reader (cf);
678 casereader_read (r, &c) ;
682 const int case_no = casereader_cnum(r);
684 const double weight =
685 dict_get_case_weight(default_dict, &c, &bad_weight_warn);
687 if ( cmd->miss == XMN_LISTWISE )
689 for ( v = 0 ; v < n_dependent_vars ; ++v )
691 const struct variable *var = dependent_vars[v];
692 const union value *val = case_data (&c, var->fv);
694 if ( value_is_missing(val,var))
700 for ( v = 0 ; v < n_dependent_vars ; ++v )
702 const struct variable *var = dependent_vars[v];
703 const union value *val = case_data (&c, var->fv);
705 if ( value_is_missing(val,var) || case_missing )
708 metrics_calc(&totals[v], val, weight, case_no);
712 factor_calc(&c, case_no, weight, case_missing);
717 for ( v = 0 ; v < n_dependent_vars ; ++v)
722 struct hsh_iterator hi;
723 struct factor_statistics *fs;
725 for ( fs = hsh_first(fctr->fstats, &hi);
727 fs = hsh_next(fctr->fstats, &hi))
730 fs->m[v].ptile_hash = list_to_ptile_hash(&percentile_list);
731 fs->m[v].ptile_alg = percentile_algorithm;
732 metrics_postcalc(&fs->m[v]);
738 totals[v].ptile_hash = list_to_ptile_hash(&percentile_list);
739 totals[v].ptile_alg = percentile_algorithm;
740 metrics_postcalc(&totals[v]);
744 /* Make sure that the combination of factors are complete */
749 struct hsh_iterator hi;
750 struct hsh_iterator hi0;
751 struct hsh_iterator hi1;
752 struct factor_statistics *fs;
754 struct hsh_table *idh0=0;
755 struct hsh_table *idh1=0;
759 idh0 = hsh_create(4, (hsh_compare_func *) compare_values,
760 (hsh_hash_func *) hash_value,
763 idh1 = hsh_create(4, (hsh_compare_func *) compare_values,
764 (hsh_hash_func *) hash_value,
768 for ( fs = hsh_first(fctr->fstats, &hi);
770 fs = hsh_next(fctr->fstats, &hi))
772 hsh_insert(idh0,(void *) &fs->id[0]);
773 hsh_insert(idh1,(void *) &fs->id[1]);
776 /* Ensure that the factors combination is complete */
777 for ( val0 = hsh_first(idh0, &hi0);
779 val0 = hsh_next(idh0, &hi0))
781 for ( val1 = hsh_first(idh1, &hi1);
783 val1 = hsh_next(idh1, &hi1))
785 struct factor_statistics **ffs;
790 ffs = (struct factor_statistics **)
791 hsh_probe(fctr->fstats, (void *) &key );
795 (*ffs) = create_factor_statistics (n_dependent_vars,
797 for ( i = 0 ; i < n_dependent_vars ; ++i )
798 metrics_precalc( &(*ffs)->m[i]);
806 fctr->fs = (struct factor_statistics **) hsh_sort_copy(fctr->fstats);
817 for ( i = 0 ; i < n_dependent_vars ; ++i )
819 metrics_destroy(&totals[i]);
827 show_summary(struct variable **dependent_var, int n_dep_var,
828 const struct factor *fctr)
830 static const char *subtitle[]=
838 int heading_columns ;
840 const int heading_rows = 3;
841 struct tab_table *tbl;
849 n_factors = hsh_count(fctr->fstats);
850 n_rows = n_dep_var * n_factors ;
852 if ( fctr->indep_var[1] )
861 n_rows += heading_rows;
863 n_cols = heading_columns + 6;
865 tbl = tab_create (n_cols,n_rows,0);
866 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
868 tab_dim (tbl, tab_natural_dimensions);
870 /* Outline the box */
875 n_cols - 1, n_rows - 1);
877 /* Vertical lines for the data only */
882 n_cols - 1, n_rows - 1);
885 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
886 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
887 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
889 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
892 tab_title (tbl, 0, _("Case Processing Summary"));
895 tab_joint_text(tbl, heading_columns, 0,
897 TAB_CENTER | TAT_TITLE,
900 /* Remove lines ... */
907 for ( i = 0 ; i < 3 ; ++i )
909 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
912 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
915 tab_joint_text(tbl, heading_columns + i*2 , 1,
916 heading_columns + i*2 + 1, 1,
917 TAB_CENTER | TAT_TITLE,
920 tab_box (tbl, -1, -1,
922 heading_columns + i*2, 1,
923 heading_columns + i*2 + 1, 1);
928 /* Titles for the independent variables */
931 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
932 var_to_string(fctr->indep_var[0]));
934 if ( fctr->indep_var[1] )
936 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
937 var_to_string(fctr->indep_var[1]));
943 for ( i = 0 ; i < n_dep_var ; ++i )
947 n_factors = hsh_count(fctr->fstats);
951 tab_hline(tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
954 0, i * n_factors + heading_rows,
955 TAB_LEFT | TAT_TITLE,
956 var_to_string(dependent_var[i])
961 populate_summary(tbl, heading_columns,
962 (i * n_factors) + heading_rows,
968 struct factor_statistics **fs = fctr->fs;
973 static union value prev;
975 if ( 0 != compare_values(&prev, &(*fs)->id[0],
976 fctr->indep_var[0]->width))
980 (i * n_factors ) + count +
982 TAB_LEFT | TAT_TITLE,
983 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
986 if (fctr->indep_var[1] && count > 0 )
987 tab_hline(tbl, TAL_1, 1, n_cols - 1,
988 (i * n_factors ) + count + heading_rows);
995 if ( fctr->indep_var[1])
998 (i * n_factors ) + count +
1000 TAB_LEFT | TAT_TITLE,
1001 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1004 populate_summary(tbl, heading_columns,
1005 (i * n_factors) + count
1020 populate_summary(struct tab_table *t, int col, int row,
1021 const struct metrics *m)
1024 const double total = m->n + m->n_missing ;
1026 tab_float(t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1027 tab_float(t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1028 tab_float(t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1032 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1033 100.0 * m->n / total );
1035 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1036 100.0 * m->n_missing / total );
1038 /* This seems a bit pointless !!! */
1039 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1040 100.0 * total / total );
1051 show_extremes(struct variable **dependent_var, int n_dep_var,
1052 const struct factor *fctr, int n_extremities)
1055 int heading_columns ;
1057 const int heading_rows = 1;
1058 struct tab_table *tbl;
1065 heading_columns = 2;
1066 n_factors = hsh_count(fctr->fstats);
1068 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1070 if ( fctr->indep_var[1] )
1071 heading_columns = 3;
1075 heading_columns = 1;
1076 n_rows = n_dep_var * 2 * n_extremities;
1079 n_rows += heading_rows;
1081 heading_columns += 2;
1082 n_cols = heading_columns + 2;
1084 tbl = tab_create (n_cols,n_rows,0);
1085 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1087 tab_dim (tbl, tab_natural_dimensions);
1089 /* Outline the box, No internal lines*/
1094 n_cols - 1, n_rows - 1);
1096 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1098 tab_title (tbl, 0, _("Extreme Values"));
1100 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1101 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1105 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1106 var_to_string(fctr->indep_var[0]));
1108 if ( fctr->indep_var[1] )
1109 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1110 var_to_string(fctr->indep_var[1]));
1113 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Value"));
1114 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Case Number"));
1116 for ( i = 0 ; i < n_dep_var ; ++i )
1120 tab_hline(tbl, TAL_1, 0, n_cols -1 ,
1121 i * 2 * n_extremities * n_factors + heading_rows);
1124 i * 2 * n_extremities * n_factors + heading_rows,
1125 TAB_LEFT | TAT_TITLE,
1126 var_to_string(dependent_var[i])
1131 populate_extremes(tbl, heading_columns - 2,
1132 i * 2 * n_extremities * n_factors + heading_rows,
1133 n_extremities, &totals[i]);
1137 struct factor_statistics **fs = fctr->fs;
1142 static union value prev ;
1144 const int row = heading_rows + ( 2 * n_extremities ) *
1145 ( ( i * n_factors ) + count );
1148 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1149 fctr->indep_var[0]->width))
1153 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1157 TAB_LEFT | TAT_TITLE,
1158 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1162 prev = (*fs)->id[0];
1164 if (fctr->indep_var[1] && count > 0 )
1165 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1167 if ( fctr->indep_var[1])
1168 tab_text (tbl, 2, row,
1169 TAB_LEFT | TAT_TITLE,
1170 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1173 populate_extremes(tbl, heading_columns - 2,
1188 /* Fill in the extremities table */
1190 populate_extremes(struct tab_table *t,
1191 int col, int row, int n, const struct metrics *m)
1197 tab_text(t, col, row,
1198 TAB_RIGHT | TAT_TITLE ,
1202 tab_text(t, col, row + n ,
1203 TAB_RIGHT | TAT_TITLE ,
1208 tab_hline(t, TAL_1, col, col + 3, row + n );
1210 for (extremity = 0; extremity < n ; ++extremity )
1213 tab_float(t, col + 1, row + extremity,
1215 extremity + 1, 8, 0);
1219 tab_float(t, col + 1, row + extremity + n,
1221 extremity + 1, 8, 0);
1227 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1230 const struct weighted_value *wv = m->wvp[idx];
1231 struct case_node *cn = wv->case_nos;
1234 for (j = 0 ; j < wv->w ; ++j )
1236 if ( extremity + j >= n )
1239 tab_float(t, col + 3, row + extremity + j + n,
1243 tab_float(t, col + 2, row + extremity + j + n,
1252 extremity += wv->w ;
1257 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1260 const struct weighted_value *wv = m->wvp[idx];
1261 struct case_node *cn = wv->case_nos;
1263 for (j = 0 ; j < wv->w ; ++j )
1265 if ( extremity + j >= n )
1268 tab_float(t, col + 3, row + extremity + j,
1272 tab_float(t, col + 2, row + extremity + j,
1281 extremity += wv->w ;
1286 /* Show the descriptives table */
1288 show_descriptives(struct variable **dependent_var,
1290 struct factor *fctr)
1293 int heading_columns ;
1295 const int n_stat_rows = 13;
1297 const int heading_rows = 1;
1299 struct tab_table *tbl;
1306 heading_columns = 4;
1307 n_factors = hsh_count(fctr->fstats);
1309 n_rows = n_dep_var * n_stat_rows * n_factors;
1311 if ( fctr->indep_var[1] )
1312 heading_columns = 5;
1316 heading_columns = 3;
1317 n_rows = n_dep_var * n_stat_rows;
1320 n_rows += heading_rows;
1322 n_cols = heading_columns + 2;
1325 tbl = tab_create (n_cols, n_rows, 0);
1327 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1329 tab_dim (tbl, tab_natural_dimensions);
1331 /* Outline the box and have no internal lines*/
1336 n_cols - 1, n_rows - 1);
1338 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1340 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1341 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1342 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1344 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Statistic"));
1345 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1347 tab_title (tbl, 0, _("Descriptives"));
1350 for ( i = 0 ; i < n_dep_var ; ++i )
1352 const int row = heading_rows + i * n_stat_rows * n_factors ;
1355 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1358 i * n_stat_rows * n_factors + heading_rows,
1359 TAB_LEFT | TAT_TITLE,
1360 var_to_string(dependent_var[i])
1366 struct factor_statistics **fs = fctr->fs;
1369 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1370 var_to_string(fctr->indep_var[0]));
1373 if ( fctr->indep_var[1])
1374 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1375 var_to_string(fctr->indep_var[1]));
1380 static union value prev ;
1382 const int row = heading_rows + n_stat_rows *
1383 ( ( i * n_factors ) + count );
1386 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1387 fctr->indep_var[0]->width))
1391 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1395 TAB_LEFT | TAT_TITLE,
1396 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1400 prev = (*fs)->id[0];
1402 if (fctr->indep_var[1] && count > 0 )
1403 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1405 if ( fctr->indep_var[1])
1406 tab_text (tbl, 2, row,
1407 TAB_LEFT | TAT_TITLE,
1408 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1411 populate_descriptives(tbl, heading_columns - 2,
1423 populate_descriptives(tbl, heading_columns - 2,
1424 i * n_stat_rows * n_factors + heading_rows,
1436 /* Fill in the descriptives data */
1438 populate_descriptives(struct tab_table *tbl, int col, int row,
1439 const struct metrics *m)
1442 const double t = gsl_cdf_tdist_Qinv(1 - cmd.n_cinterval[0]/100.0/2.0, \
1448 TAB_LEFT | TAT_TITLE,
1451 tab_float (tbl, col + 2,
1457 tab_float (tbl, col + 3,
1466 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1467 _("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1470 tab_text (tbl, col + 1,
1472 TAB_LEFT | TAT_TITLE,
1475 tab_float (tbl, col + 2,
1478 m->mean - t * m->se_mean,
1481 tab_text (tbl, col + 1,
1483 TAB_LEFT | TAT_TITLE,
1487 tab_float (tbl, col + 2,
1490 m->mean + t * m->se_mean,
1495 TAB_LEFT | TAT_TITLE,
1496 _("5% Trimmed Mean"));
1498 tab_float (tbl, col + 2,
1506 TAB_LEFT | TAT_TITLE,
1510 struct percentile *p;
1513 p = hsh_find(m->ptile_hash, &d);
1517 tab_float (tbl, col + 2,
1526 TAB_LEFT | TAT_TITLE,
1529 tab_float (tbl, col + 2,
1538 TAB_LEFT | TAT_TITLE,
1539 _("Std. Deviation"));
1542 tab_float (tbl, col + 2,
1551 TAB_LEFT | TAT_TITLE,
1554 tab_float (tbl, col + 2,
1562 TAB_LEFT | TAT_TITLE,
1565 tab_float (tbl, col + 2,
1574 TAB_LEFT | TAT_TITLE,
1578 tab_float (tbl, col + 2,
1586 TAB_LEFT | TAT_TITLE,
1587 _("Interquartile Range"));
1590 struct percentile *p1;
1591 struct percentile *p2;
1594 p1 = hsh_find(m->ptile_hash, &d);
1597 p2 = hsh_find(m->ptile_hash, &d);
1602 tab_float (tbl, col + 2,
1613 TAB_LEFT | TAT_TITLE,
1617 tab_float (tbl, col + 2,
1623 /* stderr of skewness */
1624 tab_float (tbl, col + 3,
1633 TAB_LEFT | TAT_TITLE,
1637 tab_float (tbl, col + 2,
1643 /* stderr of kurtosis */
1644 tab_float (tbl, col + 3,
1656 box_plot_variables(const struct factor *fctr,
1657 const struct variable **vars, int n_vars,
1658 const struct variable *id)
1662 struct factor_statistics **fs ;
1666 box_plot_group(fctr, vars, n_vars, id);
1670 for ( fs = fctr->fs ; *fs ; ++fs )
1672 double y_min = DBL_MAX;
1673 double y_max = -DBL_MAX;
1676 ch = chart_create();
1678 const char *s = factor_to_string(fctr, *fs, 0 );
1680 chart_write_title(ch, s);
1682 for ( i = 0 ; i < n_vars ; ++i )
1684 y_max = max(y_max, (*fs)->m[i].max);
1685 y_min = min(y_min, (*fs)->m[i].min);
1688 boxplot_draw_yscale(ch, y_max, y_min);
1690 for ( i = 0 ; i < n_vars ; ++i )
1693 const double box_width = (ch->data_right - ch->data_left)
1696 const double box_centre = ( i * 2 + 1) * box_width
1699 boxplot_draw_boxplot(ch,
1700 box_centre, box_width,
1702 var_to_string(vars[i]));
1714 /* Do a box plot, grouping all factors into one plot ;
1715 each dependent variable has its own plot.
1718 box_plot_group(const struct factor *fctr,
1719 const struct variable **vars,
1721 const struct variable *id UNUSED)
1726 for ( i = 0 ; i < n_vars ; ++i )
1728 struct factor_statistics **fs ;
1731 ch = chart_create();
1733 boxplot_draw_yscale(ch, totals[i].max, totals[i].min);
1739 for ( fs = fctr->fs ; *fs ; ++fs )
1742 chart_write_title(ch, _("Boxplot of %s vs. %s"),
1743 var_to_string(vars[i]), var_to_string(fctr->indep_var[0]) );
1745 for ( fs = fctr->fs ; *fs ; ++fs )
1748 const char *s = factor_to_string_concise(fctr, *fs);
1750 const double box_width = (ch->data_right - ch->data_left)
1751 / (n_factors * 2.0 ) ;
1753 const double box_centre = ( f++ * 2 + 1) * box_width
1756 boxplot_draw_boxplot(ch,
1757 box_centre, box_width,
1764 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1765 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1767 chart_write_title(ch, _("Boxplot"));
1769 boxplot_draw_boxplot(ch,
1770 box_centre, box_width,
1772 var_to_string(vars[i]) );
1781 /* Plot the normal and detrended normal plots for m
1782 Label the plots with factorname */
1784 np_plot(const struct metrics *m, const char *factorname)
1787 double yfirst=0, ylast=0;
1790 struct chart *np_chart;
1792 /* Detrended Normal Plot */
1793 struct chart *dnp_chart;
1795 /* The slope and intercept of the ideal normal probability line */
1796 const double slope = 1.0 / m->stddev;
1797 const double intercept = - m->mean / m->stddev;
1799 /* Cowardly refuse to plot an empty data set */
1800 if ( m->n_data == 0 )
1803 np_chart = chart_create();
1804 dnp_chart = chart_create();
1806 if ( !np_chart || ! dnp_chart )
1809 chart_write_title(np_chart, _("Normal Q-Q Plot of %s"), factorname);
1810 chart_write_xlabel(np_chart, _("Observed Value"));
1811 chart_write_ylabel(np_chart, _("Expected Normal"));
1814 chart_write_title(dnp_chart, _("Detrended Normal Q-Q Plot of %s"),
1816 chart_write_xlabel(dnp_chart, _("Observed Value"));
1817 chart_write_ylabel(dnp_chart, _("Dev from Normal"));
1819 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1820 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1824 /* Need to make sure that both the scatter plot and the ideal fit into the
1826 double x_lower = min(m->min, (yfirst - intercept) / slope) ;
1827 double x_upper = max(m->max, (ylast - intercept) / slope) ;
1828 double slack = (x_upper - x_lower) * 0.05 ;
1830 chart_write_xscale(np_chart, x_lower - slack, x_upper + slack, 5);
1832 chart_write_xscale(dnp_chart, m->min, m->max, 5);
1836 chart_write_yscale(np_chart, yfirst, ylast, 5);
1839 /* We have to cache the detrended data, beacause we need to
1840 find its limits before we can plot it */
1842 d_data = xmalloc (m->n_data * sizeof(double));
1843 double d_max = -DBL_MAX;
1844 double d_min = DBL_MAX;
1845 for ( i = 0 ; i < m->n_data; ++i )
1847 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1849 chart_datum(np_chart, 0, m->wvp[i]->v.f, ns);
1851 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1853 if ( d_data[i] < d_min ) d_min = d_data[i];
1854 if ( d_data[i] > d_max ) d_max = d_data[i];
1856 chart_write_yscale(dnp_chart, d_min, d_max, 5);
1858 for ( i = 0 ; i < m->n_data; ++i )
1859 chart_datum(dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1864 chart_line(np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1865 chart_line(dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1867 chart_submit(np_chart);
1868 chart_submit(dnp_chart);
1874 /* Show the percentiles */
1876 show_percentiles(struct variable **dependent_var,
1878 struct factor *fctr)
1880 struct tab_table *tbl;
1886 struct hsh_table *ptiles ;
1888 int n_heading_columns;
1889 const int n_heading_rows = 2;
1890 const int n_stat_rows = 2;
1896 struct factor_statistics **fs = fctr->fs ;
1897 n_heading_columns = 3;
1898 n_factors = hsh_count(fctr->fstats);
1900 ptiles = (*fs)->m[0].ptile_hash;
1902 if ( fctr->indep_var[1] )
1903 n_heading_columns = 4;
1908 n_heading_columns = 2;
1910 ptiles = totals[0].ptile_hash;
1913 n_ptiles = hsh_count(ptiles);
1915 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1917 n_cols = n_heading_columns + n_ptiles ;
1919 tbl = tab_create (n_cols, n_rows, 0);
1921 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1923 tab_dim (tbl, tab_natural_dimensions);
1925 /* Outline the box and have no internal lines*/
1930 n_cols - 1, n_rows - 1);
1932 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
1934 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
1937 tab_title (tbl, 0, _("Percentiles"));
1940 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
1947 n_heading_columns - 1, n_rows - 1);
1953 n_heading_columns, n_heading_rows - 1,
1954 n_cols - 1, n_rows - 1);
1956 tab_joint_text(tbl, n_heading_columns + 1, 0,
1958 TAB_CENTER | TAT_TITLE ,
1963 /* Put in the percentile break points as headings */
1965 struct percentile **p = (struct percentile **) hsh_sort(ptiles);
1970 tab_float(tbl, n_heading_columns + i++ , 1,
1979 for ( i = 0 ; i < n_dep_var ; ++i )
1981 const int n_stat_rows = 2;
1982 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
1985 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1988 i * n_stat_rows * n_factors + n_heading_rows,
1989 TAB_LEFT | TAT_TITLE,
1990 var_to_string(dependent_var[i])
1995 struct factor_statistics **fs = fctr->fs;
1998 tab_text (tbl, 1, n_heading_rows - 1,
1999 TAB_CENTER | TAT_TITLE,
2000 var_to_string(fctr->indep_var[0]));
2003 if ( fctr->indep_var[1])
2004 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2005 var_to_string(fctr->indep_var[1]));
2010 static union value prev ;
2012 const int row = n_heading_rows + n_stat_rows *
2013 ( ( i * n_factors ) + count );
2016 if ( 0 != compare_values(&prev, &(*fs)->id[0],
2017 fctr->indep_var[0]->width))
2021 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2025 TAB_LEFT | TAT_TITLE,
2026 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
2032 prev = (*fs)->id[0];
2034 if (fctr->indep_var[1] && count > 0 )
2035 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
2037 if ( fctr->indep_var[1])
2038 tab_text (tbl, 2, row,
2039 TAB_LEFT | TAT_TITLE,
2040 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
2044 populate_percentiles(tbl, n_heading_columns - 1,
2056 populate_percentiles(tbl, n_heading_columns - 1,
2057 i * n_stat_rows * n_factors + n_heading_rows,
2074 populate_percentiles(struct tab_table *tbl, int col, int row,
2075 const struct metrics *m)
2079 struct percentile **p = (struct percentile **) hsh_sort(m->ptile_hash);
2083 TAB_LEFT | TAT_TITLE,
2084 _("Tukey\'s Hinges")
2089 TAB_LEFT | TAT_TITLE,
2090 ptile_alg_desc[m->ptile_alg]
2097 tab_float(tbl, col + i + 1 , row,
2100 if ( (*p)->p == 25 )
2101 tab_float(tbl, col + i + 1 , row + 1,
2105 if ( (*p)->p == 50 )
2106 tab_float(tbl, col + i + 1 , row + 1,
2110 if ( (*p)->p == 75 )
2111 tab_float(tbl, col + i + 1 , row + 1,
2126 factor_to_string(const struct factor *fctr,
2127 struct factor_statistics *fs,
2128 const struct variable *var)
2131 static char buf1[100];
2137 sprintf(buf1, "%s (",var_to_string(var) );
2140 snprintf(buf2, 100, "%s = %s",
2141 var_to_string(fctr->indep_var[0]),
2142 value_to_string(&fs->id[0],fctr->indep_var[0]));
2146 if ( fctr->indep_var[1] )
2148 sprintf(buf2, "; %s = %s)",
2149 var_to_string(fctr->indep_var[1]),
2150 value_to_string(&fs->id[1],
2151 fctr->indep_var[1]));
2166 factor_to_string_concise(const struct factor *fctr,
2167 struct factor_statistics *fs)
2171 static char buf[100];
2175 snprintf(buf, 100, "%s",
2176 value_to_string(&fs->id[0], fctr->indep_var[0]));
2178 if ( fctr->indep_var[1] )
2180 sprintf(buf2, ",%s)", value_to_string(&fs->id[1], fctr->indep_var[1]) );