1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2009, 2010, 2011, 2012, 2013, 2014 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/>. */
22 #include <gsl/gsl_cdf.h>
23 #include <gsl/gsl_matrix.h>
25 #include <data/dataset.h>
26 #include <data/casewriter.h>
28 #include "language/command.h"
29 #include "language/lexer/lexer.h"
30 #include "language/lexer/value-parser.h"
31 #include "language/lexer/variable-parser.h"
34 #include "data/casegrouper.h"
35 #include "data/casereader.h"
36 #include "data/dictionary.h"
38 #include "math/covariance.h"
39 #include "math/linreg.h"
40 #include "math/moments.h"
42 #include "libpspp/message.h"
43 #include "libpspp/taint.h"
45 #include "output/tab.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
52 #include <gl/intprops.h>
54 #define REG_LARGE_DATA 1000
63 #define STATS_DEFAULT (STATS_R | STATS_COEFF | STATS_ANOVA | STATS_OUTS)
71 const struct variable **vars;
74 const struct variable **dep_vars;
84 struct regression_workspace
86 /* The new variables which will be introduced by /SAVE */
87 const struct variable **predvars;
88 const struct variable **residvars;
90 /* A reader/writer pair to temporarily hold the
91 values of the new variables */
92 struct casewriter *writer;
93 struct casereader *reader;
95 /* Indeces of the new values in the reader/writer (-1 if not applicable) */
99 /* 0, 1 or 2 depending on what new variables are to be created */
103 static void run_regression (const struct regression *cmd,
104 struct regression_workspace *ws,
105 struct casereader *input);
108 /* Return a string based on PREFIX which may be used as the name
109 of a new variable in DICT */
111 reg_get_name (const struct dictionary *dict, const char *prefix)
116 /* XXX handle too-long prefixes */
117 name = xmalloc (strlen (prefix) + INT_BUFSIZE_BOUND (i) + 1);
120 sprintf (name, "%s%d", prefix, i);
121 if (dict_lookup_var (dict, name) == NULL)
127 static const struct variable *
128 create_aux_var (struct dataset *ds, const char *prefix)
130 struct variable *var;
131 struct dictionary *dict = dataset_dict (ds);
132 char *name = reg_get_name (dict, prefix);
133 var = dict_create_var_assert (dict, name, 0);
138 /* Auxilliary data for transformation when /SAVE is entered */
139 struct save_trans_data
142 struct regression_workspace *ws;
146 save_trans_free (void *aux)
148 struct save_trans_data *save_trans_data = aux;
149 free (save_trans_data->ws->predvars);
150 free (save_trans_data->ws->residvars);
152 casereader_destroy (save_trans_data->ws->reader);
153 free (save_trans_data->ws);
154 free (save_trans_data);
159 save_trans_func (void *aux, struct ccase **c, casenumber x UNUSED)
161 struct save_trans_data *save_trans_data = aux;
162 struct regression_workspace *ws = save_trans_data->ws;
163 struct ccase *in = casereader_read (ws->reader);
168 *c = case_unshare (*c);
170 for (k = 0; k < save_trans_data->n_dep_vars; ++k)
172 if (ws->pred_idx != -1)
174 double pred = case_data_idx (in, ws->extras * k + ws->pred_idx)->f;
175 case_data_rw (*c, ws->predvars[k])->f = pred;
178 if (ws->res_idx != -1)
180 double resid = case_data_idx (in, ws->extras * k + ws->res_idx)->f;
181 case_data_rw (*c, ws->residvars[k])->f = resid;
187 return TRNS_CONTINUE;
192 cmd_regression (struct lexer *lexer, struct dataset *ds)
194 struct regression_workspace workspace;
195 struct regression regression;
196 const struct dictionary *dict = dataset_dict (ds);
199 memset (®ression, 0, sizeof (struct regression));
201 regression.ci = 0.95;
202 regression.stats = STATS_DEFAULT;
203 regression.pred = false;
204 regression.resid = false;
208 /* Accept an optional, completely pointless "/VARIABLES=" */
209 lex_match (lexer, T_SLASH);
210 if (lex_match_id (lexer, "VARIABLES"))
212 if (!lex_force_match (lexer, T_EQUALS))
216 if (!parse_variables_const (lexer, dict,
217 ®ression.vars, ®ression.n_vars,
218 PV_NO_DUPLICATE | PV_NUMERIC))
222 while (lex_token (lexer) != T_ENDCMD)
224 lex_match (lexer, T_SLASH);
226 if (lex_match_id (lexer, "DEPENDENT"))
228 if (!lex_force_match (lexer, T_EQUALS))
231 free (regression.dep_vars);
232 regression.n_dep_vars = 0;
234 if (!parse_variables_const (lexer, dict,
235 ®ression.dep_vars,
236 ®ression.n_dep_vars,
237 PV_NO_DUPLICATE | PV_NUMERIC))
240 else if (lex_match_id (lexer, "METHOD"))
242 lex_match (lexer, T_EQUALS);
244 if (!lex_force_match_id (lexer, "ENTER"))
249 else if (lex_match_id (lexer, "STATISTICS"))
251 lex_match (lexer, T_EQUALS);
253 while (lex_token (lexer) != T_ENDCMD
254 && lex_token (lexer) != T_SLASH)
256 if (lex_match (lexer, T_ALL))
258 regression.stats = ~0;
260 else if (lex_match_id (lexer, "DEFAULTS"))
262 regression.stats |= STATS_DEFAULT;
264 else if (lex_match_id (lexer, "R"))
266 regression.stats |= STATS_R;
268 else if (lex_match_id (lexer, "COEFF"))
270 regression.stats |= STATS_COEFF;
272 else if (lex_match_id (lexer, "ANOVA"))
274 regression.stats |= STATS_ANOVA;
276 else if (lex_match_id (lexer, "BCOV"))
278 regression.stats |= STATS_BCOV;
280 else if (lex_match_id (lexer, "CI"))
282 regression.stats |= STATS_CI;
284 if (lex_match (lexer, T_LPAREN))
286 regression.ci = lex_number (lexer) / 100.0;
288 lex_force_match (lexer, T_RPAREN);
293 lex_error (lexer, NULL);
298 else if (lex_match_id (lexer, "SAVE"))
300 lex_match (lexer, T_EQUALS);
302 while (lex_token (lexer) != T_ENDCMD
303 && lex_token (lexer) != T_SLASH)
305 if (lex_match_id (lexer, "PRED"))
307 regression.pred = true;
309 else if (lex_match_id (lexer, "RESID"))
311 regression.resid = true;
315 lex_error (lexer, NULL);
322 lex_error (lexer, NULL);
327 if (!regression.vars)
329 dict_get_vars (dict, ®ression.vars, ®ression.n_vars, 0);
332 save = regression.pred || regression.resid;
333 workspace.extras = 0;
334 workspace.res_idx = -1;
335 workspace.pred_idx = -1;
336 workspace.writer = NULL;
337 workspace.reader = NULL;
341 struct caseproto *proto = caseproto_create ();
343 if (regression.resid)
346 workspace.res_idx = 0;
347 workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
349 for (i = 0; i < regression.n_dep_vars; ++i)
351 workspace.residvars[i] = create_aux_var (ds, "RES");
352 proto = caseproto_add_width (proto, 0);
359 workspace.pred_idx = 1;
360 workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
362 for (i = 0; i < regression.n_dep_vars; ++i)
364 workspace.predvars[i] = create_aux_var (ds, "PRED");
365 proto = caseproto_add_width (proto, 0);
369 if (proc_make_temporary_transformations_permanent (ds))
370 msg (SW, _("REGRESSION with SAVE ignores TEMPORARY. "
371 "Temporary transformations will be made permanent."));
373 workspace.writer = autopaging_writer_create (proto);
374 caseproto_unref (proto);
379 struct casegrouper *grouper;
380 struct casereader *group;
383 grouper = casegrouper_create_splits (proc_open_filtering (ds, !save), dict);
386 while (casegrouper_get_next_group (grouper, &group))
388 run_regression (®ression,
393 ok = casegrouper_destroy (grouper);
394 ok = proc_commit (ds) && ok;
397 if (workspace.writer)
399 struct save_trans_data *save_trans_data = xmalloc (sizeof *save_trans_data);
400 struct casereader *r = casewriter_make_reader (workspace.writer);
401 workspace.writer = NULL;
402 workspace.reader = r;
403 save_trans_data->ws = xmalloc (sizeof (workspace));
404 memcpy (save_trans_data->ws, &workspace, sizeof (workspace));
405 save_trans_data->n_dep_vars = regression.n_dep_vars;
407 add_transformation (ds, save_trans_func, save_trans_free, save_trans_data);
411 free (regression.vars);
412 free (regression.dep_vars);
417 free (regression.vars);
418 free (regression.dep_vars);
422 /* Return the size of the union of dependent and independent variables */
424 get_n_all_vars (const struct regression *cmd)
426 size_t result = cmd->n_vars;
430 result += cmd->n_dep_vars;
431 for (i = 0; i < cmd->n_dep_vars; i++)
433 for (j = 0; j < cmd->n_vars; j++)
435 if (cmd->vars[j] == cmd->dep_vars[i])
444 /* Fill VARS with the union of dependent and independent variables */
446 fill_all_vars (const struct variable **vars, const struct regression *cmd)
450 for (i = 0; i < cmd->n_vars; i++)
452 vars[i] = cmd->vars[i];
455 for (i = 0; i < cmd->n_dep_vars; i++)
459 for (j = 0; j < cmd->n_vars; j++)
461 if (cmd->dep_vars[i] == cmd->vars[j])
469 vars[cmd->n_vars + x++] = cmd->dep_vars[i];
475 Is variable k the dependent variable?
478 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
480 return v == cmd->vars[k];
484 /* Identify the explanatory variables in v_variables. Returns
485 the number of independent variables. */
487 identify_indep_vars (const struct regression *cmd,
488 const struct variable **indep_vars,
489 const struct variable *depvar)
491 int n_indep_vars = 0;
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))
500 There is only one independent variable, and it is the same
501 as the dependent variable. Print a warning and continue.
505 ("The dependent variable is equal to the independent variable. "
506 "The least squares line is therefore Y=X. "
507 "Standard errors and related statistics may be meaningless."));
509 indep_vars[0] = cmd->vars[0];
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,
524 size_t dep_subscript;
526 const gsl_matrix *ssizes;
527 const gsl_matrix *mean_matrix;
528 const gsl_matrix *ssize_matrix;
531 const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
536 rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
538 for (i = 0; i < n_all_vars; i++)
540 for (j = 0; j < n_vars; j++)
542 if (vars[j] == all_vars[i])
547 if (all_vars[i] == dep_var)
552 mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
553 ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
554 for (i = 0; i < cov->size1 - 1; i++)
556 means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
557 / gsl_matrix_get (ssize_matrix, rows[i], 0);
558 for (j = 0; j < cov->size2 - 1; j++)
560 gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
561 gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
564 means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
565 / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
566 ssizes = covariance_moments (all_cov, MOMENT_NONE);
567 result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
568 for (i = 0; i < cov->size1 - 1; i++)
570 gsl_matrix_set (cov, i, cov->size1 - 1,
571 gsl_matrix_get (cm, rows[i], dep_subscript));
572 gsl_matrix_set (cov, cov->size1 - 1, i,
573 gsl_matrix_get (cm, rows[i], dep_subscript));
574 if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
576 result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
579 gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
580 gsl_matrix_get (cm, dep_subscript, dep_subscript));
588 STATISTICS subcommand output functions.
590 static void reg_stats_r (const linreg *, const struct variable *);
591 static void reg_stats_coeff (const linreg *, const gsl_matrix *, const struct variable *, const struct regression *);
592 static void reg_stats_anova (const linreg *, const struct variable *);
593 static void reg_stats_bcov (const linreg *, const struct variable *);
597 subcommand_statistics (const struct regression *cmd, const linreg * c, const gsl_matrix * cm,
598 const struct variable *var)
600 if (cmd->stats & STATS_R)
601 reg_stats_r (c, var);
603 if (cmd->stats & STATS_ANOVA)
604 reg_stats_anova (c, var);
606 if (cmd->stats & STATS_COEFF)
607 reg_stats_coeff (c, cm, var, cmd);
609 if (cmd->stats & STATS_BCOV)
610 reg_stats_bcov (c, var);
615 run_regression (const struct regression *cmd,
616 struct regression_workspace *ws,
617 struct casereader *input)
624 struct covariance *cov;
625 struct casereader *reader;
626 size_t n_all_vars = get_n_all_vars (cmd);
627 const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
629 double *means = xnmalloc (n_all_vars, sizeof (*means));
631 fill_all_vars (all_vars, cmd);
632 cov = covariance_1pass_create (n_all_vars, all_vars,
633 dict_get_weight (dataset_dict (cmd->ds)),
636 reader = casereader_clone (input);
637 reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
642 struct casereader *r = casereader_clone (reader);
644 for (; (c = casereader_read (r)) != NULL; case_unref (c))
646 covariance_accumulate (cov, c);
648 casereader_destroy (r);
651 models = xcalloc (cmd->n_dep_vars, sizeof (*models));
652 for (k = 0; k < cmd->n_dep_vars; k++)
654 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
655 const struct variable *dep_var = cmd->dep_vars[k];
656 int n_indep = identify_indep_vars (cmd, vars, dep_var);
657 gsl_matrix *this_cm = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
658 double n_data = fill_covariance (this_cm, cov, vars, n_indep,
659 dep_var, all_vars, n_all_vars, means);
660 models[k] = linreg_alloc (dep_var, vars, n_data, n_indep);
661 models[k]->depvar = dep_var;
662 for (i = 0; i < n_indep; i++)
664 linreg_set_indep_variable_mean (models[k], i, means[i]);
666 linreg_set_depvar_mean (models[k], means[i]);
668 For large data sets, use QR decomposition.
670 if (n_data > sqrt (n_indep) && n_data > REG_LARGE_DATA)
672 models[k]->method = LINREG_QR;
678 Find the least-squares estimates and other statistics.
680 linreg_fit (this_cm, models[k]);
682 if (!taint_has_tainted_successor (casereader_get_taint (input)))
684 subcommand_statistics (cmd, models[k], this_cm, dep_var);
689 msg (SE, _("No valid data found. This command was skipped."));
691 gsl_matrix_free (this_cm);
698 struct casereader *r = casereader_clone (reader);
700 for (; (c = casereader_read (r)) != NULL; case_unref (c))
702 struct ccase *outc = case_clone (c);
703 for (k = 0; k < cmd->n_dep_vars; k++)
705 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
706 const struct variable *dep_var = cmd->dep_vars[k];
707 int n_indep = identify_indep_vars (cmd, vars, dep_var);
708 double *vals = xnmalloc (n_indep, sizeof (*vals));
709 for (i = 0; i < n_indep; i++)
711 const union value *tmp = case_data (c, vars[i]);
717 double pred = linreg_predict (models[k], vals, n_indep);
718 case_data_rw_idx (outc, k * ws->extras + ws->pred_idx)->f = pred;
723 double obs = case_data (c, models[k]->depvar)->f;
724 double res = linreg_residual (models[k], obs, vals, n_indep);
725 case_data_rw_idx (outc, k * ws->extras + ws->res_idx)->f = res;
730 casewriter_write (ws->writer, outc);
732 casereader_destroy (r);
735 casereader_destroy (reader);
737 for (k = 0; k < cmd->n_dep_vars; k++)
739 linreg_unref (models[k]);
745 casereader_destroy (input);
746 covariance_destroy (cov);
753 reg_stats_r (const linreg * c, const struct variable *var)
763 rsq = linreg_ssreg (c) / linreg_sst (c);
765 (1.0 - rsq) * linreg_n_coeffs (c) / (linreg_n_obs (c) -
766 linreg_n_coeffs (c) - 1);
767 std_error = sqrt (linreg_mse (c));
768 t = tab_create (n_cols, n_rows);
769 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
770 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
771 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
772 tab_vline (t, TAL_0, 1, 0, 0);
774 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("R"));
775 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("R Square"));
776 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Adjusted R Square"));
777 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error of the Estimate"));
778 tab_double (t, 1, 1, TAB_RIGHT, sqrt (rsq), NULL);
779 tab_double (t, 2, 1, TAB_RIGHT, rsq, NULL);
780 tab_double (t, 3, 1, TAB_RIGHT, adjrsq, NULL);
781 tab_double (t, 4, 1, TAB_RIGHT, std_error, NULL);
782 tab_title (t, _("Model Summary (%s)"), var_to_string (var));
787 Table showing estimated regression coefficients.
790 reg_stats_coeff (const linreg * c, const gsl_matrix *cov, const struct variable *var, const struct regression *cmd)
794 const int heading_rows = 2;
803 const struct variable *v;
806 const double df = linreg_n_obs (c) - linreg_n_coeffs (c) - 1;
807 double q = (1 - cmd->ci) / 2.0; /* 2-tailed test */
808 double tval = gsl_cdf_tdist_Qinv (q, df);
811 n_rows = linreg_n_coeffs (c) + heading_rows + 1;
813 if (cmd->stats & STATS_CI)
816 t = tab_create (n_cols, n_rows);
817 tab_headers (t, 2, 0, 1, 0);
818 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
819 tab_hline (t, TAL_2, 0, n_cols - 1, heading_rows);
820 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
821 tab_vline (t, TAL_0, 1, 0, 0);
824 tab_hline (t, TAL_1, 2, 4, 1);
825 tab_joint_text (t, 2, 0, 3, 0, TAB_CENTER | TAT_TITLE, _("Unstandardized Coefficients"));
826 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("B"));
827 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
828 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Standardized Coefficients"));
829 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Beta"));
830 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("t"));
831 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
832 tab_text (t, 1, heading_rows, TAB_LEFT | TAT_TITLE, _("(Constant)"));
833 tab_double (t, 2, heading_rows, 0, linreg_intercept (c), NULL);
834 std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
836 if (cmd->stats & STATS_CI)
838 double lower = linreg_intercept (c) - tval * std_err ;
839 double upper = linreg_intercept (c) + tval * std_err ;
840 tab_double (t, 7, heading_rows, 0, lower, NULL);
841 tab_double (t, 8, heading_rows, 0, upper, NULL);
843 tab_joint_text_format (t, 7, 0, 8, 0, TAB_CENTER | TAT_TITLE, _("%g%% Confidence Interval for B"), cmd->ci * 100);
844 tab_hline (t, TAL_1, 7, 8, 1);
845 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
846 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
848 tab_double (t, 3, heading_rows, 0, std_err, NULL);
849 tab_double (t, 4, heading_rows, 0, 0.0, NULL);
850 t_stat = linreg_intercept (c) / std_err;
851 tab_double (t, 5, heading_rows, 0, t_stat, NULL);
853 2 * gsl_cdf_tdist_Q (fabs (t_stat),
854 (double) (linreg_n_obs (c) - linreg_n_coeffs (c)));
855 tab_double (t, 6, heading_rows, 0, pval, NULL);
857 for (j = 0; j < linreg_n_coeffs (c); j++)
860 ds_init_empty (&tstr);
861 this_row = j + heading_rows + 1;
863 v = linreg_indep_var (c, j);
864 label = var_to_string (v);
865 /* Do not overwrite the variable's name. */
866 ds_put_cstr (&tstr, label);
867 tab_text (t, 1, this_row, TAB_LEFT, ds_cstr (&tstr));
869 Regression coefficients.
871 tab_double (t, 2, this_row, 0, linreg_coeff (c, j), NULL);
873 Standard error of the coefficients.
875 std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
876 tab_double (t, 3, this_row, 0, std_err, NULL);
878 Standardized coefficient, i.e., regression coefficient
879 if all variables had unit variance.
881 beta = sqrt (gsl_matrix_get (cov, j, j));
882 beta *= linreg_coeff (c, j) /
883 sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1));
884 tab_double (t, 4, this_row, 0, beta, NULL);
887 Test statistic for H0: coefficient is 0.
889 t_stat = linreg_coeff (c, j) / std_err;
890 tab_double (t, 5, this_row, 0, t_stat, NULL);
892 P values for the test statistic above.
894 pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), df);
895 tab_double (t, 6, this_row, 0, pval, NULL);
898 if (cmd->stats & STATS_CI)
900 double lower = linreg_coeff (c, j) - tval * std_err ;
901 double upper = linreg_coeff (c, j) + tval * std_err ;
903 tab_double (t, 7, this_row, 0, lower, NULL);
904 tab_double (t, 8, this_row, 0, upper, NULL);
907 tab_title (t, _("Coefficients (%s)"), var_to_string (var));
912 Display the ANOVA table.
915 reg_stats_anova (const linreg * c, const struct variable *var)
919 const double msm = linreg_ssreg (c) / linreg_dfmodel (c);
920 const double mse = linreg_mse (c);
921 const double F = msm / mse;
922 const double pval = gsl_cdf_fdist_Q (F, c->dfm, c->dfe);
927 t = tab_create (n_cols, n_rows);
928 tab_headers (t, 2, 0, 1, 0);
930 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
932 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
933 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
934 tab_vline (t, TAL_0, 1, 0, 0);
936 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
937 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
938 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
939 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
940 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
942 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Regression"));
943 tab_text (t, 1, 2, TAB_LEFT | TAT_TITLE, _("Residual"));
944 tab_text (t, 1, 3, TAB_LEFT | TAT_TITLE, _("Total"));
946 /* Sums of Squares */
947 tab_double (t, 2, 1, 0, linreg_ssreg (c), NULL);
948 tab_double (t, 2, 3, 0, linreg_sst (c), NULL);
949 tab_double (t, 2, 2, 0, linreg_sse (c), NULL);
952 /* Degrees of freedom */
953 tab_text_format (t, 3, 1, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dfm);
954 tab_text_format (t, 3, 2, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dfe);
955 tab_text_format (t, 3, 3, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dft);
958 tab_double (t, 4, 1, TAB_RIGHT, msm, NULL);
959 tab_double (t, 4, 2, TAB_RIGHT, mse, NULL);
961 tab_double (t, 5, 1, 0, F, NULL);
963 tab_double (t, 6, 1, 0, pval, NULL);
965 tab_title (t, _("ANOVA (%s)"), var_to_string (var));
971 reg_stats_bcov (const linreg * c, const struct variable *var)
983 n_cols = c->n_indeps + 1 + 2;
984 n_rows = 2 * (c->n_indeps + 1);
985 t = tab_create (n_cols, n_rows);
986 tab_headers (t, 2, 0, 1, 0);
987 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
988 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
989 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
990 tab_vline (t, TAL_0, 1, 0, 0);
991 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Model"));
992 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Covariances"));
993 for (i = 0; i < linreg_n_coeffs (c); i++)
995 const struct variable *v = linreg_indep_var (c, i);
996 label = var_to_string (v);
997 tab_text (t, 2, i, TAB_CENTER, label);
998 tab_text (t, i + 2, 0, TAB_CENTER, label);
999 for (k = 1; k < linreg_n_coeffs (c); k++)
1001 col = (i <= k) ? k : i;
1002 row = (i <= k) ? i : k;
1003 tab_double (t, k + 2, i, TAB_CENTER,
1004 gsl_matrix_get (c->cov, row, col), NULL);
1007 tab_title (t, _("Coefficient Correlations (%s)"), var_to_string (var));