Skip to content

Commit

Permalink
Merge pull request #262 from Gilead-BioStats/fix-221
Browse files Browse the repository at this point in the history
fixes #221 - qc updates for Analyze_Chisq()
  • Loading branch information
mattroumaya authored Mar 11, 2022
2 parents 1c149b0 + 42a918a commit 8d886f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ importFrom(lamW,lambertW0)
importFrom(lamW,lambertWm1)
importFrom(magrittr,"%>%")
importFrom(purrr,map)
importFrom(purrr,map_df)
importFrom(stats,as.formula)
importFrom(stats,chisq.test)
importFrom(stats,fisher.test)
Expand Down
29 changes: 17 additions & 12 deletions R/Analyze_Chisq.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Creates Analysis results data for count data using the chi-squared test
#'
#' @details
#' @details
#'
#' Analyzes count data using the chi-squared test
#'
Expand All @@ -12,17 +12,19 @@
#'
#' @section Data Specification:
#'
#' The input data (` dfTransformed`) for the Analyze_Chisq is typically created using \code{\link{Transform_EventCount}} and should be one record per Site with columns for:
#' The input data (`dfTransformed`) for Analyze_Chisq is typically created using \code{\link{Transform_EventCount}} and should be one record per site with required columns for:
#' - `SiteID` - Site ID
#' - `N` - Total number of participants at site
#' - `Count` - Total number of participants at site with event of interest
#' - `TotalCount` - Total number of participants at site with event of interest
#'
#'
#' @param dfTransformed data.frame in format produced by \code{\link{Transform_EventCount}}
#' @param strOutcome required, name of column in dfTransformed dataset to perform the chi-squared test on
#' @param strOutcome required, name of column in dfTransformed dataset to perform the chi-squared test on. Default is "TotalCount".
#'
#' @importFrom stats chisq.test as.formula
#' @importFrom purrr map map_df
#' @import dplyr
#' @importFrom tidyr unnest
#' @importFrom stats chisq.test
#' @importFrom purrr map
#' @importFrom broom glance
#'
#' @return data.frame with one row per site, columns: SiteID, TotalCount, TotalCount_Other, N, N_Other, Prop, Prop_Other, Statistic, PValue
Expand All @@ -37,8 +39,11 @@
Analyze_Chisq <- function( dfTransformed , strOutcome = "TotalCount") {

stopifnot(
is.data.frame(dfTransformed),
all(c("SiteID", "N", strOutcome) %in% names(dfTransformed))
"dfTransformed is not a data.frame" = is.data.frame(dfTransformed),
"One or more of these columns: SiteID, N, or the value in strOutcome not found in dfTransformed" = all(c("SiteID", "N", strOutcome) %in% names(dfTransformed)),
"NA value(s) found in SiteID" = all(!is.na(dfTransformed[["SiteID"]])),
"strOutcome must be length 1" = length(strOutcome) == 1,
"strOutcome is not character" = is.character(strOutcome)
)

chisq_model<- function(site){
Expand All @@ -51,13 +56,13 @@ Analyze_Chisq <- function( dfTransformed , strOutcome = "TotalCount") {
) %>%
select(.data$Flag, .data$NoFlag)

chisq.test(SiteTable)
stats::chisq.test(SiteTable)
}

dfAnalyzed <- dfTransformed %>%
mutate(model = map(.data$SiteID, chisq_model)) %>%
mutate(summary = map(.data$model, broom::glance)) %>%
unnest(summary) %>%
mutate(model = purrr::map(.data$SiteID, chisq_model)) %>%
mutate(summary = purrr::map(.data$model, broom::glance)) %>%
tidyr::unnest(summary) %>%
rename(
Statistic = .data$statistic,
PValue = .data[['p.value']]
Expand Down
8 changes: 3 additions & 5 deletions man/Analyze_Chisq.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d886f0

Please sign in to comment.