1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2009, 2010, 2011, 2012, 2013, 2014,
3 2016, 2017 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/tab.h"
50 #define _(msgid) gettext (msgid)
51 #define N_(msgid) msgid
54 #include <gl/intprops.h>
63 #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)
118 /* XXX handle too-long prefixes */
119 name = xmalloc (strlen (prefix) + INT_BUFSIZE_BOUND (i) + 1);
122 sprintf (name, "%s%d", prefix, i);
123 if (dict_lookup_var (dict, name) == NULL)
129 static const struct variable *
130 create_aux_var (struct dataset *ds, const char *prefix)
132 struct variable *var;
133 struct dictionary *dict = dataset_dict (ds);
134 char *name = reg_get_name (dict, prefix);
135 var = dict_create_var_assert (dict, name, 0);
140 /* Auxilliary data for transformation when /SAVE is entered */
141 struct save_trans_data
144 struct regression_workspace *ws;
148 save_trans_free (void *aux)
150 struct save_trans_data *save_trans_data = aux;
151 free (save_trans_data->ws->predvars);
152 free (save_trans_data->ws->residvars);
154 casereader_destroy (save_trans_data->ws->reader);
155 free (save_trans_data->ws);
156 free (save_trans_data);
161 save_trans_func (void *aux, struct ccase **c, casenumber x UNUSED)
163 struct save_trans_data *save_trans_data = aux;
164 struct regression_workspace *ws = save_trans_data->ws;
165 struct ccase *in = casereader_read (ws->reader);
170 *c = case_unshare (*c);
172 for (k = 0; k < save_trans_data->n_dep_vars; ++k)
174 if (ws->pred_idx != -1)
176 double pred = case_data_idx (in, ws->extras * k + ws->pred_idx)->f;
177 case_data_rw (*c, ws->predvars[k])->f = pred;
180 if (ws->res_idx != -1)
182 double resid = case_data_idx (in, ws->extras * k + ws->res_idx)->f;
183 case_data_rw (*c, ws->residvars[k])->f = resid;
189 return TRNS_CONTINUE;
194 cmd_regression (struct lexer *lexer, struct dataset *ds)
196 struct regression_workspace workspace;
197 struct regression regression;
198 const struct dictionary *dict = dataset_dict (ds);
201 memset (®ression, 0, sizeof (struct regression));
203 regression.ci = 0.95;
204 regression.stats = STATS_DEFAULT;
205 regression.pred = false;
206 regression.resid = false;
209 regression.origin = false;
211 bool variables_seen = false;
212 bool method_seen = false;
213 bool dependent_seen = false;
214 while (lex_token (lexer) != T_ENDCMD)
216 lex_match (lexer, T_SLASH);
218 if (lex_match_id (lexer, "VARIABLES"))
222 msg (SE, _("VARIABLES may not appear after %s"), "METHOD");
227 msg (SE, _("VARIABLES may not appear after %s"), "DEPENDENT");
230 variables_seen = true;
231 lex_match (lexer, T_EQUALS);
233 if (!parse_variables_const (lexer, dict,
234 ®ression.vars, ®ression.n_vars,
235 PV_NO_DUPLICATE | PV_NUMERIC))
238 else if (lex_match_id (lexer, "DEPENDENT"))
240 dependent_seen = true;
241 lex_match (lexer, T_EQUALS);
243 free (regression.dep_vars);
244 regression.n_dep_vars = 0;
246 if (!parse_variables_const (lexer, dict,
247 ®ression.dep_vars,
248 ®ression.n_dep_vars,
249 PV_NO_DUPLICATE | PV_NUMERIC))
252 else if (lex_match_id (lexer, "ORIGIN"))
254 regression.origin = true;
256 else if (lex_match_id (lexer, "NOORIGIN"))
258 regression.origin = false;
260 else if (lex_match_id (lexer, "METHOD"))
263 lex_match (lexer, T_EQUALS);
265 if (!lex_force_match_id (lexer, "ENTER"))
270 if (! variables_seen)
272 if (!parse_variables_const (lexer, dict,
273 ®ression.vars, ®ression.n_vars,
274 PV_NO_DUPLICATE | PV_NUMERIC))
278 else if (lex_match_id (lexer, "STATISTICS"))
280 unsigned long statistics = 0;
281 lex_match (lexer, T_EQUALS);
283 while (lex_token (lexer) != T_ENDCMD
284 && lex_token (lexer) != T_SLASH)
286 if (lex_match (lexer, T_ALL))
290 else if (lex_match_id (lexer, "DEFAULTS"))
292 statistics |= STATS_DEFAULT;
294 else if (lex_match_id (lexer, "R"))
296 statistics |= STATS_R;
298 else if (lex_match_id (lexer, "COEFF"))
300 statistics |= STATS_COEFF;
302 else if (lex_match_id (lexer, "ANOVA"))
304 statistics |= STATS_ANOVA;
306 else if (lex_match_id (lexer, "BCOV"))
308 statistics |= STATS_BCOV;
310 else if (lex_match_id (lexer, "CI"))
312 statistics |= STATS_CI;
314 if (lex_match (lexer, T_LPAREN) &&
315 lex_force_num (lexer))
317 regression.ci = lex_number (lexer) / 100.0;
319 if (! lex_force_match (lexer, T_RPAREN))
325 lex_error (lexer, NULL);
331 regression.stats = statistics;
334 else if (lex_match_id (lexer, "SAVE"))
336 lex_match (lexer, T_EQUALS);
338 while (lex_token (lexer) != T_ENDCMD
339 && lex_token (lexer) != T_SLASH)
341 if (lex_match_id (lexer, "PRED"))
343 regression.pred = true;
345 else if (lex_match_id (lexer, "RESID"))
347 regression.resid = true;
351 lex_error (lexer, NULL);
358 lex_error (lexer, NULL);
363 if (!regression.vars)
365 dict_get_vars (dict, ®ression.vars, ®ression.n_vars, 0);
368 save = regression.pred || regression.resid;
369 workspace.extras = 0;
370 workspace.res_idx = -1;
371 workspace.pred_idx = -1;
372 workspace.writer = NULL;
373 workspace.reader = NULL;
374 workspace.residvars = NULL;
375 workspace.predvars = NULL;
379 struct caseproto *proto = caseproto_create ();
381 if (regression.resid)
383 workspace.res_idx = workspace.extras ++;
384 workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
386 for (i = 0; i < regression.n_dep_vars; ++i)
388 workspace.residvars[i] = create_aux_var (ds, "RES");
389 proto = caseproto_add_width (proto, 0);
395 workspace.pred_idx = workspace.extras ++;
396 workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
398 for (i = 0; i < regression.n_dep_vars; ++i)
400 workspace.predvars[i] = create_aux_var (ds, "PRED");
401 proto = caseproto_add_width (proto, 0);
405 if (proc_make_temporary_transformations_permanent (ds))
406 msg (SW, _("REGRESSION with SAVE ignores TEMPORARY. "
407 "Temporary transformations will be made permanent."));
409 if (dict_get_filter (dict))
410 msg (SW, _("REGRESSION with SAVE ignores FILTER. "
411 "All cases will be processed."));
413 workspace.writer = autopaging_writer_create (proto);
414 caseproto_unref (proto);
419 struct casegrouper *grouper;
420 struct casereader *group;
423 grouper = casegrouper_create_splits (proc_open_filtering (ds, !save), dict);
426 while (casegrouper_get_next_group (grouper, &group))
428 run_regression (®ression,
433 ok = casegrouper_destroy (grouper);
434 ok = proc_commit (ds) && ok;
437 if (workspace.writer)
439 struct save_trans_data *save_trans_data = xmalloc (sizeof *save_trans_data);
440 struct casereader *r = casewriter_make_reader (workspace.writer);
441 workspace.writer = NULL;
442 workspace.reader = r;
443 save_trans_data->ws = xmalloc (sizeof (workspace));
444 memcpy (save_trans_data->ws, &workspace, sizeof (workspace));
445 save_trans_data->n_dep_vars = regression.n_dep_vars;
447 add_transformation (ds, save_trans_func, save_trans_free, save_trans_data);
451 free (regression.vars);
452 free (regression.dep_vars);
457 free (regression.vars);
458 free (regression.dep_vars);
462 /* Return the size of the union of dependent and independent variables */
464 get_n_all_vars (const struct regression *cmd)
466 size_t result = cmd->n_vars;
470 result += cmd->n_dep_vars;
471 for (i = 0; i < cmd->n_dep_vars; i++)
473 for (j = 0; j < cmd->n_vars; j++)
475 if (cmd->vars[j] == cmd->dep_vars[i])
484 /* Fill VARS with the union of dependent and independent variables */
486 fill_all_vars (const struct variable **vars, const struct regression *cmd)
490 for (i = 0; i < cmd->n_vars; i++)
492 vars[i] = cmd->vars[i];
495 for (i = 0; i < cmd->n_dep_vars; i++)
499 for (j = 0; j < cmd->n_vars; j++)
501 if (cmd->dep_vars[i] == cmd->vars[j])
509 vars[cmd->n_vars + x++] = cmd->dep_vars[i];
515 Is variable k the dependent variable?
518 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
520 return v == cmd->vars[k];
524 /* Identify the explanatory variables in v_variables. Returns
525 the number of independent variables. */
527 identify_indep_vars (const struct regression *cmd,
528 const struct variable **indep_vars,
529 const struct variable *depvar)
531 int n_indep_vars = 0;
534 for (i = 0; i < cmd->n_vars; i++)
535 if (!is_depvar (cmd, i, depvar))
536 indep_vars[n_indep_vars++] = cmd->vars[i];
537 if ((n_indep_vars < 1) && is_depvar (cmd, 0, depvar))
540 There is only one independent variable, and it is the same
541 as the dependent variable. Print a warning and continue.
545 ("The dependent variable is equal to the independent variable. "
546 "The least squares line is therefore Y=X. "
547 "Standard errors and related statistics may be meaningless."));
549 indep_vars[0] = cmd->vars[0];
555 fill_covariance (gsl_matrix * cov, struct covariance *all_cov,
556 const struct variable **vars,
557 size_t n_vars, const struct variable *dep_var,
558 const struct variable **all_vars, size_t n_all_vars,
563 size_t dep_subscript;
565 const gsl_matrix *ssizes;
566 const gsl_matrix *mean_matrix;
567 const gsl_matrix *ssize_matrix;
570 const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
575 rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
577 for (i = 0; i < n_all_vars; i++)
579 for (j = 0; j < n_vars; j++)
581 if (vars[j] == all_vars[i])
586 if (all_vars[i] == dep_var)
591 mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
592 ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
593 for (i = 0; i < cov->size1 - 1; i++)
595 means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
596 / gsl_matrix_get (ssize_matrix, rows[i], 0);
597 for (j = 0; j < cov->size2 - 1; j++)
599 gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
600 gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
603 means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
604 / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
605 ssizes = covariance_moments (all_cov, MOMENT_NONE);
606 result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
607 for (i = 0; i < cov->size1 - 1; i++)
609 gsl_matrix_set (cov, i, cov->size1 - 1,
610 gsl_matrix_get (cm, rows[i], dep_subscript));
611 gsl_matrix_set (cov, cov->size1 - 1, i,
612 gsl_matrix_get (cm, rows[i], dep_subscript));
613 if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
615 result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
618 gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
619 gsl_matrix_get (cm, dep_subscript, dep_subscript));
627 STATISTICS subcommand output functions.
629 static void reg_stats_r (const struct linreg *, const struct variable *);
630 static void reg_stats_coeff (const struct linreg *, const gsl_matrix *, const struct variable *, const struct regression *);
631 static void reg_stats_anova (const struct linreg *, const struct variable *);
632 static void reg_stats_bcov (const struct linreg *, const struct variable *);
636 subcommand_statistics (const struct regression *cmd, const struct linreg * c, const gsl_matrix * cm,
637 const struct variable *var)
639 if (cmd->stats & STATS_R)
640 reg_stats_r (c, var);
642 if (cmd->stats & STATS_ANOVA)
643 reg_stats_anova (c, var);
645 if (cmd->stats & STATS_COEFF)
646 reg_stats_coeff (c, cm, var, cmd);
648 if (cmd->stats & STATS_BCOV)
649 reg_stats_bcov (c, var);
654 run_regression (const struct regression *cmd,
655 struct regression_workspace *ws,
656 struct casereader *input)
659 struct linreg **models;
663 struct covariance *cov;
664 struct casereader *reader;
665 size_t n_all_vars = get_n_all_vars (cmd);
666 const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
668 double *means = xnmalloc (n_all_vars, sizeof (*means));
670 fill_all_vars (all_vars, cmd);
671 cov = covariance_1pass_create (n_all_vars, all_vars,
672 dict_get_weight (dataset_dict (cmd->ds)),
673 MV_ANY, cmd->origin == false);
675 reader = casereader_clone (input);
676 reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
681 struct casereader *r = casereader_clone (reader);
683 for (; (c = casereader_read (r)) != NULL; case_unref (c))
685 covariance_accumulate (cov, c);
687 casereader_destroy (r);
690 models = xcalloc (cmd->n_dep_vars, sizeof (*models));
691 for (k = 0; k < cmd->n_dep_vars; k++)
693 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
694 const struct variable *dep_var = cmd->dep_vars[k];
695 int n_indep = identify_indep_vars (cmd, vars, dep_var);
696 gsl_matrix *this_cm = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
697 double n_data = fill_covariance (this_cm, cov, vars, n_indep,
698 dep_var, all_vars, n_all_vars, means);
699 models[k] = linreg_alloc (dep_var, vars, n_data, n_indep, cmd->origin);
700 for (i = 0; i < n_indep; i++)
702 linreg_set_indep_variable_mean (models[k], i, means[i]);
704 linreg_set_depvar_mean (models[k], means[i]);
708 Find the least-squares estimates and other statistics.
710 linreg_fit (this_cm, models[k]);
712 if (!taint_has_tainted_successor (casereader_get_taint (input)))
714 subcommand_statistics (cmd, models[k], this_cm, dep_var);
719 msg (SE, _("No valid data found. This command was skipped."));
721 gsl_matrix_free (this_cm);
728 struct casereader *r = casereader_clone (reader);
730 for (; (c = casereader_read (r)) != NULL; case_unref (c))
732 struct ccase *outc = case_create (casewriter_get_proto (ws->writer));
733 for (k = 0; k < cmd->n_dep_vars; k++)
735 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
736 const struct variable *dep_var = cmd->dep_vars[k];
737 int n_indep = identify_indep_vars (cmd, vars, dep_var);
738 double *vals = xnmalloc (n_indep, sizeof (*vals));
739 for (i = 0; i < n_indep; i++)
741 const union value *tmp = case_data (c, vars[i]);
747 double pred = linreg_predict (models[k], vals, n_indep);
748 case_data_rw_idx (outc, k * ws->extras + ws->pred_idx)->f = pred;
753 double obs = case_data (c, linreg_dep_var (models[k]))->f;
754 double res = linreg_residual (models[k], obs, vals, n_indep);
755 case_data_rw_idx (outc, k * ws->extras + ws->res_idx)->f = res;
760 casewriter_write (ws->writer, outc);
762 casereader_destroy (r);
765 casereader_destroy (reader);
767 for (k = 0; k < cmd->n_dep_vars; k++)
769 linreg_unref (models[k]);
775 casereader_destroy (input);
776 covariance_destroy (cov);
783 reg_stats_r (const struct linreg * c, const struct variable *var)
793 rsq = linreg_ssreg (c) / linreg_sst (c);
795 (1.0 - rsq) * linreg_n_coeffs (c) / (linreg_n_obs (c) -
796 linreg_n_coeffs (c) - 1);
797 std_error = sqrt (linreg_mse (c));
798 t = tab_create (n_cols, n_rows);
799 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
800 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
801 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
802 tab_vline (t, TAL_0, 1, 0, 0);
804 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("R"));
805 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("R Square"));
806 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Adjusted R Square"));
807 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error of the Estimate"));
808 tab_double (t, 1, 1, TAB_RIGHT, sqrt (rsq), NULL, RC_OTHER);
809 tab_double (t, 2, 1, TAB_RIGHT, rsq, NULL, RC_OTHER);
810 tab_double (t, 3, 1, TAB_RIGHT, adjrsq, NULL, RC_OTHER);
811 tab_double (t, 4, 1, TAB_RIGHT, std_error, NULL, RC_OTHER);
812 tab_title (t, _("Model Summary (%s)"), var_to_string (var));
817 Table showing estimated regression coefficients.
820 reg_stats_coeff (const struct linreg * c, const gsl_matrix *cov, const struct variable *var, const struct regression *cmd)
824 const int heading_rows = 2;
826 int this_row = heading_rows;
832 const struct variable *v;
835 const double df = linreg_n_obs (c) - linreg_n_coeffs (c) - 1;
836 double q = (1 - cmd->ci) / 2.0; /* 2-tailed test */
837 double tval = gsl_cdf_tdist_Qinv (q, df);
840 n_rows = linreg_n_coeffs (c) + heading_rows + 1;
842 if (cmd->stats & STATS_CI)
845 t = tab_create (n_cols, n_rows);
846 tab_headers (t, 2, 0, 1, 0);
847 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
848 tab_hline (t, TAL_2, 0, n_cols - 1, heading_rows);
849 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
850 tab_vline (t, TAL_0, 1, 0, 0);
853 tab_hline (t, TAL_1, 2, 4, 1);
854 tab_joint_text (t, 2, 0, 3, 0, TAB_CENTER | TAT_TITLE, _("Unstandardized Coefficients"));
855 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("B"));
856 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
857 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Standardized Coefficients"));
858 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Beta"));
859 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("t"));
860 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
862 std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
864 if (cmd->stats & STATS_CI)
866 double lower = linreg_intercept (c) - tval * std_err ;
867 double upper = linreg_intercept (c) + tval * std_err ;
868 tab_double (t, 7, heading_rows, 0, lower, NULL, RC_OTHER);
869 tab_double (t, 8, heading_rows, 0, upper, NULL, RC_OTHER);
871 tab_joint_text_format (t, 7, 0, 8, 0, TAB_CENTER | TAT_TITLE, _("%g%% Confidence Interval for B"), cmd->ci * 100);
872 tab_hline (t, TAL_1, 7, 8, 1);
873 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
874 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
879 tab_text (t, 1, this_row, TAB_LEFT | TAT_TITLE, _("(Constant)"));
880 tab_double (t, 2, this_row, 0, linreg_intercept (c), NULL, RC_OTHER);
881 tab_double (t, 3, this_row, 0, std_err, NULL, RC_OTHER);
882 tab_double (t, 4, this_row, 0, 0.0, NULL, RC_OTHER);
883 double t_stat = linreg_intercept (c) / std_err;
884 tab_double (t, 5, this_row, 0, t_stat, NULL, RC_OTHER);
887 2 * gsl_cdf_tdist_Q (fabs (t_stat),
888 (double) (linreg_n_obs (c) - linreg_n_coeffs (c)));
889 tab_double (t, 6, this_row, 0, pval, NULL, RC_PVALUE);
893 for (j = 0; j < linreg_n_coeffs (c); j++, this_row++)
896 ds_init_empty (&tstr);
898 v = linreg_indep_var (c, j);
899 label = var_to_string (v);
900 /* Do not overwrite the variable's name. */
901 ds_put_cstr (&tstr, label);
902 tab_text (t, 1, this_row, TAB_LEFT, ds_cstr (&tstr));
904 Regression coefficients.
906 tab_double (t, 2, this_row, 0, linreg_coeff (c, j), NULL, RC_OTHER);
908 Standard error of the coefficients.
910 std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
911 tab_double (t, 3, this_row, 0, std_err, NULL, RC_OTHER);
913 Standardized coefficient, i.e., regression coefficient
914 if all variables had unit variance.
916 beta = sqrt (gsl_matrix_get (cov, j, j));
917 beta *= linreg_coeff (c, j) /
918 sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1));
919 tab_double (t, 4, this_row, 0, beta, NULL, RC_OTHER);
922 Test statistic for H0: coefficient is 0.
924 double t_stat = linreg_coeff (c, j) / std_err;
925 tab_double (t, 5, this_row, 0, t_stat, NULL, RC_OTHER);
927 P values for the test statistic above.
929 pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), df);
930 tab_double (t, 6, this_row, 0, pval, NULL, RC_PVALUE);
933 if (cmd->stats & STATS_CI)
935 double lower = linreg_coeff (c, j) - tval * std_err ;
936 double upper = linreg_coeff (c, j) + tval * std_err ;
938 tab_double (t, 7, this_row, 0, lower, NULL, RC_OTHER);
939 tab_double (t, 8, this_row, 0, upper, NULL, RC_OTHER);
942 tab_title (t, _("Coefficients (%s)"), var_to_string (var));
947 Display the ANOVA table.
950 reg_stats_anova (const struct linreg * c, const struct variable *var)
954 const double msm = linreg_ssreg (c) / linreg_dfmodel (c);
955 const double mse = linreg_mse (c);
956 const double F = msm / mse;
957 const double pval = gsl_cdf_fdist_Q (F, linreg_dfmodel (c),
963 t = tab_create (n_cols, n_rows);
964 tab_headers (t, 2, 0, 1, 0);
966 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
968 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
969 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
970 tab_vline (t, TAL_0, 1, 0, 0);
972 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
973 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
974 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
975 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
976 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
978 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Regression"));
979 tab_text (t, 1, 2, TAB_LEFT | TAT_TITLE, _("Residual"));
980 tab_text (t, 1, 3, TAB_LEFT | TAT_TITLE, _("Total"));
982 /* Sums of Squares */
983 tab_double (t, 2, 1, 0, linreg_ssreg (c), NULL, RC_OTHER);
984 tab_double (t, 2, 3, 0, linreg_sst (c), NULL, RC_OTHER);
985 tab_double (t, 2, 2, 0, linreg_sse (c), NULL, RC_OTHER);
988 /* Degrees of freedom */
989 tab_text_format (t, 3, 1, TAB_RIGHT, "%.*g", DBL_DIG + 1, linreg_dfmodel (c));
990 tab_text_format (t, 3, 2, TAB_RIGHT, "%.*g", DBL_DIG + 1, linreg_dferror (c));
991 tab_text_format (t, 3, 3, TAB_RIGHT, "%.*g", DBL_DIG + 1, linreg_dftotal (c));
994 tab_double (t, 4, 1, TAB_RIGHT, msm, NULL, RC_OTHER);
995 tab_double (t, 4, 2, TAB_RIGHT, mse, NULL, RC_OTHER);
997 tab_double (t, 5, 1, 0, F, NULL, RC_OTHER);
999 tab_double (t, 6, 1, 0, pval, NULL, RC_PVALUE);
1001 tab_title (t, _("ANOVA (%s)"), var_to_string (var));
1007 reg_stats_bcov (const struct linreg * c, const struct variable *var)
1016 struct tab_table *t;
1019 n_cols = linreg_n_indeps (c) + 1 + 2;
1020 n_rows = 2 * (linreg_n_indeps (c) + 1);
1021 t = tab_create (n_cols, n_rows);
1022 tab_headers (t, 2, 0, 1, 0);
1023 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
1024 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1025 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1026 tab_vline (t, TAL_0, 1, 0, 0);
1027 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Model"));
1028 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Covariances"));
1029 for (i = 0; i < linreg_n_coeffs (c); i++)
1031 const struct variable *v = linreg_indep_var (c, i);
1032 label = var_to_string (v);
1033 tab_text (t, 2, i, TAB_CENTER, label);
1034 tab_text (t, i + 2, 0, TAB_CENTER, label);
1035 for (k = 1; k < linreg_n_coeffs (c); k++)
1037 col = (i <= k) ? k : i;
1038 row = (i <= k) ? i : k;
1039 tab_double (t, k + 2, i, TAB_CENTER,
1040 gsl_matrix_get (linreg_cov (c), row, col), NULL, RC_OTHER);
1043 tab_title (t, _("Coefficient Correlations (%s)"), var_to_string (var));