projects
/
pspp-builds.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
CROSSTABS: Handle long string variables without crashing (bug #26131).
[pspp-builds.git]
/
src
/
language
/
stats
/
chisquare.c
diff --git
a/src/language/stats/chisquare.c
b/src/language/stats/chisquare.c
index e9ab64c29ddd4879aebe06487834ed9399e14258..6287977b5abc1fb21272823e6ec5c0f4bd2fa101 100644
(file)
--- a/
src/language/stats/chisquare.c
+++ b/
src/language/stats/chisquare.c
@@
-1,20
+1,18
@@
-/* PSPP -
computes sample statistic
s.
+/* PSPP -
a program for statistical analysi
s.
Copyright (C) 2006, 2007 Free Software Foundation, Inc.
Copyright (C) 2006, 2007 Free Software Foundation, Inc.
- This program is free software
; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of the
-
License, or
(at your option) any later version.
+ This program is free software
: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful,
but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU
- General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+
but
WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ G
NU G
eneral Public License for more details.
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <config.h>
@@
-23,6
+21,7
@@
#include <stdlib.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
+#include <data/format.h>
#include <data/case.h>
#include <data/casereader.h>
#include <data/dictionary.h>
#include <data/case.h>
#include <data/casereader.h>
#include <data/dictionary.h>
@@
-31,7
+30,6
@@
#include <data/variable.h>
#include <language/stats/freq.h>
#include <language/stats/npar.h>
#include <data/variable.h>
#include <language/stats/freq.h>
#include <language/stats/npar.h>
-#include <libpspp/alloc.h>
#include <libpspp/assertion.h>
#include <libpspp/compiler.h>
#include <libpspp/hash.h>
#include <libpspp/assertion.h>
#include <libpspp/compiler.h>
#include <libpspp/hash.h>
@@
-41,6
+39,8
@@
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_cdf.h>
+#include "xalloc.h"
+
#include "gettext.h"
#define _(msgid) gettext (msgid)
#include "gettext.h"
#define _(msgid) gettext (msgid)
@@
-196,6
+196,8
@@
create_variable_frequency_table (const struct dictionary *dict,
test->n_expected, n_cells,
var_get_name (var)
);
test->n_expected, n_cells,
var_get_name (var)
);
+ hsh_destroy (*freq_hash);
+ *freq_hash = NULL;
return NULL;
}
return NULL;
}
@@
-268,8
+270,8
@@
create_combo_frequency_table (const struct chisquare_test *test)
}
for ( i = test->lo ; i <= test->hi ; ++i )
}
for ( i = test->lo ; i <= test->hi ; ++i )
- tab_f
loat
(table, 0, 2 + i - test->lo,
- TAB_LEFT, 1 + i - test->lo, 8, 0);
+ tab_f
ixed
(table, 0, 2 + i - test->lo,
+
TAB_LEFT, 1 + i - test->lo, 8, 0);
tab_headers (table, 1, 0, 2, 0);
tab_headers (table, 1, 0, 2, 0);
@@
-327,6
+329,9
@@
chisquare_execute (const struct dataset *ds,
struct chisquare_test *cst = (struct chisquare_test *) test;
int n_cells = 0;
double total_expected = 0.0;
struct chisquare_test *cst = (struct chisquare_test *) test;
int n_cells = 0;
double total_expected = 0.0;
+ const struct variable *wvar = dict_get_weight (dict);
+ const struct fmt_spec *wfmt = wvar ?
+ var_get_print_format (wvar) : & F_8_0;
double *df = xzalloc (sizeof (*df) * ost->n_vars);
double *xsq = xzalloc (sizeof (*df) * ost->n_vars);
double *df = xzalloc (sizeof (*df) * ost->n_vars);
double *xsq = xzalloc (sizeof (*df) * ost->n_vars);
@@
-361,36
+366,41
@@
chisquare_execute (const struct dataset *ds,
xsq[v] = 0.0;
for ( i = 0 ; i < n_cells ; ++i )
{
xsq[v] = 0.0;
for ( i = 0 ; i < n_cells ; ++i )
{
+ struct string str;
double exp;
const union value *observed_value = ff[i]->value;
double exp;
const union value *observed_value = ff[i]->value;
+ ds_init_empty (&str);
+ var_append_value_name (ost->vars[v], observed_value, &str);
+
/* The key */
/* The key */
- tab_text (freq_table, 0, i + 1, TAB_LEFT,
- var_get_value_name (ost->vars[v], observed_value));
+ tab_text (freq_table, 0, i + 1, TAB_LEFT, ds_cstr (&str));
+ ds_destroy (&str);
+
/* The observed N */
/* The observed N */
- tab_
float
(freq_table, 1, i + 1, TAB_NONE,
- ff[i]->count,
8, 0
);
+ tab_
double
(freq_table, 1, i + 1, TAB_NONE,
+ ff[i]->count,
wfmt
);
if ( cst->n_expected > 0 )
exp = cst->expected[i] * total_obs / total_expected ;
else
exp = total_obs / (double) n_cells;
if ( cst->n_expected > 0 )
exp = cst->expected[i] * total_obs / total_expected ;
else
exp = total_obs / (double) n_cells;
- tab_
float
(freq_table, 2, i + 1, TAB_NONE,
- exp,
8, 2
);
+ tab_
double
(freq_table, 2, i + 1, TAB_NONE,
+ exp,
NULL
);
/* The residual */
/* The residual */
- tab_
float
(freq_table, 3, i + 1, TAB_NONE,
- ff[i]->count - exp,
8, 2
);
+ tab_
double
(freq_table, 3, i + 1, TAB_NONE,
+ ff[i]->count - exp,
NULL
);
xsq[v] += (ff[i]->count - exp) * (ff[i]->count - exp) / exp;
}
df[v] = n_cells - 1.0;
xsq[v] += (ff[i]->count - exp) * (ff[i]->count - exp) / exp;
}
df[v] = n_cells - 1.0;
- tab_
float
(freq_table, 1, i + 1, TAB_NONE,
- total_obs,
8, 0
);
+ tab_
double
(freq_table, 1, i + 1, TAB_NONE,
+ total_obs,
wfmt
);
tab_submit (freq_table);
tab_submit (freq_table);
@@
-427,17
+437,21
@@
chisquare_execute (const struct dataset *ds,
xsq[v] = 0.0;
for ( i = 0 ; i < hsh_count (freq_hash) ; ++i )
{
xsq[v] = 0.0;
for ( i = 0 ; i < hsh_count (freq_hash) ; ++i )
{
+ struct string str;
double exp;
const union value *observed_value = ff[i]->value;
double exp;
const union value *observed_value = ff[i]->value;
+ ds_init_empty (&str);
+ var_append_value_name (ost->vars[v], observed_value, &str);
/* The key */
tab_text (freq_table, v * 4 + 1, i + 2 , TAB_LEFT,
/* The key */
tab_text (freq_table, v * 4 + 1, i + 2 , TAB_LEFT,
- var_get_value_name (ost->vars[v], observed_value));
+ ds_cstr (&str));
+ ds_destroy (&str);
/* The observed N */
/* The observed N */
- tab_
float
(freq_table, v * 4 + 2, i + 2 , TAB_NONE,
- ff[i]->count,
8, 0
);
+ tab_
double
(freq_table, v * 4 + 2, i + 2 , TAB_NONE,
+ ff[i]->count,
wfmt
);
if ( cst->n_expected > 0 )
exp = cst->expected[i] * total_obs / total_expected ;
if ( cst->n_expected > 0 )
exp = cst->expected[i] * total_obs / total_expected ;
@@
-445,19
+459,19
@@
chisquare_execute (const struct dataset *ds,
exp = total_obs / (double) hsh_count (freq_hash);
/* The expected N */
exp = total_obs / (double) hsh_count (freq_hash);
/* The expected N */
- tab_
float
(freq_table, v * 4 + 3, i + 2 , TAB_NONE,
- exp,
8, 2
);
+ tab_
double
(freq_table, v * 4 + 3, i + 2 , TAB_NONE,
+ exp,
NULL
);
/* The residual */
/* The residual */
- tab_
float
(freq_table, v * 4 + 4, i + 2 , TAB_NONE,
- ff[i]->count - exp,
8, 2
);
+ tab_
double
(freq_table, v * 4 + 4, i + 2 , TAB_NONE,
+ ff[i]->count - exp,
NULL
);
xsq[v] += (ff[i]->count - exp) * (ff[i]->count - exp) / exp;
}
xsq[v] += (ff[i]->count - exp) * (ff[i]->count - exp) / exp;
}
- tab_
float
(freq_table, v * 4 + 2, tab_nr (freq_table) - 1, TAB_NONE,
- total_obs,
8, 0
);
+ tab_
double
(freq_table, v * 4 + 2, tab_nr (freq_table) - 1, TAB_NONE,
+ total_obs,
wfmt
);
df[v] = n_cells - 1.0;
df[v] = n_cells - 1.0;
@@
-480,11
+494,11
@@
chisquare_execute (const struct dataset *ds,
tab_text (stats_table, 1 + v, 0, TAB_CENTER, var_get_name (var));
tab_text (stats_table, 1 + v, 0, TAB_CENTER, var_get_name (var));
- tab_
float (stats_table, 1 + v, 1, TAB_NONE, xsq[v], 8,3
);
- tab_f
loat (stats_table, 1 + v, 2, TAB_NONE, df[v], 8,
0);
+ tab_
double (stats_table, 1 + v, 1, TAB_NONE, xsq[v], NULL
);
+ tab_f
ixed (stats_table, 1 + v, 2, TAB_NONE, df[v], 8,
0);
- tab_
float
(stats_table, 1 + v, 3, TAB_NONE,
- gsl_cdf_chisq_Q (xsq[v], df[v]),
8,3
);
+ tab_
double
(stats_table, 1 + v, 3, TAB_NONE,
+ gsl_cdf_chisq_Q (xsq[v], df[v]),
NULL
);
}
tab_submit (stats_table);
}
}
tab_submit (stats_table);
}