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