Skip to content

Commit 12dafc1

Browse files
authored
Merge branch 'main' into km-test-epi_df
2 parents f61962b + 0177a5a commit 12dafc1

File tree

5 files changed

+139
-5
lines changed

5 files changed

+139
-5
lines changed

R/growth_rate.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@
100100
#' user.
101101
#'
102102
#' @export
103+
#' @examples
104+
#' # COVID cases growth rate by state using default method relative change
105+
#' jhu_csse_daily_subset %>%
106+
#' group_by(geo_value) %>%
107+
#' mutate(cases_gr = growth_rate(x = time_value, y = cases))
108+
#'
109+
#' # Log scale, degree 4 polynomial and 6-fold cross validation
110+
#' jhu_csse_daily_subset %>%
111+
#' group_by(geo_value) %>%
112+
#' mutate(gr_poly = growth_rate( x = time_value, y = cases, log_scale = TRUE, ord = 4, k = 6))
113+
103114
growth_rate = function(x = seq_along(y), y, x0 = x,
104115
method = c("rel_change", "linear_reg",
105116
"smooth_spline", "trend_filter"),

R/utils.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ guess_geo_type = function(geo_value) {
3939
if (all(geo_value %in% state_values)) return("state")
4040

4141
# Else if all geo values are 2 letters, then use "nation"
42-
else if (all(grepl("[a-z]{2}", geo_value))) return("nation")
42+
else if (all(grepl("[a-z]{2}", geo_value))
43+
& !any(grepl("[a-z]{3}", geo_value))) return("nation")
4344

4445
# Else if all geo values are 5 numbers, then use "county"
45-
else if (all(grepl("[0-9]{5}", geo_value))) return("county")
46+
else if (all(grepl("[0-9]{5}", geo_value)) &
47+
!any(grepl("[0-9]{6}", geo_value))) return("county")
4648
}
4749

4850
else if (is.numeric(geo_value)) {
@@ -79,7 +81,7 @@ guess_time_type = function(time_value) {
7981

8082
# Else, if a Date class, then use "week" or "day" depending on gaps
8183
else if (inherits(time_value, "Date")) {
82-
return(ifelse(all(diff(sort(time_value)) == -7), "week", "day"))
84+
return(ifelse(all(diff(sort(time_value)) == 7), "week", "day"))
8385
}
8486

8587
# Else, check whether it's one of the tsibble classes

man/epi_slide.Rd

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

man/growth_rate.Rd

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

tests/testthat/test-utils.R

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
test_that("break_string works properly",{
2+
expect_equal(break_str("A dog is here", 6),"A dog\nis\nhere")
3+
})
4+
5+
test_that("Abort and Warn work",{
6+
expect_error(Abort("abort"))
7+
expect_warning(Warn("warn"))
8+
})
9+
10+
test_that("in_range works",{
11+
expect_equal(in_range(1,c(2,4)),2)
12+
expect_equal(in_range(3,c(2,4)),3)
13+
expect_equal(in_range(5,c(2,4)),4)
14+
})
15+
16+
test_that("new summarizing functions work",{
17+
x <- c(3,4,5,9,NA)
18+
expect_equal(Min(x),3)
19+
expect_equal(Max(x),9)
20+
expect_equal(Sum(x),21)
21+
expect_equal(Mean(x),5.25)
22+
expect_equal(Median(x),4.5)
23+
})
24+
25+
test_that("Other capital letter functions work",{
26+
x <- c(1,2,3,4,5)
27+
expect_equal(Start(x),1)
28+
expect_equal(End(x),5)
29+
expect_equal(MiddleL(x),2) # Questionable for odd length vectors
30+
expect_equal(MiddleR(x),3) # Questionable for odd length vectors
31+
expect_equal(ExtendL(x),c(1,1,2,3,4,5))
32+
expect_equal(ExtendR(x),c(1,2,3,4,5,5))
33+
})
34+
35+
test_that("guess_geo_type tests for different types of geo_value's",{
36+
# California, New York
37+
states <- c("ca","ny")
38+
39+
# Canada, USA, United Kingdom
40+
nations <- c("ca","us","uk")
41+
42+
# Note: These are just five-number names that may not necessarily be existent
43+
# counties
44+
counties <- c("12345","67890")
45+
46+
# HHS regions
47+
hhs <- c(1:3)
48+
49+
# HRR regions
50+
hrr <- c(100,200)
51+
52+
# Long numbers should be custom
53+
long_nums <- c(123456789,111222333)
54+
55+
# Health regions in British Columbia
56+
bc <- c("Vancouver Coastal","Interior","Fraser",
57+
"Northern","Vancouver Island")
58+
59+
# Long numbers as strings should also be custom
60+
long_num_strings <- c("123456789","111222333")
61+
62+
expect_equal(guess_geo_type(states),"state")
63+
expect_equal(guess_geo_type(nations),"nation")
64+
expect_equal(guess_geo_type(counties),"county")
65+
expect_equal(guess_geo_type(hhs),"hhs")
66+
expect_equal(guess_geo_type(hrr),"hrr")
67+
expect_equal(guess_geo_type(long_num_strings),"custom")
68+
expect_equal(guess_geo_type(bc),"custom")
69+
expect_equal(guess_geo_type(long_nums),"custom")
70+
})
71+
72+
test_that("guess_time_type works for different types",{
73+
days <- as.Date("2022-01-01") + 0:6
74+
weeks <- as.Date("2022-01-01") + 7 * 0:6
75+
76+
yearweeks <- tsibble::yearweek(10)
77+
yearmonths <- tsibble::yearmonth(10)
78+
yearquarters <- tsibble::yearquarter(10)
79+
80+
years <- c(1999,2000)
81+
82+
# YYYY-MM-DD is the accepted format
83+
not_ymd1 <- "January 1, 2022"
84+
not_ymd2 <- "1 January 2022"
85+
not_ymd3 <- "1 Jan 2022"
86+
87+
not_a_date <- "asdf"
88+
89+
expect_equal(guess_time_type(days),"day")
90+
expect_equal(guess_time_type(weeks),"week")
91+
92+
expect_equal(guess_time_type(yearweeks),"yearweek")
93+
expect_equal(guess_time_type(yearmonths),"yearmonth")
94+
expect_equal(guess_time_type(yearquarters),"yearquarter")
95+
96+
expect_equal(guess_time_type(years),"year")
97+
98+
expect_equal(guess_time_type(not_ymd1),"custom")
99+
expect_equal(guess_time_type(not_ymd2),"custom")
100+
expect_equal(guess_time_type(not_ymd3),"custom")
101+
expect_equal(guess_time_type(not_a_date),"custom")
102+
})
103+
104+
test_that("enlist works",{
105+
my_list <- enlist(x=1,y=2,z=3)
106+
expect_equal(my_list$x,1)
107+
expect_true(inherits(my_list,"list"))
108+
})

0 commit comments

Comments
 (0)