最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Confidence intervals for each grouping factor in glmmTMB model - Stack Overflow

programmeradmin2浏览0评论

I'm treating a data with one continuous variable and one categorical variable. I built a model as following using glmmTMB:

m=glmmTMB(y~x+(1+x|group), family=Gamma(link="log"), data=dat)

'group' is the categorical variable with 20 levels and I want to calculate the confidence interval for each group considering both fixed and random slope. Now I have two questions:

  1. Is it plausible to calculate CIs for each group in a model with random slope?
  2. If it's okay, how can I implement that?

So far I tried confint() but both seemed something is wrong.

confint(m, method="profile")
                        2.5 %      97.5 %
(Intercept)        3.47032911  3.62824116
x                  0.03020511  0.06036813
theta_1+x|group.1 -1.85226484 -1.30972404
theta_1+x|group.2 -3.74515169 -3.04747076
theta_1+x|group.3 -0.14530387  0.83050143

Although group is the variable with 20 levels, only 3 is showed here and their names are dropped.And I also tried parametric bootstrapping with chatGPT's help.

for(i in 1:nboot){

  sim_data <- simulate(m)[[1]]
  dat_boot <- m$frame
  dat_boot$y <- sim_data

  m_boot <- try(glmmTMB(y ~ x + (1+x|group),
                         family=Gamma(link="log"), data=dat_boot), silent=TRUE)
  if(!inherits(m_boot, "try-error")){
    fix_boot <- fixef(m_boot)$cond["x"]
    ranef_boot <- ranef(m_boot)$cond$plot[,"x"]
    boot_slopes[i, ] <- fix_boot + ranef_boot
  }
}

ci <- apply(boot_slopes, 2, function(x){
  quantile(x, probs = c(0.025, 0.975), na.rm=TRUE)
})
print(ci)

I checked the result but I highly suspect something is wrong, especially the grouping structure is lost because the CIs are too homogenic/similar among groups(seeing my row data, I believe they shouldn't).

I don't cling to glmmTMB, but fitting is somehow better with this than lme4.

I'm treating a data with one continuous variable and one categorical variable. I built a model as following using glmmTMB:

m=glmmTMB(y~x+(1+x|group), family=Gamma(link="log"), data=dat)

'group' is the categorical variable with 20 levels and I want to calculate the confidence interval for each group considering both fixed and random slope. Now I have two questions:

  1. Is it plausible to calculate CIs for each group in a model with random slope?
  2. If it's okay, how can I implement that?

So far I tried confint() but both seemed something is wrong.

confint(m, method="profile")
                        2.5 %      97.5 %
(Intercept)        3.47032911  3.62824116
x                  0.03020511  0.06036813
theta_1+x|group.1 -1.85226484 -1.30972404
theta_1+x|group.2 -3.74515169 -3.04747076
theta_1+x|group.3 -0.14530387  0.83050143

Although group is the variable with 20 levels, only 3 is showed here and their names are dropped.And I also tried parametric bootstrapping with chatGPT's help.

for(i in 1:nboot){

  sim_data <- simulate(m)[[1]]
  dat_boot <- m$frame
  dat_boot$y <- sim_data

  m_boot <- try(glmmTMB(y ~ x + (1+x|group),
                         family=Gamma(link="log"), data=dat_boot), silent=TRUE)
  if(!inherits(m_boot, "try-error")){
    fix_boot <- fixef(m_boot)$cond["x"]
    ranef_boot <- ranef(m_boot)$cond$plot[,"x"]
    boot_slopes[i, ] <- fix_boot + ranef_boot
  }
}

ci <- apply(boot_slopes, 2, function(x){
  quantile(x, probs = c(0.025, 0.975), na.rm=TRUE)
})
print(ci)

I checked the result but I highly suspect something is wrong, especially the grouping structure is lost because the CIs are too homogenic/similar among groups(seeing my row data, I believe they shouldn't).

I don't cling to glmmTMB, but fitting is somehow better with this than lme4.

Share Improve this question asked Mar 28 at 11:40 Y.Y.Y.Y. 31 bronze badge 5
  • 2 It really sounds like you want group as a fixed effect rather than random. – PBulls Commented Mar 28 at 11:53
  • @PBulls Thank you for answering. So, if I’m interested in the slope of each group, it’s better to move it to fixed effect and do like y~ x*group?I’m not quite familiar with these problems, but I thought 20 levels are too much for fixed effect, and it generated convergence problem when I tried. – Y.Y. Commented Mar 28 at 12:12
  • maybe useful ; github/glmmTMB/glmmTMB/issues/1096 – user20650 Commented Mar 28 at 12:58
  • 20 levels may well be too many to model as a fixed effect, depending on your overall and per-group sample size. But adding random slopes won’t solve that problem. – zephryl Commented Mar 28 at 13:18
  • 1 Also, your model includes random slopes for x — if x is continuous, this doesn’t make much sense. – zephryl Commented Mar 28 at 13:20
Add a comment  | 

1 Answer 1

Reset to default 1

There is both a statistical question and a computational question here.

The statistical question is "can I get confidence intervals on expressions involving a random effect?" The answer is, "it depends whether you're a frequentist or not, and how rigorous you want to be". According to a frequentist, random effects are not parameters, so they (and expressions involving them) don't have confidence intervals. However, we can get prediction intervals, or intervals combining the sampling distribution of the parameters with the conditional distributions of the random effects ...

There is an implementation for lme4 here and for glmmTMB here (I realize this violates the "link-only answer" rule, I will try to come back later and copy the content here ...)

发布评论

评论列表(0)

  1. 暂无评论