View source: R/ei_summary_random.R
| ei_summary_random | R Documentation |
Randomly assigns voters to artificial units and computes ecological inference summaries, ignoring the geographic boundaries of real precincts. This allows assessing how sensitive the results are to the specific way voters are grouped — a concern known in the literature as the Modifiable Areal Unit Problem (MAUP).
ei_summary_random(
elections,
data = NULL,
source = NULL,
format = c("lphom", "eipack"),
n_units = NULL,
unit_size = NULL,
size_range = NULL,
seed = NULL
)
elections |
Character vector of race codes (e.g. |
data |
Optional |
source |
Character string or |
format |
Character. Output format for the
|
n_units |
Integer or |
unit_size |
Integer or |
size_range |
Integer vector of length 2, |
seed |
Integer or |
The total number of votes for each option is identical to that
returned by ei_summary() on the same data — only the distribution
of votes across units changes.
The function returns a standard ei_summary object, fully compatible
with to_lphom(), to_eipack(), and subset_elections(). The
randomisation parameters are stored in meta$random for
reproducibility and auditing.
To repeat the randomisation n times (e.g. for a sensitivity
analysis), call the function inside lapply(), varying seed each
time:
results <- lapply(1:100, function(i)
ei_summary_random(c("PRE", "USS"), data = d, seed = i))
An object of class "ei_summary" with the same structure as
ei_summary(), which contains the components: margins, joint_precinct,
joint_total and meta; see ei_summary() for details.
To document the randomisation the meta component includes,
in addition to the fields elections, counties, n_precincts,
n_voters, opt_levels, format, precinct_info, created_at
the field random, with the following components:
methodCharacter string describing the aggregation method used and the resulting unit structure.
n_unitsInteger. Number of random units created.
unit_sizesInteger vector of length n_units. Size
(number of voters) of each unit. Always sums to
meta$n_voters.
seedThe seed supplied to seed, or NULL if none.
n_units_argValue of the n_units argument as supplied
by the user.
unit_size_argValue of the unit_size argument as
supplied by the user.
size_range_argValue of the size_range argument as
supplied by the user.
When multiple sizing arguments are supplied simultaneously, they are resolved in this order (highest to lowest priority):
unit_size — fixed size per unit; n_units and
size_range are ignored with a warning.
size_range — random sizes within bounds; if n_units
is also supplied, the effective sampling interval is the largest
symmetric interval around the required mean (N / n_units) that
fits within [min, max]. The last unit absorbs the residual and
may fall slightly outside the effective interval. A warning is
issued if the required mean falls outside [min, max], in which
case all units are assigned approximately equal sizes.
n_units — number of units; sizes are drawn
proportionally from the real precinct size distribution.
Default (no sizing argument) — same number of units as real precincts; sizes match the real precinct size distribution; voters are randomly shuffled across units.
As in ei_summary(), only voters non-NA in all elections
simultaneously are included (the intersection universe). The
randomisation is applied to this universe, not to the full dataset.
The meta$n_voters field of the returned object always refers to
this intersection count.
ei_summary_random() produces an object of the same class and
structure as ei_summary(), and is fully compatible with
to_lphom(), to_eipack(), and subset_elections(). The only
difference is that voters are grouped into random units rather than
real (geographic) precincts. To compare random and real aggregations
directly, run both functions on the same data object:
d <- get_election_data(c("PRE", "STS35"))
real <- ei_summary(c("PRE", "STS35"), data = d)
rnd <- ei_summary_random(c("PRE", "STS35"), data = d, seed = 1)
ei_summary(), subset_elections(), to_lphom(),
to_eipack(), get_election_data()
# Using the built-in example dataset
obj <- ei_summary(c("PRE", "USS"), data = example_ballots)
print(obj)
summary(obj)
# Default: same number and size distribution as real precincts
obj <- ei_summary_random(c("PRE", "STS35"), seed = 42)
# 200 units with sizes proportional to the real precinct distribution
obj <- ei_summary_random(c("PRE", "STS35"), n_units = 200, seed = 42)
# Fixed size of 500 voters per unit (residual unit if N not divisible)
obj <- ei_summary_random(c("PRE", "STS35"), unit_size = 500, seed = 42)
# Random size between 200 and 800 voters per unit
obj <- ei_summary_random(c("PRE", "STS35"),
size_range = c(200, 800), seed = 42)
# 200 units with random sizes between 100 and 600
obj <- ei_summary_random(c("PRE", "STS35"),
n_units = 200, size_range = c(100, 600),
seed = 42)
# Sensitivity analysis: 100 random aggregations
results <- lapply(1:100, function(i)
ei_summary_random(c("PRE", "USS"), seed = i))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.