1 /* PSPP - computes sample statistics. -*-c-*-
3 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 Written by John Williams <johnr.williams@stonebow.otago.ac.nz>.
5 Almost completly re-written by John Darrington 2004
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "dcdflib/cdflib.h"
36 #include "value-labels.h"
47 variables=varlist("PV_NO_SCRATCH | PV_NUMERIC");
49 +missing=miss:!analysis/listwise,
50 incl:include/!exclude;
51 format=fmt:!labels/nolabels;
52 criteria=:cin(d:criteria,"%s > 0. && %s < 1.").
57 static struct cmd_t_test cmd;
61 static struct pool *t_test_pool ;
63 /* Variable for the GROUPS subcommand, if given. */
64 static struct variable *groups;
66 /* GROUPS: Number of values specified by the user; the values
68 static int n_groups_values;
69 static union value groups_values[2];
71 /* PAIRS: Number of pairs to be compared ; each pair. */
75 /* The variables comprising the pair */
76 struct variable *v[2];
78 /* The correlation coefficient between the variables */
81 /* The sum of the differences */
84 /* The mean of the differences */
87 /* The sum of the squares of the differences */
90 /* The std deviation of the differences */
93 static struct pair *pairs;
96 static int parse_value (union value * v, int type) ;
99 /* Structures and Functions for the Statistics Summary Box */
101 typedef void populate_ssbox_func(struct ssbox *ssb,
102 struct cmd_t_test *cmd);
103 typedef void finalize_ssbox_func(struct ssbox *ssb);
109 populate_ssbox_func *populate;
110 finalize_ssbox_func *finalize;
115 void ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode);
117 /* Populate a ssbox according to cmd */
118 void ssbox_populate(struct ssbox *ssb, struct cmd_t_test *cmd);
120 /* Submit and destroy a ssbox */
121 void ssbox_finalize(struct ssbox *ssb);
123 /* A function to create, populate and submit the Paired Samples Correlation
125 void pscbox(struct cmd_t_test *cmd);
128 /* Structures and Functions for the Test Results Box */
131 typedef void populate_trbox_func(struct trbox *trb,
132 struct cmd_t_test *cmd);
133 typedef void finalize_trbox_func(struct trbox *trb);
137 populate_trbox_func *populate;
138 finalize_trbox_func *finalize;
142 void trbox_create(struct trbox *trb, struct cmd_t_test *cmd, int mode);
144 /* Populate a ssbox according to cmd */
145 void trbox_populate(struct trbox *trb, struct cmd_t_test *cmd);
147 /* Submit and destroy a ssbox */
148 void trbox_finalize(struct trbox *trb);
150 /* Which mode was T-TEST invoked */
158 static int common_calc (struct ccase *);
159 static void common_precalc (void);
160 static void common_postcalc (void);
162 static int one_sample_calc (struct ccase *);
163 static void one_sample_precalc (void);
164 static void one_sample_postcalc (void);
166 static int paired_calc (struct ccase *);
167 static void paired_precalc (void);
168 static void paired_postcalc (void);
170 static int compare_var_name (const void *a_, const void *b_, void *v_ unused);
171 static unsigned hash_var_name (const void *a_, void *v_ unused);
179 struct ssbox stat_summary_box;
180 struct trbox test_results_box;
182 if (!lex_force_match_id ("T"))
186 lex_match_id ("TEST");
188 if ( !parse_t_test(&cmd) )
191 if (! cmd.sbc_criteria)
194 if ( cmd.sbc_testval + cmd.sbc_groups + cmd.sbc_pairs != 1 )
197 _("Exactly one of TESTVAL, GROUPS or PAIRS subcommands is required")
204 else if (cmd.sbc_groups)
209 if ( mode == T_PAIRED)
211 if (cmd.sbc_variables)
213 msg(SE, _("VARIABLES subcommand is not appropriate with PAIRS"));
218 /* Iterate through the pairs and put each variable that is a
219 member of a pair into cmd.v_variables */
222 struct hsh_iterator hi;
223 struct hsh_table *hash;
226 hash=hsh_create(n_pairs,compare_var_name,hash_var_name,0,0);
228 for (i=0; i < n_pairs; ++i)
230 hsh_insert(hash,pairs[i].v[0]);
231 hsh_insert(hash,pairs[i].v[1]);
234 assert(cmd.n_variables == 0);
235 cmd.n_variables = hsh_count(hash);
237 cmd.v_variables = xrealloc(cmd.v_variables,
238 sizeof(struct variable) * cmd.n_variables);
239 /* Iterate through the hash */
240 for (i=0,v = (struct variable *) hsh_first(hash,&hi);
242 v=hsh_next(hash,&hi) )
243 cmd.v_variables[i++]=v;
250 procedure(common_precalc,common_calc,common_postcalc);
255 procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc);
258 procedure(paired_precalc,paired_calc,paired_postcalc);
263 t_test_pool = pool_create ();
265 ssbox_create(&stat_summary_box,&cmd,mode);
266 ssbox_populate(&stat_summary_box,&cmd);
267 ssbox_finalize(&stat_summary_box);
269 if ( mode == T_PAIRED)
274 trbox_create(&test_results_box,&cmd,mode);
275 trbox_populate(&test_results_box,&cmd);
276 trbox_finalize(&test_results_box);
278 pool_destroy (t_test_pool);
286 tts_custom_groups (struct cmd_t_test *cmd unused)
290 if (token != T_ALL &&
291 (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
294 msg(SE,_("`%s' is not a variable name"),tokid);
298 groups = parse_variable ();
301 lex_error ("expecting variable name in GROUPS subcommand");
305 if (groups->type == T_STRING && groups->width > MAX_SHORT_STRING)
307 msg (SE, _("Long string variable %s is not valid here."),
312 if (!lex_match ('('))
314 if (groups->type == NUMERIC)
317 groups_values[0].f = 1;
318 groups_values[1].f = 2;
323 msg (SE, _("When applying GROUPS to a string variable, at "
324 "least one value must be specified."));
329 if (!parse_value (&groups_values[0],groups->type))
337 if (!parse_value (&groups_values[1],groups->type))
341 if (!lex_force_match (')'))
351 tts_custom_pairs (struct cmd_t_test *cmd unused)
353 struct variable **vars;
357 int n_after_WITH = -1;
358 int paired ; /* Was the PAIRED keyword given ? */
362 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
365 msg(SE,_("`%s' is not a variable name"),tokid);
370 if (!parse_variables (default_dict, &vars, &n_vars,
371 PV_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH))
379 if (lex_match (T_WITH))
381 n_before_WITH = n_vars;
382 if (!parse_variables (default_dict, &vars, &n_vars,
383 PV_DUPLICATE | PV_APPEND
384 | PV_NUMERIC | PV_NO_SCRATCH))
389 n_after_WITH = n_vars - n_before_WITH;
392 paired = (lex_match ('(') && lex_match_id ("PAIRED") && lex_match (')'));
394 /* Determine the number of pairs needed */
397 if (n_before_WITH != n_after_WITH)
400 msg (SE, _("PAIRED was specified but the number of variables "
401 "preceding WITH (%d) did not match the number "
403 n_before_WITH, n_after_WITH );
406 n_pairs=n_before_WITH;
408 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
410 n_pairs=n_before_WITH * n_after_WITH ;
412 else /* Neither WITH nor PAIRED keyword given */
417 msg (SE, _("At least two variables must be specified "
422 /* how many ways can you pick 2 from n_vars ? */
423 n_pairs = n_vars * (n_vars -1 ) /2 ;
426 /* Allocate storage for the pairs */
427 pairs = xrealloc(pairs,sizeof(struct pair) *n_pairs);
429 /* Populate the pairs with the appropriate variables */
434 assert(n_pairs == n_vars/2);
435 for (i = 0; i < n_pairs ; ++i)
437 pairs[i].v[0] = vars[i];
438 pairs[i].v[1] = vars[i+n_pairs];
441 else if (n_before_WITH > 0) /* WITH keyword given, but not PAIRED keyword */
446 for(i=0 ; i < n_before_WITH ; ++i )
448 for(j=0 ; j < n_after_WITH ; ++j)
450 pairs[p].v[0] = vars[i];
451 pairs[p].v[1] = vars[j+n_before_WITH];
456 else /* Neither WITH nor PAIRED given */
461 for(i=0 ; i < n_vars ; ++i )
463 for(j=i+1 ; j < n_vars ; ++j)
465 pairs[p].v[0] = vars[i];
466 pairs[p].v[1] = vars[j];
475 /* Parses the current token (numeric or string, depending on type)
476 value v and returns success. */
478 parse_value (union value * v, int type )
482 if (!lex_force_num ())
488 if (!lex_force_string ())
490 strncpy (v->s, ds_value (&tokstr), ds_length (&tokstr));
499 /* Implementation of the SSBOX object */
501 void ssbox_base_init(struct ssbox *this, int cols,int rows);
503 void ssbox_base_finalize(struct ssbox *ssb);
505 void ssbox_one_sample_init(struct ssbox *this,
506 struct cmd_t_test *cmd );
508 void ssbox_independent_samples_init(struct ssbox *this,
509 struct cmd_t_test *cmd);
511 void ssbox_paired_init(struct ssbox *this,
512 struct cmd_t_test *cmd);
514 /* Factory to create an ssbox */
516 ssbox_create(struct ssbox *ssb, struct cmd_t_test *cmd, int mode)
521 ssbox_one_sample_init(ssb,cmd);
524 ssbox_independent_samples_init(ssb,cmd);
527 ssbox_paired_init(ssb,cmd);
535 /* Despatcher for the populate method */
537 ssbox_populate(struct ssbox *ssb,struct cmd_t_test *cmd)
539 ssb->populate(ssb,cmd);
543 /* Despatcher for finalize */
545 ssbox_finalize(struct ssbox *ssb)
551 /* Submit the box and clear up */
553 ssbox_base_finalize(struct ssbox *ssb)
558 /* Initialize a ssbox struct */
560 ssbox_base_init(struct ssbox *this, int cols,int rows)
562 this->finalize = ssbox_base_finalize;
563 this->t = tab_create (cols, rows, 0);
565 tab_columns (this->t, SOM_COL_DOWN, 1);
566 tab_headers (this->t,0,0,1,0);
567 tab_box (this->t, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
568 tab_hline(this->t, TAL_2,0,cols-1,1);
569 tab_dim (this->t, tab_natural_dimensions);
572 void ssbox_one_sample_populate(struct ssbox *ssb,
573 struct cmd_t_test *cmd);
575 /* Initialize the one_sample ssbox */
577 ssbox_one_sample_init(struct ssbox *this,
578 struct cmd_t_test *cmd )
581 const int vsize=cmd->n_variables+1;
583 this->populate = ssbox_one_sample_populate;
585 ssbox_base_init(this, hsize,vsize);
586 tab_title (this->t, 0, _("One-Sample Statistics"));
587 tab_vline(this->t, TAL_2, 1,0,vsize);
588 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, _("N"));
589 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
590 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
591 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
594 void ssbox_independent_samples_populate(struct ssbox *ssb,
595 struct cmd_t_test *cmd);
597 /* Initialize the independent samples ssbox */
599 ssbox_independent_samples_init(struct ssbox *this,
600 struct cmd_t_test *cmd)
603 int vsize = cmd->n_variables*2 +1;
605 this->populate = ssbox_independent_samples_populate;
607 ssbox_base_init(this, hsize,vsize);
608 tab_title (this->t, 0, _("Group Statistics"));
609 tab_vline(this->t,0,1,0,vsize);
610 tab_text (this->t, 1, 0, TAB_CENTER | TAT_TITLE, groups->name);
611 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("N"));
612 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
613 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
614 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
618 /* Populate the ssbox for independent samples */
620 ssbox_independent_samples_populate(struct ssbox *ssb,
621 struct cmd_t_test *cmd)
628 if ( groups->type == NUMERIC )
630 val_lab1 = val_labs_find( groups->val_labs,groups_values[0]);
631 val_lab2 = val_labs_find( groups->val_labs,groups_values[1]);
635 val_lab1 = groups_values[0].s;
636 val_lab2 = groups_values[1].s;
641 for (i=0; i < cmd->n_variables; ++i)
643 tab_text (ssb->t, 0, i*2+1, TAB_LEFT, cmd->v_variables[i]->name);
646 tab_text (ssb->t, 1, i*2+1, TAB_LEFT, val_lab1);
648 tab_float(ssb->t, 1 ,i*2+1, TAB_LEFT, groups_values[0].f, 2,0);
651 tab_text (ssb->t, 1, i*2+1+1, TAB_LEFT, val_lab2);
653 tab_float(ssb->t, 1 ,i*2+1+1, TAB_LEFT, groups_values[1].f,2,0);
658 void ssbox_paired_populate(struct ssbox *ssb,
659 struct cmd_t_test *cmd);
661 /* Initialize the paired values ssbox */
663 ssbox_paired_init(struct ssbox *this, struct cmd_t_test *cmd unused)
667 int vsize = n_pairs*2+1;
669 this->populate = ssbox_paired_populate;
671 ssbox_base_init(this, hsize,vsize);
672 tab_title (this->t, 0, _("Paired Sample Statistics"));
673 tab_vline(this->t,TAL_0,1,0,vsize-1);
674 tab_vline(this->t,TAL_2,2,0,vsize-1);
675 tab_text (this->t, 2, 0, TAB_CENTER | TAT_TITLE, _("Mean"));
676 tab_text (this->t, 3, 0, TAB_CENTER | TAT_TITLE, _("N"));
677 tab_text (this->t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
678 tab_text (this->t, 5, 0, TAB_CENTER | TAT_TITLE, _("SE. Mean"));
682 /* Populate the ssbox for paired values */
684 ssbox_paired_populate(struct ssbox *ssb,struct cmd_t_test *cmd unused)
690 for (i=0; i < n_pairs; ++i)
694 tab_text (ssb->t, 0, i*2+1, TAB_LEFT | TAT_PRINTF , _("Pair %d"),i);
696 for (j=0 ; j < 2 ; ++j)
698 struct t_test_proc *ttp;
700 ttp=&pairs[i].v[j]->p.t_t;
704 tab_text (ssb->t, 1, i*2+j+1, TAB_LEFT, pairs[i].v[j]->name);
707 tab_float (ssb->t,2, i*2+j+1, TAB_RIGHT, ttp->mean, 8, 2);
708 tab_float (ssb->t,3, i*2+j+1, TAB_RIGHT, ttp->n, 2, 0);
709 tab_float (ssb->t,4, i*2+j+1, TAB_RIGHT, ttp->std_dev, 8, 3);
710 tab_float (ssb->t,5, i*2+j+1, TAB_RIGHT, ttp->se_mean, 8, 3);
718 /* Populate the one sample ssbox */
720 ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd)
726 for (i=0; i < cmd->n_variables; ++i)
728 struct t_test_proc *ttp;
729 ttp= &cmd->v_variables[i]->p.t_t;
731 tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name);
732 tab_float (ssb->t,1, i+1, TAB_RIGHT, ttp->n, 2, 0);
733 tab_float (ssb->t,2, i+1, TAB_RIGHT, ttp->mean, 8, 2);
734 tab_float (ssb->t,3, i+1, TAB_RIGHT, ttp->std_dev, 8, 2);
735 tab_float (ssb->t,4, i+1, TAB_RIGHT, ttp->se_mean, 8, 3);
742 /* Implementation of the Test Results box struct */
744 void trbox_base_init(struct trbox *self,int n_vars, int cols);
745 void trbox_base_finalize(struct trbox *trb);
747 void trbox_independent_samples_init(struct trbox *trb,
748 struct cmd_t_test *cmd );
750 void trbox_independent_samples_populate(struct trbox *trb,
751 struct cmd_t_test *cmd);
753 void trbox_one_sample_init(struct trbox *self,
754 struct cmd_t_test *cmd );
756 void trbox_one_sample_populate(struct trbox *trb,
757 struct cmd_t_test *cmd);
759 void trbox_paired_init(struct trbox *self,
760 struct cmd_t_test *cmd );
762 void trbox_paired_populate(struct trbox *trb,
763 struct cmd_t_test *cmd);
767 /* Create a trbox according to mode*/
769 trbox_create(struct trbox *trb,
770 struct cmd_t_test *cmd, int mode)
775 trbox_one_sample_init(trb,cmd);
778 trbox_independent_samples_init(trb,cmd);
781 trbox_paired_init(trb,cmd);
788 /* Populate a trbox according to cmd */
790 trbox_populate(struct trbox *trb, struct cmd_t_test *cmd)
792 trb->populate(trb,cmd);
795 /* Submit and destroy a trbox */
797 trbox_finalize(struct trbox *trb)
802 /* Initialize the independent samples trbox */
804 trbox_independent_samples_init(struct trbox *self,
805 struct cmd_t_test *cmd unused)
808 const int vsize=cmd->n_variables*2+3;
811 self->populate = trbox_independent_samples_populate;
813 trbox_base_init(self,cmd->n_variables*2,hsize);
814 tab_title(self->t,0,_("Independent Samples Test"));
815 tab_hline(self->t,TAL_1,2,hsize-1,1);
816 tab_vline(self->t,TAL_2,2,0,vsize-1);
817 tab_vline(self->t,TAL_1,4,0,vsize-1);
818 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,hsize-2,vsize-1);
819 tab_hline(self->t,TAL_1, hsize-2,hsize-1,2);
820 tab_box(self->t,-1,-1,-1,TAL_1, hsize-2,2,hsize-1,vsize-1);
821 tab_joint_text(self->t, 2, 0, 3, 0,
822 TAB_CENTER,_("Levine's Test for Equality of Variances"));
823 tab_joint_text(self->t, 4,0,hsize-1,0,
824 TAB_CENTER,_("t-test for Equality of Means"));
826 tab_text(self->t,2,2, TAB_CENTER | TAT_TITLE,_("F"));
827 tab_text(self->t,3,2, TAB_CENTER | TAT_TITLE,_("Sig."));
828 tab_text(self->t,4,2, TAB_CENTER | TAT_TITLE,_("t"));
829 tab_text(self->t,5,2, TAB_CENTER | TAT_TITLE,_("df"));
830 tab_text(self->t,6,2, TAB_CENTER | TAT_TITLE,_("Sig. (2-tailed)"));
831 tab_text(self->t,7,2, TAB_CENTER | TAT_TITLE,_("Mean Difference"));
832 tab_text(self->t,8,2, TAB_CENTER | TAT_TITLE,_("Std. Error Difference"));
833 tab_text(self->t,9,2, TAB_CENTER | TAT_TITLE,_("Lower"));
834 tab_text(self->t,10,2, TAB_CENTER | TAT_TITLE,_("Upper"));
836 tab_joint_text(self->t, 9, 1, 10, 1, TAB_CENTER | TAT_PRINTF,
837 _("%d%% Confidence Interval of the Difference"),
838 (int)round(cmd->criteria*100.0));
842 /* Populate the independent samples trbox */
844 trbox_independent_samples_populate(struct trbox *self,
845 struct cmd_t_test *cmd )
850 for (i=0; i < cmd->n_variables; ++i)
852 tab_text (self->t, 0, i*2+3, TAB_LEFT, cmd->v_variables[i]->name);
854 tab_text (self->t, 1, i*2+3, TAB_LEFT, _("Equal variances assumed"));
856 tab_text (self->t, 1, i*2+3+1,
857 TAB_LEFT, _("Equal variances not assumed"));
861 /* Initialize the paired samples trbox */
863 trbox_paired_init(struct trbox *self,
864 struct cmd_t_test *cmd unused)
868 const int vsize=n_pairs+3;
870 self->populate = trbox_paired_populate;
872 trbox_base_init(self,n_pairs,hsize);
873 tab_title (self->t, 0, _("Paired Samples Test"));
874 tab_hline(self->t,TAL_1,2,6,1);
875 tab_vline(self->t,TAL_2,2,0,vsize);
876 tab_joint_text(self->t,2,0,6,0,TAB_CENTER,_("Paired Differences"));
877 tab_box(self->t,-1,-1,-1,TAL_1, 2,1,6,vsize-1);
878 tab_box(self->t,-1,-1,-1,TAL_1, 6,0,hsize-1,vsize-1);
879 tab_hline(self->t,TAL_1,5,6, 2);
880 tab_vline(self->t,TAL_0,6,0,1);
882 tab_joint_text(self->t, 5, 1, 6, 1, TAB_CENTER | TAT_PRINTF,
883 _("%d%% Confidence Interval of the Difference"),
884 (int)round(cmd->criteria*100.0));
886 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("Mean"));
887 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Std. Deviation"));
888 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Std. Error Mean"));
889 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
890 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
891 tab_text (self->t, 7, 2, TAB_CENTER | TAT_TITLE, _("t"));
892 tab_text (self->t, 8, 2, TAB_CENTER | TAT_TITLE, _("df"));
893 tab_text (self->t, 9, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
896 /* Populate the paired samples trbox */
898 trbox_paired_populate(struct trbox *trb,
899 struct cmd_t_test *cmd unused)
903 for (i=0; i < n_pairs; ++i)
911 struct variable *v0 = pairs[i].v[0];
912 struct variable *v1 = pairs[i].v[1];
914 struct t_test_proc *ttp0 = &v0->p.t_t;
915 struct t_test_proc *ttp1 = &v1->p.t_t;
921 tab_text (trb->t, 0, i+3, TAB_LEFT | TAT_PRINTF, _("Pair %d"),i);
923 tab_text (trb->t, 1, i+3, TAB_LEFT | TAT_PRINTF, "%s - %s",
924 pairs[i].v[0]->name, pairs[i].v[1]->name);
926 tab_float(trb->t, 2, i+3, TAB_RIGHT, pairs[i].mean_diff, 8, 4);
928 tab_float(trb->t, 3, i+3, TAB_RIGHT, pairs[i].std_dev_diff, 8, 5);
931 se_mean = pairs[i].std_dev_diff / sqrt(n) ;
932 tab_float(trb->t, 4, i+3, TAB_RIGHT, se_mean, 8,5 );
934 /* Now work out the confidence interval */
935 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
937 which=2; /* Calc T from p,q and df */
938 cdft(&which, &p, &q, &t, &df, &status, &bound);
942 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
945 tab_float(trb->t, 5, i+3, TAB_RIGHT,
946 pairs[i].mean_diff - t * se_mean , 8, 4);
948 tab_float(trb->t, 6, i+3, TAB_RIGHT,
949 pairs[i].mean_diff + t * se_mean , 8, 4);
951 t = ( ttp0->mean - ttp1->mean)
953 ( sqr(ttp0->s_std_dev) + sqr(ttp1->s_std_dev) -
954 2 * pairs[i].correlation * ttp0->s_std_dev * ttp1->s_std_dev )
958 tab_float(trb->t, 7, i+3, TAB_RIGHT, t , 8,3 );
960 /* Degrees of freedom */
961 tab_float(trb->t, 8, i+3, TAB_RIGHT, df , 2, 0 );
964 cdft(&which, &p, &q, &t, &df, &status, &bound);
968 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
972 tab_float(trb->t, 9, i+3, TAB_RIGHT, p*2.0 , 8, 3);
979 /* Initialize the one sample trbox */
981 trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd )
984 const int vsize=cmd->n_variables+3;
986 self->populate = trbox_one_sample_populate;
988 trbox_base_init(self, cmd->n_variables,hsize);
989 tab_title (self->t, 0, _("One-Sample Test"));
990 tab_hline(self->t, TAL_1, 1, hsize - 1, 1);
991 tab_vline(self->t, TAL_2, 1, 0, vsize);
993 tab_joint_text(self->t, 1, 0, hsize-1,0, TAB_CENTER | TAT_PRINTF,
994 _("Test Value = %f"),cmd->n_testval);
996 tab_box(self->t, -1, -1, -1, TAL_1, 1,1,hsize-1,vsize-1);
999 tab_joint_text(self->t,5,1,6,1,TAB_CENTER | TAT_PRINTF,
1000 _("%d%% Confidence Interval of the Difference"),
1001 (int)round(cmd->criteria*100.0));
1003 tab_vline(self->t,TAL_0,6,1,1);
1004 tab_hline(self->t,TAL_1,5,6,2);
1005 tab_text (self->t, 1, 2, TAB_CENTER | TAT_TITLE, _("t"));
1006 tab_text (self->t, 2, 2, TAB_CENTER | TAT_TITLE, _("df"));
1007 tab_text (self->t, 3, 2, TAB_CENTER | TAT_TITLE, _("Sig. (2-tailed)"));
1008 tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference"));
1009 tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower"));
1010 tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper"));
1015 /* Populate the one sample trbox */
1017 trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd)
1023 for (i=0; i < cmd->n_variables; ++i)
1031 struct t_test_proc *ttp;
1032 ttp= &cmd->v_variables[i]->p.t_t;
1035 tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name);
1037 t = (ttp->mean - cmd->n_testval ) * sqrt(ttp->n) / ttp->std_dev ;
1039 tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3);
1041 /* degrees of freedom */
1044 tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0);
1046 cdft(&which, &p, &q, &t, &df, &status, &bound);
1050 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1054 /* Multiply by 2 to get 2-tailed significance */
1055 tab_float (trb->t, 3, i+3, TAB_RIGHT, q*2.0, 8,3);
1057 tab_float (trb->t, 4, i+3, TAB_RIGHT, ttp->mean_diff, 8,3);
1060 q = (1 - cmd->criteria)/2.0; /* 2-tailed test */
1062 which=2; /* Calc T from p,q and df */
1063 cdft(&which, &p, &q, &t, &df, &status, &bound);
1066 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1069 tab_float (trb->t, 5, i+3, TAB_RIGHT,
1070 ttp->mean_diff - t * ttp->se_mean, 8,4);
1072 tab_float (trb->t, 6, i+3, TAB_RIGHT,
1073 ttp->mean_diff + t * ttp->se_mean, 8,4);
1077 /* Base initializer for the generalized trbox */
1079 trbox_base_init(struct trbox *self, int data_rows, int cols)
1081 const int rows = 3 + data_rows;
1083 self->finalize = trbox_base_finalize;
1084 self->t = tab_create (cols, rows, 0);
1085 tab_headers (self->t,0,0,3,0);
1086 tab_box (self->t, TAL_2, TAL_2, TAL_0, TAL_0, 0, 0, cols -1, rows -1);
1087 tab_hline(self->t, TAL_2,0,cols-1,3);
1088 tab_dim (self->t, tab_natural_dimensions);
1092 /* Base finalizer for the trbox */
1094 trbox_base_finalize(struct trbox *trb)
1100 /* Create , populate and submit the Paired Samples Correlation box */
1102 pscbox(struct cmd_t_test *cmd)
1104 const int rows=1+n_pairs;
1108 struct tab_table *table;
1110 table = tab_create (cols,rows,0);
1112 tab_columns (table, SOM_COL_DOWN, 1);
1113 tab_headers (table,0,0,1,0);
1114 tab_box (table, TAL_2, TAL_2, TAL_0, TAL_1, 0, 0, cols -1, rows -1 );
1115 tab_hline(table, TAL_2, 0, cols - 1, 1);
1116 tab_vline(table, TAL_2, 2, 0, rows - 1);
1117 tab_dim(table, tab_natural_dimensions);
1118 tab_title(table, 0, _("Paired Samples Correlations"));
1120 /* column headings */
1121 tab_text(table, 2,0, TAB_CENTER | TAT_TITLE, _("N"));
1122 tab_text(table, 3,0, TAB_CENTER | TAT_TITLE, _("Correlation"));
1123 tab_text(table, 4,0, TAB_CENTER | TAT_TITLE, _("Sig."));
1126 for (i=0; i < n_pairs; ++i)
1134 double df = pairs[i].v[0]->p.t_t.n -2;
1136 double correlation_t =
1137 pairs[i].correlation * sqrt(df) /
1138 sqrt(1 - sqr(pairs[i].correlation));
1142 tab_text(table, 0,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1145 tab_text(table, 1,i+1, TAB_LEFT | TAT_TITLE | TAT_PRINTF,
1146 _("%s & %s"), pairs[i].v[0]->name, pairs[i].v[1]->name);
1150 tab_float(table, 3, i+1, TAB_RIGHT, pairs[i].correlation, 8, 3);
1151 tab_float(table, 2, i+1, TAB_RIGHT, pairs[i].v[0]->p.t_t.n , 4, 0);
1154 cdft(&which, &p, &q, &correlation_t, &df, &status, &bound);
1158 msg( SE, _("Error calculating T statistic (cdft returned %d)."),status);
1162 tab_float(table, 4, i+1, TAB_RIGHT, q*2.0, 8, 3);
1174 /* Calculation Implementation */
1176 /* Per case calculations common to all variants of the T test */
1178 common_calc (struct ccase *c)
1182 double weight = dict_get_case_weight(default_dict,c);
1184 for(i=0; i< cmd.n_variables ; ++i)
1186 struct t_test_proc *ttp;
1187 struct variable *v = cmd.v_variables[i];
1188 union value *val = &c->data[v->fv];
1190 ttp= &cmd.v_variables[i]->p.t_t;
1192 if (val->f != SYSMIS)
1195 ttp->sum+=weight * val->f;
1196 ttp->ssq+=weight * val->f * val->f;
1202 /* Pre calculations common to all variants of the T test */
1204 common_precalc (void)
1208 for(i=0; i< cmd.n_variables ; ++i)
1210 struct t_test_proc *ttp;
1211 ttp= &cmd.v_variables[i]->p.t_t;
1220 /* Post calculations common to all variants of the T test */
1222 common_postcalc (void)
1226 for(i=0; i< cmd.n_variables ; ++i)
1228 struct t_test_proc *ttp;
1229 ttp= &cmd.v_variables[i]->p.t_t;
1231 ttp->mean=ttp->sum / ttp->n;
1232 ttp->s_std_dev= sqrt(
1233 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1238 ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean )
1241 ttp->se_mean = ttp->std_dev / sqrt(ttp->n);
1242 ttp->mean_diff= ttp->sum_diff / ttp->n;
1246 /* Per case calculations for one sample t test */
1248 one_sample_calc (struct ccase *c)
1252 double weight = dict_get_case_weight(default_dict,c);
1254 for(i=0; i< cmd.n_variables ; ++i)
1256 struct t_test_proc *ttp;
1257 struct variable *v = cmd.v_variables[i];
1258 union value *val = &c->data[v->fv];
1260 ttp= &cmd.v_variables[i]->p.t_t;
1262 if (val->f != SYSMIS)
1263 ttp->sum_diff += weight * (val->f - cmd.n_testval);
1269 /* Pre calculations for one sample t test */
1271 one_sample_precalc (void)
1275 for(i=0; i< cmd.n_variables ; ++i)
1277 struct t_test_proc *ttp;
1278 ttp= &cmd.v_variables[i]->p.t_t;
1284 /* Post calculations for one sample t test */
1286 one_sample_postcalc (void)
1290 for(i=0; i< cmd.n_variables ; ++i)
1292 struct t_test_proc *ttp;
1293 ttp= &cmd.v_variables[i]->p.t_t;
1296 ttp->mean_diff = ttp->sum_diff / ttp->n ;
1303 compare_var_name (const void *a_, const void *b_, void *v_ unused)
1305 const struct variable *a = a_;
1306 const struct variable *b = b_;
1308 return strcmp(a->name,b->name);
1312 hash_var_name (const void *a_, void *v_ unused)
1314 const struct variable *a = a_;
1316 return hsh_hash_bytes (a->name, strlen(a->name));
1321 paired_precalc (void)
1325 for(i=0; i < n_pairs ; ++i )
1327 pairs[i].correlation=0;
1328 pairs[i].sum_of_diffs=0;
1329 pairs[i].ssq_diffs=0;
1335 paired_calc (struct ccase *c)
1339 for(i=0; i < n_pairs ; ++i )
1341 struct variable *v0 = pairs[i].v[0];
1342 struct variable *v1 = pairs[i].v[1];
1344 union value *val0 = &c->data[v0->fv];
1345 union value *val1 = &c->data[v1->fv];
1347 pairs[i].correlation += ( val0->f - pairs[i].v[0]->p.t_t.mean )
1349 ( val1->f - pairs[i].v[1]->p.t_t.mean );
1351 pairs[i].sum_of_diffs += val0->f - val1->f ;
1352 pairs[i].ssq_diffs += sqr(val0->f - val1->f);
1361 paired_postcalc (void)
1365 for(i=0; i < n_pairs ; ++i )
1367 const double n = pairs[i].v[0]->p.t_t.n ;
1369 pairs[i].correlation /= pairs[i].v[0]->p.t_t.std_dev *
1370 pairs[i].v[1]->p.t_t.std_dev ;
1371 pairs[i].correlation /= pairs[i].v[0]->p.t_t.n -1;
1374 pairs[i].mean_diff = pairs[i].sum_of_diffs / n ;
1377 pairs[i].std_dev_diff = sqrt ( n / (n - 1) * (
1378 ( pairs[i].ssq_diffs / n )
1380 sqr(pairs[i].mean_diff )