Skip to content

Commit

Permalink
update for WINratio,
Browse files Browse the repository at this point in the history
added fct flex2rmd, conditional on interactive
  • Loading branch information
abusjahn committed Jan 28, 2024
1 parent 3a4bdbd commit f9f6474
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Description: The main functionalities of 'wrappedtools' are:
descriptive statistics and p-values; creating specialized plots for
correlation matrices. Functions were mainly written for my own daily work or
teaching, but may be of use to others as well.
Version: 0.9.4
Version: 0.9.4.2
Date: 2023-12-23
Authors@R: c(
person(given = "Andreas",
Expand Down Expand Up @@ -45,7 +45,8 @@ Imports:
lifecycle,
broom,
rlist,
DescTools
DescTools,
flextable
Depends:
R (>= 4.2)
Roxygen: list(markdown = TRUE)
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(compare_n_numvars)
export(compare_n_qualvars)
export(cortestR)
export(detect_outliers)
export(flex2rmd)
export(formatP)
export(ggcormat)
export(glmCI)
Expand Down Expand Up @@ -52,6 +53,7 @@ import(stringr)
import(tibble)
importFrom(DescTools,BinomRatioCI)
importFrom(broom,tidy)
importFrom(flextable,flextable_to_rmd)
importFrom(forcats,fct_drop)
importFrom(forcats,fct_lump_n)
importFrom(grDevices,boxplot.stats)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- new function label_outliers to extend boxplots
- new function detect_outliers for compatibility with geom_boxplot
- new function WINratio to calculate the ratio of wins to losses in clinical trials
- new function flex2rmd to convert flextable to rmarkdown table if non-interactive

# wrappedtools 0.9.3
- compare2numvars checks indep. variables for 2 levels
Expand Down
17 changes: 17 additions & 0 deletions R/basefunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,20 @@ tab.search <- function(searchdata = rawdata, pattern,
surprisal <- function(p, precision = 1){
round(-log2(as.numeric(p)),precision) |> as.character()
}


#' Transform flextable to rmd if non-interactive
#'
#' \code{flex2rmd} takes a flextable and returns a markdown table if not in an interactive session
#'
#' @param ft a flextable
#'
#' @return either a markdown table or the flextable
#' @export
flex2rmd <- function(ft){
if(interactive()){
return(ft)
} else {
return(flextable_to_rmd(ft))
}
}
3 changes: 3 additions & 0 deletions R/pkgstart.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ NULL

#' @importFrom DescTools BinomRatioCI
NULL

#' @importFrom flextable flextable_to_rmd
NULL
23 changes: 15 additions & 8 deletions R/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ utils::globalVariables('p_wcox_t_out')
#'
#' WINratio=vector with WINratio and CIs,
#'
#' WINodds=odds ratio of wins and losses, taking ties into account,
#'
#' p.value=p.value from prop.test,
#'
#' WINratioCI=character with merged WINratio, CI, and p
Expand Down Expand Up @@ -1261,26 +1263,31 @@ WINratio <- function(data,groupvar,testvars,rules, idvar=NULL,
.default=.x))) |>
mutate(rule=c(paste(testvars,rules, sep=": "),"all"))

pT <- WINners |>
p_w <- WINners |>
filter(rule=="all") |>
pull("Wins")# / nrow(testdata)
p_l <- WINners |>
filter(rule=="all") |>
pull(Wins)# / nrow(testdata)
pC <- WINners |>
pull("Losses")# / nrow(testdata)
p_t <- WINners |>
filter(rule=="all") |>
pull(Losses)# / nrow(testdata)
pull("Ties")# / nrow(testdata)
# WINratio=pT/pC
p.value <- prop.test(pT,pT+pC)$p.value
WINratio <- DescTools::BinomRatioCI(pT,nrow(testdata),
pC,nrow(testdata),
p.value <- prop.test(p_w,p_w+p_l)$p.value
WINratio <- DescTools::BinomRatioCI(p_w,nrow(testdata),
p_l,nrow(testdata),
method = 'katz') |>
roundR(3)
WINodds <- (p_w+p_t*.5) / (p_l+p_t*.5)
WINratioCI <- paste0(roundR(WINratio[1],3)," (",
roundR(WINratio[2],3),"/",
roundR(WINratio[3],3),")",
" p ",formatP(p.value, ndigits=p_digits, pretext=TRUE))
return(list(WIN=WINners,
WINratio=WINratio,
WINodds=WINodds,
p.value=p.value,
WINratioCI=WINratioCI,
testdata=testdata))
}
utils::globalVariables(c('outcome',"Wins","Losses","rule"))
utils::globalVariables(c('outcome',"rule"))
2 changes: 2 additions & 0 deletions man/WINratio.Rd

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

17 changes: 17 additions & 0 deletions man/flex2rmd.Rd

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

0 comments on commit f9f6474

Please sign in to comment.