Skip to contents

These functions build a finite population on a map, step by step, with every value known, and the frames a planner would draw a sample from. The population holds a territory with regions and districts, households and persons placed in villages and towns, and outcomes with chosen variance components. A frame cuts the households of a population into enumeration areas (EAs) delineated by the package's own engine, and holds the EAs as a planner would receive them. One population can carry any number of frames, so frames are compared on the same persons and outcomes. The result feeds svyplan for planning and samplyr for drawing, and population_truth() and frame_truth() give the values the estimates are compared with.

  • sim_territory() starts a population from a territory.

  • sim_households() places households and persons.

  • sim_outcome() adds an outcome to every person.

  • sim_frame() delineates EAs from the households, as a frame.

  • sim_frame_defects() makes the frame stale and incomplete.

  • sim_population() runs all steps with their defaults.

Each step takes the object returned by the one before, so they chain with the native pipe. populate_frame() starts instead from an existing EA layer with its counts, and as_population() from households made elsewhere.

Usage

sim_territory(
  boundary = NULL,
  district = NULL,
  region = NULL,
  n_regions = 2,
  n_districts = 8,
  width = 20000,
  height = 20000,
  crs = 32630,
  seed = NULL
)

sim_households(
  population,
  n = 20000,
  urban_share = 0.3,
  n_towns = 2,
  town_spread = 800,
  village_size = 60,
  village_spread = 150,
  settlement_cv = sqrt(0.5),
  household_size = 5,
  raster = NULL,
  urban_density = 300,
  seed = NULL
)

sim_frame(
  population,
  target = 150,
  max = 250,
  cell = 250,
  stratum = NULL,
  seed = NULL
)

sim_outcome(
  x,
  name,
  components = c(spatial = 0.05, household = 0.15, person = 0.8),
  mean = 100,
  sd = 20,
  prevalence = 0.3,
  range = 2000,
  family = c("gaussian", "binary"),
  effects = NULL,
  frame = NULL,
  seed = NULL
)

sim_frame_defects(
  frame,
  years = 5,
  growth = 0.03,
  growth_sd = 0.02,
  undercoverage = 0.02,
  seed = NULL
)

sim_population(outcomes = list(y = list()), defects = TRUE, seed = NULL, ...)

Arguments

boundary

An sf object of polygons in a projected CRS, the units of a real territory, or NULL (default) for a synthetic rectangle.

district, region

For a boundary, the columns (or vectors) giving each unit's district and region. NULL makes each unit a district of one region.

n_regions, n_districts

For a synthetic territory, the counts of regions and of districts. Districts are shared among regions as evenly as their count allows.

width, height

For a synthetic territory, its size in meters.

crs

For a synthetic territory, its projected CRS.

seed

An integer seed, so a step can be repeated, or NULL (default) to use the current random number stream. The caller's stream is left as it was.

population

A decoupage_population from the step before. sim_frame() needs its households.

n

The count of households.

urban_share

For a synthetic population, the share of households in towns.

n_towns

The count of towns.

town_spread, village_spread

The standard deviation, in meters, of the distance of a household from the center of its town or village. Towns are larger and so denser at their core.

village_size

The mean count of households per village. Village sizes vary around it, as settlement_cv says.

settlement_cv

For sim_households() and the villages of populate_frame(), the coefficient of variation of the sizes of towns and villages. The default sqrt(0.5), about 0.71, lets one town be several times another. 0 gives every town, and every village, the same expected size, and larger values give a few large villages and many small ones.

household_size

The mean count of persons per household. Sizes are one plus a Poisson count.

raster

A terra::SpatRaster (or file path) of population or household counts used as the intensity instead of towns and villages, or NULL (default). Households fall in each cell with probability proportional to its value times the share of it inside the territory, at a uniform position in that share. A cell half inside the territory gets half the households of a whole cell of the same value.

urban_density

With raster, the density of households per square kilometer from which a household counts as urban, read on a 1 km grid. The default 300 matches 1,500 persons per square kilometer at five persons per household, the density of an urban center in the Degree of Urbanisation.

target, max

For sim_frame(), the household count EAs should be near and must not exceed, passed to group_units().

cell

For sim_frame(), the side in meters of the grid cells EAs are built from. Cells are cut by district lines, so every EA nests in one district, and a cell holding more than max households is cut on them by split_units() first.

