| nl_fit | R Documentation |
Fits a nonlinear regression model for an outcome y with a focal
predictor x, modeled using natural cubic splines ("ns"),
B-splines ("bs"), or GAM smooths ("gam").
Version 2 additions: two-way and nested clustering via the
nested argument; random spline slopes via random_slope;
B-spline basis via method = "bs"; automatic df selection via
df = "auto".
nl_fit(
data,
y,
x,
time = NULL,
cluster = NULL,
nested = FALSE,
controls = NULL,
method = c("ns", "bs", "gam"),
df = 4,
df_range = 2:8,
df_criterion = c("AIC", "BIC"),
k = 5,
bs_degree = 3,
random_slope = FALSE,
family = stats::gaussian(),
...
)
data |
A data frame (often long format for longitudinal data). |
y |
Outcome variable name (string). |
x |
Focal nonlinear predictor name (string). Must be numeric. |
time |
Optional time variable name (string). |
cluster |
Optional character vector of grouping variable name(s) for
random effects, e.g. |
nested |
Logical; only used when |
controls |
Optional character vector of additional covariate names to include as linear fixed effects. |
method |
Spline basis to use: |
df |
Degrees of freedom for the spline basis. Supply a single integer
greater than or equal to 1, or the string |
df_range |
Integer vector of candidate df values evaluated when
|
df_criterion |
Information criterion used for automatic df selection:
|
k |
Basis dimension for |
bs_degree |
Polynomial degree for |
random_slope |
Logical; if |
family |
A family object such as |
... |
Additional arguments passed to the underlying fitting function
( |
An object of class "nl_fit" (a named list). It contains
the fitted model object, the method, variable names
(y, x, time, cluster, controls),
spline settings (df, df_selected, df_search,
k, bs_degree), flags (nested,
random_slope), the family, the model formula,
the call, and metadata used for prediction (x_info,
levels_info, control_defaults).
nl_predict, nl_derivatives,
nl_compare, nl_r2, nl_knots
## Not run:
# Single-level natural spline with automatic df selection
fit <- nl_fit(data = mydata, y = "score", x = "age", df = "auto")
# Two-way cross-classified clustering
fit2 <- nl_fit(
data = mydata,
y = "score",
x = "age",
cluster = c("student_id", "school_id"),
nested = FALSE
)
# Nested clustering (students within schools)
fit3 <- nl_fit(
data = mydata,
y = "score",
x = "age",
cluster = c("student_id", "school_id"),
nested = TRUE
)
# Random spline slopes
fit4 <- nl_fit(
data = mydata,
y = "score",
x = "age",
cluster = "id",
random_slope = TRUE
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.