if (run_graphs) {
RD <- function(p1, p2) p2 - p1
RR <- function(p1, p2) p2 / p1
odds <- function(p) p / (1 - p)
OR <- function(p1, p2) odds(p2) / odds(p1)
OR_minus_RR <- function(p1, p2) OR(p2, p1) - RR(p2, p1)
n_ticks <- 201
probs <- seq(.001, .99, length.out = n_ticks)
RD_mat <- outer(probs, probs, RD)
RR_mat <- outer(probs, probs, RR)
OR_mat <- outer(probs, probs, OR)
opacity <- .3
z_min <- -1
z_max <- 5
plotly::plot_ly(
x = ~probs,
y = ~probs
) |>
plotly::add_surface(
z = ~ t(RD_mat),
contours = list(
z = list(
show = TRUE,
start = -1,
end = 1,
size = .1
)
),
name = "Risk Difference",
showscale = FALSE,
opacity = opacity,
colorscale = list(c(0, 1), c("green", "green"))
) |>
plotly::add_surface(
opacity = opacity,
colorscale = list(c(0, 1), c("red", "red")),
z = ~ t(RR_mat),
contours = list(
z = list(
show = TRUE,
start = z_min,
end = z_max,
size = .2
)
),
showscale = FALSE,
name = "Risk Ratio"
) |>
plotly::add_surface(
opacity = opacity,
colorscale = list(c(0, 1), c("blue", "blue")),
z = ~ t(OR_mat),
contours = list(
z = list(
show = TRUE,
start = z_min,
end = z_max,
size = .2
)
),
showscale = FALSE,
name = "Odds Ratio"
) |>
plotly::layout(
scene = list(
xaxis = list(
# type = "log",
title = "reference group probability"
),
yaxis = list(
# type = "log",
title = "comparison group probability"
),
zaxis = list(
# type = "log",
range = c(z_min, z_max),
title = "comparison metric"
),
camera = list(eye = list(x = -1.25, y = -1.25, z = 0.5)),
aspectratio = list(x = .9, y = .8, z = 0.7)
)
)
}