1 /* PSPP - EXAMINE data for normality . -*-c-*-
3 Copyright (C) 2004 Free Software Foundation, Inc.
4 Author: John Darrington 2004
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <gsl/gsl_cdf.h>
30 #include "dictionary.h"
38 #include "value-labels.h"
43 #include "factor_stats.h"
45 #include "percentiles.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
59 +missing=miss:pairwise/!listwise,
61 incl:include/!exclude;
62 +compare=cmp:variables/!groups;
65 +plot[plt_]=stemleaf,boxplot,npplot,:spreadlevel(*d:n),histogram,all,none;
67 +statistics[st_]=descriptives,:extreme(*d:n),all,none.
76 static struct cmd_examine cmd;
78 static struct variable **dependent_vars;
80 static int n_dependent_vars;
85 /* The independent variable */
86 struct variable *indep_var[2];
89 /* Hash table of factor stats indexed by 2 values */
90 struct hsh_table *fstats;
92 /* The hash table after it has been crunched */
93 struct factor_statistics **fs;
99 /* Linked list of factors */
100 static struct factor *factors=0;
102 static struct metrics *totals=0;
104 /* Parse the clause specifying the factors */
105 static int examine_parse_independent_vars(struct cmd_examine *cmd);
109 /* Output functions */
110 static void show_summary(struct variable **dependent_var, int n_dep_var,
111 const struct factor *f);
113 static void show_extremes(struct variable **dependent_var,
115 const struct factor *factor,
118 static void show_descriptives(struct variable **dependent_var,
120 struct factor *factor);
122 static void show_percentiles(struct variable **dependent_var,
124 struct factor *factor);
129 void np_plot(const struct metrics *m, const char *factorname);
132 void box_plot_group(const struct factor *fctr,
133 const struct variable **vars, int n_vars,
134 const struct variable *id
138 void box_plot_variables(const struct factor *fctr,
139 const struct variable **vars, int n_vars,
140 const struct variable *id
145 /* Per Split function */
146 static void run_examine(const struct casefile *cf, void *cmd_);
148 static void output_examine(void);
151 void factor_calc(struct ccase *c, int case_no,
152 double weight, int case_missing);
155 /* Represent a factor as a string, so it can be
156 printed in a human readable fashion */
157 const char * factor_to_string(const struct factor *fctr,
158 struct factor_statistics *fs,
159 const struct variable *var);
162 /* Represent a factor as a string, so it can be
163 printed in a human readable fashion,
164 but sacrificing some readablility for the sake of brevity */
165 const char *factor_to_string_concise(const struct factor *fctr,
166 struct factor_statistics *fs);
171 /* Function to use for testing for missing values */
172 static is_missing_func value_is_missing;
177 static subc_list_double percentile_list;
179 static enum pc_alg percentile_algorithm;
181 static short sbc_percentile;
188 subc_list_double_create(&percentile_list);
189 percentile_algorithm = PC_HAVERAGE;
191 if ( !parse_examine(&cmd) )
194 /* If /MISSING=INCLUDE is set, then user missing values are ignored */
195 if (cmd.incl == XMN_INCLUDE )
196 value_is_missing = is_system_missing;
198 value_is_missing = is_missing;
200 if ( cmd.st_n == SYSMIS )
203 if ( ! cmd.sbc_cinterval)
204 cmd.n_cinterval[0] = 95.0;
206 /* If descriptives have been requested, make sure the
207 quartiles are calculated */
208 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES] )
210 subc_list_double_push(&percentile_list, 25);
211 subc_list_double_push(&percentile_list, 50);
212 subc_list_double_push(&percentile_list, 75);
215 multipass_procedure_with_splits (run_examine, &cmd);
222 if ( dependent_vars )
223 free (dependent_vars);
226 struct factor *f = factors ;
229 struct factor *ff = f;
233 hsh_destroy ( ff->fstats ) ;
238 subc_list_double_destroy(&percentile_list);
245 /* Show all the appropriate tables */
251 /* Show totals if appropriate */
252 if ( ! cmd.sbc_nototal || factors == 0 )
254 show_summary(dependent_vars, n_dependent_vars, 0);
256 if ( cmd.sbc_statistics )
258 if ( cmd.a_statistics[XMN_ST_EXTREME])
259 show_extremes(dependent_vars, n_dependent_vars, 0, cmd.st_n);
261 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
262 show_descriptives(dependent_vars, n_dependent_vars, 0);
265 if ( sbc_percentile )
266 show_percentiles(dependent_vars, n_dependent_vars, 0);
271 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
273 for ( v = 0 ; v < n_dependent_vars; ++v )
274 np_plot(&totals[v], var_to_string(dependent_vars[v]));
277 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
279 if ( cmd.cmp == XMN_GROUPS )
281 box_plot_group(0, dependent_vars, n_dependent_vars,
285 box_plot_variables(0, dependent_vars, n_dependent_vars,
289 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
291 for ( v = 0 ; v < n_dependent_vars; ++v )
293 struct normal_curve normal;
295 normal.N = totals[v].n;
296 normal.mean = totals[v].mean;
297 normal.stddev = totals[v].stddev;
299 histogram_plot(totals[v].histogram,
300 var_to_string(dependent_vars[v]),
310 /* Show grouped statistics as appropriate */
314 show_summary(dependent_vars, n_dependent_vars, fctr);
316 if ( cmd.sbc_statistics )
318 if ( cmd.a_statistics[XMN_ST_EXTREME])
319 show_extremes(dependent_vars, n_dependent_vars, fctr, cmd.st_n);
321 if ( cmd.a_statistics[XMN_ST_DESCRIPTIVES])
322 show_descriptives(dependent_vars, n_dependent_vars, fctr);
325 if ( sbc_percentile )
326 show_percentiles(dependent_vars, n_dependent_vars, fctr);
333 struct factor_statistics **fs = fctr->fs ;
335 if ( cmd.a_plot[XMN_PLT_BOXPLOT] )
337 if ( cmd.cmp == XMN_VARIABLES )
338 box_plot_variables(fctr, dependent_vars, n_dependent_vars,
341 box_plot_group(fctr, dependent_vars, n_dependent_vars,
345 for ( v = 0 ; v < n_dependent_vars; ++v )
348 for ( fs = fctr->fs ; *fs ; ++fs )
350 const char *s = factor_to_string(fctr, *fs, dependent_vars[v]);
352 if ( cmd.a_plot[XMN_PLT_NPPLOT] )
353 np_plot(&(*fs)->m[v], s);
355 if ( cmd.a_plot[XMN_PLT_HISTOGRAM] )
357 struct normal_curve normal;
359 normal.N = (*fs)->m[v].n;
360 normal.mean = (*fs)->m[v].mean;
361 normal.stddev = (*fs)->m[v].stddev;
363 histogram_plot((*fs)->m[v].histogram,
367 } /* for ( fs .... */
369 } /* for ( v = 0 ..... */
379 /* Create a hash table of percentiles and their values from the list of
381 static struct hsh_table *
382 list_to_ptile_hash(const subc_list_double *l)
386 struct hsh_table *h ;
388 h = hsh_create(subc_list_double_count(l),
389 (hsh_compare_func *) ptile_compare,
390 (hsh_hash_func *) ptile_hash,
391 (hsh_free_func *) free,
395 for ( i = 0 ; i < subc_list_double_count(l) ; ++i )
397 struct percentile *p = xmalloc (sizeof (struct percentile));
399 p->p = subc_list_double_at(l,i);
410 /* Parse the PERCENTILES subcommand */
412 xmn_custom_percentiles(struct cmd_examine *p UNUSED)
420 while ( lex_is_number() )
422 subc_list_double_push(&percentile_list,lex_number());
432 if ( lex_match_id("HAVERAGE"))
433 percentile_algorithm = PC_HAVERAGE;
435 else if ( lex_match_id("WAVERAGE"))
436 percentile_algorithm = PC_WAVERAGE;
438 else if ( lex_match_id("ROUND"))
439 percentile_algorithm = PC_ROUND;
441 else if ( lex_match_id("EMPIRICAL"))
442 percentile_algorithm = PC_EMPIRICAL;
444 else if ( lex_match_id("AEMPIRICAL"))
445 percentile_algorithm = PC_AEMPIRICAL;
447 else if ( lex_match_id("NONE"))
448 percentile_algorithm = PC_NONE;
451 if ( 0 == subc_list_double_count(&percentile_list))
453 subc_list_double_push(&percentile_list, 5);
454 subc_list_double_push(&percentile_list, 10);
455 subc_list_double_push(&percentile_list, 25);
456 subc_list_double_push(&percentile_list, 50);
457 subc_list_double_push(&percentile_list, 75);
458 subc_list_double_push(&percentile_list, 90);
459 subc_list_double_push(&percentile_list, 95);
465 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
467 xmn_custom_total(struct cmd_examine *p)
469 if ( p->sbc_nototal )
471 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
479 xmn_custom_nototal(struct cmd_examine *p)
483 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
492 /* Parser for the variables sub command
493 Returns 1 on success */
495 xmn_custom_variables(struct cmd_examine *cmd )
499 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
505 if (!parse_variables (default_dict, &dependent_vars, &n_dependent_vars,
506 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
508 free (dependent_vars);
512 assert(n_dependent_vars);
514 totals = xmalloc( sizeof(struct metrics) * n_dependent_vars);
516 if ( lex_match(T_BY))
519 success = examine_parse_independent_vars(cmd);
520 if ( success != 1 ) {
521 free (dependent_vars);
532 /* Parse the clause specifying the factors */
534 examine_parse_independent_vars(struct cmd_examine *cmd)
537 struct factor *sf = xmalloc(sizeof(struct factor));
539 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
547 sf->indep_var[0] = parse_variable();
548 sf->indep_var[1] = 0;
555 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
562 sf->indep_var[1] = parse_variable();
567 sf->fstats = hsh_create(4,
568 (hsh_compare_func *) factor_statistics_compare,
569 (hsh_hash_func *) factor_statistics_hash,
570 (hsh_free_func *) factor_statistics_free,
578 if ( token == '.' || token == '/' )
581 success = examine_parse_independent_vars(cmd);
592 void populate_percentiles(struct tab_table *tbl, int col, int row,
593 const struct metrics *m);
595 void populate_descriptives(struct tab_table *t, int col, int row,
596 const struct metrics *fs);
598 void populate_extremes(struct tab_table *t, int col, int row, int n,
599 const struct metrics *m);
601 void populate_summary(struct tab_table *t, int col, int row,
602 const struct metrics *m);
607 static int bad_weight_warn = 1;
610 /* Perform calculations for the sub factors */
612 factor_calc(struct ccase *c, int case_no, double weight, int case_missing)
615 struct factor *fctr = factors;
619 struct factor_statistics **foo ;
620 union value indep_vals[2] ;
622 indep_vals[0] = * case_data(c, fctr->indep_var[0]->fv);
624 if ( fctr->indep_var[1] )
625 indep_vals[1] = * case_data(c, fctr->indep_var[1]->fv);
627 indep_vals[1].f = SYSMIS;
629 assert(fctr->fstats);
631 foo = ( struct factor_statistics ** )
632 hsh_probe(fctr->fstats, (void *) &indep_vals);
637 *foo = create_factor_statistics(n_dependent_vars,
641 for ( v = 0 ; v < n_dependent_vars ; ++v )
643 metrics_precalc( &(*foo)->m[v] );
648 for ( v = 0 ; v < n_dependent_vars ; ++v )
650 const struct variable *var = dependent_vars[v];
651 const union value *val = case_data (c, var->fv);
653 if ( value_is_missing(val,var) || case_missing )
656 metrics_calc( &(*foo)->m[v], val, weight, case_no);
670 run_examine(const struct casefile *cf, void *cmd_ )
672 struct casereader *r;
676 const struct cmd_examine *cmd = (struct cmd_examine *) cmd_;
678 /* Make sure we haven't got rubbish left over from a
681 struct factor *fctr = factors;
684 struct factor *next = fctr->next;
686 hsh_clear(fctr->fstats);
695 for ( v = 0 ; v < n_dependent_vars ; ++v )
696 metrics_precalc(&totals[v]);
698 for(r = casefile_get_reader (cf);
699 casereader_read (r, &c) ;
703 const int case_no = casereader_cnum(r);
705 const double weight =
706 dict_get_case_weight(default_dict, &c, &bad_weight_warn);
708 if ( cmd->miss == XMN_LISTWISE )
710 for ( v = 0 ; v < n_dependent_vars ; ++v )
712 const struct variable *var = dependent_vars[v];
713 const union value *val = case_data (&c, var->fv);
715 if ( value_is_missing(val,var))
721 for ( v = 0 ; v < n_dependent_vars ; ++v )
723 const struct variable *var = dependent_vars[v];
724 const union value *val = case_data (&c, var->fv);
726 if ( value_is_missing(val,var) || case_missing )
729 metrics_calc(&totals[v], val, weight, case_no);
733 factor_calc(&c, case_no, weight, case_missing);
738 for ( v = 0 ; v < n_dependent_vars ; ++v)
743 struct hsh_iterator hi;
744 struct factor_statistics *fs;
746 for ( fs = hsh_first(fctr->fstats, &hi);
748 fs = hsh_next(fctr->fstats, &hi))
751 fs->m[v].ptile_hash = list_to_ptile_hash(&percentile_list);
752 fs->m[v].ptile_alg = percentile_algorithm;
753 metrics_postcalc(&fs->m[v]);
759 totals[v].ptile_hash = list_to_ptile_hash(&percentile_list);
760 totals[v].ptile_alg = percentile_algorithm;
761 metrics_postcalc(&totals[v]);
765 /* Make sure that the combination of factors are complete */
770 struct hsh_iterator hi;
771 struct hsh_iterator hi0;
772 struct hsh_iterator hi1;
773 struct factor_statistics *fs;
775 struct hsh_table *idh0=0;
776 struct hsh_table *idh1=0;
780 idh0 = hsh_create(4, (hsh_compare_func *) compare_values,
781 (hsh_hash_func *) hash_value,
784 idh1 = hsh_create(4, (hsh_compare_func *) compare_values,
785 (hsh_hash_func *) hash_value,
789 for ( fs = hsh_first(fctr->fstats, &hi);
791 fs = hsh_next(fctr->fstats, &hi))
793 hsh_insert(idh0,(void *) &fs->id[0]);
794 hsh_insert(idh1,(void *) &fs->id[1]);
797 /* Ensure that the factors combination is complete */
798 for ( val0 = hsh_first(idh0, &hi0);
800 val0 = hsh_next(idh0, &hi0))
802 for ( val1 = hsh_first(idh1, &hi1);
804 val1 = hsh_next(idh1, &hi1))
806 struct factor_statistics **ffs;
811 ffs = (struct factor_statistics **)
812 hsh_probe(fctr->fstats, (void *) &key );
816 (*ffs) = create_factor_statistics (n_dependent_vars,
818 for ( i = 0 ; i < n_dependent_vars ; ++i )
819 metrics_precalc( &(*ffs)->m[i]);
827 fctr->fs = (struct factor_statistics **) hsh_sort_copy(fctr->fstats);
838 for ( i = 0 ; i < n_dependent_vars ; ++i )
840 metrics_destroy(&totals[i]);
848 show_summary(struct variable **dependent_var, int n_dep_var,
849 const struct factor *fctr)
851 static const char *subtitle[]=
859 int heading_columns ;
861 const int heading_rows = 3;
862 struct tab_table *tbl;
870 n_factors = hsh_count(fctr->fstats);
871 n_rows = n_dep_var * n_factors ;
873 if ( fctr->indep_var[1] )
882 n_rows += heading_rows;
884 n_cols = heading_columns + 6;
886 tbl = tab_create (n_cols,n_rows,0);
887 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
889 tab_dim (tbl, tab_natural_dimensions);
891 /* Outline the box */
896 n_cols - 1, n_rows - 1);
898 /* Vertical lines for the data only */
903 n_cols - 1, n_rows - 1);
906 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
907 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
908 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
910 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
913 tab_title (tbl, 0, _("Case Processing Summary"));
916 tab_joint_text(tbl, heading_columns, 0,
918 TAB_CENTER | TAT_TITLE,
921 /* Remove lines ... */
928 for ( i = 0 ; i < 3 ; ++i )
930 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
933 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
936 tab_joint_text(tbl, heading_columns + i*2 , 1,
937 heading_columns + i*2 + 1, 1,
938 TAB_CENTER | TAT_TITLE,
941 tab_box (tbl, -1, -1,
943 heading_columns + i*2, 1,
944 heading_columns + i*2 + 1, 1);
949 /* Titles for the independent variables */
952 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
953 var_to_string(fctr->indep_var[0]));
955 if ( fctr->indep_var[1] )
957 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
958 var_to_string(fctr->indep_var[1]));
964 for ( i = 0 ; i < n_dep_var ; ++i )
968 n_factors = hsh_count(fctr->fstats);
972 tab_hline(tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
975 0, i * n_factors + heading_rows,
976 TAB_LEFT | TAT_TITLE,
977 var_to_string(dependent_var[i])
982 populate_summary(tbl, heading_columns,
983 (i * n_factors) + heading_rows,
989 struct factor_statistics **fs = fctr->fs;
994 static union value prev;
996 if ( 0 != compare_values(&prev, &(*fs)->id[0],
997 fctr->indep_var[0]->width))
1001 (i * n_factors ) + count +
1003 TAB_LEFT | TAT_TITLE,
1004 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1007 if (fctr->indep_var[1] && count > 0 )
1008 tab_hline(tbl, TAL_1, 1, n_cols - 1,
1009 (i * n_factors ) + count + heading_rows);
1013 prev = (*fs)->id[0];
1016 if ( fctr->indep_var[1])
1019 (i * n_factors ) + count +
1021 TAB_LEFT | TAT_TITLE,
1022 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1025 populate_summary(tbl, heading_columns,
1026 (i * n_factors) + count
1041 populate_summary(struct tab_table *t, int col, int row,
1042 const struct metrics *m)
1045 const double total = m->n + m->n_missing ;
1047 tab_float(t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1048 tab_float(t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1049 tab_float(t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1053 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1054 100.0 * m->n / total );
1056 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1057 100.0 * m->n_missing / total );
1059 /* This seems a bit pointless !!! */
1060 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1061 100.0 * total / total );
1072 show_extremes(struct variable **dependent_var, int n_dep_var,
1073 const struct factor *fctr, int n_extremities)
1076 int heading_columns ;
1078 const int heading_rows = 1;
1079 struct tab_table *tbl;
1086 heading_columns = 2;
1087 n_factors = hsh_count(fctr->fstats);
1089 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1091 if ( fctr->indep_var[1] )
1092 heading_columns = 3;
1096 heading_columns = 1;
1097 n_rows = n_dep_var * 2 * n_extremities;
1100 n_rows += heading_rows;
1102 heading_columns += 2;
1103 n_cols = heading_columns + 2;
1105 tbl = tab_create (n_cols,n_rows,0);
1106 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1108 tab_dim (tbl, tab_natural_dimensions);
1110 /* Outline the box, No internal lines*/
1115 n_cols - 1, n_rows - 1);
1117 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1119 tab_title (tbl, 0, _("Extreme Values"));
1121 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1122 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1126 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1127 var_to_string(fctr->indep_var[0]));
1129 if ( fctr->indep_var[1] )
1130 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1131 var_to_string(fctr->indep_var[1]));
1134 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Value"));
1135 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Case Number"));
1137 for ( i = 0 ; i < n_dep_var ; ++i )
1141 tab_hline(tbl, TAL_1, 0, n_cols -1 ,
1142 i * 2 * n_extremities * n_factors + heading_rows);
1145 i * 2 * n_extremities * n_factors + heading_rows,
1146 TAB_LEFT | TAT_TITLE,
1147 var_to_string(dependent_var[i])
1152 populate_extremes(tbl, heading_columns - 2,
1153 i * 2 * n_extremities * n_factors + heading_rows,
1154 n_extremities, &totals[i]);
1158 struct factor_statistics **fs = fctr->fs;
1163 static union value prev ;
1165 const int row = heading_rows + ( 2 * n_extremities ) *
1166 ( ( i * n_factors ) + count );
1169 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1170 fctr->indep_var[0]->width))
1174 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1178 TAB_LEFT | TAT_TITLE,
1179 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1183 prev = (*fs)->id[0];
1185 if (fctr->indep_var[1] && count > 0 )
1186 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1188 if ( fctr->indep_var[1])
1189 tab_text (tbl, 2, row,
1190 TAB_LEFT | TAT_TITLE,
1191 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1194 populate_extremes(tbl, heading_columns - 2,
1209 /* Fill in the extremities table */
1211 populate_extremes(struct tab_table *t,
1212 int col, int row, int n, const struct metrics *m)
1218 tab_text(t, col, row,
1219 TAB_RIGHT | TAT_TITLE ,
1223 tab_text(t, col, row + n ,
1224 TAB_RIGHT | TAT_TITLE ,
1229 tab_hline(t, TAL_1, col, col + 3, row + n );
1231 for (extremity = 0; extremity < n ; ++extremity )
1234 tab_float(t, col + 1, row + extremity,
1236 extremity + 1, 8, 0);
1240 tab_float(t, col + 1, row + extremity + n,
1242 extremity + 1, 8, 0);
1248 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1251 const struct weighted_value *wv = m->wvp[idx];
1252 struct case_node *cn = wv->case_nos;
1255 for (j = 0 ; j < wv->w ; ++j )
1257 if ( extremity + j >= n )
1260 tab_float(t, col + 3, row + extremity + j + n,
1264 tab_float(t, col + 2, row + extremity + j + n,
1273 extremity += wv->w ;
1278 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1281 const struct weighted_value *wv = m->wvp[idx];
1282 struct case_node *cn = wv->case_nos;
1284 for (j = 0 ; j < wv->w ; ++j )
1286 if ( extremity + j >= n )
1289 tab_float(t, col + 3, row + extremity + j,
1293 tab_float(t, col + 2, row + extremity + j,
1302 extremity += wv->w ;
1307 /* Show the descriptives table */
1309 show_descriptives(struct variable **dependent_var,
1311 struct factor *fctr)
1314 int heading_columns ;
1316 const int n_stat_rows = 13;
1318 const int heading_rows = 1;
1320 struct tab_table *tbl;
1327 heading_columns = 4;
1328 n_factors = hsh_count(fctr->fstats);
1330 n_rows = n_dep_var * n_stat_rows * n_factors;
1332 if ( fctr->indep_var[1] )
1333 heading_columns = 5;
1337 heading_columns = 3;
1338 n_rows = n_dep_var * n_stat_rows;
1341 n_rows += heading_rows;
1343 n_cols = heading_columns + 2;
1346 tbl = tab_create (n_cols, n_rows, 0);
1348 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1350 tab_dim (tbl, tab_natural_dimensions);
1352 /* Outline the box and have no internal lines*/
1357 n_cols - 1, n_rows - 1);
1359 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1361 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1362 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1363 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1365 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Statistic"));
1366 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1368 tab_title (tbl, 0, _("Descriptives"));
1371 for ( i = 0 ; i < n_dep_var ; ++i )
1373 const int row = heading_rows + i * n_stat_rows * n_factors ;
1376 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1379 i * n_stat_rows * n_factors + heading_rows,
1380 TAB_LEFT | TAT_TITLE,
1381 var_to_string(dependent_var[i])
1387 struct factor_statistics **fs = fctr->fs;
1390 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1391 var_to_string(fctr->indep_var[0]));
1394 if ( fctr->indep_var[1])
1395 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1396 var_to_string(fctr->indep_var[1]));
1401 static union value prev ;
1403 const int row = heading_rows + n_stat_rows *
1404 ( ( i * n_factors ) + count );
1407 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1408 fctr->indep_var[0]->width))
1412 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1416 TAB_LEFT | TAT_TITLE,
1417 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1421 prev = (*fs)->id[0];
1423 if (fctr->indep_var[1] && count > 0 )
1424 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1426 if ( fctr->indep_var[1])
1427 tab_text (tbl, 2, row,
1428 TAB_LEFT | TAT_TITLE,
1429 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1432 populate_descriptives(tbl, heading_columns - 2,
1444 populate_descriptives(tbl, heading_columns - 2,
1445 i * n_stat_rows * n_factors + heading_rows,
1457 /* Fill in the descriptives data */
1459 populate_descriptives(struct tab_table *tbl, int col, int row,
1460 const struct metrics *m)
1463 const double t = gsl_cdf_tdist_Qinv(1 - cmd.n_cinterval[0]/100.0/2.0, \
1469 TAB_LEFT | TAT_TITLE,
1472 tab_float (tbl, col + 2,
1478 tab_float (tbl, col + 3,
1487 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1488 _("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1491 tab_text (tbl, col + 1,
1493 TAB_LEFT | TAT_TITLE,
1496 tab_float (tbl, col + 2,
1499 m->mean - t * m->se_mean,
1502 tab_text (tbl, col + 1,
1504 TAB_LEFT | TAT_TITLE,
1508 tab_float (tbl, col + 2,
1511 m->mean + t * m->se_mean,
1516 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1517 _("5%% Trimmed Mean"));
1519 tab_float (tbl, col + 2,
1527 TAB_LEFT | TAT_TITLE,
1531 struct percentile *p;
1534 p = hsh_find(m->ptile_hash, &d);
1539 tab_float (tbl, col + 2,
1549 TAB_LEFT | TAT_TITLE,
1552 tab_float (tbl, col + 2,
1561 TAB_LEFT | TAT_TITLE,
1562 _("Std. Deviation"));
1565 tab_float (tbl, col + 2,
1574 TAB_LEFT | TAT_TITLE,
1577 tab_float (tbl, col + 2,
1585 TAB_LEFT | TAT_TITLE,
1588 tab_float (tbl, col + 2,
1597 TAB_LEFT | TAT_TITLE,
1601 tab_float (tbl, col + 2,
1609 TAB_LEFT | TAT_TITLE,
1610 _("Interquartile Range"));
1613 struct percentile *p1;
1614 struct percentile *p2;
1617 p1 = hsh_find(m->ptile_hash, &d);
1620 p2 = hsh_find(m->ptile_hash, &d);
1625 tab_float (tbl, col + 2,
1636 TAB_LEFT | TAT_TITLE,
1640 tab_float (tbl, col + 2,
1646 /* stderr of skewness */
1647 tab_float (tbl, col + 3,
1656 TAB_LEFT | TAT_TITLE,
1660 tab_float (tbl, col + 2,
1666 /* stderr of kurtosis */
1667 tab_float (tbl, col + 3,
1679 box_plot_variables(const struct factor *fctr,
1680 const struct variable **vars, int n_vars,
1681 const struct variable *id)
1685 struct factor_statistics **fs ;
1689 box_plot_group(fctr, vars, n_vars, id);
1693 for ( fs = fctr->fs ; *fs ; ++fs )
1695 double y_min = DBL_MAX;
1696 double y_max = -DBL_MAX;
1697 struct chart *ch = chart_create();
1698 const char *s = factor_to_string(fctr, *fs, 0 );
1700 chart_write_title(ch, s);
1702 for ( i = 0 ; i < n_vars ; ++i )
1704 y_max = max(y_max, (*fs)->m[i].max);
1705 y_min = min(y_min, (*fs)->m[i].min);
1708 boxplot_draw_yscale(ch, y_max, y_min);
1710 for ( i = 0 ; i < n_vars ; ++i )
1713 const double box_width = (ch->data_right - ch->data_left)
1716 const double box_centre = ( i * 2 + 1) * box_width
1719 boxplot_draw_boxplot(ch,
1720 box_centre, box_width,
1722 var_to_string(vars[i]));
1734 /* Do a box plot, grouping all factors into one plot ;
1735 each dependent variable has its own plot.
1738 box_plot_group(const struct factor *fctr,
1739 const struct variable **vars,
1741 const struct variable *id UNUSED)
1746 for ( i = 0 ; i < n_vars ; ++i )
1748 struct factor_statistics **fs ;
1751 ch = chart_create();
1753 boxplot_draw_yscale(ch, totals[i].max, totals[i].min);
1759 for ( fs = fctr->fs ; *fs ; ++fs )
1762 chart_write_title(ch, _("Boxplot of %s vs. %s"),
1763 var_to_string(vars[i]), var_to_string(fctr->indep_var[0]) );
1765 for ( fs = fctr->fs ; *fs ; ++fs )
1768 const char *s = factor_to_string_concise(fctr, *fs);
1770 const double box_width = (ch->data_right - ch->data_left)
1771 / (n_factors * 2.0 ) ;
1773 const double box_centre = ( f++ * 2 + 1) * box_width
1776 boxplot_draw_boxplot(ch,
1777 box_centre, box_width,
1784 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1785 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1787 chart_write_title(ch, _("Boxplot"));
1789 boxplot_draw_boxplot(ch,
1790 box_centre, box_width,
1792 var_to_string(vars[i]) );
1801 /* Plot the normal and detrended normal plots for m
1802 Label the plots with factorname */
1804 np_plot(const struct metrics *m, const char *factorname)
1807 double yfirst=0, ylast=0;
1810 struct chart *np_chart;
1812 /* Detrended Normal Plot */
1813 struct chart *dnp_chart;
1815 /* The slope and intercept of the ideal normal probability line */
1816 const double slope = 1.0 / m->stddev;
1817 const double intercept = - m->mean / m->stddev;
1819 /* Cowardly refuse to plot an empty data set */
1820 if ( m->n_data == 0 )
1823 np_chart = chart_create();
1824 dnp_chart = chart_create();
1826 if ( !np_chart || ! dnp_chart )
1829 chart_write_title(np_chart, _("Normal Q-Q Plot of %s"), factorname);
1830 chart_write_xlabel(np_chart, _("Observed Value"));
1831 chart_write_ylabel(np_chart, _("Expected Normal"));
1834 chart_write_title(dnp_chart, _("Detrended Normal Q-Q Plot of %s"),
1836 chart_write_xlabel(dnp_chart, _("Observed Value"));
1837 chart_write_ylabel(dnp_chart, _("Dev from Normal"));
1839 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1840 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1844 /* Need to make sure that both the scatter plot and the ideal fit into the
1846 double x_lower = min(m->min, (yfirst - intercept) / slope) ;
1847 double x_upper = max(m->max, (ylast - intercept) / slope) ;
1848 double slack = (x_upper - x_lower) * 0.05 ;
1850 chart_write_xscale(np_chart, x_lower - slack, x_upper + slack, 5);
1852 chart_write_xscale(dnp_chart, m->min, m->max, 5);
1856 chart_write_yscale(np_chart, yfirst, ylast, 5);
1859 /* We have to cache the detrended data, beacause we need to
1860 find its limits before we can plot it */
1861 double *d_data = xmalloc (m->n_data * sizeof(double));
1862 double d_max = -DBL_MAX;
1863 double d_min = DBL_MAX;
1864 for ( i = 0 ; i < m->n_data; ++i )
1866 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1868 chart_datum(np_chart, 0, m->wvp[i]->v.f, ns);
1870 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1872 if ( d_data[i] < d_min ) d_min = d_data[i];
1873 if ( d_data[i] > d_max ) d_max = d_data[i];
1875 chart_write_yscale(dnp_chart, d_min, d_max, 5);
1877 for ( i = 0 ; i < m->n_data; ++i )
1878 chart_datum(dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1883 chart_line(np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1884 chart_line(dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1886 chart_submit(np_chart);
1887 chart_submit(dnp_chart);
1893 /* Show the percentiles */
1895 show_percentiles(struct variable **dependent_var,
1897 struct factor *fctr)
1899 struct tab_table *tbl;
1905 struct hsh_table *ptiles ;
1907 int n_heading_columns;
1908 const int n_heading_rows = 2;
1909 const int n_stat_rows = 2;
1915 struct factor_statistics **fs = fctr->fs ;
1916 n_heading_columns = 3;
1917 n_factors = hsh_count(fctr->fstats);
1919 ptiles = (*fs)->m[0].ptile_hash;
1921 if ( fctr->indep_var[1] )
1922 n_heading_columns = 4;
1927 n_heading_columns = 2;
1929 ptiles = totals[0].ptile_hash;
1932 n_ptiles = hsh_count(ptiles);
1934 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1936 n_cols = n_heading_columns + n_ptiles ;
1938 tbl = tab_create (n_cols, n_rows, 0);
1940 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1942 tab_dim (tbl, tab_natural_dimensions);
1944 /* Outline the box and have no internal lines*/
1949 n_cols - 1, n_rows - 1);
1951 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
1953 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
1956 tab_title (tbl, 0, _("Percentiles"));
1959 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
1966 n_heading_columns - 1, n_rows - 1);
1972 n_heading_columns, n_heading_rows - 1,
1973 n_cols - 1, n_rows - 1);
1975 tab_joint_text(tbl, n_heading_columns + 1, 0,
1977 TAB_CENTER | TAT_TITLE ,
1982 /* Put in the percentile break points as headings */
1984 struct percentile **p = (struct percentile **) hsh_sort(ptiles);
1989 tab_float(tbl, n_heading_columns + i++ , 1,
1998 for ( i = 0 ; i < n_dep_var ; ++i )
2000 const int n_stat_rows = 2;
2001 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
2004 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
2007 i * n_stat_rows * n_factors + n_heading_rows,
2008 TAB_LEFT | TAT_TITLE,
2009 var_to_string(dependent_var[i])
2014 struct factor_statistics **fs = fctr->fs;
2017 tab_text (tbl, 1, n_heading_rows - 1,
2018 TAB_CENTER | TAT_TITLE,
2019 var_to_string(fctr->indep_var[0]));
2022 if ( fctr->indep_var[1])
2023 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2024 var_to_string(fctr->indep_var[1]));
2029 static union value prev ;
2031 const int row = n_heading_rows + n_stat_rows *
2032 ( ( i * n_factors ) + count );
2035 if ( 0 != compare_values(&prev, &(*fs)->id[0],
2036 fctr->indep_var[0]->width))
2040 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2044 TAB_LEFT | TAT_TITLE,
2045 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
2051 prev = (*fs)->id[0];
2053 if (fctr->indep_var[1] && count > 0 )
2054 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
2056 if ( fctr->indep_var[1])
2057 tab_text (tbl, 2, row,
2058 TAB_LEFT | TAT_TITLE,
2059 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
2063 populate_percentiles(tbl, n_heading_columns - 1,
2075 populate_percentiles(tbl, n_heading_columns - 1,
2076 i * n_stat_rows * n_factors + n_heading_rows,
2093 populate_percentiles(struct tab_table *tbl, int col, int row,
2094 const struct metrics *m)
2098 struct percentile **p = (struct percentile **) hsh_sort(m->ptile_hash);
2102 TAB_LEFT | TAT_TITLE,
2103 _("Tukey\'s Hinges")
2108 TAB_LEFT | TAT_TITLE,
2109 ptile_alg_desc[m->ptile_alg]
2116 tab_float(tbl, col + i + 1 , row,
2119 if ( (*p)->p == 25 )
2120 tab_float(tbl, col + i + 1 , row + 1,
2124 if ( (*p)->p == 50 )
2125 tab_float(tbl, col + i + 1 , row + 1,
2129 if ( (*p)->p == 75 )
2130 tab_float(tbl, col + i + 1 , row + 1,
2145 factor_to_string(const struct factor *fctr,
2146 struct factor_statistics *fs,
2147 const struct variable *var)
2150 static char buf1[100];
2156 sprintf(buf1, "%s (",var_to_string(var) );
2159 snprintf(buf2, 100, "%s = %s",
2160 var_to_string(fctr->indep_var[0]),
2161 value_to_string(&fs->id[0],fctr->indep_var[0]));
2165 if ( fctr->indep_var[1] )
2167 sprintf(buf2, "; %s = %s)",
2168 var_to_string(fctr->indep_var[1]),
2169 value_to_string(&fs->id[1],
2170 fctr->indep_var[1]));
2185 factor_to_string_concise(const struct factor *fctr,
2186 struct factor_statistics *fs)
2190 static char buf[100];
2194 snprintf(buf, 100, "%s",
2195 value_to_string(&fs->id[0], fctr->indep_var[0]));
2197 if ( fctr->indep_var[1] )
2199 sprintf(buf2, ",%s)", value_to_string(&fs->id[1], fctr->indep_var[1]) );