Skip to contents

Samples rows from a data frame with replacement, which preserves the joint distribution of the selected attributes, and renders each sampled row as a persona. This is the joint-distribution counterpart of panel_from_margins(), which samples attributes independently. The margins the report cites are computed from the source data, one prop.table(table()) per selected column.

Usage

panel_from_data(
  data,
  n,
  persona_template = NULL,
  columns = NULL,
  weights = NULL
)

Arguments

data

A data frame, one row per source case.

n

Panel size.

persona_template

Text with {attribute} placeholders rendered per persona. NULL builds a plain "attribute: value" persona.

columns

Attribute columns to keep. Defaults to every column except the weights column when one is given.

weights

Optional name of a single column of nonnegative sampling weights (rows are drawn with probability proportional to it).

Value

A silicon_panel: a tibble with persona_id, the selected attribute columns, and persona.

Details

For a reproducible panel, set a seed before calling (the function never sets one itself).

Examples

set.seed(110)
src <- data.frame(
  education = c("college", "college", "no college", "no college"),
  income    = c("high", "high", "low", "low"),
  weight    = c(2, 2, 1, 1))
panel_from_data(src, n = 10, columns = c("education", "income"),
                weights = "weight",
                persona_template = "A {education} respondent earning {income}.")