R/template/03_sim.R

Defines functions simulate make_map

# Simulate plans for ```SLUG```
# ``COPYRIGHT``

shp_path = "data/``STATE``/``SLUG``_vtd_20.rds"

# Set up the redistricting problem, including filtering, cores, and population tolerance
make_map = function(shp_path) {
    ``state``_shp = read_rds(here(shp_path))
    ``state``_map = redist_map(``state``_shp, pop_tol=0.01,
                               existing_plan=cd, adj=``state``_shp$adj)

    ``state``_map
}

# Simulate redistricting plans
# Analyze and summarize simulated plans
# Returns a simulation-free summary frame with all the necessary data for visualization
simulate = function(map) {
    plans1 = redist_smc(map, nsims=10e3, counties=county)

    plans = list(
        tol_01 = plans1
    )

    ndists = attr(map, "ndists")
    dvote = map$ndv
    rvote = map$nrv
    statewide = sum(dvote) / (sum(dvote) + sum(rvote))
    ker = function(x) pt((x-0.5)/0.035136, df=22)

    plans = purrr::imap(plans, function(p, name) {
        p = p %>%
            #pullback() %>%
            mutate(dev =  plan_parity(map),
                   comp = distr_compactness(map),
                   county_splits = county_splits(map, county),
                   dem = group_frac(map, ndv, ndv+nrv),
                   black = group_frac(map, pop_black),
                   hisp = group_frac(map, pop_hisp),
                   minority = group_frac(map, pop - pop_white))

        m_dem = ker(district_group(p, dem))

        p %>%
            mutate(represent = rep(as.numeric(dvote %*% m_dem + rvote %*% (1-m_dem)) /
                       sum(dvote + rvote), each=ndists)) %>%
            group_by(draw) %>%
            mutate(proportion = statewide - sum(ker(dem)) / ndists) %>%
            ungroup()
    })

    path = "data/``STATE``/``SLUG``_results.rds"
    write_rds(plans, here(path), compress="xz")

    plans
}
CoryMcCartan/redistricting21 documentation built on July 13, 2022, 8:30 p.m.