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 static struct hsh_table *
376 list_to_ptile_hash(const subc_list_double *l)
380 struct hsh_table *h ;
382 h = hsh_create(subc_list_double_count(l),
383 (hsh_compare_func *) ptile_compare,
384 (hsh_hash_func *) ptile_hash,
385 (hsh_free_func *) free,
389 for ( i = 0 ; i < subc_list_double_count(l) ; ++i )
391 struct percentile *p = xmalloc (sizeof (struct percentile));
393 p->p = subc_list_double_at(l,i);
403 /* Parse the PERCENTILES subcommand */
405 xmn_custom_percentiles(struct cmd_examine *p UNUSED)
413 while ( lex_double_p() )
415 subc_list_double_push(&percentile_list,lex_double());
425 if ( lex_match_id("HAVERAGE"))
426 percentile_algorithm = PC_HAVERAGE;
428 else if ( lex_match_id("WAVERAGE"))
429 percentile_algorithm = PC_WAVERAGE;
431 else if ( lex_match_id("ROUND"))
432 percentile_algorithm = PC_ROUND;
434 else if ( lex_match_id("EMPIRICAL"))
435 percentile_algorithm = PC_EMPIRICAL;
437 else if ( lex_match_id("AEMPIRICAL"))
438 percentile_algorithm = PC_AEMPIRICAL;
440 else if ( lex_match_id("NONE"))
441 percentile_algorithm = PC_NONE;
444 if ( 0 == subc_list_double_count(&percentile_list))
446 subc_list_double_push(&percentile_list, 5);
447 subc_list_double_push(&percentile_list, 10);
448 subc_list_double_push(&percentile_list, 25);
449 subc_list_double_push(&percentile_list, 50);
450 subc_list_double_push(&percentile_list, 75);
451 subc_list_double_push(&percentile_list, 90);
452 subc_list_double_push(&percentile_list, 95);
458 /* TOTAL and NOTOTAL are simple, mutually exclusive flags */
460 xmn_custom_total(struct cmd_examine *p)
462 if ( p->sbc_nototal )
464 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
472 xmn_custom_nototal(struct cmd_examine *p)
476 msg (SE, _("%s and %s are mutually exclusive"),"TOTAL","NOTOTAL");
485 /* Parser for the variables sub command
486 Returns 1 on success */
488 xmn_custom_variables(struct cmd_examine *cmd )
492 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
498 if (!parse_variables (default_dict, &dependent_vars, &n_dependent_vars,
499 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
501 free (dependent_vars);
505 assert(n_dependent_vars);
507 totals = xmalloc( sizeof(struct metrics) * n_dependent_vars);
509 if ( lex_match(T_BY))
512 success = examine_parse_independent_vars(cmd);
513 if ( success != 1 ) {
514 free (dependent_vars);
525 /* Parse the clause specifying the factors */
527 examine_parse_independent_vars(struct cmd_examine *cmd)
530 struct factor *sf = xmalloc(sizeof(struct factor));
532 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
540 sf->indep_var[0] = parse_variable();
541 sf->indep_var[1] = 0;
548 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
555 sf->indep_var[1] = parse_variable();
560 sf->fstats = hsh_create(4,
561 (hsh_compare_func *) factor_statistics_compare,
562 (hsh_hash_func *) factor_statistics_hash,
563 (hsh_free_func *) factor_statistics_free,
571 if ( token == '.' || token == '/' )
574 success = examine_parse_independent_vars(cmd);
585 void populate_percentiles(struct tab_table *tbl, int col, int row,
586 const struct metrics *m);
588 void populate_descriptives(struct tab_table *t, int col, int row,
589 const struct metrics *fs);
591 void populate_extremes(struct tab_table *t, int col, int row, int n,
592 const struct metrics *m);
594 void populate_summary(struct tab_table *t, int col, int row,
595 const struct metrics *m);
600 static int bad_weight_warn = 1;
603 /* Perform calculations for the sub factors */
605 factor_calc(struct ccase *c, int case_no, double weight, int case_missing)
608 struct factor *fctr = factors;
612 union value indep_vals[2] ;
614 indep_vals[0] = * case_data(c, fctr->indep_var[0]->fv);
616 if ( fctr->indep_var[1] )
617 indep_vals[1] = * case_data(c, fctr->indep_var[1]->fv);
619 indep_vals[1].f = SYSMIS;
621 assert(fctr->fstats);
623 struct factor_statistics **foo = ( struct factor_statistics ** )
624 hsh_probe(fctr->fstats, (void *) &indep_vals);
629 *foo = create_factor_statistics(n_dependent_vars,
633 for ( v = 0 ; v < n_dependent_vars ; ++v )
635 metrics_precalc( &(*foo)->m[v] );
640 for ( v = 0 ; v < n_dependent_vars ; ++v )
642 const struct variable *var = dependent_vars[v];
643 const union value *val = case_data (c, var->fv);
645 if ( value_is_missing(val,var) || case_missing )
648 metrics_calc( &(*foo)->m[v], val, weight, case_no);
662 run_examine(const struct casefile *cf, void *cmd_ )
664 struct casereader *r;
668 const struct cmd_examine *cmd = (struct cmd_examine *) cmd_;
670 /* Make sure we haven't got rubbish left over from a
673 struct factor *fctr = factors;
676 struct factor *next = fctr->next;
678 hsh_clear(fctr->fstats);
687 for ( v = 0 ; v < n_dependent_vars ; ++v )
688 metrics_precalc(&totals[v]);
690 for(r = casefile_get_reader (cf);
691 casereader_read (r, &c) ;
695 const int case_no = casereader_cnum(r);
697 const double weight =
698 dict_get_case_weight(default_dict, &c, &bad_weight_warn);
700 if ( cmd->miss == XMN_LISTWISE )
702 for ( v = 0 ; v < n_dependent_vars ; ++v )
704 const struct variable *var = dependent_vars[v];
705 const union value *val = case_data (&c, var->fv);
707 if ( value_is_missing(val,var))
713 for ( v = 0 ; v < n_dependent_vars ; ++v )
715 const struct variable *var = dependent_vars[v];
716 const union value *val = case_data (&c, var->fv);
718 if ( value_is_missing(val,var) || case_missing )
721 metrics_calc(&totals[v], val, weight, case_no);
725 factor_calc(&c, case_no, weight, case_missing);
730 for ( v = 0 ; v < n_dependent_vars ; ++v)
735 struct hsh_iterator hi;
736 struct factor_statistics *fs;
738 for ( fs = hsh_first(fctr->fstats, &hi);
740 fs = hsh_next(fctr->fstats, &hi))
743 fs->m[v].ptile_hash = list_to_ptile_hash(&percentile_list);
744 fs->m[v].ptile_alg = percentile_algorithm;
745 metrics_postcalc(&fs->m[v]);
751 totals[v].ptile_hash = list_to_ptile_hash(&percentile_list);
752 totals[v].ptile_alg = percentile_algorithm;
753 metrics_postcalc(&totals[v]);
757 /* Make sure that the combination of factors are complete */
762 struct hsh_iterator hi;
763 struct hsh_iterator hi0;
764 struct hsh_iterator hi1;
765 struct factor_statistics *fs;
767 struct hsh_table *idh0=0;
768 struct hsh_table *idh1=0;
772 idh0 = hsh_create(4, (hsh_compare_func *) compare_values,
773 (hsh_hash_func *) hash_value,
776 idh1 = hsh_create(4, (hsh_compare_func *) compare_values,
777 (hsh_hash_func *) hash_value,
781 for ( fs = hsh_first(fctr->fstats, &hi);
783 fs = hsh_next(fctr->fstats, &hi))
785 hsh_insert(idh0,(void *) &fs->id[0]);
786 hsh_insert(idh1,(void *) &fs->id[1]);
789 /* Ensure that the factors combination is complete */
790 for ( val0 = hsh_first(idh0, &hi0);
792 val0 = hsh_next(idh0, &hi0))
794 for ( val1 = hsh_first(idh1, &hi1);
796 val1 = hsh_next(idh1, &hi1))
798 struct factor_statistics **ffs;
803 ffs = (struct factor_statistics **)
804 hsh_probe(fctr->fstats, (void *) &key );
808 (*ffs) = create_factor_statistics (n_dependent_vars,
810 for ( i = 0 ; i < n_dependent_vars ; ++i )
811 metrics_precalc( &(*ffs)->m[i]);
819 fctr->fs = (struct factor_statistics **) hsh_sort_copy(fctr->fstats);
830 for ( i = 0 ; i < n_dependent_vars ; ++i )
832 metrics_destroy(&totals[i]);
840 show_summary(struct variable **dependent_var, int n_dep_var,
841 const struct factor *fctr)
843 static const char *subtitle[]=
851 int heading_columns ;
853 const int heading_rows = 3;
854 struct tab_table *tbl;
862 n_factors = hsh_count(fctr->fstats);
863 n_rows = n_dep_var * n_factors ;
865 if ( fctr->indep_var[1] )
874 n_rows += heading_rows;
876 n_cols = heading_columns + 6;
878 tbl = tab_create (n_cols,n_rows,0);
879 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
881 tab_dim (tbl, tab_natural_dimensions);
883 /* Outline the box */
888 n_cols - 1, n_rows - 1);
890 /* Vertical lines for the data only */
895 n_cols - 1, n_rows - 1);
898 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
899 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, 1 );
900 tab_hline (tbl, TAL_1, heading_columns, n_cols - 1, heading_rows -1 );
902 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
905 tab_title (tbl, 0, _("Case Processing Summary"));
908 tab_joint_text(tbl, heading_columns, 0,
910 TAB_CENTER | TAT_TITLE,
913 /* Remove lines ... */
920 for ( i = 0 ; i < 3 ; ++i )
922 tab_text (tbl, heading_columns + i*2 , 2, TAB_CENTER | TAT_TITLE,
925 tab_text (tbl, heading_columns + i*2 + 1, 2, TAB_CENTER | TAT_TITLE,
928 tab_joint_text(tbl, heading_columns + i*2 , 1,
929 heading_columns + i*2 + 1, 1,
930 TAB_CENTER | TAT_TITLE,
933 tab_box (tbl, -1, -1,
935 heading_columns + i*2, 1,
936 heading_columns + i*2 + 1, 1);
941 /* Titles for the independent variables */
944 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
945 var_to_string(fctr->indep_var[0]));
947 if ( fctr->indep_var[1] )
949 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
950 var_to_string(fctr->indep_var[1]));
956 for ( i = 0 ; i < n_dep_var ; ++i )
960 n_factors = hsh_count(fctr->fstats);
964 tab_hline(tbl, TAL_1, 0, n_cols -1 , i * n_factors + heading_rows);
967 0, i * n_factors + heading_rows,
968 TAB_LEFT | TAT_TITLE,
969 var_to_string(dependent_var[i])
974 populate_summary(tbl, heading_columns,
975 (i * n_factors) + heading_rows,
981 struct factor_statistics **fs = fctr->fs;
986 static union value prev;
988 if ( 0 != compare_values(&prev, &(*fs)->id[0],
989 fctr->indep_var[0]->width))
993 (i * n_factors ) + count +
995 TAB_LEFT | TAT_TITLE,
996 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
999 if (fctr->indep_var[1] && count > 0 )
1000 tab_hline(tbl, TAL_1, 1, n_cols - 1,
1001 (i * n_factors ) + count + heading_rows);
1005 prev = (*fs)->id[0];
1008 if ( fctr->indep_var[1])
1011 (i * n_factors ) + count +
1013 TAB_LEFT | TAT_TITLE,
1014 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1017 populate_summary(tbl, heading_columns,
1018 (i * n_factors) + count
1033 populate_summary(struct tab_table *t, int col, int row,
1034 const struct metrics *m)
1037 const double total = m->n + m->n_missing ;
1039 tab_float(t, col + 0, row + 0, TAB_RIGHT, m->n, 8, 0);
1040 tab_float(t, col + 2, row + 0, TAB_RIGHT, m->n_missing, 8, 0);
1041 tab_float(t, col + 4, row + 0, TAB_RIGHT, total, 8, 0);
1045 tab_text (t, col + 1, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1046 100.0 * m->n / total );
1048 tab_text (t, col + 3, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1049 100.0 * m->n_missing / total );
1051 /* This seems a bit pointless !!! */
1052 tab_text (t, col + 5, row + 0, TAB_RIGHT | TAT_PRINTF, "%2.0f%%",
1053 100.0 * total / total );
1064 show_extremes(struct variable **dependent_var, int n_dep_var,
1065 const struct factor *fctr, int n_extremities)
1068 int heading_columns ;
1070 const int heading_rows = 1;
1071 struct tab_table *tbl;
1078 heading_columns = 2;
1079 n_factors = hsh_count(fctr->fstats);
1081 n_rows = n_dep_var * 2 * n_extremities * n_factors;
1083 if ( fctr->indep_var[1] )
1084 heading_columns = 3;
1088 heading_columns = 1;
1089 n_rows = n_dep_var * 2 * n_extremities;
1092 n_rows += heading_rows;
1094 heading_columns += 2;
1095 n_cols = heading_columns + 2;
1097 tbl = tab_create (n_cols,n_rows,0);
1098 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
1100 tab_dim (tbl, tab_natural_dimensions);
1102 /* Outline the box, No internal lines*/
1107 n_cols - 1, n_rows - 1);
1109 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1111 tab_title (tbl, 0, _("Extreme Values"));
1113 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows -1);
1114 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows -1);
1118 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1119 var_to_string(fctr->indep_var[0]));
1121 if ( fctr->indep_var[1] )
1122 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1123 var_to_string(fctr->indep_var[1]));
1126 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Value"));
1127 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Case Number"));
1129 for ( i = 0 ; i < n_dep_var ; ++i )
1133 tab_hline(tbl, TAL_1, 0, n_cols -1 ,
1134 i * 2 * n_extremities * n_factors + heading_rows);
1137 i * 2 * n_extremities * n_factors + heading_rows,
1138 TAB_LEFT | TAT_TITLE,
1139 var_to_string(dependent_var[i])
1144 populate_extremes(tbl, heading_columns - 2,
1145 i * 2 * n_extremities * n_factors + heading_rows,
1146 n_extremities, &totals[i]);
1150 struct factor_statistics **fs = fctr->fs;
1155 static union value prev ;
1157 const int row = heading_rows + ( 2 * n_extremities ) *
1158 ( ( i * n_factors ) + count );
1161 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1162 fctr->indep_var[0]->width))
1166 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1170 TAB_LEFT | TAT_TITLE,
1171 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1175 prev = (*fs)->id[0];
1177 if (fctr->indep_var[1] && count > 0 )
1178 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1180 if ( fctr->indep_var[1])
1181 tab_text (tbl, 2, row,
1182 TAB_LEFT | TAT_TITLE,
1183 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1186 populate_extremes(tbl, heading_columns - 2,
1201 /* Fill in the extremities table */
1203 populate_extremes(struct tab_table *t,
1204 int col, int row, int n, const struct metrics *m)
1210 tab_text(t, col, row,
1211 TAB_RIGHT | TAT_TITLE ,
1215 tab_text(t, col, row + n ,
1216 TAB_RIGHT | TAT_TITLE ,
1221 tab_hline(t, TAL_1, col, col + 3, row + n );
1223 for (extremity = 0; extremity < n ; ++extremity )
1226 tab_float(t, col + 1, row + extremity,
1228 extremity + 1, 8, 0);
1232 tab_float(t, col + 1, row + extremity + n,
1234 extremity + 1, 8, 0);
1240 for (idx = 0, extremity = 0; extremity < n && idx < m->n_data ; ++idx )
1243 const struct weighted_value *wv = m->wvp[idx];
1244 struct case_node *cn = wv->case_nos;
1247 for (j = 0 ; j < wv->w ; ++j )
1249 if ( extremity + j >= n )
1252 tab_float(t, col + 3, row + extremity + j + n,
1256 tab_float(t, col + 2, row + extremity + j + n,
1265 extremity += wv->w ;
1270 for (idx = m->n_data - 1, extremity = 0; extremity < n && idx >= 0; --idx )
1273 const struct weighted_value *wv = m->wvp[idx];
1274 struct case_node *cn = wv->case_nos;
1276 for (j = 0 ; j < wv->w ; ++j )
1278 if ( extremity + j >= n )
1281 tab_float(t, col + 3, row + extremity + j,
1285 tab_float(t, col + 2, row + extremity + j,
1294 extremity += wv->w ;
1299 /* Show the descriptives table */
1301 show_descriptives(struct variable **dependent_var,
1303 struct factor *fctr)
1306 int heading_columns ;
1308 const int n_stat_rows = 13;
1310 const int heading_rows = 1;
1312 struct tab_table *tbl;
1319 heading_columns = 4;
1320 n_factors = hsh_count(fctr->fstats);
1322 n_rows = n_dep_var * n_stat_rows * n_factors;
1324 if ( fctr->indep_var[1] )
1325 heading_columns = 5;
1329 heading_columns = 3;
1330 n_rows = n_dep_var * n_stat_rows;
1333 n_rows += heading_rows;
1335 n_cols = heading_columns + 2;
1338 tbl = tab_create (n_cols, n_rows, 0);
1340 tab_headers (tbl, heading_columns + 1, 0, heading_rows, 0);
1342 tab_dim (tbl, tab_natural_dimensions);
1344 /* Outline the box and have no internal lines*/
1349 n_cols - 1, n_rows - 1);
1351 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows );
1353 tab_vline (tbl, TAL_1, heading_columns, 0, n_rows - 1);
1354 tab_vline (tbl, TAL_2, n_cols - 2, 0, n_rows - 1);
1355 tab_vline (tbl, TAL_1, n_cols - 1, 0, n_rows - 1);
1357 tab_text (tbl, n_cols - 2, 0, TAB_CENTER | TAT_TITLE, _("Statistic"));
1358 tab_text (tbl, n_cols - 1, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
1360 tab_title (tbl, 0, _("Descriptives"));
1363 for ( i = 0 ; i < n_dep_var ; ++i )
1365 const int row = heading_rows + i * n_stat_rows * n_factors ;
1368 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
1371 i * n_stat_rows * n_factors + heading_rows,
1372 TAB_LEFT | TAT_TITLE,
1373 var_to_string(dependent_var[i])
1379 struct factor_statistics **fs = fctr->fs;
1382 tab_text (tbl, 1, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1383 var_to_string(fctr->indep_var[0]));
1386 if ( fctr->indep_var[1])
1387 tab_text (tbl, 2, heading_rows - 1, TAB_CENTER | TAT_TITLE,
1388 var_to_string(fctr->indep_var[1]));
1393 static union value prev ;
1395 const int row = heading_rows + n_stat_rows *
1396 ( ( i * n_factors ) + count );
1399 if ( 0 != compare_values(&prev, &(*fs)->id[0],
1400 fctr->indep_var[0]->width))
1404 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
1408 TAB_LEFT | TAT_TITLE,
1409 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
1413 prev = (*fs)->id[0];
1415 if (fctr->indep_var[1] && count > 0 )
1416 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
1418 if ( fctr->indep_var[1])
1419 tab_text (tbl, 2, row,
1420 TAB_LEFT | TAT_TITLE,
1421 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
1424 populate_descriptives(tbl, heading_columns - 2,
1436 populate_descriptives(tbl, heading_columns - 2,
1437 i * n_stat_rows * n_factors + heading_rows,
1449 /* Fill in the descriptives data */
1451 populate_descriptives(struct tab_table *tbl, int col, int row,
1452 const struct metrics *m)
1455 const double t = gsl_cdf_tdist_Qinv(1 - cmd.n_cinterval[0]/100.0/2.0, \
1461 TAB_LEFT | TAT_TITLE,
1464 tab_float (tbl, col + 2,
1470 tab_float (tbl, col + 3,
1479 TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1480 _("%g%% Confidence Interval for Mean"), cmd.n_cinterval[0]);
1483 tab_text (tbl, col + 1,
1485 TAB_LEFT | TAT_TITLE,
1488 tab_float (tbl, col + 2,
1491 m->mean - t * m->se_mean,
1494 tab_text (tbl, col + 1,
1496 TAB_LEFT | TAT_TITLE,
1500 tab_float (tbl, col + 2,
1503 m->mean + t * m->se_mean,
1508 TAB_LEFT | TAT_TITLE,
1509 _("5% Trimmed Mean"));
1511 tab_float (tbl, col + 2,
1519 TAB_LEFT | TAT_TITLE,
1523 struct percentile *p;
1526 p = hsh_find(m->ptile_hash, &d);
1530 tab_float (tbl, col + 2,
1539 TAB_LEFT | TAT_TITLE,
1542 tab_float (tbl, col + 2,
1551 TAB_LEFT | TAT_TITLE,
1552 _("Std. Deviation"));
1555 tab_float (tbl, col + 2,
1564 TAB_LEFT | TAT_TITLE,
1567 tab_float (tbl, col + 2,
1575 TAB_LEFT | TAT_TITLE,
1578 tab_float (tbl, col + 2,
1587 TAB_LEFT | TAT_TITLE,
1591 tab_float (tbl, col + 2,
1599 TAB_LEFT | TAT_TITLE,
1600 _("Interquartile Range"));
1603 struct percentile *p1;
1604 struct percentile *p2;
1607 p1 = hsh_find(m->ptile_hash, &d);
1610 p2 = hsh_find(m->ptile_hash, &d);
1615 tab_float (tbl, col + 2,
1626 TAB_LEFT | TAT_TITLE,
1630 tab_float (tbl, col + 2,
1636 /* stderr of skewness */
1637 tab_float (tbl, col + 3,
1646 TAB_LEFT | TAT_TITLE,
1650 tab_float (tbl, col + 2,
1656 /* stderr of kurtosis */
1657 tab_float (tbl, col + 3,
1669 box_plot_variables(const struct factor *fctr,
1670 const struct variable **vars, int n_vars,
1671 const struct variable *id)
1675 struct factor_statistics **fs ;
1679 box_plot_group(fctr, vars, n_vars, id);
1683 for ( fs = fctr->fs ; *fs ; ++fs )
1685 double y_min = DBL_MAX;
1686 double y_max = -DBL_MAX;
1689 ch = chart_create();
1691 const char *s = factor_to_string(fctr, *fs, 0 );
1693 chart_write_title(ch, s);
1695 for ( i = 0 ; i < n_vars ; ++i )
1697 y_max = max(y_max, (*fs)->m[i].max);
1698 y_min = min(y_min, (*fs)->m[i].min);
1701 boxplot_draw_yscale(ch, y_max, y_min);
1703 for ( i = 0 ; i < n_vars ; ++i )
1706 const double box_width = (ch->data_right - ch->data_left)
1709 const double box_centre = ( i * 2 + 1) * box_width
1712 boxplot_draw_boxplot(ch,
1713 box_centre, box_width,
1715 var_to_string(vars[i]));
1727 /* Do a box plot, grouping all factors into one plot ;
1728 each dependent variable has its own plot.
1731 box_plot_group(const struct factor *fctr,
1732 const struct variable **vars,
1734 const struct variable *id UNUSED)
1739 for ( i = 0 ; i < n_vars ; ++i )
1741 struct factor_statistics **fs ;
1744 ch = chart_create();
1746 boxplot_draw_yscale(ch, totals[i].max, totals[i].min);
1752 for ( fs = fctr->fs ; *fs ; ++fs )
1755 chart_write_title(ch, _("Boxplot of %s vs. %s"),
1756 var_to_string(vars[i]), var_to_string(fctr->indep_var[0]) );
1758 for ( fs = fctr->fs ; *fs ; ++fs )
1761 const char *s = factor_to_string_concise(fctr, *fs);
1763 const double box_width = (ch->data_right - ch->data_left)
1764 / (n_factors * 2.0 ) ;
1766 const double box_centre = ( f++ * 2 + 1) * box_width
1769 boxplot_draw_boxplot(ch,
1770 box_centre, box_width,
1777 const double box_width = (ch->data_right - ch->data_left) / 3.0;
1778 const double box_centre = (ch->data_right + ch->data_left) / 2.0;
1780 chart_write_title(ch, _("Boxplot"));
1782 boxplot_draw_boxplot(ch,
1783 box_centre, box_width,
1785 var_to_string(vars[i]) );
1794 /* Plot the normal and detrended normal plots for m
1795 Label the plots with factorname */
1797 np_plot(const struct metrics *m, const char *factorname)
1800 double yfirst=0, ylast=0;
1803 struct chart *np_chart;
1805 /* Detrended Normal Plot */
1806 struct chart *dnp_chart;
1808 /* The slope and intercept of the ideal normal probability line */
1809 const double slope = 1.0 / m->stddev;
1810 const double intercept = - m->mean / m->stddev;
1812 /* Cowardly refuse to plot an empty data set */
1813 if ( m->n_data == 0 )
1816 np_chart = chart_create();
1817 dnp_chart = chart_create();
1819 if ( !np_chart || ! dnp_chart )
1822 chart_write_title(np_chart, _("Normal Q-Q Plot of %s"), factorname);
1823 chart_write_xlabel(np_chart, _("Observed Value"));
1824 chart_write_ylabel(np_chart, _("Expected Normal"));
1827 chart_write_title(dnp_chart, _("Detrended Normal Q-Q Plot of %s"),
1829 chart_write_xlabel(dnp_chart, _("Observed Value"));
1830 chart_write_ylabel(dnp_chart, _("Dev from Normal"));
1832 yfirst = gsl_cdf_ugaussian_Pinv (m->wvp[0]->rank / ( m->n + 1));
1833 ylast = gsl_cdf_ugaussian_Pinv (m->wvp[m->n_data-1]->rank / ( m->n + 1));
1837 /* Need to make sure that both the scatter plot and the ideal fit into the
1839 double x_lower = min(m->min, (yfirst - intercept) / slope) ;
1840 double x_upper = max(m->max, (ylast - intercept) / slope) ;
1841 double slack = (x_upper - x_lower) * 0.05 ;
1843 chart_write_xscale(np_chart, x_lower - slack, x_upper + slack, 5);
1845 chart_write_xscale(dnp_chart, m->min, m->max, 5);
1849 chart_write_yscale(np_chart, yfirst, ylast, 5);
1852 /* We have to cache the detrended data, beacause we need to
1853 find its limits before we can plot it */
1855 d_data = xmalloc (m->n_data * sizeof(double));
1856 double d_max = -DBL_MAX;
1857 double d_min = DBL_MAX;
1858 for ( i = 0 ; i < m->n_data; ++i )
1860 const double ns = gsl_cdf_ugaussian_Pinv (m->wvp[i]->rank / ( m->n + 1));
1862 chart_datum(np_chart, 0, m->wvp[i]->v.f, ns);
1864 d_data[i] = (m->wvp[i]->v.f - m->mean) / m->stddev - ns;
1866 if ( d_data[i] < d_min ) d_min = d_data[i];
1867 if ( d_data[i] > d_max ) d_max = d_data[i];
1869 chart_write_yscale(dnp_chart, d_min, d_max, 5);
1871 for ( i = 0 ; i < m->n_data; ++i )
1872 chart_datum(dnp_chart, 0, m->wvp[i]->v.f, d_data[i]);
1877 chart_line(np_chart, slope, intercept, yfirst, ylast , CHART_DIM_Y);
1878 chart_line(dnp_chart, 0, 0, m->min, m->max , CHART_DIM_X);
1880 chart_submit(np_chart);
1881 chart_submit(dnp_chart);
1887 /* Show the percentiles */
1889 show_percentiles(struct variable **dependent_var,
1891 struct factor *fctr)
1893 struct tab_table *tbl;
1899 struct hsh_table *ptiles ;
1901 int n_heading_columns;
1902 const int n_heading_rows = 2;
1903 const int n_stat_rows = 2;
1909 struct factor_statistics **fs = fctr->fs ;
1910 n_heading_columns = 3;
1911 n_factors = hsh_count(fctr->fstats);
1913 ptiles = (*fs)->m[0].ptile_hash;
1915 if ( fctr->indep_var[1] )
1916 n_heading_columns = 4;
1921 n_heading_columns = 2;
1923 ptiles = totals[0].ptile_hash;
1926 n_ptiles = hsh_count(ptiles);
1928 n_rows = n_heading_rows + n_dep_var * n_stat_rows * n_factors;
1930 n_cols = n_heading_columns + n_ptiles ;
1932 tbl = tab_create (n_cols, n_rows, 0);
1934 tab_headers (tbl, n_heading_columns + 1, 0, n_heading_rows, 0);
1936 tab_dim (tbl, tab_natural_dimensions);
1938 /* Outline the box and have no internal lines*/
1943 n_cols - 1, n_rows - 1);
1945 tab_hline (tbl, TAL_2, 0, n_cols - 1, n_heading_rows );
1947 tab_vline (tbl, TAL_2, n_heading_columns, 0, n_rows - 1);
1950 tab_title (tbl, 0, _("Percentiles"));
1953 tab_hline (tbl, TAL_1, n_heading_columns, n_cols - 1, 1 );
1960 n_heading_columns - 1, n_rows - 1);
1966 n_heading_columns, n_heading_rows - 1,
1967 n_cols - 1, n_rows - 1);
1969 tab_joint_text(tbl, n_heading_columns + 1, 0,
1971 TAB_CENTER | TAT_TITLE ,
1976 /* Put in the percentile break points as headings */
1978 struct percentile **p = (struct percentile **) hsh_sort(ptiles);
1983 tab_float(tbl, n_heading_columns + i++ , 1,
1992 for ( i = 0 ; i < n_dep_var ; ++i )
1994 const int n_stat_rows = 2;
1995 const int row = n_heading_rows + i * n_stat_rows * n_factors ;
1998 tab_hline(tbl, TAL_1, 0, n_cols - 1, row );
2001 i * n_stat_rows * n_factors + n_heading_rows,
2002 TAB_LEFT | TAT_TITLE,
2003 var_to_string(dependent_var[i])
2008 struct factor_statistics **fs = fctr->fs;
2011 tab_text (tbl, 1, n_heading_rows - 1,
2012 TAB_CENTER | TAT_TITLE,
2013 var_to_string(fctr->indep_var[0]));
2016 if ( fctr->indep_var[1])
2017 tab_text (tbl, 2, n_heading_rows - 1, TAB_CENTER | TAT_TITLE,
2018 var_to_string(fctr->indep_var[1]));
2023 static union value prev ;
2025 const int row = n_heading_rows + n_stat_rows *
2026 ( ( i * n_factors ) + count );
2029 if ( 0 != compare_values(&prev, &(*fs)->id[0],
2030 fctr->indep_var[0]->width))
2034 tab_hline (tbl, TAL_1, 1, n_cols - 1, row);
2038 TAB_LEFT | TAT_TITLE,
2039 value_to_string(&(*fs)->id[0], fctr->indep_var[0])
2045 prev = (*fs)->id[0];
2047 if (fctr->indep_var[1] && count > 0 )
2048 tab_hline(tbl, TAL_1, 2, n_cols - 1, row);
2050 if ( fctr->indep_var[1])
2051 tab_text (tbl, 2, row,
2052 TAB_LEFT | TAT_TITLE,
2053 value_to_string(&(*fs)->id[1], fctr->indep_var[1])
2057 populate_percentiles(tbl, n_heading_columns - 1,
2069 populate_percentiles(tbl, n_heading_columns - 1,
2070 i * n_stat_rows * n_factors + n_heading_rows,
2087 populate_percentiles(struct tab_table *tbl, int col, int row,
2088 const struct metrics *m)
2092 struct percentile **p = (struct percentile **) hsh_sort(m->ptile_hash);
2096 TAB_LEFT | TAT_TITLE,
2097 _("Tukey\'s Hinges")
2102 TAB_LEFT | TAT_TITLE,
2103 ptile_alg_desc[m->ptile_alg]
2110 tab_float(tbl, col + i + 1 , row,
2113 if ( (*p)->p == 25 )
2114 tab_float(tbl, col + i + 1 , row + 1,
2118 if ( (*p)->p == 50 )
2119 tab_float(tbl, col + i + 1 , row + 1,
2123 if ( (*p)->p == 75 )
2124 tab_float(tbl, col + i + 1 , row + 1,
2139 factor_to_string(const struct factor *fctr,
2140 struct factor_statistics *fs,
2141 const struct variable *var)
2144 static char buf1[100];
2150 sprintf(buf1, "%s (",var_to_string(var) );
2153 snprintf(buf2, 100, "%s = %s",
2154 var_to_string(fctr->indep_var[0]),
2155 value_to_string(&fs->id[0],fctr->indep_var[0]));
2159 if ( fctr->indep_var[1] )
2161 sprintf(buf2, "; %s = %s)",
2162 var_to_string(fctr->indep_var[1]),
2163 value_to_string(&fs->id[1],
2164 fctr->indep_var[1]));
2179 factor_to_string_concise(const struct factor *fctr,
2180 struct factor_statistics *fs)
2184 static char buf[100];
2188 snprintf(buf, 100, "%s",
2189 value_to_string(&fs->id[0], fctr->indep_var[0]));
2191 if ( fctr->indep_var[1] )
2193 sprintf(buf2, ",%s)", value_to_string(&fs->id[1], fctr->indep_var[1]) );