fit_ml: Fit idiographic supervised machine-learning models

View source: R/idiographic_ml.R

fit_mlR Documentation

Fit idiographic supervised machine-learning models

Description

Fits train/test supervised prediction models in an idiographic design: each subject can receive a model trained only on that subject's earlier rows, and the same held-out rows can also be scored by a pooled model trained on all subjects' earlier rows. This mirrors individualized modelling designs where person-specific prediction is compared against a nomothetic pooled baseline.

The implementation is dependency-free beyond base R. Regression supports mean baseline, ordinary least squares, ridge, lasso, elastic net, principal component regression, k-nearest neighbours, and a one-split regression tree. Binary classification supports majority baseline, logistic regression, ridge/lasso/elastic-net logistic regression, linear discriminant analysis, Gaussian naive Bayes, k-nearest neighbours, and a one-split classification tree. Predictors are standardized using training rows only.

Usage

fit_ml(
  data,
  outcome,
  predictors,
  id,
  day = NULL,
  beep = NULL,
  task = c("auto", "regression", "classification"),
  model = NULL,
  estimator = NULL,
  compare = c("both", "individual", "pooled"),
  test_prop = 0.2,
  min_train = 10L,
  min_test = 1L,
  lambda = 1,
  alpha = 0.5,
  k = 5L,
  n_components = NULL,
  max_iter = 100L,
  tol = 1e-06,
  standardize = TRUE,
  keep_fits = FALSE,
  ...
)

fit_idiographic_ml(
  data,
  outcome,
  predictors,
  id,
  day = NULL,
  beep = NULL,
  task = c("auto", "regression", "classification"),
  model = NULL,
  estimator = NULL,
  compare = c("both", "individual", "pooled"),
  test_prop = 0.2,
  min_train = 10L,
  min_test = 1L,
  lambda = 1,
  alpha = 0.5,
  k = 5L,
  n_components = NULL,
  max_iter = 100L,
  tol = 1e-06,
  standardize = TRUE,
  keep_fits = FALSE,
  ...
)

fit_individualized_ml(
  data,
  outcome,
  predictors,
  id,
  day = NULL,
  beep = NULL,
  task = c("auto", "regression", "classification"),
  model = NULL,
  estimator = NULL,
  compare = c("both", "individual", "pooled"),
  test_prop = 0.2,
  min_train = 10L,
  min_test = 1L,
  lambda = 1,
  alpha = 0.5,
  k = 5L,
  n_components = NULL,
  max_iter = 100L,
  tol = 1e-06,
  standardize = TRUE,
  keep_fits = FALSE,
  ...
)

Arguments

data

A data.frame or matrix.

outcome

Character. Name of the outcome column.

predictors

Character vector of predictor columns.

id

Character. Name of the subject/person ID column.

day, beep

Optional ordering columns. Rows are ordered by id, day, and beep before the last rows for each subject are held out.

task

"auto", "regression", or "classification". Auto treats a numeric outcome as regression and a two-level non-numeric outcome as binary classification.

model

NULL for the task default, "all" for all native models for the selected task, or a character vector of simple model names. Regression models are "mean", "linear", "ridge", "lasso", "elastic", "pcr", "knn", and "tree". Classification models are "majority", "logistic", "ridge", "lasso", "elastic", "lda", "bayes", "knn", and "tree".

estimator

NULL for each model's default estimator, or a named character vector/list mapping model names to estimator names. The native base-R estimator is "native". This is where package-specific backends belong when the same model can be estimated more than one way.

compare

Which models to fit: "both" (default), "individual", or "pooled".

test_prop

Proportion of each subject's ordered rows held out from the end of the series. Default 0.2.

min_train

Minimum complete training rows required for a model. Default 10.

min_test

Minimum held-out rows required per subject. Default 1.

lambda

Ridge penalty for model = "ridge". The intercept is not penalized. Also used by lasso and elastic-net models. Default 1.

alpha

Elastic-net mixing value in ⁠[0, 1]⁠; 0 is ridge and 1 is lasso. Default 0.5.

k

Number of neighbours for model = "knn". Default 5.

n_components

Number of principal components for model = "pcr". Default uses min(5, n_predictors, n_train - 1).

max_iter

Maximum iterations for coordinate-descent penalized models. Default 100.

tol

Convergence tolerance for iterative models. Default 1e-6.

standardize

Logical. Standardize predictors using training-set means and SDs? Default TRUE.

keep_fits

Logical. Store fitted internal model objects? Default FALSE.

...

Optional model controls using the same names as the explicit tuning arguments (lambda, alpha, k, n_components, max_iter, or tol). Unknown names are rejected.

Value

An idioml_result with ⁠$predictions⁠, ⁠$metrics⁠, ⁠$coefficients⁠, ⁠$failures⁠, and optionally ⁠$fits⁠.

Examples

set.seed(1)
d <- data.frame(
  id = rep(1:4, each = 40),
  beep = rep(seq_len(40), 4),
  x1 = rnorm(160),
  x2 = rnorm(160)
)
d$y <- 0.4 * d$x1 - 0.2 * d$x2 + rep(c(-1, 0, 1, 0.5), each = 40) +
  rnorm(160, sd = 0.4)
fit <- fit_ml(d, outcome = "y", predictors = c("x1", "x2"),
              id = "id", beep = "beep",
              model = c("linear", "ridge", "knn"))
fit$metrics
coefs(fit)

idiographic documentation built on Aug. 4, 2026, 1:07 a.m.