Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mix and share

The question

You have a categorical field — region, plan, answer option, the brands a respondent ticked — and you want to know its mix: how the rows split across the categories, what share each option has, and whether that mix differs from one group to another.

It sounds like:

  • “What’s our customer mix by region?”
  • “What share of each region’s customers converted?”
  • “Which attributes go with which brand?”

Counts per category answer the first question; shares answer the rest. In a crosstab the share can run along the row (each row sums to one), down the column (each column sums to one) or over the whole table, and the three answer different questions, so choose the direction from the sentence you want to write.

Pick your analysis

This question fits data shaped like:

  • category: one categorical, bool or set field; by: optionally one categorical or bool field

The operators that answer it, by kind; each name links to its catalog entry. Basic ones need the least setup; the worked example below uses AGG_COUNT and GROUP_CATEGORY.

Aggregators

UseIn plain wordsLevel
AGG_FREQUENCYHow many rows hold one chosen value of a field, such as the Yes answers, over all rows or per group.basic
AGG_MODE_COUNTHow many rows share a field’s most common value, over all rows or per group.basic
AGG_RATIOTotal of one field divided by the total of another, over all rows or per group, such as revenue per order.basic
AGG_SET_FREQUENCYFor a multi-select field, how many rows chose each option, over all rows or per group.basic
AGG_SUMTotal of a numeric field, over all rows or per group.basic

Groupers

UseIn plain wordsLevel
GROUP_CATEGORYSplits the rows into one group per distinct value of a field, such as one group per region or per answer option.basic
GROUP_SET_PER_ELEMENTSplits a multi-select answer into one group per option, counting each row once in every option it picked.intermediate
GROUP_SET_VALUEGroups the rows by the exact combination of options picked in a multi-select answer, such as A only, A and B, or none.intermediate

Window operators

UseIn plain wordsLevel
WIN_SHAREAdds each row’s share of its partition’s total, or the running share down the ordered rows, such as the share of defaults caught so far.basic
WIN_FORMULAAdds a number to every result row computed from that row’s columns and earlier windows, such as one share divided by another.intermediate
WIN_LIFTAdds how many times its share of one column a row holds of another, such as a decile’s share of defaults over its share of loans.intermediate

Feature operators

UseIn plain wordsLevel
FEAT_FREQUENCY_ENCODEReplaces each category with the share of records that carry it, one number that says how common the category is.intermediate

Overlays

UseIn plain wordsLevel
OVERLAY_INDEX_VS_TOTALEach group’s value on a grouped result as an index value against the sum over all groups (group / total x 100).basic
OVERLAY_SHARE_OF_COLEach crosstab cell as a share of its column margin (0 to 1): the mix of rows within each column, as a 100% stacked column shows it.basic
OVERLAY_SHARE_OF_ROWEach crosstab cell as a share of its row margin (0 to 1): the mix of columns within each row, as a 100% stacked bar shows it.basic
OVERLAY_SHARE_OF_TOTALEach crosstab cell or group of a grouped result as a share of the grand total (0 to 1): what part of the whole each piece makes up.basic
OVERLAY_CORRESPONDENCECorrespondence analysis of a crosstab: a perceptual map placing rows and columns in a few dimensions so similar profiles sit close together.advanced
OVERLAY_RAKERake a crosstab to known row and column totals: the fitted table, per-cell adjustment factors and whether the fit converged.advanced

A worked example

To run the example below yourself, write the example cohorts once and work from their directory, since command-line cohort paths resolve against the working directory (Get the data). Start in a scratch directory outside your PULSE_DATA_DIR, so pulse-data does not nest inside the directory pulse mcp serves:

pulse examples data --out pulse-data
cd pulse-data
pulse examples show crosstab_row_normalize_conversion_rate > req.json
pulse api process --request req.json --json

What share of customers in each region converted?

The request (example crosstab_row_normalize_conversion_rate; pulse examples show crosstab_row_normalize_conversion_rate prints it):

{
  "cohort": {
    "filename": "experiment.pulse"
  },
  "crosstab": {
    "rows": [
      {
        "type": "GROUP_CATEGORY",
        "field": "region"
      }
    ],
    "columns": [
      {
        "type": "GROUP_CATEGORY",
        "field": "converted"
      }
    ],
    "cell": {
      "type": "AGG_COUNT",
      "field": "id",
      "label": "n"
    },
    "margins": {
      "rows": true
    },
    "normalize": "row"
  },
  "return": {
    "precision": 4
  }
}

The result:

