1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011 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/>. */
19 #include <gsl/gsl_cdf.h>
20 #include <gsl/gsl_matrix.h>
23 #include "data/casegrouper.h"
24 #include "data/casereader.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "data/format.h"
28 #include "data/variable.h"
29 #include "language/command.h"
30 #include "language/dictionary/split-file.h"
31 #include "language/lexer/lexer.h"
32 #include "language/lexer/variable-parser.h"
33 #include "libpspp/assertion.h"
34 #include "libpspp/message.h"
35 #include "libpspp/misc.h"
36 #include "math/correlation.h"
37 #include "math/covariance.h"
38 #include "math/moments.h"
39 #include "output/tab.h"
41 #include "gl/xalloc.h"
42 #include "gl/minmax.h"
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
54 const struct variable **vars;
58 /* Handling of missing values. */
59 enum corr_missing_type
61 CORR_PAIRWISE, /* Handle missing values on a per-variable-pair basis. */
62 CORR_LISTWISE /* Discard entire case if any variable is missing. */
67 STATS_DESCRIPTIVES = 0x01,
69 STATS_ALL = STATS_XPROD | STATS_DESCRIPTIVES
74 enum corr_missing_type missing_type;
75 enum mv_class exclude; /* Classes of missing values to exclude. */
77 bool sig; /* Flag significant values or not */
78 int tails; /* Report significance with how many tails ? */
79 enum stats_opts statistics;
81 const struct variable *wv; /* The weight variable (if any) */
86 output_descriptives (const struct corr *corr, const gsl_matrix *means,
87 const gsl_matrix *vars, const gsl_matrix *ns)
89 const int nr = corr->n_vars_total + 1;
93 const int heading_columns = 1;
94 const int heading_rows = 1;
96 struct tab_table *t = tab_create (nc, nr);
97 tab_title (t, _("Descriptive Statistics"));
99 tab_headers (t, heading_columns, 0, heading_rows, 0);
101 /* Outline the box */
115 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
116 tab_hline (t, TAL_1, 0, nc - 1, heading_rows);
118 tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
119 tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
120 tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
122 for (r = 0 ; r < corr->n_vars_total ; ++r)
124 const struct variable *v = corr->vars[r];
125 tab_text (t, 0, r + heading_rows, TAB_LEFT | TAT_TITLE, var_to_string (v));
127 for (c = 1 ; c < nc ; ++c)
134 x = gsl_matrix_get (means, r, 0);
137 x = gsl_matrix_get (vars, r, 0);
139 /* Here we want to display the non-biased estimator */
140 n = gsl_matrix_get (ns, r, 0);
146 x = gsl_matrix_get (ns, r, 0);
152 tab_double (t, c, r + heading_rows, 0, x, NULL);
160 output_correlation (const struct corr *corr, const struct corr_opts *opts,
161 const gsl_matrix *cm, const gsl_matrix *samples,
162 const gsl_matrix *cv)
167 int nr = corr->n_vars1;
168 int nc = matrix_cols = corr->n_vars_total > corr->n_vars1 ?
169 corr->n_vars_total - corr->n_vars1 : corr->n_vars1;
171 const struct fmt_spec *wfmt = opts->wv ? var_get_print_format (opts->wv) : & F_8_0;
173 const int heading_columns = 2;
174 const int heading_rows = 1;
176 int rows_per_variable = opts->missing_type == CORR_LISTWISE ? 2 : 3;
178 if (opts->statistics & STATS_XPROD)
179 rows_per_variable += 2;
181 /* Two header columns */
182 nc += heading_columns;
184 /* Three data per variable */
185 nr *= rows_per_variable;
190 t = tab_create (nc, nr);
191 tab_title (t, _("Correlations"));
193 tab_headers (t, heading_columns, 0, heading_rows, 0);
195 /* Outline the box */
209 tab_vline (t, TAL_2, heading_columns, 0, nr - 1);
211 tab_vline (t, TAL_1, 1, heading_rows, nr - 1);
213 for (r = 0 ; r < corr->n_vars1 ; ++r)
215 tab_text (t, 0, 1 + r * rows_per_variable, TAB_LEFT | TAT_TITLE,
216 var_to_string (corr->vars[r]));
218 tab_text (t, 1, 1 + r * rows_per_variable, TAB_LEFT | TAT_TITLE, _("Pearson Correlation"));
219 tab_text (t, 1, 2 + r * rows_per_variable, TAB_LEFT | TAT_TITLE,
220 (opts->tails == 2) ? _("Sig. (2-tailed)") : _("Sig. (1-tailed)"));
222 if (opts->statistics & STATS_XPROD)
224 tab_text (t, 1, 3 + r * rows_per_variable, TAB_LEFT | TAT_TITLE, _("Cross-products"));
225 tab_text (t, 1, 4 + r * rows_per_variable, TAB_LEFT | TAT_TITLE, _("Covariance"));
228 if ( opts->missing_type != CORR_LISTWISE )
229 tab_text (t, 1, rows_per_variable + r * rows_per_variable, TAB_LEFT | TAT_TITLE, _("N"));
231 tab_hline (t, TAL_1, 0, nc - 1, r * rows_per_variable + 1);
234 for (c = 0 ; c < matrix_cols ; ++c)
236 const struct variable *v = corr->n_vars_total > corr->n_vars1 ?
237 corr->vars[corr->n_vars1 + c] : corr->vars[c];
238 tab_text (t, heading_columns + c, 0, TAB_LEFT | TAT_TITLE, var_to_string (v));
241 for (r = 0 ; r < corr->n_vars1 ; ++r)
243 const int row = r * rows_per_variable + heading_rows;
244 for (c = 0 ; c < matrix_cols ; ++c)
246 unsigned char flags = 0;
247 const int col_index = corr->n_vars_total > corr->n_vars1 ?
248 corr->n_vars_total - corr->n_vars1 - 1 + c :
250 double pearson = gsl_matrix_get (cm, r, col_index);
251 double w = gsl_matrix_get (samples, r, col_index);
252 double sig = opts->tails * significance_of_correlation (pearson, w);
254 if ( opts->missing_type != CORR_LISTWISE )
255 tab_double (t, c + heading_columns, row + rows_per_variable - 1, 0, w, wfmt);
258 tab_double (t, c + heading_columns, row + 1, 0, sig, NULL);
260 if ( opts->sig && c != r && sig < 0.05)
263 tab_double (t, c + heading_columns, row, flags, pearson, NULL);
265 if (opts->statistics & STATS_XPROD)
267 double cov = gsl_matrix_get (cv, r, col_index);
268 const double xprod_dev = cov * w;
269 cov *= w / (w - 1.0);
271 tab_double (t, c + heading_columns, row + 2, 0, xprod_dev, NULL);
272 tab_double (t, c + heading_columns, row + 3, 0, cov, NULL);
282 run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr *corr)
285 const gsl_matrix *var_matrix, *samples_matrix, *mean_matrix;
286 gsl_matrix *cov_matrix;
287 gsl_matrix *corr_matrix;
288 struct covariance *cov = covariance_2pass_create (corr->n_vars_total, corr->vars,
290 opts->wv, opts->exclude);
292 struct casereader *rc = casereader_clone (r);
293 for ( ; (c = casereader_read (r) ); case_unref (c))
295 covariance_accumulate_pass1 (cov, c);
298 for ( ; (c = casereader_read (rc) ); case_unref (c))
300 covariance_accumulate_pass2 (cov, c);
303 cov_matrix = covariance_calculate (cov);
305 casereader_destroy (rc);
307 samples_matrix = covariance_moments (cov, MOMENT_NONE);
308 var_matrix = covariance_moments (cov, MOMENT_VARIANCE);
309 mean_matrix = covariance_moments (cov, MOMENT_MEAN);
311 corr_matrix = correlation_from_covariance (cov_matrix, var_matrix);
313 if ( opts->statistics & STATS_DESCRIPTIVES)
314 output_descriptives (corr, mean_matrix, var_matrix, samples_matrix);
316 output_correlation (corr, opts, corr_matrix,
317 samples_matrix, cov_matrix);
319 covariance_destroy (cov);
320 gsl_matrix_free (corr_matrix);
321 gsl_matrix_free (cov_matrix);
325 cmd_correlation (struct lexer *lexer, struct dataset *ds)
328 int n_all_vars = 0; /* Total number of variables involved in this command */
329 const struct variable **all_vars ;
330 const struct dictionary *dict = dataset_dict (ds);
333 struct casegrouper *grouper;
334 struct casereader *group;
336 struct corr *corr = NULL;
339 struct corr_opts opts;
340 opts.missing_type = CORR_PAIRWISE;
341 opts.wv = dict_get_weight (dict);
344 opts.exclude = MV_ANY;
347 /* Parse CORRELATIONS. */
348 while (lex_token (lexer) != T_ENDCMD)
350 lex_match (lexer, T_SLASH);
351 if (lex_match_id (lexer, "MISSING"))
353 lex_match (lexer, T_EQUALS);
354 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
356 if (lex_match_id (lexer, "PAIRWISE"))
357 opts.missing_type = CORR_PAIRWISE;
358 else if (lex_match_id (lexer, "LISTWISE"))
359 opts.missing_type = CORR_LISTWISE;
361 else if (lex_match_id (lexer, "INCLUDE"))
362 opts.exclude = MV_SYSTEM;
363 else if (lex_match_id (lexer, "EXCLUDE"))
364 opts.exclude = MV_ANY;
367 lex_error (lexer, NULL);
370 lex_match (lexer, T_COMMA);
373 else if (lex_match_id (lexer, "PRINT"))
375 lex_match (lexer, T_EQUALS);
376 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
378 if ( lex_match_id (lexer, "TWOTAIL"))
380 else if (lex_match_id (lexer, "ONETAIL"))
382 else if (lex_match_id (lexer, "SIG"))
384 else if (lex_match_id (lexer, "NOSIG"))
388 lex_error (lexer, NULL);
392 lex_match (lexer, T_COMMA);
395 else if (lex_match_id (lexer, "STATISTICS"))
397 lex_match (lexer, T_EQUALS);
398 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
400 if ( lex_match_id (lexer, "DESCRIPTIVES"))
401 opts.statistics = STATS_DESCRIPTIVES;
402 else if (lex_match_id (lexer, "XPROD"))
403 opts.statistics = STATS_XPROD;
404 else if (lex_token (lexer) == T_ALL)
406 opts.statistics = STATS_ALL;
411 lex_error (lexer, NULL);
415 lex_match (lexer, T_COMMA);
420 if (lex_match_id (lexer, "VARIABLES"))
422 lex_match (lexer, T_EQUALS);
425 corr = xrealloc (corr, sizeof (*corr) * (n_corrs + 1));
426 corr[n_corrs].n_vars_total = corr[n_corrs].n_vars1 = 0;
428 if ( ! parse_variables_const (lexer, dict, &corr[n_corrs].vars,
429 &corr[n_corrs].n_vars_total,
437 corr[n_corrs].n_vars1 = corr[n_corrs].n_vars_total;
439 if ( lex_match (lexer, T_WITH))
441 if ( ! parse_variables_const (lexer, dict,
442 &corr[n_corrs].vars, &corr[n_corrs].n_vars_total,
443 PV_NUMERIC | PV_APPEND))
450 n_all_vars += corr[n_corrs].n_vars_total;
458 msg (SE, _("No variables specified."));
463 all_vars = xmalloc (sizeof (*all_vars) * n_all_vars);
466 /* FIXME: Using a hash here would make more sense */
467 const struct variable **vv = all_vars;
469 for (i = 0 ; i < n_corrs; ++i)
472 const struct corr *c = &corr[i];
473 for (v = 0 ; v < c->n_vars_total; ++v)
478 grouper = casegrouper_create_splits (proc_open (ds), dict);
480 while (casegrouper_get_next_group (grouper, &group))
482 for (i = 0 ; i < n_corrs; ++i)
484 /* FIXME: No need to iterate the data multiple times */
485 struct casereader *r = casereader_clone (group);
487 if ( opts.missing_type == CORR_LISTWISE)
488 r = casereader_create_filter_missing (r, all_vars, n_all_vars,
489 opts.exclude, NULL, NULL);
492 run_corr (r, &opts, &corr[i]);
493 casereader_destroy (r);
495 casereader_destroy (group);
498 ok = casegrouper_destroy (grouper);
499 ok = proc_commit (ds) && ok;
508 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;