Thanks to visit codestin.com
Credit goes to github.com

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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Imports:
lubridate,
dataRetrieval,
ggplot2,
sf
sf,
maps
Depends:
R (>= 3.5.0)
Suggests:
Expand Down
24 changes: 12 additions & 12 deletions R/CensoredDataSuite.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param od_method A text string indicating the type of method used to populate an over-detect (upper limit) data value. Can be set to "multiplier" or "as-is" (default).
#' @param od_multiplier A number to be multiplied to the UPPER detection limit for each entry to obtain the censored data value. Must be supplied if od_method = "multiplier". Defaults to 0.5, or half the detection limit.
#'
#' @return A TADA dataframe with an additional column named TADA.Censored_Method, which documents the method used to fill censored data values.
#' @return A TADA dataframe with additional columns named TADA.CensoredData.Flag, which indicates if there are disagreements in ResultDetectionCondition and DetectionQuantitationLimitTypeName, and TADA.CensoredMethod, which documents the method used to fill censored data values.
#'
#'
#' @export
Expand All @@ -35,7 +35,7 @@ simpleCensoredMethods <- function(.data, nd_method = "multiplier", nd_multiplier
}

## First step: identify censored data
cens = .data%>%dplyr::filter(TADA.ResultMeasureValue.DataTypeFlag=="Result Value/Unit Copied from Detection Limit")
cens = .data%>%dplyr::filter(TADA.ResultMeasureValueDataTypes.Flag=="Result Value/Unit Copied from Detection Limit")
not_cens = .data%>%dplyr::filter(!ResultIdentifier%in%cens$ResultIdentifier)

## Bring in det cond reference table
Expand All @@ -51,50 +51,50 @@ simpleCensoredMethods <- function(.data, nd_method = "multiplier", nd_multiplier
cens = dplyr::left_join(cens, limtype.ref)

## Create flag for condition and limit type combinations
cens = cens%>%dplyr::mutate(TADA.Censored_Flag = dplyr::case_when(
cens = cens%>%dplyr::mutate(TADA.CensoredData.Flag = dplyr::case_when(
TADA.Detection_Type=="Non-Detect"&TADA.Limit_Type=="Non-Detect" ~ as.character("Non-Detect"),
TADA.Detection_Type=="Over-Detect"&TADA.Limit_Type=="Over-Detect" ~ as.character("Over-Detect"),
TADA.Detection_Type=="Other"&TADA.Limit_Type=="Other" ~ as.character("Other Condition/Limit Populated"),
!TADA.Detection_Type==TADA.Limit_Type ~ as.character("Conflict between Condition and Limit")
))

## warn when some limit metadata may be problematic
if("Conflict between Condition and Limit"%in%cens$TADA.Censored_Flag){
num = length(cens$TADA.Censored_Flag[cens$TADA.Censored_Flag=="Conflict between Condition and Limit"])
if("Conflict between Condition and Limit"%in%cens$TADA.CensoredData.Flag){
num = length(cens$TADA.CensoredData.Flag[cens$TADA.CensoredData.Flag=="Conflict between Condition and Limit"])
warning(paste0(num," records in supplied dataset have conflicting detection condition and detection limit type information. These records will not be included in detection limit handling calculations."))
}

cens = cens%>%dplyr::select(-TADA.Detection_Type, -TADA.Limit_Type)

# split out over detects and non detects
nd = subset(cens, cens$TADA.Censored_Flag=="Non-Detect")
od = subset(cens, cens$TADA.Censored_Flag=="Over-Detect")
nd = subset(cens, cens$TADA.CensoredData.Flag=="Non-Detect")
od = subset(cens, cens$TADA.CensoredData.Flag=="Over-Detect")
other = subset(cens, !cens$ResultIdentifier%in%c(nd$ResultIdentifier,od$ResultIdentifier))

# ND handling
if(dim(nd)[1]>0){
if(nd_method=="multiplier"){
nd$TADA.ResultMeasureValue = nd$TADA.ResultMeasureValue*nd_multiplier
nd$TADA.Censored_Method = paste0("Detection Limit Value Multiplied by ",nd_multiplier)
nd$TADA.CensoredMethod = paste0("Detection Limit Value Multiplied by ",nd_multiplier)
}
if(nd_method=="randombelowlimit"){
nd$multiplier = stats::runif(dim(nd)[1],0,1)
nd$TADA.ResultMeasureValue = nd$TADA.ResultMeasureValue*nd$multiplier
nd$TADA.Censored_Method = paste0("Random Value Between 0 and Detection Limit Using this Multiplier: ",round(nd$multiplier,digits=3))
nd$TADA.CensoredMethod = paste0("Random Value Between 0 and Detection Limit Using this Multiplier: ",round(nd$multiplier,digits=3))
nd = nd%>%dplyr::select(-multiplier)
}
if(nd_method=="as-is"){
nd$TADA.Censored_Method = "Detection Limit Value Unchanged"
nd$TADA.CensoredMethod = "Detection Limit Value Unchanged"
}
}
# OD handling
if(dim(od)[1]>0){
if(od_method=="multiplier"){
od$TADA.ResultMeasureValue = od$TADA.ResultMeasureValue*od_multiplier
od$TADA.Censored_Method = paste0("Detection Limit Value Multiplied by ",od_multiplier)
od$TADA.CensoredMethod = paste0("Detection Limit Value Multiplied by ",od_multiplier)
}
if(od_method=="as-is"){
od$TADA.Censored_Method = "Detection Limit Value Unchanged"
od$TADA.CensoredMethod = "Detection Limit Value Unchanged"
}
}

Expand Down
4 changes: 2 additions & 2 deletions R/Filtering.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ FilterFields <- function(.data) {
col.names <- col.names[, c(2, 1)]
# Filter dataframe to include only fields for filtering
col.names <- dplyr::filter(col.names, FieldName %in% c(
"ActivityTypeCode", "ActivityMediaName",
"ActivityTypeCode", "TADA.ActivityMediaName",
"ActivityMediaSubdivisionName",
"ActivityCommentText", "MonitoringLocationTypeName",
"StateName", "TribalLandName",
Expand Down Expand Up @@ -71,7 +71,7 @@ FilterFields <- function(.data) {

# Filter list to include only fields for filtering
UniqueValList <- UniqueValList[c(
"ActivityTypeCode", "ActivityMediaName",
"ActivityTypeCode", "TADA.ActivityMediaName",
"ActivityMediaSubdivisionName",
"ActivityCommentText", "MonitoringLocationTypeName",
"StateName", "TribalLandName",
Expand Down
Loading