{
  "metadata": {
    "total_rows": 400,
    "filtered_rows": 400,
    "cohort_file": "experiment.pulse"
  },
  "crosstab": {
    "shape": "matrix",
    "matrix": {
      "row_header": {
        "fields": [
          "region"
        ],
        "types": [
          "GROUP_CATEGORY"
        ]
      },
      "column_header": {
        "fields": [
          "converted"
        ],
        "types": [
          "GROUP_CATEGORY"
        ]
      },
      "row_keys": [
        [
          "east"
        ],
        [
          "north"
        ],
        [
          "south"
        ],
        [
          "west"
        ]
      ],
      "column_keys": [
        [
          "no"
        ],
        [
          "yes"
        ]
      ],
      "cells": [
        [
          {
            "value": 0.6,
            "present": true
          },
          {
            "value": 0.4,
            "present": true
          }
        ],
        [
          {
            "value": 0.5657,
            "present": true
          },
          {
            "value": 0.4343,
            "present": true
          }
        ],
        [
          {
            "value": 0.6176,
            "present": true
          },
          {
            "value": 0.3824,
            "present": true
          }
        ],
        [
          {
            "value": 0.6869,
            "present": true
          },
          {
            "value": 0.3131,
            "present": true
          }
        ]
      ],
      "row_margins": [
        {
          "value": 100,
          "present": true
        },
        {
          "value": 99,
          "present": true
        },
        {
          "value": 102,
          "present": true
        },
        {
          "value": 99,
          "present": true
        }
      ],
      "grand_total": {
        "present": false
      },
      "cell_label": "n",
      "normalize_applied": "row"
    }
  },
  "components": {
    "crosstab": {
      "cell_counts": [
        [
          60,
          40
        ],
        [
          56,
          43
        ],
        [
          63,
          39
        ],
        [
          68,
          31
        ]
      ],
      "cell_components": [
        [
          {
            "n": 60,
            "n_null": 0
          },
          {
            "n": 40,
            "n_null": 0
          }
        ],
        [
          {
            "n": 56,
            "n_null": 0
          },
          {
            "n": 43,
            "n_null": 0
          }
        ],
        [
          {
            "n": 63,
            "n_null": 0
          },
          {
            "n": 39,
            "n_null": 0
          }
        ],
        [
          {
            "n": 68,
            "n_null": 0
          },
          {
            "n": 31,
            "n_null": 0
          }
        ]
      ],
      "row_margin_counts": [
        100,
        99,
        102,
        99
      ],
      "row_margin_components": [
        {
          "n": 100,
          "n_null": 0
        },
        {
          "n": 99,
          "n_null": 0
        },
        {
          "n": 102,
          "n_null": 0
        },
        {
          "n": 99,
          "n_null": 0
        }
      ],
      "row_key_components": [
        {
          "count": 100,
          "key": "east",
          "label": "east"
        },
        {
          "count": 99,
          "key": "north",
          "label": "north"
        },
        {
          "count": 102,
          "key": "south",
          "label": "south"
        },
        {
          "count": 99,
          "key": "west",
          "label": "west"
        }
      ],
      "column_key_components": [
        {
          "count": 247,
          "key": "no",
          "label": "no"
        },
        {
          "count": 153,
          "key": "yes",
          "label": "yes"
        }
      ],
      "included_records": 400
    }
  },
  "returned": {
    "preset": "full",
    "digest": "rp1:2a47356146fe7626329ca77359f19495ac57bb6a599dfdd7658729be64597be6",
    "precision": 4
  }
}

Each row splits one region’s customers into those who converted and those who did not, so the two cells of a row sum to one and the yes cell is that region’s conversion rate. Conversion runs from under a third of customers in west to over two in five in north, with east and south in between. With around a hundred customers per region a gap that size can easily arise by chance: shares describe the mix, they do not test it, so run a chi-square test across the regions before calling north and west different. The row margin keeps each region’s raw count beside its shares.

How to read the result

The figures these operators return read as their names say; for the ones that need more, see Reading your results.

Pitfalls

  • Say what the share is a share of. The same cell is a different share of its row, of its column and of the whole table; name the base every time you report one.
  • A share hides the count behind it. A category with a striking share may rest on a handful of rows; keep the margin counts beside the shares and treat small bases with care.
  • A different mix is not yet a tested difference. Shares describe the data; to say two groups’ mixes really differ, add a chi-square test or a cell-level significance overlay.
  • Multi-select answers do not sum to one. When a respondent can tick several options, option shares add up to more than the whole; read each as the share of rows choosing that option.
  • Missing values change the base. Rows with no category are left out of the shares by default; check how many there were before comparing mixes.
  • Survey data may need a weight. An unweighted mix describes the sample; when some groups are over- or under-represented, set the request’s weight or rake the sample to known population totals.

Next steps

Related questions:

Skills: intents, aggregation-design, feature-engineering, grouper-design, overlay-formula-namespace, overlay-pairwise, overlay-reference-tests, overlay-system, pairwise-n-sources, panel-n-sources, window-design, windows-first, op-agg-count, op-group-category

From the command line:

  • pulse recommend --intent composition --cohort PATH drafts requests for this question bound to your cohort’s fields.
  • pulse api predict checks a request against your cohort without running it.
  • pulse api process runs it.
  • pulse explain --response FILE --request REQ reads a result back in plain words: save the pulse api process --json output as FILE, and pass the request that produced it as REQ so the findings name its fields.