1 /* This file is part of GNU PSPP
2 Computes Levene test statistic.
4 Copyright (C) 2004 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <libpspp/message.h>
24 #include <data/case.h>
25 #include <data/casefile.h>
26 #include <data/dictionary.h>
27 #include "group-proc.h"
28 #include <libpspp/hash.h>
29 #include <libpspp/str.h>
30 #include <data/variable.h>
31 #include <data/procedure.h>
32 #include <data/casefilter.h>
33 #include <libpspp/alloc.h>
34 #include <libpspp/misc.h>
41 /* This module calculates the Levene statistic for variables.
43 Just for reference, the Levene Statistic is a defines as follows:
45 W = \frac{ (n-k)\sum_{i=1}^k n_i(Z_{iL} - Z_{LL})^2}
46 { (k-1)\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} - Z_{iL})^2}
49 k is the number of groups
50 n is the total number of samples
51 n_i is the number of samples in the ith group
52 Z_{ij} is | Y_{ij} - Y_{iL} | where Y_{iL} is the mean of the ith group
53 Z_{iL} is the mean of Z_{ij} over the ith group
54 Z_{LL} is the grand mean of Z_{ij}
56 Imagine calculating that with pencil and paper!
64 /* Per group statistics */
65 struct t_test_proc **group_stats;
67 /* The independent variable */
68 struct variable *v_indep;
70 /* Number of dependent variables */
73 /* The dependent variables */
74 struct variable **v_dep;
76 /* Filter for missing values */
77 struct casefilter *filter;
81 static void levene_precalc (const struct levene_info *l);
82 static int levene_calc (const struct dictionary *dict, const struct ccase *,
83 const struct levene_info *l);
84 static void levene_postcalc (void *);
88 static void levene2_precalc (struct levene_info *l);
89 static int levene2_calc (const struct dictionary *, const struct ccase *,
90 struct levene_info *l);
91 static void levene2_postcalc (void *);
95 levene(const struct dictionary *dict,
96 const struct casefile *cf,
97 struct variable *v_indep, size_t n_dep, struct variable **v_dep,
98 struct casefilter *filter)
100 struct casereader *r;
102 struct levene_info l;
111 for(r = casefile_get_reader (cf, filter);
112 casereader_read (r, &c) ;
115 levene_calc (dict, &c, &l);
117 casereader_destroy (r);
118 levene_postcalc (&l);
121 for(r = casefile_get_reader (cf, filter);
122 casereader_read (r, &c) ;
125 levene2_calc (dict, &c,&l);
127 casereader_destroy (r);
128 levene2_postcalc (&l);
131 /* Internal variables used in calculating the Levene statistic */
133 /* Per variable statistics */
136 /* Total of all lz */
142 /* The total number of cases */
145 /* Number of groups */
149 /* An array of lz_stats for each variable */
150 static struct lz_stats *lz;
154 levene_precalc (const struct levene_info *l)
158 lz = xnmalloc (l->n_dep, sizeof *lz);
160 for(i = 0; i < l->n_dep ; ++i )
162 struct variable *var = l->v_dep[i];
163 struct group_proc *gp = group_proc_get (var);
164 struct group_statistics *gs;
165 struct hsh_iterator hi;
167 lz[i].grand_total = 0;
169 lz[i].n_groups = gp->n_groups ;
172 for ( gs = hsh_first(gp->group_hash, &hi);
174 gs = hsh_next(gp->group_hash, &hi))
184 levene_calc (const struct dictionary *dict, const struct ccase *c,
185 const struct levene_info *l)
189 const union value *gv = case_data (c, l->v_indep);
190 struct group_statistics key;
191 double weight = dict_get_case_weight (dict, c, &warn);
195 for (i = 0; i < l->n_dep; ++i)
197 struct variable *var = l->v_dep[i];
198 struct group_proc *gp = group_proc_get (var);
200 const union value *v = case_data (c, var);
201 struct group_statistics *gs;
203 gs = hsh_find(gp->group_hash,(void *) &key );
208 if ( ! casefilter_variable_missing (l->filter, c, var))
210 levene_z= fabs(v->f - gs->mean);
211 lz[i].grand_total += levene_z * weight;
212 lz[i].total_n += weight;
214 gs->lz_total += levene_z * weight;
222 levene_postcalc (void *_l)
226 struct levene_info *l = (struct levene_info *) _l;
228 for (v = 0; v < l->n_dep; ++v)
231 lz[v].grand_mean = lz[v].grand_total / lz[v].total_n ;
239 /* The denominator for the expression for the Levene */
240 static double *lz_denominator = 0;
243 levene2_precalc (struct levene_info *l)
247 lz_denominator = xnmalloc (l->n_dep, sizeof *lz_denominator);
249 /* This stuff could go in the first post calc . . . */
254 struct hsh_iterator hi;
255 struct group_statistics *g;
257 struct variable *var = l->v_dep[v] ;
258 struct hsh_table *hash = group_proc_get (var)->group_hash;
261 for(g = (struct group_statistics *) hsh_first(hash,&hi);
263 g = (struct group_statistics *) hsh_next(hash,&hi) )
265 g->lz_mean = g->lz_total / g->n ;
267 lz_denominator[v] = 0;
272 levene2_calc (const struct dictionary *dict, const struct ccase *c,
273 struct levene_info *l)
278 double weight = dict_get_case_weight (dict, c, &warn);
280 const union value *gv = case_data (c, l->v_indep);
281 struct group_statistics key;
285 for (i = 0; i < l->n_dep; ++i)
288 struct variable *var = l->v_dep[i] ;
289 const union value *v = case_data (c, var);
290 struct group_statistics *gs;
292 gs = hsh_find(group_proc_get (var)->group_hash,(void *) &key );
297 if ( ! casefilter_variable_missing (l->filter, c, var))
300 levene_z = fabs(v->f - gs->mean);
301 lz_denominator[i] += weight * pow2 (levene_z - gs->lz_mean);
310 levene2_postcalc (void *_l)
314 struct levene_info *l = (struct levene_info *) _l;
316 for (v = 0; v < l->n_dep; ++v)
318 double lz_numerator = 0;
319 struct hsh_iterator hi;
320 struct group_statistics *g;
322 struct variable *var = l->v_dep[v] ;
323 struct group_proc *gp = group_proc_get (var);
324 struct hsh_table *hash = gp->group_hash;
326 for(g = (struct group_statistics *) hsh_first(hash,&hi);
328 g = (struct group_statistics *) hsh_next(hash,&hi) )
330 lz_numerator += g->n * pow2(g->lz_mean - lz[v].grand_mean );
332 lz_numerator *= ( gp->ugs.n - gp->n_groups );
334 lz_denominator[v] *= (gp->n_groups - 1);
336 gp->levene = lz_numerator / lz_denominator[v] ;
340 /* Now clear up after ourselves */
341 free(lz_denominator);