-/* PSPP - computes sample statistics.
- Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
- Written by Ben Pfaff <blp@gnu.org>.
+/* PSPP - a program for statistical analysis.
+ Copyright (C) 1997-9, 2000, 2009 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
+ GNU General Public License for more details.
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
-
-/* FIXME: Many possible optimizations. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <data/dictionary.h>
#include <data/procedure.h>
#include <data/transformations.h>
#include <data/dictionary.h>
#include <data/procedure.h>
#include <data/transformations.h>
#include <language/dictionary/split-file.h>
#include <language/lexer/lexer.h>
#include <language/lexer/variable-parser.h>
#include <language/dictionary/split-file.h>
#include <language/lexer/lexer.h>
#include <language/lexer/variable-parser.h>
#include <math/moments.h>
#include <output/manager.h>
#include <output/table.h>
#include <math/moments.h>
#include <output/manager.h>
#include <output/table.h>
- int src_idx; /* Source index into case data. */
- int dst_idx; /* Destination index into case data. */
+ const struct variable *src_var; /* Variable on which z-score is based. */
+ struct variable *z_var; /* New z-score variable. */
size_t var_cnt; /* Number of variables. */
enum dsc_missing_type missing_type; /* Treatment of missing values. */
size_t var_cnt; /* Number of variables. */
enum dsc_missing_type missing_type; /* Treatment of missing values. */
- struct variable *v; /* Variable to calculate on. */
- char z_name[LONG_NAME_LEN + 1]; /* Name for z-score variable. */
+ const struct variable *v; /* Variable to calculate on. */
+ char z_name[VAR_NAME_LEN + 1]; /* Name for z-score variable. */
double valid, missing; /* Valid, missing counts. */
struct moments *moments; /* Moments. */
double min, max; /* Maximum and mimimum values. */
double valid, missing; /* Valid, missing counts. */
struct moments *moments; /* Moments. */
double min, max; /* Maximum and mimimum values. */
{
DSC_LINE, /* Abbreviated format. */
DSC_SERIAL /* Long format. */
};
/* A DESCRIPTIVES procedure. */
{
DSC_LINE, /* Abbreviated format. */
DSC_SERIAL /* Long format. */
};
/* A DESCRIPTIVES procedure. */
int show_var_labels; /* Nonzero to show variable labels. */
int show_index; /* Nonzero to show variable index. */
enum dsc_format format; /* Output format. */
int show_var_labels; /* Nonzero to show variable labels. */
int show_index; /* Nonzero to show variable index. */
enum dsc_format format; /* Output format. */
-static int try_name (struct dsc_proc *dsc, char *name);
-static int generate_z_varname (struct dsc_proc *dsc, char *z_name,
- const char *name, size_t *z_cnt);
+static bool try_name (const struct dictionary *dict,
+ struct dsc_proc *dsc, const char *name);
+static bool generate_z_varname (const struct dictionary *dict,
+ struct dsc_proc *dsc, char *z_name,
+ const char *name, int *z_cnt);
-static bool calc_descriptives (const struct ccase *first,
- const struct casefile *, void *dsc_);
+static void calc_descriptives (struct dsc_proc *, struct casereader *,
+ struct dataset *);
static void display (struct dsc_proc *dsc);
\f
/* Parser and outline. */
/* Handles DESCRIPTIVES. */
int
static void display (struct dsc_proc *dsc);
\f
/* Parser and outline. */
/* Handles DESCRIPTIVES. */
int
/* Create and initialize dsc. */
dsc = xmalloc (sizeof *dsc);
dsc->vars = NULL;
dsc->var_cnt = 0;
dsc->missing_type = DSC_VARIABLE;
/* Create and initialize dsc. */
dsc = xmalloc (sizeof *dsc);
dsc->vars = NULL;
dsc->var_cnt = 0;
dsc->missing_type = DSC_VARIABLE;
- else if (lex_match_id ("INCLUDE"))
- dsc->include_user_missing = 1;
+ else if (lex_match_id (lexer, "INCLUDE"))
+ dsc->exclude = MV_SYSTEM;
-
- if (!parse_variables (default_dict, &vars, &var_cnt,
+
+ if (!parse_variables_const (lexer, dict, &vars, &var_cnt,
- " a duplicate variable name."), tokid);
- lex_get ();
- if (!lex_force_match (')'))
+ " a duplicate variable name."), lex_tokid (lexer));
+ lex_get (lexer);
+ if (!lex_force_match (lexer, ')'))
/* Construct z-score varnames, show translation table. */
if (z_cnt || save_z_scores)
{
/* Construct z-score varnames, show translation table. */
if (z_cnt || save_z_scores)
{
- if (!generate_z_varname (dsc, dsc->vars[i].z_name,
- dsc->vars[i].v->name, &gen_cnt))
+ if (!generate_z_varname (dict, dsc, dsc->vars[i].z_name,
+ var_get_name (dsc->vars[i].v),
+ &gen_cnt))
if (dsc->calc_stats & (1ul << i) && dsc_info[i].moment > dsc->max_moment)
dsc->max_moment = dsc_info[i].moment;
if (dsc->max_moment != MOMENT_NONE)
if (dsc->calc_stats & (1ul << i) && dsc_info[i].moment > dsc->max_moment)
dsc->max_moment = dsc_info[i].moment;
if (dsc->max_moment != MOMENT_NONE)
- ok = multipass_procedure_with_splits (calc_descriptives, dsc);
+ grouper = casegrouper_create_splits (proc_open (ds), dict);
+ while (casegrouper_get_next_group (grouper, &group))
+ calc_descriptives (dsc, group, ds);
+ ok = casegrouper_destroy (grouper);
+ ok = proc_commit (ds) && ok;
-/* Returns 0 if NAME is a duplicate of any existing variable name or
- of any previously-declared z-var name; otherwise returns 1. */
-static int
-try_name (struct dsc_proc *dsc, char *name)
+/* Returns false if NAME is a duplicate of any existing variable name or
+ of any previously-declared z-var name; otherwise returns true. */
+static bool
+try_name (const struct dictionary *dict, struct dsc_proc *dsc,
+ const char *name)
for (i = 0; i < dsc->var_cnt; i++)
if (!strcasecmp (dsc->vars[i].z_name, name))
for (i = 0; i < dsc->var_cnt; i++)
if (!strcasecmp (dsc->vars[i].z_name, name))
}
/* Generates a name for a Z-score variable based on a variable
named VAR_NAME, given that *Z_CNT generated variable names are
}
/* Generates a name for a Z-score variable based on a variable
named VAR_NAME, given that *Z_CNT generated variable names are
- known to already exist. If successful, returns nonzero and
- copies the new name into Z_NAME. On failure, returns zero. */
-static int
-generate_z_varname (struct dsc_proc *dsc, char *z_name,
- const char *var_name, size_t *z_cnt)
+ known to already exist. If successful, returns true and
+ copies the new name into Z_NAME. On failure, returns false. */
+static bool
+generate_z_varname (const struct dictionary *dict, struct dsc_proc *dsc, char *z_name,
+ const char *var_name, int *z_cnt)
/* Try a name based on the original variable name. */
name[0] = 'Z';
str_copy_trunc (name + 1, sizeof name - 1, var_name);
/* Try a name based on the original variable name. */
name[0] = 'Z';
str_copy_trunc (name + 1, sizeof name - 1, var_name);
msg (SE, _("Ran out of generic names for Z-score variables. "
"There are only 126 generic names: ZSC001-ZSC0999, "
"STDZ01-STDZ09, ZZZZ01-ZZZZ09, ZQZQ01-ZQZQ09."));
msg (SE, _("Ran out of generic names for Z-score variables. "
"There are only 126 generic names: ZSC001-ZSC0999, "
"STDZ01-STDZ09, ZZZZ01-ZZZZ09, ZQZQ01-ZQZQ09."));
t = tab_create (2, cnt + 1, 0);
tab_title (t, _("Mapping of variables to corresponding Z-scores."));
tab_columns (t, SOM_COL_DOWN, 1);
t = tab_create (2, cnt + 1, 0);
tab_title (t, _("Mapping of variables to corresponding Z-scores."));
tab_columns (t, SOM_COL_DOWN, 1);
tab_hline (t, TAL_2, 0, 1, 1);
tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Target"));
tab_hline (t, TAL_2, 0, 1, 1);
tab_text (t, 0, 0, TAB_CENTER | TAT_TITLE, _("Source"));
tab_text (t, 1, 0, TAB_CENTER | TAT_TITLE, _("Target"));
for (i = 0, y = 1; i < dsc->var_cnt; i++)
if (dsc->vars[i].z_name[0] != '\0')
{
for (i = 0, y = 1; i < dsc->var_cnt; i++)
if (dsc->vars[i].z_name[0] != '\0')
{
- tab_text (t, 0, y, TAB_LEFT, dsc->vars[i].v->name);
+ tab_text (t, 0, y, TAB_LEFT, var_get_name (dsc->vars[i].v));
tab_text (t, 1, y++, TAB_LEFT, dsc->vars[i].z_name);
}
}
tab_text (t, 1, y++, TAB_LEFT, dsc->vars[i].z_name);
}
}
-descriptives_trns_proc (void *trns_, struct ccase * c,
- int case_idx UNUSED)
+descriptives_trns_proc (void *trns_, struct ccase **c,
+ casenumber case_idx UNUSED)
- double score = case_num (c, (*vars)->fv);
- if ( score == SYSMIS
- || (!t->include_user_missing
- && mv_is_num_user_missing (&(*vars)->miss, score)))
+ double score = case_num (*c, *vars);
+ if (var_is_num_missing (*vars, score, t->exclude))
- double input = case_num (c, z->src_idx);
- double *output = &case_data_rw (c, z->dst_idx)->f;
+ double input = case_num (*c, z->src_var);
+ double *output = &case_data_rw (*c, z->z_var)->f;
- if (z->mean == SYSMIS || z->std_dev == SYSMIS
- || all_sysmis || input == SYSMIS
- || (!t->include_user_missing
- && mv_is_num_user_missing (&z->v->miss, input)))
+ if (z->mean == SYSMIS || z->std_dev == SYSMIS || all_sysmis
+ || var_is_num_missing (z->src_var, input, t->exclude))
t->z_scores = xnmalloc (cnt, sizeof *t->z_scores);
t->z_score_cnt = cnt;
t->missing_type = dsc->missing_type;
t->z_scores = xnmalloc (cnt, sizeof *t->z_scores);
t->z_score_cnt = cnt;
t->missing_type = dsc->missing_type;
- dst_var = dict_create_var_assert (default_dict, dv->z_name, 0);
- if (dv->v->label)
- {
- dst_var->label = xmalloc (strlen (dv->v->label) + 12);
- cp = stpcpy (dst_var->label, _("Z-score of "));
- strcpy (cp, dv->v->label);
- }
- else
- {
- dst_var->label = xmalloc (strlen (dv->v->name) + 12);
- cp = stpcpy (dst_var->label, _("Z-score of "));
- strcpy (cp, dv->v->name);
- }
+ dst_var = dict_create_var_assert (dataset_dict (ds), dv->z_name, 0);
+ var_set_label (dst_var, xasprintf (_("Z-score of %s"),
+ var_to_string (dv->v)));
- add_transformation (descriptives_trns_proc, descriptives_trns_free, t);
+ add_transformation (ds,
+ descriptives_trns_proc, descriptives_trns_free, t);
-static bool
-calc_descriptives (const struct ccase *first,
- const struct casefile *cf, void *dsc_)
+static void
+calc_descriptives (struct dsc_proc *dsc, struct casereader *group,
+ struct dataset *ds)
- output_split_file_values (first);
+ c = casereader_peek (group, 0);
+ if (c == NULL)
+ {
+ casereader_destroy (group);
+ return;
+ }
+ output_split_file_values (ds, c);
+ case_unref (c);
+
+ group = casereader_create_filter_weight (group, dataset_dict (ds),
+ NULL, NULL);
+
+ pass1 = group;
+ pass2 = dsc->max_moment <= MOMENT_MEAN ? NULL : casereader_clone (pass1);
- double x = case_num (&c, dv->v->fv);
-
- if (dsc->missing_type != DSC_LISTWISE
- && (x == SYSMIS
- || (!dsc->include_user_missing
- && mv_is_num_user_missing (&dv->v->miss, x))))
+ double x = case_num (c, dv->v);
+
+ if (var_is_num_missing (dv->v, x, dsc->exclude))
- double x = case_num (&c, dv->v->fv);
-
- if (dsc->missing_type != DSC_LISTWISE
- && (x == SYSMIS
- || (!dsc->include_user_missing
- && mv_is_num_user_missing (&dv->v->miss, x))))
+ double x = case_num (c, dv->v);
+
+ if (var_is_num_missing (dv->v, x, dsc->exclude))
if (dsc->calc_stats & (1ul << DSC_STDDEV)
&& dv->stats[DSC_VARIANCE] != SYSMIS)
dv->stats[DSC_STDDEV] = sqrt (dv->stats[DSC_VARIANCE]);
if (dsc->calc_stats & (1ul << DSC_STDDEV)
&& dv->stats[DSC_VARIANCE] != SYSMIS)
dv->stats[DSC_STDDEV] = sqrt (dv->stats[DSC_VARIANCE]);
if (dv->stats[DSC_KURTOSIS] != SYSMIS)
dv->stats[DSC_SEKURT] = calc_sekurt (W);
if (dsc->calc_stats & (1ul << DSC_SESKEW)
if (dv->stats[DSC_KURTOSIS] != SYSMIS)
dv->stats[DSC_SEKURT] = calc_sekurt (W);
if (dsc->calc_stats & (1ul << DSC_SESKEW)
-/* Returns nonzero if any of the descriptives variables in DSC's
- variable list have missing values in case C, zero otherwise. */
-static int
-listwise_missing (struct dsc_proc *dsc, const struct ccase *c)
+/* Returns true if any of the descriptives variables in DSC's
+ variable list have missing values in case C, false otherwise. */
+static bool
+listwise_missing (struct dsc_proc *dsc, const struct ccase *c)
- if (x == SYSMIS
- || (!dsc->include_user_missing
- && mv_is_num_user_missing (&dv->v->miss, x)))
- return 1;
+ if (var_is_num_missing (dv->v, x, dsc->exclude))
+ return true;
tab_box (t, -1, -1, -1, TAL_1, 1, 0, nc - 1, dsc->var_cnt);
tab_hline (t, TAL_2, 0, nc - 1, 1);
tab_vline (t, TAL_2, 1, 0, dsc->var_cnt);
tab_box (t, -1, -1, -1, TAL_1, 1, 0, nc - 1, dsc->var_cnt);
tab_hline (t, TAL_2, 0, nc - 1, 1);
tab_vline (t, TAL_2, 1, 0, dsc->var_cnt);
nc = 0;
tab_text (t, nc++, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
nc = 0;
tab_text (t, nc++, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
- tab_text (t, nc++, i + 1, TAB_LEFT, dv->v->name);
- tab_text (t, nc++, i + 1, TAT_PRINTF, "%g", dv->valid);
+ tab_text (t, nc++, i + 1, TAB_LEFT, var_get_name (dv->v));
+ tab_text_format (t, nc++, i + 1, 0, "%g", dv->valid);
- tab_text (t, nc++, i + 1, TAT_PRINTF, "%g", dv->missing);
+ tab_text_format (t, nc++, i + 1, 0, "%g", dv->missing);
+
- tab_float (t, nc++, i + 1, TAB_NONE, dv->stats[j], 10, 3);
+ tab_double (t, nc++, i + 1, TAB_NONE, dv->stats[j], NULL);