| Title: | Hierarchical Partitioning of Marginal R2 for Generalized Mixed-Effect Models |
|---|---|
| Description: | Conducts hierarchical partitioning to calculate individual contributions of each predictor (fixed effects) towards marginal R2 for generalized linear mixed-effect model (including lm, glm and glmm) based on output of r.squaredGLMM() in 'MuMIn', applying the algorithm of Lai J.,Zou Y., Zhang S.,Zhang X.,Mao L.(2022)glmm.hp: an R package for computing individual effect of predictors in generalized linear mixed models.Journal of Plant Ecology,15(6)1302-1307<doi:10.1093/jpe/rtac096>. |
| Authors: | Jiangshan Lai [aut, cre] (ORCID: <https://orcid.org/0000-0002-0279-8816>), Kim Nimon [aut], Yao Liu [aut] |
| Maintainer: | Jiangshan Lai <[email protected]> |
| License: | GPL |
| Version: | 1.0-0 |
| Built: | 2026-06-06 10:01:40 UTC |
| Source: | https://github.com/laijiangshan/glmm.hp |
This function assists users in selecting an appropriate family and link function
for generalized linear models (GLM) based on the distributional properties of the response variable.
It provides a quick diagnostic summary, distribution plots, and an optional AIC comparison among candidate models.
detect_glm_family(y, plot = TRUE, aic_test = FALSE)detect_glm_family(y, plot = TRUE, aic_test = FALSE)
y |
A numeric vector representing the response variable. |
plot |
Logical; if |
aic_test |
Logical; if |
The function inspects the basic characteristics of the response variable, including its range, mean, variance, and whether it contains only integers or proportions. Based on these diagnostics, it suggests one or more candidate GLM families among:
"gaussian" – continuous response, may include negative values
"poisson" or "quasipoisson" – integer count data (possibly overdispersed)
"Gamma" or "inverse.gaussian" – strictly positive continuous data
"binomial" – proportion or binary data (0–1 range)
The suggested link functions are: "identity" for Gaussian, "log" for Poisson/Gamma,
and "logit" for Binomial.
A list containing:
family |
Suggested GLM family |
link |
Suggested link function |
# Example 1: Continuous response (can be negative) set.seed(123) y1 <- rnorm(100) detect_glm_family(y1) # Example 2: Count data y2 <- rpois(100, lambda = 5) detect_glm_family(y2) # Example 3: Proportion data y3 <- rbeta(100, 2, 5) detect_glm_family(y3)# Example 1: Continuous response (can be negative) set.seed(123) y1 <- rnorm(100) detect_glm_family(y1) # Example 2: Count data y2 <- rpois(100, lambda = 5) detect_glm_family(y2) # Example 3: Proportion data y3 <- rbeta(100, 2, 5) detect_glm_family(y3)
Hierarchical Partitioning of Marginal R2 for Generalized Mixed-Effect Models
glmm.hp(mod, iv = NULL, type = "adjR2", commonality = FALSE)glmm.hp(mod, iv = NULL, type = "adjR2", commonality = FALSE)
mod |
Fitted lme4,nlme,glmmTMB,glm or lm model objects. |
iv |
optional The relative importance of predictor groups will be assessed. The input for iv should be a list, where each element contains the names of variables belonging to a specific group. These variable names must correspond to the predictor variables defined in the model (mod). |
type |
The type of R-square of lm, either "R2" or "adjR2", in which "R2" is unadjusted R-square and "adjR2" is adjusted R-square, the default is "adjR2". The adjusted R-square is calculated using Ezekiel's formula (Ezekiel 1930) for lm. |
commonality |
Logical; If TRUE, the result of commonality analysis (2^N-1 fractions for N predictors) is shown, the default is FALSE. |
This function conducts hierarchical partitioning to calculate the individual contributions of each predictor towards total (marginal) R2 for Generalized Linear Mixed-effect Model (including lm,glm and glmm). The marginal R2 is the output of r.squaredGLMM in MuMIn package for glm and glmm.
r.squaredGLMM |
The R2 for the full model. |
commonality.analysis |
If commonality=TRUE, a matrix containing the value and percentage of all commonality (2^N-1 for N predictors or matrices). |
hierarchical.partitioning |
A matrix containing individual effects and percentage of individual effects towards total (marginal) R2 for each predictor. |
Jiangshan Lai [email protected]
Lai J.,Zhu W., Cui D.,Mao L.(2023)Extension of the glmm.hp package to Zero-Inflated generalized linear mixed models and multiple regression.Journal of Plant Ecology,16(6):rtad038<DOI:10.1093/jpe/rtad038>
Lai J.,Zou Y., Zhang S.,Zhang X.,Mao L.(2022)glmm.hp: an R package for computing individual effect of predictors in generalized linear mixed models.Journal of Plant Ecology,15(6):1302-1307<DOI:10.1093/jpe/rtac096>
Lai J.,Zou Y., Zhang J.,Peres-Neto P.(2022) Generalizing hierarchical and variation partitioning in multiple regression and canonical analyses using the rdacca.hp R package.Methods in Ecology and Evolution,13(4):782-788<DOI:10.1111/2041-210X.13800>
Chevan, A. & Sutherland, M. (1991). Hierarchical partitioning. American Statistician, 45, 90-96. doi:10.1080/00031305.1991.10475776
Nimon, K., Oswald, F.L. & Roberts, J.K. (2013). Yhat: Interpreting regression effects. R package version 2.0.0.
Nakagawa, S., & Schielzeth, H. (2013). A general and simple method for obtaining R2 from generalized linear mixed-effects models. Methods in Ecology and Evolution, 4(2), 133-142.
Nakagawa, S., Johnson, P. C., & Schielzeth, H. (2017). The coefficient of determination R2 and intra-class correlation coefficient from generalized linear mixed-effects models revisited and expanded. Journal of the Royal Society Interface, 14(134), 20170213.
Ezekiel, M. (1930) Methods of Correlational Analysis. Wiley, New York.
library(MuMIn) library(lme4) mod1 <- lmer(Sepal.Length ~ Petal.Length + Petal.Width+(1|Species),data = iris) r.squaredGLMM(mod1) glmm.hp(mod1) a <- glmm.hp(mod1) plot(a) mod2 <- glm(Sepal.Length ~ Petal.Length + Petal.Width, data = iris) r.squaredGLMM(mod2) glmm.hp(mod2) b <- glmm.hp(mod2) plot(b) plot(glmm.hp(mod2)) mod3 <- lm(Sepal.Length ~ Petal.Length + Petal.Width + Petal.Length:Petal.Width, data = iris) glmm.hp(mod3,type="R2") glmm.hp(mod3,commonality=TRUE) mod4 <- lm(Sepal.Length ~ Petal.Length + Petal.Width + Sepal.Width, data = iris) iv=list(pred1="Sepal.Width",pred2=c("Petal.Length","Petal.Width")) glmm.hp(mod4,iv)library(MuMIn) library(lme4) mod1 <- lmer(Sepal.Length ~ Petal.Length + Petal.Width+(1|Species),data = iris) r.squaredGLMM(mod1) glmm.hp(mod1) a <- glmm.hp(mod1) plot(a) mod2 <- glm(Sepal.Length ~ Petal.Length + Petal.Width, data = iris) r.squaredGLMM(mod2) glmm.hp(mod2) b <- glmm.hp(mod2) plot(b) plot(glmm.hp(mod2)) mod3 <- lm(Sepal.Length ~ Petal.Length + Petal.Width + Petal.Length:Petal.Width, data = iris) glmm.hp(mod3,type="R2") glmm.hp(mod3,commonality=TRUE) mod4 <- lm(Sepal.Length ~ Petal.Length + Petal.Width + Sepal.Width, data = iris) iv=list(pred1="Sepal.Width",pred2=c("Petal.Length","Petal.Width")) glmm.hp(mod4,iv)
glmm.hp objectPlot for a glmm.hp object
## S3 method for class 'glmmhp' plot(x, plot.perc = FALSE, color = NULL, n = 1, dig = 4, ...)## S3 method for class 'glmmhp' plot(x, plot.perc = FALSE, color = NULL, n = 1, dig = 4, ...)
x |
A |
plot.perc |
Logical;if TRUE, the bar plot (based on ggplot2 package) of the percentage to individual effects of variables or groups towards total explained variation, the default is FALSE to show plot with original individual effects. |
color |
Color of variables. |
n |
Integer; which marginal R2 in output of r.squaredGLMM to plot. |
dig |
Integer; number of decimal places in Venn diagram. |
... |
unused |
a ggplot object
Jiangshan Lai [email protected]
library(MuMIn) library(lme4) mod1 <- lmer(Sepal.Length ~ Petal.Length + Petal.Width +(1 | Species), data = iris) a <- glmm.hp(mod1) plot(a) mod3 <- lm(Sepal.Length ~ Petal.Length+Petal.Width,data = iris) plot(glmm.hp(mod3,type="R2")) plot(glmm.hp(mod3,commonality=TRUE),color = c("#8DD3C7", "#FFFFB3"))library(MuMIn) library(lme4) mod1 <- lmer(Sepal.Length ~ Petal.Length + Petal.Width +(1 | Species), data = iris) a <- glmm.hp(mod1) plot(a) mod3 <- lm(Sepal.Length ~ Petal.Length+Petal.Width,data = iris) plot(glmm.hp(mod3,type="R2")) plot(glmm.hp(mod3,commonality=TRUE),color = c("#8DD3C7", "#FFFFB3"))