Remove unneeded #includes.
[pspp] / src / language / stats / regression.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2005, 2009, 2010, 2011, 2012, 2013, 2014,
3    2016, 2017, 2019 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include <float.h>
21 #include <stdbool.h>
22
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_cdf.h>
25 #include <gsl/gsl_matrix.h>
26
27 #include <data/dataset.h>
28 #include <data/casewriter.h>
29
30 #include "language/command.h"
31 #include "language/lexer/lexer.h"
32 #include "language/lexer/value-parser.h"
33 #include "language/lexer/variable-parser.h"
34
35
36 #include "data/casegrouper.h"
37 #include "data/casereader.h"
38 #include "data/dictionary.h"
39
40 #include "math/covariance.h"
41 #include "math/linreg.h"
42 #include "math/moments.h"
43
44 #include "libpspp/message.h"
45 #include "libpspp/taint.h"
46
47 #include "output/pivot-table.h"
48
49 #include "gl/intprops.h"
50 #include "gl/minmax.h"
51
52 #include "gettext.h"
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) msgid
55
56 #define STATS_R      (1 << 0)
57 #define STATS_COEFF  (1 << 1)
58 #define STATS_ANOVA  (1 << 2)
59 #define STATS_OUTS   (1 << 3)
60 #define STATS_CI     (1 << 4)
61 #define STATS_BCOV   (1 << 5)
62 #define STATS_TOL    (1 << 6)
63
64 #define STATS_DEFAULT  (STATS_R | STATS_COEFF | STATS_ANOVA | STATS_OUTS)
65
66
67 struct regression
68   {
69     struct dataset *ds;
70
71     const struct variable **vars;
72     size_t n_vars;
73
74     const struct variable **dep_vars;
75     size_t n_dep_vars;
76
77     unsigned int stats;
78     double ci;
79
80     bool resid;
81     bool pred;
82
83     bool origin;
84   };
85
86 struct regression_workspace
87 {
88   /* The new variables which will be introduced by /SAVE */
89   const struct variable **predvars;
90   const struct variable **residvars;
91
92   /* A reader/writer pair to temporarily hold the
93      values of the new variables */
94   struct casewriter *writer;
95   struct casereader *reader;
96
97   /* Indeces of the new values in the reader/writer (-1 if not applicable) */
98   int res_idx;
99   int pred_idx;
100
101   /* 0, 1 or 2 depending on what new variables are to be created */
102   int extras;
103 };
104
105 static void run_regression (const struct regression *cmd,
106                             struct regression_workspace *ws,
107                             struct casereader *input);
108
109
110 /* Return a string based on PREFIX which may be used as the name
111    of a new variable in DICT */
112 static char *
113 reg_get_name (const struct dictionary *dict, const char *prefix)
114 {
115   for (size_t i = 1; ; i++)
116     {
117       char *name = xasprintf ("%s%zu", prefix, i);
118       if (!dict_lookup_var (dict, name))
119         return name;
120       free (name);
121     }
122 }
123
124 static const struct variable *
125 create_aux_var (struct dataset *ds, const char *prefix)
126 {
127   struct dictionary *dict = dataset_dict (ds);
128   char *name = reg_get_name (dict, prefix);
129   struct variable *var = dict_create_var_assert (dict, name, 0);
130   free (name);
131   return var;
132 }
133
134 /* Auxiliary data for transformation when /SAVE is entered */
135 struct save_trans_data
136   {
137     int n_dep_vars;
138     struct regression_workspace *ws;
139   };
140
141 static bool
142 save_trans_free (void *aux)
143 {
144   struct save_trans_data *save_trans_data = aux;
145   free (save_trans_data->ws->predvars);
146   free (save_trans_data->ws->residvars);
147
148   casereader_destroy (save_trans_data->ws->reader);
149   free (save_trans_data->ws);
150   free (save_trans_data);
151   return true;
152 }
153
154 static enum trns_result
155 save_trans_func (void *aux, struct ccase **c, casenumber x UNUSED)
156 {
157   struct save_trans_data *save_trans_data = aux;
158   struct regression_workspace *ws = save_trans_data->ws;
159   struct ccase *in = casereader_read (ws->reader);
160
161   if (in)
162     {
163       *c = case_unshare (*c);
164
165       for (size_t k = 0; k < save_trans_data->n_dep_vars; ++k)
166         {
167           if (ws->pred_idx != -1)
168             {
169               double pred = case_num_idx (in, ws->extras * k + ws->pred_idx);
170               *case_num_rw (*c, ws->predvars[k]) = pred;
171             }
172
173           if (ws->res_idx != -1)
174             {
175               double resid = case_num_idx (in, ws->extras * k + ws->res_idx);
176               *case_num_rw (*c, ws->residvars[k]) = resid;
177             }
178         }
179       case_unref (in);
180     }
181
182   return TRNS_CONTINUE;
183 }
184
185 int
186 cmd_regression (struct lexer *lexer, struct dataset *ds)
187 {
188   const struct dictionary *dict = dataset_dict (ds);
189
190   struct regression regression = {
191     .ci = 0.95,
192     .stats = STATS_DEFAULT,
193     .pred = false,
194     .resid = false,
195     .ds = ds,
196     .origin = false,
197   };
198
199   bool variables_seen = false;
200   bool method_seen = false;
201   bool dependent_seen = false;
202   int save_start = 0;
203   int save_end = 0;
204   while (lex_token (lexer) != T_ENDCMD)
205     {
206       lex_match (lexer, T_SLASH);
207
208       if (lex_match_id (lexer, "VARIABLES"))
209         {
210           if (method_seen)
211             {
212               lex_next_error (lexer, -1, -1,
213                               _("VARIABLES may not appear after %s"), "METHOD");
214               goto error;
215             }
216           if (dependent_seen)
217             {
218               lex_next_error (lexer, -1, -1,
219                               _("VARIABLES may not appear after %s"), "DEPENDENT");
220               goto error;
221             }
222           variables_seen = true;
223           lex_match (lexer, T_EQUALS);
224
225           if (!parse_variables_const (lexer, dict,
226                                       &regression.vars, &regression.n_vars,
227                                       PV_NO_DUPLICATE | PV_NUMERIC))
228             goto error;
229         }
230       else if (lex_match_id (lexer, "DEPENDENT"))
231         {
232           dependent_seen = true;
233           lex_match (lexer, T_EQUALS);
234
235           free (regression.dep_vars);
236           regression.n_dep_vars = 0;
237
238           if (!parse_variables_const (lexer, dict,
239                                       &regression.dep_vars,
240                                       &regression.n_dep_vars,
241                                       PV_NO_DUPLICATE | PV_NUMERIC))
242             goto error;
243         }
244       else if (lex_match_id (lexer, "ORIGIN"))
245         regression.origin = true;
246       else if (lex_match_id (lexer, "NOORIGIN"))
247         regression.origin = false;
248       else if (lex_match_id (lexer, "METHOD"))
249         {
250           method_seen = true;
251           lex_match (lexer, T_EQUALS);
252
253           if (!lex_force_match_id (lexer, "ENTER"))
254             goto error;
255
256           if (!variables_seen)
257             {
258               if (!parse_variables_const (lexer, dict,
259                                           &regression.vars, &regression.n_vars,
260                                           PV_NO_DUPLICATE | PV_NUMERIC))
261                 goto error;
262             }
263         }
264       else if (lex_match_id (lexer, "STATISTICS"))
265         {
266           unsigned long statistics = 0;
267           lex_match (lexer, T_EQUALS);
268
269           while (lex_token (lexer) != T_ENDCMD
270                  && lex_token (lexer) != T_SLASH)
271             {
272               if (lex_match (lexer, T_ALL))
273                 statistics = ~0;
274               else if (lex_match_id (lexer, "DEFAULTS"))
275                 statistics |= STATS_DEFAULT;
276               else if (lex_match_id (lexer, "R"))
277                 statistics |= STATS_R;
278               else if (lex_match_id (lexer, "COEFF"))
279                 statistics |= STATS_COEFF;
280               else if (lex_match_id (lexer, "ANOVA"))
281                 statistics |= STATS_ANOVA;
282               else if (lex_match_id (lexer, "BCOV"))
283                 statistics |= STATS_BCOV;
284               else if (lex_match_id (lexer, "TOL"))
285                 statistics |= STATS_TOL;
286               else if (lex_match_id (lexer, "CI"))
287                 {
288                   statistics |= STATS_CI;
289
290                   if (lex_match (lexer, T_LPAREN))
291                     {
292                       if (!lex_force_num (lexer))
293                         goto error;
294                       regression.ci = lex_number (lexer) / 100.0;
295                       lex_get (lexer);
296
297                       if (!lex_force_match (lexer, T_RPAREN))
298                         goto error;
299                     }
300                 }
301               else
302                 {
303                   lex_error_expecting (lexer, "ALL", "DEFAULTS", "R", "COEFF",
304                                        "ANOVA", "BCOV", "TOL", "CI");
305                   goto error;
306                 }
307             }
308
309           if (statistics)
310             regression.stats = statistics;
311         }
312       else if (lex_match_id (lexer, "SAVE"))
313         {
314           save_start = lex_ofs (lexer) - 1;
315           lex_match (lexer, T_EQUALS);
316
317           while (lex_token (lexer) != T_ENDCMD
318                  && lex_token (lexer) != T_SLASH)
319             {
320               if (lex_match_id (lexer, "PRED"))
321                 regression.pred = true;
322               else if (lex_match_id (lexer, "RESID"))
323                 regression.resid = true;
324               else
325                 {
326                   lex_error_expecting (lexer, "PRED", "RESID");
327                   goto error;
328                 }
329             }
330           save_end = lex_ofs (lexer) - 1;
331         }
332       else
333         {
334           lex_error_expecting (lexer, "VARIABLES", "DEPENDENT", "ORIGIN",
335                                "NOORIGIN", "METHOD", "STATISTICS", "SAVE");
336           goto error;
337         }
338     }
339
340   if (!regression.vars)
341     dict_get_vars (dict, &regression.vars, &regression.n_vars, 0);
342
343   struct regression_workspace workspace = {
344     .res_idx = -1,
345     .pred_idx = -1,
346   };
347
348   bool save = regression.pred || regression.resid;
349   if (save)
350     {
351       struct caseproto *proto = caseproto_create ();
352
353       if (regression.resid)
354         {
355           workspace.res_idx = workspace.extras ++;
356           workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
357
358           for (size_t i = 0; i < regression.n_dep_vars; ++i)
359             {
360               workspace.residvars[i] = create_aux_var (ds, "RES");
361               proto = caseproto_add_width (proto, 0);
362             }
363         }
364
365       if (regression.pred)
366         {
367           workspace.pred_idx = workspace.extras ++;
368           workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
369
370           for (size_t i = 0; i < regression.n_dep_vars; ++i)
371             {
372               workspace.predvars[i] = create_aux_var (ds, "PRED");
373               proto = caseproto_add_width (proto, 0);
374             }
375         }
376
377       if (proc_make_temporary_transformations_permanent (ds))
378         lex_ofs_msg (lexer, SW, save_start, save_end,
379                      _("REGRESSION with SAVE ignores TEMPORARY.  "
380                        "Temporary transformations will be made permanent."));
381
382       if (dict_get_filter (dict))
383         lex_ofs_msg (lexer, SW, save_start, save_end,
384                      _("REGRESSION with SAVE ignores FILTER.  "
385                        "All cases will be processed."));
386
387       workspace.writer = autopaging_writer_create (proto);
388       caseproto_unref (proto);
389     }
390
391   struct casegrouper *grouper = casegrouper_create_splits (
392     proc_open_filtering (ds, !save), dict);
393   struct casereader *group;
394   while (casegrouper_get_next_group (grouper, &group))
395     {
396       run_regression (&regression,
397                       &workspace,
398                       group);
399
400     }
401   bool ok = casegrouper_destroy (grouper);
402   ok = proc_commit (ds) && ok;
403
404   if (workspace.writer)
405     {
406       struct save_trans_data *save_trans_data = xmalloc (sizeof *save_trans_data);
407       struct casereader *r = casewriter_make_reader (workspace.writer);
408       workspace.writer = NULL;
409       workspace.reader = r;
410       save_trans_data->ws = xmalloc (sizeof (workspace));
411       memcpy (save_trans_data->ws, &workspace, sizeof (workspace));
412       save_trans_data->n_dep_vars = regression.n_dep_vars;
413
414       static const struct trns_class trns_class = {
415         .name = "REGRESSION",
416         .execute = save_trans_func,
417         .destroy = save_trans_free,
418       };
419       add_transformation (ds, &trns_class, save_trans_data);
420     }
421
422   free (regression.vars);
423   free (regression.dep_vars);
424   return ok ? CMD_SUCCESS : CMD_FAILURE;
425
426 error:
427   free (regression.vars);
428   free (regression.dep_vars);
429   return CMD_FAILURE;
430 }
431
432 /* Return the size of the union of dependent and independent variables */
433 static size_t
434 get_n_all_vars (const struct regression *cmd)
435 {
436   size_t result = cmd->n_vars + cmd->n_dep_vars;
437   for (size_t i = 0; i < cmd->n_dep_vars; i++)
438     for (size_t j = 0; j < cmd->n_vars; j++)
439       if (cmd->vars[j] == cmd->dep_vars[i])
440         result--;
441   return result;
442 }
443
444 /* Fill VARS with the union of dependent and independent variables */
445 static void
446 fill_all_vars (const struct variable **vars, const struct regression *cmd)
447 {
448   for (size_t i = 0; i < cmd->n_vars; i++)
449     vars[i] = cmd->vars[i];
450
451   size_t x = 0;
452   for (size_t i = 0; i < cmd->n_dep_vars; i++)
453     {
454       bool absent = true;
455       for (size_t j = 0; j < cmd->n_vars; j++)
456         if (cmd->dep_vars[i] == cmd->vars[j])
457           {
458             absent = false;
459             break;
460           }
461       if (absent)
462         vars[cmd->n_vars + x++] = cmd->dep_vars[i];
463     }
464 }
465
466
467 /* Fill the array VARS, with all the predictor variables from CMD, except
468    variable X */
469 static void
470 fill_predictor_x (const struct variable **vars, const struct variable *x, const struct regression *cmd)
471 {
472   size_t n = 0;
473   for (size_t i = 0; i < cmd->n_vars; i++)
474     if (cmd->vars[i] != x)
475       vars[n++] = cmd->vars[i];
476 }
477
478 /*
479   Is variable k the dependent variable?
480 */
481 static bool
482 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
483 {
484   return v == cmd->vars[k];
485 }
486
487 /* Identify the explanatory variables in v_variables.  Returns
488    the number of independent variables. */
489 static int
490 identify_indep_vars (const struct regression *cmd,
491                      const struct variable **indep_vars,
492                      const struct variable *depvar)
493 {
494   int n_indep_vars = 0;
495
496   for (size_t i = 0; i < cmd->n_vars; i++)
497     if (!is_depvar (cmd, i, depvar))
498       indep_vars[n_indep_vars++] = cmd->vars[i];
499   if (n_indep_vars < 1 && is_depvar (cmd, 0, depvar))
500     {
501       /*
502          There is only one independent variable, and it is the same
503          as the dependent variable. Print a warning and continue.
504        */
505       msg (SW,
506            _("The dependent variable is equal to the independent variable. "
507              "The least squares line is therefore Y=X. "
508              "Standard errors and related statistics may be meaningless."));
509       n_indep_vars = 1;
510       indep_vars[0] = cmd->vars[0];
511     }
512   return n_indep_vars;
513 }
514
515 static double
516 fill_covariance (gsl_matrix * cov, struct covariance *all_cov,
517                  const struct variable **vars,
518                  size_t n_vars, const struct variable *dep_var,
519                  const struct variable **all_vars, size_t n_all_vars,
520                  double *means)
521 {
522   const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
523   if (!cm)
524     return 0;
525
526   size_t *rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
527
528   size_t dep_subscript = SIZE_MAX;
529   for (size_t i = 0; i < n_all_vars; i++)
530     {
531       for (size_t j = 0; j < n_vars; j++)
532         if (vars[j] == all_vars[i])
533           rows[j] = i;
534       if (all_vars[i] == dep_var)
535         dep_subscript = i;
536     }
537   assert (dep_subscript != SIZE_MAX);
538
539   const gsl_matrix *mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
540   const gsl_matrix *ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
541   for (size_t i = 0; i < cov->size1 - 1; i++)
542     {
543       means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
544         / gsl_matrix_get (ssize_matrix, rows[i], 0);
545       for (size_t j = 0; j < cov->size2 - 1; j++)
546         {
547           gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
548           gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
549         }
550     }
551   means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
552     / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
553   const gsl_matrix *ssizes = covariance_moments (all_cov, MOMENT_NONE);
554   double result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
555   for (size_t i = 0; i < cov->size1 - 1; i++)
556     {
557       gsl_matrix_set (cov, i, cov->size1 - 1,
558                       gsl_matrix_get (cm, rows[i], dep_subscript));
559       gsl_matrix_set (cov, cov->size1 - 1, i,
560                       gsl_matrix_get (cm, rows[i], dep_subscript));
561       if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
562         result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
563     }
564   gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
565                   gsl_matrix_get (cm, dep_subscript, dep_subscript));
566   free (rows);
567   return result;
568 }
569
570 \f
571
572 struct model_container
573 {
574   struct linreg **models;
575 };
576
577 /*
578   STATISTICS subcommand output functions.
579 */
580 static void reg_stats_r (const struct linreg *,     const struct variable *);
581 static void reg_stats_coeff (const struct regression *, const struct linreg *,
582                              const struct model_container *, const gsl_matrix *,
583                              const struct variable *);
584 static void reg_stats_anova (const struct linreg *, const struct variable *);
585 static void reg_stats_bcov (const struct linreg *,  const struct variable *);
586
587
588 static struct linreg **
589 run_regression_get_models (const struct regression *cmd,
590                            struct casereader *input,
591                            bool output)
592 {
593   struct model_container *model_container = XCALLOC (cmd->n_vars, struct model_container);
594
595   struct ccase *c;
596   struct covariance *cov;
597   struct casereader *reader;
598
599   if (cmd->stats & STATS_TOL)
600     for (size_t i = 0; i < cmd->n_vars; i++)
601       {
602         struct regression subreg = {
603           .origin = cmd->origin,
604           .ds = cmd->ds,
605           .n_vars = cmd->n_vars - 1,
606           .n_dep_vars = 1,
607           .vars = xmalloc ((cmd->n_vars - 1) * sizeof *subreg.vars),
608           .dep_vars = &cmd->vars[i],
609           .stats = STATS_R,
610           .ci = 0,
611           .resid = false,
612           .pred = false,
613         };
614         fill_predictor_x (subreg.vars, cmd->vars[i], cmd);
615
616         model_container[i].models =
617           run_regression_get_models (&subreg, input, false);
618         free (subreg.vars);
619       }
620
621   size_t n_all_vars = get_n_all_vars (cmd);
622   const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
623
624   /* In the (rather pointless) case where the dependent variable is
625      the independent variable, n_all_vars == 1.
626      However this would result in a buffer overflow so we must
627      over-allocate the space required in this malloc call.
628      See bug #58599  */
629   double *means = xnmalloc (MAX (2, n_all_vars), sizeof *means);
630   fill_all_vars (all_vars, cmd);
631   cov = covariance_1pass_create (n_all_vars, all_vars,
632                                  dict_get_weight (dataset_dict (cmd->ds)),
633                                  MV_ANY, cmd->origin == false);
634
635   reader = casereader_clone (input);
636   reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
637                                              MV_ANY, NULL, NULL);
638
639   struct casereader *r = casereader_clone (reader);
640   for (; (c = casereader_read (r)) != NULL; case_unref (c))
641       covariance_accumulate (cov, c);
642   casereader_destroy (r);
643
644   struct linreg **models = XCALLOC (cmd->n_dep_vars, struct linreg*);
645   for (size_t k = 0; k < cmd->n_dep_vars; k++)
646     {
647       const struct variable **vars = xnmalloc (cmd->n_vars, sizeof *vars);
648       const struct variable *dep_var = cmd->dep_vars[k];
649       int n_indep = identify_indep_vars (cmd, vars, dep_var);
650       gsl_matrix *cov_matrix = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
651       double n_data = fill_covariance (cov_matrix, cov, vars, n_indep,
652                                        dep_var, all_vars, n_all_vars, means);
653       models[k] = linreg_alloc (dep_var, vars,  n_data, n_indep, cmd->origin);
654       for (size_t i = 0; i < n_indep; i++)
655         linreg_set_indep_variable_mean (models[k], i, means[i]);
656       linreg_set_depvar_mean (models[k], means[n_indep]);
657       if (n_data > 0)
658         {
659           linreg_fit (cov_matrix, models[k]);
660
661           if (output
662               && !taint_has_tainted_successor (casereader_get_taint (input)))
663             {
664               /*
665                 Find the least-squares estimates and other statistics.
666               */
667               if (cmd->stats & STATS_R)
668                 reg_stats_r (models[k], dep_var);
669
670               if (cmd->stats & STATS_ANOVA)
671                 reg_stats_anova (models[k], dep_var);
672
673               if (cmd->stats & STATS_COEFF)
674                 reg_stats_coeff (cmd, models[k],
675                                  model_container,
676                                  cov_matrix, dep_var);
677
678               if (cmd->stats & STATS_BCOV)
679                 reg_stats_bcov  (models[k], dep_var);
680             }
681         }
682       else
683         msg (SE, _("No valid data found. This command was skipped."));
684       free (vars);
685       gsl_matrix_free (cov_matrix);
686     }
687
688   casereader_destroy (reader);
689
690   for (size_t i = 0; i < cmd->n_vars; i++)
691     {
692       if (model_container[i].models)
693         linreg_unref (model_container[i].models[0]);
694       free (model_container[i].models);
695     }
696   free (model_container);
697
698   free (all_vars);
699   free (means);
700   covariance_destroy (cov);
701   return models;
702 }
703
704 static void
705 run_regression (const struct regression *cmd,
706                 struct regression_workspace *ws,
707                 struct casereader *input)
708 {
709   struct linreg **models = run_regression_get_models (cmd, input, true);
710
711   if (ws->extras > 0)
712     {
713       struct ccase *c;
714       struct casereader *r = casereader_clone (input);
715
716       for (; (c = casereader_read (r)) != NULL; case_unref (c))
717         {
718           struct ccase *outc = case_create (casewriter_get_proto (ws->writer));
719           for (int k = 0; k < cmd->n_dep_vars; k++)
720             {
721               const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
722               const struct variable *dep_var = cmd->dep_vars[k];
723               int n_indep = identify_indep_vars (cmd, vars, dep_var);
724               double *vals = xnmalloc (n_indep, sizeof (*vals));
725               for (int i = 0; i < n_indep; i++)
726                 {
727                   const union value *tmp = case_data (c, vars[i]);
728                   vals[i] = tmp->f;
729                 }
730
731               if (cmd->pred)
732                 {
733                   double pred = linreg_predict (models[k], vals, n_indep);
734                   *case_num_rw_idx (outc, k * ws->extras + ws->pred_idx) = pred;
735                 }
736
737               if (cmd->resid)
738                 {
739                   double obs = case_num (c, linreg_dep_var (models[k]));
740                   double res = linreg_residual (models[k], obs,  vals, n_indep);
741                   *case_num_rw_idx (outc, k * ws->extras + ws->res_idx) = res;
742                 }
743               free (vals);
744               free (vars);
745             }
746           casewriter_write (ws->writer, outc);
747         }
748       casereader_destroy (r);
749     }
750
751   for (size_t k = 0; k < cmd->n_dep_vars; k++)
752     linreg_unref (models[k]);
753
754   free (models);
755   casereader_destroy (input);
756 }
757
758 \f
759
760
761 static void
762 reg_stats_r (const struct linreg * c, const struct variable *var)
763 {
764   struct pivot_table *table = pivot_table_create__ (
765     pivot_value_new_text_format (N_("Model Summary (%s)"),
766                                  var_to_string (var)),
767     "Model Summary");
768
769   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
770                           N_("R"), N_("R Square"), N_("Adjusted R Square"),
771                           N_("Std. Error of the Estimate"));
772
773   double rsq = linreg_ssreg (c) / linreg_sst (c);
774   double adjrsq = (rsq -
775                    (1.0 - rsq) * linreg_n_coeffs (c)
776                    / (linreg_n_obs (c) - linreg_n_coeffs (c) - 1));
777   double std_error = sqrt (linreg_mse (c));
778
779   double entries[] = {
780     sqrt (rsq), rsq, adjrsq, std_error
781   };
782   for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
783     pivot_table_put1 (table, i, pivot_value_new_number (entries[i]));
784
785   pivot_table_submit (table);
786 }
787
788 /*
789   Table showing estimated regression coefficients.
790 */
791 static void
792 reg_stats_coeff (const struct regression *cmd, const struct linreg *c,
793                  const struct model_container *mc, const gsl_matrix *cov,
794                  const struct variable *var)
795 {
796   struct pivot_table *table = pivot_table_create__ (
797     pivot_value_new_text_format (N_("Coefficients (%s)"), var_to_string (var)),
798     "Coefficients");
799
800   struct pivot_dimension *statistics = pivot_dimension_create (
801     table, PIVOT_AXIS_COLUMN, N_("Statistics"));
802   pivot_category_create_group (statistics->root,
803                                N_("Unstandardized Coefficients"),
804                                N_("B"), N_("Std. Error"));
805   pivot_category_create_group (statistics->root,
806                                N_("Standardized Coefficients"), N_("Beta"));
807   pivot_category_create_leaves (statistics->root, N_("t"),
808                                 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
809   if (cmd->stats & STATS_CI)
810     {
811       struct pivot_category *interval = pivot_category_create_group__ (
812         statistics->root, pivot_value_new_text_format (
813           N_("%g%% Confidence Interval for B"),
814           cmd->ci * 100.0));
815       pivot_category_create_leaves (interval, N_("Lower Bound"),
816                                     N_("Upper Bound"));
817     }
818
819   if (cmd->stats & STATS_TOL)
820     pivot_category_create_group (statistics->root,
821                                  N_("Collinearity Statistics"),
822                                  N_("Tolerance"), N_("VIF"));
823
824
825   struct pivot_dimension *variables = pivot_dimension_create (
826     table, PIVOT_AXIS_ROW, N_("Variables"));
827
828   double df = linreg_n_obs (c) - linreg_n_coeffs (c) - 1;
829   double q = (1 - cmd->ci) / 2.0;  /* 2-tailed test */
830   double tval = gsl_cdf_tdist_Qinv (q, df);
831
832   if (!cmd->origin)
833     {
834       int var_idx = pivot_category_create_leaf (
835         variables->root, pivot_value_new_text (N_("(Constant)")));
836
837       double std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
838       double t_stat = linreg_intercept (c) / std_err;
839       double base_entries[] = {
840         linreg_intercept (c),
841         std_err,
842         0.0,
843         t_stat,
844         2.0 * gsl_cdf_tdist_Q (fabs (t_stat),
845                                linreg_n_obs (c) - linreg_n_coeffs (c)),
846       };
847
848       size_t col = 0;
849       for (size_t i = 0; i < sizeof base_entries / sizeof *base_entries; i++)
850         pivot_table_put2 (table, col++, var_idx,
851                           pivot_value_new_number (base_entries[i]));
852
853       if (cmd->stats & STATS_CI)
854         {
855           double interval_entries[] = {
856             linreg_intercept (c) - tval * std_err,
857             linreg_intercept (c) + tval * std_err,
858           };
859
860           for (size_t i = 0; i < sizeof interval_entries / sizeof *interval_entries; i++)
861             pivot_table_put2 (table, col++, var_idx,
862                               pivot_value_new_number (interval_entries[i]));
863         }
864     }
865
866   for (size_t j = 0; j < linreg_n_coeffs (c); j++)
867     {
868       const struct variable *v = linreg_indep_var (c, j);
869       int var_idx = pivot_category_create_leaf (
870         variables->root, pivot_value_new_variable (v));
871
872       double std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
873       double t_stat = linreg_coeff (c, j) / std_err;
874       double base_entries[] = {
875         linreg_coeff (c, j),
876         sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1)),
877         (sqrt (gsl_matrix_get (cov, j, j)) * linreg_coeff (c, j) /
878          sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1))),
879         t_stat,
880         2 * gsl_cdf_tdist_Q (fabs (t_stat), df)
881       };
882
883       size_t col = 0;
884       for (size_t i = 0; i < sizeof base_entries / sizeof *base_entries; i++)
885         pivot_table_put2 (table, col++, var_idx,
886                           pivot_value_new_number (base_entries[i]));
887
888       if (cmd->stats & STATS_CI)
889         {
890           double interval_entries[] = {
891             linreg_coeff (c, j)  - tval * std_err,
892             linreg_coeff (c, j)  + tval * std_err,
893           };
894
895
896           for (size_t i = 0; i < sizeof interval_entries / sizeof *interval_entries; i++)
897             pivot_table_put2 (table, col++, var_idx,
898                               pivot_value_new_number (interval_entries[i]));
899         }
900
901       if (cmd->stats & STATS_TOL)
902         {
903           {
904             struct linreg *m = mc[j].models[0];
905             double rsq = linreg_ssreg (m) / linreg_sst (m);
906             pivot_table_put2 (table, col++, var_idx, pivot_value_new_number (1.0 - rsq));
907             pivot_table_put2 (table, col++, var_idx, pivot_value_new_number (1.0 / (1.0 - rsq)));
908           }
909         }
910     }
911
912   pivot_table_submit (table);
913 }
914
915 /*
916   Display the ANOVA table.
917 */
918 static void
919 reg_stats_anova (const struct linreg * c, const struct variable *var)
920 {
921   struct pivot_table *table = pivot_table_create__ (
922     pivot_value_new_text_format (N_("ANOVA (%s)"), var_to_string (var)),
923     "ANOVA");
924
925   pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
926                           N_("Sum of Squares"), PIVOT_RC_OTHER,
927                           N_("df"), PIVOT_RC_INTEGER,
928                           N_("Mean Square"), PIVOT_RC_OTHER,
929                           N_("F"), PIVOT_RC_OTHER,
930                           N_("Sig."), PIVOT_RC_SIGNIFICANCE);
931
932   pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Source"),
933                           N_("Regression"), N_("Residual"), N_("Total"));
934
935   double msm = linreg_ssreg (c) / linreg_dfmodel (c);
936   double mse = linreg_mse (c);
937   double F = msm / mse;
938
939   struct entry
940     {
941       int stat_idx;
942       int source_idx;
943       double x;
944     }
945   entries[] = {
946     /* Sums of Squares. */
947     { 0, 0, linreg_ssreg (c) },
948     { 0, 1, linreg_sse (c) },
949     { 0, 2, linreg_sst (c) },
950     /* Degrees of freedom. */
951     { 1, 0, linreg_dfmodel (c) },
952     { 1, 1, linreg_dferror (c) },
953     { 1, 2, linreg_dftotal (c) },
954     /* Mean Squares. */
955     { 2, 0, msm },
956     { 2, 1, mse },
957     /* F */
958     { 3, 0, F },
959     /* Significance. */
960     { 4, 0, gsl_cdf_fdist_Q (F, linreg_dfmodel (c), linreg_dferror (c)) },
961   };
962   for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
963     {
964       const struct entry *e = &entries[i];
965       pivot_table_put2 (table, e->stat_idx, e->source_idx,
966                         pivot_value_new_number (e->x));
967     }
968
969   pivot_table_submit (table);
970 }
971
972
973 static void
974 reg_stats_bcov (const struct linreg * c, const struct variable *var)
975 {
976   struct pivot_table *table = pivot_table_create__ (
977     pivot_value_new_text_format (N_("Coefficient Correlations (%s)"),
978                                  var_to_string (var)),
979     "Coefficient Correlations");
980
981   for (size_t i = 0; i < 2; i++)
982     {
983       struct pivot_dimension *models = pivot_dimension_create (
984         table, i ? PIVOT_AXIS_ROW : PIVOT_AXIS_COLUMN, N_("Models"));
985       for (size_t j = 0; j < linreg_n_coeffs (c); j++)
986         pivot_category_create_leaf (
987           models->root, pivot_value_new_variable (
988             linreg_indep_var (c, j)));
989     }
990
991   pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Statistics"),
992                           N_("Covariances"));
993
994   for (size_t i = 0; i < linreg_n_coeffs (c); i++)
995     for (size_t k = 0; k < linreg_n_coeffs (c); k++)
996       {
997         double cov = gsl_matrix_get (linreg_cov (c), MIN (i, k), MAX (i, k));
998         pivot_table_put3 (table, k, i, 0, pivot_value_new_number (cov));
999       }
1000
1001   pivot_table_submit (table);
1002 }