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/hash-functions.h"
64 #include "libpspp/hmap.h"
65 #include "libpspp/ll.h"
66 #include "libpspp/message.h"
67 #include "libpspp/misc.h"
68 #include "math/categoricals.h"
69 #include "math/interaction.h"
70 #include "output/pivot-table.h"
73 #define N_(msgid) msgid
74 #define _(msgid) gettext (msgid)
79 #define PRINT_EACH_STEP 0x01
80 #define PRINT_SUMMARY 0x02
81 #define PRINT_CORR 0x04
82 #define PRINT_ITER 0x08
83 #define PRINT_GOODFIT 0x10
87 #define PRINT_DEFAULT (PRINT_SUMMARY | PRINT_EACH_STEP)
90 The constant parameters of the procedure.
91 That is, those which are set by the user.
95 /* The dependent variable */
96 const struct variable *dep_var;
98 /* The predictor variables (excluding categorical ones) */
99 const struct variable **predictor_vars;
100 size_t n_predictor_vars;
102 /* The categorical predictors */
103 struct interaction **cat_predictors;
104 size_t n_cat_predictors;
107 /* The union of the categorical and non-categorical variables */
108 const struct variable **indep_vars;
112 /* Which classes of missing vars are to be excluded */
113 enum mv_class exclude;
115 /* The weight variable */
116 const struct variable *wv;
118 /* The dictionary of the dataset */
119 const struct dictionary *dict;
121 /* True iff the constant (intercept) is to be included in the model */
124 /* Ths maximum number of iterations */
127 /* Other iteration limiting conditions */
132 /* The confidence interval (in percent) */
135 /* What results should be presented */
138 /* Inverse logit of the cut point */
139 double ilogit_cut_point;
143 /* The results and intermediate result of the procedure.
144 These are mutated as the procedure runs. Used for
145 temporary variables etc.
149 /* Used to indicate if a pass should flag a warning when
150 invalid (ie negative or missing) weight values are encountered */
151 bool warn_bad_weight;
153 /* The two values of the dependent variable. */
158 /* The sum of caseweights */
161 /* The number of missing and nonmissing cases */
162 casenumber n_missing;
163 casenumber n_nonmissing;
168 /* The categoricals and their payload. Null if the analysis has no
169 categorical predictors */
170 struct categoricals *cats;
174 /* The estimates of the predictor coefficients */
175 gsl_vector *beta_hat;
177 /* The predicted classifications:
178 True Negative, True Positive, False Negative, False Positive */
179 double tn, tp, fn, fp;
184 Convert INPUT into a dichotomous scalar, according to how the dependent variable's
186 For simple cases, this is a 1:1 mapping
187 The return value is always either 0 or 1
190 map_dependent_var (const struct lr_spec *cmd, const struct lr_result *res, const union value *input)
192 const int width = var_get_width (cmd->dep_var);
193 if (value_equal (input, &res->y0, width))
196 if (value_equal (input, &res->y1, width))
199 /* This should never happen. If it does, then y0 and/or y1 have probably not been set */
205 static void output_classification_table (const struct lr_spec *cmd, const struct lr_result *res);
207 static void output_categories (const struct lr_spec *cmd, const struct lr_result *res);
209 static void output_depvarmap (const struct lr_spec *cmd, const struct lr_result *);
211 static void output_variables (const struct lr_spec *cmd,
212 const struct lr_result *);
214 static void output_model_summary (const struct lr_result *,
215 double initial_likelihood, double likelihood);
217 static void case_processing_summary (const struct lr_result *);
220 /* Return the value of case C corresponding to the INDEX'th entry in the
223 predictor_value (const struct ccase *c,
224 const struct variable **x, size_t n_x,
225 const struct categoricals *cats,
228 /* Values of the scalar predictor variables */
230 return case_num (c, x[index]);
232 /* Coded values of categorical predictor variables (or interactions) */
233 if (cats && index - n_x < categoricals_df_total (cats))
235 double x = categoricals_get_dummy_code_for_case (cats, index - n_x, c);
239 /* The constant term */
245 Return the probability beta_hat (that is the estimator logit(y))
246 corresponding to the coefficient estimator for case C
249 pi_hat (const struct lr_spec *cmd,
250 const struct lr_result *res,
251 const struct variable **x, size_t n_x,
252 const struct ccase *c)
256 size_t n_coeffs = res->beta_hat->size;
260 pi += gsl_vector_get (res->beta_hat, res->beta_hat->size - 1);
264 for (v0 = 0; v0 < n_coeffs; ++v0)
266 pi += gsl_vector_get (res->beta_hat, v0) *
267 predictor_value (c, x, n_x, res->cats, v0);
270 pi = 1.0 / (1.0 + exp(-pi));
277 Calculates the Hessian matrix X' V X,
278 where: X is the n by N_X matrix comprising the n cases in INPUT
279 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})}
280 (the partial derivative of the predicted values)
282 If ALL predicted values derivatives are close to zero or one, then CONVERGED
286 hessian (const struct lr_spec *cmd,
287 struct lr_result *res,
288 struct casereader *input,
289 const struct variable **x, size_t n_x,
292 struct casereader *reader;
295 double max_w = -DBL_MAX;
297 gsl_matrix_set_zero (res->hessian);
299 for (reader = casereader_clone (input);
300 (c = casereader_read (reader)) != NULL; case_unref (c))
303 double pi = pi_hat (cmd, res, x, n_x, c);
305 double weight = dict_get_case_weight (cmd->dict, c, &res->warn_bad_weight);
306 double w = pi * (1 - pi);
311 for (v0 = 0; v0 < res->beta_hat->size; ++v0)
313 double in0 = predictor_value (c, x, n_x, res->cats, v0);
314 for (v1 = 0; v1 < res->beta_hat->size; ++v1)
316 double in1 = predictor_value (c, x, n_x, res->cats, v1);
317 double *o = gsl_matrix_ptr (res->hessian, v0, v1);
322 casereader_destroy (reader);
324 if (max_w < cmd->min_epsilon)
327 msg (MN, _("All predicted values are either 1 or 0"));
332 /* Calculates the value X' (y - pi)
333 where X is the design model,
334 y is the vector of observed independent variables
335 pi is the vector of estimates for y
338 the likelihood is stored in LIKELIHOOD;
339 the predicted values are placed in the respective tn, fn, tp fp values in RES
342 xt_times_y_pi (const struct lr_spec *cmd,
343 struct lr_result *res,
344 struct casereader *input,
345 const struct variable **x, size_t n_x,
346 const struct variable *y_var,
349 struct casereader *reader;
351 gsl_vector *output = gsl_vector_calloc (res->beta_hat->size);
354 res->tn = res->tp = res->fn = res->fp = 0;
355 for (reader = casereader_clone (input);
356 (c = casereader_read (reader)) != NULL; case_unref (c))
360 double pi = pi_hat (cmd, res, x, n_x, c);
361 double weight = dict_get_case_weight (cmd->dict, c, &res->warn_bad_weight);
364 double y = map_dependent_var (cmd, res, case_data (c, y_var));
366 *llikelihood += (weight * y) * log (pi) + log (1 - pi) * weight * (1 - y);
368 for (v0 = 0; v0 < res->beta_hat->size; ++v0)
370 double in0 = predictor_value (c, x, n_x, res->cats, v0);
371 double *o = gsl_vector_ptr (output, v0);
372 *o += in0 * (y - pi) * weight;
373 pred_y += gsl_vector_get (res->beta_hat, v0) * in0;
376 /* Count the number of cases which would be correctly/incorrectly classified by this
378 if (pred_y <= cmd->ilogit_cut_point)
394 casereader_destroy (reader);
401 /* "payload" functions for the categoricals.
402 The only function is to accumulate the frequency of each
407 frq_create (const void *aux1 UNUSED, void *aux2 UNUSED)
409 return xzalloc (sizeof (double));
413 frq_update (const void *aux1 UNUSED, void *aux2 UNUSED,
414 void *ud, const struct ccase *c UNUSED , double weight)
421 frq_destroy (const void *aux1 UNUSED, void *aux2 UNUSED, void *user_data)
429 Makes an initial pass though the data, doing the following:
431 * Checks that the dependent variable is dichotomous,
432 * Creates and initialises the categoricals,
433 * Accumulates summary results,
434 * Calculates necessary initial values.
435 * Creates an initial value for \hat\beta the vector of beta_hats of \beta
437 Returns true if successful
440 initial_pass (const struct lr_spec *cmd, struct lr_result *res, struct casereader *input)
442 const int width = var_get_width (cmd->dep_var);
445 struct casereader *reader;
454 size_t n_coefficients = cmd->n_predictor_vars;
458 /* Create categoricals if appropriate */
459 if (cmd->n_cat_predictors > 0)
461 res->cp.create = frq_create;
462 res->cp.update = frq_update;
463 res->cp.calculate = NULL;
464 res->cp.destroy = frq_destroy;
466 res->cats = categoricals_create (cmd->cat_predictors, cmd->n_cat_predictors,
469 categoricals_set_payload (res->cats, &res->cp, cmd, res);
473 for (reader = casereader_clone (input);
474 (c = casereader_read (reader)) != NULL; case_unref (c))
477 bool missing = false;
478 double weight = dict_get_case_weight (cmd->dict, c, &res->warn_bad_weight);
479 const union value *depval = case_data (c, cmd->dep_var);
481 if (var_is_value_missing (cmd->dep_var, depval) & cmd->exclude)
486 for (v = 0; v < cmd->n_indep_vars; ++v)
488 const union value *val = case_data (c, cmd->indep_vars[v]);
489 if (var_is_value_missing (cmd->indep_vars[v], val) & cmd->exclude)
496 /* Accumulate the missing and non-missing counts */
504 /* Find the values of the dependent variable */
507 value_clone (&res->y0, depval, width);
512 if (!value_equal (&res->y0, depval, width))
514 value_clone (&res->y1, depval, width);
520 if (! value_equal (&res->y0, depval, width)
522 ! value_equal (&res->y1, depval, width)
525 msg (ME, _("Dependent variable's values are not dichotomous."));
531 if (v0set && value_equal (&res->y0, depval, width))
534 if (v1set && value_equal (&res->y1, depval, width))
540 categoricals_update (res->cats, c);
542 casereader_destroy (reader);
544 categoricals_done (res->cats);
548 /* Ensure that Y0 is less than Y1. Otherwise the mapping gets
549 inverted, which is confusing to users */
550 if (var_is_numeric (cmd->dep_var) && value_compare_3way (&res->y0, &res->y1, width) > 0)
553 value_clone (&tmp, &res->y0, width);
554 value_copy (&res->y0, &res->y1, width);
555 value_copy (&res->y1, &tmp, width);
556 value_destroy (&tmp, width);
560 n_coefficients += categoricals_df_total (res->cats);
561 res->beta_hat = gsl_vector_calloc (n_coefficients);
565 double mean = sum / res->cc;
566 gsl_vector_set (res->beta_hat, res->beta_hat->size - 1, log (mean / (1 - mean)));
572 casereader_destroy (reader);
578 /* Start of the logistic regression routine proper */
580 run_lr (const struct lr_spec *cmd, struct casereader *input,
581 const struct dataset *ds UNUSED)
585 bool converged = false;
587 /* Set the log likelihoods to a sentinel value */
588 double log_likelihood = SYSMIS;
589 double prev_log_likelihood = SYSMIS;
590 double initial_log_likelihood = SYSMIS;
592 struct lr_result work;
594 work.n_nonmissing = 0;
595 work.warn_bad_weight = true;
597 work.beta_hat = NULL;
600 /* Get the initial estimates of \beta and their standard errors.
601 And perform other auxiliary initialisation. */
602 if (! initial_pass (cmd, &work, input))
605 for (i = 0; i < cmd->n_cat_predictors; ++i)
607 if (1 >= categoricals_n_count (work.cats, i))
610 ds_init_empty (&str);
612 interaction_to_string (cmd->cat_predictors[i], &str);
614 msg (ME, _("Category %s does not have at least two distinct values. Logistic regression will not be run."),
621 output_depvarmap (cmd, &work);
623 case_processing_summary (&work);
626 input = casereader_create_filter_missing (input,
633 input = casereader_create_filter_missing (input,
640 work.hessian = gsl_matrix_calloc (work.beta_hat->size, work.beta_hat->size);
642 /* Start the Newton Raphson iteration process... */
643 for(i = 0 ; i < cmd->max_iter ; ++i)
649 hessian (cmd, &work, input,
650 cmd->predictor_vars, cmd->n_predictor_vars,
653 gsl_linalg_cholesky_decomp (work.hessian);
654 gsl_linalg_cholesky_invert (work.hessian);
656 v = xt_times_y_pi (cmd, &work, input,
657 cmd->predictor_vars, cmd->n_predictor_vars,
663 gsl_vector *delta = gsl_vector_alloc (v->size);
664 gsl_blas_dgemv (CblasNoTrans, 1.0, work.hessian, v, 0, delta);
668 gsl_vector_add (work.beta_hat, delta);
670 gsl_vector_minmax (delta, &min, &max);
672 if (fabs (min) < cmd->bcon && fabs (max) < cmd->bcon)
674 msg (MN, _("Estimation terminated at iteration number %d because parameter estimates changed by less than %g"),
679 gsl_vector_free (delta);
684 if (-log_likelihood > -(1.0 - cmd->lcon) * prev_log_likelihood)
686 msg (MN, _("Estimation terminated at iteration number %d because Log Likelihood decreased by less than %g%%"), i + 1, 100 * cmd->lcon);
691 initial_log_likelihood = log_likelihood;
692 prev_log_likelihood = log_likelihood;
701 msg (MW, _("Estimation terminated at iteration number %d because maximum iterations has been reached"), i);
704 output_model_summary (&work, initial_log_likelihood, log_likelihood);
707 output_categories (cmd, &work);
709 output_classification_table (cmd, &work);
710 output_variables (cmd, &work);
712 casereader_destroy (input);
713 gsl_matrix_free (work.hessian);
714 gsl_vector_free (work.beta_hat);
715 categoricals_destroy (work.cats);
720 casereader_destroy (input);
721 gsl_matrix_free (work.hessian);
722 gsl_vector_free (work.beta_hat);
723 categoricals_destroy (work.cats);
730 struct hmap_node node; /* Node in hash map. */
731 const struct variable *var; /* The variable */
734 static struct variable_node *
735 lookup_variable (const struct hmap *map, const struct variable *var, unsigned int hash)
737 struct variable_node *vn = NULL;
738 HMAP_FOR_EACH_WITH_HASH (vn, struct variable_node, node, hash, map)
748 /* Parse the LOGISTIC REGRESSION command syntax */
750 cmd_logistic (struct lexer *lexer, struct dataset *ds)
753 /* Temporary location for the predictor variables.
754 These may or may not include the categorical predictors */
755 const struct variable **pred_vars;
761 lr.dict = dataset_dict (ds);
762 lr.n_predictor_vars = 0;
763 lr.predictor_vars = NULL;
765 lr.wv = dict_get_weight (lr.dict);
769 lr.min_epsilon = 0.00000001;
772 lr.print = PRINT_DEFAULT;
773 lr.cat_predictors = NULL;
774 lr.n_cat_predictors = 0;
775 lr.indep_vars = NULL;
778 if (lex_match_id (lexer, "VARIABLES"))
779 lex_match (lexer, T_EQUALS);
781 if (! (lr.dep_var = parse_variable_const (lexer, lr.dict)))
784 if (! lex_force_match (lexer, T_WITH))
787 if (!parse_variables_const (lexer, lr.dict,
788 &pred_vars, &n_pred_vars,
793 while (lex_token (lexer) != T_ENDCMD)
795 lex_match (lexer, T_SLASH);
797 if (lex_match_id (lexer, "MISSING"))
799 lex_match (lexer, T_EQUALS);
800 while (lex_token (lexer) != T_ENDCMD
801 && lex_token (lexer) != T_SLASH)
803 if (lex_match_id (lexer, "INCLUDE"))
805 lr.exclude = MV_SYSTEM;
807 else if (lex_match_id (lexer, "EXCLUDE"))
813 lex_error (lexer, NULL);
818 else if (lex_match_id (lexer, "ORIGIN"))
822 else if (lex_match_id (lexer, "NOORIGIN"))
826 else if (lex_match_id (lexer, "NOCONST"))
830 else if (lex_match_id (lexer, "EXTERNAL"))
832 /* This is for compatibility. It does nothing */
834 else if (lex_match_id (lexer, "CATEGORICAL"))
836 lex_match (lexer, T_EQUALS);
839 lr.cat_predictors = xrealloc (lr.cat_predictors,
840 sizeof (*lr.cat_predictors) * ++lr.n_cat_predictors);
841 lr.cat_predictors[lr.n_cat_predictors - 1] = 0;
843 while (parse_design_interaction (lexer, lr.dict,
844 lr.cat_predictors + lr.n_cat_predictors - 1));
845 lr.n_cat_predictors--;
847 else if (lex_match_id (lexer, "PRINT"))
849 lex_match (lexer, T_EQUALS);
850 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
852 if (lex_match_id (lexer, "DEFAULT"))
854 lr.print |= PRINT_DEFAULT;
856 else if (lex_match_id (lexer, "SUMMARY"))
858 lr.print |= PRINT_SUMMARY;
861 else if (lex_match_id (lexer, "CORR"))
863 lr.print |= PRINT_CORR;
865 else if (lex_match_id (lexer, "ITER"))
867 lr.print |= PRINT_ITER;
869 else if (lex_match_id (lexer, "GOODFIT"))
871 lr.print |= PRINT_GOODFIT;
874 else if (lex_match_id (lexer, "CI"))
876 lr.print |= PRINT_CI;
877 if (lex_force_match (lexer, T_LPAREN))
879 if (! lex_force_num (lexer))
881 lex_error (lexer, NULL);
884 lr.confidence = lex_number (lexer);
886 if (! lex_force_match (lexer, T_RPAREN))
888 lex_error (lexer, NULL);
893 else if (lex_match_id (lexer, "ALL"))
899 lex_error (lexer, NULL);
904 else if (lex_match_id (lexer, "CRITERIA"))
906 lex_match (lexer, T_EQUALS);
907 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
909 if (lex_match_id (lexer, "BCON"))
911 if (lex_force_match (lexer, T_LPAREN))
913 if (! lex_force_num (lexer))
915 lex_error (lexer, NULL);
918 lr.bcon = lex_number (lexer);
920 if (! lex_force_match (lexer, T_RPAREN))
922 lex_error (lexer, NULL);
927 else if (lex_match_id (lexer, "ITERATE"))
929 if (lex_force_match (lexer, T_LPAREN))
931 if (! lex_force_int_range (lexer, "ITERATE", 0, INT_MAX))
933 lex_error (lexer, NULL);
936 lr.max_iter = lex_integer (lexer);
938 if (! lex_force_match (lexer, T_RPAREN))
940 lex_error (lexer, NULL);
945 else if (lex_match_id (lexer, "LCON"))
947 if (lex_force_match (lexer, T_LPAREN))
949 if (! lex_force_num (lexer))
951 lex_error (lexer, NULL);
954 lr.lcon = lex_number (lexer);
956 if (! lex_force_match (lexer, T_RPAREN))
958 lex_error (lexer, NULL);
963 else if (lex_match_id (lexer, "EPS"))
965 if (lex_force_match (lexer, T_LPAREN))
967 if (! lex_force_num (lexer))
969 lex_error (lexer, NULL);
972 lr.min_epsilon = lex_number (lexer);
974 if (! lex_force_match (lexer, T_RPAREN))
976 lex_error (lexer, NULL);
981 else if (lex_match_id (lexer, "CUT"))
983 if (lex_force_match (lexer, T_LPAREN))
985 if (!lex_force_num_range_closed (lexer, "CUT", 0, 1))
988 cp = lex_number (lexer);
991 if (! lex_force_match (lexer, T_RPAREN))
993 lex_error (lexer, NULL);
1000 lex_error (lexer, NULL);
1007 lex_error (lexer, NULL);
1012 lr.ilogit_cut_point = - log (1/cp - 1);
1015 /* Copy the predictor variables from the temporary location into the
1016 final one, dropping any categorical variables which appear there.
1017 FIXME: This is O(NxM).
1020 struct variable_node *vn, *next;
1021 struct hmap allvars;
1022 hmap_init (&allvars);
1023 for (v = x = 0; v < n_pred_vars; ++v)
1026 const struct variable *var = pred_vars[v];
1029 unsigned int hash = hash_pointer (var, 0);
1030 struct variable_node *vn = lookup_variable (&allvars, var, hash);
1033 vn = xmalloc (sizeof *vn);
1035 hmap_insert (&allvars, &vn->node, hash);
1038 for (cv = 0; cv < lr.n_cat_predictors ; ++cv)
1041 const struct interaction *iact = lr.cat_predictors[cv];
1042 for (iv = 0 ; iv < iact->n_vars ; ++iv)
1044 const struct variable *ivar = iact->vars[iv];
1045 unsigned int hash = hash_pointer (ivar, 0);
1046 struct variable_node *vn = lookup_variable (&allvars, ivar, hash);
1049 vn = xmalloc (sizeof *vn);
1052 hmap_insert (&allvars, &vn->node, hash);
1065 lr.predictor_vars = xrealloc (lr.predictor_vars, sizeof *lr.predictor_vars * (x + 1));
1066 lr.predictor_vars[x++] = var;
1067 lr.n_predictor_vars++;
1071 lr.n_indep_vars = hmap_count (&allvars);
1072 lr.indep_vars = xmalloc (lr.n_indep_vars * sizeof *lr.indep_vars);
1074 /* Interate over each variable and push it into the array */
1076 HMAP_FOR_EACH_SAFE (vn, next, struct variable_node, node, &allvars)
1078 lr.indep_vars[x++] = vn->var;
1081 hmap_destroy (&allvars);
1085 /* logistical regression for each split group */
1087 struct casegrouper *grouper;
1088 struct casereader *group;
1091 grouper = casegrouper_create_splits (proc_open (ds), lr.dict);
1092 while (casegrouper_get_next_group (grouper, &group))
1093 ok = run_lr (&lr, group, ds);
1094 ok = casegrouper_destroy (grouper);
1095 ok = proc_commit (ds) && ok;
1098 for (i = 0 ; i < lr.n_cat_predictors; ++i)
1100 interaction_destroy (lr.cat_predictors[i]);
1102 free (lr.predictor_vars);
1103 free (lr.cat_predictors);
1104 free (lr.indep_vars);
1110 for (i = 0 ; i < lr.n_cat_predictors; ++i)
1112 interaction_destroy (lr.cat_predictors[i]);
1114 free (lr.predictor_vars);
1115 free (lr.cat_predictors);
1116 free (lr.indep_vars);
1124 /* Show the Dependent Variable Encoding box.
1125 This indicates how the dependent variable
1126 is mapped to the internal zero/one values.
1129 output_depvarmap (const struct lr_spec *cmd, const struct lr_result *res)
1131 struct pivot_table *table = pivot_table_create (
1132 N_("Dependent Variable Encoding"));
1134 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Mapping"),
1135 N_("Internal Value"));
1137 struct pivot_dimension *original = pivot_dimension_create (
1138 table, PIVOT_AXIS_ROW, N_("Original Value"));
1139 original->root->show_label = true;
1141 for (int i = 0; i < 2; i++)
1143 const union value *v = i ? &res->y1 : &res->y0;
1144 int orig_idx = pivot_category_create_leaf (
1145 original->root, pivot_value_new_var_value (cmd->dep_var, v));
1146 pivot_table_put2 (table, 0, orig_idx, pivot_value_new_number (
1147 map_dependent_var (cmd, res, v)));
1150 pivot_table_submit (table);
1154 /* Show the Variables in the Equation box */
1156 output_variables (const struct lr_spec *cmd,
1157 const struct lr_result *res)
1159 struct pivot_table *table = pivot_table_create (
1160 N_("Variables in the Equation"));
1162 struct pivot_dimension *statistics = pivot_dimension_create (
1163 table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1164 N_("B"), PIVOT_RC_OTHER,
1165 N_("S.E."), PIVOT_RC_OTHER,
1166 N_("Wald"), PIVOT_RC_OTHER,
1167 N_("df"), PIVOT_RC_INTEGER,
1168 N_("Sig."), PIVOT_RC_SIGNIFICANCE,
1169 N_("Exp(B)"), PIVOT_RC_OTHER);
1170 if (cmd->print & PRINT_CI)
1172 struct pivot_category *group = pivot_category_create_group__ (
1174 pivot_value_new_text_format (N_("%d%% CI for Exp(B)"),
1176 pivot_category_create_leaves (group, N_("Lower"), N_("Upper"));
1179 struct pivot_dimension *variables = pivot_dimension_create (
1180 table, PIVOT_AXIS_ROW, N_("Variables"));
1181 struct pivot_category *step1 = pivot_category_create_group (
1182 variables->root, N_("Step 1"));
1185 int idx_correction = 0;
1188 int nr = cmd->n_predictor_vars;
1192 nr += categoricals_df_total (res->cats) + cmd->n_cat_predictors;
1194 for (int row = 0; row < nr; row++)
1196 const int idx = row - idx_correction;
1199 if (idx < cmd->n_predictor_vars)
1200 var_idx = pivot_category_create_leaf (
1201 step1, pivot_value_new_variable (cmd->predictor_vars[idx]));
1202 else if (i < cmd->n_cat_predictors)
1204 const struct interaction *cat_predictors = cmd->cat_predictors[i];
1205 struct string str = DS_EMPTY_INITIALIZER;
1206 interaction_to_string (cat_predictors, &str);
1208 ds_put_format (&str, "(%d)", ivar);
1209 var_idx = pivot_category_create_leaf (
1210 step1, pivot_value_new_user_text_nocopy (ds_steal_cstr (&str)));
1212 int df = categoricals_df (res->cats, i);
1213 bool summary = ivar == 0;
1216 /* Calculate the Wald statistic,
1217 which is \beta' C^-1 \beta .
1218 where \beta is the vector of the coefficient estimates comprising this
1219 categorial variable. and C is the corresponding submatrix of the
1222 gsl_matrix_const_view mv =
1223 gsl_matrix_const_submatrix (res->hessian, idx, idx, df, df);
1224 gsl_matrix *subhessian = gsl_matrix_alloc (mv.matrix.size1, mv.matrix.size2);
1225 gsl_vector_const_view vv = gsl_vector_const_subvector (res->beta_hat, idx, df);
1226 gsl_vector *temp = gsl_vector_alloc (df);
1228 gsl_matrix_memcpy (subhessian, &mv.matrix);
1229 gsl_linalg_cholesky_decomp (subhessian);
1230 gsl_linalg_cholesky_invert (subhessian);
1232 gsl_blas_dgemv (CblasTrans, 1.0, subhessian, &vv.vector, 0, temp);
1234 gsl_blas_ddot (temp, &vv.vector, &wald);
1236 double entries[] = { wald, df, gsl_cdf_chisq_Q (wald, df) };
1237 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1238 pivot_table_put2 (table, j + 2, var_idx,
1239 pivot_value_new_number (entries[j]));
1242 gsl_matrix_free (subhessian);
1243 gsl_vector_free (temp);
1248 ++i; /* next interaction */
1256 var_idx = pivot_category_create_leaves (step1, N_("Constant"));
1258 double b = gsl_vector_get (res->beta_hat, idx);
1259 double sigma2 = gsl_matrix_get (res->hessian, idx, idx);
1260 double wald = pow2 (b) / sigma2;
1262 double wc = (gsl_cdf_ugaussian_Pinv (0.5 + cmd->confidence / 200.0)
1264 bool show_ci = cmd->print & PRINT_CI && row < nr - cmd->constant;
1266 double entries[] = {
1271 gsl_cdf_chisq_Q (wald, df),
1273 show_ci ? exp (b - wc) : SYSMIS,
1274 show_ci ? exp (b + wc) : SYSMIS,
1276 for (size_t j = 0; j < sizeof entries / sizeof *entries; j++)
1277 if (entries[j] != SYSMIS)
1278 pivot_table_put2 (table, j, var_idx,
1279 pivot_value_new_number (entries[j]));
1282 pivot_table_submit (table);
1286 /* Show the model summary box */
1288 output_model_summary (const struct lr_result *res,
1289 double initial_log_likelihood, double log_likelihood)
1291 struct pivot_table *table = pivot_table_create (N_("Model Summary"));
1293 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1294 N_("-2 Log likelihood"), PIVOT_RC_OTHER,
1295 N_("Cox & Snell R Square"), PIVOT_RC_OTHER,
1296 N_("Nagelkerke R Square"), PIVOT_RC_OTHER);
1298 struct pivot_dimension *step = pivot_dimension_create (
1299 table, PIVOT_AXIS_ROW, N_("Step"));
1300 step->root->show_label = true;
1301 pivot_category_create_leaf (step->root, pivot_value_new_integer (1));
1303 double cox = (1.0 - exp ((initial_log_likelihood - log_likelihood)
1305 double entries[] = {
1306 -2 * log_likelihood,
1308 cox / (1.0 - exp(initial_log_likelihood * (2 / res->cc)))
1310 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1311 pivot_table_put2 (table, i, 0, pivot_value_new_number (entries[i]));
1313 pivot_table_submit (table);
1316 /* Show the case processing summary box */
1318 case_processing_summary (const struct lr_result *res)
1320 struct pivot_table *table = pivot_table_create (
1321 N_("Case Processing Summary"));
1323 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1324 N_("N"), PIVOT_RC_COUNT,
1325 N_("Percent"), PIVOT_RC_PERCENT);
1327 struct pivot_dimension *cases = pivot_dimension_create (
1328 table, PIVOT_AXIS_ROW, N_("Unweighted Cases"),
1329 N_("Included in Analysis"), N_("Missing Cases"), N_("Total"));
1330 cases->root->show_label = true;
1332 double total = res->n_nonmissing + res->n_missing;
1340 { 0, 0, res->n_nonmissing },
1341 { 0, 1, res->n_missing },
1343 { 1, 0, 100.0 * res->n_nonmissing / total },
1344 { 1, 1, 100.0 * res->n_missing / total },
1347 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1348 pivot_table_put2 (table, entries[i].stat_idx, entries[i].case_idx,
1349 pivot_value_new_number (entries[i].x));
1351 pivot_table_submit (table);
1355 output_categories (const struct lr_spec *cmd, const struct lr_result *res)
1357 struct pivot_table *table = pivot_table_create (
1358 N_("Categorical Variables' Codings"));
1359 pivot_table_set_weight_var (table, dict_get_weight (cmd->dict));
1363 for (int i = 0; i < cmd->n_cat_predictors; ++i)
1365 size_t n = categoricals_n_count (res->cats, i);
1366 size_t df = categoricals_df (res->cats, i);
1372 struct pivot_dimension *codings = pivot_dimension_create (
1373 table, PIVOT_AXIS_COLUMN, N_("Codings"),
1374 N_("Frequency"), PIVOT_RC_COUNT);
1375 struct pivot_category *coding_group = pivot_category_create_group (
1376 codings->root, N_("Parameter coding"));
1377 for (int i = 0; i < max_df; ++i)
1378 pivot_category_create_leaf_rc (
1380 pivot_value_new_user_text_nocopy (xasprintf ("(%d)", i + 1)),
1383 struct pivot_dimension *categories = pivot_dimension_create (
1384 table, PIVOT_AXIS_ROW, N_("Categories"));
1386 int cumulative_df = 0;
1387 for (int v = 0; v < cmd->n_cat_predictors; ++v)
1390 const struct interaction *cat_predictors = cmd->cat_predictors[v];
1391 int df = categoricals_df (res->cats, v);
1393 struct string str = DS_EMPTY_INITIALIZER;
1394 interaction_to_string (cat_predictors, &str);
1395 struct pivot_category *var_group = pivot_category_create_group__ (
1397 pivot_value_new_user_text_nocopy (ds_steal_cstr (&str)));
1399 for (cat = 0; cat < categoricals_n_count (res->cats, v) ; ++cat)
1401 const struct ccase *c = categoricals_get_case_by_category_real (
1403 struct string label = DS_EMPTY_INITIALIZER;
1404 for (int x = 0; x < cat_predictors->n_vars; ++x)
1406 if (!ds_is_empty (&label))
1407 ds_put_byte (&label, ' ');
1409 const union value *val = case_data (c, cat_predictors->vars[x]);
1410 var_append_value_name (cat_predictors->vars[x], val, &label);
1412 int cat_idx = pivot_category_create_leaf (
1414 pivot_value_new_user_text_nocopy (ds_steal_cstr (&label)));
1416 double *freq = categoricals_get_user_data_by_category_real (
1418 pivot_table_put2 (table, 0, cat_idx, pivot_value_new_number (*freq));
1420 for (int x = 0; x < df; ++x)
1421 pivot_table_put2 (table, x + 1, cat_idx,
1422 pivot_value_new_number (cat == x));
1424 cumulative_df += df;
1427 pivot_table_submit (table);
1431 create_classification_dimension (const struct lr_spec *cmd,
1432 const struct lr_result *res,
1433 struct pivot_table *table,
1434 enum pivot_axis_type axis_type,
1435 const char *label, const char *total)
1437 struct pivot_dimension *d = pivot_dimension_create (
1438 table, axis_type, label);
1439 d->root->show_label = true;
1440 struct pivot_category *pred_group = pivot_category_create_group__ (
1441 d->root, pivot_value_new_variable (cmd->dep_var));
1442 for (int i = 0; i < 2; i++)
1444 const union value *y = i ? &res->y1 : &res->y0;
1445 pivot_category_create_leaf_rc (
1446 pred_group, pivot_value_new_var_value (cmd->dep_var, y),
1449 pivot_category_create_leaves (d->root, total, PIVOT_RC_PERCENT);
1453 output_classification_table (const struct lr_spec *cmd, const struct lr_result *res)
1455 struct pivot_table *table = pivot_table_create (N_("Classification Table"));
1456 pivot_table_set_weight_var (table, cmd->wv);
1458 create_classification_dimension (cmd, res, table, PIVOT_AXIS_COLUMN,
1459 N_("Predicted"), N_("Percentage Correct"));
1460 create_classification_dimension (cmd, res, table, PIVOT_AXIS_ROW,
1461 N_("Observed"), N_("Overall Percentage"));
1463 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Step"), N_("Step 1"));
1476 { 2, 0, 100 * res->tn / (res->tn + res->fp) },
1477 { 2, 1, 100 * res->tp / (res->tp + res->fn) },
1479 100 * (res->tp + res->tn) / (res->tp + res->tn + res->fp + res->fn)},
1481 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1483 const struct entry *e = &entries[i];
1484 pivot_table_put3 (table, e->pred_idx, e->obs_idx, 0,
1485 pivot_value_new_number (e->x));
1488 pivot_table_submit (table);