Logistic Regression: Fix crash if categorical variable has no distinct values
[pspp] / src / language / stats / logistic.c
1 /* pspp - a program for statistical analysis.
2    Copyright (C) 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 /* 
19    References: 
20    1. "Coding Logistic Regression with Newton-Raphson", James McCaffrey
21    http://msdn.microsoft.com/en-us/magazine/jj618304.aspx
22
23    2. "SPSS Statistical Algorithms" Chapter LOGISTIC REGRESSION Algorithms
24
25
26    The Newton Raphson method finds successive approximations to $\bf b$ where 
27    approximation ${\bf b}_t$ is (hopefully) better than the previous ${\bf b}_{t-1}$.
28
29    $ {\bf b}_t = {\bf b}_{t -1} + ({\bf X}^T{\bf W}_{t-1}{\bf X})^{-1}{\bf X}^T({\bf y} - {\bf \pi}_{t-1})$
30    where:
31
32    $\bf X$ is the $n \times p$ design matrix, $n$ being the number of cases, 
33    $p$ the number of parameters, \par
34    $\bf W$ is the diagonal matrix whose diagonal elements are
35    $\hat{\pi}_0(1 - \hat{\pi}_0), \, \hat{\pi}_1(1 - \hat{\pi}_2)\dots \hat{\pi}_{n-1}(1 - \hat{\pi}_{n-1})$
36    \par
37
38 */
39
40 #include <config.h>
41
42 #include <gsl/gsl_blas.h> 
43
44 #include <gsl/gsl_linalg.h>
45 #include <gsl/gsl_cdf.h>
46 #include <gsl/gsl_matrix.h>
47 #include <gsl/gsl_vector.h>
48 #include <math.h>
49
50 #include "data/case.h"
51 #include "data/casegrouper.h"
52 #include "data/casereader.h"
53 #include "data/dataset.h"
54 #include "data/dictionary.h"
55 #include "data/format.h"
56 #include "data/value.h"
57 #include "language/command.h"
58 #include "language/dictionary/split-file.h"
59 #include "language/lexer/lexer.h"
60 #include "language/lexer/value-parser.h"
61 #include "language/lexer/variable-parser.h"
62 #include "libpspp/assertion.h"
63 #include "libpspp/ll.h"
64 #include "libpspp/message.h"
65 #include "libpspp/misc.h"
66 #include "math/categoricals.h"
67 #include "math/interaction.h"
68
69 #include "output/tab.h"
70
71 #include "gettext.h"
72 #define _(msgid) gettext (msgid)
73
74
75
76
77 #define   PRINT_EACH_STEP  0x01
78 #define   PRINT_SUMMARY    0x02
79 #define   PRINT_CORR       0x04
80 #define   PRINT_ITER       0x08
81 #define   PRINT_GOODFIT    0x10
82 #define   PRINT_CI         0x20
83
84
85 #define PRINT_DEFAULT (PRINT_SUMMARY | PRINT_EACH_STEP)
86
87 /*
88   The constant parameters of the procedure.
89   That is, those which are set by the user.
90 */
91 struct lr_spec
92 {
93   /* The dependent variable */
94   const struct variable *dep_var;
95
96   /* The predictor variables (excluding categorical ones) */
97   const struct variable **predictor_vars;
98   size_t n_predictor_vars;
99
100   /* The categorical predictors */
101   struct interaction **cat_predictors;
102   size_t n_cat_predictors;
103
104   /* Which classes of missing vars are to be excluded */
105   enum mv_class exclude;
106
107   /* The weight variable */
108   const struct variable *wv;
109
110   /* The dictionary of the dataset */
111   const struct dictionary *dict;
112
113   /* True iff the constant (intercept) is to be included in the model */
114   bool constant;
115
116   /* Ths maximum number of iterations */
117   int max_iter;
118
119   /* Other iteration limiting conditions */
120   double bcon;
121   double min_epsilon;
122   double lcon;
123
124   /* The confidence interval (in percent) */
125   int confidence;
126
127   /* What results should be presented */
128   unsigned int print;
129
130   double cut_point;
131 };
132
133
134 /* The results and intermediate result of the procedure.
135    These are mutated as the procedure runs. Used for
136    temporary variables etc.
137 */
138 struct lr_result
139 {
140   /* Used to indicate if a pass should flag a warning when 
141      invalid (ie negative or missing) weight values are encountered */
142   bool warn_bad_weight;
143
144   /* The two values of the dependent variable. */
145   union value y0;
146   union value y1;
147
148
149   /* The sum of caseweights */
150   double cc;
151
152   /* The number of missing and nonmissing cases */
153   casenumber n_missing;
154   casenumber n_nonmissing;
155
156
157   gsl_matrix *hessian;
158
159   /* The categoricals and their payload. Null if  the analysis has no
160    categorical predictors */
161   struct categoricals *cats;
162   struct payload cp;
163 };
164
165
166 /*
167   Convert INPUT into a dichotomous scalar, according to how the dependent variable's
168   values are mapped.
169   For simple cases, this is a 1:1 mapping
170   The return value is always either 0 or 1
171 */
172 static double
173 map_dependent_var (const struct lr_spec *cmd, const struct lr_result *res, const union value *input)
174 {
175   const int width = var_get_width (cmd->dep_var);
176   if (value_equal (input, &res->y0, width))
177     return 0;
178
179   if (value_equal (input, &res->y1, width))
180     return 1;
181
182   /* This should never happen.  If it does,  then y0 and/or y1 have probably not been set */
183   NOT_REACHED ();
184
185   return SYSMIS;
186 }
187
188
189 static void output_categories (const struct lr_spec *cmd, const struct lr_result *res);
190
191 static void output_depvarmap (const struct lr_spec *cmd, const struct lr_result *);
192
193 static void output_variables (const struct lr_spec *cmd, 
194                               const struct lr_result *,
195                               const gsl_vector *);
196
197 static void output_model_summary (const struct lr_result *,
198                                   double initial_likelihood, double likelihood);
199
200 static void case_processing_summary (const struct lr_result *);
201
202
203 /* Return the value of case C corresponding to the INDEX'th entry in the
204    model */
205 static double
206 predictor_value (const struct ccase *c, 
207                     const struct variable **x, size_t n_x, 
208                     const struct categoricals *cats,
209                     size_t index)
210 {
211   /* Values of the scalar predictor variables */
212   if (index < n_x) 
213     return case_data (c, x[index])->f;
214
215   /* Coded values of categorical predictor variables (or interactions) */
216   if (cats && index - n_x  < categoricals_df_total (cats))
217     {
218       double x = categoricals_get_dummy_code_for_case (cats, index - n_x, c);
219       return x;
220     }
221
222   /* The constant term */
223   return 1.0;
224 }
225
226
227 /*
228   Return the probability estimator (that is the estimator of logit(y) )
229   corresponding to the coefficient estimator beta_hat for case C
230 */
231 static double 
232 pi_hat (const struct lr_spec *cmd, 
233         struct lr_result *res,
234         const gsl_vector *beta_hat,
235         const struct variable **x, size_t n_x,
236         const struct ccase *c)
237 {
238   int v0;
239   double pi = 0;
240   size_t n_coeffs = beta_hat->size;
241
242   if (cmd->constant)
243     {
244       pi += gsl_vector_get (beta_hat, beta_hat->size - 1);
245       n_coeffs--;
246     }
247   
248   for (v0 = 0; v0 < n_coeffs; ++v0)
249     {
250       pi += gsl_vector_get (beta_hat, v0) * 
251         predictor_value (c, x, n_x, res->cats, v0);
252     }
253
254   pi = 1.0 / (1.0 + exp(-pi));
255
256   return pi;
257 }
258
259
260 /*
261   Calculates the Hessian matrix X' V  X,
262   where: X is the n by N_X matrix comprising the n cases in INPUT
263   V is a diagonal matrix { (pi_hat_0)(1 - pi_hat_0), (pi_hat_1)(1 - pi_hat_1), ... (pi_hat_{N-1})(1 - pi_hat_{N-1})} 
264   (the partial derivative of the predicted values)
265
266   If ALL predicted values derivatives are close to zero or one, then CONVERGED
267   will be set to true.
268 */
269 static void
270 hessian (const struct lr_spec *cmd, 
271          struct lr_result *res,
272          struct casereader *input,
273          const struct variable **x, size_t n_x,
274          const gsl_vector *beta_hat,
275          bool *converged)
276 {
277   struct casereader *reader;
278   struct ccase *c;
279
280   double max_w = -DBL_MAX;
281
282   gsl_matrix_set_zero (res->hessian);
283
284   for (reader = casereader_clone (input);
285        (c = casereader_read (reader)) != NULL; case_unref (c))
286     {
287       int v0, v1;
288       double pi = pi_hat (cmd, res, beta_hat, x, n_x, c);
289
290       double weight = dict_get_case_weight (cmd->dict, c, &res->warn_bad_weight);
291       double w = pi * (1 - pi);
292       if (w > max_w)
293         max_w = w;
294       w *= weight;
295
296       for (v0 = 0; v0 < beta_hat->size; ++v0)
297         {
298           double in0 = predictor_value (c, x, n_x, res->cats, v0);
299           for (v1 = 0; v1 < beta_hat->size; ++v1)
300             {
301               double in1 = predictor_value (c, x, n_x, res->cats, v1);
302               double *o = gsl_matrix_ptr (res->hessian, v0, v1);
303               *o += in0 * w * in1;
304             }
305         }
306     }
307   casereader_destroy (reader);
308
309   if ( max_w < cmd->min_epsilon)
310     {
311       *converged = true;
312       msg (MN, _("All predicted values are either 1 or 0"));
313     }
314 }
315
316
317 /* Calculates the value  X' (y - pi)
318    where X is the design model, 
319    y is the vector of observed independent variables
320    pi is the vector of estimates for y
321
322    As a side effect, the likelihood is stored in LIKELIHOOD
323 */
324 static gsl_vector *
325 xt_times_y_pi (const struct lr_spec *cmd,
326                struct lr_result *res,
327                struct casereader *input,
328                const struct variable **x, size_t n_x,
329                const struct variable *y_var,
330                const gsl_vector *beta_hat,
331                double *likelihood)
332 {
333   struct casereader *reader;
334   struct ccase *c;
335   gsl_vector *output = gsl_vector_calloc (beta_hat->size);
336
337   *likelihood = 1.0;
338   for (reader = casereader_clone (input);
339        (c = casereader_read (reader)) != NULL; case_unref (c))
340     {
341       int v0;
342       double pi = pi_hat (cmd, res, beta_hat, x, n_x, c);
343       double weight = dict_get_case_weight (cmd->dict, c, &res->warn_bad_weight);
344
345
346       double y = map_dependent_var (cmd, res, case_data (c, y_var));
347
348       *likelihood *= pow (pi, weight * y) * pow (1 - pi, weight * (1 - y));
349
350       for (v0 = 0; v0 < beta_hat->size; ++v0)
351         {
352           double in0 = predictor_value (c, x, n_x, res->cats, v0);
353           double *o = gsl_vector_ptr (output, v0);
354           *o += in0 * (y - pi) * weight;
355         }
356     }
357
358   casereader_destroy (reader);
359
360   return output;
361 }
362
363 \f
364
365 /* "payload" functions for the categoricals.
366    The only function is to accumulate the frequency of each
367    category.
368  */
369
370 static void *
371 frq_create  (const void *aux1 UNUSED, void *aux2 UNUSED)
372 {
373   return xzalloc (sizeof (double));
374 }
375
376 static void
377 frq_update  (const void *aux1 UNUSED, void *aux2 UNUSED,
378              void *ud, const struct ccase *c UNUSED , double weight)
379 {
380   double *freq = ud;
381   *freq += weight;
382 }
383
384 static void 
385 frq_destroy (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data UNUSED)
386 {
387   free (user_data);
388 }
389
390 \f
391
392 /* 
393    Makes an initial pass though the data, doing the following:
394
395    * Checks that the dependent variable is  dichotomous,
396    * Creates and initialises the categoricals,
397    * Accumulates summary results,
398    * Calculates necessary initial values.
399
400    Returns an initial value for \hat\beta the vector of estimators of \beta
401 */
402 static gsl_vector * 
403 beta_hat_initial (const struct lr_spec *cmd, struct lr_result *res, struct casereader *input)
404 {
405   const int width = var_get_width (cmd->dep_var);
406
407   struct ccase *c;
408   struct casereader *reader;
409   gsl_vector *b0 ;
410   double sum;
411   double sumA = 0.0;
412   double sumB = 0.0;
413
414   bool v0set = false;
415   bool v1set = false;
416
417   size_t n_coefficients = cmd->n_predictor_vars;
418   if (cmd->constant)
419     n_coefficients++;
420
421   /* Create categoricals if appropriate */
422   if (cmd->n_cat_predictors > 0)
423     {
424       res->cp.create = frq_create;
425       res->cp.update = frq_update;
426       res->cp.calculate = NULL;
427       res->cp.destroy = frq_destroy;
428
429       res->cats = categoricals_create (cmd->cat_predictors, cmd->n_cat_predictors,
430                                        cmd->wv, cmd->exclude, MV_ANY);
431
432       categoricals_set_payload (res->cats, &res->cp, cmd, res);
433     }
434
435   res->cc = 0;
436   for (reader = casereader_clone (input);
437        (c = casereader_read (reader)) != NULL; case_unref (c))
438     {
439       int v;
440       bool missing = false;
441       double weight = dict_get_case_weight (cmd->dict, c, &res->warn_bad_weight);
442       const union value *depval = case_data (c, cmd->dep_var);
443
444       for (v = 0; v < cmd->n_predictor_vars; ++v)
445         {
446           const union value *val = case_data (c, cmd->predictor_vars[v]);
447           if (var_is_value_missing (cmd->predictor_vars[v], val, cmd->exclude))
448             {
449               missing = true;
450               break;
451             }
452         }
453
454       /* Accumulate the missing and non-missing counts */
455       if (missing)
456         {
457           res->n_missing++;
458           continue;
459         }
460       res->n_nonmissing++;
461
462       /* Find the values of the dependent variable */
463       if (!v0set)
464         {
465           value_clone (&res->y0, depval, width);
466           v0set = true;
467         }
468       else if (!v1set)
469         {
470           if ( !value_equal (&res->y0, depval, width))
471             {
472               value_clone (&res->y1, depval, width);
473               v1set = true;
474             }
475         }
476       else
477         {
478           if (! value_equal (&res->y0, depval, width)
479               &&
480               ! value_equal (&res->y1, depval, width)
481               )
482             {
483               msg (ME, _("Dependent variable's values are not dichotomous."));
484               goto error;
485             }
486         }
487
488       if (v0set && value_equal (&res->y0, depval, width))
489           sumA += weight;
490
491       if (v1set && value_equal (&res->y1, depval, width))
492           sumB += weight;
493
494
495       res->cc += weight;
496
497       categoricals_update (res->cats, c);
498     }
499   casereader_destroy (reader);
500
501   categoricals_done (res->cats);
502
503   sum = sumB;
504
505   /* Ensure that Y0 is less than Y1.  Otherwise the mapping gets
506      inverted, which is confusing to users */
507   if (var_is_numeric (cmd->dep_var) && value_compare_3way (&res->y0, &res->y1, width) > 0)
508     {
509       union value tmp;
510       value_clone (&tmp, &res->y0, width);
511       value_copy (&res->y0, &res->y1, width);
512       value_copy (&res->y1, &tmp, width);
513       value_destroy (&tmp, width);
514       sum = sumA;
515     }
516
517   n_coefficients += categoricals_df_total (res->cats);
518   b0 = gsl_vector_calloc (n_coefficients);
519
520   if ( cmd->constant)
521     {
522       double mean = sum / res->cc;
523       gsl_vector_set (b0, b0->size - 1, log (mean / (1 - mean)));
524     }
525
526   return b0;
527
528  error:
529   casereader_destroy (reader);
530   return NULL;
531 }
532
533
534
535 /* Start of the logistic regression routine proper */
536 static bool
537 run_lr (const struct lr_spec *cmd, struct casereader *input,
538         const struct dataset *ds UNUSED)
539 {
540   int i;
541
542   gsl_vector *beta_hat;
543
544   bool converged = false;
545
546   /* Set the likelihoods to a negative sentinel value */
547   double likelihood = -1;
548   double prev_likelihood = -1;
549   double initial_likelihood = -1;
550
551   struct lr_result work;
552   work.n_missing = 0;
553   work.n_nonmissing = 0;
554   work.warn_bad_weight = true;
555   work.cats = NULL;
556
557
558   /* Get the initial estimates of \beta and their standard errors */
559   beta_hat = beta_hat_initial (cmd, &work, input);
560   if (NULL == beta_hat)
561     return false;
562
563   
564   for (i = 0; i < cmd->n_cat_predictors; ++i)
565     {
566       if (1 >= categoricals_n_count (work.cats, i))
567         {
568           struct string str;
569           ds_init_empty (&str);
570           
571           interaction_to_string (cmd->cat_predictors[i], &str);
572
573           msg (ME, _("Category %s does not have at least two distinct values. Logistic regression will not be run."),
574                ds_cstr(&str));
575           ds_destroy (&str);
576           return false;
577         }
578     }
579
580   output_depvarmap (cmd, &work);
581
582   case_processing_summary (&work);
583
584
585   input = casereader_create_filter_missing (input,
586                                             cmd->predictor_vars,
587                                             cmd->n_predictor_vars,
588                                             cmd->exclude,
589                                             NULL,
590                                             NULL);
591
592
593   work.hessian = gsl_matrix_calloc (beta_hat->size, beta_hat->size);
594
595   /* Start the Newton Raphson iteration process... */
596   for( i = 0 ; i < cmd->max_iter ; ++i)
597     {
598       double min, max;
599       gsl_vector *v ;
600
601       
602       hessian (cmd, &work, input,
603                    cmd->predictor_vars, cmd->n_predictor_vars,
604                    beta_hat,
605                    &converged);
606
607       gsl_linalg_cholesky_decomp (work.hessian);
608       gsl_linalg_cholesky_invert (work.hessian);
609
610       v = xt_times_y_pi (cmd, &work, input,
611                          cmd->predictor_vars, cmd->n_predictor_vars,
612                          cmd->dep_var,
613                          beta_hat,
614                          &likelihood);
615
616       {
617         /* delta = M.v */
618         gsl_vector *delta = gsl_vector_alloc (v->size);
619         gsl_blas_dgemv (CblasNoTrans, 1.0, work.hessian, v, 0, delta);
620         gsl_vector_free (v);
621
622
623         gsl_vector_add (beta_hat, delta);
624
625         gsl_vector_minmax (delta, &min, &max);
626
627         if ( fabs (min) < cmd->bcon && fabs (max) < cmd->bcon)
628           {
629             msg (MN, _("Estimation terminated at iteration number %d because parameter estimates changed by less than %g"),
630                  i + 1, cmd->bcon);
631             converged = true;
632           }
633
634         gsl_vector_free (delta);
635       }
636
637       if ( prev_likelihood >= 0)
638         {
639           if (-log (likelihood) > -(1.0 - cmd->lcon) * log (prev_likelihood))
640             {
641               msg (MN, _("Estimation terminated at iteration number %d because Log Likelihood decreased by less than %g%%"), i + 1, 100 * cmd->lcon);
642               converged = true;
643             }
644         }
645       if (i == 0)
646         initial_likelihood = likelihood;
647       prev_likelihood = likelihood;
648
649       if (converged)
650         break;
651     }
652   casereader_destroy (input);
653   assert (initial_likelihood >= 0);
654
655   if ( ! converged) 
656     msg (MW, _("Estimation terminated at iteration number %d because maximum iterations has been reached"), i );
657
658
659   output_model_summary (&work, initial_likelihood, likelihood);
660
661   if (work.cats)
662     output_categories (cmd, &work);
663
664   output_variables (cmd, &work, beta_hat);
665
666   gsl_matrix_free (work.hessian);
667   gsl_vector_free (beta_hat); 
668   
669   categoricals_destroy (work.cats);
670
671   return true;
672 }
673
674 /* Parse the LOGISTIC REGRESSION command syntax */
675 int
676 cmd_logistic (struct lexer *lexer, struct dataset *ds)
677 {
678   /* Temporary location for the predictor variables.
679      These may or may not include the categorical predictors */
680   const struct variable **pred_vars;
681   size_t n_pred_vars;
682
683   int v, x;
684   struct lr_spec lr;
685   lr.dict = dataset_dict (ds);
686   lr.n_predictor_vars = 0;
687   lr.predictor_vars = NULL;
688   lr.exclude = MV_ANY;
689   lr.wv = dict_get_weight (lr.dict);
690   lr.max_iter = 20;
691   lr.lcon = 0.0000;
692   lr.bcon = 0.001;
693   lr.min_epsilon = 0.00000001;
694   lr.cut_point = 0.5;
695   lr.constant = true;
696   lr.confidence = 95;
697   lr.print = PRINT_DEFAULT;
698   lr.cat_predictors = NULL;
699   lr.n_cat_predictors = 0;
700
701
702
703   if (lex_match_id (lexer, "VARIABLES"))
704     lex_match (lexer, T_EQUALS);
705
706   if (! (lr.dep_var = parse_variable_const (lexer, lr.dict)))
707     goto error;
708
709   lex_force_match (lexer, T_WITH);
710
711   if (!parse_variables_const (lexer, lr.dict,
712                               &pred_vars, &n_pred_vars,
713                               PV_NO_DUPLICATE))
714     goto error;
715
716
717   while (lex_token (lexer) != T_ENDCMD)
718     {
719       lex_match (lexer, T_SLASH);
720
721       if (lex_match_id (lexer, "MISSING"))
722         {
723           lex_match (lexer, T_EQUALS);
724           while (lex_token (lexer) != T_ENDCMD
725                  && lex_token (lexer) != T_SLASH)
726             {
727               if (lex_match_id (lexer, "INCLUDE"))
728                 {
729                   lr.exclude = MV_SYSTEM;
730                 }
731               else if (lex_match_id (lexer, "EXCLUDE"))
732                 {
733                   lr.exclude = MV_ANY;
734                 }
735               else
736                 {
737                   lex_error (lexer, NULL);
738                   goto error;
739                 }
740             }
741         }
742       else if (lex_match_id (lexer, "ORIGIN"))
743         {
744           lr.constant = false;
745         }
746       else if (lex_match_id (lexer, "NOORIGIN"))
747         {
748           lr.constant = true;
749         }
750       else if (lex_match_id (lexer, "NOCONST"))
751         {
752           lr.constant = false;
753         }
754       else if (lex_match_id (lexer, "EXTERNAL"))
755         {
756           /* This is for compatibility.  It does nothing */
757         }
758       else if (lex_match_id (lexer, "CATEGORICAL"))
759         {
760           lex_match (lexer, T_EQUALS);
761           do
762             {
763               lr.cat_predictors = xrealloc (lr.cat_predictors,
764                                   sizeof (*lr.cat_predictors) * ++lr.n_cat_predictors);
765               lr.cat_predictors[lr.n_cat_predictors - 1] = 0;
766             }
767           while (parse_design_interaction (lexer, lr.dict, 
768                                            lr.cat_predictors + lr.n_cat_predictors - 1));
769           lr.n_cat_predictors--;
770         }
771       else if (lex_match_id (lexer, "PRINT"))
772         {
773           lex_match (lexer, T_EQUALS);
774           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
775             {
776               if (lex_match_id (lexer, "DEFAULT"))
777                 {
778                   lr.print |= PRINT_DEFAULT;
779                 }
780               else if (lex_match_id (lexer, "SUMMARY"))
781                 {
782                   lr.print |= PRINT_SUMMARY;
783                 }
784 #if 0
785               else if (lex_match_id (lexer, "CORR"))
786                 {
787                   lr.print |= PRINT_CORR;
788                 }
789               else if (lex_match_id (lexer, "ITER"))
790                 {
791                   lr.print |= PRINT_ITER;
792                 }
793               else if (lex_match_id (lexer, "GOODFIT"))
794                 {
795                   lr.print |= PRINT_GOODFIT;
796                 }
797 #endif
798               else if (lex_match_id (lexer, "CI"))
799                 {
800                   lr.print |= PRINT_CI;
801                   if (lex_force_match (lexer, T_LPAREN))
802                     {
803                       if (! lex_force_int (lexer))
804                         {
805                           lex_error (lexer, NULL);
806                           goto error;
807                         }
808                       lr.confidence = lex_integer (lexer);
809                       lex_get (lexer);
810                       if ( ! lex_force_match (lexer, T_RPAREN))
811                         {
812                           lex_error (lexer, NULL);
813                           goto error;
814                         }
815                     }
816                 }
817               else if (lex_match_id (lexer, "ALL"))
818                 {
819                   lr.print = ~0x0000;
820                 }
821               else
822                 {
823                   lex_error (lexer, NULL);
824                   goto error;
825                 }
826             }
827         }
828       else if (lex_match_id (lexer, "CRITERIA"))
829         {
830           lex_match (lexer, T_EQUALS);
831           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
832             {
833               if (lex_match_id (lexer, "BCON"))
834                 {
835                   if (lex_force_match (lexer, T_LPAREN))
836                     {
837                       if (! lex_force_num (lexer))
838                         {
839                           lex_error (lexer, NULL);
840                           goto error;
841                         }
842                       lr.bcon = lex_number (lexer);
843                       lex_get (lexer);
844                       if ( ! lex_force_match (lexer, T_RPAREN))
845                         {
846                           lex_error (lexer, NULL);
847                           goto error;
848                         }
849                     }
850                 }
851               else if (lex_match_id (lexer, "ITERATE"))
852                 {
853                   if (lex_force_match (lexer, T_LPAREN))
854                     {
855                       if (! lex_force_int (lexer))
856                         {
857                           lex_error (lexer, NULL);
858                           goto error;
859                         }
860                       lr.max_iter = lex_integer (lexer);
861                       lex_get (lexer);
862                       if ( ! lex_force_match (lexer, T_RPAREN))
863                         {
864                           lex_error (lexer, NULL);
865                           goto error;
866                         }
867                     }
868                 }
869               else if (lex_match_id (lexer, "LCON"))
870                 {
871                   if (lex_force_match (lexer, T_LPAREN))
872                     {
873                       if (! lex_force_num (lexer))
874                         {
875                           lex_error (lexer, NULL);
876                           goto error;
877                         }
878                       lr.lcon = lex_number (lexer);
879                       lex_get (lexer);
880                       if ( ! lex_force_match (lexer, T_RPAREN))
881                         {
882                           lex_error (lexer, NULL);
883                           goto error;
884                         }
885                     }
886                 }
887               else if (lex_match_id (lexer, "EPS"))
888                 {
889                   if (lex_force_match (lexer, T_LPAREN))
890                     {
891                       if (! lex_force_num (lexer))
892                         {
893                           lex_error (lexer, NULL);
894                           goto error;
895                         }
896                       lr.min_epsilon = lex_number (lexer);
897                       lex_get (lexer);
898                       if ( ! lex_force_match (lexer, T_RPAREN))
899                         {
900                           lex_error (lexer, NULL);
901                           goto error;
902                         }
903                     }
904                 }
905               else
906                 {
907                   lex_error (lexer, NULL);
908                   goto error;
909                 }
910             }
911         }
912       else
913         {
914           lex_error (lexer, NULL);
915           goto error;
916         }
917     }
918
919   /* Copy the predictor variables from the temporary location into the 
920      final one, dropping any categorical variables which appear there.
921      FIXME: This is O(NxM).
922   */
923   for (v = x = 0; v < n_pred_vars; ++v)
924     {
925       bool drop = false;
926       const struct variable *var = pred_vars[v];
927       int cv = 0;
928       for (cv = 0; cv < lr.n_cat_predictors ; ++cv)
929         {
930           int iv;
931           const struct interaction *iact = lr.cat_predictors[cv];
932           for (iv = 0 ; iv < iact->n_vars ; ++iv)
933             {
934               if (var == iact->vars[iv])
935                 {
936                   drop = true;
937                   goto dropped;
938                 }
939             }
940         }
941
942     dropped:
943       if (drop)
944         continue;
945
946       lr.predictor_vars = xrealloc (lr.predictor_vars, sizeof *lr.predictor_vars * (x + 1));
947       lr.predictor_vars[x++] = var;
948       lr.n_predictor_vars++;
949     }
950   free (pred_vars);
951
952
953   /* Run logistical regression for each split group */
954   {
955     struct casegrouper *grouper;
956     struct casereader *group;
957     bool ok;
958
959     grouper = casegrouper_create_splits (proc_open (ds), lr.dict);
960     while (casegrouper_get_next_group (grouper, &group))
961       ok = run_lr (&lr, group, ds);
962     ok = casegrouper_destroy (grouper);
963     ok = proc_commit (ds) && ok;
964   }
965
966   free (lr.predictor_vars);
967   free (lr.cat_predictors);
968   return CMD_SUCCESS;
969
970  error:
971
972   free (lr.predictor_vars);
973   free (lr.cat_predictors);
974   return CMD_FAILURE;
975 }
976
977
978 \f
979
980 /* Show the Dependent Variable Encoding box.
981    This indicates how the dependent variable
982    is mapped to the internal zero/one values.
983 */
984 static void
985 output_depvarmap (const struct lr_spec *cmd, const struct lr_result *res)
986 {
987   const int heading_columns = 0;
988   const int heading_rows = 1;
989   struct tab_table *t;
990   struct string str;
991
992   const int nc = 2;
993   int nr = heading_rows + 2;
994
995   t = tab_create (nc, nr);
996   tab_title (t, _("Dependent Variable Encoding"));
997
998   tab_headers (t, heading_columns, 0, heading_rows, 0);
999
1000   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1001
1002   tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1003   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1004
1005   tab_text (t,  0, 0, TAB_CENTER | TAT_TITLE, _("Original Value"));
1006   tab_text (t,  1, 0, TAB_CENTER | TAT_TITLE, _("Internal Value"));
1007
1008
1009
1010   ds_init_empty (&str);
1011   var_append_value_name (cmd->dep_var, &res->y0, &str);
1012   tab_text (t,  0, 0 + heading_rows,  0, ds_cstr (&str));
1013
1014   ds_clear (&str);
1015   var_append_value_name (cmd->dep_var, &res->y1, &str);
1016   tab_text (t,  0, 1 + heading_rows,  0, ds_cstr (&str));
1017
1018
1019   tab_double (t, 1, 0 + heading_rows, 0, map_dependent_var (cmd, res, &res->y0), &F_8_0);
1020   tab_double (t, 1, 1 + heading_rows, 0, map_dependent_var (cmd, res, &res->y1), &F_8_0);
1021   ds_destroy (&str);
1022
1023   tab_submit (t);
1024 }
1025
1026
1027 /* Show the Variables in the Equation box */
1028 static void
1029 output_variables (const struct lr_spec *cmd, 
1030                   const struct lr_result *res,
1031                   const gsl_vector *beta)
1032 {
1033   int row = 0;
1034   const int heading_columns = 1;
1035   int heading_rows = 1;
1036   struct tab_table *t;
1037
1038   int nc = 8;
1039   int nr ;
1040   int i = 0;
1041   int ivar = 0;
1042   int idx_correction = 0;
1043
1044   if (cmd->print & PRINT_CI)
1045     {
1046       nc += 2;
1047       heading_rows += 1;
1048       row++;
1049     }
1050   nr = heading_rows + cmd->n_predictor_vars;
1051   if (cmd->constant)
1052     nr++;
1053
1054   if (res->cats)
1055     nr += categoricals_df_total (res->cats) + cmd->n_cat_predictors;
1056
1057   t = tab_create (nc, nr);
1058   tab_title (t, _("Variables in the Equation"));
1059
1060   tab_headers (t, heading_columns, 0, heading_rows, 0);
1061
1062   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1063
1064   tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1065   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1066
1067   tab_text (t,  0, row + 1, TAB_CENTER | TAT_TITLE, _("Step 1"));
1068
1069   tab_text (t,  2, row, TAB_CENTER | TAT_TITLE, _("B"));
1070   tab_text (t,  3, row, TAB_CENTER | TAT_TITLE, _("S.E."));
1071   tab_text (t,  4, row, TAB_CENTER | TAT_TITLE, _("Wald"));
1072   tab_text (t,  5, row, TAB_CENTER | TAT_TITLE, _("df"));
1073   tab_text (t,  6, row, TAB_CENTER | TAT_TITLE, _("Sig."));
1074   tab_text (t,  7, row, TAB_CENTER | TAT_TITLE, _("Exp(B)"));
1075
1076   if (cmd->print & PRINT_CI)
1077     {
1078       tab_joint_text_format (t, 8, 0, 9, 0,
1079                              TAB_CENTER | TAT_TITLE, _("%d%% CI for Exp(B)"), cmd->confidence);
1080
1081       tab_text (t,  8, row, TAB_CENTER | TAT_TITLE, _("Lower"));
1082       tab_text (t,  9, row, TAB_CENTER | TAT_TITLE, _("Upper"));
1083     }
1084  
1085   for (row = heading_rows ; row < nr; ++row)
1086     {
1087       const int idx = row - heading_rows - idx_correction;
1088
1089       const double b = gsl_vector_get (beta, idx);
1090       const double sigma2 = gsl_matrix_get (res->hessian, idx, idx);
1091       const double wald = pow2 (b) / sigma2;
1092       const double df = 1;
1093
1094       if (idx < cmd->n_predictor_vars)
1095         {
1096           tab_text (t, 1, row, TAB_LEFT | TAT_TITLE, 
1097                     var_to_string (cmd->predictor_vars[idx]));
1098         }
1099       else if (i < cmd->n_cat_predictors)
1100         {
1101           double wald;
1102           bool summary = false;
1103           struct string str;
1104           const struct interaction *cat_predictors = cmd->cat_predictors[i];
1105           const int df = categoricals_df (res->cats, i);
1106
1107           ds_init_empty (&str);
1108           interaction_to_string (cat_predictors, &str);
1109
1110           if (ivar == 0)
1111             {
1112               /* Calculate the Wald statistic,
1113                  which is \beta' C^-1 \beta .
1114                  where \beta is the vector of the coefficient estimates comprising this
1115                  categorial variable. and C is the corresponding submatrix of the 
1116                  hessian matrix.
1117               */
1118               gsl_matrix_const_view mv =
1119                 gsl_matrix_const_submatrix (res->hessian, idx, idx, df, df);
1120               gsl_matrix *subhessian = gsl_matrix_alloc (mv.matrix.size1, mv.matrix.size2);
1121               gsl_vector_const_view vv = gsl_vector_const_subvector (beta, idx, df);
1122               gsl_vector *temp = gsl_vector_alloc (df);
1123
1124               gsl_matrix_memcpy (subhessian, &mv.matrix);
1125               gsl_linalg_cholesky_decomp (subhessian);
1126               gsl_linalg_cholesky_invert (subhessian);
1127
1128               gsl_blas_dgemv (CblasTrans, 1.0, subhessian, &vv.vector, 0, temp);
1129               gsl_blas_ddot (temp, &vv.vector, &wald);
1130
1131               tab_double (t, 4, row, 0, wald, 0);
1132               tab_double (t, 5, row, 0, df, &F_8_0);
1133               tab_double (t, 6, row, 0, gsl_cdf_chisq_Q (wald, df), 0);
1134
1135               idx_correction ++;
1136               summary = true;
1137               gsl_matrix_free (subhessian);
1138               gsl_vector_free (temp);
1139             }
1140           else
1141             {
1142               ds_put_format (&str, "(%d)", ivar);
1143             }
1144
1145           tab_text (t, 1, row, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
1146           if (ivar++ == df)
1147             {
1148               ++i; /* next interaction */
1149               ivar = 0;
1150             }
1151
1152           ds_destroy (&str);
1153
1154           if (summary)
1155             continue;
1156         }
1157       else
1158         {
1159           tab_text (t, 1, row, TAB_LEFT | TAT_TITLE, _("Constant"));
1160         }
1161
1162       tab_double (t, 2, row, 0, b, 0);
1163       tab_double (t, 3, row, 0, sqrt (sigma2), 0);
1164       tab_double (t, 4, row, 0, wald, 0);
1165       tab_double (t, 5, row, 0, df, &F_8_0);
1166       tab_double (t, 6, row, 0, gsl_cdf_chisq_Q (wald, df), 0);
1167       tab_double (t, 7, row, 0, exp (b), 0);
1168
1169       if (cmd->print & PRINT_CI)
1170         {
1171           double wc = gsl_cdf_ugaussian_Pinv (0.5 + cmd->confidence / 200.0);
1172           wc *= sqrt (sigma2);
1173
1174           if (idx < cmd->n_predictor_vars)
1175             {
1176               tab_double (t, 8, row, 0, exp (b - wc), 0);
1177               tab_double (t, 9, row, 0, exp (b + wc), 0);
1178             }
1179         }
1180     }
1181
1182   tab_submit (t);
1183 }
1184
1185
1186 /* Show the model summary box */
1187 static void
1188 output_model_summary (const struct lr_result *res,
1189                       double initial_likelihood, double likelihood)
1190 {
1191   const int heading_columns = 0;
1192   const int heading_rows = 1;
1193   struct tab_table *t;
1194
1195   const int nc = 4;
1196   int nr = heading_rows + 1;
1197   double cox;
1198
1199   t = tab_create (nc, nr);
1200   tab_title (t, _("Model Summary"));
1201
1202   tab_headers (t, heading_columns, 0, heading_rows, 0);
1203
1204   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1205
1206   tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1207   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1208
1209   tab_text (t,  0, 0, TAB_LEFT | TAT_TITLE, _("Step 1"));
1210   tab_text (t,  1, 0, TAB_CENTER | TAT_TITLE, _("-2 Log likelihood"));
1211   tab_double (t,  1, 1, 0, -2 * log (likelihood), 0);
1212
1213
1214   tab_text (t,  2, 0, TAB_CENTER | TAT_TITLE, _("Cox & Snell R Square"));
1215   cox =  1.0 - pow (initial_likelihood /likelihood, 2 / res->cc);
1216   tab_double (t,  2, 1, 0, cox, 0);
1217
1218   tab_text (t,  3, 0, TAB_CENTER | TAT_TITLE, _("Nagelkerke R Square"));
1219   tab_double (t,  3, 1, 0, cox / ( 1.0 - pow (initial_likelihood, 2 / res->cc)), 0);
1220
1221
1222   tab_submit (t);
1223 }
1224
1225 /* Show the case processing summary box */
1226 static void
1227 case_processing_summary (const struct lr_result *res)
1228 {
1229   const int heading_columns = 1;
1230   const int heading_rows = 1;
1231   struct tab_table *t;
1232
1233   const int nc = 3;
1234   const int nr = heading_rows + 3;
1235   casenumber total;
1236
1237   t = tab_create (nc, nr);
1238   tab_title (t, _("Case Processing Summary"));
1239
1240   tab_headers (t, heading_columns, 0, heading_rows, 0);
1241
1242   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1243
1244   tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1245   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1246
1247   tab_text (t,  0, 0, TAB_LEFT | TAT_TITLE, _("Unweighted Cases"));
1248   tab_text (t,  1, 0, TAB_CENTER | TAT_TITLE, _("N"));
1249   tab_text (t,  2, 0, TAB_CENTER | TAT_TITLE, _("Percent"));
1250
1251
1252   tab_text (t,  0, 1, TAB_LEFT | TAT_TITLE, _("Included in Analysis"));
1253   tab_text (t,  0, 2, TAB_LEFT | TAT_TITLE, _("Missing Cases"));
1254   tab_text (t,  0, 3, TAB_LEFT | TAT_TITLE, _("Total"));
1255
1256   tab_double (t,  1, 1, 0, res->n_nonmissing, &F_8_0);
1257   tab_double (t,  1, 2, 0, res->n_missing, &F_8_0);
1258
1259   total = res->n_nonmissing + res->n_missing;
1260   tab_double (t,  1, 3, 0, total , &F_8_0);
1261
1262   tab_double (t,  2, 1, 0, 100 * res->n_nonmissing / (double) total, 0);
1263   tab_double (t,  2, 2, 0, 100 * res->n_missing / (double) total, 0);
1264   tab_double (t,  2, 3, 0, 100 * total / (double) total, 0);
1265
1266   tab_submit (t);
1267 }
1268
1269 static void
1270 output_categories (const struct lr_spec *cmd, const struct lr_result *res)
1271 {
1272   const struct fmt_spec *wfmt =
1273     cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
1274
1275   int cumulative_df;
1276   int i = 0;
1277   const int heading_columns = 2;
1278   const int heading_rows = 2;
1279   struct tab_table *t;
1280
1281   int nc ;
1282   int nr ;
1283
1284   int v;
1285   int r = 0;
1286
1287   int max_df = 0;
1288   int total_cats = 0;
1289   for (i = 0; i < cmd->n_cat_predictors; ++i)
1290     {
1291       size_t n = categoricals_n_count (res->cats, i);
1292       size_t df = categoricals_df (res->cats, i);
1293       if (max_df < df)
1294         max_df = df;
1295       total_cats += n;
1296     }
1297
1298   nc = heading_columns + 1 + max_df;
1299   nr = heading_rows + total_cats;
1300
1301   t = tab_create (nc, nr);
1302   tab_title (t, _("Categorical Variables' Codings"));
1303
1304   tab_headers (t, heading_columns, 0, heading_rows, 0);
1305
1306   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1307
1308   tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1309   tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1310
1311
1312   tab_text (t, heading_columns, 1, TAB_CENTER | TAT_TITLE, _("Frequency"));
1313
1314   tab_joint_text_format (t, heading_columns + 1, 0, nc - 1, 0,
1315                          TAB_CENTER | TAT_TITLE, _("Parameter coding"));
1316
1317
1318   for (i = 0; i < max_df; ++i)
1319     {
1320       int c = heading_columns + 1 + i;
1321       tab_text_format (t,  c, 1, TAB_CENTER | TAT_TITLE, _("(%d)"), i + 1);
1322     }
1323
1324   cumulative_df = 0;
1325   for (v = 0; v < cmd->n_cat_predictors; ++v)
1326     {
1327       int cat;
1328       const struct interaction *cat_predictors = cmd->cat_predictors[v];
1329       int df =  categoricals_df (res->cats, v);
1330       struct string str;
1331       ds_init_empty (&str);
1332
1333       interaction_to_string (cat_predictors, &str);
1334
1335       tab_text (t, 0, heading_rows + r, TAB_LEFT | TAT_TITLE, ds_cstr (&str) );
1336
1337       ds_destroy (&str);
1338
1339       for (cat = 0; cat < categoricals_n_count (res->cats, v) ; ++cat)
1340         {
1341           struct string str;
1342           const struct ccase *c = categoricals_get_case_by_category_real (res->cats, v, cat);
1343           const double *freq = categoricals_get_user_data_by_category_real (res->cats, v, cat);
1344           
1345           int x;
1346           ds_init_empty (&str);
1347
1348           for (x = 0; x < cat_predictors->n_vars; ++x)
1349             {
1350               const union value *val = case_data (c, cat_predictors->vars[x]);
1351               var_append_value_name (cat_predictors->vars[x], val, &str);
1352
1353               if (x < cat_predictors->n_vars - 1)
1354                 ds_put_cstr (&str, " ");
1355             }
1356           
1357           tab_text   (t, 1, heading_rows + r, 0, ds_cstr (&str));
1358           ds_destroy (&str);
1359           tab_double (t, 2, heading_rows + r, 0, *freq, wfmt);
1360
1361           for (x = 0; x < df; ++x)
1362             {
1363               tab_double (t, heading_columns + 1 + x, heading_rows + r, 0, (cat == x), &F_8_0);
1364             }
1365           ++r;
1366         }
1367       cumulative_df += df;
1368     }
1369
1370   tab_submit (t);
1371
1372 }