Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Imports:
Remotes:
github::PHSKC-APDE/rads, github::PHSKC-APDE/dtsurvey
Suggests:
devtools,
httr,
keyring,
knitr,
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(chi_get_proper_pop)
export(chi_get_yaml)
export(chi_keep_proper_ages)
export(chi_qa_tro)
export(chi_suppress_results)
export(chi_update_sql)
import(data.table)
import(dtsurvey)
Expand All @@ -30,10 +31,12 @@ importFrom(DBI,dbGetQuery)
importFrom(DBI,dbWriteTable)
importFrom(data.table,"%between%")
importFrom(data.table,":=")
importFrom(data.table,':=')
importFrom(data.table,.GRP)
importFrom(data.table,.SD)
importFrom(data.table,CJ)
importFrom(data.table,`:=`)
importFrom(data.table,alloc.col)
importFrom(data.table,as.data.table)
importFrom(data.table,between)
importFrom(data.table,copy)
Expand All @@ -46,18 +49,23 @@ importFrom(data.table,rbindlist)
importFrom(data.table,set)
importFrom(data.table,setDT)
importFrom(data.table,setcolorder)
importFrom(data.table,setkey)
importFrom(data.table,setnames)
importFrom(data.table,setorder)
importFrom(data.table,setorderv)
importFrom(data.table,tstrsplit)
importFrom(data.table,uniqueN)
importFrom(future,plan)
importFrom(future.apply,future_lapply)
importFrom(glue,glue)
importFrom(glue,glue_sql)
importFrom(odbc,odbc)
importFrom(progressr,handler_progress)
importFrom(progressr,handlers)
importFrom(progressr,progressor)
importFrom(progressr,with_progress)
importFrom(qs,qread)
importFrom(qs,qsave)
importFrom(rads,calc)
importFrom(rads,round2)
importFrom(rads,string_clean)
Expand Down
33 changes: 25 additions & 8 deletions R/chi_calc.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,32 @@ chi_calc <- function(ph.data = NULL,


# Tidy results ----
# When there are no observation in a row, rads::calc() gives NA for numerator, but want zero ----
tempCHIest[is.na(numerator), `:=` (
# When we have no events, but a valid denominator ----
tempCHIest[
(is.na(numerator) | numerator == 0) & !is.na(denominator) & denominator > 0,
`:=` (
result = 0,
se = 0,
lower_bound = 0,
upper_bound = 0,
numerator = 0,
numerator = ifelse(is.na(numerator), 0, numerator),
rse = NA # undefined because mean will be zero
)]
)
]

# When we have no events in the denominator ----
tempCHIest[
(is.na(denominator) | denominator == 0),
`:=` (
result = NA,
se = NA,
lower_bound = NA,
upper_bound = NA,
numerator = 0, # This might not be necessary depending on your needs
denominator = 0, # Setting NA denominator to 0
rse = NA
)
]

# drop when cat1_group is missing (e.g., cat1 == 'Regions' and region is NA) ----
tempCHIest <- tempCHIest[!is.na(cat1_group)]
Expand Down Expand Up @@ -367,10 +384,10 @@ chi_calc <- function(ph.data = NULL,
tempCHIest[, data_source := source_name]

if(small_num_suppress == TRUE){
tempCHIest <- rads::suppress(sup_data = tempCHIest,
suppress_range = c(suppress_low, suppress_high),
secondary = T,
secondary_exclude = cat1_varname != 'race3')
tempCHIest <- apde.chi.tools::chi_suppress_results(ph.data = tempCHIest,
suppress_range = c(suppress_low, suppress_high),
secondary = T,
secondary_exclude = cat1_varname != 'race3')
} else {tempCHIest[, suppression := NA_character_]}

tempCHIest[rse>=30 | numerator == 0, caution := "!"]
Expand Down
16 changes: 8 additions & 8 deletions R/chi_compare_estimates.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ chi_compare_estimates <- function(OLD = NULL, NEW = NULL, OLD.year = NULL, NEW.y

# calculate percent differences between old (x) and new(y)
comp[, relative.diff := round2(abs((result.x - result.y) / result.x)*100, 1)]
comp[result_type != "rate", absolute.diff := round2(abs(result.x - result.y)*100, 1)]
comp[result_type == "rate", absolute.diff := round2(abs(result.x - result.y), 1)]
comp[grepl("mean|proportion", result_type, ignore.case = T), absolute.diff := round2(abs(result.x - result.y)*100, 1)]
comp[grepl("rate", result_type, ignore.case = T), absolute.diff := round2(abs(result.x - result.y), 1)]
comp <- comp[!is.na(absolute.diff)] # drop if absolute difference is NA

# order variables
Expand All @@ -111,18 +111,18 @@ chi_compare_estimates <- function(OLD = NULL, NEW = NULL, OLD.year = NULL, NEW.y
# First, identify whether to use absolute or relative change for each indicator_key
qa_type = NEW[tab=='_kingcounty']
qa_type[, qa_type := 'relative']
qa_type[result_type=='proportion' & result >= 0.05, qa_type := 'absolute']
qa_type[result_type=='rate' & result >= 5, qa_type := 'absolute']
qa_type[grepl('mean|proportion', result_type, ignore.case = T) & result >= 0.05, qa_type := 'absolute']
qa_type[grepl('rate', result_type, ignore.case = T) & result >= 5, qa_type := 'absolute']
qa_type <- qa_type[, list(indicator_key, qa_type)]

# Merge on qa_type
comp <- merge(comp, qa_type, by = 'indicator_key', all = T)

# Identify notable changes
comp[qa_type == 'absolute' & result_type=='proportion' & absolute.diff >= 3, notable := 1] # absolute difference was multiplied by 100, so assess with >= 3
comp[qa_type == 'absolute' & result_type=='rate' & absolute.diff >= 3, notable := 1]
comp[qa_type == 'relative' & result_type=='proportion' & relative.diff >= 50, notable := 1] # absolute difference was multiplied by 100, so assess with >= 3
comp[qa_type == 'relative' & result_type=='rate' & relative.diff >= 50, notable := 1]
comp[qa_type == 'absolute' & grepl('mean|proportion', result_type, ignore.case = T) & absolute.diff >= 3, notable := 1] # absolute difference was multiplied by 100, so assess with >= 3
comp[qa_type == 'absolute' & grepl('rate', result_type, ignore.case = T) & absolute.diff >= 3, notable := 1]
comp[qa_type == 'relative' & grepl('mean|proportion', result_type, ignore.case = T) & relative.diff >= 50, notable := 1] # absolute difference was multiplied by 100, so assess with >= 3
comp[qa_type == 'relative' & grepl('rate', result_type, ignore.case = T) & relative.diff >= 50, notable := 1]

# Return object ----
return(comp)
Expand Down
Loading