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.
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.
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.
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/>. */
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_cdf.h>
25 #include <gsl/gsl_matrix.h>
27 #include <data/dataset.h>
28 #include <data/casewriter.h>
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"
36 #include "data/casegrouper.h"
37 #include "data/casereader.h"
38 #include "data/dictionary.h"
40 #include "math/covariance.h"
41 #include "math/linreg.h"
42 #include "math/moments.h"
44 #include "libpspp/message.h"
45 #include "libpspp/taint.h"
47 #include "output/pivot-table.h"
49 #include "gl/intprops.h"
50 #include "gl/minmax.h"
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) msgid
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)
64 #define STATS_DEFAULT (STATS_R | STATS_COEFF | STATS_ANOVA | STATS_OUTS)
71 const struct variable **vars;
74 const struct variable **dep_vars;
86 struct regression_workspace
88 /* The new variables which will be introduced by /SAVE */
89 const struct variable **predvars;
90 const struct variable **residvars;
92 /* A reader/writer pair to temporarily hold the
93 values of the new variables */
94 struct casewriter *writer;
95 struct casereader *reader;
97 /* Indeces of the new values in the reader/writer (-1 if not applicable) */
101 /* 0, 1 or 2 depending on what new variables are to be created */
105 static void run_regression (const struct regression *cmd,
106 struct regression_workspace *ws,
107 struct casereader *input);
110 /* Return a string based on PREFIX which may be used as the name
111 of a new variable in DICT */
113 reg_get_name (const struct dictionary *dict, const char *prefix)
115 for (size_t i = 1; ; i++)
117 char *name = xasprintf ("%s%zu", prefix, i);
118 if (!dict_lookup_var (dict, name))
124 static const struct variable *
125 create_aux_var (struct dataset *ds, const char *prefix)
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);
134 /* Auxiliary data for transformation when /SAVE is entered */
135 struct save_trans_data
138 struct regression_workspace *ws;
142 save_trans_free (void *aux)
144 struct save_trans_data *save_trans_data = aux;
145 free (save_trans_data->ws->predvars);
146 free (save_trans_data->ws->residvars);
148 casereader_destroy (save_trans_data->ws->reader);
149 free (save_trans_data->ws);
150 free (save_trans_data);
154 static enum trns_result
155 save_trans_func (void *aux, struct ccase **c, casenumber x UNUSED)
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);
163 *c = case_unshare (*c);
165 for (size_t k = 0; k < save_trans_data->n_dep_vars; ++k)
167 if (ws->pred_idx != -1)
169 double pred = case_num_idx (in, ws->extras * k + ws->pred_idx);
170 *case_num_rw (*c, ws->predvars[k]) = pred;
173 if (ws->res_idx != -1)
175 double resid = case_num_idx (in, ws->extras * k + ws->res_idx);
176 *case_num_rw (*c, ws->residvars[k]) = resid;
182 return TRNS_CONTINUE;
186 cmd_regression (struct lexer *lexer, struct dataset *ds)
188 const struct dictionary *dict = dataset_dict (ds);
190 struct regression regression = {
192 .stats = STATS_DEFAULT,
199 bool variables_seen = false;
200 bool method_seen = false;
201 bool dependent_seen = false;
204 while (lex_token (lexer) != T_ENDCMD)
206 lex_match (lexer, T_SLASH);
208 if (lex_match_id (lexer, "VARIABLES"))
212 lex_next_error (lexer, -1, -1,
213 _("VARIABLES may not appear after %s"), "METHOD");
218 lex_next_error (lexer, -1, -1,
219 _("VARIABLES may not appear after %s"), "DEPENDENT");
222 variables_seen = true;
223 lex_match (lexer, T_EQUALS);
225 if (!parse_variables_const (lexer, dict,
226 ®ression.vars, ®ression.n_vars,
227 PV_NO_DUPLICATE | PV_NUMERIC))
230 else if (lex_match_id (lexer, "DEPENDENT"))
232 dependent_seen = true;
233 lex_match (lexer, T_EQUALS);
235 free (regression.dep_vars);
236 regression.n_dep_vars = 0;
238 if (!parse_variables_const (lexer, dict,
239 ®ression.dep_vars,
240 ®ression.n_dep_vars,
241 PV_NO_DUPLICATE | PV_NUMERIC))
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"))
251 lex_match (lexer, T_EQUALS);
253 if (!lex_force_match_id (lexer, "ENTER"))
258 if (!parse_variables_const (lexer, dict,
259 ®ression.vars, ®ression.n_vars,
260 PV_NO_DUPLICATE | PV_NUMERIC))
264 else if (lex_match_id (lexer, "STATISTICS"))
266 unsigned long statistics = 0;
267 lex_match (lexer, T_EQUALS);
269 while (lex_token (lexer) != T_ENDCMD
270 && lex_token (lexer) != T_SLASH)
272 if (lex_match (lexer, T_ALL))
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"))
288 statistics |= STATS_CI;
290 if (lex_match (lexer, T_LPAREN))
292 if (!lex_force_num (lexer))
294 regression.ci = lex_number (lexer) / 100.0;
297 if (!lex_force_match (lexer, T_RPAREN))
303 lex_error_expecting (lexer, "ALL", "DEFAULTS", "R", "COEFF",
304 "ANOVA", "BCOV", "TOL", "CI");
310 regression.stats = statistics;
312 else if (lex_match_id (lexer, "SAVE"))
314 save_start = lex_ofs (lexer) - 1;
315 lex_match (lexer, T_EQUALS);
317 while (lex_token (lexer) != T_ENDCMD
318 && lex_token (lexer) != T_SLASH)
320 if (lex_match_id (lexer, "PRED"))
321 regression.pred = true;
322 else if (lex_match_id (lexer, "RESID"))
323 regression.resid = true;
326 lex_error_expecting (lexer, "PRED", "RESID");
330 save_end = lex_ofs (lexer) - 1;
334 lex_error_expecting (lexer, "VARIABLES", "DEPENDENT", "ORIGIN",
335 "NOORIGIN", "METHOD", "STATISTICS", "SAVE");
340 if (!regression.vars)
341 dict_get_vars (dict, ®ression.vars, ®ression.n_vars, 0);
343 struct regression_workspace workspace = {
348 bool save = regression.pred || regression.resid;
351 struct caseproto *proto = caseproto_create ();
353 if (regression.resid)
355 workspace.res_idx = workspace.extras ++;
356 workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
358 for (size_t i = 0; i < regression.n_dep_vars; ++i)
360 workspace.residvars[i] = create_aux_var (ds, "RES");
361 proto = caseproto_add_width (proto, 0);
367 workspace.pred_idx = workspace.extras ++;
368 workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
370 for (size_t i = 0; i < regression.n_dep_vars; ++i)
372 workspace.predvars[i] = create_aux_var (ds, "PRED");
373 proto = caseproto_add_width (proto, 0);
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."));
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."));
387 workspace.writer = autopaging_writer_create (proto);
388 caseproto_unref (proto);
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))
396 run_regression (®ression,
401 bool ok = casegrouper_destroy (grouper);
402 ok = proc_commit (ds) && ok;
404 if (workspace.writer)
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;
414 static const struct trns_class trns_class = {
415 .name = "REGRESSION",
416 .execute = save_trans_func,
417 .destroy = save_trans_free,
419 add_transformation (ds, &trns_class, save_trans_data);
422 free (regression.vars);
423 free (regression.dep_vars);
424 return ok ? CMD_SUCCESS : CMD_FAILURE;
427 free (regression.vars);
428 free (regression.dep_vars);
432 /* Return the size of the union of dependent and independent variables */
434 get_n_all_vars (const struct regression *cmd)
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])
444 /* Fill VARS with the union of dependent and independent variables */
446 fill_all_vars (const struct variable **vars, const struct regression *cmd)
448 for (size_t i = 0; i < cmd->n_vars; i++)
449 vars[i] = cmd->vars[i];
452 for (size_t i = 0; i < cmd->n_dep_vars; i++)
455 for (size_t j = 0; j < cmd->n_vars; j++)
456 if (cmd->dep_vars[i] == cmd->vars[j])
462 vars[cmd->n_vars + x++] = cmd->dep_vars[i];
467 /* Fill the array VARS, with all the predictor variables from CMD, except
470 fill_predictor_x (const struct variable **vars, const struct variable *x, const struct regression *cmd)
473 for (size_t i = 0; i < cmd->n_vars; i++)
474 if (cmd->vars[i] != x)
475 vars[n++] = cmd->vars[i];
479 Is variable k the dependent variable?
482 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
484 return v == cmd->vars[k];
487 /* Identify the explanatory variables in v_variables. Returns
488 the number of independent variables. */
490 identify_indep_vars (const struct regression *cmd,
491 const struct variable **indep_vars,
492 const struct variable *depvar)
494 int n_indep_vars = 0;
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))
502 There is only one independent variable, and it is the same
503 as the dependent variable. Print a warning and continue.
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."));
510 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,
522 const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
526 size_t *rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
528 size_t dep_subscript = SIZE_MAX;
529 for (size_t i = 0; i < n_all_vars; i++)
531 for (size_t j = 0; j < n_vars; j++)
532 if (vars[j] == all_vars[i])
534 if (all_vars[i] == dep_var)
537 assert (dep_subscript != SIZE_MAX);
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++)
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++)
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]));
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++)
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);
564 gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
565 gsl_matrix_get (cm, dep_subscript, dep_subscript));
572 struct model_container
574 struct linreg **models;
578 STATISTICS subcommand output functions.
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 *);
588 static struct linreg **
589 run_regression_get_models (const struct regression *cmd,
590 struct casereader *input,
593 struct model_container *model_container = XCALLOC (cmd->n_vars, struct model_container);
596 struct covariance *cov;
597 struct casereader *reader;
599 if (cmd->stats & STATS_TOL)
600 for (size_t i = 0; i < cmd->n_vars; i++)
602 struct regression subreg = {
603 .origin = cmd->origin,
605 .n_vars = cmd->n_vars - 1,
607 .vars = xmalloc ((cmd->n_vars - 1) * sizeof *subreg.vars),
608 .dep_vars = &cmd->vars[i],
614 fill_predictor_x (subreg.vars, cmd->vars[i], cmd);
616 model_container[i].models =
617 run_regression_get_models (&subreg, input, false);
621 size_t n_all_vars = get_n_all_vars (cmd);
622 const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
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.
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);
635 reader = casereader_clone (input);
636 reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
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);
644 struct linreg **models = XCALLOC (cmd->n_dep_vars, struct linreg*);
645 for (size_t k = 0; k < cmd->n_dep_vars; k++)
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]);
659 linreg_fit (cov_matrix, models[k]);
662 && !taint_has_tainted_successor (casereader_get_taint (input)))
665 Find the least-squares estimates and other statistics.
667 if (cmd->stats & STATS_R)
668 reg_stats_r (models[k], dep_var);
670 if (cmd->stats & STATS_ANOVA)
671 reg_stats_anova (models[k], dep_var);
673 if (cmd->stats & STATS_COEFF)
674 reg_stats_coeff (cmd, models[k],
676 cov_matrix, dep_var);
678 if (cmd->stats & STATS_BCOV)
679 reg_stats_bcov (models[k], dep_var);
683 msg (SE, _("No valid data found. This command was skipped."));
685 gsl_matrix_free (cov_matrix);
688 casereader_destroy (reader);
690 for (size_t i = 0; i < cmd->n_vars; i++)
692 if (model_container[i].models)
693 linreg_unref (model_container[i].models[0]);
694 free (model_container[i].models);
696 free (model_container);
700 covariance_destroy (cov);
705 run_regression (const struct regression *cmd,
706 struct regression_workspace *ws,
707 struct casereader *input)
709 struct linreg **models = run_regression_get_models (cmd, input, true);
714 struct casereader *r = casereader_clone (input);
716 for (; (c = casereader_read (r)) != NULL; case_unref (c))
718 struct ccase *outc = case_create (casewriter_get_proto (ws->writer));
719 for (int k = 0; k < cmd->n_dep_vars; k++)
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++)
727 const union value *tmp = case_data (c, vars[i]);
733 double pred = linreg_predict (models[k], vals, n_indep);
734 *case_num_rw_idx (outc, k * ws->extras + ws->pred_idx) = pred;
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;
746 casewriter_write (ws->writer, outc);
748 casereader_destroy (r);
751 for (size_t k = 0; k < cmd->n_dep_vars; k++)
752 linreg_unref (models[k]);
755 casereader_destroy (input);
762 reg_stats_r (const struct linreg * c, const struct variable *var)
764 struct pivot_table *table = pivot_table_create__ (
765 pivot_value_new_text_format (N_("Model Summary (%s)"),
766 var_to_string (var)),
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"));
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));
780 sqrt (rsq), rsq, adjrsq, std_error
782 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
783 pivot_table_put1 (table, i, pivot_value_new_number (entries[i]));
785 pivot_table_submit (table);
789 Table showing estimated regression coefficients.
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)
796 struct pivot_table *table = pivot_table_create__ (
797 pivot_value_new_text_format (N_("Coefficients (%s)"), var_to_string (var)),
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)
811 struct pivot_category *interval = pivot_category_create_group__ (
812 statistics->root, pivot_value_new_text_format (
813 N_("%g%% Confidence Interval for B"),
815 pivot_category_create_leaves (interval, N_("Lower Bound"),
819 if (cmd->stats & STATS_TOL)
820 pivot_category_create_group (statistics->root,
821 N_("Collinearity Statistics"),
822 N_("Tolerance"), N_("VIF"));
825 struct pivot_dimension *variables = pivot_dimension_create (
826 table, PIVOT_AXIS_ROW, N_("Variables"));
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);
834 int var_idx = pivot_category_create_leaf (
835 variables->root, pivot_value_new_text (N_("(Constant)")));
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),
844 2.0 * gsl_cdf_tdist_Q (fabs (t_stat),
845 linreg_n_obs (c) - linreg_n_coeffs (c)),
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]));
853 if (cmd->stats & STATS_CI)
855 double interval_entries[] = {
856 linreg_intercept (c) - tval * std_err,
857 linreg_intercept (c) + tval * std_err,
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]));
866 for (size_t j = 0; j < linreg_n_coeffs (c); j++)
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));
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[] = {
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))),
880 2 * gsl_cdf_tdist_Q (fabs (t_stat), df)
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]));
888 if (cmd->stats & STATS_CI)
890 double interval_entries[] = {
891 linreg_coeff (c, j) - tval * std_err,
892 linreg_coeff (c, j) + tval * std_err,
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]));
901 if (cmd->stats & STATS_TOL)
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)));
912 pivot_table_submit (table);
916 Display the ANOVA table.
919 reg_stats_anova (const struct linreg * c, const struct variable *var)
921 struct pivot_table *table = pivot_table_create__ (
922 pivot_value_new_text_format (N_("ANOVA (%s)"), var_to_string (var)),
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);
932 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Source"),
933 N_("Regression"), N_("Residual"), N_("Total"));
935 double msm = linreg_ssreg (c) / linreg_dfmodel (c);
936 double mse = linreg_mse (c);
937 double F = msm / mse;
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) },
960 { 4, 0, gsl_cdf_fdist_Q (F, linreg_dfmodel (c), linreg_dferror (c)) },
962 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
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));
969 pivot_table_submit (table);
974 reg_stats_bcov (const struct linreg * c, const struct variable *var)
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");
981 for (size_t i = 0; i < 2; i++)
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)));
991 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Statistics"),
994 for (size_t i = 0; i < linreg_n_coeffs (c); i++)
995 for (size_t k = 0; k < linreg_n_coeffs (c); k++)
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));
1001 pivot_table_submit (table);