context("StackYears")
library(verbs)
dates <- c("2012-08-12", "2012-08-19", "2012-08-26", "2012-09-02", "2012-09-09", "2012-09-16", "2012-09-23", "2012-09-30", "2012-10-07", "2012-10-14", "2012-10-21", "2012-10-28", "2012-11-04", "2012-11-11", "2012-11-18", "2012-11-25", "2012-12-02", "2012-12-09", "2012-12-16", "2012-12-23", "2012-12-30", "2013-01-06", "2013-01-13", "2013-01-20", "2013-01-27", "2013-02-03", "2013-02-10", "2013-02-17", "2013-02-24", "2013-03-03", "2013-03-10", "2013-03-17", "2013-03-24", "2013-03-31", "2013-04-07", "2013-04-14", "2013-04-21", "2013-04-28", "2013-05-05", "2013-05-12", "2013-05-19", "2013-05-26", "2013-06-02", "2013-06-09", "2013-06-16", "2013-06-23", "2013-06-30", "2013-07-07", "2013-07-14", "2013-07-21", "2013-07-28", "2013-08-04", "2013-08-11", "2013-08-18", "2013-08-25", "2013-09-01", "2013-09-08", "2013-09-15", "2013-09-22", "2013-09-29", "2013-10-06", "2013-10-13", "2013-10-20", "2013-10-27", "2013-11-03", "2013-11-10", "2013-11-17", "2013-11-24", "2013-12-01", "2013-12-08", "2013-12-15", "2013-12-22", "2013-12-29", "2014-01-05", "2014-01-12", "2014-01-19", "2014-01-26", "2014-02-02", "2014-02-09", "2014-02-16", "2014-02-23", "2014-03-02", "2014-03-09", "2014-03-16", "2014-03-23", "2014-03-30", "2014-04-06", "2014-04-13", "2014-04-20", "2014-04-27", "2014-05-04", "2014-05-11", "2014-05-18", "2014-05-25", "2014-06-01", "2014-06-08", "2014-06-15", "2014-06-22", "2014-06-29", "2014-07-06", "2014-07-13", "2014-07-20", "2014-07-27", "2014-08-03", "2014-08-10", "2014-08-17", "2014-08-24", "2014-08-31", "2014-09-07", "2014-09-14", "2014-09-21", "2014-09-28", "2014-10-05", "2014-10-12", "2014-10-19", "2014-10-26", "2014-11-02", "2014-11-09", "2014-11-16", "2014-11-23", "2014-11-30", "2014-12-07", "2014-12-14", "2014-12-21", "2014-12-28", "2015-01-04", "2015-01-11", "2015-01-18", "2015-01-25", "2015-02-01", "2015-02-08", "2015-02-15", "2015-02-22", "2015-03-01", "2015-03-08", "2015-03-15", "2015-03-22", "2015-03-29", "2015-04-05", "2015-04-12", "2015-04-19", "2015-04-26", "2015-05-03", "2015-05-10", "2015-05-17", "2015-05-24", "2015-05-31", "2015-06-07", "2015-06-14", "2015-06-21", "2015-06-28", "2015-07-05", "2015-07-12", "2015-07-19", "2015-07-26", "2015-08-02", "2015-08-09", "2015-08-16", "2015-08-23", "2015-08-30", "2015-09-06", "2015-09-13", "2015-09-20", "2015-09-27", "2015-10-04", "2015-10-11", "2015-10-18", "2015-10-25", "2015-11-01", "2015-11-08", "2015-11-15", "2015-11-22", "2015-11-29", "2015-12-06", "2015-12-13", "2015-12-20", "2015-12-27", "2016-01-03", "2016-01-10", "2016-01-17", "2016-01-24", "2016-01-31", "2016-02-07", "2016-02-14", "2016-02-21", "2016-02-28", "2016-03-06", "2016-03-13", "2016-03-20", "2016-03-27", "2016-04-03", "2016-04-10", "2016-04-17", "2016-04-24", "2016-05-01", "2016-05-08", "2016-05-15", "2016-05-22", "2016-05-29", "2016-06-05", "2016-06-12", "2016-06-19", "2016-06-26", "2016-07-03", "2016-07-10", "2016-07-17", "2016-07-24", "2016-07-31", "2016-08-07", "2016-08-14", "2016-08-21", "2016-08-28", "2016-09-04", "2016-09-11", "2016-09-18", "2016-09-25", "2016-10-02", "2016-10-09", "2016-10-16", "2016-10-23", "2016-10-30", "2016-11-06", "2016-11-13", "2016-11-20", "2016-11-27", "2016-12-04", "2016-12-11", "2016-12-18", "2016-12-25", "2017-01-01", "2017-01-08", "2017-01-15", "2017-01-22", "2017-01-29", "2017-02-05", "2017-02-12", "2017-02-19", "2017-02-26", "2017-03-05", "2017-03-12", "2017-03-19", "2017-03-26", "2017-04-02", "2017-04-09", "2017-04-16", "2017-04-23", "2017-04-30", "2017-05-07", "2017-05-14", "2017-05-21", "2017-05-28", "2017-06-04", "2017-06-11", "2017-06-18", "2017-06-25", "2017-07-02", "2017-07-09", "2017-07-16", "2017-07-23", "2017-07-30")
dates <- as.Date(dates)
adhd = c(30, 0, 10, 20, 13, 0, 0, 0, 9, 9, 0, 9, 9, 15, 0, 0, 9, 0, 0, 15, 9, 9, 9, 9, 8, 0, 25, 20, 8, 0, 13, 12, 9, 8, 17, 17, 8, 8, 28, 9, 0, 9, 17, 9, 18, 0, 9, 9, 0, 9, 9, 13, 0, 9, 8, 9, 8, 0, 8, 8, 20, 8, 14, 8, 20, 8, 8, 9, 8, 0, 8, 10, 13, 8, 8, 8, 8, 25, 0, 24, 15, 12, 0, 13, 8, 0, 0, 11, 16, 16, 24, 11, 18, 9, 14, 14, 10, 9, 15, 14, 28, 19, 14, 20, 14, 28, 15, 9, 30, 29, 35, 17, 15, 17, 29, 9, 13, 26, 17, 14, 33, 21, 9, 10, 10, 26, 11, 20, 13, 25, 17, 0, 14, 17, 26, 13, 24, 22, 13, 17, 17, 14, 21, 27, 18, 9, 18, 13, 19, 19, 10, 10, 9, 14, 19, 14, 19, 25, 18, 9, 18, 23, 13, 20, 39, 17, 20, 33, 25, 25, 13, 18, 20, 16, 23, 23, 18, 18, 14, 26, 16, 9, 18, 7, 18, 16, 14, 9, 11, 14, 16, 14, 16, 14, 10, 10, 12, 20, 22, 11, 15, 18, 23, 21, 23, 8, 15, 8, 26, 27, 13, 27, 29, 24, 33, 26, 12, 17, 14, 19, 28, 20, 16, 12, 22, 17, 22, 10, 29, 10, 31, 28, 19, 26, 21, 51, 22, 29, 27, 38, 37, 45, 43, 58, 52, 58, 100, 80, 72, 59, 31, 40, 38, 51, 17, 28, 19, 24, 25, 19)
test_that("StackYears - diferent typesof data input",{
# vectors
stacked = suppressWarnings(StackYears(adhd, dates))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# vector
x <- adhd
names(x) = dates
stacked = suppressWarnings(StackYears(x))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# 1-D array
x <- as.array(x)
stacked = suppressWarnings(StackYears(x))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# matrix
dt = cbind(as.character(dates), adhd)
stacked = suppressWarnings(StackYears(dt))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# transposed matrix
dt = cbind(as.character(dates), adhd)
stacked = suppressWarnings(StackYears(dt))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# list
dt = list(dates, adhd)
stacked = suppressWarnings(StackYears(dt))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# data.frame - 2 columns
dt = data.frame(dates, adhd)
stacked = suppressWarnings(StackYears(dt))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# data.frame - 1 columns
dt = data.frame(adhd)
rownames(dt) <- dates
stacked = suppressWarnings(StackYears(dt))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# table
dt = as.table(cbind(as.character(dates), adhd))
stacked = suppressWarnings(StackYears(dt))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
# table
dt = t(dt)
stacked = suppressWarnings(StackYears(dt))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
})
test_that("StackYears - arguments",{
stacked = suppressWarnings(StackYears(adhd, dates))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 6)
expect_equal(ncol(stacked), 52)
expect_warning(StackYears(adhd, dates, n.years = 10))
stacked = suppressWarnings(StackYears(adhd, dates, n.years = 2))
expect_equal(colnames(stacked)[1], "2017-01-01")
expect_equal(stacked[1,1], 10)
expect_equal(nrow(stacked), 2)
expect_equal(ncol(stacked), 52)
stacked = StackYears(adhd, dates, n.years = 3, calendar = FALSE)
expect_equal(colnames(stacked)[1], "2016-07-31")
expect_equal(stacked[1,1], 8)
expect_equal(stacked[2,1], 14)
expect_equal(nrow(stacked), 3)
expect_equal(ncol(stacked), 53)
stacked = StackYears(adhd, dates, n.years = 3, calendar = FALSE, period.number = TRUE)
expect_equal(colnames(stacked)[2], "2")
expect_equal(stacked[2,1], 14)
expect_equal(nrow(stacked), 3)
expect_equal(ncol(stacked), 53)
stacked = StackYears(adhd, dates, n.years = 3, calendar = FALSE, period.number = TRUE, transpose = TRUE)
expect_equal(rownames(stacked)[2], "2")
expect_equal(stacked[1,2], 14)
expect_equal(nrow(stacked), 53)
expect_equal(ncol(stacked), 3)
})
test_that("Quarters", {
dates <- c("Sep-72","Dec-72","Mar-73","Jun-73","Sep-73","Dec-73","Mar-74",
"Jun-74","Sep-74","Dec-74","Mar-75","Jun-75","Sep-75","Dec-75",
"Mar-76","Jun-76","Sep-76","Dec-76","Mar-77","Jun-77","Sep-77",
"Dec-77","Mar-78","Jun-78","Sep-78","Dec-78","Mar-79","Jun-79",
"Sep-79","Dec-79","Mar-80","Jun-80","Sep-80","Dec-80","Mar-81",
"Jun-81","Sep-81","Dec-81","Mar-82","Jun-82","Sep-82","Dec-82",
"Mar-83","Jun-83","Sep-83","Dec-83","Mar-84","Jun-84","Sep-84",
"Dec-84","Mar-85","Jun-85","Sep-85","Dec-85","Mar-86","Jun-86",
"Sep-86","Dec-86","Mar-87","Jun-87","Sep-87","Dec-87","Mar-88",
"Jun-88","Sep-88","Dec-88","Mar-89","Jun-89","Sep-89","Dec-89",
"Mar-90","Jun-90","Sep-90","Dec-90","Mar-91","Jun-91","Sep-91",
"Dec-91","Mar-92","Jun-92","Sep-92","Dec-92","Mar-93","Jun-93",
"Sep-93","Dec-93","Mar-94","Jun-94","Sep-94","Dec-94","Mar-95",
"Jun-95","Sep-95","Dec-95","Mar-96","Jun-96","Sep-96","Dec-96",
"Mar-97","Jun-97","Sep-97","Dec-97","Mar-98","Jun-98","Sep-98",
"Dec-98","Mar-99","Jun-99","Sep-99","Dec-99","Mar-00","Jun-00",
"Sep-00","Dec-00","Mar-01","Jun-01","Sep-01","Dec-01","Mar-02",
"Jun-02","Sep-02","Dec-02","Mar-03","Jun-03","Sep-03","Dec-03",
"Mar-04","Jun-04","Sep-04","Dec-04","Mar-05","Jun-05","Sep-05",
"Dec-05","Mar-06","Jun-06","Sep-06","Dec-06","Mar-07","Jun-07",
"Sep-07","Dec-07","Mar-08","Jun-08","Sep-08","Dec-08","Mar-09",
"Jun-09","Sep-09","Dec-09","Mar-10","Jun-10","Sep-10","Dec-10",
"Mar-11","Jun-11","Sep-11","Dec-11","Mar-12","Jun-12","Sep-12",
"Dec-12","Mar-13","Jun-13","Sep-13","Dec-13","Mar-14","Jun-14",
"Sep-14","Dec-14","Mar-15","Jun-15","Sep-15","Dec-15","Mar-16",
"Jun-16","Sep-16","Dec-16","Mar-17","Jun-17")
vals <- c(10.2,10.3,10.7,11.2,12,12.6,13,13.4,13.8,13.8,14,14.3,14.6,15,
15.8,15.7,16.1,16.8,17,17.4,17.9,18.4,18.6,19.1,19.7,20.4,21.1,
22.1,22.8,23.3,24.5,25.1,25.5,26.3,26.8,27.2,27.8,28.4,28.7,29.1,
29.9,30.4,31.1,32.3,32.7,33.2,33.8,33.6,34.2,34.9,35.3,35.8,36.3,
37.5,38.3,38.7,39.8,40.6,41,41.1,41.5,42.3,43,44.4,45.5,46.5,
47.1,48.7,50,50,50.6,51.8,51.2,51.9,53.4,52.7,53.3,53.7,54,53.8,
53.4,54,55.4,54.6,55,55.5,55.8,55.4,55.8,56.4,57.4,57.7,58.7,58.9,
59,59.6,59.8,60.7,61.3,61.5,61.4,61.5,62.1,62.6,63.3,64.2,65.4,
65.1,65.5,65.6,65.6,66.2,67.5,68.1,70.4,70.5,70.8,72.9,73.2,72.8,
73.3,74.8,76.1,76,75.4,77.3,78.9,77.8,77.3,78.6,79.4,79.1,80,
81.5,82.5,86.2,88.5,88.8,86.3,87.4,89,88.7,90.9,90.5,91.8,93.9,
96.1,94.8,93.9,95.7,97,96.5,96.1,98.6,101.5,102.5,102.3,100.6,
98.3,98.8,101.5,100.9,99.9,100.1,100.3,101.8,102.5,102.4,104,
104.2,104.8,104.7,104.5,104.9,104.7,104.4,106.9,107.5,107.5,107.3)
stacked <- StackYears(vals, dates)
expect_equal(colnames(stacked), c("2017-03", "2017-06", "2017-09", "2017-12"))
expect_equal(rownames(stacked),
c("2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010",
"2009", "2008", "2007", "2006", "2005", "2004", "2003", "2002",
"2001", "2000", "1999", "1998", "1997", "1996", "1995", "1994",
"1993", "1992", "1991", "1990", "1989", "1988", "1987", "1986",
"1985", "1984", "1983", "1982", "1981", "1980", "1979", "1978",
"1977", "1976", "1975", "1974", "1973", "1972"))
expect_equal(unname(stacked[1, ]), c(107.5, 107.3, NA, NA))
expect_equal(unname(stacked[46, ]), c(NA, NA, 10.2, 10.3))
# All of 2016 included, no error
expect_error(StackYears(Last(vals, 6), Last(dates, 6)), NA)
# First quarter of 2016 excluded
expect_error(StackYears(Last(vals, 5), Last(dates, 5)),
"At least one whole year of data is required.")
# Calendar == FALSE, last 4 quarters, no error
expect_error(StackYears(Last(vals, 4), Last(dates, 4), calendar = FALSE), NA)
# Calendar == FALSE, last 3 quarters
expect_error(StackYears(Last(vals, 3), Last(dates, 3), calendar = FALSE),
"At least one whole year of data is required.")
})
test_that("Months", {
dat <- c(`2018-04` = 1, `2018-05` = 2, `2018-06` = 3, `2018-07` = 4,
`2018-08` = 5, `2018-09` = 6, `2018-10` = 7, `2018-11` = 8, `2018-12` = 9,
`2019-01` = 10, `2019-02` = 11, `2019-03` = 12, `2019-04` = 13,
`2019-05` = 14, `2019-06` = 15, `2019-07` = 16, `2019-08` = 17,
`2019-09` = 18, `2019-10` = 19, `2019-11` = 20, `2019-12` = 21,
`2020-01` = 22, `2020-02` = 23, `2020-03` = 24, `2020-04` = 25,
`2020-05` = 26, `2020-06` = 27, `2020-07` = 28, `2020-08` = 29,
`2020-09` = 30)
stacked <- StackYears(dat)
expect_equal(colnames(stacked), c("2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06",
"2020-07", "2020-08", "2020-09", "2020-10", "2020-11", "2020-12"))
expect_equal(rownames(stacked), c("2020", "2019", "2018"))
expect_equal(unname(stacked[1, ]),
c(22, 23, 24, 25, 26, 27, 28, 29, 30, NA, NA, NA))
expect_equal(unname(stacked[3, ]),
c(NA, NA, NA, 1, 2, 3, 4, 5, 6, 7, 8, 9))
# All of 2019 included, no error
expect_error(StackYears(Last(dat, 21)), NA)
# Jan 2019 excluded
expect_error(StackYears(Last(dat, 20)),
"At least one whole year of data is required.")
# Calendar == FALSE, last 12 months, no error
expect_error(StackYears(Last(dat, 12), calendar = FALSE), NA)
# Calendar == FALSE, last 11 months
expect_error(StackYears(Last(dat, 11), calendar = FALSE),
"At least one whole year of data is required.")
})
test_that("Insufficient data with weekly data", {
# All of 2016 included, no error
suppressWarnings(expect_error(StackYears(Last(adhd, 83), Last(dates, 83)), NA))
# First week of 2016 excluded
expect_error(StackYears(Last(adhd, 82), Last(dates, 82)),
"At least one whole year of data is required.")
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.