const struct ctables_cell *example;
double d_valid; /* Dictionary weight. */
- double d_missing;
+ double d_count;
+ double d_total;
double e_valid; /* Effective weight */
- double e_missing;
+ double e_count;
+ double e_total;
};
enum ctables_summary_variant
struct hmap_node node;
/* The domains that contain this cell. */
- bool contributes_to_domains;
+ uint32_t omit_domains;
struct ctables_domain *domains[N_CTDTS];
bool hide;
+
bool postcompute;
enum ctables_summary_variant sv;
axes[PIVOT_N_AXES];
union ctables_summary *summaries;
+
+ //char *name;
};
struct ctables
size_t n;
size_t allocated;
- struct variable *scale_var;
+ /* The variable to which the summary specs are applied. */
+ struct variable *var;
+
+ /* Whether the variable to which the summary specs are applied is a scale
+ variable for the purpose of summarization.
+
+ (VALIDN and TOTALN act differently for summarizing scale and categorical
+ variables.) */
+ bool is_scale;
+
+ /* If any of these optional additional scale variables are missing, then
+ treat 'var' as if it's missing too. This is for implementing
+ SMISSING=LISTWISE. */
+ struct variable **listwise_vars;
+ size_t n_listwise_vars;
};
static void ctables_summary_spec_set_clone (struct ctables_summary_spec_set *,
size_t scale_idx;
size_t *domains[N_CTDTS];
size_t n_domains[N_CTDTS];
+ size_t group_head;
struct ctables_summary_spec_set specs[N_CSVS];
};
struct ctables_pairwise *pairwise;
};
-struct ctables_var
- {
- bool is_mrset;
- union
- {
- struct variable *var;
- const struct mrset *mrset;
- };
- };
-
-static const struct fmt_spec *
-ctables_var_get_print_format (const struct ctables_var *var)
-{
- return (var->is_mrset
- ? var_get_print_format (var->mrset->vars[0])
- : var_get_print_format (var->var));
-}
-
-static const char *
-ctables_var_name (const struct ctables_var *var)
-{
- return var->is_mrset ? var->mrset->name : var_get_name (var->var);
-}
-
struct ctables_categories
{
size_t n_refs;
CCT_VALUE,
CCT_LABEL,
CCT_FUNCTION,
+
+ /* For contributing to TOTALN. */
+ CCT_EXCLUDED_MISSING,
}
type;
union
{
double number; /* CCT_NUMBER. */
- char *string; /* CCT_STRING. */
+ char *string; /* CCT_STRING. In dictionary encoding. */
double range[2]; /* CCT_RANGE. */
struct
};
/* Source location. This is null for CCT_TOTAL, CCT_VALUE, CCT_LABEL,
- CCT_FUNCTION. */
+ CCT_FUNCTION, CCT_EXCLUDED_MISSING. */
struct msg_location *location;
};
case CCT_LABEL:
case CCT_FUNCTION:
break;
+
+ case CCT_EXCLUDED_MISSING:
+ break;
}
}
&& a->sort_function == b->sort_function
&& a->sort_var == b->sort_var
&& a->percentile == b->percentile);
+
+ case CCT_EXCLUDED_MISSING:
+ return true;
}
NOT_REACHED ();
/* Terminals. */
struct
{
- struct ctables_var var;
+ struct variable *var;
bool scale;
struct ctables_summary_spec_set specs[N_CSVS];
};
.specs = specs,
.n = src->n,
.allocated = src->n,
- .scale_var = src->scale_var
+ .var = src->var,
+ .is_scale = src->is_scale,
};
}
static struct fmt_spec
ctables_summary_default_format (enum ctables_summary_function function,
- const struct ctables_var *var)
+ const struct variable *var)
{
static const enum ctables_format default_formats[] = {
#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
case CTF_GENERAL:
- return *ctables_var_get_print_format (var);
+ return *var_get_print_format (var);
default:
NOT_REACHED ();
if (axis->op == CTAO_VAR)
{
const char *function_name = ctables_summary_function_name (function);
- const char *var_name = ctables_var_name (&axis->var);
+ const char *var_name = var_get_name (axis->var);
switch (ctables_function_availability (function))
{
case CTFA_MRSETS:
- if (!axis->var.is_mrset)
- {
- msg_at (SE, loc, _("Summary function %s applies only to multiple "
- "response sets."), function_name);
- msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
- var_name);
- return false;
- }
- break;
+ msg_at (SE, loc, _("Summary function %s applies only to multiple "
+ "response sets."), function_name);
+ msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
+ var_name);
+ return false;
case CTFA_SCALE:
+#if 0
if (!axis->scale)
{
msg_at (SE, loc,
var_name);
return false;
}
+#endif
break;
case CTFA_ALL:
.percentile = percentile,
.label = xstrdup (label),
.format = (format ? *format
- : ctables_summary_default_format (function, &axis->var)),
+ : ctables_summary_default_format (function, axis->var)),
.is_ctables_format = is_ctables_format,
};
return true;
static struct ctables_axis *ctables_axis_parse_stack (
struct ctables_axis_parse_ctx *);
-static bool
-ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
- struct ctables_var *var)
-{
- if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
- {
- *var = (struct ctables_var) {
- .is_mrset = true,
- .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
- };
- if (!var->mrset)
- {
- lex_error (lexer, _("'%s' does not name a multiple-response set "
- "in the active file dictionary."),
- lex_tokcstr (lexer));
- return false;
- }
- lex_get (lexer);
- return true;
- }
- else
- {
- *var = (struct ctables_var) {
- .is_mrset = false,
- .var = parse_variable (lexer, dict),
- };
- return var->var != NULL;
- }
-}
static struct ctables_axis *
ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
return NULL;
int start_ofs = lex_ofs (ctx->lexer);
- struct ctables_var var;
- if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
+ struct variable *var = parse_variable (ctx->lexer, ctx->dict);
+ if (!var)
return NULL;
struct ctables_axis *axis = xmalloc (sizeof *axis);
*axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
/* XXX should figure out default measures by reading data */
- axis->scale = (var.is_mrset ? false
- : lex_match_phrase (ctx->lexer, "[S]") ? true
+ axis->scale = (lex_match_phrase (ctx->lexer, "[S]") ? true
: lex_match_phrase (ctx->lexer, "[C]") ? false
- : var_get_measure (var.var) == MEASURE_SCALE);
+ : var_get_measure (var) == MEASURE_SCALE);
axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
lex_ofs (ctx->lexer) - 1);
+ if (axis->scale && var_is_alpha (var))
+ {
+ msg_at (SE, axis->loc, _("Cannot use string variable %s as a scale "
+ "variable."),
+ var_get_name (var));
+ ctables_axis_destroy (axis);
+ return NULL;
+ }
+
return axis;
}
if (!axis)
return NULL;
else if (axis->op == CTAO_VAR)
- {
- if (axis->scale)
- {
- assert (!axis->var.is_mrset);
- return axis;
- }
- else
- return NULL;
- }
+ return axis->scale ? axis : NULL;
else
{
for (size_t i = 0; i < 2; i++)
case CCT_VALUE:
case CCT_LABEL:
case CCT_FUNCTION:
+ case CCT_EXCLUDED_MISSING:
break;
}
}
assert (n == allocate);
const struct ctables_nest *summary_src;
- if (!a->specs[CSV_CELL].n && !a->specs[CSV_CELL].scale_var)
+ if (!a->specs[CSV_CELL].var)
summary_src = b;
- else if (!b->specs[CSV_CELL].n && !b->specs[CSV_CELL].scale_var)
+ else if (!b->specs[CSV_CELL].var)
summary_src = a;
else
NOT_REACHED ();
for (size_t i = 0; i < s0.n; i++)
stack.nests[stack.n++] = s0.nests[i];
for (size_t i = 0; i < s1.n; i++)
- stack.nests[stack.n++] = s1.nests[i];
+ {
+ stack.nests[stack.n] = s1.nests[i];
+ stack.nests[stack.n].group_head += s0.n;
+ stack.n++;
+ }
assert (stack.n == s0.n + s1.n);
free (s0.nests);
free (s1.nests);
return stack;
}
+static struct ctables_stack
+var_fts (const struct ctables_axis *a)
+{
+ struct variable **vars = xmalloc (sizeof *vars);
+ *vars = a->var;
+
+ struct ctables_nest *nest = xmalloc (sizeof *nest);
+ *nest = (struct ctables_nest) {
+ .vars = vars,
+ .n = 1,
+ .scale_idx = a->scale ? 0 : SIZE_MAX,
+ };
+ if (a->specs[CSV_CELL].n || a->scale)
+ for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
+ {
+ ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
+ nest->specs[sv].var = a->var;
+ nest->specs[sv].is_scale = a->scale;
+ }
+ return (struct ctables_stack) { .nests = nest, .n = 1 };
+}
+
static struct ctables_stack
enumerate_fts (enum pivot_axis_type axis_type, const struct ctables_axis *a)
{
switch (a->op)
{
case CTAO_VAR:
- assert (!a->var.is_mrset);
-
- struct variable **vars = xmalloc (sizeof *vars);
- *vars = a->var.var;
-
- struct ctables_nest *nest = xmalloc (sizeof *nest);
- *nest = (struct ctables_nest) {
- .vars = vars,
- .n = 1,
- .scale_idx = a->scale ? 0 : SIZE_MAX,
- };
- if (a->specs[CSV_CELL].n || a->scale)
- for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
- {
- ctables_summary_spec_set_clone (&nest->specs[sv], &a->specs[sv]);
- nest->specs[sv].scale_var = a->var.var;
- }
- return (struct ctables_stack) { .nests = nest, .n = 1 };
+ return var_fts (a);
case CTAO_STACK:
return stack_fts (enumerate_fts (axis_type, a->subs[0]),
enumerate_fts (axis_type, a->subs[1]));
case CTAO_NEST:
+ /* This should consider any of the scale variables found in the result to
+ be linked to each other listwise for SMISSING=LISTWISE. */
return nest_fts (enumerate_fts (axis_type, a->subs[0]),
enumerate_fts (axis_type, a->subs[1]));
}
union ctables_summary
{
/* COUNT, VALIDN, TOTALN. */
- struct
- {
- double valid;
- double missing;
- };
+ double count;
/* MINIMUM, MAXIMUM, RANGE. */
struct
case CTSF_ETOTALN:
case CTSF_VALIDN:
case CTSF_EVALIDN:
- s->missing = s->valid = 0;
+ s->count = 0;
break;
case CTSF_MAXIMUM:
static void
ctables_summary_add (union ctables_summary *s,
const struct ctables_summary_spec *ss,
- const struct variable *scale_var, const union value *value,
+ const struct variable *var, const union value *value,
+ bool is_scale, bool is_scale_missing,
+ bool is_missing, bool excluded_missing,
double d_weight, double e_weight)
{
/* To determine whether a case is included in a given table for a particular
*/
switch (ss->function)
{
- case CTSF_COUNT:
-
case CSTF_TOTALN:
- case CTSF_VALIDN:
- if (scale_var && var_is_value_missing (scale_var, value))
- s->missing += d_weight;
- else
- s->valid += d_weight;
+ case CTSF_ROWPCT_TOTALN:
+ case CTSF_COLPCT_TOTALN:
+ case CTSF_TABLEPCT_TOTALN:
+ case CTSF_SUBTABLEPCT_TOTALN:
+ case CTSF_LAYERPCT_TOTALN:
+ case CTSF_LAYERROWPCT_TOTALN:
+ case CTSF_LAYERCOLPCT_TOTALN:
+ s->count += d_weight;
break;
- case CTSF_ECOUNT:
+ case CTSF_COUNT:
case CTSF_ROWPCT_COUNT:
case CTSF_COLPCT_COUNT:
case CTSF_TABLEPCT_COUNT:
case CTSF_LAYERPCT_COUNT:
case CTSF_LAYERROWPCT_COUNT:
case CTSF_LAYERCOLPCT_COUNT:
+ if (is_scale || !excluded_missing)
+ s->count += d_weight;
+ break;
+
+ case CTSF_VALIDN:
case CTSF_ROWPCT_VALIDN:
case CTSF_COLPCT_VALIDN:
case CTSF_TABLEPCT_VALIDN:
case CTSF_LAYERPCT_VALIDN:
case CTSF_LAYERROWPCT_VALIDN:
case CTSF_LAYERCOLPCT_VALIDN:
- case CTSF_ROWPCT_TOTALN:
- case CTSF_COLPCT_TOTALN:
- case CTSF_TABLEPCT_TOTALN:
- case CTSF_SUBTABLEPCT_TOTALN:
- case CTSF_LAYERPCT_TOTALN:
- case CTSF_LAYERROWPCT_TOTALN:
- case CTSF_LAYERCOLPCT_TOTALN:
+ if (is_scale
+ ? !is_scale_missing
+ : !is_missing)
+ s->count += d_weight;
+ break;
+
case CTSF_MISSING:
- case CTSF_ETOTALN:
+ if (is_missing)
+ s->count += d_weight;
+ break;
+
+ case CTSF_ECOUNT:
+ if (is_scale || !excluded_missing)
+ s->count += e_weight;
+ break;
+
case CTSF_EVALIDN:
- if (scale_var && var_is_value_missing (scale_var, value))
- s->missing += e_weight;
- else
- s->valid += e_weight;
+ if (is_scale
+ ? !is_scale_missing
+ : !is_missing)
+ s->count += e_weight;
+ break;
+
+ case CTSF_ETOTALN:
+ s->count += e_weight;
break;
case CTSF_MAXIMUM:
case CTSF_MINIMUM:
case CTSF_RANGE:
- if (!var_is_value_missing (scale_var, value))
+ if (!is_scale_missing)
{
- assert (!var_is_alpha (scale_var)); /* XXX? */
+ assert (!var_is_alpha (var)); /* XXX? */
if (s->min == SYSMIS || value->f < s->min)
s->min = value->f;
if (s->max == SYSMIS || value->f > s->max)
case CTSF_LAYERPCT_SUM:
case CTSF_LAYERROWPCT_SUM:
case CTSF_LAYERCOLPCT_SUM:
- if (!var_is_value_missing (scale_var, value))
+ if (!is_scale_missing)
moments1_add (s->moments, value->f, e_weight);
break;
case CTSF_MEDIAN:
case CTSF_MODE:
case CTSF_PTILE:
- if (var_is_value_missing (scale_var, value))
+ if (!is_scale_missing)
{
s->ovalid += e_weight;
{
case CTSF_COUNT:
case CTSF_ECOUNT:
- return s->valid;
+ return s->count;
case CTSF_ROWPCT_COUNT:
case CTSF_COLPCT_COUNT:
case CTSF_LAYERCOLPCT_COUNT:
{
enum ctables_domain_type d = ctables_function_domain (ss->function);
- return (cell->domains[d]->e_valid
- ? s->valid / cell->domains[d]->e_valid * 100
+ return (cell->domains[d]->e_count
+ ? s->count / cell->domains[d]->e_count * 100
: SYSMIS);
}
case CTSF_LAYERPCT_VALIDN:
case CTSF_LAYERROWPCT_VALIDN:
case CTSF_LAYERCOLPCT_VALIDN:
+ {
+ enum ctables_domain_type d = ctables_function_domain (ss->function);
+ return (cell->domains[d]->e_valid
+ ? s->count / cell->domains[d]->e_valid * 100
+ : SYSMIS);
+ }
+
case CTSF_ROWPCT_TOTALN:
case CTSF_COLPCT_TOTALN:
case CTSF_TABLEPCT_TOTALN:
case CTSF_LAYERPCT_TOTALN:
case CTSF_LAYERROWPCT_TOTALN:
case CTSF_LAYERCOLPCT_TOTALN:
- NOT_REACHED ();
+ {
+ enum ctables_domain_type d = ctables_function_domain (ss->function);
+ return (cell->domains[d]->e_total
+ ? s->count / cell->domains[d]->e_total * 100
+ : SYSMIS);
+ }
case CTSF_MISSING:
- return s->missing;
+ return s->count;
case CSTF_TOTALN:
case CTSF_ETOTALN:
- return s->valid + s->missing;
+ return s->count;
case CTSF_VALIDN:
+ return s->count;
+
case CTSF_EVALIDN:
- return s->valid;
+ return s->count;
case CTSF_MAXIMUM:
return s->max;
case CCT_SUBTOTAL:
case CCT_TOTAL:
case CCT_POSTCOMPUTE:
+ case CCT_EXCLUDED_MISSING:
/* Must be equal. */
continue;
for (size_t i = 0; i < nest->n_domains[domain]; i++)
{
size_t v_idx = nest->domains[domain][i];
- hash = value_hash (&cell->axes[a].cvs[v_idx].value,
- var_get_width (nest->vars[v_idx]), hash);
+ struct ctables_cell_value *cv = &cell->axes[a].cvs[v_idx];
+ hash = hash_pointer (cv->category, hash);
+ if (cv->category->type != CCT_TOTAL
+ && cv->category->type != CCT_SUBTOTAL
+ && cv->category->type != CCT_POSTCOMPUTE)
+ hash = value_hash (&cv->value,
+ var_get_width (nest->vars[v_idx]), hash);
}
}
for (size_t i = 0; i < nest->n_domains[domain]; i++)
{
size_t v_idx = nest->domains[domain][i];
- if (!value_equal (&df->axes[a].cvs[v_idx].value,
- &cell->axes[a].cvs[v_idx].value,
- var_get_width (nest->vars[v_idx])))
+ struct ctables_cell_value *cv1 = &df->axes[a].cvs[v_idx];
+ struct ctables_cell_value *cv2 = &cell->axes[a].cvs[v_idx];
+ if (cv1->category != cv2->category
+ || (cv1->category->type != CCT_TOTAL
+ && cv1->category->type != CCT_SUBTOTAL
+ && cv1->category->type != CCT_POSTCOMPUTE
+ && !value_equal (&cv1->value, &cv2->value,
+ var_get_width (nest->vars[v_idx]))))
goto not_equal;
}
}
ctables_categories_match (const struct ctables_categories *c,
const union value *v, const struct variable *var)
{
+ if (var_is_numeric (var) && v->f == SYSMIS)
+ return NULL;
+
const struct ctables_category *othernm = NULL;
for (size_t i = c->n_cats; i-- > 0; )
{
case CCT_FUNCTION:
return (cat->include_missing || !var_is_value_missing (var, v) ? cat
: NULL);
+
+ case CCT_EXCLUDED_MISSING:
+ break;
}
}
cell = xmalloc (sizeof *cell);
cell->hide = false;
cell->sv = sv;
- cell->contributes_to_domains = true;
+ cell->omit_domains = 0;
cell->postcompute = false;
+ //struct string name = DS_EMPTY_INITIALIZER;
for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
{
const struct ctables_nest *nest = s->nests[a];
cell->axes[a].cvs = (nest->n
- ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
- : NULL);
+ ? xnmalloc (nest->n, sizeof *cell->axes[a].cvs)
+ : NULL);
for (size_t i = 0; i < nest->n; i++)
{
const struct ctables_category *cat = cats[a][i];
+ const struct variable *var = nest->vars[i];
+ const union value *value = case_data (c, var);
if (i != nest->scale_idx)
{
const struct ctables_category *subtotal = cat->subtotal;
if (cat->type == CCT_TOTAL
|| cat->type == CCT_SUBTOTAL
|| cat->type == CCT_POSTCOMPUTE)
- cell->contributes_to_domains = false;
+ {
+ /* XXX these should be more encompassing I think.*/
+
+ switch (a)
+ {
+ case PIVOT_AXIS_COLUMN:
+ cell->omit_domains |= ((1u << CTDT_TABLE) |
+ (1u << CTDT_LAYER) |
+ (1u << CTDT_LAYERCOL) |
+ (1u << CTDT_SUBTABLE) |
+ (1u << CTDT_COL));
+ break;
+ case PIVOT_AXIS_ROW:
+ cell->omit_domains |= ((1u << CTDT_TABLE) |
+ (1u << CTDT_LAYER) |
+ (1u << CTDT_LAYERROW) |
+ (1u << CTDT_SUBTABLE) |
+ (1u << CTDT_ROW));
+ break;
+ case PIVOT_AXIS_LAYER:
+ cell->omit_domains |= ((1u << CTDT_TABLE) |
+ (1u << CTDT_LAYER));
+ break;
+ }
+ }
if (cat->type == CCT_POSTCOMPUTE)
cell->postcompute = true;
}
cell->axes[a].cvs[i].category = cat;
- value_clone (&cell->axes[a].cvs[i].value, case_data (c, nest->vars[i]),
- var_get_width (nest->vars[i]));
+ value_clone (&cell->axes[a].cvs[i].value, value, var_get_width (var));
+
+#if 0
+ if (i != nest->scale_idx)
+ {
+ if (!ds_is_empty (&name))
+ ds_put_cstr (&name, ", ");
+ char *value_s = data_out (value, var_get_encoding (var),
+ var_get_print_format (var),
+ settings_get_fmt_settings ());
+ if (cat->type == CCT_TOTAL
+ || cat->type == CCT_SUBTOTAL
+ || cat->type == CCT_POSTCOMPUTE)
+ ds_put_format (&name, "%s=total", var_get_name (var));
+ else
+ ds_put_format (&name, "%s=%s", var_get_name (var),
+ value_s + strspn (value_s, " "));
+ free (value_s);
+ }
+#endif
}
}
+ //cell->name = ds_steal_cstr (&name);
const struct ctables_nest *ss = s->nests[s->table->summary_axis];
const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
return cell;
}
+static bool
+is_scale_missing (const struct ctables_summary_spec_set *specs,
+ const struct ccase *c)
+{
+ if (!specs->is_scale)
+ return false;
+
+ if (var_is_num_missing (specs->var, case_num (c, specs->var)))
+ return true;
+
+ for (size_t i = 0; i < specs->n_listwise_vars; i++)
+ {
+ const struct variable *var = specs->listwise_vars[i];
+ if (var_is_num_missing (var, case_num (c, var)))
+ return true;
+ }
+
+ return false;
+}
+
static void
ctables_cell_add__ (struct ctables_section *s, const struct ccase *c,
const struct ctables_category *cats[PIVOT_N_AXES][10],
+ bool is_missing, bool excluded_missing,
double d_weight, double e_weight)
{
struct ctables_cell *cell = ctables_cell_insert__ (s, c, cats);
const struct ctables_nest *ss = s->nests[s->table->summary_axis];
const struct ctables_summary_spec_set *specs = &ss->specs[cell->sv];
+
+ bool scale_missing = is_scale_missing (specs, c);
for (size_t i = 0; i < specs->n; i++)
- {
- const struct variable *scale_var = specs->scale_var;
- const union value *value = scale_var ? case_data (c, scale_var) : NULL;
- ctables_summary_add (&cell->summaries[i], &specs->specs[i],
- scale_var, value, d_weight, e_weight);
- }
- if (cell->contributes_to_domains)
- {
- for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
- {
- cell->domains[dt]->d_valid += d_weight;
- cell->domains[dt]->e_valid += e_weight;
- }
- }
+ ctables_summary_add (&cell->summaries[i], &specs->specs[i],
+ specs->var, case_data (c, specs->var), specs->is_scale,
+ scale_missing, is_missing, excluded_missing,
+ d_weight, e_weight);
+ for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+ if (!(cell->omit_domains && (1u << dt)))
+ {
+ struct ctables_domain *d = cell->domains[dt];
+ d->d_total += d_weight;
+ d->e_total += e_weight;
+ if (!excluded_missing)
+ {
+ d->d_count += d_weight;
+ d->e_count += e_weight;
+ }
+ if (!is_missing)
+ {
+ d->d_valid += d_weight;
+ d->e_valid += e_weight;
+ }
+ }
}
static void
recurse_totals (struct ctables_section *s, const struct ccase *c,
const struct ctables_category *cats[PIVOT_N_AXES][10],
+ bool is_missing, bool excluded_missing,
double d_weight, double e_weight,
enum pivot_axis_type start_axis, size_t start_nest)
{
{
const struct ctables_category *save = cats[a][i];
cats[a][i] = total;
- ctables_cell_add__ (s, c, cats, d_weight, e_weight);
- recurse_totals (s, c, cats, d_weight, e_weight, a, i + 1);
+ ctables_cell_add__ (s, c, cats, is_missing, excluded_missing,
+ d_weight, e_weight);
+ recurse_totals (s, c, cats, is_missing, excluded_missing,
+ d_weight, e_weight, a, i + 1);
cats[a][i] = save;
}
}
static void
recurse_subtotals (struct ctables_section *s, const struct ccase *c,
const struct ctables_category *cats[PIVOT_N_AXES][10],
+ bool is_missing, bool excluded_missing,
double d_weight, double e_weight,
enum pivot_axis_type start_axis, size_t start_nest)
{
if (save->subtotal)
{
cats[a][i] = save->subtotal;
- ctables_cell_add__ (s, c, cats, d_weight, e_weight);
- recurse_subtotals (s, c, cats, d_weight, e_weight, a, i + 1);
+ ctables_cell_add__ (s, c, cats, is_missing, excluded_missing,
+ d_weight, e_weight);
+ recurse_subtotals (s, c, cats, is_missing, excluded_missing,
+ d_weight, e_weight, a, i + 1);
cats[a][i] = save;
}
}
double d_weight, double e_weight)
{
const struct ctables_category *cats[PIVOT_N_AXES][10]; /* XXX */
+
+ /* Does at least one categorical variable have a missing value in an included
+ or excluded category? */
+ bool is_missing = false;
+
+ /* Does at least one categorical variable have a missing value in an excluded
+ category? */
+ bool excluded_missing = false;
+
for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
{
const struct ctables_nest *nest = s->nests[a];
const struct variable *var = nest->vars[i];
const union value *value = case_data (c, var);
- if (var_is_numeric (var) && value->f == SYSMIS)
- return;
+ bool var_missing = var_is_value_missing (var, value) != 0;
+ if (var_missing)
+ is_missing = true;
cats[a][i] = ctables_categories_match (
s->table->categories[var_get_dict_index (var)], value, var);
if (!cats[a][i])
- return;
+ {
+ if (!var_missing)
+ return;
+
+ static const struct ctables_category cct_excluded_missing = {
+ .type = CCT_EXCLUDED_MISSING,
+ .hide = true,
+ };
+ cats[a][i] = &cct_excluded_missing;
+ excluded_missing = true;
+ }
}
}
- for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
- {
- const struct ctables_nest *nest = s->nests[a];
- for (size_t i = 0; i < nest->n; i++)
- if (i != nest->scale_idx)
- {
- const struct variable *var = nest->vars[i];
- const union value *value = case_data (c, var);
- ctables_add_occurrence (var, value, &s->occurrences[a][i]);
- }
- }
+ if (!excluded_missing)
+ for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+ {
+ const struct ctables_nest *nest = s->nests[a];
+ for (size_t i = 0; i < nest->n; i++)
+ if (i != nest->scale_idx)
+ {
+ const struct variable *var = nest->vars[i];
+ const union value *value = case_data (c, var);
+ ctables_add_occurrence (var, value, &s->occurrences[a][i]);
+ }
+ }
- ctables_cell_add__ (s, c, cats, d_weight, e_weight);
+ ctables_cell_add__ (s, c, cats, is_missing, excluded_missing,
+ d_weight, e_weight);
- recurse_totals (s, c, cats, d_weight, e_weight, 0, 0);
- recurse_subtotals (s, c, cats, d_weight, e_weight, 0, 0);
+ //if (!excluded_missing)
+ {
+ recurse_totals (s, c, cats, is_missing, excluded_missing,
+ d_weight, e_weight, 0, 0);
+ recurse_subtotals (s, c, cats, is_missing, excluded_missing,
+ d_weight, e_weight, 0, 0);
+ }
}
struct merge_item
struct ctables_cell_sort_aux aux = { .nest = nest, .a = a };
sort (sorted, n_sorted, sizeof *sorted, ctables_cell_compare_3way, &aux);
+#if 0
+ for (size_t j = 0; j < n_sorted; j++)
+ {
+ printf ("%s (%s): %f/%f = %.1f%%\n", sorted[j]->name, sorted[j]->contributes_to_domains ? "y" : "n", sorted[j]->summaries[0].count, sorted[j]->domains[CTDT_COL]->e_count, sorted[j]->summaries[0].count / sorted[j]->domains[CTDT_COL]->e_count * 100.0);
+ }
+ printf ("\n");
+#endif
+
struct ctables_level
{
enum ctables_level_type
specs->n = 1;
enum ctables_summary_function function
- = specs->scale_var ? CTSF_MEAN : CTSF_COUNT;
- struct ctables_var var = { .var = specs->scale_var };
+ = specs->is_scale ? CTSF_MEAN : CTSF_COUNT;
*specs->specs = (struct ctables_summary_spec) {
.function = function,
- .format = ctables_summary_default_format (function, &var),
+ .format = ctables_summary_default_format (function, specs->var),
.label = ctables_summary_default_label (function, 0),
};
+ if (!specs->var)
+ specs->var = nest->vars[0];
ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
&nest->specs[CSV_CELL]);
else if (!nest->specs[CSV_TOTAL].n)
ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
&nest->specs[CSV_CELL]);
+
+ if (t->ctables->smissing_listwise)
+ {
+ struct variable **listwise_vars = NULL;
+ size_t n = 0;
+ size_t allocated = 0;
+
+ for (size_t j = nest->group_head; j < stack->n; j++)
+ {
+ const struct ctables_nest *other_nest = &stack->nests[j];
+ if (other_nest->group_head != nest->group_head)
+ break;
+
+ if (nest != other_nest && other_nest->scale_idx < other_nest->n)
+ {
+ if (n >= allocated)
+ listwise_vars = x2nrealloc (listwise_vars, &allocated,
+ sizeof *listwise_vars);
+ listwise_vars[n++] = other_nest->vars[other_nest->scale_idx];
+ }
+ }
+ for (size_t j = 0; j < N_CSVS; j++)
+ {
+ nest->specs[j].listwise_vars = listwise_vars;
+ nest->specs[j].n_listwise_vars = n;
+ }
+ }
}
struct ctables_summary_spec_set *merged = &t->summary_specs;
if (c->include_missing || !var_is_value_missing (var, &vl->value))
ctables_add_occurrence (var, &vl->value, occurrences);
break;
+
+ case CCT_EXCLUDED_MISSING:
+ break;
}
}
}