1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2005, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <gsl/gsl_cdf.h>
23 #include <gsl/gsl_matrix.h>
25 #include <data/dataset.h>
26 #include <data/casewriter.h>
28 #include "language/command.h"
29 #include "language/lexer/lexer.h"
30 #include "language/lexer/value-parser.h"
31 #include "language/lexer/variable-parser.h"
34 #include "data/casegrouper.h"
35 #include "data/casereader.h"
36 #include "data/dictionary.h"
38 #include "math/covariance.h"
39 #include "math/linreg.h"
40 #include "math/moments.h"
42 #include "libpspp/message.h"
43 #include "libpspp/taint.h"
45 #include "output/tab.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
52 #include <gl/intprops.h>
54 #define REG_LARGE_DATA 1000
63 #define STATS_DEFAULT (STATS_R | STATS_COEFF | STATS_ANOVA | STATS_OUTS)
71 const struct variable **vars;
74 const struct variable **dep_vars;
84 struct regression_workspace
86 /* The new variables which will be introduced by /SAVE */
87 const struct variable **predvars;
88 const struct variable **residvars;
90 /* A reader/writer pair to temporarily hold the
91 values of the new variables */
92 struct casewriter *writer;
93 struct casereader *reader;
95 /* Indeces of the new values in the reader/writer (-1 if not applicable) */
99 /* 0, 1 or 2 depending on what new variables are to be created */
103 static void run_regression (const struct regression *cmd,
104 struct regression_workspace *ws,
105 struct casereader *input);
108 /* Return a string based on PREFIX which may be used as the name
109 of a new variable in DICT */
111 reg_get_name (const struct dictionary *dict, const char *prefix)
116 /* XXX handle too-long prefixes */
117 name = xmalloc (strlen (prefix) + INT_BUFSIZE_BOUND (i) + 1);
120 sprintf (name, "%s%d", prefix, i);
121 if (dict_lookup_var (dict, name) == NULL)
127 static const struct variable *
128 create_aux_var (struct dataset *ds, const char *prefix)
130 struct variable *var;
131 struct dictionary *dict = dataset_dict (ds);
132 char *name = reg_get_name (dict, prefix);
133 var = dict_create_var_assert (dict, name, 0);
138 /* Auxilliary data for transformation when /SAVE is entered */
139 struct save_trans_data
142 struct regression_workspace *ws;
146 save_trans_free (void *aux)
148 struct save_trans_data *save_trans_data = aux;
149 free (save_trans_data->ws->predvars);
150 free (save_trans_data->ws->residvars);
152 casereader_destroy (save_trans_data->ws->reader);
153 free (save_trans_data->ws);
154 free (save_trans_data);
159 save_trans_func (void *aux, struct ccase **c, casenumber x UNUSED)
161 struct save_trans_data *save_trans_data = aux;
162 struct regression_workspace *ws = save_trans_data->ws;
163 struct ccase *in = casereader_read (ws->reader);
168 *c = case_unshare (*c);
170 for (k = 0; k < save_trans_data->n_dep_vars; ++k)
172 if (ws->pred_idx != -1)
174 double pred = case_data_idx (in, ws->extras * k + ws->pred_idx)->f;
175 case_data_rw (*c, ws->predvars[k])->f = pred;
178 if (ws->res_idx != -1)
180 double resid = case_data_idx (in, ws->extras * k + ws->res_idx)->f;
181 case_data_rw (*c, ws->residvars[k])->f = resid;
187 return TRNS_CONTINUE;
192 cmd_regression (struct lexer *lexer, struct dataset *ds)
194 struct regression_workspace workspace;
195 struct regression regression;
196 const struct dictionary *dict = dataset_dict (ds);
199 memset (®ression, 0, sizeof (struct regression));
201 regression.ci = 0.95;
202 regression.stats = STATS_DEFAULT;
203 regression.pred = false;
204 regression.resid = false;
208 /* Accept an optional, completely pointless "/VARIABLES=" */
209 lex_match (lexer, T_SLASH);
210 if (lex_match_id (lexer, "VARIABLES"))
212 if (!lex_force_match (lexer, T_EQUALS))
216 if (!parse_variables_const (lexer, dict,
217 ®ression.vars, ®ression.n_vars,
218 PV_NO_DUPLICATE | PV_NUMERIC))
222 while (lex_token (lexer) != T_ENDCMD)
224 lex_match (lexer, T_SLASH);
226 if (lex_match_id (lexer, "DEPENDENT"))
228 lex_match (lexer, T_EQUALS);
230 free (regression.dep_vars);
231 regression.n_dep_vars = 0;
233 if (!parse_variables_const (lexer, dict,
234 ®ression.dep_vars,
235 ®ression.n_dep_vars,
236 PV_NO_DUPLICATE | PV_NUMERIC))
239 else if (lex_match_id (lexer, "METHOD"))
241 lex_match (lexer, T_EQUALS);
243 if (!lex_force_match_id (lexer, "ENTER"))
248 else if (lex_match_id (lexer, "STATISTICS"))
250 unsigned long statistics = 0;
251 lex_match (lexer, T_EQUALS);
253 while (lex_token (lexer) != T_ENDCMD
254 && lex_token (lexer) != T_SLASH)
256 if (lex_match (lexer, T_ALL))
260 else if (lex_match_id (lexer, "DEFAULTS"))
262 statistics |= STATS_DEFAULT;
264 else if (lex_match_id (lexer, "R"))
266 statistics |= STATS_R;
268 else if (lex_match_id (lexer, "COEFF"))
270 statistics |= STATS_COEFF;
272 else if (lex_match_id (lexer, "ANOVA"))
274 statistics |= STATS_ANOVA;
276 else if (lex_match_id (lexer, "BCOV"))
278 statistics |= STATS_BCOV;
280 else if (lex_match_id (lexer, "CI"))
282 statistics |= STATS_CI;
284 if (lex_match (lexer, T_LPAREN) &&
285 lex_force_num (lexer))
287 regression.ci = lex_number (lexer) / 100.0;
289 if (! lex_force_match (lexer, T_RPAREN))
295 lex_error (lexer, NULL);
301 regression.stats = statistics;
304 else if (lex_match_id (lexer, "SAVE"))
306 lex_match (lexer, T_EQUALS);
308 while (lex_token (lexer) != T_ENDCMD
309 && lex_token (lexer) != T_SLASH)
311 if (lex_match_id (lexer, "PRED"))
313 regression.pred = true;
315 else if (lex_match_id (lexer, "RESID"))
317 regression.resid = true;
321 lex_error (lexer, NULL);
328 lex_error (lexer, NULL);
333 if (!regression.vars)
335 dict_get_vars (dict, ®ression.vars, ®ression.n_vars, 0);
338 save = regression.pred || regression.resid;
339 workspace.extras = 0;
340 workspace.res_idx = -1;
341 workspace.pred_idx = -1;
342 workspace.writer = NULL;
343 workspace.reader = NULL;
344 workspace.residvars = NULL;
345 workspace.predvars = NULL;
349 struct caseproto *proto = caseproto_create ();
351 if (regression.resid)
353 workspace.res_idx = workspace.extras ++;
354 workspace.residvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.residvars));
356 for (i = 0; i < regression.n_dep_vars; ++i)
358 workspace.residvars[i] = create_aux_var (ds, "RES");
359 proto = caseproto_add_width (proto, 0);
365 workspace.pred_idx = workspace.extras ++;
366 workspace.predvars = xcalloc (regression.n_dep_vars, sizeof (*workspace.predvars));
368 for (i = 0; i < regression.n_dep_vars; ++i)
370 workspace.predvars[i] = create_aux_var (ds, "PRED");
371 proto = caseproto_add_width (proto, 0);
375 if (proc_make_temporary_transformations_permanent (ds))
376 msg (SW, _("REGRESSION with SAVE ignores TEMPORARY. "
377 "Temporary transformations will be made permanent."));
379 if (dict_get_filter (dict))
380 msg (SW, _("REGRESSION with SAVE ignores FILTER. "
381 "All cases will be processed."));
383 workspace.writer = autopaging_writer_create (proto);
384 caseproto_unref (proto);
389 struct casegrouper *grouper;
390 struct casereader *group;
393 grouper = casegrouper_create_splits (proc_open_filtering (ds, !save), dict);
396 while (casegrouper_get_next_group (grouper, &group))
398 run_regression (®ression,
403 ok = casegrouper_destroy (grouper);
404 ok = proc_commit (ds) && ok;
407 if (workspace.writer)
409 struct save_trans_data *save_trans_data = xmalloc (sizeof *save_trans_data);
410 struct casereader *r = casewriter_make_reader (workspace.writer);
411 workspace.writer = NULL;
412 workspace.reader = r;
413 save_trans_data->ws = xmalloc (sizeof (workspace));
414 memcpy (save_trans_data->ws, &workspace, sizeof (workspace));
415 save_trans_data->n_dep_vars = regression.n_dep_vars;
417 add_transformation (ds, save_trans_func, save_trans_free, save_trans_data);
421 free (regression.vars);
422 free (regression.dep_vars);
427 free (regression.vars);
428 free (regression.dep_vars);
432 /* Return the size of the union of dependent and independent variables */
434 get_n_all_vars (const struct regression *cmd)
436 size_t result = cmd->n_vars;
440 result += cmd->n_dep_vars;
441 for (i = 0; i < cmd->n_dep_vars; i++)
443 for (j = 0; j < cmd->n_vars; j++)
445 if (cmd->vars[j] == cmd->dep_vars[i])
454 /* Fill VARS with the union of dependent and independent variables */
456 fill_all_vars (const struct variable **vars, const struct regression *cmd)
460 for (i = 0; i < cmd->n_vars; i++)
462 vars[i] = cmd->vars[i];
465 for (i = 0; i < cmd->n_dep_vars; i++)
469 for (j = 0; j < cmd->n_vars; j++)
471 if (cmd->dep_vars[i] == cmd->vars[j])
479 vars[cmd->n_vars + x++] = cmd->dep_vars[i];
485 Is variable k the dependent variable?
488 is_depvar (const struct regression *cmd, size_t k, const struct variable *v)
490 return v == cmd->vars[k];
494 /* Identify the explanatory variables in v_variables. Returns
495 the number of independent variables. */
497 identify_indep_vars (const struct regression *cmd,
498 const struct variable **indep_vars,
499 const struct variable *depvar)
501 int n_indep_vars = 0;
504 for (i = 0; i < cmd->n_vars; i++)
505 if (!is_depvar (cmd, i, depvar))
506 indep_vars[n_indep_vars++] = cmd->vars[i];
507 if ((n_indep_vars < 1) && is_depvar (cmd, 0, depvar))
510 There is only one independent variable, and it is the same
511 as the dependent variable. Print a warning and continue.
515 ("The dependent variable is equal to the independent variable. "
516 "The least squares line is therefore Y=X. "
517 "Standard errors and related statistics may be meaningless."));
519 indep_vars[0] = cmd->vars[0];
526 fill_covariance (gsl_matrix * cov, struct covariance *all_cov,
527 const struct variable **vars,
528 size_t n_vars, const struct variable *dep_var,
529 const struct variable **all_vars, size_t n_all_vars,
534 size_t dep_subscript;
536 const gsl_matrix *ssizes;
537 const gsl_matrix *mean_matrix;
538 const gsl_matrix *ssize_matrix;
541 const gsl_matrix *cm = covariance_calculate_unnormalized (all_cov);
546 rows = xnmalloc (cov->size1 - 1, sizeof (*rows));
548 for (i = 0; i < n_all_vars; i++)
550 for (j = 0; j < n_vars; j++)
552 if (vars[j] == all_vars[i])
557 if (all_vars[i] == dep_var)
562 mean_matrix = covariance_moments (all_cov, MOMENT_MEAN);
563 ssize_matrix = covariance_moments (all_cov, MOMENT_NONE);
564 for (i = 0; i < cov->size1 - 1; i++)
566 means[i] = gsl_matrix_get (mean_matrix, rows[i], 0)
567 / gsl_matrix_get (ssize_matrix, rows[i], 0);
568 for (j = 0; j < cov->size2 - 1; j++)
570 gsl_matrix_set (cov, i, j, gsl_matrix_get (cm, rows[i], rows[j]));
571 gsl_matrix_set (cov, j, i, gsl_matrix_get (cm, rows[j], rows[i]));
574 means[cov->size1 - 1] = gsl_matrix_get (mean_matrix, dep_subscript, 0)
575 / gsl_matrix_get (ssize_matrix, dep_subscript, 0);
576 ssizes = covariance_moments (all_cov, MOMENT_NONE);
577 result = gsl_matrix_get (ssizes, dep_subscript, rows[0]);
578 for (i = 0; i < cov->size1 - 1; i++)
580 gsl_matrix_set (cov, i, cov->size1 - 1,
581 gsl_matrix_get (cm, rows[i], dep_subscript));
582 gsl_matrix_set (cov, cov->size1 - 1, i,
583 gsl_matrix_get (cm, rows[i], dep_subscript));
584 if (result > gsl_matrix_get (ssizes, rows[i], dep_subscript))
586 result = gsl_matrix_get (ssizes, rows[i], dep_subscript);
589 gsl_matrix_set (cov, cov->size1 - 1, cov->size1 - 1,
590 gsl_matrix_get (cm, dep_subscript, dep_subscript));
598 STATISTICS subcommand output functions.
600 static void reg_stats_r (const linreg *, const struct variable *);
601 static void reg_stats_coeff (const linreg *, const gsl_matrix *, const struct variable *, const struct regression *);
602 static void reg_stats_anova (const linreg *, const struct variable *);
603 static void reg_stats_bcov (const linreg *, const struct variable *);
607 subcommand_statistics (const struct regression *cmd, const linreg * c, const gsl_matrix * cm,
608 const struct variable *var)
610 if (cmd->stats & STATS_R)
611 reg_stats_r (c, var);
613 if (cmd->stats & STATS_ANOVA)
614 reg_stats_anova (c, var);
616 if (cmd->stats & STATS_COEFF)
617 reg_stats_coeff (c, cm, var, cmd);
619 if (cmd->stats & STATS_BCOV)
620 reg_stats_bcov (c, var);
625 run_regression (const struct regression *cmd,
626 struct regression_workspace *ws,
627 struct casereader *input)
634 struct covariance *cov;
635 struct casereader *reader;
636 size_t n_all_vars = get_n_all_vars (cmd);
637 const struct variable **all_vars = xnmalloc (n_all_vars, sizeof (*all_vars));
639 double *means = xnmalloc (n_all_vars, sizeof (*means));
641 fill_all_vars (all_vars, cmd);
642 cov = covariance_1pass_create (n_all_vars, all_vars,
643 dict_get_weight (dataset_dict (cmd->ds)),
646 reader = casereader_clone (input);
647 reader = casereader_create_filter_missing (reader, all_vars, n_all_vars,
652 struct casereader *r = casereader_clone (reader);
654 for (; (c = casereader_read (r)) != NULL; case_unref (c))
656 covariance_accumulate (cov, c);
658 casereader_destroy (r);
661 models = xcalloc (cmd->n_dep_vars, sizeof (*models));
662 for (k = 0; k < cmd->n_dep_vars; k++)
664 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
665 const struct variable *dep_var = cmd->dep_vars[k];
666 int n_indep = identify_indep_vars (cmd, vars, dep_var);
667 gsl_matrix *this_cm = gsl_matrix_alloc (n_indep + 1, n_indep + 1);
668 double n_data = fill_covariance (this_cm, cov, vars, n_indep,
669 dep_var, all_vars, n_all_vars, means);
670 models[k] = linreg_alloc (dep_var, vars, n_data, n_indep);
671 models[k]->depvar = dep_var;
672 for (i = 0; i < n_indep; i++)
674 linreg_set_indep_variable_mean (models[k], i, means[i]);
676 linreg_set_depvar_mean (models[k], means[i]);
678 For large data sets, use QR decomposition.
680 if (n_data > sqrt (n_indep) && n_data > REG_LARGE_DATA)
682 models[k]->method = LINREG_QR;
688 Find the least-squares estimates and other statistics.
690 linreg_fit (this_cm, models[k]);
692 if (!taint_has_tainted_successor (casereader_get_taint (input)))
694 subcommand_statistics (cmd, models[k], this_cm, dep_var);
699 msg (SE, _("No valid data found. This command was skipped."));
701 gsl_matrix_free (this_cm);
708 struct casereader *r = casereader_clone (reader);
710 for (; (c = casereader_read (r)) != NULL; case_unref (c))
712 struct ccase *outc = case_create (casewriter_get_proto (ws->writer));
713 for (k = 0; k < cmd->n_dep_vars; k++)
715 const struct variable **vars = xnmalloc (cmd->n_vars, sizeof (*vars));
716 const struct variable *dep_var = cmd->dep_vars[k];
717 int n_indep = identify_indep_vars (cmd, vars, dep_var);
718 double *vals = xnmalloc (n_indep, sizeof (*vals));
719 for (i = 0; i < n_indep; i++)
721 const union value *tmp = case_data (c, vars[i]);
727 double pred = linreg_predict (models[k], vals, n_indep);
728 case_data_rw_idx (outc, k * ws->extras + ws->pred_idx)->f = pred;
733 double obs = case_data (c, models[k]->depvar)->f;
734 double res = linreg_residual (models[k], obs, vals, n_indep);
735 case_data_rw_idx (outc, k * ws->extras + ws->res_idx)->f = res;
740 casewriter_write (ws->writer, outc);
742 casereader_destroy (r);
745 casereader_destroy (reader);
747 for (k = 0; k < cmd->n_dep_vars; k++)
749 linreg_unref (models[k]);
755 casereader_destroy (input);
756 covariance_destroy (cov);
763 reg_stats_r (const linreg * c, const struct variable *var)
773 rsq = linreg_ssreg (c) / linreg_sst (c);
775 (1.0 - rsq) * linreg_n_coeffs (c) / (linreg_n_obs (c) -
776 linreg_n_coeffs (c) - 1);
777 std_error = sqrt (linreg_mse (c));
778 t = tab_create (n_cols, n_rows);
779 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
780 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
781 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
782 tab_vline (t, TAL_0, 1, 0, 0);
784 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("R"));
785 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("R Square"));
786 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Adjusted R Square"));
787 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error of the Estimate"));
788 tab_double (t, 1, 1, TAB_RIGHT, sqrt (rsq), NULL, RC_OTHER);
789 tab_double (t, 2, 1, TAB_RIGHT, rsq, NULL, RC_OTHER);
790 tab_double (t, 3, 1, TAB_RIGHT, adjrsq, NULL, RC_OTHER);
791 tab_double (t, 4, 1, TAB_RIGHT, std_error, NULL, RC_OTHER);
792 tab_title (t, _("Model Summary (%s)"), var_to_string (var));
797 Table showing estimated regression coefficients.
800 reg_stats_coeff (const linreg * c, const gsl_matrix *cov, const struct variable *var, const struct regression *cmd)
804 const int heading_rows = 2;
813 const struct variable *v;
816 const double df = linreg_n_obs (c) - linreg_n_coeffs (c) - 1;
817 double q = (1 - cmd->ci) / 2.0; /* 2-tailed test */
818 double tval = gsl_cdf_tdist_Qinv (q, df);
821 n_rows = linreg_n_coeffs (c) + heading_rows + 1;
823 if (cmd->stats & STATS_CI)
826 t = tab_create (n_cols, n_rows);
827 tab_headers (t, 2, 0, 1, 0);
828 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
829 tab_hline (t, TAL_2, 0, n_cols - 1, heading_rows);
830 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
831 tab_vline (t, TAL_0, 1, 0, 0);
834 tab_hline (t, TAL_1, 2, 4, 1);
835 tab_joint_text (t, 2, 0, 3, 0, TAB_CENTER | TAT_TITLE, _("Unstandardized Coefficients"));
836 tab_text (t, 2, 1, TAB_CENTER | TAT_TITLE, _("B"));
837 tab_text (t, 3, 1, TAB_CENTER | TAT_TITLE, _("Std. Error"));
838 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Standardized Coefficients"));
839 tab_text (t, 4, 1, TAB_CENTER | TAT_TITLE, _("Beta"));
840 tab_text (t, 5, 1, TAB_CENTER | TAT_TITLE, _("t"));
841 tab_text (t, 6, 1, TAB_CENTER | TAT_TITLE, _("Sig."));
842 tab_text (t, 1, heading_rows, TAB_LEFT | TAT_TITLE, _("(Constant)"));
843 tab_double (t, 2, heading_rows, 0, linreg_intercept (c), NULL, RC_OTHER);
844 std_err = sqrt (gsl_matrix_get (linreg_cov (c), 0, 0));
846 if (cmd->stats & STATS_CI)
848 double lower = linreg_intercept (c) - tval * std_err ;
849 double upper = linreg_intercept (c) + tval * std_err ;
850 tab_double (t, 7, heading_rows, 0, lower, NULL, RC_OTHER);
851 tab_double (t, 8, heading_rows, 0, upper, NULL, RC_OTHER);
853 tab_joint_text_format (t, 7, 0, 8, 0, TAB_CENTER | TAT_TITLE, _("%g%% Confidence Interval for B"), cmd->ci * 100);
854 tab_hline (t, TAL_1, 7, 8, 1);
855 tab_text (t, 7, 1, TAB_CENTER | TAT_TITLE, _("Lower Bound"));
856 tab_text (t, 8, 1, TAB_CENTER | TAT_TITLE, _("Upper Bound"));
858 tab_double (t, 3, heading_rows, 0, std_err, NULL, RC_OTHER);
859 tab_double (t, 4, heading_rows, 0, 0.0, NULL, RC_OTHER);
860 t_stat = linreg_intercept (c) / std_err;
861 tab_double (t, 5, heading_rows, 0, t_stat, NULL, RC_OTHER);
863 2 * gsl_cdf_tdist_Q (fabs (t_stat),
864 (double) (linreg_n_obs (c) - linreg_n_coeffs (c)));
865 tab_double (t, 6, heading_rows, 0, pval, NULL, RC_PVALUE);
867 for (j = 0; j < linreg_n_coeffs (c); j++)
870 ds_init_empty (&tstr);
871 this_row = j + heading_rows + 1;
873 v = linreg_indep_var (c, j);
874 label = var_to_string (v);
875 /* Do not overwrite the variable's name. */
876 ds_put_cstr (&tstr, label);
877 tab_text (t, 1, this_row, TAB_LEFT, ds_cstr (&tstr));
879 Regression coefficients.
881 tab_double (t, 2, this_row, 0, linreg_coeff (c, j), NULL, RC_OTHER);
883 Standard error of the coefficients.
885 std_err = sqrt (gsl_matrix_get (linreg_cov (c), j + 1, j + 1));
886 tab_double (t, 3, this_row, 0, std_err, NULL, RC_OTHER);
888 Standardized coefficient, i.e., regression coefficient
889 if all variables had unit variance.
891 beta = sqrt (gsl_matrix_get (cov, j, j));
892 beta *= linreg_coeff (c, j) /
893 sqrt (gsl_matrix_get (cov, cov->size1 - 1, cov->size2 - 1));
894 tab_double (t, 4, this_row, 0, beta, NULL, RC_OTHER);
897 Test statistic for H0: coefficient is 0.
899 t_stat = linreg_coeff (c, j) / std_err;
900 tab_double (t, 5, this_row, 0, t_stat, NULL, RC_OTHER);
902 P values for the test statistic above.
904 pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), df);
905 tab_double (t, 6, this_row, 0, pval, NULL, RC_PVALUE);
908 if (cmd->stats & STATS_CI)
910 double lower = linreg_coeff (c, j) - tval * std_err ;
911 double upper = linreg_coeff (c, j) + tval * std_err ;
913 tab_double (t, 7, this_row, 0, lower, NULL, RC_OTHER);
914 tab_double (t, 8, this_row, 0, upper, NULL, RC_OTHER);
917 tab_title (t, _("Coefficients (%s)"), var_to_string (var));
922 Display the ANOVA table.
925 reg_stats_anova (const linreg * c, const struct variable *var)
929 const double msm = linreg_ssreg (c) / linreg_dfmodel (c);
930 const double mse = linreg_mse (c);
931 const double F = msm / mse;
932 const double pval = gsl_cdf_fdist_Q (F, c->dfm, c->dfe);
937 t = tab_create (n_cols, n_rows);
938 tab_headers (t, 2, 0, 1, 0);
940 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
942 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
943 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
944 tab_vline (t, TAL_0, 1, 0, 0);
946 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
947 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
948 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
949 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
950 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Sig."));
952 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Regression"));
953 tab_text (t, 1, 2, TAB_LEFT | TAT_TITLE, _("Residual"));
954 tab_text (t, 1, 3, TAB_LEFT | TAT_TITLE, _("Total"));
956 /* Sums of Squares */
957 tab_double (t, 2, 1, 0, linreg_ssreg (c), NULL, RC_OTHER);
958 tab_double (t, 2, 3, 0, linreg_sst (c), NULL, RC_OTHER);
959 tab_double (t, 2, 2, 0, linreg_sse (c), NULL, RC_OTHER);
962 /* Degrees of freedom */
963 tab_text_format (t, 3, 1, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dfm);
964 tab_text_format (t, 3, 2, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dfe);
965 tab_text_format (t, 3, 3, TAB_RIGHT, "%.*g", DBL_DIG + 1, c->dft);
968 tab_double (t, 4, 1, TAB_RIGHT, msm, NULL, RC_OTHER);
969 tab_double (t, 4, 2, TAB_RIGHT, mse, NULL, RC_OTHER);
971 tab_double (t, 5, 1, 0, F, NULL, RC_OTHER);
973 tab_double (t, 6, 1, 0, pval, NULL, RC_PVALUE);
975 tab_title (t, _("ANOVA (%s)"), var_to_string (var));
981 reg_stats_bcov (const linreg * c, const struct variable *var)
993 n_cols = c->n_indeps + 1 + 2;
994 n_rows = 2 * (c->n_indeps + 1);
995 t = tab_create (n_cols, n_rows);
996 tab_headers (t, 2, 0, 1, 0);
997 tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1);
998 tab_hline (t, TAL_2, 0, n_cols - 1, 1);
999 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
1000 tab_vline (t, TAL_0, 1, 0, 0);
1001 tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Model"));
1002 tab_text (t, 1, 1, TAB_CENTER | TAT_TITLE, _("Covariances"));
1003 for (i = 0; i < linreg_n_coeffs (c); i++)
1005 const struct variable *v = linreg_indep_var (c, i);
1006 label = var_to_string (v);
1007 tab_text (t, 2, i, TAB_CENTER, label);
1008 tab_text (t, i + 2, 0, TAB_CENTER, label);
1009 for (k = 1; k < linreg_n_coeffs (c); k++)
1011 col = (i <= k) ? k : i;
1012 row = (i <= k) ? i : k;
1013 tab_double (t, k + 2, i, TAB_CENTER,
1014 gsl_matrix_get (c->cov, row, col), NULL, RC_OTHER);
1017 tab_title (t, _("Coefficient Correlations (%s)"), var_to_string (var));