The first or second derivative of a fitted linear predictor with
respect to one covariate, on a grid, with delta-method standard errors
and the same pair of intervals frm_curve() gives: pointwise, and
simultaneous over the whole grid.
Usage
frm_curve_deriv(
object,
var,
order = 1L,
newdata = NULL,
contrast = NULL,
dpar = NULL,
resp = NULL,
re_formula = NA,
allow_new_levels = FALSE,
level = 0.95,
simultaneous = TRUE,
nsim = 10000L,
eps = NULL,
seed = NULL,
tol = 1e-06
)Arguments
- object
A
frmtmb_fit, or afrmtmb_curvefromfrm_curve(), in which case its grid and itsdpar,respandre_formulaare reused. Itsallow_new_levelsis reused too when this call does not supply the argument; a value supplied here,FALSEincluded, wins.- var
Name of the covariate to differentiate with respect to. It must be a numeric column of the grid.
- order
1 or 2.
- newdata
The grid. Required when
objectis a fit; taken from the curve otherwise.- contrast
A second grid with the same number of rows, or
NULLfor an ordinary curve. With it the curve isnewdataminuscontrast, row by row.- dpar
Distributional parameter to read the curve off.
NULL, the default, is the location parametermu.- resp
Response name, for a multivariate fit.
- re_formula
NA(the default) drops the(x | g)group-level terms, the conventionmgcvandgratiaplot. It KEEPS every smooth, as brms does, a smooth indexed by a grouping factor included, so on a model withs(t, g, bs = "fs")the grid has to carrygatNAtoo and the curve is that level's.NULLkeeps every random effect, so the grid must carry every grouping column.?frm_linpredhas the full rule.- allow_new_levels
Allow a grouping level the fit did not see.
FALSE, the default, refuses such a row by name.TRUEpredicts it at the population level, and it is the way to read the POPULATION curve off a model withs(t, g, bs = "fs"): putgin the grid at a level the fit did not see, and the factor smooth contributes nothing. Passed to everyfrm_lp_basis()andfrm_linpred()call, so the design, the estimate and the covariance check read the same rows. See the section "The population curve of a factor-smooth model" offrm_curve(). At an unseen level of a termre_formulakeeps, such as(1 | g)atNULL, every route carries that level's draw; see the section "An unseen level of a(1 | g)term" offrm_curve().- level
Coverage of both intervals.
- simultaneous
Compute the simultaneous band.
FALSEreturns the pointwise interval alone and skips the simulation.- nsim
Draws in the max-deviation simulation. The default 10000 puts the Monte Carlo error of the critical value near 0.013; 200000 puts it near 0.003.
- eps
Step size.
NULL, the default, is1e-6of the grid's range atorder = 1and1e-4of it atorder = 2.- seed
Seed for the simulation, for a reproducible band.
- tol
Largest relative disagreement with
frm_linpred(se.fit = TRUE)the assembled covariance may show before the call refuses.
Value
A frmtmb_curve data frame, as frm_curve() returns, whose
.estimate is the derivative.
Details
The derivative is taken by central differences of the DESIGN, not of
the fitted values, so the estimate and its standard error describe one
function rather than two. Writing D for the differenced design and
V for the joint coefficient covariance, the reported curve is D c
and its covariance is D V D', which is exact for the differenced
basis; the only approximation is the difference itself, and eps
controls that.
On a difference curve
A frmtmb_curve from frm_curve(contrast = ) carries its second
grid here, so what is differentiated is the DIFFERENCE. var moves
in both grids together, so contrast must hold the same values of it
as newdata does.
A new newdata on a difference curve needs a new contrast with it,
and a call that gives one grid and not the other is refused. Without
the refusal the answer would be the derivative of the FIRST curve
alone, for an object whose every row is a difference.
Past a ps() knot span
Warned once per ps() term per call, as frm_curve()'s
section describes, and counted over the grid you passed rather than
over the three-point stencil the design is built on. A difference
warns for each of its two grids and says which one.
Past the outer knot the basis is exactly zero, so the DERIVATIVE
design is exactly zero and those rows carry a standard error of
exactly zero. They are covered with probability one, so they leave
the max-deviation simulation instead of standardizing a zero
deviation by a zero divisor; simultaneous = TRUE therefore still
returns a band, computed over the rows that carry uncertainty. A grid
on which EVERY row is past the outer knot has no such row, and the
call refuses rather than returning a band with no content.
Examples
set.seed(1)
dd <- data.frame(x = sort(runif(200)))
dd$y <- 2 * sin(pi * dd$x) + rnorm(200, 0, 0.4)
fit <- frm(bf(y ~ s(x, k = 8)),
family = stats::gaussian(), data = dd)
g <- data.frame(x = seq(0.05, 0.95, length.out = 19))
d1 <- frm_curve_deriv(fit, var = "x", order = 1, newdata = g,
simultaneous = FALSE)
# the derivative of 2 sin(pi x) is 2 pi cos(pi x)
head(cbind(g, fitted = d1$.estimate, truth = 2 * pi * cos(pi * g$x)))
#> x fitted truth
#> 1 0.05 4.992539 6.205829
#> 2 0.10 4.808136 5.975664
#> 3 0.15 4.574525 5.598359
#> 4 0.20 4.494088 5.083204
#> 5 0.25 4.589733 4.442883
#> 6 0.30 4.605192 3.693164