Skip to contents

Survey R6 class

Survey R6 class

Format

An R6 class generator (R6ClassGenerator)

Value

R6 class generator for Survey.

Details

R6 class that encapsulates survey data, metadata (type, edition, periodicity), sampling design (simple/replicate), steps/recipes/workflows, and utilities to manage them.

Only copies the data; reuses design objects and other metadata. Much faster than clone(deep=TRUE) but design objects are shared.

Main methods

$new(data, edition, type, psu, strata, engine, weight, design = NULL, steps = NULL, recipes = list())

Constructor.

$set_data(data)

Set data.

$set_edition(edition)

Set edition.

$set_type(type)

Set type.

$set_weight(weight)

Set weight specification.

$print()

Print summarized metadata.

$add_step(step)

Add a step and invalidate design.

$add_recipe(recipe)

Add a recipe (validates type and dependencies).

$add_workflow(workflow)

Add a workflow.

$bake()

Apply recipes and return updated Survey.

$ensure_design()

Lazily initialize the sampling design.

$update_design()

Update design variables with current data.

$shallow_clone()

Efficient copy (shares design, copies data).

Public fields

data

Survey data (data.frame/data.table).

edition

Reference edition or period.

type

Survey type, e.g., "ech" (character).

periodicity

Periodicity detected by validate_time_pattern.

default_engine

Default engine (character).

weight

List with weight specifications per estimation type.

steps

List of steps applied to the survey.

recipes

List of Recipe objects associated.

workflows

List of workflows.

design

List of survey design objects (survey/surveyrep).

psu

Primary Sampling Unit specification (formula or character).

strata

Stratification variable name (character or NULL).

design_initialized

Logical flag for lazy design initialization.

provenance

Data lineage metadata (see provenance()).

Active bindings

design_active

Deprecated. Use ensure_design() instead.

Methods


Method new()

Create a Survey object

Usage

Survey$new(
  data,
  edition,
  type,
  psu = NULL,
  strata = NULL,
  engine,
  weight,
  design = NULL,
  steps = NULL,
  recipes = list()
)

Arguments

data

Survey data

edition

Edition or period

type

Survey type (character)

psu

PSU variable or formula (optional)

strata

Stratification variable name (optional)

engine

Default engine

weight

Weight specification(s) per estimation type

design

Pre-built design (optional)

steps

Initial steps list (optional)

recipes

List of Recipe (optional)


Method get_data()

Return the underlying data

Usage

Survey$get_data()


Method get_edition()

Return the survey edition/period

Usage

Survey$get_edition()


Method get_type()

Return the survey type identifier

Usage

Survey$get_type()


Method set_data()

Set the underlying data

Usage

Survey$set_data(data)

Arguments

data

New survey data


Method set_edition()

Set the survey edition/period

Usage

Survey$set_edition(edition)

Arguments

edition

New edition or period


Method set_type()

Set the survey type

Usage

Survey$set_type(type)

Arguments

type

New type identifier


Method set_weight()

Set weight specification(s) per estimation type

Usage

Survey$set_weight(weight)

Arguments

weight

Weight specification list or character


Method print()

Print summarized metadata to console

Usage

Survey$print()


Method add_step()

Add a step and invalidate design

Usage

Survey$add_step(step)

Arguments

step

Step object


Method add_recipe()

Add a recipe

Usage

Survey$add_recipe(recipe, bake = lazy_default())

Arguments

recipe

Recipe object

bake

Whether to bake lazily (internal flag)


Method add_workflow()

Add a workflow to the survey

Usage

Survey$add_workflow(workflow)

Arguments

workflow

Workflow object


Method bake()

Apply recipes and return updated Survey

Usage

Survey$bake()


Method head()

Return the head of the underlying data

Usage

Survey$head()


Method str()

Display the structure of the underlying data

Usage

Survey$str()


Method set_design()

Set the survey design object

Usage

Survey$set_design(design)

Arguments

design

Survey design object or list


Method ensure_design()

Ensure survey design is initialized (lazy initialization)

Usage

Survey$ensure_design()

Returns

Invisibly returns self


Method update_design()

Update design variables using current data and weight

Usage

Survey$update_design()


Method shallow_clone()

Create a shallow copy of the Survey (optimized for performance)

Usage

Survey$shallow_clone()

Returns

New Survey object with copied data but shared design


Method clone()

The objects of this class are cloneable with this method.

Usage

Survey$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

dt <- data.table::data.table(id = 1:5, x = rnorm(5), w = rep(1, 5))
svy <- Survey$new(
  data = dt, edition = "2023", type = "test",
  psu = NULL, engine = "data.table", weight = add_weight(annual = "w")
)
svy
#> Type: TEST
#> Edition: 2023
#> Periodicity: Annual
#> Engine: data.table
#> Design: 
#>   Design: Not initialized (lazy initialization - will be created when needed)
#> 
#> Steps: None
#> Recipes: None