Skip to content

Commit

Permalink
Merge pull request #272 from OHDSI/issue_208
Browse files Browse the repository at this point in the history
test for interval argument in summariseConceptCount
  • Loading branch information
catalamarti authored Dec 17, 2024
2 parents 48f1f02 + 241481e commit fa78c7c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Suggests:
gt,
here,
knitr,
lubridate,
odbc,
remotes,
rmarkdown,
Expand Down
2 changes: 1 addition & 1 deletion R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ validateIntervals <- function(interval, call = parent.frame()){

unitInterval <- dplyr::case_when(
interval == "overall" ~ NA,
interval == "quarters" ~ 4,
interval == "quarters" ~ 3,
interval == "months" ~ 1,
interval == "years" ~ 1
)
Expand Down
10 changes: 5 additions & 5 deletions R/summariseRecordCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ getOmopTableEndDate <- function(omopTable, date){
}

getIntervalTibble <- function(omopTable, start_date_name, end_date_name, interval, unitInterval){
startDate <- getOmopTableStartDate(omopTable, start_date_name)
endDate <- getOmopTableEndDate(omopTable, end_date_name)
startDate <- getOmopTableStartDate(omopTable, start_date_name)
endDate <- getOmopTableEndDate(omopTable, end_date_name)

tibble::tibble(
"group" = seq.Date(as.Date(startDate), as.Date(endDate), "month")
) |>
dplyr::rowwise() |>
dplyr::mutate("interval" = max(which(
.data$group >= seq.Date(from = startDate, to = endDate, by = paste(.env$unitInterval, .env$interval))
.data$group >= seq.Date(from = startDate, to = endDate, by = paste(.env$interval))
),
na.rm = TRUE)) |>
dplyr::ungroup() |>
Expand All @@ -227,9 +227,9 @@ getIntervalTibble <- function(omopTable, start_date_name, end_date_name, interva
"interval_start_date" = as.Date(.data$interval_start_date),
"interval_end_date" = as.Date(.data$interval_end_date)
) |>
dplyr::mutate(
dplyr::mutate(
"interval_group" = paste(.data$interval_start_date,"to",.data$interval_end_date)
) |>
) |>
dplyr::ungroup() |>
dplyr::mutate("my" = paste0(clock::get_month(.data$group),"-",clock::get_year(.data$group))) |>
dplyr::select("interval_group", "my", "interval_start_date","interval_end_date") |>
Expand Down
101 changes: 101 additions & 0 deletions tests/testthat/test-summariseConceptSetCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,104 @@ test_that("dateRange argument works", {
expect_equal(colnames(settings(z)), colnames(settings(x)))
PatientProfiles::mockDisconnect(cdm = cdm)
})
test_that("interval argument works", {
skip_on_cran()
# Load mock database ----
cdm <- mockOmopSketch()
expect_no_error(y<-summariseConceptCounts(list(ANTIHISTAMINES= c(21603444)),
cdm = cdm,
interval = "years"))

expect_no_error(o<-summariseConceptCounts(list(ANTIHISTAMINES= c(21603444)),
cdm = cdm,
interval = "overall"))
expect_no_error(q<-summariseConceptCounts(list(ANTIHISTAMINES= c(21603444)),
cdm = cdm,
interval = "quarters"))
expect_no_error(m<-summariseConceptCounts(list(ANTIHISTAMINES= c(21603444)),
cdm = cdm,
interval = "months"))



m_quarters <- m|>omopgenerics::splitAdditional()|>
omopgenerics::pivotEstimates() |>
dplyr::filter(time_interval != "overall" & variable_name == "Number records" & standard_concept_id == 21603444) |>
dplyr::mutate(
start_date = as.Date(sub(" to .*", "", time_interval)),
quarter_start = lubridate::quarter(start_date, type = "date_first"),
quarter_end = lubridate::quarter(start_date, type = "date_last"),
quarter = paste(quarter_start, "to", quarter_end)
) |>
dplyr::select(!c("time_interval", "start_date", "quarter_start", "quarter_end")) |>
dplyr::group_by(quarter,)|>
dplyr::summarise(count = sum(count), .groups = "drop") |>
dplyr::rename("time_interval" = quarter) |>
dplyr::arrange(time_interval)

q_quarters <- q|>omopgenerics::splitAdditional()|>
omopgenerics::pivotEstimates()|>
dplyr::filter(time_interval != "overall" & variable_name == "Number records"& standard_concept_id == 21603444)|>
dplyr::select(time_interval, count)|>
dplyr::arrange(time_interval)

expect_equal(m_quarters, q_quarters)

m_year <- m|>
omopgenerics::splitAdditional()|>
dplyr::filter(time_interval != "overall" & variable_name == "Number records" & standard_concept_id == 21603444)|>
dplyr::mutate(
# Extract the start date
start_date = clock::date_parse(stringr::str_extract(time_interval, "^\\d{4}-\\d{2}-\\d{2}")),
# Convert start_date to a year-month-day object and extract the year
year = clock::get_year(clock::as_year_month_day(start_date))
)|>
omopgenerics::pivotEstimates()|>
dplyr::group_by(year) %>%
dplyr::summarise(
count = sum(count),
.groups = "drop"
)|>
dplyr::arrange(year)
y_year <- y|>
omopgenerics::splitAdditional()|>
dplyr::filter(time_interval != "overall" & variable_name == "Number records" & standard_concept_id == 21603444)|>
dplyr::mutate(
# Extract the start date
start_date = clock::date_parse(stringr::str_extract(time_interval, "^\\d{4}-\\d{2}-\\d{2}")),
# Convert start_date to a year-month-day object and extract the year
year = clock::get_year(clock::as_year_month_day(start_date))
)|>
omopgenerics::pivotEstimates()|>
dplyr::select(year, count)|>
dplyr::arrange(year)

expect_equal(m_year, y_year)
o <- o |> omopgenerics::splitAdditional()|>
dplyr::filter(variable_name == "Number records" & standard_concept_id == 21603444)|>
omopgenerics::pivotEstimates()|>
dplyr::select(count)

expect_equal(y_year|>dplyr::summarise(count = sum(count)), o)


q_year <- q|>
omopgenerics::splitAdditional()|>
dplyr::filter(time_interval != "overall" & variable_name == "Number records" & standard_concept_id == 21603444)|>
dplyr::mutate(
# Extract the start date
start_date = clock::date_parse(stringr::str_extract(time_interval, "^\\d{4}-\\d{2}-\\d{2}")),
# Convert start_date to a year-month-day object and extract the year
year = clock::get_year(clock::as_year_month_day(start_date))
)|>
omopgenerics::pivotEstimates()|>
dplyr::group_by(year) %>%
dplyr::summarise(
count = sum(count),
.groups = "drop"
)|>
dplyr::arrange(year)

expect_equal(q_year, y_year)
PatientProfiles::mockDisconnect(cdm = cdm)
})

0 comments on commit fa78c7c

Please sign in to comment.