Skip to content

Commit

Permalink
Update tkonfound_fig.R
Browse files Browse the repository at this point in the history
  • Loading branch information
wwang93 authored Feb 18, 2024
1 parent 780921b commit fa4a05c
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions R/tkonfound_fig.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
#'
#' @return Returns two plots showing the effect of hypothetical case switches
#' on the effect size in a 2x2 table.
tkonfound_fig <- function(a, b, c, d, thr_p = 0.05, switch_trm = TRUE, test = "fisher", replace = "control"){
tkonfound_fig <- function(a, b, c, d, thr_p = 0.05, switch_trm = TRUE,
test = "fisher", replace = "control"){

n_obs <- a + b + c + d
###***generate the log odds for each step of switch
meta <- data.frame(matrix(ncol = 10, nrow = n_obs-3))
colnames(meta) <- c("a", "b", "c", "d", "nobs", "switch", "logodds","cntrl_p","tr_p","pdif")
colnames(meta) <- c("a", "b", "c", "d", "nobs", "switch",
"logodds","cntrl_p","tr_p","pdif")
if (switch_trm == TRUE) {
for (i in 1:(n_obs-3)){
if (i <= a){
Expand Down Expand Up @@ -123,7 +125,8 @@ if (test == "chisq"){
if (test == "fisher"){
solution <- getswitch_fisher(a, b, c, d, thr_p, switch_trm)
for (i in 1:(n_obs-3)){
meta$logodds[i] <- log(fisher_oddsratio(meta$a[i], meta$b[i], meta$c[i], meta$d[i]))
meta$logodds[i] <- log(fisher_oddsratio(meta$a[i], meta$b[i],
meta$c[i], meta$d[i]))
}
}

Expand Down Expand Up @@ -272,24 +275,32 @@ if (!switch_trm && !dcroddsratio_ob) {
}

zoom$label <- ifelse(zoom$sigpoint=="positive",
paste("sig pos:RIR=", zoom[zoom$sigpoint=="positive",]$RIR),NA)
paste("sig pos:RIR=",
zoom[zoom$sigpoint=="positive",]$RIR),
NA)
zoom$label <- ifelse(zoom$sigpoint=="negative",
paste("sig neg:RIR=", zoom[zoom$sigpoint=="negative",]$RIR),zoom$label)
paste("sig neg:RIR=",
zoom[zoom$sigpoint=="negative",]$RIR),
zoom$label)
zoom$label <- ifelse(zoom$sigpoint=="current",
paste("current"),zoom$label)

fig2 <- ggplot2::ggplot(zoom, ggplot2::aes_string(x="RIR",y="pdif"))+
ggplot2::geom_line(ggplot2::aes_string(y="pdif"), size = 1) +
ggplot2::geom_point(ggplot2::aes_string(y="pdif", shape = "current",fill = "sigpoint"),
ggplot2::geom_point(ggplot2::aes_string(y="pdif",
shape = "current",
fill = "sigpoint"),
size = 1)+
ggrepel::geom_label_repel(ggplot2::aes_string(label="label"))+
ggplot2::scale_fill_manual(values=fillcol)+
ggplot2::scale_shape_manual(values=pointshape)+
ggplot2::scale_y_continuous(name="Difference in probability of successful outcome (treatment - control)")+
ggplot2::scale_x_continuous(name="RIR (Fragility)",
breaks= c(zoom$RIR[1], zoom$RIR[as.integer(length(zoom$RIR)/2)],
breaks= c(zoom$RIR[1],
zoom$RIR[as.integer(length(zoom$RIR)/2)],
zoom$RIR[as.integer(length(zoom$RIR))]),
labels= c(zoom$xaxis[1], zoom$xaxis[as.integer(length(zoom$RIR)/2)],
labels= c(zoom$xaxis[1],
zoom$xaxis[as.integer(length(zoom$RIR)/2)],
zoom$xaxis[as.integer(length(zoom$RIR))])) +
ggplot2::theme(panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
Expand All @@ -298,11 +309,13 @@ fig2 <- ggplot2::ggplot(zoom, ggplot2::aes_string(x="RIR",y="pdif"))+
legend.position = "none")

if (pos_thr_pdif <= max(zoom$pdif) && pos_thr_pdif >= min(zoom$pdif)) {
fig2 <- fig2 + ggplot2::geom_hline(yintercept = pos_thr_pdif, linetype = "dashed", color="green4", size = 1)
fig2 <- fig2 + ggplot2::geom_hline(yintercept = pos_thr_pdif,
linetype = "dashed", color="green4", size = 1)
}

if (neg_thr_pdif <= max(zoom$pdif) && neg_thr_pdif >= min(zoom$pdif)) {
fig2 <- fig2 + ggplot2::geom_hline(yintercept = neg_thr_pdif, linetype = "dashed", color="red", size = 1)
fig2 <- fig2 + ggplot2::geom_hline(yintercept = neg_thr_pdif,
linetype = "dashed", color="red", size = 1)

Check warning on line 318 in R/tkonfound_fig.R

View check run for this annotation

Codecov / codecov/patch

R/tkonfound_fig.R#L317-L318

Added lines #L317 - L318 were not covered by tests
}

###plot figure 3 RIS% as sample size gets larger, using t statistic as the criterion
Expand Down Expand Up @@ -331,9 +344,11 @@ if (neg_thr_pdif <= max(zoom$pdif) && neg_thr_pdif >= min(zoom$pdif)) {
#}

if (switch_trm == TRUE) {
note <- "A bend in line indicates switches from the control row because the treatment row was exhausted."
note <- "A bend in line indicates switches from the control
row because the treatment row was exhausted."
} else {
note <- "A bend in line indicates switches from the treatment row because the control row was exhausted."
note <- "A bend in line indicates switches from the treatment row
because the control row was exhausted."
}

result <- list(fig1, note, fig2)
Expand Down

0 comments on commit fa4a05c

Please sign in to comment.