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_cdf.h>
24 #include <gsl/gsl_matrix.h>
26 #include <data/dataset.h>
27 #include <data/casewriter.h>
29 #include "language/command.h"
30 #include "language/lexer/lexer.h"
31 #include "language/lexer/value-parser.h"
32 #include "language/lexer/variable-parser.h"
35 #include "data/casegrouper.h"
36 #include "data/casereader.h"
37 #include "data/dictionary.h"
39 #include "math/covariance.h"
40 #include "math/linreg.h"
41 #include "math/moments.h"
43 #include "libpspp/message.h"
44 #include "libpspp/taint.h"
46 #include "output/tab.h"
49 #define _(msgid) gettext (msgid)
50 #define N_(msgid) msgid
53 #include <gl/intprops.h>
55 #define REG_LARGE_DATA 1000
64 #define STATS_DEFAULT (STATS_R | STATS_COEFF | STATS_ANOVA | STATS_OUTS)
72 const struct variable **vars;
75 const struct variable **dep_vars;
87 struct regression_workspace
89 /* The new variables which will be introduced by /SAVE */
90 const struct variable **predvars;
91 const struct variable **residvars;
93 /* A reader/writer pair to temporarily hold the
94 values of the new variables */
95 struct casewriter *writer;
96 struct casereader *reader;
98 /* Indeces of the new values in the reader/writer (-1 if not applicable) */
102 /* 0, 1 or 2 depending on what new variables are to be created */
106 static void run_regression (const struct regression *cmd,
107 struct regression_workspace *ws,
108 struct casereader *input);
111 /* Return a string based on PREFIX which may be used as the name
112 of a new variable in DICT */
114 reg_get_name (const struct dictionary *dict, const char *prefix)
119 /* XXX handle too-long prefixes */
120 name = xmalloc (strlen (prefix) + INT_BUFSIZE_BOUND (i) + 1);
123 sprintf (name, "%s%d", prefix, i);
124 if (dict_lookup_var (dict, name) == NULL)
130 static const struct variable *
131 create_aux_var (struct dataset *ds, const char *prefix)
133 struct variable *var;
134 struct dictionary *dict = dataset_dict (ds);
135 char *name = reg_get_name (dict, prefix);
136 var = dict_create_var_assert (dict, name, 0);
141 /* Auxilliary data for transformation when /SAVE is entered */
142 struct save_trans_data
145 struct regression_workspace *ws;
149 save_trans_free (void *aux)
151 struct save_trans_data *save_trans_data = aux;
152 free (save_trans_data->ws->predvars);
153 free (save_trans_data->ws->residvars);
155 casereader_destroy (save_trans_data->ws->reader);
156 free (save_trans_data->ws);
157 free (save_trans_data);
162 save_trans_func (void *aux, struct ccase **c, casenumber x UNUSED)
164 struct save_trans_data *save_trans_data = aux;
165 struct regression_workspace *ws = save_trans_data->ws;
166 struct ccase *in = casereader_read (ws->reader);
171 *c = case_unshare (*c);
173 for (k = 0; k < save_trans_data->n_dep_vars; ++k)
175 if (ws->pred_idx != -1)
177 double pred = case_data_idx (in, ws->extras * k + ws->pred_idx)->f;
178 case_data_rw (*c, ws->predvars[k])->f = pred;
181 if (ws->res_idx != -1)
183 double resid = case_data_idx (in, ws->extras * k + ws->res_idx)->f;
184 case_data_rw (*c, ws->residvars[k])->f = resid;
190 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, "CI"))
313 statistics |= STATS_CI;
315 if (lex_match (lexer, T_LPAREN) &&
316 lex_force_num (lexer))
318 regression.ci = lex_number (lexer) / 100.0;
320 if (! lex_force_match (lexer, T_RPAREN))
326 lex_error (lexer, NULL);
332 regression.stats = statistics;
335 else if (lex_match_id (lexer, "SAVE"))
337 lex_match (lexer, T_EQUALS);
339 while (lex_token (lexer) != T_ENDCMD
340 && lex_token (lexer) != T_SLASH)
342 if (lex_match_id (lexer, "PRED"))
344 regression.pred = true;
346 else if (lex_match_id (lexer, "RESID"))
348 regression.resid = true;
352 lex_error (lexer, NULL);
359 lex_error (lexer, NULL);
364 if (!regression.vars)
366 dict_get_vars (dict, ®ression.vars, ®ression.n_vars, 0);
369 save = regression.pred || regression.resid;
370 workspace.extras = 0;
371 workspace.res_idx = -1;
372 workspace.pred_idx = -1;
373 workspace.writer = NULL;
374 workspace.reader = NULL;
375 workspace.residvars = NULL;
376 workspace.predvars = NULL;
380 struct caseproto *proto = caseproto_create ();
382 if (regression.resid)
384 workspace.res_idx = workspace.extras ++;
385 workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
387 for (i = 0; i < regression.n_dep_vars; ++i)
389 workspace.residvars[i] = create_aux_var (ds, "RES");
390 proto = caseproto_add_width (proto, 0);
396 workspace.pred_idx = workspace.extras ++;
397 workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
399 for (i = 0; i < regression.n_dep_vars; ++i)
401 workspace.predvars[i] = create_aux_var (ds, "PRED");
402 proto = caseproto_add_width (proto, 0);
406 if (proc_make_temporary_transformations_permanent (ds))
407 msg (SW, _("REGRESSION with SAVE ignores TEMPORARY. "
408 "Temporary transformations will be made permanent."));
410 if (dict_get_filter (dict))
411 msg (SW, _("REGRESSION with SAVE ignores FILTER. "
412 "All cases will be processed."));
414 workspace.writer = autopaging_writer_create (proto);
415 caseproto_unref (proto);
420 struct casegrouper *grouper;
421 struct casereader *group;
424 grouper = casegrouper_create_splits (proc_open_filtering (ds, !save), dict);
427 while (casegrouper_get_next_group (grouper, &group))
429 run_regression (®ression,
434 ok = casegrouper_destroy (grouper);
435 ok = proc_commit (ds) && ok;
438 if (workspace.writer)
440 struct save_trans_data *save_trans_data = xmalloc (sizeof *save_trans_data);
441 struct casereader *r = casewriter_make_reader (workspace.writer);
442 workspace.writer = NULL;
443 workspace.reader = r;
444 save_trans_data->ws = xmalloc (sizeof (workspace));
445 memcpy (save_trans_data->ws, &workspace, sizeof (workspace));
446 save_trans_data->n_dep_vars = regression.n_dep_vars;
448 add_transformation (ds, save_trans_func, save_trans_free, save_trans_data);
452 free (regression.vars);
453 free (regression.dep_vars);
458 free (regression.vars);
459 free (regression.dep_vars);
463 /* Return the size of the union of dependent and independent variables */
465 get_n_all_vars (const struct regression *cmd)
467 size_t result = cmd->n_vars;
471 result += cmd->n_dep_vars;
472 for (i = 0; i < cmd->n_dep_vars; i++)
474 for (j = 0; j < cmd->n_vars; j++)
476 if (cmd->vars[j] == cmd->dep_vars[i])
485 /* Fill VARS with the union of dependent and independent variables */
487 fill_all_vars (const struct variable **vars, const struct regression *cmd)
491 for (i = 0; i < cmd->n_vars; i++)
493 vars[i] = cmd->vars[i];
496 for (i = 0; i < cmd->n_dep_vars; i++)
500 for (j = 0; j < cmd->n_vars; j++)
502 if (cmd->dep_vars[i] == cmd->vars[j])
510 vars[cmd->n_vars + x++] = cmd->dep_vars[i];
516 Is variable k the dependent variable?
519 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
521 return v == cmd->vars[k];
525 /* Identify the explanatory variables in v_variables. Returns
526 the number of independent variables. */
528 identify_indep_vars (const struct regression *cmd,
529 const struct variable **indep_vars,
530 const struct variable *depvar)
532 int n_indep_vars = 0;
535 for (i = 0; i < cmd->n_vars; i++)
536 if (!is_depvar (cmd, i, depvar))
537 indep_vars[n_indep_vars++] = cmd->vars[i];
538 if ((n_indep_vars < 1) && is_depvar (cmd, 0, depvar))
541 There is only one independent variable, and it is the same
542 as the dependent variable. Print a warning and continue.
546 ("The dependent variable is equal to the independent variable. "
547 "The least squares line is therefore Y=X. "
548 "Standard errors and related statistics may be meaningless."));
550 indep_vars[0] = cmd->vars[0];
557 fill_covariance (gsl_matrix * cov, struct covariance *all_cov,
558 const struct variable **vars,
559 size_t n_vars, const struct variable *dep_var,
560 const struct variable **all_vars, size_t n_all_vars,
565 size_t dep_subscript;
567 const gsl_matrix *ssizes;
568 const gsl_matrix *mean_matrix;
569 const gsl_matrix *ssize_matrix;
572 const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
577 rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
579 for (i = 0; i < n_all_vars; i++)
581 for (j = 0; j < n_vars; j++)
583 if (vars[j] == all_vars[i])
588 if (all_vars[i] == dep_var)
593 mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
594 ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
595 for (i = 0; i < cov->size1 - 1; i++)
597 means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
598 / gsl_matrix_get (ssize_matrix, rows[i], 0);
599 for (j = 0; j < cov->size2 - 1; j++)
601 gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
602 gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
605 means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
606 / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
607 ssizes = covariance_moments (all_cov, MOMENT_NONE);
608 result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
609 for (i = 0; i < cov->size1 - 1; i++)
611 gsl_matrix_set (cov, i, cov->size1 - 1,
612 gsl_matrix_get (cm, rows[i], dep_subscript));
613 gsl_matrix_set (cov, cov->size1 - 1, i,
614 gsl_matrix_get (cm, rows[i], dep_subscript));
615 if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
617 result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
620 gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
621 gsl_matrix_get (cm, dep_subscript, dep_subscript));
629 STATISTICS subcommand output functions.
631 static void reg_stats_r (const linreg *, const struct variable *);
632 static void reg_stats_coeff (const linreg *, const gsl_matrix *, const struct variable *, const struct regression *);
633 static void reg_stats_anova (const linreg *, const struct variable *);
634 static void reg_stats_bcov (const linreg *, const struct variable *);
638 subcommand_statistics (const struct regression *cmd, const linreg * c, const gsl_matrix * cm,
639 const struct variable *var)
641 if (cmd->stats & STATS_R)
642 reg_stats_r (c, var);
644 if (cmd->stats & STATS_ANOVA)
645 reg_stats_anova (c, var);
647 if (cmd->stats & STATS_COEFF)
648 reg_stats_coeff (c, cm, var, cmd);
650 if (cmd->stats & STATS_BCOV)
651 reg_stats_bcov (c, var);
656 run_regression (const struct regression *cmd,
657 struct regression_workspace *ws,
658 struct casereader *input)
665 struct covariance *cov;
666 struct casereader *reader;
667 size_t n_all_vars = get_n_all_vars (cmd);
668 const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
670 double *means = xnmalloc (n_all_vars, sizeof (*means));
672 fill_all_vars (all_vars, cmd);
673 cov = covariance_1pass_create (n_all_vars, all_vars,
674 dict_get_weight (dataset_dict (cmd->ds)),
675 MV_ANY, cmd->origin == false);
677 reader = casereader_clone (input);
678 reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
683 struct casereader *r = casereader_clone (reader);
685 for (; (c = casereader_read (r)) != NULL; case_unref (c))
687 covariance_accumulate (cov, c);
689 casereader_destroy (r);
692 models = xcalloc (cmd->n_dep_vars, sizeof (*models));
693 for (k = 0; k < cmd->n_dep_vars; k++)
695 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
696 const struct variable *dep_var = cmd->dep_vars[k];
697 int n_indep = identify_indep_vars (cmd, vars, dep_var);
698 gsl_matrix *this_cm = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
699 double n_data = fill_covariance (this_cm, cov, vars, n_indep,
700 dep_var, all_vars, n_all_vars, means);
701 models[k] = linreg_alloc (dep_var, vars, n_data, n_indep, cmd->origin);
702 models[k]->depvar = dep_var;
703 for (i = 0; i < n_indep; i++)
705 linreg_set_indep_variable_mean (models[k], i, means[i]);
707 linreg_set_depvar_mean (models[k], means[i]);
709 For large data sets, use QR decomposition.
711 if (n_data > sqrt (n_indep) && n_data > REG_LARGE_DATA)
713 models[k]->method = LINREG_QR;
719 Find the least-squares estimates and other statistics.
721 linreg_fit (this_cm, models[k]);
723 if (!taint_has_tainted_successor (casereader_get_taint (input)))
725 subcommand_statistics (cmd, models[k], this_cm, dep_var);
730 msg (SE, _("No valid data found. This command was skipped."));
732 gsl_matrix_free (this_cm);
739 struct casereader *r = casereader_clone (reader);
741 for (; (c = casereader_read (r)) != NULL; case_unref (c))
743 struct ccase *outc = case_create (casewriter_get_proto (ws->writer));
744 for (k = 0; k < cmd->n_dep_vars; k++)
746 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
747 const struct variable *dep_var = cmd->dep_vars[k];
748 int n_indep = identify_indep_vars (cmd, vars, dep_var);
749 double *vals = xnmalloc (n_indep, sizeof (*vals));
750 for (i = 0; i < n_indep; i++)
752 const union value *tmp = case_data (c, vars[i]);
758 double pred = linreg_predict (models[k], vals, n_indep);
759 case_data_rw_idx (outc, k * ws->extras + ws->pred_idx)->f = pred;
764 double obs = case_data (c, models[k]->depvar)->f;
765 double res = linreg_residual (models[k], obs, vals, n_indep);
766 case_data_rw_idx (outc, k * ws->extras + ws->res_idx)->f = res;
771 casewriter_write (ws->writer, outc);
773 casereader_destroy (r);
776 casereader_destroy (reader);
778 for (k = 0; k < cmd->n_dep_vars; k++)
780 linreg_unref (models[k]);
786 casereader_destroy (input);
787 covariance_destroy (cov);
794 reg_stats_r (const linreg * c, const struct variable *var)
804 rsq = linreg_ssreg (c) / linreg_sst (c);
806 (1.0 - rsq) * linreg_n_coeffs (c) / (linreg_n_obs (c) -
807 linreg_n_coeffs (c) - 1);
808 std_error = sqrt (linreg_mse (c));
809 t = tab_create (n_cols, n_rows);
810 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
811 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
812 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
813 tab_vline (t, TAL_0, 1, 0, 0);
815 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("R"));
816 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("R Square"));
817 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Adjusted R Square"));
818 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error of the Estimate"));
819 tab_double (t, 1, 1, TAB_RIGHT, sqrt (rsq), NULL, RC_OTHER);
820 tab_double (t, 2, 1, TAB_RIGHT, rsq, NULL, RC_OTHER);
821 tab_double (t, 3, 1, TAB_RIGHT, adjrsq, NULL, RC_OTHER);
822 tab_double (t, 4, 1, TAB_RIGHT, std_error, NULL, RC_OTHER);
823 tab_title (t, _("Model Summary (%s)"), var_to_string (var));
828 Table showing estimated regression coefficients.
831 reg_stats_coeff (const linreg * c, const gsl_matrix *cov, const struct variable *var, const struct regression *cmd)
835 const int heading_rows = 2;
837 int this_row = heading_rows;
843 const struct variable *v;
846 const double df = linreg_n_obs (c) - linreg_n_coeffs (c) - 1;
847 double q = (1 - cmd->ci) / 2.0; /* 2-tailed test */
848 double tval = gsl_cdf_tdist_Qinv (q, df);
851 n_rows = linreg_n_coeffs (c) + heading_rows + 1;
853 if (cmd->stats & STATS_CI)
856 t = tab_create (n_cols, n_rows);
857 tab_headers (t, 2, 0, 1, 0);
858 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
859 tab_hline (t, TAL_2, 0, n_cols - 1, heading_rows);
860 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
861 tab_vline (t, TAL_0, 1, 0, 0);
864 tab_hline (t, TAL_1, 2, 4, 1);
865 tab_joint_text (t, 2, 0, 3, 0, TAB_CENTER | TAT_TITLE, _("Unstandardized Coefficients"));
866 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("B"));
867 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
868 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Standardized Coefficients"));
869 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Beta"));
870 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("t"));
871 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
873 std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
875 if (cmd->stats & STATS_CI)
877 double lower = linreg_intercept (c) - tval * std_err ;
878 double upper = linreg_intercept (c) + tval * std_err ;
879 tab_double (t, 7, heading_rows, 0, lower, NULL, RC_OTHER);
880 tab_double (t, 8, heading_rows, 0, upper, NULL, RC_OTHER);
882 tab_joint_text_format (t, 7, 0, 8, 0, TAB_CENTER | TAT_TITLE, _("%g%% Confidence Interval for B"), cmd->ci * 100);
883 tab_hline (t, TAL_1, 7, 8, 1);
884 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
885 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
890 tab_text (t, 1, this_row, TAB_LEFT | TAT_TITLE, _("(Constant)"));
891 tab_double (t, 2, this_row, 0, linreg_intercept (c), NULL, RC_OTHER);
892 tab_double (t, 3, this_row, 0, std_err, NULL, RC_OTHER);
893 tab_double (t, 4, this_row, 0, 0.0, NULL, RC_OTHER);
894 double t_stat = linreg_intercept (c) / std_err;
895 tab_double (t, 5, this_row, 0, t_stat, NULL, RC_OTHER);
898 2 * gsl_cdf_tdist_Q (fabs (t_stat),
899 (double) (linreg_n_obs (c) - linreg_n_coeffs (c)));
900 tab_double (t, 6, this_row, 0, pval, NULL, RC_PVALUE);
904 for (j = 0; j < linreg_n_coeffs (c); j++, this_row++)
907 ds_init_empty (&tstr);
909 v = linreg_indep_var (c, j);
910 label = var_to_string (v);
911 /* Do not overwrite the variable's name. */
912 ds_put_cstr (&tstr, label);
913 tab_text (t, 1, this_row, TAB_LEFT, ds_cstr (&tstr));
915 Regression coefficients.
917 tab_double (t, 2, this_row, 0, linreg_coeff (c, j), NULL, RC_OTHER);
919 Standard error of the coefficients.
921 std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
922 tab_double (t, 3, this_row, 0, std_err, NULL, RC_OTHER);
924 Standardized coefficient, i.e., regression coefficient
925 if all variables had unit variance.
927 beta = sqrt (gsl_matrix_get (cov, j, j));
928 beta *= linreg_coeff (c, j) /
929 sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1));
930 tab_double (t, 4, this_row, 0, beta, NULL, RC_OTHER);
933 Test statistic for H0: coefficient is 0.
935 double t_stat = linreg_coeff (c, j) / std_err;
936 tab_double (t, 5, this_row, 0, t_stat, NULL, RC_OTHER);
938 P values for the test statistic above.
940 pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), df);
941 tab_double (t, 6, this_row, 0, pval, NULL, RC_PVALUE);
944 if (cmd->stats & STATS_CI)
946 double lower = linreg_coeff (c, j) - tval * std_err ;
947 double upper = linreg_coeff (c, j) + tval * std_err ;
949 tab_double (t, 7, this_row, 0, lower, NULL, RC_OTHER);
950 tab_double (t, 8, this_row, 0, upper, NULL, RC_OTHER);
953 tab_title (t, _("Coefficients (%s)"), var_to_string (var));
958 Display the ANOVA table.
961 reg_stats_anova (const linreg * c, const struct variable *var)
965 const double msm = linreg_ssreg (c) / linreg_dfmodel (c);
966 const double mse = linreg_mse (c);
967 const double F = msm / mse;
968 const double pval = gsl_cdf_fdist_Q (F, c->dfm, c->dfe);
973 t = tab_create (n_cols, n_rows);
974 tab_headers (t, 2, 0, 1, 0);
976 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
978 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
979 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
980 tab_vline (t, TAL_0, 1, 0, 0);
982 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
983 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
984 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
985 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
986 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
988 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Regression"));
989 tab_text (t, 1, 2, TAB_LEFT | TAT_TITLE, _("Residual"));
990 tab_text (t, 1, 3, TAB_LEFT | TAT_TITLE, _("Total"));
992 /* Sums of Squares */
993 tab_double (t, 2, 1, 0, linreg_ssreg (c), NULL, RC_OTHER);
994 tab_double (t, 2, 3, 0, linreg_sst (c), NULL, RC_OTHER);
995 tab_double (t, 2, 2, 0, linreg_sse (c), NULL, RC_OTHER);
998 /* Degrees of freedom */
999 tab_text_format (t, 3, 1, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dfm);
1000 tab_text_format (t, 3, 2, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dfe);
1001 tab_text_format (t, 3, 3, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dft);
1004 tab_double (t, 4, 1, TAB_RIGHT, msm, NULL, RC_OTHER);
1005 tab_double (t, 4, 2, TAB_RIGHT, mse, NULL, RC_OTHER);
1007 tab_double (t, 5, 1, 0, F, NULL, RC_OTHER);
1009 tab_double (t, 6, 1, 0, pval, NULL, RC_PVALUE);
1011 tab_title (t, _("ANOVA (%s)"), var_to_string (var));
1017 reg_stats_bcov (const linreg * c, const struct variable *var)
1026 struct tab_table *t;
1029 n_cols = c->n_indeps + 1 + 2;
1030 n_rows = 2 * (c->n_indeps + 1);
1031 t = tab_create (n_cols, n_rows);
1032 tab_headers (t, 2, 0, 1, 0);
1033 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
1034 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
1035 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1036 tab_vline (t, TAL_0, 1, 0, 0);
1037 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Model"));
1038 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Covariances"));
1039 for (i = 0; i < linreg_n_coeffs (c); i++)
1041 const struct variable *v = linreg_indep_var (c, i);
1042 label = var_to_string (v);
1043 tab_text (t, 2, i, TAB_CENTER, label);
1044 tab_text (t, i + 2, 0, TAB_CENTER, label);
1045 for (k = 1; k < linreg_n_coeffs (c); k++)
1047 col = (i <= k) ? k : i;
1048 row = (i <= k) ? i : k;
1049 tab_double (t, k + 2, i, TAB_CENTER,
1050 gsl_matrix_get (c->cov, row, col), NULL, RC_OTHER);
1053 tab_title (t, _("Coefficient Correlations (%s)"), var_to_string (var));