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