nl_het: Cluster heterogeneity in nonlinear effects

View source: R/nl_het.R

nl_hetR Documentation

Cluster heterogeneity in nonlinear effects

Description

Quantifies and visualises how much the nonlinear relationship between x and y varies across clusters, using a model with random spline slopes.

The function:

  1. Refits the model with random spline slopes (if not already fitted with random_slope = TRUE).

  2. Extracts cluster-specific predicted curves (BLUPs).

  3. Returns a heterogeneity summary: variance of slopes across clusters at each x value, and a plot of the cluster-specific trajectories.

  4. Performs a likelihood-ratio test comparing the random-slope model against a random-intercept-only model to assess whether heterogeneity is statistically significant.

Usage

nl_het(
  object,
  n_clusters_plot = 30L,
  x_seq = NULL,
  level = 0.95,
  plot = TRUE,
  seed = 42L
)

Arguments

object

An nl_fit object fitted with a single cluster variable (and either random_slope = TRUE or FALSE; in the latter case the model is refit internally).

n_clusters_plot

Maximum number of cluster curves to display in the trajectory plot. Default 30L. Use Inf for all clusters.

x_seq

Optional numeric vector of x values for prediction.

level

Confidence level for the population-mean ribbon. Default 0.95.

plot

Logical; print the trajectory plot. Default TRUE.

seed

Integer seed for reproducibility when sub-sampling clusters. Default 42L.

Value

A list (invisibly) with:

trajectory_df

Long data frame of cluster-specific predicted values.

slope_variance

Named numeric: SD of first-derivative estimates across clusters at each x value.

lrt

LRT comparing random-slope vs random-intercept model (NULL if random_slope = TRUE was supplied).

plot

A ggplot object.

See Also

nl_fit, nl_derivatives


MultiSpline documentation built on April 16, 2026, 9:06 a.m.