Skip to content

Commit

Permalink
Corrected URL to redistricting data set, tests passing for 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
mdblocker committed Jun 28, 2023
1 parent 111972c commit 3bcdefc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions R/get_census_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' can specify the variables and region(s) for which to obtain data.
#'
#' @param data_url URL root of the API,
#' e.g., \code{"https://api.census.gov/data/2020/dec/dhc"}.
#' e.g., \code{"https://api.census.gov/data/2020/dec/pl"}.
#' @param key A required character object containing user's Census API key,
#' which can be requested \href{https://api.census.gov/data/key_signup.html}{here}.
#' @param var.names A character vector of variables to get,
Expand All @@ -23,7 +23,7 @@
#' @examples
#' \dontrun{
#' get_census_api(
#' data_url = "https://api.census.gov/data/2020/dec/dhc", key = "...",
#' data_url = "https://api.census.gov/data/2020/dec/pl", key = "...",
#' var.names = c("P2_005N", "P2_006N", "P2_007N", "P2_008N"), region = "for=county:*&in=state:34"
#' )
#' }
Expand Down
4 changes: 2 additions & 2 deletions R/get_census_api_2.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' function directly.
#'
#' @param data_url URL root of the API,
#' e.g., \code{"https://api.census.gov/data/2020/dec/dhc"}.
#' e.g., \code{"https://api.census.gov/data/2020/dec/pl"}.
#' @param key A required character object containing user's Census API key,
#' which can be requested \href{https://api.census.gov/data/key_signup.html}{here}.
#' @param get A character vector of variables to get,
Expand All @@ -22,7 +22,7 @@
#' If unsuccessful, function prints the URL query that was constructed.
#'
#' @examples
#' \dontrun{try(get_census_api_2(data_url = "https://api.census.gov/data/2020/dec/dhc", key = "...",
#' \dontrun{try(get_census_api_2(data_url = "https://api.census.gov/data/2020/dec/pl", key = "...",
#' get = c("P2_005N", "P2_006N", "P2_007N", "P2_008N"), region = "for=county:*&in=state:34"))}
#'
#' @references
Expand Down
Binary file modified tests/testthat/data/census_test_nj_block_2020.rds
Binary file not shown.
32 changes: 16 additions & 16 deletions tests/testthat/test-predict_race_2020.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ test_that("Test BISG NJ at county level", {
expect_equal(dim(x), c(7, 20))
expect_equal(sum(is.na(x)), 0L)
expect_equal(sum(x$surname == "Johnson"), 0)
expect_equal(round(x[x$surname == "Khanna", "pred.whi"], 4), 0.0314, tolerance = 0.01)
expect_equal(round(x[x$surname == "Khanna", "pred.asi"], 4), 0.9367, tolerance = 0.01)
expect_equal(round(x[x$surname == "Fifield", "pred.whi"], 4), 0.9230, tolerance = 0.01)
expect_equal(round(x[x$surname == "Lopez", "pred.his"], 4), 0.9178, tolerance = 0.01)
expect_equal(round(x[x$surname == "Khanna", "pred.whi"], 4), 0.0181, tolerance = 0.01)
expect_equal(round(x[x$surname == "Khanna", "pred.asi"], 4), 0.9444, tolerance = 0.01)
expect_equal(round(x[x$surname == "Fifield", "pred.whi"], 4), 0.8664, tolerance = 0.01)
expect_equal(round(x[x$surname == "Lopez", "pred.his"], 4), 0.9392, tolerance = 0.01)
})

