Learn R Programming

⚠️There's a newer version (0.11.1) of this package.Take me there.

gratia

Overview

Graceful ‘ggplot’-based graphics and utility functions for working with generalized additive models (GAMs) fitted using the ‘mgcv’ package. Provides 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 specialist smoothers (bs %in% c("mrf","so")) are not currently supported, but univariate, factor and continuous by-variable smooths, simple random effect smooths (bs = 're'), factor-smooth interaction smooths (bs = "fs"), and bivariate tensor product smooths are supported,

  • Estimatation 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:

# Enable repository from gavinsimpson
options(repos = c(
  gavinsimpson = "https://gavinsimpson.r-universe.dev",
  CRAN = "https://cloud.r-project.org"))

# Download and install gratia in R
install.packages("gratia")

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

10,880

Version

0.8.2

License

MIT + file LICENSE

Maintainer

Gavin Simpson

Last Published

January 9th, 2024

Functions in gratia (0.8.2)

draw.gamlss

Plot smooths of a GAMLSS model estimated by GJRM::gamlss
draw.difference_smooth

Plot differences of smooths
difference_smooths

Differences of factor smooth interactions
draw

Generic plotting via ggplot2
draw.compare_smooths

Plot comparisons of smooths
draw.gam

Plot estimated smooths from a fitted GAM
draw.basis

Plot basis functions
draw.derivatives

Plot derivatives of smooths
draw.evaluated_smooth

Plot estimated smooths
draw.mgcv_smooth

Plot basis functions
draw.parametric_effects

Plot estimated effects for model parametric terms
draw_parametric_effect

Internal function to draw an individual parametric effect
evaluate_parametric_term

Evaluate parametric model terms
draw.penalty_df

Display penalty matrices of smooths using ggplot
draw.rootogram

Draw a rootogram
draw.smooth_estimates

Plot the result of a call to smooth_estimates()
edf

Effective degrees of freedom for smooths and GAMs
eval_smooth

S3 methods to evaluate individual smooths
evaluate_smooth

Evaluate a smooth
evenly

Create a sequence of evenly-spaced values
family_name

Name of family used to fit model
factor_combos

All combinations of factor levels
fitted_samples

Draw fitted values from the posterior distribution
fix_offset

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

Generate fitted values from a estimated GAM
draw.smooth_samples

Plot posterior smooths
get_smooth

Extract an mgcv smooth by name
is_by_smooth

Tests for by variable smooths
fderiv

First derivatives of fitted GAM functions
family_type

Extracts the type of family in a consistent way
fixef

Extract fixed effects estimates
family.gam

Extract family objects from models
fixef.gam

Extract fixed effects estimates from a fitted GAM
has_theta

Are additional parameters available for a GAM?
is_factor_term

Is a model term a factor (categorical)?
is_mgcv_smooth

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

Extract an factor-by smooth by name
get_smooths_by_id

Extract an mgcv smooth given its position in the model object
gss_vocab

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

Gu and Wabha test functions
model_vars

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

Concurvity of an estimated GAM
observed_fitted_plot

Plot of fitted against observed response values
parametric_effects

Estimated values for parametric model terms
parametric_terms

Names of any parametric terms in a GAM
load_mgcv

Load mgcv quietly
overview

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

Negative binomial parameter theta
lp_matrix

Return the linear prediction matrix of a fitted GAM
qq_plot

Quantile-quantile plot of model residuals
link

Extract link and inverse link functions from models
is_offset

Is a model term an offset?
n_smooths

How many smooths in a fitted model
nested_rug_values

Values for rug plot in nested form
nested_partial_residuals

Partial residuals in nested form
rep_first_factor_value

Repeat the first level of a factor n times
predicted_samples

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

Partial residuals
seq_min_max_eps

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

Lead-210 age-depth measurements for Small Water
posterior_samples

Draw samples from the posterior distribution of an estimated model
ref_level

Return the reference or specific level of a factor
simulate.gam

Simulate from the posterior distribution of a GAM
partial_derivatives

Partial derivatives of estimated multivariate smooths via finite differences
penalty

Extract and tidy penalty matrices
shift_values

Shift numeric values in a data frame by an amount eps
term_variables

Names of variables involved in a specified model term
smooth_estimates

Evaluate smooths at covariate values
smooth_coefs

Coefficients for a particular smooth
residuals_hist_plot

Histogram of model residuals
smooth_samples

Posterior draws for individual smooths
residuals_linpred_plot

Plot of residuals versus linear predictor values
ref_sims

Reference simulation data
to_na

Sets the elements of vector to NA
too_far

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

A tidy basis representation of a smooth object
transform_fun

Transform estimated values and confidence intervals by applying a function
rootogram

Rootograms to assess goodness of model fit
smooth_dim

Dimension of a smooth
vars_from_label

Returns names of variables from a smooth label
smooths

Names of smooths in a GAM
too_far_to_na

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

General extractor for additional parameters in mgcv models
typical_values

Typical values of model covariates
smooth_type

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

Indices of the parametric terms for a particular smooth
smooth_data

Generate regular data over the covariates of a smooth
term_names

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

Identify a smooth term by its label
variance_comp

Variance components of smooths from smoothness estimates
worm_plot

Worm plot of model residuals
zooplankton

Madison lakes zooplankton data
basis

Basis expansions for smooths
add_residuals.gam

Add residuals from a GAM to a data frame
add_fitted

Add fitted values from a model to a data frame
add_confint

Add a confidence interval to an existing object
bird_move

Simulated bird migration data
add_fitted.gam

Add fitted values from a GAM to a data frame
data_sim

Simulate example data for fitting GAMs
data_slice

Prepare a data slice through model covariates
compare_smooths

Compare smooths across models
check_user_select_smooths

Select smooths based on user's choices
coef.scam

Extract coefficients from a fitted scam model.
add_constant

Add a constant to estimated values
appraise

Model diagnostic plots
confint.fderiv

Point-wise and simultaneous confidence intervals for derivatives of smooths
add_partial_residuals

Add partial residuals
add_residuals

Add residuals from a model to a data frame
confint.gam

Point-wise and simultaneous confidence intervals for smooths
data_combos

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

Derivatives of estimated smooths via finite differences
datagen

Generate data over the range of variables used in smooths