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 reliability_destroy (struct reliability *rel)
115 ds_destroy (&rel->scale_name);
116 for (j = 0; j < rel->n_sc ; ++j)
119 free (rel->sc[j].items);
120 moments1_destroy (rel->sc[j].total);
121 for (x = 0; x < rel->sc[j].n_items; ++x)
122 free (rel->sc[j].m[x]);
127 free (rel->variables);
131 cmd_reliability (struct lexer *lexer, struct dataset *ds)
133 const struct dictionary *dict = dataset_dict (ds);
135 struct reliability reliability;
136 reliability.n_variables = 0;
137 reliability.variables = NULL;
138 reliability.model = MODEL_ALPHA;
139 reliability.exclude = MV_ANY;
140 reliability.summary = 0;
142 reliability.wv = dict_get_weight (dict);
144 reliability.total_start = 0;
146 lex_match (lexer, T_SLASH);
148 if (!lex_force_match_id (lexer, "VARIABLES"))
153 lex_match (lexer, T_EQUALS);
155 if (!parse_variables_const (lexer, dict, &reliability.variables, &reliability.n_variables,
156 PV_NO_DUPLICATE | PV_NUMERIC))
159 if (reliability.n_variables < 2)
160 msg (MW, _("Reliability on a single variable is not useful."));
166 /* Create a default Scale */
168 reliability.n_sc = 1;
169 reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc);
171 ds_init_cstr (&reliability.scale_name, "ANY");
173 c = &reliability.sc[0];
174 c->n_items = reliability.n_variables;
175 c->items = xzalloc (sizeof (struct variable*) * c->n_items);
177 for (i = 0 ; i < c->n_items ; ++i)
178 c->items[i] = reliability.variables[i];
183 while (lex_token (lexer) != T_ENDCMD)
185 lex_match (lexer, T_SLASH);
187 if (lex_match_id (lexer, "SCALE"))
189 struct const_var_set *vs;
190 if ( ! lex_force_match (lexer, T_LPAREN))
193 if ( ! lex_force_string (lexer) )
196 ds_assign_substring (&reliability.scale_name, lex_tokss (lexer));
200 if ( ! lex_force_match (lexer, T_RPAREN))
203 lex_match (lexer, T_EQUALS);
205 vs = const_var_set_create_from_array (reliability.variables, reliability.n_variables);
207 free (reliability.sc->items);
208 if (!parse_const_var_set_vars (lexer, vs, &reliability.sc->items, &reliability.sc->n_items, 0))
210 const_var_set_destroy (vs);
214 const_var_set_destroy (vs);
216 else if (lex_match_id (lexer, "MODEL"))
218 lex_match (lexer, T_EQUALS);
219 if (lex_match_id (lexer, "ALPHA"))
221 reliability.model = MODEL_ALPHA;
223 else if (lex_match_id (lexer, "SPLIT"))
225 reliability.model = MODEL_SPLIT;
226 reliability.split_point = -1;
228 if ( lex_match (lexer, T_LPAREN))
230 lex_force_num (lexer);
231 reliability.split_point = lex_number (lexer);
233 lex_force_match (lexer, T_RPAREN);
239 else if (lex_match_id (lexer, "SUMMARY"))
241 lex_match (lexer, T_EQUALS);
242 if (lex_match_id (lexer, "TOTAL"))
244 reliability.summary |= SUMMARY_TOTAL;
246 else if (lex_match (lexer, T_ALL))
248 reliability.summary = 0xFFFF;
253 else if (lex_match_id (lexer, "MISSING"))
255 lex_match (lexer, T_EQUALS);
256 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
258 if (lex_match_id (lexer, "INCLUDE"))
260 reliability.exclude = MV_SYSTEM;
262 else if (lex_match_id (lexer, "EXCLUDE"))
264 reliability.exclude = MV_ANY;
268 lex_error (lexer, NULL);
275 lex_error (lexer, NULL);
280 if ( reliability.model == MODEL_SPLIT)
283 const struct cronbach *s;
285 if ( reliability.split_point >= reliability.n_variables)
287 msg (ME, _("The split point must be less than the number of variables"));
291 reliability.n_sc += 2 ;
292 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
294 s = &reliability.sc[0];
296 reliability.sc[1].n_items =
297 (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
299 reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
300 reliability.sc[1].items = xzalloc (sizeof (struct variable *)
301 * reliability.sc[1].n_items);
303 reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
304 reliability.sc[2].n_items);
306 for (i = 0; i < reliability.sc[1].n_items ; ++i)
307 reliability.sc[1].items[i] = s->items[i];
309 while (i < s->n_items)
311 reliability.sc[2].items[i - reliability.sc[1].n_items] = s->items[i];
316 if ( reliability.summary & SUMMARY_TOTAL)
319 const int base_sc = reliability.n_sc;
321 reliability.total_start = base_sc;
323 reliability.n_sc += reliability.sc[0].n_items ;
324 reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
327 for (i = 0 ; i < reliability.sc[0].n_items; ++i )
331 struct cronbach *s = &reliability.sc[i + base_sc];
333 s->n_items = reliability.sc[0].n_items - 1;
334 s->items = xzalloc (sizeof (struct variable *) * s->n_items);
335 for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
338 s->items[v_dest++] = reliability.sc[0].items[v_src];
344 if ( ! run_reliability (ds, &reliability))
347 reliability_destroy (&reliability);
351 reliability_destroy (&reliability);
357 do_reliability (struct casereader *group, struct dataset *ds,
358 const struct reliability *rel);
361 static void reliability_summary_total (const struct reliability *rel);
363 static void reliability_statistics (const struct reliability *rel);
367 run_reliability (struct dataset *ds, const struct reliability *reliability)
369 struct dictionary *dict = dataset_dict (ds);
371 struct casereader *group;
373 struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
375 while (casegrouper_get_next_group (grouper, &group))
377 do_reliability (group, ds, reliability);
379 reliability_statistics (reliability);
381 if (reliability->summary & SUMMARY_TOTAL )
382 reliability_summary_total (reliability);
385 ok = casegrouper_destroy (grouper);
386 ok = proc_commit (ds) && ok;
395 /* Return the sum of all the item variables in S */
397 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
400 const struct cronbach *s = aux;
403 for (v = 0 ; v < s->n_items; ++v)
405 sum += case_data (c, s->items[v])->f;
412 case_processing_summary (casenumber n_valid, casenumber n_missing,
413 const struct dictionary *dict);
417 alpha (int k, double sum_of_variances, double variance_of_sums)
419 return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
423 do_reliability (struct casereader *input, struct dataset *ds,
424 const struct reliability *rel)
429 casenumber n_missing ;
430 casenumber n_valid = 0;
433 for (si = 0 ; si < rel->n_sc; ++si)
435 struct cronbach *s = &rel->sc[si];
437 s->m = xzalloc (sizeof (s->m) * s->n_items);
438 s->total = moments1_create (MOMENT_VARIANCE);
440 for (i = 0 ; i < s->n_items ; ++i )
441 s->m[i] = moments1_create (MOMENT_VARIANCE);
444 input = casereader_create_filter_missing (input,
451 for (si = 0 ; si < rel->n_sc; ++si)
453 struct cronbach *s = &rel->sc[si];
456 s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
458 casereader_create_append_numeric (input, append_sum,
462 for (; (c = casereader_read (input)) != NULL; case_unref (c))
467 for (si = 0; si < rel->n_sc; ++si)
469 struct cronbach *s = &rel->sc[si];
471 for (i = 0 ; i < s->n_items ; ++i )
472 moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
474 moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
477 casereader_destroy (input);
479 for (si = 0; si < rel->n_sc; ++si)
481 struct cronbach *s = &rel->sc[si];
483 s->sum_of_variances = 0;
484 for (i = 0 ; i < s->n_items ; ++i )
486 double weight, mean, variance;
487 moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
489 s->sum_of_variances += variance;
492 moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
496 alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
499 text_item_submit (text_item_create_format (TEXT_ITEM_PARAGRAPH, _("Scale: %s"),
500 ds_cstr (&rel->scale_name)));
502 case_processing_summary (n_valid, n_missing, dataset_dict (ds));
510 case_processing_summary (casenumber n_valid, casenumber n_missing,
511 const struct dictionary *dict)
513 const struct variable *wv = dict_get_weight (dict);
514 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
519 int heading_columns = 2;
520 int heading_rows = 1;
521 struct tab_table *tbl;
522 tbl = tab_create (n_cols, n_rows);
523 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
525 tab_title (tbl, _("Case Processing Summary"));
527 /* Vertical lines for the data only */
532 n_cols - 1, n_rows - 1);
534 /* Box around table */
539 n_cols - 1, n_rows - 1);
542 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
544 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
547 tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
550 tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
553 tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
556 tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
559 tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
562 tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%"));
564 total = n_missing + n_valid;
566 tab_double (tbl, 2, heading_rows, TAB_RIGHT,
570 tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
574 tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
578 tab_double (tbl, 3, heading_rows, TAB_RIGHT,
579 100 * n_valid / (double) total, NULL);
582 tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
583 100 * n_missing / (double) total, NULL);
586 tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
587 100 * total / (double) total, NULL);
596 reliability_summary_total (const struct reliability *rel)
599 const int n_cols = 5;
600 const int heading_columns = 1;
601 const int heading_rows = 1;
602 const int n_rows = rel->sc[0].n_items + heading_rows ;
604 struct tab_table *tbl = tab_create (n_cols, n_rows);
605 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
607 tab_title (tbl, _("Item-Total Statistics"));
609 /* Vertical lines for the data only */
614 n_cols - 1, n_rows - 1);
616 /* Box around table */
621 n_cols - 1, n_rows - 1);
624 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
626 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
628 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
629 _("Scale Mean if Item Deleted"));
631 tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
632 _("Scale Variance if Item Deleted"));
634 tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
635 _("Corrected Item-Total Correlation"));
637 tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
638 _("Cronbach's Alpha if Item Deleted"));
641 for (i = 0 ; i < rel->sc[0].n_items; ++i)
643 double cov, item_to_total_r;
644 double mean, weight, var;
646 const struct cronbach *s = &rel->sc[rel->total_start + i];
647 tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
648 var_to_string (rel->sc[0].items[i]));
650 moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
652 tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
655 tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
656 s->variance_of_sums, NULL);
658 tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
662 moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
663 cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
666 item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
669 tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
670 item_to_total_r, NULL);
678 static void reliability_statistics_model_alpha (struct tab_table *tbl,
679 const struct reliability *rel);
681 static void reliability_statistics_model_split (struct tab_table *tbl,
682 const struct reliability *rel);
685 struct reliability_output_table
691 void (*populate) (struct tab_table *, const struct reliability *);
695 static struct reliability_output_table rol[2] =
697 { 2, 2, 1, 1, reliability_statistics_model_alpha},
698 { 4, 9, 3, 0, reliability_statistics_model_split}
702 reliability_statistics (const struct reliability *rel)
704 int n_cols = rol[rel->model].n_cols;
705 int n_rows = rol[rel->model].n_rows;
706 int heading_columns = rol[rel->model].heading_cols;
707 int heading_rows = rol[rel->model].heading_rows;
709 struct tab_table *tbl = tab_create (n_cols, n_rows);
710 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
712 tab_title (tbl, _("Reliability Statistics"));
714 /* Vertical lines for the data only */
719 n_cols - 1, n_rows - 1);
721 /* Box around table */
726 n_cols - 1, n_rows - 1);
729 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
731 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
733 if ( rel->model == MODEL_ALPHA )
734 reliability_statistics_model_alpha (tbl, rel);
735 else if (rel->model == MODEL_SPLIT )
736 reliability_statistics_model_split (tbl, rel);
743 reliability_statistics_model_alpha (struct tab_table *tbl,
744 const struct reliability *rel)
746 const struct variable *wv = rel->wv;
747 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
749 const struct cronbach *s = &rel->sc[0];
751 tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
752 _("Cronbach's Alpha"));
754 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
757 tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
759 tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
764 reliability_statistics_model_split (struct tab_table *tbl,
765 const struct reliability *rel)
767 const struct variable *wv = rel->wv;
768 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
770 tab_text (tbl, 0, 0, TAB_LEFT,
771 _("Cronbach's Alpha"));
773 tab_text (tbl, 1, 0, TAB_LEFT,
776 tab_text (tbl, 2, 0, TAB_LEFT,
779 tab_text (tbl, 2, 1, TAB_LEFT,
784 tab_text (tbl, 1, 2, TAB_LEFT,
787 tab_text (tbl, 2, 2, TAB_LEFT,
790 tab_text (tbl, 2, 3, TAB_LEFT,
795 tab_text (tbl, 1, 4, TAB_LEFT,
796 _("Total N of Items"));
798 tab_text (tbl, 0, 5, TAB_LEFT,
799 _("Correlation Between Forms"));
802 tab_text (tbl, 0, 6, TAB_LEFT,
803 _("Spearman-Brown Coefficient"));
805 tab_text (tbl, 1, 6, TAB_LEFT,
808 tab_text (tbl, 1, 7, TAB_LEFT,
809 _("Unequal Length"));
812 tab_text (tbl, 0, 8, TAB_LEFT,
813 _("Guttman Split-Half Coefficient"));
817 tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL);
818 tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL);
820 tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt);
821 tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt);
823 tab_double (tbl, 3, 4, TAB_RIGHT,
824 rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
827 /* R is the correlation between the two parts */
828 double r = rel->sc[0].variance_of_sums -
829 rel->sc[1].variance_of_sums -
830 rel->sc[2].variance_of_sums ;
832 /* Guttman Split Half Coefficient */
833 double g = 2 * r / rel->sc[0].variance_of_sums;
835 /* Unequal Length Spearman Brown Coefficient, and
836 intermediate value used in the computation thereof */
839 r /= sqrt (rel->sc[1].variance_of_sums);
840 r /= sqrt (rel->sc[2].variance_of_sums);
843 tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
845 /* Equal length Spearman-Brown Coefficient */
846 tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
848 tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
850 tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
851 pow2 (rel->sc[0].n_items);
853 uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
857 tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);