Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

fderiv

First derivatives of fitted GAM functions


Description

The first derivative of the smooth functions of a GAM model calculated using finite differences.

Usage

fderiv(model, ...)

## S3 method for class 'gam'
fderiv(
  model,
  newdata,
  term,
  n = 200,
  eps = 1e-07,
  unconditional = FALSE,
  offset = NULL,
  ...
)

## S3 method for class 'gamm'
fderiv(model, ...)

Arguments

model

A fitted GAM. Currently only models fitted by mgcv::gam() and mgcv::gamm() are supported.

...

Arguments that are passed to other methods.

newdata

a data frame containing the values of the model covariates at which to evaluate the first derivatives of the smooths.

term

character; vector of one or more terms for which derivatives are required. If missing, derivatives for all smooth terms will be returned.

n

integer; if newdata is missing the original data can be reconstructed from model and then n controls the number of values over the range of each covariate with which to populate newdata.

eps

numeric; the value of the finite difference used to approximate the first derivative.

unconditional

logical; if TRUE, the smoothing parameter uncertainty corrected covariance matrix is used, if available, otherwise the uncorrected Bayesian posterior covariance matrix is used.

offset

numeric; value of offset to use in generating predictions.

Value

An object of class "fderiv" is returned.

Author(s)

Gavin L. Simpson

Examples

load_mgcv()

dat <- gamSim(1, n = 400, dist = "normal", scale = 2)
mod <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat, method = "REML")

## first derivatives of all smooths...
fd <- fderiv(mod)

## ...and a selected smooth
fd2 <- fderiv(mod, term = "x1")

## Models with factors
set.seed(2)
dat <- gamSim(4, n = 400, dist = "normal", scale = 2)
mod <- gam(y ~ s(x0) + s(x1) + fac, data = dat, method = "REML")

## first derivatives of all smooths...
fd <- fderiv(mod)

## ...and a selected smooth
fd2 <- fderiv(mod, term = "x1")

gratia

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

v0.6.0
MIT + file LICENSE
Authors
Gavin L. Simpson [aut, cre] (<https://orcid.org/0000-0002-9084-8413>), Henrik Singmann [ctb] (<https://orcid.org/0000-0002-4842-3657>)
Initial release
2021-04-17

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.