Skip to content

Instantly share code, notes, and snippets.

@bbolker
Created September 27, 2021 16:25
Show Gist options
  • Save bbolker/d2f1754ce7ce41736de120d09f075288 to your computer and use it in GitHub Desktop.
Save bbolker/d2f1754ce7ce41736de120d09f075288 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(dotwhisker)
library(broom.mixed)
data <- read_csv("https://raw.githubusercontent.com/mgree013/Rethinking_Biodiversity_Streams/main/data.csv",
col_types = cols())
## add centred/scaled vars
## might not help but can't hurt
data_s <- mutate(data,
sRdl = drop(scale(River.dist.lake)),
sHrd = drop(scale(Head.river.dist)))
## fit all models
library(glmmTMB)
null <- glmmTMB(betas.LCBD ~ 1+ (1|O.NET), family=beta_family(), data=data_s)
mod1 <- update(null, . ~ . + sRdl)
mod2 <- update(null, . ~ . + sHrd)
mod3 <- update(null, . ~ . + sHrd*sRdl)
## tidy so we can look at results
res <- purrr::map_dfr(list(null=null, riverdistlake=mod1, headriverdist=mod2, all = mod3),
tidy,
conf.int=TRUE,
.id = "model")
## FE parameters look reasonable: intercepts are all small, not
## surprising since mean of data is small [mean(data$betas.LCBD) == 0.0459]
res %>% filter(effect=="fixed") %>%
ggplot(aes(estimate, term, colour = model)) +
geom_pointrange(aes(xmin = conf.low, xmax = conf.high), position = position_dodge(width=0.5)) +
geom_vline(xintercept=0, lty=2)
## RE variances all look OK/similar
res %>% filter(effect=="ran_pars") %>%
mutate(term = paste(group, term, sep = ".")) %>%
ggplot(aes(estimate, term, colour = model)) +
geom_pointrange(aes(xmin = conf.low, xmax = conf.high), position = position_dodge(width=0.5)) +
geom_vline(xintercept=0, lty=2)
## performance::r2_nakagawa
insight::get_variance(mod1, tolerance = 1e-5, name_fun = "r2()",
name_full = "r-squared")
insight:::.compute_variances(mod1, component="all", name_full = "junk")
## debug(insight:::.compute_variances)
reported.table2 <- bbmle::AICtab(mod1,mod2,mod3,null,weights = TRUE, sort = FALSE)
## error from variance_distributional
performance::r2(mod1)
performance::r2(mod2)
performance::r2(mod3)
performance::r2(null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment