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
65 #define STATS_DEFAULT (STATS_R | STATS_COEFF | STATS_ANOVA | STATS_OUTS)
73 const struct variable **vars;
76 const struct variable **dep_vars;
88 struct regression_workspace
90 /* The new variables which will be introduced by /SAVE */
91 const struct variable **predvars;
92 const struct variable **residvars;
94 /* A reader/writer pair to temporarily hold the
95 values of the new variables */
96 struct casewriter *writer;
97 struct casereader *reader;
99 /* Indeces of the new values in the reader/writer (-1 if not applicable) */
103 /* 0, 1 or 2 depending on what new variables are to be created */
107 static void run_regression (const struct regression *cmd,
108 struct regression_workspace *ws,
109 struct casereader *input);
112 /* Return a string based on PREFIX which may be used as the name
113 of a new variable in DICT */
115 reg_get_name (const struct dictionary *dict, const char *prefix)
120 /* XXX handle too-long prefixes */
121 name = xmalloc (strlen (prefix) + INT_BUFSIZE_BOUND (i) + 1);
124 sprintf (name, "%s%d", prefix, i);
125 if (dict_lookup_var (dict, name) == NULL)
131 static const struct variable *
132 create_aux_var (struct dataset *ds, const char *prefix)
134 struct variable *var;
135 struct dictionary *dict = dataset_dict (ds);
136 char *name = reg_get_name (dict, prefix);
137 var = dict_create_var_assert (dict, name, 0);
142 /* Auxiliary data for transformation when /SAVE is entered */
143 struct save_trans_data
146 struct regression_workspace *ws;
150 save_trans_free (void *aux)
152 struct save_trans_data *save_trans_data = aux;
153 free (save_trans_data->ws->predvars);
154 free (save_trans_data->ws->residvars);
156 casereader_destroy (save_trans_data->ws->reader);
157 free (save_trans_data->ws);
158 free (save_trans_data);
163 save_trans_func (void *aux, struct ccase **c, casenumber x UNUSED)
165 struct save_trans_data *save_trans_data = aux;
166 struct regression_workspace *ws = save_trans_data->ws;
167 struct ccase *in = casereader_read (ws->reader);
172 *c = case_unshare (*c);
174 for (k = 0; k < save_trans_data->n_dep_vars; ++k)
176 if (ws->pred_idx != -1)
178 double pred = case_num_idx (in, ws->extras * k + ws->pred_idx);
179 *case_num_rw (*c, ws->predvars[k]) = pred;
182 if (ws->res_idx != -1)
184 double resid = case_num_idx (in, ws->extras * k + ws->res_idx);
185 *case_num_rw (*c, ws->residvars[k]) = resid;
191 return TRNS_CONTINUE;
196 cmd_regression (struct lexer *lexer, struct dataset *ds)
198 struct regression_workspace workspace;
199 struct regression regression;
200 const struct dictionary *dict = dataset_dict (ds);
203 memset (®ression, 0, sizeof (struct regression));
205 regression.ci = 0.95;
206 regression.stats = STATS_DEFAULT;
207 regression.pred = false;
208 regression.resid = false;
211 regression.origin = false;
213 bool variables_seen = false;
214 bool method_seen = false;
215 bool dependent_seen = false;
216 while (lex_token (lexer) != T_ENDCMD)
218 lex_match (lexer, T_SLASH);
220 if (lex_match_id (lexer, "VARIABLES"))
224 msg (SE, _("VARIABLES may not appear after %s"), "METHOD");
229 msg (SE, _("VARIABLES may not appear after %s"), "DEPENDENT");
232 variables_seen = true;
233 lex_match (lexer, T_EQUALS);
235 if (!parse_variables_const (lexer, dict,
236 ®ression.vars, ®ression.n_vars,
237 PV_NO_DUPLICATE | PV_NUMERIC))
240 else if (lex_match_id (lexer, "DEPENDENT"))
242 dependent_seen = true;
243 lex_match (lexer, T_EQUALS);
245 free (regression.dep_vars);
246 regression.n_dep_vars = 0;
248 if (!parse_variables_const (lexer, dict,
249 ®ression.dep_vars,
250 ®ression.n_dep_vars,
251 PV_NO_DUPLICATE | PV_NUMERIC))
254 else if (lex_match_id (lexer, "ORIGIN"))
256 regression.origin = true;
258 else if (lex_match_id (lexer, "NOORIGIN"))
260 regression.origin = false;
262 else if (lex_match_id (lexer, "METHOD"))
265 lex_match (lexer, T_EQUALS);
267 if (!lex_force_match_id (lexer, "ENTER"))
272 if (! variables_seen)
274 if (!parse_variables_const (lexer, dict,
275 ®ression.vars, ®ression.n_vars,
276 PV_NO_DUPLICATE | PV_NUMERIC))
280 else if (lex_match_id (lexer, "STATISTICS"))
282 unsigned long statistics = 0;
283 lex_match (lexer, T_EQUALS);
285 while (lex_token (lexer) != T_ENDCMD
286 && lex_token (lexer) != T_SLASH)
288 if (lex_match (lexer, T_ALL))
292 else if (lex_match_id (lexer, "DEFAULTS"))
294 statistics |= STATS_DEFAULT;
296 else if (lex_match_id (lexer, "R"))
298 statistics |= STATS_R;
300 else if (lex_match_id (lexer, "COEFF"))
302 statistics |= STATS_COEFF;
304 else if (lex_match_id (lexer, "ANOVA"))
306 statistics |= STATS_ANOVA;
308 else if (lex_match_id (lexer, "BCOV"))
310 statistics |= STATS_BCOV;
312 else if (lex_match_id (lexer, "TOL"))
314 statistics |= STATS_TOL;
316 else if (lex_match_id (lexer, "CI"))
318 statistics |= STATS_CI;
320 if (lex_match (lexer, T_LPAREN) &&
321 lex_force_num (lexer))
323 regression.ci = lex_number (lexer) / 100.0;
325 if (! lex_force_match (lexer, T_RPAREN))
331 lex_error (lexer, NULL);
337 regression.stats = statistics;
340 else if (lex_match_id (lexer, "SAVE"))
342 lex_match (lexer, T_EQUALS);
344 while (lex_token (lexer) != T_ENDCMD
345 && lex_token (lexer) != T_SLASH)
347 if (lex_match_id (lexer, "PRED"))
349 regression.pred = true;
351 else if (lex_match_id (lexer, "RESID"))
353 regression.resid = true;
357 lex_error (lexer, NULL);
364 lex_error (lexer, NULL);
369 if (!regression.vars)
371 dict_get_vars (dict, ®ression.vars, ®ression.n_vars, 0);
374 save = regression.pred || regression.resid;
375 workspace.extras = 0;
376 workspace.res_idx = -1;
377 workspace.pred_idx = -1;
378 workspace.writer = NULL;
379 workspace.reader = NULL;
380 workspace.residvars = NULL;
381 workspace.predvars = NULL;
385 struct caseproto *proto = caseproto_create ();
387 if (regression.resid)
389 workspace.res_idx = workspace.extras ++;
390 workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
392 for (i = 0; i < regression.n_dep_vars; ++i)
394 workspace.residvars[i] = create_aux_var (ds, "RES");
395 proto = caseproto_add_width (proto, 0);
401 workspace.pred_idx = workspace.extras ++;
402 workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
404 for (i = 0; i < regression.n_dep_vars; ++i)
406 workspace.predvars[i] = create_aux_var (ds, "PRED");
407 proto = caseproto_add_width (proto, 0);
411 if (proc_make_temporary_transformations_permanent (ds))
412 msg (SW, _("REGRESSION with SAVE ignores TEMPORARY. "
413 "Temporary transformations will be made permanent."));
415 if (dict_get_filter (dict))
416 msg (SW, _("REGRESSION with SAVE ignores FILTER. "
417 "All cases will be processed."));
419 workspace.writer = autopaging_writer_create (proto);
420 caseproto_unref (proto);
425 struct casegrouper *grouper;
426 struct casereader *group;
429 grouper = casegrouper_create_splits (proc_open_filtering (ds, !save), dict);
432 while (casegrouper_get_next_group (grouper, &group))
434 run_regression (®ression,
439 ok = casegrouper_destroy (grouper);
440 ok = proc_commit (ds) && ok;
443 if (workspace.writer)
445 struct save_trans_data *save_trans_data = xmalloc (sizeof *save_trans_data);
446 struct casereader *r = casewriter_make_reader (workspace.writer);
447 workspace.writer = NULL;
448 workspace.reader = r;
449 save_trans_data->ws = xmalloc (sizeof (workspace));
450 memcpy (save_trans_data->ws, &workspace, sizeof (workspace));
451 save_trans_data->n_dep_vars = regression.n_dep_vars;
453 add_transformation (ds, save_trans_func, save_trans_free, save_trans_data);
457 free (regression.vars);
458 free (regression.dep_vars);
463 free (regression.vars);
464 free (regression.dep_vars);
468 /* Return the size of the union of dependent and independent variables */
470 get_n_all_vars (const struct regression *cmd)
472 size_t result = cmd->n_vars;
476 result += cmd->n_dep_vars;
477 for (i = 0; i < cmd->n_dep_vars; i++)
479 for (j = 0; j < cmd->n_vars; j++)
481 if (cmd->vars[j] == cmd->dep_vars[i])
490 /* Fill VARS with the union of dependent and independent variables */
492 fill_all_vars (const struct variable **vars, const struct regression *cmd)
496 for (i = 0; i < cmd->n_vars; i++)
498 vars[i] = cmd->vars[i];
501 for (i = 0; i < cmd->n_dep_vars; i++)
505 for (j = 0; j < cmd->n_vars; j++)
507 if (cmd->dep_vars[i] == cmd->vars[j])
515 vars[cmd->n_vars + x++] = cmd->dep_vars[i];
521 /* Fill the array VARS, with all the predictor variables from CMD, except
524 fill_predictor_x (const struct variable **vars, const struct variable *x, const struct regression *cmd)
529 for (i = 0; i < cmd->n_vars; i++)
531 if (cmd->vars[i] == x)
534 vars[n++] = cmd->vars[i];
539 Is variable k the dependent variable?
542 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
544 return v == cmd->vars[k];
548 /* Identify the explanatory variables in v_variables. Returns
549 the number of independent variables. */
551 identify_indep_vars (const struct regression *cmd,
552 const struct variable **indep_vars,
553 const struct variable *depvar)
555 int n_indep_vars = 0;
558 for (i = 0; i < cmd->n_vars; i++)
559 if (!is_depvar (cmd, i, depvar))
560 indep_vars[n_indep_vars++] = cmd->vars[i];
561 if ((n_indep_vars < 1) && is_depvar (cmd, 0, depvar))
564 There is only one independent variable, and it is the same
565 as the dependent variable. Print a warning and continue.
569 ("The dependent variable is equal to the independent variable. "
570 "The least squares line is therefore Y=X. "
571 "Standard errors and related statistics may be meaningless."));
573 indep_vars[0] = cmd->vars[0];
579 fill_covariance (gsl_matrix * cov, struct covariance *all_cov,
580 const struct variable **vars,
581 size_t n_vars, const struct variable *dep_var,
582 const struct variable **all_vars, size_t n_all_vars,
587 size_t dep_subscript = SIZE_MAX;
589 const gsl_matrix *ssizes;
590 const gsl_matrix *mean_matrix;
591 const gsl_matrix *ssize_matrix;
594 const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
599 rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
601 for (i = 0; i < n_all_vars; i++)
603 for (j = 0; j < n_vars; j++)
605 if (vars[j] == all_vars[i])
610 if (all_vars[i] == dep_var)
615 assert (dep_subscript != SIZE_MAX);
617 mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
618 ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
619 for (i = 0; i < cov->size1 - 1; i++)
621 means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
622 / gsl_matrix_get (ssize_matrix, rows[i], 0);
623 for (j = 0; j < cov->size2 - 1; j++)
625 gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
626 gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
629 means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
630 / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
631 ssizes = covariance_moments (all_cov, MOMENT_NONE);
632 result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
633 for (i = 0; i < cov->size1 - 1; i++)
635 gsl_matrix_set (cov, i, cov->size1 - 1,
636 gsl_matrix_get (cm, rows[i], dep_subscript));
637 gsl_matrix_set (cov, cov->size1 - 1, i,
638 gsl_matrix_get (cm, rows[i], dep_subscript));
639 if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
641 result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
644 gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
645 gsl_matrix_get (cm, dep_subscript, dep_subscript));
652 struct model_container
654 struct linreg **models;
658 STATISTICS subcommand output functions.
660 static void reg_stats_r (const struct linreg *, const struct variable *);
661 static void reg_stats_coeff (const struct regression *, const struct linreg *,
662 const struct model_container *, const gsl_matrix *,
663 const struct variable *);
664 static void reg_stats_anova (const struct linreg *, const struct variable *);
665 static void reg_stats_bcov (const struct linreg *, const struct variable *);
668 static struct linreg **
669 run_regression_get_models (const struct regression *cmd,
670 struct casereader *input,
674 struct linreg **models = NULL;
675 struct model_container *model_container = xzalloc (sizeof (*model_container) * cmd->n_vars);
678 struct covariance *cov;
679 struct casereader *reader;
681 if (cmd->stats & STATS_TOL)
683 for (i = 0; i < cmd->n_vars; i++)
685 struct regression subreg;
686 subreg.origin = cmd->origin;
688 subreg.n_vars = cmd->n_vars - 1;
689 subreg.n_dep_vars = 1;
690 subreg.vars = xmalloc (sizeof (*subreg.vars) * cmd->n_vars - 1);
691 subreg.dep_vars = xmalloc (sizeof (*subreg.dep_vars));
692 fill_predictor_x (subreg.vars, cmd->vars[i], cmd);
693 subreg.dep_vars[0] = cmd->vars[i];
694 subreg.stats = STATS_R;
696 subreg.resid = false;
699 model_container[i].models =
700 run_regression_get_models (&subreg, input, false);
702 free (subreg.dep_vars);
706 size_t n_all_vars = get_n_all_vars (cmd);
707 const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
709 /* In the (rather pointless) case where the dependent variable is
710 the independent variable, n_all_vars == 1.
711 However this would result in a buffer overflow so we must
712 over-allocate the space required in this malloc call.
714 double *means = xnmalloc (n_all_vars <= 1 ? 2 : n_all_vars,
716 fill_all_vars (all_vars, cmd);
717 cov = covariance_1pass_create (n_all_vars, all_vars,
718 dict_get_weight (dataset_dict (cmd->ds)),
719 MV_ANY, cmd->origin == false);
721 reader = casereader_clone (input);
722 reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
725 struct casereader *r = casereader_clone (reader);
727 for (; (c = casereader_read (r)) != NULL; case_unref (c))
729 covariance_accumulate (cov, c);
731 casereader_destroy (r);
734 models = xcalloc (cmd->n_dep_vars, sizeof (*models));
736 for (int k = 0; k < cmd->n_dep_vars; k++)
738 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
739 const struct variable *dep_var = cmd->dep_vars[k];
740 int n_indep = identify_indep_vars (cmd, vars, dep_var);
741 gsl_matrix *cov_matrix = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
742 double n_data = fill_covariance (cov_matrix, cov, vars, n_indep,
743 dep_var, all_vars, n_all_vars, means);
744 models[k] = linreg_alloc (dep_var, vars, n_data, n_indep, cmd->origin);
745 for (i = 0; i < n_indep; i++)
747 linreg_set_indep_variable_mean (models[k], i, means[i]);
749 linreg_set_depvar_mean (models[k], means[i]);
752 linreg_fit (cov_matrix, models[k]);
754 if (output && !taint_has_tainted_successor (casereader_get_taint (input)))
757 Find the least-squares estimates and other statistics.
759 if (cmd->stats & STATS_R)
760 reg_stats_r (models[k], dep_var);
762 if (cmd->stats & STATS_ANOVA)
763 reg_stats_anova (models[k], dep_var);
765 if (cmd->stats & STATS_COEFF)
766 reg_stats_coeff (cmd, models[k],
768 cov_matrix, dep_var);
770 if (cmd->stats & STATS_BCOV)
771 reg_stats_bcov (models[k], dep_var);
776 msg (SE, _("No valid data found. This command was skipped."));
779 gsl_matrix_free (cov_matrix);
782 casereader_destroy (reader);
784 for (int i = 0; i < cmd->n_vars; i++)
786 if (model_container[i].models)
788 linreg_unref (model_container[i].models[0]);
790 free (model_container[i].models);
792 free (model_container);
796 covariance_destroy (cov);
801 run_regression (const struct regression *cmd,
802 struct regression_workspace *ws,
803 struct casereader *input)
805 struct linreg **models = run_regression_get_models (cmd, input, true);
810 struct casereader *r = casereader_clone (input);
812 for (; (c = casereader_read (r)) != NULL; case_unref (c))
814 struct ccase *outc = case_create (casewriter_get_proto (ws->writer));
815 for (int k = 0; k < cmd->n_dep_vars; k++)
817 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
818 const struct variable *dep_var = cmd->dep_vars[k];
819 int n_indep = identify_indep_vars (cmd, vars, dep_var);
820 double *vals = xnmalloc (n_indep, sizeof (*vals));
821 for (int i = 0; i < n_indep; i++)
823 const union value *tmp = case_data (c, vars[i]);
829 double pred = linreg_predict (models[k], vals, n_indep);
830 *case_num_rw_idx (outc, k * ws->extras + ws->pred_idx) = pred;
835 double obs = case_num (c, linreg_dep_var (models[k]));
836 double res = linreg_residual (models[k], obs, vals, n_indep);
837 *case_num_rw_idx (outc, k * ws->extras + ws->res_idx) = res;
842 casewriter_write (ws->writer, outc);
844 casereader_destroy (r);
847 for (int k = 0; k < cmd->n_dep_vars; k++)
849 linreg_unref (models[k]);
853 casereader_destroy (input);
860 reg_stats_r (const struct linreg * c, const struct variable *var)
862 struct pivot_table *table = pivot_table_create__ (
863 pivot_value_new_text_format (N_("Model Summary (%s)"),
864 var_to_string (var)),
867 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
868 N_("R"), N_("R Square"), N_("Adjusted R Square"),
869 N_("Std. Error of the Estimate"));
871 double rsq = linreg_ssreg (c) / linreg_sst (c);
872 double adjrsq = (rsq -
873 (1.0 - rsq) * linreg_n_coeffs (c)
874 / (linreg_n_obs (c) - linreg_n_coeffs (c) - 1));
875 double std_error = sqrt (linreg_mse (c));
878 sqrt (rsq), rsq, adjrsq, std_error
880 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
881 pivot_table_put1 (table, i, pivot_value_new_number (entries[i]));
883 pivot_table_submit (table);
887 Table showing estimated regression coefficients.
890 reg_stats_coeff (const struct regression *cmd, const struct linreg *c,
891 const struct model_container *mc, const gsl_matrix *cov,
892 const struct variable *var)
894 struct pivot_table *table = pivot_table_create__ (
895 pivot_value_new_text_format (N_("Coefficients (%s)"), var_to_string (var)),
898 struct pivot_dimension *statistics = pivot_dimension_create (
899 table, PIVOT_AXIS_COLUMN, N_("Statistics"));
900 pivot_category_create_group (statistics->root,
901 N_("Unstandardized Coefficients"),
902 N_("B"), N_("Std. Error"));
903 pivot_category_create_group (statistics->root,
904 N_("Standardized Coefficients"), N_("Beta"));
905 pivot_category_create_leaves (statistics->root, N_("t"),
906 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
907 if (cmd->stats & STATS_CI)
909 struct pivot_category *interval = pivot_category_create_group__ (
910 statistics->root, pivot_value_new_text_format (
911 N_("%g%% Confidence Interval for B"),
913 pivot_category_create_leaves (interval, N_("Lower Bound"),
917 if (cmd->stats & STATS_TOL)
918 pivot_category_create_group (statistics->root,
919 N_("Collinearity Statistics"),
920 N_("Tolerance"), N_("VIF"));
923 struct pivot_dimension *variables = pivot_dimension_create (
924 table, PIVOT_AXIS_ROW, N_("Variables"));
926 double df = linreg_n_obs (c) - linreg_n_coeffs (c) - 1;
927 double q = (1 - cmd->ci) / 2.0; /* 2-tailed test */
928 double tval = gsl_cdf_tdist_Qinv (q, df);
932 int var_idx = pivot_category_create_leaf (
933 variables->root, pivot_value_new_text (N_("(Constant)")));
935 double std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
936 double t_stat = linreg_intercept (c) / std_err;
937 double base_entries[] = {
938 linreg_intercept (c),
942 2.0 * gsl_cdf_tdist_Q (fabs (t_stat),
943 linreg_n_obs (c) - linreg_n_coeffs (c)),
947 for (size_t i = 0; i < sizeof base_entries / sizeof *base_entries; i++)
948 pivot_table_put2 (table, col++, var_idx,
949 pivot_value_new_number (base_entries[i]));
951 if (cmd->stats & STATS_CI)
953 double interval_entries[] = {
954 linreg_intercept (c) - tval * std_err,
955 linreg_intercept (c) + tval * std_err,
958 for (size_t i = 0; i < sizeof interval_entries / sizeof *interval_entries; i++)
959 pivot_table_put2 (table, col++, var_idx,
960 pivot_value_new_number (interval_entries[i]));
964 for (size_t j = 0; j < linreg_n_coeffs (c); j++)
966 const struct variable *v = linreg_indep_var (c, j);
967 int var_idx = pivot_category_create_leaf (
968 variables->root, pivot_value_new_variable (v));
970 double std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
971 double t_stat = linreg_coeff (c, j) / std_err;
972 double base_entries[] = {
974 sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1)),
975 (sqrt (gsl_matrix_get (cov, j, j)) * linreg_coeff (c, j) /
976 sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1))),
978 2 * gsl_cdf_tdist_Q (fabs (t_stat), df)
982 for (size_t i = 0; i < sizeof base_entries / sizeof *base_entries; i++)
983 pivot_table_put2 (table, col++, var_idx,
984 pivot_value_new_number (base_entries[i]));
986 if (cmd->stats & STATS_CI)
988 double interval_entries[] = {
989 linreg_coeff (c, j) - tval * std_err,
990 linreg_coeff (c, j) + tval * std_err,
994 for (size_t i = 0; i < sizeof interval_entries / sizeof *interval_entries; i++)
995 pivot_table_put2 (table, col++, var_idx,
996 pivot_value_new_number (interval_entries[i]));
999 if (cmd->stats & STATS_TOL)
1002 struct linreg *m = mc[j].models[0];
1003 double rsq = linreg_ssreg (m) / linreg_sst (m);
1004 pivot_table_put2 (table, col++, var_idx, pivot_value_new_number (1.0 - rsq));
1005 pivot_table_put2 (table, col++, var_idx, pivot_value_new_number (1.0 / (1.0 - rsq)));
1010 pivot_table_submit (table);
1014 Display the ANOVA table.
1017 reg_stats_anova (const struct linreg * c, const struct variable *var)
1019 struct pivot_table *table = pivot_table_create__ (
1020 pivot_value_new_text_format (N_("ANOVA (%s)"), var_to_string (var)),
1023 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1024 N_("Sum of Squares"), PIVOT_RC_OTHER,
1025 N_("df"), PIVOT_RC_INTEGER,
1026 N_("Mean Square"), PIVOT_RC_OTHER,
1027 N_("F"), PIVOT_RC_OTHER,
1028 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1030 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Source"),
1031 N_("Regression"), N_("Residual"), N_("Total"));
1033 double msm = linreg_ssreg (c) / linreg_dfmodel (c);
1034 double mse = linreg_mse (c);
1035 double F = msm / mse;
1044 /* Sums of Squares. */
1045 { 0, 0, linreg_ssreg (c) },
1046 { 0, 1, linreg_sse (c) },
1047 { 0, 2, linreg_sst (c) },
1048 /* Degrees of freedom. */
1049 { 1, 0, linreg_dfmodel (c) },
1050 { 1, 1, linreg_dferror (c) },
1051 { 1, 2, linreg_dftotal (c) },
1058 { 4, 0, gsl_cdf_fdist_Q (F, linreg_dfmodel (c), linreg_dferror (c)) },
1060 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1062 const struct entry *e = &entries[i];
1063 pivot_table_put2 (table, e->stat_idx, e->source_idx,
1064 pivot_value_new_number (e->x));
1067 pivot_table_submit (table);
1072 reg_stats_bcov (const struct linreg * c, const struct variable *var)
1074 struct pivot_table *table = pivot_table_create__ (
1075 pivot_value_new_text_format (N_("Coefficient Correlations (%s)"),
1076 var_to_string (var)),
1077 "Coefficient Correlations");
1079 for (size_t i = 0; i < 2; i++)
1081 struct pivot_dimension *models = pivot_dimension_create (
1082 table, i ? PIVOT_AXIS_ROW : PIVOT_AXIS_COLUMN, N_("Models"));
1083 for (size_t j = 0; j < linreg_n_coeffs (c); j++)
1084 pivot_category_create_leaf (
1085 models->root, pivot_value_new_variable (
1086 linreg_indep_var (c, j)));
1089 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Statistics"),
1092 for (size_t i = 0; i < linreg_n_coeffs (c); i++)
1093 for (size_t k = 0; k < linreg_n_coeffs (c); k++)
1095 double cov = gsl_matrix_get (linreg_cov (c), MIN (i, k), MAX (i, k));
1096 pivot_table_put3 (table, k, i, 0, pivot_value_new_number (cov));
1099 pivot_table_submit (table);