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 reliability.n_sc += 2 ;
267 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
269 s = &reliability.sc[0];
271 reliability.sc[1].n_items =
272 (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
274 reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
275 reliability.sc[1].items = xzalloc (sizeof (struct variable *)
276 * reliability.sc[1].n_items);
278 reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
279 reliability.sc[2].n_items);
281 for (i = 0; i < reliability.sc[1].n_items ; ++i)
282 reliability.sc[1].items[i] = s->items[i];
284 while (i < s->n_items)
286 reliability.sc[2].items[i - reliability.sc[1].n_items] = s->items[i];
291 if ( reliability.summary & SUMMARY_TOTAL)
294 const int base_sc = reliability.n_sc;
296 reliability.total_start = base_sc;
298 reliability.n_sc += reliability.sc[0].n_items ;
299 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
302 for (i = 0 ; i < reliability.sc[0].n_items; ++i )
306 struct cronbach *s = &reliability.sc[i + base_sc];
308 s->n_items = reliability.sc[0].n_items - 1;
309 s->items = xzalloc (sizeof (struct variable *) * s->n_items);
310 for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
313 s->items[v_dest++] = reliability.sc[0].items[v_src];
319 if ( ! run_reliability (ds, &reliability))
322 free (reliability.variables);
326 free (reliability.variables);
332 do_reliability (struct casereader *group, struct dataset *ds,
333 const struct reliability *rel);
336 static void reliability_summary_total (const struct reliability *rel);
338 static void reliability_statistics (const struct reliability *rel);
342 run_reliability (struct dataset *ds, const struct reliability *reliability)
344 struct dictionary *dict = dataset_dict (ds);
346 struct casereader *group;
348 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
351 while (casegrouper_get_next_group (grouper, &group))
353 do_reliability (group, ds, reliability);
355 reliability_statistics (reliability);
357 if (reliability->summary & SUMMARY_TOTAL )
358 reliability_summary_total (reliability);
361 ok = casegrouper_destroy (grouper);
362 ok = proc_commit (ds) && ok;
371 /* Return the sum of all the item variables in S */
373 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
376 const struct cronbach *s = aux;
379 for (v = 0 ; v < s->n_items; ++v)
381 sum += case_data (c, s->items[v])->f;
388 case_processing_summary (casenumber n_valid, casenumber n_missing,
389 const struct dictionary *dict);
393 alpha (int k, double sum_of_variances, double variance_of_sums)
395 return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
399 do_reliability (struct casereader *input, struct dataset *ds,
400 const struct reliability *rel)
405 casenumber n_missing ;
406 casenumber n_valid = 0;
409 for (si = 0 ; si < rel->n_sc; ++si)
411 struct cronbach *s = &rel->sc[si];
413 s->m = xzalloc (sizeof (s->m) * s->n_items);
414 s->total = moments1_create (MOMENT_VARIANCE);
416 for (i = 0 ; i < s->n_items ; ++i )
417 s->m[i] = moments1_create (MOMENT_VARIANCE);
420 input = casereader_create_filter_missing (input,
427 for (si = 0 ; si < rel->n_sc; ++si)
429 struct cronbach *s = &rel->sc[si];
432 s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
434 casereader_create_append_numeric (input, append_sum,
438 for (; (c = casereader_read (input)) != NULL; case_unref (c))
443 for (si = 0; si < rel->n_sc; ++si)
445 struct cronbach *s = &rel->sc[si];
447 for (i = 0 ; i < s->n_items ; ++i )
448 moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
450 moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
453 casereader_destroy (input);
455 for (si = 0; si < rel->n_sc; ++si)
457 struct cronbach *s = &rel->sc[si];
459 s->sum_of_variances = 0;
460 for (i = 0 ; i < s->n_items ; ++i )
462 double weight, mean, variance;
463 moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
465 s->sum_of_variances += variance;
468 moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
472 alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
475 text_item_submit (text_item_create_format (TEXT_ITEM_PARAGRAPH, "Scale: %s",
476 ds_cstr (&rel->scale_name)));
478 case_processing_summary (n_valid, n_missing, dataset_dict (ds));
486 case_processing_summary (casenumber n_valid, casenumber n_missing,
487 const struct dictionary *dict)
489 const struct variable *wv = dict_get_weight (dict);
490 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
495 int heading_columns = 2;
496 int heading_rows = 1;
497 struct tab_table *tbl;
498 tbl = tab_create (n_cols, n_rows);
499 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
501 tab_title (tbl, _("Case Processing Summary"));
503 /* Vertical lines for the data only */
508 n_cols - 1, n_rows - 1);
510 /* Box around table */
515 n_cols - 1, n_rows - 1);
518 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
520 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
523 tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
526 tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
529 tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
532 tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
535 tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
538 tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%"));
540 total = n_missing + n_valid;
542 tab_double (tbl, 2, heading_rows, TAB_RIGHT,
546 tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
550 tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
554 tab_double (tbl, 3, heading_rows, TAB_RIGHT,
555 100 * n_valid / (double) total, NULL);
558 tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
559 100 * n_missing / (double) total, NULL);
562 tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
563 100 * total / (double) total, NULL);
572 reliability_summary_total (const struct reliability *rel)
575 const int n_cols = 5;
576 const int heading_columns = 1;
577 const int heading_rows = 1;
578 const int n_rows = rel->sc[0].n_items + heading_rows ;
580 struct tab_table *tbl = tab_create (n_cols, n_rows);
581 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
583 tab_title (tbl, _("Item-Total Statistics"));
585 /* Vertical lines for the data only */
590 n_cols - 1, n_rows - 1);
592 /* Box around table */
597 n_cols - 1, n_rows - 1);
600 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
602 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
604 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
605 _("Scale Mean if Item Deleted"));
607 tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
608 _("Scale Variance if Item Deleted"));
610 tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
611 _("Corrected Item-Total Correlation"));
613 tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
614 _("Cronbach's Alpha if Item Deleted"));
617 for (i = 0 ; i < rel->sc[0].n_items; ++i)
619 double cov, item_to_total_r;
620 double mean, weight, var;
622 const struct cronbach *s = &rel->sc[rel->total_start + i];
623 tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
624 var_to_string (rel->sc[0].items[i]));
626 moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
628 tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
631 tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
632 s->variance_of_sums, NULL);
634 tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
638 moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
639 cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
642 item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
645 tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
646 item_to_total_r, NULL);
654 static void reliability_statistics_model_alpha (struct tab_table *tbl,
655 const struct reliability *rel);
657 static void reliability_statistics_model_split (struct tab_table *tbl,
658 const struct reliability *rel);
661 struct reliability_output_table
667 void (*populate) (struct tab_table *, const struct reliability *);
671 static struct reliability_output_table rol[2] =
673 { 2, 2, 1, 1, reliability_statistics_model_alpha},
674 { 4, 9, 3, 0, reliability_statistics_model_split}
678 reliability_statistics (const struct reliability *rel)
680 int n_cols = rol[rel->model].n_cols;
681 int n_rows = rol[rel->model].n_rows;
682 int heading_columns = rol[rel->model].heading_cols;
683 int heading_rows = rol[rel->model].heading_rows;
685 struct tab_table *tbl = tab_create (n_cols, n_rows);
686 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
688 tab_title (tbl, _("Reliability Statistics"));
690 /* Vertical lines for the data only */
695 n_cols - 1, n_rows - 1);
697 /* Box around table */
702 n_cols - 1, n_rows - 1);
705 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
707 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
709 if ( rel->model == MODEL_ALPHA )
710 reliability_statistics_model_alpha (tbl, rel);
711 else if (rel->model == MODEL_SPLIT )
712 reliability_statistics_model_split (tbl, rel);
719 reliability_statistics_model_alpha (struct tab_table *tbl,
720 const struct reliability *rel)
722 const struct variable *wv = rel->wv;
723 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
725 const struct cronbach *s = &rel->sc[0];
727 tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
728 _("Cronbach's Alpha"));
730 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
733 tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
735 tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
740 reliability_statistics_model_split (struct tab_table *tbl,
741 const struct reliability *rel)
743 const struct variable *wv = rel->wv;
744 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
746 tab_text (tbl, 0, 0, TAB_LEFT,
747 _("Cronbach's Alpha"));
749 tab_text (tbl, 1, 0, TAB_LEFT,
752 tab_text (tbl, 2, 0, TAB_LEFT,
755 tab_text (tbl, 2, 1, TAB_LEFT,
760 tab_text (tbl, 1, 2, TAB_LEFT,
763 tab_text (tbl, 2, 2, TAB_LEFT,
766 tab_text (tbl, 2, 3, TAB_LEFT,
771 tab_text (tbl, 1, 4, TAB_LEFT,
772 _("Total N of Items"));
774 tab_text (tbl, 0, 5, TAB_LEFT,
775 _("Correlation Between Forms"));
778 tab_text (tbl, 0, 6, TAB_LEFT,
779 _("Spearman-Brown Coefficient"));
781 tab_text (tbl, 1, 6, TAB_LEFT,
784 tab_text (tbl, 1, 7, TAB_LEFT,
785 _("Unequal Length"));
788 tab_text (tbl, 0, 8, TAB_LEFT,
789 _("Guttman Split-Half Coefficient"));
793 tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL);
794 tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL);
796 tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt);
797 tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt);
799 tab_double (tbl, 3, 4, TAB_RIGHT,
800 rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
803 /* R is the correlation between the two parts */
804 double r = rel->sc[0].variance_of_sums -
805 rel->sc[1].variance_of_sums -
806 rel->sc[2].variance_of_sums ;
808 /* Guttman Split Half Coefficient */
809 double g = 2 * r / rel->sc[0].variance_of_sums;
811 /* Unequal Length Spearman Brown Coefficient, and
812 intermediate value used in the computation thereof */
815 r /= sqrt (rel->sc[1].variance_of_sums);
816 r /= sqrt (rel->sc[2].variance_of_sums);
819 tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
821 /* Equal length Spearman-Brown Coefficient */
822 tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
824 tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
826 tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
827 pow2 (rel->sc[0].n_items);
829 uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
833 tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);