Skip to contents

Updates client zone targets for the day pattern model using predicted probabilities, scaling to match total persons and adjusting by household weights. Use to harmonize day pattern targets for weighting and reporting.

Usage

update_daypat_targets(
  probs_dt,
  seed,
  targets,
  households,
  hh_weights,
  settings,
  integerize = TRUE
)

Arguments

probs_dt

data.table. Predicted probabilities from day pattern model. Required columns:

  • hh_id : household ID

  • person_id : person ID

  • day_group : day-of-week group

  • prob_made_* : predicted probabilities for each day pattern

  • is_adult <logical/numeric>: adult flag Rows: one per person-day. Modified by reference: no (returns copy).

seed

data.table. Seed data for day pattern model. Required columns:

  • hh_id, day_group, hh_id_dow, client_zone_id, p_total, h_total, etc. Rows: one per household-day. Modified by reference: no (returns copy).

targets

data.table. Input targets by client zone. Required columns:

  • client_zone_id, p_total, h_total, etc. Rows: one per client zone. Modified by reference: no (returns copy).

households

data.table. Household data. Required columns:

  • hh_id, num_kids, num_people, etc. Rows: one per household. Modified by reference: no (returns copy).

hh_weights

data.table. Household weights. Required columns:

  • hh_id, day_group, hh_weight Rows: one per household-day. Modified by reference: no (returns copy).

settings

list. Project settings; must include relevant pipeline settings.

integerize

logical(1). If TRUE, integerizes output values (TODO: not currently implemented).

Value

data.table. Updated client zone targets. Columns:

  • client_zone_id : client zone ID

  • Household and person target columns (e.g., h_total, p_total, p_made_*)

  • All columns from input targets and predicted probabilities

  • Row order: by client zone

Details

  • Asserts that probs_dt contains only adults (no children).

  • Aggregates predicted probabilities by person and household, using regex pattern prob_made_ for columns.

  • Checks that row sums of probabilities equal 1 for each person.

  • Sums probabilities by household and adds child column from households.

  • Renames columns for consistency (prob_made_p_made_).

  • Casts to wide format by day_group.

  • Merges with seed data by household and day group.

  • Checks that merged data matches seed row count and IDs.

  • Scales probabilities by ratio of persons to participants in each household to match p_total.

  • Uses regex pattern ^p_made_ to select columns for scaling and checks.

  • Validates group sums with check_group_sum().

  • Merges household weights and calculates weighted sums by client zone.

  • Merges predicted targets with input targets by client zone.

  • Checks that regional sum matches p_total (not guaranteed at zone level).

  • Scales client zone targets to match p_total exactly.

  • Validates group sums after adjustment.

  • Sets column order for output table.

  • Returns updated targets as a data.table.

  • Error handling: stops if input tables are inconsistent or sums do not match.

  • TODO: Consider integerization logic for final output (currently controlled by integerize argument, but not implemented in this function).

Settings

  • Uses relevant pipeline settings from settings for scaling and validation.

Examples

## Not run:
update_daypat_targets(probs_dt, seed, targets, households, hh_weights, settings)
#> Error: object 'probs_dt' not found
## End(Not run)