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);
265 for (x = 0 ; x < c->n_items; ++x)
266 moments1_destroy (c->m[x]);
270 ds_destroy (&rel.scale_name);
279 /* Return the sum of all the item variables in S */
281 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
284 const struct cronbach *s = aux;
287 for (v = 0 ; v < s->n_items; ++v)
289 sum += case_data (c, s->items[v])->f;
296 static void case_processing_summary (casenumber n_valid, casenumber n_missing);
299 run_reliability (struct casereader *input, struct dataset *ds UNUSED,
300 struct reliability *rel)
305 casenumber n_missing ;
306 casenumber n_valid = 0;
309 for (si = 0 ; si < rel->n_sc; ++si)
311 struct cronbach *s = &rel->sc[si];
313 s->m = xzalloc (sizeof (s->m) * s->n_items);
314 s->total = moments1_create (MOMENT_VARIANCE);
316 for (i = 0 ; i < s->n_items ; ++i )
317 s->m[i] = moments1_create (MOMENT_VARIANCE);
320 input = casereader_create_filter_missing (input,
327 for (si = 0 ; si < rel->n_sc; ++si)
329 struct cronbach *s = &rel->sc[si];
332 s->totals_idx = casereader_get_value_cnt (input);
334 casereader_create_append_numeric (input, append_sum,
338 for (; casereader_read (input, &c); case_destroy (&c))
343 for (si = 0; si < rel->n_sc; ++si)
345 struct cronbach *s = &rel->sc[si];
347 for (i = 0 ; i < s->n_items ; ++i )
348 moments1_add (s->m[i], case_data (&c, s->items[i])->f, weight);
350 moments1_add (s->total, case_data_idx (&c, s->totals_idx)->f, weight);
353 casereader_destroy (input);
355 for (si = 0; si < rel->n_sc; ++si)
357 struct cronbach *s = &rel->sc[si];
359 s->sum_of_variances = 0;
360 for (i = 0 ; i < s->n_items ; ++i )
362 double weight, mean, variance;
363 moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
365 s->sum_of_variances += variance;
368 moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
372 alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
377 struct tab_table *tab = tab_create(1, 1, 0);
379 tab_dim (tab, tab_natural_dimensions);
380 tab_flags (tab, SOMF_NO_TITLE );
382 tab_text(tab, 0, 0, TAT_PRINTF, "Scale: %s", ds_cstr (&rel->scale_name));
388 case_processing_summary (n_valid, n_missing);
392 static void reliability_statistics_model_alpha (struct tab_table *tbl,
393 const struct reliability *rel);
395 static void reliability_statistics_model_split (struct tab_table *tbl,
396 const struct reliability *rel);
398 struct reliability_output_table
404 void (*populate)(struct tab_table *, const struct reliability *);
407 static struct reliability_output_table rol[2] =
409 { 2, 2, 1, 1, reliability_statistics_model_alpha},
410 { 4, 9, 3, 0, reliability_statistics_model_split}
414 reliability_statistics (const struct reliability *rel)
416 int n_cols = rol[rel->model].n_cols;
417 int n_rows = rol[rel->model].n_rows;
418 int heading_columns = rol[rel->model].heading_cols;
419 int heading_rows = rol[rel->model].heading_rows;
421 struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
422 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
424 tab_dim (tbl, tab_natural_dimensions);
426 tab_title (tbl, _("Reliability Statistics"));
428 /* Vertical lines for the data only */
433 n_cols - 1, n_rows - 1);
435 /* Box around table */
440 n_cols - 1, n_rows - 1);
443 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
445 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
447 if ( rel->model == MODEL_ALPHA )
448 reliability_statistics_model_alpha (tbl, rel);
449 else if (rel->model == MODEL_SPLIT )
450 reliability_statistics_model_split (tbl, rel);
456 reliability_summary_total (const struct reliability *rel)
459 const int n_cols = 5;
460 const int heading_columns = 1;
461 const int heading_rows = 1;
462 const int n_rows = rel->sc[0].n_items + heading_rows ;
464 struct tab_table *tbl = tab_create (n_cols, n_rows, 0);
465 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
467 tab_dim (tbl, tab_natural_dimensions);
469 tab_title (tbl, _("Item-Total Statistics"));
471 /* Vertical lines for the data only */
476 n_cols - 1, n_rows - 1);
478 /* Box around table */
483 n_cols - 1, n_rows - 1);
486 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
488 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
490 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
491 _("Scale Mean if Item Deleted"));
493 tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
494 _("Scale Variance if Item Deleted"));
496 tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
497 _("Corrected Item-Total Correlation"));
499 tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
500 _("Cronbach's Alpha if Item Deleted"));
503 for (i = 0 ; i < rel->sc[0].n_items; ++i)
505 double cov, item_to_total_r;
506 double mean, weight, var;
508 const struct cronbach *s = &rel->sc[rel->total_start + i];
509 tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
510 var_to_string (rel->sc[0].items[i]));
512 moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
514 tab_float (tbl, 1, heading_rows + i, TAB_RIGHT,
517 tab_float (tbl, 2, heading_rows + i, TAB_RIGHT,
518 s->variance_of_sums, 8, 3);
520 tab_float (tbl, 4, heading_rows + i, TAB_RIGHT,
524 moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
525 cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
528 item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
531 tab_float (tbl, 3, heading_rows + i, TAB_RIGHT,
532 item_to_total_r, 8, 3);
541 reliability_statistics_model_alpha (struct tab_table *tbl,
542 const struct reliability *rel)
544 const struct cronbach *s = &rel->sc[0];
546 tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
547 _("Cronbach's Alpha"));
549 tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
552 tab_float (tbl, 0, 1, TAB_RIGHT, s->alpha, 8, 3);
554 tab_float (tbl, 1, 1, TAB_RIGHT, s->n_items, 8, 0);
559 reliability_statistics_model_split (struct tab_table *tbl,
560 const struct reliability *rel)
562 tab_text (tbl, 0, 0, TAB_LEFT,
563 _("Cronbach's Alpha"));
565 tab_text (tbl, 1, 0, TAB_LEFT,
568 tab_text (tbl, 2, 0, TAB_LEFT,
571 tab_text (tbl, 2, 1, TAB_LEFT,
576 tab_text (tbl, 1, 2, TAB_LEFT,
579 tab_text (tbl, 2, 2, TAB_LEFT,
582 tab_text (tbl, 2, 3, TAB_LEFT,
587 tab_text (tbl, 1, 4, TAB_LEFT,
588 _("Total N of Items"));
590 tab_text (tbl, 0, 5, TAB_LEFT,
591 _("Correlation Between Forms"));
594 tab_text (tbl, 0, 6, TAB_LEFT,
595 _("Spearman-Brown Coefficient"));
597 tab_text (tbl, 1, 6, TAB_LEFT,
600 tab_text (tbl, 1, 7, TAB_LEFT,
601 _("Unequal Length"));
604 tab_text (tbl, 0, 8, TAB_LEFT,
605 _("Guttman Split-Half Coefficient"));
609 tab_float (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, 8, 3);
610 tab_float (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, 8, 3);
612 tab_float (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, 8, 0);
613 tab_float (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, 8, 0);
615 tab_float (tbl, 3, 4, TAB_RIGHT,
616 rel->sc[1].n_items + rel->sc[2].n_items, 8, 0);
619 /* R is the correlation between the two parts */
620 double r = rel->sc[0].variance_of_sums -
621 rel->sc[1].variance_of_sums -
622 rel->sc[2].variance_of_sums ;
624 /* Guttman Split Half Coefficient */
625 double g = 2 * r / rel->sc[0].variance_of_sums;
627 /* Unequal Length Spearman Brown Coefficient, and
628 intermediate value used in the computation thereof */
631 r /= sqrt (rel->sc[1].variance_of_sums);
632 r /= sqrt (rel->sc[2].variance_of_sums);
635 tab_float (tbl, 3, 5, TAB_RIGHT, r, 8, 3);
637 /* Equal length Spearman-Brown Coefficient */
638 tab_float (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), 8, 3);
640 tab_float (tbl, 3, 8, TAB_RIGHT, g, 8, 3);
642 tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
643 pow2 (rel->sc[0].n_items);
645 uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
649 tab_float (tbl, 3, 7, TAB_RIGHT, uly, 8, 3);
657 case_processing_summary (casenumber n_valid, casenumber n_missing)
662 int heading_columns = 2;
663 int heading_rows = 1;
664 struct tab_table *tbl;
665 tbl = tab_create (n_cols, n_rows, 0);
666 tab_headers (tbl, heading_columns, 0, heading_rows, 0);
668 tab_dim (tbl, tab_natural_dimensions);
670 tab_title (tbl, _("Case Processing Summary"));
672 /* Vertical lines for the data only */
677 n_cols - 1, n_rows - 1);
679 /* Box around table */
684 n_cols - 1, n_rows - 1);
687 tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
689 tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
692 tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
695 tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
698 tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
701 tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
704 tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
707 tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE | TAT_PRINTF,
710 total = n_missing + n_valid;
712 tab_float (tbl, 2, heading_rows, TAB_RIGHT,
716 tab_float (tbl, 2, heading_rows + 1, TAB_RIGHT,
720 tab_float (tbl, 2, heading_rows + 2, TAB_RIGHT,
724 tab_float (tbl, 3, heading_rows, TAB_RIGHT,
725 100 * n_valid / (double) total, 8, 1);
728 tab_float (tbl, 3, heading_rows + 1, TAB_RIGHT,
729 100 * n_missing / (double) total, 8, 1);
732 tab_float (tbl, 3, heading_rows + 2, TAB_RIGHT,
733 100 * total / (double) total, 8, 1);
740 rel_custom_model (struct lexer *lexer, struct dataset *ds UNUSED,
741 struct cmd_reliability *cmd UNUSED, void *aux)
743 struct reliability *rel = aux;
745 if (lex_match_id (lexer, "ALPHA"))
747 rel->model = MODEL_ALPHA;
749 else if (lex_match_id (lexer, "SPLIT"))
751 rel->model = MODEL_SPLIT;
752 rel->split_point = -1;
753 if ( lex_match (lexer, '('))
755 lex_force_num (lexer);
756 rel->split_point = lex_number (lexer);
758 lex_force_match (lexer, ')');
770 rel_custom_scale (struct lexer *lexer, struct dataset *ds UNUSED,
771 struct cmd_reliability *p, void *aux)
773 struct const_var_set *vs;
774 struct reliability *rel = aux;
775 struct cronbach *scale;
778 rel->sc = xzalloc (sizeof (struct cronbach) * rel->n_sc);
781 if ( ! lex_force_match (lexer, '(')) return 0;
783 if ( ! lex_force_string (lexer) ) return 0;
785 ds_init_string (&rel->scale_name, lex_tokstr (lexer));
789 if ( ! lex_force_match (lexer, ')')) return 0;
791 lex_match (lexer, '=');
793 vs = const_var_set_create_from_array (p->v_variables, p->n_variables);
795 if (!parse_const_var_set_vars (lexer, vs, &scale->items, &scale->n_items, 0))
797 const_var_set_destroy (vs);
801 const_var_set_destroy (vs);