Learn R Programming

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 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,152

Version

0.9.0

License

MIT + file LICENSE

Maintainer

Last Published

March 27th, 2024

Functions in gratia (0.9.0)

check_user_select_smooths

Select smooths based on user's choices
confint.gam

Point-wise and simultaneous confidence intervals for smooths
basis

Basis expansions for smooths
coef.scam

Extract coefficients from a fitted scam model.
bird_move

Simulated bird migration data
compare_smooths

Compare smooths across models
basis_size

Extract basis dimension of a smooth
confint.fderiv

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

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

Extract the boundary of a soap film smooth
draw.compare_smooths

Plot comparisons of smooths
datagen

Generate data over the range of variables used in smooths
draw

Generic plotting via ggplot2
data_slice

Prepare a data slice through model covariates
derivatives

Derivatives of estimated smooths via finite differences
data_sim

Simulate example data for fitting GAMs
difference_smooths

Differences of factor smooth interactions
draw.derivatives

Plot derivatives of smooths
draw.basis

Plot basis functions
derivative_samples

Posterior expectations of derivatives from an estimated model
draw.rootogram

Draw a rootogram
draw.gam

Plot estimated smooths from a fitted GAM
draw.parametric_effects

Plot estimated effects for model parametric terms
draw.mgcv_smooth

Plot basis functions
draw.smooth_estimates

Plot the result of a call to smooth_estimates()
draw.difference_smooth

Plot differences of smooths
draw.evaluated_parametric_term

Plot estimated parametric effects
draw.penalty_df

Display penalty matrices of smooths using ggplot
draw.gamlss

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

S3 methods to evaluate individual smooths
edf

Effective degrees of freedom for smooths and GAMs
evaluate_parametric_term

Evaluate parametric model terms
evaluate_smooth

Evaluate a smooth
family.gam

Extract family objects from models
family_name

Name of family used to fit model
fixef.gam

Extract fixed effects estimates from a fitted GAM
gaussian_draws

Posterior samples using a simple Metropolis Hastings sampler
draw.pairwise_concurvity

Plot concurvity measures
fitted_values

Generate fitted values from a estimated GAM
draw_parametric_effect

Internal function to draw an individual parametric effect
draw.smooth_samples

Plot posterior smooths
fitted_samples

Draw fitted values from the posterior distribution
fix_offset

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

Extract fixed effects estimates
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
is_by_smooth

Tests for by variable smooths
has_theta

Are additional parameters available for a GAM?
get_smooth

Extract an mgcv smooth by name
get_by_smooth

Extract an factor-by smooth by name
model_vars

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

First derivatives of fitted GAM functions
family_type

Extracts the type of family in a consistent way
model_concurvity

Concurvity of an estimated GAM
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?
nb_theta

Negative binomial parameter theta
link

Extract link and inverse link functions from models
nested_partial_residuals

Partial residuals in nested form
load_mgcv

Load mgcv quietly
is_mgcv_smooth

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

Estimated values for parametric model terms
parametric_terms

Names of any parametric terms in a GAM
evenly

Create a sequence of evenly-spaced values
lp_matrix

Return the linear prediction matrix of a fitted GAM
post_draws

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

Extract and tidy penalty matrices
rep_first_factor_value

Repeat the first level of a factor n times
post_link_funs

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

How many smooths in a fitted model
posterior_samples

Draw samples from the posterior distribution of an estimated model
overview

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

Plot of fitted against observed response values
ref_level

Return the reference or specific level of a factor
ref_sims

Reference simulation data
predicted_samples

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

Histogram of model residuals
qq_plot

Quantile-quantile plot of model residuals
response_derivatives

Derivatives on the response scale from an estimated GAM
residuals_linpred_plot

Plot of residuals versus linear predictor values
mh_draws

Posterior samples using a Gaussian approximation to the posterior distribution
rootogram

Rootograms to assess goodness of model fit
shift_values

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

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

Extract the null deviance of a fitted model
lss_parameters

General names of LSS parameters for each GAM family
smooth_samples

Posterior draws for individual smooths
term_names

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

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

Evaluate a spline at provided covariate values
nested_rug_values

Values for rug plot in nested form
factor_combos

All combinations of factor levels
partial_derivatives

Partial derivatives of estimated multivariate smooths via finite differences
smooth_terms

List the variables involved in smooths
get_smooths_by_id

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

Simulate from the posterior distribution of a GAM
tidy_basis

A tidy basis representation of a smooth object
to_na

Sets the elements of vector to NA
smooth_type

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

Worm plot of model residuals
gratia-package

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

Dimension of a smooth
smooth_estimates

Evaluate smooths at covariate values
term_variables

Names of variables involved in a specified model term
too_far

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

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

Coefficients for a particular smooth
smooth_data

Generate regular data over the covariates of a smooth
user_draws

Handle user-supplied posterior draws
zooplankton

Madison lakes zooplankton data
which_smooths

Identify a smooth term by its label
partial_residuals

Partial residuals
theta

General extractor for additional parameters in mgcv models
vars_from_label

Returns names of variables from a smooth label
variance_comp

Variance components of smooths from smoothness estimates
reorder_tensor_smooth_terms

Reorder tensor product terms for nicer plotting
reorder_fs_smooth_terms

Reorder random factor smooth terms to place factor last
smallAges

Lead-210 age-depth measurements for Small Water
spline_values

Evaluate a spline at provided covariate values
transform_fun

Transform estimated values and confidence intervals by applying a function
smooth_coef_indices

Indices of the parametric terms for a particular smooth
typical_values

Typical values of model covariates
smooths

Names of smooths in a GAM
add_sizer

Add indicators of significant change after SiZeR
appraise

Model diagnostic plots
add_partial_residuals

Add partial residuals
add_confint

Add a confidence interval to an existing object
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_residuals.gam

Add residuals from a GAM to a data frame
add_fitted.gam

Add fitted values from a GAM to a data frame
add_fitted

Add fitted values from a model to a data frame
add_constant

Add a constant to estimated values