1 /* PSPP - linear regression.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Written by Jason H Stover <jason@sakla.net>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 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, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <gsl/gsl_cdf.h>
23 #include <gsl/gsl_vector.h>
24 #include <gsl/gsl_matrix.h>
27 #include "dictionary.h"
28 #include "file-handle.h"
35 #include <linreg/pspp_linreg.h>
41 "REGRESSION" (regression_):
65 static struct cmd_regression cmd;
68 Array holding the subscripts of the independent variables.
72 static void run_regression( const struct casefile * );
74 STATISTICS subcommand output functions.
76 static void reg_stats_r (pspp_linreg_cache *);
77 static void reg_stats_coeff (pspp_linreg_cache *);
78 static void reg_stats_anova (pspp_linreg_cache *);
79 static void reg_stats_outs(pspp_linreg_cache *);
80 static void reg_stats_zpp (pspp_linreg_cache *);
81 static void reg_stats_label (pspp_linreg_cache *);
82 static void reg_stats_sha (pspp_linreg_cache *);
83 static void reg_stats_ci (pspp_linreg_cache *);
84 static void reg_stats_f (pspp_linreg_cache *);
85 static void reg_stats_bcov(pspp_linreg_cache *);
86 static void reg_stats_ses (pspp_linreg_cache *);
87 static void reg_stats_xtx (pspp_linreg_cache *);
88 static void reg_stats_collin(pspp_linreg_cache *);
89 static void reg_stats_tol (pspp_linreg_cache *);
90 static void reg_stats_selection(pspp_linreg_cache *);
91 static void statistics_keyword_output ( void (*) (pspp_linreg_cache *),
92 int, pspp_linreg_cache *);
95 reg_stats_r (pspp_linreg_cache *c)
100 Table showing estimated regression coefficients.
103 reg_stats_coeff (pspp_linreg_cache *c)
117 n_rows = 2 + c->param_estimates->size;
118 t = tab_create (n_cols, n_rows, 0);
119 tab_headers (t, 2, 0, 1, 0);
120 tab_dim( t, tab_natural_dimensions);
121 tab_box ( t, TAL_2, TAL_2, -1, TAL_1, 0, 0,
122 n_cols - 1, n_rows - 1);
123 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
124 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
125 tab_vline (t, TAL_0, 1, 0, 0);
127 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("B"));
128 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Error"));
129 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Beta"));
130 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t"));
131 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
132 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("(Constant)"));
133 coeff = gsl_vector_get ( c->param_estimates, 0);
134 tab_float ( t, 2, 1, 0, coeff, 10, 2 );
135 std_err = sqrt(gsl_matrix_get ( c->cov, 0, 0));
136 tab_float ( t, 3, 1, 0, std_err, 10, 2);
137 beta = coeff / c->depvar_std;
138 tab_float ( t, 4, 1, 0, beta, 10, 2);
139 t_stat = coeff / std_err;
140 tab_float ( t, 5, 1, 0, t_stat, 10, 2);
141 pval = 2 * gsl_cdf_tdist_Q ( fabs(t_stat), 1.0);
142 tab_float ( t, 6, 1, 0, pval, 10, 2);
143 for( j = 0; j < c->n_indeps; j++ )
146 struct variable *v = cmd.v_variables[i];
147 label = var_to_string(v);
148 tab_text ( t, 1, j + 2, TAB_CENTER, label);
150 Regression coefficients.
152 coeff = gsl_vector_get ( c->param_estimates, j+1 );
153 tab_float ( t, 2, j + 2, 0, coeff, 10, 2 );
155 Standard error of the coefficients.
157 std_err = sqrt ( gsl_matrix_get ( c->cov, j+1, j+1 ));
158 tab_float ( t, 3, j + 2, 0, std_err, 10, 2 );
160 'Standardized' coefficient, i.e., regression coefficient
161 if all variables had unit variance.
163 beta = gsl_vector_get(c->indep_std, j+1);
164 beta *= coeff / c->depvar_std;
165 tab_float ( t, 4, j + 2, 0, beta, 10, 2);
168 Test statistic for H0: coefficient is 0.
170 t_stat = coeff / std_err;
171 tab_float ( t, 5, j + 2, 0, t_stat, 10, 2);
173 P values for the test statistic above.
175 pval = 2 * gsl_cdf_tdist_Q ( fabs(t_stat), 1.0 );
176 tab_float ( t, 6, j + 2, 0, pval, 10, 2);
178 tab_title (t, 0, _("Coefficients"));
182 Display the ANOVA table.
185 reg_stats_anova (pspp_linreg_cache *c)
189 const double msm = c->ssm / c->dfm;
190 const double mse = c->sse / c->dfe;
191 const double F = msm / mse ;
192 const double pval = gsl_cdf_fdist_Q(F, c->dfm, c->dfe);
196 t = tab_create (n_cols,n_rows,0);
197 tab_headers (t, 2, 0, 1, 0);
198 tab_dim (t, tab_natural_dimensions);
204 n_cols - 1, n_rows - 1);
206 tab_hline (t, TAL_2, 0, n_cols - 1, 1 );
207 tab_vline (t, TAL_2, 2, 0, n_rows - 1);
208 tab_vline (t, TAL_0, 1, 0, 0);
210 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Sum of Squares"));
211 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("df"));
212 tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Mean Square"));
213 tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("F"));
214 tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance"));
216 tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("Regression"));
217 tab_text (t, 1, 2, TAB_LEFT | TAT_TITLE, _("Residual"));
218 tab_text (t, 1, 3, TAB_LEFT | TAT_TITLE, _("Total"));
220 /* Sums of Squares */
221 tab_float (t, 2, 1, 0, c->ssm, 10, 2);
222 tab_float (t, 2, 3, 0, c->sst, 10, 2);
223 tab_float (t, 2, 2, 0, c->sse, 10, 2);
226 /* Degrees of freedom */
227 tab_float (t, 3, 1, 0, c->dfm, 4, 0);
228 tab_float (t, 3, 2, 0, c->dfe, 4, 0);
229 tab_float (t, 3, 3, 0, c->dft, 4, 0);
233 tab_float (t, 4, 1, TAB_RIGHT, msm, 8, 3);
234 tab_float (t, 4, 2, TAB_RIGHT, mse, 8, 3);
236 tab_float (t, 5, 1, 0, F, 8, 3);
238 tab_float (t, 6, 1, 0, pval, 8, 3);
240 tab_title (t, 0, _("ANOVA"));
244 reg_stats_outs(pspp_linreg_cache *c)
249 reg_stats_zpp (pspp_linreg_cache *c)
254 reg_stats_label (pspp_linreg_cache *c)
259 reg_stats_sha (pspp_linreg_cache *c)
264 reg_stats_ci (pspp_linreg_cache *c)
269 reg_stats_f (pspp_linreg_cache *c)
274 reg_stats_bcov(pspp_linreg_cache *c)
279 reg_stats_ses (pspp_linreg_cache *c)
284 reg_stats_xtx (pspp_linreg_cache *c)
289 reg_stats_collin(pspp_linreg_cache *c)
294 reg_stats_tol (pspp_linreg_cache *c)
299 reg_stats_selection(pspp_linreg_cache *c)
305 statistics_keyword_output ( void (*function) (pspp_linreg_cache *),
307 pspp_linreg_cache *c)
316 subcommand_statistics ( int *keywords,
317 pspp_linreg_cache *c)
320 The order here must match the order in which the STATISTICS
321 keywords appear in the specification section above.
346 Set everything but F.
348 for ( i = 0; i < f; i++)
355 for ( i = 0; i < all; i++)
363 Default output: ANOVA table, parameter estimates,
364 and statistics for variables not entered into model,
367 if(keywords[defaults] | d)
369 *(keywords + anova) = 1;
370 *(keywords + outs) = 1;
371 *(keywords + coeff) = 1;
375 statistics_keyword_output ( reg_stats_r,
377 statistics_keyword_output ( reg_stats_anova,
378 keywords[anova], c );
379 statistics_keyword_output ( reg_stats_coeff,
380 keywords[coeff], c );
381 statistics_keyword_output ( reg_stats_outs,
383 statistics_keyword_output ( reg_stats_zpp,
385 statistics_keyword_output ( reg_stats_label,
386 keywords[label], c );
387 statistics_keyword_output ( reg_stats_sha,
389 statistics_keyword_output ( reg_stats_ci,
391 statistics_keyword_output ( reg_stats_f,
393 statistics_keyword_output ( reg_stats_bcov,
395 statistics_keyword_output ( reg_stats_ses,
397 statistics_keyword_output ( reg_stats_xtx,
399 statistics_keyword_output ( reg_stats_collin,
400 keywords[collin], c );
401 statistics_keyword_output ( reg_stats_tol,
403 statistics_keyword_output ( reg_stats_selection,
404 keywords[selection], c );
410 if(!parse_regression(&cmd))
414 multipass_procedure_with_splits (run_regression, &cmd);
419 Is variable k one of the dependent variables?
422 is_depvar ( size_t k)
425 for ( j = 0; j < cmd.n_dependent; j++)
428 compare_var_names returns 0 if the variable
431 if (!compare_var_names( cmd.v_dependent[j],
432 cmd.v_variables[k], NULL))
439 run_regression ( const struct casefile *cf )
446 const union value *val;
447 struct casereader *r;
448 struct casereader *r2;
450 const struct variable *v;
451 struct recoded_categorical_array *ca;
452 struct recoded_categorical *rc;
453 struct design_matrix *X;
455 pspp_linreg_cache *lcache;
456 pspp_linreg_opts lopts;
458 n_data = casefile_get_case_cnt (cf);
459 n_indep = cmd.n_variables - cmd.n_dependent;
460 indep_vars = (size_t *) malloc ( n_indep * sizeof (*indep_vars));
462 Y = gsl_vector_alloc (n_data);
463 lopts.get_depvar_mean_std = 1;
464 lopts.get_indep_mean_std = (int *) malloc ( n_indep * sizeof (int));
466 lcache = pspp_linreg_cache_alloc(n_data, n_indep);
467 lcache->indep_means = gsl_vector_alloc(n_indep);
468 lcache->indep_std = gsl_vector_alloc(n_indep);
471 Read from the active file. The first pass encodes categorical
474 ca = cr_recoded_cat_ar_create ( cmd.n_variables, cmd.v_variables );
475 for (r = casefile_get_reader (cf);
476 casereader_read (r, &c ); case_destroy (&c))
478 for (i = 0; i < ca->n_vars; i++)
480 v = (*(ca->a + i))->v;
481 val = case_data ( &c, v->fv );
482 cr_value_update ( *(ca->a + i), val);
486 cr_create_value_matrices ( ca );
487 X = design_matrix_create ( n_indep, cmd.v_variables,
491 The second pass creates the design matrix.
493 for(r2 = casefile_get_reader (cf);
494 casereader_read (r2, &c) ;
495 case_destroy (&c)) /* Iterate over the cases. */
498 row = casereader_cnum(r2) - 1;
499 for(i = 0; i < cmd.n_variables ; ++i) /* Iterate over the variables
500 for the current case.
503 v = cmd.v_variables[i];
504 val = case_data ( &c, v->fv );
506 Independent/dependent variable separation. The
507 'variables' subcommand specifies a varlist which contains
508 both dependent and independent variables. The dependent
509 variables are specified with the 'dependent'
510 subcommand. We need to separate the two.
514 if ( v->type == NUMERIC )
516 gsl_vector_set(Y, row, val->f);
521 fprintf( stderr, "%s:%d: Dependent variable should be numeric: %s\n",
522 __FILE__,__LINE__,strerror(errno));
528 if ( v->type == ALPHA )
530 rc = cr_var_to_recoded_categorical ( v, ca );
531 design_matrix_set_categorical ( X, row, v, val, rc);
533 else if (v->type == NUMERIC)
535 design_matrix_set_numeric ( X, row, v, val);
540 lopts.get_indep_mean_std[i] = 1;
545 Find the least-squares estimates and other statistics.
547 pspp_linreg ( Y, X->m, &lopts, lcache );
548 subcommand_statistics ( &cmd.a_statistics, lcache );
550 design_matrix_destroy(X);
551 pspp_linreg_cache_free(lcache);
552 free( lopts.get_indep_mean_std );
554 casereader_destroy(r);