stratum

For sim_frame(), populate_frame() and as_frame(), the stratum of each EA, as in strata of survey::svydesign(). One or more column names, crossed into one label pasted with a space, or a vector with one value per EA. A name is a column of the EA layer given, or else one of the columns the frame computes, such as district, region and urban, and a logical column is pasted as TRUE or FALSE. NULL (default) gives the region and "urban" or "rural". An EA with no stratum is an error.

x

For sim_outcome(), a decoupage_population with households, or a decoupage_frame, whose population gets the outcome.

name

The name of the outcome, a column added to persons.

components

The shares of the variance of the latent outcome that come from each level, a named numeric vector with names among region, district, spatial, ea, household and person, nonnegative and summing to 1. Levels left out get 0. All levels but ea belong to the population. An ea share needs the EAs of a frame, given as frame.

mean, sd

For family = "gaussian", the mean and standard deviation of the outcome, in the reference group of effects, as finite numbers. An sd of 0 gives every person the mean.

prevalence

For family = "binary", the share of persons with the outcome, in the reference group of effects.

range

For the spatial component, the distance in meters at which the correlation of the field falls to exp(-1). The field is drawn on a grid of cells of range / 10 m, and each household takes the value of its cell. The grid spans twice the extent of the households in each direction, so its memory grows with the extent over the range, at about 56 bytes per cell. A territory of 900 by 300 km at a range of 1,500 m needs 49 million cells, about 2.6 GiB. Above getOption("decoupage.max_field_cells", 2.5e7) cells, the step stops before it allocates the grid.

family

"gaussian" (default) or "binary". See Details.

effects

A named numeric vector of fixed effects on the latent outcome, in latent standard deviations per unit of a numeric or logical column of the households, such as c(urban = 0.4), or NULL (default) for none. See Details.

frame

For sim_frame_defects(), a decoupage_frame. For sim_outcome() with an ea component, the decoupage_frame whose EAs carry it, built on the same households. With x a frame, it defaults to x.

years, growth, growth_sd

For sim_frame_defects(), the age of the frame in years, and the mean and standard deviation of the annual growth rate of EAs. Each EA grows at its own rate, drawn around growth, so the frame's mos is stale unevenly. growth is one mean for all EAs, or a vector named by the EA ids of the frame with one mean per EA. A rate drawn below -0.5 is taken as -0.5.

undercoverage

For sim_frame_defects(), the share of EAs missing from the frame, removed at random, or a vector named by the EA ids of the frame with the probability that each EA is missing. With a vector, EAs go missing independently of one another, so the count missing varies around the sum of the probabilities.

outcomes

For sim_population(), a named list with one list of arguments to sim_outcome() per outcome. They are drawn on the frame, so an ea component is carried by its EAs.

defects

For sim_population(), whether the frame has defects, TRUE (default) or FALSE. With FALSE, the frame is the EAs as delineated.

...

For sim_population(), arguments passed to the steps by name. A name no step takes is an error, as is a setting of sim_frame_defects() with defects = FALSE.

Value

sim_territory(), sim_households(), and sim_outcome() on a population, return a decoupage_population, a list holding:

  • territory, an sf object of polygons with district and region.

  • households, an sf object of points with household, district, region, urban, settlement (the town or village of the household, NA with a raster) and size (persons).

  • persons, a data frame with person, household and one column per outcome.

  • steps, the effective arguments of each step with the package version, and the CRS, extent, features and columns of a supplied boundary or raster, for the record.

sim_frame(), sim_frame_defects(), sim_population(), and sim_outcome() on a frame, return a decoupage_frame, a list holding:

  • population, the population the frame was built on.

  • eas, an sf object of EAs with ea, district, region, urban (a majority of urban households), stratum, households and persons (the realized counts), mos (the measure of size the frame records, see Details) and area, then the other columns of an EA layer given to populate_frame() or as_frame().

  • assignment, the EA of each household, in the order of population$households.

  • frame, the EAs as a planner would receive them, with mos and without the realized households and persons.

  • steps, the arguments of sim_frame() and of sim_frame_defects(), for the record.

Details

A synthetic territory is a rectangle cut into regions by the Voronoi cells of random points, and each region into districts the same way, so every district is convex and nests in one region.

