populate_frame() takes an EA layer with a household count per EA and
creates a population whose households fall in their EAs. A real frame,
such as the EAs of a census cartography, then carries a population with
a known truth. It returns a decoupage_frame on that population, with
the EAs as given.
as_population() wraps households made elsewhere, from a census or
another generator, and their persons when given, into a population on a
territory from sim_territory().
as_frame() lays an EA layer over a population that already exists and
returns it as a frame of that population, with no new households. A
real frame can then be compared with frames from sim_frame() on the
same persons.
A household can be any listing unit with members, such as an
establishment with its employees or a plot with its trees. The sizes
and members of as_population() come from the user, so they follow
any distribution.
The three functions check their input and stop with an error that names the rows that fail. None of them repairs it.
Usage
populate_frame(
eas,
households = "households",
id = NULL,
district = NULL,
region = NULL,
urban = NULL,
stratum = NULL,
counts = c("exact", "expected"),
n = NULL,
place = c("uniform", "raster", "clustered"),
raster = NULL,
household_size = 5,
village_size = 60,
village_spread = 150,
settlement_cv = sqrt(0.5),
urban_density = 300,
seed = NULL
)
as_population(
households,
territory,
persons = NULL,
id = NULL,
size = NULL,
urban = NULL,
urban_density = 300
)
as_frame(
population,
eas,
id = NULL,
mos = NULL,
district = NULL,
region = NULL,
urban = NULL,
stratum = NULL,
rule = c("within", "nearest"),
max_dist = Inf
)Arguments
- eas
An
sfobject of EA polygons in a projected CRS.- households
For
populate_frame(), the household count of each EA, a column ofeasor a numeric vector,"households"by default. Foras_population(), ansfobject of household points in the CRS of the territory.- id
For
populate_frame()andas_frame(), the EA codes, a column ofeasor a vector. Foras_population(), the household ids, a column ofhouseholdsor a vector. Codes must be unique.NULL(default) gives the row numbers.- district, region
The district and region of each EA, columns of
easor vectors. Forpopulate_frame(), they go tosim_territory(), andNULLmakes each EA a district of one region. Foras_frame(),NULLtakes them from the territory, see Details.- urban
For
populate_frame()andas_frame(), whether each EA is urban, a logical column ofeasor a logical vector. The households ofpopulate_frame()take it. Foras_population(), whether each household is urban, a column ofhouseholdsor a vector.NULL(default) makes a household urban by the density of households around it, assim_households()does with a raster, and foras_frame()an EA urban when most of its households are.- stratum
For
sim_frame(),populate_frame()andas_frame(), the stratum of each EA, as instrataofsurvey::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 asdistrict,regionandurban, and a logical column is pasted asTRUEorFALSE.NULL(default) gives the region and"urban"or"rural". An EA with no stratum is an error.- counts
"exact"(default) gives each EA exactly its count, which must be a whole number."expected"takes the counts as expected values. See Details.- n
With
counts = "expected", the total count of households, shared among EAs in proportion to their counts, orNULL(default) for a Poisson count in each EA.- place
Where households fall inside their EA,
"uniform"(default),"raster"in proportion toraster, or"clustered"in villages.- raster
With
place = "raster", aterra::SpatRaster(or file path) of population or household counts. Households fall in each cell with probability proportional to its value times the share of it inside their EA, so a cell on the line between two EAs shares its weight between them.- household_size
The mean count of persons per household. Sizes are one plus a Poisson count.
- village_size
The mean count of households per village. Village sizes vary around it, as
settlement_cvsays.- village_spread
With
place = "clustered", the standard deviation, in meters, of the distance of a household from the center of its village.- settlement_cv
For
sim_households()and the villages ofpopulate_frame(), the coefficient of variation of the sizes of towns and villages. The defaultsqrt(0.5), about 0.71, lets one town be several times another.0gives every town, and every village, the same expected size, and larger values give a few large villages and many small ones.- urban_density
Without
urban, the density of households per square kilometer from which a household counts as urban, read on a 1 km grid. The default300matches 1,500 persons per square kilometer at five persons per household.- 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.- territory
For
as_population(), adecoupage_populationfromsim_territory(). Households it already holds are replaced.- persons
For
as_population(), a data frame of persons with a columnhouseholdholding the id of each person's household, and optionally a columnpersonof unique ids, orNULL(default) to createsizepersons in each household.- size
For
as_population(), the count of persons of each household, a column ofhouseholdsor a vector of whole numbers of at least 1. Withpersons,NULL(default) counts them, and a givensizemust agree with that count.- population
For
as_frame(), adecoupage_populationwith households.- mos
For
as_frame(), the frame's measure of size, a column ofeasor a vector of finite nonnegative numbers, stored asmos, orNULL(default) for the realized counts. A layer's own counts rarely match the population, andframe_truth()then shows what the mismatch costs.- rule, max_dist
For
as_frame(), passed toassign_points(). With"within"(default), a household outside every EA is an error, and"nearest"gives it the nearest EA withinmax_distmeters.
Value
populate_frame() returns a decoupage_frame, described in
simulate, whose EAs hold the codes of id, the input geometry, the
input counts as mos, the realized counts and the other columns of
eas.
as_population() returns a decoupage_population with the territory
of territory, the households with their columns, and the persons.
as_frame() returns a decoupage_frame on population, whose EAs
hold the codes of id, the input geometry, the realized counts, the
measure of size as mos and the other columns of eas.
Details
The columns of eas follow the columns the frame computes, ea,
district, region, urban, stratum, households, persons,
mos and area, which replace input columns of the same name. A
stratum or a code of the layer is kept that way, and stratum can name
it.
The territory of the population is the EA layer itself, one territory
unit per EA. sim_frame() on that population then delineates new EAs
on the same households, which can be compared with the given ones.
With counts = "expected" and n = NULL, each EA draws a Poisson count
with its count as the mean, and with n the n households are drawn
among EAs by a multinomial draw with probabilities proportional to the
counts. The frame keeps the input counts as its measure of size, mos,
so it differs from the realized counts by chance alone, and
sim_frame_defects() ages it.
With place = "clustered", each EA with households holds villages of
about village_size households, at least one, whose households fall
around their center with a spread of village_spread meters, and
whose sizes vary as settlement_cv says. The settlement of each
household gives its village, numbered across EAs, and is NA for the
other places.
Without urban, a household is urban when its 1 km cell of the CRS
holds at least urban_density households. An EA of populate_frame()
is then urban when most of its households are.
as_population() gives each household the district and region of the
territory unit it falls in, by the rule of assign_points() for a
household on the line between two units. The columns household,
district, region, urban and size are its own, so an input column
of one of these names that id, urban or size does not name is an
error.
Other columns of households are kept, as are the columns of persons
other than person and household, which count as outcomes.
as_frame() gives each household the EA that holds it by
assign_points(), whose rule settles a household on the line between
two EAs, or in two overlapping EAs, the same way whatever the row order,
except in EAs of the same centroid and area.
check_units() finds overlaps and gaps in the layer. Without
district or region, an EA takes those of the territory unit that
holds a point on its surface (sf::st_point_on_surface()), the nearest
one if the point is outside the territory.
See also
simulate for the other steps, and frame_truth() for the
truth of an outcome on the frame.
Other simulation functions:
population_truth(),
simulate
Examples
path <- system.file("extdata", "yako.gpkg", package = "decoupage")
eas <- sf::st_read(path, layer = "eas", quiet = TRUE)
f <- populate_frame(eas, id = "ea", region = "commune",
urban = eas$stratum == "urban", stratum = "stratum", seed = 1)
f
#> <decoupage_frame>
#> 21 EAs, 9 to 99 households
#> frame: 21 EAs, as given
#> MOS 1515 for 1515 households in its EAs
#> on the population
#> territory: 21 districts in 2 regions
#> 1515 households, 7743 persons, 65% urban
f <- sim_outcome(f, "y", components = c(household = 0.2, ea = 0.1,
person = 0.7), seed = 2)
frame_truth(f, "y", by = "urban")
#> domain eas eas_frame households persons households_missed persons_missed
#> 1 FALSE 8 8 535 2769 0 0
#> 2 TRUE 13 13 980 4974 0 0
#> total mean total_covered mean_covered unit_relvar icc var_ratio
#> 1 281142.2 101.5320 281142.2 101.5320 0.04011365 0.2036316 1.109360
#> 2 504391.5 101.4056 504391.5 101.4056 0.03846686 0.1335232 1.021987
#> icc_ssu var_ratio_ssu icc_ea
#> 1 0.8355488 4.905510 0.1351813
#> 2 0.8497711 5.377212 0.1239725
## Households from elsewhere, on the same territory
hh <- f$population$households[, "size"]
pop <- as_population(hh, sim_territory(eas), size = "size")
pop
#> <decoupage_population>
#> territory: 21 districts in 1 regions
#> 1515 households, 7743 persons, 0% urban
## The yako EAs as a frame of a population made on its communes
communes <- sf::st_read(path, layer = "communes", quiet = TRUE)
pop <- sim_territory(communes, district = "commune") |>
sim_households(n = 3000, seed = 3)
real <- as_frame(pop, eas, id = "ea", mos = "households")
real
#> <decoupage_frame>
#> 21 EAs, 0 to 1164 households
#> frame: 21 EAs, as given, sizes from the layer
#> MOS 1515 for 3000 households in its EAs
#> on the population
#> territory: 2 districts in 1 regions
#> 3000 households, 14963 persons, 30% urban
#> 2 towns of 751 and 149 households
#> 35 villages of 6 to 157 households (median 56)
cut <- sim_frame(pop, target = 150, max = 250, seed = 4)
pop <- sim_outcome(pop, "y", components = c(spatial = 0.3,
household = 0.2, person = 0.5), range = 800, seed = 5)
## The layer's counts do not follow households placed at random, and
## drawing EAs by them costs precision, as the components show
rbind(frame_truth(real, "y", population = pop),
frame_truth(cut, "y", population = pop))
#> domain eas eas_frame households persons households_missed persons_missed
#> 1 all 21 21 3000 14963 0 0
#> 2 all 20 20 3000 14963 0 0
#> total mean total_covered mean_covered unit_relvar icc var_ratio
#> 1 1510417 100.9435 1510417 100.9435 0.03592672 0.9583804 112.186259
#> 2 1510417 100.9435 1510417 100.9435 0.03592672 0.1203971 1.040871
#> icc_ssu var_ratio_ssu icc_ea
#> 1 0.8907382 28.136470 0.04550919
#> 2 0.8877222 5.774748 0.08988550