# NOT RUN {
# strip ordering in factors (currently ordered factor not supported)
library(dplyr)
library(broom)
m0 <- esoph %>%
mutate_if(is.factor, ~factor(., ordered = FALSE)) %>%
glm(cbind(ncases, ncontrols) ~ agegp + tobgp * alcgp, data = .,
family = binomial())
# tidy
tidy(m0)
# add further columns to tidy output to help manage categorical variables
m0 %>%
tidy() %>%
tidy_categorical(m = m0, include_reference = FALSE)
# include reference categories and column to indicate the additional terms
m0 %>%
tidy() %>%
tidy_categorical(m = m0)
# coefficient plots
d0 <- m0 %>%
tidy(conf.int = TRUE) %>%
tidy_categorical(m = m0) %>%
# drop the intercept term
slice(-1)
d0
# typical coefficient plot
library(ggplot2)
library(tidyr)
ggplot(data = d0 %>% drop_na(),
mapping = aes(x = term, y = estimate,
ymin = conf.low, ymax = conf.high)) +
coord_flip() +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_pointrange()
# enhanced coefficient plot using additional columns from tidy_categorical and ggforce::facet_row()
library(ggforce)
ggplot(data = d0,
mapping = aes(x = level, colour = reference,
y = estimate, ymin = conf.low, ymax = conf.high)) +
facet_row(facets = vars(variable), scales = "free_x", space = "free") +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_pointrange() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# }
Run the code above in your browser using DataLab