Instead of making system or portable file readers responsible for
[pspp-builds.git] / src / frequencies.q
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 /*
21   TODO:
22
23   * Remember that histograms, bar charts need mean, stddev.
24 */
25
26 #include <config.h>
27 #include "error.h"
28 #include <math.h>
29 #include <stdlib.h>
30 #include "alloc.h"
31 #include "bitvector.h"
32 #include "case.h"
33 #include "dictionary.h"
34 #include "hash.h"
35 #include "pool.h"
36 #include "command.h"
37 #include "lexer.h"
38 #include "moments.h"
39 #include "error.h"
40 #include "algorithm.h"
41 #include "magic.h"
42 #include "misc.h"
43 #include "output.h"
44 #include "som.h"
45 #include "str.h"
46 #include "tab.h"
47 #include "value-labels.h"
48 #include "var.h"
49 #include "vfm.h"
50 #include "settings.h"
51 #include "chart.h"
52 /* (headers) */
53
54 #include "debug-print.h"
55
56 /* (specification)
57    FREQUENCIES (frq_):
58      *variables=custom;
59      format=cond:condense/onepage(*n:onepage_limit,"%s>=0")/!standard,
60             table:limit(n:limit,"%s>0")/notable/!table, 
61             labels:!labels/nolabels,
62             sort:!avalue/dvalue/afreq/dfreq,
63             spaces:!single/double,
64             paging:newpage/!oldpage;
65      missing=miss:include/!exclude;
66      barchart(ba_)=:minimum(d:min),
67             :maximum(d:max),
68             scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0");
69      piechart(pie_)=:minimum(d:min),
70             :maximum(d:max),
71             missing:missing/!nomissing;
72      histogram(hi_)=:minimum(d:min),
73             :maximum(d:max),
74             scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
75             norm:!nonormal/normal,
76             incr:increment(d:inc,"%s>0");
77      hbar(hb_)=:minimum(d:min),
78             :maximum(d:max),
79             scale:freq(*n:freq,"%s>0")/percent(*n:pcnt,"%s>0"),
80             norm:!nonormal/normal,
81             incr:increment(d:inc,"%s>0");
82      grouped=custom;
83      ntiles=integer;
84      +percentiles = double list;
85      statistics[st_]=1|mean,2|semean,3|median,4|mode,5|stddev,6|variance,
86             7|kurtosis,8|skewness,9|range,10|minimum,11|maximum,12|sum,
87             13|default,14|seskewness,15|sekurtosis,all,none.
88 */
89 /* (declarations) */
90 /* (functions) */
91
92 /* Statistics. */
93 enum
94   {
95     frq_mean = 0, frq_semean, frq_median, frq_mode, frq_stddev, frq_variance,
96     frq_kurt, frq_sekurt, frq_skew, frq_seskew, frq_range, frq_min, frq_max,
97     frq_sum, frq_n_stats
98   };
99
100 /* Description of a statistic. */
101 struct frq_info
102   {
103     int st_indx;                /* Index into a_statistics[]. */
104     const char *s10;            /* Identifying string. */
105   };
106
107 /* Table of statistics, indexed by dsc_*. */
108 static struct frq_info st_name[frq_n_stats + 1] =
109 {
110   {FRQ_ST_MEAN, N_("Mean")},
111   {FRQ_ST_SEMEAN, N_("S.E. Mean")},
112   {FRQ_ST_MEDIAN, N_("Median")},
113   {FRQ_ST_MODE, N_("Mode")},
114   {FRQ_ST_STDDEV, N_("Std Dev")},
115   {FRQ_ST_VARIANCE, N_("Variance")},
116   {FRQ_ST_KURTOSIS, N_("Kurtosis")},
117   {FRQ_ST_SEKURTOSIS, N_("S.E. Kurt")},
118   {FRQ_ST_SKEWNESS, N_("Skewness")},
119   {FRQ_ST_SESKEWNESS, N_("S.E. Skew")},
120   {FRQ_ST_RANGE, N_("Range")},
121   {FRQ_ST_MINIMUM, N_("Minimum")},
122   {FRQ_ST_MAXIMUM, N_("Maximum")},
123   {FRQ_ST_SUM, N_("Sum")},
124   {-1, 0},
125 };
126
127 /* Percentiles to calculate. */
128
129 struct percentile
130 {
131   double p;        /* the %ile to be calculated */
132   double value;    /* the %ile's value */
133   double x1;       /* The datum value <= the percentile */
134   double x2;       /* The datum value >= the percentile */
135   int flag;        
136   int flag2;       /* Set to 1 if this percentile value has been found */
137 };
138
139
140 static void add_percentile (double x) ;
141
142 static struct percentile *percentiles;
143 static int n_percentiles;
144
145 static int implicit_50th ; 
146
147 /* Groups of statistics. */
148 #define BI          BIT_INDEX
149 #define frq_default                                                     \
150         (BI (frq_mean) | BI (frq_stddev) | BI (frq_min) | BI (frq_max))
151 #define frq_all                                                 \
152         (BI (frq_sum) | BI(frq_min) | BI(frq_max)               \
153          | BI(frq_mean) | BI(frq_semean) | BI(frq_stddev)       \
154          | BI(frq_variance) | BI(frq_kurt) | BI(frq_sekurt)     \
155          | BI(frq_skew) | BI(frq_seskew) | BI(frq_range)        \
156          | BI(frq_range) | BI(frq_mode) | BI(frq_median))
157
158 /* Statistics; number of statistics. */
159 static unsigned long stats;
160 static int n_stats;
161
162 /* Types of graphs. */
163 enum
164   {
165     GFT_NONE,                   /* Don't draw graphs. */
166     GFT_BAR,                    /* Draw bar charts. */
167     GFT_HIST,                   /* Draw histograms. */
168     GFT_PIE,                    /* Draw piechart */
169     GFT_HBAR                    /* Draw bar charts or histograms at our discretion. */
170   };
171
172 /* Parsed command. */
173 static struct cmd_frequencies cmd;
174
175 /* Summary of the barchart, histogram, and hbar subcommands. */
176 /* FIXME: These should not be mututally exclusive */
177 static int chart;               /* NONE/BAR/HIST/HBAR/PIE. */
178 static double min, max;         /* Minimum, maximum on y axis. */
179 static int format;              /* FREQ/PERCENT: Scaling of y axis. */
180 static double scale, incr;      /* FIXME */
181 static int normal;              /* FIXME */
182
183 /* Variables for which to calculate statistics. */
184 static int n_variables;
185 static struct variable **v_variables;
186
187 /* Arenas used to store semi-permanent storage. */
188 static struct pool *int_pool;   /* Integer mode. */
189 static struct pool *gen_pool;   /* General mode. */
190
191 /* Per-variable frequency data. */
192 struct var_freqs
193   {
194     /* Freqency table. */
195     struct freq_tab tab;        /* Frequencies table to use. */
196
197     /* Percentiles. */
198     int n_groups;               /* Number of groups. */
199     double *groups;             /* Groups. */
200
201     /* Statistics. */
202     double stat[frq_n_stats];
203   };
204
205 static inline struct var_freqs *
206 get_var_freqs (struct variable *v)
207 {
208   assert (v != NULL);
209   assert (v->aux != NULL);
210   return v->aux;
211 }
212
213 static void determine_charts (void);
214
215 static void calc_stats (struct variable *v, double d[frq_n_stats]);
216
217 static void precalc (void *);
218 static int calc (struct ccase *, void *);
219 static void postcalc (void *);
220
221 static void postprocess_freq_tab (struct variable *);
222 static void dump_full (struct variable *);
223 static void dump_condensed (struct variable *);
224 static void dump_statistics (struct variable *, int show_varname);
225 static void cleanup_freq_tab (struct variable *);
226
227 static hsh_hash_func hash_value_numeric, hash_value_alpha;
228 static hsh_compare_func compare_value_numeric_a, compare_value_alpha_a;
229 static hsh_compare_func compare_value_numeric_d, compare_value_alpha_d;
230 static hsh_compare_func compare_freq_numeric_a, compare_freq_alpha_a;
231 static hsh_compare_func compare_freq_numeric_d, compare_freq_alpha_d;
232 \f
233 /* Parser and outline. */
234
235 static int internal_cmd_frequencies (void);
236
237 int
238 cmd_frequencies (void)
239 {
240   int result;
241
242   int_pool = pool_create ();
243   result = internal_cmd_frequencies ();
244   pool_destroy (int_pool);
245   int_pool=0;
246   pool_destroy (gen_pool);
247   gen_pool=0;
248   free (v_variables);
249   v_variables=0;
250   return result;
251 }
252
253 static int
254 internal_cmd_frequencies (void)
255 {
256   int i;
257
258   n_percentiles = 0;
259   percentiles = NULL;
260
261   n_variables = 0;
262   v_variables = NULL;
263
264   if (!parse_frequencies (&cmd))
265     return CMD_FAILURE;
266
267   if (cmd.onepage_limit == NOT_LONG)
268     cmd.onepage_limit = 50;
269
270   /* Figure out statistics to calculate. */
271   stats = 0;
272   if (cmd.a_statistics[FRQ_ST_DEFAULT] || !cmd.sbc_statistics)
273     stats |= frq_default;
274   if (cmd.a_statistics[FRQ_ST_ALL])
275     stats |= frq_all;
276   if (cmd.sort != FRQ_AVALUE && cmd.sort != FRQ_DVALUE)
277     stats &= ~frq_median;
278   for (i = 0; i < frq_n_stats; i++)
279     if (cmd.a_statistics[st_name[i].st_indx])
280       stats |= BIT_INDEX (i);
281   if (stats & frq_kurt)
282     stats |= frq_sekurt;
283   if (stats & frq_skew)
284     stats |= frq_seskew;
285
286   /* Calculate n_stats. */
287   n_stats = 0;
288   for (i = 0; i < frq_n_stats; i++)
289     if ((stats & BIT_INDEX (i)))
290       n_stats++;
291
292   /* Charting. */
293   determine_charts ();
294   if (chart != GFT_NONE || cmd.sbc_ntiles)
295     cmd.sort = FRQ_AVALUE;
296
297   /* Work out what percentiles need to be calculated */
298   if ( cmd.sbc_percentiles ) 
299     {
300       for ( i = 0 ; i < MAXLISTS ; ++i ) 
301         {
302           int pl;
303           subc_list_double *ptl_list = &cmd.dl_percentiles[i];
304           for ( pl = 0 ; pl < subc_list_double_count(ptl_list); ++pl)
305               add_percentile(subc_list_double_at(ptl_list,pl) / 100.0 );
306         }
307     }
308   if ( cmd.sbc_ntiles ) 
309     {
310       for ( i = 0 ; i < cmd.sbc_ntiles ; ++i ) 
311         {
312           int j;
313           for (j = 0; j <= cmd.n_ntiles[i]; ++j ) 
314               add_percentile(j / (double) cmd.n_ntiles[i]);
315         }
316     }
317   
318
319   /* Do it! */
320   procedure_with_splits (precalc, calc, postcalc, NULL);
321
322   return CMD_SUCCESS;
323 }
324
325 /* Figure out which charts the user requested.  */
326 static void
327 determine_charts (void)
328 {
329   int count = (!!cmd.sbc_histogram) + (!!cmd.sbc_barchart) + 
330     (!!cmd.sbc_hbar) + (!!cmd.sbc_piechart);
331
332   if (!count)
333     {
334       chart = GFT_NONE;
335       return;
336     }
337   else if (count > 1)
338     {
339       chart = GFT_HBAR;
340       msg (SW, _("At most one of BARCHART, HISTOGRAM, or HBAR should be "
341            "given.  HBAR will be assumed.  Argument values will be "
342            "given precedence increasing along the order given."));
343     }
344   else if (cmd.sbc_histogram)
345     chart = GFT_HIST;
346   else if (cmd.sbc_barchart)
347     chart = GFT_BAR;
348   else if (cmd.sbc_piechart)
349     chart = GFT_PIE;
350   else
351     chart = GFT_HBAR;
352
353   min = max = SYSMIS;
354   format = FRQ_FREQ;
355   scale = SYSMIS;
356   incr = SYSMIS;
357   normal = 0;
358
359   if (cmd.sbc_barchart)
360     {
361       if (cmd.ba_min != SYSMIS)
362         min = cmd.ba_min;
363       if (cmd.ba_max != SYSMIS)
364         max = cmd.ba_max;
365       if (cmd.ba_scale == FRQ_FREQ)
366         {
367           format = FRQ_FREQ;
368           scale = cmd.ba_freq;
369         }
370       else if (cmd.ba_scale == FRQ_PERCENT)
371         {
372           format = FRQ_PERCENT;
373           scale = cmd.ba_pcnt;
374         }
375     }
376
377   if (cmd.sbc_histogram)
378     {
379       if (cmd.hi_min != SYSMIS)
380         min = cmd.hi_min;
381       if (cmd.hi_max != SYSMIS)
382         max = cmd.hi_max;
383       if (cmd.hi_scale == FRQ_FREQ)
384         {
385           format = FRQ_FREQ;
386           scale = cmd.hi_freq;
387         }
388       else if (cmd.hi_scale == FRQ_PERCENT)
389         {
390           format = FRQ_PERCENT;
391           scale = cmd.ba_pcnt;
392         }
393       if (cmd.hi_norm != FRQ_NONORMAL )
394         normal = 1;
395       if (cmd.hi_incr == FRQ_INCREMENT)
396         incr = cmd.hi_inc;
397     }
398
399   if (cmd.sbc_hbar)
400     {
401       if (cmd.hb_min != SYSMIS)
402         min = cmd.hb_min;
403       if (cmd.hb_max != SYSMIS)
404         max = cmd.hb_max;
405       if (cmd.hb_scale == FRQ_FREQ)
406         {
407           format = FRQ_FREQ;
408           scale = cmd.hb_freq;
409         }
410       else if (cmd.hb_scale == FRQ_PERCENT)
411         {
412           format = FRQ_PERCENT;
413           scale = cmd.ba_pcnt;
414         }
415       if (cmd.hb_norm)
416         normal = 1;
417       if (cmd.hb_incr == FRQ_INCREMENT)
418         incr = cmd.hb_inc;
419     }
420
421   if (min != SYSMIS && max != SYSMIS && min >= max)
422     {
423       msg (SE, _("MAX must be greater than or equal to MIN, if both are "
424            "specified.  However, MIN was specified as %g and MAX as %g.  "
425            "MIN and MAX will be ignored."), min, max);
426       min = max = SYSMIS;
427     }
428 }
429
430 /* Add data from case C to the frequency table. */
431 static int
432 calc (struct ccase *c, void *aux UNUSED)
433 {
434   double weight;
435   int i;
436   int bad_warn = 1;
437
438   weight = dict_get_case_weight (default_dict, c, &bad_warn);
439
440   for (i = 0; i < n_variables; i++)
441     {
442       struct variable *v = v_variables[i];
443       const union value *val = case_data (c, v->fv);
444       struct freq_tab *ft = &get_var_freqs (v)->tab;
445
446       switch (ft->mode)
447         {
448           case FRQM_GENERAL:
449             {
450
451               /* General mode. */
452               struct freq **fpp = (struct freq **) hsh_probe (ft->data, val);
453
454               if (*fpp != NULL)
455                 (*fpp)->c += weight;
456               else
457                 {
458                   struct freq *fp = *fpp = pool_alloc (gen_pool, sizeof *fp);
459                   fp->v = *val;
460                   fp->c = weight;
461                 }
462             }
463           break;
464         case FRQM_INTEGER:
465           /* Integer mode. */
466           if (val->f == SYSMIS)
467             ft->sysmis += weight;
468           else if (val->f > INT_MIN+1 && val->f < INT_MAX-1)
469             {
470               int i = val->f;
471               if (i >= ft->min && i <= ft->max)
472                 ft->vector[i - ft->min] += weight;
473             }
474           else
475             ft->out_of_range += weight;
476           break;
477         default:
478           assert (0);
479         }
480     }
481   return 1;
482 }
483
484 /* Prepares each variable that is the target of FREQUENCIES by setting
485    up its hash table. */
486 static void
487 precalc (void *aux UNUSED)
488 {
489   int i;
490
491   pool_destroy (gen_pool);
492   gen_pool = pool_create ();
493   
494   for (i = 0; i < n_variables; i++)
495     {
496       struct variable *v = v_variables[i];
497       struct freq_tab *ft = &get_var_freqs (v)->tab;
498
499       if (ft->mode == FRQM_GENERAL)
500         {
501           hsh_hash_func *hash;
502           hsh_compare_func *compare;
503
504           if (v->type == NUMERIC) 
505             {
506               hash = hash_value_numeric;
507               compare = compare_value_numeric_a; 
508             }
509           else 
510             {
511               hash = hash_value_alpha;
512               compare = compare_value_alpha_a;
513             }
514           ft->data = hsh_create (16, compare, hash, NULL, v);
515         }
516       else
517         {
518           int j;
519
520           for (j = (ft->max - ft->min); j >= 0; j--)
521             ft->vector[j] = 0.0;
522           ft->out_of_range = 0.0;
523           ft->sysmis = 0.0;
524         }
525     }
526 }
527
528 /* Finishes up with the variables after frequencies have been
529    calculated.  Displays statistics, percentiles, ... */
530 static void
531 postcalc (void *aux UNUSED)
532 {
533   int i;
534
535   for (i = 0; i < n_variables; i++)
536     {
537       struct variable *v = v_variables[i];
538       struct var_freqs *vf = get_var_freqs (v);
539       struct freq_tab *ft = &vf->tab;
540       int n_categories;
541       int dumped_freq_tab = 1;
542
543       postprocess_freq_tab (v);
544
545       /* Frequencies tables. */
546       n_categories = ft->n_valid + ft->n_missing;
547       if (cmd.table == FRQ_TABLE
548           || (cmd.table == FRQ_LIMIT && n_categories <= cmd.limit))
549         switch (cmd.cond)
550           {
551           case FRQ_CONDENSE:
552             dump_condensed (v);
553             break;
554           case FRQ_STANDARD:
555             dump_full (v);
556             break;
557           case FRQ_ONEPAGE:
558             if (n_categories > cmd.onepage_limit)
559               dump_condensed (v);
560             else
561               dump_full (v);
562             break;
563           default:
564             assert (0);
565           }
566       else
567         dumped_freq_tab = 0;
568
569       /* Statistics. */
570       if (n_stats)
571         dump_statistics (v, !dumped_freq_tab);
572
573
574       if ( chart == GFT_HIST) 
575         {
576           struct chart ch;
577           double d[frq_n_stats];
578           
579           struct normal_curve norm;
580           norm.N = vf->tab.total_cases;
581
582           calc_stats(v,d);
583           norm.mean = d[frq_mean];
584           norm.stddev = d[frq_stddev];
585
586           chart_initialise(&ch);
587           draw_histogram(&ch, v_variables[i], ft, "HISTOGRAM",&norm,normal);
588           chart_finalise(&ch);
589         }
590
591
592       if ( chart == GFT_PIE) 
593         {
594           struct chart ch;
595
596           chart_initialise(&ch);
597           
598           draw_piechart(&ch, v_variables[i], ft);
599
600           chart_finalise(&ch);
601         }
602
603
604       cleanup_freq_tab (v);
605
606     }
607 }
608
609 /* Returns the comparison function that should be used for
610    sorting a frequency table by FRQ_SORT using VAR_TYPE
611    variables. */
612 static hsh_compare_func *
613 get_freq_comparator (int frq_sort, int var_type) 
614 {
615   /* Note that q2c generates tags beginning with 1000. */
616   switch (frq_sort | (var_type << 16))
617     {
618     case FRQ_AVALUE | (NUMERIC << 16):  return compare_value_numeric_a;
619     case FRQ_AVALUE | (ALPHA << 16):    return compare_value_alpha_a;
620     case FRQ_DVALUE | (NUMERIC << 16):  return compare_value_numeric_d;
621     case FRQ_DVALUE | (ALPHA << 16):    return compare_value_alpha_d;
622     case FRQ_AFREQ | (NUMERIC << 16):   return compare_freq_numeric_a;
623     case FRQ_AFREQ | (ALPHA << 16):     return compare_freq_alpha_a;
624     case FRQ_DFREQ | (NUMERIC << 16):   return compare_freq_numeric_d;
625     case FRQ_DFREQ | (ALPHA << 16):     return compare_freq_alpha_d;
626     default: assert (0);
627     }
628
629   return 0;
630 }
631
632 /* Returns nonzero iff the value in struct freq F is non-missing
633    for variable V. */
634 static int
635 not_missing (const void *f_, void *v_) 
636 {
637   const struct freq *f = f_;
638   struct variable *v = v_;
639
640   return !is_missing (&f->v, v);
641 }
642
643 /* Summarizes the frequency table data for variable V. */
644 static void
645 postprocess_freq_tab (struct variable *v)
646 {
647   hsh_compare_func *compare;
648   struct freq_tab *ft;
649   size_t count;
650   void **data;
651   struct freq *freqs, *f;
652   size_t i;
653
654   ft = &get_var_freqs (v)->tab;
655   assert (ft->mode == FRQM_GENERAL);
656   compare = get_freq_comparator (cmd.sort, v->type);
657
658   /* Extract data from hash table. */
659   count = hsh_count (ft->data);
660   data = hsh_data (ft->data);
661
662   /* Copy dereferenced data into freqs. */
663   freqs = xmalloc (count * sizeof *freqs);
664   for (i = 0; i < count; i++) 
665     {
666       struct freq *f = data[i];
667       freqs[i] = *f; 
668     }
669
670   /* Put data into ft. */
671   ft->valid = freqs;
672   ft->n_valid = partition (freqs, count, sizeof *freqs, not_missing, v);
673   ft->missing = freqs + ft->n_valid;
674   ft->n_missing = count - ft->n_valid;
675
676   /* Sort data. */
677   sort (ft->valid, ft->n_valid, sizeof *ft->valid, compare, v);
678   sort (ft->missing, ft->n_missing, sizeof *ft->missing, compare, v);
679
680   /* Summary statistics. */
681   ft->valid_cases = 0.0;
682   for(i = 0 ;  i < ft->n_valid ; ++i ) 
683     {
684       f = &ft->valid[i];
685       ft->valid_cases += f->c;
686
687     }
688
689   ft->total_cases = ft->valid_cases ; 
690   for(i = 0 ;  i < ft->n_missing ; ++i ) 
691     {
692       f = &ft->missing[i];
693       ft->total_cases += f->c;
694     }
695
696 }
697
698 /* Frees the frequency table for variable V. */
699 static void
700 cleanup_freq_tab (struct variable *v)
701 {
702   struct freq_tab *ft = &get_var_freqs (v)->tab;
703   assert (ft->mode == FRQM_GENERAL);
704   free (ft->valid);
705   hsh_destroy (ft->data);
706 }
707
708 /* Parses the VARIABLES subcommand, adding to
709    {n_variables,v_variables}. */
710 static int
711 frq_custom_variables (struct cmd_frequencies *cmd UNUSED)
712 {
713   int mode;
714   int min = 0, max = 0;
715
716   int old_n_variables = n_variables;
717   int i;
718
719   lex_match ('=');
720   if (token != T_ALL && (token != T_ID
721                          || dict_lookup_var (default_dict, tokid) == NULL))
722     return 2;
723
724   if (!parse_variables (default_dict, &v_variables, &n_variables,
725                         PV_APPEND | PV_NO_SCRATCH))
726     return 0;
727
728   if (!lex_match ('('))
729     mode = FRQM_GENERAL;
730   else
731     {
732       mode = FRQM_INTEGER;
733       if (!lex_force_int ())
734         return 0;
735       min = lex_integer ();
736       lex_get ();
737       if (!lex_force_match (','))
738         return 0;
739       if (!lex_force_int ())
740         return 0;
741       max = lex_integer ();
742       lex_get ();
743       if (!lex_force_match (')'))
744         return 0;
745       if (max < min)
746         {
747           msg (SE, _("Upper limit of integer mode value range must be "
748                      "greater than lower limit."));
749           return 0;
750         }
751     }
752
753   for (i = old_n_variables; i < n_variables; i++)
754     {
755       struct variable *v = v_variables[i];
756       struct var_freqs *vf;
757
758       if (v->aux != NULL)
759         {
760           msg (SE, _("Variable %s specified multiple times on VARIABLES "
761                      "subcommand."), v->name);
762           return 0;
763         }
764       if (mode == FRQM_INTEGER && v->type != NUMERIC)
765         {
766           msg (SE, _("Integer mode specified, but %s is not a numeric "
767                      "variable."), v->name);
768           return 0;
769         }
770
771       vf = var_attach_aux (v, xmalloc (sizeof *vf), var_dtor_free);
772       vf->tab.mode = mode;
773       vf->tab.valid = vf->tab.missing = NULL;
774       if (mode == FRQM_INTEGER)
775         {
776           vf->tab.min = min;
777           vf->tab.max = max;
778           vf->tab.vector = pool_alloc (int_pool,
779                                        sizeof (struct freq) * (max - min + 1));
780         }
781       else
782         vf->tab.vector = NULL;
783       vf->n_groups = 0;
784       vf->groups = NULL;
785     }
786   return 1;
787 }
788
789 /* Parses the GROUPED subcommand, setting the n_grouped, grouped
790    fields of specified variables. */
791 static int
792 frq_custom_grouped (struct cmd_frequencies *cmd UNUSED)
793 {
794   lex_match ('=');
795   if ((token == T_ID && dict_lookup_var (default_dict, tokid) != NULL)
796       || token == T_ID)
797     for (;;)
798       {
799         int i;
800
801         /* Max, current size of list; list itself. */
802         int nl, ml;
803         double *dl;
804
805         /* Variable list. */
806         int n;
807         struct variable **v;
808
809         if (!parse_variables (default_dict, &v, &n,
810                               PV_NO_DUPLICATE | PV_NUMERIC))
811           return 0;
812         if (lex_match ('('))
813           {
814             nl = ml = 0;
815             dl = NULL;
816             while (token == T_NUM)
817               {
818                 if (nl >= ml)
819                   {
820                     ml += 16;
821                     dl = pool_realloc (int_pool, dl, ml * sizeof (double));
822                   }
823                 dl[nl++] = tokval;
824                 lex_get ();
825                 lex_match (',');
826               }
827             /* Note that nl might still be 0 and dl might still be
828                NULL.  That's okay. */
829             if (!lex_match (')'))
830               {
831                 free (v);
832                 msg (SE, _("`)' expected after GROUPED interval list."));
833                 return 0;
834               }
835           }
836         else 
837           {
838             nl = 0;
839             dl = NULL;
840           }
841
842         for (i = 0; i < n; i++)
843           if (v[i]->aux == NULL)
844             msg (SE, _("Variables %s specified on GROUPED but not on "
845                        "VARIABLES."), v[i]->name);
846           else 
847             {
848               struct var_freqs *vf = get_var_freqs (v[i]);
849                 
850               if (vf->groups != NULL)
851                 msg (SE, _("Variables %s specified multiple times on GROUPED "
852                            "subcommand."), v[i]->name);
853               else
854                 {
855                   vf->n_groups = nl;
856                   vf->groups = dl;
857                 }
858             }
859         free (v);
860         if (!lex_match ('/'))
861           break;
862         if ((token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
863             && token != T_ALL)
864           {
865             lex_put_back ('/');
866             break;
867           }
868       }
869
870   return 1;
871 }
872
873 /* Adds X to the list of percentiles, keeping the list in proper
874    order. */
875 static void
876 add_percentile (double x)
877 {
878   int i;
879
880   for (i = 0; i < n_percentiles; i++)
881     {
882       /* Do nothing if it's already in the list */
883       if ( fabs(x - percentiles[i].p) < DBL_EPSILON ) 
884         return;
885
886       if (x < percentiles[i].p)
887         break;
888     }
889
890   if (i >= n_percentiles || tokval != percentiles[i].p)
891     {
892       percentiles
893         = pool_realloc (int_pool, percentiles,
894                         (n_percentiles + 1) * sizeof (struct percentile ));
895
896       if (i < n_percentiles)
897           memmove (&percentiles[i + 1], &percentiles[i],
898                    (n_percentiles - i) * sizeof (struct percentile) );
899
900       percentiles[i].p = x;
901       n_percentiles++;
902     }
903 }
904
905 /* Comparison functions. */
906
907 /* Hash of numeric values. */
908 static unsigned
909 hash_value_numeric (const void *value_, void *foo UNUSED)
910 {
911   const struct freq *value = value_;
912   return hsh_hash_double (value->v.f);
913 }
914
915 /* Hash of string values. */
916 static unsigned
917 hash_value_alpha (const void *value_, void *v_)
918 {
919   const struct freq *value = value_;
920   struct variable *v = v_;
921
922   return hsh_hash_bytes (value->v.s, v->width);
923 }
924
925 /* Ascending numeric compare of values. */
926 static int
927 compare_value_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
928 {
929   const struct freq *a = a_;
930   const struct freq *b = b_;
931
932   if (a->v.f > b->v.f)
933     return 1;
934   else if (a->v.f < b->v.f)
935     return -1;
936   else
937     return 0;
938 }
939
940 /* Ascending string compare of values. */
941 static int
942 compare_value_alpha_a (const void *a_, const void *b_, void *v_)
943 {
944   const struct freq *a = a_;
945   const struct freq *b = b_;
946   const struct variable *v = v_;
947
948   return memcmp (a->v.s, b->v.s, v->width);
949 }
950
951 /* Descending numeric compare of values. */
952 static int
953 compare_value_numeric_d (const void *a, const void *b, void *foo UNUSED)
954 {
955   return -compare_value_numeric_a (a, b, foo);
956 }
957
958 /* Descending string compare of values. */
959 static int
960 compare_value_alpha_d (const void *a, const void *b, void *v)
961 {
962   return -compare_value_alpha_a (a, b, v);
963 }
964
965 /* Ascending numeric compare of frequency;
966    secondary key on ascending numeric value. */
967 static int
968 compare_freq_numeric_a (const void *a_, const void *b_, void *foo UNUSED)
969 {
970   const struct freq *a = a_;
971   const struct freq *b = b_;
972
973   if (a->c > b->c)
974     return 1;
975   else if (a->c < b->c)
976     return -1;
977
978   if (a->v.f > b->v.f)
979     return 1;
980   else if (a->v.f < b->v.f)
981     return -1;
982   else
983     return 0;
984 }
985
986 /* Ascending numeric compare of frequency;
987    secondary key on ascending string value. */
988 static int
989 compare_freq_alpha_a (const void *a_, const void *b_, void *v_)
990 {
991   const struct freq *a = a_;
992   const struct freq *b = b_;
993   const struct variable *v = v_;
994
995   if (a->c > b->c)
996     return 1;
997   else if (a->c < b->c)
998     return -1;
999   else
1000     return memcmp (a->v.s, b->v.s, v->width);
1001 }
1002
1003 /* Descending numeric compare of frequency;
1004    secondary key on ascending numeric value. */
1005 static int
1006 compare_freq_numeric_d (const void *a_, const void *b_, void *foo UNUSED)
1007 {
1008   const struct freq *a = a_;
1009   const struct freq *b = b_;
1010
1011   if (a->c > b->c)
1012     return -1;
1013   else if (a->c < b->c)
1014     return 1;
1015
1016   if (a->v.f > b->v.f)
1017     return 1;
1018   else if (a->v.f < b->v.f)
1019     return -1;
1020   else
1021     return 0;
1022 }
1023
1024 /* Descending numeric compare of frequency;
1025    secondary key on ascending string value. */
1026 static int
1027 compare_freq_alpha_d (const void *a_, const void *b_, void *v_)
1028 {
1029   const struct freq *a = a_;
1030   const struct freq *b = b_;
1031   const struct variable *v = v_;
1032
1033   if (a->c > b->c)
1034     return -1;
1035   else if (a->c < b->c)
1036     return 1;
1037   else
1038     return memcmp (a->v.s, b->v.s, v->width);
1039 }
1040 \f
1041 /* Frequency table display. */
1042
1043 /* Sets the widths of all the columns and heights of all the rows in
1044    table T for driver D. */
1045 static void
1046 full_dim (struct tab_table *t, struct outp_driver *d)
1047 {
1048   int lab = cmd.labels == FRQ_LABELS;
1049   int i;
1050
1051   if (lab)
1052     t->w[0] = min (tab_natural_width (t, d, 0), d->prop_em_width * 15);
1053   for (i = lab; i < lab + 5; i++)
1054     t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1055   for (i = 0; i < t->nr; i++)
1056     t->h[i] = d->font_height;
1057 }
1058
1059 /* Displays a full frequency table for variable V. */
1060 static void
1061 dump_full (struct variable *v)
1062 {
1063   int n_categories;
1064   struct freq_tab *ft;
1065   struct freq *f;
1066   struct tab_table *t;
1067   int r;
1068   double cum_total = 0.0;
1069   double cum_freq = 0.0;
1070
1071   struct init
1072     {
1073       int c, r;
1074       const char *s;
1075     };
1076
1077   struct init *p;
1078
1079   static struct init vec[] =
1080   {
1081     {4, 0, N_("Valid")},
1082     {5, 0, N_("Cum")},
1083     {1, 1, N_("Value")},
1084     {2, 1, N_("Frequency")},
1085     {3, 1, N_("Percent")},
1086     {4, 1, N_("Percent")},
1087     {5, 1, N_("Percent")},
1088     {0, 0, NULL},
1089     {1, 0, NULL},
1090     {2, 0, NULL},
1091     {3, 0, NULL},
1092     {-1, -1, NULL},
1093   };
1094
1095   int lab = cmd.labels == FRQ_LABELS;
1096
1097   ft = &get_var_freqs (v)->tab;
1098   n_categories = ft->n_valid + ft->n_missing;
1099   t = tab_create (5 + lab, n_categories + 3, 0);
1100   tab_headers (t, 0, 0, 2, 0);
1101   tab_dim (t, full_dim);
1102
1103   if (lab)
1104     tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value Label"));
1105   for (p = vec; p->s; p++)
1106     tab_text (t, p->c - (p->r ? !lab : 0), p->r,
1107                   TAB_CENTER | TAT_TITLE, gettext (p->s));
1108
1109   r = 2;
1110   for (f = ft->valid; f < ft->missing; f++)
1111     {
1112       double percent, valid_percent;
1113
1114       cum_freq += f->c;
1115
1116       percent = f->c / ft->total_cases * 100.0;
1117       valid_percent = f->c / ft->valid_cases * 100.0;
1118       cum_total += valid_percent;
1119
1120       if (lab)
1121         {
1122           const char *label = val_labs_find (v->val_labs, f->v);
1123           if (label != NULL)
1124             tab_text (t, 0, r, TAB_LEFT, label);
1125         }
1126
1127       tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1128       tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1129       tab_float (t, 2 + lab, r, TAB_NONE, percent, 5, 1);
1130       tab_float (t, 3 + lab, r, TAB_NONE, valid_percent, 5, 1);
1131       tab_float (t, 4 + lab, r, TAB_NONE, cum_total, 5, 1);
1132       r++;
1133     }
1134   for (; f < &ft->valid[n_categories]; f++)
1135     {
1136       cum_freq += f->c;
1137
1138       if (lab)
1139         {
1140           const char *label = val_labs_find (v->val_labs, f->v);
1141           if (label != NULL)
1142             tab_text (t, 0, r, TAB_LEFT, label);
1143         }
1144
1145       tab_value (t, 0 + lab, r, TAB_NONE, &f->v, &v->print);
1146       tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0);
1147       tab_float (t, 2 + lab, r, TAB_NONE,
1148                      f->c / ft->total_cases * 100.0, 5, 1);
1149       tab_text (t, 3 + lab, r, TAB_NONE, _("Missing"));
1150       r++;
1151     }
1152
1153   tab_box (t, TAL_1, TAL_1,
1154            cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1155            0, 0, 4 + lab, r);
1156   tab_hline (t, TAL_2, 0, 4 + lab, 2);
1157   tab_hline (t, TAL_2, 0, 4 + lab, r);
1158   tab_joint_text (t, 0, r, 0 + lab, r, TAB_RIGHT | TAT_TITLE, _("Total"));
1159   tab_vline (t, TAL_0, 1, r, r);
1160   tab_float (t, 1 + lab, r, TAB_NONE, cum_freq, 8, 0);
1161   tab_float (t, 2 + lab, r, TAB_NONE, 100.0, 5, 1);
1162   tab_float (t, 3 + lab, r, TAB_NONE, 100.0, 5, 1);
1163
1164   tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1165   tab_submit (t);
1166
1167 }
1168
1169 /* Sets the widths of all the columns and heights of all the rows in
1170    table T for driver D. */
1171 static void
1172 condensed_dim (struct tab_table *t, struct outp_driver *d)
1173 {
1174   int cum_w = max (outp_string_width (d, _("Cum")),
1175                    max (outp_string_width (d, _("Cum")),
1176                         outp_string_width (d, "000")));
1177
1178   int i;
1179
1180   for (i = 0; i < 2; i++)
1181     t->w[i] = max (tab_natural_width (t, d, i), d->prop_em_width * 8);
1182   for (i = 2; i < 4; i++)
1183     t->w[i] = cum_w;
1184   for (i = 0; i < t->nr; i++)
1185     t->h[i] = d->font_height;
1186 }
1187
1188 /* Display condensed frequency table for variable V. */
1189 static void
1190 dump_condensed (struct variable *v)
1191 {
1192   int n_categories;
1193   struct freq_tab *ft;
1194   struct freq *f;
1195   struct tab_table *t;
1196   int r;
1197   double cum_total = 0.0;
1198
1199   ft = &get_var_freqs (v)->tab;
1200   n_categories = ft->n_valid + ft->n_missing;
1201   t = tab_create (4, n_categories + 2, 0);
1202
1203   tab_headers (t, 0, 0, 2, 0);
1204   tab_text (t, 0, 1, TAB_CENTER | TAT_TITLE, _("Value"));
1205   tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Freq"));
1206   tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1207   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Cum"));
1208   tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Pct"));
1209   tab_dim (t, condensed_dim);
1210
1211   r = 2;
1212   for (f = ft->valid; f < ft->missing; f++)
1213     {
1214       double percent;
1215
1216       percent = f->c / ft->total_cases * 100.0;
1217       cum_total += f->c / ft->valid_cases * 100.0;
1218
1219       tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1220       tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1221       tab_float (t, 2, r, TAB_NONE, percent, 3, 0);
1222       tab_float (t, 3, r, TAB_NONE, cum_total, 3, 0);
1223       r++;
1224     }
1225   for (; f < &ft->valid[n_categories]; f++)
1226     {
1227       tab_value (t, 0, r, TAB_NONE, &f->v, &v->print);
1228       tab_float (t, 1, r, TAB_NONE, f->c, 8, 0);
1229       tab_float (t, 2, r, TAB_NONE,
1230                  f->c / ft->total_cases * 100.0, 3, 0);
1231       r++;
1232     }
1233
1234   tab_box (t, TAL_1, TAL_1,
1235            cmd.spaces == FRQ_SINGLE ? -1 : (TAL_1 | TAL_SPACING), TAL_1,
1236            0, 0, 3, r - 1);
1237   tab_hline (t, TAL_2, 0, 3, 2);
1238   tab_title (t, 1, "%s: %s", v->name, v->label ? v->label : "");
1239   tab_columns (t, SOM_COL_DOWN, 1);
1240   tab_submit (t);
1241 }
1242 \f
1243 /* Statistical display. */
1244
1245 /* Calculates all the pertinent statistics for variable V, putting
1246    them in array D[].  FIXME: This could be made much more optimal. */
1247 static void
1248 calc_stats (struct variable *v, double d[frq_n_stats])
1249 {
1250   struct freq_tab *ft = &get_var_freqs (v)->tab;
1251   double W = ft->valid_cases;
1252   struct moments *m;
1253   struct freq *f=0; 
1254   int most_often;
1255   double X_mode;
1256
1257   double rank;
1258   int i = 0;
1259   int idx;
1260   double *median_value;
1261
1262   /* Calculate percentiles. */
1263
1264   /* If the 50th percentile was not explicitly requested then we must 
1265      calculate it anyway --- it's the median */
1266   median_value = 0 ;
1267   for (i = 0; i < n_percentiles; i++) 
1268     {
1269       if (percentiles[i].p == 0.5)
1270         {
1271           median_value = &percentiles[i].value;
1272           break;
1273         }
1274     }
1275
1276   if ( 0 == median_value )  
1277     {
1278       add_percentile (0.5);
1279       implicit_50th = 1;
1280     }
1281
1282   for (i = 0; i < n_percentiles; i++) 
1283     {
1284       percentiles[i].flag = 0;
1285       percentiles[i].flag2 = 0;
1286     }
1287
1288   rank = 0;
1289   for (idx = 0; idx < ft->n_valid; ++idx)
1290     {
1291       static double prev_value = SYSMIS;
1292       f = &ft->valid[idx]; 
1293       rank += f->c ;
1294       for (i = 0; i < n_percentiles; i++) 
1295         {
1296           double tp;
1297           if ( percentiles[i].flag2  ) continue ; 
1298
1299           if ( get_algorithm() != COMPATIBLE ) 
1300             tp = 
1301               (ft->valid_cases - 1) *  percentiles[i].p;
1302           else
1303             tp = 
1304               (ft->valid_cases + 1) *  percentiles[i].p - 1;
1305
1306           if ( percentiles[i].flag ) 
1307             {
1308               percentiles[i].x2 = f->v.f;
1309               percentiles[i].x1 = prev_value;
1310               percentiles[i].flag2 = 1;
1311               continue;
1312             }
1313
1314           if (rank >  tp ) 
1315           {
1316             if ( f->c > 1 && rank - (f->c - 1) > tp ) 
1317               {
1318                 percentiles[i].x2 = percentiles[i].x1 = f->v.f;
1319                 percentiles[i].flag2 = 1;
1320               }
1321             else
1322               {
1323                 percentiles[i].flag=1;
1324               }
1325
1326             continue;
1327           }
1328         }
1329       prev_value = f->v.f;
1330     }
1331
1332   for (i = 0; i < n_percentiles; i++) 
1333     {
1334       /* Catches the case when p == 100% */
1335       if ( ! percentiles[i].flag2 ) 
1336         percentiles[i].x1 = percentiles[i].x2 = f->v.f;
1337
1338       /*
1339       printf("percentile %d (p==%.2f); X1 = %g; X2 = %g\n",
1340              i,percentiles[i].p,percentiles[i].x1,percentiles[i].x2);
1341       */
1342     }
1343
1344   for (i = 0; i < n_percentiles; i++) 
1345     {
1346       struct freq_tab *ft = &get_var_freqs (v)->tab;
1347       double s;
1348
1349       double dummy;
1350       if ( get_algorithm() != COMPATIBLE ) 
1351         {
1352           s = modf((ft->valid_cases - 1) * percentiles[i].p , &dummy);
1353         }
1354       else
1355         {
1356           s = modf((ft->valid_cases + 1) * percentiles[i].p -1, &dummy);
1357         }
1358
1359       percentiles[i].value = percentiles[i].x1 + 
1360         ( percentiles[i].x2 - percentiles[i].x1) * s ; 
1361
1362       if ( percentiles[i].p == 0.50) 
1363         median_value = &percentiles[i].value; 
1364     }
1365
1366
1367   /* Calculate the mode. */
1368   most_often = -1;
1369   X_mode = SYSMIS;
1370   for (f = ft->valid; f < ft->missing; f++)
1371     {
1372       if (most_often < f->c) 
1373         {
1374           most_often = f->c;
1375           X_mode = f->v.f;
1376         }
1377       else if (most_often == f->c) 
1378         {
1379           /* A duplicate mode is undefined.
1380              FIXME: keep track of *all* the modes. */
1381           X_mode = SYSMIS;
1382         }
1383     }
1384
1385   /* Calculate moments. */
1386   m = moments_create (MOMENT_KURTOSIS);
1387   for (f = ft->valid; f < ft->missing; f++)
1388     moments_pass_one (m, f->v.f, f->c);
1389   for (f = ft->valid; f < ft->missing; f++)
1390     moments_pass_two (m, f->v.f, f->c);
1391   moments_calculate (m, NULL, &d[frq_mean], &d[frq_variance],
1392                      &d[frq_skew], &d[frq_kurt]);
1393   moments_destroy (m);
1394                      
1395   /* Formulas below are taken from _SPSS Statistical Algorithms_. */
1396   d[frq_min] = ft->valid[0].v.f;
1397   d[frq_max] = ft->valid[ft->n_valid - 1].v.f;
1398   d[frq_mode] = X_mode;
1399   d[frq_range] = d[frq_max] - d[frq_min];
1400   d[frq_median] = *median_value;
1401   d[frq_sum] = d[frq_mean] * W;
1402   d[frq_stddev] = sqrt (d[frq_variance]);
1403   d[frq_semean] = d[frq_stddev] / sqrt (W);
1404   d[frq_seskew] = calc_seskew (W);
1405   d[frq_sekurt] = calc_sekurt (W);
1406 }
1407
1408 /* Displays a table of all the statistics requested for variable V. */
1409 static void
1410 dump_statistics (struct variable *v, int show_varname)
1411 {
1412   struct freq_tab *ft;
1413   double stat_value[frq_n_stats];
1414   struct tab_table *t;
1415   int i, r;
1416
1417   int n_explicit_percentiles = n_percentiles;
1418
1419   if ( implicit_50th && n_percentiles > 0 ) 
1420     --n_percentiles;
1421
1422   if (v->type == ALPHA)
1423     return;
1424   ft = &get_var_freqs (v)->tab;
1425   if (ft->n_valid == 0)
1426     {
1427       msg (SW, _("No valid data for variable %s; statistics not displayed."),
1428            v->name);
1429       return;
1430     }
1431   calc_stats (v, stat_value);
1432
1433   t = tab_create (3, n_stats + n_explicit_percentiles + 2, 0);
1434   tab_dim (t, tab_natural_dimensions);
1435
1436   tab_box (t, TAL_1, TAL_1, -1, -1 , 0 , 0 , 2, tab_nr(t) - 1) ;
1437
1438
1439   tab_vline (t, TAL_1 , 2, 0, tab_nr(t) - 1);
1440   tab_vline (t, TAL_1 | TAL_SPACING , 1, 0, tab_nr(t) - 1 ) ;
1441   
1442   r=2; /* N missing and N valid are always dumped */
1443
1444   for (i = 0; i < frq_n_stats; i++)
1445     if (stats & BIT_INDEX (i))
1446       {
1447         tab_text (t, 0, r, TAB_LEFT | TAT_TITLE,
1448                       gettext (st_name[i].s10));
1449         tab_float (t, 2, r, TAB_NONE, stat_value[i], 11, 3);
1450         r++;
1451       }
1452
1453   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("N"));
1454   tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Valid"));
1455   tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Missing"));
1456
1457   tab_float(t, 2, 0, TAB_NONE, ft->valid_cases, 11, 0);
1458   tab_float(t, 2, 1, TAB_NONE, ft->total_cases - ft->valid_cases, 11, 0);
1459
1460
1461   for (i = 0; i < n_explicit_percentiles; i++, r++) 
1462     {
1463       if ( i == 0 ) 
1464         { 
1465           tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Percentiles"));
1466         }
1467
1468       tab_float (t, 1, r, TAB_LEFT, percentiles[i].p * 100, 3, 0 );
1469       tab_float (t, 2, r, TAB_NONE, percentiles[i].value, 11, 3);
1470
1471     }
1472
1473   tab_columns (t, SOM_COL_DOWN, 1);
1474   if (show_varname)
1475     {
1476       if (v->label)
1477         tab_title (t, 1, "%s: %s", v->name, v->label);
1478       else
1479         tab_title (t, 0, v->name);
1480     }
1481   else
1482     tab_flags (t, SOMF_NO_TITLE);
1483
1484
1485   tab_submit (t);
1486 }
1487 /* 
1488    Local Variables:
1489    mode: c
1490    End:
1491 */