Merge 'master' into 'psppsheet'.
[pspp] / src / language / stats / reliability.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <math.h>
20
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"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41 struct cronbach
42 {
43   const struct variable **items;
44   size_t n_items;
45   double alpha;
46   double sum_of_variances;
47   double variance_of_sums;
48   int totals_idx;          /* Casereader index into the totals */
49
50   struct moments1 **m ;    /* Moments of the items */
51   struct moments1 *total ; /* Moments of the totals */
52 };
53
54 #if 0
55 static void
56 dump_cronbach (const struct cronbach *s)
57 {
58   int i;
59   printf ("N items %d\n", s->n_items);
60   for (i = 0 ; i < s->n_items; ++i)
61     {
62       printf ("%s\n", var_get_name (s->items[i]));
63     }
64
65   printf ("Totals idx %d\n", s->totals_idx);
66
67   printf ("scale variance %g\n", s->variance_of_sums);
68   printf ("alpha %g\n", s->alpha);
69   putchar ('\n');
70 }
71 #endif
72
73 enum model
74   {
75     MODEL_ALPHA,
76     MODEL_SPLIT
77   };
78
79
80 enum summary_opts
81   {
82     SUMMARY_TOTAL = 0x0001,
83   };
84
85
86 struct reliability
87 {
88   const struct variable **variables;
89   size_t n_variables;
90   enum mv_class exclude;
91
92   struct cronbach *sc;
93   int n_sc;
94
95   int total_start;
96
97   struct string scale_name;
98
99   enum model model;
100   int split_point;
101
102
103   enum summary_opts summary;
104
105   const struct variable *wv;
106 };
107
108
109 static bool run_reliability (struct dataset *ds, const struct reliability *reliability);
110
111 static void
112 reliability_destroy (struct reliability *rel)
113 {
114   int j;
115   ds_destroy (&rel->scale_name);
116   for (j = 0; j < rel->n_sc ; ++j)
117     {
118       int x;
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]);
123       free (rel->sc[j].m);
124     }
125
126   free (rel->sc);
127   free (rel->variables);
128 }
129
130 int
131 cmd_reliability (struct lexer *lexer, struct dataset *ds)
132 {
133   const struct dictionary *dict = dataset_dict (ds);
134
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;
141
142   reliability.wv = dict_get_weight (dict);
143
144   reliability.total_start = 0;
145
146   lex_match (lexer, T_SLASH);
147
148   if (!lex_force_match_id (lexer, "VARIABLES"))
149     {
150       goto error;
151     }
152
153   lex_match (lexer, T_EQUALS);
154
155   if (!parse_variables_const (lexer, dict, &reliability.variables, &reliability.n_variables,
156                               PV_NO_DUPLICATE | PV_NUMERIC))
157     goto error;
158
159   if (reliability.n_variables < 2)
160     msg (MW, _("Reliability on a single variable is not useful."));
161
162
163     {
164       int i;
165       struct cronbach *c;
166       /* Create a default Scale */
167
168       reliability.n_sc = 1;
169       reliability.sc = xzalloc (sizeof (struct cronbach) * reliability.n_sc);
170
171       ds_init_cstr (&reliability.scale_name, "ANY");
172
173       c = &reliability.sc[0];
174       c->n_items = reliability.n_variables;
175       c->items = xzalloc (sizeof (struct variable*) * c->n_items);
176
177       for (i = 0 ; i < c->n_items ; ++i)
178         c->items[i] = reliability.variables[i];
179     }
180
181
182
183   while (lex_token (lexer) != T_ENDCMD)
184     {
185       lex_match (lexer, T_SLASH);
186
187       if (lex_match_id (lexer, "SCALE"))
188         {
189           struct const_var_set *vs;
190           if ( ! lex_force_match (lexer, T_LPAREN))
191             goto error;
192
193           if ( ! lex_force_string (lexer) ) 
194             goto error;
195
196           ds_assign_substring (&reliability.scale_name, lex_tokss (lexer));
197
198           lex_get (lexer);
199
200           if ( ! lex_force_match (lexer, T_RPAREN))
201             goto error;
202
203           lex_match (lexer, T_EQUALS);
204
205           vs = const_var_set_create_from_array (reliability.variables, reliability.n_variables);
206
207           free (reliability.sc->items);
208           if (!parse_const_var_set_vars (lexer, vs, &reliability.sc->items, &reliability.sc->n_items, 0))
209             {
210               const_var_set_destroy (vs);
211               goto error;
212             }
213
214           const_var_set_destroy (vs);
215         }
216       else if (lex_match_id (lexer, "MODEL"))
217         {
218           lex_match (lexer, T_EQUALS);
219           if (lex_match_id (lexer, "ALPHA"))
220             {
221               reliability.model = MODEL_ALPHA;
222             }
223           else if (lex_match_id (lexer, "SPLIT"))
224             {
225               reliability.model = MODEL_SPLIT;
226               reliability.split_point = -1;
227
228               if ( lex_match (lexer, T_LPAREN))
229                 {
230                   lex_force_num (lexer);
231                   reliability.split_point = lex_number (lexer);
232                   lex_get (lexer);
233                   lex_force_match (lexer, T_RPAREN);
234                 }
235             }
236           else
237             goto error;
238         }
239       else if (lex_match_id (lexer, "SUMMARY"))
240         {
241           lex_match (lexer, T_EQUALS);
242           if (lex_match_id (lexer, "TOTAL"))
243             {
244               reliability.summary |= SUMMARY_TOTAL;
245             }
246           else if (lex_match (lexer, T_ALL))
247             {
248               reliability.summary = 0xFFFF;
249             }
250           else
251             goto error;
252         }
253       else if (lex_match_id (lexer, "MISSING"))
254         {
255           lex_match (lexer, T_EQUALS);
256           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
257             {
258               if (lex_match_id (lexer, "INCLUDE"))
259                 {
260                   reliability.exclude = MV_SYSTEM;
261                 }
262               else if (lex_match_id (lexer, "EXCLUDE"))
263                 {
264                   reliability.exclude = MV_ANY;
265                 }
266               else
267                 {
268                   lex_error (lexer, NULL);
269                   goto error;
270                 }
271             }
272         }
273       else
274         {
275           lex_error (lexer, NULL);
276           goto error;
277         }
278     }
279
280   if ( reliability.model == MODEL_SPLIT)
281     {
282       int i;
283       const struct cronbach *s;
284
285       if ( reliability.split_point >= reliability.n_variables)
286         {
287           msg (ME, _("The split point must be less than the number of variables"));
288           goto error;
289         }
290
291       reliability.n_sc += 2 ;
292       reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
293
294       s = &reliability.sc[0];
295
296       reliability.sc[1].n_items =
297         (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
298
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);
302
303       reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
304                                  reliability.sc[2].n_items);
305
306       for  (i = 0; i < reliability.sc[1].n_items ; ++i)
307         reliability.sc[1].items[i] = s->items[i];
308
309       while (i < s->n_items)
310         {
311           reliability.sc[2].items[i - reliability.sc[1].n_items] = s->items[i];
312           i++;
313         }
314     }
315
316   if ( reliability.summary & SUMMARY_TOTAL)
317     {
318       int i;
319       const int base_sc = reliability.n_sc;
320
321       reliability.total_start = base_sc;
322
323       reliability.n_sc +=  reliability.sc[0].n_items ;
324       reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
325
326
327       for (i = 0 ; i < reliability.sc[0].n_items; ++i )
328         {
329           int v_src;
330           int v_dest = 0;
331           struct cronbach *s = &reliability.sc[i + base_sc];
332
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)
336             {
337               if ( v_src != i)
338                 s->items[v_dest++] = reliability.sc[0].items[v_src];
339             }
340         }
341     }
342
343
344   if ( ! run_reliability (ds, &reliability)) 
345     goto error;
346
347   reliability_destroy (&reliability);
348   return CMD_SUCCESS;
349
350  error:
351   reliability_destroy (&reliability);
352   return CMD_FAILURE;
353 }
354
355
356 static void
357 do_reliability (struct casereader *group, struct dataset *ds,
358                 const struct reliability *rel);
359
360
361 static void reliability_summary_total (const struct reliability *rel);
362
363 static void reliability_statistics (const struct reliability *rel);
364
365
366 static bool
367 run_reliability (struct dataset *ds, const struct reliability *reliability)
368 {
369   struct dictionary *dict = dataset_dict (ds);
370   bool ok;
371   struct casereader *group;
372
373   struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
374
375   while (casegrouper_get_next_group (grouper, &group))
376     {
377       do_reliability (group, ds, reliability);
378
379       reliability_statistics (reliability);
380
381       if (reliability->summary & SUMMARY_TOTAL )
382         reliability_summary_total (reliability);
383     }
384
385   ok = casegrouper_destroy (grouper);
386   ok = proc_commit (ds) && ok;
387
388   return ok;
389 }
390
391
392 \f
393
394
395 /* Return the sum of all the item variables in S */
396 static  double
397 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
398 {
399   double sum = 0;
400   const struct cronbach *s = aux;
401
402   int v;
403   for (v = 0 ; v < s->n_items; ++v)
404     {
405       sum += case_data (c, s->items[v])->f;
406     }
407
408   return sum;
409 };
410
411 static void
412 case_processing_summary (casenumber n_valid, casenumber n_missing,
413                          const struct dictionary *dict);
414
415
416 static double
417 alpha (int k, double sum_of_variances, double variance_of_sums)
418 {
419   return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
420 }
421
422 static void
423 do_reliability (struct casereader *input, struct dataset *ds,
424                 const struct reliability *rel)
425 {
426   int i;
427   int si;
428   struct ccase *c;
429   casenumber n_missing ;
430   casenumber n_valid = 0;
431
432
433   for (si = 0 ; si < rel->n_sc; ++si)
434     {
435       struct cronbach *s = &rel->sc[si];
436
437       s->m = xzalloc (sizeof (s->m) * s->n_items);
438       s->total = moments1_create (MOMENT_VARIANCE);
439
440       for (i = 0 ; i < s->n_items ; ++i )
441         s->m[i] = moments1_create (MOMENT_VARIANCE);
442     }
443
444   input = casereader_create_filter_missing (input,
445                                             rel->variables,
446                                             rel->n_variables,
447                                             rel->exclude,
448                                             &n_missing,
449                                             NULL);
450
451   for (si = 0 ; si < rel->n_sc; ++si)
452     {
453       struct cronbach *s = &rel->sc[si];
454
455
456       s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
457       input =
458         casereader_create_append_numeric (input, append_sum,
459                                           s, NULL);
460     }
461
462   for (; (c = casereader_read (input)) != NULL; case_unref (c))
463     {
464       double weight = 1.0;
465       n_valid ++;
466
467       for (si = 0; si < rel->n_sc; ++si)
468         {
469           struct cronbach *s = &rel->sc[si];
470
471           for (i = 0 ; i < s->n_items ; ++i )
472             moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
473
474           moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
475         }
476     }
477   casereader_destroy (input);
478
479   for (si = 0; si < rel->n_sc; ++si)
480     {
481       struct cronbach *s = &rel->sc[si];
482
483       s->sum_of_variances = 0;
484       for (i = 0 ; i < s->n_items ; ++i )
485         {
486           double weight, mean, variance;
487           moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
488
489           s->sum_of_variances += variance;
490         }
491
492       moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
493                           NULL, NULL);
494
495       s->alpha =
496         alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
497     }
498
499   text_item_submit (text_item_create_format (TEXT_ITEM_PARAGRAPH, _("Scale: %s"),
500                                              ds_cstr (&rel->scale_name)));
501
502   case_processing_summary (n_valid, n_missing, dataset_dict (ds));
503 }
504
505
506
507
508
509 static void
510 case_processing_summary (casenumber n_valid, casenumber n_missing,
511                          const struct dictionary *dict)
512 {
513   const struct variable *wv = dict_get_weight (dict);
514   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
515
516   casenumber total;
517   int n_cols = 4;
518   int n_rows = 4;
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);
524
525   tab_title (tbl, _("Case Processing Summary"));
526
527   /* Vertical lines for the data only */
528   tab_box (tbl,
529            -1, -1,
530            -1, TAL_1,
531            heading_columns, 0,
532            n_cols - 1, n_rows - 1);
533
534   /* Box around table */
535   tab_box (tbl,
536            TAL_2, TAL_2,
537            -1, -1,
538            0, 0,
539            n_cols - 1, n_rows - 1);
540
541
542   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
543
544   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
545
546
547   tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
548                 _("Cases"));
549
550   tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
551                 _("Valid"));
552
553   tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
554                 _("Excluded"));
555
556   tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
557                 _("Total"));
558
559   tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
560                 _("N"));
561
562   tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%"));
563
564   total = n_missing + n_valid;
565
566   tab_double (tbl, 2, heading_rows, TAB_RIGHT,
567              n_valid, wfmt);
568
569
570   tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
571              n_missing, wfmt);
572
573
574   tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
575              total, wfmt);
576
577
578   tab_double (tbl, 3, heading_rows, TAB_RIGHT,
579              100 * n_valid / (double) total, NULL);
580
581
582   tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
583              100 * n_missing / (double) total, NULL);
584
585
586   tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
587              100 * total / (double) total, NULL);
588
589
590   tab_submit (tbl);
591 }
592
593
594
595 static void
596 reliability_summary_total (const struct reliability *rel)
597 {
598   int i;
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 ;
603
604   struct tab_table *tbl = tab_create (n_cols, n_rows);
605   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
606
607   tab_title (tbl, _("Item-Total Statistics"));
608
609   /* Vertical lines for the data only */
610   tab_box (tbl,
611            -1, -1,
612            -1, TAL_1,
613            heading_columns, 0,
614            n_cols - 1, n_rows - 1);
615
616   /* Box around table */
617   tab_box (tbl,
618            TAL_2, TAL_2,
619            -1, -1,
620            0, 0,
621            n_cols - 1, n_rows - 1);
622
623
624   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
625
626   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
627
628   tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
629             _("Scale Mean if Item Deleted"));
630
631   tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
632             _("Scale Variance if Item Deleted"));
633
634   tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
635             _("Corrected Item-Total Correlation"));
636
637   tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
638             _("Cronbach's Alpha if Item Deleted"));
639
640
641   for (i = 0 ; i < rel->sc[0].n_items; ++i)
642     {
643       double cov, item_to_total_r;
644       double mean, weight, var;
645
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]));
649
650       moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
651
652       tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
653                  mean, NULL);
654
655       tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
656                  s->variance_of_sums, NULL);
657
658       tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
659                  s->alpha, NULL);
660
661
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;
664       cov /= 2.0;
665
666       item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
667
668
669       tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
670                  item_to_total_r, NULL);
671     }
672
673
674   tab_submit (tbl);
675 }
676
677
678 static void reliability_statistics_model_alpha (struct tab_table *tbl,
679                                                 const struct reliability *rel);
680
681 static void reliability_statistics_model_split (struct tab_table *tbl,
682                                                 const struct reliability *rel);
683
684
685 struct reliability_output_table
686 {
687   int n_cols;
688   int n_rows;
689   int heading_cols;
690   int heading_rows;
691   void (*populate) (struct tab_table *, const struct reliability *);
692 };
693
694
695 static struct reliability_output_table rol[2] =
696   {
697     { 2, 2, 1, 1, reliability_statistics_model_alpha},
698     { 4, 9, 3, 0, reliability_statistics_model_split}
699   };
700
701 static void
702 reliability_statistics (const struct reliability *rel)
703 {
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;
708
709   struct tab_table *tbl = tab_create (n_cols, n_rows);
710   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
711
712   tab_title (tbl, _("Reliability Statistics"));
713
714   /* Vertical lines for the data only */
715   tab_box (tbl,
716            -1, -1,
717            -1, TAL_1,
718            heading_columns, 0,
719            n_cols - 1, n_rows - 1);
720
721   /* Box around table */
722   tab_box (tbl,
723            TAL_2, TAL_2,
724            -1, -1,
725            0, 0,
726            n_cols - 1, n_rows - 1);
727
728
729   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
730
731   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
732
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);
737
738   tab_submit (tbl);
739 }
740
741
742 static void
743 reliability_statistics_model_alpha (struct tab_table *tbl,
744                                     const struct reliability *rel)
745 {
746   const struct variable *wv = rel->wv;
747   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
748
749   const struct cronbach *s = &rel->sc[0];
750
751   tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
752                 _("Cronbach's Alpha"));
753
754   tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
755                 _("N of Items"));
756
757   tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
758
759   tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
760 }
761
762
763 static void
764 reliability_statistics_model_split (struct tab_table *tbl,
765                                     const struct reliability *rel)
766 {
767   const struct variable *wv = rel->wv;
768   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
769
770   tab_text (tbl, 0, 0, TAB_LEFT,
771             _("Cronbach's Alpha"));
772
773   tab_text (tbl, 1, 0, TAB_LEFT,
774             _("Part 1"));
775
776   tab_text (tbl, 2, 0, TAB_LEFT,
777             _("Value"));
778
779   tab_text (tbl, 2, 1, TAB_LEFT,
780             _("N of Items"));
781
782
783
784   tab_text (tbl, 1, 2, TAB_LEFT,
785             _("Part 2"));
786
787   tab_text (tbl, 2, 2, TAB_LEFT,
788             _("Value"));
789
790   tab_text (tbl, 2, 3, TAB_LEFT,
791             _("N of Items"));
792
793
794
795   tab_text (tbl, 1, 4, TAB_LEFT,
796             _("Total N of Items"));
797
798   tab_text (tbl, 0, 5, TAB_LEFT,
799             _("Correlation Between Forms"));
800
801
802   tab_text (tbl, 0, 6, TAB_LEFT,
803             _("Spearman-Brown Coefficient"));
804
805   tab_text (tbl, 1, 6, TAB_LEFT,
806             _("Equal Length"));
807
808   tab_text (tbl, 1, 7, TAB_LEFT,
809             _("Unequal Length"));
810
811
812   tab_text (tbl, 0, 8, TAB_LEFT,
813             _("Guttman Split-Half Coefficient"));
814
815
816
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);
819
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);
822
823   tab_double (tbl, 3, 4, TAB_RIGHT,
824              rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
825
826   {
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 ;
831
832     /* Guttman Split Half Coefficient */
833     double g = 2 * r / rel->sc[0].variance_of_sums;
834
835     /* Unequal Length Spearman Brown Coefficient, and
836      intermediate value used in the computation thereof */
837     double uly, tmp;
838
839     r /= sqrt (rel->sc[1].variance_of_sums);
840     r /= sqrt (rel->sc[2].variance_of_sums);
841     r /= 2.0;
842
843     tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
844
845     /* Equal length Spearman-Brown Coefficient */
846     tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
847
848     tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
849
850     tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
851       pow2 (rel->sc[0].n_items);
852
853     uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
854     uly -= pow2 (r);
855     uly /= 2 * tmp;
856
857     tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);
858   }
859 }
860