compute_tangent: Compute tangent line(s) from a 'modeler' object

View source: R/14_tangent.R

compute_tangentR Documentation

Compute tangent line(s) from a modeler object

Description

Computes the slope and intercept of the tangent line(s) to a fitted curve at one or more specified x-values.

Usage

compute_tangent(object, x = NULL, id = NULL)

Arguments

object

A fitted object of class modeler, created by modeler().

x

A numeric vector of x-values at which to compute tangent lines.

id

Optional vector of uids indicating which groups to compute tangent lines for. If NULL, all groups are used.

Value

A tibble with one row per tangent line and the following columns:

  • uid: unique identifier of the group.

  • fn_name: Name of the fitted function.

  • x: x-value where the tangent line is evaluated.

  • y: Fitted y-value at x.

  • slope: First derivative (slope of tangent) at x.

  • intercept: y-intercept of the tangent line.

Examples

library(flexFitR)
library(ggplot2)
data(dt_potato)
mod <- dt_potato |>
  modeler(
    x = DAP,
    y = Canopy,
    grp = Plot,
    fn = "fn_logistic",
    parameters = c(a = 4, t0 = 40, k = 100),
    subset = 2
  )
plot(mod)
tl <- compute_tangent(mod, x = c(48.35, 65))
print(tl)
plot(mod) +
  geom_abline(
    data = tl,
    mapping = aes(slope = slope, intercept = intercept),
    linetype = 2,
    color = "blue"
  ) +
  geom_point(
    data = tl,
    mapping = aes(x = x, y = y),
    shape = 8,
    color = "blue",
    size = 2
  )

flexFitR documentation built on April 16, 2025, 5:09 p.m.