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 over its values as the personas state them, so a missing-data label is not a category.

Usage

panel_from_data(
  data,
  n,
  persona_template = NULL,
  columns = NULL,
  weights = NULL,
  na_strings = 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. A placeholder whose value is left out as missing is filled with nothing, and a placeholder may not name an identifier or the weights column.

columns

Attribute columns to keep. Defaults to every column except the weights column when one is given. Identifier columns are kept in the panel but never rendered into the persona text.

weights

Optional name of a single column of nonnegative sampling weights (rows are drawn with probability proportional to it). The column is never rendered into the persona text.

na_strings

Regular expressions, matched ignoring case against each value and value label, that mark it as missing. NULL (the default) uses the patterns of FocusGroup's fg_default_na_strings(), which match the missing-data labels of the ANES and similar releases: "Inapplicable", "Refused", "Don't know", "Not asked", "DK/RF", "No post-election interview", "Insufficient partial, interview deleted", "RESTRICTED", "Error", "Technical error", "Not reported", and the like. character(0) treats no label as missing. Answers that use such words ("2. Refused to take sides", "I keep missing the bus") are kept.

Value

A silicon_panel: a tibble with persona_id, the selected attribute columns, and persona. Its source_rows attribute gives, for each persona in persona_id order, the row of data it was drawn from.

Details

Without a template, a plain data frame renders as "attribute: value" pairs, and a persona_frame (see as_persona_frame()) renders as background and stated answers, as panel_from_personas() does. Values are written as text by the rules under "Rendering of values" in panel_from_personas().

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}.")