1

I am trying to put confidence intervals into a tibble so I can graph them; however, I keep getting an error message saying an incorrect number of dimensions. I have pasted my model, along with the code I am using to attempt to extract the upper and lower confidence limits. Please let me know to get around this.

mod6 <- glmer(count.inv ~ mon.sst.levels + (1 | Year), family = poisson(link = "log"), data = model_set)

tib <-  tibble(
  levels = names(exp(fixef(mod6b))),
  coef = exp((fixef(mod6b))), 
  lower = exp((fixef(mod6b)))[,1], 
  upper = exp((fixef(mod6b)))[,2])

Error in exp((fixef(mod6b)))[, 1] : incorrect number of dimensions

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Sean
  • 19
  • 1
  • 2
  • If you could provide a reproducible example, that would help with determining why a model with your data resulted in an "incorrect number of dimensions" error – MBorg May 11 '21 at 13:49
  • @MBorg in general it's best practice to provide a [mcve], but in this case the problem is pretty clear by inspection if you know what you're looking at. `fixef(mod6b)` is a vector, so `exp(fixef(mod6b))` is also a vector, so trying to refer to its first column (via `[,1]`) throws an error ... – Ben Bolker May 11 '21 at 14:13

1 Answers1

0

How about using broom.mixed::tidy()? (hint, fixef() returns a vector, not an object with multiple columns: how did you get the idea for the code shown in your example?)

library(lme4)
library(broom.mixed)
library(dplyr)
## built-in example from `?glmer`
m1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
                  family = binomial, data = cbpp)
tidy(m1, effects="fixed", conf.int=TRUE, conf.method="profile",
     exponentiate=TRUE) %>% select(term, estimate, conf.low, conf.high)

? Results:

  term        estimate conf.low conf.high
  <chr>          <dbl>    <dbl>     <dbl>
1 (Intercept)    0.247   0.149      0.388
2 period2        0.371   0.199      0.665
3 period3        0.324   0.165      0.600
4 period4        0.206   0.0820     0.449

If you leave out conf.method="profile" you'll get faster, but less accurate, Wald confidence intervals.

You may also be interested in the dotwhisker package.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453