Learn R Programming

gratia

Overview

Working with GAMs within the ‘tidyverse’ can be tedious and even difficult without a good understanding of GAMs themselves and how the model is returned by ‘mgcv’ and what the model objects contain. ‘gratia’ is designed to help with this.

‘gratia’ provides ‘ggplot’-based graphics and utility functions for working with generalized additive models (GAMs) fitted using the ‘mgcv’ package, via a reimplementation of the plot() method for GAMs that ‘mgcv’ provides, as well as ‘tidyverse’ compatible representations of estimated smooths.

Features

The main features of gratia are currently

  • A ggplot2-based replacement for mgcv:::plot.gam(): draw.gam().

    For example, the classic four term additive example from Gu & Wahba:

    Or for a bivariate smooth:

    Note that some specialist smoothers (bs %in% c("mrf","sw", "sf")) are not currently supported, but univariate, factor and continuous by-variable smooths, simple random effect smooths (bs = 're'), factor-smooth interaction smooths (bs = "fs"), constrained factor smooths (bs = "sz"), full soap film smooths (bs = "so"), and bivariate, trivariate, and quadvariate TPRS and tensor product smooths are supported,

  • Estimation of derivatives of fitted smoothers: derivatives(),

  • Estimation of point-wise across-the-function confidence intervals and simultaneous intervals for smooths: confint.gam().

  • Model diagnostics via appraise()

Installing gratia

gratia is now available on CRAN, and can be installed with

install.packages("gratia")

however gratia is under active development and you may wish to install the development version from github. The easiest way to do this is via the install_github() function from package remotes. Make sure you have remotes installed, then run

remotes::install_github("gavinsimpson/gratia")

to install the package. Alternatively, binary packages of the development version are available from rOpenSci’s R Universe service:

# Install gratia in R
install.packages("gratia", repos = c(
  "https://gavinsimpson.r-universe.dev",
  "https://cloud.r-project.org"
))

History

gratia grew out of an earlier package, schoenberg, itself a development of the earlier package tsgam, which was originally intended to be used with GAMs fitted to time series. As I was developing tsgam however it became clear that the package could be used more generally and that the name “tsgam” was no longer appropriate. To avoid breaking blog posts I had written using tsgam I decided to copy the git repo and all the history to a new repo for the package under the name schoenberg. At a later date someone released another package called schoenberg to CRAN, so that scuppered that idea. Now I’m calling the package gratia. Hopefully I won’t have to change it again…

Why gratia?

In naming his greta package, Nick Golding observed the recent phenomena of naming statistical modelling software, such as Stan or Edward, after individuals that played a prominent role in the development of the field. This lead Nick to name his Tensor Flow-based package greta after Grete Hermann.

In the same spirit, gratia is named in recognition of the contributions of Grace Wahba, who did pioneering work on the penalised spline models that are at the foundation of the way GAMs are estimated in mgcv. I wanted to name the package grace, to explicitly recognise Grace’s contributions, but unfortunately there was already a package named Grace on CRAN. So I looked elsewhere for inspiration.

The English word “grace” derives from the Latin gratia, meaning “favor, charm, thanks” (according to Merriam Webster).

The chair that Grace Wabha currently holds is named after Isaac J Schoenberg, a former University Madison-Wisconsin Professor of Mathematics, who in a 1946 paper provided the first mathematical reference to “splines”. (Hence the previous name for the package.)

Copy Link

Version

Install

install.packages('gratia')

Monthly Downloads

3,362

Version

0.10.0

License

MIT + file LICENSE

Maintainer

Gavin Simpson

Last Published

December 19th, 2024

Functions in gratia (0.10.0)

conditional_values

Conditional predictions from a GAM
derivative_samples

Posterior expectations of derivatives from an estimated model
data_sim

Simulate example data for fitting GAMs
difference_smooths

Differences of factor smooth interactions
draw

Generic plotting via ggplot2
draw.basis

Plot basis functions
derivatives

Derivatives of estimated smooths via finite differences
dispersion

Dispersion parameter for fitted model
data_combos

All combinations of factor levels plus typical values of continuous variables
datagen

Generate data over the range of variables used in smooths
data_slice

Prepare a data slice through model covariates
draw.evaluated_parametric_term

Plot estimated parametric effects
draw.compare_smooths

Plot comparisons of smooths
draw.parametric_effects

Plot estimated effects for model parametric terms
draw.mgcv_smooth

Plot basis functions
draw.conditional_values

Plot conditional predictions
draw.difference_smooth

Plot differences of smooths
draw.pairwise_concurvity

Plot concurvity measures
draw.gam

Plot estimated smooths from a fitted GAM
draw.derivatives

Plot derivatives of smooths
draw.gamlss

Plot smooths of a GAMLSS model estimated by GJRM::gamlss
draw_parametric_effect

Internal function to draw an individual parametric effect
evaluate_smooth

Evaluate a smooth
draw.penalty_df

Display penalty matrices of smooths using ggplot
evenly

Create a sequence of evenly-spaced values
draw.rootogram

Draw a rootogram
family_name

Name of family used to fit model
factor_combos

All combinations of factor levels
draw.smooth_samples

Plot posterior smooths
draw.smooth_estimates

Plot the result of a call to smooth_estimates()
family_type

Extracts the type of family in a consistent way
family.gam

Extract family objects from models
eval_smooth

S3 methods to evaluate individual smooths
fitted_values

Generate fitted values from a estimated GAM
fix_offset

Fix the names of a data frame containing an offset variable.
evaluate_parametric_term

Evaluate parametric model terms
edf

Effective degrees of freedom for smooths and GAMs
gratia-package