test_that("Test fBISG NJ at tract level", {
Expand All @@ -59,16 +59,16 @@ test_that("Test fBISG NJ at tract level", {
expect_equal(dim(x), c(7, 20))
expect_equal(sum(is.na(x)), 0L)
expect_equal(sum(x$surname == "Johnson"), 0)
expect_equal(round(x[x$surname == "Khanna", "pred.whi"], 4), 0.063, tolerance = 0.01) # 0.0644
expect_equal(round(x[x$surname == "Lopez", "pred.his"], 4), 0.78, tolerance = 0.01) # 0.0644
expect_equal(round(x[x$surname == "Khanna", "pred.whi"], 4), 0.031, tolerance = 0.01)
expect_equal(round(x[x$surname == "Lopez", "pred.his"], 4), 0.798, tolerance = 0.01)
})

test_that("BISG NJ at block level", {
skip_on_cran()
set.seed(42)
data(voters)
census <- readRDS(test_path("data/census_test_nj_block_2020.rds"))
voters[voters$surname=="Ratkovic", "block"] <- "3001"
voters <- mutate(voters, block = case_when(block == 1025 ~ "3001", TRUE ~ block))

x <- suppressMessages(predict_race(
voter.file = voters[voters$state == "NJ", ],
Expand All @@ -81,9 +81,9 @@ test_that("BISG NJ at block level", {
expect_equal(dim(x), c(7, 20))
expect_equal(sum(is.na(x$pred.asi)), 0L)
expect_true(!any(duplicated(x$surname)))
expect_equal(x[x$surname == "Khanna", "pred.asi"], 0.7640, tolerance = 0.01)
expect_equal(x[x$surname == "Zhou", "pred.asi"], 1.0, tolerance = 0.1)
expect_equal(x[x$surname == "Lopez", "pred.his"], 0.7, tolerance = 0.1)
expect_equal(x[x$surname == "Khanna", "pred.asi"], 0.8078, tolerance = 0.01)
expect_equal(x[x$surname == "Zhou", "pred.asi"], 0.9926, tolerance = 0.1)
expect_equal(x[x$surname == "Lopez", "pred.his"], 0.8605, tolerance = 0.1)
})

test_that("BISG NJ at block_group level", {
Expand All @@ -106,9 +106,9 @@ test_that("BISG NJ at block_group level", {
expect_equal(dim(x), c(7, 21))
expect_equal(sum(is.na(x$pred.asi)), 0)
expect_true(!any(duplicated(x$surname)))
expect_equal(x[x$surname == "Khanna", "pred.asi"], 0.9183, tolerance = 0.01)
expect_equal(x[x$surname == "Zhou", "pred.asi"], 1.0, tolerance = 0.01)
expect_equal(x[x$surname == "Lopez", "pred.his"], 0.75, tolerance = 0.01)
expect_equal(x[x$surname == "Khanna", "pred.asi"], 0.9374, tolerance = 0.01)
expect_equal(x[x$surname == "Zhou", "pred.asi"], 0.9954, tolerance = 0.01)
expect_equal(x[x$surname == "Lopez", "pred.his"], 0.8361, tolerance = 0.01)
})

test_that("Fails on territories", {
Expand Down Expand Up @@ -152,9 +152,9 @@ test_that("Handles zero-pop. geolocations", {
expect_equal(dim(x), c(7, 20))
expect_equal(sum(is.na(x$pred.asi)), 0)
expect_true(!any(duplicated(x$surname)))
expect_equal(x[x$surname == "Khanna", "pred.asi"], 0.91, tolerance = 0.01)
expect_equal(x[x$surname == "Zhou", "pred.asi"], 0.99, tolerance = 0.01)
expect_equal(x[x$surname == "Lopez", "pred.his"], 0.92, tolerance = 0.01)
expect_equal(x[x$surname == "Khanna", "pred.asi"], 0.9444, tolerance = 0.01)
expect_equal(x[x$surname == "Zhou", "pred.asi"], 0.9932, tolerance = 0.01)
expect_equal(x[x$surname == "Lopez", "pred.his"], 0.9392, tolerance = 0.01)
})

test_that("Fixes for issue #68 work as expected", {
Expand Down

0 comments on commit 3bcdefc

Please sign in to comment.