You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.1 KiB

6 years ago
4 years ago
6 years ago
5 years ago
4 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
4 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
  1. ---
  2. output: rmarkdown::github_document
  3. ---
  4. ```{r, echo=FALSE}
  5. knitr::opts_chunk$set(
  6. collapse = TRUE,
  7. warning = FALSE,
  8. message = FALSE,
  9. comment = "#>",
  10. fig.path = "README_files/README-",
  11. fig.retina = 2
  12. )
  13. ```
  14. ### :mask: cdcfluview - Retrieve U.S. Flu Season Data from the CDC FluView Portal
  15. [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/cdcfluview)](https://cran.r-project.org/package=cdcfluview)
  16. [![Travis-CI Build Status](https://travis-ci.org/hrbrmstr/cdcfluview.svg?branch=master)](https://travis-ci.org/hrbrmstr/cdcfluview)
  17. **NOTE** If there's a particular data set from https://www.cdc.gov/flu/weekly/fluviewinteractive.htm that you want and that isn't in the package, please file it as an issue and be as specific as you can (screen shot if possible).
  18. -----
  19. The U.S. Centers for Disease Control (CDC) maintains a [portal](https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html) for accessing state, regional and national influenza statistics. The portal's Flash interface makes it difficult and time-consuming to select and retrieve influenza data. This package provides functions to access the data provided by the portal's underlying API.
  20. The following functions are implemented:
  21. - `get_flu_data`: Retrieves state, regional or national influenza statistics from the CDC
  22. - `get_state_data`: Retrieves state/territory-level influenza statistics from the CDC
  23. - `get_weekly_flu_report`: Retrieves (high-level) weekly influenza surveillance report from the CDC
  24. - `get_mortality_surveillance_data` : (fairly self explanatory but also pretty new to the pkg and uses data from: https://www.cdc.gov/flu/weekly/nchs.htm
  25. The following data sets are included:
  26. - `hhs_regions` HHS Region Table (a data frame with 59 rows and 4 variables)
  27. - `census_regions` Census Region Table (a data frame with 51 rows and 2 variables)
  28. ### News
  29. - See NEWS
  30. - Version 0.4.0 - [CRAN release](http://cran.r-project.org/web/packages/cdcfluview)
  31. - Version 0.4.0.999 released : another fix for the CDC API (for region parameter); added data files for HHS/Census region lookups; added weekly high-level flu report retrieval
  32. - Version 0.3 released : fix for the CDC API (it changed how year & region params are encoded in the request)
  33. - Version 0.2.1 released : bumped up `httr` version # requirement in `DESCRIPTION` (via Issue [1](https://github.com/hrbrmstr/cdcfluview/issues/1))
  34. - Version 0.2 released : added state-level data retrieval
  35. - Version 0.1 released
  36. ### Installation
  37. ```{r eval=FALSE}
  38. install.packages("cdcfluview")
  39. # **OR**
  40. devtools::install_github("hrbrmstr/cdcfluview")
  41. ```
  42. ```{r echo=FALSE, message=FALSE, warning=FALSE, error=FALSE}
  43. options(width=120)
  44. ```
  45. ### Usage
  46. ```{r state2015, message=FALSE, warning=FALSE, fig.height=10, fig.width=6}
  47. library(cdcfluview)
  48. library(ggplot2)
  49. library(dplyr)
  50. library(statebins)
  51. # current verison
  52. packageVersion("cdcfluview")
  53. flu <- get_flu_data("hhs", sub_region=1:10, "ilinet", years=2014)
  54. glimpse(flu)
  55. state_flu <- get_state_data(years=2015)
  56. glimpse(state_flu)
  57. gg <- ggplot(flu, aes(x=WEEK, y=`% WEIGHTED ILI`, group=REGION))
  58. gg <- gg + geom_line()
  59. gg <- gg + facet_wrap(~REGION, ncol=2)
  60. gg <- gg + theme_bw()
  61. gg
  62. ```
  63. ```{r mortality, message=FALSE, warning=FALSE, fig.height=6, fig.width=10}
  64. msd <- get_mortality_surveillance_data()
  65. mutate(msd$by_state, ym=as.Date(sprintf("%04d-%02d-1", Year, Week), "%Y-%U-%u")) %>%
  66. select(state, wk=ym, death_pct=`Percent of Deaths Due to Pneumonia and Influenza`) %>%
  67. mutate(death_pct=death_pct/100) -> df
  68. gg <- ggplot() + geom_smooth(data=df, aes(wk, death_pct, group=state),
  69. se=FALSE, color="#2b2b2b", size=0.25)
  70. gb <- ggplot_build(gg)
  71. gb$data[[1]] %>%
  72. arrange(desc(x)) %>%
  73. group_by(group) %>%
  74. slice(1) %>%
  75. ungroup() %>%
  76. arrange(desc(y)) %>%
  77. head(1) -> top
  78. top_state <- sort(unique(msd$by_state$state))[top$group]
  79. gg <- gg + geom_text(data=top, aes(as.Date(x, origin="1970-01-01"), y, label=top_state),
  80. hjust=1, family="Arial Narrow", size=3, nudge_x=-5, nudge_y=-0.001)
  81. gg <- gg + scale_x_date(expand=c(0,0))
  82. gg <- gg + scale_y_continuous(label=scales::percent)
  83. gg <- gg + labs(x=NULL, y=NULL,
  84. title="Percent of In-State Deaths Due to Pneumonia and Pnfluenza (2010-Present)")
  85. gg <- gg + theme_bw(base_family="Arial Narrow")
  86. gg <- gg + theme(axis.text.x=element_text(margin=margin(0,0,0,0)))
  87. gg <- gg + theme(axis.text.y=element_text(margin=margin(0,0,0,0)))
  88. gg <- gg + theme(axis.ticks=element_blank())
  89. gg <- gg + theme(plot.title=element_text(face="bold", size=16))
  90. gg
  91. ```
  92. ```{r region, eval=FALSE, include=FALSE, warning=FALSE, error=FALSE, message=FALSE}
  93. dat <- get_flu_data(region="hhs",
  94. sub_region=1:10,
  95. data_source="ilinet",
  96. years=2000:2014)
  97. dat %>%
  98. mutate(REGION=factor(REGION,
  99. levels=unique(REGION),
  100. labels=c("Boston", "New York",
  101. "Philadelphia", "Atlanta",
  102. "Chicago", "Dallas",
  103. "Kansas City", "Denver",
  104. "San Francisco", "Seattle"),
  105. ordered=TRUE)) %>%
  106. mutate(season_week=ifelse(WEEK>=40, WEEK-40, WEEK),
  107. season=ifelse(WEEK<40,
  108. sprintf("%d-%d", YEAR-1, YEAR),
  109. sprintf("%d-%d", YEAR, YEAR+1))) -> dat
  110. prev_years <- dat %>% filter(season != "2014-2015")
  111. curr_year <- dat %>% filter(season == "2014-2015")
  112. curr_week <- tail(dat, 1)$season_week
  113. gg <- ggplot()
  114. gg <- gg + geom_point(data=prev_years,
  115. aes(x=season_week, y=`% WEIGHTED ILI`, group=season),
  116. color="#969696", size=1, alpha=0.25)
  117. gg <- gg + geom_point(data=curr_year,
  118. aes(x=season_week, y=`% WEIGHTED ILI`, group=season),
  119. color="red", size=1.25, alpha=1)
  120. gg <- gg + geom_line(data=curr_year,
  121. aes(x=season_week, y=`% WEIGHTED ILI`, group=season),
  122. size=1.25, color="#d7301f")
  123. gg <- gg + geom_vline(xintercept=curr_week, color="#d7301f", size=0.5, linetype="dashed", alpha=0.5)
  124. gg <- gg + facet_wrap(~REGION, ncol=3)
  125. gg <- gg + labs(x=NULL, y="Weighted ILI Index",
  126. title="ILINet - 1999-2015 year weighted flu index history by CDC region\nWeek Ending Jan 3, 2015 (Red == 2014-2015 season)\n")
  127. gg <- gg + theme_bw()
  128. gg <- gg + theme(panel.grid=element_blank())
  129. gg <- gg + theme(strip.background=element_blank())
  130. gg <- gg + theme(axis.ticks.x=element_blank())
  131. gg <- gg + theme(axis.text.x=element_blank())
  132. ```
  133. ```{r bins, message=FALSE, fig.height=5, fig.width=7}
  134. gg_s <- state_flu %>%
  135. filter(weekend=="Jan-02-2016") %>%
  136. select(state=statename, value=activity_level) %>%
  137. filter(!(state %in% c("Puerto Rico", "New York City"))) %>% # need to add PR to statebins
  138. mutate(value=as.numeric(gsub("Level ", "", value))) %>%
  139. statebins(brewer_pal="RdPu", breaks=4,
  140. labels=c("Minimal", "Low", "Moderate", "High"),
  141. legend_position="bottom", legend_title="ILI Activity Level") +
  142. ggtitle("CDC State FluView (2015-01-03)")
  143. gg_s
  144. ```
  145. ### Test Results
  146. ```{r message=FALSE}
  147. library(cdcfluview)
  148. library(testthat)
  149. date()
  150. test_dir("tests/")
  151. ```