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

r - How to extract Std.Dev from VarCorr glmmTMB - Stack Overflow

programmeradmin2浏览0评论

I'm using the emmeans package with a negative-binomial model implemented using the glmmTMB package. I'm trying to bias adjust my backtransformed emmeans per the workflow illustrated here: .html#bias-adj

As I've understood, I need the bias adjustment to be based on the variance of the random effects which can be obtained with VarCorr(nbinom_mod)

The problem is, I can't figure out how to extract that Std.Dev. value

library(glmmTMB)
library(emmeans)
library(dplyr)

data(cbpp, package="lme4")

nbinom_mod <- glmmTMB(incidence ~ period + (1|herd), 
                data = cbpp,
                family = nbinom2)

# Here's what I want to do but it fails since VarCorr doesn't return a number, it returns an object
nbinom_em <- emmeans(nbinom_mod, ~ period, 
               bias.adjust = T,
               sigma = VarCorr(nbinom_mod),
               type = "response")

So I've tried to extract data from VarCorr(nbinom_mod)and failed as such:

> class(VarCorr(nbinom_mod))
[1] "VarCorr.glmmTMB"
> 
> typeof(VarCorr(nbinom_mod))
[1] "list"

# This didn't work
> unlist(VarCorr(nbinom_mod))
   cond.herd 
8.186695e-09 

> VarCorr(nbinom_mod)

Conditional model:
 Groups Name        Std.Dev. 
 herd   (Intercept) 9.048e-05

> VarCorr(nbinom_mod)$cond
$herd
             (Intercept)
(Intercept) 8.186695e-09
attr(,"stddev")
 (Intercept) 
9.048036e-05 
attr(,"correlation")
            (Intercept)
(Intercept)           1
attr(,"blockCode")
us 
 1 

attr(,"sc")
[1] 1.626599
attr(,"useSc")
[1] FALSE
> VarCorr(nbinom_mod)$cond$herd
             (Intercept)
(Intercept) 8.186695e-09
attr(,"stddev")
 (Intercept) 
9.048036e-05 
attr(,"correlation")
            (Intercept)
(Intercept)           1
attr(,"blockCode")
us 
 1 

# Still didnt work
> VarCorr(nbinom_mod)$cond$herd[1]
[1] 8.186695e-09

> VarCorr(nbinom_mod)$cond$herd[2]
[1] NA

I'm using the emmeans package with a negative-binomial model implemented using the glmmTMB package. I'm trying to bias adjust my backtransformed emmeans per the workflow illustrated here: https://cran.r-project./web/packages/emmeans/vignettes/transformations.html#bias-adj

As I've understood, I need the bias adjustment to be based on the variance of the random effects which can be obtained with VarCorr(nbinom_mod)

The problem is, I can't figure out how to extract that Std.Dev. value

library(glmmTMB)
library(emmeans)
library(dplyr)

data(cbpp, package="lme4")

nbinom_mod <- glmmTMB(incidence ~ period + (1|herd), 
                data = cbpp,
                family = nbinom2)

# Here's what I want to do but it fails since VarCorr doesn't return a number, it returns an object
nbinom_em <- emmeans(nbinom_mod, ~ period, 
               bias.adjust = T,
               sigma = VarCorr(nbinom_mod),
               type = "response")

So I've tried to extract data from VarCorr(nbinom_mod)and failed as such:

> class(VarCorr(nbinom_mod))
[1] "VarCorr.glmmTMB"
> 
> typeof(VarCorr(nbinom_mod))
[1] "list"

# This didn't work
> unlist(VarCorr(nbinom_mod))
   cond.herd 
8.186695e-09 

> VarCorr(nbinom_mod)

Conditional model:
 Groups Name        Std.Dev. 
 herd   (Intercept) 9.048e-05

> VarCorr(nbinom_mod)$cond
$herd
             (Intercept)
(Intercept) 8.186695e-09
attr(,"stddev")
 (Intercept) 
9.048036e-05 
attr(,"correlation")
            (Intercept)
(Intercept)           1
attr(,"blockCode")
us 
 1 

attr(,"sc")
[1] 1.626599
attr(,"useSc")
[1] FALSE
> VarCorr(nbinom_mod)$cond$herd
             (Intercept)
(Intercept) 8.186695e-09
attr(,"stddev")
 (Intercept) 
9.048036e-05 
attr(,"correlation")
            (Intercept)
(Intercept)           1
attr(,"blockCode")
us 
 1 

# Still didnt work
> VarCorr(nbinom_mod)$cond$herd[1]
[1] 8.186695e-09

> VarCorr(nbinom_mod)$cond$herd[2]
[1] NA
Share Improve this question asked Mar 6 at 14:03 myfatsonmyfatson 5615 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2
c(sqrt(VarCorr(nbinom_mod)$cond$herd))

should get the standard deviation associated with this random effect.

  • $cond extracts the conditional component
  • $herd extracts the component associate with this particular random effect (there could be more than one)
  • sqrt() is obvious (we need the std dev, not the variance)
  • c() drops a lot of harmless but potentially confusing extra information
发布评论

评论列表(0)

  1. 暂无评论