Data Generation with the American Community Survey (ACS)
Gradient Metrics
2025-10-16
Source:vignettes/data_generation_acs.Rmd
data_generation_acs.RmdACS dataset
Currently we selected a subset of the whole ACS dataset that contains
variables of our interest. The g6tr.geo package contains
data for county, state and
region, available through this standard interface:
We also included zipcode level data, however due to its large size, this data set is stored on S3. You can download this dataset using:
zip_data <- geo_read_s3(
object = 'ds_zip_acs_raw.csv', fun = readr::read_csv
)On all levels dataset contains 279 variables from 63 concepts. You
can check the full list of included Concepts and
Labels here
or by viewing the included geo_dictionary dataset:
In order to make this data useful, it first needs to be cleaned and
transformed. g6tr.geo package contains the
geo_tidy_acs_data() function, which takes care of renaming
the variables, calculating ratios of variables within particular
concepts to concept’s total and combining variables within concepts into
wider breaks defined in geo_dictionary.
Preparing data with geo_tidy_acs_data()
The function takes 6 arguments: data,
dictionary, geo_type,
adj_other_vals, vars_to_adjust and
vars_denominator. Mandatory are: data,
dictionary and geo_type. The 3 other arguments
are used when in the data set there are single variables, that should be
calculated as a proportion of population variable. In this
case you would need to set adj_other_var value to
TRUE, provide vector of quoted or unquoted (i.e. both
c("pop_u18", "children_u18") and
c(pop_u18, children_u18) will work) variables that you want
to divide by variable passed to vars_denominator. The last
variable needs to be unquoted.
Here is an example on how to prepare region level data, with
pop_u18 and children_u18 variables converted
to ratio of population.
[38;5;246m# A tibble: 5 × 5
[39m
GEOID NAME B01001_001E B01001_001M B01001_002E
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[38;5;250m1
[39m 3 South Region 123
[4m4
[24m
[4m0
[24m
[4m8
[24m246
[31mNA
[39m 60
[4m4
[24m
[4m8
[24m
[4m7
[24m628
[38;5;250m2
[39m 4 West Region 77
[4m1
[24m
[4m4
[24m
[4m8
[24m324
[31mNA
[39m 38
[4m4
[24m
[4m7
[24m
[4m4
[24m974
[38;5;250m3
[39m 1 Northeast Region 56
[4m0
[24m
[4m3
[24m
[4m3
[24m139
[31mNA
[39m 27
[4m3
[24m
[4m1
[24m
[4m3
[24m118
[38;5;250m4
[39m 2 Midwest Region 68
[4m1
[24m
[4m0
[24m
[4m8
[24m086
[31mNA
[39m 33
[4m6
[24m
[4m1
[24m
[4m1
[24m199
[38;5;250m5
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
ds_region_acs_clean <- geo_tidy_acs_data(
data = ds_region_acs_raw,
dictionary = geo_dictionary,
geo_type = "region",
adj_other_vars = TRUE,
vars_to_adj = c(pop_u18, children_u18),
vars_denominator = population
)
ds_region_acs_clean[1:5, 1:7] %>% head()
[38;5;246m# A tibble: 5 × 7
[39m
geoid region agg_earnings agg_hhincome agg_income agg_n_vehicles agg_n_vehicles_female
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[38;5;250m1
[39m 1 Northeast Region 1
[4m7
[24m
[4m3
[24m
[4m5
[24m170
[4m3
[24m
[4m4
[24m
[4m8
[24m300 2
[4m1
[24m
[4m4
[24m
[4m7
[24m939
[4m8
[24m
[4m3
[24m
[4m9
[24m000 2
[4m2
[24m
[4m0
[24m
[4m4
[24m772
[4m7
[24m
[4m9
[24m
[4m2
[24m300 19
[4m1
[24m
[4m9
[24m
[4m2
[24m040 9
[4m1
[24m
[4m4
[24m
[4m2
[24m985
[38;5;250m2
[39m 2 Midwest Region 1
[4m7
[24m
[4m3
[24m
[4m2
[24m937
[4m7
[24m
[4m0
[24m
[4m9
[24m900 2
[4m1
[24m
[4m9
[24m
[4m2
[24m104
[4m5
[24m
[4m2
[24m
[4m6
[24m900 2
[4m2
[24m
[4m4
[24m
[4m2
[24m712
[4m3
[24m
[4m0
[24m
[4m4
[24m700 27
[4m6
[24m
[4m0
[24m
[4m4
[24m295 13
[4m1
[24m
[4m2
[24m
[4m3
[24m360
[38;5;250m3
[39m 3 South Region 2
[4m9
[24m
[4m2
[24m
[4m1
[24m738
[4m6
[24m
[4m4
[24m
[4m6
[24m900 3
[4m7
[24m
[4m2
[24m
[4m8
[24m858
[4m7
[24m
[4m2
[24m
[4m9
[24m100 3
[4m8
[24m
[4m9
[24m
[4m4
[24m645
[4m2
[24m
[4m4
[24m
[4m0
[24m500 47
[4m5
[24m
[4m7
[24m
[4m6
[24m195 22
[4m4
[24m
[4m7
[24m
[4m3
[24m570
[38;5;250m4
[39m 4 West Region 2
[4m0
[24m
[4m9
[24m
[4m7
[24m036
[4m4
[24m
[4m8
[24m
[4m8
[24m000 2
[4m6
[24m
[4m3
[24m
[4m0
[24m889
[4m8
[24m
[4m4
[24m
[4m0
[24m600 2
[4m7
[24m
[4m3
[24m
[4m1
[24m001
[4m3
[24m
[4m5
[24m
[4m7
[24m400 28
[4m3
[24m
[4m7
[24m
[4m6
[24m005 12
[4m9
[24m
[4m0
[24m
[4m9
[24m540
[38;5;250m5
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
[31mNA
[39m
[38;5;246m# A tibble: 4 × 3
[39m
population pop_u18 children_u18
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[38;5;250m1
[39m 56
[4m0
[24m
[4m3
[24m
[4m3
[24m139 0.209 0.185
[38;5;250m2
[39m 68
[4m1
[24m
[4m0
[24m
[4m8
[24m086 0.228 0.205
[38;5;250m3
[39m 123
[4m4
[24m
[4m0
[24m
[4m8
[24m246 0.230 0.199
[38;5;250m4
[39m 77
[4m1
[24m
[4m4
[24m
[4m8
[24m324 0.231 0.200
Below is an example of preparing county level data, without adjusting any other variables.
[38;5;246m# A tibble: 5 × 5
[39m
GEOID NAME B01001_001E B01001_001M B01001_002E
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[38;5;250m1
[39m 01 Alabama 4
[4m8
[24m
[4m7
[24m
[4m6
[24m250
[31mNA
[39m 2
[4m3
[24m
[4m5
[24m
[4m9
[24m355
[38;5;250m2
[39m 02 Alaska
[4m7
[24m
[4m3
[24m
[4m7
[24m068
[31mNA
[39m
[4m3
[24m
[4m8
[24m
[4m4
[24m915
[38;5;250m3
[39m 04 Arizona 7
[4m0
[24m
[4m5
[24m
[4m0
[24m299
[31mNA
[39m 3
[4m5
[24m
[4m0
[24m
[4m4
[24m509
[38;5;250m4
[39m 05 Arkansas 2
[4m9
[24m
[4m9
[24m
[4m9
[24m370
[31mNA
[39m 1
[4m4
[24m
[4m7
[24m
[4m1
[24m760
[38;5;250m5
[39m 06 California 39
[4m2
[24m
[4m8
[24m
[4m3
[24m497
[31mNA
[39m 19
[4m5
[24m
[4m2
[24m
[4m6
[24m298
ds_county_acs_clean <- geo_tidy_acs_data(
data = ds_county_acs_raw,
dictionary = geo_dictionary,
geo_type = "county"
)
ds_county_acs_clean[1:5, c(1:3, 20:22)] %>% head()
[38;5;246m# A tibble: 5 × 6
[39m
geoid county state bachelor_field_business bachelor_field_education bachelor_field_engineering
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<chr>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[38;5;250m1
[39m 01001 Autauga County Alabama 0.230 0.161 0.060
[4m7
[24m
[38;5;250m2
[39m 01003 Baldwin County Alabama 0.220 0.164 0.079
[4m4
[24m
[38;5;250m3
[39m 01005 Barbour County Alabama 0.204 0.302 0.058
[4m9
[24m
[38;5;250m4
[39m 01007 Bibb County Alabama 0.257 0.190 0.053
[4m1
[24m
[38;5;250m5
[39m 01009 Blount County Alabama 0.163 0.241 0.046
[4m2
[24m
[38;5;246m# A tibble: 4 × 3
[39m
population pop_u18 children_u18
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[3m
[38;5;246m<dbl>
[39m
[23m
[38;5;250m1
[39m 56
[4m0
[24m
[4m3
[24m
[4m3
[24m139 0.209 0.185
[38;5;250m2
[39m 68
[4m1
[24m
[4m0
[24m
[4m8
[24m086 0.228 0.205
[38;5;250m3
[39m 123
[4m4
[24m
[4m0
[24m
[4m8
[24m246 0.230 0.199
[38;5;250m4
[39m 77
[4m1
[24m
[4m4
[24m
[4m8
[24m324 0.231 0.200
Postprocessing
The data created with geo_tidy_acs_data() will most
likely contain more variables than is indeed needed for your particular
study. Most likely the variables that represent total levels of concept
(which in most cases are the same as population value), are also the
ones that needs to be dropped manually. Also, you’ll need to remember
that not all variables should be used for modeling (e.g., you should
include only male or female variable in the model when one of the
predictors is sex). You should decide by your own judgment which
variables are most suitable for the study, as well which variables
should be used for modeling.