From 2381b300df6b6a2023d15cc3bce02db92039aa87 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Wed, 4 Feb 2004 08:03:06 +0000 Subject: [PATCH] Added calculations for the one sample t test --- src/ChangeLog | 4 + src/crosstabs.q | 1 - src/lexer.c | 2 +- src/lexer.h | 2 +- src/t-test.q | 193 ++++++++++++++++++++++++++++++++++++++++++++++++ src/tab.c | 5 ++ src/var.h | 28 +++++++ 7 files changed, 232 insertions(+), 3 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 7b5fb3d7..862ffdaa 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +Wed Feb 4 15:34:11 WST 2004 John Darrington + + * t-test.q: Added calculations for the one sample variant of the T-TEST + Tue Feb 3 20:09:54 2004 Ben Pfaff * tab.c: (render_strip) Fix bug that sometimes caused joined text diff --git a/src/crosstabs.q b/src/crosstabs.q index 4bd5e68d..bf9adc3d 100644 --- a/src/crosstabs.q +++ b/src/crosstabs.q @@ -38,7 +38,6 @@ #include "alloc.h" #include "hash.h" #include "pool.h" -#include "dcdflib/cdflib.h" #include "command.h" #include "lexer.h" #include "error.h" diff --git a/src/lexer.c b/src/lexer.c index 94793bfb..a3b43a96 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -422,7 +422,7 @@ lex_double_p (void) /* Returns the value of the current token, which must be a floating point number. */ -long +double lex_double (void) { assert (lex_double_p ()); diff --git a/src/lexer.h b/src/lexer.h index 83bfa825..cf745158 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -88,7 +88,7 @@ int lex_end_of_command (void); int lex_integer_p (void); long lex_integer (void); int lex_double_p (void); -long lex_double (void); +double lex_double (void); /* Token matching functions. */ int lex_match (int); diff --git a/src/t-test.q b/src/t-test.q index 589e64a8..34c3f078 100644 --- a/src/t-test.q +++ b/src/t-test.q @@ -55,6 +55,7 @@ static struct cmd_t_test cmd; + static struct pool *t_test_pool ; /* Variable for the GROUPS subcommand, if given. */ @@ -129,6 +130,16 @@ enum { T_PAIRED }; + +static int common_calc (struct ccase *); +static void common_precalc (void); +static void common_postcalc (void); + +static int one_sample_calc (struct ccase *); +static void one_sample_precalc (void); +static void one_sample_postcalc (void); + + int cmd_t_test(void) { @@ -170,6 +181,11 @@ cmd_t_test(void) return CMD_FAILURE; } + procedure(common_precalc,common_calc,common_postcalc); + + if (mode == T_1_SAMPLE) + procedure(one_sample_precalc,one_sample_calc,one_sample_postcalc); + t_test_pool = pool_create (); ssbox_create(&stat_summary_box,&cmd,mode); @@ -250,11 +266,15 @@ tts_custom_groups (struct cmd_t_test *cmd unused) return 1; } + + + static int tts_custom_pairs (struct cmd_t_test *cmd unused) { struct variable **vars; int n_vars; + int n_before_WITH ; int n_after_WITH = -1; int paired ; /* Was the PAIRED keyword given ? */ @@ -615,7 +635,14 @@ ssbox_one_sample_populate(struct ssbox *ssb, struct cmd_t_test *cmd) for (i=0; i < cmd->n_variables; ++i) { + struct t_test_proc *ttp; + ttp= &cmd->v_variables[i]->p.t_t; + tab_text (ssb->t, 0, i+1, TAB_LEFT, cmd->v_variables[i]->name); + tab_float (ssb->t,1, i+1, TAB_RIGHT, ttp->n, 2, 0); + tab_float (ssb->t,2, i+1, TAB_RIGHT, ttp->mean, 8, 2); + tab_float (ssb->t,3, i+1, TAB_RIGHT, ttp->std_dev, 8, 2); + tab_float (ssb->t,4, i+1, TAB_RIGHT, ttp->se_mean, 8, 3); } } @@ -844,6 +871,7 @@ trbox_one_sample_init(struct trbox *self, struct cmd_t_test *cmd ) tab_text (self->t, 4, 2, TAB_CENTER | TAT_TITLE, _("Mean Difference")); tab_text (self->t, 5, 2, TAB_CENTER | TAT_TITLE, _("Lower")); tab_text (self->t, 6, 2, TAB_CENTER | TAT_TITLE, _("Upper")); + } @@ -857,7 +885,48 @@ trbox_one_sample_populate(struct trbox *trb, struct cmd_t_test *cmd) for (i=0; i < cmd->n_variables; ++i) { + int which =1; + double t; + double p,q; + double df; + int status; + double bound; + struct t_test_proc *ttp; + ttp= &cmd->v_variables[i]->p.t_t; + + tab_text (trb->t, 0, i+3, TAB_LEFT, cmd->v_variables[i]->name); + + t = (ttp->mean - cmd->n_testval ) * sqrt(ttp->n) / ttp->std_dev ; + + tab_float (trb->t, 1, i+3, TAB_RIGHT, t, 8,3); + + /* degrees of freedom */ + df = ttp->n - 1; + + tab_float (trb->t, 2, i+3, TAB_RIGHT, df, 8,0); + + cdft(&which, &p, &q, &t, &df, &status, &bound); + + assert(status == 0 ); /* FIXME: use proper error message */ + + /* Multiply by 2 to get 2-tailed significance */ + tab_float (trb->t, 3, i+3, TAB_RIGHT, q*2.0, 8,3); + + tab_float (trb->t, 4, i+3, TAB_RIGHT, ttp->mean_diff, 8,3); + + + q = (1 - cmd->criteria)/2.0; /* 2-tailed test */ + p = 1 - q ; + which=2; /* Calc T from p,q and df */ + cdft(&which, &p, &q, &t, &df, &status, &bound); + assert(status == 0 ); /* FIXME: proper error message */ + + tab_float (trb->t, 5, i+3, TAB_RIGHT, + ttp->mean_diff - t * ttp->se_mean, 8,4); + + tab_float (trb->t, 6, i+3, TAB_RIGHT, + ttp->mean_diff + t * ttp->se_mean, 8,4); } } @@ -882,3 +951,127 @@ trbox_base_finalize(struct trbox *trb) { tab_submit(trb->t); } + + +/* Calculation Implementation */ + +/* Per case calculations common to all variants of the T test */ +static int +common_calc (struct ccase *c) +{ + int i; + + double weight = dict_get_case_weight(default_dict,c); + + for(i=0; i< cmd.n_variables ; ++i) + { + struct t_test_proc *ttp; + struct variable *v = cmd.v_variables[i]; + union value *val = &c->data[v->fv]; + + ttp= &cmd.v_variables[i]->p.t_t; + + if (val->f != SYSMIS) + { + ttp->n+=weight; + ttp->sum+=weight * val->f; + ttp->ssq+=weight * val->f * val->f; + } + } + return 0; +} + +/* Pre calculations common to all variants of the T test */ +static void +common_precalc (void) +{ + int i=0; + + for(i=0; i< cmd.n_variables ; ++i) + { + struct t_test_proc *ttp; + ttp= &cmd.v_variables[i]->p.t_t; + + ttp->sum=0; + ttp->n=0; + ttp->ssq=0; + ttp->sum_diff=0; + } +} + +/* Post calculations common to all variants of the T test */ +void +common_postcalc (void) +{ + int i=0; + + for(i=0; i< cmd.n_variables ; ++i) + { + struct t_test_proc *ttp; + ttp= &cmd.v_variables[i]->p.t_t; + + ttp->mean=ttp->sum / ttp->n; + ttp->std_dev= sqrt( + ttp->n/(ttp->n-1) * + ( (ttp->ssq / ttp->n ) - ttp->mean * ttp->mean ) + ) ; + + ttp->se_mean = ttp->std_dev / sqrt(ttp->n); + + ttp->mean_diff= ttp->sum_diff / ttp->n; + } +} + +/* Per case calculations for one sample t test */ +static int +one_sample_calc (struct ccase *c) +{ + int i; + + double weight = dict_get_case_weight(default_dict,c); + + for(i=0; i< cmd.n_variables ; ++i) + { + struct t_test_proc *ttp; + struct variable *v = cmd.v_variables[i]; + union value *val = &c->data[v->fv]; + + ttp= &cmd.v_variables[i]->p.t_t; + + if (val->f != SYSMIS) + ttp->sum_diff += weight * fabs(val->f - cmd.n_testval); + } + + return 0; +} + +/* Pre calculations for one sample t test */ +static void +one_sample_precalc (void) +{ + int i=0; + + for(i=0; i< cmd.n_variables ; ++i) + { + struct t_test_proc *ttp; + ttp= &cmd.v_variables[i]->p.t_t; + + ttp->sum_diff=0; + } +} + +/* Post calculations for one sample t test */ +static void +one_sample_postcalc (void) +{ + int i=0; + + for(i=0; i< cmd.n_variables ; ++i) + { + struct t_test_proc *ttp; + ttp= &cmd.v_variables[i]->p.t_t; + + + ttp->mean_diff = ttp->sum_diff / ttp->n ; + } +} diff --git a/src/tab.c b/src/tab.c index 3166740f..e017418d 100644 --- a/src/tab.c +++ b/src/tab.c @@ -612,6 +612,11 @@ tab_float (struct tab_table *table, int c, int r, unsigned char opt, assert (table != NULL && w <= 40); + assert (c >= 0); + assert (c < table->nc); + assert (r >= 0); + assert (r < table->nr); + f.type = FMT_F; f.w = w; f.d = d; diff --git a/src/var.h b/src/var.h index cc384153..71c64538 100644 --- a/src/var.h +++ b/src/var.h @@ -128,6 +128,33 @@ struct crosstab_proc int count; /* max - min. */ }; + +/* T-TEST private data */ +struct t_test_proc + { + double mean; + + double std_dev; + + /* count */ + double n; + + double sum; + + /* Sum of squares */ + double ssq; + + /* Std Err of Mean */ + double se_mean; + + /* Sum of differnces */ + double sum_diff; + + /* Mean of differences */ + double mean_diff ; + }; + + /* FREQUENCIES private data. */ enum { @@ -312,6 +339,7 @@ struct variable struct sort_cases_proc srt; struct matrix_data_proc mxd; struct match_files_proc mtf; + struct t_test_proc t_t; } p; }; -- 2.30.2