gratia: Graceful 'ggplot'-Based Graphics and Other Functions for GAMs Fitted Using 'mgcv'
fixef

Extract fixed effects estimates
get_smooths_by_id

Extract an mgcv smooth given its position in the model object
fixef.gam

Extract fixed effects estimates from a fitted GAM
get_smooth

Extract an mgcv smooth by name
gw_f0

Gu and Wabha test functions
gaussian_draws

Posterior samples using a simple Metropolis Hastings sampler
link

Extract link and inverse link functions from models
parametric_terms

Names of any parametric terms in a GAM
gss_vocab

Data from the General Social Survey (GSS) from the National Opinion Research Center of the University of Chicago
get_by_smooth

Extract an factor-by smooth by name
model_concurvity

Concurvity of an estimated GAM
fitted_samples

Draw fitted values from the posterior distribution
mh_draws

Posterior samples using a Gaussian approximation to the posterior distribution
n_smooths

How many smooths in a fitted model
parametric_effects

Estimated values for parametric model terms
fderiv

First derivatives of fitted GAM functions
model_constant

Extract the model constant term
is_factor_term

Is a model term a factor (categorical)?
is_offset

Is a model term an offset?
is_mgcv_smooth

Check if objects are smooths or are a particular type of smooth
is_by_smooth

Tests for by variable smooths
nested_rug_values

Values for rug plot in nested form
n_eta

The Number of linear predictors in model
load_mgcv

Load mgcv quietly
nb_theta

Negative binomial parameter theta
null_deviance

Extract the null deviance of a fitted model
has_theta

Are additional parameters available for a GAM?
ref_level

Return the reference or specific level of a factor
post_link_funs

A list of transformation functions named for LSS parameters in a GAMLSS
posterior_samples

Draw samples from the posterior distribution of an estimated model
nested_partial_residuals

Partial residuals in nested form
partial_derivatives

Partial derivatives of estimated multivariate smooths via finite differences
partial_residuals

Partial residuals
lp_matrix

Return the linear prediction matrix of a fitted GAM
lss_parameters

General names of LSS parameters for each GAM family
rep_first_factor_value

Repeat the first level of a factor n times
residuals_hist_plot

Histogram of model residuals
shift_values

Shift numeric values in a data frame by an amount eps
simulate.gam

Simulate from the posterior distribution of a GAM
seq_min_max_eps

Create a sequence of evenly-spaced values adjusted to accommodate a small adjustment
rootogram

Rootograms to assess goodness of model fit
qq_plot

Quantile-quantile plot of model residuals
predicted_samples

Draw new response values from the conditional distribution of the response
model_vars

List the variables involved in a model fitted with a formula
smooth_dim

Dimension of a smooth
ref_sims

Reference simulation data
smooth_samples

Posterior draws for individual smooths
smooth_label

Extract the label for a smooth used by 'mgcv'
response_derivatives

Derivatives on the response scale from an estimated GAM
residuals_linpred_plot

Plot of residuals versus linear predictor values
smooth_coef_indices

Indices of the parametric terms for a particular smooth
smallAges

Lead-210 age-depth measurements for Small Water
observed_fitted_plot

Plot of fitted against observed response values
overview

Provides an overview of a model and the terms in that model
penalty

Extract and tidy penalty matrices
too_far

Exclude values that lie too far from the support of data
reorder_fs_smooth_terms

Reorder random factor smooth terms to place factor last
post_draws

Low-level Functions to generate draws from the posterior distribution of model coefficients
too_far_to_na

Set rows of data to NA if the lie too far from a reference set of values
typical_values

Typical values of model covariates
transform_fun

Transform estimated values and confidence intervals by applying a function
smooth_estimates

Evaluate smooths at covariate values
spline_values2

Evaluate a spline at provided covariate values
term_names

Extract names of all variables needed to fit a GAM or a smooth
user_draws

Handle user-supplied posterior draws
vars_from_label

Returns names of variables from a smooth label
tidy_basis

A tidy basis representation of a smooth object
to_na

Sets the elements of vector to NA
smooths

Names of smooths in a GAM
smooth_terms

List the variables involved in smooths
smooth_data

Generate regular data over the covariates of a smooth
variance_comp

Variance components of smooths from smoothness estimates
smooth_coefs

Coefficients for a particular smooth
which_smooths

Identify a smooth term by its label
smooth_type

Determine the type of smooth and return it n a human readable form
reorder_tensor_smooth_terms

Reorder tensor product terms for nicer plotting
theta

General extractor for additional parameters in mgcv models
spline_values

Evaluate a spline at provided covariate values
term_variables

Names of variables involved in a specified model term
worm_plot

Worm plot of model residuals
zooplankton

Madison lakes zooplankton data
add_confint

Add a confidence interval to an existing object
add_residuals.gam

Add residuals from a GAM to a data frame
add_constant

Add a constant to estimated values
add_partial_residuals

Add partial residuals
add_residuals

Add residuals from a model to a data frame
add_fitted_samples

Add posterior draws from a model to a data object
add_sizer

Add indicators of significant change after SiZeR
add_fitted.gam

Add fitted values from a GAM to a data frame
appraise

Model diagnostic plots
add_fitted

Add fitted values from a model to a data frame
confint.gam

Point-wise and simultaneous confidence intervals for smooths
compare_smooths

Compare smooths across models
boundary

Extract the boundary of a soap film smooth
check_user_select_smooths

Select smooths based on user's choices
basis_size

Extract basis dimension of a smooth
basis

Basis expansions for smooths
confint.fderiv

Point-wise and simultaneous confidence intervals for derivatives of smooths
coef.scam

Extract coefficients from a fitted scam model.
bird_move

Simulated bird migration data