Skip to content

Commit

Permalink
bind census data
Browse files Browse the repository at this point in the history
  • Loading branch information
atyre2 committed Sep 18, 2020
1 parent fef8949 commit cde0acc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
11 changes: 8 additions & 3 deletions R/assemble_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
assemble_data <- function(x, path_2_case_data = here::here("data-raw/wnv_by_county.csv"),
case_type = c("neuro", "all"),
match_variable = "fips",
case_variable = "cases"
){
case_variable = "cases"){
if(is.null(x) | !(is.data.frame(x) | tibble::is_tibble(x))){
stop("x must be a data frame or tibble.")
}
Expand All @@ -42,5 +41,11 @@ assemble_data <- function(x, path_2_case_data = here::here("data-raw/wnv_by_coun
stop("Some prediction targets have no matches in case data.")
}
}
return(wnvcases)
case_type = match.arg(case_type)

# bind census data
data(census.data, package = "wnvdata")
fit_data <- dplyr::left_join(wnvcases, census.data, by = c(match_variable, "year"))

return(fit_data)
}
1 change: 1 addition & 0 deletions R/fit_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fit_models <- function(x, path_2_case_data = here::here("data-raw/wnv_by_county.
case_type = c("neuro", "all"),
match_variable = "fips",
case_variable = "cases"){
case_type = match.arg(case_type)
fit_data <- assemble_data(x = x, path_2_case_data = path_2_case_data,
case_type = case_type,
match_variable = match_variable,
Expand Down
8 changes: 6 additions & 2 deletions tests/testthat/test-assemble_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ test_that("data checks work", {
"Some prediction targets have no matches in case data.")
expect_error(assemble_data(x = sampledat, path_2_case_data = here::here("tests/testthat/data/missing_fips.csv")),
regexp = "^match variable \\S* not found in file")
expect_error(assemble_data(x = sampledat, path_2_case_data = here::here("data-raw/predictionsthrough2018.csv"),
case_variable = "expected_cases",
case_type = "foobar"),
regexp = "'arg' should be one of \"neuro\", \"all\"")
})

test_that("return value valid", {
expect_type(assemble_data(x = sampledat,
expect_s3_class(assemble_data(x = sampledat,
path_2_case_data = here::here("data-raw/predictionsthrough2018.csv"),
case_variable = "expected_cases"), "list")
case_variable = "expected_cases"), "data.frame")
})

0 comments on commit cde0acc

Please sign in to comment.