Skip to contents

Parse list-experiment data into a nested tibble where each row represents one experiment detected via literal column names or name templates. Every entry in the data list-column is a tidy tibble with columns id, treatment, private, public, majority (factor levels "agree", "disagree", "unsure", may contain NAs) and a packed predictors column. The attribute n_insens stores the inferred number of non-sensitive statements for each bundle. When there is only one experiment, the tibble still has a single row with experiment = NA_character_.

For multiple experiments, pass templates with a single "exp" placeholder (e.g. "m1_private_{exp}", "m1_public_{exp}"). The function returns a tibble with columns experiment and data where each row contains the parsed single-bundle tibble in data.

Parsed lx_data objects automatically receive experiment metadata (when an experiment code is available). If these extracted codes are not informative enough for your workflow, you can update metadata afterwards with lx_set_metadata() (for example, to overwrite experiment and/or add a human-readable statement).

Usage

lx_parse_data(
  data,
  id_col,
  treatment,
  private,
  public,
  n_insens,
  majority = NULL,
  predictors,
  weight_col = NULL,
  treatment_coding = NULL,
  public_coding = NULL,
  majority_coding = NULL
)

Arguments

data

A data.frame in one-row-per-respondent form.

id_col

Quoted or unquoted id column.

treatment

Either exact column name (unquoted) or a character template. Typically a single column for all bundles.

private, public, majority

Either exact column names (unquoted) for a single experiment, or character templates with a single placeholder for multiple experiments. majority is optional (can be missing entirely or missing for some bundles).

n_insens

Number of insensitive statements. It will be checked that control group answers to the private question are <= n_insens and, for, the treatment group, <= n_insens + 1.

predictors

Optional (?) tidyselect for predictors to be packed into a predictors column of each single-bundle tibble.

weight_col

Optional weights column (quoted or unquoted), stored as weight in each parsed lx_data. If omitted, equal weights of 1 are used. Weights do not affect model fitting, but setting them at parse time is useful for quick summaries on cohorts generated from model predictors.

treatment_coding

Optional mapping for treatment values. A length-2 named character vector c(control = "...", treatment = "..."). For example, c(control = "ctrl", treatment = "trt"). If omitted, the parser falls back to built-in regex heuristics to detect control/treatment values (matching common strings like "control"/"ctrl" and "treatment"/"trt"/"treat").

public_coding, majority_coding

Optional named character vectors mapping response values to standardized levels. Each must have agree, disagree, unsure elements (vectors of source values). If majority_coding is NULL, public_coding is reused. If both NULL, regex heuristics look for common tokens such as "agree"/"support", "disagree"/"oppose", and "don't know"/"unsure"/"dk"/"refuse".

Value

A tibble with columns experiment and data. Each element of data is a tibble (class lx_data) corresponding to a single list-experiment bundle.

Examples


parsed_lx_data <- lx_parse_data(
  example_srv_lx,
  n_insens = 4,
  id_col = response_id,
  weight_col = weight,
  treatment = m2_list_treatment,
  private = "m2_list_{exp}_private",
  public = "m3_list_{exp}_public",
  majority = "m3_list_{exp}_majority",
  predictors = starts_with("d_"),
  treatment_coding = c(control = "ctrl", treatment = "trt"),
  public_coding = c(agree = "support", disagree = "oppose", unsure = "mixed")
)
#> 
#> ── Experiment pets 
#> • treatment column: m2_list_treatment
#> ├─ treatment <- "trt"
#> └─ control   <- "ctrl"
#> • private column: m2_list_pets_private
#> • public column: m3_list_pets_public
#> ├─ disagree <- "oppose"
#> ├─ unsure   <- "mixed"
#> └─ agree    <- "support"
#> • majority column: m3_list_pets_majority
#> ├─ disagree <- "oppose"
#> ├─ unsure   <- "mixed"
#> └─ agree    <- "support"
#> 
#> ── Experiment privacy 
#> • treatment column: m2_list_treatment
#> ├─ treatment <- "trt"
#> └─ control   <- "ctrl"
#> • private column: m2_list_privacy_private
#> • public column: m3_list_privacy_public
#> ├─ unsure   <- "mixed"
#> ├─ disagree <- "oppose"
#> └─ agree    <- "support"
#> • majority column: m3_list_privacy_majority
#> ├─ unsure   <- "mixed"
#> ├─ disagree <- "oppose"
#> └─ agree    <- "support"
#> 
#> ── Experiment climate 
#> • treatment column: m2_list_treatment
#> ├─ treatment <- "trt"
#> └─ control   <- "ctrl"
#> • private column: m2_list_climate_private
#> • public column: m3_list_climate_public
#> ├─ disagree <- "oppose"
#> ├─ unsure   <- "mixed"
#> └─ agree    <- "support"
#> • majority column: m3_list_climate_majority
#> ├─ agree    <- "support"
#> ├─ disagree <- "oppose"
#> └─ unsure   <- "mixed"