Browse Source

enhancements to maps and incorporating MMWR week calcs

tags/v0.7.0
boB Rudis 3 years ago
parent
commit
3b143cc66d
No known key found for this signature in database GPG Key ID: 2A514A4997464560
18 changed files with 220 additions and 113 deletions
  1. +1
    -0
      DESCRIPTION
  2. +4
    -2
      NAMESPACE
  3. +42
    -2
      R/aaa.R
  4. +10
    -4
      R/agd-ipt.r
  5. +2
    -1
      R/cdcfluview-package.R
  6. +29
    -17
      R/coverage-map.r
  7. +0
    -1
      R/geographic-spread.R
  8. +1
    -1
      R/ili-weekly-state.r
  9. +17
    -2
      R/ilinet.r
  10. +2
    -2
      R/zzz.r
  11. +28
    -18
      README.Rmd
  12. +2
    -4
      README.md
  13. +3
    -3
      man/age_group_distribution.Rd
  14. +37
    -0
      man/cdc_basemap.Rd
  15. +0
    -32
      man/cdc_coverage_map.Rd
  16. +2
    -2
      man/state_data_providers.Rd
  17. +21
    -3
      tests/testthat/test-cdcfluview.R

+ 1
- 0
DESCRIPTION View File

@ -34,5 +34,6 @@ Imports:
stats,
utils,
sf,
MMWRweek,
units (>= 0.4-6)
RoxygenNote: 6.0.1

+ 4
- 2
NAMESPACE View File

@ -1,7 +1,7 @@
# Generated by roxygen2: do not edit by hand
export(agd_ipt)
export(cdc_coverage_map)
export(age_group_distribution)
export(cdc_basemap)
export(geographic_spread)
export(hospitalizations)
export(ili_weekly_activity_indicators)
@ -10,9 +10,11 @@ export(pi_mortality)
export(state_data_providers)
export(surveillance_areas)
export(who_nrevss)
import(MMWRweek)
import(httr)
importFrom(dplyr,"%>%")
importFrom(dplyr,bind_rows)
importFrom(dplyr,data_frame)
importFrom(dplyr,filter)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)


+ 42
- 2
R/aaa.R View File

@ -1,3 +1,15 @@
# THIS IS NOT EXPORTED FROM MMWRweek but I need it
# Find start date for a calendar year
#
# Finds the state date given a numeric calendar year
# @author Jarad Niemi \email{niemi@@iastate.edu}
.start_date = function(year) {
# Finds start state for this calendar year
jan1 = as.Date(paste(year, '-01-01', sep=''))
wday = as.numeric(MMWRweekday(jan1))
jan1 - (wday-1) + 7*(wday>4)
}
# CDC U.S. region names to ID map
.region_map <- c(national=3, hhs=1, census=2, state=5)
@ -11,5 +23,33 @@
# Our bot's user-agent string
.cdcfluview_ua <- "Mozilla/5.0 (compatible; R-cdcvluview Bot/2.0; https://github.com/hrbrmstr/cdcfluview)"
# CDC Basemap
.cdc_basemap <- "https://gis.cdc.gov/grasp/fluview/FluView1References/data/US_States_w_PR_labels.json"
# CDC Basemaps
.national_outline <- "https://gis.cdc.gov/grasp/fluview/FluView2References/Data/US_84.json"
.hhs_subregions_basemap <- "https://gis.cdc.gov/grasp/fluview/FluView2References/Data/HHSRegions_w_SubGroups.json"
.census_divisions_basemap <- "https://gis.cdc.gov/grasp/fluview/FluView2References/Data/CensusDivs_w_SubGroups.json"
.states_basemap <- "https://gis.cdc.gov/grasp/fluview/FluView2References/Data/StatesFluView.json"
.spread_basemap <- "https://gis.cdc.gov/grasp/fluview/FluView8References/Data/States_Territories_labels.json"
.surv_basemap <- "https://gis.cdc.gov/grasp/fluview/FluView1References/data/US_States_w_PR_labels.json"
# CDC Age Groups
.age_grp <- c("0-4 yr", "5-24 yr", "25-64 yr", "65+ yr")
# CDC Virus Groups
.vir_grp <- c("A (Subtyping not Performed)", "A (H1N1)pdm09", "A (Unable to Subtype)",
"B (Lineage Unspecified)", "A (H1)", "A (H3)", "B (Victoria Lineage)",
"B (Yamagata Lineage)", "H3N2v")
# Week Starts
.tmp <- lapply(1962:2030, .start_date)
mapply(function(.x, .y) {
data_frame(
wk_start = seq(.tmp[[.x]], .tmp[[.y]], "1 week"),
wk_num = 1:length(wk_start)
) -> tmp
tmp[-nrow(tmp),]
}, 1:(length(.tmp)-1), 2:length(.tmp), SIMPLIFY=FALSE) -> .wk
.wk <- Reduce(rbind.data.frame, .wk)
.wk$mmwrid <- 1:nrow(.wk)

