92f4ac1c22b8a8730a41e3f891a59603b2250527
[pspp-builds.git] / 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/dictionary.h"
24 #include "data/format.h"
25 #include "data/missing-values.h"
26 #include "data/procedure.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       reliability.n_sc += 2 ;
267       reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
268
269       s = &reliability.sc[0];
270
271       reliability.sc[1].n_items =
272         (reliability.split_point == -1) ? s->n_items / 2 : reliability.split_point;
273
274       reliability.sc[2].n_items = s->n_items - reliability.sc[1].n_items;
275       reliability.sc[1].items = xzalloc (sizeof (struct variable *)
276                                  * reliability.sc[1].n_items);
277
278       reliability.sc[2].items = xzalloc (sizeof (struct variable *) *
279                                  reliability.sc[2].n_items);
280
281       for  (i = 0; i < reliability.sc[1].n_items ; ++i)
282         reliability.sc[1].items[i] = s->items[i];
283
284       while (i < s->n_items)
285         {
286           reliability.sc[2].items[i - reliability.sc[1].n_items] = s->items[i];
287           i++;
288         }
289     }
290
291   if ( reliability.summary & SUMMARY_TOTAL)
292     {
293       int i;
294       const int base_sc = reliability.n_sc;
295
296       reliability.total_start = base_sc;
297
298       reliability.n_sc +=  reliability.sc[0].n_items ;
299       reliability.sc = xrealloc (reliability.sc, sizeof (struct cronbach) * reliability.n_sc);
300
301
302       for (i = 0 ; i < reliability.sc[0].n_items; ++i )
303         {
304           int v_src;
305           int v_dest = 0;
306           struct cronbach *s = &reliability.sc[i + base_sc];
307
308           s->n_items = reliability.sc[0].n_items - 1;
309           s->items = xzalloc (sizeof (struct variable *) * s->n_items);
310           for (v_src = 0 ; v_src < reliability.sc[0].n_items ; ++v_src)
311             {
312               if ( v_src != i)
313                 s->items[v_dest++] = reliability.sc[0].items[v_src];
314             }
315         }
316     }
317
318
319   if ( ! run_reliability (ds, &reliability)) 
320     goto error;
321
322   free (reliability.variables);
323   return CMD_SUCCESS;
324
325  error:
326   free (reliability.variables);
327   return CMD_FAILURE;
328 }
329
330
331 static void
332 do_reliability (struct casereader *group, struct dataset *ds,
333                 const struct reliability *rel);
334
335
336 static void reliability_summary_total (const struct reliability *rel);
337
338 static void reliability_statistics (const struct reliability *rel);
339
340
341 static bool
342 run_reliability (struct dataset *ds, const struct reliability *reliability)
343 {
344   struct dictionary *dict = dataset_dict (ds);
345   bool ok;
346   struct casereader *group;
347
348   struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
349
350
351   while (casegrouper_get_next_group (grouper, &group))
352     {
353       do_reliability (group, ds, reliability);
354
355       reliability_statistics (reliability);
356
357       if (reliability->summary & SUMMARY_TOTAL )
358         reliability_summary_total (reliability);
359     }
360
361   ok = casegrouper_destroy (grouper);
362   ok = proc_commit (ds) && ok;
363
364   return ok;
365 }
366
367
368 \f
369
370
371 /* Return the sum of all the item variables in S */
372 static  double
373 append_sum (const struct ccase *c, casenumber n UNUSED, void *aux)
374 {
375   double sum = 0;
376   const struct cronbach *s = aux;
377
378   int v;
379   for (v = 0 ; v < s->n_items; ++v)
380     {
381       sum += case_data (c, s->items[v])->f;
382     }
383
384   return sum;
385 };
386
387 static void
388 case_processing_summary (casenumber n_valid, casenumber n_missing,
389                          const struct dictionary *dict);
390
391
392 static double
393 alpha (int k, double sum_of_variances, double variance_of_sums)
394 {
395   return k / ( k - 1.0) * ( 1 - sum_of_variances / variance_of_sums);
396 }
397
398 static void
399 do_reliability (struct casereader *input, struct dataset *ds,
400                 const struct reliability *rel)
401 {
402   int i;
403   int si;
404   struct ccase *c;
405   casenumber n_missing ;
406   casenumber n_valid = 0;
407
408
409   for (si = 0 ; si < rel->n_sc; ++si)
410     {
411       struct cronbach *s = &rel->sc[si];
412
413       s->m = xzalloc (sizeof (s->m) * s->n_items);
414       s->total = moments1_create (MOMENT_VARIANCE);
415
416       for (i = 0 ; i < s->n_items ; ++i )
417         s->m[i] = moments1_create (MOMENT_VARIANCE);
418     }
419
420   input = casereader_create_filter_missing (input,
421                                             rel->variables,
422                                             rel->n_variables,
423                                             rel->exclude,
424                                             &n_missing,
425                                             NULL);
426
427   for (si = 0 ; si < rel->n_sc; ++si)
428     {
429       struct cronbach *s = &rel->sc[si];
430
431
432       s->totals_idx = caseproto_get_n_widths (casereader_get_proto (input));
433       input =
434         casereader_create_append_numeric (input, append_sum,
435                                           s, NULL);
436     }
437
438   for (; (c = casereader_read (input)) != NULL; case_unref (c))
439     {
440       double weight = 1.0;
441       n_valid ++;
442
443       for (si = 0; si < rel->n_sc; ++si)
444         {
445           struct cronbach *s = &rel->sc[si];
446
447           for (i = 0 ; i < s->n_items ; ++i )
448             moments1_add (s->m[i], case_data (c, s->items[i])->f, weight);
449
450           moments1_add (s->total, case_data_idx (c, s->totals_idx)->f, weight);
451         }
452     }
453   casereader_destroy (input);
454
455   for (si = 0; si < rel->n_sc; ++si)
456     {
457       struct cronbach *s = &rel->sc[si];
458
459       s->sum_of_variances = 0;
460       for (i = 0 ; i < s->n_items ; ++i )
461         {
462           double weight, mean, variance;
463           moments1_calculate (s->m[i], &weight, &mean, &variance, NULL, NULL);
464
465           s->sum_of_variances += variance;
466         }
467
468       moments1_calculate (s->total, NULL, NULL, &s->variance_of_sums,
469                           NULL, NULL);
470
471       s->alpha =
472         alpha (s->n_items, s->sum_of_variances, s->variance_of_sums);
473     }
474
475   text_item_submit (text_item_create_format (TEXT_ITEM_PARAGRAPH, "Scale: %s",
476                                              ds_cstr (&rel->scale_name)));
477
478   case_processing_summary (n_valid, n_missing, dataset_dict (ds));
479 }
480
481
482
483
484
485 static void
486 case_processing_summary (casenumber n_valid, casenumber n_missing,
487                          const struct dictionary *dict)
488 {
489   const struct variable *wv = dict_get_weight (dict);
490   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
491
492   casenumber total;
493   int n_cols = 4;
494   int n_rows = 4;
495   int heading_columns = 2;
496   int heading_rows = 1;
497   struct tab_table *tbl;
498   tbl = tab_create (n_cols, n_rows);
499   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
500
501   tab_title (tbl, _("Case Processing Summary"));
502
503   /* Vertical lines for the data only */
504   tab_box (tbl,
505            -1, -1,
506            -1, TAL_1,
507            heading_columns, 0,
508            n_cols - 1, n_rows - 1);
509
510   /* Box around table */
511   tab_box (tbl,
512            TAL_2, TAL_2,
513            -1, -1,
514            0, 0,
515            n_cols - 1, n_rows - 1);
516
517
518   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
519
520   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
521
522
523   tab_text (tbl, 0, heading_rows, TAB_LEFT | TAT_TITLE,
524                 _("Cases"));
525
526   tab_text (tbl, 1, heading_rows, TAB_LEFT | TAT_TITLE,
527                 _("Valid"));
528
529   tab_text (tbl, 1, heading_rows + 1, TAB_LEFT | TAT_TITLE,
530                 _("Excluded"));
531
532   tab_text (tbl, 1, heading_rows + 2, TAB_LEFT | TAT_TITLE,
533                 _("Total"));
534
535   tab_text (tbl, heading_columns, 0, TAB_CENTER | TAT_TITLE,
536                 _("N"));
537
538   tab_text (tbl, heading_columns + 1, 0, TAB_CENTER | TAT_TITLE, _("%"));
539
540   total = n_missing + n_valid;
541
542   tab_double (tbl, 2, heading_rows, TAB_RIGHT,
543              n_valid, wfmt);
544
545
546   tab_double (tbl, 2, heading_rows + 1, TAB_RIGHT,
547              n_missing, wfmt);
548
549
550   tab_double (tbl, 2, heading_rows + 2, TAB_RIGHT,
551              total, wfmt);
552
553
554   tab_double (tbl, 3, heading_rows, TAB_RIGHT,
555              100 * n_valid / (double) total, NULL);
556
557
558   tab_double (tbl, 3, heading_rows + 1, TAB_RIGHT,
559              100 * n_missing / (double) total, NULL);
560
561
562   tab_double (tbl, 3, heading_rows + 2, TAB_RIGHT,
563              100 * total / (double) total, NULL);
564
565
566   tab_submit (tbl);
567 }
568
569
570
571 static void
572 reliability_summary_total (const struct reliability *rel)
573 {
574   int i;
575   const int n_cols = 5;
576   const int heading_columns = 1;
577   const int heading_rows = 1;
578   const int n_rows = rel->sc[0].n_items + heading_rows ;
579
580   struct tab_table *tbl = tab_create (n_cols, n_rows);
581   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
582
583   tab_title (tbl, _("Item-Total Statistics"));
584
585   /* Vertical lines for the data only */
586   tab_box (tbl,
587            -1, -1,
588            -1, TAL_1,
589            heading_columns, 0,
590            n_cols - 1, n_rows - 1);
591
592   /* Box around table */
593   tab_box (tbl,
594            TAL_2, TAL_2,
595            -1, -1,
596            0, 0,
597            n_cols - 1, n_rows - 1);
598
599
600   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
601
602   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
603
604   tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
605             _("Scale Mean if Item Deleted"));
606
607   tab_text (tbl, 2, 0, TAB_CENTER | TAT_TITLE,
608             _("Scale Variance if Item Deleted"));
609
610   tab_text (tbl, 3, 0, TAB_CENTER | TAT_TITLE,
611             _("Corrected Item-Total Correlation"));
612
613   tab_text (tbl, 4, 0, TAB_CENTER | TAT_TITLE,
614             _("Cronbach's Alpha if Item Deleted"));
615
616
617   for (i = 0 ; i < rel->sc[0].n_items; ++i)
618     {
619       double cov, item_to_total_r;
620       double mean, weight, var;
621
622       const struct cronbach *s = &rel->sc[rel->total_start + i];
623       tab_text (tbl, 0, heading_rows + i, TAB_LEFT| TAT_TITLE,
624                 var_to_string (rel->sc[0].items[i]));
625
626       moments1_calculate (s->total, &weight, &mean, &var, 0, 0);
627
628       tab_double (tbl, 1, heading_rows + i, TAB_RIGHT,
629                  mean, NULL);
630
631       tab_double (tbl, 2, heading_rows + i, TAB_RIGHT,
632                  s->variance_of_sums, NULL);
633
634       tab_double (tbl, 4, heading_rows + i, TAB_RIGHT,
635                  s->alpha, NULL);
636
637
638       moments1_calculate (rel->sc[0].m[i], &weight, &mean, &var, 0,0);
639       cov = rel->sc[0].variance_of_sums + var - s->variance_of_sums;
640       cov /= 2.0;
641
642       item_to_total_r = (cov - var) / (sqrt(var) * sqrt (s->variance_of_sums));
643
644
645       tab_double (tbl, 3, heading_rows + i, TAB_RIGHT,
646                  item_to_total_r, NULL);
647     }
648
649
650   tab_submit (tbl);
651 }
652
653
654 static void reliability_statistics_model_alpha (struct tab_table *tbl,
655                                                 const struct reliability *rel);
656
657 static void reliability_statistics_model_split (struct tab_table *tbl,
658                                                 const struct reliability *rel);
659
660
661 struct reliability_output_table
662 {
663   int n_cols;
664   int n_rows;
665   int heading_cols;
666   int heading_rows;
667   void (*populate) (struct tab_table *, const struct reliability *);
668 };
669
670
671 static struct reliability_output_table rol[2] =
672   {
673     { 2, 2, 1, 1, reliability_statistics_model_alpha},
674     { 4, 9, 3, 0, reliability_statistics_model_split}
675   };
676
677 static void
678 reliability_statistics (const struct reliability *rel)
679 {
680   int n_cols = rol[rel->model].n_cols;
681   int n_rows = rol[rel->model].n_rows;
682   int heading_columns = rol[rel->model].heading_cols;
683   int heading_rows = rol[rel->model].heading_rows;
684
685   struct tab_table *tbl = tab_create (n_cols, n_rows);
686   tab_headers (tbl, heading_columns, 0, heading_rows, 0);
687
688   tab_title (tbl, _("Reliability Statistics"));
689
690   /* Vertical lines for the data only */
691   tab_box (tbl,
692            -1, -1,
693            -1, TAL_1,
694            heading_columns, 0,
695            n_cols - 1, n_rows - 1);
696
697   /* Box around table */
698   tab_box (tbl,
699            TAL_2, TAL_2,
700            -1, -1,
701            0, 0,
702            n_cols - 1, n_rows - 1);
703
704
705   tab_hline (tbl, TAL_2, 0, n_cols - 1, heading_rows);
706
707   tab_vline (tbl, TAL_2, heading_columns, 0, n_rows - 1);
708
709   if ( rel->model == MODEL_ALPHA )
710     reliability_statistics_model_alpha (tbl, rel);
711   else if (rel->model == MODEL_SPLIT )
712     reliability_statistics_model_split (tbl, rel);
713
714   tab_submit (tbl);
715 }
716
717
718 static void
719 reliability_statistics_model_alpha (struct tab_table *tbl,
720                                     const struct reliability *rel)
721 {
722   const struct variable *wv = rel->wv;
723   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
724
725   const struct cronbach *s = &rel->sc[0];
726
727   tab_text (tbl, 0, 0, TAB_CENTER | TAT_TITLE,
728                 _("Cronbach's Alpha"));
729
730   tab_text (tbl, 1, 0, TAB_CENTER | TAT_TITLE,
731                 _("N of Items"));
732
733   tab_double (tbl, 0, 1, TAB_RIGHT, s->alpha, NULL);
734
735   tab_double (tbl, 1, 1, TAB_RIGHT, s->n_items, wfmt);
736 }
737
738
739 static void
740 reliability_statistics_model_split (struct tab_table *tbl,
741                                     const struct reliability *rel)
742 {
743   const struct variable *wv = rel->wv;
744   const struct fmt_spec *wfmt = wv ? var_get_print_format (wv) : & F_8_0;
745
746   tab_text (tbl, 0, 0, TAB_LEFT,
747             _("Cronbach's Alpha"));
748
749   tab_text (tbl, 1, 0, TAB_LEFT,
750             _("Part 1"));
751
752   tab_text (tbl, 2, 0, TAB_LEFT,
753             _("Value"));
754
755   tab_text (tbl, 2, 1, TAB_LEFT,
756             _("N of Items"));
757
758
759
760   tab_text (tbl, 1, 2, TAB_LEFT,
761             _("Part 2"));
762
763   tab_text (tbl, 2, 2, TAB_LEFT,
764             _("Value"));
765
766   tab_text (tbl, 2, 3, TAB_LEFT,
767             _("N of Items"));
768
769
770
771   tab_text (tbl, 1, 4, TAB_LEFT,
772             _("Total N of Items"));
773
774   tab_text (tbl, 0, 5, TAB_LEFT,
775             _("Correlation Between Forms"));
776
777
778   tab_text (tbl, 0, 6, TAB_LEFT,
779             _("Spearman-Brown Coefficient"));
780
781   tab_text (tbl, 1, 6, TAB_LEFT,
782             _("Equal Length"));
783
784   tab_text (tbl, 1, 7, TAB_LEFT,
785             _("Unequal Length"));
786
787
788   tab_text (tbl, 0, 8, TAB_LEFT,
789             _("Guttman Split-Half Coefficient"));
790
791
792
793   tab_double (tbl, 3, 0, TAB_RIGHT, rel->sc[1].alpha, NULL);
794   tab_double (tbl, 3, 2, TAB_RIGHT, rel->sc[2].alpha, NULL);
795
796   tab_double (tbl, 3, 1, TAB_RIGHT, rel->sc[1].n_items, wfmt);
797   tab_double (tbl, 3, 3, TAB_RIGHT, rel->sc[2].n_items, wfmt);
798
799   tab_double (tbl, 3, 4, TAB_RIGHT,
800              rel->sc[1].n_items + rel->sc[2].n_items, wfmt);
801
802   {
803     /* R is the correlation between the two parts */
804     double r = rel->sc[0].variance_of_sums -
805       rel->sc[1].variance_of_sums -
806       rel->sc[2].variance_of_sums ;
807
808     /* Guttman Split Half Coefficient */
809     double g = 2 * r / rel->sc[0].variance_of_sums;
810
811     /* Unequal Length Spearman Brown Coefficient, and
812      intermediate value used in the computation thereof */
813     double uly, tmp;
814
815     r /= sqrt (rel->sc[1].variance_of_sums);
816     r /= sqrt (rel->sc[2].variance_of_sums);
817     r /= 2.0;
818
819     tab_double (tbl, 3, 5, TAB_RIGHT, r, NULL);
820
821     /* Equal length Spearman-Brown Coefficient */
822     tab_double (tbl, 3, 6, TAB_RIGHT, 2 * r / (1.0 + r), NULL);
823
824     tab_double (tbl, 3, 8, TAB_RIGHT, g, NULL);
825
826     tmp = (1.0 - r*r) * rel->sc[1].n_items * rel->sc[2].n_items /
827       pow2 (rel->sc[0].n_items);
828
829     uly = sqrt( pow4 (r) + 4 * pow2 (r) * tmp);
830     uly -= pow2 (r);
831     uly /= 2 * tmp;
832
833     tab_double (tbl, 3, 7, TAB_RIGHT, uly, NULL);
834   }
835 }
836