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/>. */
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/tab.h"
35 #include "output/text-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 cmd_reliability (struct lexer *lexer, struct dataset *ds)
114 const struct dictionary *dict = dataset_dict (ds);
116 struct reliability reliability;
117 reliability.n_variables = 0;
118 reliability.variables = NULL;
119 reliability.model = MODEL_ALPHA;
120 reliability.exclude = MV_ANY;
121 reliability.summary = 0;
123 reliability.wv = dict_get_weight (dict);
125 reliability.total_start = 0;
127 lex_match (lexer, T_SLASH);
129 if (!lex_force_match_id (lexer, "VARIABLES"))
134 lex_match (lexer, T_EQUALS);
136 if (!parse_variables_const (lexer, dict, &reliability.variables, &reliability.n_variables,
137 PV_NO_DUPLICATE | PV_NUMERIC))
140 if (reliability.n_variables < 2)
141 msg (MW, _("Reliability on a single variable is not useful."));
147 /* Create a default Scale */
149 reliability.n_sc = 1;
150 reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc);
152 ds_init_cstr (&reliability.scale_name, "ANY");
154 c = &reliability.sc[0];
155 c->n_items = reliability.n_variables;
156 c->items = xzalloc (sizeof (struct variable*) * c->n_items);
158 for (i = 0 ; i < c->n_items ; ++i)
159 c->items[i] = reliability.variables[i];
164 while (lex_token (lexer) != T_ENDCMD)
166 lex_match (lexer, T_SLASH);
168 if (lex_match_id (lexer, "SCALE"))
170 struct const_var_set *vs;
171 if ( ! lex_force_match (lexer, T_LPAREN))
174 if ( ! lex_force_string (lexer) )
177 ds_init_substring (&reliability.scale_name, lex_tokss (lexer));
181 if ( ! lex_force_match (lexer, T_RPAREN))
184 lex_match (lexer, T_EQUALS);
186 vs = const_var_set_create_from_array (reliability.variables, reliability.n_variables);
189 if (!parse_const_var_set_vars (lexer, vs, &reliability.sc->items, &reliability.sc->n_items, 0))
191 const_var_set_destroy (vs);
195 const_var_set_destroy (vs);
197 else if (lex_match_id (lexer, "MODEL"))
199 lex_match (lexer, T_EQUALS);
200 if (lex_match_id (lexer, "ALPHA"))
202 reliability.model = MODEL_ALPHA;
204 else if (lex_match_id (lexer, "SPLIT"))
206 reliability.model = MODEL_SPLIT;
207 reliability.split_point = -1;
209 if ( lex_match (lexer, T_LPAREN))
211 lex_force_num (lexer);
212 reliability.split_point = lex_number (lexer);
214 lex_force_match (lexer, T_RPAREN);
220 else if (lex_match_id (lexer, "SUMMARY"))
222 lex_match (lexer, T_EQUALS);
223 if (lex_match_id (lexer, "TOTAL"))
225 reliability.summary |= SUMMARY_TOTAL;
227 else if (lex_match (lexer, T_ALL))
229 reliability.summary = 0xFFFF;
234 else if (lex_match_id (lexer, "MISSING"))
236 lex_match (lexer, T_EQUALS);
237 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
239 if (lex_match_id (lexer, "INCLUDE"))
241 reliability.exclude = MV_SYSTEM;
243 else if (lex_match_id (lexer, "EXCLUDE"))
245 reliability.exclude = MV_ANY;
249 lex_error (lexer, NULL);
256 lex_error (lexer, NULL);
261 if ( reliability.model == MODEL_SPLIT)
264 const struct cronbach *s;
266 if ( reliability.split_point >= reliability.n_variables)
268 msg (ME, _("The split point must be less than the number of variables"));
272 reliability.n_sc += 2 ;
273 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
275 s = &reliability.sc[0];
277 reliability.sc[1].n_items =
278 (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
280 reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
281 reliability.sc[1].items = xzalloc (sizeof (struct variable *)
282 * reliability.sc[1].n_items);
284 reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
285 reliability.sc[2].n_items);
287 for (i = 0; i < reliability.sc[1].n_items ; ++i)
288 reliability.sc[1].items[i] = s->items[i];
290 while (i < s->n_items)
292 reliability.sc[2].items[i - reliability.sc[1].n_items] = s->items[i];
297 if ( reliability.summary & SUMMARY_TOTAL)
300 const int base_sc = reliability.n_sc;
302 reliability.total_start = base_sc;
304 reliability.n_sc += reliability.sc[0].n_items ;
305 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
308 for (i = 0 ; i < reliability.sc[0].n_items; ++i )
312 struct cronbach *s = &reliability.sc[i + base_sc];
314 s->n_items = reliability.sc[0].n_items - 1;
315 s->items = xzalloc (sizeof (struct variable *) * s->n_items);
316 for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
319 s->items[v_dest++] = reliability.sc[0].items[v_src];
325 if ( ! run_reliability (ds, &reliability))
328 free (reliability.variables);
332 free (reliability.variables);
338 do_reliability (struct casereader *group, struct dataset *ds,
339 const struct reliability *rel);
342 static void reliability_summary_total (const struct reliability *rel);
344 static void reliability_statistics (const struct reliability *rel);
348 run_reliability (struct dataset *ds, const struct reliability *reliability)
350 struct dictionary *dict = dataset_dict (ds);
352 struct casereader *group;
354 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
357 while (casegrouper_get_next_group (grouper, &group))
359 do_reliability (group, ds, reliability);
361 reliability_statistics (reliability);
363 if (reliability->summary & SUMMARY_TOTAL )
364 reliability_summary_total (reliability);
367 ok = casegrouper_destroy (grouper);
368 ok = proc_commit (ds) && ok;
377 /* Return the sum of all the item variables in S */
379 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
382 const struct cronbach *s = aux;
385 for (v = 0 ; v < s->n_items; ++v)
387 sum += case_data (c, s->items[v])->f;
394 case_processing_summary (casenumber n_valid, casenumber n_missing,
395 const struct dictionary *dict);
399 alpha (int k, double sum_of_variances, double variance_of_sums)
401 return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
405 do_reliability (struct casereader *input, struct dataset *ds,
406 const struct reliability *rel)
411 casenumber n_missing ;
412 casenumber n_valid = 0;
415 for (si = 0 ; si < rel->n_sc; ++si)
417 struct cronbach *s = &rel->sc[si];
419 s->m = xzalloc (sizeof (s->m) * s->n_items);
420 s->total = moments1_create (MOMENT_VARIANCE);
422 for (i = 0 ; i < s->n_items ; ++i )
423 s->m[i] = moments1_create (MOMENT_VARIANCE);
426 input = casereader_create_filter_missing (input,
433 for (si = 0 ; si < rel->n_sc; ++si)
435 struct cronbach *s = &rel->sc[si];
438 s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
440 casereader_create_append_numeric (input, append_sum,
444 for (; (c = casereader_read (input)) != NULL; case_unref (c))
449 for (si = 0; si < rel->n_sc; ++si)
451 struct cronbach *s = &rel->sc[si];
453 for (i = 0 ; i < s->n_items ; ++i )
454 moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
456 moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
459 casereader_destroy (input);
461 for (si = 0; si < rel->n_sc; ++si)
463 struct cronbach *s = &rel->sc[si];
465 s->sum_of_variances = 0;
466 for (i = 0 ; i < s->n_items ; ++i )
468 double weight, mean, variance;
469 moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
471 s->sum_of_variances += variance;
474 moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
478 alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
481 text_item_submit (text_item_create_format (TEXT_ITEM_PARAGRAPH, _("Scale: %s"),
482 ds_cstr (&rel->scale_name)));
484 case_processing_summary (n_valid, n_missing, dataset_dict (ds));
492 case_processing_summary (casenumber n_valid, casenumber n_missing,
493 const struct dictionary *dict)
495 const struct variable *wv = dict_get_weight (dict);
496 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
501 int heading_columns = 2;
502 int heading_rows = 1;
503 struct tab_table *tbl;
504 tbl = tab_create (n_cols, n_rows);
505 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
507 tab_title (tbl, _("Case Processing Summary"));
509 /* Vertical lines for the data only */
514 n_cols - 1, n_rows - 1);
516 /* Box around table */
521 n_cols - 1, n_rows - 1);
524 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
526 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
529 tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
532 tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
535 tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
538 tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
541 tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
544 tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%"));
546 total = n_missing + n_valid;
548 tab_double (tbl, 2, heading_rows, TAB_RIGHT,
552 tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
556 tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
560 tab_double (tbl, 3, heading_rows, TAB_RIGHT,
561 100 * n_valid / (double) total, NULL);
564 tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
565 100 * n_missing / (double) total, NULL);
568 tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
569 100 * total / (double) total, NULL);
578 reliability_summary_total (const struct reliability *rel)
581 const int n_cols = 5;
582 const int heading_columns = 1;
583 const int heading_rows = 1;
584 const int n_rows = rel->sc[0].n_items + heading_rows ;
586 struct tab_table *tbl = tab_create (n_cols, n_rows);
587 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
589 tab_title (tbl, _("Item-Total Statistics"));
591 /* Vertical lines for the data only */
596 n_cols - 1, n_rows - 1);
598 /* Box around table */
603 n_cols - 1, n_rows - 1);
606 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
608 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
610 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
611 _("Scale Mean if Item Deleted"));
613 tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
614 _("Scale Variance if Item Deleted"));
616 tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
617 _("Corrected Item-Total Correlation"));
619 tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
620 _("Cronbach's Alpha if Item Deleted"));
623 for (i = 0 ; i < rel->sc[0].n_items; ++i)
625 double cov, item_to_total_r;
626 double mean, weight, var;
628 const struct cronbach *s = &rel->sc[rel->total_start + i];
629 tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
630 var_to_string (rel->sc[0].items[i]));
632 moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
634 tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
637 tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
638 s->variance_of_sums, NULL);
640 tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
644 moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
645 cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
648 item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
651 tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
652 item_to_total_r, NULL);
660 static void reliability_statistics_model_alpha (struct tab_table *tbl,
661 const struct reliability *rel);
663 static void reliability_statistics_model_split (struct tab_table *tbl,
664 const struct reliability *rel);
667 struct reliability_output_table
673 void (*populate) (struct tab_table *, const struct reliability *);
677 static struct reliability_output_table rol[2] =
679 { 2, 2, 1, 1, reliability_statistics_model_alpha},
680 { 4, 9, 3, 0, reliability_statistics_model_split}
684 reliability_statistics (const struct reliability *rel)
686 int n_cols = rol[rel->model].n_cols;
687 int n_rows = rol[rel->model].n_rows;
688 int heading_columns = rol[rel->model].heading_cols;
689 int heading_rows = rol[rel->model].heading_rows;
691 struct tab_table *tbl = tab_create (n_cols, n_rows);
692 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
694 tab_title (tbl, _("Reliability Statistics"));
696 /* Vertical lines for the data only */
701 n_cols - 1, n_rows - 1);
703 /* Box around table */
708 n_cols - 1, n_rows - 1);
711 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
713 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
715 if ( rel->model == MODEL_ALPHA )
716 reliability_statistics_model_alpha (tbl, rel);
717 else if (rel->model == MODEL_SPLIT )
718 reliability_statistics_model_split (tbl, rel);
725 reliability_statistics_model_alpha (struct tab_table *tbl,
726 const struct reliability *rel)
728 const struct variable *wv = rel->wv;
729 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
731 const struct cronbach *s = &rel->sc[0];
733 tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
734 _("Cronbach's Alpha"));
736 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
739 tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
741 tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
746 reliability_statistics_model_split (struct tab_table *tbl,
747 const struct reliability *rel)
749 const struct variable *wv = rel->wv;
750 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
752 tab_text (tbl, 0, 0, TAB_LEFT,
753 _("Cronbach's Alpha"));
755 tab_text (tbl, 1, 0, TAB_LEFT,
758 tab_text (tbl, 2, 0, TAB_LEFT,
761 tab_text (tbl, 2, 1, TAB_LEFT,
766 tab_text (tbl, 1, 2, TAB_LEFT,
769 tab_text (tbl, 2, 2, TAB_LEFT,
772 tab_text (tbl, 2, 3, TAB_LEFT,
777 tab_text (tbl, 1, 4, TAB_LEFT,
778 _("Total N of Items"));
780 tab_text (tbl, 0, 5, TAB_LEFT,
781 _("Correlation Between Forms"));
784 tab_text (tbl, 0, 6, TAB_LEFT,
785 _("Spearman-Brown Coefficient"));
787 tab_text (tbl, 1, 6, TAB_LEFT,
790 tab_text (tbl, 1, 7, TAB_LEFT,
791 _("Unequal Length"));
794 tab_text (tbl, 0, 8, TAB_LEFT,
795 _("Guttman Split-Half Coefficient"));
799 tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL);
800 tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL);
802 tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt);
803 tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt);
805 tab_double (tbl, 3, 4, TAB_RIGHT,
806 rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
809 /* R is the correlation between the two parts */
810 double r = rel->sc[0].variance_of_sums -
811 rel->sc[1].variance_of_sums -
812 rel->sc[2].variance_of_sums ;
814 /* Guttman Split Half Coefficient */
815 double g = 2 * r / rel->sc[0].variance_of_sums;
817 /* Unequal Length Spearman Brown Coefficient, and
818 intermediate value used in the computation thereof */
821 r /= sqrt (rel->sc[1].variance_of_sums);
822 r /= sqrt (rel->sc[2].variance_of_sums);
825 tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
827 /* Equal length Spearman-Brown Coefficient */
828 tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
830 tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
832 tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
833 pow2 (rel->sc[0].n_items);
835 uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
839 tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);