1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008, 2009 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/>. */
23 #define _(msgid) gettext (msgid)
24 #define N_(msgid) msgid
26 #include <data/variable.h>
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/casereader.h>
30 #include <data/casegrouper.h>
31 #include <math/moments.h>
32 #include <data/case.h>
34 #include <language/command.h>
36 #include <output/manager.h>
37 #include <output/table.h>
43 *^variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
45 missing=miss:!exclude/include;
54 static int rel_custom_scale (struct lexer *lexer, struct dataset *ds,
55 struct cmd_reliability *p, void *aux);
57 static int rel_custom_model (struct lexer *, struct dataset *,
58 struct cmd_reliability *, void *);
60 int cmd_reliability (struct lexer *lexer, struct dataset *ds);
64 const struct variable **items;
67 double sum_of_variances;
68 double variance_of_sums;
69 int totals_idx; /* Casereader index into the totals */
71 struct moments1 **m ; /* Moments of the items */
72 struct moments1 *total ; /* Moments of the totals */
77 dump_cronbach (const struct cronbach *s)
80 printf ("N items %d\n", s->n_items);
81 for (i = 0 ; i < s->n_items; ++i)
83 printf ("%s\n", var_get_name (s->items[i]));
86 printf ("Totals idx %d\n", s->totals_idx);
88 printf ("scale variance %g\n", s->variance_of_sums);
89 printf ("alpha %g\n", s->alpha);
103 const struct dictionary *dict;
104 const struct variable **variables;
106 enum mv_class exclude;
113 struct string scale_name;
121 alpha (int k, double sum_of_variances, double variance_of_sums)
123 return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
126 static void reliability_summary_total (const struct reliability *rel);
128 static void reliability_statistics (const struct reliability *rel);
133 run_reliability (struct casereader *group, struct dataset *ds,
134 struct reliability *rel);
138 cmd_reliability (struct lexer *lexer, struct dataset *ds)
142 struct casegrouper *grouper;
143 struct casereader *group;
144 struct cmd_reliability cmd;
146 struct reliability rel = {NULL,
147 NULL, 0, MV_ANY, NULL, 0, -1,
148 DS_EMPTY_INITIALIZER,
151 cmd.v_variables = NULL;
153 if ( ! parse_reliability (lexer, ds, &cmd, &rel) )
158 rel.dict = dataset_dict (ds);
159 rel.variables = cmd.v_variables;
160 rel.n_variables = cmd.n_variables;
161 rel.exclude = MV_ANY;
167 /* Create a default Scale */
170 rel.sc = xzalloc (sizeof (struct cronbach) * rel.n_sc);
172 ds_init_cstr (&rel.scale_name, "ANY");
175 c->n_items = cmd.n_variables;
176 c->items = xzalloc (sizeof (struct variable*) * c->n_items);
178 for (i = 0 ; i < c->n_items ; ++i)
179 c->items[i] = cmd.v_variables[i];
182 if ( cmd.miss == REL_INCLUDE)
183 rel.exclude = MV_SYSTEM;
185 if ( rel.model == MODEL_SPLIT)
188 const struct cronbach *s;
191 rel.sc = xrealloc (rel.sc, sizeof (struct cronbach) * rel.n_sc);
196 (rel.split_point == -1) ? s->n_items / 2 : rel.split_point;
198 rel.sc[2].n_items = s->n_items - rel.sc[1].n_items;
199 rel.sc[1].items = xzalloc (sizeof (struct variable *)
200 * rel.sc[1].n_items);
202 rel.sc[2].items = xzalloc (sizeof (struct variable *) *
205 for (i = 0; i < rel.sc[1].n_items ; ++i)
206 rel.sc[1].items[i] = s->items[i];
208 while (i < s->n_items)
210 rel.sc[2].items[i - rel.sc[1].n_items] = s->items[i];
215 if (cmd.a_summary[REL_SUM_TOTAL])
218 const int base_sc = rel.n_sc;
220 rel.total_start = base_sc;
222 rel.n_sc += rel.sc[0].n_items ;
223 rel.sc = xrealloc (rel.sc, sizeof (struct cronbach) * rel.n_sc);
225 for (i = 0 ; i < rel.sc[0].n_items; ++i )
229 struct cronbach *s = &rel.sc[i + base_sc];
231 s->n_items = rel.sc[0].n_items - 1;
232 s->items = xzalloc (sizeof (struct variable *) * s->n_items);
233 for (v_src = 0 ; v_src < rel.sc[0].n_items ; ++v_src)
236 s->items[v_dest++] = rel.sc[0].items[v_src];
242 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
243 while (casegrouper_get_next_group (grouper, &group))
245 run_reliability (group, ds, &rel);
247 reliability_statistics (&rel);
249 if (cmd.a_summary[REL_SUM_TOTAL])
250 reliability_summary_total (&rel);
252 ok = casegrouper_destroy (grouper);
253 ok = proc_commit (ds) && ok;
255 free_reliability (&cmd);
259 /* Free all the stuff */
260 for (i = 0 ; i < rel.n_sc; ++i)
263 struct cronbach *c = &rel.sc[i];
266 moments1_destroy (c->total);
269 for (x = 0 ; x < c->n_items; ++x)
270 moments1_destroy (c->m[x]);
275 ds_destroy (&rel.scale_name);
284 /* Return the sum of all the item variables in S */
286 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
289 const struct cronbach *s = aux;
292 for (v = 0 ; v < s->n_items; ++v)
294 sum += case_data (c, s->items[v])->f;
301 static void case_processing_summary (casenumber n_valid, casenumber n_missing,
302 const struct dictionary *dict);
305 run_reliability (struct casereader *input, struct dataset *ds,
306 struct reliability *rel)
311 casenumber n_missing ;
312 casenumber n_valid = 0;
315 for (si = 0 ; si < rel->n_sc; ++si)
317 struct cronbach *s = &rel->sc[si];
319 s->m = xzalloc (sizeof (s->m) * s->n_items);
320 s->total = moments1_create (MOMENT_VARIANCE);
322 for (i = 0 ; i < s->n_items ; ++i )
323 s->m[i] = moments1_create (MOMENT_VARIANCE);
326 input = casereader_create_filter_missing (input,
333 for (si = 0 ; si < rel->n_sc; ++si)
335 struct cronbach *s = &rel->sc[si];
338 s->totals_idx = casereader_get_value_cnt (input);
340 casereader_create_append_numeric (input, append_sum,
344 for (; (c = casereader_read (input)) != NULL; case_unref (c))
349 for (si = 0; si < rel->n_sc; ++si)
351 struct cronbach *s = &rel->sc[si];
353 for (i = 0 ; i < s->n_items ; ++i )
354 moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
356 moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
359 casereader_destroy (input);
361 for (si = 0; si < rel->n_sc; ++si)
363 struct cronbach *s = &rel->sc[si];
365 s->sum_of_variances = 0;
366 for (i = 0 ; i < s->n_items ; ++i )
368 double weight, mean, variance;
369 moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
371 s->sum_of_variances += variance;
374 moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
378 alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
383 struct tab_table *tab = tab_create(1, 1, 0);
385 tab_dim (tab, tab_natural_dimensions);
386 tab_flags (tab, SOMF_NO_TITLE );
388 tab_text(tab, 0, 0, TAT_PRINTF, "Scale: %s", ds_cstr (&rel->scale_name));
394 case_processing_summary (n_valid, n_missing, dataset_dict (ds));
398 static void reliability_statistics_model_alpha (struct tab_table *tbl,
399 const struct reliability *rel);
401 static void reliability_statistics_model_split (struct tab_table *tbl,
402 const struct reliability *rel);
404 struct reliability_output_table
410 void (*populate) (struct tab_table *, const struct reliability *);
413 static struct reliability_output_table rol[2] =
415 { 2, 2, 1, 1, reliability_statistics_model_alpha},
416 { 4, 9, 3, 0, reliability_statistics_model_split}
420 reliability_statistics (const struct reliability *rel)
422 int n_cols = rol[rel->model].n_cols;
423 int n_rows = rol[rel->model].n_rows;
424 int heading_columns = rol[rel->model].heading_cols;
425 int heading_rows = rol[rel->model].heading_rows;
427 struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
428 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
430 tab_dim (tbl, tab_natural_dimensions);
432 tab_title (tbl, _("Reliability Statistics"));
434 /* Vertical lines for the data only */
439 n_cols - 1, n_rows - 1);
441 /* Box around table */
446 n_cols - 1, n_rows - 1);
449 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
451 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
453 if ( rel->model == MODEL_ALPHA )
454 reliability_statistics_model_alpha (tbl, rel);
455 else if (rel->model == MODEL_SPLIT )
456 reliability_statistics_model_split (tbl, rel);
462 reliability_summary_total (const struct reliability *rel)
465 const int n_cols = 5;
466 const int heading_columns = 1;
467 const int heading_rows = 1;
468 const int n_rows = rel->sc[0].n_items + heading_rows ;
470 struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
471 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
473 tab_dim (tbl, tab_natural_dimensions);
475 tab_title (tbl, _("Item-Total Statistics"));
477 /* Vertical lines for the data only */
482 n_cols - 1, n_rows - 1);
484 /* Box around table */
489 n_cols - 1, n_rows - 1);
492 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
494 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
496 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
497 _("Scale Mean if Item Deleted"));
499 tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
500 _("Scale Variance if Item Deleted"));
502 tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
503 _("Corrected Item-Total Correlation"));
505 tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
506 _("Cronbach's Alpha if Item Deleted"));
509 for (i = 0 ; i < rel->sc[0].n_items; ++i)
511 double cov, item_to_total_r;
512 double mean, weight, var;
514 const struct cronbach *s = &rel->sc[rel->total_start + i];
515 tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
516 var_to_string (rel->sc[0].items[i]));
518 moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
520 tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
523 tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
524 s->variance_of_sums, NULL);
526 tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
530 moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
531 cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
534 item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
537 tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
538 item_to_total_r, NULL);
547 reliability_statistics_model_alpha (struct tab_table *tbl,
548 const struct reliability *rel)
550 const struct variable *wv = dict_get_weight (rel->dict);
551 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
553 const struct cronbach *s = &rel->sc[0];
555 tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
556 _("Cronbach's Alpha"));
558 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
561 tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
563 tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
568 reliability_statistics_model_split (struct tab_table *tbl,
569 const struct reliability *rel)
571 const struct variable *wv = dict_get_weight (rel->dict);
572 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
574 tab_text (tbl, 0, 0, TAB_LEFT,
575 _("Cronbach's Alpha"));
577 tab_text (tbl, 1, 0, TAB_LEFT,
580 tab_text (tbl, 2, 0, TAB_LEFT,
583 tab_text (tbl, 2, 1, TAB_LEFT,
588 tab_text (tbl, 1, 2, TAB_LEFT,
591 tab_text (tbl, 2, 2, TAB_LEFT,
594 tab_text (tbl, 2, 3, TAB_LEFT,
599 tab_text (tbl, 1, 4, TAB_LEFT,
600 _("Total N of Items"));
602 tab_text (tbl, 0, 5, TAB_LEFT,
603 _("Correlation Between Forms"));
606 tab_text (tbl, 0, 6, TAB_LEFT,
607 _("Spearman-Brown Coefficient"));
609 tab_text (tbl, 1, 6, TAB_LEFT,
612 tab_text (tbl, 1, 7, TAB_LEFT,
613 _("Unequal Length"));
616 tab_text (tbl, 0, 8, TAB_LEFT,
617 _("Guttman Split-Half Coefficient"));
621 tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL);
622 tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL);
624 tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt);
625 tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt);
627 tab_double (tbl, 3, 4, TAB_RIGHT,
628 rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
631 /* R is the correlation between the two parts */
632 double r = rel->sc[0].variance_of_sums -
633 rel->sc[1].variance_of_sums -
634 rel->sc[2].variance_of_sums ;
636 /* Guttman Split Half Coefficient */
637 double g = 2 * r / rel->sc[0].variance_of_sums;
639 /* Unequal Length Spearman Brown Coefficient, and
640 intermediate value used in the computation thereof */
643 r /= sqrt (rel->sc[1].variance_of_sums);
644 r /= sqrt (rel->sc[2].variance_of_sums);
647 tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
649 /* Equal length Spearman-Brown Coefficient */
650 tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
652 tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
654 tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
655 pow2 (rel->sc[0].n_items);
657 uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
661 tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);
668 case_processing_summary (casenumber n_valid, casenumber n_missing,
669 const struct dictionary *dict)
671 const struct variable *wv = dict_get_weight (dict);
672 const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
677 int heading_columns = 2;
678 int heading_rows = 1;
679 struct tab_table *tbl;
680 tbl = tab_create (n_cols, n_rows, 0);
681 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
683 tab_dim (tbl, tab_natural_dimensions);
685 tab_title (tbl, _("Case Processing Summary"));
687 /* Vertical lines for the data only */
692 n_cols - 1, n_rows - 1);
694 /* Box around table */
699 n_cols - 1, n_rows - 1);
702 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
704 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
707 tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
710 tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
713 tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
716 tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
719 tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
722 tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE | TAT_PRINTF,
725 total = n_missing + n_valid;
727 tab_double (tbl, 2, heading_rows, TAB_RIGHT,
731 tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
735 tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
739 tab_double (tbl, 3, heading_rows, TAB_RIGHT,
740 100 * n_valid / (double) total, NULL);
743 tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
744 100 * n_missing / (double) total, NULL);
747 tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
748 100 * total / (double) total, NULL);
755 rel_custom_model (struct lexer *lexer, struct dataset *ds UNUSED,
756 struct cmd_reliability *cmd UNUSED, void *aux)
758 struct reliability *rel = aux;
760 if (lex_match_id (lexer, "ALPHA"))
762 rel->model = MODEL_ALPHA;
764 else if (lex_match_id (lexer, "SPLIT"))
766 rel->model = MODEL_SPLIT;
767 rel->split_point = -1;
768 if ( lex_match (lexer, '('))
770 lex_force_num (lexer);
771 rel->split_point = lex_number (lexer);
773 lex_force_match (lexer, ')');
785 rel_custom_scale (struct lexer *lexer, struct dataset *ds UNUSED,
786 struct cmd_reliability *p, void *aux)
788 struct const_var_set *vs;
789 struct reliability *rel = aux;
790 struct cronbach *scale;
793 rel->sc = xzalloc (sizeof (struct cronbach) * rel->n_sc);
796 if ( ! lex_force_match (lexer, '(')) return 0;
798 if ( ! lex_force_string (lexer) ) return 0;
800 ds_init_string (&rel->scale_name, lex_tokstr (lexer));
804 if ( ! lex_force_match (lexer, ')')) return 0;
806 lex_match (lexer, '=');
808 vs = const_var_set_create_from_array (p->v_variables, p->n_variables);
810 if (!parse_const_var_set_vars (lexer, vs, &scale->items, &scale->n_items, 0))
812 const_var_set_destroy (vs);
816 const_var_set_destroy (vs);