Without a raster, sim_households() draws two point patterns of clusters around random centers, like the Thomas process of spatstat.random::rThomas(), whose scale is the spread here. Unlike it, the total count is fixed and cluster sizes follow gamma weights. The n_towns towns take urban_share of the households, which are urban, and the villages, about n_rural / village_size of them, take the others. Each town or village draws its share of households from a gamma weight whose coefficient of variation is settlement_cv, and its households fall around its center with a normal spread of town_spread or village_spread meters. A town or a village is a cluster of points, nothing more, and settlement gives the cluster of each household, towns first.

A frame records a measure of size, mos, apart from the realized counts. It is the realized household count in sim_frame(), the input counts in populate_frame(), and the given measure or the realized counts in as_frame(). sim_frame_defects() ages it, rounded when it holds whole numbers, and frame_truth() draws EAs in proportion to it.

A frame holds a copy of its population. An outcome added to the population after a frame was built is not in that copy, so outcomes come first, or the newer population goes to frame_truth() as population. An ea component is drawn on the EAs of the frame given as frame and recorded with the outcome. Read on another frame, it is variation that frame did not create.

sim_outcome() draws a latent outcome with mean 0 and variance 1, whose variance is shared among levels as components says. An effect adds its value times the household's column to it, so an effect of 0.4 on urban puts urban persons 0.4 latent standard deviations above rural ones. A gaussian outcome is mean + sd times the latent outcome, and a binary one is 1 where the latent outcome exceeds the quantile of 1 - prevalence of the standard normal, a probit model. mean, sd and prevalence thus hold in the reference group, where every column named in effects is 0 or FALSE, and population_truth() gives the realized values over the whole population. A column added to households before sim_outcome(), such as a covariate or an eligibility flag, can carry an effect.

With seed = NULL, a step draws from the current random number stream, so replicates run in parallel can each take their own stream, such as one of parallel::nextRNGStream(). sim_population() gives each step its own seed from seed.

See also

population_truth() and frame_truth() for the truth of an outcome, and the article "The same population, different frames" (https://dickoa.gitlab.io/decoupage/articles/frames-compared.html) for a study of three frames on one population.

Other simulation functions: populate_frame(), population_truth()

Examples

pop <- sim_territory(n_regions = 2, n_districts = 4, width = 8000,
  height = 6000, seed = 1) |>
  sim_households(n = 1500, seed = 2) |>
  sim_outcome("y", components = c(spatial = 0.3, household = 0.2,
    person = 0.5), range = 1500, seed = 3)
pop
#> <decoupage_population>
#>   territory: 4 districts in 2 regions
#>   1500 households, 7437 persons, 30% urban
#>   2 towns of 409 and 41 households
#>   18 villages of 15 to 233 households (median 41.5)
#>   outcomes: y
small <- sim_frame(pop, target = 60, max = 100, seed = 4)
large <- sim_frame(pop, target = 200, max = 300, seed = 4)
small
#> <decoupage_frame>
#>   25 EAs, 44 to 95 households
#>   frame: 25 EAs, as delineated
#>   MOS 1500 for 1500 households in its EAs
#>   on the population
#>     territory: 4 districts in 2 regions
#>     1500 households, 7437 persons, 30% urban
#>     2 towns of 409 and 41 households
#>     18 villages of 15 to 233 households (median 41.5)
#>     outcomes: y
population_truth(pop, "y", by = "urban")
#>   domain households persons    total     mean variance unit_relvar
#> 1  FALSE       1050    5229 505372.7 96.64807 378.5221  0.04052331
#> 2   TRUE        450    2208 215308.6 97.51297 388.7055  0.04087859
#>   icc_household
#> 1     0.4430742
#> 2     0.4760913
rbind(frame_truth(small, "y"), frame_truth(large, "y"))
#>   domain eas eas_frame households persons households_missed persons_missed
#> 1    all  25        25       1500    7437                 0              0
#> 2    all   7         7       1500    7437                 0              0
#>      total     mean total_covered mean_covered unit_relvar       icc var_ratio
#> 1 720681.4 96.90485      720681.4     96.90485   0.0406419 0.2867171  1.180749
#> 2 720681.4 96.90485      720681.4     96.90485   0.0406419 0.1154052  1.039018
#>     icc_ssu var_ratio_ssu     icc_ea
#> 1 0.8800441       5.29408 0.16585929
#> 2 0.8836449       5.45791 0.09370233