Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple synthetic population generator #18

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,6 @@ docs/site/

# lock file should be personalized
Cargo.lock

# Emacs
*~
107 changes: 107 additions & 0 deletions scripts/create_synthetic_population.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
## =================================#
## Setup ---------------
## =================================#
library(tidyverse)
library(tigris)
library(tidycensus)
library(patchwork)

set.seed(1234)

state_synth <- "WY"
year_synth <- 2023
population_size <- 40

## =================================#
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have a fixed seed here to have a consistent synthetic population

## Get population ---------------
## =================================#
pums_vars <- pums_variables |>
filter(year == 2018, survey == "acs1") |>
distinct(var_code, var_label, data_type, level)

person_variables <- c(
"SPORDER", "SERIALNO", "PWGTP",
"AGEP", "SEX", "PUMA", "REGION"
)
house_variables <- c("WGTP", "NP")

sample_pums <- get_pums(
variables = c(person_variables, house_variables),
state = state_synth,
survey = "acs1",
year = year_synth
)

household_pums <- sample_pums |>
dplyr::select(SERIALNO, all_of(house_variables)) |>
distinct()

## =================================#
## Create population ---------------
## =================================#
synth_pop_df <- tibble()
house_counter <- 0
while (nrow(synth_pop_df) < population_size) {
house_counter <- house_counter + 1
house_sample <- household_pums |>
sample_n(1, weight = WGTP) |>
left_join(sample_pums, by = (c("SERIALNO", "WGTP", "NP"))) |>
mutate(house_number = house_counter)
synth_pop_df <- bind_rows(synth_pop_df, house_sample)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter that this can produce a data frame that doesn't necessarily have exactly the population_size but only at least that size? I don't think so since we can calculate the population later when using the synthetic data, but wanted to draw attention to the potential variability. I could really only see this mattering for population sizes that are small enough to be within the distribution of residents per household

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get fancier, maybe we can fix that. But for how simple this is, I don't think we should care too much about that.


## =================================#
## Recode and math GEO -----------
## =================================#
## For now, we will use PUMA codes
## instead of census tracts
pumas_st <- pumas(state = state_synth)
tracts_st <- tracts(state = state_synth)

synth_pop_region_df <- synth_pop_df |>
left_join(
pumas_st |>
dplyr::select(STATEFP20, PUMACE20, INTPTLAT20, INTPTLON20),
by = c("PUMA" = "PUMACE20")
) |>
dplyr::select(-geometry) |>
mutate(
censusTractId = sprintf("%02d%09d", as.numeric(STATE), as.numeric(PUMA)),
homeId = sprintf(
"%02d%09d%06d",
as.numeric(STATE), as.numeric(PUMA), house_number
)
)


## split pop in persons and regions
## People columns: age, homeId
people_df <- synth_pop_region_df |>
dplyr::select(AGEP, homeId) |>
dplyr::rename(age = AGEP)


## Region columns: region_id, lat, lon
region_df <- synth_pop_region_df |>
dplyr::mutate(lat = as.numeric(INTPTLAT20), lon = as.numeric(INTPTLON20)) |>
dplyr::select(censusTractId, lat, lon)

write_csv(
region_df,
file.path("input", sprintf("synth_pop_region_%s.csv", state_synth))
)
write_csv(
people_df,
file.path("input", sprintf("synth_pop_people_%s.csv", state_synth))
)
## =================================#
## Quick plot -----------
## =================================#
g1 <- ggplot(region_df) +
aes(x = lon, y = lat) +
geom_point()

g2 <- ggplot(pumas_st) +
geom_sf() +
theme_void()
g1 + g2
Loading