+ 10
- 4
R/agd-ipt.r View File

@ -11,7 +11,8 @@
#' @examples \dontrun{
#' agd_ipt()
#' }
agd_ipt <- function() {
age_group_distribution <- function() {
httr::GET(
url = "https://gis.cdc.gov/grasp/fluView6/GetFlu6AllDataP",
httr::user_agent(.cdcfluview_ua),
@ -47,9 +48,14 @@ agd_ipt <- function() {
class(vir_df) <- c("tbl_df", "tbl", "data.frame")
vir_df_cols <- c("sea_label", "age_label", "vir_label", "count", "mmwrid", "seasonid",
"publishyearweekid", "sea_description", "sea_startweek", "sea_endweek",
"vir_description", "vir_startmmwrid", "vir_endmmwrid")
"publishyearweekid", "sea_description", "sea_startweek",
"sea_endweek", "vir_description", "vir_startmmwrid", "vir_endmmwrid")
vir_df <- vir_df[,vir_df_cols]
vir_df$age_label <- factor(vir_df$age_label, levels=.age_grp)
vir_df$vir_label <- factor(vir_df$vir_label, levels=.vir_grp)
vir_df[,vir_df_cols]
vir_df
}

+ 2
- 1
R/cdcfluview-package.R View File

@ -12,8 +12,9 @@
#' @docType package
#' @author Bob Rudis (bob@@rud.is)
#' @import httr
#' @import MMWRweek
#' @importFrom tools file_path_sans_ext
#' @importFrom dplyr left_join bind_rows mutate filter %>%
#' @importFrom dplyr left_join bind_rows mutate filter data_frame %>%
#' @importFrom jsonlite fromJSON
#' @importFrom stats setNames
#' @importFrom sf st_read


+ 29
- 17
R/coverage-map.r View File

@ -1,28 +1,40 @@
#' Retrieve CDC U.S. Coverage Map
#' Retrieve CDC U.S. Basemaps
#'
#' The CDC FluView application uses a composite basemap of coverage areas
#' within the United States that elides and scales Alaska, Hawaii and
#' Puerto Rico and provides elided and scaled breakouts for New York City
#' and the District of Columbia.\cr
#' The CDC FluView application uses a composite basemaps of coverage areas
#' within the United States that elides and scales Alaska, Hawaii,
#' Puerto Rico & the Virgin Islands and some further provide elided and scaled
#' breakouts for New York City and the District of Columbia.\cr
#' \cr
#' The basemap provides polygon identifiers by:
#' \cr
#' - `STATE_FIPS`
#' - `STATE_ABBR`
#' - `STATE_NAME`
#' - `HHS_Region`
#' - `FIPSTXT`)
#' \cr
#' This function retrieves the shapefile, projects to EPSG:5069 and
#' This function retrieves the given shapefile, projects to EPSG:5069 and
#' returns it as an `sf` (simple features) object.
#'
#' @md
#' @export
#' @param basemap select the CDC basemap. One of:
#' - "`national`": outline of the U.S. + AK, HI, PR + VI
#' - "`hhs`": outline of the U.S. + HHS Region Outlines + AK, HI, PR + VI
#' - "`census`": outline of the U.S. + Census Region Outlines + AK, HI, PR + VI
#' - "`states`": outline of the U.S. + State Outlines + AK, HI, PR + VI
#' - "`spread`": outline of the U.S. + State Outlines + AK, HI, PR + VI & Guam
#' - "`surv`": outline of the U.S. + State Outlines + AK, HI, PR + VI
#' @note These are just the basemaps. You need to pair it with the data you wish to visualize.
#' @examples \dontrun{
#' plot(cdc_coverage_map())
#' plot(cdc_basemap("national"))
#' }
cdc_coverage_map <- function() {
xsf <- sf::st_read(.cdc_basemap, quiet=TRUE, stringsAsFactors=FALSE)
cdc_basemap <- function(basemap = c("national", "hhs", "census", "states", "spread", "surv")) {
switch(
basemap,
national = .national_outline,
hhs = .hhs_subregions_basemap,
census = .census_divisions_basemap,
states = .states_basemap,
spread = .spread_basemap,
surv = .surv_basemap
) -> selected_map
xsf <- sf::st_read(selected_map, quiet=TRUE, stringsAsFactors=FALSE)
sf::st_crs(xsf) <- 4326
sf::st_transform(xsf, 5069)
}

+ 0
- 1
R/geographic-spread.R View File

@ -7,7 +7,6 @@
geographic_spread <- function() {
meta <- jsonlite::fromJSON("https://gis.cdc.gov/grasp/Flu8/GetPhase08InitApp?appVersion=Public")
meta$seasons$seasonid
httr::POST(
url = "https://gis.cdc.gov/grasp/Flu8/PostPhase08DownloadData",


+ 1
- 1
R/ili-weekly-state.r View File

@ -60,7 +60,7 @@ ili_weekly_activity_indicators <- function(season_start_year) {
}
#' Retrieve metadat about U.S. State CDC Provider Data
#' Retrieve metadata about U.S. State CDC Provider Data
#'
#' @md
#' @export


+ 17
- 2
R/ilinet.r View File

@ -71,9 +71,24 @@ ilinet <- function(region=c("national", "hhs", "census", "state")) {
xdf <- read.csv(nm, skip = 1, stringsAsFactors=FALSE)
xdf <- .mcga(xdf)
class(xdf) <- c("tbl_df", "tbl", "data.frame")
xdf[xdf=="X"] <- NA
suppressWarnings(xdf$weighted_ili <- to_num(xdf$weighted_ili))
suppressWarnings(xdf$unweighted_ili <- to_num(xdf$unweighted_ili))
suppressWarnings(xdf$age_0_4 <- to_num(xdf$age_0_4))
suppressWarnings(xdf$age_25_49 <- to_num(xdf$age_25_49))
suppressWarnings(xdf$age_25_64 <- to_num(xdf$age_25_64))
suppressWarnings(xdf$age_5_24 <- to_num(xdf$age_5_24))
suppressWarnings(xdf$age_50_64 <- to_num(xdf$age_50_64))
suppressWarnings(xdf$age_65 <- to_num(xdf$age_65))
suppressWarnings(xdf$ilitotal <- to_num(xdf$ilitotal))
suppressWarnings(xdf$num_of_providers <- to_num(xdf$num_of_providers))
suppressWarnings(xdf$total_patients <- to_num(xdf$total_patients))
suppressWarnings(xdf$week_start <- as.Date(sprintf("%s-%02d-1", xdf$year, xdf$week), "%Y-%U-%u"))
if (region == "national") xdf$region <- "National"
if (region == "hhs") xdf$region <- factor(xdf$region, levels=sprintf("Region %s", 1:10))
class(xdf) <- c("tbl_df", "tbl", "data.frame")
xdf

+ 2
- 2
R/zzz.r View File

@ -1,6 +1,6 @@
# this is only used during active development phases before/after CRAN releases
.onAttach <- function(...) {
.onAttach <- function(...) { # nocov start
if (!interactive()) return()
@ -8,4 +8,4 @@
"There are *MASSIVE* breaking changes*. ",
"See https://github.com/hrbrmstr/cdcfluview for info/news."))
}
} # nocov end

+ 28
- 18
README.Rmd View File

@ -28,14 +28,14 @@ The U.S. Centers for Disease Control (CDC) maintains a portal <http://gis.cdc.go
The following functions are implemented:
- `agd_ipt`: Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories
- `cdc_coverage_map`: Retrieve CDC U.S. Coverage Map
- `age_group_distribution`: Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories
- `cdc_basemap`: Retrieve CDC U.S. Basemaps
- `geographic_spread`: State and Territorial Epidemiologists Reports of Geographic Spread of Influenza
- `hospitalizations`: Laboratory-Confirmed Influenza Hospitalizations
- `ilinet`: Retrieve ILINet Surveillance Data
- `ili_weekly_activity_indicators`: Retrieve weekly state-level ILI indicators per-state for a given season
- `pi_mortality`: Pneumonia and Influenza Mortality Surveillance
- `state_data_providers`: Retrieve metadat about U.S. State CDC Provider Data
- `state_data_providers`: Retrieve metadata about U.S. State CDC Provider Data
- `surveillance_areas`: Retrieve a list of valid sub-regions for each surveillance area.
- `who_nrevss`: Retrieve WHO/NREVSS Surveillance Data
@ -58,6 +58,7 @@ options(width=120)
```{r message=FALSE, warning=FALSE, error=FALSE}
library(cdcfluview)
library(hrbrthemes)
library(tidyverse)
# current verison
@ -67,13 +68,18 @@ packageVersion("cdcfluview")
### Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories
```{r message=FALSE, warning=FALSE}
glimpse(agd_ipt())
glimpse(age_group_distribution())
```
### Retrieve CDC U.S. Coverage Map
```{r message=FALSE, warning=FALSE}
plot(cdc_coverage_map())
plot(cdc_basemap("national"))
plot(cdc_basemap("hhs"))
plot(cdc_basemap("census"))
plot(cdc_basemap("states"))
plot(cdc_basemap("spread"))
plot(cdc_basemap("surv"))
```
### State and Territorial Epidemiologists Reports of Geographic Spread of Influenza
@ -101,13 +107,22 @@ glimpse(hospitalizations("ihsp", "Oklahoma"))
### Retrieve ILINet Surveillance Data
```{r message=FALSE, warning=FALSE}
ilinet("national")
ilinet("hhs")
ilinet("census")
ilinet("state")
walk(c("national", "hhs", "census", "state"), ~{
ili_df <- ilinet(region = .x)
print(glimpse(ili_df))
ggplot(ili_df, aes(week_start, unweighted_ili, group=region, color=region)) +
geom_line() +
viridis::scale_color_viridis(discrete=TRUE) +
labs(x=NULL, y="Unweighted ILI", title=ili_df$region_type[1]) +
theme_ipsum_rc(grid="XY") +
theme(legend.position = "none") -> gg
print(gg)
})
```
### Retrieve weekly state-level ILI indicators per-state for a given season
@ -128,12 +143,7 @@ pi_mortality("state")
pi_mortality("region")
```
### Retrieve metadat about U.S. State CDC Provider Data
```{r message=FALSE, warning=FALSE}
```
### Retrieve a list of valid sub-regions for each surveillance area.
### Retrieve metadata about U.S. State CDC Provider Data
```{r message=FALSE, warning=FALSE}
state_data_providers()


+ 2
- 4
README.md View File

@ -58,7 +58,7 @@ The following functions are implemented:
- `ili_weekly_activity_indicators`: Retrieve weekly state-level ILI
indicators per-state for a given season
- `pi_mortality`: Pneumonia and Influenza Mortality Surveillance
- `state_data_providers`: Retrieve metadat about U.S. State CDC
- `state_data_providers`: Retrieve metadata about U.S. State CDC
Provider Data
- `surveillance_areas`: Retrieve a list of valid sub-regions for each
surveillance area.
@ -484,9 +484,7 @@ pi_mortality("region")
## # ... with 4,180 more rows, and 10 more variables: weeknumber <chr>, geo_description <chr>, age_label <chr>,
## # weekend <date>, weekstart <date>, year <int>, yearweek <int>, coverage_area <chr>, region_name <chr>, callout <chr>
### Retrieve metadat about U.S. State CDC Provider Data
### Retrieve a list of valid sub-regions for each surveillance area.
### Retrieve metadata about U.S. State CDC Provider Data
``` r
state_data_providers()


man/agd_ipt.Rd → man/age_group_distribution.Rd View File

@ -1,10 +1,10 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/agd-ipt.r
\name{agd_ipt}
\alias{agd_ipt}
\name{age_group_distribution}
\alias{age_group_distribution}
\title{Age Group Distribution of Influenza Positive Tests Reported by Public Health Laboratories}
\usage{
agd_ipt()
age_group_distribution()
}
\description{
Retrieves the age group distribution of influenza positive tests that are reported by

+ 37
- 0
man/cdc_basemap.Rd View File

@ -0,0 +1,37 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/coverage-map.r
\name{cdc_basemap}
\alias{cdc_basemap}
\title{Retrieve CDC U.S. Basemaps}
\usage{
cdc_basemap(basemap = c("national", "hhs", "census", "states", "spread",
"surv"))
}
\arguments{
\item{basemap}{select the CDC basemap. One of:
\itemize{
\item "\code{national}": outline of the U.S. + AK, HI, PR + VI
\item "\code{hhs}": outline of the U.S. + HHS Region Outlines + AK, HI, PR + VI
\item "\code{census}": outline of the U.S. + Census Region Outlines + AK, HI, PR + VI
\item "\code{states}": outline of the U.S. + State Outlines + AK, HI, PR + VI
\item "\code{spread}": outline of the U.S. + State Outlines + AK, HI, PR + VI & Guam
\item "\code{surv}": outline of the U.S. + State Outlines + AK, HI, PR + VI
}}
}
\description{
The CDC FluView application uses a composite basemaps of coverage areas
within the United States that elides and scales Alaska, Hawaii,
Puerto Rico & the Virgin Islands and some further provide elided and scaled
breakouts for New York City and the District of Columbia.\cr
\cr
This function retrieves the given shapefile, projects to EPSG:5069 and
returns it as an \code{sf} (simple features) object.
}
\note{
These are just the basemaps. You need to pair it with the data you wish to visualize.
}
\examples{
\dontrun{
plot(cdc_basemap("national"))
}
}

+ 0
- 32
man/cdc_coverage_map.Rd View File

@ -1,32 +0,0 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/coverage-map.r
\name{cdc_coverage_map}
\alias{cdc_coverage_map}
\title{Retrieve CDC U.S. Coverage Map}
\usage{
cdc_coverage_map()
}
\description{
The CDC FluView application uses a composite basemap of coverage areas
within the United States that elides and scales Alaska, Hawaii and
Puerto Rico and provides elided and scaled breakouts for New York City
and the District of Columbia.\cr
\cr
The basemap provides polygon identifiers by:
\cr
\itemize{
\item \code{STATE_FIPS}
\item \code{STATE_ABBR}
\item \code{STATE_NAME}
\item \code{HHS_Region}
\item \code{FIPSTXT})
\cr
This function retrieves the shapefile, projects to EPSG:5069 and
returns it as an \code{sf} (simple features) object.
}
}
\examples{
\dontrun{
plot(cdc_coverage_map())
}
}

+ 2
- 2
man/state_data_providers.Rd View File

@ -2,12 +2,12 @@
% Please edit documentation in R/ili-weekly-state.r
\name{state_data_providers}
\alias{state_data_providers}
\title{Retrieve metadat about U.S. State CDC Provider Data}
\title{Retrieve metadata about U.S. State CDC Provider Data}
\usage{
state_data_providers()
}
\description{
Retrieve metadat about U.S. State CDC Provider Data
Retrieve metadata about U.S. State CDC Provider Data
}
\examples{
state_data_providers()


+ 21
- 3
tests/testthat/test-cdcfluview.R View File

@ -1,26 +1,44 @@
context("basic functionality")
test_that("we can do something", {
skip_on_cran()
expect_that(agd_ipt(), is_a("data.frame"))
expect_that(geographic_spread(), is_a("data.frame"))
expect_that(surveillance_areas(), is_a("data.frame"))
expect_that(state_data_providers(), is_a("data.frame"))
expect_that(hospitalizations("flusurv"), is_a("data.frame"))
expect_that(hospitalizations("eip"), is_a("data.frame"))
expect_that(hospitalizations("eip", "Colorado"), is_a("data.frame"))
expect_that(hospitalizations("ihsp"), is_a("data.frame"))
expect_that(hospitalizations("ihsp", "Oklahoma"), is_a("data.frame"))
expect_that(ilinet("national"), is_a("data.frame"))
expect_that(ilinet("hhs"), is_a("data.frame"))
expect_that(ilinet("census"), is_a("data.frame"))
expect_that(ilinet("state"), is_a("data.frame"))
expect_that(ili_weekly_activity_indicators(2017), is_a("data.frame"))
expect_that(pi_mortality("national"), is_a("data.frame"))
expect_that(pi_mortality("state"), is_a("data.frame"))
expect_that(pi_mortality("region"), is_a("data.frame"))
expect_that(state_data_providers(), is_a("data.frame"))
expect_that(surveillance_areas(), is_a("data.frame"))
expect_that(who_nrevss("national"), is_a("list"))
expect_that(who_nrevss("hhs"), is_a("list"))
expect_that(who_nrevss("census"), is_a("list"))
expect_that(who_nrevss("state"), is_a("list"))
expect_that(cdc_coverage_map(), is_a("sf"))
expect_that(cdc_basemap("national"), is_a("sf"))
expect_that(cdc_basemap("hhs"), is_a("sf"))
expect_that(cdc_basemap("census"), is_a("sf"))
expect_that(cdc_basemap("states"), is_a("sf"))
expect_that(cdc_basemap("spread"), is_a("sf"))
expect_that(cdc_basemap("surv"), is_a("sf"))
})

Loading…
Cancel
Save