Skip to content

Commit 6d31320

Browse files
committed
remove rmd example to fix error
1 parent 6f2009b commit 6d31320

File tree

4 files changed

+145
-186
lines changed

4 files changed

+145
-186
lines changed

R/epi_df.R

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,51 @@ NULL
111111
#' @return An `epi_df` object.
112112
#'
113113
#' @export
114-
#' @includeRmd man/rmd/epi_df_example.Rmd examples
114+
#' @examples
115+
#' # Convert a `tsibble` that has county code as an extra key
116+
#' ex1 <- tsibble::tibble(
117+
#' geo_value = rep(c("ca", "fl", "pa"), each = 3),
118+
#' county_code = c(06059,06061,06067,
119+
#' 12111,12113,12117,
120+
#' 42101, 42103,42105),
121+
#' time_value = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
122+
#' by = "day"), length.out = length(geo_value)),
123+
#' value = 1:length(geo_value) + 0.01 * rnorm(length(geo_value))
124+
#' ) %>%
125+
#' tsibble::as_tsibble(index = time_value, key = c(geo_value, county_code))
126+
#'
127+
#' ex1 <- as_epi_df(x = ex1, geo_type = "state", time_type = "day", as_of = "2020-06-03")
128+
#' attr(ex1,"metadata")
129+
#'
130+
#' # Dealing with misspecified column names
131+
#' ex2 <- tsibble::tibble(
132+
#' state = rep(c("ca", "fl", "pa"), each = 3), # misnamed
133+
#' pol = rep(c("blue", "swing", "swing"), each = 3), # extra key
134+
#' reported_date = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
135+
#' by = "day"), length.out = length(state)), # misnamed
136+
#' value = 1:length(state) + 0.01 * rnorm(length(state))
137+
#' ) %>% data.frame()
138+
#'
139+
#' head(ex2)
140+
#'
141+
#' ex2 <- ex2 %>% dplyr::rename(geo_value = state, time_value = reported_date) %>%
142+
#' as_epi_df(geo_type = "state", as_of = "2020-06-03",
143+
#' additional_metadata = c(other_keys = "pol"))
144+
#'
145+
#' attr(ex2,"metadata")
146+
#'
147+
#' # Adding additional keys to an `epi_df` object
148+
#'
149+
#' ex3 <- jhu_csse_county_level_subset %>%
150+
#' filter(time_value > "2021-12-01", state_name == "Massachusetts") %>%
151+
#' dplyr::slice_tail(n = 6)
152+
#'
153+
#' ex3 <- ex3 %>%
154+
#' tsibble::as_tsibble() %>% # needed to add the additional metadata
155+
#' dplyr::mutate(state = rep("MA",6)) %>%
156+
#' as_epi_df(additional_metadata = c(other_keys = "state"))
157+
#'
158+
#' attr(ex3,"metadata")
115159
as_epi_df = function(x, ...) {
116160
UseMethod("as_epi_df")
117161
}

man/as_epi_df.Rd

Lines changed: 18 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rmd/epi_df_example.Rmd

Lines changed: 0 additions & 91 deletions
This file was deleted.

vignettes/epiprocess.Rmd

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,87 @@ ggplot(x, aes(x = time_value, y = cases)) +
200200
```
201201

202202
## Examples on Additional Keys in epi_df
203+
In the following examples we will show how to create an `epi_df` with additional keys.
203204

204-
```{r child = 'man/rmd/epi_df_example.Rmd'}
205+
### Convert a `tsibble` that has county code as an extra key
206+
```{r}
207+
ex1 <- tibble(
208+
geo_value = rep(c("ca", "fl", "pa"), each = 3),
209+
county_code = c(06059,06061,06067,
210+
12111,12113,12117,
211+
42101, 42103,42105),
212+
time_value = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
213+
by = "day"), length.out = length(geo_value)),
214+
value = 1:length(geo_value) + 0.01 * rnorm(length(geo_value))
215+
) %>%
216+
as_tsibble(index = time_value, key = c(geo_value, county_code))
217+
218+
ex1 <- as_epi_df(x = ex1, geo_type = "state", time_type = "day", as_of = "2020-06-03")
219+
```
220+
221+
The metadata now includes `county_code` as an extra key.
222+
```{r}
223+
attr(ex1,"metadata")
224+
```
225+
226+
227+
### Dealing with misspecified column names
228+
229+
`epi_df` requires there to be columns `geo_value` and `time_value`, if they do not exist then `as_epi_df()` throws an error.
230+
```{r, error = TRUE}
231+
data.frame(
232+
state = rep(c("ca", "fl", "pa"), each = 3), # misnamed
233+
pol = rep(c("blue", "swing", "swing"), each = 3), # extra key
234+
reported_date = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
235+
by = "day"), length.out = length(geo_value)), # misnamed
236+
value = 1:length(geo_value) + 0.01 * rnorm(length(geo_value))
237+
) %>% as_epi_df()
205238
```
239+
240+
The columns can be renamed to match `epi_df` format. In the example below, notice there is also an additional key `pol`.
241+
```{r}
242+
ex2 <- tibble(
243+
state = rep(c("ca", "fl", "pa"), each = 3), # misnamed
244+
pol = rep(c("blue", "swing", "swing"), each = 3), # extra key
245+
reported_date = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
246+
by = "day"), length.out = length(state)), # misnamed
247+
value = 1:length(state) + 0.01 * rnorm(length(state))
248+
) %>% data.frame()
249+
250+
head(ex2)
251+
252+
ex2 <- ex2 %>% rename(geo_value = state, time_value = reported_date) %>%
253+
as_epi_df(geo_type = "state", as_of = "2020-06-03",
254+
additional_metadata = c(other_keys = "pol"))
255+
256+
attr(ex2,"metadata")
257+
```
258+
259+
260+
### Adding additional keys to an `epi_df` object
261+
262+
In the above examples, all the keys are added to objects that are not `epi_df` objects. We illustrate how to add keys to an `epi_df` object.
263+
264+
We use a subset dataset from the the `covidcast` library.
265+
266+
```{r}
267+
ex3 <- jhu_csse_county_level_subset %>%
268+
filter(time_value > "2021-12-01", state_name == "Massachusetts") %>%
269+
slice_tail(n = 6)
270+
271+
attr(ex3,"metadata") # geo_type is county currently
272+
```
273+
274+
Now we add state (MA) as a new column and a key to the metadata.
275+
```{r}
276+
277+
ex3 <- ex3 %>%
278+
as_tsibble() %>% # needed to add the additional metadata
279+
mutate(state = rep("MA",6)) %>%
280+
as_epi_df(additional_metadata = c(other_keys = "state"))
281+
282+
attr(ex3,"metadata")
283+
```
284+
285+
286+

0 commit comments

Comments
 (0)