1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008 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 variable **variables;
105 enum mv_class exclude;
112 struct string scale_name;
120 alpha (int k, double sum_of_variances, double variance_of_sums)
122 return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
125 static void reliability_summary_total (const struct reliability *rel);
127 static void reliability_statistics (const struct reliability *rel);
132 run_reliability (struct casereader *group, struct dataset *ds,
133 struct reliability *rel);
137 cmd_reliability (struct lexer *lexer, struct dataset *ds)
141 struct casegrouper *grouper;
142 struct casereader *group;
143 struct cmd_reliability cmd;
145 struct reliability rel = {
146 NULL, 0, MV_ANY, NULL, 0, -1,
147 DS_EMPTY_INITIALIZER,
150 cmd.v_variables = NULL;
152 if ( ! parse_reliability (lexer, ds, &cmd, &rel) )
157 rel.variables = cmd.v_variables;
158 rel.n_variables = cmd.n_variables;
159 rel.exclude = MV_ANY;
165 /* Create a default Scale */
168 rel.sc = xzalloc (sizeof (struct cronbach) * rel.n_sc);
170 ds_init_cstr (&rel.scale_name, "ANY");
173 c->n_items = cmd.n_variables;
174 c->items = xzalloc (sizeof (struct variable*) * c->n_items);
176 for (i = 0 ; i < c->n_items ; ++i)
177 c->items[i] = cmd.v_variables[i];
180 if ( cmd.miss == REL_INCLUDE)
181 rel.exclude = MV_SYSTEM;
183 if ( rel.model == MODEL_SPLIT)
186 const struct cronbach *s;
189 rel.sc = xrealloc (rel.sc, sizeof (struct cronbach) * rel.n_sc);
194 (rel.split_point == -1) ? s->n_items / 2 : rel.split_point;
196 rel.sc[2].n_items = s->n_items - rel.sc[1].n_items;
197 rel.sc[1].items = xzalloc (sizeof (struct variable *)
198 * rel.sc[1].n_items);
200 rel.sc[2].items = xzalloc (sizeof (struct variable *) *
203 for (i = 0; i < rel.sc[1].n_items ; ++i)
204 rel.sc[1].items[i] = s->items[i];
206 while (i < s->n_items)
208 rel.sc[2].items[i - rel.sc[1].n_items] = s->items[i];
213 if (cmd.a_summary[REL_SUM_TOTAL])
216 const int base_sc = rel.n_sc;
218 rel.total_start = base_sc;
220 rel.n_sc += rel.sc[0].n_items ;
221 rel.sc = xrealloc (rel.sc, sizeof (struct cronbach) * rel.n_sc);
223 for (i = 0 ; i < rel.sc[0].n_items; ++i )
227 struct cronbach *s = &rel.sc[i + base_sc];
229 s->n_items = rel.sc[0].n_items - 1;
230 s->items = xzalloc (sizeof (struct variable *) * s->n_items);
231 for (v_src = 0 ; v_src < rel.sc[0].n_items ; ++v_src)
234 s->items[v_dest++] = rel.sc[0].items[v_src];
240 grouper = casegrouper_create_splits (proc_open (ds), dataset_dict (ds));
241 while (casegrouper_get_next_group (grouper, &group))
243 run_reliability (group, ds, &rel);
245 reliability_statistics (&rel);
247 if (cmd.a_summary[REL_SUM_TOTAL])
248 reliability_summary_total (&rel);
250 ok = casegrouper_destroy (grouper);
251 ok = proc_commit (ds) && ok;
253 free_reliability (&cmd);
257 /* Free all the stuff */
258 for (i = 0 ; i < rel.n_sc; ++i)
261 struct cronbach *c = &rel.sc[i];
264 moments1_destroy (c->total);
267 for (x = 0 ; x < c->n_items; ++x)
268 moments1_destroy (c->m[x]);
273 ds_destroy (&rel.scale_name);
282 /* Return the sum of all the item variables in S */
284 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
287 const struct cronbach *s = aux;
290 for (v = 0 ; v < s->n_items; ++v)
292 sum += case_data (c, s->items[v])->f;
299 static void case_processing_summary (casenumber n_valid, casenumber n_missing);
302 run_reliability (struct casereader *input, struct dataset *ds UNUSED,
303 struct reliability *rel)
308 casenumber n_missing ;
309 casenumber n_valid = 0;
312 for (si = 0 ; si < rel->n_sc; ++si)
314 struct cronbach *s = &rel->sc[si];
316 s->m = xzalloc (sizeof (s->m) * s->n_items);
317 s->total = moments1_create (MOMENT_VARIANCE);
319 for (i = 0 ; i < s->n_items ; ++i )
320 s->m[i] = moments1_create (MOMENT_VARIANCE);
323 input = casereader_create_filter_missing (input,
330 for (si = 0 ; si < rel->n_sc; ++si)
332 struct cronbach *s = &rel->sc[si];
335 s->totals_idx = casereader_get_value_cnt (input);
337 casereader_create_append_numeric (input, append_sum,
341 for (; casereader_read (input, &c); case_destroy (&c))
346 for (si = 0; si < rel->n_sc; ++si)
348 struct cronbach *s = &rel->sc[si];
350 for (i = 0 ; i < s->n_items ; ++i )
351 moments1_add (s->m[i], case_data (&c, s->items[i])->f, weight);
353 moments1_add (s->total, case_data_idx (&c, s->totals_idx)->f, weight);
356 casereader_destroy (input);
358 for (si = 0; si < rel->n_sc; ++si)
360 struct cronbach *s = &rel->sc[si];
362 s->sum_of_variances = 0;
363 for (i = 0 ; i < s->n_items ; ++i )
365 double weight, mean, variance;
366 moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
368 s->sum_of_variances += variance;
371 moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
375 alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
380 struct tab_table *tab = tab_create(1, 1, 0);
382 tab_dim (tab, tab_natural_dimensions);
383 tab_flags (tab, SOMF_NO_TITLE );
385 tab_text(tab, 0, 0, TAT_PRINTF, "Scale: %s", ds_cstr (&rel->scale_name));
391 case_processing_summary (n_valid, n_missing);
395 static void reliability_statistics_model_alpha (struct tab_table *tbl,
396 const struct reliability *rel);
398 static void reliability_statistics_model_split (struct tab_table *tbl,
399 const struct reliability *rel);
401 struct reliability_output_table
407 void (*populate)(struct tab_table *, const struct reliability *);
410 static struct reliability_output_table rol[2] =
412 { 2, 2, 1, 1, reliability_statistics_model_alpha},
413 { 4, 9, 3, 0, reliability_statistics_model_split}
417 reliability_statistics (const struct reliability *rel)
419 int n_cols = rol[rel->model].n_cols;
420 int n_rows = rol[rel->model].n_rows;
421 int heading_columns = rol[rel->model].heading_cols;
422 int heading_rows = rol[rel->model].heading_rows;
424 struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
425 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
427 tab_dim (tbl, tab_natural_dimensions);
429 tab_title (tbl, _("Reliability Statistics"));
431 /* Vertical lines for the data only */
436 n_cols - 1, n_rows - 1);
438 /* Box around table */
443 n_cols - 1, n_rows - 1);
446 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
448 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
450 if ( rel->model == MODEL_ALPHA )
451 reliability_statistics_model_alpha (tbl, rel);
452 else if (rel->model == MODEL_SPLIT )
453 reliability_statistics_model_split (tbl, rel);
459 reliability_summary_total (const struct reliability *rel)
462 const int n_cols = 5;
463 const int heading_columns = 1;
464 const int heading_rows = 1;
465 const int n_rows = rel->sc[0].n_items + heading_rows ;
467 struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
468 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
470 tab_dim (tbl, tab_natural_dimensions);
472 tab_title (tbl, _("Item-Total Statistics"));
474 /* Vertical lines for the data only */
479 n_cols - 1, n_rows - 1);
481 /* Box around table */
486 n_cols - 1, n_rows - 1);
489 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
491 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
493 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
494 _("Scale Mean if Item Deleted"));
496 tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
497 _("Scale Variance if Item Deleted"));
499 tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
500 _("Corrected Item-Total Correlation"));
502 tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
503 _("Cronbach's Alpha if Item Deleted"));
506 for (i = 0 ; i < rel->sc[0].n_items; ++i)
508 double cov, item_to_total_r;
509 double mean, weight, var;
511 const struct cronbach *s = &rel->sc[rel->total_start + i];
512 tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
513 var_to_string (rel->sc[0].items[i]));
515 moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
517 tab_float (tbl, 1, heading_rows + i, TAB_RIGHT,
520 tab_float (tbl, 2, heading_rows + i, TAB_RIGHT,
521 s->variance_of_sums, 8, 3);
523 tab_float (tbl, 4, heading_rows + i, TAB_RIGHT,
527 moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
528 cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
531 item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
534 tab_float (tbl, 3, heading_rows + i, TAB_RIGHT,
535 item_to_total_r, 8, 3);
544 reliability_statistics_model_alpha (struct tab_table *tbl,
545 const struct reliability *rel)
547 const struct cronbach *s = &rel->sc[0];
549 tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
550 _("Cronbach's Alpha"));
552 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
555 tab_float (tbl, 0, 1, TAB_RIGHT, s->alpha, 8, 3);
557 tab_float (tbl, 1, 1, TAB_RIGHT, s->n_items, 8, 0);
562 reliability_statistics_model_split (struct tab_table *tbl,
563 const struct reliability *rel)
565 tab_text (tbl, 0, 0, TAB_LEFT,
566 _("Cronbach's Alpha"));
568 tab_text (tbl, 1, 0, TAB_LEFT,
571 tab_text (tbl, 2, 0, TAB_LEFT,
574 tab_text (tbl, 2, 1, TAB_LEFT,
579 tab_text (tbl, 1, 2, TAB_LEFT,
582 tab_text (tbl, 2, 2, TAB_LEFT,
585 tab_text (tbl, 2, 3, TAB_LEFT,
590 tab_text (tbl, 1, 4, TAB_LEFT,
591 _("Total N of Items"));
593 tab_text (tbl, 0, 5, TAB_LEFT,
594 _("Correlation Between Forms"));
597 tab_text (tbl, 0, 6, TAB_LEFT,
598 _("Spearman-Brown Coefficient"));
600 tab_text (tbl, 1, 6, TAB_LEFT,
603 tab_text (tbl, 1, 7, TAB_LEFT,
604 _("Unequal Length"));
607 tab_text (tbl, 0, 8, TAB_LEFT,
608 _("Guttman Split-Half Coefficient"));
612 tab_float (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, 8, 3);
613 tab_float (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, 8, 3);
615 tab_float (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, 8, 0);
616 tab_float (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, 8, 0);
618 tab_float (tbl, 3, 4, TAB_RIGHT,
619 rel->sc[1].n_items + rel->sc[2].n_items, 8, 0);
622 /* R is the correlation between the two parts */
623 double r = rel->sc[0].variance_of_sums -
624 rel->sc[1].variance_of_sums -
625 rel->sc[2].variance_of_sums ;
627 /* Guttman Split Half Coefficient */
628 double g = 2 * r / rel->sc[0].variance_of_sums;
630 /* Unequal Length Spearman Brown Coefficient, and
631 intermediate value used in the computation thereof */
634 r /= sqrt (rel->sc[1].variance_of_sums);
635 r /= sqrt (rel->sc[2].variance_of_sums);
638 tab_float (tbl, 3, 5, TAB_RIGHT, r, 8, 3);
640 /* Equal length Spearman-Brown Coefficient */
641 tab_float (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), 8, 3);
643 tab_float (tbl, 3, 8, TAB_RIGHT, g, 8, 3);
645 tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
646 pow2 (rel->sc[0].n_items);
648 uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
652 tab_float (tbl, 3, 7, TAB_RIGHT, uly, 8, 3);
660 case_processing_summary (casenumber n_valid, casenumber n_missing)
665 int heading_columns = 2;
666 int heading_rows = 1;
667 struct tab_table *tbl;
668 tbl = tab_create (n_cols, n_rows, 0);
669 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
671 tab_dim (tbl, tab_natural_dimensions);
673 tab_title (tbl, _("Case Processing Summary"));
675 /* Vertical lines for the data only */
680 n_cols - 1, n_rows - 1);
682 /* Box around table */
687 n_cols - 1, n_rows - 1);
690 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
692 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
695 tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
698 tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
701 tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
704 tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
707 tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
710 tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE | TAT_PRINTF,
713 total = n_missing + n_valid;
715 tab_float (tbl, 2, heading_rows, TAB_RIGHT,
719 tab_float (tbl, 2, heading_rows + 1, TAB_RIGHT,
723 tab_float (tbl, 2, heading_rows + 2, TAB_RIGHT,
727 tab_float (tbl, 3, heading_rows, TAB_RIGHT,
728 100 * n_valid / (double) total, 8, 1);
731 tab_float (tbl, 3, heading_rows + 1, TAB_RIGHT,
732 100 * n_missing / (double) total, 8, 1);
735 tab_float (tbl, 3, heading_rows + 2, TAB_RIGHT,
736 100 * total / (double) total, 8, 1);
743 rel_custom_model (struct lexer *lexer, struct dataset *ds UNUSED,
744 struct cmd_reliability *cmd UNUSED, void *aux)
746 struct reliability *rel = aux;
748 if (lex_match_id (lexer, "ALPHA"))
750 rel->model = MODEL_ALPHA;
752 else if (lex_match_id (lexer, "SPLIT"))
754 rel->model = MODEL_SPLIT;
755 rel->split_point = -1;
756 if ( lex_match (lexer, '('))
758 lex_force_num (lexer);
759 rel->split_point = lex_number (lexer);
761 lex_force_match (lexer, ')');
773 rel_custom_scale (struct lexer *lexer, struct dataset *ds UNUSED,
774 struct cmd_reliability *p, void *aux)
776 struct const_var_set *vs;
777 struct reliability *rel = aux;
778 struct cronbach *scale;
781 rel->sc = xzalloc (sizeof (struct cronbach) * rel->n_sc);
784 if ( ! lex_force_match (lexer, '(')) return 0;
786 if ( ! lex_force_string (lexer) ) return 0;
788 ds_init_string (&rel->scale_name, lex_tokstr (lexer));
792 if ( ! lex_force_match (lexer, ')')) return 0;
794 lex_match (lexer, '=');
796 vs = const_var_set_create_from_array (p->v_variables, p->n_variables);
798 if (!parse_const_var_set_vars (lexer, vs, &scale->items, &scale->n_items, 0))
800 const_var_set_destroy (vs);
804 const_var_set_destroy (vs);