Skip to content

Model terms reader for the FactorsForm term specification - #232

Open
FBartos wants to merge 1 commit into
jasp-stats:masterfrom
FBartos:feature/model-terms-reader
Open

FBartos wants to merge 1 commit into
jasp-stats:masterfrom
FBartos:feature/model-terms-reader

Conversation

@FBartos

@FBartos FBartos commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a reader for FactorsForm model-terms options. It is the R side of the opt-in term specification for jasp-desktop's FactorsForm. prepareModelTerms() reads a model-terms option, with or without the term specification. For each model it returns:

  • the formula (modelTermsFormula()) or the numeric design columns (modelTermsDesign());

  • display labels for the fit's coefficients (modelTermsLabels());

  • the offsets (modelTermsOffset(), modelTermsConstantOffset()).

  • Without the term specification, the formula is exactly the one analyses built before. Analyses can switch to the reader without changing their results.

  • With the term specification, terms are defined once and combined into each model's terms:

    • definitions pairs a variable with a specification;
    • terms are products of definitions.
  • Catalogue: inst/modelTermSpecifications.json lists every specification once. Each entry gives its kind, whether it is offered by default or only on request, its title, its label pattern, and its parameters with their defaults. jasp-desktop embeds the same file at build time to build its table, so the desktop and R produce the same labels. To add or remove a specification, edit this file and add a builder in R/modelTerms.R; a test checks that every entry has a builder and that every label pattern fills in.

  • Specifications:

    • Transformations: center, scale, logarithms, power, root, Box-Cox, Yeo-Johnson, asinh, logit, probit, arcsine square root, orthogonal and raw polynomials, natural and B-splines, hinge, threshold, quantile bins, Fourier terms, rank and normal scores, Arrhenius.
    • Codings: treatment, simple, sum, Helmert and reverse Helmert, successive and forward differences, orthogonal polynomial, SAS, indicators.
    • Offsets: an offset by a variable, or a constant offset.
  • Fitting details:

    • Constants learned from the fitting data are embedded in the formula, so new data is coded the same way as the fitting data. Rank terms are flagged as sample-dependent (sampleDependent), like rank(x) in lm().
    • Categorical terms become coded matrices.
    • Supports restrictions (fixed coefficients) and nesting of consecutive models.
    • Every inconsistency stops with a message that names the model.
  • Version bumped to 0.20.6.

Related PRs

Merge this PR first. The desktop PR then moves its submodule to the merged commit, and jaspRegression needs a jaspBase that has the reader.

Tests

tests/testthat/test-modelTerms.R: 103 tests, 1199 expectations, all passing. They cover:

  • the basic-mode formulas;
  • every catalogue entry (it has a builder, and its label pattern fills in);
  • labels;
  • each coding compared with lm();
  • nesting, restrictions, bins, rank, and refusals.

🤖 Generated with Claude Code

prepareModelTerms() reads the value of a FactorsForm model-terms option, with or without the term
specification of jasp-desktop's FactorsForm, and gives each model's formula (modelTermsFormula()) or
numeric design columns (modelTermsDesign()), the display labels of a fit's coefficients
(modelTermsLabels()) and its offsets (modelTermsOffset(), modelTermsConstantOffset()).

- Without term specification the formula is exactly the one analyses built before.
- With it, terms are defined once (`definitions`: a variable and its specification) and combined
  into each model's terms (`terms`: products of definitions). inst/modelTermSpecifications.json lists
  every specification once (kind, offered by default or opt-in, title, label pattern, parameters with
  defaults); jasp-desktop embeds the same file to build its table, and the labels are the form's.
- Specifications: transformations (center, scale, logarithms, power, root, Box-Cox, Yeo-Johnson,
  asinh, logit, probit, arcsine square root, orthogonal and raw polynomials, natural and B-splines,
  hinge, threshold, quantile bins, Fourier terms, rank and normal scores, Arrhenius), codings
  (treatment, simple, sum, Helmert and reverse Helmert, successive and forward differences,
  orthogonal polynomial, SAS, indicators), and offsets of a variable and constant offsets.
- Constants learned on the fitting data are embedded in the formula, so new data is coded as the
  fitting data (rank terms are sample-dependent, like rank(x) in lm()); categorical terms are coded
  matrices; restrictions (fixed coefficients) and nesting of consecutive models; every inconsistency
  stops with a message naming the model.
- Tests: tests/testthat/test-modelTerms.R.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant