1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011, 2013, 2015, 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/>. */
21 #include "data/casegrouper.h"
22 #include "data/casereader.h"
23 #include "data/dataset.h"
24 #include "data/dictionary.h"
25 #include "data/format.h"
26 #include "data/missing-values.h"
27 #include "language/command.h"
28 #include "language/lexer/lexer.h"
29 #include "language/lexer/variable-parser.h"
30 #include "libpspp/message.h"
31 #include "libpspp/misc.h"
32 #include "libpspp/str.h"
33 #include "math/moments.h"
34 #include "output/pivot-table.h"
35 #include "output/output-item.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
43 const struct variable **items;
46 double sum_of_variances;
47 double variance_of_sums;
48 int totals_idx; /* Casereader index into the totals */
50 struct moments1 **m ; /* Moments of the items */
51 struct moments1 *total ; /* Moments of the totals */
56 dump_cronbach (const struct cronbach *s)
59 printf ("N items %d\n", s->n_items);
60 for (i = 0 ; i < s->n_items; ++i)
62 printf ("%s\n", var_get_name (s->items[i]));
65 printf ("Totals idx %d\n", s->totals_idx);
67 printf ("scale variance %g\n", s->variance_of_sums);
68 printf ("alpha %g\n", s->alpha);
82 SUMMARY_TOTAL = 0x0001,
88 const struct variable **variables;
90 enum mv_class exclude;
97 struct string scale_name;
103 enum summary_opts summary;
105 const struct variable *wv;
109 static bool run_reliability (struct dataset *ds, const struct reliability *reliability);
112 reliability_destroy (struct reliability *rel)
115 ds_destroy (&rel->scale_name);
117 for (j = 0; j < rel->n_sc ; ++j)
120 free (rel->sc[j].items);
121 moments1_destroy (rel->sc[j].total);
123 for (x = 0; x < rel->sc[j].n_items; ++x)
124 free (rel->sc[j].m[x]);
129 free (rel->variables);
133 cmd_reliability (struct lexer *lexer, struct dataset *ds)
135 const struct dictionary *dict = dataset_dict (ds);
137 struct reliability reliability;
138 reliability.n_variables = 0;
139 reliability.variables = NULL;
140 reliability.model = MODEL_ALPHA;
141 reliability.exclude = MV_ANY;
142 reliability.summary = 0;
143 reliability.n_sc = 0;
144 reliability.sc = NULL;
145 reliability.wv = dict_get_weight (dict);
146 reliability.total_start = 0;
147 ds_init_empty (&reliability.scale_name);
150 lex_match (lexer, T_SLASH);
152 if (!lex_force_match_id (lexer, "VARIABLES"))
157 lex_match (lexer, T_EQUALS);
159 if (!parse_variables_const (lexer, dict, &reliability.variables, &reliability.n_variables,
160 PV_NO_DUPLICATE | PV_NUMERIC))
163 if (reliability.n_variables < 2)
164 msg (MW, _("Reliability on a single variable is not useful."));
170 /* Create a default Scale */
172 reliability.n_sc = 1;
173 reliability.sc = xcalloc (reliability.n_sc, sizeof (struct cronbach));
175 ds_assign_cstr (&reliability.scale_name, "ANY");
177 c = &reliability.sc[0];
178 c->n_items = reliability.n_variables;
179 c->items = xcalloc (c->n_items, sizeof (struct variable*));
181 for (i = 0 ; i < c->n_items ; ++i)
182 c->items[i] = reliability.variables[i];
187 while (lex_token (lexer) != T_ENDCMD)
189 lex_match (lexer, T_SLASH);
191 if (lex_match_id (lexer, "SCALE"))
193 struct const_var_set *vs;
194 if (! lex_force_match (lexer, T_LPAREN))
197 if (! lex_force_string (lexer))
200 ds_assign_substring (&reliability.scale_name, lex_tokss (lexer));
204 if (! lex_force_match (lexer, T_RPAREN))
207 lex_match (lexer, T_EQUALS);
209 vs = const_var_set_create_from_array (reliability.variables, reliability.n_variables);
211 free (reliability.sc->items);
212 if (!parse_const_var_set_vars (lexer, vs, &reliability.sc->items, &reliability.sc->n_items, 0))
214 const_var_set_destroy (vs);
218 const_var_set_destroy (vs);
220 else if (lex_match_id (lexer, "MODEL"))
222 lex_match (lexer, T_EQUALS);
223 if (lex_match_id (lexer, "ALPHA"))
225 reliability.model = MODEL_ALPHA;
227 else if (lex_match_id (lexer, "SPLIT"))
229 reliability.model = MODEL_SPLIT;
230 reliability.split_point = -1;
232 if (lex_match (lexer, T_LPAREN)
233 && lex_force_num (lexer))
235 reliability.split_point = lex_number (lexer);
237 if (! lex_force_match (lexer, T_RPAREN))
244 else if (lex_match_id (lexer, "SUMMARY"))
246 lex_match (lexer, T_EQUALS);
247 if (lex_match_id (lexer, "TOTAL"))
249 reliability.summary |= SUMMARY_TOTAL;
251 else if (lex_match (lexer, T_ALL))
253 reliability.summary = 0xFFFF;
258 else if (lex_match_id (lexer, "MISSING"))
260 lex_match (lexer, T_EQUALS);
261 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
263 if (lex_match_id (lexer, "INCLUDE"))
265 reliability.exclude = MV_SYSTEM;
267 else if (lex_match_id (lexer, "EXCLUDE"))
269 reliability.exclude = MV_ANY;
273 lex_error (lexer, NULL);
278 else if (lex_match_id (lexer, "STATISTICS"))
280 lex_match (lexer, T_EQUALS);
281 msg (SW, _("The STATISTICS subcommand is not yet implemented. "
282 "No statistics will be produced."));
283 while (lex_match (lexer, T_ID))
288 lex_error (lexer, NULL);
293 if (reliability.model == MODEL_SPLIT)
296 const struct cronbach *s;
298 if (reliability.split_point >= reliability.n_variables)
300 msg (ME, _("The split point must be less than the number of variables"));
304 reliability.n_sc += 2 ;
305 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
307 s = &reliability.sc[0];
309 reliability.sc[1].n_items =
310 (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
312 reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
313 reliability.sc[1].items = XCALLOC (reliability.sc[1].n_items, const struct variable *);
314 reliability.sc[2].items = XCALLOC (reliability.sc[2].n_items, const struct variable *);
316 for (i = 0; i < reliability.sc[1].n_items ; ++i)
317 reliability.sc[1].items[i] = s->items[i];
319 while (i < s->n_items)
321 reliability.sc[2].items[i - reliability.sc[1].n_items] = s->items[i];
326 if (reliability.summary & SUMMARY_TOTAL)
329 const int base_sc = reliability.n_sc;
331 reliability.total_start = base_sc;
333 reliability.n_sc += reliability.sc[0].n_items ;
334 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
337 for (i = 0 ; i < reliability.sc[0].n_items; ++i)
341 struct cronbach *s = &reliability.sc[i + base_sc];
343 s->n_items = reliability.sc[0].n_items - 1;
344 s->items = xcalloc (s->n_items, sizeof (struct variable *));
345 for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
348 s->items[v_dest++] = reliability.sc[0].items[v_src];
354 if (! run_reliability (ds, &reliability))
357 reliability_destroy (&reliability);
361 reliability_destroy (&reliability);
367 do_reliability (struct casereader *group, struct dataset *ds,
368 const struct reliability *rel);
371 static void reliability_summary_total (const struct reliability *rel);
373 static void reliability_statistics (const struct reliability *rel);
377 run_reliability (struct dataset *ds, const struct reliability *reliability)
379 struct dictionary *dict = dataset_dict (ds);
381 struct casereader *group;
383 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
386 for (si = 0 ; si < reliability->n_sc; ++si)
388 struct cronbach *s = &reliability->sc[si];
391 s->m = xcalloc (s->n_items, sizeof *s->m);
392 s->total = moments1_create (MOMENT_VARIANCE);
394 for (i = 0 ; i < s->n_items ; ++i)
395 s->m[i] = moments1_create (MOMENT_VARIANCE);
399 while (casegrouper_get_next_group (grouper, &group))
401 do_reliability (group, ds, reliability);
403 reliability_statistics (reliability);
405 if (reliability->summary & SUMMARY_TOTAL)
406 reliability_summary_total (reliability);
409 ok = casegrouper_destroy (grouper);
410 ok = proc_commit (ds) && ok;
419 /* Return the sum of all the item variables in S */
421 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
424 const struct cronbach *s = aux;
426 for (int v = 0 ; v < s->n_items; ++v)
427 sum += case_num (c, s->items[v]);
433 case_processing_summary (casenumber n_valid, casenumber n_missing,
434 const struct dictionary *dict);
438 alpha (int k, double sum_of_variances, double variance_of_sums)
440 return k / (k - 1.0) * (1 - sum_of_variances / variance_of_sums);
444 do_reliability (struct casereader *input, struct dataset *ds,
445 const struct reliability *rel)
450 casenumber n_missing ;
451 casenumber n_valid = 0;
454 for (si = 0 ; si < rel->n_sc; ++si)
456 struct cronbach *s = &rel->sc[si];
458 moments1_clear (s->total);
460 for (i = 0 ; i < s->n_items ; ++i)
461 moments1_clear (s->m[i]);
464 input = casereader_create_filter_missing (input,
471 for (si = 0 ; si < rel->n_sc; ++si)
473 struct cronbach *s = &rel->sc[si];
476 s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
478 casereader_create_append_numeric (input, append_sum,
482 for (; (c = casereader_read (input)) != NULL; case_unref (c))
487 for (si = 0; si < rel->n_sc; ++si)
489 struct cronbach *s = &rel->sc[si];
491 for (i = 0 ; i < s->n_items ; ++i)
492 moments1_add (s->m[i], case_num (c, s->items[i]), weight);
494 moments1_add (s->total, case_num_idx (c, s->totals_idx), weight);
497 casereader_destroy (input);
499 for (si = 0; si < rel->n_sc; ++si)
501 struct cronbach *s = &rel->sc[si];
503 s->sum_of_variances = 0;
504 for (i = 0 ; i < s->n_items ; ++i)
506 double weight, mean, variance;
507 moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
509 s->sum_of_variances += variance;
512 moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
516 alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
519 output_item_submit (text_item_create_nocopy (
521 xasprintf (_("Scale: %s"), ds_cstr (&rel->scale_name)),
524 case_processing_summary (n_valid, n_missing, dataset_dict (ds));
532 case_processing_summary (casenumber n_valid, casenumber n_missing,
533 const struct dictionary *dict)
535 struct pivot_table *table = pivot_table_create (
536 N_("Case Processing Summary"));
537 pivot_table_set_weight_var (table, dict_get_weight (dict));
539 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
540 N_("N"), PIVOT_RC_COUNT,
541 N_("Percent"), PIVOT_RC_PERCENT);
543 struct pivot_dimension *cases = pivot_dimension_create (
544 table, PIVOT_AXIS_ROW, N_("Cases"), N_("Valid"), N_("Excluded"),
546 cases->root->show_label = true;
548 casenumber total = n_missing + n_valid;
560 { 1, 0, 100.0 * n_valid / total },
561 { 1, 1, 100.0 * n_missing / total },
565 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
567 const struct entry *e = &entries[i];
568 pivot_table_put2 (table, e->stat_idx, e->case_idx,
569 pivot_value_new_number (e->x));
572 pivot_table_submit (table);
576 reliability_summary_total (const struct reliability *rel)
578 struct pivot_table *table = pivot_table_create (N_("Item-Total Statistics"));
580 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Statistics"),
581 N_("Scale Mean if Item Deleted"),
582 N_("Scale Variance if Item Deleted"),
583 N_("Corrected Item-Total Correlation"),
584 N_("Cronbach's Alpha if Item Deleted"));
586 struct pivot_dimension *variables = pivot_dimension_create (
587 table, PIVOT_AXIS_ROW, N_("Variables"));
589 for (size_t i = 0 ; i < rel->sc[0].n_items; ++i)
591 const struct cronbach *s = &rel->sc[rel->total_start + i];
593 int var_idx = pivot_category_create_leaf (
594 variables->root, pivot_value_new_variable (rel->sc[0].items[i]));
597 moments1_calculate (s->total, NULL, &mean, NULL, NULL, NULL);
600 moments1_calculate (rel->sc[0].m[i], NULL, NULL, &var, NULL, NULL);
602 = (rel->sc[0].variance_of_sums + var - s->variance_of_sums) / 2.0;
607 (cov - var) / sqrt (var * s->variance_of_sums),
610 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
611 pivot_table_put2 (table, i, var_idx,
612 pivot_value_new_number (entries[i]));
615 pivot_table_submit (table);
620 reliability_statistics (const struct reliability *rel)
622 struct pivot_table *table = pivot_table_create (
623 N_("Reliability Statistics"));
624 pivot_table_set_weight_var (table, rel->wv);
626 if (rel->model == MODEL_ALPHA)
628 pivot_dimension_create (table, PIVOT_AXIS_COLUMN,
630 N_("Cronbach's Alpha"), PIVOT_RC_OTHER,
631 N_("N of Items"), PIVOT_RC_COUNT);
633 const struct cronbach *s = &rel->sc[0];
634 pivot_table_put1 (table, 0, pivot_value_new_number (s->alpha));
635 pivot_table_put1 (table, 1, pivot_value_new_number (s->n_items));
639 struct pivot_dimension *statistics = pivot_dimension_create (
640 table, PIVOT_AXIS_ROW, N_("Statistics"));
641 struct pivot_category *alpha = pivot_category_create_group (
642 statistics->root, N_("Cronbach's Alpha"));
643 pivot_category_create_group (alpha, N_("Part 1"),
644 N_("Value"), PIVOT_RC_OTHER,
645 N_("N of Items"), PIVOT_RC_COUNT);
646 pivot_category_create_group (alpha, N_("Part 2"),
647 N_("Value"), PIVOT_RC_OTHER,
648 N_("N of Items"), PIVOT_RC_COUNT);
649 pivot_category_create_leaves (alpha,
650 N_("Total N of Items"), PIVOT_RC_COUNT);
651 pivot_category_create_leaves (statistics->root,
652 N_("Correlation Between Forms"),
654 pivot_category_create_group (statistics->root,
655 N_("Spearman-Brown Coefficient"),
656 N_("Equal Length"), PIVOT_RC_OTHER,
657 N_("Unequal Length"), PIVOT_RC_OTHER);
658 pivot_category_create_leaves (statistics->root,
659 N_("Guttman Split-Half Coefficient"),
662 /* R is the correlation between the two parts */
663 double r0 = rel->sc[0].variance_of_sums -
664 rel->sc[1].variance_of_sums -
665 rel->sc[2].variance_of_sums ;
666 double r1 = (r0 / sqrt (rel->sc[1].variance_of_sums)
667 / sqrt (rel->sc[2].variance_of_sums)
670 /* Guttman Split Half Coefficient */
671 double g = 2 * r0 / rel->sc[0].variance_of_sums;
673 double tmp = (1.0 - r1*r1) * rel->sc[1].n_items * rel->sc[2].n_items /
674 pow2 (rel->sc[0].n_items);
681 rel->sc[1].n_items + rel->sc[2].n_items,
684 (sqrt (pow4 (r1) + 4 * pow2 (r1) * tmp) - pow2 (r1)) / (2 * tmp),
687 for (size_t i = 0; i < sizeof entries / sizeof *entries; i++)
688 pivot_table_put1 (table, i, pivot_value_new_number (entries[i]));
691 pivot_table_submit (table);