Learn R Programming

partR2

The goal of partR2 is to estimate R2 in GLMMs (sensu Nakagawa & Schielzeth 2013) and to partition the R2 into the variance explained by the predictors.

The package takes a fitted lme4 model as input and gives you:

  • R2 (marginal or conditional)
  • Part (semi-partial) R2, the variance explained uniquely by each predictor and combinations of predictors
  • Inclusive R2, the variance explained by a predictor independent of all other predictors
  • Structure coefficients, the correlation between a predictor and the fitted response
  • Beta weights, standardised model estimates

All estimates can be combined with parametric bootstrapping to get confidence intervals.

Installation

You can install the stable version of partR2 from CRAN with:

install.packages("partR2")

Or the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("mastoffel/partR2", build_vignettes = TRUE, dependencies = TRUE) 

Access the vignette with:

# check vignette
browseVignettes("partR2")

partR2 is still in an early phase of development and might contain bugs. If you find one, please report a minimal reproducible example in the issues.

Citation

When using partR2, please cite our paper:

Stoffel MA, Nakagawa S, Schielzeth H. 2021. partR2: partitioning R2 in generalized linear mixed models. PeerJ 9:e11414 https://doi.org/10.7717/peerj.11414

Example

library(partR2)
library(lme4)

?`partR2-package`

# load data
data(biomass)
# fit lme4 model
mod <- lmer(Biomass ~  Year + Temperature + SpeciesDiversity + (1|Population),
            data = biomass)
# R2s and partial R2s
(R2 <- partR2(mod,  partvars = c("SpeciesDiversity", "Temperature", "Year"),
              R2_type = "marginal", nboot = 100, CI = 0.95))
#> 
#> 
#> R2 (marginal) and 95% CI for the full model: 
#>  R2     CI_lower CI_upper nboot ndf
#>  0.5133 0.4439   0.5908   100   4  
#> 
#> ----------
#> 
#> Part (semi-partial) R2:
#>  Predictor(s)                      R2     CI_lower CI_upper nboot ndf
#>  Model                             0.5133 0.4439   0.5908   100   4  
#>  SpeciesDiversity                  0.1729 0.0736   0.2913   100   3  
#>  Temperature                       0.3058 0.2193   0.4106   100   3  
#>  Year                              0.0140 0.0000   0.1537   100   3  
#>  SpeciesDiversity+Temperature      0.4916 0.4205   0.5714   100   2  
#>  SpeciesDiversity+Year             0.1862 0.0883   0.3029   100   2  
#>  Temperature+Year                  0.3276 0.2430   0.4293   100   2  
#>  SpeciesDiversity+Temperature+Year 0.5133 0.4439   0.5908   100   1

And to plot the results:

forestplot(R2, type = "R2", line_size = 0.7, text_size = 14, point_size = 3)

Copy Link

Version

Install

install.packages('partR2')

Monthly Downloads

748

Version

0.9.2

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Martin A Stoffel

Last Published

March 4th, 2024

Functions in partR2 (0.9.2)

R2_pe

Calculate R2
make_combs

Create list of combination of variables.
forestplot

Plot a partR2 object
fixvar_of_red_mod

Calculate fixed effect variance from a reduced model
get_ndf

Get numerator dfs for reduced models
calc_CI

Calculates CI from bootstrap replicates
get_var_comps

Extract variance components from merMod.
fixef_simple

Get tidy summary of fixed effect estimates
bootstrap_all

Parametric bootstrapping
mergeR2

Merge partR2 objects to combine R2s for main effects and interactions
get_bw

Get beta weights
partR2-package

partR2: Partitioning R2 in generalized linear mixed models
sim_data

sim_data dataset
model_overdisp

Adds an observational level random effect to a model, if applicable.
partR2

Partitioning R2 (R-square) in mixed models
get_ran_var

Extracts random effect variances
with_warnings

Captures and suppresses (still to find out why) warnings of an expression
part_R2s

Calculate part R2
var_comps_binary

Get variance components for binomial model with binary response.
summary.partR2

Complete summary of a partR2 object
mod_names_partbatch

Modify term names if partbatch is a named list
var_comps_poisson

Get variance components for merMod with poisson response.
var_comps_proportion

Get variance components for binomial model with proportion response.
var_comps_gaussian

Get variance components for gaussian model.
print.partR2

Print a partR2 object
%>%

Pipe operator
BeetlesBody

BeetlesBody dataset
Grasshoppers

Grasshoppers dataset
GuineaPigs

GuineaPigs dataset
add_CI_cols

Adds columns for CI
BeetlesMale

BeetlesMale dataset
SC_pe

Structure coefficients
biomass

biomass dataset
boot_to_df

List to data.frame with bootstrap samples per row
BeetlesFemale

BeetlesFemale dataset