Fix leaks in REGRESSION
[pspp] / src / language / stats / regression.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2005, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdbool.h>
20
21 #include <gsl/gsl_cdf.h>
22 #include <gsl/gsl_matrix.h>
23
24 #include <data/dataset.h>
25
26 #include "language/command.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/value-parser.h"
29 #include "language/lexer/variable-parser.h"
30
31
32 #include "data/casegrouper.h"
33 #include "data/casereader.h"
34 #include "data/dictionary.h"
35
36 #include "math/covariance.h"
37 #include "math/linreg.h"
38 #include "math/moments.h"
39
40 #include "libpspp/message.h"
41 #include "libpspp/taint.h"
42
43 #include "output/tab.h"
44
45 #include "gettext.h"
46 #define _(msgid) gettext (msgid)
47 #define N_(msgid) msgid
48
49
50 #include <gl/intprops.h>
51
52 #define REG_LARGE_DATA 1000
53
54 struct regression
55 {
56   struct dataset *ds;
57
58   const struct variable **vars;
59   size_t n_vars;
60
61   const struct variable **dep_vars;
62   size_t n_dep_vars;
63
64   bool r;
65   bool coeff;
66   bool anova;
67   bool bcov;
68
69
70   bool resid;
71   bool pred;
72
73   linreg **models;
74 };
75
76
77 static void run_regression (const struct regression *cmd, struct casereader *input);
78
79
80
81 /*
82   Transformations for saving predicted values
83   and residuals, etc.
84 */
85 struct reg_trns
86 {
87   int n_trns;                   /* Number of transformations. */
88   int trns_id;                  /* Which trns is this one? */
89   linreg *c;            /* Linear model for this trns. */
90 };
91
92 /*
93   Gets the predicted values.
94 */
95 static int
96 regression_trns_pred_proc (void *t_, struct ccase **c,
97                            casenumber case_idx UNUSED)
98 {
99   size_t i;
100   size_t n_vals;
101   struct reg_trns *trns = t_;
102   linreg *model;
103   union value *output = NULL;
104   const union value *tmp;
105   double *vals;
106   const struct variable **vars = NULL;
107
108   assert (trns != NULL);
109   model = trns->c;
110   assert (model != NULL);
111   assert (model->depvar != NULL);
112   assert (model->pred != NULL);
113
114   vars = linreg_get_vars (model);
115   n_vals = linreg_n_coeffs (model);
116   vals = xnmalloc (n_vals, sizeof (*vals));
117   *c = case_unshare (*c);
118
119   output = case_data_rw (*c, model->pred);
120
121   for (i = 0; i < n_vals; i++)
122     {
123       tmp = case_data (*c, vars[i]);
124       vals[i] = tmp->f;
125     }
126   output->f = linreg_predict (model, vals, n_vals);
127   free (vals);
128   return TRNS_CONTINUE;
129 }
130
131 /*
132   Gets the residuals.
133 */
134 static int
135 regression_trns_resid_proc (void *t_, struct ccase **c,
136                             casenumber case_idx UNUSED)
137 {
138   size_t i;
139   size_t n_vals;
140   struct reg_trns *trns = t_;
141   linreg *model;
142   union value *output = NULL;
143   const union value *tmp;
144   double *vals = NULL;
145   double obs;
146   const struct variable **vars = NULL;
147
148   assert (trns != NULL);
149   model = trns->c;
150   assert (model != NULL);
151   assert (model->depvar != NULL);
152   assert (model->resid != NULL);
153
154   vars = linreg_get_vars (model);
155   n_vals = linreg_n_coeffs (model);
156
157   vals = xnmalloc (n_vals, sizeof (*vals));
158   *c = case_unshare (*c);
159   output = case_data_rw (*c, model->resid);
160   assert (output != NULL);
161
162   for (i = 0; i < n_vals; i++)
163     {
164       tmp = case_data (*c, vars[i]);
165       vals[i] = tmp->f;
166     }
167   tmp = case_data (*c, model->depvar);
168   obs = tmp->f;
169   output->f = linreg_residual (model, obs, vals, n_vals);
170   free (vals);
171
172   return TRNS_CONTINUE;
173 }
174
175
176 static char *
177 reg_get_name (const struct dictionary *dict, const char *prefix)
178 {
179   char *name;
180   int i;
181
182   /* XXX handle too-long prefixes */
183   name = xmalloc (strlen (prefix) + INT_BUFSIZE_BOUND (i) + 1);
184   for (i = 1; ; i++)
185     {
186       sprintf (name, "%s%d", prefix, i);
187       if (dict_lookup_var (dict, name) == NULL)
188         return name;
189     }
190 }
191
192 /*
193   Free the transformation. Free its linear model if this
194   transformation is the last one.
195 */
196 static bool
197 regression_trns_free (void *t_)
198 {
199   bool result = true;
200   struct reg_trns *t = t_;
201
202   if (t->trns_id == t->n_trns)
203     {
204       result = linreg_free (t->c);
205     }
206   free (t);
207
208   return result;
209 }
210
211 static void
212 reg_save_var (struct dataset *ds, const char *prefix, trns_proc_func * f,
213               linreg * c, struct variable **v, int n_trns)
214 {
215   struct dictionary *dict = dataset_dict (ds);
216   static int trns_index = 1;
217   char *name;
218   struct variable *new_var;
219   struct reg_trns *t = NULL;
220
221   t = xmalloc (sizeof (*t));
222   t->trns_id = trns_index;
223   t->n_trns = n_trns;
224   t->c = c;
225
226   name = reg_get_name (dict, prefix);
227   new_var = dict_create_var_assert (dict, name, 0);
228   free (name);
229
230   *v = new_var;
231   add_transformation (ds, f, regression_trns_free, t);
232   trns_index++;
233 }
234
235 static void
236 subcommand_save (const struct regression *cmd)
237 {
238   linreg **lc;
239   int n_trns = 0;
240
241   if ( cmd->resid ) n_trns++;
242   if ( cmd->pred ) n_trns++;
243
244   n_trns *= cmd->n_dep_vars;
245
246   for (lc = cmd->models; lc < cmd->models + cmd->n_dep_vars; lc++)
247     {
248       if (*lc != NULL)
249         {
250           if ((*lc)->depvar != NULL)
251             {
252               if (cmd->resid)
253                 {
254                   reg_save_var (cmd->ds, "RES", regression_trns_resid_proc, *lc,
255                                 &(*lc)->resid, n_trns);
256                 }
257               if (cmd->pred)
258                 {
259                   reg_save_var (cmd->ds, "PRED", regression_trns_pred_proc, *lc,
260                                 &(*lc)->pred, n_trns);
261                 }
262             }
263         }
264     }
265 }
266
267 int
268 cmd_regression (struct lexer *lexer, struct dataset *ds)
269 {
270   int k;
271   struct regression regression;
272   const struct dictionary *dict = dataset_dict (ds);
273
274   memset (&regression, 0, sizeof (struct regression));
275
276   regression.anova = true;
277   regression.coeff = true;
278   regression.r = true;
279
280   regression.pred = false;
281   regression.resid = false;
282
283   regression.ds = ds;
284
285   /* Accept an optional, completely pointless "/VARIABLES=" */
286   lex_match (lexer, T_SLASH);
287   if (lex_match_id  (lexer, "VARIABLES"))
288     {
289       if (! lex_force_match (lexer, T_EQUALS) )
290         goto error;
291     }
292
293   if (!parse_variables_const (lexer, dict,
294                               &regression.vars, &regression.n_vars,
295                               PV_NO_DUPLICATE | PV_NUMERIC))
296     goto error;
297
298
299   while (lex_token (lexer) != T_ENDCMD)
300     {
301       lex_match (lexer, T_SLASH);
302
303       if (lex_match_id  (lexer, "DEPENDENT"))
304         {
305           if (! lex_force_match (lexer, T_EQUALS) )
306             goto error;
307
308           if (!parse_variables_const (lexer, dict,
309                                       &regression.dep_vars, &regression.n_dep_vars,
310                                       PV_NO_DUPLICATE | PV_NUMERIC))
311             goto error;
312         }
313       else if (lex_match_id (lexer, "METHOD"))
314         {
315           lex_match (lexer, T_EQUALS);
316
317           if (!lex_force_match_id (lexer, "ENTER"))
318             {
319               goto error;
320             }
321         }
322       else if (lex_match_id (lexer, "STATISTICS"))
323         {
324           lex_match (lexer, T_EQUALS);
325
326           while (lex_token (lexer) != T_ENDCMD
327                  && lex_token (lexer) != T_SLASH)
328             {
329               if (lex_match (lexer, T_ALL))
330                 {
331                 }
332               else if (lex_match_id (lexer, "DEFAULTS"))
333                 {
334                 }
335               else if (lex_match_id (lexer, "R"))
336                 {
337                 }
338               else if (lex_match_id (lexer, "COEFF"))
339                 {
340                 }
341               else if (lex_match_id (lexer, "ANOVA"))
342                 {
343                 }
344               else if (lex_match_id (lexer, "BCOV"))
345                 {
346                 }
347               else
348                 {
349                   lex_error (lexer, NULL);
350                   goto error;
351                 }
352             }
353         }
354       else if (lex_match_id (lexer, "SAVE"))
355         {
356           lex_match (lexer, T_EQUALS);
357
358           while (lex_token (lexer) != T_ENDCMD
359                  && lex_token (lexer) != T_SLASH)
360             {
361               if (lex_match_id (lexer, "PRED"))
362                 {
363                   regression.pred = true;
364                 }
365               else if (lex_match_id (lexer, "RESID"))
366                 {
367                   regression.resid = true;
368                 }
369               else
370                 {
371                   lex_error (lexer, NULL);
372                   goto error;
373                 }
374             }
375         }
376       else
377         {
378           lex_error (lexer, NULL);
379           goto error;
380         }
381     }
382
383   if (!regression.vars)
384     {
385       dict_get_vars (dict, &regression.vars, &regression.n_vars, 0);
386     }
387
388
389   regression.models = xcalloc (regression.n_dep_vars, sizeof *regression.models);
390
391   {
392     struct casegrouper *grouper;
393     struct casereader *group;
394     bool ok;
395     
396     grouper = casegrouper_create_splits (proc_open (ds), dict);
397     while (casegrouper_get_next_group (grouper, &group))
398       run_regression (&regression, group);
399     ok = casegrouper_destroy (grouper);
400     ok = proc_commit (ds) && ok;
401   }
402
403   if (regression.pred || regression.resid )
404     subcommand_save (&regression);
405  
406
407   for (k = 0; k < regression.n_dep_vars; k++)
408     linreg_free (regression.models[k]);
409   free (regression.models);
410   free (regression.vars);
411   free (regression.dep_vars);
412   return CMD_SUCCESS;
413   
414  error:
415   for (k = 0; k < regression.n_dep_vars; k++)
416     linreg_free (regression.models[k]);
417   free (regression.models);
418   free (regression.vars);
419   free (regression.dep_vars);
420   return CMD_FAILURE;
421 }
422
423
424 static size_t
425 get_n_all_vars (const struct regression *cmd)
426 {
427   size_t result = cmd->n_vars;
428   size_t i;
429   size_t j;
430
431   result += cmd->n_dep_vars;
432   for (i = 0; i < cmd->n_dep_vars; i++)
433     {
434       for (j = 0; j < cmd->n_vars; j++)
435         {
436           if (cmd->vars[j] == cmd->dep_vars[i])
437             {
438               result--;
439             }
440         }
441     }
442   return result;
443 }
444
445 static void
446 fill_all_vars (const struct variable **vars, const struct regression *cmd)
447 {
448   size_t i;
449   size_t j;
450   bool absent;
451   
452   for (i = 0; i < cmd->n_vars; i++)
453     {
454       vars[i] = cmd->vars[i];
455     }
456   for (i = 0; i < cmd->n_dep_vars; i++)
457     {
458       absent = true;
459       for (j = 0; j < cmd->n_vars; j++)
460         {
461           if (cmd->dep_vars[i] == cmd->vars[j])
462             {
463               absent = false;
464               break;
465             }
466         }
467       if (absent)
468         {
469           vars[i + cmd->n_vars] = cmd->dep_vars[i];
470         }
471     }
472 }
473
474 /*
475   Is variable k the dependent variable?
476 */
477 static bool
478 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
479 {
480   return v == cmd->vars[k];
481 }
482
483
484 /* Identify the explanatory variables in v_variables.  Returns
485    the number of independent variables. */
486 static int
487 identify_indep_vars (const struct regression *cmd, 
488                      const struct variable **indep_vars,
489                      const struct variable *depvar)
490 {
491   int n_indep_vars = 0;
492   int i;
493
494   for (i = 0; i < cmd->n_vars; i++)
495     if (!is_depvar (cmd, i, depvar))
496       indep_vars[n_indep_vars++] = cmd->vars[i];
497   if ((n_indep_vars < 1) && is_depvar (cmd, 0, depvar))
498     {
499       /*
500         There is only one independent variable, and it is the same
501         as the dependent variable. Print a warning and continue.
502       */
503       msg (SE,
504            gettext ("The dependent variable is equal to the independent variable." 
505                     "The least squares line is therefore Y=X." 
506                     "Standard errors and related statistics may be meaningless."));
507       n_indep_vars = 1;
508       indep_vars[0] = cmd->vars[0];
509     }
510   return n_indep_vars;
511 }
512
513
514 static double
515 fill_covariance (gsl_matrix *cov, struct covariance *all_cov, 
516                  const struct variable **vars,
517                  size_t n_vars, const struct variable *dep_var, 
518                  const struct variable **all_vars, size_t n_all_vars,
519                  double *means)
520 {
521   size_t i;
522   size_t j;
523   size_t dep_subscript;
524   size_t *rows;
525   const gsl_matrix *ssizes;
526   const gsl_matrix *mean_matrix;
527   const gsl_matrix *ssize_matrix;
528   double result = 0.0;
529   
530   gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
531
532   if ( cm == NULL)
533     return 0;
534
535   rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
536   
537   for (i = 0; i < n_all_vars; i++)
538     {
539       for (j = 0; j < n_vars; j++)
540         {
541           if (vars[j] == all_vars[i])
542             {
543               rows[j] = i;
544             }
545         }
546       if (all_vars[i] == dep_var)
547         {
548           dep_subscript = i;
549         }
550     }
551   mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
552   ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
553   for (i = 0; i < cov->size1 - 1; i++)
554     {
555       means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
556         / gsl_matrix_get (ssize_matrix, rows[i], 0);
557       for (j = 0; j < cov->size2 - 1; j++)
558         {
559           gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
560           gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
561         }
562     }
563   means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
564     / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
565   ssizes = covariance_moments (all_cov, MOMENT_NONE);
566   result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
567   for (i = 0; i < cov->size1 - 1; i++)
568     {
569       gsl_matrix_set (cov, i, cov->size1 - 1, 
570                       gsl_matrix_get (cm, rows[i], dep_subscript));
571       gsl_matrix_set (cov, cov->size1 - 1, i, 
572                       gsl_matrix_get (cm, rows[i], dep_subscript));
573       if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
574         {
575           result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
576         }
577     }
578   gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1, 
579                   gsl_matrix_get (cm, dep_subscript, dep_subscript));
580   free (rows);
581   gsl_matrix_free (cm);
582   return result;
583 }
584
585
586 /*
587   STATISTICS subcommand output functions.
588 */
589 static void reg_stats_r (linreg *, void *);
590 static void reg_stats_coeff (linreg *, void *);
591 static void reg_stats_anova (linreg *, void *);
592 static void reg_stats_bcov (linreg *, void *);
593
594 static void statistics_keyword_output (void (*)(linreg *, void *),
595                                        bool, linreg *, void *);
596
597
598
599 static void
600 subcommand_statistics (const struct regression *cmd , linreg * c, void *aux)
601 {
602   statistics_keyword_output (reg_stats_r, cmd->r, c, aux);
603   statistics_keyword_output (reg_stats_anova, cmd->anova, c, aux);
604   statistics_keyword_output (reg_stats_coeff, cmd->coeff, c, aux);
605   statistics_keyword_output (reg_stats_bcov, cmd->bcov, c, aux);
606 }
607
608
609 static void
610 run_regression (const struct regression *cmd, struct casereader *input)
611 {
612   size_t i;
613   int n_indep = 0;
614   int k;
615   double *means;
616   struct ccase *c;
617   struct covariance *cov;
618   const struct variable **vars;
619   const struct variable **all_vars;
620   const struct variable *dep_var;
621   struct casereader *reader;
622   size_t n_all_vars;
623
624   linreg **models = cmd->models;
625
626   n_all_vars = get_n_all_vars (cmd);
627   all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
628   fill_all_vars (all_vars, cmd);
629   vars = xnmalloc (cmd->n_vars, sizeof (*vars));
630   means  = xnmalloc (n_all_vars, sizeof (*means));
631   cov = covariance_1pass_create (n_all_vars, all_vars,
632                                  dict_get_weight (dataset_dict (cmd->ds)), MV_ANY);
633
634   reader = casereader_clone (input);
635   reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
636                                              MV_ANY, NULL, NULL);
637
638
639   for (; (c = casereader_read (reader)) != NULL; case_unref (c))
640     {
641       covariance_accumulate (cov, c);
642     }
643
644   for (k = 0; k < cmd->n_dep_vars; k++)
645     {
646       double n_data;
647
648       gsl_matrix *this_cm;
649       dep_var = cmd->dep_vars[k];
650       n_indep = identify_indep_vars (cmd, vars, dep_var);
651       
652       this_cm = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
653       n_data = fill_covariance (this_cm, cov, vars, n_indep, 
654                                 dep_var, all_vars, n_all_vars, means);
655       models[k] = linreg_alloc (dep_var, (const struct variable **) vars,
656                                 n_data, n_indep);
657       models[k]->depvar = dep_var;
658       for (i = 0; i < n_indep; i++)
659         {
660           linreg_set_indep_variable_mean (models[k], i, means[i]);
661         }
662       linreg_set_depvar_mean (models[k], means[i]);
663       /*
664         For large data sets, use QR decomposition.
665       */
666       if (n_data > sqrt (n_indep) && n_data > REG_LARGE_DATA)
667         {
668           models[k]->method = LINREG_QR;
669         }
670
671       if (n_data > 0)
672         {
673           /*
674             Find the least-squares estimates and other statistics.
675           */
676           linreg_fit (this_cm, models[k]);
677           
678           if (!taint_has_tainted_successor (casereader_get_taint (input)))
679             {
680               subcommand_statistics (cmd, models[k], this_cm);
681             }
682         }
683       else
684         {
685           msg (SE,
686                _("No valid data found. This command was skipped."));
687           linreg_free (models[k]);
688           models[k] = NULL;
689         }
690       gsl_matrix_free (this_cm);
691     }
692   
693   casereader_destroy (reader);
694   free (vars);
695   free (all_vars);
696   free (means);
697   casereader_destroy (input);
698   covariance_destroy (cov);
699 }
700
701
702 \f
703
704
705 static void
706 reg_stats_r (linreg *c, void *aux UNUSED)
707 {
708   struct tab_table *t;
709   int n_rows = 2;
710   int n_cols = 5;
711   double rsq;
712   double adjrsq;
713   double std_error;
714
715   assert (c != NULL);
716   rsq = linreg_ssreg (c) / linreg_sst (c);
717   adjrsq = 1.0 - (1.0 - rsq) * (linreg_n_obs (c) - 1.0) / (linreg_n_obs (c) - linreg_n_coeffs (c));
718   std_error = sqrt (linreg_mse (c));
719   t = tab_create (n_cols, n_rows);
720   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
721   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
722   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
723   tab_vline (t, TAL_0, 1, 0, 0);
724
725   tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("R"));
726   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("R Square"));
727   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Adjusted R Square"));
728   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error of the Estimate"));
729   tab_double (t, 1, 1, TAB_RIGHT, sqrt (rsq), NULL);
730   tab_double (t, 2, 1, TAB_RIGHT, rsq, NULL);
731   tab_double (t, 3, 1, TAB_RIGHT, adjrsq, NULL);
732   tab_double (t, 4, 1, TAB_RIGHT, std_error, NULL);
733   tab_title (t, _("Model Summary"));
734   tab_submit (t);
735 }
736
737 /*
738   Table showing estimated regression coefficients.
739 */
740 static void
741 reg_stats_coeff (linreg * c, void *aux_)
742 {
743   size_t j;
744   int n_cols = 7;
745   int n_rows;
746   int this_row;
747   double t_stat;
748   double pval;
749   double std_err;
750   double beta;
751   const char *label;
752
753   const struct variable *v;
754   struct tab_table *t;
755   gsl_matrix *cov = aux_;
756
757   assert (c != NULL);
758   n_rows = linreg_n_coeffs (c) + 3;
759
760   t = tab_create (n_cols, n_rows);
761   tab_headers (t, 2, 0, 1, 0);
762   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
763   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
764   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
765   tab_vline (t, TAL_0, 1, 0, 0);
766
767   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("B"));
768   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
769   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Beta"));
770   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
771   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
772   tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("(Constant)"));
773   tab_double (t, 2, 1, 0, linreg_intercept (c), NULL);
774   std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
775   tab_double (t, 3, 1, 0, std_err, NULL);
776   tab_double (t, 4, 1, 0, 0.0, NULL);
777   t_stat = linreg_intercept (c) / std_err;
778   tab_double (t, 5, 1, 0, t_stat, NULL);
779   pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), (double) (linreg_n_obs (c) - linreg_n_coeffs (c)));
780   tab_double (t, 6, 1, 0, pval, NULL);
781   for (j = 0; j < linreg_n_coeffs (c); j++)
782     {
783       struct string tstr;
784       ds_init_empty (&tstr);
785       this_row = j + 2;
786
787       v = linreg_indep_var (c, j);
788       label = var_to_string (v);
789       /* Do not overwrite the variable's name. */
790       ds_put_cstr (&tstr, label);
791       tab_text (t, 1, this_row, TAB_CENTER, ds_cstr (&tstr));
792       /*
793         Regression coefficients.
794       */
795       tab_double (t, 2, this_row, 0, linreg_coeff (c, j), NULL);
796       /*
797         Standard error of the coefficients.
798       */
799       std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
800       tab_double (t, 3, this_row, 0, std_err, NULL);
801       /*
802         Standardized coefficient, i.e., regression coefficient
803         if all variables had unit variance.
804       */
805       beta = sqrt (gsl_matrix_get (cov, j, j));
806       beta *= linreg_coeff (c, j) / 
807         sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1));
808       tab_double (t, 4, this_row, 0, beta, NULL);
809
810       /*
811         Test statistic for H0: coefficient is 0.
812       */
813       t_stat = linreg_coeff (c, j) / std_err;
814       tab_double (t, 5, this_row, 0, t_stat, NULL);
815       /*
816         P values for the test statistic above.
817       */
818       pval =
819         2 * gsl_cdf_tdist_Q (fabs (t_stat),
820                              (double) (linreg_n_obs (c) - linreg_n_coeffs (c)));
821       tab_double (t, 6, this_row, 0, pval, NULL);
822       ds_destroy (&tstr);
823     }
824   tab_title (t, _("Coefficients"));
825   tab_submit (t);
826 }
827
828 /*
829   Display the ANOVA table.
830 */
831 static void
832 reg_stats_anova (linreg * c, void *aux UNUSED)
833 {
834   int n_cols = 7;
835   int n_rows = 4;
836   const double msm = linreg_ssreg (c) / linreg_dfmodel (c);
837   const double mse = linreg_mse (c);
838   const double F = msm / mse;
839   const double pval = gsl_cdf_fdist_Q (F, c->dfm, c->dfe);
840
841   struct tab_table *t;
842
843   assert (c != NULL);
844   t = tab_create (n_cols, n_rows);
845   tab_headers (t, 2, 0, 1, 0);
846
847   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
848
849   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
850   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
851   tab_vline (t, TAL_0, 1, 0, 0);
852
853   tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
854   tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
855   tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
856   tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
857   tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
858
859   tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Regression"));
860   tab_text (t, 1, 2, TAB_LEFT | TAT_TITLE, _("Residual"));
861   tab_text (t, 1, 3, TAB_LEFT | TAT_TITLE, _("Total"));
862
863   /* Sums of Squares */
864   tab_double (t, 2, 1, 0, linreg_ssreg (c), NULL);
865   tab_double (t, 2, 3, 0, linreg_sst (c), NULL);
866   tab_double (t, 2, 2, 0, linreg_sse (c), NULL);
867
868
869   /* Degrees of freedom */
870   tab_text_format (t, 3, 1, TAB_RIGHT, "%g", c->dfm);
871   tab_text_format (t, 3, 2, TAB_RIGHT, "%g", c->dfe);
872   tab_text_format (t, 3, 3, TAB_RIGHT, "%g", c->dft);
873
874   /* Mean Squares */
875   tab_double (t, 4, 1, TAB_RIGHT, msm, NULL);
876   tab_double (t, 4, 2, TAB_RIGHT, mse, NULL);
877
878   tab_double (t, 5, 1, 0, F, NULL);
879
880   tab_double (t, 6, 1, 0, pval, NULL);
881
882   tab_title (t, _("ANOVA"));
883   tab_submit (t);
884 }
885
886
887 static void
888 reg_stats_bcov (linreg * c, void *aux UNUSED)
889 {
890   int n_cols;
891   int n_rows;
892   int i;
893   int k;
894   int row;
895   int col;
896   const char *label;
897   struct tab_table *t;
898
899   assert (c != NULL);
900   n_cols = c->n_indeps + 1 + 2;
901   n_rows = 2 * (c->n_indeps + 1);
902   t = tab_create (n_cols, n_rows);
903   tab_headers (t, 2, 0, 1, 0);
904   tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
905   tab_hline (t, TAL_2, 0, n_cols - 1, 1);
906   tab_vline (t, TAL_2, 2, 0, n_rows - 1);
907   tab_vline (t, TAL_0, 1, 0, 0);
908   tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Model"));
909   tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Covariances"));
910   for (i = 0; i < linreg_n_coeffs (c); i++)
911     {
912       const struct variable *v = linreg_indep_var (c, i);
913       label = var_to_string (v);
914       tab_text (t, 2, i, TAB_CENTER, label);
915       tab_text (t, i + 2, 0, TAB_CENTER, label);
916       for (k = 1; k < linreg_n_coeffs (c); k++)
917         {
918           col = (i <= k) ? k : i;
919           row = (i <= k) ? i : k;
920           tab_double (t, k + 2, i, TAB_CENTER,
921                       gsl_matrix_get (c->cov, row, col), NULL);
922         }
923     }
924   tab_title (t, _("Coefficient Correlations"));
925   tab_submit (t);
926 }
927
928 static void
929 statistics_keyword_output (void (*function) (linreg *, void *),
930                            bool keyword, linreg * c, void *aux)
931 {
932   if (keyword)
933     {
934       (*function) (c, aux);
935     }
936 }