1 /* pspp - a program for statistical analysis.
2 Copyright (C) 2012 Free Software Foundation, Inc.
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.
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.
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/>. */
20 1. "Coding Logistic Regression with Newton-Raphson", James McCaffrey
21 http://msdn.microsoft.com/en-us/magazine/jj618304.aspx
23 2. "SPSS Statistical Algorithms" Chapter LOGISTIC REGRESSION Algorithms
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}$.
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})$
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})$
42 #include <gsl/gsl_blas.h>
44 #include <gsl/gsl_linalg.h>
45 #include <gsl/gsl_cdf.h>
46 #include <gsl/gsl_matrix.h>
47 #include <gsl/gsl_vector.h>
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"
69 #include "output/tab.h"
72 #define _(msgid) gettext (msgid)
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
85 #define PRINT_DEFAULT (PRINT_SUMMARY | PRINT_EACH_STEP)
88 The constant parameters of the procedure.
89 That is, those which are set by the user.
93 /* The dependent variable */
94 const struct variable *dep_var;
96 /* The predictor variables (excluding categorical ones) */
97 const struct variable **predictor_vars;
98 size_t n_predictor_vars;
100 /* The categorical predictors */
101 struct interaction **cat_predictors;
102 size_t n_cat_predictors;
104 /* Which classes of missing vars are to be excluded */
105 enum mv_class exclude;
107 /* The weight variable */
108 const struct variable *wv;
110 /* The dictionary of the dataset */
111 const struct dictionary *dict;
113 /* True iff the constant (intercept) is to be included in the model */
116 /* Ths maximum number of iterations */
119 /* Other iteration limiting conditions */
124 /* The confidence interval (in percent) */
127 /* What results should be presented */
134 /* The results and intermediate result of the procedure.
135 These are mutated as the procedure runs. Used for
136 temporary variables etc.
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;
144 /* The two values of the dependent variable. */
149 /* The sum of caseweights */
152 /* The number of missing and nonmissing cases */
153 casenumber n_missing;
154 casenumber n_nonmissing;
159 /* The categoricals and their payload. Null if the analysis has no
160 categorical predictors */
161 struct categoricals *cats;
167 Convert INPUT into a dichotomous scalar, according to how the dependent variable's
169 For simple cases, this is a 1:1 mapping
170 The return value is always either 0 or 1
173 map_dependent_var (const struct lr_spec *cmd, const struct lr_result *res, const union value *input)
175 const int width = var_get_width (cmd->dep_var);
176 if (value_equal (input, &res->y0, width))
179 if (value_equal (input, &res->y1, width))
182 /* This should never happen. If it does, then y0 and/or y1 have probably not been set */
189 static void output_categories (const struct lr_spec *cmd, const struct lr_result *res);
191 static void output_depvarmap (const struct lr_spec *cmd, const struct lr_result *);
193 static void output_variables (const struct lr_spec *cmd,
194 const struct lr_result *,
197 static void output_model_summary (const struct lr_result *,
198 double initial_likelihood, double likelihood);
200 static void case_processing_summary (const struct lr_result *);
203 /* Return the value of case C corresponding to the INDEX'th entry in the
206 predictor_value (const struct ccase *c,
207 const struct variable **x, size_t n_x,
208 const struct categoricals *cats,
211 /* Values of the scalar predictor variables */
213 return case_data (c, x[index])->f;
215 /* Coded values of categorical predictor variables (or interactions) */
216 if (cats && index - n_x < categoricals_df_total (cats))
218 double x = categoricals_get_dummy_code_for_case (cats, index - n_x, c);
222 /* The constant term */
228 Return the probability estimator (that is the estimator of logit(y) )
229 corresponding to the coefficient estimator beta_hat for case C
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)
240 size_t n_coeffs = beta_hat->size;
244 pi += gsl_vector_get (beta_hat, beta_hat->size - 1);
248 for (v0 = 0; v0 < n_coeffs; ++v0)
250 pi += gsl_vector_get (beta_hat, v0) *
251 predictor_value (c, x, n_x, res->cats, v0);
254 pi = 1.0 / (1.0 + exp(-pi));
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)
266 If ALL predicted values derivatives are close to zero or one, then CONVERGED
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,
277 struct casereader *reader;
280 double max_w = -DBL_MAX;
282 gsl_matrix_set_zero (res->hessian);
284 for (reader = casereader_clone (input);
285 (c = casereader_read (reader)) != NULL; case_unref (c))
288 double pi = pi_hat (cmd, res, beta_hat, x, n_x, c);
290 double weight = dict_get_case_weight (cmd->dict, c, &res->warn_bad_weight);
291 double w = pi * (1 - pi);
296 for (v0 = 0; v0 < beta_hat->size; ++v0)
298 double in0 = predictor_value (c, x, n_x, res->cats, v0);
299 for (v1 = 0; v1 < beta_hat->size; ++v1)
301 double in1 = predictor_value (c, x, n_x, res->cats, v1);
302 double *o = gsl_matrix_ptr (res->hessian, v0, v1);
307 casereader_destroy (reader);
309 if ( max_w < cmd->min_epsilon)
312 msg (MN, _("All predicted values are either 1 or 0"));
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
322 As a side effect, the likelihood is stored in LIKELIHOOD
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,
333 struct casereader *reader;
335 gsl_vector *output = gsl_vector_calloc (beta_hat->size);
338 for (reader = casereader_clone (input);
339 (c = casereader_read (reader)) != NULL; case_unref (c))
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);
346 double y = map_dependent_var (cmd, res, case_data (c, y_var));
348 *likelihood *= pow (pi, weight * y) * pow (1 - pi, weight * (1 - y));
350 for (v0 = 0; v0 < beta_hat->size; ++v0)
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;
358 casereader_destroy (reader);
365 /* "payload" functions for the categoricals.
366 The only function is to accumulate the frequency of each
371 frq_create (const void *aux1 UNUSED, void *aux2 UNUSED)
373 return xzalloc (sizeof (double));
377 frq_update (const void *aux1 UNUSED, void *aux2 UNUSED,
378 void *ud, const struct ccase *c UNUSED , double weight)
385 frq_destroy (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data UNUSED)
393 Makes an initial pass though the data, doing the following:
395 * Checks that the dependent variable is dichotomous,
396 * Creates and initialises the categoricals,
397 * Accumulates summary results,
398 * Calculates necessary initial values.
400 Returns an initial value for \hat\beta the vector of estimators of \beta
403 beta_hat_initial (const struct lr_spec *cmd, struct lr_result *res, struct casereader *input)
405 const int width = var_get_width (cmd->dep_var);
408 struct casereader *reader;
417 size_t n_coefficients = cmd->n_predictor_vars;
421 /* Create categoricals if appropriate */
422 if (cmd->n_cat_predictors > 0)
424 res->cp.create = frq_create;
425 res->cp.update = frq_update;
426 res->cp.calculate = NULL;
427 res->cp.destroy = frq_destroy;
429 res->cats = categoricals_create (cmd->cat_predictors, cmd->n_cat_predictors,
430 cmd->wv, cmd->exclude, MV_ANY);
432 categoricals_set_payload (res->cats, &res->cp, cmd, res);
436 for (reader = casereader_clone (input);
437 (c = casereader_read (reader)) != NULL; case_unref (c))
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);
444 for (v = 0; v < cmd->n_predictor_vars; ++v)
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))
454 /* Accumulate the missing and non-missing counts */
462 /* Find the values of the dependent variable */
465 value_clone (&res->y0, depval, width);
470 if ( !value_equal (&res->y0, depval, width))
472 value_clone (&res->y1, depval, width);
478 if (! value_equal (&res->y0, depval, width)
480 ! value_equal (&res->y1, depval, width)
483 msg (ME, _("Dependent variable's values are not dichotomous."));
488 if (v0set && value_equal (&res->y0, depval, width))
491 if (v1set && value_equal (&res->y1, depval, width))
497 categoricals_update (res->cats, c);
499 casereader_destroy (reader);
501 categoricals_done (res->cats);
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)
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);
517 n_coefficients += categoricals_df_total (res->cats);
518 b0 = gsl_vector_calloc (n_coefficients);
522 double mean = sum / res->cc;
523 gsl_vector_set (b0, b0->size - 1, log (mean / (1 - mean)));
529 casereader_destroy (reader);
535 /* Start of the logistic regression routine proper */
537 run_lr (const struct lr_spec *cmd, struct casereader *input,
538 const struct dataset *ds UNUSED)
542 gsl_vector *beta_hat;
544 bool converged = false;
546 /* Set the likelihoods to a negative sentinel value */
547 double likelihood = -1;
548 double prev_likelihood = -1;
549 double initial_likelihood = -1;
551 struct lr_result work;
553 work.n_nonmissing = 0;
554 work.warn_bad_weight = true;
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)
563 output_depvarmap (cmd, &work);
565 case_processing_summary (&work);
568 input = casereader_create_filter_missing (input,
570 cmd->n_predictor_vars,
576 work.hessian = gsl_matrix_calloc (beta_hat->size, beta_hat->size);
578 /* Start the Newton Raphson iteration process... */
579 for( i = 0 ; i < cmd->max_iter ; ++i)
585 hessian (cmd, &work, input,
586 cmd->predictor_vars, cmd->n_predictor_vars,
590 gsl_linalg_cholesky_decomp (work.hessian);
591 gsl_linalg_cholesky_invert (work.hessian);
593 v = xt_times_y_pi (cmd, &work, input,
594 cmd->predictor_vars, cmd->n_predictor_vars,
601 gsl_vector *delta = gsl_vector_alloc (v->size);
602 gsl_blas_dgemv (CblasNoTrans, 1.0, work.hessian, v, 0, delta);
606 gsl_vector_add (beta_hat, delta);
608 gsl_vector_minmax (delta, &min, &max);
610 if ( fabs (min) < cmd->bcon && fabs (max) < cmd->bcon)
612 msg (MN, _("Estimation terminated at iteration number %d because parameter estimates changed by less than %g"),
617 gsl_vector_free (delta);
620 if ( prev_likelihood >= 0)
622 if (-log (likelihood) > -(1.0 - cmd->lcon) * log (prev_likelihood))
624 msg (MN, _("Estimation terminated at iteration number %d because Log Likelihood decreased by less than %g%%"), i + 1, 100 * cmd->lcon);
629 initial_likelihood = likelihood;
630 prev_likelihood = likelihood;
635 casereader_destroy (input);
636 assert (initial_likelihood >= 0);
639 msg (MW, _("Estimation terminated at iteration number %d because maximum iterations has been reached"), i );
642 output_model_summary (&work, initial_likelihood, likelihood);
645 output_categories (cmd, &work);
647 output_variables (cmd, &work, beta_hat);
649 gsl_matrix_free (work.hessian);
650 gsl_vector_free (beta_hat);
652 categoricals_destroy (work.cats);
657 /* Parse the LOGISTIC REGRESSION command syntax */
659 cmd_logistic (struct lexer *lexer, struct dataset *ds)
661 /* Temporary location for the predictor variables.
662 These may or may not include the categorical predictors */
663 const struct variable **pred_vars;
668 lr.dict = dataset_dict (ds);
669 lr.n_predictor_vars = 0;
670 lr.predictor_vars = NULL;
672 lr.wv = dict_get_weight (lr.dict);
676 lr.min_epsilon = 0.00000001;
680 lr.print = PRINT_DEFAULT;
681 lr.cat_predictors = NULL;
682 lr.n_cat_predictors = 0;
686 if (lex_match_id (lexer, "VARIABLES"))
687 lex_match (lexer, T_EQUALS);
689 if (! (lr.dep_var = parse_variable_const (lexer, lr.dict)))
692 lex_force_match (lexer, T_WITH);
694 if (!parse_variables_const (lexer, lr.dict,
695 &pred_vars, &n_pred_vars,
700 while (lex_token (lexer) != T_ENDCMD)
702 lex_match (lexer, T_SLASH);
704 if (lex_match_id (lexer, "MISSING"))
706 lex_match (lexer, T_EQUALS);
707 while (lex_token (lexer) != T_ENDCMD
708 && lex_token (lexer) != T_SLASH)
710 if (lex_match_id (lexer, "INCLUDE"))
712 lr.exclude = MV_SYSTEM;
714 else if (lex_match_id (lexer, "EXCLUDE"))
720 lex_error (lexer, NULL);
725 else if (lex_match_id (lexer, "ORIGIN"))
729 else if (lex_match_id (lexer, "NOORIGIN"))
733 else if (lex_match_id (lexer, "NOCONST"))
737 else if (lex_match_id (lexer, "EXTERNAL"))
739 /* This is for compatibility. It does nothing */
741 else if (lex_match_id (lexer, "CATEGORICAL"))
743 lex_match (lexer, T_EQUALS);
746 lr.cat_predictors = xrealloc (lr.cat_predictors,
747 sizeof (*lr.cat_predictors) * ++lr.n_cat_predictors);
748 lr.cat_predictors[lr.n_cat_predictors - 1] = 0;
750 while (parse_design_interaction (lexer, lr.dict,
751 lr.cat_predictors + lr.n_cat_predictors - 1));
752 lr.n_cat_predictors--;
754 else if (lex_match_id (lexer, "PRINT"))
756 lex_match (lexer, T_EQUALS);
757 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
759 if (lex_match_id (lexer, "DEFAULT"))
761 lr.print |= PRINT_DEFAULT;
763 else if (lex_match_id (lexer, "SUMMARY"))
765 lr.print |= PRINT_SUMMARY;
768 else if (lex_match_id (lexer, "CORR"))
770 lr.print |= PRINT_CORR;
772 else if (lex_match_id (lexer, "ITER"))
774 lr.print |= PRINT_ITER;
776 else if (lex_match_id (lexer, "GOODFIT"))
778 lr.print |= PRINT_GOODFIT;
781 else if (lex_match_id (lexer, "CI"))
783 lr.print |= PRINT_CI;
784 if (lex_force_match (lexer, T_LPAREN))
786 if (! lex_force_int (lexer))
788 lex_error (lexer, NULL);
791 lr.confidence = lex_integer (lexer);
793 if ( ! lex_force_match (lexer, T_RPAREN))
795 lex_error (lexer, NULL);
800 else if (lex_match_id (lexer, "ALL"))
806 lex_error (lexer, NULL);
811 else if (lex_match_id (lexer, "CRITERIA"))
813 lex_match (lexer, T_EQUALS);
814 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
816 if (lex_match_id (lexer, "BCON"))
818 if (lex_force_match (lexer, T_LPAREN))
820 if (! lex_force_num (lexer))
822 lex_error (lexer, NULL);
825 lr.bcon = lex_number (lexer);
827 if ( ! lex_force_match (lexer, T_RPAREN))
829 lex_error (lexer, NULL);
834 else if (lex_match_id (lexer, "ITERATE"))
836 if (lex_force_match (lexer, T_LPAREN))
838 if (! lex_force_int (lexer))
840 lex_error (lexer, NULL);
843 lr.max_iter = lex_integer (lexer);
845 if ( ! lex_force_match (lexer, T_RPAREN))
847 lex_error (lexer, NULL);
852 else if (lex_match_id (lexer, "LCON"))
854 if (lex_force_match (lexer, T_LPAREN))
856 if (! lex_force_num (lexer))
858 lex_error (lexer, NULL);
861 lr.lcon = lex_number (lexer);
863 if ( ! lex_force_match (lexer, T_RPAREN))
865 lex_error (lexer, NULL);
870 else if (lex_match_id (lexer, "EPS"))
872 if (lex_force_match (lexer, T_LPAREN))
874 if (! lex_force_num (lexer))
876 lex_error (lexer, NULL);
879 lr.min_epsilon = lex_number (lexer);
881 if ( ! lex_force_match (lexer, T_RPAREN))
883 lex_error (lexer, NULL);
890 lex_error (lexer, NULL);
897 lex_error (lexer, NULL);
902 /* Copy the predictor variables from the temporary location into the
903 final one, dropping any categorical variables which appear there.
904 FIXME: This is O(NxM).
906 for (v = x = 0; v < n_pred_vars; ++v)
909 const struct variable *var = pred_vars[v];
911 for (cv = 0; cv < lr.n_cat_predictors ; ++cv)
914 const struct interaction *iact = lr.cat_predictors[cv];
915 for (iv = 0 ; iv < iact->n_vars ; ++iv)
917 if (var == iact->vars[iv])
929 lr.predictor_vars = xrealloc (lr.predictor_vars, sizeof *lr.predictor_vars * (x + 1));
930 lr.predictor_vars[x++] = var;
931 lr.n_predictor_vars++;
936 /* Run logistical regression for each split group */
938 struct casegrouper *grouper;
939 struct casereader *group;
942 grouper = casegrouper_create_splits (proc_open (ds), lr.dict);
943 while (casegrouper_get_next_group (grouper, &group))
944 ok = run_lr (&lr, group, ds);
945 ok = casegrouper_destroy (grouper);
946 ok = proc_commit (ds) && ok;
949 free (lr.predictor_vars);
950 free (lr.cat_predictors);
955 free (lr.predictor_vars);
956 free (lr.cat_predictors);
963 /* Show the Dependent Variable Encoding box.
964 This indicates how the dependent variable
965 is mapped to the internal zero/one values.
968 output_depvarmap (const struct lr_spec *cmd, const struct lr_result *res)
970 const int heading_columns = 0;
971 const int heading_rows = 1;
976 int nr = heading_rows + 2;
978 t = tab_create (nc, nr);
979 tab_title (t, _("Dependent Variable Encoding"));
981 tab_headers (t, heading_columns, 0, heading_rows, 0);
983 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
985 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
986 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
988 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Original Value"));
989 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Internal Value"));
993 ds_init_empty (&str);
994 var_append_value_name (cmd->dep_var, &res->y0, &str);
995 tab_text (t, 0, 0 + heading_rows, 0, ds_cstr (&str));
998 var_append_value_name (cmd->dep_var, &res->y1, &str);
999 tab_text (t, 0, 1 + heading_rows, 0, ds_cstr (&str));
1002 tab_double (t, 1, 0 + heading_rows, 0, map_dependent_var (cmd, res, &res->y0), &F_8_0);
1003 tab_double (t, 1, 1 + heading_rows, 0, map_dependent_var (cmd, res, &res->y1), &F_8_0);
1010 /* Show the Variables in the Equation box */
1012 output_variables (const struct lr_spec *cmd,
1013 const struct lr_result *res,
1014 const gsl_vector *beta)
1017 const int heading_columns = 1;
1018 int heading_rows = 1;
1019 struct tab_table *t;
1025 int idx_correction = 0;
1027 if (cmd->print & PRINT_CI)
1033 nr = heading_rows + cmd->n_predictor_vars;
1038 nr += categoricals_df_total (res->cats) + cmd->n_cat_predictors;
1040 t = tab_create (nc, nr);
1041 tab_title (t, _("Variables in the Equation"));
1043 tab_headers (t, heading_columns, 0, heading_rows, 0);
1045 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1047 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1048 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1050 tab_text (t, 0, row + 1, TAB_CENTER | TAT_TITLE, _("Step 1"));
1052 tab_text (t, 2, row, TAB_CENTER | TAT_TITLE, _("B"));
1053 tab_text (t, 3, row, TAB_CENTER | TAT_TITLE, _("S.E."));
1054 tab_text (t, 4, row, TAB_CENTER | TAT_TITLE, _("Wald"));
1055 tab_text (t, 5, row, TAB_CENTER | TAT_TITLE, _("df"));
1056 tab_text (t, 6, row, TAB_CENTER | TAT_TITLE, _("Sig."));
1057 tab_text (t, 7, row, TAB_CENTER | TAT_TITLE, _("Exp(B)"));
1059 if (cmd->print & PRINT_CI)
1061 tab_joint_text_format (t, 8, 0, 9, 0,
1062 TAB_CENTER | TAT_TITLE, _("%d%% CI for Exp(B)"), cmd->confidence);
1064 tab_text (t, 8, row, TAB_CENTER | TAT_TITLE, _("Lower"));
1065 tab_text (t, 9, row, TAB_CENTER | TAT_TITLE, _("Upper"));
1068 for (row = heading_rows ; row < nr; ++row)
1070 const int idx = row - heading_rows - idx_correction;
1072 const double b = gsl_vector_get (beta, idx);
1073 const double sigma2 = gsl_matrix_get (res->hessian, idx, idx);
1074 const double wald = pow2 (b) / sigma2;
1075 const double df = 1;
1077 if (idx < cmd->n_predictor_vars)
1079 tab_text (t, 1, row, TAB_LEFT | TAT_TITLE,
1080 var_to_string (cmd->predictor_vars[idx]));
1082 else if (i < cmd->n_cat_predictors)
1085 bool summary = false;
1087 const struct interaction *cat_predictors = cmd->cat_predictors[i];
1088 const int df = categoricals_df (res->cats, i);
1090 ds_init_empty (&str);
1091 interaction_to_string (cat_predictors, &str);
1095 /* Calculate the Wald statistic,
1096 which is \beta' C^-1 \beta .
1097 where \beta is the vector of the coefficient estimates comprising this
1098 categorial variable. and C is the corresponding submatrix of the
1101 gsl_matrix_const_view mv =
1102 gsl_matrix_const_submatrix (res->hessian, idx, idx, df, df);
1103 gsl_matrix *subhessian = gsl_matrix_alloc (mv.matrix.size1, mv.matrix.size2);
1104 gsl_vector_const_view vv = gsl_vector_const_subvector (beta, idx, df);
1105 gsl_vector *temp = gsl_vector_alloc (df);
1107 gsl_matrix_memcpy (subhessian, &mv.matrix);
1108 gsl_linalg_cholesky_decomp (subhessian);
1109 gsl_linalg_cholesky_invert (subhessian);
1111 gsl_blas_dgemv (CblasTrans, 1.0, subhessian, &vv.vector, 0, temp);
1112 gsl_blas_ddot (temp, &vv.vector, &wald);
1114 tab_double (t, 4, row, 0, wald, 0);
1115 tab_double (t, 5, row, 0, df, &F_8_0);
1116 tab_double (t, 6, row, 0, gsl_cdf_chisq_Q (wald, df), 0);
1120 gsl_matrix_free (subhessian);
1121 gsl_vector_free (temp);
1125 ds_put_format (&str, "(%d)", ivar);
1128 tab_text (t, 1, row, TAB_LEFT | TAT_TITLE, ds_cstr (&str));
1131 ++i; /* next interaction */
1142 tab_text (t, 1, row, TAB_LEFT | TAT_TITLE, _("Constant"));
1145 tab_double (t, 2, row, 0, b, 0);
1146 tab_double (t, 3, row, 0, sqrt (sigma2), 0);
1147 tab_double (t, 4, row, 0, wald, 0);
1148 tab_double (t, 5, row, 0, df, &F_8_0);
1149 tab_double (t, 6, row, 0, gsl_cdf_chisq_Q (wald, df), 0);
1150 tab_double (t, 7, row, 0, exp (b), 0);
1152 if (cmd->print & PRINT_CI)
1154 double wc = gsl_cdf_ugaussian_Pinv (0.5 + cmd->confidence / 200.0);
1155 wc *= sqrt (sigma2);
1157 if (idx < cmd->n_predictor_vars)
1159 tab_double (t, 8, row, 0, exp (b - wc), 0);
1160 tab_double (t, 9, row, 0, exp (b + wc), 0);
1169 /* Show the model summary box */
1171 output_model_summary (const struct lr_result *res,
1172 double initial_likelihood, double likelihood)
1174 const int heading_columns = 0;
1175 const int heading_rows = 1;
1176 struct tab_table *t;
1179 int nr = heading_rows + 1;
1182 t = tab_create (nc, nr);
1183 tab_title (t, _("Model Summary"));
1185 tab_headers (t, heading_columns, 0, heading_rows, 0);
1187 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1189 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1190 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1192 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Step 1"));
1193 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("-2 Log likelihood"));
1194 tab_double (t, 1, 1, 0, -2 * log (likelihood), 0);
1197 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Cox & Snell R Square"));
1198 cox = 1.0 - pow (initial_likelihood /likelihood, 2 / res->cc);
1199 tab_double (t, 2, 1, 0, cox, 0);
1201 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Nagelkerke R Square"));
1202 tab_double (t, 3, 1, 0, cox / ( 1.0 - pow (initial_likelihood, 2 / res->cc)), 0);
1208 /* Show the case processing summary box */
1210 case_processing_summary (const struct lr_result *res)
1212 const int heading_columns = 1;
1213 const int heading_rows = 1;
1214 struct tab_table *t;
1217 const int nr = heading_rows + 3;
1220 t = tab_create (nc, nr);
1221 tab_title (t, _("Case Processing Summary"));
1223 tab_headers (t, heading_columns, 0, heading_rows, 0);
1225 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1227 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1228 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1230 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Unweighted Cases"));
1231 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
1232 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Percent"));
1235 tab_text (t, 0, 1, TAB_LEFT | TAT_TITLE, _("Included in Analysis"));
1236 tab_text (t, 0, 2, TAB_LEFT | TAT_TITLE, _("Missing Cases"));
1237 tab_text (t, 0, 3, TAB_LEFT | TAT_TITLE, _("Total"));
1239 tab_double (t, 1, 1, 0, res->n_nonmissing, &F_8_0);
1240 tab_double (t, 1, 2, 0, res->n_missing, &F_8_0);
1242 total = res->n_nonmissing + res->n_missing;
1243 tab_double (t, 1, 3, 0, total , &F_8_0);
1245 tab_double (t, 2, 1, 0, 100 * res->n_nonmissing / (double) total, 0);
1246 tab_double (t, 2, 2, 0, 100 * res->n_missing / (double) total, 0);
1247 tab_double (t, 2, 3, 0, 100 * total / (double) total, 0);
1253 output_categories (const struct lr_spec *cmd, const struct lr_result *res)
1255 const struct fmt_spec *wfmt =
1256 cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0;
1260 const int heading_columns = 2;
1261 const int heading_rows = 2;
1262 struct tab_table *t;
1272 for (i = 0; i < cmd->n_cat_predictors; ++i)
1274 size_t n = categoricals_n_count (res->cats, i);
1275 size_t df = categoricals_df (res->cats, i);
1281 nc = heading_columns + 1 + max_df;
1282 nr = heading_rows + total_cats;
1284 t = tab_create (nc, nr);
1285 tab_title (t, _("Categorical Variables' Codings"));
1287 tab_headers (t, heading_columns, 0, heading_rows, 0);
1289 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, nc - 1, nr - 1);
1291 tab_hline (t, TAL_2, 0, nc - 1, heading_rows);
1292 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
1295 tab_text (t, heading_columns, 1, TAB_CENTER | TAT_TITLE, _("Frequency"));
1297 tab_joint_text_format (t, heading_columns + 1, 0, nc - 1, 0,
1298 TAB_CENTER | TAT_TITLE, _("Parameter coding"));
1301 for (i = 0; i < max_df; ++i)
1303 int c = heading_columns + 1 + i;
1304 tab_text_format (t, c, 1, TAB_CENTER | TAT_TITLE, _("(%d)"), i + 1);
1308 for (v = 0; v < cmd->n_cat_predictors; ++v)
1311 const struct interaction *cat_predictors = cmd->cat_predictors[v];
1312 int df = categoricals_df (res->cats, v);
1314 ds_init_empty (&str);
1316 interaction_to_string (cat_predictors, &str);
1318 tab_text (t, 0, heading_rows + r, TAB_LEFT | TAT_TITLE, ds_cstr (&str) );
1322 for (cat = 0; cat < categoricals_n_count (res->cats, v) ; ++cat)
1325 const struct ccase *c = categoricals_get_case_by_category_real (res->cats, v, cat);
1326 const double *freq = categoricals_get_user_data_by_category_real (res->cats, v, cat);
1329 ds_init_empty (&str);
1331 for (x = 0; x < cat_predictors->n_vars; ++x)
1333 const union value *val = case_data (c, cat_predictors->vars[x]);
1334 var_append_value_name (cat_predictors->vars[x], val, &str);
1336 if (x < cat_predictors->n_vars - 1)
1337 ds_put_cstr (&str, " ");
1340 tab_text (t, 1, heading_rows + r, 0, ds_cstr (&str));
1342 tab_double (t, 2, heading_rows + r, 0, *freq, wfmt);
1344 for (x = 0; x < df; ++x)
1346 tab_double (t, heading_columns + 1 + x, heading_rows + r, 0, (cat == x), &F_8_0);
1350 cumulative_df += df;