NightingaleHealth / ggforestplot

An R package to create forestplots of measures of effects with their confidence intervals.
MIT License
55 stars 12 forks source link

Confidence Intervals involving Delta-Method Standard Errors #3

Closed marosteg closed 3 years ago

marosteg commented 4 years ago

Is there a way to directly feed the forestplot() function the confidence intervals of an odds ratio, rather than having the function automatically calculate it? I ask because some of the odds ratios I am calculating involve summing multiple coefficients and therefore require the delta-method to calculate the standard error of the odds ratio. As the function is written, it does not seem to be able to handle this situation. I have the delta-method calculated standard errors (and CIs) of the odds ratios but am unsure how to incorporate that into the structure of this function because it takes the standard errors of the coefficient estimates. Your help with this would be greatly appreciated! Many thanks.

mariakalimeri commented 4 years ago

The quick answer is no. But you should, almost, never need to do that. The function calculates the intervals from the standard error (one of the inputs, see se) and a given confidence level (also one of the inputs, default is 95%, see ci). E.g. for a 95% CI the resulting interval will be [beta - 1.959964 SE, beta + 1.959964 SE]. From what you say, you do have the standard errors, so you should be fine. Your data frame must contain a column with it; that column may have whatever name but by default the function expects the name se. If you don't have standard errors, you can calculate them backwards from the CIs you have using the formula with the beta and SE I wrote above; and of course assuming you know what is the level of confidence they correspond to, if not 95% then the constant is different. But if it is you who calculated them, you should get the SE before the CIs, no?

mariakalimeri commented 3 years ago

I recently got this question again from elsewhere and I noticed this issue was never closed. (The truth is I was actually on holidays and away from my computer when I gave the previous answer and I wasn't very comprehensive with it - sorry for the late comeback).

There is actually a way to give the min and max of the estimates. You would have to use, not the forestplot() function but instead, its main constituent geom_effect() and, sort of, reproduce what the wrapper forestplot() does. I copy paste below the example from the documentation here.

library(ggforestplot)
library(ggplot2)
library(magrittr)

df <-
  # Use built-in demo dataset
  df_linear_associations %>%
    # Arrange by name in order to filter the first few biomarkers for more
    # than one studies
    dplyr::arrange(name) %>%
    # Estimate confidence intervals
    dplyr::mutate(
      xmin = beta - qnorm(1 - (1 - 0.95) / 2) * se,
      xmax = beta + qnorm(1 - (1 - 0.95) / 2) * se
    ) %>%
    # Select only first 30 rows (10 biomarkers)
    dplyr::filter(dplyr::row_number() <= 30) %>%
    # Add a logical variable for statistical significance
    dplyr::mutate(filled = pvalue < 0.001)

g <-
  ggplot(data = df, aes(x = beta, y = name)) +
  # And point+errorbars
  geom_effect(
    ggplot2::aes(
      xmin = xmin,
      xmax = xmax,
      colour = trait,
      shape = trait,
      filled = filled
    ),
    position = ggstance::position_dodgev(height = 0.5)
  )

# # At this point, either use this g object or go a step further and add more features, like below
# print(g)

# Add custom theme, horizontal gray rectangles, vertical line to signify the
# NULL point, custom color palettes.
g <-
  g +
  # Add custom theme
  theme_forest() +
  # Add striped background
  geom_stripes() +
  # Add vertical line at null point
  geom_vline(
    xintercept = 0,
    linetype = "solid",
    size = 0.4,
    colour = "black"
  )

print(g)

I hope this helps. I will close the issue now but if this is not what you were looking for feel free to reopen it.