strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
603 stars 91 forks source link

Fix incorrect CIs (error bars) for forest plots in negative binomial GLMs #819

Closed fragla closed 2 years ago

fragla commented 2 years ago

Hi Daniel

I noticed that some of the error bar values plotted in the forest plots for my negative binomial GLMs were different to those using exp(confint(<model>)). It looks like this is due to the hardcoded p.val = "wald" line in the call to tidy_model in plot_type_est.R (so the CIs are calculated using Wald instead of profile).

I've included example code below. This could be fixed by just setting p.val = NULL in plot_type_est.R. However, the submitted pull request allows you to specify the method you want to use from the plot_model function.

Thanks

library(MASS)
library(sjPlot)

set.seed(2)
y <- rnbinom(n = 500, mu = 1.8, size = 0.7266)

group <- sample(c(rep("GroupA",450), rep("GroupB",47), rep("GroupC",3)))

dat <- data.frame(y,group)
m <- glm.nb(y ~ group, dat)

#Profile CIs
round(exp(confint(m)),2)[-1,]

#Wald CIs
round(exp(confint.default(m)),2)[-1,]

#sjPlot CIs
p <- plot_model(m)

round(p$data$conf.low,2)
round(p$data$conf.high,2)
strengejacke commented 2 years ago

Thanks a lot, looks good! I wait for the checks before merging.

strengejacke commented 2 years ago

check failures seem to be unrelated to this PR.