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);
162 static enum trns_result
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;
195 cmd_regression (struct lexer *lexer, struct dataset *ds)
197 struct regression_workspace workspace;
198 struct regression regression;
199 const struct dictionary *dict = dataset_dict (ds);
202 memset (®ression, 0, sizeof (struct regression));
204 regression.ci = 0.95;
205 regression.stats = STATS_DEFAULT;
206 regression.pred = false;
207 regression.resid = false;
210 regression.origin = false;
212 bool variables_seen = false;
213 bool method_seen = false;
214 bool dependent_seen = false;
215 while (lex_token (lexer) != T_ENDCMD)
217 lex_match (lexer, T_SLASH);
219 if (lex_match_id (lexer, "VARIABLES"))
223 msg (SE, _("VARIABLES may not appear after %s"), "METHOD");
228 msg (SE, _("VARIABLES may not appear after %s"), "DEPENDENT");
231 variables_seen = true;
232 lex_match (lexer, T_EQUALS);
234 if (!parse_variables_const (lexer, dict,
235 ®ression.vars, ®ression.n_vars,
236 PV_NO_DUPLICATE | PV_NUMERIC))
239 else if (lex_match_id (lexer, "DEPENDENT"))
241 dependent_seen = true;
242 lex_match (lexer, T_EQUALS);
244 free (regression.dep_vars);
245 regression.n_dep_vars = 0;
247 if (!parse_variables_const (lexer, dict,
248 ®ression.dep_vars,
249 ®ression.n_dep_vars,
250 PV_NO_DUPLICATE | PV_NUMERIC))
253 else if (lex_match_id (lexer, "ORIGIN"))
255 regression.origin = true;
257 else if (lex_match_id (lexer, "NOORIGIN"))
259 regression.origin = false;
261 else if (lex_match_id (lexer, "METHOD"))
264 lex_match (lexer, T_EQUALS);
266 if (!lex_force_match_id (lexer, "ENTER"))
271 if (! variables_seen)
273 if (!parse_variables_const (lexer, dict,
274 ®ression.vars, ®ression.n_vars,
275 PV_NO_DUPLICATE | PV_NUMERIC))
279 else if (lex_match_id (lexer, "STATISTICS"))
281 unsigned long statistics = 0;
282 lex_match (lexer, T_EQUALS);
284 while (lex_token (lexer) != T_ENDCMD
285 && lex_token (lexer) != T_SLASH)
287 if (lex_match (lexer, T_ALL))
291 else if (lex_match_id (lexer, "DEFAULTS"))
293 statistics |= STATS_DEFAULT;
295 else if (lex_match_id (lexer, "R"))
297 statistics |= STATS_R;
299 else if (lex_match_id (lexer, "COEFF"))
301 statistics |= STATS_COEFF;
303 else if (lex_match_id (lexer, "ANOVA"))
305 statistics |= STATS_ANOVA;
307 else if (lex_match_id (lexer, "BCOV"))
309 statistics |= STATS_BCOV;
311 else if (lex_match_id (lexer, "TOL"))
313 statistics |= STATS_TOL;
315 else if (lex_match_id (lexer, "CI"))
317 statistics |= STATS_CI;
319 if (lex_match (lexer, T_LPAREN) &&
320 lex_force_num (lexer))
322 regression.ci = lex_number (lexer) / 100.0;
324 if (! lex_force_match (lexer, T_RPAREN))
330 lex_error (lexer, NULL);
336 regression.stats = statistics;
339 else if (lex_match_id (lexer, "SAVE"))
341 lex_match (lexer, T_EQUALS);
343 while (lex_token (lexer) != T_ENDCMD
344 && lex_token (lexer) != T_SLASH)
346 if (lex_match_id (lexer, "PRED"))
348 regression.pred = true;
350 else if (lex_match_id (lexer, "RESID"))
352 regression.resid = true;
356 lex_error (lexer, NULL);
363 lex_error (lexer, NULL);
368 if (!regression.vars)
370 dict_get_vars (dict, ®ression.vars, ®ression.n_vars, 0);
373 save = regression.pred || regression.resid;
374 workspace.extras = 0;
375 workspace.res_idx = -1;
376 workspace.pred_idx = -1;
377 workspace.writer = NULL;
378 workspace.reader = NULL;
379 workspace.residvars = NULL;
380 workspace.predvars = NULL;
384 struct caseproto *proto = caseproto_create ();
386 if (regression.resid)
388 workspace.res_idx = workspace.extras ++;
389 workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
391 for (i = 0; i < regression.n_dep_vars; ++i)
393 workspace.residvars[i] = create_aux_var (ds, "RES");
394 proto = caseproto_add_width (proto, 0);
400 workspace.pred_idx = workspace.extras ++;
401 workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
403 for (i = 0; i < regression.n_dep_vars; ++i)
405 workspace.predvars[i] = create_aux_var (ds, "PRED");
406 proto = caseproto_add_width (proto, 0);
410 if (proc_make_temporary_transformations_permanent (ds))
411 msg (SW, _("REGRESSION with SAVE ignores TEMPORARY. "
412 "Temporary transformations will be made permanent."));
414 if (dict_get_filter (dict))
415 msg (SW, _("REGRESSION with SAVE ignores FILTER. "
416 "All cases will be processed."));
418 workspace.writer = autopaging_writer_create (proto);
419 caseproto_unref (proto);
424 struct casegrouper *grouper;
425 struct casereader *group;
428 grouper = casegrouper_create_splits (proc_open_filtering (ds, !save), dict);
431 while (casegrouper_get_next_group (grouper, &group))
433 run_regression (®ression,
438 ok = casegrouper_destroy (grouper);
439 ok = proc_commit (ds) && ok;
442 if (workspace.writer)
444 struct save_trans_data *save_trans_data = xmalloc (sizeof *save_trans_data);
445 struct casereader *r = casewriter_make_reader (workspace.writer);
446 workspace.writer = NULL;
447 workspace.reader = r;
448 save_trans_data->ws = xmalloc (sizeof (workspace));
449 memcpy (save_trans_data->ws, &workspace, sizeof (workspace));
450 save_trans_data->n_dep_vars = regression.n_dep_vars;
452 add_transformation (ds, save_trans_func, save_trans_free, save_trans_data);
456 free (regression.vars);
457 free (regression.dep_vars);
462 free (regression.vars);
463 free (regression.dep_vars);
467 /* Return the size of the union of dependent and independent variables */
469 get_n_all_vars (const struct regression *cmd)
471 size_t result = cmd->n_vars;
475 result += cmd->n_dep_vars;
476 for (i = 0; i < cmd->n_dep_vars; i++)
478 for (j = 0; j < cmd->n_vars; j++)
480 if (cmd->vars[j] == cmd->dep_vars[i])
489 /* Fill VARS with the union of dependent and independent variables */
491 fill_all_vars (const struct variable **vars, const struct regression *cmd)
495 for (i = 0; i < cmd->n_vars; i++)
497 vars[i] = cmd->vars[i];
500 for (i = 0; i < cmd->n_dep_vars; i++)
504 for (j = 0; j < cmd->n_vars; j++)
506 if (cmd->dep_vars[i] == cmd->vars[j])
514 vars[cmd->n_vars + x++] = cmd->dep_vars[i];
520 /* Fill the array VARS, with all the predictor variables from CMD, except
523 fill_predictor_x (const struct variable **vars, const struct variable *x, const struct regression *cmd)
528 for (i = 0; i < cmd->n_vars; i++)
530 if (cmd->vars[i] == x)
533 vars[n++] = cmd->vars[i];
538 Is variable k the dependent variable?
541 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
543 return v == cmd->vars[k];
547 /* Identify the explanatory variables in v_variables. Returns
548 the number of independent variables. */
550 identify_indep_vars (const struct regression *cmd,
551 const struct variable **indep_vars,
552 const struct variable *depvar)
554 int n_indep_vars = 0;
557 for (i = 0; i < cmd->n_vars; i++)
558 if (!is_depvar (cmd, i, depvar))
559 indep_vars[n_indep_vars++] = cmd->vars[i];
560 if ((n_indep_vars < 1) && is_depvar (cmd, 0, depvar))
563 There is only one independent variable, and it is the same
564 as the dependent variable. Print a warning and continue.
568 ("The dependent variable is equal to the independent variable. "
569 "The least squares line is therefore Y=X. "
570 "Standard errors and related statistics may be meaningless."));
572 indep_vars[0] = cmd->vars[0];
578 fill_covariance (gsl_matrix * cov, struct covariance *all_cov,
579 const struct variable **vars,
580 size_t n_vars, const struct variable *dep_var,
581 const struct variable **all_vars, size_t n_all_vars,
586 size_t dep_subscript = SIZE_MAX;
588 const gsl_matrix *ssizes;
589 const gsl_matrix *mean_matrix;
590 const gsl_matrix *ssize_matrix;
593 const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
598 rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
600 for (i = 0; i < n_all_vars; i++)
602 for (j = 0; j < n_vars; j++)
604 if (vars[j] == all_vars[i])
609 if (all_vars[i] == dep_var)
614 assert (dep_subscript != SIZE_MAX);
616 mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
617 ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
618 for (i = 0; i < cov->size1 - 1; i++)
620 means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
621 / gsl_matrix_get (ssize_matrix, rows[i], 0);
622 for (j = 0; j < cov->size2 - 1; j++)
624 gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
625 gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
628 means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
629 / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
630 ssizes = covariance_moments (all_cov, MOMENT_NONE);
631 result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
632 for (i = 0; i < cov->size1 - 1; i++)
634 gsl_matrix_set (cov, i, cov->size1 - 1,
635 gsl_matrix_get (cm, rows[i], dep_subscript));
636 gsl_matrix_set (cov, cov->size1 - 1, i,
637 gsl_matrix_get (cm, rows[i], dep_subscript));
638 if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
640 result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
643 gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
644 gsl_matrix_get (cm, dep_subscript, dep_subscript));
651 struct model_container
653 struct linreg **models;
657 STATISTICS subcommand output functions.
659 static void reg_stats_r (const struct linreg *, const struct variable *);
660 static void reg_stats_coeff (const struct regression *, const struct linreg *,
661 const struct model_container *, const gsl_matrix *,
662 const struct variable *);
663 static void reg_stats_anova (const struct linreg *, const struct variable *);
664 static void reg_stats_bcov (const struct linreg *, const struct variable *);
667 static struct linreg **
668 run_regression_get_models (const struct regression *cmd,
669 struct casereader *input,
673 struct model_container *model_container = XCALLOC (cmd->n_vars, struct model_container);
676 struct covariance *cov;
677 struct casereader *reader;
679 if (cmd->stats & STATS_TOL)
681 for (i = 0; i < cmd->n_vars; i++)
683 struct regression subreg;
684 subreg.origin = cmd->origin;
686 subreg.n_vars = cmd->n_vars - 1;
687 subreg.n_dep_vars = 1;
688 subreg.vars = xmalloc (sizeof (*subreg.vars) * cmd->n_vars - 1);
689 subreg.dep_vars = xmalloc (sizeof (*subreg.dep_vars));
690 fill_predictor_x (subreg.vars, cmd->vars[i], cmd);
691 subreg.dep_vars[0] = cmd->vars[i];
692 subreg.stats = STATS_R;
694 subreg.resid = false;
697 model_container[i].models =
698 run_regression_get_models (&subreg, input, false);
700 free (subreg.dep_vars);
704 size_t n_all_vars = get_n_all_vars (cmd);
705 const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
707 /* In the (rather pointless) case where the dependent variable is
708 the independent variable, n_all_vars == 1.
709 However this would result in a buffer overflow so we must
710 over-allocate the space required in this malloc call.
712 double *means = xnmalloc (n_all_vars <= 1 ? 2 : n_all_vars,
714 fill_all_vars (all_vars, cmd);
715 cov = covariance_1pass_create (n_all_vars, all_vars,
716 dict_get_weight (dataset_dict (cmd->ds)),
717 MV_ANY, cmd->origin == false);
719 reader = casereader_clone (input);
720 reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
723 struct casereader *r = casereader_clone (reader);
725 for (; (c = casereader_read (r)) != NULL; case_unref (c))
727 covariance_accumulate (cov, c);
729 casereader_destroy (r);
732 struct linreg **models = XCALLOC (cmd->n_dep_vars, struct linreg*);
734 for (int k = 0; k < cmd->n_dep_vars; k++)
736 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
737 const struct variable *dep_var = cmd->dep_vars[k];
738 int n_indep = identify_indep_vars (cmd, vars, dep_var);
739 gsl_matrix *cov_matrix = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
740 double n_data = fill_covariance (cov_matrix, cov, vars, n_indep,
741 dep_var, all_vars, n_all_vars, means);
742 models[k] = linreg_alloc (dep_var, vars, n_data, n_indep, cmd->origin);
743 for (i = 0; i < n_indep; i++)
745 linreg_set_indep_variable_mean (models[k], i, means[i]);
747 linreg_set_depvar_mean (models[k], means[i]);
750 linreg_fit (cov_matrix, models[k]);
752 if (output && !taint_has_tainted_successor (casereader_get_taint (input)))
755 Find the least-squares estimates and other statistics.
757 if (cmd->stats & STATS_R)
758 reg_stats_r (models[k], dep_var);
760 if (cmd->stats & STATS_ANOVA)
761 reg_stats_anova (models[k], dep_var);
763 if (cmd->stats & STATS_COEFF)
764 reg_stats_coeff (cmd, models[k],
766 cov_matrix, dep_var);
768 if (cmd->stats & STATS_BCOV)
769 reg_stats_bcov (models[k], dep_var);
774 msg (SE, _("No valid data found. This command was skipped."));
777 gsl_matrix_free (cov_matrix);
780 casereader_destroy (reader);
782 for (int i = 0; i < cmd->n_vars; i++)
784 if (model_container[i].models)
786 linreg_unref (model_container[i].models[0]);
788 free (model_container[i].models);
790 free (model_container);
794 covariance_destroy (cov);
799 run_regression (const struct regression *cmd,
800 struct regression_workspace *ws,
801 struct casereader *input)
803 struct linreg **models = run_regression_get_models (cmd, input, true);
808 struct casereader *r = casereader_clone (input);
810 for (; (c = casereader_read (r)) != NULL; case_unref (c))
812 struct ccase *outc = case_create (casewriter_get_proto (ws->writer));
813 for (int k = 0; k < cmd->n_dep_vars; k++)
815 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
816 const struct variable *dep_var = cmd->dep_vars[k];
817 int n_indep = identify_indep_vars (cmd, vars, dep_var);
818 double *vals = xnmalloc (n_indep, sizeof (*vals));
819 for (int i = 0; i < n_indep; i++)
821 const union value *tmp = case_data (c, vars[i]);
827 double pred = linreg_predict (models[k], vals, n_indep);
828 *case_num_rw_idx (outc, k * ws->extras + ws->pred_idx) = pred;
833 double obs = case_num (c, linreg_dep_var (models[k]));
834 double res = linreg_residual (models[k], obs, vals, n_indep);
835 *case_num_rw_idx (outc, k * ws->extras + ws->res_idx) = res;
840 casewriter_write (ws->writer, outc);
842 casereader_destroy (r);
845 for (int k = 0; k < cmd->n_dep_vars; k++)
847 linreg_unref (models[k]);
851 casereader_destroy (input);
858 reg_stats_r (const struct linreg * c, const struct variable *var)
860 struct pivot_table *table = pivot_table_create__ (
861 pivot_value_new_text_format (N_("Model Summary (%s)"),
862 var_to_string (var)),
865 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
866 N_("R"), N_("R Square"), N_("Adjusted R Square"),
867 N_("Std. Error of the Estimate"));
869 double rsq = linreg_ssreg (c) / linreg_sst (c);
870 double adjrsq = (rsq -
871 (1.0 - rsq) * linreg_n_coeffs (c)
872 / (linreg_n_obs (c) - linreg_n_coeffs (c) - 1));
873 double std_error = sqrt (linreg_mse (c));
876 sqrt (rsq), rsq, adjrsq, std_error
878 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
879 pivot_table_put1 (table, i, pivot_value_new_number (entries[i]));
881 pivot_table_submit (table);
885 Table showing estimated regression coefficients.
888 reg_stats_coeff (const struct regression *cmd, const struct linreg *c,
889 const struct model_container *mc, const gsl_matrix *cov,
890 const struct variable *var)
892 struct pivot_table *table = pivot_table_create__ (
893 pivot_value_new_text_format (N_("Coefficients (%s)"), var_to_string (var)),
896 struct pivot_dimension *statistics = pivot_dimension_create (
897 table, PIVOT_AXIS_COLUMN, N_("Statistics"));
898 pivot_category_create_group (statistics->root,
899 N_("Unstandardized Coefficients"),
900 N_("B"), N_("Std. Error"));
901 pivot_category_create_group (statistics->root,
902 N_("Standardized Coefficients"), N_("Beta"));
903 pivot_category_create_leaves (statistics->root, N_("t"),
904 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
905 if (cmd->stats & STATS_CI)
907 struct pivot_category *interval = pivot_category_create_group__ (
908 statistics->root, pivot_value_new_text_format (
909 N_("%g%% Confidence Interval for B"),
911 pivot_category_create_leaves (interval, N_("Lower Bound"),
915 if (cmd->stats & STATS_TOL)
916 pivot_category_create_group (statistics->root,
917 N_("Collinearity Statistics"),
918 N_("Tolerance"), N_("VIF"));
921 struct pivot_dimension *variables = pivot_dimension_create (
922 table, PIVOT_AXIS_ROW, N_("Variables"));
924 double df = linreg_n_obs (c) - linreg_n_coeffs (c) - 1;
925 double q = (1 - cmd->ci) / 2.0; /* 2-tailed test */
926 double tval = gsl_cdf_tdist_Qinv (q, df);
930 int var_idx = pivot_category_create_leaf (
931 variables->root, pivot_value_new_text (N_("(Constant)")));
933 double std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
934 double t_stat = linreg_intercept (c) / std_err;
935 double base_entries[] = {
936 linreg_intercept (c),
940 2.0 * gsl_cdf_tdist_Q (fabs (t_stat),
941 linreg_n_obs (c) - linreg_n_coeffs (c)),
945 for (size_t i = 0; i < sizeof base_entries / sizeof *base_entries; i++)
946 pivot_table_put2 (table, col++, var_idx,
947 pivot_value_new_number (base_entries[i]));
949 if (cmd->stats & STATS_CI)
951 double interval_entries[] = {
952 linreg_intercept (c) - tval * std_err,
953 linreg_intercept (c) + tval * std_err,
956 for (size_t i = 0; i < sizeof interval_entries / sizeof *interval_entries; i++)
957 pivot_table_put2 (table, col++, var_idx,
958 pivot_value_new_number (interval_entries[i]));
962 for (size_t j = 0; j < linreg_n_coeffs (c); j++)
964 const struct variable *v = linreg_indep_var (c, j);
965 int var_idx = pivot_category_create_leaf (
966 variables->root, pivot_value_new_variable (v));
968 double std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
969 double t_stat = linreg_coeff (c, j) / std_err;
970 double base_entries[] = {
972 sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1)),
973 (sqrt (gsl_matrix_get (cov, j, j)) * linreg_coeff (c, j) /
974 sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1))),
976 2 * gsl_cdf_tdist_Q (fabs (t_stat), df)
980 for (size_t i = 0; i < sizeof base_entries / sizeof *base_entries; i++)
981 pivot_table_put2 (table, col++, var_idx,
982 pivot_value_new_number (base_entries[i]));
984 if (cmd->stats & STATS_CI)
986 double interval_entries[] = {
987 linreg_coeff (c, j) - tval * std_err,
988 linreg_coeff (c, j) + tval * std_err,
992 for (size_t i = 0; i < sizeof interval_entries / sizeof *interval_entries; i++)
993 pivot_table_put2 (table, col++, var_idx,
994 pivot_value_new_number (interval_entries[i]));
997 if (cmd->stats & STATS_TOL)
1000 struct linreg *m = mc[j].models[0];
1001 double rsq = linreg_ssreg (m) / linreg_sst (m);
1002 pivot_table_put2 (table, col++, var_idx, pivot_value_new_number (1.0 - rsq));
1003 pivot_table_put2 (table, col++, var_idx, pivot_value_new_number (1.0 / (1.0 - rsq)));
1008 pivot_table_submit (table);
1012 Display the ANOVA table.
1015 reg_stats_anova (const struct linreg * c, const struct variable *var)
1017 struct pivot_table *table = pivot_table_create__ (
1018 pivot_value_new_text_format (N_("ANOVA (%s)"), var_to_string (var)),
1021 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
1022 N_("Sum of Squares"), PIVOT_RC_OTHER,
1023 N_("df"), PIVOT_RC_INTEGER,
1024 N_("Mean Square"), PIVOT_RC_OTHER,
1025 N_("F"), PIVOT_RC_OTHER,
1026 N_("Sig."), PIVOT_RC_SIGNIFICANCE);
1028 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Source"),
1029 N_("Regression"), N_("Residual"), N_("Total"));
1031 double msm = linreg_ssreg (c) / linreg_dfmodel (c);
1032 double mse = linreg_mse (c);
1033 double F = msm / mse;
1042 /* Sums of Squares. */
1043 { 0, 0, linreg_ssreg (c) },
1044 { 0, 1, linreg_sse (c) },
1045 { 0, 2, linreg_sst (c) },
1046 /* Degrees of freedom. */
1047 { 1, 0, linreg_dfmodel (c) },
1048 { 1, 1, linreg_dferror (c) },
1049 { 1, 2, linreg_dftotal (c) },
1056 { 4, 0, gsl_cdf_fdist_Q (F, linreg_dfmodel (c), linreg_dferror (c)) },
1058 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
1060 const struct entry *e = &entries[i];
1061 pivot_table_put2 (table, e->stat_idx, e->source_idx,
1062 pivot_value_new_number (e->x));
1065 pivot_table_submit (table);
1070 reg_stats_bcov (const struct linreg * c, const struct variable *var)
1072 struct pivot_table *table = pivot_table_create__ (
1073 pivot_value_new_text_format (N_("Coefficient Correlations (%s)"),
1074 var_to_string (var)),
1075 "Coefficient Correlations");
1077 for (size_t i = 0; i < 2; i++)
1079 struct pivot_dimension *models = pivot_dimension_create (
1080 table, i ? PIVOT_AXIS_ROW : PIVOT_AXIS_COLUMN, N_("Models"));
1081 for (size_t j = 0; j < linreg_n_coeffs (c); j++)
1082 pivot_category_create_leaf (
1083 models->root, pivot_value_new_variable (
1084 linreg_indep_var (c, j)));
1087 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Statistics"),
1090 for (size_t i = 0; i < linreg_n_coeffs (c); i++)
1091 for (size_t k = 0; k < linreg_n_coeffs (c); k++)
1093 double cov = gsl_matrix_get (linreg_cov (c), MIN (i, k), MAX (i, k));
1094 pivot_table_put3 (table, k, i, 0, pivot_value_new_number (cov));
1097 pivot_table_submit (table);