I'm trying to reproduce a graph from Clause Willke's data visualization guide (https://clauswilke.com/dataviz/visualizing-uncertainty.html#fig:cocoa-data-vs-CI), and the code he wrote is choking for me.
I apologize in advance if this is long, but trying to explain what I think is going on. (Also, you can find the cacao
data at https://github.com/clauswilke/dviz.supp/blob/master/data/cacao.rda.)
Full code chunk:
library(forcats)
library(lubridate)
library(mgcv)
library(tidyr)
library(purrr)
library(broom)
library(emmeans)
library(ungeviz)
library(ggridges)
library(tidybayes)
cacao <- load(file=url("https://github.com/clauswilke/dviz.supp/blob/master/data/cacao.rda"))
cacao %>%
filter(location == "Canada") -> cacao_single
fit <- lm(rating ~ 1, data = cacao_single)
CI_df <- data.frame(type = c(0.8, 0.95, 0.99)) %>%
mutate(df = map(type, ~tidy(emmeans(fit, ~ 1, options = list(level = .x) )))) %>%
unnest(df) %>% select(type, estimate, std.error, conf.low, conf.high) %>%
mutate(type = paste0(signif(100*type, 2), "% confidence interval")) %>%
select(type, estimate, std.error, conf.low, conf.high) %>%
mutate(type = paste0(signif(100*type, 2), "% confidence interval"))
R returns: Error: Can't subset columns that don't exist. x Column `conf.low` doesn't exist.
I've isolated some of this line-by-line, and the "problem" seems to be with the interaction of emmeans
and mutate
. When I use the code above, the returned object lacks the CIs; when I surround emmeans
with a summary()
, I get the CIs, but then I lack the SEs.