Validation matrix
generators/analytic/reviewed_reference_corrections.py
"""Reproduce STAT-reviewed analytical corrections over retained R references.
Only the specified eigenvalues and complete Turnbull mass/survival block change.
The archived original references, dataset hashes and independent proofs are inputs;
no product results are read.
"""
from __future__ import annotations
import copy
import hashlib
import json
from decimal import Decimal, localcontext
from fractions import Fraction
from pathlib import Path
from typing import Any
from tabelz.validation.statistical.indexing import write_json
ROOT = Path(__file__).resolve().parents[2]
REVISION_PATH = "reference_data/accuracy_reference_revisions.json"
PROOF_PATH = "reference_data/accuracy_adjudications.json"
def correction_rationale(proof: str) -> str:
return (
f"Independent analytical reference correction: {proof}; "
"retained proof in accuracy_adjudications.json."
)
def _read_bound(root: Path, reference: dict[str, str]) -> dict[str, Any]:
content = (root / reference["path"]).read_bytes()
if hashlib.sha256(content).hexdigest() != reference["sha256"]:
raise ValueError(f"Reference revision input changed: {reference['path']}")
return json.loads(content)
def corrected_reference(root: Path, case_id: str) -> dict[str, Any]:
revisions = json.loads((root / REVISION_PATH).read_bytes())
proof = _read_bound(root, revisions["proof"])
revision = revisions["cases"][case_id]
original = _read_bound(root, revision["original_expected"])
data = (root / revision["csv"]["path"]).read_bytes()
if hashlib.sha256(data).hexdigest() != revision["csv"]["sha256"]:
raise ValueError("Analytical correction applies only to the reviewed dataset")
result = copy.deepcopy(original)
entries = [
item
for item in proof["entries"]
if item["case_id"] == case_id
and item["disposition"] == "approved_mathematical_zero_reference_correction"
]
if not entries:
raise ValueError("No approved correction for this case")
for item in entries:
if result["outputs"][item["path"]] != item["retained_reference"]:
raise ValueError("Analytical correction preimage differs")
result["outputs"][item["path"]].update(
value="0",
rationale=correction_rationale(item["proof"]),
)
if case_id == "STAT-SURV-NP-002":
solution = proof["proofs"]["turnbull_exact_kkt"]
with localcontext() as context:
context.prec = 70
sqrt5 = Decimal(5).sqrt()
def decimal_fraction(value: str) -> Decimal:
fraction = Fraction(value)
return Decimal(fraction.numerator) / Decimal(fraction.denominator)
masses = [
decimal_fraction(a) + decimal_fraction(b) * sqrt5
for a, b in solution["mass_Qsqrt5_coefficients"]
]
# Sum the remaining masses directly, avoiding subtractive residue at zero.
for index, mass in enumerate(masses):
for field, value in (
("mass", mass),
("survival_before", sum(masses[index:], Decimal(0))),
("survival_after", sum(masses[index + 1 :], Decimal(0))),
):
result["outputs"][f"/estimate/turnbull/{index}/{field}"].update(
value=str(value) if value else "0",
rationale=correction_rationale("turnbull_exact_kkt"),
)
result["generator_run"] = {
"reference_revision": revisions["version"],
"original_expected": revision["original_expected"],
"retained_generator": revision["retained_generator"],
"proof": revisions["proof"],
"corrections": sorted({item["proof"] for item in entries}),
}
return result
def main() -> None:
revisions = json.loads((ROOT / REVISION_PATH).read_bytes())
for case_id, revision in revisions["cases"].items():
write_json(ROOT / revision["expected_path"], corrected_reference(ROOT, case_id))
if __name__ == "__main__":
main()
generators/build_harvested_cases.py
"""Transcribe source-attributed unit-test fixtures without running Tabelz."""
from __future__ import annotations
import argparse
import csv
from decimal import Decimal, getcontext
from pathlib import Path
from tabelz.validation.statistical.indexing import write_json
ROOT = Path(__file__).resolve().parents[1]
DATA = ROOT / "reference_data"
SURVIVAL_TEST = "tests/unit/analysis/test_survival_nonparametric.py"
MP_TIMES = [6, 6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35]
MP_EVENTS = [1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]
PLACEBO_TIMES = [1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23]
def _dataset(
dataset_id: str,
columns: list[tuple[str, str]],
rows: list[tuple[object, ...]],
source: str,
citation: str,
) -> None:
csv_path = f"reference_data/datasets/{dataset_id}.csv"
with (ROOT / csv_path).open("w", newline="", encoding="utf-8") as stream:
writer = csv.writer(stream, lineterminator="\n")
writer.writerow([name for name, _dtype in columns])
writer.writerows(rows)
write_json(
DATA / "datasets" / f"{dataset_id}.json",
{
"schema_version": "2.0.0",
"dataset_id": dataset_id,
"source": source,
"citation": citation,
"license_note": "Transcribed from repository unit test; original source not rechecked.",
"columns": [{"name": name, "dtype": dtype} for name, dtype in columns],
"row_count": len(rows),
"csv": {"path": csv_path, "sha256": ""},
},
)
def _printed(value: str, absolute: str, rationale: str) -> dict[str, str]:
return {
"comparison": "tolerance",
"value": value,
"absolute_tolerance": absolute,
"relative_tolerance": "0",
"rationale": rationale,
}
def _relative(value: Decimal, rationale: str) -> dict[str, str]:
return {
"comparison": "tolerance",
"value": f"{value:.20e}",
"absolute_tolerance": "0",
"relative_tolerance": "1e-9",
"rationale": f"Closed-form distribution function; owner 1e-9 tier. {rationale}",
}
def _t_upper_even_df(t: Decimal, nu: int) -> Decimal:
"""P(T > t) for t >= 0 and even nu, from the exact finite series.
F(t) = 1/2 + (x/2) sum_{k<nu/2} C(2k,k)/4^k (1-x^2)^k with x = t/sqrt(nu+t^2)
(Abramowitz and Stegun 26.7.3).
"""
x = t / (nu + t * t).sqrt()
q = 1 - x * x
coefficient = Decimal(1)
total = Decimal(0)
for k in range(nu // 2):
if k:
coefficient = coefficient * (2 * k - 1) / (2 * k)
total += coefficient * q**k
return Decimal("0.5") - x / 2 * total
def _grubbs_exact(values: tuple[float, ...], alpha: str) -> tuple[Decimal, Decimal, Decimal]:
"""Grubbs G, critical value and p-value to 60 digits (manual chapter 30)."""
getcontext().prec = 60
xs = [Decimal(repr(value)) for value in values]
n = len(xs)
nu = n - 2
if nu % 2:
raise ValueError("The exact series needs even n - 2.")
mean = sum(xs) / n
sd = (sum((x - mean) ** 2 for x in xs) / (n - 1)).sqrt()
g = max(abs(x - mean) for x in xs) / sd
target = Decimal(alpha) / (2 * n)
low, high = Decimal(0), Decimal(1000)
for _ in range(400):
middle = (low + high) / 2
if _t_upper_even_df(middle, nu) > target:
low = middle
else:
high = middle
t = (low + high) / 2
critical = (n - 1) / Decimal(n).sqrt() * (t * t / (nu + t * t)).sqrt()
t_of_g = (n * nu * g * g / ((n - 1) ** 2 - n * g * g)).sqrt()
p_value = min(Decimal(1), 2 * n * _t_upper_even_df(t_of_g, nu))
return g, critical, p_value
def _exact(value: int | str, rationale: str) -> dict[str, object]:
return {"comparison": "exact", "value": value, "rationale": rationale}
def _case(
case_id: str,
dataset_id: str,
entry_point: str,
menu_item: str,
config: dict[str, object],
outputs: dict[str, dict[str, object] | dict[str, str]],
citation: str,
test_name: str,
notes: list[str],
oracle_class: str = "published",
oracle_notes: list[str] | None = None,
generator_path: str | None = None,
) -> None:
expected_path = f"reference_data/expected/harvested/{case_id}.json"
write_json(
ROOT / expected_path,
{
"schema_version": "2.0.0",
"case_id": case_id,
"outputs": outputs,
"expected_error": None,
"deferred_outputs": [],
"generator_run": None,
},
)
write_json(
DATA / "cases" / "harvested" / f"{case_id}.json",
{
"schema_version": "2.0.0",
"case_id": case_id,
"version": "1.0.0",
"family": "harvested",
"entry_point": entry_point,
"menu_items": [menu_item],
"title": test_name.replace("_", " "),
"case_kind": "nominal",
"dataset": {"path": f"reference_data/datasets/{dataset_id}.json", "sha256": ""},
"excluded_rows": [],
"configuration": config,
"repeat": 1,
"expected": {"path": expected_path, "sha256": ""},
"oracle": {
"oracle_class": oracle_class,
"source": citation,
"citation": citation,
"generator": (
{"path": generator_path, "sha256": ""} if generator_path is not None else None
),
"independent": True,
"convention_notes": oracle_notes
or [
(
f"Transcribed from {notes[0]}::{test_name}; "
"not yet re-checked against the original source."
),
*notes[1:],
],
},
"supported": True,
"unsupported_reason": None,
"requirement_ids": ["REQ-STAT-HARVEST-001"],
"risk_ids": ["RISK-REFERENCE-001"],
"review_reference": None,
},
)
def _grubbs() -> None:
dataset_id = "HARVEST_NIST_GRUBBS_PRIMARY"
source = (
"NIST e-Handbook 1.3.5.17.1, Grubbs' Test for Outliers, "
"https://www.itl.nist.gov/div898/handbook/eda/section3/eda35h1.htm"
)
values = (199.31, 199.53, 200.19, 200.82, 201.92, 201.95, 202.18, 245.57)
_dataset(dataset_id, [("x", "Float64")], [(x,) for x in values], source, source)
g, critical, p_value = _grubbs_exact(values, "0.05")
_case(
"STAT-HARV-NIST-GRUBBS-001",
dataset_id,
"tabelz.executor.distribution_diagnostics",
"distributionDiagnosticsMenuItem",
{"column": "x", "method": "grubbs"},
{
"/statistic": _relative(g, "G = max|x - mean| / s in 60-digit decimal."),
"/critical_value": _relative(
critical, "(n-1)/sqrt(n) sqrt(t^2/(n-2+t^2)), t the alpha/(2n) upper quantile."
),
"/p_value": _relative(p_value, "min(1, 2n P(T > t_G)) with n - 2 = 6 df."),
},
source,
"test_grubbs_matches_nist_fixture_and_preserves_candidate",
["tests/unit/analysis/test_distribution_diagnostics.py"],
oracle_class="analytic",
oracle_notes=[
"Eight uranium-isotope measurements verified against NIST e-Handbook 1.3.5.17.1.",
"This case uses NIST's two-sided critical-value formula; its worked "
"example instead prints the upper one-sided critical value 2.032.",
"Expected values are computed here in 60-digit decimal from the exact even-df "
"Student t series (Abramowitz and Stegun 26.7.3), independent of SciPy.",
"Until 2026-09-25 the expectations were the unit test's 14-digit values, which "
"were SciPy output: the critical value was truncated and the p-value 1.7e-13 off.",
],
generator_path="generators/build_harvested_cases.py",
)
def _dunnett() -> None:
dataset_id = "HARVEST_SCIPY_DUNNETT"
citation = "SciPy official Dunnett documentation: two treatments against one control."
values = [2.9, 3.0, 2.5, 2.6, 3.2, 3.8, 2.7, 4.0, 2.4, 2.8, 3.4, 3.7, 2.2, 2.0]
groups = ["Control"] * 5 + ["T1"] * 4 + ["T2"] * 5
_dataset(
dataset_id,
[("y", "Float64"), ("g", "String")],
list(zip(values, groups, strict=True)),
citation,
citation,
)
_case(
"STAT-HARV-SCIPY-DUNNETT-001",
dataset_id,
"tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem",
{"response_column": "y", "group_column": "g", "control_level": "Control", "seed": 12345},
{
"/comparisons/0/statistic": _printed(
"0.90874545", "1e-7", "Unit-test tolerance for rounded statistic."
),
"/comparisons/1/statistic": _printed(
"-0.05007117", "1e-7", "Unit-test tolerance for rounded statistic."
),
"/comparisons/0/p_value": _printed(
"0.58325", "0.001", "Unit-test tolerance for randomized multivariate integration."
),
"/comparisons/1/p_value": _printed(
"0.99819", "0.001", "Unit-test tolerance for randomized multivariate integration."
),
},
citation,
"test_dunnett_matches_published_small_reference",
[
"tests/unit/analysis/test_rank_comparison_equivalence_boundaries.py",
(
"Unit test notes randomized integration and rounded published probabilities; "
"seed 12345 retained."
),
],
)
def _survival() -> None:
citation = "Freireich et al. (1963); Gehan (1965), 6-MP/placebo remission data."
columns = [("time", "Float64"), ("status", "Int64"), ("arm", "String")]
mp_rows = list(zip(MP_TIMES, MP_EVENTS, ["6-MP"] * 21, strict=True))
_dataset("HARVEST_GEHAN_MP", columns, mp_rows, citation, citation)
both_rows = mp_rows + list(zip(PLACEBO_TIMES, [1] * 21, ["placebo"] * 21, strict=True))
_dataset("HARVEST_GEHAN_BOTH", columns, both_rows, citation, citation)
common = {
"time_column": "time",
"event_column": "status",
"event_value": "1",
"censored_value": "0",
}
printed = {
6.0: (21, 3, "0.8571", "0.0764", "0.6197", "0.9516"),
7.0: (17, 1, "0.8067", "0.0869", "0.5631", "0.9228"),
10.0: (15, 1, "0.7529", "0.0963", "0.5032", "0.8894"),
13.0: (12, 1, "0.6902", "0.1068", "0.4316", "0.8491"),
16.0: (11, 1, "0.6275", "0.1141", "0.3675", "0.8049"),
22.0: (7, 1, "0.5378", "0.1282", "0.2678", "0.7468"),
23.0: (6, 1, "0.4482", "0.1346", "0.1881", "0.6801"),
}
outputs: dict[str, dict[str, object] | dict[str, str]] = {
"/curves/0/n": _exact(21, "Unit-test 6-MP count."),
"/curves/0/events": _exact(9, "Unit-test 6-MP event count."),
"/curves/0/censored": _exact(12, "Unit-test 6-MP censored count."),
"/curves/0/median/time": _printed("23.0", "0", "Unit-test median time."),
}
unique_times = sorted(set(MP_TIMES))
for time, (risk, deaths, survival, se, lower, upper) in printed.items():
index = unique_times.index(time)
base = "/curves/0"
outputs[f"{base}/at_risk/{index}"] = _exact(risk, "Published risk count in unit test.")
outputs[f"{base}/event_counts/{index}"] = _exact(
deaths, "Published event count in unit test."
)
for field, value in (
("survival", survival),
("standard_error", se),
("lower", lower),
("upper", upper),
):
outputs[f"{base}/{field}/{index}"] = _printed(
value, "0.00005", "Published Greenwood/log-log table printed to four decimals."
)
_case(
"STAT-HARV-GEHAN-KM-001",
"HARVEST_GEHAN_MP",
"tabelz.executor.survival",
"survivalMenuItem",
common,
outputs,
citation,
"test_six_mp_table_matches_the_published_greenwood_log_log_values",
[
SURVIVAL_TEST,
"R also distributes Gehan 6-MP data as MASS::gehan; cross-check in V16-V22.",
],
)
_case(
"STAT-HARV-GEHAN-LOGRANK-001",
"HARVEST_GEHAN_BOTH",
"tabelz.executor.survival",
"survivalMenuItem",
{**common, "group_column": "arm"},
{
"/log_rank/observed/0": _exact(9, "Unit-test observed 6-MP events."),
"/log_rank/observed/1": _exact(21, "Unit-test observed placebo events."),
"/log_rank/expected/0": _printed(
"19.25", "0.005", "Published log-rank expected count printed to 2 decimals."
),
"/log_rank/expected/1": _printed(
"10.75", "0.005", "Published log-rank expected count printed to 2 decimals."
),
"/log_rank/statistic": _printed(
"16.79", "0.005", "Published chi-square printed to 2 decimals."
),
"/log_rank/degrees_of_freedom": _exact(1, "Unit-test log-rank df."),
"/log_rank/p_value": _printed(
"4.17e-5", "0.00000005", "Unit test rounded p-value and used 1% relative tolerance."
),
"/curves/1/median/time": _printed("8.0", "0", "Unit-test placebo median time."),
},
citation,
"test_gehan_log_rank_matches_the_published_chi_square",
[
SURVIVAL_TEST,
"R also distributes Gehan 6-MP data as MASS::gehan; cross-check in V16-V22.",
],
)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--grubbs-only", action="store_true")
args = parser.parse_args()
(DATA / "cases" / "harvested").mkdir(parents=True, exist_ok=True)
(DATA / "expected" / "harvested").mkdir(parents=True, exist_ok=True)
_grubbs()
if args.grubbs_only:
return
_dunnett()
_survival()
if __name__ == "__main__":
main()
generators/chart_published_reference.py
"""Retain published chart inputs separately from derived full-precision oracles.
Run from the repository root. This generator uses only source transcription and
independent arithmetic; it never imports Tabelz chart implementations.
"""
from __future__ import annotations
import csv
import hashlib
import math
from pathlib import Path
from chart_published_sequences import LOWRY_T2, LOWRY_Z, LUCAS_EWMA, MONTGOMERY_EWMA
from tabelz.validation.statistical.indexing import write_json
ROOT = Path(__file__).resolve().parents[1]
DATA = ROOT / "reference_data/datasets"
CASES = ROOT / "reference_data/cases/quality"
EXPECTED = ROOT / "reference_data/expected/quality"
PUBLISHED = ROOT / "reference_data/published/quality"
GENERATOR = "generators/chart_published_reference.py"
SEQUENCES = "generators/chart_published_sequences.py"
def digest(path: Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()
def tolerance(value: float, rationale: str, absolute: str = "1e-10") -> dict[str, object]:
return {
"comparison": "tolerance",
"value": format(value, ".17g"),
"absolute_tolerance": absolute,
"relative_tolerance": "1e-9",
"rationale": rationale,
}
def exact(value: int | str | bool, rationale: str) -> dict[str, object]:
return {"comparison": "exact", "value": value, "rationale": rationale}
def sequence(values: list[float], rationale: str, absolute: str = "1e-10") -> dict[str, object]:
return {
"comparison": "sequence",
"values": [format(value, ".17g") for value in values],
"absolute_tolerance": absolute,
"relative_tolerance": "1e-9",
"rationale": rationale,
}
def ewma(values: list[float], center: float, weight: float) -> list[float]:
level = center
result = []
for value in values:
level = weight * value + (1 - weight) * level
result.append(level)
return result
def published(name: str, source: str, evidence: dict[str, object]) -> str:
PUBLISHED.mkdir(parents=True, exist_ok=True)
path = PUBLISHED / f"{name}.json"
write_json(path, {"schema_version": "1.0.0", "source": source, **evidence})
return f"{path.relative_to(ROOT).as_posix()} sha256={digest(path)}"
def dataset(
identifier: str,
columns: list[tuple[str, str]],
rows: list[tuple[object, ...]],
source: str,
citation: str,
) -> str:
csv_path = DATA / f"{identifier}.csv"
with csv_path.open("w", encoding="utf-8", newline="") as stream:
writer = csv.writer(stream, lineterminator="\n")
writer.writerow([name for name, _ in columns])
writer.writerows(rows)
write_json(
DATA / f"{identifier}.json",
{
"schema_version": "2.0.0",
"dataset_id": identifier,
"source": source,
"citation": citation,
"license_note": (
"Source numbers transcribed for reproducible statistical validation; "
"constructed rows are identified in source."
),
"columns": [{"name": name, "dtype": dtype} for name, dtype in columns],
"row_count": len(rows),
"csv": {
"path": f"reference_data/datasets/{identifier}.csv",
"sha256": digest(csv_path),
},
},
)
return identifier
def case(
number: int,
title: str,
entry: str,
menu: str,
dataset_id: str,
configuration: dict[str, object],
outputs: dict[str, object],
citation: str,
notes: list[str],
) -> str:
identifier = f"STAT-QUAL-PUBCHART-{number:03d}"
write_json(
EXPECTED / f"{identifier}.json",
{
"schema_version": "2.0.0",
"case_id": identifier,
"outputs": outputs,
"expected_error": None,
"deferred_outputs": [],
"generator_run": {},
},
)
write_json(
CASES / f"{identifier}.json",
{
"schema_version": "2.0.0",
"case_id": identifier,
"version": "1.0.0",
"family": "quality",
"entry_point": entry,
"menu_items": [menu],
"title": title,
"case_kind": "nominal",
"dataset": {
"path": f"reference_data/datasets/{dataset_id}.json",
"sha256": digest(DATA / f"{dataset_id}.json"),
},
"excluded_rows": [],
"configuration": configuration,
"repeat": 1,
"expected": {
"path": f"reference_data/expected/quality/{identifier}.json",
"sha256": digest(EXPECTED / f"{identifier}.json"),
},
"oracle": {
"oracle_class": "analytic",
"source": "Published inputs and independent analytic calculation",
"citation": citation,
"generator": {"path": GENERATOR, "sha256": digest(ROOT / GENERATOR)},
"independent": True,
"convention_notes": [
*notes,
"Printed sequence transcription: "
f"{SEQUENCES} sha256={digest(ROOT / SEQUENCES)}",
],
},
"supported": True,
"unsupported_reason": None,
"requirement_ids": ["REQ-STAT-QUAL-001"],
"risk_ids": ["RISK-NUMERIC-001"],
"review_reference": None,
},
)
return identifier
def main() -> None:
spc = "tabelz.analysis.spc:compute_spc"
attribute = "tabelz.analysis.spc_attributes:compute_attribute_spc"
weighted = "tabelz.analysis.spc_timeweighted:compute_time_weighted"
nist_r = "https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc321.htm"
dataset(
"PUB_NIST_R5",
[("value", "Float64")],
[(x,) for x in [0, 1, 2, 3, 4, 1, 2, 3, 4, 5]],
"Constructed two subgroups of five with R-bar=4, to exercise NIST's n=5 D4 factor.",
nist_r,
)
case(
1,
"NIST R-chart D4(5): printed table versus full precision",
spc,
"spcMenuItem",
"PUB_NIST_R5",
{"y_column": "value", "subgroup_mode": "constant", "subgroup_size": 5, "run_rules": []},
{
"/secondary/center_line": tolerance(4, "Constructed R-bar = 4."),
"/secondary/upper_control_limit": tolerance(
4 * 2.1144991450958885,
"Full precision D4(5) times R-bar; NIST table prints D4=2.115.",
),
},
nist_r,
[
"NIST's printed D4(5)=2.115 is retained as source evidence, "
"not rounded into the full-precision expected output."
],
)
wafers = [
16,
14,
28,
16,
12,
20,
10,
12,
10,
17,
19,
17,
14,
16,
15,
13,
14,
16,
11,
20,
11,
19,
16,
31,
13,
]
nist_c = "https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc331.htm"
dataset(
"PUB_NIST_WAFERS",
[("defects", "Int64")],
[(x,) for x in wafers],
"NIST published 25 wafer defect counts, total 400.",
nist_c,
)
case(
2,
"NIST wafer c chart with strict on-limit decision",
attribute,
"spcMenuItem",
"PUB_NIST_WAFERS",
{"count_column": "defects", "chart": "c", "run_rules": []},
{
"/chart/center_line": tolerance(16, "NIST published c-bar=16."),
"/chart/lower_control_limit": tolerance(4, "NIST published LCL=4."),
"/chart/upper_control_limit": tolerance(28, "NIST published UCL=28."),
"/chart/points/2/value": tolerance(
28, "Wafer 3 is exactly on UCL and does not signal."
),
"/chart/points/23/value": tolerance(31, "Wafer 24 exceeds UCL."),
"/violation_keys": {
"comparison": "set",
"values": ["primary:rule_1:23"],
"rationale": "Strict crossing gives only wafer 24.",
},
},
nist_c,
[
"All 25 source counts are transcribed; NIST's stated center and "
"limits have integer precision."
],
)
nist_cusum = "https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc323.htm"
dataset(
"PUB_NIST_CUSUM_DERIVED",
[("value", "Float64")],
[(x,) for x in [1, 1, 1, -1]],
"Constructed four observations for NIST's tabular CUSUM recursion.",
nist_cusum,
)
case(
3,
"NIST tabular CUSUM strict H boundary",
weighted,
"spcMenuItem",
"PUB_NIST_CUSUM_DERIVED",
{
"y_column": "value",
"method": "cusum",
"target": 0,
"sigma": 1,
"cusum_k": 0.5,
"cusum_h": 1,
},
{
**{
f"/primary/points/{i}/value": tolerance(
x, "Hand recursion C+ = max(0,C+ previous + x - 0.5)."
)
for i, x in enumerate([0.5, 1, 1.5, 0])
},
"/violation_keys": {
"comparison": "set",
"values": ["primary:rule_1:2"],
"rationale": "C+=H at step 2 is not a breach; step 3 is.",
},
},
nist_cusum,
[
"The four inputs and exact outputs are derived from NIST's formula, "
"not transcribed from a NIST example."
],
)
montgomery = (
"Montgomery, Introduction to Statistical Quality Control, 6th ed., "
"Examples 9.2-9.3 and Tables 9.10/9.14, pp. 421-429"
)
observations = [
9.45,
7.99,
9.29,
11.66,
12.16,
10.18,
8.04,
11.46,
9.2,
10.34,
9.03,
11.47,
10.51,
9.4,
10.08,
9.37,
10.62,
10.31,
8.52,
10.84,
10.9,
9.33,
12.29,
11.5,
10.6,
11.08,
10.38,
11.62,
11.31,
10.52,
]
dataset(
"PUB_MONTGOMERY_9_2_9_3",
[("value", "Float64"), ("row_id", "String")],
[(x, str(i)) for i, x in enumerate(observations)],
"Montgomery published 30 observations for Examples 9.2 and 9.3.",
montgomery,
)
montgomery_derived = ewma(observations, 10.0, 0.1)
montgomery_upper = [
10 + 2.7 * math.sqrt(0.1 / 1.9 * (1 - 0.9 ** (2 * i))) for i in range(1, 31)
]
montgomery_evidence = published(
"PUB_MONTGOMERY_EWMA_SEQUENCE",
montgomery,
{
"printed_ewma": MONTGOMERY_EWMA,
"derived_from_printed_observations": [format(x, ".17g") for x in montgomery_derived],
"printed_signal_observations": [29, 30],
"precision_note": (
"Table 9.10 prints 3 to 6 decimals; derived values and "
"finite-time limits use its printed observations and stated "
"lambda=0.1, L=2.7, target=10, sigma=1."
),
},
)
case(
4,
"Montgomery Example 9.2 finite-time EWMA",
weighted,
"spcMenuItem",
"PUB_MONTGOMERY_9_2_9_3",
{
"y_column": "value",
"method": "ewma",
"target": 10,
"sigma": 1,
"ewma_lambda": 0.1,
"ewma_l": 2.7,
},
{
**{
f"/primary/points/{i}/value": tolerance(
value, "Independent EWMA recursion from published observations."
)
for i, value in enumerate(montgomery_derived)
},
"/primary/point_upper_limits": sequence(
montgomery_upper,
"Exact finite-time upper limits from stated known parameters.",
),
"/primary/point_lower_limits": sequence(
[20 - value for value in montgomery_upper],
"Exact finite-time lower limits symmetric about target 10.",
),
"/violation_keys": {
"comparison": "set",
"values": ["primary:rule_1:28", "primary:rule_1:29"],
"rationale": "Finite-time limits signal observations 29 and 30.",
},
},
montgomery,
[
"All 30 printed EWMA values are retained at source precision in " + montgomery_evidence,
"Expected values are independently recursed from the printed input series; "
"the source's rounded output is not treated as full precision.",
],
)
averages = [
9.45,
8.72,
8.91,
9.5975,
10.11,
10.256,
10.266,
10.7,
10.208,
9.844,
9.614,
10.3,
10.11,
10.15,
10.098,
10.166,
9.996,
9.956,
9.78,
9.932,
10.238,
9.98,
10.376,
10.972,
10.924,
10.96,
11.17,
11.036,
10.998,
10.982,
]
case(
5,
"Montgomery Example 9.3 all 30 moving averages",
"tabelz.executor.spc_moving_average",
"spcMovingAverageMenuItem",
"PUB_MONTGOMERY_9_2_9_3",
{"value_column": "value", "window_width": 5, "historical_mean": 10, "historical_sigma": 1},
{
**{
f"/points/{i}/moving_average": tolerance(
x, "Published Table 9.14 average from the printed observations."
)
for i, x in enumerate(averages)
},
"/points/0/window_size": exact(1, "Expanding startup."),
"/points/4/window_size": exact(5, "Steady window width."),
"/points/4/ucl": tolerance(
10 + 3 / math.sqrt(5),
"Known sigma yields full-precision steady upper limit; source prints 11.34.",
),
},
montgomery,
[
"Published averages are transcribed at their printed decimals; "
"steady-state printed limits 8.66/11.34 remain distinct from "
"full-precision products."
],
)
lucas = "Lucas and Saccucci, Technometrics 32(1), 1990, Table 1"
lucas_values = [
1,
-0.5,
0,
-0.8,
-0.8,
-1.2,
1.5,
-0.6,
1,
-0.9,
1.2,
0.5,
2.6,
0.7,
1.1,
2,
1.4,
1.9,
0.8,
]
dataset(
"PUB_LUCAS_EWMA",
[("value", "Float64")],
[(x,) for x in lucas_values],
"Lucas-Saccucci published Table 1 observations.",
lucas,
)
lucas_derived = ewma(lucas_values, 0.0, 0.25)
lucas_evidence = published(
"PUB_LUCAS_EWMA_SEQUENCE",
lucas,
{
"printed_ewma": LUCAS_EWMA,
"derived_from_printed_observations": [format(x, ".17g") for x in lucas_derived],
"printed_asymptotic_limits": ["-1.134", "+1.134"],
"printed_signal_observations": [16, 17, 18, 19],
"precision_note": (
"Table 1 prints EWMA to three decimals; expectations use exact "
"recursion from the printed observations, while Tabelz evaluates "
"finite-time limits."
),
},
)
case(
6,
"Lucas-Saccucci Table 1 EWMA finite-time decisions",
weighted,
"spcMenuItem",
"PUB_LUCAS_EWMA",
{
"y_column": "value",
"method": "ewma",
"target": 0,
"sigma": 1,
"ewma_lambda": 0.25,
"ewma_l": 3,
},
{
**{
f"/primary/points/{i}/value": tolerance(
value, "Independent EWMA recursion from the published Table 1 observations."
)
for i, value in enumerate(lucas_derived)
},
"/violation_keys": {
"comparison": "set",
"values": [f"primary:rule_1:{i}" for i in [15, 16, 17, 18]],
"rationale": "Signals at published observations 16-19.",
},
},
lucas,
[
"All 19 printed and independently derived EWMA values are retained in "
+ lucas_evidence,
"Table displays asymptotic limits +/-1.134; product comparisons "
"use finite-time limits from the printed lambda/L inputs.",
],
)
minitab = "https://support.minitab.com/en-us/minitab/help-and-how-to/quality-and-process-improvement/control-charts/how-to/variables-charts-for-individuals/z-mr-chart/methods-and-formulas/methods-and-formulas/"
dataset(
"PUB_ZMR_DERIVED",
[("value", "Float64"), ("product", "String")],
[(x, "A") for x in [0, 1, 2, 3]],
"Constructed standardized observations for Minitab Z-MR factor comparison.",
minitab,
)
case(
7,
"Minitab Z-MR printed factors versus full precision",
"tabelz.executor.spc_short_run",
"spcShortRunMenuItem",
"PUB_ZMR_DERIVED",
{
"measurement_column": "value",
"product_column": "product",
"products": [{"product": "A", "center": 0, "sigma": 1}],
},
{
"/mr_chart/upper_control_limit": tolerance(
3.6858865663777776,
"Full precision d2+3d3; Minitab printed rounded factors are "
"provenance, not this exact target.",
),
},
minitab,
[
"Minitab's factor display is rounded. The product uses d2=2/sqrt(pi), "
"d3=sqrt(2-4/pi), with D4=3.2665319192886013 derived."
],
)
benneyan = "Benneyan, Health Care Management Science 4(4), 2001, Example 1, p. 311"
dataset(
"PUB_BENNEYAN_G_TRANSLATED",
[("between", "Int64"), ("phase", "String")],
[
(x, "reference" if i < 5 else "monitoring")
for i, x in enumerate([41, 42, 42, 42, 44, 276, 277])
],
"Constructed one-based intervals with mean 42.2 translated from "
"Benneyan's published zero-based mean 41.2.",
benneyan,
)
case(
8,
"Benneyan geometric support translated to Tabelz g chart",
attribute,
"spcMenuItem",
"PUB_BENNEYAN_G_TRANSLATED",
{
"count_column": "between",
"chart": "g",
"run_rules": [],
"baseline": {"column": "phase", "op": "eq", "value": "reference"},
},
{
"/chart/center_line": tolerance(
42.2,
"Five constructed one-based baseline intervals; Benneyan reports zero-based 41.2.",
),
"/chart/lower_control_limit": tolerance(1, "Derived geometric lower quantile."),
"/chart/upper_control_limit": tolerance(276, "Derived geometric upper quantile."),
"/violation_keys": {
"comparison": "set",
"values": ["primary:rule_1:6"],
"rationale": "276 is on the limit; 277 exceeds it.",
},
},
benneyan,
[
"The source reports a zero-based mean, not these constructed rows "
"or quantile limits. One-based center 42.2 is a support translation."
],
)
lowry = "Lowry, Woodall, Champ and Rigdon, Technometrics 34(1), 1992, Table 4"
a, b = math.sqrt(1.5), math.sqrt(1.125)
base = [(a, a / 2), (-a, -a / 2), (0, b), (0, -b)]
monitor = [
(-1.19, 0.59),
(0.12, 0.90),
(-1.69, 0.40),
(0.30, 0.46),
(0.89, -0.75),
(0.82, 0.98),
(-0.30, 2.28),
(0.63, 1.75),
(1.56, 1.58),
(1.46, 3.05),
]
dataset(
"PUB_LOWRY_TABLE4",
[("x", "Float64"), ("y", "Float64"), ("phase", "String")],
[(x, y, "reference") for x, y in base] + [(x, y, "monitoring") for x, y in monitor],
"Four constructed reference vectors with exact covariance [[1,.5],[.5,1]]; "
"ten monitoring vectors transcribed from Lowry Table 4.",
lowry,
)
lowry_derived_z: list[list[float]] = []
lowry_derived_t2: list[float] = []
z1 = z2 = 0.0
for step, (x1, x2) in enumerate(monitor, start=1):
z1 = 0.1 * x1 + 0.9 * z1
z2 = 0.1 * x2 + 0.9 * z2
factor = 0.1 / 1.9 * (1 - 0.9 ** (2 * step))
lowry_derived_z.append([z1, z2])
lowry_derived_t2.append((z1 * z1 - z1 * z2 + z2 * z2) / (0.75 * factor))
lowry_evidence = published(
"PUB_LOWRY_MEWMA_SEQUENCE",
lowry,
{
"printed_z_vectors": LOWRY_Z,
"printed_t_squared_equation_2_5": LOWRY_T2,
"derived_z_vectors_from_printed_inputs": [
[format(value, ".17g") for value in pair] for pair in lowry_derived_z
],
"derived_t_squared_from_printed_inputs": [
format(value, ".17g") for value in lowry_derived_t2
],
"precision_note": (
"Table 4 prints Z and T-squared to two decimals. The public "
"MewmaPoint exposes T-squared but not internal Z, so canonical "
"expected compares all ten T-squared values; Z is retained "
"here as independently derived machine-readable evidence."
),
},
)
case(
9,
"Lowry Table 4 MEWMA from printed inputs",
"tabelz.analysis.spc_mewma:compute_mewma",
"mewmaMenuItem",
"PUB_LOWRY_TABLE4",
{
"variables": ["x", "y"],
"reference_column": "phase",
"reference_value": "reference",
"smoothing": 0.1,
"decision_limit": 8.79,
},
{
"/baseline/mean/0": tolerance(0, "Constructed reference mean.", "1e-12"),
"/baseline/covariance/0/1": tolerance(
0.5, "Constructed reference covariance.", "1e-12"
),
**{
f"/points/{i}/statistic": tolerance(
value,
"Independent two-variable finite-time T-squared from printed observations.",
)
for i, value in enumerate(lowry_derived_t2)
},
"/signal_count": exact(
2, "No reset after signal; monitoring steps 9 and 10 exceed h=8.79."
),
},
lowry,
[
"All ten printed Z vectors/T-squared values and independently derived "
"sequences are retained in " + lowry_evidence,
"MewmaPoint's public contract exposes T-squared, signal, and row identity, "
"but not the internal Z vector; no public Z output is claimed.",
"Published T-squared 3.17/9.65 differ from calculations using the "
"paper's rounded inputs; both are retained without widening "
"precision silently.",
],
)
mont11 = "Montgomery, Introduction to Statistical Quality Control, 6th ed., Example 11.1"
amplitude = math.sqrt(9 / 2)
pattern = [(amplitude, 0), (-amplitude, 0), (0, amplitude), (0, -amplitude)] + [(0, 0)] * 6
subrows = [
(x, y, str(g), "reference" if g < 20 else "monitoring")
for g in range(21)
for x, y in pattern
]
dataset(
"PUB_MONTGOMERY_11_1_DERIVED",
[("x", "Float64"), ("y", "Float64"), ("subgroup", "String"), ("phase", "String")],
subrows,
"Constructed 20 reference subgroups and one monitoring subgroup, "
"each n=10 with sample covariance I, for Montgomery Example 11.1 limit.",
mont11,
)
case(
10,
"Montgomery Example 11.1 subgroup Hotelling full-precision limit",
"tabelz.analysis.spc_multivariate_subgroups:compute_hotelling_subgroups",
"hotellingSubgroupsMenuItem",
"PUB_MONTGOMERY_11_1_DERIVED",
{
"variables": ["x", "y"],
"subgroup_column": "subgroup",
"subgroup_size": 10,
"reference_column": "phase",
"reference_value": "reference",
"alpha": 0.001,
},
{
"/baseline/t_squared_ucl": tolerance(
15.1650301830, "Full-precision F quantile; source prints approximate 15.16.", "1e-8"
),
"/baseline/reference_mean_generalized_variance": tolerance(
1, "Each constructed subgroup covariance determinant is one."
),
"/baseline/sigma_determinant": tolerance(9 / 8, "Mean determinant divided by b1=8/9."),
},
mont11,
[
"Montgomery's 15.16 is approximate; a full-precision evaluation "
"is 15.1650301830. Rows are constructed, not presented by Montgomery."
],
)
if __name__ == "__main__":
main()
generators/r/descriptives_association.R
source("common.R", local = TRUE)
# Independent references for Crosstab statistics (F135): the Pearson chi-square family, the
# nominal and ordinal association measures with their SPSS/SAS asymptotic standard errors,
# the linear-by-linear test, eta, and the 2 x 2 odds ratio and relative risk. Every
# measure is written out from its published definition (Goodman-Kruskal 1954; Agresti 2013;
# IBM SPSS Statistics Algorithms, CROSSTABS) and cross-checked against DescTools and base R
# where an implementation exists.
suppressPackageStartupMessages(library(DescTools))
# 33 rows. exposure x outcome is a 3 x 3 table whose row, column and cell maxima are all
# untied (lambda's ASE is then defined); treated x passed is 2 x 2 without zeros; treated x
# severe has an empty cell (B never severe).
counts <- matrix(c(7, 2, 1,
3, 6, 3,
1, 2, 8), nrow = 3, byrow = TRUE,
dimnames = list(c("Low", "Medium", "High"), c("0", "1", "2")))
exposure <- rep(rownames(counts), times = rowSums(counts))
outcome <- unlist(lapply(seq_len(nrow(counts)), function(i) rep(0:2, times = counts[i, ])))
treated <- c(rep("A", 20), rep("B", 13))
passed <- as.integer(c(rep(0, 8), rep(1, 12), rep(0, 7), rep(1, 6)))
severe <- as.integer(c(rep(0, 14), rep(1, 6), rep(0, 13)))
raw <- data.frame(exposure = exposure, outcome = as.integer(outcome), treated = treated,
passed = passed, severe = severe, stringsAsFactors = FALSE)
export_dataset(
raw, "DESC_ASSOC_01", "Constructed by descriptives_association.R",
"R stats::chisq.test; DescTools Lambda, GoodmanKruskalTau, UncertCoef, GoodmanKruskalGamma, SomersDelta, KendallTauB, StuartTauC, ConDisPairs; IBM SPSS Statistics Algorithms (CROSSTABS)",
"Constructed test data; no third-party dataset license.",
c(exposure = "String", outcome = "Int64", treated = "String", passed = "Int64", severe = "Int64")
)
d <- read_dataset("DESC_ASSOC_01")
run <- generator_run(c("stats", "DescTools", "jsonlite"))
tol <- function(value, label, rel = "1e-9") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = rel,
rationale = paste(label, "from the re-read DESC_ASSOC_01 CSV."))
}
prob <- function(value, label) {
list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = "1e-8",
rationale = paste(label, "from the re-read DESC_ASSOC_01 CSV."))
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
absent <- function(label) list(comparison = "absent", rationale = label)
manual <- "docs/manual/chapters/82-descriptive-reports-and-association.tex"
emit <- function(id, title, kind, configuration, outputs, notes) {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "descriptives",
entry_point = "tabelz.analysis.association_measures:compute_association",
menu_items = list("crosstabStatisticsMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/DESC_ASSOC_01.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"),
sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R stats::chisq.test, cor(method = kendall) and aov; DescTools association measures; SPSS CROSSTABS ASE formulas written out",
citation = "Goodman & Kruskal (1954) JASA 49:732-764; Agresti (2013) Categorical Data Analysis ch. 2-3; IBM SPSS Statistics Algorithms, CROSSTABS; DescTools 0.99 documentation",
generator = list(path = "generators/r/descriptives_association.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.")
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-CROSSTAB-ASSOCIATION-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
))
}
# --- closed forms on a table of counts ------------------------------------------------------
chi_family <- function(tab) {
n <- sum(tab)
test <- stats::chisq.test(tab, correct = FALSE)
chi2 <- unname(test$statistic)
k <- min(dim(tab))
phi <- if (all(dim(tab) == 2)) {
(tab[1, 1] * tab[2, 2] - tab[1, 2] * tab[2, 1]) /
sqrt(prod(rowSums(tab)) * prod(colSums(tab)))
} else sqrt(chi2 / n)
list(chi2 = chi2, df = unname(test$parameter), p = test$p.value, phi = phi,
v = sqrt(chi2 / (n * (k - 1))), c = sqrt(chi2 / (chi2 + n)), expected = test$expected,
stdres = test$stdres)
}
lambda_col <- function(tab) { # column predicted from the row (column dependent)
n <- sum(tab)
(sum(apply(tab, 1, max)) - max(colSums(tab))) / (n - max(colSums(tab)))
}
lambda_sym <- function(tab) {
n <- sum(tab)
(sum(apply(tab, 1, max)) + sum(apply(tab, 2, max)) - max(colSums(tab)) - max(rowSums(tab))) /
(2 * n - max(colSums(tab)) - max(rowSums(tab)))
}
gk_tau_col <- function(tab) { # column predicted from the row
n <- sum(tab)
(sum(tab^2 / rowSums(tab)) - sum(colSums(tab)^2) / n) / (n - sum(colSums(tab)^2) / n)
}
entropy <- function(p) { p <- p[p > 0]; -sum(p * log(p)) }
uncert <- function(tab, direction) {
p <- tab / sum(tab)
hr <- entropy(rowSums(p)); hc <- entropy(colSums(p)); hrc <- entropy(as.vector(p))
mutual <- hr + hc - hrc
switch(direction, column = mutual / hc, row = mutual / hr, symmetric = 2 * mutual / (hr + hc))
}
concordance <- function(tab) {
r <- nrow(tab); c <- ncol(tab)
A <- D <- matrix(0, r, c)
for (i in 1:r) for (j in 1:c) {
A[i, j] <- sum(tab[(i:r)[-1], (j:c)[-1], drop = FALSE]) + sum(tab[seq_len(i - 1), seq_len(j - 1), drop = FALSE])
D[i, j] <- sum(tab[(i:r)[-1], seq_len(j - 1), drop = FALSE]) + sum(tab[seq_len(i - 1), (j:c)[-1], drop = FALSE])
}
list(A = A, D = D)
}
# SPSS CROSSTABS ASE1 of Somers' d with the column dependent (ties on the row variable excluded).
somers_ase1 <- function(tab) {
n <- sum(tab)
cd <- concordance(tab)
P <- sum(tab * cd$A)
Q <- sum(tab * cd$D)
Dr <- n^2 - sum(rowSums(tab)^2)
2 / Dr^2 * sqrt(sum(tab * (Dr * (cd$A - cd$D) - (P - Q) * (n - rowSums(tab)))^2))
}
ordinal <- function(tab) {
n <- sum(tab)
cd <- concordance(tab)
P <- sum(tab * cd$A) # SPSS P: twice the concordant pairs
Q <- sum(tab * cd$D)
pairs <- DescTools::ConDisPairs(tab)
stopifnot(abs(P - 2 * pairs$C) < 1e-9, abs(Q - 2 * pairs$D) < 1e-9)
Dr <- n^2 - sum(rowSums(tab)^2)
Dc <- n^2 - sum(colSums(tab)^2)
m <- min(dim(tab))
S <- sum(tab * (cd$A - cd$D)^2) - (P - Q)^2 / n # the common null-variance core
gamma <- (P - Q) / (P + Q)
list(
gamma = gamma,
gamma_ase1 = 4 / (P + Q)^2 * sqrt(sum(tab * (Q * cd$A - P * cd$D)^2)),
gamma_ase0 = 2 / (P + Q) * sqrt(S),
d_col = (P - Q) / Dr, # column dependent: ties on the row (independent) excluded
d_col_ase1 = somers_ase1(tab),
d_col_ase0 = 2 / Dr * sqrt(S),
d_row = (P - Q) / Dc,
d_row_ase1 = somers_ase1(t(tab)), # the same formula on the transposed table
d_row_ase0 = 2 / Dc * sqrt(S),
d_sym = 2 * (P - Q) / (Dr + Dc),
d_sym_ase0 = 4 / (Dr + Dc) * sqrt(S),
tau_b = (P - Q) / sqrt(Dr * Dc),
tau_b_ase1 = sqrt(sum(tab * (2 * sqrt(Dr * Dc) * (cd$A - cd$D) + (P - Q) / sqrt(Dr * Dc) *
(rowSums(tab) %o% rep(1, ncol(tab)) * Dc +
rep(1, nrow(tab)) %o% colSums(tab) * Dr))^2) -
n^3 * ((P - Q) / sqrt(Dr * Dc))^2 * (Dr + Dc)^2) / (Dr * Dc),
tau_b_ase0 = 2 / sqrt(Dr * Dc) * sqrt(S),
tau_c = m * (P - Q) / (n^2 * (m - 1)),
tau_c_ase = 2 * m / ((m - 1) * n^2) * sqrt(sum(tab * (cd$A - cd$D)^2) - (P - Q)^2 / n),
P = P, Q = Q, Dr = Dr, Dc = Dc
)
}
linear_by_linear <- function(tab) {
n <- sum(tab)
r <- stats::cor(rep(seq_len(nrow(tab)), times = rowSums(tab)),
unlist(lapply(seq_len(nrow(tab)), function(i) rep(seq_len(ncol(tab)), times = tab[i, ]))))
m2 <- (n - 1) * r^2
list(m2 = m2, p = stats::pchisq(m2, 1, lower.tail = FALSE))
}
risks <- function(tab, level, haldane = FALSE) {
cells <- tab + if (haldane) 0.5 else 0
a <- cells[1, 1]; b <- cells[1, 2]; c <- cells[2, 1]; dd <- cells[2, 2]
z <- stats::qnorm(0.5 + level / 2)
odds <- a * dd / (b * c)
or_se <- sqrt(1 / a + 1 / b + 1 / c + 1 / dd)
rr <- (a / (a + b)) / (c / (c + dd))
rr_se <- sqrt(1 / a - 1 / (a + b) + 1 / c - 1 / (c + dd))
list(odds = odds, odds_lower = exp(log(odds) - z * or_se), odds_upper = exp(log(odds) + z * or_se),
rr = rr, rr_lower = exp(log(rr) - z * rr_se), rr_upper = exp(log(rr) + z * rr_se))
}
# --- 3 x 3 exposure (declared order) x outcome (numeric) --------------------------------------
tab <- table(factor(d$exposure, levels = c("Low", "Medium", "High")), d$outcome)
stopifnot(all(tab == counts))
n <- sum(tab)
cf <- chi_family(tab)
ord <- ordinal(tab)
lbl <- linear_by_linear(tab)
z95 <- stats::qnorm(0.975)
# Cross-checks against DescTools and base R (the oracle values remain the hand formulas).
stopifnot(abs(lambda_col(tab) - DescTools::Lambda(tab, direction = "row")) < 1e-12 ||
abs(lambda_col(tab) - DescTools::Lambda(tab, direction = "column")) < 1e-12)
stopifnot(abs(lambda_sym(tab) - DescTools::Lambda(tab, direction = "symmetric")) < 1e-12)
stopifnot(abs(gk_tau_col(tab) - DescTools::GoodmanKruskalTau(tab, direction = "row")) < 1e-12 ||
abs(gk_tau_col(tab) - DescTools::GoodmanKruskalTau(tab, direction = "column")) < 1e-12)
stopifnot(abs(uncert(tab, "symmetric") - DescTools::UncertCoef(tab, direction = "symmetric")) < 1e-9)
stopifnot(abs(ord$gamma - DescTools::GoodmanKruskalGamma(tab)) < 1e-12)
stopifnot(abs(ord$tau_b - DescTools::KendallTauB(tab)) < 1e-12)
stopifnot(abs(ord$tau_c - DescTools::StuartTauC(tab)) < 1e-12)
row_score <- rep(1:3, times = rowSums(tab))
col_score <- unlist(lapply(1:3, function(i) rep(1:3, times = tab[i, ])))
stopifnot(abs(ord$tau_b - stats::cor(row_score, col_score, method = "kendall")) < 1e-12)
gamma_ci <- DescTools::GoodmanKruskalGamma(tab, conf.level = 0.95)
stopifnot(abs(gamma_ci[["lwr.ci"]] - (ord$gamma - z95 * ord$gamma_ase1)) < 1e-9)
# DescTools::KendallTauB's interval is not used: its variance code weights the row and column
# margins asymmetrically and does not reproduce the published SAS/SPSS ASE1 written out above.
tau_c_ci <- DescTools::StuartTauC(tab, conf.level = 0.95)
stopifnot(abs(tau_c_ci[["lwr.ci"]] - (ord$tau_c - z95 * ord$tau_c_ase)) < 1e-9)
# DescTools direction = "column" uses the row sums (ties on the row variable), i.e. d(C|R);
# its "row" branch recycles the column sums over the matrix, so the transposed table is used.
d_col_ci <- DescTools::SomersDelta(tab, direction = "column", conf.level = 0.95)
stopifnot(abs(d_col_ci[["somers"]] - ord$d_col) < 1e-12)
stopifnot(abs(d_col_ci[["lwr.ci"]] - (ord$d_col - z95 * ord$d_col_ase1)) < 1e-9)
d_row_ci <- DescTools::SomersDelta(t(tab), direction = "column", conf.level = 0.95)
stopifnot(abs(d_row_ci[["somers"]] - ord$d_row) < 1e-12)
stopifnot(abs(d_row_ci[["lwr.ci"]] - (ord$d_row - z95 * ord$d_row_ase1)) < 1e-9)
# eta with the numeric outcome as the scale: between-group over total sum of squares.
fit <- stats::anova(stats::lm(outcome ~ exposure, data = d))
eta_outcome <- sqrt(fit[["Sum Sq"]][1] / sum(fit[["Sum Sq"]]))
measure <- function(index, name, direction, field, value, label, rel = "1e-9") {
out <- list()
out[[sprintf("/measures/%d/name", index)]] <- exact(name, "measure at this index")
out[[sprintf("/measures/%d/direction", index)]] <- exact(direction, "direction at this index")
out[[sprintf("/measures/%d/%s", index, field)]] <- tol(value, label, rel)
out
}
# Merge output lists by key, so a measure's name and direction are written once.
merge_outputs <- function(...) {
out <- list()
for (item in list(...)) for (key in names(item)) out[[key]] <- item[[key]]
out
}
m1 <- list(
"/rows_analysed" = exact(as.integer(n), "all 33 rows are complete"),
"/missing_count" = exact(0L, "no missing values"),
"/row_categories/0" = exact("Low", "declared order starts with Low"),
"/row_order_source" = exact("declared", "text order declared"),
"/column_order_source" = exact("numeric", "numeric columns take numeric order"),
"/counts/0/0" = exact(7L, "Low x 0"),
"/counts/2/2" = exact(8L, "High x 2"),
"/chi_square" = tol(cf$chi2, "Pearson chi-square (chisq.test, no correction)"),
"/chi_square_df" = exact(as.integer(cf$df), "(3-1)(3-1) df"),
"/chi_square_p" = prob(cf$p, "chi-square upper tail"),
"/cells_expected_below_5" = exact(as.integer(sum(cf$expected < 5)), "cells with expected count below 5"),
"/cells/0/expected" = tol(cf$expected[1, 1], "expected count Low x 0"),
"/cells/0/standardized_residual" = tol((tab[1, 1] - cf$expected[1, 1]) / sqrt(cf$expected[1, 1]), "standardized residual Low x 0"),
"/cells/0/adjusted_residual" = tol(cf$stdres[1, 1], "adjusted standardized residual (chisq.test stdres)"),
"/cells/8/adjusted_residual" = tol(cf$stdres[3, 3], "adjusted residual High x 2"),
"/risks/0" = absent("no 2 x 2 risks for a 3 x 3 table")
)
m1 <- merge_outputs(m1,
measure(0, "Phi", "symmetric", "estimate", cf$phi, "phi = sqrt(chi2/n)"),
measure(1, "Cramer's V", "symmetric", "estimate", cf$v, "Cramer's V"),
measure(2, "Contingency coefficient", "symmetric", "estimate", cf$c, "contingency coefficient"),
measure(3, "Lambda", "symmetric", "estimate", lambda_sym(tab), "symmetric lambda (Goodman-Kruskal 1954)"),
measure(4, "Lambda", "row dependent", "estimate", lambda_col(t(tab)), "lambda predicting the row from the column"),
measure(5, "Lambda", "column dependent", "estimate", lambda_col(tab), "lambda predicting the column from the row"),
measure(6, "Goodman and Kruskal tau", "row dependent", "estimate", gk_tau_col(t(tab)), "GK tau, row predicted"),
measure(7, "Goodman and Kruskal tau", "column dependent", "estimate", gk_tau_col(tab), "GK tau, column predicted"),
measure(8, "Uncertainty coefficient", "symmetric", "estimate", uncert(tab, "symmetric"), "symmetric U = 2I/(H_R + H_C)"),
measure(9, "Uncertainty coefficient", "row dependent", "estimate", uncert(tab, "row"), "U(R|C) = I/H_R"),
measure(10, "Uncertainty coefficient", "column dependent", "estimate", uncert(tab, "column"), "U(C|R) = I/H_C"),
measure(11, "Gamma", "symmetric", "estimate", ord$gamma, "gamma = (P-Q)/(P+Q)"),
measure(11, "Gamma", "symmetric", "ase1", ord$gamma_ase1, "gamma ASE1 (SPSS CROSSTABS; DescTools interval agrees)"),
measure(11, "Gamma", "symmetric", "ase0", ord$gamma_ase0, "gamma ASE0 = 2/(P+Q) sqrt(sum n(A-D)^2 - (P-Q)^2/n)"),
measure(11, "Gamma", "symmetric", "lower", ord$gamma - z95 * ord$gamma_ase1, "Wald lower bound estimate - z ASE1 (DescTools agrees)"),
measure(11, "Gamma", "symmetric", "upper", ord$gamma + z95 * ord$gamma_ase1, "Wald upper bound estimate + z ASE1, not truncated at 1 (DescTools clips it to 1)"),
measure(12, "Somers' d", "column dependent", "estimate", ord$d_col, "Somers' d(C|R) = (P-Q)/(n^2 - sum n_i+^2)"),
measure(12, "Somers' d", "column dependent", "ase1", ord$d_col_ase1, "Somers' d ASE1 (SPSS CROSSTABS; DescTools interval agrees)"),
measure(12, "Somers' d", "column dependent", "ase0", ord$d_col_ase0, "Somers' d ASE0"),
measure(13, "Somers' d", "row dependent", "estimate", ord$d_row, "Somers' d(R|C)"),
measure(13, "Somers' d", "row dependent", "ase1", ord$d_row_ase1, "Somers' d(R|C) ASE1 (SPSS formula on the transposed table; DescTools agrees)"),
measure(13, "Somers' d", "row dependent", "ase0", ord$d_row_ase0, "Somers' d(R|C) ASE0"),
measure(14, "Somers' d", "symmetric", "estimate", ord$d_sym, "symmetric Somers' d = 2(P-Q)/(D_r + D_c)"),
measure(14, "Somers' d", "symmetric", "ase0", ord$d_sym_ase0, "symmetric Somers' d ASE0"),
measure(15, "Kendall's tau-b", "symmetric", "estimate", ord$tau_b, "tau-b (equals cor(method = kendall) on integer scores)"),
measure(15, "Kendall's tau-b", "symmetric", "ase0", ord$tau_b_ase0, "tau-b ASE0 = 2 sqrt(S)/sqrt(D_r D_c)"),
measure(15, "Kendall's tau-b", "symmetric", "ase1", ord$tau_b_ase1, "tau-b ASE1 by the SAS/SPSS formula (1/w^2) sqrt(sum n (2 w d + tau v)^2 - n^3 tau^2 (w_r + w_c)^2)"),
measure(15, "Kendall's tau-b", "symmetric", "lower", ord$tau_b - z95 * ord$tau_b_ase1, "Wald lower bound with the SAS/SPSS ASE1"),
measure(15, "Kendall's tau-b", "symmetric", "upper", ord$tau_b + z95 * ord$tau_b_ase1, "Wald upper bound with the SAS/SPSS ASE1"),
measure(16, "Kendall's tau-c", "symmetric", "estimate", ord$tau_c, "tau-c = m(P-Q)/(n^2 (m-1))"),
measure(16, "Kendall's tau-c", "symmetric", "ase1", ord$tau_c_ase, "tau-c ASE (SAS; DescTools interval agrees)"),
measure(16, "Kendall's tau-c", "symmetric", "ase0", ord$tau_c_ase, "tau-c ASE0 equals its ASE1"),
measure(17, "Linear-by-linear association", "symmetric", "estimate", lbl$m2, "(n-1) r^2 with integer scores in declared order"),
measure(18, "Eta", "outcome dependent", "estimate", eta_outcome, "eta with the numeric outcome as the scale (aov between/total SS)")
)
m1[["/measures/17/p_value"]] <- prob(lbl$p, "linear-by-linear chi-square tail, 1 df")
m1[["/measures/11/p_value"]] <- prob(2 * stats::pnorm(-abs(ord$gamma / ord$gamma_ase0)), "gamma T = estimate/ASE0, normal two-sided")
m1[["/measures/15/p_value"]] <- prob(2 * stats::pnorm(-abs(ord$tau_b / ord$tau_b_ase0)), "tau-b T = estimate/ASE0, normal two-sided")
m1[["/measures/19"]] <- absent("the row variable is text, so no second eta")
emit("STAT-DESC-ASSOCIATION-001", "3 x 3 crosstab: chi-square family, nominal, ordinal, linear-by-linear and eta",
"nominal",
list(row = "exposure", column = "outcome", confidence = 0.95, haldane_correction = FALSE,
row_order = list("Low", "Medium", "High")),
m1,
c("R: chisq.test(correct = FALSE) for chi-square, expected counts and adjusted residuals; lambda, GK tau and U from the Goodman-Kruskal/Theil definitions; gamma, Somers' d, tau-b, tau-c with the SAS/SPSS CROSSTABS ASE1/ASE0 formulas written out (DescTools reproduces the gamma, Somers' d and tau-c intervals; its tau-b variance code differs from the published formula and is not used); tau-b equals cor(method = 'kendall') on integer scores.",
"Intervals are estimate +/- z ASE1 without truncation to [-1, 1]; the gamma upper bound exceeds 1 on this table, where DescTools clips to 1.",
"Measure indexes follow the window's list: chi-square family, lambda (symmetric, row, column), GK tau (row, column), U (symmetric, row, column), gamma, Somers' d (column, row, symmetric), tau-b, tau-c, linear-by-linear, eta.",
paste0(manual, ":65-82: declared text order for ordinal measures, integer scores, ASE1 for intervals and separate null ASE for tests, eta in the numeric direction.")))
# --- 2 x 2 treated (text, alphabetical A then B) x passed (0 then 1) at 90% ------------------
tab2 <- table(d$treated, d$passed)
stopifnot(all(tab2 == matrix(c(8, 12, 7, 6), 2, byrow = TRUE)))
cf2 <- chi_family(tab2)
rk <- risks(tab2, 0.90)
stopifnot(abs(rk$odds - DescTools::OddsRatio(tab2)) < 1e-12)
stopifnot(abs(rk$rr - DescTools::RelRisk(tab2)) < 1e-12)
or_dt <- DescTools::OddsRatio(tab2, conf.level = 0.90, method = "wald")
stopifnot(abs(or_dt[["lwr.ci"]] - rk$odds_lower) < 1e-9)
ord2 <- ordinal(tab2)
m2 <- list(
"/rows_analysed" = exact(as.integer(sum(tab2)), "33 rows"),
"/row_categories/0" = exact("A", "text rows in alphabetical display order"),
"/column_categories/0" = exact("0", "numeric columns in numeric order"),
"/row_order_source" = exact("undeclared", "no declared row order"),
"/counts/0/0" = exact(8L, "A x 0 is cell a"),
"/chi_square" = tol(cf2$chi2, "Pearson chi-square without continuity correction"),
"/chi_square_df" = exact(1L, "1 df"),
"/chi_square_p" = prob(cf2$p, "chi-square tail"),
"/measures/0/estimate" = tol(cf2$phi, "signed phi (ad-bc)/sqrt(margins)"),
"/measures/1/estimate" = tol(cf2$v, "Cramer's V equals |phi| for 2 x 2"),
"/risks/0/name" = exact("Odds ratio", "first risk"),
"/risks/0/estimate" = tol(rk$odds, "odds ratio ad/(bc) with a = A x 0"),
"/risks/0/lower" = tol(rk$odds_lower, "Woolf log-scale 90% lower (DescTools wald agrees)"),
"/risks/0/upper" = tol(rk$odds_upper, "Woolf log-scale 90% upper"),
"/risks/1/name" = exact("Relative risk (row 1)", "second risk"),
"/risks/1/estimate" = tol(rk$rr, "risk of column 0 in row A over row B"),
"/risks/1/lower" = tol(rk$rr_lower, "Katz log-scale 90% lower"),
"/risks/1/upper" = tol(rk$rr_upper, "Katz log-scale 90% upper"),
"/measures/11/estimate" = absent("gamma is unavailable without a declared order for the text rows"),
"/measures/17/estimate" = absent("linear-by-linear is unavailable without a declared order"),
"/measures/18/direction" = exact("passed dependent", "eta in the numeric direction only")
)
m2[["/measures/18/estimate"]] <- tol(sqrt(stats::anova(stats::lm(passed ~ treated, data = d))[["Sum Sq"]][1] /
sum(stats::anova(stats::lm(passed ~ treated, data = d))[["Sum Sq"]])),
"eta with passed as the scale")
emit("STAT-DESC-ASSOCIATION-002", "2 x 2 crosstab at 90%: signed phi, odds ratio and relative risk intervals",
"axis_coverage",
list(row = "treated", column = "passed", confidence = 0.90, haldane_correction = FALSE),
m2,
c("R: table rows A, B (alphabetical) and columns 0, 1; a = A x 0 = 8, b = 12, c = 7, d = 6; odds ratio ad/(bc) with the Woolf interval exp(log OR +/- z sqrt(1/a+1/b+1/c+1/d)); relative risk of column 0 for row A over row B with the Katz interval; z at 90%.",
"Without a declared order for the text rows the ordinal measures and the linear-by-linear test are unavailable; the manual says alphabetical order is a display rule.",
paste0(manual, ":65-69 and 85-96.")))
# --- 2 x 2 with an empty cell: Haldane-Anscombe 0.5 correction -------------------------------
tab3 <- table(d$treated, d$severe)
stopifnot(all(tab3 == matrix(c(14, 6, 13, 0), 2, byrow = TRUE)))
rk3 <- risks(tab3, 0.95, haldane = TRUE)
emit("STAT-DESC-ASSOCIATION-003", "2 x 2 crosstab with an empty cell and the 0.5 correction",
"axis_coverage",
list(row = "treated", column = "severe", confidence = 0.95, haldane_correction = TRUE),
list(
"/counts/1/1" = exact(0L, "B x 1 is empty"),
"/risks/0/estimate" = tol(rk3$odds, "odds ratio after adding 0.5 to all four cells"),
"/risks/0/lower" = tol(rk3$odds_lower, "Woolf interval on the corrected cells"),
"/risks/0/upper" = tol(rk3$odds_upper, "Woolf interval on the corrected cells"),
"/risks/0/infinite" = exact(FALSE, "finite after correction"),
"/risks/1/estimate" = tol(rk3$rr, "relative risk on the corrected cells"),
"/risks/1/lower" = tol(rk3$rr_lower, "Katz interval on the corrected cells"),
"/risks/1/upper" = tol(rk3$rr_upper, "Katz interval on the corrected cells"),
"/chi_square" = tol(chi_family(tab3)$chi2, "chi-square is on the uncorrected counts")
),
c("R: cells 14.5, 6.5, 13.5, 0.5; odds ratio ad/(bc) and relative risk (a/(a+b))/(c/(c+d)) with Woolf and Katz log-scale intervals at 95%; the chi-square ignores the correction.",
paste0(manual, ":96-98: the 0.5 correction adds 0.5 to all cells only when explicitly selected.")))
emit("STAT-DESC-ASSOCIATION-004", "2 x 2 crosstab with an empty cell and no correction",
"boundary",
list(row = "treated", column = "severe", confidence = 0.95, haldane_correction = FALSE),
list(
"/risks/0/estimate" = tol(0, "odds ratio ad/(bc) = 0 when d = 0"),
"/risks/0/lower" = absent("log-scale interval unavailable with a zero cell"),
"/risks/1/estimate" = tol((14 / 20) / (13 / 13), "relative risk of column 0: 0.7"),
"/risks/1/lower" = tol(exp(log(0.7) - stats::qnorm(0.975) * sqrt(1 / 14 - 1 / 20 + 1 / 13 - 1 / 13)), "Katz lower bound, defined because both row risks are positive"),
"/measures/0/estimate" = tol(chi_family(tab3)$phi, "signed phi with a zero cell")
),
c("R: a = 14, b = 6, c = 13, d = 0: odds ratio 0 with no log-scale interval; relative risk 0.7 with the Katz interval.",
paste0(manual, ":96-98: zero cells may make log-scale intervals unavailable.")))
cat("association references generated\n")
generators/r/descriptives_custom_tables.R
source("common.R", local = TRUE)
# Independent references for Custom tables (F150, ruling #52): nested counts and percents with
# explicit denominators, the per-row-variable chi-square test, pooled two-proportion z column
# comparisons with Holm adjustment, Welch t column comparisons with Bonferroni adjustment, and
# scale summaries. Everything is base R: table(), chisq.test(), prop.test(correct = FALSE),
# t.test(var.equal = FALSE) and p.adjust().
set.seed(2026)
dept_counts <- list(Ops = c(East = 10, North = 1, West = 3),
Sales = c(East = 1, North = 9, West = 3),
Tech = c(East = 2, North = 2, West = 9))
dept <- site <- character()
for (name in names(dept_counts)) {
for (s in names(dept_counts[[name]])) {
k <- dept_counts[[name]][[s]]
dept <- c(dept, rep(name, k))
site <- c(site, rep(s, k))
}
}
level <- character(length(dept))
for (name in names(dept_counts)) {
idx <- which(dept == name)
level[idx] <- rep(c("Junior", "Senior"), length.out = length(idx))
}
site_mean <- c(East = 30, North = 50, West = 70)
sales <- round(site_mean[site] + stats::rnorm(length(site), 0, 3), 1)
sales[which(dept == "Tech" & site == "West")[1:2]] <- NA
raw <- data.frame(dept = dept, level = level, site = site, sales = as.double(sales),
stringsAsFactors = FALSE)
raw <- rbind(raw,
data.frame(dept = NA_character_, level = "Junior", site = "East", sales = 41.0),
data.frame(dept = "Ops", level = "Senior", site = NA_character_, sales = 39.5))
export_dataset(
raw, "DESC_CUSTOM_01", "Constructed by descriptives_custom_tables.R (set.seed(2026) normal noise)",
"R stats::chisq.test, prop.test, t.test (Welch), p.adjust (holm, bonferroni)",
"Constructed test data; no third-party dataset license.",
c(dept = "String", level = "String", site = "String", sales = "Float64")
)
d <- read_dataset("DESC_CUSTOM_01")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(value, label, rel = "1e-9") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = rel,
rationale = paste(label, "from the re-read DESC_CUSTOM_01 CSV."))
}
prob <- function(value, label) {
list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = "1e-8",
rationale = paste(label, "from the re-read DESC_CUSTOM_01 CSV."))
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
absent <- function(label) list(comparison = "absent", rationale = label)
manual <- "docs/manual/chapters/82-descriptive-reports-and-association.tex"
emit <- function(id, title, kind, configuration, outputs, notes) {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "descriptives",
entry_point = "tabelz.analysis.report_custom_tables:compute_custom_table",
menu_items = list("customTablesMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/DESC_CUSTOM_01.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"),
sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R base table, stats::chisq.test, prop.test, t.test and p.adjust",
citation = "R stats documentation: chisq.test, prop.test (correct = FALSE), t.test (var.equal = FALSE), p.adjust (holm, bonferroni)",
generator = list(path = "generators/r/descriptives_custom_tables.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.")
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-CUSTOM-TABLES-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
))
}
complete <- !is.na(d$dept) & !is.na(d$site)
dd <- d[complete, ]
depts <- sort(unique(dd$dept))
sites <- sort(unique(dd$site))
tab <- table(factor(dd$dept, levels = depts), factor(dd$site, levels = sites))
N <- sum(tab)
stopifnot(N == 40, sum(!complete) == 2)
# Cell index in the flat cell list: block (table row in emission order), column (leaf columns
# then Total), statistic; each block holds (columns + 1) x statistics cells.
cell <- function(block, column, statistic, n_columns, n_statistics) {
sprintf("/cells/%d/value", (block * (n_columns + 1) + column) * n_statistics + statistic)
}
# --- one row variable: counts, percents with explicit denominators, chi-square --------------
stats1 <- c("count", "column_percent", "row_percent", "table_percent")
chi <- stats::chisq.test(tab, correct = FALSE)
o1 <- list(
"/used_rows" = exact(as.integer(N), "rows with a dept and a site"),
"/missing_rows" = exact(2L, "one row without dept, one without site"),
"/row_keys/0/0" = exact("Ops", "rows in alphabetical order"),
"/column_keys/2/0" = exact("West", "columns in alphabetical order"),
"/column_letters/2" = exact("C", "third column letter"),
"/independence/0/variable" = exact("dept", "test of the row variable"),
"/independence/0/chi_square" = tol(chi$statistic, "Pearson chi-square of dept x site"),
"/independence/0/df" = exact(as.integer(chi$parameter), "(3-1)(3-1) df"),
"/independence/0/p_value" = prob(chi$p.value, "chi-square tail"),
"/independence/0/min_expected" = tol(min(chi$expected), "smallest expected count"),
"/independence/0/small_expected" = exact(as.integer(sum(chi$expected < 5)), "cells expected below 5"),
"/independence/0/cells" = exact(9L, "nine cells")
)
for (r in seq_along(depts)) for (c in seq_along(sites)) {
count <- tab[r, c]
label <- sprintf("%s x %s", depts[r], sites[c])
o1[[cell(r - 1, c - 1, 0, 3, 4)]] <- tol(count, paste("count", label))
o1[[cell(r - 1, c - 1, 1, 3, 4)]] <- tol(100 * count / sum(tab[, c]), paste("column percent (of the column total)", label))
o1[[cell(r - 1, c - 1, 2, 3, 4)]] <- tol(100 * count / sum(tab[r, ]), paste("row percent (of the row total)", label))
o1[[cell(r - 1, c - 1, 3, 3, 4)]] <- tol(100 * count / N, paste("table percent", label))
}
o1[[cell(0, 3, 0, 3, 4)]] <- tol(sum(tab[1, ]), "Ops total column count")
o1[[cell(0, 3, 1, 3, 4)]] <- tol(100 * sum(tab[1, ]) / N, "Ops total column percent is its share of all rows")
o1[[cell(3, 1, 0, 3, 4)]] <- tol(sum(tab[, 2]), "Total row, North count")
o1[[cell(3, 1, 1, 3, 4)]] <- tol(100, "Total row column percent is 100")
o1[[cell(3, 3, 0, 3, 4)]] <- tol(N, "grand total")
o1[["/cells/64"]] <- absent("4 blocks x 4 columns x 4 statistics = 64 cells")
emit("STAT-DESC-CUSTOMTABLES-001", "Custom table: counts, column, row and table percents, chi-square",
"nominal",
list(rows = list("dept"), columns = list("site"), statistics = as.list(stats1),
totals = TRUE, independence = TRUE, compare = "none", adjustment = "holm",
alpha = 0.05),
o1,
c("R: table(dept, site) over the 40 complete rows; column percent divides by the column total (the outer row group is the whole table with one row variable), row percent by the row total, table percent by 40; chisq.test(correct = FALSE).",
"Cell order: for each row (Ops, Sales, Tech, Total) the columns East, North, West, Total, each with the four statistics in the requested order.",
paste0(manual, ":50-55: column percentages are taken within the outer row group; chi-square with expected-count diagnostics.")))
# --- proportions compared by column with Holm --------------------------------------------------
pairs <- list(c(1, 2), c(1, 3), c(2, 3))
family <- list()
for (r in seq_along(depts)) for (k in seq_along(pairs)) {
a <- pairs[[k]][1]; b <- pairs[[k]][2]
na <- sum(tab[, a]); nb <- sum(tab[, b])
xa <- tab[r, a]; xb <- tab[r, b]
pooled <- (xa + xb) / (na + nb)
z <- (xa / na - xb / nb) / sqrt(pooled * (1 - pooled) * (1 / na + 1 / nb))
p <- 2 * stats::pnorm(-abs(z))
pt <- stats::prop.test(c(xa, xb), c(na, nb), correct = FALSE)
stopifnot(abs(z^2 - pt$statistic) < 1e-9, abs(p - pt$p.value) < 1e-12)
family[[length(family) + 1]] <- list(row = r, a = a, b = b, z = z, p = p)
}
p_raw <- vapply(family, function(f) f$p, 0)
holm <- stats::p.adjust(p_raw, method = "holm")
o2 <- list("/comparisons/9" = absent("3 rows x 3 column pairs = 9 comparisons"))
flags <- list()
for (i in seq_along(family)) {
f <- family[[i]]
o2[[sprintf("/comparisons/%d/subject", i - 1)]] <- exact(paste0("dept: ", depts[f$row]), "row category compared")
o2[[sprintf("/comparisons/%d/first", i - 1)]] <- exact(sites[f$a], "first column")
o2[[sprintf("/comparisons/%d/second", i - 1)]] <- exact(sites[f$b], "second column")
o2[[sprintf("/comparisons/%d/statistic", i - 1)]] <- tol(f$z, "pooled two-proportion z (prop.test without correction is z^2)")
o2[[sprintf("/comparisons/%d/p_value", i - 1)]] <- prob(f$p, "two-sided normal p")
o2[[sprintf("/comparisons/%d/adjusted_p", i - 1)]] <- prob(holm[i], "Holm-adjusted p over the family of 9")
o2[[sprintf("/comparisons/%d/family_size", i - 1)]] <- exact(9L, "family size")
if (holm[i] < 0.05) {
larger <- if (f$z > 0) f$a else f$b
smaller <- if (f$z > 0) f$b else f$a
key <- paste(f$row, larger)
flags[[key]] <- sort(c(flags[[key]], LETTERS[smaller]))
}
}
stopifnot(length(flags) > 0)
for (r in seq_along(depts)) for (c in seq_along(sites)) {
key <- paste(r, c)
path <- sub("/value$", "/flags", cell(r - 1, c - 1, 0, 3, 2))
o2[[path]] <- exact(if (is.null(flags[[key]])) "" else paste(flags[[key]], collapse = ""),
sprintf("letters of the significantly smaller columns for %s x %s", depts[r], sites[c]))
}
emit("STAT-DESC-CUSTOMTABLES-002", "Custom table: column proportion comparisons with Holm letters",
"axis_coverage",
list(rows = list("dept"), columns = list("site"), statistics = list("count", "column_percent"),
totals = TRUE, independence = FALSE, compare = "proportions", adjustment = "holm",
alpha = 0.05),
o2,
c("R: for each dept and each pair of sites, the pooled-null two-proportion z of the dept's share of the two site columns (prop.test(correct = FALSE) gives z^2 and the same p); p.adjust(holm) over the 9 comparisons; a count cell carries the letters of the columns that are significantly smaller.",
paste0(manual, ":57-61: two-proportion tests, Holm by default over all comparisons in the layer, a letter names a column with a significantly smaller value.")))
# --- means compared by column with Bonferroni, scale summaries --------------------------------
stats3 <- c("mean", "sd", "median", "valid_n", "count")
family3 <- list()
for (r in seq_along(depts)) for (k in seq_along(pairs)) {
a <- pairs[[k]][1]; b <- pairs[[k]][2]
va <- dd$sales[dd$dept == depts[r] & dd$site == sites[a] & !is.na(dd$sales)]
vb <- dd$sales[dd$dept == depts[r] & dd$site == sites[b] & !is.na(dd$sales)]
if (length(va) < 2 || length(vb) < 2) {
family3[[length(family3) + 1]] <- list(row = r, a = a, b = b, t = NA, p = NA)
} else {
tt <- stats::t.test(va, vb, var.equal = FALSE)
family3[[length(family3) + 1]] <- list(row = r, a = a, b = b, t = unname(tt$statistic), p = tt$p.value)
}
}
p3 <- vapply(family3, function(f) if (is.na(f$p)) 1 else f$p, 0)
bonf <- stats::p.adjust(p3, method = "bonferroni")
o3 <- list()
for (i in seq_along(family3)) {
f <- family3[[i]]
o3[[sprintf("/comparisons/%d/family_size", i - 1)]] <- exact(9L, "every planned comparison counts")
if (is.na(f$p)) {
o3[[sprintf("/comparisons/%d/statistic", i - 1)]] <- absent("a cell with fewer than two values keeps its slot without a statistic")
o3[[sprintf("/comparisons/%d/adjusted_p", i - 1)]] <- absent("no adjusted p for an uncomputable comparison")
} else {
o3[[sprintf("/comparisons/%d/statistic", i - 1)]] <- tol(f$t, "Welch t")
o3[[sprintf("/comparisons/%d/p_value", i - 1)]] <- prob(f$p, "Welch two-sided p")
o3[[sprintf("/comparisons/%d/adjusted_p", i - 1)]] <- prob(bonf[i], "Bonferroni over 9 (uncomputable slots enter as p = 1)")
}
}
stopifnot(sum(vapply(family3, function(f) is.na(f$p), TRUE)) > 0)
for (r in seq_along(depts)) for (c in seq_along(sites)) {
v <- dd$sales[dd$dept == depts[r] & dd$site == sites[c] & !is.na(dd$sales)]
label <- sprintf("%s x %s", depts[r], sites[c])
o3[[cell(r - 1, c - 1, 0, 3, 5)]] <- tol(mean(v), paste("mean sales", label))
if (length(v) > 1) {
o3[[cell(r - 1, c - 1, 1, 3, 5)]] <- tol(sd(v), paste("sample SD", label))
} else {
o3[[cell(r - 1, c - 1, 1, 3, 5)]] <- absent(paste("SD undefined for one value", label))
}
o3[[cell(r - 1, c - 1, 2, 3, 5)]] <- tol(stats::median(v), paste("median", label))
o3[[cell(r - 1, c - 1, 3, 3, 5)]] <- tol(length(v), paste("valid N (non-missing sales)", label))
o3[[cell(r - 1, c - 1, 4, 3, 5)]] <- tol(tab[r, c], paste("count (rows, missing sales included)", label))
}
all_sales <- dd$sales[!is.na(dd$sales)]
o3[[cell(3, 3, 0, 3, 5)]] <- tol(mean(all_sales), "grand mean of sales")
o3[[cell(3, 3, 3, 3, 5)]] <- tol(length(all_sales), "valid N over all complete rows")
emit("STAT-DESC-CUSTOMTABLES-003", "Custom table: scale summaries and Welch mean comparisons with Bonferroni",
"axis_coverage",
list(rows = list("dept"), columns = list("site"), summary = "sales",
statistics = as.list(stats3), totals = TRUE, independence = FALSE,
compare = "means", adjustment = "bonferroni", alpha = 0.05),
o3,
c("R: mean, sd, median and non-missing count of sales per dept x site cell; t.test(var.equal = FALSE) per dept and site pair; p.adjust(bonferroni) over the 9 planned comparisons with an uncomputable pair (one value) entering as p = 1.",
paste0(manual, ":57-61: Welch tests for numerical means; Bonferroni as an option.")))
# --- two nested row variables with subtotals -----------------------------------------------------
levels_ <- sort(unique(dd$level))
tab3 <- table(factor(dd$dept, levels = depts), factor(dd$level, levels = levels_),
factor(dd$site, levels = sites))
dept_site <- tab
# Emission order: Ops/Junior, Ops/Senior, Ops Subtotal, Sales/Junior, ..., Tech Subtotal, Total.
block_of <- function(r, l) (r - 1) * 3 + (l - 1)
subtotal_block <- function(r) (r - 1) * 3 + 2
o4 <- list(
"/row_keys/0/0" = exact("Ops", "outer row"),
"/row_keys/0/1" = exact("Junior", "inner row"),
"/row_keys/6" = absent("3 x 2 leaf rows"),
"/cells/120" = absent("10 blocks x 4 columns x 3 statistics = 120 cells")
)
for (r in seq_along(depts)) {
for (l in seq_along(levels_)) for (c in seq_along(sites)) {
count <- tab3[r, l, c]
label <- sprintf("%s/%s x %s", depts[r], levels_[l], sites[c])
o4[[cell(block_of(r, l), c - 1, 0, 3, 3)]] <- tol(count, paste("count", label))
o4[[cell(block_of(r, l), c - 1, 1, 3, 3)]] <- tol(100 * count / dept_site[r, c], paste("column percent within the outer dept group", label))
o4[[cell(block_of(r, l), c - 1, 2, 3, 3)]] <- tol(100 * count / N, paste("layer percent (no layer: of all rows)", label))
}
for (c in seq_along(sites)) {
o4[[cell(subtotal_block(r), c - 1, 0, 3, 3)]] <- tol(dept_site[r, c], sprintf("%s subtotal count at %s", depts[r], sites[c]))
o4[[cell(subtotal_block(r), c - 1, 1, 3, 3)]] <- tol(100 * dept_site[r, c] / sum(dept_site[, c]), sprintf("%s subtotal column percent of the column total", depts[r]))
}
o4[[cell(subtotal_block(r), 3, 0, 3, 3)]] <- tol(sum(dept_site[r, ]), sprintf("%s subtotal total", depts[r]))
}
o4[[cell(9, 3, 0, 3, 3)]] <- tol(N, "grand total")
o4[[cell(9, 0, 1, 3, 3)]] <- tol(100, "Total row column percent is 100")
emit("STAT-DESC-CUSTOMTABLES-004", "Custom table: nested rows with subtotals and outer-group column percents",
"axis_coverage",
list(rows = list("dept", "level"), columns = list("site"),
statistics = list("count", "column_percent", "layer_percent"), totals = TRUE,
independence = FALSE, compare = "none", adjustment = "holm", alpha = 0.05),
o4,
c("R: table(dept, level, site); a leaf row's column percent divides by the dept group's count in that site column; a subtotal's column percent divides by the column total; with no layer the layer percent divides by all 40 rows.",
"Cell order: Ops/Junior, Ops/Senior, Ops subtotal, Sales/Junior, Sales/Senior, Sales subtotal, Tech/Junior, Tech/Senior, Tech subtotal, Total; columns East, North, West, Total; statistics count, column %, layer %.",
paste0(manual, ":50-55: up to three nested variables, subtotals, column percentages within the outer row group.")))
cat("custom table references generated\n")
generators/r/descriptives_distribution_fit.R
source("common.R", local = TRUE)
# References for tabelz.analysis.distribution_fit:fit_distributions.
# Manual chapter 07, section "Distribution fit...", and the statistician's
# ruling on GitHub issue #3: maximum likelihood per candidate; lognormal, gamma,
# Weibull and exponential describe x = origin + y with the origin fixed at the
# declared value (zero unless changed) and not counted in k. Lognormal, gamma
# and Weibull are skipped when any value is at or below the origin; the
# exponential's support is x >= origin, so it is skipped only for a value below
# it. Ranking is by BIC, then AIC. AIC = 2k - 2 log L and BIC = k log N - 2 log L.
# Result labels name the exact origin, as in "Weibull (origin fixed at 5)".
# Parameters follow the SciPy parameterization the manual names: normal,
# laplace and logistic (loc, scale); Student t (df, loc, scale); lognormal
# (s, loc, scale) with scale = exp(mu); gamma (a, loc, scale); Weibull
# (c, loc, scale); exponential (loc, scale).
run <- generator_run(c("stats", "jsonlite"))
family <- "descriptives"
entry <- "tabelz.analysis.distribution_fit:fit_distributions"
generator <- "generators/r/descriptives_distribution_fit.R"
closed <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Closed-form maximum likelihood; owner 1e-9 tier.", why)
)
iterative <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-6",
rationale = paste("Iterative maximum likelihood; owner 1e-6 tier.", why)
)
exact <- function(value, why) {
if (is.numeric(value)) value <- as.integer(value)
list(comparison = "exact", value = value, rationale = why)
}
keys_in_order <- function(keys, why) list(comparison = "sequence", values = as.list(keys), rationale = why)
emit <- function(id, title, dataset, config, outputs, notes, kind = "nominal",
excluded = list(), error = NULL, deferred = list(), version = "1.0.0") {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = deferred, generator_run = run
))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = version,
family = family, entry_point = entry,
menu_items = list("distributionFitMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats (optimize, uniroot, nlminb)",
citation = "R stats::nlminb, stats::uniroot and stats::dt/dlogis/dgamma/dweibull documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = c(notes,
"Manual chapter 07, Distribution fit, and GitHub issue #3: origin fixed for positive families and not counted in k; BIC then AIC ranking.",
"All reference calculations re-read the exported CSV.")
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL
))
}
# ---- maximum-likelihood fits ----------------------------------------------
fit_normal <- function(x) {
m <- mean(x); s <- sqrt(mean((x - m)^2))
list(key = "normal", k = 2L, par = c(loc = m, scale = s),
ll = sum(dnorm(x, m, s, log = TRUE)), closed = TRUE)
}
fit_laplace <- function(x) {
if (length(x) %% 2 == 0) stop("Laplace location is unique only for odd n")
m <- median(x); b <- mean(abs(x - m))
list(key = "laplace", k = 2L, par = c(loc = m, scale = b),
ll = sum(-log(2 * b) - abs(x - m) / b), closed = TRUE)
}
fit_exponential <- function(x, origin) {
y <- x - origin; b <- mean(y)
list(key = "exponential", k = 1L, par = c(loc = origin, scale = b),
ll = sum(dexp(y, 1 / b, log = TRUE)), closed = TRUE)
}
fit_lognormal <- function(x, origin) {
z <- log(x - origin); mu <- mean(z); s <- sqrt(mean((z - mu)^2))
list(key = "lognormal", k = 2L, par = c(s = s, loc = origin, scale = exp(mu)),
ll = sum(dlnorm(x - origin, mu, s, log = TRUE)), closed = TRUE)
}
fit_gamma <- function(x, origin) {
y <- x - origin
rhs <- log(mean(y)) - mean(log(y))
a <- uniroot(function(a) log(a) - digamma(a) - rhs, c(1e-8, 1e8), tol = 1e-15)$root
b <- mean(y) / a
list(key = "gamma", k = 2L, par = c(a = a, loc = origin, scale = b),
ll = sum(dgamma(y, shape = a, scale = b, log = TRUE)), closed = FALSE)
}
fit_weibull <- function(x, origin) {
# The shape score is unchanged by rescaling y, so it is solved on y / max(y)
# (all powers at most 1) and the scale is restored afterwards.
y <- x - origin; top <- max(y); u <- y / top; lu <- log(u)
score <- function(c) sum(u^c * lu) / sum(u^c) - 1 / c - mean(lu)
c <- uniroot(score, c(1e-2, 200), tol = 1e-15)$root
b <- top * mean(u^c)^(1 / c)
list(key = "weibull", k = 2L, par = c(c = c, loc = origin, scale = b),
ll = sum(dweibull(y, shape = c, scale = b, log = TRUE)), closed = FALSE)
}
fit_logistic <- function(x) {
# Score equations, with z = (x - mu) / s: sum(tanh(z / 2)) = 0 fixes mu for
# a given s (monotone in mu), and sum(z * tanh(z / 2)) = n then fixes s.
mu_for <- function(s) uniroot(function(m) sum(tanh((x - m) / (2 * s))),
range(x), tol = 1e-15)$root
s_score <- function(s) { z <- (x - mu_for(s)) / s; sum(z * tanh(z / 2)) - length(x) }
s <- uniroot(s_score, c(sd(x) * 1e-3, sd(x) * 10), tol = 1e-15)$root
m <- mu_for(s)
list(key = "logistic", k = 2L, par = c(loc = m, scale = s),
ll = sum(dlogis(x, m, s, log = TRUE)), closed = FALSE)
}
t_profile <- function(x, nu) {
# For fixed df the location and scale maximum is the fixed point of the EM
# weights w = (nu + 1) / (nu + z^2); iterated until nothing changes.
m <- median(x); s2 <- var(x)
for (step in 1:100000) {
w <- (nu + 1) / (nu + (x - m)^2 / s2)
m_new <- sum(w * x) / sum(w)
s2_new <- sum(w * (x - m_new)^2) / length(x)
if (abs(m_new - m) <= 1e-15 * max(1, abs(m)) && abs(s2_new - s2) <= 1e-15 * s2) break
m <- m_new; s2 <- s2_new
}
c(loc = m_new, scale = sqrt(s2_new))
}
t_df_score <- function(x, nu) {
# d log L / d nu at the profiled location and scale (envelope theorem).
p <- t_profile(x, nu); z2 <- ((x - p[["loc"]]) / p[["scale"]])^2
0.5 * sum(digamma((nu + 1) / 2) - digamma(nu / 2) - 1 / nu - log1p(z2 / nu) +
(nu + 1) * z2 / (nu * (nu + z2)))
}
fit_t <- function(x) {
# Returns NULL when the df score stays positive up to 1e4: the likelihood
# keeps rising toward the normal and no finite Student t maximum exists.
if (t_df_score(x, 1e4) > 0) return(NULL)
nu <- uniroot(function(v) t_df_score(x, v), c(0.05, 1e4), tol = 1e-15)$root
p <- t_profile(x, nu)
list(key = "student_t", k = 3L, par = c(df = nu, loc = p[["loc"]], scale = p[["scale"]]),
ll = sum(dt((x - p[["loc"]]) / p[["scale"]], nu, log = TRUE) - log(p[["scale"]])),
closed = FALSE)
}
candidate_outputs <- function(fit, n, prefix = "/columns/0/candidates") {
node <- paste0(prefix, "/[key=", fit$key, "]")
tier <- if (fit$closed) closed else iterative
out <- list()
for (index in seq_along(fit$par)) {
name <- names(fit$par)[index]
if (name == "loc" && fit$key %in% c("lognormal", "gamma", "weibull", "exponential")) {
out[[paste0(node, "/parameters/", index - 1L, "/1")]] <- closed(
fit$par[[index]], "The declared origin is returned as the fixed location.")
} else {
out[[paste0(node, "/parameters/", index - 1L, "/1")]] <- tier(
fit$par[[index]], paste0("Parameter ", name, "."))
}
out[[paste0(node, "/parameters/", index - 1L, "/0")]] <- exact(
name, "SciPy parameter name in SciPy order.")
}
aic <- 2 * fit$k - 2 * fit$ll
bic <- log(n) * fit$k - 2 * fit$ll
out[[paste0(node, "/log_likelihood")]] <- tier(fit$ll, "Maximized log likelihood.")
out[[paste0(node, "/aic")]] <- tier(aic, "AIC = 2k - 2 log L, origin not counted.")
out[[paste0(node, "/bic")]] <- tier(bic, "BIC = k log N - 2 log L, origin not counted.")
out
}
ranked <- function(fits, n) {
aic <- vapply(fits, function(f) 2 * f$k - 2 * f$ll, 0)
bic <- vapply(fits, function(f) log(n) * f$k - 2 * f$ll, 0)
order_keys <- c("normal", "student_t", "laplace", "logistic", "lognormal", "gamma",
"weibull", "exponential")
pos <- match(vapply(fits, `[[`, "", "key"), order_keys)
vapply(fits, `[[`, "", "key")[order(bic, aic, pos)]
}
min_gap <- function(fits, n) {
bic <- sort(vapply(fits, function(f) log(n) * f$k - 2 * f$ll, 0))
min(diff(bic))
}
anderson_darling <- function(x, cdf) {
u <- sort(cdf(x)); n <- length(u); i <- seq_len(n)
-n - mean((2 * i - 1) * (log(u) + log1p(-rev(u))))
}
all8 <- list("normal", "student_t", "laplace", "logistic", "lognormal", "gamma",
"weibull", "exponential")
# ---- dataset 1: 25 positive, unimodal values -------------------------------
set.seed(20260924)
export_dataset(data.frame(y = round(rgamma(25, shape = 6, rate = 1.5) + 1, 6)),
"DESC_DISTFIT_01", "Constructed: rgamma(25, shape 6, rate 1.5) + 1, seed 20260924, rounded to 6 decimals",
"R stats::rgamma documentation", "Constructed validation data; no third-party license.",
c(y = "Float64"))
x <- read_dataset("DESC_DISTFIT_01")$y
n <- length(x)
if (!is.null(fit_t(x))) stop("expected no finite Student t maximum on DESC_DISTFIT_01")
fits <- list(fit_normal(x), fit_laplace(x), fit_logistic(x), fit_lognormal(x, 0),
fit_gamma(x, 0), fit_weibull(x, 0), fit_exponential(x, 0))
out <- list(); for (f in fits) out <- c(out, candidate_outputs(f, n))
gap <- min_gap(fits, n)
out[["/candidate_keys"]] <- keys_in_order(unlist(all8), "Selected candidates in the stable user-facing order.")
out[["/columns/0/count"]] <- exact(n, "All 25 values are finite.")
emit("STAT-DESC-DISTFIT-001", "All eight candidates on positive unimodal data", "DESC_DISTFIT_01",
list(columns = list("y"), candidates = all8, origin = 0), out,
c(sprintf("Smallest BIC gap between the seven finite candidates: %.6g.", gap),
"Gamma and Weibull solve their one-dimensional profile score with uniroot(tol = 1e-15); logistic solves its two score equations with nested uniroot(tol = 1e-15).",
"Student t: the df score of the profile likelihood is still positive at df = 1e4, so the likelihood rises toward the normal and no finite maximum exists; its outputs are deferred."),
deferred = list(list(path = "/columns/0/candidates/[key=student_t]",
reason = "No finite Student t maximum-likelihood estimate on these data (df diverges); no reference value exists.")))
ranking <- ranked(fits, n)
if (gap < 1e-3) stop("BIC gap too small for an unambiguous ranking")
rank_out <- list()
for (index in seq_along(ranking)) {
rank_out[[paste0("/columns/0/candidates/", index - 1L, "/key")]] <- exact(
ranking[[index]], "Candidate order by BIC then AIC is a decision; exact.")
}
emit("STAT-DESC-DISTFIT-002", "Candidate ranking by BIC then AIC", "DESC_DISTFIT_01",
list(columns = list("y"), candidates = list("normal", "laplace", "logistic", "lognormal",
"gamma", "weibull", "exponential"), origin = 0),
c(rank_out, list(
"/columns/0/best_label" = exact(
c(normal = "Normal", student_t = "Student t", laplace = "Laplace", logistic = "Logistic",
lognormal = "Lognormal (origin fixed at 0)", gamma = "Gamma (origin fixed at 0)",
weibull = "Weibull (origin fixed at 0)",
exponential = "Exponential (origin fixed at 0)")[[ranking[1]]],
"The leading label names the exact fixed origin of a positive family (issue #3)."),
"/columns/0/bic" = (if (ranking[1] %in% c("normal", "laplace", "lognormal", "exponential")) closed else iterative)(
log(n) * fits[[match(ranking[1], vapply(fits, `[[`, "", "key"))]]$k -
2 * fits[[match(ranking[1], vapply(fits, `[[`, "", "key"))]]$ll,
"BIC of the leading candidate."))),
c(sprintf("R ranking by BIC then AIC: %s; smallest BIC gap %.6g.", paste(ranking, collapse = ", "), gap),
"Student t is left out: it has no finite maximum on these data (see STAT-DESC-DISTFIT-001)."),
kind = "threshold")
# ---- Student t on heavy-tailed data, where a finite maximum exists ---------
set.seed(20260927)
export_dataset(data.frame(y = round(10 + 2 * rt(40, df = 3), 6)), "DESC_DISTFIT_HEAVY",
"Constructed: 10 + 2 * rt(40, df = 3), seed 20260927, rounded to 6 decimals",
"R stats::rt documentation", "Constructed validation data; no third-party license.",
c(y = "Float64"))
xh <- read_dataset("DESC_DISTFIT_HEAVY")$y
th <- fit_t(xh)
if (is.null(th)) stop("expected a finite Student t maximum on DESC_DISTFIT_HEAVY")
# The statistician's ruling on issue #7: with free df, location and scale the
# Student t likelihood has no finite global maximum (location at one observation,
# scale -> 0, 0 < df < 1/(N - 1)), so the candidate is refused. The profile
# solution above is only a local maximum and is kept as a note, not a reference.
emit("STAT-DESC-DISTFIT-012", "Unrestricted Student t is refused", "DESC_DISTFIT_HEAVY",
list(columns = list("y"), candidates = list("student_t"), origin = 0), list(),
c(sprintf("A local profile maximum exists at df = %.10g, but the likelihood is unbounded as the scale tends to zero with df < 1/(N - 1).", th$par[["df"]]),
"Until 2026-09-25 this case expected that local maximum; the issue #7 ruling makes the candidate unavailable."),
kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "No finite unrestricted maximum-likelihood fit"))
# ---- non-default origin -----------------------------------------------------
origin <- 2.5
positive <- list(fit_lognormal(x, origin), fit_gamma(x, origin), fit_weibull(x, origin),
fit_exponential(x, origin))
if (min(x) <= origin) stop("dataset must lie above the origin")
out <- list(); for (f in positive) out <- c(out, candidate_outputs(f, n))
out[["/columns/0/candidates/[key=lognormal]/label"]] <- exact("Lognormal (origin fixed at 2.5)", "The label names the exact declared origin (issue #3).")
emit("STAT-DESC-DISTFIT-003", "Positive families at a declared origin of 2.5", "DESC_DISTFIT_01",
list(columns = list("y"), candidates = list("lognormal", "gamma", "weibull", "exponential"), origin = origin),
out, c("The origin 2.5 lies below every value, so no family is skipped; the fits use y - 2.5."))
# ---- Anderson-Darling statistic for a normal-only fit -----------------------
nf <- fit_normal(x)
emit("STAT-DESC-DISTFIT-004", "Anderson-Darling statistic of the normal fit", "DESC_DISTFIT_01",
list(columns = list("y"), candidates = list("normal"), origin = 0,
calibration = "fixed", mc_samples = 99L, seed = 17L),
c(candidate_outputs(nf, n),
list("/columns/0/goodness/value" = closed(
anderson_darling(x, function(v) pnorm(v, nf$par[["loc"]], nf$par[["scale"]])),
"A^2 = -n - (1/n) sum (2i-1)[log F(x_i) + log(1 - F(x_(n+1-i)))] at the MLE."),
"/columns/0/goodness/statistic" = exact("Anderson-Darling", "Named statistic."))),
c("The Monte Carlo p-value (99 seeded refits) belongs to the V27 simulation studies."),
deferred = list(list(path = "/columns/0/goodness/p_value",
reason = "Monte Carlo p-value; covered by V27 simulation studies, not a closed-form reference.")),
version = "1.1.0")
normality_outputs <- function(z) {
n <- length(z)
raw <- anderson_darling(z, function(v) pnorm(v, mean(z), sd(z)))
modified <- raw * (1 + 0.75 / n + 2.25 / n^2)
if (modified < 0.119 || modified > 1.159) stop("reference needs an in-span statistic")
p <- if (modified <= 0.2) {
-expm1(-13.436 + 101.14 * modified - 223.73 * modified^2)
} else if (modified <= 0.34) {
-expm1(-8.318 + 42.796 * modified - 59.938 * modified^2)
} else if (modified <= 0.6) {
exp(0.9177 - 4.279 * modified - 1.38 * modified^2)
} else {
exp(1.2937 - 5.709 * modified + 0.0186 * modified^2)
}
numeric_reference <- function(value, rationale) list(
comparison = "tolerance", value = dec(value),
absolute_tolerance = "0", relative_tolerance = "1e-9", rationale = rationale
)
list(
"/columns/0/goodness/value" = numeric_reference(raw, "EDF A2 at the sample mean and sample SD (n - 1), computed independently in R."),
"/columns/0/goodness/modified_statistic" = numeric_reference(modified, "Stephens (1986), normal Case 3: A* = A2 (1 + 0.75/n + 2.25/n^2)."),
"/columns/0/goodness/p_value" = numeric_reference(p, "Stephens (1986), Table 4.9, A2 Case 3 approximation, within its published span."),
"/columns/0/goodness/calibration" = exact("published_table", "The hybrid route uses the approved normality test."),
"/columns/0/goodness/monte_carlo_interval" = list(comparison = "absent", rationale = "A deterministic table approximation has no Monte Carlo interval.")
)
}
emit("STAT-DESC-DISTFIT-015", "Hybrid normality test with separate sample-SD estimators", "DESC_DISTFIT_01",
list(columns = list("y"), candidates = list("normal"), origin = 0, calibration = "hybrid"),
c(candidate_outputs(nf, n), normality_outputs(x)),
c("Owner-approved #201 amendment: test mean/sample SD differ from the retained ML distribution fit.",
"D'Agostino and Stephens (1986), chapter 4, Case 3 p. 122; Tables 4.7 p. 123 and 4.9 p. 127."))
lnf <- fit_lognormal(x, 2.5)
emit("STAT-DESC-DISTFIT-016", "Hybrid fixed-origin lognormal normality test", "DESC_DISTFIT_01",
list(columns = list("y"), candidates = list("lognormal"), origin = 2.5, calibration = "hybrid"),
c(candidate_outputs(lnf, n), normality_outputs(log(x - 2.5))),
c("Owner-approved #201 amendment: normality of log(x - origin), with origin fixed at 2.5 and sample SD for the test only.",
"D'Agostino and Stephens (1986), chapter 4, Case 3 p. 122; Tables 4.7 p. 123 and 4.9 p. 127."))
# ---- boundary: the smallest accepted sample (8) ----------------------------
export_dataset(data.frame(y = c(4.1, 5.3, 4.8, 6.2, 5.9, 5.1, 4.4, 5.6)),
"DESC_DISTFIT_MIN8", "Constructed: eight values, the manual's minimum",
"Hand-constructed", "Constructed validation data; no third-party license.", c(y = "Float64"))
x8 <- read_dataset("DESC_DISTFIT_MIN8")$y
out <- c(candidate_outputs(fit_normal(x8), 8), candidate_outputs(fit_exponential(x8, 0), 8))
out[["/columns/0/count"]] <- exact(8L, "Eight finite values, the minimum.")
emit("STAT-DESC-DISTFIT-005", "Smallest accepted sample of eight", "DESC_DISTFIT_MIN8",
list(columns = list("y"), candidates = list("normal", "exponential"), origin = 0), out,
c("distribution_fit.MIN_OBSERVATIONS is 8; Laplace is omitted because its location is not unique for even n."),
kind = "boundary")
# ---- boundary: positive families skipped when a value is at the origin ------
export_dataset(data.frame(y = c(0, 1.2, 2.3, 1.9, 3.1, 2.6, 1.4, 2.2, 2.8, 1.7, 2.0)),
"DESC_DISTFIT_ZERO", "Constructed: eleven values including one at the origin 0",
"Hand-constructed", "Constructed validation data; no third-party license.", c(y = "Float64"))
xz <- read_dataset("DESC_DISTFIT_ZERO")$y
emit("STAT-DESC-DISTFIT-006", "Positive families skipped for a value at the origin", "DESC_DISTFIT_ZERO",
list(columns = list("y"), candidates = list("normal", "lognormal", "gamma"), origin = 0),
c(candidate_outputs(fit_normal(xz), length(xz)),
list("/columns/0/candidates/1" = list(comparison = "absent",
rationale = "Lognormal and gamma need every value strictly above the origin, so both are skipped, leaving the normal fit only."))),
c("One value equals the origin 0, so both strictly positive families are skipped (issue #3 ruling, item 5)."),
kind = "boundary")
# ---- missing values plus excluded rows -------------------------------------
set.seed(20260925)
raw <- round(rnorm(16, 50, 4), 4)
raw[c(3, 11)] <- NA
# An id column keeps every row non-blank: read.csv skips a line whose only
# field is empty, which would drop the missing rows and shift the exclusions.
export_dataset(data.frame(id = seq_len(16L), y = raw), "DESC_DISTFIT_MISSING",
"Constructed: rnorm(16, 50, 4), seed 20260925, rounded to 4 decimals, rows 3 and 11 missing",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(id = "Int64", y = "Float64"))
xm <- read_dataset("DESC_DISTFIT_MISSING")$y
if (length(xm) != 16L) stop("DESC_DISTFIT_MISSING lost rows on reading")
excluded <- c(0L, 7L, 12L)
kept <- xm[-(excluded + 1L)]; kept <- kept[!is.na(kept)]
out <- c(candidate_outputs(fit_normal(kept), length(kept)), candidate_outputs(fit_laplace(kept), length(kept)))
out[["/columns/0/count"]] <- exact(length(kept), "Excluded rows and missing values are both dropped.")
emit("STAT-DESC-DISTFIT-007", "Missing values and excluded rows", "DESC_DISTFIT_MISSING",
list(columns = list("y"), candidates = list("normal", "laplace"), origin = 0), out,
c(sprintf("Rows 0, 7 and 12 (0-based) excluded and two missing values dropped: %d values remain (odd, so the Laplace location is unique).", length(kept))),
kind = "missing_excluded", excluded = as.list(excluded))
# ---- hard numeric: large offset, small spread ------------------------------
set.seed(20260926)
export_dataset(data.frame(y = 1e6 + round(rnorm(21, 0, 1e-3), 9)), "DESC_DISTFIT_OFFSET",
"Constructed: 1e6 + rnorm(21, 0, 0.001), seed 20260926, rounded to 1e-9",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(y = "Float64"))
xo <- read_dataset("DESC_DISTFIT_OFFSET")$y
emit("STAT-DESC-DISTFIT-008", "Large offset with a small spread", "DESC_DISTFIT_OFFSET",
list(columns = list("y"), candidates = list("normal", "laplace"), origin = 0),
c(candidate_outputs(fit_normal(xo), length(xo)), candidate_outputs(fit_laplace(xo), length(xo))),
c("Values near 1e6 with a spread of 1e-3; the MLE scale is computed from centered values in R."),
kind = "hard_numeric")
# ---- invalid inputs --------------------------------------------------------
export_dataset(data.frame(y = c(1.5, 2.5, 3.5, 2.0, 4.0, 3.0, 2.2)), "DESC_DISTFIT_SEVEN",
"Constructed: seven values, one below the minimum", "Hand-constructed",
"Constructed validation data; no third-party license.", c(y = "Float64"))
emit("STAT-DESC-DISTFIT-009", "Seven values are below the minimum", "DESC_DISTFIT_SEVEN",
list(columns = list("y"), candidates = list("normal"), origin = 0), list(),
c("distribution_fit.MIN_OBSERVATIONS is 8."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "needs at least 8 finite observations"))
export_dataset(data.frame(y = rep(3.25, 10)), "DESC_DISTFIT_CONSTANT",
"Constructed: ten equal values", "Hand-constructed",
"Constructed validation data; no third-party license.", c(y = "Float64"))
emit("STAT-DESC-DISTFIT-010", "A constant column has no distribution", "DESC_DISTFIT_CONSTANT",
list(columns = list("y"), candidates = list("normal"), origin = 0), list(),
c("A zero-variance column is refused before fitting."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "is constant"))
emit("STAT-DESC-DISTFIT-011", "An unknown candidate is refused", "DESC_DISTFIT_01",
list(columns = list("y"), candidates = list("normal", "cauchy"), origin = 0), list(),
c("Only the eight manual candidates are accepted (manual lines 772-774)."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "Unknown distributions: cauchy"))
# ---- statistician's ruling on issue #3 -------------------------------------
# The exponential's support is x >= origin: a value at the origin is a valid fit.
emit("STAT-DESC-DISTFIT-013", "Exponential with a value at its origin", "DESC_DISTFIT_ZERO",
list(columns = list("y"), candidates = list("exponential"), origin = 0),
c(candidate_outputs(fit_exponential(xz, 0), length(xz)),
list("/columns/0/goodness/value" = list(comparison = "absent",
rationale = "At a value on the origin F(x) = 0, so the Anderson-Darling statistic is undefined and is reported unavailable, not approximated (issue #3)."))),
c("Until 2026-09-24 the product refused this valid fit; the statistician's ruling on issue #3 allows x = origin for the exponential only."),
kind = "boundary")
# The ruling's independent reference table: 1..8 at origin 0.
export_dataset(data.frame(y = as.numeric(1:8)), "DESC_DISTFIT_ONE_TO_EIGHT",
"Constructed: the integers 1 to 8, the statistician's issue #3 example",
"Hand-constructed", "Constructed validation data; no third-party license.", c(y = "Float64"))
x18 <- read_dataset("DESC_DISTFIT_ONE_TO_EIGHT")$y
out <- list()
for (f in list(fit_lognormal(x18, 0), fit_gamma(x18, 0), fit_weibull(x18, 0), fit_exponential(x18, 0))) {
out <- c(out, candidate_outputs(f, length(x18)))
}
emit("STAT-DESC-DISTFIT-014", "Fixed-origin positive families on 1 to 8", "DESC_DISTFIT_ONE_TO_EIGHT",
list(columns = list("y"), candidates = list("lognormal", "gamma", "weibull", "exponential"), origin = 0),
out,
c("Reproduces the statistician's issue #3 table (lognormal, gamma, Weibull, exponential at origin 0).",
"Weibull shape from its score equation, which SciPy's fit missed by 7.3e-6 relative."))
generators/r/descriptives_missing_values.R
source("common.R", local = TRUE)
# Missing value analysis (Statistics > Missing data > Missing value analysis): univariate
# counts and fences, missingness patterns, separate-variance (Welch) comparisons with Holm
# adjustment, normal-model EM and Little's MCAR test, all written in base R from the
# definitions of docs/manual/chapters/89-missingness-diagnostics.tex.
mva <- data.frame(
x = c(5.1, 4.8, NA, 6.2, 5.9, NA, 7.4, 5.5, 4.9, NA, 6.8, 5.2,
6.1, NA, 5.7, 7.9, 4.4, 6.5, NA, 5.0, 6.3, 5.8, 4.7, 6.9),
y = c(10.2, 9.1, 11.5, NA, 10.8, 9.7, 12.9, 10.1, NA, 9.4, 11.8, 10.4,
NA, 10.9, 10.0, 13.5, 8.8, 11.2, 10.6, NA, 11.0, 10.3, 9.0, 12.4),
z = c(2.3, 1.9, 2.8, 3.1, 2.6, 2.0, 3.6, 2.4, 2.1, NA, 3.2, 2.5,
2.9, 2.2, 2.7, 3.9, 1.7, 3.0, 2.4, 2.2, 3.1, 2.6, 1.8, 3.4),
g = c("A", "B", "A", NA, "B", "A", "B", "A", NA, "B", "A", "B",
"A", "B", NA, "A", "B", "A", "B", "A", "B", "A", "B", "A"),
stringsAsFactors = FALSE
)
export_dataset(
mva, "DESC_MVA_01", "Constructed by descriptives_missing_values.R",
"Base R: quantile (type 7), t.test (Welch), p.adjust (Holm), a hand-written normal-model EM and Little (1988) JASA 83:1198-1202.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64", z = "Float64", g = "String")
)
d <- read_dataset("DESC_MVA_01")
run <- generator_run(c("stats", "jsonlite"))
source_ref <- "Base R (stats) with a hand-written multivariate-normal EM"
citation <- paste(
"Little, R. J. A. (1988). A test of missing completely at random for multivariate data",
"with missing values. JASA 83:1198-1202; Dempster, Laird and Rubin (1977) JRSS B 39:1-38;",
"R stats: quantile (type 7), t.test, p.adjust, pchisq, pt."
)
tol <- function(value, label, tier = "1e-9") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste("Base R", label, "from the re-read DESC_MVA_01 CSV."))
}
prob <- function(value, label, tier = "1e-8") {
list(comparison = "tail_probability", value = dec(unname(value)), relative_tolerance = tier,
rationale = paste("Base R", label, "from the re-read DESC_MVA_01 CSV."))
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
# --- the diagnostics in base R ----------------------------------------------------------
univariate <- function(values) {
v <- values[!is.na(values)]
q <- stats::quantile(v, c(0.25, 0.75), type = 7, names = FALSE)
spread <- 1.5 * (q[2] - q[1])
list(present = length(v), missing = sum(is.na(values)),
percent = 100 * sum(is.na(values)) / length(values),
mean = mean(v), sd = stats::sd(v),
low = sum(v < q[1] - spread), high = sum(v > q[2] + spread))
}
welch_rows <- function(data, quantitative, categorical, min_percent, adjustment) {
variables <- c(quantitative, categorical)
missing <- lapply(variables, function(name) is.na(data[[name]]))
names(missing) <- variables
percent <- vapply(missing, function(flag) 100 * mean(flag), 0)
indicators <- variables[percent > 0 & percent >= min_percent]
rows <- list()
for (indicator in indicators) {
flag <- missing[[indicator]]
for (name in quantitative) {
if (name == indicator) next
observed <- !is.na(data[[name]])
present <- data[[name]][observed & !flag]
absent <- data[[name]][observed & flag]
if (length(present) < 2 || length(absent) < 2) {
rows[[length(rows) + 1]] <- list(indicator = indicator, variable = name,
status = "not computed: fewer than two cases in a group",
present_count = length(present), missing_count = length(absent))
next
}
test <- stats::t.test(present, absent, var.equal = FALSE)
rows[[length(rows) + 1]] <- list(indicator = indicator, variable = name, status = "",
present_count = length(present), missing_count = length(absent),
difference = mean(present) - mean(absent),
t = unname(test$statistic), df = unname(test$parameter),
p = test$p.value)
}
}
tested <- which(vapply(rows, function(r) r$status == "", TRUE))
if (adjustment == "holm" && length(tested)) {
adjusted <- stats::p.adjust(vapply(rows[tested], function(r) r$p, 0), method = "holm")
for (i in seq_along(tested)) rows[[tested[i]]]$adjusted <- adjusted[i]
}
filtered <- variables[percent > 0 & percent < min_percent]
list(rows = rows, indicators = indicators, filtered = filtered, percent = percent)
}
em_mvn <- function(X, tol, maxit) {
obs <- !is.na(X)
use <- rowSums(obs) > 0
X <- X[use, , drop = FALSE]
obs <- obs[use, , drop = FALSE]
n <- nrow(X); k <- ncol(X)
mu <- colMeans(X, na.rm = TRUE)
S <- diag(apply(X, 2, function(col) { v <- col[!is.na(col)]; mean((v - mean(v))^2) }), k)
ll_obs <- function(mu, S) {
total <- 0
for (i in seq_len(n)) {
o <- obs[i, ]
So <- S[o, o, drop = FALSE]
r <- X[i, o] - mu[o]
total <- total - 0.5 * (sum(o) * log(2 * pi) + as.numeric(determinant(So, logarithm = TRUE)$modulus) +
drop(t(r) %*% solve(So, r)))
}
total
}
previous <- ll_obs(mu, S)
converged <- FALSE
ll <- previous
for (it in seq_len(maxit)) {
T1 <- numeric(k); T2 <- matrix(0, k, k)
for (i in seq_len(n)) {
o <- obs[i, ]; m <- !o
xh <- X[i, ]
C <- matrix(0, k, k)
if (any(m)) {
B <- S[m, o, drop = FALSE] %*% solve(S[o, o, drop = FALSE])
xh[m] <- mu[m] + drop(B %*% (X[i, o] - mu[o]))
C[m, m] <- S[m, m, drop = FALSE] - B %*% S[o, m, drop = FALSE]
}
T1 <- T1 + xh
T2 <- T2 + xh %*% t(xh) + C
}
mu_new <- T1 / n
S_new <- T2 / n - mu_new %*% t(mu_new)
S_new <- (S_new + t(S_new)) / 2
sd <- sqrt(diag(S_new))
change <- max(max(abs(mu_new - mu) / sd), max(abs(S_new - S) / outer(sd, sd)))
mu <- mu_new; S <- S_new
ll <- ll_obs(mu, S)
if (change <= tol && abs(ll - previous) <= tol) { converged <- TRUE; break }
previous <- ll
}
stopifnot(converged)
list(mu = mu, S = S, R = S / outer(sd, sd), ll = ll, iterations = it, n = n, obs = obs, X = X)
}
little <- function(em) {
k <- ncol(em$X)
keys <- apply(em$obs, 1, function(o) paste(as.integer(o), collapse = ""))
d2 <- 0; df <- -k
for (key in unique(keys)) {
rows <- which(keys == key)
o <- em$obs[rows[1], ]
ybar <- colMeans(em$X[rows, o, drop = FALSE])
r <- ybar - em$mu[o]
d2 <- d2 + length(rows) * drop(t(r) %*% solve(em$S[o, o, drop = FALSE], r))
df <- df + sum(o)
}
list(statistic = d2, df = df, p = stats::pchisq(d2, df, lower.tail = FALSE),
patterns = length(unique(keys)))
}
pattern_table <- function(data, variables) {
flags <- sapply(variables, function(name) is.na(data[[name]]))
keys <- apply(flags, 1, function(row) paste(as.integer(row), collapse = ""))
counts <- table(keys)
# HypoNum sorts by (-count, pattern); the pattern tuple compares False < True elementwise,
# which is the lexical order of the "0"/"1" keys.
ordered <- counts[order(-as.integer(counts), names(counts))]
list(total = length(counts), first_count = as.integer(ordered[1]),
first_missing = sum(as.integer(strsplit(names(ordered)[1], "")[[1]])))
}
emit <- function(id, title, kind, configuration, outputs, notes, excluded = list()) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("descriptives", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "descriptives",
entry_point = "tabelz.analysis.missing_value_analysis:compute_missing_value_analysis",
menu_items = list("missingValueAnalysisMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/DESC_MVA_01.json", sha256 = ""),
excluded_rows = excluded, configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/descriptives_missing_values.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes, "Expected computed from the exported CSV.",
"docs/manual/chapters/89-missingness-diagnostics.tex:11-17: present/missing counts, mean, SD, counts outside Q1 - 1.5 IQR and Q3 + 1.5 IQR with interpolated quartiles; pattern table of distinct present/missing combinations.",
"docs/manual/chapters/89-missingness-diagnostics.tex:19-24: a variable missing in at least the threshold percent splits the cases; Welch's test, difference = present minus missing; Holm over the whole computed family by default.",
"docs/manual/chapters/89-missingness-diagnostics.tex:55-74: EM under the multivariate normal (tolerance 1e-6, 100 iterations; SPSS-style 1e-4, 25); Little's d^2 = sum_p n_p (ybar_p - mu)' Sigma_pp^-1 (ybar_p - mu), df = sum_p |O_p| - k."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-MISSING_VALUE_ANALYSIS-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
)
write_case("descriptives", id, case)
}
quant <- c("x", "y", "z")
welch_outputs <- function(w, indices, tier = "1e-9") {
out <- list()
for (i in indices) {
row <- w$rows[[i]]
base <- paste0("/welch/", i - 1)
label <- paste(row$indicator, "missing vs present on", row$variable)
out[[paste0(base, "/present_count")]] <- exact(as.integer(row$present_count), paste("present cases:", label))
out[[paste0(base, "/missing_count")]] <- exact(as.integer(row$missing_count), paste("missing cases:", label))
if (row$status != "") {
out[[paste0(base, "/status")]] <- exact(row$status, label)
next
}
out[[paste0(base, "/difference")]] <- tol(row$difference, paste("present minus missing mean:", label), tier)
out[[paste0(base, "/t_value")]] <- tol(row$t, paste("Welch t:", label), tier)
out[[paste0(base, "/df")]] <- tol(row$df, paste("Welch-Satterthwaite df:", label), tier)
out[[paste0(base, "/p_value")]] <- prob(row$p, paste("Welch p:", label))
if (!is.null(row$adjusted)) out[[paste0(base, "/adjusted_p_value")]] <- prob(row$adjusted, paste("Holm-adjusted p:", label))
}
out
}
em_outputs <- function(em, lt, tier, ptier) {
list(
"/em/status" = exact("converged", "EM converged within the cap"),
"/em/cases_used" = exact(as.integer(em$n), "cases with at least one quantitative value"),
"/em/means/0" = tol(em$mu[1], "EM mean of x", tier),
"/em/means/1" = tol(em$mu[2], "EM mean of y", tier),
"/em/means/2" = tol(em$mu[3], "EM mean of z", tier),
"/em/covariance/0/0" = tol(em$S[1, 1], "EM ML variance of x", tier),
"/em/covariance/0/1" = tol(em$S[1, 2], "EM covariance of x and y", tier),
"/em/covariance/1/2" = tol(em$S[2, 3], "EM covariance of y and z", tier),
"/em/correlations/0/2" = tol(em$R[1, 3], "EM correlation of x and z", tier),
"/em/log_likelihood" = tol(em$ll, "observed-data log-likelihood at the EM estimate", "1e-7"),
"/little/status" = exact("computed", "Little's test is computed"),
"/little/statistic" = tol(lt$statistic, "Little's d^2 from the EM estimates", tier),
"/little/df" = exact(as.integer(lt$df), "sum of observed counts over patterns minus k"),
"/little/p_value" = prob(lt$p, "Little chi-square tail", ptier),
"/little/patterns" = exact(as.integer(lt$patterns), "patterns among cases with any quantitative value")
)
}
# --- case 001: defaults --------------------------------------------------------------------
ux <- univariate(d$x)
w <- welch_rows(d, quant, "g", 5.0, "holm")
stopifnot(identical(w$indicators, c("x", "y", "g")), identical(w$filtered, "z"))
pt <- pattern_table(d, c(quant, "g"))
em <- em_mvn(as.matrix(d[quant]), 1e-13, 20000)
lt <- little(em)
outputs <- list(
"/cases" = exact(nrow(d), "24 rows"),
"/univariate/0/present" = exact(as.integer(ux$present), "x present"),
"/univariate/0/missing" = exact(as.integer(ux$missing), "x missing"),
"/univariate/0/percent_missing" = tol(ux$percent, "x percent missing"),
"/univariate/0/mean" = tol(ux$mean, "x available-case mean"),
"/univariate/0/sd" = tol(ux$sd, "x available-case SD (n - 1)"),
"/univariate/0/low_extremes" = exact(as.integer(ux$low), "x values below Q1 - 1.5 IQR (type-7 quartiles)"),
"/univariate/0/high_extremes" = exact(as.integer(ux$high), "x values above Q3 + 1.5 IQR (type-7 quartiles)"),
"/univariate/3/missing" = exact(sum(is.na(d$g)), "g missing"),
"/univariate/3/percent_missing" = tol(100 * mean(is.na(d$g)), "g percent missing"),
"/patterns_total" = exact(as.integer(pt$total), "distinct missingness patterns"),
"/patterns/0/count" = exact(pt$first_count, "most frequent pattern count"),
"/patterns/0/percent" = tol(100 * pt$first_count / nrow(d), "most frequent pattern percent"),
"/patterns/0/variables_missing" = exact(as.integer(pt$first_missing), "variables missing in the most frequent pattern"),
"/welch_filtered/0/0" = exact("z", "z is below the 5% threshold"),
"/welch_filtered/0/1" = tol(w$percent[["z"]], "percent missing of the filtered indicator z")
)
outputs <- c(outputs, welch_outputs(w, seq_along(w$rows)), em_outputs(em, lt, "1e-4", "1e-4"))
# Crosstab rows: g by indicator x then y; categories A, B then the null category.
gx_a_missing <- sum(d$g == "A" & is.na(d$x), na.rm = TRUE)
gx_null_missing <- sum(is.na(d$g) & is.na(d$x))
outputs[["/crosstabs/0/missing"]] <- exact(as.integer(gx_a_missing), "g = A cases missing x")
outputs[["/crosstabs/0/present"]] <- exact(as.integer(sum(d$g == "A" & !is.na(d$x), na.rm = TRUE)), "g = A cases present on x")
outputs[["/crosstabs/2/missing"]] <- exact(as.integer(gx_null_missing), "g missing cases missing x")
outputs[["/crosstabs/2/percent_missing"]] <- tol(100 * gx_null_missing / sum(is.na(d$g)), "percent of g-missing cases missing x")
emit("STAT-DESC-MVA-001", "Missing value analysis with the default settings", "nominal",
list(quantitative = list("x", "y", "z"), categorical = list("g")),
outputs,
list("Univariate: available-case mean and SD; fences from quantile(type = 7) (numpy linear).",
"Welch rows are listed per indicator (x, y, g; z is filtered at 4.17% < 5%) and then per other quantitative variable; t.test(var.equal = FALSE), difference present minus missing; p.adjust(method = 'holm') over the six computed tests (g->y has one missing case and is not computed).",
"EM is iterated in R to a scaled change of 1e-13 (tolerance 1e-13, 20000 iterations); the product stops at 1e-6, so EM estimates, Little's statistic and the log-likelihood carry relative tolerances 1e-4 and 1e-7.",
"Little's test uses the EM maximum-likelihood mean and n-denominator covariance over the patterns of the three quantitative variables (every row has at least one value, so 24 cases enter)."))
# --- case 002: raw p-values, 1% threshold, SPSS-style EM preset ----------------------------
w2 <- welch_rows(d, quant, "g", 1.0, "none")
stopifnot(identical(w2$indicators, c("x", "y", "z", "g")), length(w2$filtered) == 0)
not_computed <- which(vapply(w2$rows, function(r) r$status != "", TRUE))
stopifnot(identical(not_computed, c(5L, 6L, 8L))) # z->x, z->y and g->y have under two missing cases
outputs <- c(
list("/cases" = exact(nrow(d), "24 rows"),
"/em/status" = exact("converged", "EM converges under the SPSS-style preset (1e-4, 25 iterations)"),
"/em/tolerance" = tol(1e-4, "SPSS-style tolerance"),
"/em/max_iterations" = exact(25L, "SPSS-style iteration cap"),
"/em/means/0" = tol(em$mu[1], "EM mean of x (SPSS-style preset)", "1e-3"),
"/little/df" = exact(as.integer(lt$df), "Little df does not depend on the EM tolerance")),
welch_outputs(w2, seq_along(w2$rows))
)
emit("STAT-DESC-MVA-002", "Raw p-values, 1% indicator threshold, SPSS-style EM", "axis_coverage",
list(quantitative = list("x", "y", "z"), categorical = list("g"), min_percent = 1.0,
adjustment = "none", em_preset = "spss"),
outputs,
list("With the threshold at 1% the indicator z (one missing case) enters: its two comparisons have under two cases in the missing group and are reported as not computed, as is g->y; the nine rows are x->y, x->z, y->x, y->z, z->x, z->y, g->x, g->y, g->z.",
"adjustment = none leaves the raw Welch p-values; no adjusted value is compared.",
"The SPSS-style preset stops EM at a scaled change of 1e-4, so only the mean of x is compared, at relative tolerance 1e-3; the R EM is run to 1e-13."))
# --- case 003: custom tight EM tolerance ---------------------------------------------------
outputs <- c(
list("/cases" = exact(nrow(d), "24 rows"),
"/em/tolerance" = tol(1e-10, "custom tolerance"),
"/em/max_iterations" = exact(5000L, "custom iteration cap")),
em_outputs(em, lt, "1e-7", "1e-6")
)
emit("STAT-DESC-MVA-003", "EM to a custom tolerance of 1e-10", "axis_coverage",
list(quantitative = list("x", "y", "z"), categorical = list("g"), em_preset = "custom",
em_tolerance = 1e-10, em_max_iterations = 5000L),
outputs,
list("Both EM runs stop near the same maximum (R at a scaled change of 1e-13, the product at 1e-10), so the EM estimates and Little's statistic are compared at relative tolerance 1e-7 and the p-value at 1e-6.",
"The convergence check compares the largest scaled parameter change and the observed log-likelihood change with the tolerance."))
# --- case 004: excluded rows -------------------------------------------------------------
kept <- d[-c(1, 16), , drop = FALSE] # zero-based source rows 0 and 15 are excluded
ux4 <- univariate(kept$x)
w4 <- welch_rows(kept, quant, "g", 5.0, "holm")
stopifnot(identical(w4$indicators, c("x", "y", "g")))
pt4 <- pattern_table(kept, c(quant, "g"))
em4 <- em_mvn(as.matrix(kept[quant]), 1e-13, 20000)
lt4 <- little(em4)
outputs <- c(
list("/cases" = exact(nrow(kept), "22 rows after excluding two"),
"/excluded_count" = exact(2L, "two excluded rows"),
"/univariate/0/present" = exact(as.integer(ux4$present), "x present without the excluded rows"),
"/univariate/0/mean" = tol(ux4$mean, "x mean without the excluded rows"),
"/univariate/0/high_extremes" = exact(as.integer(ux4$high), "x high extremes without row 15 (7.9)"),
"/patterns_total" = exact(as.integer(pt4$total), "patterns without the excluded rows"),
"/patterns/0/count" = exact(pt4$first_count, "most frequent pattern count without the excluded rows")),
welch_outputs(w4, 1:2),
em_outputs(em4, lt4, "1e-4", "1e-4")
)
emit("STAT-DESC-MVA-004", "Excluded rows leave the analysis population", "missing_excluded",
list(quantitative = list("x", "y", "z"), categorical = list("g")),
outputs,
list("Zero-based source rows 0 and 15 are excluded before every count, test and EM; the reference drops the same rows from the re-read CSV."),
excluded = list(0L, 15L))
cat("missing value analysis cases generated\n")
generators/r/descriptives_model_scoring.R
source("common.R", local = TRUE)
# References for Stored regression scoring (manual chapter 37):
# tabelz.executor.model_scoring fit, predictions, influence
# tabelz.analysis.model_scoring:score_model_scoring scoring with a frozen artifact
# operation:tabelz.model_scoring_scores replayable score columns
# Conventions (chapter 37): categorical predictors use sorted levels with the
# first as reference (lines 18-20); OLS/WLS mean variance x' Cov(b) x, OLS
# individual adds s^2, both with t on n - p df (lines 61-64); logistic and
# Poisson mean limits are normal Wald limits on the linear predictor through
# the inverse link, with the Poisson log-exposure offset (lines 65-67); OLS
# influence: leverage, internally studentized residual with the full-fit
# variance, Cook's distance, DFBETAS with the deleted-row variance (lines 74-78).
run <- generator_run(c("stats", "jsonlite"))
family <- "descriptives"
generator <- "generators/r/descriptives_model_scoring.R"
fit_entry <- "tabelz.executor.model_scoring"
score_entry <- "tabelz.analysis.model_scoring:score_model_scoring"
op_entry <- "operation:tabelz.model_scoring_scores"
menu <- "modelScoringMenuItem"
closed_tol <- "1e-9"
glm_tol <- "1e-6"
tol <- function(value, why, tier = closed_tol) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste(if (tier == closed_tol) "Closed-form least squares; owner 1e-9 tier." else
"Iterative GLM fit; owner 1e-6 tier.", why))
seq_tol <- function(values, why, tier = closed_tol) list(
comparison = "sequence", values = lapply(unname(values), dec), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste(if (tier == closed_tol) "Closed-form least squares; owner 1e-9 tier." else
"Iterative GLM fit; owner 1e-6 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(id, entry, title, dataset, config, outputs, notes, kind = "nominal",
excluded = integer(), error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run
))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family,
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats::lm, glm, predict and influence measures",
citation = "R stats::lm, stats::glm, stats::predict.lm, stats::predict.glm and stats::influence.measures documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes,
"Manual chapter 37 lines 18-20, 61-67 and 74-78 fix the encoding, the intervals and the OLS diagnostics.",
"All reference calculations re-read the exported CSV."))
),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-DESC-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL
))
}
# ---- data -------------------------------------------------------------------
set.seed(20260928)
n <- 30L
grp <- rep(c("B", "A", "C"), length.out = n)
x1 <- round(runif(n, 1, 9), 3)
w <- round(runif(n, 0.5, 3), 3)
expo <- round(runif(n, 0.5, 4), 3)
eta_y <- 2 + 0.8 * x1 + c(A = 0, B = 1.5, C = -1)[grp]
y <- round(eta_y + rnorm(n, 0, 1.2), 4)
yb <- ifelse(runif(n) < plogis(-2 + 0.45 * x1 + c(A = 0, B = 0.8, C = -0.6)[grp]), "yes", "no")
cnt <- rpois(n, expo * exp(0.2 + 0.15 * x1 + c(A = 0, B = 0.3, C = -0.2)[grp]))
export_dataset(data.frame(x1 = x1, grp = grp, w = w, expo = expo, y = y, yb = yb, cnt = as.integer(cnt)),
"DESC_SCORE_FIT", "Constructed regression data, seed 20260928 (runif, rnorm, rpois)",
"R stats::lm and stats::glm documentation", "Constructed validation data; no third-party license.",
c(x1 = "Float64", grp = "String", w = "Float64", expo = "Float64", y = "Float64",
yb = "String", cnt = "Int64"))
d <- read_dataset("DESC_SCORE_FIT")
d$grp <- factor(d$grp, levels = sort(unique(d$grp)))
coef_names <- c("Intercept", "x1", "grp = B (vs A)", "grp = C (vs A)")
fit_outputs <- function(model, prefix_tier, kind, level = 0.95, family_name = "ols", data = d) {
tier <- prefix_tier
out <- list()
out[["/artifact/coefficient_names"]] <- list(comparison = "sequence", values = as.list(coef_names),
rationale = "Intercept, then numeric predictors, then indicators against the first sorted level.")
out[["/artifact/coefficients"]] <- seq_tol(coef(model), "Coefficients in artifact order.", tier)
v <- vcov(model)
for (i in seq_len(nrow(v))) {
out[[paste0("/artifact/covariance/", i - 1L)]] <- seq_tol(v[i, ], sprintf("Covariance row %d.", i), tier)
}
out[["/artifact/design_rank"]] <- exact(ncol(v), "Full-rank design.")
if (family_name %in% c("ols", "wls")) {
out[["/artifact/residual_df"]] <- exact(as.integer(df.residual(model)), "n - p, with the intercept in p.")
out[["/artifact/residual_variance"]] <- tol(summary(model)$sigma^2, "Residual variance s^2.", tier)
}
if (family_name %in% c("ols", "wls")) {
interval <- if (kind == "individual") "prediction" else "confidence"
p <- predict(model, interval = interval, level = level)
out[["/predictions"]] <- seq_tol(p[, "fit"], "Fitted values in row order.", tier)
out[["/lower"]] <- seq_tol(p[, "lwr"], sprintf("Lower %s limit, t on n - p df.", interval), tier)
out[["/upper"]] <- seq_tol(p[, "upr"], sprintf("Upper %s limit, t on n - p df.", interval), tier)
} else {
link <- predict(model, type = "link", se.fit = TRUE)
z <- qnorm(1 - (1 - level) / 2)
inverse <- family(model)$linkinv
out[["/predictions"]] <- seq_tol(inverse(link$fit), "Response-scale predictions in row order.", tier)
out[["/lower"]] <- seq_tol(inverse(link$fit - z * link$se.fit), "Wald lower limit through the inverse link.", tier)
out[["/upper"]] <- seq_tol(inverse(link$fit + z * link$se.fit), "Wald upper limit through the inverse link.", tier)
}
out
}
influence_outputs <- function(model) {
out <- list()
h <- hatvalues(model); r <- rstandard(model); cd <- cooks.distance(model); b <- dfbetas(model)
for (i in seq_along(h)) {
node <- paste0("/influence/", i - 1L)
out[[paste0(node, "/leverage")]] <- tol(h[[i]], "Hat-matrix leverage.")
out[[paste0(node, "/studentized_residual")]] <- tol(r[[i]], "Internally studentized with the full-fit variance (R rstandard).")
out[[paste0(node, "/cooks_distance")]] <- tol(cd[[i]], "Cook's distance.")
out[[paste0(node, "/dfbetas")]] <- seq_tol(b[i, ], "DFBETAS with the deleted-row variance (R dfbetas).")
}
out[["/influence_eligible"]] <- exact(TRUE, "OLS fits report influence.")
out
}
config <- function(family_name, response, interval_kind = "mean", level = NULL, extra = list()) {
base <- list(family = family_name, response = response, predictors = list("x1", "grp"),
interval_kind = interval_kind)
if (!is.null(level)) base$confidence_level <- level
c(base, extra)
}
# ---- fit: OLS mean, OLS individual, WLS, logistic, Poisson ------------------
ols <- lm(y ~ x1 + grp, data = d)
emit("STAT-DESC-SCOREFIT-001", fit_entry, "OLS fit with mean-response intervals and influence", "DESC_SCORE_FIT",
config("ols", "y"), c(fit_outputs(ols, closed_tol, "mean"), influence_outputs(ols)),
c("lm(y ~ x1 + grp) with levels A < B < C; predict(interval = 'confidence')."))
emit("STAT-DESC-SCOREFIT-002", fit_entry, "OLS individual prediction intervals", "DESC_SCORE_FIT",
config("ols", "y", "individual"), fit_outputs(ols, closed_tol, "individual"),
c("predict(interval = 'prediction') adds s^2 to the mean variance (manual line 62)."))
wls <- lm(y ~ x1 + grp, data = d, weights = w)
emit("STAT-DESC-SCOREFIT-003", fit_entry, "WLS fit with mean-response intervals", "DESC_SCORE_FIT",
config("wls", "y", extra = list(weight_column = "w")),
c(fit_outputs(wls, closed_tol, "mean", family_name = "wls"),
list("/influence_eligible" = exact(FALSE, "WLS fits do not report OLS influence (line 80-81)."))),
c("lm(y ~ x1 + grp, weights = w): weights are relative precisions (line 64-65)."))
logit <- glm(I(yb == "yes") ~ x1 + grp, data = d, family = binomial(),
control = glm.control(epsilon = 1e-14, maxit = 100))
emit("STAT-DESC-SCOREFIT-004", fit_entry, "Binary logistic fit with Wald probability limits", "DESC_SCORE_FIT",
config("logistic", "yb", extra = list(event_value = "yes")),
c(fit_outputs(logit, glm_tol, "mean", family_name = "logistic"),
list("/influence_eligible" = exact(FALSE, "Logistic fits do not report OLS influence."))),
c("glm(binomial) with epsilon = 1e-14, maxit = 100; event 'yes'."))
pois <- glm(cnt ~ x1 + grp + offset(log(expo)), data = d, family = poisson(),
control = glm.control(epsilon = 1e-14, maxit = 100))
emit("STAT-DESC-SCOREFIT-005", fit_entry, "Poisson fit with an exposure offset", "DESC_SCORE_FIT",
config("poisson", "cnt", extra = list(exposure_column = "expo")),
fit_outputs(pois, glm_tol, "mean", family_name = "poisson"),
c("glm(poisson) with offset log(expo), epsilon = 1e-14, maxit = 100; predictions are expected counts at the entered exposure."))
pois0 <- glm(cnt ~ x1 + grp, data = d, family = poisson(), control = glm.control(epsilon = 1e-14, maxit = 100))
emit("STAT-DESC-SCOREFIT-006", fit_entry, "Poisson fit without exposure", "DESC_SCORE_FIT",
config("poisson", "cnt"), fit_outputs(pois0, glm_tol, "mean", family_name = "poisson"),
c("No exposure is a zero offset (model_scoring.ModelScoringConfig)."))
emit("STAT-DESC-SCOREFIT-007", fit_entry, "OLS at a 90% confidence level", "DESC_SCORE_FIT",
config("ols", "y", level = 0.9), fit_outputs(ols, closed_tol, "mean", level = 0.9),
c("A non-default confidence level of 0.90."))
# ---- missing values and excluded rows ---------------------------------------
m <- read_dataset("DESC_SCORE_FIT")
m$y[c(4, 17)] <- NA
m$x1[9] <- NA
export_dataset(m, "DESC_SCORE_FIT_MISSING", "DESC_SCORE_FIT with y missing in rows 4 and 17 and x1 in row 9 (1-based)",
"R stats::lm documentation", "Constructed validation data; no third-party license.",
c(x1 = "Float64", grp = "String", w = "Float64", expo = "Float64", y = "Float64",
yb = "String", cnt = "Int64"))
mm <- read_dataset("DESC_SCORE_FIT_MISSING")
excluded <- c(0L, 21L)
kept <- mm[-(excluded + 1L), ]
kept <- kept[stats::complete.cases(kept[c("y", "x1", "grp")]), ]
kept$grp <- factor(kept$grp, levels = sort(unique(kept$grp)))
olsm <- lm(y ~ x1 + grp, data = kept)
emit("STAT-DESC-SCOREFIT-008", fit_entry, "Missing values and excluded rows are left out of the fit",
"DESC_SCORE_FIT_MISSING", config("ols", "y"), fit_outputs(olsm, closed_tol, "mean", data = kept),
c("Rows 0 and 21 (0-based) are excluded; rows with missing y or x1 are omitted and counted (line 50-53).",
sprintf("%d rows remain for the fit.", nrow(kept))),
kind = "missing_excluded", excluded = excluded)
# ---- hard numeric: a predictor at a large offset ----------------------------
h <- read_dataset("DESC_SCORE_FIT")
h$x1 <- h$x1 + 1e6
export_dataset(h, "DESC_SCORE_FIT_OFFSET", "DESC_SCORE_FIT with x1 shifted by 1e6",
"R stats::lm documentation", "Constructed validation data; no third-party license.",
c(x1 = "Float64", grp = "String", w = "Float64", expo = "Float64", y = "Float64",
yb = "String", cnt = "Int64"))
ho <- read_dataset("DESC_SCORE_FIT_OFFSET")
ho$grp <- factor(ho$grp, levels = sort(unique(ho$grp)))
olsh <- lm(y ~ x1 + grp, data = ho)
out <- fit_outputs(olsh, closed_tol, "mean", data = ho)
emit("STAT-DESC-SCOREFIT-009", fit_entry, "Predictor at a 1e6 offset", "DESC_SCORE_FIT_OFFSET",
config("ols", "y"), out[c("/artifact/coefficients", "/predictions", "/lower", "/upper", "/artifact/residual_variance")],
c("x1 + 1e6 leaves the slope and fitted values unchanged and moves the intercept; lm uses a QR decomposition."),
kind = "hard_numeric")
# ---- boundary: the smallest OLS fit with one residual df --------------------
export_dataset(data.frame(x = c(1, 2, 4), y = c(2.1, 3.9, 8.2)), "DESC_SCORE_MIN",
"Constructed: three points for a straight line", "Hand-constructed",
"Constructed validation data; no third-party license.", c(x = "Float64", y = "Float64"))
s <- read_dataset("DESC_SCORE_MIN")
olss <- lm(y ~ x, data = s)
ps <- predict(olss, interval = "confidence")
emit("STAT-DESC-SCOREFIT-010", fit_entry, "Three rows leave one residual df and no deletion diagnostics", "DESC_SCORE_MIN",
list(family = "ols", response = "y", predictors = list("x")),
list("/artifact/coefficients" = seq_tol(coef(olss), "Least-squares line."),
"/artifact/residual_df" = exact(1L, "n - p = 3 - 2."),
"/predictions" = seq_tol(ps[, "fit"], "Fitted values."),
"/lower" = seq_tol(ps[, "lwr"], "t limits with 1 df."),
"/upper" = seq_tol(ps[, "upr"], "t limits with 1 df."),
"/influence/0/leverage" = tol(hatvalues(olss)[[1]], "Leverage needs no deletion (issue #8)."),
"/influence/0/studentized_residual" = tol(rstandard(olss)[[1]], "Internally standardized residual; defined with d = 1 (issue #8)."),
"/influence/0/cooks_distance" = tol(cooks.distance(olss)[[1]], "Cook's distance uses the full-fit variance; defined with d = 1 (issue #8)."),
"/influence/0/deleted_studentized_residual" = absent("The deleted fit has no residual df."),
"/influence/0/dffits" = absent("The deleted fit has no residual df.")),
c("With n = p + 1 the full-fit diagnostics are defined (standardized magnitudes are all 1); deleted-variance diagnostics are not (statistician's ruling on issue #8).",
"Until 2026-09-25 this case expected every diagnostic but leverage to be absent."),
kind = "boundary")
# ---- invalid inputs ---------------------------------------------------------
emit("STAT-DESC-SCOREFIT-011", fit_entry, "WLS does not offer individual prediction intervals", "DESC_SCORE_FIT",
config("wls", "y", "individual", extra = list(weight_column = "w")), list(),
c("Manual line 34-36: WLS reports mean uncertainty only."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "WLS supports mean-response intervals only"))
c0 <- read_dataset("DESC_SCORE_FIT")
c0$x1 <- 5
export_dataset(c0, "DESC_SCORE_FIT_CONSTANT", "DESC_SCORE_FIT with a constant x1",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(x1 = "Float64", grp = "String", w = "Float64", expo = "Float64", y = "Float64",
yb = "String", cnt = "Int64"))
emit("STAT-DESC-SCOREFIT-012", fit_entry, "A constant numeric predictor stops the fit", "DESC_SCORE_FIT_CONSTANT",
config("ols", "y"), list(),
c("Manual line 54-55: a constant numeric predictor stops the fit with an error.",
"The message fragment is the product's wording for this error (model_scoring._predictor_specs)."),
kind = "invalid_input",
error = list(error_class = "ModelScoringExecutionError", message_contains = "must be finite and vary"))
# ---- scoring a target table with a frozen artifact --------------------------
artifact_of <- function(model, family_name, link, interval_kind = "mean", level = 0.95, extra = list()) {
v <- unname(vcov(model))
base <- list(
family = family_name, response = if (family_name == "logistic") "yb" else if (family_name == "poisson") "cnt" else "y",
predictors = list(list(name = "x1", kind = "numeric"),
list(name = "grp", kind = "categorical", levels = list("A", "B", "C"), reference_level = "A")),
feature_order = as.list(coef_names), coefficient_names = as.list(coef_names),
coefficients = lapply(unname(coef(model)), function(v) v),
covariance = lapply(seq_len(nrow(v)), function(i) as.list(v[i, ])),
link = link, interval_kind = interval_kind, confidence_level = level,
design_rank = ncol(v), fitted_row_ids = as.list(sprintf("fit-%02d", seq_len(nrow(d))))
)
if (family_name %in% c("ols", "wls")) {
base$residual_variance <- summary(model)$sigma^2
base$residual_df <- as.integer(df.residual(model))
}
c(base, extra)
}
set.seed(20260929)
nt <- 12L
target <- data.frame(
x1 = round(runif(nt, 0.5, 10), 3),
grp = rep(c("C", "A", "B"), length.out = nt),
expo = round(runif(nt, 0.5, 4), 3)
)
target$x1[5] <- NA
export_dataset(target, "DESC_SCORE_TARGET", "Constructed scoring target, seed 20260929; x1 missing in row 5 (1-based); no response column",
"R stats::predict documentation", "Constructed validation data; no third-party license.",
c(x1 = "Float64", grp = "String", expo = "Float64"))
t <- read_dataset("DESC_SCORE_TARGET")
t$grp <- factor(t$grp, levels = c("A", "B", "C"))
target_excluded <- c(2L)
scored <- setdiff(which(!is.na(t$x1)), target_excluded + 1L)
design <- cbind(1, t$x1, as.numeric(t$grp == "B"), as.numeric(t$grp == "C"))[scored, , drop = FALSE]
score_outputs <- function(model, family_name, interval_kind = "mean", level = 0.95, offset = 0) {
b <- coef(model); v <- vcov(model)
eta <- as.vector(design %*% b) + offset
se <- sqrt(rowSums((design %*% v) * design))
tier <- if (family_name %in% c("ols", "wls")) closed_tol else glm_tol
if (family_name %in% c("ols", "wls")) {
s2 <- summary(model)$sigma^2
if (interval_kind == "individual") se <- sqrt(se^2 + s2)
q <- qt(1 - (1 - level) / 2, df.residual(model))
fit <- eta; lo <- eta - q * se; hi <- eta + q * se
} else {
z <- qnorm(1 - (1 - level) / 2)
inverse <- if (family_name == "logistic") plogis else exp
fit <- inverse(eta); lo <- inverse(eta - z * se); hi <- inverse(eta + z * se)
}
list(fit = fit, lo = lo, hi = hi, tier = tier)
}
score_case <- function(id, title, model, family_name, link, interval_kind = "mean", extra = list(), offset = 0, notes = character()) {
s <- score_outputs(model, family_name, interval_kind, offset = offset)
emit(id, score_entry, title, "DESC_SCORE_TARGET",
list(artifact = artifact_of(model, family_name, link, interval_kind, extra = extra)),
list("/predictions" = seq_tol(s$fit, "Target predictions from the frozen coefficients, in target row order.", s$tier),
"/lower" = seq_tol(s$lo, "Lower limit from the frozen covariance.", s$tier),
"/upper" = seq_tol(s$hi, "Upper limit from the frozen covariance.", s$tier)),
c(notes, "The artifact carries R's coefficients and covariance; target row 4 (0-based) has a missing x1 and row 2 is excluded, so neither is scored (line 94-97)."),
kind = "row_alignment", excluded = target_excluded)
}
score_case("STAT-DESC-SCORE-001", "Score a target with a frozen OLS artifact (mean)", ols, "ols", "identity")
score_case("STAT-DESC-SCORE-002", "Score a target with a frozen OLS artifact (individual)", ols, "ols", "identity", "individual")
score_case("STAT-DESC-SCORE-003", "Score a target with a frozen WLS artifact", wls, "wls", "identity",
extra = list(weight_column = "w"), notes = "The target needs no weight column to be scored.")
score_case("STAT-DESC-SCORE-004", "Score a target with a frozen logistic artifact", logit, "logistic", "logit",
extra = list(event_value = "yes", reference_value = "no"))
score_case("STAT-DESC-SCORE-005", "Score a target with a frozen Poisson artifact and exposure", pois, "poisson", "log",
extra = list(exposure_column = "expo"), offset = log(t$expo[scored]),
notes = "Predictions are expected counts at the target's entered exposure.")
unseen <- read_dataset("DESC_SCORE_TARGET")
unseen$grp[3] <- "D"
export_dataset(unseen, "DESC_SCORE_TARGET_UNSEEN", "DESC_SCORE_TARGET with an unseen level D in row 3 (1-based)",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(x1 = "Float64", grp = "String", expo = "Float64"))
emit("STAT-DESC-SCORE-006", score_entry, "An unseen categorical level is rejected", "DESC_SCORE_TARGET_UNSEEN",
list(artifact = artifact_of(ols, "ols", "identity")), list(),
c("Manual line 94-95: an unknown categorical level in a scoreable row is rejected explicitly."),
kind = "invalid_input", error = list(error_class = "ValueError", message_contains = "D"))
no_x <- read_dataset("DESC_SCORE_TARGET")[c("grp", "expo")]
export_dataset(no_x, "DESC_SCORE_TARGET_NO_X1", "DESC_SCORE_TARGET without the x1 column",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(grp = "String", expo = "Float64"))
emit("STAT-DESC-SCORE-007", score_entry, "A target without a fitted predictor is refused", "DESC_SCORE_TARGET_NO_X1",
list(artifact = artifact_of(ols, "ols", "identity")), list(),
c("Manual line 90-91: the target needs every fitted predictor."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "x1"))
# ---- the replayable score-column operation ----------------------------------
s <- score_outputs(ols, "ols", "mean")
positions <- seq_len(nt)
op_out <- list()
for (i in positions) {
k <- match(i, scored)
for (column in c("pred", "lo", "hi")) {
path <- paste0("/", column, "/", i - 1L)
if (is.na(k)) {
op_out[[path]] <- absent("Rows outside the retained population or unscoreable stay null (line 96-99).")
} else {
value <- switch(column, pred = s$fit[k], lo = s$lo[k], hi = s$hi[k])
op_out[[path]] <- tol(value, "Score column value from the frozen artifact.")
}
}
}
emit("STAT-DESC-SCOREOP-001", op_entry, "Score columns from the frozen OLS artifact", "DESC_SCORE_TARGET",
list(artifact = artifact_of(ols, "ols", "identity"), names = list("pred", "lo", "hi")), op_out,
c("Three columns: prediction, lower and upper; the excluded row 2 and the row with a missing x1 stay null."),
kind = "row_alignment", excluded = target_excluded)
emit("STAT-DESC-SCOREOP-002", op_entry, "Repeated score column names are refused", "DESC_SCORE_TARGET",
list(artifact = artifact_of(ols, "ols", "identity"), names = list("pred", "pred")), list(),
c("Score names must be distinct (ModelScoringScoreParameters)."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "must be distinct"))
generators/r/descriptives_multiple_response.R
source("common.R", local = TRUE)
# Independent references for Multiple response frequencies and crosstab (F134, #103): the
# case base (rows with at least one counted response), occurrences versus distinct cases per
# category, and the crosstab on the cases and responses bases. All counts are built in base R
# from a long table of (row, category) pairs; nothing is read from the engine.
raw <- data.frame(
s1 = as.integer(c(1, 0, 1, NA, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0)),
s2 = as.integer(c(0, 0, 1, NA, 1, 1, 0, 0, 1, 1, NA, 0, 1, 1)),
s3 = as.integer(c(1, 0, 0, NA, 1, 0, 0, 1, 0, 1, 0, 0, 1, NA)),
c1 = c("A", "A", "B", "C", NA, "B", "C", "A", "B", "A", "C", "B", "A", "A"),
c2 = c("B", "A", NA, "A", NA, "B", "C", "C", "A", "B", "B", "C", "A", NA),
region = c("North", "North", "South", "South", "North", NA, "South", "North", "South",
"North", "South", "North", "South", "North"),
stringsAsFactors = FALSE
)
export_dataset(
raw, "DESC_MR_01", "Constructed by descriptives_multiple_response.R",
"R base table() on a long (row, category) layout",
"Constructed test data; no third-party dataset license.",
c(s1 = "Int64", s2 = "Int64", s3 = "Int64", c1 = "String", c2 = "String", region = "String")
)
d <- read_dataset("DESC_MR_01")
run <- generator_run(c("stats", "jsonlite"))
n_rows <- nrow(d)
tol <- function(value, label) {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste(label, "from the re-read DESC_MR_01 CSV."))
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
absent <- function(label) list(comparison = "absent", rationale = label)
manual <- "docs/manual/chapters/80-column-meaning-and-populations.tex"
features <- list(name = "features", kind = "dichotomy", columns = list("s1", "s2", "s3"),
counted_value = 1L, label = "Features used")
choices <- list(name = "choices", kind = "category", columns = list("c1", "c2"),
counted_value = NULL, label = "Suppliers named")
emit <- function(id, title, kind, entry_point, menu_item, window, configuration, outputs, notes) {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "descriptives",
entry_point = entry_point, menu_items = list(menu_item), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/DESC_MR_01.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"),
sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R base table() and aggregate() on a long (row, category) layout",
citation = "R base::table documentation; manual chapter 80 multiple-response definitions",
generator = list(path = "generators/r/descriptives_multiple_response.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.")
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(paste0("REQ-STAT-DESC-", window, "-001")),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
))
}
# Long layouts: one record per occurrence (row, category).
dich_long <- do.call(rbind, lapply(c("s1", "s2", "s3"), function(name) {
rows <- which(!is.na(d[[name]]) & d[[name]] == 1L)
if (length(rows)) data.frame(row = rows, category = name, stringsAsFactors = FALSE) else NULL
}))
cat_long <- do.call(rbind, lapply(c("c1", "c2"), function(name) {
rows <- which(!is.na(d[[name]]))
data.frame(row = rows, category = d[[name]][rows], stringsAsFactors = FALSE)
}))
all_missing <- function(columns) sum(apply(is.na(d[, columns]), 1, all))
frequencies <- function(long, order) {
cases <- length(unique(long$row))
occurrences <- table(factor(long$category, levels = order))
distinct <- table(factor(unique(long[, c("row", "category")])$category, levels = order))
list(cases = cases, total = nrow(long), occurrences = occurrences, distinct = distinct)
}
# --- Multiple response frequencies: a dichotomy set and a category set ---------------------------
f1 <- frequencies(dich_long, c("s1", "s2", "s3"))
stopifnot(f1$cases == 11, f1$total == 19)
o1 <- list(
"/rows_analysed" = exact(as.integer(n_rows), "14 rows"),
"/valid_cases" = exact(as.integer(f1$cases), "rows with at least one 1 among s1-s3"),
"/cases_without_response" = exact(as.integer(n_rows - f1$cases), "rows with no 1 (all 0 or all missing)"),
"/all_missing_rows" = exact(as.integer(all_missing(c("s1", "s2", "s3"))), "rows missing on every column of the set"),
"/total_responses" = exact(as.integer(f1$total), "all 1s"),
"/categories/3" = absent("three dichotomies")
)
for (k in 1:3) {
name <- c("s1", "s2", "s3")[k]
base <- sprintf("/categories/%d", k - 1)
o1[[paste0(base, "/category")]] <- exact(name, "dichotomies in column order")
o1[[paste0(base, "/responses")]] <- exact(as.integer(f1$occurrences[[name]]), paste("1s in", name))
o1[[paste0(base, "/cases")]] <- exact(as.integer(f1$distinct[[name]]), paste("cases selecting", name))
o1[[paste0(base, "/percent_of_responses")]] <- tol(100 * f1$occurrences[[name]] / f1$total, paste("percent of all responses for", name))
o1[[paste0(base, "/percent_of_cases")]] <- tol(100 * f1$distinct[[name]] / f1$cases, paste("percent of valid cases selecting", name))
}
emit("STAT-DESC-MRFREQ-001", "Multiple-dichotomy frequencies: case base, responses and percents",
"nominal", "tabelz.analysis.multiple_response:compute_mr_frequencies",
"multipleResponseFrequenciesMenuItem", "MULTIPLE-RESPONSE-FREQUENCIES",
list(set_name = "features", response_set = features),
o1,
c("R: table() of the (row, column) pairs where the column holds the counted value 1; a case is a row with at least one such pair; percent of responses divides by all pairs, percent of cases by the cases.",
paste0(manual, ":111-117: the case base consists of rows with at least one counted response; a row answering No to every dichotomy is outside that base; rows with no counted response and rows missing all responses are counted separately.")))
f2 <- frequencies(cat_long, c("A", "B", "C"))
stopifnot(f2$cases == 13, f2$total == 24)
o2 <- list(
"/valid_cases" = exact(as.integer(f2$cases), "rows with at least one code"),
"/cases_without_response" = exact(as.integer(n_rows - f2$cases), "the row with both codes missing"),
"/all_missing_rows" = exact(as.integer(all_missing(c("c1", "c2"))), "rows missing both codes"),
"/total_responses" = exact(as.integer(f2$total), "all codes, repeats included")
)
for (code in c("A", "B", "C")) {
base <- sprintf("/categories/%d", match(code, c("A", "B", "C")) - 1)
o2[[paste0(base, "/category")]] <- exact(code, "pooled codes in value order")
o2[[paste0(base, "/responses")]] <- exact(as.integer(f2$occurrences[[code]]), paste("occurrences of", code))
o2[[paste0(base, "/cases")]] <- exact(as.integer(f2$distinct[[code]]), paste("distinct cases naming", code))
o2[[paste0(base, "/percent_of_responses")]] <- tol(100 * f2$occurrences[[code]] / f2$total, paste("percent of responses for", code))
o2[[paste0(base, "/percent_of_cases")]] <- tol(100 * f2$distinct[[code]] / f2$cases, paste("percent of cases naming", code))
}
emit("STAT-DESC-MRFREQ-002", "Multiple-category frequencies: repeated codes count as responses once per case",
"axis_coverage", "tabelz.analysis.multiple_response:compute_mr_frequencies",
"multipleResponseFrequenciesMenuItem", "MULTIPLE-RESPONSE-FREQUENCIES",
list(set_name = "choices", response_set = choices),
o2,
c("R: table() of every non-missing code across c1 and c2 (occurrences) and of the distinct (row, code) pairs (cases).",
paste0(manual, ":119-158: the worked example distinguishes mentions from technicians; case percentages can total more than 100.")))
# --- Multiple response crosstab: set x column on the cases base ------------------------------------
by_region <- merge(dich_long, data.frame(row = seq_len(n_rows), region = d$region), by = "row")
by_region <- by_region[!is.na(by_region$region), ]
cases3 <- length(unique(by_region$row))
cell_cases <- table(factor(by_region$category, levels = c("s1", "s2", "s3")),
factor(by_region$region, levels = c("North", "South")))
row_cases <- vapply(c("s1", "s2", "s3"), function(k) length(unique(by_region$row[by_region$category == k])), 0L)
col_cases <- vapply(c("North", "South"), function(g) length(unique(by_region$row[by_region$region == g])), 0L)
stopifnot(cases3 == 10)
o3 <- list(
"/rows_analysed" = exact(as.integer(n_rows), "14 rows"),
"/valid_cases" = exact(as.integer(cases3), "cases with a response and a region"),
"/left_out_cases" = exact(as.integer(n_rows - cases3), "rows without a response or without a region"),
"/base_total" = exact(as.integer(cases3), "the cases base"),
"/row_totals/s1" = exact(as.integer(row_cases[["s1"]]), "cases selecting s1 with a region"),
"/column_totals/North" = exact(as.integer(col_cases[["North"]]), "cases in North with a response"),
"/cells/6" = absent("3 x 2 cells")
)
for (r in 1:3) for (c in 1:2) {
k <- c("s1", "s2", "s3")[r]; g <- c("North", "South")[c]
base <- sprintf("/cells/%d", (r - 1) * 2 + (c - 1))
count <- cell_cases[k, g]
o3[[paste0(base, "/row")]] <- exact(k, "row category")
o3[[paste0(base, "/column")]] <- exact(g, "column category")
o3[[paste0(base, "/count")]] <- exact(as.integer(count), sprintf("cases %s x %s", k, g))
o3[[paste0(base, "/row_percent")]] <- tol(100 * count / row_cases[[k]], "of the row's cases")
o3[[paste0(base, "/column_percent")]] <- tol(100 * count / col_cases[[g]], "of the column's cases")
o3[[paste0(base, "/total_percent")]] <- tol(100 * count / cases3, "of all cases")
}
emit("STAT-DESC-MRCROSS-001", "Multiple-response crosstab against a column on the cases base",
"nominal", "tabelz.analysis.multiple_response:compute_mr_crosstab",
"multipleResponseCrosstabMenuItem", "MULTIPLE-RESPONSE-CROSSTAB",
list(set_name = "features", response_set = features, by_column = "region",
base = "cases"),
o3,
c("R: (row, dichotomy) pairs joined to the row's region, rows without a region dropped; each cell counts distinct cases; row and column totals count distinct cases; percents divide by those totals.",
paste0(manual, ":160-165: on a case base a case counts once for each category pair; denominators match the selected base.")))
# --- set x set on the responses base ----------------------------------------------------------------
both <- merge(dich_long, cat_long, by = "row", suffixes = c(".left", ".right"))
pairs <- table(factor(both$category.left, levels = c("s1", "s2", "s3")),
factor(both$category.right, levels = c("A", "B", "C")))
cases4 <- length(unique(both$row))
distinct_pairs <- table(factor(unique(both[, c("row", "category.left", "category.right")])$category.left,
levels = c("s1", "s2", "s3")),
factor(unique(both[, c("row", "category.left", "category.right")])$category.right,
levels = c("A", "B", "C")))
stopifnot(sum(pairs) > sum(distinct_pairs)) # a repeated code multiplies the occurrence pairs
o4 <- list(
"/valid_cases" = exact(as.integer(cases4), "cases with a response in both sets"),
"/base_total" = exact(as.integer(sum(pairs)), "all occurrence pairs"),
"/row_totals/s2" = exact(as.integer(sum(pairs["s2", ])), "occurrence pairs in the s2 row"),
"/column_totals/A" = exact(as.integer(sum(pairs[, "A"])), "occurrence pairs in the A column")
)
for (r in 1:3) for (c in 1:3) {
k <- c("s1", "s2", "s3")[r]; code <- c("A", "B", "C")[c]
base <- sprintf("/cells/%d", (r - 1) * 3 + (c - 1))
o4[[paste0(base, "/row")]] <- exact(k, "row category")
o4[[paste0(base, "/column")]] <- exact(code, "column category")
o4[[paste0(base, "/count")]] <- exact(as.integer(pairs[k, code]), "occurrence pairs on the responses base")
o4[[paste0(base, "/responses")]] <- exact(as.integer(pairs[k, code]), "occurrence pairs")
o4[[paste0(base, "/cases")]] <- exact(as.integer(distinct_pairs[k, code]), "distinct cases giving the pair")
o4[[paste0(base, "/row_percent")]] <- tol(100 * pairs[k, code] / sum(pairs[k, ]), "of the row's occurrence pairs")
o4[[paste0(base, "/total_percent")]] <- tol(100 * pairs[k, code] / sum(pairs), "of all occurrence pairs")
}
emit("STAT-DESC-MRCROSS-002", "Multiple-response crosstab of two sets on the responses base",
"axis_coverage", "tabelz.analysis.multiple_response:compute_mr_crosstab",
"multipleResponseCrosstabMenuItem", "MULTIPLE-RESPONSE-CROSSTAB",
list(set_name = "features", response_set = features, by_set_name = "choices",
by_set = choices, base = "responses"),
o4,
c("R: the (row, dichotomy) pairs joined to the (row, code) pairs of the same row; a cell counts the joined records (occurrence pairs, repeated codes multiplying them) and, separately, the distinct cases; totals sum the cells.",
paste0(manual, ":160-165: on a response base repetitions multiply the paired response count.")))
cat("multiple response references generated\n")
generators/r/descriptives_probability.R
source("common.R", local = TRUE)
# References for the Probability distributions window (manual chapter 61):
# tabelz.analysis.distribution_utilities:compute_probability_calculation
# tabelz.analysis.distribution_sampling:generate_random_data
# operation:tabelz.random_table
# Parameterizations (chapter 61 lines 15-26): Student t has location 0 and
# scale 1; exponential takes its scale theta (rate 1 / theta); Weibull is the
# two-parameter (shape k, scale lambda) family. Tails (lines 48-53): P(X <= x)
# is the CDF and P(X > x) the survival function, each with a base-10 log.
# Quantiles (lines 54-60): lower Q(p) = inf{x : F(x) >= p}; upper = smallest x
# with P(X > x) <= p; discrete ones are the smallest integer satisfying it.
run <- generator_run(c("stats", "jsonlite"))
family <- "descriptives"
generator <- "generators/r/descriptives_probability.R"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Closed-form R distribution function; owner 1e-9 tier.", why)
)
tail_p <- function(value, why) list(
comparison = "tail_probability", value = dec(unname(value)), relative_tolerance = "1e-9",
rationale = paste("R tail probability; owner 1e-9 tier.", why)
)
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
contains <- function(values, why) list(comparison = "contains", values = as.list(values), rationale = why)
emit <- function(id, entry, menu, title, config, outputs, notes, kind = "nominal",
error = NULL, deferred = list(), repeat_n = 1L, oracle_class = "independent_implementation") {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = deferred, generator_run = run
))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = family, entry_point = entry, menu_items = list(menu), title = title,
case_kind = kind, dataset = NULL, excluded_rows = list(), configuration = config,
"repeat" = repeat_n,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = oracle_class, source = "R stats distribution functions",
citation = "R stats::Normal, TDist, Chisquare, FDist, Binomial, Poisson, Exponential and Weibull documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "Manual chapter 61 lines 13-60 define the parameters, tails and quantiles."))
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL
))
}
calc <- "tabelz.analysis.distribution_utilities:compute_probability_calculation"
menu <- "probabilityDistributionsMenuItem"
# Each family: d, p, q with R's own tails and log tails.
families <- list(
normal = list(spec = list(family = "normal", mean = 10, standard_deviation = 2),
d = function(x) dnorm(x, 10, 2),
p = function(x, lower = TRUE, log = FALSE) pnorm(x, 10, 2, lower.tail = lower, log.p = log),
q = function(p, lower = TRUE) qnorm(p, 10, 2, lower.tail = lower),
mean = 10, var = 4, lower = NULL, upper = NULL,
x = c(7, 10, 13.5, 20), probs = c(0.025, 0.5, 0.95), interval = c(8, 12)),
t = list(spec = list(family = "t", degrees_of_freedom = 4.5),
d = function(x) dt(x, 4.5),
p = function(x, lower = TRUE, log = FALSE) pt(x, 4.5, lower.tail = lower, log.p = log),
q = function(p, lower = TRUE) qt(p, 4.5, lower.tail = lower),
mean = 0, var = 4.5 / (4.5 - 2), lower = NULL, upper = NULL,
x = c(-3, 0, 1.25, 6), probs = c(0.01, 0.5, 0.975), interval = c(-1, 2)),
chi_square = list(spec = list(family = "chi_square", degrees_of_freedom = 7),
d = function(x) dchisq(x, 7),
p = function(x, lower = TRUE, log = FALSE) pchisq(x, 7, lower.tail = lower, log.p = log),
q = function(p, lower = TRUE) qchisq(p, 7, lower.tail = lower),
mean = 7, var = 14, lower = 0, upper = NULL,
x = c(0.5, 7, 14, 30), probs = c(0.05, 0.5, 0.95), interval = c(2, 12)),
f = list(spec = list(family = "f", numerator_df = 3, denominator_df = 12),
d = function(x) df(x, 3, 12),
p = function(x, lower = TRUE, log = FALSE) pf(x, 3, 12, lower.tail = lower, log.p = log),
q = function(p, lower = TRUE) qf(p, 3, 12, lower.tail = lower),
mean = 12 / 10, var = 2 * 12^2 * (3 + 12 - 2) / (3 * (12 - 2)^2 * (12 - 4)), lower = 0, upper = NULL,
x = c(0.25, 1, 3.49, 9), probs = c(0.1, 0.5, 0.99), interval = c(0.5, 4)),
exponential = list(spec = list(family = "exponential", scale = 2.5),
d = function(x) dexp(x, 1 / 2.5),
p = function(x, lower = TRUE, log = FALSE) pexp(x, 1 / 2.5, lower.tail = lower, log.p = log),
q = function(p, lower = TRUE) qexp(p, 1 / 2.5, lower.tail = lower),
mean = 2.5, var = 6.25, lower = 0, upper = NULL,
x = c(0.1, 1, 2.5, 12), probs = c(0.05, 0.5, 0.9), interval = c(1, 4)),
weibull = list(spec = list(family = "weibull", shape = 1.7, scale = 3),
d = function(x) dweibull(x, 1.7, 3),
p = function(x, lower = TRUE, log = FALSE) pweibull(x, 1.7, 3, lower.tail = lower, log.p = log),
q = function(p, lower = TRUE) qweibull(p, 1.7, 3, lower.tail = lower),
mean = 3 * gamma(1 + 1 / 1.7), var = 9 * (gamma(1 + 2 / 1.7) - gamma(1 + 1 / 1.7)^2),
lower = 0, upper = NULL,
x = c(0.2, 1.5, 3, 9), probs = c(0.1, 0.632, 0.99), interval = c(1, 5))
)
number <- 0L
for (name in names(families)) {
f <- families[[name]]
number <- number + 1L
out <- list()
for (i in seq_along(f$x)) {
x <- f$x[[i]]; node <- paste0("/values/", i - 1L)
out[[paste0(node, "/density")]] <- tol(f$d(x), "Density at x.")
out[[paste0(node, "/cdf")]] <- tail_p(f$p(x), "P(X <= x).")
out[[paste0(node, "/sf")]] <- tail_p(f$p(x, lower = FALSE), "P(X > x) from the survival function.")
out[[paste0(node, "/log10_cdf")]] <- tol(f$p(x, log = TRUE) / log(10), "log10 P(X <= x) from the log CDF.")
out[[paste0(node, "/log10_sf")]] <- tol(f$p(x, lower = FALSE, log = TRUE) / log(10), "log10 P(X > x) from the log survival function.")
out[[paste0(node, "/mass")]] <- absent("A continuous family reports a density, not a mass.")
}
for (i in seq_along(f$probs)) {
p <- f$probs[[i]]; node <- paste0("/quantiles/", i - 1L)
out[[paste0(node, "/lower_quantile")]] <- tol(f$q(p), "Lower-tail quantile Q(p).")
out[[paste0(node, "/upper_quantile")]] <- tol(f$q(p, lower = FALSE), "Upper-tail value with P(X > x) = p.")
}
prob <- f$p(f$interval[2]) - f$p(f$interval[1])
out[["/interval/probability"]] <- tol(prob, "P(a <= X <= b) = F(b) - F(a).")
out[["/interval/complement"]] <- tol(f$p(f$interval[1]) + f$p(f$interval[2], lower = FALSE),
"Complement F(a) + S(b), evaluated with both tails.")
out[["/distribution/mean"]] <- tol(f$mean, "Theoretical mean.")
out[["/distribution/variance"]] <- tol(f$var, "Theoretical variance.")
out[["/distribution/kind"]] <- exact("continuous", "Family kind.")
if (!is.null(f$lower)) out[["/distribution/support_lower"]] <- tol(f$lower, "Support starts at zero.")
out[["/distribution/support_upper"]] <- absent("Unbounded above.")
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu,
paste("Probability calculator:", name),
list(distribution = f$spec, x_values = as.list(f$x), probabilities = as.list(f$probs),
interval = list(lower = f$interval[1], upper = f$interval[2])),
out, c(sprintf("Family %s evaluated with R's d/p/q functions and log.p tails.", name)))
}
# ---- binomial: the manual's two interval examples ---------------------------
binomial <- list(family = "binomial", trials = 10L, probability = 0.5)
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu,
"Binomial(10, 1/2): P(2 < X <= 5)",
list(distribution = binomial, interval = list(lower = 2, upper = 5, lower_inclusive = FALSE)),
list("/interval/probability" = tol(sum(dbinom(3:5, 10, 0.5)), "Sum of P(X = 3..5) = 582/1024."),
"/interval/integer_lower" = exact(3L, "The excluded endpoint 2 leaves integers from 3."),
"/interval/integer_upper" = exact(5L, "The included endpoint 5.")),
c("Manual chapter 61 line 65: for Binomial(10, 1/2) the probability P(2 < X <= 5) is 582/1024.",
sprintf("R: sum(dbinom(3:5, 10, 0.5)) * 1024 = %s.", format(sum(dbinom(3:5, 10, 0.5)) * 1024))),
kind = "axis_coverage")
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu,
"Binomial(10, 1/2): P(2 <= X < 5)",
list(distribution = binomial, interval = list(lower = 2, upper = 5, upper_inclusive = FALSE)),
list("/interval/probability" = tol(sum(dbinom(2:4, 10, 0.5)), "Sum of P(X = 2..4) = 375/1024."),
"/interval/integer_lower" = exact(2L, "The included endpoint 2."),
"/interval/integer_upper" = exact(4L, "The excluded endpoint 5 leaves integers to 4.")),
c("Manual chapter 61 line 66: for Binomial(10, 1/2) the probability P(2 <= X < 5) is 375/1024.",
sprintf("R: sum(dbinom(2:4, 10, 0.5)) * 1024 = %s.", format(sum(dbinom(2:4, 10, 0.5)) * 1024))),
kind = "axis_coverage")
# ---- Poisson values, including a non-integer x, and discrete quantiles ------
lambda <- 3.7
xs <- c(0, 2, 2.5, 9)
out <- list()
for (i in seq_along(xs)) {
x <- xs[[i]]; node <- paste0("/values/", i - 1L)
out[[paste0(node, "/mass")]] <- tol(if (x == round(x)) dpois(x, lambda) else 0,
"P(X = x); a non-integer x has zero probability (line 47).")
out[[paste0(node, "/cdf")]] <- tail_p(ppois(floor(x), lambda), "P(X <= x) = P(X <= floor(x)) (line 53).")
out[[paste0(node, "/sf")]] <- tail_p(ppois(floor(x), lambda, lower.tail = FALSE), "Survival P(X > x).")
out[[paste0(node, "/density")]] <- absent("A discrete family reports a mass, not a density.")
out[[paste0(node, "/in_support")]] <- exact(x == round(x), "Only integers are support points.")
}
probs <- c(0.05, 0.5, 0.95)
for (i in seq_along(probs)) {
p <- probs[[i]]; node <- paste0("/quantiles/", i - 1L)
low <- qpois(p, lambda)
high <- 0L; while (ppois(high, lambda, lower.tail = FALSE) > p) high <- high + 1L
out[[paste0(node, "/lower_quantile")]] <- tol(low, "Smallest k with P(X <= k) >= p (R qpois).")
out[[paste0(node, "/upper_quantile")]] <- tol(high, "Smallest k with P(X > k) <= p, by search.")
out[[paste0(node, "/achieved_cdf")]] <- tail_p(ppois(low, lambda), "P(X <= lower quantile).")
out[[paste0(node, "/achieved_sf")]] <- tail_p(ppois(high, lambda, lower.tail = FALSE), "P(X > upper quantile).")
}
out[["/distribution/mean"]] <- tol(lambda, "Poisson mean.")
out[["/distribution/variance"]] <- tol(lambda, "Poisson variance equals the mean.")
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "Poisson(3.7) values and discrete quantiles",
list(distribution = list(family = "poisson", mean = lambda), x_values = as.list(xs),
probabilities = as.list(probs)),
out, c("R qpois gives the smallest k with F(k) >= p; the upper value is found by increasing k until S(k) <= p."))
# ---- boundary: a probability equal to a cumulative step ---------------------
step <- pbinom(1, 4, 0.5)
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "Quantile at a cumulative step returns that step",
list(distribution = list(family = "binomial", trials = 4L, probability = 0.5), probabilities = list(step)),
list("/quantiles/0/lower_quantile" = tol(1, "F(1) = 5/16 exactly, so Q(5/16) = 1 (line 56-57)."),
"/quantiles/0/achieved_cdf" = tail_p(step, "The achieved CDF equals p.")),
c(sprintf("pbinom(1, 4, 0.5) = %s = 5/16, a binary-exact value.", format(step, digits = 17))),
kind = "boundary")
# ---- hard numeric: an upper tail that underflows ----------------------------
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "Upper tail below the double range keeps its logarithm",
list(distribution = list(family = "normal", mean = 0, standard_deviation = 1), x_values = list(40)),
list("/values/0/sf" = tail_p(0, "P(Z > 40) is about 3.7e-350, below the smallest double."),
"/values/0/log10_sf" = tol(pnorm(40, lower.tail = FALSE, log.p = TRUE) / log(10),
"log10 P(Z > 40) from R's log survival function (line 51-52)."),
"/values/0/cdf" = tail_p(1, "P(Z <= 40) rounds to 1.")),
c(sprintf("R: pnorm(40, lower.tail = FALSE, log.p = TRUE) / log(10) = %s.",
sprintf("%.17g", pnorm(40, lower.tail = FALSE, log.p = TRUE) / log(10)))),
kind = "hard_numeric")
# ---- boundary: undefined moments of Student t -------------------------------
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "Student t with one degree of freedom has no mean",
list(distribution = list(family = "t", degrees_of_freedom = 1), x_values = list(1)),
list("/distribution/mean" = absent("The mean of t with df <= 1 is undefined (line 32)."),
"/distribution/variance" = absent("The variance of t with df <= 2 is undefined (line 32)."),
"/values/0/cdf" = tail_p(pt(1, 1), "P(T <= 1) = 3/4 for df = 1."),
"/warnings" = contains("The mean is undefined", "Undefined moments are reported, not numbers.")),
c("Student t with df = 1 is the Cauchy distribution; pt(1, 1) = 0.75."), kind = "boundary")
# ---- boundary: a degenerate binomial ----------------------------------------
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "Binomial with p = 0 is degenerate",
list(distribution = list(family = "binomial", trials = 5L, probability = 0), x_values = list(0, 1)),
list("/values/0/mass" = tol(dbinom(0, 5, 0), "All mass at zero."),
"/values/1/mass" = tol(dbinom(1, 5, 0), "No mass above zero."),
"/warnings" = contains("degenerate", "A p of 0 or 1 is disclosed as degenerate.")),
c("dbinom(0, 5, 0) = 1 and dbinom(1, 5, 0) = 0."), kind = "boundary")
# ---- invalid inputs ---------------------------------------------------------
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "A quantile probability of 1 is refused",
list(distribution = list(family = "normal"), probabilities = list(1)), list(),
c("Line 54: each probability must lie strictly between 0 and 1."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "strictly between 0 and 1"))
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "A negative standard deviation is refused",
list(distribution = list(family = "normal", standard_deviation = -1), x_values = list(0)), list(),
c("Line 18: sigma must be positive."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "The standard deviation must be greater than 0"))
number <- number + 1L
emit(sprintf("STAT-DESC-PROBCALC-%03d", number), calc, menu, "An empty request is refused",
list(distribution = list(family = "poisson", mean = 2)), list(),
c("Method details line 138: an empty request fails."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "Enter at least one x value"))
# ---- seeded random data -----------------------------------------------------
# The draws come from PCG64 (chapter 61 lines 106-113), which R cannot
# reproduce independently; their distribution is a V27 simulation property.
# These cases check what follows analytically from the request and the family.
gen <- "tabelz.analysis.distribution_sampling:generate_random_data"
normal_request <- list(distribution = list(family = "normal", mean = 5, standard_deviation = 2),
rows = 50L, column_names = list("a", "b"), seed = 12345L)
emit("STAT-DESC-RANDOM-001", gen, menu, "Seeded normal table: shape, names and theoretical moments",
normal_request,
list("/summary/rows" = exact(50L, "Requested rows."),
"/summary/column_names" = list(comparison = "sequence", values = list("a", "b"), rationale = "Requested names in order."),
"/summary/dtype" = exact("float64", "Continuous columns are floating point (line 103)."),
"/summary/algorithm" = exact("pcg64-inverse-cdf-v1", "The documented algorithm (line 106)."),
"/summary/seed" = exact(12345L, "The requested seed is recorded."),
"/summary/distribution/mean" = tol(5, "Theoretical mean."),
"/summary/distribution/standard_deviation" = tol(2, "Theoretical standard deviation."),
"/summary/columns/0/count" = exact(50L, "Every row is drawn."),
"/summary/columns/1/count" = exact(50L, "Every row is drawn."),
"/frame/50" = absent("Exactly 50 rows.")),
c("Draws are covered by the V27 simulation studies; repeat 2 checks that the same seed recreates the same table (line 113)."),
kind = "determinism", repeat_n = 2L, oracle_class = "analytic")
emit("STAT-DESC-RANDOM-002", gen, menu, "Seeded Poisson table uses integer columns",
list(distribution = list(family = "poisson", mean = 3.5), rows = 20L, column_names = list("count"), seed = 7L),
list("/summary/dtype" = exact("int64", "Poisson columns are integers (line 103-104)."),
"/summary/distribution/mean" = tol(3.5, "Theoretical mean."),
"/summary/distribution/standard_deviation" = tol(sqrt(3.5), "Theoretical standard deviation sqrt(lambda)."),
"/summary/columns/0/count" = exact(20L, "Every row is drawn."),
"/frame/20" = absent("Exactly 20 rows.")),
c("A discrete family produces int64 values (line 103-104)."), oracle_class = "analytic")
emit("STAT-DESC-RANDOM-003", gen, menu, "One row is the smallest table",
list(distribution = list(family = "exponential", scale = 2), rows = 1L, column_names = list("x"), seed = 0L),
list("/summary/rows" = exact(1L, "A single row."), "/frame/1" = absent("Exactly one row."),
"/summary/distribution/mean" = tol(2, "The exponential mean is its scale.")),
c("rows has a minimum of 1."), kind = "boundary", oracle_class = "analytic")
emit("STAT-DESC-RANDOM-004", gen, menu, "Column names that differ only in case are refused",
list(distribution = list(family = "normal"), rows = 5L, column_names = list("Value", "value"), seed = 1L),
list(), c("Line 94: names must be distinct ignoring case."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "is repeated"), oracle_class = "analytic")
emit("STAT-DESC-RANDOM-005", gen, menu, "More than 1,000,000 cells is refused",
list(distribution = list(family = "normal"), rows = 500001L, column_names = list("a", "b"), seed = 1L),
list(), c("Line 93: at most 1,000,000 cells; 500,001 x 2 = 1,000,002."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "the maximum is 1,000,000"), oracle_class = "analytic")
# ---- the recorded random-table operation -------------------------------------
op <- "operation:tabelz.random_table"
emit("STAT-DESC-RANDTABLE-001", op, menu, "Replaying the recorded operation recreates the table",
c(normal_request, list(recorded = TRUE)),
list("/frame/50" = absent("Exactly the 50 requested rows."),
"/row_ids_unique" = exact(TRUE, "Generated row identities are stable and distinct (line 103)."),
"/receipt/row_identity" = exact("generated", "The table's rows have generated identities."),
"/receipt/seed" = exact(12345L, "The recorded seed (line 115-116)."),
"/receipt/algorithm" = exact("pcg64-inverse-cdf-v1", "The recorded algorithm.")),
c("Lines 115-121: creating the tab records one operation; replay regenerates the table and compares its fingerprint."),
kind = "determinism", repeat_n = 2L, oracle_class = "analytic")
emit("STAT-DESC-RANDTABLE-002", op, menu, "A different recorded fingerprint is reported, not accepted",
c(normal_request, list(recorded = FALSE)), list(),
c("Lines 119-121: replay reports different values instead of accepting new numbers."),
kind = "invalid_input", oracle_class = "analytic",
error = list(error_class = "ValueError", message_contains = "produced different values than were recorded"))
generators/r/descriptives_profiler.R
source("common.R", local = TRUE)
# References for tabelz.executor.stored_model_profiler (manual chapter 54).
# The retained artifacts are built from R's own fits of DESC_SCORE_FIT, which
# descriptives_model_scoring.R writes; this script only reads that dataset.
run <- generator_run(c("stats", "jsonlite"))
family <- "descriptives"
generator <- "generators/r/descriptives_profiler.R"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9",
rationale = paste("Closed-form evaluation of a frozen artifact; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
d <- read_dataset("DESC_SCORE_FIT")
d$grp <- factor(d$grp, levels = sort(unique(d$grp)))
coef_names <- c("Intercept", "x1", "grp = B (vs A)", "grp = C (vs A)")
ols <- lm(y ~ x1 + grp, data = d)
logit <- glm(I(yb == "yes") ~ x1 + grp, data = d, family = binomial(),
control = glm.control(epsilon = 1e-14, maxit = 100))
pois <- glm(cnt ~ x1 + grp + offset(log(expo)), data = d, family = poisson(),
control = glm.control(epsilon = 1e-14, maxit = 100))
artifact_of <- function(model, family_name, link, interval_kind = "mean", level = 0.95, extra = list()) {
v <- unname(vcov(model))
base <- list(
family = family_name, response = if (family_name == "logistic") "yb" else if (family_name == "poisson") "cnt" else "y",
predictors = list(list(name = "x1", kind = "numeric"),
list(name = "grp", kind = "categorical", levels = list("A", "B", "C"), reference_level = "A")),
feature_order = as.list(coef_names), coefficient_names = as.list(coef_names),
coefficients = lapply(unname(coef(model)), function(v) v),
covariance = lapply(seq_len(nrow(v)), function(i) as.list(v[i, ])),
link = link, interval_kind = interval_kind, confidence_level = level,
design_rank = ncol(v), fitted_row_ids = as.list(sprintf("fit-%02d", seq_len(nrow(d))))
)
if (family_name %in% c("ols", "wls")) {
base$residual_variance <- summary(model)$sigma^2
base$residual_df <- as.integer(df.residual(model))
}
c(base, extra)
}
# ---- Stored-model profiler and desirability (manual chapter 54) -------------
# Charts (lines 89-91): 101 equally spaced settings per numeric predictor and
# every permitted level, other predictors at their held values; mean-response
# limits t (OLS/WLS) or Wald through the inverse link (lines 68-74), always mean
# limits. Desirability (lines 80-87): linear ramps for maximize/minimize, a
# tent for target; D is the weighted geometric mean, zero if any d is zero.
# The search always evaluates the all-upper corner (lines 92-94), so when
# every goal increases monotonically in every numeric predictor and one level
# is best for all, the best setting found is that corner.
prof_entry <- "tabelz.executor.stored_model_profiler"
prof_menu <- "modelProfilerMenuItem"
emit_profiler <- function(id, title, config, outputs, notes, kind = "nominal", error = NULL, deferred = list()) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = deferred, generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family,
entry_point = prof_entry, menu_items = list(prof_menu), title = title, case_kind = kind,
dataset = NULL, excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats::lm/glm coefficients with hand-coded desirability",
citation = "R stats::lm, stats::glm and stats::qt/qnorm documentation; Derringer-Suich desirability as defined in manual chapter 54",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "Manual chapter 54 lines 60-99 define predictions, limits, desirability and the search."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-DESC-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
desire <- function(value, goal) {
low <- goal$low; high <- goal$high
if (goal$goal == "maximize") return(min(1, max(0, (value - low) / (high - low))))
if (goal$goal == "minimize") return(min(1, max(0, (high - value) / (high - low))))
target <- if (is.null(goal$target)) (low + high) / 2 else goal$target
if (value <= low || value >= high) return(0)
if (value <= target) (value - low) / (target - low) else (high - value) / (high - target)
}
profile_point <- function(model, family_name, x1, level, confidence, exposure = 1) {
row <- c(1, x1, as.numeric(level == "B"), as.numeric(level == "C"))
b <- coef(model); v <- vcov(model)
eta <- sum(row * b); se <- sqrt(max(0, as.numeric(t(row) %*% v %*% row)))
if (family_name %in% c("ols", "wls")) {
q <- qt(0.5 + confidence / 2, df.residual(model))
return(c(fit = eta, lo = eta - q * se, hi = eta + q * se))
}
z <- qnorm(0.5 + confidence / 2)
if (family_name == "logistic") return(c(fit = plogis(eta), lo = plogis(eta - z * se), hi = plogis(eta + z * se)))
c(fit = exp(eta) * exposure, lo = exp(eta - z * se + log(exposure)), hi = exp(eta + z * se + log(exposure)))
}
predictors_spec <- function(held_x = 5, held_level = "A", levels = c("A", "B", "C")) list(
list(name = "x1", kind = "numeric", min_val = 1, max_val = 9, current_value = held_x),
list(name = "grp", kind = "categorical", categories = as.list(levels), current_value = held_level))
chart_outputs <- function(chart_index, model, family_name, goal, confidence, exposure = 1, points = c(0L, 37L, 100L)) {
out <- list(); xs <- seq(1, 9, length.out = 101)
for (k in points) {
pt <- profile_point(model, family_name, xs[k + 1], "A", confidence, exposure)
node <- sprintf("/charts/%d/points/%d", chart_index, k)
out[[paste0(node, "/predictor_value")]] <- tol(xs[k + 1], "Equally spaced setting (101 points).")
out[[paste0(node, "/prediction")]] <- tol(pt[["fit"]], "Prediction at the setting, grp held at A.")
out[[paste0(node, "/lower")]] <- tol(pt[["lo"]], "Pointwise mean-response lower limit.")
out[[paste0(node, "/upper")]] <- tol(pt[["hi"]], "Pointwise mean-response upper limit.")
out[[paste0(node, "/desirability")]] <- tol(desire(pt[["fit"]], goal), "Desirability of the prediction.")
}
out
}
level_outputs <- function(chart_index, model, family_name, goal, confidence, exposure = 1) {
out <- list()
for (k in 1:3) {
level <- c("A", "B", "C")[k]
pt <- profile_point(model, family_name, 5, level, confidence, exposure)
node <- sprintf("/charts/%d/points/%d", chart_index, k - 1L)
out[[paste0(node, "/predictor_value")]] <- exact(level, "Every permitted level, in order.")
out[[paste0(node, "/prediction")]] <- tol(pt[["fit"]], "Prediction at the level, x1 held at 5.")
out[[paste0(node, "/desirability")]] <- tol(desire(pt[["fit"]], goal), "Desirability of the prediction.")
}
out
}
if (!(coef(ols)[["x1"]] > 0 && coef(ols)[["grpB"]] > 0 && coef(ols)[["grpB"]] > coef(ols)[["grpC"]])) stop("OLS monotone design assumption failed")
if (!(coef(logit)[["x1"]] > 0 && coef(logit)[["grpB"]] > 0 && coef(logit)[["grpB"]] > coef(logit)[["grpC"]])) stop("logistic monotone design assumption failed")
goal_y <- list(response_name = "y", goal = "maximize", weight = 2, low = 0, high = 20)
config1 <- list(models = list(list(artifact = artifact_of(ols, "ols", "identity"))),
predictors = predictors_spec(), goals = list(goal_y))
best <- profile_point(ols, "ols", 9, "B", 0.95)
out <- c(chart_outputs(0L, ols, "ols", goal_y, 0.95), level_outputs(1L, ols, "ols", goal_y, 0.95),
list("/optimum_settings/x1" = tol(9, "The all-upper corner is always evaluated and is best for an increasing goal."),
"/optimum_settings/grp" = exact("B", "B has the largest level effect."),
"/optimum_outcomes/0/prediction" = tol(best[["fit"]], "Prediction at the best setting."),
"/optimum_outcomes/0/lower" = tol(best[["lo"]], "Mean-response lower limit there."),
"/optimum_outcomes/0/upper" = tol(best[["hi"]], "Mean-response upper limit there."),
"/combined_desirability" = tol(desire(best[["fit"]], goal_y), "D equals d for one response."),
"/categorical_combinations" = exact(3L, "Three permitted levels.")))
emit_profiler("STAT-DESC-PROFILER-001", "OLS profile, desirability and the best corner", config1, out,
c("The high threshold 20 lies above every prediction, so d rises strictly with x1 and with the B effect."))
goal_b <- list(response_name = "yb", goal = "maximize", weight = 1, low = 0, high = 1)
config2 <- list(models = list(list(artifact = artifact_of(ols, "ols", "identity")),
list(artifact = artifact_of(logit, "logistic", "logit", extra = list(event_value = "yes", reference_value = "no")))),
predictors = predictors_spec(), goals = list(goal_y, goal_b))
bl <- profile_point(logit, "logistic", 9, "B", 0.95)
dy <- desire(best[["fit"]], goal_y); db <- desire(bl[["fit"]], goal_b)
out <- c(chart_outputs(1L, logit, "logistic", goal_b, 0.95), list(
"/optimum_settings/x1" = tol(9, "Both responses increase with x1."),
"/optimum_settings/grp" = exact("B", "B is best for both responses."),
"/combined_desirability" = tol((dy^2 * db^1)^(1 / 3), "D = (d_y^2 d_yb^1)^(1/3) with weights 2 and 1."),
"/categorical_combinations" = exact(3L, "Three permitted levels.")))
emit_profiler("STAT-DESC-PROFILER-002", "Two models with weighted joint desirability", config2, out,
c("Chart 1 is x1 against the logistic model (charts run predictor by predictor, then model by model).",
"Logistic limits are Wald limits on the logit scale (line 72-74)."), kind = "axis_coverage")
goal_t <- list(response_name = "y", goal = "target", weight = 1, low = 4, high = 12, target = 7)
config3 <- list(models = list(list(artifact = artifact_of(ols, "ols", "identity"))),
predictors = predictors_spec(), goals = list(goal_t), confidence_level = 0.9)
emit_profiler("STAT-DESC-PROFILER-003", "Target goal at a 90% confidence level", config3,
c(chart_outputs(0L, ols, "ols", goal_t, 0.9, points = c(0L, 20L, 45L, 70L, 100L)),
level_outputs(1L, ols, "ols", goal_t, 0.9)),
c("Target 7 inside (4, 12): d rises linearly to 1 at 7 and falls to 0 at 12 (line 82-83)."),
deferred = list(list(path = "/optimum_settings",
reason = "Many settings reach the target; the best setting found depends on the seeded search order (line 96-99).")))
goal_p <- list(response_name = "cnt", goal = "minimize", weight = 1, low = 0, high = 60)
config4 <- list(models = list(list(artifact = artifact_of(pois, "poisson", "log", extra = list(exposure_column = "expo")), exposure = 2.5)),
predictors = predictors_spec(), goals = list(goal_p))
emit_profiler("STAT-DESC-PROFILER-004", "Poisson profile at an entered exposure of 2.5", config4,
c(chart_outputs(0L, pois, "poisson", goal_p, 0.95, exposure = 2.5), level_outputs(1L, pois, "poisson", goal_p, 0.95, exposure = 2.5)),
c("Expected counts are multiplied by the entered exposure; Wald limits include log(2.5) (line 62-63, 74)."),
deferred = list(list(path = "/optimum_settings", reason = "The minimizing corner depends on the signs of the fitted Poisson effects; charts carry the reference.")))
goal_zero <- list(response_name = "y", goal = "maximize", weight = 1, low = 100, high = 200)
config5 <- list(models = list(list(artifact = artifact_of(ols, "ols", "identity"))),
predictors = predictors_spec(), goals = list(goal_zero))
emit_profiler("STAT-DESC-PROFILER-005", "An unattainable goal gives joint desirability zero", config5,
list("/combined_desirability" = tol(0, "Every prediction lies below the lower threshold 100 (line 99)."),
"/charts/0/points/100/desirability" = tol(0, "d is 0 at and below the lower threshold.")),
c(sprintf("The largest prediction in the region is %.6g, far below 100.", best[["fit"]])), kind = "boundary")
bad_target <- goal_t; bad_target$target <- 12
emit_profiler("STAT-DESC-PROFILER-006", "A target on a bound is refused",
list(models = config1$models, predictors = predictors_spec(), goals = list(bad_target)), list(),
c("Line 43-44: an explicit target must lie strictly between the bounds."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "strictly inside its desirability bounds"))
emit_profiler("STAT-DESC-PROFILER-007", "Predictors must cover every retained role",
list(models = config1$models, predictors = predictors_spec()[1], goals = list(goal_y)), list(),
c("Line 19: all predictors must be specified."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "exactly cover the retained model roles"))
generators/r/descriptives_reliability_agreement.R
source("common.R", local = TRUE)
# Survey > Scale reliability and Survey > Rater agreement against base R, psych and irr.
# Every reference reads the exported CSV back before computing.
suppressMessages({library(psych); library(irr)})
run <- generator_run(c("stats", "psych", "irr", "jsonlite"))
tol <- function(value, why, rel = "1e-9") list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = rel,
rationale = paste("R reference:", why)
)
tail <- function(value, why, rel = "1e-9") list(
comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = rel, rationale = paste("R p-value reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
emit <- function(id, title, entry, menu, requirement, dataset, configuration, outputs, notes,
source, citation, kind = "nominal") {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "descriptives", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = source, citation = citation,
generator = list(path = "generators/r/descriptives_reliability_agreement.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.")
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(requirement), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
# =======================================================================================
# Scale reliability: 20 respondents, five 1-5 items; i4 is reverse worded; row 7 has a
# missing i2 (omitted listwise).
set.seed(20261011)
true <- rnorm(20, 3, 0.8)
item <- function(load, shift, flip = FALSE) {
value <- round(pmin(5, pmax(1, shift + load * true + rnorm(20, 0, 0.7))))
if (flip) 6 - value else value
}
scale <- data.frame(i1 = item(1.0, 0.0), i2 = item(0.9, 0.3), i3 = item(1.1, -0.2),
i4 = item(1.0, 0.1, flip = TRUE), i5 = item(0.8, 0.4))
scale <- as.data.frame(lapply(scale, as.integer))
scale$i2[7] <- NA
export_dataset(scale, "DESC_SCALE_B7", "Constructed five-item 1-5 scale (B7)",
"Cronbach (1951); Guttman (1945); McGraw and Wong (1996); psych package documentation",
"Constructed validation data; no third-party license.",
c(i1 = "Int64", i2 = "Int64", i3 = "Int64", i4 = "Int64", i5 = "Int64"))
sd_raw <- read_dataset("DESC_SCALE_B7")
stopifnot(all(sapply(sd_raw[-7, ], function(v) length(unique(v)) > 1)))
SCALE <- "tabelz.analysis.reliability_scale:compute_scale_reliability"
SCALE_REQ <- "REQ-STAT-DESC-SCALE-RELIABILITY-001"
MANUAL_SCALE <- "docs/manual/chapters/88-reliability-agreement.tex"
SCALE_SOURCE <- "psych::alpha, psych::ICC, stats::friedman.test and base R covariance algebra"
SCALE_CITATION <- "Revelle, W. (2024). psych: procedures for psychological research. R package version 2.6.5; Cronbach (1951); Guttman (1945) Psychometrika 10:255-282; McGraw and Wong (1996) Psychological Methods 1:30-46; Shrout and Fleiss (1979)."
ICC_PSYCH_NOTE <- paste("psych::ICC(lmer = FALSE) computes the classical McGraw and Wong (1996) two-way ANOVA",
"mean squares; psych's default lmer = TRUE substitutes REML variance components, whose mean",
"squares are different quantities and do not match the manual's ANOVA formulas.",
"The hand-computed mean squares reproduce the lmer = FALSE rows exactly.")
GUTTMAN_L6_NOTE <- paste("Guttman lambda 6 is computed on the raw scores, 1 - sum_j 1/(S^-1)_jj / (1'S1);",
"psych's G6(smc) is the correlation-matrix analogue of lambda 6 and is used only as a",
"cross-check, because it equals the raw-score value only when the item variances are equal.")
# The adjusted complete-case matrix of the chosen items (reverse-coded ones reflected once).
adjusted <- function(items, reverse = character(0), low = 1, high = 5) {
m <- sd_raw[, items, drop = FALSE]
m <- m[stats::complete.cases(m), , drop = FALSE]
for (name in reverse) m[[name]] <- low + high - m[[name]]
as.matrix(m)
}
scale_core <- function(m, first, second) {
n <- nrow(m); k <- ncol(m)
S <- stats::cov(m)
total <- sum(S)
a <- psych::alpha(m, warnings = FALSE)
R <- stats::cor(m)
rbar <- mean(R[upper.tri(R)])
off <- S - diag(diag(S))
lambda1 <- 1 - sum(diag(S)) / total
lambda2 <- lambda1 + sqrt(k / (k - 1) * sum(off^2)) / total
lambda3 <- k / (k - 1) * lambda1
lambda5 <- lambda1 + 2 * sqrt(max(colSums(off^2))) / total
# Guttman's lambda 6 on the raw scores: residual variance of item j regressed on the others,
# e_j^2 = 1 / (S^-1)_jj = s_j^2 (1 - smc_j). psych's G6(smc) is the correlation-matrix
# analogue 1 - (k - sum smc) / sum R, a different quantity unless the items have equal
# variances; it is only cross-checked here, never used as the reference.
smc <- 1 - 1 / diag(solve(R))
lambda6 <- 1 - sum(1 / diag(solve(S))) / total
stopifnot(isTRUE(all.equal(1 - (k - sum(smc)) / sum(R), a$total[["G6(smc)"]], tolerance = 1e-12)))
# Split-half on the declared halves.
ha <- rowSums(m[, first, drop = FALSE]); hb <- rowSums(m[, second, drop = FALSE])
r <- stats::cor(ha, hb)
k1 <- length(first); k2 <- length(second)
share <- k1 * k2 / (k * k)
unequal <- (-r^2 + sqrt(r^4 + 4 * r^2 * (1 - r^2) * share)) / (2 * (1 - r^2) * share)
guttman4 <- 2 * (1 - (stats::var(ha) + stats::var(hb)) / stats::var(ha + hb))
out <- list(
"/n" = exact(n, "complete cases"),
"/k" = exact(k, "items"),
"/alpha" = tol(a$total$raw_alpha, "psych::alpha raw alpha"),
"/standardized_alpha" = tol(a$total$std.alpha, "psych::alpha standardized alpha"),
"/guttman/0" = tol(lambda1, "Guttman lambda1 = 1 - tr(S) / 1'S1"),
"/guttman/1" = tol(lambda2, "Guttman lambda2 = lambda1 + sqrt(k/(k-1) sum_{i!=j} s_ij^2) / 1'S1"),
"/guttman/2" = tol(lambda3, "Guttman lambda3 = k/(k-1) lambda1 (equals raw alpha)"),
"/guttman/3" = tol(guttman4, "Guttman split-half of the declared split (lambda4 of this split, no search)"),
"/guttman/4" = tol(lambda5, "Guttman lambda5 = lambda1 + 2 sqrt(max_j sum_{i!=j} s_ij^2) / 1'S1"),
"/guttman/5" = tol(lambda6, "Guttman (1945) lambda6 = 1 - sum_j e_j^2 / s_T^2, e_j^2 = 1 / (S^-1)_jj (covariance-based; psych's G6(smc) is the correlation-matrix analogue and differs here)"),
"/split_half/correlation" = tol(r, "correlation of the half totals"),
"/split_half/spearman_brown_equal" = tol(2 * r / (1 + r), "2r / (1 + r)"),
"/split_half/spearman_brown_unequal" = tol(unequal, "unequal-length Spearman-Brown (Horst 1951 form used by SPSS Reliability)"),
"/split_half/guttman_split_half" = tol(guttman4, "2 (1 - (s1^2 + s2^2) / sT^2)"),
"/summaries/0/mean" = tol(mean(colMeans(m)), "mean of the item means"),
"/summaries/3/mean" = tol(rbar, "mean inter-item correlation")
)
if (k1 > 1) out[["/split_half/first_alpha"]] <- tol(psych::alpha(m[, first], warnings = FALSE)$total$raw_alpha, "raw alpha of the first half")
if (k2 > 1) out[["/split_half/second_alpha"]] <- tol(psych::alpha(m[, second], warnings = FALSE)$total$raw_alpha, "raw alpha of the second half")
for (j in seq_len(k)) {
name <- colnames(m)[j]
base <- paste0("/item_totals/", j - 1)
out[[paste0(base, "/mean")]] <- tol(mean(m[, j]), paste("adjusted mean of", name))
out[[paste0(base, "/sd")]] <- tol(stats::sd(m[, j]), paste("adjusted SD of", name))
out[[paste0(base, "/corrected_item_total")]] <- tol(a$item.stats$r.drop[[j]], paste("psych::alpha r.drop for", name))
out[[paste0(base, "/scale_mean_if_deleted")]] <- tol(mean(rowSums(m[, -j, drop = FALSE])), paste("mean of the other items' total without", name))
out[[paste0(base, "/scale_variance_if_deleted")]] <- tol(stats::var(rowSums(m[, -j, drop = FALSE])), paste("variance of the other items' total without", name))
if (k > 2) out[[paste0(base, "/alpha_if_deleted")]] <- tol(a$alpha.drop$raw_alpha[[j]], paste("psych::alpha raw alpha without", name))
}
# Friedman across items (tie-corrected chi-square) and Hotelling's T^2 on successive differences.
fr <- stats::friedman.test(m)
out[["/tests/[name=Friedman's chi-square]/statistic"]] <- tol(fr$statistic, "stats::friedman.test tie-corrected chi-square")
out[["/tests/[name=Friedman's chi-square]/p_value"]] <- tail(fr$p.value, "stats::friedman.test chi-square(k-1) p")
dif <- m[, -k, drop = FALSE] - m[, -1, drop = FALSE]
mu <- colMeans(dif)
t2 <- as.double(n * t(mu) %*% solve(stats::cov(dif)) %*% mu)
f <- (n - k + 1) / ((n - 1) * (k - 1)) * t2
out[["/tests/[name=Hotelling's T-squared (equal item means)]/statistic"]] <- tol(t2, "n mu' S^-1 mu on the k-1 successive differences")
out[["/tests/[name=Hotelling's T-squared (equal item means)]/p_value"]] <- tail(stats::pf(f, k - 1, n - k + 1, lower.tail = FALSE), "F((n-k+1)/((n-1)(k-1)) T^2; k-1, n-k+1) upper tail")
# Two-way ANOVA sums of squares (cases by items).
long <- data.frame(value = as.vector(m), case = factor(rep(seq_len(n), k)), item = factor(rep(colnames(m), each = n)))
ss <- stats::anova(stats::lm(value ~ case + item, data = long))
out[["/anova/0/1"]] <- tol(ss["case", "Sum Sq"], "between-people sum of squares (stats::anova)")
out[["/anova/1/1"]] <- tol(ss["item", "Sum Sq"], "between-items sum of squares")
out[["/anova/2/1"]] <- tol(ss["Residuals", "Sum Sq"], "residual sum of squares")
out[["/anova/2/3"]] <- tol(ss["Residuals", "Mean Sq"], "residual mean square")
out
}
icc_outputs <- function(m, model, type, level, approximate_rel = "1e-9") {
# lmer = FALSE keeps psych::ICC on the classical two-way ANOVA mean squares
# (aov(values ~ subject + item)), the McGraw and Wong (1996) decomposition the manual
# states. The package default lmer = TRUE mixes REML variance components instead, whose
# mean squares are different quantities; the hand check below pins the choice.
res <- psych::ICC(m, alpha = 1 - level, lmer = FALSE)$results
pick <- function(label) res[res$type == label, ]
n <- nrow(m); k <- ncol(m)
grand <- mean(m)
ssr <- k * sum((rowMeans(m) - grand)^2)
ssc <- n * sum((colMeans(m) - grand)^2)
sse <- sum((m - grand)^2) - ssr - ssc
msr <- ssr / (n - 1)
msc <- ssc / (k - 1)
mse <- sse / ((n - 1) * (k - 1))
msw <- (ssc + sse) / (n * (k - 1))
stopifnot(isTRUE(all.equal(unname(pick("ICC3")$F), msr / mse, tolerance = 1e-12)),
isTRUE(all.equal(unname(pick("ICC1")$F), msr / msw, tolerance = 1e-12)),
isTRUE(all.equal(unname(pick("ICC3")$ICC), (msr - mse) / (msr + (k - 1) * mse),
tolerance = 1e-12)),
isTRUE(all.equal(unname(pick("ICC2")$ICC),
(msr - mse) / (msr + (k - 1) * mse + k * (msc - mse) / n),
tolerance = 1e-12)))
single <- switch(paste(model, type),
"one_way consistency" = pick("ICC1"), "one_way agreement" = pick("ICC1"),
"two_way_random agreement" = pick("ICC2"), "two_way_mixed agreement" = pick("ICC2"),
pick("ICC3"))
average <- switch(paste(model, type),
"one_way consistency" = pick("ICC1k"), "one_way agreement" = pick("ICC1k"),
"two_way_random agreement" = pick("ICC2k"), "two_way_mixed agreement" = pick("ICC2k"),
pick("ICC3k"))
bound_rel <- if (type == "agreement" && model != "one_way") approximate_rel else "1e-9"
list(
"/icc/0/estimate" = tol(single$ICC, paste("psych::ICC", single$type, "single-rating estimate")),
"/icc/0/lower" = tol(single[["lower bound"]], paste("psych::ICC", single$type, "lower bound"), bound_rel),
"/icc/0/upper" = tol(single[["upper bound"]], paste("psych::ICC", single$type, "upper bound"), bound_rel),
"/icc/0/f_value" = tol(single$F, paste("psych::ICC", single$type, "F")),
"/icc/0/df1" = tol(single$df1, "numerator df n - 1"),
"/icc/0/df2" = tol(single$df2, "denominator df"),
"/icc/0/p_value" = tail(single$p, paste("psych::ICC", single$type, "F p-value")),
"/icc/1/estimate" = tol(average$ICC, paste("psych::ICC", average$type, "average-rating estimate")),
"/icc/1/lower" = tol(average[["lower bound"]], paste("psych::ICC", average$type, "lower bound"), bound_rel),
"/icc/1/upper" = tol(average[["upper bound"]], paste("psych::ICC", average$type, "upper bound"), bound_rel)
)
}
ITEMS <- c("i1", "i2", "i3", "i4", "i5")
# 001: five items, i4 reversed, first/second halves, two-way mixed consistency.
m <- adjusted(ITEMS, "i4")
outputs <- c(scale_core(m, c("i1", "i2", "i3"), c("i4", "i5")),
icc_outputs(m, "two_way_mixed", "consistency", 0.95),
list("/missing_count" = exact(1L, "row 7 has a missing i2")))
emit("STAT-DESC-SCALE-REL-001", "Scale reliability with a reverse-coded item (halves split, ICC(3,1))",
SCALE, "scaleReliabilityMenuItem", SCALE_REQ, "DESC_SCALE_B7",
list(items = as.list(ITEMS), reverse_items = list("i4"), scale_low = 1, scale_high = 5,
split = "halves", first_half = list(), icc_model = "two_way_mixed",
icc_type = "consistency", confidence_level = 0.95),
outputs,
c(paste0(MANUAL_SCALE, ":14-17: reversal uses x* = L + U - x once; incomplete cases are omitted."),
paste0(MANUAL_SCALE, ":19-29: Cronbach's alpha from the covariance matrix; standardized alpha from the mean inter-item correlation; Guttman's coefficients are covariance-based summaries."),
paste0(MANUAL_SCALE, ":33-38: first/second half split; Spearman-Brown 2r/(1+r) and Guttman's split-half value."),
paste0(MANUAL_SCALE, ":54-72: ICC(C,1) = (MSR - MSE)/(MSR + (k-1) MSE), ICC(C,k) = (MSR - MSE)/MSR; consistency intervals use the F pivot."),
"psych::ICC ICC3 / ICC3k are the two-way consistency coefficients ICC(C,1) and ICC(C,k) (McGraw and Wong).",
ICC_PSYCH_NOTE,
GUTTMAN_L6_NOTE,
"Halves: first i1, i2, i3 ((k+1)/2 = 3 items); second i4, i5."),
SCALE_SOURCE, SCALE_CITATION)
# 002: odd/even split, two-way random absolute agreement.
outputs <- c(scale_core(m, c("i1", "i3", "i5"), c("i2", "i4")),
icc_outputs(m, "two_way_random", "agreement", 0.95))
emit("STAT-DESC-SCALE-REL-002", "Scale reliability with an odd/even split and ICC(2,1) absolute agreement",
SCALE, "scaleReliabilityMenuItem", SCALE_REQ, "DESC_SCALE_B7",
list(items = as.list(ITEMS), reverse_items = list("i4"), scale_low = 1, scale_high = 5,
split = "odd_even", first_half = list(), icc_model = "two_way_random",
icc_type = "agreement", confidence_level = 0.95),
outputs,
c(paste0(MANUAL_SCALE, ":54-72: ICC(A,1) = (MSR - MSE)/(MSR + (k-1) MSE + k (MSC - MSE)/n); agreement intervals use an approximate df calculation."),
"psych::ICC ICC2 / ICC2k are the two-way random absolute-agreement coefficients ICC(A,1) and ICC(A,k); their bounds follow Shrout and Fleiss (1979) / McGraw and Wong (1996).",
ICC_PSYCH_NOTE,
GUTTMAN_L6_NOTE,
"Odd/even split: first i1, i3, i5; second i2, i4."),
SCALE_SOURCE, SCALE_CITATION, kind = "axis_coverage")
# 003: declared first half (i2, i5), one-way ICC, 90% intervals.
outputs <- c(scale_core(m, c("i2", "i5"), c("i1", "i3", "i4")),
icc_outputs(m, "one_way", "consistency", 0.90))
emit("STAT-DESC-SCALE-REL-003", "Scale reliability with a declared split and one-way ICC at 90%",
SCALE, "scaleReliabilityMenuItem", SCALE_REQ, "DESC_SCALE_B7",
list(items = as.list(ITEMS), reverse_items = list("i4"), scale_low = 1, scale_high = 5,
split = "custom", first_half = list("i2", "i5"), icc_model = "one_way",
icc_type = "consistency", confidence_level = 0.90),
outputs,
c(paste0(MANUAL_SCALE, ":66-72: one-way ICC(1,1) = (MSR - MSW)/(MSR + (k-1) MSW) and ICC(1,k) = (MSR - MSW)/MSR; F-pivot intervals."),
"psych::ICC(alpha = 0.10, lmer = FALSE) ICC1 / ICC1k; the declared first half i2, i5 keeps the items' declared order within each half.",
ICC_PSYCH_NOTE,
GUTTMAN_L6_NOTE),
SCALE_SOURCE, SCALE_CITATION, kind = "axis_coverage")
# 004: four items without reversal, no ICC table.
m4 <- adjusted(c("i1", "i2", "i3", "i5"))
outputs <- c(scale_core(m4, c("i1", "i2"), c("i3", "i5")),
list("/icc/0" = list(comparison = "absent", rationale = "icc_model none leaves the ICC table empty")))
emit("STAT-DESC-SCALE-REL-004", "Scale reliability of four items without reversal or ICC",
SCALE, "scaleReliabilityMenuItem", SCALE_REQ, "DESC_SCALE_B7",
list(items = list("i1", "i2", "i3", "i5"), reverse_items = list(), split = "halves",
first_half = list(), icc_model = "none", icc_type = "consistency",
confidence_level = 0.95),
outputs,
c("No reverse coding; halves i1, i2 and i3, i5; icc_model none leaves the ICC table empty.",
GUTTMAN_L6_NOTE),
SCALE_SOURCE, SCALE_CITATION, kind = "axis_coverage")
# =======================================================================================
# Rater agreement: 30 units rated 1-4 by three raters; r3 is missing on four units and r2
# on one.
set.seed(20261012)
severity <- sample(1:4, 30, replace = TRUE, prob = c(0.25, 0.3, 0.25, 0.2))
rate <- function(p_same) {
shift <- ifelse(runif(30) < p_same, 0L, sample(c(-1L, 1L), 30, replace = TRUE))
as.integer(pmin(4L, pmax(1L, severity + shift)))
}
raters <- data.frame(r1 = rate(0.8), r2 = rate(0.7), r3 = rate(0.65))
raters$r3[c(4, 11, 19, 26)] <- NA
raters$r2[15] <- NA
export_dataset(raters, "DESC_RATERS_B7", "Constructed three-rater ordinal ratings (B7)",
"Cohen (1960, 1968); Fleiss (1971); Krippendorff (2004); irr package documentation",
"Constructed validation data; no third-party license.",
c(r1 = "Int64", r2 = "Int64", r3 = "Int64"))
rd <- read_dataset("DESC_RATERS_B7")
stopifnot(length(unique(rd$r1)) == 4, length(unique(rd$r2[!is.na(rd$r2)])) == 4,
length(unique(rd$r3[!is.na(rd$r3)])) == 4)
RATER <- "tabelz.analysis.rater_agreement:compute_rater_agreement"
RATER_REQ <- "REQ-STAT-DESC-RATER-AGREEMENT-001"
RATER_SOURCE <- "irr::kappa2, irr::kappam.fleiss, irr::kripp.alpha and base R (Fleiss, Cohen and Everitt 1969 variance)"
RATER_CITATION <- "Gamer, M., Lemon, J. and Fellows Puspendra Singh, I. (2019). irr: various coefficients of interrater reliability and agreement. R package version 0.85; Cohen (1960); Fleiss, Cohen and Everitt (1969) Psychological Bulletin 72:323-327; Fleiss, Nee and Landis (1979); Krippendorff (2004)."
CATS <- c("1", "2", "3", "4")
# Weighted Cohen's kappa with the Fleiss-Cohen-Everitt (1969) large-sample variance.
cohen_outputs <- function(first, second, kind, level) {
keep <- !is.na(first) & !is.na(second)
f <- factor(first[keep], levels = 1:4); s <- factor(second[keep], levels = 1:4)
n <- sum(keep)
tab <- table(f, s)
p <- tab / n
c <- 4
gap <- abs(outer(1:c, 1:c, "-"))
W <- switch(kind, none = diag(c), linear = 1 - gap / (c - 1), quadratic = 1 - gap^2 / (c - 1)^2)
rows <- rowSums(p); cols <- colSums(p)
po <- sum(W * p); pe <- sum(W * outer(rows, cols))
kappa <- (po - pe) / (1 - pe)
wbar_i <- as.vector(W %*% cols) # row-wise weighted column margins
wbar_j <- as.vector(rows %*% W)
both <- outer(wbar_i, wbar_j, "+")
general <- (sum(p * (W - both * (1 - kappa))^2) - (kappa - pe * (1 - kappa))^2) / (n * (1 - pe)^2)
null <- (sum(outer(rows, cols) * (W - both)^2) - pe^2) / (n * (1 - pe)^2)
irr_weight <- switch(kind, none = "unweighted", linear = "equal", quadratic = "squared")
k2 <- irr::kappa2(cbind(first, second), weight = irr_weight)
stopifnot(isTRUE(all.equal(k2$value, kappa)), isTRUE(all.equal(k2$statistic, kappa / sqrt(null))))
z <- qnorm(0.5 + level / 2)
out <- list(
"/cohen/units" = exact(n, "units rated by both raters"),
"/cohen/kappa" = tol(k2$value, paste("irr::kappa2 kappa (", irr_weight, " weights)")),
"/cohen/observed_agreement" = tol(po, "weighted observed agreement sum w_ij p_ij"),
"/cohen/expected_agreement" = tol(pe, "weighted chance agreement sum w_ij p_i. p_.j"),
"/cohen/standard_error" = tol(sqrt(general), "Fleiss-Cohen-Everitt (1969) general large-sample SE"),
"/cohen/lower" = tol(kappa - z * sqrt(general), "kappa - z SE"),
"/cohen/upper" = tol(kappa + z * sqrt(general), "kappa + z SE"),
"/cohen/null_standard_error" = tol(sqrt(null), "Fleiss-Cohen-Everitt null SE (irr::kappa2 z denominator)"),
"/cohen/z_value" = tol(k2$statistic, "irr::kappa2 z = kappa / null SE"),
"/cohen/p_value" = tail(k2$p.value, "irr::kappa2 two-sided normal p"),
"/table/0/0" = exact(tab[1, 1], "both raters give 1"),
"/table/1/2" = exact(tab[2, 3], "first 2, second 3"),
"/complete_units" = exact(n, "units with every rater's rating"),
"/percent_agreement" = tol(mean(first[keep] == second[keep]), "share of complete units with identical ratings")
)
out
}
# Krippendorff's alpha from irr (raters in rows, units in columns, NA allowed).
alpha_outputs <- function(columns, metric) {
m <- t(as.matrix(rd[, columns]))
ka <- irr::kripp.alpha(m, method = metric)
pairable <- sum(colSums(!is.na(m))[colSums(!is.na(m)) >= 2])
units <- sum(colSums(!is.na(m)) >= 2)
list("/krippendorff/alpha" = tol(ka$value, paste("irr::kripp.alpha", metric)),
"/krippendorff/pairable_values" = exact(pairable, "values in units with at least two ratings"),
"/krippendorff/units" = exact(units, "units with at least two ratings"))
}
fleiss_outputs <- function(columns) {
m <- as.matrix(rd[, columns])
m <- m[stats::complete.cases(m), , drop = FALSE]
kf <- irr::kappam.fleiss(m, detail = TRUE)
# irr evaluates the two-sided p as 2 * (1 - pnorm(z)), which cancels in the far tail: at
# z = 5.63 it loses about nine significant digits. The accurate lower-tail form below is
# the same p-value; the stopifnot documents the size of irr's rounding.
p_value <- 2 * stats::pnorm(-abs(kf$statistic))
stopifnot(abs(p_value / kf$p.value - 1) < 1e-8)
out <- list("/fleiss/units" = exact(nrow(m), "complete units"),
"/fleiss/kappa" = tol(kf$value, "irr::kappam.fleiss kappa"),
"/fleiss/z_value" = tol(kf$statistic, "irr::kappam.fleiss z (Fleiss-Nee-Landis null SE)"),
"/fleiss/p_value" = tail(p_value, "2 * pnorm(-|z|): the two-sided normal p irr::kappam.fleiss reports, evaluated on the accurate lower tail"))
# Per-category kappa and z from Fleiss (1971): irr rounds its detail table to three
# decimals, so the exact values are computed here from the per-unit category counts and
# cross-checked against irr's rounded table.
units <- nrow(m)
raters <- ncol(m)
counts <- t(apply(m, 1, function(row) tabulate(row, nbins = length(CATS))))
p_j <- colSums(counts) / (units * raters)
q_j <- 1 - p_j
se_cat <- sqrt(2 / (units * raters * (raters - 1)))
stopifnot(isTRUE(all.equal(unname(kf$statistic),
kf$value * sum(p_j * q_j) /
sqrt(2 * ((sum(p_j * q_j))^2 - sum(p_j * q_j * (q_j - p_j))) /
(units * raters * (raters - 1))),
tolerance = 1e-12)))
for (j in seq_along(CATS)) {
kj <- 1 - sum(counts[, j] * (raters - counts[, j])) /
(units * raters * (raters - 1) * p_j[j] * q_j[j])
zj <- kj / se_cat
stopifnot(abs(kj - kf$detail[j, "Kappa"]) < 5.1e-4,
abs(zj - kf$detail[j, "z"]) < 5.1e-4)
base <- paste0("/category_kappas/[category=", CATS[j], "]")
out[[paste0(base, "/kappa")]] <- tol(kj, paste("Fleiss (1971) category kappa for", CATS[j], "from the per-unit category counts (irr's detail table agrees at its 3-decimal rounding)"))
out[[paste0(base, "/null_standard_error")]] <- tol(se_cat, paste("Fleiss (1971) category null SE sqrt(2 / (N m (m - 1))) for", CATS[j]))
out[[paste0(base, "/z_value")]] <- tol(zj, paste("category kappa / null SE for", CATS[j]))
out[[paste0(base, "/p_value")]] <- tail(2 * stats::pnorm(-abs(zj)), paste("two-sided normal p for category", CATS[j], "(2 * pnorm(-|z|))"))
}
out
}
agreement_notes <- c(
paste0(MANUAL_SCALE, ":100-110: Cohen's kappa over units rated by both raters; linear/quadratic weights need a declared category order."),
paste0(MANUAL_SCALE, ":112-119: Fleiss' kappa over complete sets; Krippendorff's alpha allows incomplete units with at least two ratings, with the nominal, ordinal or interval metric."),
"Weights use category positions: linear 1 - |i - j|/(c - 1), quadratic 1 - (i - j)^2/(c - 1)^2 (irr::kappa2 'equal' and 'squared').",
"Fleiss category kappa follows Fleiss (1971): kappa_j = 1 - sum_i n_ij (m - n_ij) / (N m (m - 1) p_j (1 - p_j)) with null SE sqrt(2 / (N m (m - 1))), the same quantities irr::kappam.fleiss prints rounded to three decimals.",
"Two-sided normal p-values use 2 * pnorm(-|z|); irr computes 2 * (1 - pnorm(z)), which loses digits to cancellation in the far tail.",
"The bootstrap limits are seed-dependent and not compared."
)
# 001: two raters, unweighted, nominal alpha.
outputs <- c(cohen_outputs(rd$r1, rd$r2, "none", 0.95), alpha_outputs(c("r1", "r2"), "nominal"),
list("/units" = exact(nrow(rd), "rows")))
emit("STAT-DESC-RATER-AGREE-001", "Rater agreement: unweighted Cohen's kappa and nominal alpha",
RATER, "raterAgreementMenuItem", RATER_REQ, "DESC_RATERS_B7",
list(raters = list("r1", "r2"), categories = list(), weights = "none", alpha_metric = "nominal",
confidence_level = 0.95, replicates = 500L, seed = 20260928L, kappa_bootstrap = FALSE),
outputs, c(agreement_notes, "Without declared categories the observed codes sort numerically: 1, 2, 3, 4."),
RATER_SOURCE, RATER_CITATION)
# 002: linear weights with declared categories, ordinal alpha.
outputs <- c(cohen_outputs(rd$r1, rd$r2, "linear", 0.95), alpha_outputs(c("r1", "r2"), "ordinal"))
emit("STAT-DESC-RATER-AGREE-002", "Rater agreement: linear-weighted kappa and ordinal alpha",
RATER, "raterAgreementMenuItem", RATER_REQ, "DESC_RATERS_B7",
list(raters = list("r1", "r2"), categories = as.list(CATS), weights = "linear",
alpha_metric = "ordinal", confidence_level = 0.95, replicates = 500L,
seed = 20260928L, kappa_bootstrap = FALSE),
outputs, agreement_notes, RATER_SOURCE, RATER_CITATION, kind = "axis_coverage")
# 003: quadratic weights, interval alpha, 90% interval.
outputs <- c(cohen_outputs(rd$r1, rd$r2, "quadratic", 0.90), alpha_outputs(c("r1", "r2"), "interval"))
emit("STAT-DESC-RATER-AGREE-003", "Rater agreement: quadratic-weighted kappa at 90% and interval alpha",
RATER, "raterAgreementMenuItem", RATER_REQ, "DESC_RATERS_B7",
list(raters = list("r1", "r2"), categories = as.list(CATS), weights = "quadratic",
alpha_metric = "interval", confidence_level = 0.90, replicates = 500L,
seed = 20260928L, kappa_bootstrap = FALSE),
outputs, c(agreement_notes, "Interval alpha uses the numeric codes' squared differences; the 90% kappa interval uses qnorm(0.95)."),
RATER_SOURCE, RATER_CITATION, kind = "axis_coverage")
# 004: three raters: Fleiss' kappa on complete units, nominal alpha over all pairable units.
complete3 <- stats::complete.cases(rd)
outputs <- c(fleiss_outputs(c("r1", "r2", "r3")), alpha_outputs(c("r1", "r2", "r3"), "nominal"),
list("/complete_units" = exact(sum(complete3), "units rated by all three raters"),
"/percent_agreement" = tol(mean(apply(rd[complete3, ], 1, function(v) length(unique(v)) == 1)), "share of complete units where all three agree")))
emit("STAT-DESC-RATER-AGREE-004", "Rater agreement: Fleiss' kappa for three raters with missing ratings",
RATER, "raterAgreementMenuItem", RATER_REQ, "DESC_RATERS_B7",
list(raters = list("r1", "r2", "r3"), categories = as.list(CATS), weights = "none",
alpha_metric = "nominal", confidence_level = 0.95, replicates = 500L,
seed = 20260928L, kappa_bootstrap = FALSE),
outputs, c(agreement_notes, "Fleiss' kappa uses the 25 units with all three ratings; Krippendorff's alpha also uses the units with two ratings."),
RATER_SOURCE, RATER_CITATION, kind = "axis_coverage")
cat("scale reliability and rater agreement references generated\n")
generators/r/descriptives_reports.R
source("common.R", local = TRUE)
# Independent references for the F150 descriptive reports (Frequencies, Codebook, Case
# summaries, Explore) and the Descriptive summary window. Every reference is computed from
# the re-read CSV with base R, MASS, nortest and e1071; closed-form SPSS-style formulas
# (trimmed mean, G1/G2 moments, Tukey hinges) are written out and cross-checked against the
# package implementations where one exists.
suppressPackageStartupMessages({
library(MASS)
library(nortest)
library(e1071)
})
# 24 rows: 20 valid scores, 2 system-missing (NA) and 2 user-missing (999) scores; grades 1-4
# with code 9 user-missing, one NA and the labelled code 5 never observed; two groups of 12;
# integer frequency weights with one missing weight.
raw <- data.frame(
score = c(12.5, 14.1, NA, 15.9, 16.4, 999, 17.2, 17.8, 18.3, 18.9, 19.4, 19.6,
20.1, NA, 21.5, 22.0, 22.8, 23.4, 24.9, 26.3, 999, 28.7, 33.2, 15.3),
grade = as.integer(c(1, 2, 3, 4, 2, 3, 9, 1, 2, 3, 3, 4, 2, NA, 3, 4, 1, 2, 9, 3, 2, 3, 4, 2)),
group = rep(c("A", "B"), each = 12),
weight = as.integer(c(1, 2, 0, 3, 1, 1, 2, 0, 1, 2, 1, 1, 2, 1, 1, 0, 3, 1, 1, 2, NA, 1, 1, 2)),
stringsAsFactors = FALSE
)
export_dataset(
raw, "DESC_REPORTS_01", "Constructed by descriptives_reports.R",
"R stats: quantile (type 6), t.test, shapiro.test; MASS::huber; nortest::lillie.test; e1071::skewness/kurtosis (type 2)",
"Constructed test data; no third-party dataset license.",
c(score = "Float64", grade = "Int64", group = "String", weight = "Int64")
)
d <- read_dataset("DESC_REPORTS_01")
run <- generator_run(c("stats", "MASS", "nortest", "e1071", "jsonlite"))
metadata <- list(
score = list(variable_label = "Test score", user_missing = list(values = list(999))),
grade = list(
variable_label = "Grade", measurement_level = "nominal",
value_labels = list(
list(value = 1L, label = "Poor"), list(value = 2L, label = "Fair"),
list(value = 3L, label = "Good"), list(value = 4L, label = "Very good"),
list(value = 5L, label = "Excellent"), list(value = 9L, label = "Not rated")
),
user_missing = list(values = list(9L))
)
)
tol <- function(value, label, rel = "1e-9") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = rel,
rationale = paste(label, "from the re-read DESC_REPORTS_01 CSV."))
}
prob <- function(value, label) {
list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = "1e-8",
rationale = paste(label, "from the re-read DESC_REPORTS_01 CSV."))
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
absent <- function(label) list(comparison = "absent", rationale = label)
manual <- "docs/manual/chapters/82-descriptive-reports-and-association.tex"
emit <- function(id, title, kind, entry_point, menu_item, window, configuration, outputs,
notes, source_ref, citation, excluded = list()) {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "descriptives",
entry_point = entry_point, menu_items = list(menu_item), title = title,
case_kind = kind,
dataset = list(path = "reference_data/datasets/DESC_REPORTS_01.json", sha256 = ""),
excluded_rows = excluded, configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"),
sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = source_ref, citation = citation,
generator = list(path = "generators/r/descriptives_reports.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.")
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(paste0("REQ-STAT-DESC-", window, "-001")),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
))
}
# --- shared closed forms -----------------------------------------------------------------
q6 <- function(x, p) unname(stats::quantile(x, p, type = 6))
g1 <- function(x) {
n <- length(x)
z <- (x - mean(x)) / sd(x)
n / ((n - 1) * (n - 2)) * sum(z^3)
}
g2 <- function(x) {
n <- length(x)
z <- (x - mean(x)) / sd(x)
n * (n + 1) / ((n - 1) * (n - 2) * (n - 3)) * sum(z^4) - 3 * (n - 1)^2 / ((n - 2) * (n - 3))
}
se_skew <- function(n) sqrt(6 * n * (n - 1) / ((n - 2) * (n + 1) * (n + 3)))
se_kurt <- function(n) 2 * se_skew(n) * sqrt((n^2 - 1) / ((n - 3) * (n + 5)))
# SPSS EXAMINE 5% trimmed mean with fractional weight on the boundary order statistics.
trimmed <- function(x, p = 0.05) {
x <- sort(x)
n <- length(x)
g <- p * n
k <- floor(g)
r <- g - k
inner <- if (k + 2 <= n - k - 1) sum(x[(k + 2):(n - k - 1)]) else 0
((1 - r) * (x[k + 1] + x[n - k]) + inner) / (n - 2 * g)
}
# M-estimators with the scale 1.4826 MAD held fixed, iterated as weighted means.
m_est <- function(x, w) {
s <- stats::mad(x)
mu <- stats::median(x)
for (i in 1:1000) {
u <- (x - mu) / s
wt <- w(u)
mu1 <- sum(wt * x) / sum(wt)
if (abs(mu1 - mu) <= 1e-13 * max(1, s)) return(mu1)
mu <- mu1
}
stop("M-estimator did not converge")
}
w_tukey <- function(u) ifelse(abs(u) <= 4.685, (1 - (u / 4.685)^2)^2, 0)
w_hampel <- function(u) {
a <- abs(u)
ifelse(a <= 1.7, 1, ifelse(a <= 3.4, 1.7 / a, ifelse(a <= 8.5, 1.7 * (8.5 - a) / ((8.5 - 3.4) * a), 0)))
}
w_andrews <- function(u) {
v <- u / 1.34
ifelse(abs(u) <= 1.34 * pi, ifelse(v == 0, 1, sin(v) / v), 0)
}
valid_score <- !is.na(d$score) & d$score != 999
x <- d$score[valid_score]
stopifnot(length(x) == 20)
stopifnot(abs(g1(x) - e1071::skewness(x, type = 2)) < 1e-12)
stopifnot(abs(g2(x) - e1071::kurtosis(x, type = 2)) < 1e-12)
stopifnot(abs(trimmed(x) - mean(x, trim = 0.05)) < 1e-12) # g = 1 is whole here
moment_index <- function(name) {
names <- c("Valid N", "Mean", "Std. error of mean", "Median", "Mode (smallest if several)",
"Std. deviation", "Variance", "Skewness", "Std. error of skewness", "Kurtosis",
"Std. error of kurtosis", "Range", "Minimum", "Maximum", "Sum")
match(name, names) - 1L
}
stat_path <- function(table, name) sprintf("/tables/%d/statistics/%d/1", table, moment_index(name))
# --- Frequencies ---------------------------------------------------------------------------
freq_source <- "R base table and stats::quantile type 6; e1071 type-2 moments"
freq_cite <- "R stats::quantile documentation (type 6, Weibull); e1071::skewness and kurtosis documentation (type 2, SPSS G1/G2)"
total <- nrow(d)
grade_valid <- d$grade[!is.na(d$grade) & d$grade != 9]
counts <- table(factor(grade_valid, levels = 1:5))
cum <- cumsum(counts)
grade_outputs <- list()
for (v in 1:5) {
key <- as.character(v)
grade_outputs[[sprintf("/tables/0/values/[value=%s]/count", key)]] <-
tol(as.numeric(counts[[key]]), sprintf("count of grade %s", key))
grade_outputs[[sprintf("/tables/0/values/[value=%s]/percent", key)]] <-
tol(100 * counts[[key]] / total, sprintf("percent of all %d rows for grade %s", total, key))
grade_outputs[[sprintf("/tables/0/values/[value=%s]/valid_percent", key)]] <-
tol(100 * counts[[key]] / length(grade_valid), sprintf("valid percent for grade %s", key))
grade_outputs[[sprintf("/tables/0/values/[value=%s]/cumulative_percent", key)]] <-
tol(100 * cum[[key]] / length(grade_valid), sprintf("cumulative valid percent through grade %s", key))
}
grade_outputs[["/tables/0/values/[value=5]/label"]] <- exact("Excellent", "The labelled code 5 is listed with no cases.")
grade_outputs[["/tables/0/values/[value=9]/count"]] <- tol(sum(d$grade == 9, na.rm = TRUE), "user-missing code 9 count")
grade_outputs[["/tables/0/values/[value=9]/missing"]] <- exact("user-missing", "Code 9 is declared user-missing.")
grade_outputs[["/tables/0/values/[value=9]/percent"]] <- tol(100 * sum(d$grade == 9, na.rm = TRUE) / total, "percent of all rows for code 9")
grade_outputs[["/tables/0/values/[missing=system-missing]/count"]] <- tol(sum(is.na(d$grade)), "system-missing grade count")
grade_outputs[["/tables/0/valid"]] <- tol(length(grade_valid), "valid grade count")
grade_outputs[["/tables/0/missing"]] <- tol(total - length(grade_valid), "missing grade count (user plus system)")
n <- length(x)
score_outputs <- list(
"/tables/1/valid" = tol(n, "valid score count"),
"/tables/1/missing" = tol(total - n, "missing score count"),
"/tables/1/values/[value=12.5]/valid_percent" = tol(100 / n, "valid percent of one score")
)
score_outputs[[stat_path(1, "Valid N")]] <- tol(n, "valid N")
score_outputs[[stat_path(1, "Mean")]] <- tol(mean(x), "mean")
score_outputs[[stat_path(1, "Std. error of mean")]] <- tol(sd(x) / sqrt(n), "standard error of the mean")
score_outputs[[stat_path(1, "Median")]] <- tol(q6(x, 0.5), "median by quantile type 6")
score_outputs[[stat_path(1, "Mode (smallest if several)")]] <- tol(min(x), "every value occurs once, so the smallest is the mode")
score_outputs[[stat_path(1, "Std. deviation")]] <- tol(sd(x), "sample SD")
score_outputs[[stat_path(1, "Variance")]] <- tol(var(x), "sample variance")
score_outputs[[stat_path(1, "Skewness")]] <- tol(g1(x), "G1 skewness (e1071 type 2)")
score_outputs[[stat_path(1, "Std. error of skewness")]] <- tol(se_skew(n), "SE of skewness sqrt(6n(n-1)/((n-2)(n+1)(n+3)))")
score_outputs[[stat_path(1, "Kurtosis")]] <- tol(g2(x), "G2 kurtosis (e1071 type 2)")
score_outputs[[stat_path(1, "Std. error of kurtosis")]] <- tol(se_kurt(n), "SE of kurtosis 2 SEskew sqrt((n^2-1)/((n-3)(n+5)))")
score_outputs[[stat_path(1, "Range")]] <- tol(max(x) - min(x), "range")
score_outputs[[stat_path(1, "Minimum")]] <- tol(min(x), "minimum")
score_outputs[[stat_path(1, "Maximum")]] <- tol(max(x), "maximum")
score_outputs[[stat_path(1, "Sum")]] <- tol(sum(x), "sum")
pct <- c(10, 25, 50, 75, 90)
for (i in seq_along(pct)) {
score_outputs[[sprintf("/tables/1/percentiles/%d/0", i - 1)]] <- exact(sprintf("%g", pct[i]), "requested percentile label")
score_outputs[[sprintf("/tables/1/percentiles/%d/1", i - 1)]] <- tol(q6(x, pct[i] / 100), sprintf("percentile %g by quantile type 6", pct[i]))
}
emit("STAT-DESC-FREQUENCIES-001", "Frequencies with labels, user-missing codes and type-6 percentiles",
"nominal", "tabelz.analysis.report_listings:compute_frequencies", "frequenciesMenuItem",
"FREQUENCIES",
list(columns = list("grade", "score"), quartiles = TRUE, percentiles = list(10, 90),
statistics = TRUE, user_missing_valid = FALSE, metadata = metadata),
c(grade_outputs, score_outputs),
c("R: table() over valid codes, labelled code 5 listed with count 0; percent of all rows, valid percent and cumulative valid percent by hand; quantile(type = 6).",
paste0(manual, ":28-33: percent of all rows versus valid percent, user-missing listed separately, percentile at (n+1)p."),
"Statistics order follows the engine's statistics list; skewness and kurtosis are the SPSS G1 and G2 (e1071 type 2)."),
freq_source, freq_cite)
# user-missing counted as valid: 999 enters the score statistics and code 9 becomes a category.
x2 <- d$score[!is.na(d$score)]
stopifnot(length(x2) == 22)
grade_valid2 <- d$grade[!is.na(d$grade)]
um_outputs <- list(
"/tables/0/values/[value=9]/valid_percent" = tol(100 * sum(grade_valid2 == 9) / length(grade_valid2), "code 9 valid percent when user-missing counts as valid"),
"/tables/0/values/[value=9]/missing" = exact("", "Code 9 is a valid category here."),
"/tables/0/valid" = tol(length(grade_valid2), "valid grades including code 9"),
"/tables/0/missing" = tol(sum(is.na(d$grade)), "only the system-missing grade is missing"),
"/tables/1/valid" = tol(length(x2), "valid scores including 999"),
"/tables/1/values/[value=999]/count" = tol(sum(x2 == 999), "999 counted as a valid score value")
)
um_outputs[[stat_path(1, "Mean")]] <- tol(mean(x2), "mean including the two 999 values")
um_outputs[[stat_path(1, "Maximum")]] <- tol(max(x2), "maximum including 999")
um_outputs[[stat_path(1, "Std. deviation")]] <- tol(sd(x2), "SD including 999")
um_outputs[["/tables/1/percentiles/0/1"]] <- tol(q6(x2, 0.5), "median (quartiles on) including 999")
emit("STAT-DESC-FREQUENCIES-002", "Frequencies treating user-missing values as valid",
"axis_coverage", "tabelz.analysis.report_listings:compute_frequencies", "frequenciesMenuItem",
"FREQUENCIES",
list(columns = list("grade", "score"), quartiles = FALSE, percentiles = list(50),
statistics = TRUE, user_missing_valid = TRUE, metadata = metadata),
um_outputs,
c("R: the same tabulation with the user-missing codes 9 and 999 kept as valid values.",
paste0(manual, ":28-33.")),
freq_source, freq_cite)
# cut points and exclusions: rows 1 and 5 (0-based 0 and 4) are excluded; no statistics.
keep <- valid_score & !(seq_len(nrow(d)) %in% c(1, 5))
x3 <- d$score[keep]
stopifnot(length(x3) == 18)
cut_outputs <- list(
"/tables/0/valid" = tol(length(x3), "valid scores after excluding two rows"),
"/tables/0/missing" = tol(nrow(d) - 2 - length(x3), "missing among the 22 remaining rows"),
"/tables/0/statistics/0" = absent("Statistics were not requested."),
"/tables/0/percentiles/0/0" = exact("25", "first cut point of four"),
"/tables/0/percentiles/0/1" = tol(q6(x3, 0.25), "25th percentile type 6 after exclusions"),
"/tables/0/percentiles/1/1" = tol(q6(x3, 0.5), "50th percentile type 6 after exclusions"),
"/tables/0/percentiles/2/1" = tol(q6(x3, 0.75), "75th percentile type 6 after exclusions"),
"/tables/0/percentiles/3" = absent("Four cut points give three percentiles.")
)
emit("STAT-DESC-FREQUENCIES-003", "Frequencies cut points after row exclusions, no statistics",
"axis_coverage", "tabelz.analysis.report_listings:compute_frequencies", "frequenciesMenuItem",
"FREQUENCIES",
list(columns = list("score"), quartiles = FALSE, percentiles = list(), cut_points = 4L,
statistics = FALSE, user_missing_valid = FALSE, metadata = metadata),
cut_outputs,
c("R: quantile(type = 6) at 25, 50 and 75 on the 18 valid scores of the 22 non-excluded rows.",
paste0(manual, ":28-33.")),
freq_source, freq_cite, excluded = list(0L, 4L))
# --- Codebook -------------------------------------------------------------------------------
cb_outputs <- list(
"/rows" = exact(nrow(d), "all 24 rows"),
"/entries/0/column" = exact("score", "first entry"),
"/entries/0/level" = exact("scale", "numeric without a declared level is scale"),
"/entries/0/valid" = tol(n, "valid scores"),
"/entries/0/user_missing_count" = tol(sum(d$score == 999, na.rm = TRUE), "two 999 scores"),
"/entries/0/system_missing_count" = tol(sum(is.na(d$score)), "two NA scores"),
"/entries/0/summary/0/1" = tol(n, "Valid N"),
"/entries/0/summary/1/1" = tol(mean(x), "mean"),
"/entries/0/summary/2/1" = tol(sd(x), "sample SD"),
"/entries/0/summary/3/1" = tol(min(x), "minimum"),
"/entries/0/summary/4/1" = tol(max(x), "maximum"),
"/entries/0/summary/5/1" = tol(q6(x, 0.25), "25th percentile type 6"),
"/entries/0/summary/6/1" = tol(q6(x, 0.5), "50th percentile type 6"),
"/entries/0/summary/7/1" = tol(q6(x, 0.75), "75th percentile type 6"),
"/entries/1/level" = exact("nominal", "declared nominal"),
"/entries/1/label" = exact("Grade", "variable label"),
"/entries/1/valid" = tol(length(grade_valid), "valid grades"),
"/entries/1/user_missing_count" = tol(sum(d$grade == 9, na.rm = TRUE), "code 9 count"),
"/entries/1/system_missing_count" = tol(sum(is.na(d$grade)), "NA grade count"),
"/entries/1/values/[value=2]/count" = tol(as.numeric(counts[["2"]]), "grade 2 count"),
"/entries/1/values/[value=2]/label" = exact("Fair", "value label"),
"/entries/1/values/[value=5]/count" = tol(0, "labelled code 5 has no cases"),
"/entries/1/values/[value=9]/missing" = exact("user-missing", "code 9 listed as user-missing"),
"/entries/1/values/[value=9]/count" = tol(sum(d$grade == 9, na.rm = TRUE), "code 9 count"),
"/entries/2/level" = exact("nominal", "text is nominal"),
"/entries/2/values/[value=A]/count" = tol(sum(d$group == "A"), "group A count"),
"/entries/2/values/[value=B]/valid_percent" = tol(100 * sum(d$group == "B") / nrow(d), "group B valid percent"),
"/entries/2/valid" = tol(nrow(d), "every group value is valid")
)
emit("STAT-DESC-CODEBOOK-001", "Codebook: scale summary, labelled categories and missing counts",
"nominal", "tabelz.analysis.report_listings:compute_codebook", "codebookMenuItem",
"CODEBOOK",
list(columns = list("score", "grade", "group"), metadata = metadata),
cb_outputs,
c("R: mean, sd, min, max and quantile(type = 6) on the 20 valid scores; table() counts for grade and group with the labelled code 5 at 0.",
paste0(manual, ":11-14: counts and labels including labelled categories with no cases; scale variables carry numerical summaries.")),
"R base table and stats; quantile type 6",
"R stats::quantile documentation (type 6)")
# --- Case summaries ----------------------------------------------------------------------
by_group <- function(g) d$score[valid_score & d$group == g]
cs_outputs <- list(
"/listed" = exact(5L, "limit of 5"),
"/total" = exact(nrow(d), "all 24 rows are summarised"),
"/rows/0/0" = exact("1", "first listed case number"),
"/rows/0/1" = exact("A", "its group"),
"/rows/0/2" = exact("12.5", "its score as shown"),
"/rows/2/2" = exact("", "the third row's score is system-missing"),
"/rows/5" = absent("only five rows are listed"),
"/statistics/0/group" = exact("A", "groups in sorted order"),
"/statistics/0/n" = exact(length(by_group("A")), "valid scores in A"),
"/statistics/0/mean" = tol(mean(by_group("A")), "mean of A"),
"/statistics/0/sd" = tol(sd(by_group("A")), "SD of A"),
"/statistics/1/group" = exact("B", "second group"),
"/statistics/1/n" = exact(length(by_group("B")), "valid scores in B"),
"/statistics/1/mean" = tol(mean(by_group("B")), "mean of B"),
"/statistics/1/sd" = tol(sd(by_group("B")), "SD of B"),
"/statistics/2/group" = exact("Total", "total row"),
"/statistics/2/n" = exact(n, "all valid scores"),
"/statistics/2/mean" = tol(mean(x), "overall mean"),
"/statistics/2/sd" = tol(sd(x), "overall SD")
)
emit("STAT-DESC-CASESUMMARIES-001", "Case summaries by group: five listed, statistics on every case",
"nominal", "tabelz.analysis.report_listings:compute_case_summaries", "caseSummariesMenuItem",
"CASESUMMARIES",
list(columns = list("score"), group = "group", limit = 5L, statistics = TRUE,
metadata = metadata, frequency_weight = NULL),
cs_outputs,
c("R: mean and sd of the valid scores (NA and 999 excluded) per group and overall.",
paste0(manual, ":16-18: summaries use all eligible cases, not just the displayed rows.")),
"R base mean and sd by group", "R stats::sd documentation")
w <- d$weight
w[is.na(w)] <- 0L
chosen <- valid_score & w > 0
W <- sum(w[chosen])
wm <- sum(w[chosen] * d$score[chosen]) / W
wsd <- sqrt(sum(w[chosen] * (d$score[chosen] - wm)^2) / (W - 1))
stopifnot(abs(wm - mean(rep(d$score[chosen], w[chosen]))) < 1e-12)
stopifnot(abs(wsd - sd(rep(d$score[chosen], w[chosen]))) < 1e-12)
cw_outputs <- list(
"/listed" = exact(nrow(d), "all rows fit within the limit"),
"/columns/1" = exact("Weight", "the weight column is shown"),
"/rows/0/1" = exact("1", "first row's weight"),
"/rows/20/1" = exact("", "the missing weight is shown blank"),
"/statistics/0/n" = exact(sum(chosen), "rows with a valid score and a positive weight"),
"/statistics/0/weight_sum" = tol(W, "W, the sum of the positive weights of valid scores"),
"/statistics/0/mean" = tol(wm, "weighted mean (mean of the replicated values)"),
"/statistics/0/sd" = tol(wsd, "SD with denominator W - 1 (sd of the replicated values)")
)
emit("STAT-DESC-CASESUMMARIES-002", "Case summaries under an integer frequency weight",
"axis_coverage", "tabelz.analysis.report_listings:compute_case_summaries", "caseSummariesMenuItem",
"CASESUMMARIES",
list(columns = list("score"), limit = 100L, statistics = TRUE,
metadata = metadata, frequency_weight = "weight"),
cw_outputs,
c("R: mean and sd of rep(score, weight) over valid scores with positive weight; a missing weight is 0.",
paste0(manual, ":19-27: weighted mean over positive weights, squared deviations divided by W - 1, zero-weight rows listed but not summarised.")),
"R base rep/mean/sd (literal replication)", "R stats::sd documentation")
# --- Explore --------------------------------------------------------------------------------
explore_cite <- "R stats::t.test, shapiro.test, quantile (type 6), fivenum; nortest::lillie.test (Dallal-Wilkinson/Stephens); MASS::huber; e1071 type-2 moments"
desc_index_h <- c("Mean", "5% trimmed mean", "Median", "Variance", "Std. deviation", "Minimum",
"Maximum", "Range", "Interquartile range", "Skewness", "Kurtosis")
desc_index_t <- c("Mean", "5% trimmed mean", "Median", "Variance", "Std. deviation", "Minimum",
"Maximum", "Range", "25th percentile (type 6)", "75th percentile (type 6)",
"Tukey's lower hinge", "Tukey's upper hinge",
"Interquartile range (Tukey's hinges)", "Skewness", "Kurtosis")
dpath <- function(cell, name, names, field = 1L) {
sprintf("/cells/%d/descriptives/%d/%d", cell, match(name, names) - 1L, field)
}
explore_cell <- function(cell, values, positions, level, names, convention) {
n <- length(values)
ci <- stats::t.test(values, conf.level = level)$conf.int
out <- list()
out[[sprintf("/cells/%d/n", cell)]] <- exact(n, "valid values in the cell")
out[[dpath(cell, "Mean", names)]] <- tol(mean(values), "mean")
out[[dpath(cell, "Mean", names, 2L)]] <- tol(sd(values) / sqrt(n), "SE of the mean")
out[[dpath(cell, "5% trimmed mean", names)]] <- tol(trimmed(values), "5% trimmed mean with fractional boundary weight")
out[[dpath(cell, "Median", names)]] <- tol(q6(values, 0.5), "median type 6")
out[[dpath(cell, "Variance", names)]] <- tol(var(values), "sample variance")
out[[dpath(cell, "Std. deviation", names)]] <- tol(sd(values), "sample SD")
out[[dpath(cell, "Minimum", names)]] <- tol(min(values), "minimum")
out[[dpath(cell, "Maximum", names)]] <- tol(max(values), "maximum")
out[[dpath(cell, "Range", names)]] <- tol(max(values) - min(values), "range")
out[[dpath(cell, "Skewness", names)]] <- tol(g1(values), "G1 skewness")
out[[dpath(cell, "Skewness", names, 2L)]] <- tol(se_skew(n), "SE of skewness")
out[[dpath(cell, "Kurtosis", names)]] <- tol(g2(values), "G2 kurtosis")
out[[dpath(cell, "Kurtosis", names, 2L)]] <- tol(se_kurt(n), "SE of kurtosis")
out[[sprintf("/cells/%d/mean_lower", cell)]] <- tol(ci[[1]], sprintf("t interval lower bound at %g", level))
out[[sprintf("/cells/%d/mean_upper", cell)]] <- tol(ci[[2]], sprintf("t interval upper bound at %g", level))
sw <- stats::shapiro.test(values)
ks <- nortest::lillie.test(values)
out[[sprintf("/cells/%d/normality/[test=Shapiro-Wilk]/statistic", cell)]] <- tol(sw$statistic, "Shapiro-Wilk W", "1e-7")
out[[sprintf("/cells/%d/normality/[test=Shapiro-Wilk]/p_value", cell)]] <- list(
comparison = "tail_probability", value = dec(unname(sw$p.value)), relative_tolerance = "1e-5",
rationale = "shapiro.test p (Royston 1995 approximation in both implementations; loosened for the SWILK algorithm's single-precision constants).")
out[[sprintf("/cells/%d/normality/[test=Kolmogorov-Smirnov (Lilliefors)]/statistic", cell)]] <- tol(ks$statistic, "Lilliefors D")
out[[sprintf("/cells/%d/normality/[test=Kolmogorov-Smirnov (Lilliefors)]/p_value", cell)]] <- prob(ks$p.value, "Lilliefors p (Dallal-Wilkinson, Stephens above 0.1)")
huber <- MASS::huber(values, k = 1.339, tol = 1e-12)$mu
out[[sprintf("/cells/%d/m_estimates/0/value", cell)]] <- tol(huber, "Huber M-estimator, MASS::huber(k = 1.339) with MAD scale", "1e-6")
out[[sprintf("/cells/%d/m_estimates/1/value", cell)]] <- tol(m_est(values, w_tukey), "Tukey biweight, fixed MAD scale, weighted-mean iteration", "1e-6")
out[[sprintf("/cells/%d/m_estimates/2/value", cell)]] <- tol(m_est(values, w_hampel), "Hampel M-estimator, fixed MAD scale", "1e-6")
out[[sprintf("/cells/%d/m_estimates/3/value", cell)]] <- tol(m_est(values, w_andrews), "Andrews wave, fixed MAD scale", "1e-6")
ord <- order(values, positions)
high <- rev(ord)[1:5]
low <- ord[1:5]
for (r in 1:5) {
out[[sprintf("/cells/%d/extremes/%d/value", cell, r - 1)]] <- tol(values[high[r]], sprintf("highest value rank %d", r))
out[[sprintf("/cells/%d/extremes/%d/case", cell, r - 1)]] <- exact(as.integer(positions[high[r]]), "original case number of the highest value")
out[[sprintf("/cells/%d/extremes/%d/value", cell, 4 + r)]] <- tol(values[low[r]], sprintf("lowest value rank %d", r))
out[[sprintf("/cells/%d/extremes/%d/case", cell, 4 + r)]] <- exact(as.integer(positions[low[r]]), "original case number of the lowest value")
}
if (convention == "tukey_hinges") {
hinges <- stats::fivenum(values)[c(2, 4)]
q1 <- hinges[1]
q3 <- hinges[2]
out[[dpath(cell, "25th percentile (type 6)", names)]] <- tol(q6(values, 0.25), "25th percentile type 6")
out[[dpath(cell, "75th percentile (type 6)", names)]] <- tol(q6(values, 0.75), "75th percentile type 6")
out[[dpath(cell, "Tukey's lower hinge", names)]] <- tol(q1, "lower hinge (fivenum)")
out[[dpath(cell, "Tukey's upper hinge", names)]] <- tol(q3, "upper hinge (fivenum)")
out[[dpath(cell, "Interquartile range (Tukey's hinges)", names)]] <- tol(q3 - q1, "hinge spread")
} else {
q1 <- q6(values, 0.25)
q3 <- q6(values, 0.75)
out[[dpath(cell, "Interquartile range", names)]] <- tol(q3 - q1, "IQR of type-6 quartiles")
}
iqr <- q3 - q1
inside <- values[values >= q1 - 1.5 * iqr & values <= q3 + 1.5 * iqr]
out[[sprintf("/cells/%d/box/q1", cell)]] <- tol(q1, "box lower quartile")
out[[sprintf("/cells/%d/box/q3", cell)]] <- tol(q3, "box upper quartile")
out[[sprintf("/cells/%d/box/median", cell)]] <- tol(q6(values, 0.5), "box median")
out[[sprintf("/cells/%d/box/whisker_low", cell)]] <- tol(min(inside), "lowest value within 1.5 IQR")
out[[sprintf("/cells/%d/box/whisker_high", cell)]] <- tol(max(inside), "highest value within 1.5 IQR")
far <- values < q1 - 3 * iqr | values > q3 + 3 * iqr
mild <- (values < q1 - 1.5 * iqr | values > q3 + 1.5 * iqr) & !far
out[[sprintf("/cells/%d/box/outliers/%d", cell, sum(mild))]] <- absent(sprintf("%d mild outliers", sum(mild)))
out[[sprintf("/cells/%d/box/extremes/%d", cell, sum(far))]] <- absent(sprintf("%d extremes", sum(far)))
idx <- which(mild)
for (k in seq_along(idx)) {
out[[sprintf("/cells/%d/box/outliers/%d/0", cell, k - 1)]] <- exact(as.integer(positions[idx[k]]), "outlier case number")
out[[sprintf("/cells/%d/box/outliers/%d/1", cell, k - 1)]] <- tol(values[idx[k]], "outlier value")
}
out
}
positions_all <- which(valid_score)
ex1 <- explore_cell(0L, x, positions_all, 0.95, desc_index_h, "haverage")
ex1[["/cells/0/missing"]] <- exact(as.integer(total - n), "user- and system-missing scores")
ex1[["/cells/0/group"]] <- exact("", "no factor")
emit("STAT-DESC-EXPLORE-001", "Explore one dependent: descriptives, M-estimators, extremes, normality, box",
"nominal", "tabelz.analysis.report_explore:compute_explore", "exploreMenuItem", "EXPLORE",
list(dependents = list("score"), confidence_level = 0.95, quartiles = "haverage",
metadata = metadata),
ex1,
c("R: t.test interval; SPSS trimmed mean formula (equals mean(trim = 0.05) here); e1071 type-2 G1/G2 with closed-form SEs; quantile(type = 6) quartiles and 1.5/3 IQR fences; shapiro.test; nortest::lillie.test; MASS::huber(k = 1.339) and the documented fixed-MAD weighted-mean iteration for the Tukey, Hampel and Andrews weights (tolerance 1e-6 for the iterative estimators).",
paste0(manual, ":35-46: trimmed mean with fractional boundary weight, whiskers within 1.5 IQR, extremes beyond 3 IQR, fixed 1.4826 MAD scale, fitted-normal K-S approximation."),
"Extreme case numbers are 1-based row positions in the dataset before exclusions."),
"R stats, MASS, nortest and e1071", explore_cite)
excluded2 <- c(1L, 4L) # 0-based rows 1 and 4: scores 14.1 and 16.4 in group A
kept <- !(seq_len(nrow(d)) %in% (excluded2 + 1L))
ex2 <- list()
for (g in c("A", "B")) {
cell <- match(g, c("A", "B")) - 1L
rows <- which(kept & d$group == g & valid_score)
ex2 <- c(ex2, explore_cell(cell, d$score[rows], rows, 0.90, desc_index_t, "tukey_hinges"))
ex2[[sprintf("/cells/%d/group", cell)]] <- exact(g, "factor level")
ex2[[sprintf("/cells/%d/missing", cell)]] <- exact(as.integer(sum(kept & d$group == g & !valid_score)), "missing scores in the level among non-excluded rows")
}
ex2[["/cells/2"]] <- absent("two levels give two cells")
emit("STAT-DESC-EXPLORE-002", "Explore by factor with Tukey's hinges, a 90% interval and exclusions",
"axis_coverage", "tabelz.analysis.report_explore:compute_explore", "exploreMenuItem", "EXPLORE",
list(dependents = list("score"), factor = "group", confidence_level = 0.90,
quartiles = "tukey_hinges", metadata = metadata),
ex2,
c("R: per level of group after excluding rows 2 and 5 (1-based); fivenum hinges give the box, fences and hinge spread; the type-6 quartiles are reported beside them; t.test at 0.90.",
paste0(manual, ":35-41."),
"Case numbers in extremes and outliers are positions before exclusion (#131)."),
"R stats, MASS, nortest and e1071", explore_cite, excluded = as.list(excluded2))
# --- Descriptive summary (operation:descriptives) ------------------------------------------
ds_source <- "R base mean, median, sd, min, max"
ds_cite <- "R stats::median and sd documentation"
ds_manual <- "docs/manual/chapters/06-analysis-foundations.tex:4-5: a descriptive summary characterizes the observed data."
emit("STAT-DESC-SUMMARY-001", "Descriptive summary of a column with user-missing values masked",
"nominal", "operation:descriptives", "descriptiveSummaryMenuItem", "SUMMARY",
list(column = "score", metadata = metadata),
list("/column" = exact("score", "the summarised column"),
"/status" = exact("ok", "a successful summary"),
"/sample_count" = exact(n, "finite values after masking 999 and NA"),
"/values/mean" = tol(mean(x), "mean"),
"/values/median" = tol(stats::median(x), "median (average of the two middle values)"),
"/values/stdev" = tol(sd(x), "sample SD (ddof 1)"),
"/values/sample_variance" = tol(var(x), "sample variance shown as stdev squared"),
"/values/min" = tol(min(x), "minimum"),
"/values/max" = tol(max(x), "maximum")),
c("R: mean, median, sd, min and max of the 20 finite scores; the declared user-missing 999 is presented as missing, as the desktop masks it before the engine runs.",
ds_manual),
ds_source, ds_cite)
excluded3 <- c(0L, 1L)
kept3 <- !(seq_len(nrow(d)) %in% (excluded3 + 1L))
finite3 <- kept3 & valid_score
pos3 <- finite3 & w > 0
rep3 <- rep(d$score[pos3], w[pos3])
emit("STAT-DESC-SUMMARY-002", "Descriptive summary under a frequency weight after exclusions",
"axis_coverage", "operation:descriptives", "descriptiveSummaryMenuItem", "SUMMARY",
list(column = "score", frequency_weight = "weight", metadata = metadata),
list("/status" = exact("ok", "a successful summary"),
"/sample_count" = exact(sum(finite3), "finite scores among the non-excluded rows, whatever their weight"),
"/values/positive_weight_rows" = tol(sum(pos3), "finite scores with a positive weight"),
"/values/frequency_total" = tol(sum(w[pos3]), "W, the total weight of those rows"),
"/values/mean" = tol(mean(rep3), "mean of the replicated values"),
"/values/median" = tol(stats::median(rep3), "median of the replicated values"),
"/values/stdev" = tol(sd(rep3), "SD of the replicated values (W - 1)"),
"/values/min" = tol(min(rep3), "minimum"),
"/values/max" = tol(max(rep3), "maximum")),
c("R: rep(score, weight) over the finite scores of the non-excluded rows with a positive weight (a missing weight counts 0); rows 1 and 2 excluded.",
ds_manual),
"R base rep/mean/median/sd (literal replication)", ds_cite, excluded = as.list(excluded3))
cat("descriptive report references generated\n")
generators/r/descriptives_survey_diagnostics.R
source("common.R", local = TRUE)
run <- generator_run(c("stats", "survey", "nortest", "jsonlite"))
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R closed-form reference:", why)
)
tail <- function(value, why) list(
comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = "1e-9", rationale = paste("R p-value reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
emit <- function(id, title, entry, menu, dataset, configuration, outputs, notes,
kind = "nominal") {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "descriptives", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R survey/stats/nortest",
citation = "R survey::svydesign/svymean, stats::shapiro.test and nortest::ad.test documentation",
generator = list(path = "generators/r/descriptives_survey_diagnostics.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.")
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
raw <- data.frame(
response = c(2.0, 3.0, 4.5, 5.0, 5.5, 7.0, 8.0, 9.5),
binary = c(0, 0, 1, 1, 0, 1, 1, 1),
weight = c(1, 1.5, 2, 0.75, 1.25, 2.5, 1, 1.75),
stratum = rep(c("A", "B"), each = 4),
psu = rep(c("1", "2"), each = 2, times = 2)
)
export_dataset(raw, "DESC_SURVEY_01", "Constructed two-stratum one-stage survey",
"R survey::svydesign and survey::svymean documentation",
"Constructed validation data; no third-party license.",
c(response = "Float64", binary = "Float64", weight = "Float64",
stratum = "String", psu = "String"))
d <- read_dataset("DESC_SURVEY_01")
design <- survey::svydesign(ids = ~psu, strata = ~stratum, weights = ~weight,
data = d, nest = TRUE)
for (mode in c("mean", "proportion")) {
response <- if (mode == "mean") "response" else "binary"
estimate <- survey::svymean(stats::as.formula(paste0("~", response)), design)
point <- as.double(coef(estimate)[[1]])
standard_error <- sqrt(as.double(stats::vcov(estimate)[1, 1]))
df <- as.integer(survey::degf(design))
bounds <- point + c(-1, 1) * stats::qt(0.975, df) * standard_error
id <- if (mode == "mean") "STAT-DESC-SURVEY-001" else "STAT-DESC-SURVEY-002"
emit(id, paste("Survey", mode), "tabelz.executor.survey_descriptives",
"surveyDescriptivesMenuItem", "DESC_SURVEY_01",
list(response_column = response, weight_column = "weight",
stratum_column = "stratum", psu_column = "psu", mode = mode),
list("/estimate" = tol(point, "Design-weighted ratio estimate; owner 1e-9 tier."),
"/standard_error" = tol(standard_error, "One-stage with-replacement Taylor SE; owner 1e-9 tier."),
"/lower" = tol(bounds[1], "Two-sided t interval lower bound; owner 1e-9 tier."),
"/upper" = tol(bounds[2], "Two-sided t interval upper bound; owner 1e-9 tier."),
"/degrees_of_freedom" = exact(df, "Four PSUs minus two strata give two design degrees of freedom."),
"/observations" = exact(nrow(d), "Every response is observed.")),
c("Manual chapter 28 lines 25-43: one-stage sampling with replacement, Taylor ratio variance and t interval.",
"R svydesign sets nest=TRUE because PSU labels repeat across strata; four original PSUs in two strata."))
}
diagnostic <- data.frame(value = c(-1.7, -1.15, -0.72, -0.31, -0.12, 0.04,
0.25, 0.51, 0.74, 1.02, 1.44, 1.91))
export_dataset(diagnostic, "DESC_DIAGNOSTIC_01", "Constructed normality diagnostic values",
"R stats::shapiro.test and nortest::ad.test documentation",
"Constructed validation data; no third-party license.", c(value = "Float64"))
diagnostic <- read_dataset("DESC_DIAGNOSTIC_01")
shapiro <- stats::shapiro.test(diagnostic$value)
emit("STAT-DESC-DIAG-001", "Shapiro-Wilk normality", "tabelz.executor.distribution_diagnostics",
"distributionDiagnosticsMenuItem", "DESC_DIAGNOSTIC_01",
list(column = "value", method = "shapiro_wilk"),
list("/statistic" = tol(shapiro$statistic, "Shapiro-Wilk W; owner 1e-9 tier."),
"/p_value" = tail(shapiro$p.value, "Calibrated Shapiro-Wilk upper-tail p-value; owner 1e-9 tier."),
"/observations" = exact(nrow(diagnostic), "Twelve finite complete cases.")),
c("Manual chapter 30 lines 15-17: Shapiro-Wilk applies to 3-5000 finite values."))
anderson <- nortest::ad.test(diagnostic$value)
emit("STAT-DESC-DIAG-002", "Anderson-Darling fitted-normal diagnostic",
"tabelz.executor.distribution_diagnostics", "distributionDiagnosticsMenuItem",
"DESC_DIAGNOSTIC_01", list(column = "value", method = "anderson_darling"),
list("/statistic" = tol(anderson$statistic, "Fitted-mean-and-variance Anderson-Darling statistic; owner 1e-9 tier."),
"/p_value" = tail(anderson$p.value, "Fitted-normal Anderson-Darling p-value; owner 1e-9 tier.")),
c("Manual chapter 30 lines 18-20: fitted normal mean and variance, not a known-parameter test."))
cat("descriptives survey and diagnostics references generated\n")
generators/r/descriptives_survey_estimates.R
source("common.R", local = TRUE)
# Complex-samples descriptive procedures (Survey > Means, totals and proportions; Survey >
# Frequencies and crosstabs; Survey > Ratios) against R survey 4.5. Every reference reads
# the exported CSV back before computing.
suppressMessages(library(survey))
run <- generator_run(c("stats", "survey", "jsonlite"))
options(survey.lonely.psu = "fail")
tol <- function(value, why, rel = "1e-9") list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = rel,
rationale = paste("R survey closed-form reference:", why)
)
tail <- function(value, why, rel = "1e-9") list(
comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = rel, rationale = paste("R survey p-value reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
MANUAL <- "docs/manual/chapters/87-complex-surveys.tex"
emit <- function(id, title, entry, menu, requirement, configuration, outputs, notes,
kind = "nominal") {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "descriptives", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/DESC_SURVEY_B7.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R survey package: svydesign, svrepdesign, svymean, svytotal, svyratio, svyby, svychisq, svyciprop",
citation = "Lumley, T. (2024). survey: analysis of complex survey samples. R package version 4.5; Lumley (2010) Complex Surveys, Wiley.",
generator = list(path = "generators/r/descriptives_survey_estimates.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.",
paste0(MANUAL, ":31-40: domain rows keep the design context; nondomain units still contribute to the variance."),
paste0(MANUAL, ":53-58: Taylor linearization aggregates influence contributions within PSUs within strata, applying declared FPCs."),
paste0(MANUAL, ":61-69: replicate variance is c * sum_r c_r (theta_r - theta)^2 centred at the full estimate or replicate mean as declared."))
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(requirement), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
# ---------------------------------------------------------------------------------------
# Dataset: two strata, four PSUs each, three rows per PSU; x has two missing values; four
# supplied replicate-weight columns for the replicate-variance cases.
set.seed(20261009)
h <- rep(c("1", "2"), each = 12)
psu <- rep(sprintf("p%d", 1:8), each = 3)
w <- round(c(1.5, 1.5, 1.5, 2.0, 2.0, 2.0, 1.2, 1.2, 1.2, 2.4, 2.4, 2.4,
3.0, 3.0, 3.0, 2.5, 2.5, 2.5, 1.8, 1.8, 1.8, 3.6, 3.6, 3.6) *
runif(24, 0.85, 1.15), 4)
N <- rep(c(20L, 30L), each = 12)
x <- round(5 + 1.5 * (h == "2") + rnorm(24, 0, 1.2), 3)
x[c(5, 17)] <- NA
y <- round(10 + 0.8 * ifelse(is.na(x), 6, x) + rnorm(24, 0, 1.5), 3)
z <- round(3 + 0.4 * y + rnorm(24, 0, 0.8), 3)
g <- c("a", "b", "c", "a", "b", "b", "c", "a", "b", "a", "c", "b",
"b", "a", "c", "b", "a", "a", "c", "b", "b", "a", "c", "c")
b <- c("yes", "no", "yes", "yes", "no", "yes", "no", "no", "yes", "yes", "no", "yes",
"no", "yes", "yes", "no", "yes", "no", "yes", "yes", "no", "yes", "yes", "no")
dom <- c("A", "A", "B", "A", "B", "B", "A", "A", "B", "B", "A", "B",
"A", "B", "A", "A", "B", "B", "A", "B", "A", "A", "B", "B")
rw <- round(w * matrix(runif(24 * 4, 0.6, 1.4), 24, 4), 4)
raw <- data.frame(h = h, psu = psu, w = w, N = N, x = x, y = y, z = z, g = g, b = b,
dom = dom, rw1 = rw[, 1], rw2 = rw[, 2], rw3 = rw[, 3], rw4 = rw[, 4],
stringsAsFactors = FALSE)
export_dataset(raw, "DESC_SURVEY_B7", "Constructed two-stratum clustered survey (B7)",
"R survey package documentation (Lumley 2024)",
"Constructed validation data; no third-party license.",
c(h = "String", psu = "String", w = "Float64", N = "Int64", x = "Float64",
y = "Float64", z = "Float64", g = "String", b = "String", dom = "String",
rw1 = "Float64", rw2 = "Float64", rw3 = "Float64", rw4 = "Float64"))
d <- read_dataset("DESC_SURVEY_B7")
taylor <- svydesign(ids = ~psu, strata = ~h, weights = ~w, data = d)
with_fpc <- svydesign(ids = ~psu, strata = ~h, weights = ~w, fpc = ~N, data = d)
REP_SCALE <- 0.75
replicate <- svrepdesign(weights = ~w, repweights = d[, c("rw1", "rw2", "rw3", "rw4")],
type = "other", scale = REP_SCALE, rscales = rep(1, 4),
mse = TRUE, data = d, combined.weights = TRUE)
design_taylor <- list(weight = "w", variance = "linearization",
stage1 = list(strata = "h", units = "psu"))
design_fpc <- list(weight = "w", variance = "linearization",
stage1 = list(strata = "h", units = "psu", fpc = "N", fpc_kind = "count"))
design_rep <- list(weight = "w", variance = "replicate",
replicates = list(columns = list("rw1", "rw2", "rw3", "rw4"),
method = "other", weight_type = "full",
scale = REP_SCALE, rscales = NULL, fay_rho = NULL,
centering = "full_sample"))
ci <- function(point, se, df, level) point + c(-1, 1) * qt(0.5 + level / 2, df) * se
se_of <- function(stat, name = 1) sqrt(as.double(vcov(stat)[name, name]))
estimate_outputs <- function(prefix, point, se, df, level, label) {
bounds <- ci(point, se, df, level)
out <- list()
out[[paste0(prefix, "/estimate")]] <- tol(point, paste(label, "point estimate"))
out[[paste0(prefix, "/standard_error")]] <- tol(se, paste(label, "design SE"))
out[[paste0(prefix, "/lower")]] <- tol(bounds[1], paste(label, "t interval lower"))
out[[paste0(prefix, "/upper")]] <- tol(bounds[2], paste(label, "t interval upper"))
out[[paste0(prefix, "/df")]] <- exact(df, "design degrees of freedom")
out
}
ESTIMATES <- "tabelz.analysis.survey_estimates:compute_survey_estimates"
EST_REQ <- "REQ-STAT-DESC-SURVEY-ESTIMATES-001"
# --- Means, totals and proportions -----------------------------------------------------
# 001: Taylor design; numeric x (two missing rows) and categorical g.
df_t <- as.integer(degf(taylor))
mean_x <- svymean(~x, taylor, na.rm = TRUE)
total_x <- svytotal(~x, taylor, na.rm = TRUE)
prop_g <- svymean(~g, taylor)
count_g <- svytotal(~g, taylor)
outputs <- c(
estimate_outputs("/estimates/0", coef(mean_x)[[1]], se_of(mean_x, 1), df_t, 0.95, "mean of x"),
estimate_outputs("/estimates/1", coef(total_x)[[1]], se_of(total_x, 1), df_t, 0.95, "total of x"),
list("/estimates/0/rows" = exact(sum(!is.na(d$x)), "rows with x observed"),
"/estimates/0/missing_rows" = exact(sum(is.na(d$x)), "rows with x missing"),
"/estimates/1/rows" = exact(sum(!is.na(d$x)), "rows with x observed")),
estimate_outputs("/estimates/2", coef(prop_g)[["ga"]], se_of(prop_g, "ga"), df_t, 0.95, "proportion g = a"),
estimate_outputs("/estimates/3", coef(count_g)[["ga"]], se_of(count_g, "ga"), df_t, 0.95, "count g = a"),
estimate_outputs("/estimates/4", coef(prop_g)[["gb"]], se_of(prop_g, "gb"), df_t, 0.95, "proportion g = b"),
estimate_outputs("/estimates/7", coef(count_g)[["gc"]], se_of(count_g, "gc"), df_t, 0.95, "count g = c"),
list("/design/design_df" = exact(df_t, "8 PSUs minus 2 strata"),
"/design/psus" = exact(8L, "eight PSUs"),
"/design/strata" = exact(2L, "two strata"))
)
emit("STAT-DESC-SURVEY-EST-001", "Survey mean, total, proportions and counts (Taylor)",
ESTIMATES, "surveyEstimatesMenuItem", EST_REQ,
list(design = design_taylor, numeric_columns = list("x"), categorical_columns = list("g"),
design_effect = "none", bounded_interval = "none", weights_population_scale = FALSE,
confidence_level = 0.95),
outputs,
c("svymean/svytotal with na.rm=TRUE keep the design and drop the missing rows' contribution, as the engine's zero-contribution convention.",
"Estimates are listed numeric columns first (Mean, Total per column), then each categorical level in natural order (Proportion, Count); g levels a, b, c give indices 2-7.",
"Intervals: estimate +/- qt(0.975, D) * SE with D = PSUs - strata = 6."))
# 002: domain dom = A, 90% intervals, numeric y and categorical b.
domain_a <- subset(taylor, dom == "A")
mean_y <- svymean(~y, domain_a)
total_y <- svytotal(~y, domain_a)
prop_b <- svymean(~b, domain_a)
count_b <- svytotal(~b, domain_a)
outputs <- c(
estimate_outputs("/estimates/0", coef(mean_y)[[1]], se_of(mean_y, 1), df_t, 0.90, "domain mean of y"),
estimate_outputs("/estimates/1", coef(total_y)[[1]], se_of(total_y, 1), df_t, 0.90, "domain total of y"),
estimate_outputs("/estimates/2", coef(prop_b)[["bno"]], se_of(prop_b, "bno"), df_t, 0.90, "domain proportion b = no"),
estimate_outputs("/estimates/5", coef(count_b)[["byes"]], se_of(count_b, "byes"), df_t, 0.90, "domain count b = yes"),
list("/estimates/0/rows" = exact(sum(d$dom == "A"), "domain rows"),
"/design/domain_rows" = exact(sum(d$dom == "A"), "domain rows"))
)
emit("STAT-DESC-SURVEY-EST-002", "Survey domain estimates with 90% intervals",
ESTIMATES, "surveyEstimatesMenuItem", EST_REQ,
list(design = design_taylor, numeric_columns = list("y"), categorical_columns = list("b"),
design_effect = "none", bounded_interval = "none", weights_population_scale = FALSE,
domain_column = "dom", domain_values = list("A"), confidence_level = 0.90),
outputs,
c("subset(design, dom == 'A') keeps nondomain PSUs in the variance (R survey domain estimation).",
"90% intervals use qt(0.95, 6)."),
kind = "axis_coverage")
# 003: stratum population counts as a first-stage FPC.
df_f <- as.integer(degf(with_fpc))
mean_yf <- svymean(~y, with_fpc)
total_yf <- svytotal(~y, with_fpc)
outputs <- c(
estimate_outputs("/estimates/0", coef(mean_yf)[[1]], se_of(mean_yf, 1), df_f, 0.95, "FPC mean of y"),
estimate_outputs("/estimates/1", coef(total_yf)[[1]], se_of(total_yf, 1), df_f, 0.95, "FPC total of y")
)
emit("STAT-DESC-SURVEY-EST-003", "Survey mean and total with a first-stage FPC",
ESTIMATES, "surveyEstimatesMenuItem", EST_REQ,
list(design = design_fpc, numeric_columns = list("y"), categorical_columns = list(),
design_effect = "none", bounded_interval = "none", weights_population_scale = FALSE,
confidence_level = 0.95),
outputs,
c("svydesign(fpc = ~N) with N the population count of PSUs per stratum (20 and 30); the (1 - n_h/N_h) factor applies per stratum."),
kind = "axis_coverage")
# 004: supplied replicate weights (type other, scale 0.75, full-sample centring).
df_r <- as.integer(degf(replicate))
mean_yr <- svymean(~y, replicate)
prop_br <- svymean(~b, replicate)
outputs <- c(
estimate_outputs("/estimates/0", coef(mean_yr)[[1]], se_of(mean_yr, 1), df_r, 0.95, "replicate mean of y"),
estimate_outputs("/estimates/2", coef(prop_br)[["bno"]], se_of(prop_br, "bno"), df_r, 0.95, "replicate proportion b = no"),
list("/design/design_df" = exact(df_r, "rank of the four replicate-weight columns minus 1"),
"/design/replicates" = exact(4L, "four replicate columns"))
)
emit("STAT-DESC-SURVEY-EST-004", "Survey mean and proportion from supplied replicate weights",
ESTIMATES, "surveyEstimatesMenuItem", EST_REQ,
list(design = design_rep, numeric_columns = list("y"), categorical_columns = list("b"),
design_effect = "none", bounded_interval = "none", weights_population_scale = FALSE,
confidence_level = 0.95),
outputs,
c("svrepdesign(type = 'other', scale = 0.75, rscales = 1, mse = TRUE, combined.weights = TRUE): V = 0.75 * sum_r (theta_r - theta_full)^2.",
"degf = qr(replicate weights, tol = 1e-5)$rank - 1 = 3."),
kind = "axis_coverage")
# 005: supplementary Korn-Graubard interval and the with-replacement SRS design effect.
kg <- svyciprop(~I(b == "no"), taylor, method = "beta", level = 0.95)
p_no <- as.double(coef(prop_g <- svymean(~b, taylor))[["bno"]])
v_no <- as.double(vcov(prop_g)["bno", "bno"])
n_eff <- p_no * (1 - p_no) / v_no * (qt(0.025, nrow(d) - 1) / qt(0.025, df_t))^2
mean_y_deff <- svymean(~y, taylor, deff = "replace")
outputs <- c(
estimate_outputs("/estimates/2", p_no, sqrt(v_no), df_t, 0.95, "proportion b = no"),
list("/estimates/2/extensions/bounded_lower" = tol(attr(kg, "ci")[[1]], "svyciprop beta (Korn-Graubard) lower limit"),
"/estimates/2/extensions/bounded_upper" = tol(attr(kg, "ci")[[2]], "svyciprop beta (Korn-Graubard) upper limit"),
"/estimates/2/extensions/effective_n" = tol(n_eff, "p(1-p)/V times [qt(alpha/2, n-1)/qt(alpha/2, D)]^2 with n = 24 rows"),
"/estimates/0/extensions/design_effect" = tol(as.double(deff(mean_y_deff)[[1]]), "svymean deff = 'replace' for the mean of y"),
"/estimates/0/extensions/deft_wr" = tol(sqrt(as.double(deff(mean_y_deff)[[1]])), "square root of the with-replacement design effect"))
)
emit("STAT-DESC-SURVEY-EST-005", "Survey proportion with Korn-Graubard limits and SRS design effect",
ESTIMATES, "surveyEstimatesMenuItem", EST_REQ,
list(design = design_taylor, numeric_columns = list("y"), categorical_columns = list("b"),
design_effect = "srs_wr", bounded_interval = "korn_graubard",
weights_population_scale = FALSE, confidence_level = 0.95),
outputs,
c(paste0(MANUAL, ":126-138: Korn-Graubard uses n_e = p(1-p)/V * (t_{n_s-1}/t_D)^2 and beta quantiles; n_s counts domain rows before item missingness."),
paste0(MANUAL, ":144-147: with-replacement SRS comparator divides the normalized weighted dispersion by the observed n - 1 (R svymean deff = 'replace')."),
"Estimates: y Mean (0), y Total (1), b = no Proportion (2), b = no Count (3), b = yes (4, 5)."),
kind = "axis_coverage")
# 006: delta-logit supplementary interval.
xl <- svyciprop(~I(b == "no"), taylor, method = "xlogit", level = 0.95)
emit("STAT-DESC-SURVEY-EST-006", "Survey proportion with a delta-logit interval",
ESTIMATES, "surveyEstimatesMenuItem", EST_REQ,
list(design = design_taylor, numeric_columns = list(), categorical_columns = list("b"),
design_effect = "none", bounded_interval = "delta_logit",
weights_population_scale = FALSE, confidence_level = 0.95),
list("/estimates/0/estimate" = tol(p_no, "proportion b = no"),
"/estimates/0/extensions/bounded_lower" = tol(attr(xl, "ci")[[1]], "svyciprop xlogit lower limit"),
"/estimates/0/extensions/bounded_upper" = tol(attr(xl, "ci")[[2]], "svyciprop xlogit upper limit")),
c(paste0(MANUAL, ":122-125: delta-logit transforms logit(p) +/- t_{D} sqrt(V)/[p(1-p)] back to the probability scale (svyciprop method 'xlogit').")),
kind = "axis_coverage")
# --- Frequencies and crosstabs ----------------------------------------------------------
CROSSTAB <- "tabelz.analysis.survey_tables:compute_survey_crosstab"
XTAB_REQ <- "REQ-STAT-DESC-SURVEY-CROSSTAB-001"
crosstab_outputs <- function(design, df, level, cell_index, g_level, b_level) {
cells <- svymean(~interaction(g, b), design)
counts <- svytotal(~interaction(g, b), design)
name <- paste0("interaction(g, b)", g_level, ".", b_level)
by_row <- svyby(~b, ~g, design, svymean)
by_col <- svyby(~g, ~b, design, svymean)
pct <- 100 * coef(cells)[[name]]
pct_se <- 100 * se_of(cells, name)
bounds <- ci(pct, pct_se, df, level)
prefix <- paste0("/cells/", cell_index)
label <- paste0("cell g = ", g_level, ", b = ", b_level)
out <- list()
out[[paste0(prefix, "/count")]] <- tol(coef(counts)[[name]], paste(label, "estimated count"))
out[[paste0(prefix, "/count_se")]] <- tol(se_of(counts, name), paste(label, "count SE"))
out[[paste0(prefix, "/percent")]] <- tol(pct, paste(label, "percent of total"))
out[[paste0(prefix, "/percent_se")]] <- tol(pct_se, paste(label, "percent SE"))
out[[paste0(prefix, "/percent_lower")]] <- tol(bounds[1], paste(label, "percent t lower"))
out[[paste0(prefix, "/percent_upper")]] <- tol(bounds[2], paste(label, "percent t upper"))
out[[paste0(prefix, "/row_percent")]] <- tol(100 * by_row[g_level, paste0("b", b_level)], paste(label, "row percent (svyby ~b by g)"))
out[[paste0(prefix, "/row_percent_se")]] <- tol(100 * by_row[g_level, paste0("se.b", b_level)], paste(label, "row percent SE"))
out[[paste0(prefix, "/column_percent")]] <- tol(100 * by_col[b_level, paste0("g", g_level)], paste(label, "column percent (svyby ~g by b)"))
out[[paste0(prefix, "/column_percent_se")]] <- tol(100 * by_col[b_level, paste0("se.g", g_level)], paste(label, "column percent SE"))
out
}
rao_scott_outputs <- function(design, n) {
test <- svychisq(~g + b, design, statistic = "F")
pearson <- chisq.test(svytable(~g + b, design, Ntotal = n), correct = FALSE)
list("/test/f_statistic" = tol(test$statistic, "svychisq second-order Rao-Scott F"),
"/test/numerator_df" = tol(test$parameter[["ndf"]], "svychisq numerator df"),
"/test/denominator_df" = tol(test$parameter[["ddf"]], "svychisq denominator df"),
"/test/p_value" = tail(test$p.value, "svychisq F p-value"),
"/test/pearson" = tol(pearson$statistic, "Pearson X2 of the weighted table scaled to the analysis rows"),
"/test/contingency_df" = exact((3 - 1) * (2 - 1), "(r-1)(c-1)"))
}
# 001: g by b crosstab with the Rao-Scott test (Taylor).
outputs <- c(crosstab_outputs(taylor, df_t, 0.95, 0, "a", "no"),
crosstab_outputs(taylor, df_t, 0.95, 3, "b", "yes"),
rao_scott_outputs(taylor, nrow(d)),
list("/analysis_rows" = exact(nrow(d), "every row has g and b"),
"/cells/0/rows" = exact(sum(d$g == "a" & d$b == "no"), "unweighted rows in cell a/no")))
emit("STAT-DESC-SURVEY-XTAB-001", "Survey crosstab with the second-order Rao-Scott F",
CROSSTAB, "surveyCrosstabMenuItem", XTAB_REQ,
list(design = design_taylor, row_column = "g", column_column = "b", confidence_level = 0.95),
outputs,
c(paste0(MANUAL, ":73-75: survey crosstabs use design-adjusted Rao-Scott inference; the ordinary Pearson chi-square is an unadjusted comparison."),
"Cells are row-major over row levels a, b, c and column levels no, yes: index = row * 2 + column.",
"svychisq(statistic = 'F'): F = X2 / tr(Delta) on (d0, d0 * D) with d0 = tr(Delta)^2 / tr(Delta^2)."))
# 002: one-way frequencies of g in domain B (no test).
domain_b <- subset(taylor, dom == "B")
freq <- svymean(~g, domain_b)
cnt <- svytotal(~g, domain_b)
outputs <- list()
for (index in 0:2) {
level <- c("a", "b", "c")[index + 1]
name <- paste0("g", level)
pct <- 100 * coef(freq)[[name]]
pct_se <- 100 * se_of(freq, name)
bounds <- ci(pct, pct_se, df_t, 0.95)
outputs[[sprintf("/cells/%d/count", index)]] <- tol(coef(cnt)[[name]], paste("domain count g =", level))
outputs[[sprintf("/cells/%d/count_se", index)]] <- tol(se_of(cnt, name), paste("domain count SE g =", level))
outputs[[sprintf("/cells/%d/percent", index)]] <- tol(pct, paste("domain percent g =", level))
outputs[[sprintf("/cells/%d/percent_se", index)]] <- tol(pct_se, paste("domain percent SE g =", level))
outputs[[sprintf("/cells/%d/percent_lower", index)]] <- tol(bounds[1], paste("domain percent lower g =", level))
outputs[[sprintf("/cells/%d/percent_upper", index)]] <- tol(bounds[2], paste("domain percent upper g =", level))
}
outputs[["/analysis_rows"]] <- exact(sum(d$dom == "B"), "domain rows")
outputs[["/cells/0/rows"]] <- exact(sum(d$dom == "B" & d$g == "a"), "unweighted domain rows with g = a")
emit("STAT-DESC-SURVEY-XTAB-002", "Survey one-way frequencies in a domain",
CROSSTAB, "surveyCrosstabMenuItem", XTAB_REQ,
list(design = design_taylor, row_column = "g", domain_column = "dom",
domain_values = list("B"), confidence_level = 0.95),
outputs,
c("One column only: cells are the levels of g in natural order; no test of independence.",
"subset(design, dom == 'B') keeps the whole design for the variance."),
kind = "axis_coverage")
# 003: crosstab under the FPC design with 90% intervals.
outputs <- c(crosstab_outputs(with_fpc, df_f, 0.90, 1, "a", "yes"),
crosstab_outputs(with_fpc, df_f, 0.90, 4, "c", "no"),
rao_scott_outputs(with_fpc, nrow(d)))
emit("STAT-DESC-SURVEY-XTAB-003", "Survey crosstab with a first-stage FPC and 90% intervals",
CROSSTAB, "surveyCrosstabMenuItem", XTAB_REQ,
list(design = design_fpc, row_column = "g", column_column = "b", confidence_level = 0.90),
outputs,
c("svydesign(fpc = ~N): finite population corrections enter the cell covariance and so the Rao-Scott design effects.",
"90% percent intervals use qt(0.95, 6)."),
kind = "axis_coverage")
# --- Ratios -------------------------------------------------------------------------------
RATIOS <- "tabelz.analysis.survey_estimates:compute_survey_ratios"
RATIO_REQ <- "REQ-STAT-DESC-SURVEY-RATIOS-001"
ratio_outputs <- function(index, design, numerator, df, level, label) {
ratio <- svyratio(as.formula(paste0("~", numerator)), ~x, design, na.rm = TRUE)
point <- as.double(coef(ratio)[[1]])
se <- as.double(se_of(ratio, 1))
bounds <- ci(point, se, df, level)
prefix <- paste0("/ratios/", index)
out <- list()
out[[paste0(prefix, "/ratio")]] <- tol(point, paste(label, "ratio of weighted totals"))
out[[paste0(prefix, "/standard_error")]] <- tol(se, paste(label, "linearized ratio SE"))
out[[paste0(prefix, "/lower")]] <- tol(bounds[1], paste(label, "t interval lower"))
out[[paste0(prefix, "/upper")]] <- tol(bounds[2], paste(label, "t interval upper"))
out[[paste0(prefix, "/df")]] <- exact(df, "design degrees of freedom")
out
}
# 001: y / x over the rows where both are observed (two rows missing x).
outputs <- c(ratio_outputs(0, taylor, "y", df_t, 0.95, "y / x"),
list("/ratios/0/rows" = exact(sum(!is.na(d$x)), "rows with both observed"),
"/ratios/0/missing_rows" = exact(sum(is.na(d$x)), "rows with x missing")))
emit("STAT-DESC-SURVEY-RATIO-001", "Survey ratio of weighted totals (Taylor)",
RATIOS, "surveyRatiosMenuItem", RATIO_REQ,
list(design = design_taylor, numerators = list("y"), denominator = "x", confidence_level = 0.95),
outputs,
c("svyratio(~y, ~x, na.rm = TRUE): rows with a missing x stay in the design with zero contribution.",
"Linearized contribution w (y - R x) / sum(w x); interval ratio +/- qt(0.975, 6) * SE."))
# 002: two numerators in domain A with 90% intervals.
outputs <- c(ratio_outputs(0, domain_a, "y", df_t, 0.90, "domain y / x"),
ratio_outputs(1, domain_a, "z", df_t, 0.90, "domain z / x"),
list("/ratios/1/rows" = exact(sum(d$dom == "A" & !is.na(d$x)), "domain rows with x observed")))
emit("STAT-DESC-SURVEY-RATIO-002", "Survey ratios of two numerators in a domain",
RATIOS, "surveyRatiosMenuItem", RATIO_REQ,
list(design = design_taylor, numerators = list("y", "z"), denominator = "x",
domain_column = "dom", domain_values = list("A"), confidence_level = 0.90),
outputs,
c("Ratios are listed in numerator order (y then z); subset(design, dom == 'A') keeps the design."),
kind = "axis_coverage")
# 003: ratio under supplied replicate weights.
outputs <- ratio_outputs(0, replicate, "y", df_r, 0.95, "replicate y / x")
emit("STAT-DESC-SURVEY-RATIO-003", "Survey ratio from supplied replicate weights",
RATIOS, "surveyRatiosMenuItem", RATIO_REQ,
list(design = design_rep, numerators = list("y"), denominator = "x", confidence_level = 0.95),
outputs,
c("svyratio on svrepdesign(type = 'other', scale = 0.75, mse = TRUE): the ratio is recomputed under each replicate weight column."),
kind = "axis_coverage")
cat("descriptives survey estimates, crosstab and ratio references generated\n")
generators/r/descriptives_tabulation.R
source("common.R", local = TRUE)
run <- generator_run(c("stats", "jsonlite"))
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Closed-form R reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(id, title, dataset, config, outputs, notes, kind = "nominal",
excluded = list(), version = "1.0.0") {
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = version,
family = "descriptives", entry_point = "tabelz.executor.tabulation",
menu_items = list("tabulationMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats",
citation = "R stats::quantile and stats::t.test documentation",
generator = list(path = "generators/r/descriptives_tabulation.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "All reference calculations re-read the exported CSV.")
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
))
}
summary_outputs <- function(values, prefix = "/numeric_summaries/0", confidence = 0.95) {
values <- values[!is.na(values)]
n <- length(values)
out <- list()
out[[paste0(prefix, "/valid_n")]] <- exact(n, "Replicated valid count is exact.")
out[[paste0(prefix, "/mean")]] <- tol(mean(values), "Arithmetic mean of valid values; owner 1e-9 tier.")
out[[paste0(prefix, "/median")]] <- tol(quantile(values, 0.5, type = 7), "Type-7 median; owner 1e-9 tier.")
out[[paste0(prefix, "/first_quartile")]] <- tol(quantile(values, 0.25, type = 7), "Type-7 first quartile; owner 1e-9 tier.")
out[[paste0(prefix, "/third_quartile")]] <- tol(quantile(values, 0.75, type = 7), "Type-7 third quartile; owner 1e-9 tier.")
if (n > 1L) {
out[[paste0(prefix, "/standard_deviation")]] <- tol(sd(values), "Sample SD divides by n-1; owner 1e-9 tier.")
out[[paste0(prefix, "/standard_error")]] <- tol(sd(values) / sqrt(n), "SE is sample SD divided by sqrt(n); owner 1e-9 tier.")
interval <- t.test(values, conf.level = confidence)$conf.int
out[[paste0(prefix, "/confidence_lower")]] <- tol(interval[1], "Two-sided t mean interval lower bound; owner 1e-9 tier.")
out[[paste0(prefix, "/confidence_upper")]] <- tol(interval[2], "Two-sided t mean interval upper bound; owner 1e-9 tier.")
} else {
out[[paste0(prefix, "/standard_deviation")]] <- absent("Sample SD is undefined for one value.")
out[[paste0(prefix, "/confidence_lower")]] <- absent("A one-value t interval is undefined.")
}
out
}
raw <- data.frame(
value = c(1, 2, 3, 4, 5, 6, 7, 8, NA, 9),
group = c(rep("A", 5), rep("B", 5)),
row = c("X", "X", "Y", "Y", "X", "Y", "Y", "X", "X", "Y"),
column = c("L", "R", "L", "R", "L", "R", "L", "R", "L", "R"),
frequency = as.integer(c(1, 2, 0, 1, 2, 1, 2, 1, 1, 3))
)
export_dataset(raw, "DESC_TABULATION_01", "Constructed grouped and categorical table",
"R stats::quantile and stats::t.test documentation",
"Constructed validation data; no third-party license.",
c(value = "Float64", group = "String", row = "String",
column = "String", frequency = "Int64"))
d <- read_dataset("DESC_TABULATION_01")
out <- summary_outputs(d$value)
out[["/source_count"]] <- exact(nrow(d), "All ten source rows are counted.")
out[["/missing_count"]] <- exact(sum(is.na(d$value)), "One response is missing.")
emit("STAT-DESC-TABNUM-001", "Ungrouped numeric summaries", "DESC_TABULATION_01",
list(kind = "numeric", value_column = "value"), out,
c("Manual chapter 29 lines 24-38: sample SD, type-7 quantiles and two-sided t interval."))
out <- summary_outputs(d$value[d$group == "A"])
out[["/numeric_summaries/1/mean"]] <- tol(mean(d$value[d$group == "B"], na.rm = TRUE),
"Second group mean; owner 1e-9 tier.")
out[["/numeric_summaries/1/missing_n"]] <- exact(1L, "The B group has one missing response.")
emit("STAT-DESC-TABNUM-002", "Two ordered groups", "DESC_TABULATION_01",
list(kind = "numeric", value_column = "value", group_columns = list("group")), out,
c("Group labels are A then B in the manual's sorted string order."))
replicated <- rep(d$value[!is.na(d$value)], d$frequency[!is.na(d$value)])
out <- summary_outputs(replicated)
out[["/numeric_summaries/0/count"]] <- exact(nrow(d), "Source rows include zero-frequency and missing rows.")
emit("STAT-DESC-TABNUM-003", "Frequency-weighted order statistics", "DESC_TABULATION_01",
list(kind = "numeric", value_column = "value", frequency_column = "frequency"), out,
c("Manual chapter 29 lines 28-36: integer frequencies are replication counts; zero-frequency rows remain in source count."))
retained <- d[-c(2, 9), ]
out <- summary_outputs(retained$value)
out[["/excluded_count"]] <- exact(2L, "Two zero-based source row indexes are excluded.")
out[["/missing_count"]] <- exact(0L, "The only missing response is excluded.")
emit("STAT-DESC-TABNUM-004", "Explicit row exclusions and missingness", "DESC_TABULATION_01",
list(kind = "numeric", value_column = "value"), out,
c("Excluded rows are zero-based positions 1 and 8 and contribute to neither valid nor missing N."),
kind = "missing_excluded", excluded = list(1L, 8L))
one <- data.frame(value = 7)
export_dataset(one, "DESC_TABULATION_ONE", "Constructed one-row boundary",
"R stats::quantile documentation", "Constructed validation data; no third-party license.",
c(value = "Float64"))
one <- read_dataset("DESC_TABULATION_ONE")
emit("STAT-DESC-TABNUM-005", "One valid value", "DESC_TABULATION_ONE",
list(kind = "numeric", value_column = "value"), summary_outputs(one$value),
c("For n=1, the mean and quantiles are seven; sample SD and t interval are unavailable."),
kind = "boundary")
hard <- data.frame(value = 1e9 + c(-3, -2, -1, 0, 1, 2, 3))
export_dataset(hard, "DESC_TABULATION_OFFSET", "Constructed large-offset small-spread values",
"R stats::sd and stats::quantile documentation",
"Constructed validation data; no third-party license.", c(value = "Float64"))
hard <- read_dataset("DESC_TABULATION_OFFSET")
emit("STAT-DESC-TABNUM-006", "Large offset and small spread", "DESC_TABULATION_OFFSET",
list(kind = "numeric", value_column = "value"), summary_outputs(hard$value),
c("Seven values lie at 1e9 plus -3,-2,-1,0,1,2,3; mean is 1e9 and sample SD is sqrt(28/6)."),
kind = "hard_numeric")
counts <- table(factor(d$row, levels = c("X", "Y")), factor(d$column, levels = c("L", "R")))
total <- sum(counts)
out <- list(
"/valid_n" = exact(total, "Ten rows have complete categorical roles."),
"/cells/0/count" = exact(counts["X", "L"], "X/L count is exact."),
"/cells/0/row_total" = exact(sum(counts["X", ]), "X row margin is exact."),
"/cells/0/column_total" = exact(sum(counts[, "L"]), "L column margin is exact."),
"/cells/0/overall_total" = exact(total, "Grand total is exact."),
"/cells/0/row_percentage" = tol(100 * counts["X", "L"] / sum(counts["X", ]),
"Cell / row total in percent units; owner 1e-9 tier."),
"/cells/0/column_percentage" = tol(100 * counts["X", "L"] / sum(counts[, "L"]),
"Cell / column total in percent units; owner 1e-9 tier."),
"/cells/0/overall_percentage" = tol(100 * counts["X", "L"] / total,
"Cell / grand total in percent units; owner 1e-9 tier.")
)
emit("STAT-DESC-TABCAT-001", "Two-way counts and all percentage denominators",
"DESC_TABULATION_01", list(kind = "categorical", row_column = "row",
column_column = "column", row_percentages = TRUE, column_percentages = TRUE,
overall_percentages = TRUE, include_margins = TRUE), out,
c("Cells are ordered by observed X,Y and L,R labels; percentage outputs are in percent units (100 times the cell fraction)."),
version = "2.0.0")
out <- list(
"/valid_n" = exact(nrow(d), "Every row has a row category."),
"/cells/0/count" = exact(sum(d$row == "X"), "One-way X count is exact."),
"/cells/0/row_percentage" = absent("Row percentages are disabled."),
"/cells/0/column_percentage" = absent("Column percentages are disabled.")
)
emit("STAT-DESC-TABCAT-002", "One-way table without margins", "DESC_TABULATION_01",
list(kind = "categorical", row_column = "row", include_margins = FALSE), out,
c("The one-way table counts each observed category once and omits margins."))
cat("descriptives tabulation references generated\n")
generators/r/design_doe.R
source("common.R", local = TRUE)
# References for the Design of experiments workflow (manual chapter 13 lines
# 19-173). Numeric Low/High are coded -1/+1 (line 33-34); factorial models use
# coded main and two-factor terms; an effect is twice its coded coefficient;
# binary responses fit a binomial logit on the same terms (line 109-116); the
# variability model regresses log sample variances on the design terms
# (line 122-132) - with equal replication per setting its slopes do not depend
# on the unstated weights or bias correction, so only slopes are asserted.
# Generated designs are compared as sorted design points, never in run order.
run <- generator_run(c("stats", "jsonlite"))
family <- "design"
generator <- "generators/r/design_doe.R"
menu <- "doeMenuItem"
tol <- function(value, why, tier = "1e-9") list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste(if (tier == "1e-9") "Closed-form least squares; owner 1e-9 tier." else "Iterative GLM; owner 1e-6 tier.", why))
tail_p <- function(value, why, tier = "1e-9") list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = tier, rationale = paste("R p-value.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
points_of <- function(m) list(comparison = "sequence", values = as.list(sort(apply(m, 1, function(r) paste(sprintf("%.12g", r), collapse = "|")), method = "radix")),
rationale = "Sorted factor settings; run order is randomized.")
emit <- function(id, entry, title, dataset, config, outputs, notes, kind = "nominal", error = NULL, deferred = list()) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = deferred, generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = if (is.null(dataset)) NULL else list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats::lm and stats::glm on coded factors; enumerated design points",
citation = "R stats::lm and stats::glm documentation; coding rules from manual chapter 13",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-DOE-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
corners <- function(lows, highs) as.matrix(expand.grid(lapply(seq_along(lows), function(i) c(lows[i], highs[i]))))
# ---- generated designs ------------------------------------------------------------------
create <- "tabelz.analysis.doe:create_factorial_design"
lows <- c(10, 1, 100); highs <- c(20, 3, 200)
pts <- rbind(corners(lows, highs), corners(lows, highs), matrix(rep((lows + highs) / 2, 3), 3, byrow = TRUE))
emit("STAT-DOE-CREATE-001", create, "Full 2^3 factorial with two replicates and three centre points", NULL,
list(factors = list(list(name = "A", low = 10, high = 20), list(name = "B", low = 1, high = 3), list(name = "C", low = 100, high = 200)),
replicates = 2L, center_points = 3L, seed = 7L),
list("/runs" = exact(19L, "8 corners x 2 replicates + 3 centres."), "/design_points" = points_of(pts)),
c("Each corner appears twice; centres sit at each factor's midpoint."))
emit("STAT-DOE-CREATE-002", create, "The smallest design: two factors, one replicate", NULL,
list(factors = list(list(name = "A", low = 0, high = 1), list(name = "B", low = -5, high = 5)), randomize = FALSE),
list("/runs" = exact(4L, "Four corners."), "/design_points" = points_of(corners(c(0, -5), c(1, 5)))),
c("Randomization off."), kind = "boundary")
emit("STAT-DOE-CREATE-003", create, "A factor with low above high is refused", NULL,
list(factors = list(list(name = "A", low = 5, high = 1), list(name = "B", low = 0, high = 1))), list(),
c("Low and High must be ordered."), kind = "invalid_input", error = list(error_class = "ValueError", message_contains = "A"))
catalog <- "tabelz.analysis.doe_designs:generate_catalog_design"
emit("STAT-DOE-CATALOG-001", catalog, "Catalog full factorial with two centre points", NULL,
list(family = "full_factorial", factors = list(list(name = "X", low = 0, high = 10), list(name = "Y", low = 50, high = 70)), center_points = 2L),
list("/runs" = exact(6L, "4 corners + 2 centres."),
"/design_points" = points_of(rbind(corners(c(0, 50), c(10, 70)), c(5, 60), c(5, 60)))), c("Catalog family full_factorial."))
face <- rbind(corners(c(-1, -1), c(1, 1)), c(-1, 0), c(1, 0), c(0, -1), c(0, 1), c(0, 0), c(0, 0), c(0, 0))
emit("STAT-DOE-CATALOG-002", catalog, "Face-centred central composite for two factors", NULL,
list(family = "central_composite", factors = list(list(name = "X", low = -1, high = 1), list(name = "Y", low = -1, high = 1)),
center_points = 3L, axial_type = "face_centered"),
list("/runs" = exact(11L, "4 corners + 4 face points + 3 centres."), "/design_points" = points_of(face)),
c("Face-centred axial points sit at +/-1 on one factor with the other at 0."), kind = "axis_coverage")
# ---- analysis of a replicated 2^3 run sheet ------------------------------------------------
set.seed(20261101)
design <- rbind(corners(lows, highs), corners(lows, highs))
colnames(design) <- c("A", "B", "C")
code <- function(x, lo, hi) (x - (lo + hi) / 2) / ((hi - lo) / 2)
ca <- code(design[, 1], 10, 20); cb <- code(design[, 2], 1, 3); cc <- code(design[, 3], 100, 200)
y <- round(50 + 4 * ca - 2.5 * cb + 1.2 * cc + 1.5 * ca * cb + rnorm(16, 0, 0.8), 4)
yield2 <- round(20 + 3 * ca + 2 * cb + 1 * cc + rnorm(16, 0, 0.5), 4)
yield3 <- round(5 + 1.5 * ca + 1 * cb + 0.8 * cc + rnorm(16, 0, 0.3), 4)
export_dataset(data.frame(A = design[, 1], B = design[, 2], C = design[, 3], y = y, y2 = yield2, y3 = yield3),
"DOE_FACTORIAL", "Constructed replicated 2^3 run sheet with three responses, seed 20261101",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(A = "Float64", B = "Float64", C = "Float64", y = "Float64", y2 = "Float64", y3 = "Float64"))
d <- read_dataset("DOE_FACTORIAL")
d$a <- code(d$A, 10, 20); d$b <- code(d$B, 1, 3); d$c <- code(d$C, 100, 200)
fit <- lm(y ~ (a + b + c)^2, data = d); s <- summary(fit)$coefficients; ci <- confint(fit)
term_out <- function(prefix, map, sep) {
out <- list()
for (i in seq_along(map)) {
r <- names(map)[i]; t <- map[[i]]; node <- sprintf("%s[term=%s]", prefix, t)
out[[paste0(node, "/coefficient")]] <- tol(s[r, 1], "Coded coefficient.")
out[[paste0(node, "/effect")]] <- tol(2 * s[r, 1], "Effect = 2 x coded coefficient.")
out[[paste0(node, "/std_error")]] <- tol(s[r, 2], "OLS standard error.")
out[[paste0(node, "/p_value")]] <- tail_p(s[r, 4], "t test on the residual df.")
}
out
}
map <- c(a = "A", b = "B", c = "C", "a:b" = "A x B", "a:c" = "A x C", "b:c" = "B x C")
existing <- list(factors = list(list(name = "A", low = 10, high = 20), list(name = "B", low = 1, high = 3), list(name = "C", low = 100, high = 200)),
response_name = "y", model = "interactions")
emit("STAT-DOE-ANALYZE-001", "tabelz.analysis.doe_advanced:analyze_catalog_design", "Existing-table factorial with two-factor interactions", "DOE_FACTORIAL",
list(response = "y", design = existing),
c(term_out("/payload/terms/", map, " x "),
list("/payload/terms/[term=A]/lower_ci" = tol(ci["a", 1], "t interval lower limit."),
"/payload/terms/[term=A]/upper_ci" = tol(ci["a", 2], "t interval upper limit."))),
c("lm(y ~ (a + b + c)^2) on factors coded to -1/+1 from the declared Low/High (line 33-34)."))
fit3 <- lm(y ~ a + b + c, data = d); s3 <- summary(fit3)$coefficients
emit("STAT-DOE-ANALYZE-002", "tabelz.analysis.doe_advanced:analyze_catalog_design", "Main-effects model at 90% confidence", "DOE_FACTORIAL",
list(response = "y", design = modifyList(existing, list(model = "main_effects")), confidence_level = 0.9),
list("/payload/terms/[term=B]/coefficient" = tol(s3["b", 1], "Main-effects coefficient."),
"/payload/terms/[term=B]/lower_ci" = tol(confint(fit3, level = 0.9)["b", 1], "90% t interval.")),
c("model = main_effects."), kind = "axis_coverage")
legacy_map <- c(a = "A", b = "B", c = "C", "a:b" = "A × B")
emit("STAT-DOE-LEGACY-001", "tabelz.analysis.doe:analyze_factorial", "Legacy factorial analysis with interactions", "DOE_FACTORIAL",
list(response = "y", factors = list("A", "B", "C")),
term_out("/terms/", legacy_map, " × "),
c("The legacy analysis codes each factor from its observed minimum and maximum, which here equal the declared levels; interactions are named with a multiplication sign."))
# ---- binary response ----------------------------------------------------------------------
set.seed(20261102)
db <- data.frame(A = rep(design[, 1], 3), B = rep(design[, 2], 3), C = rep(design[, 3], 3))
eta <- -0.3 + 0.9 * code(db$A, 10, 20) - 0.6 * code(db$B, 1, 3) + 0.4 * code(db$C, 100, 200)
db$event <- as.integer(runif(nrow(db)) < plogis(eta))
export_dataset(db, "DOE_BINARY", "Constructed 0/1 outcomes on the 2^3 design, six runs per corner, seed 20261102",
"R stats::runif documentation", "Constructed validation data; no third-party license.",
c(A = "Float64", B = "Float64", C = "Float64", event = "Int64"))
b <- read_dataset("DOE_BINARY")
b$a <- code(b$A, 10, 20); b$b <- code(b$B, 1, 3); b$c <- code(b$C, 100, 200)
g <- glm(event ~ a + b + c, family = binomial(), data = b, control = glm.control(epsilon = 1e-14, maxit = 100))
gs <- summary(g)$coefficients
bin_out <- list()
for (r in c("a", "b", "c")) {
t <- toupper(r); node <- sprintf("/payload/terms/[term=%s]", t)
bin_out[[paste0(node, "/coefficient")]] <- tol(gs[r, 1], "Logit coefficient on coded factors.", "1e-6")
bin_out[[paste0(node, "/std_error")]] <- tol(gs[r, 2], "Wald standard error.", "1e-6")
bin_out[[paste0(node, "/p_value")]] <- tail_p(gs[r, 4], "Wald z test.", "1e-6")
}
emit("STAT-DOE-BINARY-001", "tabelz.analysis.doe_binomial:analyze_doe_binomial", "Binary response with a logit link", "DOE_BINARY",
list(response = "event", design = modifyList(existing, list(response_name = "event", model = "main_effects", response_kind = "binary"))),
bin_out, c("glm(binomial) with epsilon 1e-14; one trial per run (line 110-111)."))
gp <- glm(event ~ a + b + c, family = binomial(link = "probit"), data = b, control = glm.control(epsilon = 1e-14, maxit = 100))
emit("STAT-DOE-BINARY-002", "tabelz.analysis.doe_binomial:analyze_doe_binomial", "Binary response with a probit link", "DOE_BINARY",
list(response = "event", design = modifyList(existing, list(response_name = "event", model = "main_effects", response_kind = "binary", link = "probit"))),
list("/payload/terms/[term=A]/coefficient" = tol(coef(gp)[["a"]], "Probit coefficient.", "1e-6")),
c("Explicit probit link (line 113-114)."), kind = "axis_coverage")
# ---- variability ----------------------------------------------------------------------------
set.seed(20261103)
dv <- data.frame(A = rep(c(10, 20, 10, 20), each = 5), B = rep(c(1, 1, 3, 3), each = 5))
sdv <- exp(0.2 + 0.35 * code(dv$A, 10, 20) - 0.25 * code(dv$B, 1, 3))
dv$y <- round(30 + rnorm(20, 0, sdv), 4)
export_dataset(dv, "DOE_VARIABILITY", "Constructed 2^2 design with five runs per setting and a settings-dependent SD, seed 20261103",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(A = "Float64", B = "Float64", y = "Float64"))
v <- read_dataset("DOE_VARIABILITY")
agg <- aggregate(y ~ A + B, data = v, FUN = var)
agg$a <- code(agg$A, 10, 20); agg$b <- code(agg$B, 1, 3)
lv <- coef(lm(log(y) ~ a + b, data = agg))
emit("STAT-DOE-VAR-001", "tabelz.analysis.doe_dispersion:analyze_doe_variability", "Log-variance model slopes with equal replication", "DOE_VARIABILITY",
list(response = "y", design = list(factors = list(list(name = "A", low = 10, high = 20), list(name = "B", low = 1, high = 3)),
response_name = "y", model = "main_effects")),
list("/payload/variability/terms/[term=A]/coefficient" = tol(lv[["a"]], "Slope of log s^2 on coded A."),
"/payload/variability/terms/[term=B]/coefficient" = tol(lv[["b"]], "Slope of log s^2 on coded B.")),
c("Five runs at every setting: equal df, so weighting and the digamma bias correction shift only the intercept."),
deferred = list(list(path = "/payload/variability/terms/[term=Intercept]/coefficient",
reason = "The manual does not give the bias correction or weights that set the intercept.")))
# ---- multiple-response optimization and augmentation --------------------------------------
cat_design <- list(family = "full_factorial", factors = list(list(name = "A", low = 10, high = 20), list(name = "B", low = 1, high = 3),
list(name = "C", low = 100, high = 200)), replicates = 2L)
vertices <- expand.grid(a = c(-1, 1), b = c(-1, 1), c = c(-1, 1))
for (resp in c("y2", "y3")) {
pred <- predict(lm(as.formula(paste(resp, "~ (a + b + c)^2")), data = d), newdata = vertices)
if (which.max(pred) != 8L) stop("expected the high corner to be best for ", resp)
}
sheet <- read_dataset("DOE_FACTORIAL")
export_dataset(data.frame(RunOrder = seq_len(16), StdOrder = seq_len(16), Block = rep(1L, 16), PointType = rep("Factorial", 16),
A = sheet$A, B = sheet$B, C = sheet$C, y2 = sheet$y2, y3 = sheet$y3),
"DOE_FACTORIAL_SHEET", "DOE_FACTORIAL's responses on a catalog run sheet with bookkeeping columns",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(RunOrder = "Int64", StdOrder = "Int64", Block = "Int64", PointType = "String", A = "Float64", B = "Float64",
C = "Float64", y2 = "Float64", y3 = "Float64"))
emit("STAT-DOE-OPT-001", "tabelz.analysis.doe_advanced:optimize_multiple_responses", "Two maximized responses that both peak at the high corner", "DOE_FACTORIAL_SHEET",
list(design = cat_design, goals = list(list(response = "y2", goal = "maximize", weight = 1),
list(response = "y3", goal = "maximize", weight = 1))),
list("/settings/[factor=A]/value" = tol(20, "A at its high level."),
"/settings/[factor=B]/value" = tol(3, "B at its high level."),
"/settings/[factor=C]/value" = tol(200, "C at its high level.")),
c("Under the interaction model both predictions are multilinear, so their maxima lie at vertices; R confirms the high corner is the best vertex for each, so any weighted geometric desirability of two increasing goals peaks there (line 101-105)."),
kind = "axis_coverage")
aug <- data.frame(RunOrder = 1:4, StdOrder = 1:4, Block = rep(1L, 4), PointType = rep("Factorial", 4),
X = c(0, 10, 0, 10), Y = c(50, 50, 70, 70), y = c(3.1, 4.2, 3.9, 5.3))
export_dataset(aug, "DOE_AUGMENT", "Constructed executed 2^2 run sheet with bookkeeping columns", "Hand-constructed",
"Constructed validation data; no third-party license.",
c(RunOrder = "Int64", StdOrder = "Int64", Block = "Int64", PointType = "String", X = "Float64", Y = "Float64", y = "Float64"))
aug_design <- list(family = "full_factorial", factors = list(list(name = "X", low = 0, high = 10), list(name = "Y", low = 50, high = 70)),
response_name = "y")
emit("STAT-DOE-AUGMENT-001", "tabelz.analysis.doe_augmentation:plan_design_augmentation", "Append two centre points", "DOE_AUGMENT",
list(design = aug_design, kind = "center", center_count = 2L),
list("/added_runs" = exact(2L, "Two appended centre runs."),
"/frame/4/X" = tol(5, "Centre of X."), "/frame/5/Y" = tol(60, "Centre of Y."),
"/frame/0/y" = tol(3.1, "The original runs and responses are unchanged (line 148-151).")),
c("Centre points sit at each factor's midpoint (line 156-157)."), kind = "nominal")
emit("STAT-DOE-AUGMENT-002", "tabelz.analysis.doe_augmentation:plan_design_augmentation", "Zero centre points is an explicit no-op", "DOE_AUGMENT",
list(design = aug_design, kind = "center", center_count = 0L),
list("/no_op" = exact(TRUE, "Line 157-158: requesting zero centre points is an explicit no-op.")),
c("Nothing is appended."), kind = "boundary")
generators/r/design_planning.R
source("common.R", local = TRUE)
# References for two parameter-only planners:
# tabelz.executor.extended_power (manual chapter 36): noncentral t for raw
# means (independent: n1 = n, n2 = ceiling(n ratio), pooled df), normal Z,
# noncentral F with Cohen's f = sqrt(sum n_j (mu_j - mubar)^2 / N) / s and
# lambda = f^2 N, the chi-square variance test under a true ratio, integer
# Poisson cutoffs with each null tail at most alpha, and the arcsine effect
# h = 2 (asin sqrt p - asin sqrt p0) for proportions; sample sizes are the
# smallest n reaching the target power.
# tabelz.executor.reliability_planning (chapter 58 lines 56-92): smallest n
# with P(X <= f) <= 1 - C for X ~ Binomial(n, 1 - R0); duration conversion
# R(t) = R0^((t/t0)^beta); failure-terminated T_req = theta0 / 2 chi2_(C, 2(f+1))
# with theta0 = -t0 / ln R0.
run <- generator_run(c("stats", "jsonlite"))
family <- "design"
generator <- "generators/r/design_planning.R"
tol <- function(value, why) list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form distribution function; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
emit <- function(id, entry, menu, title, config, outputs, notes, kind = "nominal", error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind, dataset = NULL, excluded_rows = list(),
configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats noncentral t, F, chi-square, binomial and Poisson functions",
citation = "R stats::TDist, stats::FDist, stats::Chisquare, stats::Binomial and stats::Poisson documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE, convention_notes = as.list(notes)),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-DOE-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
pw <- "tabelz.executor.extended_power"; pmenu <- "extendedPowerMenuItem"
t_power <- function(d, n1, n2 = NULL, alpha = 0.05) {
if (is.null(n2)) { df <- n1 - 1; ncp <- d * sqrt(n1) } else { df <- n1 + n2 - 2; ncp <- d / sqrt(1 / n1 + 1 / n2) }
q <- qt(1 - alpha / 2, df)
pt(q, df, ncp, lower.tail = FALSE) + pt(-q, df, ncp)
}
p30 <- t_power(0.5, 30)
emit("STAT-DOE-POWER-001", pw, pmenu, "One-sample t power at n = 30",
list(family = "raw_t", solve_for = "power", sample_size = 30L, mean_difference = 1, standard_deviation = 2),
list("/achieved_power" = tol(p30, "Two-sided noncentral t power with d = 0.5, ncp = d sqrt(n).")),
c("d = 1 / 2 = 0.5."))
n <- 2L; while (t_power(0.5, n) < 0.8) n <- n + 1L
emit("STAT-DOE-POWER-002", pw, pmenu, "One-sample t sample size for 80% power",
list(family = "raw_t", solve_for = "sample_size", mean_difference = 1, standard_deviation = 2),
list("/sample_size" = exact(n, "Smallest n with power >= 0.8."), "/achieved_power" = tol(t_power(0.5, n), "Power at that n.")),
c("Linear search over n in R."), kind = "axis_coverage")
n2 <- ceiling(20 * 1.5)
emit("STAT-DOE-POWER-003", pw, pmenu, "Independent t power with allocation ratio 1.5",
list(family = "raw_t", solve_for = "power", design = "independent", sample_size = 20L, ratio = 1.5, mean_difference = 1.2, standard_deviation = 2),
list("/achieved_power" = tol(t_power(0.6, 20, n2), "Pooled-df noncentral t with n1 = 20, n2 = ceiling(20 x 1.5) = 30.")),
c("Line 34-37."), kind = "axis_coverage")
z <- qnorm(0.975); zp <- pnorm(0.5 * sqrt(25) - z) + pnorm(-0.5 * sqrt(25) - z)
emit("STAT-DOE-POWER-004", pw, pmenu, "Known-sigma Z power at n = 25",
list(family = "z", solve_for = "power", sample_size = 25L, mean_difference = 1, known_sigma = 2),
list("/achieved_power" = tol(zp, "Two-sided normal power.")), c("Line 48-49."), kind = "axis_coverage")
means <- c(10, 12, 13); sizes <- c(15, 15, 15); s <- 3
f <- sqrt(sum(sizes * (means - weighted.mean(means, sizes))^2) / sum(sizes)) / s
N <- 30 # the entered sample size (default 30): "the solve's N is total observations"
# R's pf(ncp=) goes through pnbeta, whose series stops at an internal bound near
# 1e-9. Sum the Poisson mixture of central beta tails instead; pbeta is accurate
# to about 1e-15 and the Poisson remainder beyond 2000 terms is below 1e-20.
ncf_upper <- function(q, d1, d2, ncp) {
x <- d1 * q / (d1 * q + d2); j <- 0:2000
stopifnot(ppois(max(j), ncp / 2, lower.tail = FALSE) < 1e-20)
sum(stats::dpois(j, ncp / 2) * stats::pbeta(x, d1 / 2 + j, d2 / 2, lower.tail = FALSE))
}
fp <- ncf_upper(qf(0.95, 2, N - 3), 2, N - 3, f^2 * N)
emit("STAT-DOE-POWER-005", pw, pmenu, "One-way ANOVA power from group means",
list(family = "raw_anova", solve_for = "power", group_means = list(10, 12, 13), group_sizes = list(15L, 15L, 15L), standard_deviation = 3),
list("/achieved_power" = tol(fp, "Noncentral F with lambda = f^2 N, as a Poisson mixture of pbeta tails."), "/effect_size" = tol(f, "Cohen's f.")),
c("Manual chapter 36 lines 44-45: 'the supplied group sizes determine the weighting and the solve's N is total observations'; N is the entered sample size, 30 by default, and lambda = f^2 N with N - 3 denominator df.",
"Corrected before first commit: the first draft took N as the sum of the group sizes, which the quoted line does not say."),
kind = "axis_coverage")
nv <- 20; lo <- qchisq(0.025, nv - 1); hi <- qchisq(0.975, nv - 1)
vp <- pchisq(lo / 2, nv - 1) + pchisq(hi / 2, nv - 1, lower.tail = FALSE)
emit("STAT-DOE-POWER-006", pw, pmenu, "Normal variance test power for a true ratio of 2",
list(family = "normal_variance", solve_for = "power", sample_size = 20L, variance_ratio = 2),
list("/achieved_power" = tol(vp, "(n-1) s^2 / sigma0^2 ~ ratio x chi2(n - 1) under the alternative.")),
c("Line 49-51."), kind = "axis_coverage")
mu0 <- 0.1 * 10 * 15; mu1 <- 0.2 * 10 * 15
cut <- 0L; while (ppois(cut - 1, mu0, lower.tail = FALSE) > 0.05) cut <- cut + 1L
emit("STAT-DOE-POWER-007", pw, pmenu, "Poisson rate power with an integer cutoff",
list(family = "poisson_rate", solve_for = "power", sample_size = 15L, poisson_rate = 0.2, null_rate = 0.1, exposure = 10, alternative = "greater"),
list("/achieved_power" = tol(ppois(cut - 1, mu1, lower.tail = FALSE), "P(X >= c) under the true rate, with c the smallest cutoff whose null tail is at most alpha.")),
c(sprintf("Total exposure 15 x 10; null mean %g, true mean %g, cutoff %d.", mu0, mu1, cut)), kind = "axis_coverage")
h <- 2 * (asin(sqrt(0.6)) - asin(sqrt(0.5))); prp <- pnorm(abs(h) * sqrt(50) - z) + pnorm(-abs(h) * sqrt(50) - z)
emit("STAT-DOE-POWER-008", pw, pmenu, "One-sample proportion power with the arcsine effect",
list(family = "raw_proportion", solve_for = "power", sample_size = 50L, proportion = 0.6, null_proportion = 0.5),
list("/achieved_power" = tol(prp, "Two-sided normal power for h = 2 (asin sqrt 0.6 - asin sqrt 0.5)."), "/effect_size" = tol(h, "Cohen's h.")),
c("Line 40-42."), kind = "axis_coverage")
emit("STAT-DOE-POWER-009", pw, pmenu, "Paired proportion planning is refused",
list(family = "raw_proportion", solve_for = "power", design = "paired", proportion = 0.6, null_proportion = 0.5), list(),
c("Line 23-25: no paired binary covariance model is declared."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "aired"))
# ---- reliability demonstration plans ------------------------------------------------------------
rp <- "tabelz.executor.reliability_planning"; rmenu <- "reliabilityPlanningMenuItem"
binom_n <- function(R, C, f) { n <- f + 1L; while (pbinom(f, n, 1 - R) > 1 - C) n <- n + 1L; n }
n0 <- binom_n(0.9, 0.95, 0)
emit("STAT-DOE-RELPLAN-001", rp, rmenu, "Zero-failure binomial demonstration",
list(mode = "binomial", target_reliability = 0.9, mission_time = 1000, confidence = 0.95),
list("/chosen_n" = exact(n0, "Smallest n with 0.9^n <= 0.05; the manual's worked example gives 29."),
"/achieved_confidence" = tol(1 - pbinom(0, n0, 0.1), "Achieved confidence 1 - P(X <= 0) (manual chapter 58)."),
"/achieved_criterion" = tol(pbinom(0, n0, 0.1), "Criterion P(X <= 0) at the chosen n.")),
c(sprintf("ceiling(log(0.05) / log(0.9)) = %d.", ceiling(log(0.05) / log(0.9))),
"Until 2026-09-25 the achieved confidence was compared with the criterion field."))
n1 <- binom_n(0.95, 0.90, 1)
emit("STAT-DOE-RELPLAN-002", rp, rmenu, "One allowed failure at 90% confidence",
list(mode = "binomial", target_reliability = 0.95, mission_time = 1000, confidence = 0.9, allowed_failures = 1L),
list("/chosen_n" = exact(n1, "Smallest n with P(X <= 1) <= 0.10; the manual's worked example gives 77.")),
c("Line 131-132."), kind = "axis_coverage")
t_needed <- uniroot(function(t) pbinom(0, 10, 1 - 0.9^(t / 1000)) - 0.05, c(1000, 10000), tol = 1e-12)$root
emit("STAT-DOE-RELPLAN-003", rp, rmenu, "Duration for ten units with an exponential model",
list(mode = "duration", target_reliability = 0.9, mission_time = 1000, confidence = 0.95, search_dimension = "test_duration", sample_size = 10L),
list("/chosen_test_duration" = tol(1000 * log(0.05) / (10 * log(0.9)), "t = t0 ln(1-C) / (n ln R0); the manual gives about 2843 h.")),
c(sprintf("Root of the binomial rule: %.10g.", t_needed)), kind = "axis_coverage")
theta0 <- -1000 / log(0.9)
emit("STAT-DOE-RELPLAN-004", rp, rmenu, "Failure-terminated exponential chi-square rule",
list(mode = "duration", target_reliability = 0.9, mission_time = 1000, confidence = 0.95, allowed_failures = 1L,
scheme = "failure_terminated", sample_size = 10L),
list("/total_test_time" = tol(theta0 / 2 * qchisq(0.95, 4), "theta0 / 2 chi2_(0.95, 4); the manual gives about 45025.")),
c("Line 81-92."), kind = "axis_coverage")
emit("STAT-DOE-RELPLAN-005", rp, rmenu, "A Weibull plan without a shape is refused",
list(mode = "duration", target_reliability = 0.9, mission_time = 1000, confidence = 0.95, distribution = "weibull", sample_size = 10L,
search_dimension = "test_duration"), list(),
c("Line 112-113."), kind = "invalid_input", error = list(error_class = "ValueError", message_contains = "shape"))
generators/r/design_precision_planning.R
source("common.R", local = TRUE)
run <- generator_run(c("stats", "jsonlite", "tolerance"))
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Analytic planning formula:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
emit <- function(id, title, config, outputs, notes) {
write_expected("design", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("design", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "design",
entry_point = "tabelz.executor.precision_planning",
menu_items = list("precisionPlanningMenuItem"), title = title,
case_kind = "nominal", dataset = NULL, excluded_rows = list(),
configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/design/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = if (id == "STAT-DOE-PREC-TOL-001")
"R tolerance::K.factor 3.0.0, explicit HE2" else "R stats analytic formulas",
citation = if (id == "STAT-DOE-PREC-TOL-001")
"Howe (1969), equation (2.13); R tolerance 3.0.0 K.factor documentation" else
"R stats::qt, stats::qnorm, stats::qchisq and stats::pbinom documentation",
generator = list(path = "generators/r/design_precision_planning.R", sha256 = ""),
independent = TRUE,
convention_notes = as.list(notes)
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DOE-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
first <- function(values, target) {
hit <- which(values <= target)[1]
if (is.na(hit)) stop("No planned sample size meets the target")
hit + 1L
}
n <- 2L:500L
confidence <- 0.95
sd0 <- 2.4
mean_half <- stats::qt((1 + confidence) / 2, df = n - 1) * sd0 / sqrt(n)
mean_target <- 0.75
mean_n <- first(mean_half, mean_target)
stopifnot(mean_half[mean_n - 1L] <= mean_target, mean_half[mean_n - 2L] > mean_target)
emit("STAT-DOE-PREC-MEAN-001", "Student-t mean half-width",
list(mode = "mean", criterion = "absolute", target = mean_target,
confidence = confidence, assumed_sd = sd0, max_n = 500L),
list("/n" = exact(mean_n, "First integer meeting the Student-t half-width target."),
"/achieved_precision" = tol(mean_half[mean_n - 1L], "t_(0.975,n-1)*2.4/sqrt(n); owner 1e-9 tier."),
"/previous_n" = exact(mean_n - 1L, "Immediate predecessor does not meet the target."),
"/previous_achieved_precision" = tol(mean_half[mean_n - 2L], "Predecessor t half-width; owner 1e-9 tier.")),
c("Manual chapter 35: h_n=t_(0.975,n-1)*s0/sqrt(n); search smallest n with h_n<=0.75.",
"R verified h_(n-1)>0.75 and h_n<=0.75 with assumed SD 2.4."))
p0 <- 0.35
z <- stats::qnorm((1 + confidence) / 2)
denominator <- 1 + z^2 / n
proportion_half <- z / denominator * sqrt(p0 * (1 - p0) / n + z^2 / (4 * n^2))
proportion_target <- 0.1
proportion_n <- first(proportion_half, proportion_target)
emit("STAT-DOE-PREC-PROP-001", "Wilson proportion half-width",
list(mode = "proportion", criterion = "absolute", target = proportion_target,
confidence = confidence, assumed_proportion = p0, max_n = 500L),
list("/n" = exact(proportion_n, "First integer meeting Wilson half-width target."),
"/achieved_precision" = tol(proportion_half[proportion_n - 1L], "Wilson half-width at entered p0; owner 1e-9 tier."),
"/previous_n" = exact(proportion_n - 1L, "Immediate predecessor misses the target.")),
c("Manual chapter 35: Wilson score half-width uses entered p0=0.35, z=qnorm(0.975), and D=1+z^2/n."))
content <- 0.9
factor <- vapply(n, function(sample_size) tolerance::K.factor(
n = sample_size, f = sample_size - 1, alpha = 1 - confidence,
P = content, side = 2, method = "HE2"), numeric(1))
factor_target <- 2.5
factor_n <- first(factor, factor_target)
emit("STAT-DOE-PREC-TOL-001", "Howe two-sided normal tolerance factor",
list(mode = "normal_tolerance", criterion = "factor", target = factor_target,
confidence = confidence, assumed_sd = sd0, content = content,
sided = "two_sided", max_n = 500L),
list("/n" = exact(factor_n, "First integer whose Howe factor meets 2.5."),
"/achieved_precision" = tol(factor[factor_n - 1L], "Pinned R tolerance HE2 two-sided factor; owner 1e-9 tier."),
"/previous_n" = exact(factor_n - 1L, "Immediate predecessor misses the factor target.")),
c("Howe (1969) equation 2.13; tolerance::K.factor(method='HE2', f=n-1, alpha=1-C, P=content).",
"P=0.9 and C=0.95 are separate content and confidence inputs."))
range_confidence <- stats::pbinom(n - 2L, size = n, prob = content)
range_n <- which(range_confidence >= confidence)[1] + 1L
stopifnot(!is.na(range_n), range_n >= 3L)
emit("STAT-DOE-PREC-DF-001", "Distribution-free full-range coverage",
list(mode = "distribution_free_tolerance", criterion = "coverage",
confidence = confidence, content = content, max_n = 500L),
list("/n" = exact(range_n, "First full-range order-statistic n reaching 95% confidence."),
"/achieved_confidence" = tol(range_confidence[range_n - 1L], "P(Binomial(n,P)<=n-2); owner 1e-9 tier."),
"/previous_n" = exact(range_n - 1L, "Immediate predecessor has inadequate confidence.")),
c("Manual chapter 35: maximum full-range confidence is P(Binomial(n,P)<=n-2), with P=0.9."))
cat("design precision-planning references generated\n")
generators/r/design_window_coverage.R
source("common.R", local = TRUE)
# Independent deterministic references for B10. Randomized ordering and seeded
# sampling/bootstrap intervals are deliberately not reference targets.
run <- generator_run(c("stats", "jsonlite"))
generator <- "generators/r/design_window_coverage.R"
manual <- "docs/manual/chapters/97-robust-taguchi-design.tex"
tol <- function(x, why = "R arithmetic on exported CSV; relative error 1e-9.")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "1e-12",
relative_tolerance = "1e-9", rationale = why)
exact <- function(x) list(comparison = "exact", value = x, rationale = "Exact design count or label.")
seq_exact <- function(x) list(comparison = "sequence", values = as.list(x), rationale = "Exact declared design cells or labels.")
emit <- function(method, number, entry, menu, config, outputs, notes, dataset = NULL,
analytic = FALSE, deferred = list()) {
id <- sprintf("STAT-DOE-%s-%03d", method, number)
if (method == "ROBUST-ANALYSIS") deferred <- list(
list(path="/settings/0/mean_interval",reason="Seeded parametric-bootstrap interval is outside the deterministic-reference campaign."),
list(path="/settings/0/objective_interval",reason="Seeded parametric-bootstrap interval is outside the deterministic-reference campaign."),
list(path="/settings/1/mean_interval",reason="Seeded parametric-bootstrap interval is outside the deterministic-reference campaign."),
list(path="/settings/1/objective_interval",reason="Seeded parametric-bootstrap interval is outside the deterministic-reference campaign."))
write_expected("design", id, list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = deferred, generator_run = run))
write_case("design", id, list(schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "design", entry_point = entry, menu_items = list(menu), title = paste(method, number),
case_kind = if (number == 1) "nominal" else "axis_coverage",
dataset = if (is.null(dataset)) NULL else list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/design/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = if (analytic) "analytic" else "independent_implementation",
source = if (analytic) "Hand-checkable full crossing and fixed-input arithmetic" else "R stats: lm, anova, pf, pt, qt, binom.test; base mean/var/log10",
citation = "R Core Team, stats package documentation: lm, anova, FDist, TDistr, binom.test.",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "Expected values never use HypoNum output."))),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(paste0("REQ-STAT-DOE-", method, "-001")),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# 12 run rows: a balanced 2x2 layout repeated three times with nonzero run
# variation. Each row has three independent readings for the S/N definition.
t <- expand.grid(A = c(1L, 2L), B = c(1L, 2L), replicate = 1:3)
base <- 5 + 2 * (2 * t$A - 3) + (2 * t$B - 3) + (2 * t$A - 3) * (2 * t$B - 3) + c(-0.5, 0.2, 0.8)[t$replicate]
t <- data.frame(A = t$A, B = t$B, y1 = base, y2 = base + 0.5, y3 = base - 1)
export_dataset(t, "DOE_B10_TAGUCHI", "Constructed balanced 2x2 with three repeat rows", "R stats manual", "Constructed test data.",
c(A = "Int64", B = "Int64", y1 = "Float64", y2 = "Float64", y3 = "Float64"))
t <- read_dataset("DOE_B10_TAGUCHI")
ys <- as.matrix(t[c("y1", "y2", "y3")])
forms <- c("larger", "smaller", "nominal_variance", "nominal_mean_sd", "dynamic", "dynamic")
for (i in seq_along(forms)) {
form <- forms[i]; origin <- i == 6
means <- rowMeans(ys); sds <- apply(ys, 1, sd)
slopes <- NULL
if (form == "dynamic") {
signal <- c(1, 2, 3)
fits <- lapply(seq_len(nrow(ys)), function(j) if (origin) lm(ys[j, ] ~ 0 + signal) else lm(ys[j, ] ~ signal))
slopes <- vapply(fits, function(f) unname(tail(coef(f), 1)), 0.0)
sn <- vapply(seq_along(fits), function(j) 10 * log10(slopes[j]^2 / (deviance(fits[[j]]) / df.residual(fits[[j]]))), 0.0)
} else sn <- switch(form,
larger = -10 * log10(rowMeans(1 / ys^2)), smaller = -10 * log10(rowMeans(ys^2)),
nominal_variance = -10 * log10(sds^2), nominal_mean_sd = 10 * log10(means^2 / sds^2))
cfg <- list(factors = list("A", "B"), responses = list("y1", "y2", "y3"), sn = form,
predict_terms = list("A", "B"), predict_levels = list("2", "1"), anova_summary = "mean")
if (form == "dynamic") {cfg$signal_values <- list(1, 2, 3); cfg$dynamic_reference <- if (origin) "origin" else "intercept"}
out <- list()
for (j in seq_len(nrow(ys))) {
prefix <- paste0("/runs/", j - 1)
out[[paste0(prefix, "/count")]] <- exact(3L)
for (quantity in c("mean", "sd", "log_sd", "sn")) {
values <- switch(quantity, mean = means, sd = sds, log_sd = log(sds), sn = sn)
out[[paste0(prefix, "/", quantity)]] <- tol(values[j])
}
if (!is.null(slopes)) out[[paste0(prefix, "/slope")]] <- tol(slopes[j])
}
for (quantity in c("mean", "sd", "log_sd", "sn")) {
values <- switch(quantity, mean = means, sd = sds, log_sd = log(sds), sn = sn)
table_path <- paste0("/tables/[summary=", quantity, "]")
for (factor in seq_len(2)) for (level in 1:2)
out[[paste0(table_path, "/averages/", factor - 1, "/", level - 1)]] <- tol(mean(values[t[[factor]] == level]))
pred <- mean(values[t$A == 2]) + mean(values[t$B == 1]) - mean(values)
out[[paste0("/predictions/[summary=", quantity, "]/value")]] <- tol(pred)
}
a <- anova(lm(means ~ factor(A) + factor(B), data = t))
for (j in 1:2) {
prefix <- paste0("/anova/[source=", c("A", "B")[j], "]")
out[[paste0(prefix, "/df")]] <- exact(as.integer(a$Df[j]))
out[[paste0(prefix, "/ss")]] <- tol(a$`Sum Sq`[j])
out[[paste0(prefix, "/f")]] <- tol(a$`F value`[j])
out[[paste0(prefix, "/p")]] <- tol(a$`Pr(>F)`[j])
}
emit("TAGUCHI-ANALYSIS", i, "tabelz.analysis.doe_taguchi_analysis:analyze_taguchi", "taguchiAnalysisMenuItem", cfg, out,
c(paste0(manual, ":83-116: static S/N formulas, sample n-1 SD, dynamic slope/MSE and orthogonal level averages."),
"R lm/ANOVA uses orthogonal additive main effects; prediction is level-A2 mean + level-B1 mean - grand mean.",
"Dynamic reference is lm(y~signal) with SSE/(3-2) or lm(y~0+signal) with SSE/(3-1)."), "DOE_B10_TAGUCHI")
}
# Analytic finite-field constructions, with explicit standard row order.
# In GF(2), any two of a,b,a+b have determinant 1, so each pair occurs once.
# In GF(3), the directions a,b,a+b,2a+b are pairwise independent: each 2x2
# determinant is 1 or 2 mod 3, hence every pair occurs once. This derives
# strength two independently without consulting product array output/constants.
l4 <- do.call(rbind, lapply(0:1, function(a) do.call(rbind, lapply(0:1, function(b) c(a, b, (a+b)%%2) + 1))))
l9 <- do.call(rbind, lapply(0:2, function(a) do.call(rbind, lapply(0:2, function(b) c(a, b, (a+b)%%3, (2*a+b)%%3) + 1))))
for (i in 1:3) {
cfg <- list(array = if (i == 2) "L9" else "L4", factors = list("A", "B", "C"), randomize = FALSE, repeats = 2L)
arr <- if (i == 2) l9 else l4
out <- list("/runs" = exact(as.integer(nrow(arr))))
if (i == 3) {cfg$noise <- "outer"; cfg$outer_array <- "L4"; cfg$noise_factors <- list("Z"); cfg$signal_levels <- list(1,2,3)}
for (j in seq_len(nrow(arr))) for (k in 1:3) out[[paste0("/rows/",j-1,"/",k+1)]] <- exact(as.integer(arr[j,k]))
responses <- if (i == 3) unlist(lapply(1:3, function(s) paste0("Y_M",s,"_O",1:4))) else c("Y_R1","Y_R2")
out[["/response_columns"]] <- seq_exact(responses)
for (k in 1:3) out[[paste0("/assignment/", k-1, "/levels")]] <- exact(if (i == 2) 3L else 2L)
emit("TAGUCHI-DESIGN", i, "tabelz.analysis.doe_taguchi_design:compute_taguchi_design_table", "taguchiDesignMenuItem", cfg, out,
c(paste0(manual, ":67-81: orthogonal arrays, repeats/outer array and signal/response correspondence."),
"Analytic derivation: enumerate a then b in GF(2), columns a,b,a+b (mod 2), then add one. Pairwise 2x2 determinants equal one, so all four level pairs occur once; rows are 111,122,212,221. In GF(3), use columns a,b,a+b,2a+b (mod 3), plus one; pairwise determinants are nonzero mod 3, so all nine pairs occur once. Outer L4 supplies four noise rows per signal."), analytic = TRUE)
}
# Robust model: twelve independent planned runs, not twelve repeated readings.
r <- expand.grid(C = c(-1,1), Z = c(-1,1), replicate = 1:3)
r <- data.frame(PlannedRunID = sprintf("R%02d",1:12), C = r$C, Z = r$Z,
Response = 10 + 2*r$C + 3*r$Z + r$C*r$Z + c(-1,0,1)[r$replicate])
export_dataset(r, "DOE_B10_ROBUST", "Constructed crossed experiment", "R stats lm documentation", "Constructed test data.",
c(PlannedRunID="String", C="Float64", Z="Float64", Response="Float64"))
r <- read_dataset("DOE_B10_ROBUST")
design <- function(weight = 0.5) list(factors=list(list(name="C",role="control",low=-1,high=1),
list(name="Z",role="noise",low=-1,high=1,low_probability=weight,high_probability=1-weight)),replicates=3L,seed=19L)
analysis <- function(i = 1L) list(design=design(if (i == 2) 0.75 else 0.5), response_column="Response",
objective=c("target","minimize","maximize")[i], target=10, preference_weight=1.5,
bootstrap_replicates=99L,bootstrap_seed=11L)
fit <- lm(Response ~ C*Z, data=r); sf <- summary(fit); beta <- coef(fit); mse <- deviance(fit)/df.residual(fit)
for (i in 1:3) {
cfg <- analysis(i); out <- list("/run_count"=exact(12L),"/residual_df"=exact(8L),"/rank"=exact(4L),
"/term_count"=exact(4L),"/residual_variance"=tol(mse),"/r_squared"=tol(sf$r.squared))
ci <- confint(fit)
for (j in 1:4) {
prefix <- paste0("/coefficients/",j-1)
for (quantity in c("estimate","std_error","t_value","p_value","lower","upper")) {
value <- switch(quantity, estimate=sf$coefficients[j,1],std_error=sf$coefficients[j,2],t_value=sf$coefficients[j,3],p_value=sf$coefficients[j,4],lower=ci[j,1],upper=ci[j,2])
out[[paste0(prefix,"/",quantity)]] <- tol(value)
}
}
setting <- lapply(c(-1,1), function(control) {
predictions <- predict(fit,newdata=data.frame(C=control,Z=c(-1,1)))
w <- c(cfg$design$factors[[2]]$low_probability,cfg$design$factors[[2]]$high_probability)
mu <- sum(w*predictions); nv <- sum(w*(predictions-mu)^2); sd <- sqrt(nv+mse)
loss <- switch(cfg$objective,target=(mu-cfg$target)^2+nv+mse,minimize=mu+cfg$preference_weight*sd,maximize=mu-cfg$preference_weight*sd)
c(control=control,mean=mu,noise_variance=nv,residual_variance=mse,total_sd=sd,objective=loss)
})
order <- order(vapply(setting, function(s) s["objective"],0.0),decreasing=cfg$objective=="maximize")
for (j in 1:2) {
s <- setting[[order[j]]]; prefix <- paste0("/settings/",j-1)
out[[paste0(prefix,"/rank")]] <- exact(as.integer(j))
out[[paste0(prefix,"/controls/0/value")]] <- tol(s["control"])
for (quantity in c("mean","noise_variance","residual_variance","total_sd","objective")) out[[paste0(prefix,"/",quantity)]] <- tol(s[quantity])
}
emit("ROBUST-ANALYSIS",i,"tabelz.analysis.robust_parameter_design:compute_robust_design","robustDesignMenuItem",cfg,out,
c(paste0(manual,":14-34: whole-run OLS, declared normalized noise weights and objective formulas."),
"R lm(Response~C*Z); confidence intervals use residual t reference. Pointwise bootstrap intervals are deferred, not compared."),"DOE_B10_ROBUST")
}
for (i in 1:3) {
cfg <- design(); cfg$replicates <- if (i==2) 2L else 3L
axes <- expand.grid(C=c(-1,1),Z=c(-1,1))
if (i==3) {cfg$signal <- list(name="S",levels=list(1,2,3)); axes <- expand.grid(C=c(-1,1),Z=c(-1,1),S=1:3)}
points <- apply(axes,1,function(row) paste(row,collapse="|"))
emit("ROBUST-GENERATE",i,"tabelz.analysis.robust_parameter_design:generate_robust_design","robustDesignMenuItem",cfg,
list("/runs"=exact(as.integer(nrow(axes)*cfg$replicates)),"/design_points"=seq_exact(sort(rep(points,cfg$replicates),method="radix"))),
c(paste0(manual,":7-16: every control/noise/signal cell crossed with independent replicates."),"Cartesian product enumerated analytically; randomized run order not compared."),analytic=TRUE)
}
confirmation <- function(i=1L) list(analysis=analysis(),selected=list(list(factor="C",value=-1)),
predicted_mean=8,predicted_objective=9,runs_per_condition=as.integer(i+1),seed=23L,response_column="Response")
for (i in 1:3) {
cfg <- confirmation(i)
points <- paste(-1,c(-1,1),c(6,10),sep="|")
emit("ROBUST-PLAN",i,"tabelz.analysis.robust_parameter_design_confirmation:plan_robust_confirmation","robustDesignMenuItem",list(confirmation=cfg),
list("/runs"=exact(as.integer(2*cfg$runs_per_condition)),"/design_points"=seq_exact(sort(rep(points,each=cfg$runs_per_condition),method="radix"))),
c(paste0(manual,":60-64: selected setting across every declared noise condition, fresh runs, recorded design predictions."),
"R OLS fitted coefficients are exactly (10,2,3,1); selected C=-1 predicts 6 and 10 at Z=-1,+1."),"DOE_B10_ROBUST")
}
cf <- data.frame(PlannedRunID=sprintf("CONF%02d",1:12),Predicted=rep(c(6,10),each=6),C=-1,
Z=rep(c(-1,1),each=6),Response=rep(c(6,10),each=6)+rep(c(-1,0,1,-1,0,1),2))
export_dataset(cf,"DOE_B10_CONFIRM","Constructed fresh confirmation runs","R stats var and lm documentation","Constructed test data.",
c(PlannedRunID="String",Predicted="Float64",C="Float64",Z="Float64",Response="Float64"))
cf <- read_dataset("DOE_B10_CONFIRM")
for (i in 1:3) {
cfg <- confirmation(); cfg$analysis <- analysis(i)
cellmeans <- tapply(cf$Response,cf$Z,mean)
weights <- c(cfg$analysis$design$factors[[2]]$low_probability,cfg$analysis$design$factors[[2]]$high_probability)
mu <- sum(weights*cellmeans); nv <- sum(weights*(cellmeans-mu)^2)
v <- sum((cf$Response-cellmeans[as.character(cf$Z)])^2)/(nrow(cf)-2)
obj <- switch(cfg$analysis$objective,target=(mu-10)^2+nv+v,minimize=mu+1.5*sqrt(nv+v),maximize=mu-1.5*sqrt(nv+v))
emit("ROBUST-CONFIRM",i,"tabelz.analysis.robust_parameter_design_confirmation:compute_robust_confirmation","robustDesignMenuItem",cfg,
list("/run_count"=exact(12L),"/residual_df"=exact(10L),"/mean_error"=tol(mean(cf$Response-cf$Predicted)),
"/rmse"=tol(sqrt(mean((cf$Response-cf$Predicted)^2))),"/observed_mean"=tol(mu),"/observed_noise_variance"=tol(nv),
"/observed_residual_variance"=tol(v),"/observed_objective"=tol(obj)),
c(paste0(manual,":60-64: fresh confirmation errors and observed objective; design data not refitted."),
"Condition means and pooled within-condition variance from re-read fresh CSV; descriptive plug-in objective, not unbiased expected loss."),"DOE_B10_CONFIRM")
}
for (i in 1:4) {
cfg <- list(factors=3L,replicates=2L,model=if (i==2) "main" else "full",alpha=if(i==3) 0.1 else 0.05,
delta=1,sigma=1,solve_for=if (i==4) "replicates" else "power",target_power=0.8)
evaluate <- function(replicates) {
n <- 8*replicates; rank <- if(cfg$model=="full") 8 else 4; df <- n-rank
if(df==0) return(NULL)
eta <- sqrt(n)/2; critical <- qt(1-cfg$alpha/2,df)
c(n=n,rank=rank,df=df,eta=eta,critical=critical,power=pt(-critical,df,ncp=eta)+pt(critical,df,ncp=eta,lower.tail=FALSE))
}
reps <- cfg$replicates
if(i==4) reps <- which(vapply(1:50,function(k) {v<-evaluate(k);!is.null(v)&&v["power"]>=0.8},FALSE))[1]
e <- evaluate(reps)
emit("POWER-WINDOW",i,"tabelz.analysis.doe_power:compute_doe_power_table","doePowerMenuItem",cfg,
list("/total_runs"=exact(as.integer(e["n"])),"/corner_runs"=exact(as.integer(e["n"])),"/rank"=exact(as.integer(e["rank"])),
"/error_df"=exact(as.integer(e["df"])),"/noncentrality"=tol(e["eta"]),"/critical_t"=tol(e["critical"]),"/power"=tol(e["power"]),
"/solved_replicates"=exact(as.integer(reps)),"/target_met"=exact(unname(e["power"]>=0.8))),
c(paste0(manual,":118-155: high-minus-low Delta=2 beta, actual rank, two tails of noncentral t."),
"Balanced unblocked 2^3: X'X=N I, effect variance factor=1/N; eta=sqrt(N)/2; R qt and pt. Search returns first feasible replicate count."))
}
# Retained OLS artifact fitted independently in R; all inputs fixed, so no
# seeded simulation draws or uncertain coefficients are validated.
simulation_fit <- lm(Response ~ C, data=r)
artifact <- list(family="ols",response="Response",predictors=list(list(name="C",kind="numeric")),
feature_order=list("Intercept","C"),coefficient_names=list("Intercept","C"),
coefficients=as.list(unname(coef(simulation_fit))),
covariance=lapply(seq_len(2),function(j) as.list(unname(vcov(simulation_fit)[j,]))),
link="identity",design_rank=2L,fitted_row_ids=as.list(r$PlannedRunID),
residual_variance=deviance(simulation_fit)/df.residual(simulation_fit),residual_df=as.integer(df.residual(simulation_fit)))
for (i in 1:3) {
n <- if(i==2) 200L else 100L; confidence <- if(i==3) 0.9 else 0.95
fixed <- if(i==2) -1 else 1
value <- unname(predict(simulation_fit,newdata=data.frame(C=fixed)))
cfg <- list(model_kind="stored",stored_model=artifact,inputs=list(list(name="C",source="fixed",value=fixed)),draws=n,confidence_level=confidence,
percentiles=list(25,50,75),thresholds=as.list(c(value-1,value,value+1)))
out <- list("/draws"=exact(n),"/mean"=tol(value),"/standard_deviation"=tol(0),"/mean_monte_carlo_standard_error"=tol(0),
"/minimum"=tol(value),"/maximum"=tol(value))
for(j in 1:3) {
out[[paste0("/percentiles/",j-1,"/value")]] <- tol(value)
k <- if(j==1) n else 0L; ci <- binom.test(k,n,conf.level=confidence)$conf.int
prefix <- paste0("/thresholds/",j-1)
out[[paste0(prefix,"/exceedances")]] <- exact(k)
out[[paste0(prefix,"/probability")]] <- tol(k/n)
out[[paste0(prefix,"/lower")]] <- tol(ci[1]);out[[paste0(prefix,"/upper")]] <- tol(ci[2])
}
emit("SIMULATION-FIXED",i,"tabelz.analysis.model_simulation:compute_model_simulation","modelSimulationMenuItem",cfg,out,
c("docs/manual/chapters/95-input-simulation.tex:4-9,46-64: fixed inputs/formula, percentiles and strict-threshold binomial intervals.",
"Stored artifact fitted independently by R lm(Response~C) from DOE_B10_ROBUST; fixed input and conditional mean give identical outcomes. R binom.test gives Clopper-Pearson intervals at k=0,n. Seeded random draws and fitted-parameter mixtures are deferred."))
}
cfg <- list(model_kind="stored",stored_model=artifact,draws=100L,
inputs=list(list(name="C",source="entered",distribution=list(family="normal",mean=0,standard_deviation=1))))
emit("SIMULATION-FIXED",4,"tabelz.analysis.model_simulation:compute_model_simulation","modelSimulationMenuItem",cfg,
list("/draws"=exact(100L),"/inputs/0/name"=exact("C"),"/inputs/0/role"=exact("random")),
c("docs/manual/chapters/95-input-simulation.tex:4-9,39-50: retained model and declared random input, conditional-mean target.",
"This setting exercises random-input execution; finite-sample simulated values are explicitly deferred, not compared to population limits."),
analytic=TRUE,deferred=lapply(c("/mean","/standard_deviation","/minimum","/maximum","/percentiles","/thresholds"),
function(path) list(path=path,reason="Seeded simulation output needs a separately qualified RNG/reference construction; no population value is substituted for a finite draw statistic.")))
cat("B10 design-window references generated\n")
generators/r/design_worksheets.R
source("common.R", local = TRUE)
# Analytic references for MSA study worksheets (manual chapter 10 lines 9-63):
# tabelz.measurements.study_worksheets:generate_study_worksheet
# operation:tabelz.study_worksheet
# Crossed: every operator measures every part the requested number of trials;
# nested: each operator gets its own parts; attribute agreement: one rating row
# per part/operator/trial with an optional per-part standard. Run order is a
# seeded shuffle within operator blocks (PCG64), so cells are compared as a
# sorted set and the same seed must reproduce the same sheet.
run <- generator_run(c("jsonlite"))
family <- "design"
generator <- "generators/r/design_worksheets.R"
menu <- "studyWorksheetsMenuItem"
ws <- "tabelz.measurements.study_worksheets:generate_study_worksheet"
op <- "operation:tabelz.study_worksheet"
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
cells_of <- function(parts, operators, trials) list(comparison = "sequence",
values = as.list(sort(as.vector(outer(outer(parts, operators, paste, sep = "|"), seq_len(trials), paste, sep = "|")), method = "radix")),
rationale = "Every part x operator x trial cell exactly once.")
emit <- function(id, entry, title, config, outputs, notes, kind = "nominal", error = NULL, repeat_n = 1L) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind, dataset = NULL, excluded_rows = list(),
configuration = config, "repeat" = repeat_n,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "analytic", source = "Hand derivation of the worksheet design", citation = "",
generator = list(path = generator, sha256 = ""), independent = TRUE, convention_notes = as.list(notes)),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-DOE-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
parts <- c("P1", "P2", "P3"); ops <- c("Ann", "Bob")
crossed <- list(layout = "crossed", part_labels = as.list(parts), operator_labels = as.list(ops), trials = 2L, seed = 11L)
emit("STAT-DOE-WORKSHEET-001", ws, "Crossed worksheet: 3 parts x 2 operators x 2 trials", crossed,
list("/rows" = exact(12L, "3 x 2 x 2 rows."), "/cells" = cells_of(parts, ops, 2)),
c("Line 17-18: crossed assigns every operator to every part for every trial."))
emit("STAT-DOE-WORKSHEET-002", ws, "The same seed reproduces the sheet", crossed,
list("/rows" = exact(12L, "Twelve rows.")), c("Line 43-45: the same design and seed reproduce the exact run order."),
kind = "determinism", repeat_n = 2L)
emit("STAT-DOE-WORKSHEET-003", ws, "Nested worksheet: two parts per operator",
list(layout = "nested", part_count = 2L, operator_labels = list("Ann", "Bob", "Cy"), trials = 3L, seed = 5L),
list("/rows" = exact(18L, "3 operators x 2 own parts x 3 trials.")),
c("Line 19-21: each operator gets a distinct set of parts."), kind = "axis_coverage")
emit("STAT-DOE-WORKSHEET-004", ws, "Attribute agreement worksheet with standards",
list(layout = "attribute_agreement", part_labels = as.list(parts), operator_labels = as.list(ops), trials = 1L,
standard_values = list("G", "NG", "G")),
list("/rows" = exact(6L, "3 x 2 x 1 rating rows."), "/cells" = cells_of(parts, ops, 1),
"/columns" = list(comparison = "set", values = list("Part", "Operator", "Trial", "TrialID", "Value", "StandardValue"),
rationale = "Line 45-48: Value replaces Measurement, plus StandardValue when declared.")),
c("One rating row per part/operator/trial."), kind = "axis_coverage")
emit("STAT-DOE-WORKSHEET-005", ws, "Part labels are refused for nested designs",
list(layout = "nested", part_labels = list("P1", "P2"), operator_count = 2L, trials = 2L), list(),
c("Line 20-21: part labels cannot be declared for nested designs."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "ested"))
emit("STAT-DOE-WORKSHEET-006", op, "The recorded operation recreates the worksheet", crossed,
list("/rows" = exact(12L, "Twelve rows."), "/cells" = cells_of(parts, ops, 2)),
c("Line 50-54: the adopted sheet is a separate table with its own recorded operation."), kind = "determinism", repeat_n = 2L)
generators/r/explore_mode_screen_fit.R
source("common.R", local = TRUE)
library(diptest)
# Independent references for the current dip/KDE screen and retained-fit Run.
# No Tabelz output or code is imported. The Monte Carlo stream is not shared with R;
# only the deterministic dip statistic and normal-reference bandwidth are compared.
# The retained fits have one component, so their global MLEs are closed form.
run <- generator_run(c("stats", "diptest", "jsonlite"))
screen_entry <- "tabelz.analysis.mode_screen:compute_mode_screen"
fit_entry <- "tabelz.analysis.mode_retained_explanation:fit_explanation"
manual <- "docs/manual/chapters/110-multimodality.tex"
exact <- function(value, why) {
if (is.null(value)) return(list(comparison = "absent", rationale = why))
list(comparison = "exact", value = value, rationale = why)
}
tol <- function(value, why) list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "1e-12", relative_tolerance = "1e-9",
rationale = why)
defer <- function(path, reason) list(path = path, reason = reason)
emit <- function(id, title, kind, entry, config, outputs, deferred, notes, citation) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = deferred, generator_run = run)
write_expected("explore", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "explore",
entry_point = entry,
menu_items = list(if (entry == screen_entry) "multimodalityMenuItem" else "explainMultimodalityMenuItem"),
title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/EXP_MODE_CURRENT.json", sha256 = ""),
excluded_rows = list(33L), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/explore/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", independent = TRUE,
source = "R diptest::dip and base/stats closed-form likelihood calculations",
citation = citation,
generator = list(path = "generators/r/explore_mode_screen_fit.R", sha256 = ""),
convention_notes = c(notes, "All references use re-read CSV data, omit excluded zero-based row 33, and remove missing observations per column.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-EXP-MODE-CURRENT-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("explore", id, case)
}
n <- 32L
frame <- data.frame(
x = c(seq(-4.8, -3.2, length.out = 16), seq(3.2, 4.8, length.out = 16), NA, 100),
z = c(qnorm(((1:n) - 0.5) / n), NA, -100),
count = c(rep(0:7, 4), NA_integer_, 100L)
)
export_dataset(frame, "EXP_MODE_CURRENT",
"Constructed two-cluster continuous values, normal scores and counts; row 33 is missing and row 34 is excluded",
"Synthetic validation construction; no empirical provenance is claimed",
"Constructed validation data; no third-party license.",
c(x = "Float64", z = "Float64", count = "Int64"))
d <- read_dataset("EXP_MODE_CURRENT")[-34, ]
finite <- function(name) d[[name]][is.finite(d[[name]])]
stopifnot(length(finite("x")) == n, length(finite("z")) == n)
screen_base <- function(columns) list(
"/source_count" = exact(nrow(frame), "All source rows, before exclusion"),
"/excluded_count" = exact(1L, "One explicit excluded row"),
"/family_size" = exact(length(columns), "The entire requested family"),
"/family_alpha" = tol(0.05, "Declared comparison threshold")
)
screen_column <- function(name, restricted = FALSE) {
values <- finite(name)
prefix <- paste0("/columns/[column=", name, "]")
sd <- sd(values)
iqr <- IQR(values, type = 7)
h0 <- 0.9 * min(sd, iqr / 1.349) * length(values)^(-1/5)
out <- list()
out[[paste0(prefix, "/observations")]] <- exact(length(values), "Included finite observations")
out[[paste0(prefix, "/missing")]] <- exact(sum(!is.finite(d[[name]])), "Included missing/nonfinite observations")
out[[paste0(prefix, "/dip/available")]] <- exact(TRUE, "Continuous, distinct, nonconstant values with n >= 8")
out[[paste0(prefix, "/dip/statistic")]] <- tol(diptest::dip(values, min.is.0 = FALSE), "Independent R Hartigan dip with the nonzero finite-sample minimum")
out[[paste0(prefix, "/dip/adjusted_p_value")]] <- exact(NULL, "New screens use no multiplicity adjustment")
out[[paste0(prefix, "/kde/available")]] <- exact(!restricted, "Restricted support prevents the uncorrected automatic KDE signal")
out[[paste0(prefix, "/kde/standard_deviation")]] <- tol(sd, "Sample SD, denominator n - 1")
out[[paste0(prefix, "/kde/interquartile_range")]] <- tol(iqr, "R type-7 quartiles, Q3 - Q1")
out[[paste0(prefix, "/kde/reference_bandwidth")]] <- tol(h0, "0.9 min(sample SD, IQR/1.349) n^(-1/5)")
out
}
screen_deferred <- function(columns) unlist(lapply(columns, function(name) {
prefix <- paste0("/columns/[column=", name, "]")
list(
defer(paste0(prefix, "/dip/calibration"), "Seeded Monte Carlo exceedances and simulation-precision interval are not compared with R's different random stream."),
defer(paste0(prefix, "/dip/p_value"), "Monte Carlo p-value deferred; deterministic observed dip is compared."),
defer(paste0(prefix, "/dip/rejected"), "Decision depends on the deferred Monte Carlo p-value."),
defer(paste0(prefix, "/kde/flag"), "These cases compare the normal-reference bandwidth, not adaptive peak tracking or prominence/mass filters."),
defer(paste0(prefix, "/positive_methods"), "Depends on deferred Monte Carlo inference and KDE peak tracking."),
defer(paste0(prefix, "/explain_positive"), "Navigation gate depends on deferred method decisions.")
)
}), recursive = FALSE)
screen_notes <- c(paste0(manual, ":3-27: finite included population; dip on continuous measurements and new unadjusted screens."),
paste0(manual, ":37-44: h0 uses sample SD, type-7 IQR and multiplier 0.9; bounded support prevents automatic KDE interpretation."))
dip_citation <- "https://cran.r-project.org/web/packages/diptest/diptest.pdf (dip, min.is.0=FALSE); Hartigan and Hartigan (1985), Annals of Statistics 13:70-84. Bandwidth formula: manual section 110."
emit("STAT-EXP-MODESCREEN-001", "Current screen: independent dip and bandwidth on two continuous columns", "nominal",
screen_entry, list(columns = list("x", "z"), measurement = "continuous", replicates = 999L, seed = 17L),
c(screen_base(c("x", "z")), screen_column("x"), screen_column("z")),
screen_deferred(c("x", "z")), screen_notes, dip_citation)
emit("STAT-EXP-MODESCREEN-002", "Bounded support: dip remains available and automatic KDE signal is refused", "axis_coverage",
screen_entry, list(columns = list("x"), measurement = "continuous", support = "bounded", replicates = 999L, seed = 17L),
c(screen_base("x"), screen_column("x", restricted = TRUE),
list("/columns/0/available_methods" = exact(1L, "Only dip is available under the declared restricted support"))),
screen_deferred("x"), screen_notes, dip_citation)
emit("STAT-EXP-MODESCREEN-003", "Declared discrete counts refuse both continuous screen methods", "boundary",
screen_entry, list(columns = list("count"), measurement = "discrete"),
c(screen_base("count"), list(
"/columns/0/observations" = exact(length(finite("count")), "Included finite count observations"),
"/columns/0/missing" = exact(1L, "One included missing count"),
"/columns/0/dip/available" = exact(FALSE, "Known discrete data are ineligible"),
"/columns/0/dip/statistic" = exact(NULL, "No continuous dip statistic is reported"),
"/columns/0/dip/p_value" = exact(NULL, "No continuous p-value is reported"),
"/columns/0/kde/available" = exact(FALSE, "Known discrete data are ineligible"),
"/columns/0/available_methods" = exact(0L, "Neither continuous method is eligible"),
"/columns/0/positive_methods" = exact(0L, "Unavailable methods cannot be positive"),
"/columns/0/explain_positive" = exact(FALSE, "No positive screen gate")
)), list(), c(screen_notes, paste0(manual, ":10-17,92-94: known discrete data are ineligible for the continuous screen.")),
"Analytic eligibility contract in docs/manual/chapters/110-multimodality.tex; counts constructed in R.")
fit_outputs <- function(name, family, variance, ll, p) {
values <- finite(name)
key <- if (family == "gaussian") "gaussian:1" else "poisson"
out <- list(
"/variable" = exact(name, "Requested target"),
"/mode_count" = exact(1L, "One declared component; not an inferred population count"),
"/row_count" = exact(length(values), "Every included finite target is fitted"),
"/root" = exact(NULL, "The fitting Run builds no association tree"),
"/training_count" = exact(0L, "No tree training population in fit-only Run"),
"/validation_count" = exact(0L, "No tree tuning population in fit-only Run"),
"/component_fit/selected_key" = exact(key, "Only the declared candidate is fitted"),
"/component_fit/candidates/0/parameter_count" = exact(p, "Gaussian mean/variance, or Poisson rate"),
"/component_fit/candidates/0/log_likelihood" = tol(ll, "Sum of independent R log densities/masses at the global one-component MLE"),
"/component_fit/candidates/0/bic" = tol(-2 * ll + p * log(length(values)), "BIC = -2 log L + p log n"),
"/component_fit/candidates/0/components/0/family" = exact(family, "Declared component family"),
"/component_fit/candidates/0/components/0/weight" = tol(1, "The sole component has probability one"),
"/component_fit/candidates/0/components/0/mean" = tol(mean(values), "One-component MLE equals the sample mean"),
"/component_fit/candidates/0/components/0/variance" = tol(variance, "Population-denominator Gaussian MLE variance, or Poisson mean"),
"/investigation/source_row_count" = exact(nrow(frame), "Original source includes missing and excluded rows"),
"/investigation/eligible_count" = exact(length(values), "Included finite population"),
"/investigation/sampled" = exact(FALSE, "Sampling is not enabled")
)
for (i in seq_along(values)) {
out[[paste0("/component_fit/posteriors/", i - 1, "/0")]] <- tol(1, "One component gives posterior probability one for each fitted row")
out[[paste0("/component_fit/labels/", i - 1)]] <- exact(0L, "Every fitted row is allocated to the sole component")
}
out
}
x <- finite("x")
variance <- mean((x - mean(x))^2)
ll <- sum(dnorm(x, mean(x), sqrt(variance), log = TRUE))
fit_notes <- c(paste0(manual, ":98-117: declared Gaussian/count candidates, likelihood, posteriors and BIC; fixed K is distinct from BIC selection."),
paste0(manual, ":119-124: every included finite observation is fitted unless sampling is explicitly enabled."),
"em-v2 is the recorded NumPy implementation of the same fit contract; these closed-form one-component cases do not qualify multi-component starts, local optima or BIC comparisons.")
emit("STAT-EXP-MODEFIT-001", "Retained single-Gaussian fit: exact MLE, likelihood, BIC and row allocations", "nominal",
fit_entry, list(column = "x", implementation = "em-v2", config = list(build_tree = FALSE,
component_config = list(gaussian_k = 1L))),
fit_outputs("x", "gaussian", variance, ll, 2L), list(), fit_notes,
"https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Normal.html; one-Gaussian MLE mu=mean(x), variance=mean((x-mu)^2).")
y <- finite("count")
lambda <- mean(y)
ll <- sum(dpois(y, lambda, log = TRUE))
emit("STAT-EXP-MODEFIT-002", "Retained Poisson fit: exact rate, likelihood, BIC and row allocations", "axis_coverage",
fit_entry, list(column = "count", implementation = "em-v2", config = list(build_tree = FALSE,
component_config = list(measurement = "count", count_candidates = list("poisson")))),
fit_outputs("count", "poisson", lambda, ll, 1L), list(), fit_notes,
"https://stat.ethz.ch/R-manual/R-devel/library/stats/html/Poisson.html; one-Poisson MLE lambda=sum(y)/n, variance=lambda.")
cat("Generated five current-screen/retained-fit cases\n")
generators/r/explore_plots.R
source("common.R", local = TRUE)
# References for the Explore plot calculations (manual chapter 11) and the bars,
# heatmap and ECDF graphs (chapter 63). Only convention-free outputs are
# asserted: counts, order statistics, the ordinary least-squares line, Multi-Plot
# N / mean / median / standard deviation / mean +- 4 SD, group means with 95
# percent t intervals, exact ECDF proportions, and aggregated bar and heatmap
# cells. Box quartiles use R's type 7 (linear interpolation); the manual does
# not name a quantile definition. The bimodality coefficient is Sarle's
# coefficient with sample-size-adjusted skewness and excess kurtosis (SAS
# definition); the manual does not define it either.
run <- generator_run(c("stats", "jsonlite"))
family <- "explore"
generator <- "generators/r/explore_plots.R"
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
num <- function(value, why) list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = why)
seq_exact <- function(values, why) list(comparison = "sequence", values = as.list(values), rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
contains <- function(value, why) list(comparison = "contains", values = list(value), rationale = why)
emit <- function(id, entry, menu, title, config, outputs, notes, kind = "nominal", excluded = integer(),
oracle_class = "independent_implementation", dataset = "EXP_PLOTS_01") {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = oracle_class, source = "R 4.6.1 base and stats",
citation = "R stats::lm, stats::quantile (type 7), stats::t.test, stats::ecdf documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE, convention_notes = as.list(notes)),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-EXP-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
# ---- dataset ---------------------------------------------------------------------------------
noise <- c(0.3, -0.2, 0.5, -0.4, 0.1, -0.1, 0.2, -0.3)
x <- as.double(1:24)
y <- 2 + 0.5 * x + rep(noise, 3)
y[5] <- NA # one missing response in group A
y[24] <- 40 # one high outlier in group C
colour <- rep(c("red", "blue", "green"), 8)
colour[7] <- NA # one missing colour in group A, facet L
raw <- data.frame(
g = rep(c("A", "B", "C"), each = 8), s = rep(c("P", "Q"), 12), x = x, y = y,
colour = colour, facet = rep(c("L", "R"), each = 12),
m = c(qnorm(ppoints(12)), 6 + qnorm(ppoints(12))), t = as.double(rep(1:6, 4)))
export_dataset(raw, "EXP_PLOTS_01",
"Constructed: 24 rows in groups A-C with a linear response, one missing response, one outlier, a missing colour, a bimodal column m and a tied column t",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(g = "String", s = "String", x = "Float64", y = "Float64", colour = "String", facet = "String",
m = "Float64", t = "Float64"))
d <- read_dataset("EXP_PLOTS_01")
ok <- !is.na(d$y)
# ---- Plot: linear fit ------------------------------------------------------------------------
fit_case <- function(id, keep, title, kind, excluded, note) {
fit <- lm(y ~ x, data = d[keep & ok, ])
b <- coef(fit)
xs <- d$x[keep & ok]
emit(id, "tabelz.analysis.plot_preparation:linear_fit_data", "openPlotMenuItem", title, list(x = "x", y = "y"),
list("/slope" = num(b[["x"]], "stats::lm slope."), "/intercept" = num(b[["(Intercept)"]], "stats::lm intercept."),
"/r_squared" = num(summary(fit)$r.squared, "stats::lm R squared."),
"/valid_count" = exact(as.integer(sum(keep & ok)), "Rows with finite X and Y."),
"/points/0/y" = num(b[[1]] + b[[2]] * min(xs), "The line at the smallest valid X."),
"/points/47/y" = num(b[[1]] + b[[2]] * max(xs), "The line at the largest valid X.")),
c("Chapter 11: the optional fit is ordinary least squares over all included rows.", note),
kind = kind, excluded = excluded)
}
fit_case("STAT-EXP-FIT-001", rep(TRUE, 24), "OLS fit line with a missing response", "nominal", integer(),
"The missing response row is omitted.")
fit_case("STAT-EXP-FIT-002", seq_len(24) != 24, "OLS fit line with the outlier excluded", "missing_excluded", 23L,
"Excluded row 23 (0-based) never enters the fit.")
# ---- Plot: histogram and box summaries -------------------------------------------------------
v <- d$y[ok]
breaks <- seq(min(v), max(v), length.out = 5)
counts <- hist(v, breaks = breaks, right = FALSE, include.lowest = TRUE, plot = FALSE)$counts
emit("STAT-EXP-HIST-001", "tabelz.analysis.plot_preparation:histogram_data", "openPlotMenuItem",
"Four equal-width histogram bins", list(column = "y", bin_count = 4L),
list("/source_count" = exact(24L, "Every included row."), "/valid_count" = exact(23L, "Finite responses."),
"/minimum" = num(min(v), "Smallest finite response."), "/maximum" = num(max(v), "Largest finite response."),
"/bins/0/count" = exact(as.integer(counts[1]), "graphics::hist left-closed bins."),
"/bins/1/count" = exact(as.integer(counts[2]), "Second bin."),
"/bins/3/count" = exact(as.integer(counts[4]), "The last bin is closed and holds the outlier."),
"/bins/1/lower" = num(breaks[2], "Equal-width edge min + (max - min) / 4.")),
c("Equal-width bins from the minimum to the maximum; no finite value lies on an interior edge, so the edge convention does not matter."))
box <- list()
for (level in c("A", "B", "C")) {
w <- d$y[ok & d$g == level]
q <- quantile(w, c(0.25, 0.5, 0.75), type = 7, names = FALSE)
fence <- c(q[1] - 1.5 * (q[3] - q[1]), q[3] + 1.5 * (q[3] - q[1]))
box[[level]] <- list(w = w, q = q, whiskers = range(w[w >= fence[1] & w <= fence[2]]))
}
dist_out <- list("/source_count" = exact(24L, "Every included row."), "/valid_count" = exact(23L, "Finite responses."))
for (index in 0:2) {
level <- c("A", "B", "C")[index + 1]
b <- box[[level]]
p <- paste0("/groups/", index, "/")
dist_out[[paste0(p, "label")]] <- exact(level, "Groups in text order.")
dist_out[[paste0(p, "valid_count")]] <- exact(length(b$w), "Finite responses in the group.")
dist_out[[paste0(p, "minimum")]] <- num(min(b$w), "Group minimum.")
dist_out[[paste0(p, "median")]] <- num(b$q[2], "Group median.")
dist_out[[paste0(p, "maximum")]] <- num(max(b$w), "Group maximum.")
dist_out[[paste0(p, "q1")]] <- num(b$q[1], "Type 7 lower quartile.")
dist_out[[paste0(p, "q3")]] <- num(b$q[3], "Type 7 upper quartile.")
dist_out[[paste0(p, "whisker_high")]] <- num(b$whiskers[2], "Largest value within Q3 + 1.5 IQR (Tukey).")
}
emit("STAT-EXP-BOX-001", "tabelz.analysis.plot_preparation:distribution_data", "openPlotMenuItem",
"Box summaries split by group", list(y = "y", group = "g"), dist_out,
c("Quartiles use R type 7 (linear interpolation); the manual names no quantile definition.",
"Whiskers are Tukey's: the most extreme values within 1.5 IQR of the quartiles; C's outlier lies beyond."))
# ---- Multi-Plot ------------------------------------------------------------------------------
mp <- list()
for (index in 0:1) {
column <- c("x", "y")[index + 1]
w <- d[[column]][!is.na(d[[column]])]
p <- paste0("/summaries/", index, "/")
mp[[paste0(p, "n")]] <- exact(length(w), "Finite values.")
mp[[paste0(p, "mean")]] <- num(mean(w), "Arithmetic mean.")
mp[[paste0(p, "median")]] <- num(median(w), "Median.")
mp[[paste0(p, "std_dev")]] <- num(sd(w), "Sample standard deviation (n - 1).")
mp[[paste0(p, "lower_four_sigma")]] <- num(mean(w) - 4 * sd(w), "Mean - 4 SD.")
mp[[paste0(p, "upper_four_sigma")]] <- num(mean(w) + 4 * sd(w), "Mean + 4 SD.")
}
emit("STAT-EXP-MULTIPLOT-001", "tabelz.analysis.multi_plot:compute_multi_plot_summaries", "multiPlotMenuItem",
"Multi-Plot card statistics", list(columns = list("x", "y")), mp,
c("Chapter 11: N, mean, median, standard deviation and mean +- 4 sigma over all valid included values.",
"Sigma is the sample standard deviation with n - 1, the usual estimate of a process sigma."))
# ---- Grouped scatter -------------------------------------------------------------------------
valid <- ok
emit("STAT-EXP-SCATTER-001", "tabelz.analysis.grouped_scatter:grouped_scatter_data", "openPlotMenuItem",
"Colour and facet category counts", list(x = "x", y = "y", color = "colour", facet = "facet"),
list("/source_count" = exact(24L, "Every included row."), "/valid_count" = exact(23L, "Rows with numeric X and Y."),
"/colors/0/label" = exact("blue", "Categories in text order."),
"/colors/3/label" = exact("(missing)", "Missing values last as (missing)."),
"/colors/0/source_count" = exact(as.integer(sum(d$colour == "blue", na.rm = TRUE)), "Blue rows."),
"/colors/0/valid_count" = exact(as.integer(sum(d$colour == "blue" & valid, na.rm = TRUE)), "Blue rows with a response."),
"/colors/2/source_count" = exact(as.integer(sum(d$colour == "red", na.rm = TRUE)), "Red rows."),
"/colors/3/source_count" = exact(1L, "One missing colour."),
"/panels/1/label" = exact("R", "Facets in text order."),
"/panels/1/source_count" = exact(12L, "Rows in facet R."),
"/omitted_missing_count" = exact(0L, "Missing categories are shown."),
"/colors/0/rendered_count" = exact(as.integer(sum(d$colour == "blue" & valid, na.rm = TRUE)),
"Far below the 5,000-point budget, every valid point is drawn.")),
c("Chapter 11: categories ordered by text with (missing) last; counts use all rows; groups smaller than their budget share are drawn in full."),
oracle_class = "analytic")
emit("STAT-EXP-SCATTER-002", "tabelz.analysis.grouped_scatter:grouped_scatter_data", "openPlotMenuItem",
"Omitting the missing colour category", list(x = "x", y = "y", color = "colour", facet = "facet", missing_categories = "omit"),
list("/omitted_missing_count" = exact(1L, "The row with a missing colour is omitted and counted."),
"/colors/2/label" = exact("red", "Last shown category."), "/colors/3" = absent("No (missing) category.")),
c("Chapter 11: turning off Show missing categories omits those rows; the disclosure counts them."),
kind = "missing_excluded", oracle_class = "analytic")
# ---- Variation charts ------------------------------------------------------------------------
vc <- list("/n" = exact(23L, "Complete rows."), "/dropped" = exact(1L, "The missing response."),
"/overall_mean" = num(mean(v), "Mean of the complete responses."))
for (index in 0:2) {
level <- c("A", "B", "C")[index + 1]
w <- d$y[ok & d$g == level]
ci <- t.test(w, conf.level = 0.95)$conf.int
p <- paste0("/groups/", index, "/")
vc[[paste0(p, "primary")]] <- exact(level, "Groups in first-appearance order, which is also text order here.")
vc[[paste0(p, "n")]] <- exact(length(w), "Group size.")
vc[[paste0(p, "mean")]] <- num(mean(w), "Group mean.")
vc[[paste0(p, "std")]] <- num(sd(w), "Group sample standard deviation.")
vc[[paste0(p, "ci_lower")]] <- num(ci[1], "stats::t.test 95 percent interval.")
vc[[paste0(p, "ci_upper")]] <- num(ci[2], "stats::t.test 95 percent interval.")
}
emit("STAT-EXP-VARIATION-001", "tabelz.analysis.variation_charts:compute_variation_chart", "variationChartsMenuItem",
"Mean interval plot by group", list(kind = "interval", response = "y", primary = "g"), vc,
c("Chapter 11: Mean interval uses 95 percent t confidence intervals; incomplete rows are counted."))
cells <- split(d$y[ok], interaction(d$s[ok], d$g[ok], drop = TRUE))
within <- sum(vapply(cells, function(w) sum((w - mean(w))^2), 0))
pm <- tapply(d$y[ok], d$g[ok], mean)
sm <- tapply(d$y[ok], d$s[ok], mean)
emit("STAT-EXP-VARIATION-002", "tabelz.analysis.variation_charts:compute_variation_chart", "variationChartsMenuItem",
"Multi-vari chart summaries", list(kind = "multi_vari", response = "y", primary = "g", secondary = "s"),
list("/n" = exact(23L, "Complete rows."),
"/pooled_within_std" = num(sqrt(within / (sum(ok) - length(cells))), "Pooled within-cell SD, df = n - cells."),
"/primary_mean_range" = num(max(pm) - min(pm), "Range of the group means."),
"/secondary_mean_range" = num(max(sm) - min(sm), "Range of the panel means.")),
c("The pooled within-cell standard deviation pools the six g x s cells."), kind = "axis_coverage")
# ---- Multimodality ---------------------------------------------------------------------------
w <- d$m
n <- length(w)
m2 <- mean((w - mean(w))^2); m3 <- mean((w - mean(w))^3); m4 <- mean((w - mean(w))^4)
skew <- m3 / m2^1.5 * sqrt(n * (n - 1)) / (n - 2)
kurt <- ((n + 1) * (m4 / m2^2 - 3) + 6) * (n - 1) / ((n - 2) * (n - 3))
bc <- (skew^2 + 1) / (kurt + 3 * (n - 1)^2 / ((n - 2) * (n - 3)))
stopifnot(bc > 0.555)
emit("STAT-EXP-MULTIMODAL-001", "tabelz.analysis.multimodality:compute_multimodality", "multimodalityMenuItem",
"Bimodality coefficient of two separated modes", list(columns = list("m"), active_methods = list("BC")),
list("/columns/0/finite_count" = exact(24L, "All values are finite."),
"/columns/0/bc_value" = num(bc, "Sarle's coefficient with adjusted skewness G1 and excess kurtosis G2."),
"/columns/0/passed" = contains("BC", "BC exceeds the 5/9 uniform benchmark.")),
c("Sarle's bimodality coefficient (SAS definition); the manual does not define it.",
"The column is two normal-score blocks of 12, 6 units apart."), oracle_class = "analytic")
# ---- Categorical bar counts (plot builder, categorical Y) -----------------------------------
# Chapter 11: a categorical Y draws exact category counts, alphabetical with the
# missing values as one "(missing)" bar placed last. radix sorting is byte
# (code-point) order, independent of the R locale.
cb <- "tabelz.analysis.plot_preparation:categorical_bar_data"
bar_counts <- function(values) {
levels <- sort(unique(values[!is.na(values)]), method = "radix")
counts <- vapply(levels, function(level) sum(values == level, na.rm = TRUE), integer(1))
if (any(is.na(values))) {
levels <- c(levels, "(missing)")
counts <- c(counts, sum(is.na(values)))
}
list(labels = unname(levels), counts = unname(as.integer(counts)))
}
bar_outputs <- function(values, why) {
b <- bar_counts(values)
out <- list()
for (k in seq_along(b$labels)) {
out[[paste0("/categories/", k - 1, "/label")]] <- exact(b$labels[k], why)
out[[paste0("/categories/", k - 1, "/count")]] <- exact(b$counts[k], "Exact count of rows with this label.")
}
out[[paste0("/categories/", length(b$labels))]] <- absent("No further categories.")
out[["/source_count"]] <- exact(length(values), "Every source row.")
out[["/missing_count"]] <- exact(sum(is.na(values)), "Missing labels.")
out[["/valid_count"]] <- exact(sum(!is.na(values)), "Labelled rows.")
out
}
emit("STAT-EXP-BARS-001", cb, "openPlotMenuItem", "Categorical Y bars: exact counts with the missing bar last",
list(column = "colour"), bar_outputs(d$colour, "Alphabetical categories, (missing) last."),
c("Chapter 11: categorical Y draws exact category counts, alphabetical, (missing) last.",
"Order is code-point order (R sort method = 'radix'), independent of locale."),
oracle_class = "analytic")
emit("STAT-EXP-BARS-002", cb, "openPlotMenuItem", "Categorical Y bars without an excluded row",
list(column = "colour"), bar_outputs(d$colour[-1], "Alphabetical categories, (missing) last."),
c("An explicitly excluded row (row 0, red) leaves every count and the source total."),
kind = "missing_excluded", excluded = 0L, oracle_class = "analytic")
emit("STAT-EXP-BARS-003", cb, "openPlotMenuItem", "Categorical Y bars with no missing labels",
list(column = "g"), bar_outputs(d$g, "Alphabetical categories; no (missing) bar without missing labels."),
c("With no missing labels no (missing) bar is drawn."), oracle_class = "analytic")
# ---- Explain multimodality (chapter 11) ------------------------------------------------------
# Constructed so the documented rules fully determine the answer: the modes are
# separated by 24 units with a within-mode spread of 1.2 (normal scores), so each
# row's basin is its construction mode, and every rule is either exactly pure or
# absent. Which of two equally informative columns the tree splits first can
# depend on the seeded held-out rows, so the interaction case asserts only
# order-free facts through [key=value] selectors.
me <- "tabelz.analysis.mode_explainer:explain_modes"; mm <- "explainMultimodalityMenuItem"
cells1 <- expand.grid(i = 1:12, driver = c("A", "B"), kind = c("CAT", "DOG"), shift = c("day", "night"),
stringsAsFactors = FALSE)
single <- data.frame(value = ifelse(cells1$driver == "B", 12, -12) + 1.2 * qnorm(ppoints(12))[cells1$i],
driver = cells1$driver, unrelated = c("p", "q", "r")[(cells1$i - 1) %% 3 + 1])
export_dataset(single, "EXP_MODES_01",
"Constructed: 96 rows, two modes at -12 and 12 separated exactly by driver; unrelated is balanced within each mode",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(value = "Float64", driver = "String", unrelated = "String"))
emit("STAT-EXP-MODEEXPLAIN-001", me, mm, "One categorical column separates the two modes",
list(column = "value"),
list("/mode_count" = exact(2L, "Two constructed modes 24 units apart."),
"/root/split_feature" = exact("driver", "driver alone separates the modes."),
"/candidates/0/feature" = exact("driver", "Ranked first by its own gain."),
"/candidates/0/gain_normalized" = num(1, "Perfect separation removes all mode entropy."),
"/rules/[mode=0]/conditions/[feature=driver]/label" = exact("= A", "Lower mode is driver A."),
"/rules/[mode=0]/support" = exact(48L, "All 48 A rows."),
"/rules/[mode=0]/purity" = num(1, "Pure on training rows."),
"/rules/[mode=0]/validation_purity" = num(1, "Pure on held-out rows."),
"/rules/[mode=1]/conditions/[feature=driver]/label" = exact("= B", "Upper mode is driver B."),
"/rules/[mode=1]/support" = exact(48L, "All 48 B rows."),
"/rules/[mode=1]/purity" = num(1, "Pure on training rows."),
"/rules/2" = absent("One rule per driver level.")),
c("Chapter 11: rows are labelled by density basin, modes numbered from the lowest values upward; each leaf is one rule.",
"A column that separates the modes exactly has normalized information gain 1 and yields pure rules."),
oracle_class = "analytic", dataset = "EXP_MODES_01")
per <- 40
cells2 <- expand.grid(i = 1:per, kind = c("CAT", "DOG"), shift = c("day", "night"), stringsAsFactors = FALSE)
upper <- cells2$kind == "CAT" & cells2$shift == "night"
inter_value <- numeric(nrow(cells2))
inter_value[upper] <- 12 + 1.2 * qnorm(ppoints(sum(upper)))
inter_value[!upper] <- -12 + 1.2 * qnorm(ppoints(sum(!upper)))
inter <- data.frame(value = inter_value, kind = cells2$kind, shift = cells2$shift)
export_dataset(inter, "EXP_MODES_02",
"Constructed: 160 rows; only kind = CAT AND shift = night (40 rows) forms the upper mode at 12",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(value = "Float64", kind = "String", shift = "String"))
emit("STAT-EXP-MODEEXPLAIN-002", me, mm, "Only a two-column conjunction identifies the upper mode",
list(column = "value"),
list("/mode_count" = exact(2L, "Upper mode of 40 rows, lower mode of 120."),
"/rules/[mode=1]/conditions/[feature=kind]/label" = exact("= CAT", "Part of the conjunction."),
"/rules/[mode=1]/conditions/[feature=shift]/label" = exact("= night", "Part of the conjunction."),
"/rules/[mode=1]/conditions/2" = absent("Exactly two conditions."),
"/rules/[mode=1]/support" = exact(as.integer(sum(upper)), "The 40 constructed rows."),
"/rules/[mode=1]/purity" = num(1, "Pure on training rows."),
"/rules/[mode=1]/validation_purity" = num(1, "Pure on held-out rows.")),
c("Chapter 11: a mode identified only by a combination of columns appears as one rule with both conditions.",
"Neither column separates the modes alone; the split order is not asserted."),
oracle_class = "analytic", dataset = "EXP_MODES_02")
# ---- Bars, heatmap and ECDF ------------------------------------------------------------------
ex <- "tabelz.executor.exploration_extensions"; em <- "explorationExtensionsMenuItem"
tab <- table(d$g, d$s)
emit("STAT-EXP-GRAPH-001", ex, em, "Grouped counts with overall percentages",
list(graph_kind = "bar", group_columns = list("g", "s"), denominator = "overall"),
list("/bars/groups/0/label" = exact("A", "Alphabetical groups."),
"/bars/groups/0/source_count" = exact(8L, "Rows in A."),
"/bars/groups/0/segments/1/label" = exact("Q", "Series in alphabetical order."),
"/bars/groups/0/segments/1/value" = num(tab["A", "Q"], "Count of A and Q."),
"/bars/groups/2/segments/0/percentage" = num(100 * tab["C", "P"] / sum(tab), "Divided by the grand total."),
"/bars/groups/2/segments/0/denominator_total" = exact(24L, "Overall denominator.")),
c("Chapter 63: overall percentages divide by the grand total."), oracle_class = "analytic")
emit("STAT-EXP-GRAPH-002", ex, em, "Mean bars skip missing values but keep source rows",
list(graph_kind = "bar", group_columns = list("g"), aggregation = "mean", value_column = "y"),
list("/bars/groups/0/segments/0/value" = num(mean(d$y[ok & d$g == "A"]), "Mean of A's valid values."),
"/bars/groups/0/segments/0/valid_n" = exact(7L, "A's valid values."),
"/bars/groups/0/source_count" = exact(8L, "The missing row stays in the source count."),
"/bars/groups/2/segments/0/value" = num(mean(d$y[d$g == "C"]), "C's mean includes the outlier.")),
c("Chapter 63: rows with a missing value stay in the group's source-row count but do not enter the summary."),
kind = "missing_excluded")
cnt <- function(level, col) as.integer(sum(d$g == level & (if (is.na(col)) is.na(d$colour) else d$colour %in% col)))
emit("STAT-EXP-GRAPH-003", ex, em, "Count heatmap with a missing column category",
list(graph_kind = "heatmap", row_column = "g", column_column = "colour"),
list("/heatmap/column_labels" = seq_exact(c("blue", "green", "red", "(missing)"), "Alphabetical, missing last."),
"/heatmap/cells/0/value" = num(cnt("A", "blue"), "A x blue."),
"/heatmap/cells/3/value" = num(cnt("A", NA), "A x (missing)."),
"/heatmap/cells/7/value" = num(cnt("B", NA), "B has no missing colour: a counted cell shows zero."),
"/heatmap/cells/10/value" = num(cnt("C", "red"), "C x red.")),
c("Chapter 63: every observed combination is emitted and counted cells show zero; cells run row by row."),
oracle_class = "analytic")
e <- ecdf(d$t)
emit("STAT-EXP-GRAPH-004", ex, em, "ECDF of a tied column",
list(graph_kind = "ecdf", value_column = "t"),
list("/ecdf/series/0/source_count" = exact(24L, "All values."),
"/ecdf/series/0/points/0/cumulative" = num(e(1), "stats::ecdf at 1."),
"/ecdf/series/0/points/0/tie_count" = exact(4L, "Four rows hold each value."),
"/ecdf/series/0/points/3/cumulative" = num(e(4), "stats::ecdf at 4."),
"/ecdf/series/0/points/6" = absent("Six distinct values give six steps.")),
c("Chapter 63: the ECDF steps through sorted distinct values with exact cumulative proportions."))
e2 <- ecdf(d$t[-1])
emit("STAT-EXP-GRAPH-005", ex, em, "An excluded row leaves the ECDF",
list(graph_kind = "ecdf", value_column = "t"),
list("/excluded_count" = exact(1L, "One excluded row."),
"/ecdf/series/0/source_count" = exact(23L, "Remaining values."),
"/ecdf/series/0/points/0/cumulative" = num(e2(1), "stats::ecdf without row 0."),
"/ecdf/series/0/points/0/tie_count" = exact(3L, "Three remaining rows hold 1.")),
c("Chapter 63: the workflow computes every graph with explicitly excluded rows removed."),
kind = "missing_excluded", excluded = 0L)
generators/r/hypothesis_analysis_of_means.R
source("common.R", local = TRUE)
# Analysis of means (Statistics > Compare means & ANOVA > Analysis of means (ANOM)).
# Decision limits are centre +/- h SE_i. With two groups (or two levels) the deviations are
# perfectly anticorrelated and h is the two-sided t (or normal) quantile exactly; with three
# groups h is the quantile of the maximum absolute studentized deviation, integrated with
# deterministic quadrature over the singular correlation of the deviations. Everything is from the
# re-read HYP_ANOM CSV.
i <- seq_len(16L)
constructed <- data.frame(
y = c(9.8, 10.4, 9.1, 10.9, 10.2, 11.6, 9.5, 12.3, 11.9, 12.8, 10.7, 13.1, 12.4, 11.2, 13.6, 12.0),
g2 = c(rep("A", 7), rep("B", 9)),
g3 = c(rep("A", 5), rep("B", 5), rep("C", 6)),
fa = ifelse((i - 1) %/% 8 == 0, "low", "high"),
fb = ifelse(((i - 1) %/% 4) %% 2 == 0, "cold", "warm"),
events = c(9L, 12L, 8L, 14L, 11L, 13L, 10L, 15L, 12L, 16L, 9L, 17L, 13L, 11L, 18L, 14L),
trials = rep(40L, 16),
counts = c(6L, 9L, 5L, 8L, 7L, 10L, 6L, 11L, 9L, 12L, 8L, 13L, 10L, 9L, 14L, 11L),
exposure = c(1.5, 2.0, 1.2, 1.8, 1.6, 2.2, 1.4, 2.4, 2.1, 2.6, 1.9, 2.8, 2.3, 2.0, 3.0, 2.5),
stringsAsFactors = FALSE
)
export_dataset(
constructed, "HYP_ANOM", "Constructed by hypothesis_analysis_of_means.R",
"R stats::qt, qnorm, integrate, uniroot (three-group critical value by quadrature)",
"Constructed test data; no third-party dataset license.",
c(y = "Float64", g2 = "String", g3 = "String", fa = "String", fb = "String",
events = "Int64", trials = "Int64", counts = "Int64", exposure = "Float64")
)
d <- read_dataset("HYP_ANOM")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label, tier = "1e-9", absolute = "0")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = absolute,
relative_tolerance = tier, rationale = paste("R", label, "from re-read HYP_ANOM CSV."))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
word <- function(x, label) list(comparison = "exact", value = as.character(x), rationale = label)
entry <- "tabelz.analysis.analysis_of_means:compute_analysis_of_means"
menu <- "analysisOfMeansMenuItem"
notes_common <- c(
"Groups in sorted label order; centre is the weighted grand mean (pooled proportion or rate); limits are centre +/- h SE_i; a group is 'above', 'below' or 'within'.",
"docs/manual/chapters/99-analysis-of-means.tex:10-19: limits ybar +/- h s_p sqrt((N - n_i)/(N n_i)) with pooled SD s_p and the joint critical value depending on the group sizes and residual df.",
"docs/manual/chapters/99-analysis-of-means.tex:53-60: two-factor ANOM with alpha per chart; binomial ANOM pools successes and trials; Poisson ANOM pools counts and exposure."
)
emit <- function(id, title, kind, config, outputs, notes, source_ref, citation, oracle_class = "independent_implementation") {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs, expected_error = NULL,
deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/HYP_ANOM.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = oracle_class, source = source_ref, citation = citation,
generator = list(path = "generators/r/hypothesis_analysis_of_means.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, notes_common, "Expected computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-ANALYSIS-OF-MEANS-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# P(max_i |T_i| <= h) for k = 3 weighted deviations D_i = ybar_i - ybar from their weighted
# grand mean, by deterministic quadrature. Z_i = D_i / sd_i with sd_i^2 = 1/w_i - 1/W; the
# constraint sum(w_i D_i) = 0 fixes Z_3 from (Z_1, Z_2), which are bivariate normal with
# correlation -sqrt(w_1 w_2 / ((W - w_1)(W - w_2))). The inner integral over Z_2 is a normal
# interval probability in closed form; the outer integral over Z_1 is split at the kinks of the
# admissible Z_2 interval. With finite df the normal probability is integrated over the
# independent scale S = sqrt(chi2_df / df). Checked against mvtnorm::pmvnorm to 3e-9 and
# invariant to the order of the groups.
anom_prob_k3 <- function(c, w, df) {
W <- sum(w); sd <- sqrt((W - w) / (w * W)); rho <- -sqrt(w[1] * w[2] / ((W - w[1]) * (W - w[2])))
alpha <- w[1] * sd[1] / (w[3] * sd[3]); beta <- w[2] * sd[2] / (w[3] * sd[3]); s <- sqrt(1 - rho^2)
normal <- function(cc) {
f <- function(z1) {
lo <- pmax(-cc, (-cc - alpha * z1) / beta); hi <- pmin(cc, (cc - alpha * z1) / beta)
ifelse(hi > lo, stats::dnorm(z1) * (stats::pnorm((hi - rho * z1) / s) - stats::pnorm((lo - rho * z1) / s)), 0)
}
kinks <- sort(unique(c(-cc, cc, cc * (1 - beta) / alpha, -cc * (1 - beta) / alpha,
cc * (1 + beta) / alpha, -cc * (1 + beta) / alpha)))
kinks <- kinks[kinks >= -cc & kinks <= cc]
total <- 0
for (j in seq_len(length(kinks) - 1)) {
total <- total + stats::integrate(f, kinks[j], kinks[j + 1], rel.tol = 1e-13, abs.tol = 0, subdivisions = 500L)$value
}
total
}
if (is.null(df)) return(normal(c))
g <- function(sv) vapply(sv, function(si) normal(c * si) * 2 * df * si * stats::dchisq(df * si^2, df), 0)
stats::integrate(g, 0, Inf, rel.tol = 1e-11, abs.tol = 0, subdivisions = 500L)$value
}
anom_h <- function(w, alpha, df) {
if (length(w) == 2) {
return(if (is.null(df)) stats::qnorm(1 - alpha / 2) else stats::qt(1 - alpha / 2, df))
}
stopifnot(length(w) == 3)
stats::uniroot(function(h) anom_prob_k3(h, w, df) - (1 - alpha), c(1.5, 4), tol = 1e-12)$root
}
panel_outputs <- function(index, center, h, df, groups, tier, absolute) {
# groups: data.frame(label, size, statistic, se, rows)
prefix <- paste0("/panels/", index)
outputs <- list()
outputs[[paste0(prefix, "/center")]] <- tol(center, "centre line")
outputs[[paste0(prefix, "/critical_value")]] <- tol(h, "critical value h", tier, absolute)
outputs[[paste0(prefix, "/critical_df")]] <- if (is.null(df)) list(comparison = "absent", rationale = "normal approximation: no residual df") else exact(df, "residual df of the critical value")
signals <- 0L
for (g in seq_len(nrow(groups))) {
gp <- paste0(prefix, "/groups/", g - 1)
lower <- center - h * groups$se[g]
upper <- center + h * groups$se[g]
decision <- if (groups$statistic[g] > upper) "above" else if (groups$statistic[g] < lower) "below" else "within"
signals <- signals + (decision != "within")
outputs[[paste0(gp, "/label")]] <- word(groups$label[g], "group label")
outputs[[paste0(gp, "/size")]] <- tol(groups$size[g], "group weight (size, trials or exposure)")
outputs[[paste0(gp, "/rows")]] <- exact(groups$rows[g], "rows in the group")
outputs[[paste0(gp, "/statistic")]] <- tol(groups$statistic[g], "group mean, proportion or rate")
outputs[[paste0(gp, "/standard_error")]] <- tol(groups$se[g], "SE of the deviation")
outputs[[paste0(gp, "/lower")]] <- tol(lower, "lower decision limit", tier, absolute)
outputs[[paste0(gp, "/upper")]] <- tol(upper, "upper decision limit", tier, absolute)
outputs[[paste0(gp, "/decision")]] <- word(decision, "decision against the limits")
}
outputs[[paste0(prefix, "/signals")]] <- exact(signals, "groups outside their limits")
outputs
}
normal_one_way <- function(id, title, kind, group, alpha) {
cc <- d[is.finite(d$y) & !is.na(d[[group]]), ]
labels <- sort(unique(cc[[group]]))
n <- as.vector(table(factor(cc[[group]], levels = labels)))
means <- as.vector(tapply(cc$y, factor(cc[[group]], levels = labels), mean))
ss <- as.vector(tapply(cc$y, factor(cc[[group]], levels = labels), function(v) sum((v - mean(v))^2)))
N <- sum(n); k <- length(n); df <- N - k
pooled <- sqrt(sum(ss) / df)
center <- sum(n * means) / N
h <- anom_h(n, alpha, df)
closed <- k == 2
groups <- data.frame(label = labels, size = n, statistic = means,
se = pooled * sqrt((N - n) / (N * n)), rows = n)
outputs <- panel_outputs(0, center, h, df, groups,
if (closed) "1e-9" else "1e-6", "0")
outputs[["/pooled_sd"]] <- tol(pooled, "pooled within-group SD")
outputs[["/residual_df"]] <- exact(df, "N - k")
outputs[["/observations"]] <- exact(N, "analysed rows")
outputs[["/missing_count"]] <- exact(nrow(d) - N, "rows missing a role")
emit(id, title, kind,
list(analysis = "normal_one_way", response = "y", group = group, alpha = sprintf("%.2f", alpha)),
outputs,
if (closed) "Two groups: the two deviations are exactly anticorrelated, so P(max |T_i| <= h) = P(|t_df| <= h) and h = qt(1 - alpha/2, N - k) in closed form."
else "Three groups: h solves P(max |T_i| <= h) = 1 - alpha with T = Z / S, Z jointly normal with the singular deviation correlation -sqrt(n_i n_j / ((N - n_i)(N - n_j))) and S the scaled chi on N - k df; the probability is a deterministic quadrature (closed-form inner normal interval, integrate over Z_1 and over S, rel.tol 1e-11) and h a uniroot to 1e-12. Both sides are numerical integrations, hence 1e-6 relative on h and the limits.",
if (closed) "R 4.6.1 stats::qt" else "R 4.6.1 stats::integrate, uniroot",
if (closed) "Nelson LS (1982), Journal of Quality Technology 14, 172-173 (two-group ANOM reduces to the t quantile); R Core Team, stats package help: qt."
else "Nelson LS (1982), Journal of Quality Technology 14, 172-173 (the max |T_i| distribution of ANOM); R Core Team, stats package help: integrate, uniroot.")
}
normal_one_way("STAT-HYP-ANALYSIS-OF-MEANS-001", "ANOM normal two groups", "nominal", "g2", 0.05)
normal_one_way("STAT-HYP-ANALYSIS-OF-MEANS-002", "ANOM normal three groups", "axis_coverage", "g3", 0.05)
normal_one_way("STAT-HYP-ANALYSIS-OF-MEANS-003", "ANOM normal two groups alpha 0.01", "axis_coverage", "g2", 0.01)
# 004: balanced two-way 2 x 2 with four replicates: every panel has two levels or a 2 x 2
# interaction, so each critical value is qt(1 - alpha/2, ab(r - 1)) in closed form.
cc <- d
levels_a <- sort(unique(cc$fa)); levels_b <- sort(unique(cc$fb))
a <- length(levels_a); b <- length(levels_b)
cell_mean <- matrix(NA_real_, a, b); cell_ss <- 0; r <- NA
for (x in seq_len(a)) for (y_ in seq_len(b)) {
v <- cc$y[cc$fa == levels_a[x] & cc$fb == levels_b[y_]]
r <- length(v); cell_mean[x, y_] <- mean(v); cell_ss <- cell_ss + sum((v - mean(v))^2)
}
df <- a * b * (r - 1)
root <- sqrt(cell_ss / df)
grand <- mean(cell_mean)
row_means <- rowMeans(cell_mean); col_means <- colMeans(cell_mean)
h <- stats::qt(0.975, df)
outputs <- panel_outputs(0, grand, h, df,
data.frame(label = levels_a, size = b * r, statistic = row_means, se = root * sqrt((a - 1) / (a * b * r)), rows = b * r), "1e-9", "0")
outputs <- c(outputs, panel_outputs(1, grand, h, df,
data.frame(label = levels_b, size = a * r, statistic = col_means, se = root * sqrt((b - 1) / (a * b * r)), rows = a * r), "1e-9", "0"))
contrast <- cell_mean - row_means - rep(col_means, each = a) + grand
inter <- data.frame(label = as.vector(t(outer(levels_a, levels_b, paste, sep = " / "))), size = r,
statistic = as.vector(t(contrast)), se = root * sqrt((a - 1) * (b - 1) / (a * b * r)), rows = r)
outputs <- c(outputs, panel_outputs(2, 0, h, df, inter, "1e-9", "0"))
outputs[["/pooled_sd"]] <- tol(root, "root residual mean square")
outputs[["/residual_df"]] <- exact(df, "ab(r - 1)")
emit("STAT-HYP-ANALYSIS-OF-MEANS-004", "ANOM normal two-way 2 x 2", "axis_coverage",
list(analysis = "normal_two_way", response = "y", factor_a = "fa", factor_b = "fb", alpha = "0.05"),
outputs, c("Balanced 2 x 2 x 4: main-effect panels use the residual mean square on ab(r - 1) = 12 df with SE = s sqrt((a - 1)/(abr)); the interaction panel shows d_ij = m_ij - m_i. - m_.j + m with SE = s sqrt((a-1)(b-1)/(abr)) centred at zero, cells in row-major order 'fa / fb'. Every panel has two levels or a 2 x 2 contrast set, so h = qt(0.975, 12) exactly.",
"Panel order: factor A (fa), factor B (fb), interaction; levels sorted ('high' before 'low', 'cold' before 'warm')."),
"R 4.6.1 stats::qt", "Nelson PR, Wludyka PS, Copeland KAF (2005), The Analysis of Means, SIAM, ch. 6 (two-way ANOM; a 2 x 2 interaction reduces to a t quantile); R Core Team, stats package help: qt.")
# 005: binomial proportions in three groups (normal approximation, h from qmvnorm).
labels <- sort(unique(d$g3))
x <- as.vector(tapply(d$events, factor(d$g3, levels = labels), sum))
n <- as.vector(tapply(d$trials, factor(d$g3, levels = labels), sum))
rows <- as.vector(table(factor(d$g3, levels = labels)))
N <- sum(n); p_bar <- sum(x) / N
h <- anom_h(n, 0.05, NULL)
groups <- data.frame(label = labels, size = n, statistic = x / n,
se = sqrt(p_bar * (1 - p_bar)) / sqrt(n) * sqrt((N - n) / N), rows = rows)
outputs <- panel_outputs(0, p_bar, h, NULL, groups, "1e-6", "0")
for (g in seq_along(labels)) outputs[[paste0("/panels/0/groups/", g - 1, "/events")]] <- tol(x[g], "pooled successes of the group")
outputs[["/observations"]] <- exact(nrow(d), "analysed rows")
emit("STAT-HYP-ANALYSIS-OF-MEANS-005", "ANOM binomial three groups", "axis_coverage",
list(analysis = "binomial", group = "g3", events = "events", trials = "trials", alpha = "0.05"),
outputs, "Binomial: p_bar = sum x / sum n; SE_i = sqrt(p_bar (1 - p_bar) / n_i) sqrt((N - n_i)/N); h solves P(max |Z_i| <= h) = 0.95 over the deviation correlation with weights n_i (infinite df) by the same deterministic quadrature as the three-group normal case; 1e-6 relative tolerance on h and the limits.",
"R 4.6.1 stats::integrate, uniroot", "Nelson PR, Wludyka PS, Copeland KAF (2005), The Analysis of Means, SIAM, ch. 2; R Core Team, stats package help: integrate, uniroot.")
# 006: Poisson rates per unit exposure in two groups (closed-form normal quantile).
labels <- sort(unique(d$g2))
c_ <- as.vector(tapply(d$counts, factor(d$g2, levels = labels), sum))
e <- as.vector(tapply(d$exposure, factor(d$g2, levels = labels), sum))
rows <- as.vector(table(factor(d$g2, levels = labels)))
E <- sum(e); rate <- sum(c_) / E
h <- stats::qnorm(0.975)
groups <- data.frame(label = labels, size = e, statistic = c_ / e, se = sqrt(rate * (1 / e - 1 / E)), rows = rows)
outputs <- panel_outputs(0, rate, h, NULL, groups, "1e-9", "0")
for (g in seq_along(labels)) outputs[[paste0("/panels/0/groups/", g - 1, "/events")]] <- tol(c_[g], "pooled counts of the group")
emit("STAT-HYP-ANALYSIS-OF-MEANS-006", "ANOM Poisson rates two groups with exposure", "axis_coverage",
list(analysis = "poisson", group = "g2", events = "counts", exposure = "exposure", exposure_unit = "hour", alpha = "0.05"),
outputs, "Poisson: lambda_hat = sum c / sum e; rate_i = c_i / e_i; SE_i = sqrt(lambda_hat (1/e_i - 1/E)); two groups give h = qnorm(0.975) exactly.",
"R 4.6.1 stats::qnorm", "Nelson PR, Wludyka PS, Copeland KAF (2005), The Analysis of Means, SIAM, ch. 2; R Core Team, stats package help: qnorm.")
cat("analysis of means cases generated\n")
generators/r/hypothesis_anova_post_hoc.R
source("common.R", local = TRUE)
# One-way ANOVA with post hoc comparisons and planned contrasts
# (Statistics > Compare means & ANOVA > One-way ANOVA). Every reference is computed from
# the re-read HYP_POSTHOC CSV with R stats (aov, TukeyHSD, pairwise.t.test, ptukey, qt, pf)
# and deterministic quadrature for Dunnett's many-to-one family.
constructed <- data.frame(
y = c(10.2, 11.5, 9.8, 12.1, 10.9,
13.4, 12.2, 14.8, 13.9, 12.6, 15.1,
11.0, 9.4, 12.6, 10.3, 13.8, 8.9, 11.7,
NA),
group = c(rep("A", 5), rep("B", 6), rep("C", 7), "B"),
stringsAsFactors = FALSE
)
export_dataset(
constructed, "HYP_POSTHOC", "Constructed by hypothesis_anova_post_hoc.R",
"R stats::aov, TukeyHSD, pairwise.t.test, ptukey, qtukey, qt, pf, integrate, uniroot",
"Constructed test data; no third-party dataset license.",
c(y = "Float64", group = "String")
)
d <- read_dataset("HYP_POSTHOC")
run <- generator_run(c("stats", "jsonlite"))
cc <- d[is.finite(d$y) & !is.na(d$group), ]
cc$group <- factor(cc$group, levels = sort(unique(cc$group)))
tol <- function(x, label, tier = "1e-9", absolute = "0")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = absolute,
relative_tolerance = tier, rationale = paste("R", label, "from re-read HYP_POSTHOC CSV."))
prob <- function(x, label, tier = "1e-8")
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = tier,
rationale = paste("R", label, "from re-read HYP_POSTHOC CSV."))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
flag <- function(x, label) list(comparison = "exact", value = isTRUE(x), rationale = label)
entry <- "tabelz.analysis.anova_post_hoc:compute_anova_post_hoc"
menu <- "anovaPostHocMenuItem"
notes_common <- c(
"Groups in sorted label order A, B, C (n = 5, 6, 7); one row with a missing response is dropped and counted.",
"Pairwise rows are the combinations (A,B), (A,C), (B,C); difference = first mean minus second mean; SE = s sqrt(1/n_i + 1/n_j) with s^2 the ANOVA MSE on N - k df.",
"docs/manual/chapters/83-repeated-anova-and-contrasts.tex:127-129: 'offers independent-group procedures and planned contrast families. Choose a method whose variance and comparison assumptions match the design.'",
"docs/manual/chapters/83-repeated-anova-and-contrasts.tex:133-143: the procedure table (LSD individual; Bonferroni, Sidak, Holm declared family; Tukey/Tukey-Kramer pooled; Dunnett against one control; Scheffe over the contrast space; Games-Howell without equal variances)."
)
emit <- function(id, title, kind, config, outputs, notes, source_ref, citation) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs, expected_error = NULL,
deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/HYP_POSTHOC.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/hypothesis_anova_post_hoc.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, notes_common, "Expected computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-ANOVA-POST-HOC-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# ---- ANOVA table and group summaries shared by every case ------------------------------------
fit <- stats::aov(y ~ group, data = cc)
table <- summary(fit)[[1]]
k <- nlevels(cc$group)
n_i <- as.vector(table(cc$group))
means <- tapply(cc$y, cc$group, mean)
vars <- tapply(cc$y, cc$group, stats::var)
df_w <- table$Df[2]
mse <- table[["Mean Sq"]][2]
s <- sqrt(mse)
pairs <- utils::combn(k, 2)
labels <- levels(cc$group)
anova_outputs <- function() {
outputs <- list(
"/anova/between_ss" = tol(table[["Sum Sq"]][1], "aov between-groups SS"),
"/anova/within_ss" = tol(table[["Sum Sq"]][2], "aov within-groups SS"),
"/anova/between_df" = exact(table$Df[1], "k - 1"),
"/anova/within_df" = exact(df_w, "N - k"),
"/anova/mse" = tol(mse, "aov residual mean square"),
"/anova/f_value" = tol(table[["F value"]][1], "aov F"),
"/anova/p_value" = prob(table[["Pr(>F)"]][1], "aov F tail"),
"/missing_count" = exact(nrow(d) - nrow(cc), "rows with a missing response"),
"/excluded_count" = exact(0L, "no excluded rows")
)
for (g in seq_len(k)) {
outputs[[paste0("/groups/", g - 1, "/n")]] <- exact(n_i[g], paste("size of group", labels[g]))
outputs[[paste0("/groups/", g - 1, "/mean")]] <- tol(means[g], paste("mean of group", labels[g]))
outputs[[paste0("/groups/", g - 1, "/variance")]] <- tol(vars[g], paste("sample variance of group", labels[g]))
}
outputs
}
pair_base <- function(index, i, j, outputs, tier = "1e-9") {
prefix <- paste0("/comparisons/rows/", index - 1)
se <- s * sqrt(1 / n_i[i] + 1 / n_i[j])
outputs[[paste0(prefix, "/difference")]] <- tol(means[i] - means[j], paste(labels[i], "minus", labels[j]), tier)
outputs[[paste0(prefix, "/standard_error")]] <- tol(se, "pooled SE of the difference", tier)
outputs[[paste0(prefix, "/statistic")]] <- tol((means[i] - means[j]) / se, "t on the pooled SE", tier)
outputs[[paste0(prefix, "/df")]] <- tol(df_w, "pooled df", tier)
outputs
}
raw_p <- function(i, j) {
t <- (means[i] - means[j]) / (s * sqrt(1 / n_i[i] + 1 / n_i[j]))
2 * stats::pt(-abs(t), df_w)
}
# 001: Tukey HSD (Tukey-Kramer for unequal n) via TukeyHSD.
hsd <- stats::TukeyHSD(fit, "group", conf.level = 0.95)$group
outputs <- anova_outputs()
outputs[["/comparisons/critical_value"]] <- tol(stats::qtukey(0.95, k, df_w), "qtukey(0.95, k, df)", "1e-6")
for (index in seq_len(ncol(pairs))) {
i <- pairs[1, index]; j <- pairs[2, index]
outputs <- pair_base(index, i, j, outputs)
row <- hsd[paste0(labels[j], "-", labels[i]), ] # TukeyHSD reports second minus first
prefix <- paste0("/comparisons/rows/", index - 1)
outputs[[paste0(prefix, "/p_adjusted")]] <- prob(row[["p adj"]], "TukeyHSD adjusted p (ptukey)", "1e-6")
outputs[[paste0(prefix, "/lower")]] <- tol(-row[["upr"]], "TukeyHSD interval (qtukey), sign reversed to first minus second", "1e-6")
outputs[[paste0(prefix, "/upper")]] <- tol(-row[["lwr"]], "TukeyHSD interval (qtukey), sign reversed to first minus second", "1e-6")
outputs[[paste0(prefix, "/significant")]] <- flag(row[["p adj"]] <= 0.05, "adjusted p at or below alpha")
}
emit("STAT-HYP-ANOVA-POST-HOC-001", "One-way ANOVA Tukey-Kramer", "nominal",
list(response = "y", group = "group", procedure = "tukey", alpha = 0.05),
outputs, "TukeyHSD: adjusted p = ptukey(|diff| / (s sqrt((1/n_i + 1/n_j)/2)), k, df, lower = FALSE); interval half-width qtukey(0.95, k, df) s sqrt((1/n_i + 1/n_j)/2). TukeyHSD labels rows second-first, so the interval is negated. R documents qtukey as 'accurate to the 4th decimal place' (secant inversion of ptukey), so ptukey/qtukey-derived values are compared at 1e-6 relative rather than 1e-9.",
"R 4.6.1 stats::aov, TukeyHSD, qtukey", "R Core Team, stats package help: aov, TukeyHSD, ptukey, qtukey.")
# 002/003/005: Bonferroni, Holm and LSD through pairwise.t.test with the pooled SD.
simple_case <- function(id, title, procedure, adjust, alpha = 0.05) {
pt <- stats::pairwise.t.test(cc$y, cc$group, p.adjust.method = adjust, pool.sd = TRUE)$p.value
c <- ncol(pairs)
outputs <- anova_outputs()
critical <- switch(procedure,
lsd = stats::qt(1 - alpha / 2, df_w),
bonferroni = stats::qt(1 - alpha / (2 * c), df_w),
NULL)
if (!is.null(critical)) outputs[["/comparisons/critical_value"]] <- tol(critical, "t quantile for the interval")
for (index in seq_len(c)) {
i <- pairs[1, index]; j <- pairs[2, index]
outputs <- pair_base(index, i, j, outputs)
prefix <- paste0("/comparisons/rows/", index - 1)
p <- pt[labels[j], labels[i]]
outputs[[paste0(prefix, "/p_adjusted")]] <- prob(p, paste("pairwise.t.test", adjust, "p"))
outputs[[paste0(prefix, "/significant")]] <- flag(p <= alpha, "adjusted p at or below alpha")
if (!is.null(critical)) {
se <- s * sqrt(1 / n_i[i] + 1 / n_i[j])
outputs[[paste0(prefix, "/lower")]] <- tol(means[i] - means[j] - critical * se, "difference minus t quantile times SE")
outputs[[paste0(prefix, "/upper")]] <- tol(means[i] - means[j] + critical * se, "difference plus t quantile times SE")
} else {
outputs[[paste0(prefix, "/lower")]] <- list(comparison = "absent", rationale = "Holm adjusts p-values only; no interval")
}
}
emit(id, title, "axis_coverage",
list(response = "y", group = "group", procedure = procedure, alpha = alpha),
outputs, paste0("pairwise.t.test(pool.sd = TRUE, p.adjust.method = '", adjust, "')",
if (procedure == "lsd") "; LSD interval uses qt(1 - alpha/2, df)" else if (procedure == "bonferroni") "; Bonferroni interval uses qt(1 - alpha/(2c), df) with c = 3 pairs" else "."),
"R 4.6.1 stats::pairwise.t.test", "R Core Team, stats package help: pairwise.t.test, p.adjust, qt.")
}
simple_case("STAT-HYP-ANOVA-POST-HOC-002", "One-way ANOVA Bonferroni", "bonferroni", "bonferroni")
simple_case("STAT-HYP-ANOVA-POST-HOC-003", "One-way ANOVA Holm", "holm", "holm")
simple_case("STAT-HYP-ANOVA-POST-HOC-005", "One-way ANOVA LSD alpha 0.10", "lsd", "none", alpha = 0.10)
# 004: Scheffe: p = P(F_{k-1, df} >= t^2 / (k - 1)); critical sqrt((k - 1) qf(1 - alpha)).
outputs <- anova_outputs()
critical <- sqrt((k - 1) * stats::qf(0.95, k - 1, df_w))
outputs[["/comparisons/critical_value"]] <- tol(critical, "sqrt((k-1) qf(0.95, k-1, df))")
for (index in seq_len(ncol(pairs))) {
i <- pairs[1, index]; j <- pairs[2, index]
outputs <- pair_base(index, i, j, outputs)
prefix <- paste0("/comparisons/rows/", index - 1)
se <- s * sqrt(1 / n_i[i] + 1 / n_i[j])
t <- (means[i] - means[j]) / se
p <- stats::pf(t^2 / (k - 1), k - 1, df_w, lower.tail = FALSE)
outputs[[paste0(prefix, "/p_adjusted")]] <- prob(p, "Scheffe F tail of t^2/(k-1)")
outputs[[paste0(prefix, "/lower")]] <- tol(means[i] - means[j] - critical * se, "Scheffe interval lower")
outputs[[paste0(prefix, "/upper")]] <- tol(means[i] - means[j] + critical * se, "Scheffe interval upper")
outputs[[paste0(prefix, "/significant")]] <- flag(p <= 0.05, "Scheffe p at or below alpha")
}
emit("STAT-HYP-ANOVA-POST-HOC-004", "One-way ANOVA Scheffe", "axis_coverage",
list(response = "y", group = "group", procedure = "scheffe", alpha = 0.05),
outputs, "Scheffe (1959): a pairwise contrast is significant when t^2/(k-1) exceeds F_{k-1, N-k}(alpha); p = pf(t^2/(k-1), k-1, df, lower.tail = FALSE); interval half-width sqrt((k-1) F) SE.",
"R 4.6.1 stats::pf, qf", "Scheffe H (1959), The Analysis of Variance, Wiley, section 3.5; R Core Team, stats package help: pf, qf.")
# 006: Dunnett against control A, two-sided. The contrasts' correlation is the product
# lambda_i lambda_j with lambda_i = sqrt(n_i / (n_i + n_0)), so with Z_i = lambda_i W +
# sqrt(1 - lambda_i^2) U_i the joint probability is a one-dimensional integral over W, then
# over the independent scale S = sqrt(chi2_df / df) (Dunnett 1955, eq. 5).
control <- 1L
others <- setdiff(seq_len(k), control)
lambda <- sqrt(n_i[others] / (n_i[others] + n_i[control]))
dunnett_prob <- function(h) {
normal <- function(c) {
f <- function(w) {
v <- stats::dnorm(w)
for (l in lambda) v <- v * (stats::pnorm((c - l * w) / sqrt(1 - l^2)) - stats::pnorm((-c - l * w) / sqrt(1 - l^2)))
v
}
stats::integrate(f, -Inf, Inf, rel.tol = 1e-12, abs.tol = 0, subdivisions = 500L)$value
}
g <- function(sv) vapply(sv, function(si) normal(h * si) * 2 * df_w * si * stats::dchisq(df_w * si^2, df_w), 0)
stats::integrate(g, 0, Inf, rel.tol = 1e-11, abs.tol = 0, subdivisions = 500L)$value
}
critical <- stats::uniroot(function(h) dunnett_prob(h) - 0.95, c(1.5, 4), tol = 1e-12)$root
outputs <- anova_outputs()
outputs[["/comparisons/critical_value"]] <- tol(critical, "Dunnett two-sided critical value by quadrature", "1e-4", "1e-4")
for (index in seq_along(others)) {
i <- others[index]
outputs <- pair_base(index, i, control, outputs)
prefix <- paste0("/comparisons/rows/", index - 1)
se <- s * sqrt(1 / n_i[i] + 1 / n_i[control])
t <- (means[i] - means[control]) / se
p <- 1 - dunnett_prob(abs(t))
outputs[[paste0(prefix, "/p_adjusted")]] <- tol(p, "1 - P(max |T| <= |t|) over the family by quadrature", "1e-4", "1e-4")
outputs[[paste0(prefix, "/lower")]] <- tol(means[i] - means[control] - critical * se, "Dunnett interval lower", "1e-4", "1e-4")
outputs[[paste0(prefix, "/upper")]] <- tol(means[i] - means[control] + critical * se, "Dunnett interval upper", "1e-4", "1e-4")
outputs[[paste0(prefix, "/significant")]] <- flag(abs(t) > critical, "|t| beyond the Dunnett critical value")
}
emit("STAT-HYP-ANOVA-POST-HOC-006", "One-way ANOVA Dunnett against control A", "axis_coverage",
list(response = "y", group = "group", procedure = "dunnett", alpha = 0.05, control = "A"),
outputs, c("Dunnett (1955): rows are (B, A) and (C, A); critical value and adjusted p from the bivariate t with correlation sqrt(n_i n_j / ((n_i + n_0)(n_j + n_0))) on N - k df, computed by deterministic quadrature (product-correlation integral over W, then over the chi scale; rel.tol 1e-11; uniroot to 1e-12).",
"The window calibrates the maximum multivariate t by replicated quasi-Monte Carlo with a stated target half-width of 1e-4 on the probability (docs/manual/chapters/83-repeated-anova-and-contrasts.tex:149-153), so the comparison allows 1e-4 absolute and relative; the reference itself is accurate to about 1e-10."),
"R 4.6.1 stats::integrate, uniroot", "Dunnett CW (1955), A multiple comparison procedure for comparing several treatments with a control, JASA 50, 1096-1121; R Core Team, stats package help: integrate, uniroot.")
# 007: Games-Howell: separate variances, Welch df per pair, studentized range with k groups.
outputs <- anova_outputs()
for (index in seq_len(ncol(pairs))) {
i <- pairs[1, index]; j <- pairs[2, index]
prefix <- paste0("/comparisons/rows/", index - 1)
parts <- c(vars[i] / n_i[i], vars[j] / n_i[j])
se <- sqrt(sum(parts))
df_ij <- sum(parts)^2 / sum(parts^2 / (n_i[c(i, j)] - 1))
t <- (means[i] - means[j]) / se
p <- stats::ptukey(abs(t) * sqrt(2), k, df_ij, lower.tail = FALSE)
half <- stats::qtukey(0.95, k, df_ij) / sqrt(2) * se
outputs[[paste0(prefix, "/difference")]] <- tol(means[i] - means[j], paste(labels[i], "minus", labels[j]))
outputs[[paste0(prefix, "/standard_error")]] <- tol(se, "sqrt(s_i^2/n_i + s_j^2/n_j)")
outputs[[paste0(prefix, "/statistic")]] <- tol(t, "Welch t")
outputs[[paste0(prefix, "/df")]] <- tol(df_ij, "Welch-Satterthwaite df")
outputs[[paste0(prefix, "/p_adjusted")]] <- prob(p, "ptukey(|t| sqrt 2, k, Welch df)", "1e-6")
outputs[[paste0(prefix, "/lower")]] <- tol(means[i] - means[j] - half, "Games-Howell interval lower (qtukey)", "1e-6")
outputs[[paste0(prefix, "/upper")]] <- tol(means[i] - means[j] + half, "Games-Howell interval upper (qtukey)", "1e-6")
}
emit("STAT-HYP-ANOVA-POST-HOC-007", "One-way ANOVA Games-Howell", "axis_coverage",
list(response = "y", group = "group", procedure = "games_howell", alpha = 0.05),
outputs, "Games and Howell (1976): SE = sqrt(s_i^2/n_i + s_j^2/n_j), Welch-Satterthwaite df per pair, p = ptukey(|t| sqrt(2), k, df_ij, lower = FALSE), half-width qtukey(0.95, k, df_ij) SE / sqrt(2). R documents qtukey as accurate to the 4th decimal place, so ptukey/qtukey-derived values are compared at 1e-6 relative.",
"R 4.6.1 stats::ptukey, qtukey", "Games PA, Howell JF (1976), Journal of Educational Statistics 1(2), 113-125; R Core Team, stats package help: ptukey, qtukey.")
# 008: Helmert contrasts on the pooled error term, no pairwise procedure.
contrast_rows <- function(matrix, names, variance, prefix_adjust = NULL, alpha = 0.05) {
outputs <- list()
for (r in seq_len(nrow(matrix))) {
coef <- matrix[r, ]
estimate <- sum(coef * means)
if (variance == "pooled") {
se <- sqrt(mse * sum(coef^2 / n_i)); df_r <- df_w
} else {
parts <- coef^2 * vars / n_i
se <- sqrt(sum(parts)); df_r <- sum(parts)^2 / sum(parts^2 / (n_i - 1))
}
t <- estimate / se
p <- 2 * stats::pt(-abs(t), df_r)
prefix <- paste0("/contrasts/rows/", r - 1)
outputs[[paste0(prefix, "/estimate")]] <- tol(estimate, paste(names[r], "estimate"))
outputs[[paste0(prefix, "/standard_error")]] <- tol(se, paste(names[r], "SE"))
outputs[[paste0(prefix, "/df")]] <- tol(df_r, paste(names[r], "df"))
outputs[[paste0(prefix, "/t_value")]] <- tol(t, paste(names[r], "t"))
outputs[[paste0(prefix, "/p_value")]] <- prob(p, paste(names[r], "two-sided t tail"))
critical <- stats::qt(1 - alpha / 2, df_r)
outputs[[paste0(prefix, "/lower")]] <- tol(estimate - critical * se, paste(names[r], "individual interval lower"))
outputs[[paste0(prefix, "/upper")]] <- tol(estimate + critical * se, paste(names[r], "individual interval upper"))
}
outputs
}
helmert <- rbind(c(1, -1/2, -1/2), c(0, 1, -1))
outputs <- c(anova_outputs(), contrast_rows(helmert, c("A vs later", "B vs later"), "pooled"))
outputs[["/comparisons"]] <- list(comparison = "absent", rationale = "procedure none: no pairwise table")
emit("STAT-HYP-ANOVA-POST-HOC-008", "One-way ANOVA Helmert contrasts pooled", "axis_coverage",
list(response = "y", group = "group", procedure = "none", alpha = 0.05,
contrast_family = "helmert", contrast_variance = "pooled", contrast_adjustment = "none"),
outputs, "Helmert: each level against the mean of the later levels, rows (1, -1/2, -1/2) and (0, 1, -1); estimate = c'means, SE = sqrt(MSE sum(c_i^2 / n_i)), df = N - k, individual t intervals.",
"R 4.6.1 stats::pt, qt", "R Core Team, stats package help: pt, qt; contrast definition as in the SPSS ONEWAY Helmert family.")
# 009: a custom contrast with Welch (separate-variance) error and Bonferroni adjustment.
custom <- rbind(c(1, -0.5, -0.5), c(1, 0, -1))
outputs <- c(anova_outputs(), contrast_rows(custom, c("Contrast 1", "Contrast 2"), "welch"))
for (r in 1:2) {
prefix <- paste0("/contrasts/rows/", r - 1)
coef <- custom[r, ]
parts <- coef^2 * vars / n_i
se <- sqrt(sum(parts)); df_r <- sum(parts)^2 / sum(parts^2 / (n_i - 1))
t <- sum(coef * means) / se
outputs[[paste0(prefix, "/p_adjusted")]] <- prob(min(1, 2 * 2 * stats::pt(-abs(t), df_r)), "Bonferroni: 2 p capped at 1")
critical <- stats::qt(1 - 0.05 / 4, df_r)
outputs[[paste0(prefix, "/lower")]] <- tol(sum(coef * means) - critical * se, "Bonferroni interval lower qt(1 - alpha/4)")
outputs[[paste0(prefix, "/upper")]] <- tol(sum(coef * means) + critical * se, "Bonferroni interval upper qt(1 - alpha/4)")
}
emit("STAT-HYP-ANOVA-POST-HOC-009", "One-way ANOVA custom contrasts Welch Bonferroni", "axis_coverage",
list(response = "y", group = "group", procedure = "none", alpha = 0.05,
contrast_family = "custom", coefficients = list(1, -0.5, -0.5, 1, 0, -1),
contrast_variance = "welch", contrast_adjustment = "bonferroni"),
outputs, "Custom rows (1, -0.5, -0.5) and (1, 0, -1): SE = sqrt(sum c_i^2 s_i^2 / n_i), Welch-Satterthwaite df, Bonferroni over m = 2 contrasts (p times 2, qt(1 - alpha/(2m))).",
"R 4.6.1 stats::pt, qt", "Welch BL (1947), Biometrika 34, 28-35 (Satterthwaite df); R Core Team, stats package help: pt, qt.")
cat("anova post hoc cases generated\n")
generators/r/hypothesis_axes.R
source("common.R", local = TRUE)
d <- read_dataset("HYP_CORE")
rates <- data.frame(
count = c(1L, 0L, 2L, 1L, 3L, 1L, 2L, 2L),
exposure = c(8, 10, 9, 11, 8, 12, 9, 13),
g = rep(c("A", "B"), each = 4), stringsAsFactors = FALSE
)
export_dataset(
rates, "HYP_RATES", "Constructed Poisson count and exposure records",
"R stats::poisson.test help: exact one- and two-rate inference.",
"Constructed test data; no third-party dataset license.",
c(count = "Int64", exposure = "Float64", g = "String")
)
r <- read_dataset("HYP_RATES")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R independent calculation:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R independent tail:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, entry, menu, dataset, config, outputs, notes, deferred = list(),
repetitions = 1L) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = deferred, generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Configuration axis:", id), case_kind = "nominal",
dataset = if (is.null(dataset)) NULL else list(
path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = repetitions,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R 4.6.1 stats",
citation = "R stats help: t.test, poisson.test, binom.test, pnorm, pt, ptukey, power.t.test; exact enumeration by combn.",
generator = list(path = "generators/r/hypothesis_axes.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Data-based values computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
# Paired equivalence extensions, including the log-ratio back-transformation.
paired <- d$x - d$y
paired_effect <- mean(paired)
paired_se <- sd(paired) / sqrt(length(paired))
emit("STAT-HYP-AXIS-EQUIV-PAIRED-001", "tabelz.executor.equivalence_extensions",
"equivalenceMenuItem", "HYP_CORE",
list(mode = "paired_difference", first_column = "x", second_column = "y",
lower_bound = -2, upper_bound = 2),
list("/estimate" = tol(paired_effect, "paired difference mean"),
"/p_lower" = prob(stats::pt((paired_effect + 2) / paired_se, length(paired) - 1,
lower.tail = FALSE), "lower paired TOST")),
list("Paired difference is x minus y; Student t tails."))
log_ratio <- log(d$x / d$y)
log_effect <- mean(log_ratio)
log_se <- sd(log_ratio) / sqrt(length(log_ratio))
emit("STAT-HYP-AXIS-EQUIV-RATIO-001", "tabelz.executor.equivalence_extensions",
"equivalenceMenuItem", "HYP_CORE",
list(mode = "paired_log_ratio", first_column = "x", second_column = "y",
lower_bound = 0.8, upper_bound = 1.25),
list("/estimate" = tol(exp(log_effect), "geometric mean ratio"),
"/p_lower" = prob(stats::pt((log_effect - log(0.8)) / log_se,
length(log_ratio) - 1, lower.tail = FALSE), "log-ratio lower TOST"),
"/transformed_estimate" = tol(log_effect, "mean log ratio")),
list("TOST on log(x/y); display estimate is exp(mean log ratio)."))
# Four basic-inference methods, raw and summary, non-default display/unit axes.
one_rate <- stats::poisson.test(sum(r$count), T = sum(r$exposure), r = 0.15)
emit("STAT-HYP-AXIS-BASIC-POISSON-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem", "HYP_RATES",
list(method = "one_poisson_rate", input_kind = "raw", count_column = "count",
exposure_column = "exposure", null_rate = 0.15, exposure_unit = "hours"),
list("/estimate" = tol(sum(r$count) / sum(r$exposure), "events per exposure"),
"/p_value" = prob(one_rate$p.value, "exact one-rate Poisson tail")),
list("Hours are display units; exact Poisson test uses total count and total exposure."))
count_a <- sum(r$count[r$g == "A"])
count_b <- sum(r$count[r$g == "B"])
exposure_a <- sum(r$exposure[r$g == "A"])
exposure_b <- sum(r$exposure[r$g == "B"])
two_rate <- stats::poisson.test(c(count_a, count_b), T = c(exposure_a, exposure_b), r = 1)
emit("STAT-HYP-AXIS-BASIC-TWORATE-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem", "HYP_RATES",
list(method = "two_poisson_rate", input_kind = "raw", count_column = "count",
exposure_column = "exposure", group_column = "g", group_1 = "A", group_2 = "B",
null_ratio = 1, exposure_unit = "days"),
list("/estimate" = tol((count_a / exposure_a) / (count_b / exposure_b), "A/B rate ratio"),
"/p_value" = prob(two_rate$p.value, "conditional exact two-rate tail")),
list("Days are display units; conditional Poisson test compares A/B rate ratio."))
z_summary <- (mean(d$x) - 6) / (2 / sqrt(nrow(d)))
emit("STAT-HYP-AXIS-BASIC-SUMMARY-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem", NULL,
list(method = "known_sigma_z", input_kind = "summary", summary_mean = mean(d$x),
sample_size = nrow(d), known_sigma = 2, null_mean = 6),
list("/statistic" = tol(z_summary, "summary known-sigma z"),
"/p_value" = prob(2 * stats::pnorm(-abs(z_summary)), "normal tail")),
list("Summary mean and sample size are derived from the exported CSV.",
"docs/manual/chapters/31-additional-basic-tests.tex:6-8: 'Summary mode is source independent: it does not claim a table population'."))
variance <- stats::var(d$x)
chi <- (nrow(d) - 1) * variance / 4
emit("STAT-HYP-AXIS-BASIC-SD-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem", "HYP_CORE",
list(method = "one_variance_chi_square", input_kind = "raw", response_column = "x",
null_variance = 4, variance_display = "sd"),
list("/statistic" = tol(chi, "variance chi-square"),
"/p_value" = prob(min(1, 2 * min(stats::pchisq(chi, nrow(d) - 1),
stats::pchisq(chi, nrow(d) - 1, lower.tail = FALSE))),
"two-sided variance tail")),
list("Null variance 4; sd display is a presentation axis only."))
# Exact paired sign flips and independent one-sided reassignment tails.
pair_df <- data.frame(x = d$x[1:5], y = d$y[1:5])
export_dataset(pair_df, "HYP_PAIRS", "First five pairs from exported HYP_CORE",
"Constructed by hypothesis_axes.R; exact sign-flip enumeration.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64"))
pairs <- read_dataset("HYP_PAIRS")
differences <- pairs$x - pairs$y
observed_pair <- mean(differences)
signs <- as.matrix(expand.grid(rep(list(c(-1, 1)), length(differences))))
null_pair <- as.vector(signs %*% differences) / length(differences)
emit("STAT-HYP-AXIS-PERM-PAIRED-001", "tabelz.executor.permutation_tests",
"permutationTestsMenuItem", "HYP_PAIRS",
list(mode = "paired", response_column = "x", second_response_column = "y",
inference = "exact"),
list("/statistic" = tol(observed_pair, "mean paired difference"),
"/p_value" = prob(mean(abs(null_pair) >= abs(observed_pair) - 1e-12),
"all paired sign flips"),
"/assignment_count" = exact(nrow(signs), "Every sign pattern enumerated.")),
list("Five pairs give 2^5 exact sign patterns; paired difference x-y."))
a <- d$x[d$g == "A"]
b <- d$x[d$g == "B"]
observed <- mean(a) - mean(b)
assign <- utils::combn(seq_len(nrow(d)), length(a))
sa <- colSums(matrix(d$x[assign], nrow = length(a)))
null <- sa / length(a) - (sum(d$x) - sa) / length(b)
for (alternative in c("less", "greater")) {
p <- if (alternative == "less") mean(null <= observed + 1e-12) else
mean(null >= observed - 1e-12)
emit(paste0("STAT-HYP-AXIS-PERM-", toupper(alternative), "-001"),
"tabelz.executor.permutation_tests", "permutationTestsMenuItem", "HYP_CORE",
list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "exact", alternative = alternative),
list("/statistic" = tol(observed, "A-B mean"),
"/p_value" = prob(p, paste(alternative, "exact reassignment tail"))),
list("Every choose(16,8) assignment enumerated; one-sided tail inclusive of observed."))
}
emit("STAT-HYP-AXIS-PERM-MC-001", "tabelz.executor.permutation_tests",
"permutationTestsMenuItem", "HYP_CORE",
list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "monte_carlo", seed = 77L,
resamples = 100L),
list("/statistic" = tol(observed, "observed A-B mean")),
list("Monte Carlo p and histogram are deferred to V27; compare only the observed statistic."),
deferred = list(list(path = "/p_value", reason = "Seeded random permutation p-values are V27 simulation evidence.")),
repetitions = 2L)
# Median and fixed-reference runs, with exact finite-label tail enumeration.
fixed <- 6.0
sequence <- d$x > fixed
sequence <- sequence[d$x != fixed]
runs <- 1L + sum(sequence[-1] != sequence[-length(sequence)])
n1 <- sum(sequence)
all_positions <- utils::combn(seq_along(sequence), n1)
run_count <- apply(all_positions, 2, function(positions) {
labels <- rep(FALSE, length(sequence))
labels[positions] <- TRUE
1L + sum(labels[-1] != labels[-length(labels)])
})
for (alternative in c("less", "greater")) {
p <- if (alternative == "less") mean(run_count <= runs) else mean(run_count >= runs)
emit(paste0("STAT-HYP-AXIS-RANK-", toupper(alternative), "-001"),
"tabelz.executor.blocked_rank_tests", "blockedRankTestsMenuItem", "HYP_CORE",
list(method = "runs", response_column = "x", reference_mode = "fixed",
reference_value = fixed, inference = "exact", alternative = alternative),
list("/runs" = exact(as.integer(runs), "Observed ordered runs."),
"/p_value" = prob(p, paste(alternative, "exact runs tail"))),
list("Fixed reference 6; exact run-count distribution over all fixed-count label allocations."))
}
emit("STAT-HYP-AXIS-BOOT-MEDIAN-001", "tabelz.executor.bootstrap_estimates",
"bootstrapMenuItem", "HYP_CORE",
list(column = "x", statistic = "median", seed = 17L, resamples = 150L),
list("/estimate" = tol(stats::median(d$x), "sample median point statistic")),
list("Bootstrap interval is V27; only median point statistic compared."),
repetitions = 2L)
cat("hypothesis axis cases generated\n")
generators/r/hypothesis_bayesian.R
source("common.R", local = TRUE)
# Reference values for the seven Statistics > Bayesian statistics windows (F152).
# Conjugate windows use base R closed forms (qbeta, qgamma, qnorm, lbeta, lgamma); the
# JZS t test, the correlation Bayes factor, the one-way ANOVA and the g-prior regression
# use base R integrate() on the published integrands (Rouder et al. 2009; Ly, Verhagen and
# Wagenmakers 2016; Rouder, Morey, Speckman and Province 2012; Liang et al. 2008). Every
# reference is computed from the re-read CSV, never from the in-memory vectors.
set.seed(152)
n_rows <- 20L
g <- rep(c("A", "B"), c(9L, 11L))
g3 <- rep(c("low", "mid", "high"), c(6L, 7L, 7L))
x1 <- round(stats::rnorm(n_rows, 10, 2), 2)
x2 <- round(stats::runif(n_rows, 0, 5), 2)
y <- round(0.5 + 0.8 * x1 + 0.3 * (g == "B") +
c(low = 0, mid = 0.8, high = 1.5)[g3] + stats::rnorm(n_rows, 0, 1.5), 2)
d <- round(stats::rnorm(n_rows, 0.4, 1), 2)
d2 <- round(stats::rnorm(n_rows, 0, 1), 2)
outcome <- ifelse(stats::rbinom(n_rows, 1, 0.6) == 1, "pass", "fail")
count <- as.integer(stats::rpois(n_rows, 2))
exposure <- round(stats::runif(n_rows, 0.5, 2), 2)
bayes <- data.frame(y = y, d = d, d2 = d2, g = g, g3 = g3, x1 = x1, x2 = x2,
outcome = outcome, count = count, exposure = exposure,
stringsAsFactors = FALSE)
export_dataset(
bayes, "HYP_BAYES", "Simulated by hypothesis_bayesian.R (set.seed(152))",
"R stats: qbeta, qgamma, qnorm, lbeta, lgamma, integrate, uniroot, dnorm, dchisq, pt",
"Simulated test data; no third-party dataset license.",
c(y = "Float64", d = "Float64", d2 = "Float64", g = "String", g3 = "String",
x1 = "Float64", x2 = "Float64", outcome = "String", count = "Int64",
exposure = "Float64")
)
dat <- read_dataset("HYP_BAYES")
run <- generator_run(c("stats", "jsonlite"))
manual <- "docs/manual/chapters/79-bayesian-analysis.tex"
manual_notes <- c(
paste0(manual, ":68-69: 'The results show the posterior mean, SD, median and equal-tailed credible interval. A highest-density interval is given when one interval can represent the supported posterior shape'"),
paste0(manual, ":74-75: 'The result names both models in BF10, shows its reciprocal and logarithm'")
)
closed <- function(value, label) {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Closed form in base R:", label))
}
quad <- function(value, label, abs_tol = "1e-6", rel_tol = "1e-6") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = abs_tol, relative_tolerance = rel_tol,
rationale = paste("Numerical integration on both sides (R integrate/uniroot versus HypoNum quadrature):", label))
}
count_of <- function(value, label) {
list(comparison = "exact", value = as.integer(value), rationale = label)
}
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(family, menu_item, entry_point, id, title, kind, configuration, outputs,
notes, source_ref, citation, requirement, excluded = list()) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected(family, id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family,
entry_point = entry_point, menu_items = list(menu_item), title = title,
case_kind = kind,
dataset = list(path = "reference_data/datasets/HYP_BAYES.json", sha256 = ""),
excluded_rows = excluded, configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/hypothesis_bayesian.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual_notes)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(requirement), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case(family, id, case)
}
rows_kept <- function(excluded) {
if (length(excluded) == 0) return(seq_len(nrow(dat)))
setdiff(seq_len(nrow(dat)), unlist(excluded) + 1L)
}
# Highest-density interval of a unimodal closed-form density: the lower end l where the
# density equals the density at the upper end u(l) = Q(F(l) + level).
hdi_closed <- function(dens, cdf, qf, level, lower_start) {
f <- function(l) dens(l) - dens(qf(cdf(l) + level))
l <- stats::uniroot(f, c(lower_start, qf(1 - level)), tol = 1e-14)$root
c(l, qf(cdf(l) + level))
}
posterior_outputs <- function(prefix, mean, sd, median, lower, upper, hdi, label,
hdi_tol = "1e-8") {
out <- list()
out[[paste0(prefix, "/mean")]] <- closed(mean, paste(label, "posterior mean"))
out[[paste0(prefix, "/sd")]] <- closed(sd, paste(label, "posterior SD"))
out[[paste0(prefix, "/median")]] <- closed(median, paste(label, "posterior median"))
out[[paste0(prefix, "/lower")]] <- closed(lower, paste(label, "equal-tailed lower"))
out[[paste0(prefix, "/upper")]] <- closed(upper, paste(label, "equal-tailed upper"))
if (!is.null(hdi)) {
out[[paste0(prefix, "/hdi_lower")]] <- list(
comparison = "tolerance", value = dec(hdi[[1]]), absolute_tolerance = hdi_tol,
relative_tolerance = hdi_tol,
rationale = "HDI: R uniroot on equal density heights of the closed-form posterior versus HypoNum's bisection on the quadrature CDF.")
out[[paste0(prefix, "/hdi_upper")]] <- list(
comparison = "tolerance", value = dec(hdi[[2]]), absolute_tolerance = hdi_tol,
relative_tolerance = hdi_tol,
rationale = "HDI: R uniroot on equal density heights of the closed-form posterior versus HypoNum's bisection on the quadrature CDF.")
}
out
}
bf_outputs <- function(log_bf, label, kind = closed) {
list("/bayes_factors/0/bf10" = kind(exp(log_bf), paste(label, "BF10")),
"/bayes_factors/0/bf01" = kind(exp(-log_bf), paste(label, "BF01")),
"/bayes_factors/0/log_bf10" = kind(log_bf, paste(label, "log BF10")))
}
# ---------------------------------------------------------------- Binomial proportion
binomial_case <- function(id, title, kind, event, p0, a, b, level, excluded = list()) {
v <- dat$outcome[rows_kept(excluded)]
n <- length(v)
x <- sum(v == event)
A <- a + x
B <- b + n - x
log_bf <- lbeta(A, B) - lbeta(a, b) - x * log(p0) - (n - x) * log1p(-p0)
tail <- (1 - level) / 2
hdi <- hdi_closed(function(p) stats::dbeta(p, A, B), function(p) stats::pbeta(p, A, B),
function(q) stats::qbeta(q, A, B), level, stats::qbeta(1e-12, A, B))
outputs <- c(
list("/n" = count_of(n, "Number of analysed trials.")),
bf_outputs(log_bf, "beta-binomial"),
posterior_outputs("/posteriors/0", A / (A + B), sqrt(A * B / ((A + B)^2 * (A + B + 1))),
stats::qbeta(0.5, A, B), stats::qbeta(tail, A, B), stats::qbeta(1 - tail, A, B),
hdi, sprintf("Beta(%g, %g)", A, B)))
emit("hypothesis", "bayesBinomialMenuItem",
"tabelz.analysis.bayesian.conjugate:compute_bayes_binomial", id, title, kind,
list(column = "outcome", event_value = event, null_proportion = p0, prior_a = a,
prior_b = b, confidence_level = level),
outputs,
c(sprintf("x = %d events (%s) in n = %d trials; prior Beta(%g, %g) gives posterior Beta(%g, %g).", x, event, n, a, b, A, B),
"log BF10 = lbeta(a + x, b + n - x) - lbeta(a, b) - x log p0 - (n - x) log(1 - p0): the beta-binomial marginal likelihood against the point null.",
"Equal-tailed interval: qbeta at (1 - level)/2 and 1 - (1 - level)/2; HDI by equal densities at both ends.",
paste0(manual, ":25-26: 'Binomial proportion & Beta(a,b), default Beta(1,1); name the event and point-null proportion.'"),
paste0(manual, ":62-64: 'Posterior mean is 7/12; SD is sqrt(7*5/(12^2*13)) ... BF10 = B(7,5)/0.5^10 ... equal-tailed limits are the 0.025 and 0.975 quantiles'")),
"R stats: lbeta, qbeta, pbeta, dbeta, uniroot",
"R Core Team, stats package help: Beta (qbeta, pbeta, dbeta), Special (lbeta), uniroot.",
"REQ-STAT-HYP-BAYES-BINOMIAL-001", excluded)
}
binomial_case("STAT-HYP-BAYES-BINOMIAL-001", "Beta(1, 1) prior against p = 0.5", "nominal",
"pass", 0.5, 1, 1, 0.95)
binomial_case("STAT-HYP-BAYES-BINOMIAL-002", "Informative Beta(2, 5) prior against p = 0.3",
"axis_coverage", "pass", 0.3, 2, 5, 0.95)
binomial_case("STAT-HYP-BAYES-BINOMIAL-003", "90% credible interval, event 'fail'",
"axis_coverage", "fail", 0.5, 1, 1, 0.9)
binomial_case("STAT-HYP-BAYES-BINOMIAL-004", "Three excluded rows", "missing_excluded",
"pass", 0.5, 1, 1, 0.95, excluded = list(0L, 7L, 12L))
# ---------------------------------------------------------------- Poisson rate
poisson_case <- function(id, title, kind, with_exposure, prior, a, b, lambda0, level) {
rows <- rows_kept(list())
X <- sum(dat$count[rows])
Tt <- if (with_exposure) sum(dat$exposure[rows]) else length(rows)
if (prior == "jeffreys") {
shape <- X + 0.5
rate <- Tt
bf <- list("/bayes_factors/0/bf10" = absent("Jeffreys prior: no Bayes factor against a point null is defined (improper prior)."),
"/bayes_factors/0/bf01" = absent("Jeffreys prior: no Bayes factor is defined."),
"/bayes_factors/0/log_bf10" = absent("Jeffreys prior: no Bayes factor is defined."))
prior_note <- sprintf("Jeffreys prior rate^(-1/2) gives the posterior Gamma(shape X + 1/2 = %g, rate T = %g); no Bayes factor.", shape, rate)
} else {
shape <- a + X
rate <- b + Tt
log_m1 <- a * log(b) + lgamma(a + X) - lgamma(a) - (a + X) * log(b + Tt)
log_m0 <- X * log(lambda0) - lambda0 * Tt
bf <- bf_outputs(log_m1 - log_m0, "gamma-Poisson")
prior_note <- sprintf("Gamma(shape %g, rate %g) prior gives the posterior Gamma(shape %g, rate %g); BF10 = b^a Gamma(a + X) / (Gamma(a) (b + T)^(a + X)) divided by lambda0^X exp(-lambda0 T).", a, b, shape, rate)
}
tail <- (1 - level) / 2
hdi <- hdi_closed(function(v) stats::dgamma(v, shape, rate = rate),
function(v) stats::pgamma(v, shape, rate = rate),
function(q) stats::qgamma(q, shape, rate = rate), level,
stats::qgamma(1e-12, shape, rate = rate))
outputs <- c(
list("/n" = count_of(length(rows), "Number of count rows.")), bf,
posterior_outputs("/posteriors/0", shape / rate, sqrt(shape) / rate,
stats::qgamma(0.5, shape, rate = rate), stats::qgamma(tail, shape, rate = rate),
stats::qgamma(1 - tail, shape, rate = rate), hdi,
sprintf("Gamma(%g, rate %g)", shape, rate)))
configuration <- list(column = "count", null_rate = lambda0, prior = prior, prior_shape = a,
prior_rate = b, confidence_level = level)
if (with_exposure) configuration$exposure <- "exposure"
emit("hypothesis", "bayesPoissonMenuItem",
"tabelz.analysis.bayesian.conjugate:compute_bayes_poisson", id, title, kind,
configuration, outputs,
c(sprintf("X = %d events over total exposure T = %s (%s).", X, format(Tt, digits = 17),
if (with_exposure) "sum of the exposure column" else "one unit per row"),
prior_note,
"Equal-tailed interval: qgamma at (1 - level)/2 and 1 - (1 - level)/2; HDI by equal densities at both ends.",
paste0(manual, ":27-28: 'Poisson rate & Gamma shape and rate, both positive, per unit of exposure. The Jeffreys option supplies a posterior only.'"),
paste0(manual, ":86-88: 'An improper prior's arbitrary normalization prevents a point-null Bayes factor from being defined.'")),
"R stats: lgamma, qgamma, pgamma, dgamma, uniroot",
"R Core Team, stats package help: GammaDist (qgamma, pgamma, dgamma), Special (lgamma), uniroot.",
"REQ-STAT-HYP-BAYES-POISSON-001")
}
poisson_case("STAT-HYP-BAYES-POISSON-001", "Gamma(1, 1) prior, unit exposure, rate 1", "nominal",
FALSE, "gamma", 1, 1, 1, 0.95)
poisson_case("STAT-HYP-BAYES-POISSON-002", "Gamma(2, 0.5) prior with an exposure column",
"axis_coverage", TRUE, "gamma", 2, 0.5, 1.5, 0.95)
poisson_case("STAT-HYP-BAYES-POISSON-003", "Jeffreys prior with an exposure column (posterior only)",
"axis_coverage", TRUE, "jeffreys", 1, 1, 1, 0.95)
poisson_case("STAT-HYP-BAYES-POISSON-004", "99% credible interval", "axis_coverage",
FALSE, "gamma", 1, 1, 2, 0.99)
# ---------------------------------------------------------------- Normal mean, known SD
normal_case <- function(id, title, kind, sigma, m0, tau, mu0, level) {
v <- dat$d
n <- length(v)
ybar <- mean(v)
precision <- 1 / tau^2 + n / sigma^2
post_mean <- (m0 / tau^2 + n * ybar / sigma^2) / precision
post_sd <- sqrt(1 / precision)
log_bf <- stats::dnorm(ybar, m0, sqrt(tau^2 + sigma^2 / n), log = TRUE) -
stats::dnorm(ybar, mu0, sigma / sqrt(n), log = TRUE)
tail <- (1 - level) / 2
lower <- stats::qnorm(tail, post_mean, post_sd)
upper <- stats::qnorm(1 - tail, post_mean, post_sd)
outputs <- c(
list("/n" = count_of(n, "Number of observations.")),
bf_outputs(log_bf, "normal-normal"),
posterior_outputs("/posteriors/0", post_mean, post_sd, post_mean, lower, upper,
c(lower, upper), "Normal posterior"))
emit("hypothesis", "bayesNormalMenuItem",
"tabelz.analysis.bayesian.conjugate:compute_bayes_normal", id, title, kind,
list(column = "d", known_sd = sigma, prior_mean = m0, prior_sd = tau, null_value = mu0,
confidence_level = level),
outputs,
c(sprintf("ybar = %s from n = %d; posterior precision 1/tau^2 + n/sigma^2, mean (m0/tau^2 + n ybar/sigma^2)/precision.", format(ybar, digits = 17), n),
"BF10 = N(ybar; m0, tau^2 + sigma^2/n) / N(ybar; mu0, sigma^2/n) (prior predictive against the point null).",
"The posterior is symmetric: its HDI equals the equal-tailed interval from qnorm.",
paste0(manual, ":29-30: 'Normal mean, known SD & Normal prior on the mean; supply the known observation SD, prior mean and prior SD.'")),
"R stats: dnorm, qnorm",
"R Core Team, stats package help: Normal (dnorm, qnorm).",
"REQ-STAT-HYP-BAYES-NORMAL-001")
}
normal_case("STAT-HYP-BAYES-NORMAL-001", "Normal(0, 1) prior, known SD 1, null 0", "nominal",
1, 0, 1, 0, 0.95)
normal_case("STAT-HYP-BAYES-NORMAL-002", "Informative Normal(1, 0.5) prior, known SD 1.2, null 0.5",
"axis_coverage", 1.2, 1, 0.5, 0.5, 0.95)
normal_case("STAT-HYP-BAYES-NORMAL-003", "90% credible interval", "axis_coverage",
1, 0, 1, 0, 0.9)
# ---------------------------------------------------------------- JZS t test
# The noncentral t density written as an integral over the chi-square denominator, so the
# posterior of delta does not depend on R's dt(ncp) approximation.
nct_density <- function(t, nu, ncp) {
stats::integrate(function(v) {
s <- sqrt(v / nu)
stats::dnorm(t * s - ncp) * s * stats::dchisq(v, nu)
}, 0, Inf, rel.tol = 1e-12, abs.tol = 0, subdivisions = 1000L)$value
}
jzs_log_bf <- function(t, nu, N, r) {
integrand <- function(u) { # g = exp(u)
g <- exp(u)
spread <- 1 + N * g * r^2
exp(-0.5 * log(spread) - (nu + 1) / 2 * log1p(t^2 / (spread * nu)) -
0.5 * log(2 * pi) - 1.5 * u - 1 / (2 * g) + u)
}
m1 <- stats::integrate(integrand, -Inf, Inf, rel.tol = 1e-12, abs.tol = 0, subdivisions = 1000L)
log(m1$value) + (nu + 1) / 2 * log1p(t^2 / nu)
}
numeric_posterior <- function(log_dens, lower, upper, level) {
dens <- function(x) vapply(x, function(v) exp(log_dens(v)), 0)
Z <- stats::integrate(dens, lower, upper, rel.tol = 1e-11, abs.tol = 0, subdivisions = 2000L)$value
moment <- function(k) stats::integrate(function(x) x^k * dens(x), lower, upper, rel.tol = 1e-11,
abs.tol = 0, subdivisions = 2000L)$value / Z
cdf <- function(x) stats::integrate(dens, lower, x, rel.tol = 1e-11, abs.tol = 0,
subdivisions = 2000L)$value / Z
quantile <- function(p) stats::uniroot(function(x) cdf(x) - p, c(lower, upper), tol = 1e-13)$root
m <- moment(1)
tail <- (1 - level) / 2
list(mean = m, sd = sqrt(moment(2) - m^2), median = quantile(0.5),
lower = quantile(tail), upper = quantile(1 - tail))
}
numeric_outputs <- function(prefix, s, label) {
out <- list()
for (name in c("mean", "sd", "median", "lower", "upper")) {
out[[paste0(prefix, "/", name)]] <- quad(s[[name]], paste(label, name))
}
out
}
ttest_case <- function(id, title, kind, design, column, second = NULL, group = NULL,
first_group = NULL, null_value = 0, r = 1 / sqrt(2), level = 0.95) {
if (design == "independent") {
v <- dat[[column]]
labels <- dat[[group]]
levels <- sort(unique(labels))
if (!is.null(first_group)) levels <- c(first_group, setdiff(levels, first_group))
a <- v[labels == levels[[1]]]
b <- v[labels == levels[[2]]]
nu <- length(a) + length(b) - 2
pooled <- sqrt(((length(a) - 1) * stats::var(a) + (length(b) - 1) * stats::var(b)) / nu)
N <- length(a) * length(b) / (length(a) + length(b))
t <- (mean(a) - mean(b)) / (pooled * sqrt(1 / length(a) + 1 / length(b)))
n <- length(a) + length(b)
definition <- sprintf("Pooled two-sample t: groups %s minus %s, t = %s on %d df, effective N = n1 n2/(n1 + n2) = %s.",
levels[[1]], levels[[2]], format(t, digits = 17), nu, format(N, digits = 17))
} else {
v <- dat[[column]]
if (design == "paired") v <- v - dat[[second]]
n <- length(v)
nu <- n - 1
N <- n
t <- (mean(v) - null_value) / (stats::sd(v) / sqrt(n))
definition <- sprintf("%s t against %g: t = %s on %d df, effective N = %d.",
if (design == "paired") "Paired-difference" else "One-sample",
null_value, format(t, digits = 17), nu, n)
}
log_bf <- jzs_log_bf(t, nu, N, r)
root <- sqrt(N)
limit <- min(37 / root, 25)
log_post <- function(delta) log(nct_density(t, nu, delta * root)) + stats::dcauchy(delta, 0, r, log = TRUE)
# Sanity check of the hand-written noncentral t density against R's dt(ncp).
stopifnot(abs(nct_density(t, nu, t) - stats::dt(t, nu, ncp = t)) < 1e-9)
s <- numeric_posterior(log_post, -limit, limit, level)
outputs <- c(
list("/n" = count_of(n, "Number of analysed observations.")),
bf_outputs(log_bf, "JZS", kind = quad),
numeric_outputs("/posteriors/0", s, "posterior of delta"))
configuration <- list(design = design, column = column, null_value = null_value,
prior_scale = r, confidence_level = level)
if (!is.null(second)) configuration$second <- second
if (!is.null(group)) configuration$group <- group
if (!is.null(first_group)) configuration$first_group <- first_group
emit("hypothesis", "bayesTTestMenuItem", "tabelz.analysis.bayesian.jzs:compute_bayes_ttest",
id, title, kind, configuration, outputs,
c(definition,
sprintf("BF10 = integral over g of (1 + N g r^2)^(-1/2) (1 + t^2/((1 + N g r^2) nu))^(-(nu+1)/2) (2 pi)^(-1/2) g^(-3/2) exp(-1/(2g)) dg divided by (1 + t^2/nu)^(-(nu+1)/2) (Rouder et al. 2009, eq. 1), r = %s, by R integrate on u = log g.", format(r, digits = 17)),
"Posterior of delta proportional to the noncentral t density of t with ncp delta sqrt(N) (written as an integral over the chi-square denominator) times the Cauchy(0, r) prior; moments and quantiles by R integrate and uniroot.",
"Tolerance 1e-6: both sides integrate numerically (R integrate rel.tol 1e-12; HypoNum adaptive quadrature with estimated error below 1e-6).",
paste0(manual, ":21-22: 't tests & Cauchy prior on standardized effect, default scale 1/sqrt(2); declare the null mean or paired difference and the comparison direction.'"),
paste0(manual, ":38-40: 'For independent-group effects, verify which group is subtracted from which.'")),
"R stats: integrate, uniroot, dnorm, dchisq, dcauchy",
"Rouder, Speckman, Sun, Morey and Iverson (2009), Bayesian t tests for accepting and rejecting the null hypothesis, Psychonomic Bulletin & Review 16(2), 225-237, eq. 1; R Core Team, stats package help: integrate, uniroot.",
"REQ-STAT-HYP-BAYES-TTEST-001")
}
ttest_case("STAT-HYP-BAYES-TTEST-001", "One-sample JZS t, default scale", "nominal",
"one_sample", "d")
ttest_case("STAT-HYP-BAYES-TTEST-002", "Paired JZS t", "axis_coverage", "paired", "d",
second = "d2")
ttest_case("STAT-HYP-BAYES-TTEST-003", "Independent-samples JZS t, code-point group order",
"axis_coverage", "independent", "y", group = "g")
ttest_case("STAT-HYP-BAYES-TTEST-004", "Independent-samples JZS t, group B first",
"axis_coverage", "independent", "y", group = "g", first_group = "B")
ttest_case("STAT-HYP-BAYES-TTEST-005", "One-sample JZS t, scale 1, null 0.3, 90% interval",
"axis_coverage", "one_sample", "d", null_value = 0.3, r = 1, level = 0.9)
# ---------------------------------------------------------------- Pearson correlation
correlation_case <- function(id, title, kind, first, second, width, level) {
x <- dat[[first]]
yv <- dat[[second]]
n <- length(x)
r <- stats::cor(x, yv)
power <- n - 1
alpha <- 1 / width
log_lik <- function(rho) {
base <- 1 - rho * r
inner <- stats::integrate(function(w) exp(-power * log((cosh(w) - rho * r) / base)),
0, Inf, rel.tol = 1e-12, abs.tol = 0, subdivisions = 1000L)$value
power / 2 * log1p(-rho^2) - power * log(base) + log(inner)
}
log_prior <- function(rho) stats::dbeta((rho + 1) / 2, alpha, alpha, log = TRUE) - log(2)
log_l0 <- log_lik(0)
bf <- stats::integrate(function(rho) vapply(rho, function(v) exp(log_prior(v) + log_lik(v) - log_l0), 0),
-1, 1, rel.tol = 1e-11, abs.tol = 0, subdivisions = 2000L)$value
log_bf <- log(bf)
s <- numeric_posterior(function(rho) log_prior(rho) + log_lik(rho), -1, 1, level)
outputs <- c(
list("/n" = count_of(n, "Number of complete pairs.")),
bf_outputs(log_bf, "correlation", kind = quad),
numeric_outputs("/posteriors/0", s, "posterior of rho"))
emit("hypothesis", "bayesCorrelationMenuItem",
"tabelz.analysis.bayesian.jzs:compute_bayes_correlation", id, title, kind,
list(first = first, second = second, prior_width = width, confidence_level = level),
outputs,
c(sprintf("r = %s from n = %d pairs.", format(r, digits = 17), n),
"Likelihood of rho (Ly, Verhagen and Wagenmakers 2016, eq. 7): L(rho) proportional to (1 - rho^2)^((n-1)/2) integral_0^inf (cosh w - rho r)^-(n-1) dw, by R integrate.",
sprintf("Prior: stretched Beta(1/width, 1/width) on (-1, 1), width %g, density dbeta((rho + 1)/2, 1/width, 1/width)/2; BF10 = integral prior(rho) L(rho)/L(0) drho by R integrate.", width),
"Posterior moments and quantiles of rho by R integrate and uniroot on the normalized prior times likelihood.",
"Tolerance 1e-6: both sides integrate numerically (nested R integrate rel.tol 1e-11/1e-12; HypoNum adaptive quadrature with estimated error below 1e-6).",
paste0(manual, ":23-24: 'Pearson correlation & Stretched beta prior on correlation in (-1,1); default width 1 is uniform.'")),
"R stats: integrate, uniroot, dbeta, cor",
"Ly, Verhagen and Wagenmakers (2016), Harold Jeffreys's default Bayes factor hypothesis tests: explanation, extension, and application in psychology, Journal of Mathematical Psychology 72, 19-32, eqs. 7 and 9; R Core Team, stats package help: integrate, uniroot.",
"REQ-STAT-HYP-BAYES-CORRELATION-001")
}
correlation_case("STAT-HYP-BAYES-CORRELATION-001", "Uniform prior (width 1) on x1 and y",
"nominal", "x1", "y", 1, 0.95)
correlation_case("STAT-HYP-BAYES-CORRELATION-002", "Stretched Beta(2, 2) prior (width 0.5)",
"axis_coverage", "x1", "y", 0.5, 0.95)
correlation_case("STAT-HYP-BAYES-CORRELATION-003", "Weak correlation x2 and d, 90% interval",
"axis_coverage", "x2", "d", 1, 0.9)
# ---------------------------------------------------------------- Shared g mixtures
# The posterior of u = log g is integrated over (-60, 60): beyond that range the inverse-gamma
# or hyper-g prior factor underflows to zero on the log scale used here.
U_LIMIT <- 60
# Posterior summaries of a Student-t mixture over u = log g with df degrees of freedom:
# conditional(u) returns centre and scale for each coefficient; log_w(u) is the unnormalized
# log posterior of u.
mixture_summaries <- function(log_w, conditional, df, k, level) {
w <- function(u) vapply(u, function(v) exp(log_w(v)), 0)
Z <- stats::integrate(w, -U_LIMIT, U_LIMIT, rel.tol = 1e-11, abs.tol = 0, subdivisions = 2000L)$value
average <- function(f) stats::integrate(function(u) vapply(u, function(v) f(v) * exp(log_w(v)), 0),
-U_LIMIT, U_LIMIT, rel.tol = 1e-11, abs.tol = 0,
subdivisions = 2000L)$value / Z
tail <- (1 - level) / 2
lapply(seq_len(k), function(j) {
mean <- average(function(u) conditional(u)$centre[[j]])
second <- average(function(u) {
cs <- conditional(u)
cs$centre[[j]]^2 + cs$scale[[j]]^2 * df / (df - 2)
})
cdf <- function(b) average(function(u) {
cs <- conditional(u)
stats::pt((b - cs$centre[[j]]) / cs$scale[[j]], df)
})
sd <- sqrt(second - mean^2)
quantile <- function(p) stats::uniroot(function(b) cdf(b) - p, c(mean - 12 * sd, mean + 12 * sd),
tol = 1e-13)$root
list(mean = mean, sd = sd, median = quantile(0.5), lower = quantile(tail),
upper = quantile(1 - tail))
})
}
log_inverse_gamma <- function(g, shape, scale) {
shape * log(scale) - lgamma(shape) - (shape + 1) * log(g) - scale / g
}
# ---------------------------------------------------------------- One-way ANOVA
anova_case <- function(id, title, kind, factor, r, level) {
yv <- dat$y
labels <- dat[[factor]]
levels <- sort(unique(labels))
a <- length(levels)
n <- length(yv)
Z <- outer(labels, levels, "==") * 1
Q <- stats::contr.helmert(a)
Q <- Q %*% diag(1 / sqrt(colSums(Q^2)), a - 1, a - 1)
X <- Z %*% Q
X <- sweep(X, 2, colMeans(X))
yc <- yv - mean(yv)
syy <- sum(yc^2)
M <- crossprod(X)
xty <- crossprod(X, yc)
log_bf_g <- function(g) {
A <- M + diag(1 / g, a - 1, a - 1)
fitted <- as.numeric(crossprod(xty, solve(A, xty)))
-0.5 * as.numeric(determinant(diag(a - 1) + g * M, logarithm = TRUE)$modulus) -
(n - 1) / 2 * log((syy - fitted) / syy)
}
log_w <- function(u) {
g <- exp(u)
log_bf_g(g) + log_inverse_gamma(g, 0.5, r^2 / 2) + u
}
bf <- stats::integrate(function(u) vapply(u, function(v) exp(log_w(v)), 0), -U_LIMIT, U_LIMIT,
rel.tol = 1e-11, abs.tol = 0, subdivisions = 2000L)$value
conditional <- function(u) {
g <- exp(u)
A <- M + diag(1 / g, a - 1, a - 1)
inverse <- solve(A)
m <- inverse %*% xty
s2 <- (syy - as.numeric(crossprod(xty, m))) / (n - 1)
list(centre = as.numeric(Q %*% m), scale = sqrt(diag(s2 * Q %*% inverse %*% t(Q))))
}
s <- mixture_summaries(log_w, conditional, n - 1, a, level)
outputs <- c(list("/n" = count_of(n, "Number of observations.")),
bf_outputs(log(bf), "JZS ANOVA", kind = quad))
for (j in seq_len(a)) {
outputs <- c(outputs, numeric_outputs(sprintf("/posteriors/%d", j - 1), s[[j]],
sprintf("effect of %s", levels[[j]])))
}
emit("hypothesis", "bayesAnovaMenuItem", "tabelz.analysis.bayesian.linear:compute_bayes_anova",
id, title, kind,
list(response = "y", factor = factor, prior_scale = r, confidence_level = level),
outputs,
c(sprintf("%d levels (%s, in code-point order), n = %d; level effects coded by normalized Helmert contrasts Q (any orthonormal basis of the sum-to-zero space gives the same Bayes factor and level effects Q theta).", a, paste(levels, collapse = ", "), n),
sprintf("BF10(g) = |I + g X'PX|^(-1/2) ((y'Py - y'PX (X'PX + I/g)^-1 X'Py)/y'Py)^(-(n-1)/2) with P the centring matrix, averaged over g ~ Inverse-Gamma(1/2, r^2/2), r = %g (Rouder, Morey, Speckman and Province 2012, eqs. 7-10), by R integrate on u = log g.", r),
"Posterior of each level effect: given g, Student-t on n - 1 df with centre Q (X'X + I/g)^-1 X'y and scale from s^2 Q (X'X + I/g)^-1 Q', s^2 = residual/(n - 1), mixed over the posterior of g by R integrate; quantiles by uniroot on the mixture CDF; SD by the law of total variance.",
"Tolerance 1e-6: both sides integrate numerically.",
paste0(manual, ":31-32: 'One-way ANOVA & Shared scale for fixed level effects, default 0.5; at most ten levels.'")),
"R stats: integrate, uniroot, pt, contr.helmert, determinant, solve",
"Rouder, Morey, Speckman and Province (2012), Default Bayes factors for ANOVA designs, Journal of Mathematical Psychology 56(5), 356-374, eqs. 7-10; R Core Team, stats package help: integrate, uniroot.",
"REQ-STAT-HYP-BAYES-ANOVA-001")
}
anova_case("STAT-HYP-BAYES-ANOVA-001", "Three-level factor, scale 0.5", "nominal", "g3", 0.5, 0.95)
anova_case("STAT-HYP-BAYES-ANOVA-002", "Three-level factor, scale 1", "axis_coverage", "g3", 1, 0.95)
anova_case("STAT-HYP-BAYES-ANOVA-003", "Two-level factor, 90% interval", "axis_coverage", "g", 0.5, 0.9)
# ---------------------------------------------------------------- Linear regression
regression_case <- function(id, title, kind, predictors, models, model_prior = NULL,
null_prior = NULL, prior = "zellner_siow", r = sqrt(2) / 4,
hyper_a = 3, level = 0.95) {
yv <- dat$y
n <- length(yv)
yc <- yv - mean(yv)
syy <- sum(yc^2)
if (is.null(models)) {
models <- unlist(lapply(seq_along(predictors), function(size)
utils::combn(predictors, size, simplify = FALSE)), recursive = FALSE)
}
log_prior_g <- if (prior == "hyper_g") {
function(g) log((hyper_a - 2) / 2) - hyper_a / 2 * log1p(g)
} else {
function(g) log_inverse_gamma(g, 0.5, n * r^2 / 2)
}
fits <- lapply(models, function(model) {
X <- as.matrix(dat[model])
X <- sweep(X, 2, colMeans(X))
p <- ncol(X)
beta <- solve(crossprod(X), crossprod(X, yc))
r2 <- 1 - sum((yc - X %*% beta)^2) / syy
log_w <- function(u) {
g <- exp(u)
(n - 1 - p) / 2 * log1p(g) - (n - 1) / 2 * log1p(g * (1 - r2)) + log_prior_g(g) + u
}
bf <- stats::integrate(function(u) vapply(u, function(v) exp(log_w(v)), 0), -U_LIMIT, U_LIMIT,
rel.tol = 1e-11, abs.tol = 0, subdivisions = 2000L)$value
list(X = X, p = p, beta = as.numeric(beta), r2 = r2, log_w = log_w, log_bf = log(bf))
})
priors <- if (is.null(model_prior)) rep(1, length(models) + 1) else c(model_prior, null_prior)
priors <- priors / sum(priors)
logs <- c(vapply(fits, `[[`, 0, "log_bf"), 0)
weights <- log(priors) + logs
post <- exp(weights - max(weights))
post <- post / sum(post)
best <- which.max(post)
outputs <- list("/n" = count_of(n, "Number of complete rows."))
for (k in seq_along(models)) {
key <- paste0("/bayes_factors/[numerator=", paste(models[[k]], collapse = " + "), "]")
outputs[[paste0(key, "/bf10")]] <- quad(exp(fits[[k]]$log_bf), "BF10 against intercept only")
outputs[[paste0(key, "/bf01")]] <- quad(exp(-fits[[k]]$log_bf), "BF01")
outputs[[paste0(key, "/log_bf10")]] <- quad(fits[[k]]$log_bf, "log BF10")
outputs[[paste0(key, "/posterior_probability")]] <- quad(post[[k]], "posterior model probability")
outputs[[paste0(key, "/prior_probability")]] <- closed(priors[[k]], "prior model probability")
}
selected <- "intercept only"
if (best <= length(models)) {
fit <- fits[[best]]
inverse_diagonal <- diag(solve(crossprod(fit$X)))
conditional <- function(u) {
g <- exp(u)
shrink <- g / (1 + g)
s2 <- syy * (1 - shrink * fit$r2) / (n - 1)
list(centre = shrink * fit$beta, scale = sqrt(shrink * s2 * inverse_diagonal))
}
s <- mixture_summaries(fit$log_w, conditional, n - 1, fit$p, level)
for (j in seq_len(fit$p)) {
outputs <- c(outputs, numeric_outputs(sprintf("/posteriors/%d", j - 1), s[[j]],
sprintf("coefficient of %s", models[[best]][[j]])))
}
selected <- paste(models[[best]], collapse = " + ")
}
configuration <- list(response = "y", predictors = as.list(predictors), prior = prior,
prior_scale = r, hyper_a = hyper_a, confidence_level = level)
if (!is.null(model_prior)) {
configuration$models <- lapply(models, function(m) paste(m, collapse = " + "))
configuration$model_prior <- as.list(model_prior)
configuration$null_model_prior <- null_prior
}
emit("regression", "bayesRegressionMenuItem",
"tabelz.analysis.bayesian.linear:compute_bayes_regression", id, title, kind,
configuration, outputs,
c(sprintf("Candidate models: %s, plus intercept only; n = %d; the most probable model (null included) is %s, whose coefficient posteriors are compared by index.",
paste(vapply(models, paste, "", collapse = " + "), collapse = "; "), n, selected),
"BF10(g) = (1 + g)^((n-1-p)/2) (1 + g (1 - R^2))^(-(n-1)/2) against the intercept-only model (Liang, Paulo, Molina, Clyde and Berger 2008, eq. 5), averaged over g by R integrate on u = log g.",
if (prior == "hyper_g") sprintf("Hyper-g prior pi(g) = (a - 2)/2 (1 + g)^(-a/2), a = %g (Liang et al. 2008, eq. 9).", hyper_a)
else sprintf("Zellner-Siow prior g ~ Inverse-Gamma(1/2, n r^2/2), r = %s (Liang et al. 2008, section 3.2).", format(r, digits = 17)),
"Posterior model probabilities proportional to prior probability times BF10 over the candidate set including the intercept-only model (BF 1).",
"Coefficient posteriors given g: Student-t on n - 1 df with centre g/(1+g) beta-hat and scale sqrt(g/(1+g) s^2 (X'X)^-1_jj), s^2 = SSY (1 - g/(1+g) R^2)/(n - 1), mixed over the posterior of g by R integrate; quantiles by uniroot.",
"Tolerance 1e-6: both sides integrate numerically.",
paste0(manual, ":33-34: 'Linear regression & Zellner--Siow or hyper-g prior; declare candidate models and their prior probabilities, including the intercept-only model.'"),
paste0(manual, ":93-96: 'Regression compares up to 64 declared models ... Coefficient summaries for the most probable model are conditional on that model'")),
"R stats: integrate, uniroot, pt, solve, combn",
"Liang, Paulo, Molina, Clyde and Berger (2008), Mixtures of g priors for Bayesian variable selection, Journal of the American Statistical Association 103(481), 410-423, eqs. 5 and 9, section 3.2; R Core Team, stats package help: integrate, uniroot.",
"REQ-STAT-REG-BAYES-REGRESSION-001")
}
regression_case("STAT-REG-BAYES-REGRESSION-001", "All models of x1 and x2, Zellner-Siow medium scale",
"nominal", c("x1", "x2"), NULL)
regression_case("STAT-REG-BAYES-REGRESSION-002", "Hyper-g prior, a = 3", "axis_coverage",
c("x1", "x2"), NULL, prior = "hyper_g", hyper_a = 3)
regression_case("STAT-REG-BAYES-REGRESSION-003", "Zellner-Siow unit scale, 90% interval",
"axis_coverage", c("x1", "x2"), NULL, r = 1, level = 0.9)
regression_case("STAT-REG-BAYES-REGRESSION-004", "Declared models with custom prior weights",
"axis_coverage", c("x1", "x2"), list("x1", c("x1", "x2")),
model_prior = c(0.5, 0.25), null_prior = 0.25)
cat("Bayesian cases generated\n")
generators/r/hypothesis_bootstrap_estimate.R
source("common.R", local = TRUE)
# Bootstrap an estimate (Statistics > Compare means & ANOVA > Bootstrap an estimate). The
# resampling stream is NumPy's seeded generator and cannot be reproduced in R, so each case
# compares the deterministic quantities only: the statistic on the full sample, the rows used,
# the missing count and the replicate counts. The bootstrap summaries (SE, bias, mean,
# percentile limits and their Monte Carlo errors) are listed as deferred outputs.
constructed <- data.frame(
v = c(12.4, 9.8, 15.1, 11.3, NA, 13.7, 10.2, 16.4, 12.9, 8.7, 14.6, 11.9, 13.2, 9.4, 15.8, 12.1, 10.8, 14.1, 11.6, 13.5),
w = c(20.1, 18.4, 23.9, 19.7, 21.0, 22.6, 18.9, 25.2, 21.4, 17.5, 23.1, 20.6, 21.8, 18.0, 24.4, 20.9, 19.3, 22.8, 20.2, 22.0),
g = rep(c("A", "B"), 10),
stringsAsFactors = FALSE
)
export_dataset(
constructed, "HYP_BOOT", "Constructed by hypothesis_bootstrap_estimate.R",
"R base: mean, median, sd, mean(trim =), quantile(type = 7), cor",
"Constructed test data; no third-party dataset license.",
c(v = "Float64", w = "Float64", g = "String")
)
d <- read_dataset("HYP_BOOT")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label)
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("R", label, "from re-read HYP_BOOT CSV."))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
word <- function(x, label) list(comparison = "exact", value = as.character(x), rationale = label)
entry <- "tabelz.analysis.bootstrap_framework:compute_bootstrap_estimate"
menu <- "bootstrapEstimateMenuItem"
resamples <- 200L
deferred <- function(prefix = "/components/0") {
reason <- "The replicates come from NumPy's seeded generator (numpy.random.default_rng(seed) in batches as the F12 bootstrap); R cannot reproduce that stream, so no independent expected value exists for this resampling summary."
lapply(c("bootstrap_mean", "bias", "standard_error", "mc_se_bias", "mc_se_standard_error",
"percentile_lower", "percentile_upper"),
function(name) list(path = paste0(prefix, "/", name), reason = reason))
}
notes_common <- c(
"Rows with a missing or non-finite value in any required column are dropped and counted; the estimate is the statistic on the remaining rows; attempted = resamples and successful = resamples when every replicate is defined.",
"docs/manual/chapters/77-bootstrap-estimates.tex:11-17: 'Available statistics include means, medians, trimmed means, standard deviations, quantiles, correlations ... two-group mean differences, means minus a fixed target, means or medians of paired differences'",
"docs/manual/chapters/77-bootstrap-estimates.tex:27-31: 'The defaults are 5,000 replicates, seed 20260928, row resampling and a 95% percentile interval'"
)
emit <- function(id, title, kind, config, outputs, notes, extra_deferred = list()) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs, expected_error = NULL,
deferred_outputs = c(deferred(), extra_deferred), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/HYP_BOOT.json", sha256 = ""),
excluded_rows = list(), configuration = c(config, list(resamples = resamples, seed = 20260928L,
confidence_level = 0.95, intervals = "percentile", scheme = "rows")),
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R 4.6.1 base and stats",
citation = "R Core Team, base and stats package help: mean, median, sd, quantile, cor.",
generator = list(path = "generators/r/hypothesis_bootstrap_estimate.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, notes_common, "Expected computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-BOOTSTRAP-ESTIMATE-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
counts <- function(rows_used, estimate, label, name = NULL) {
outputs <- list(
"/rows_used" = exact(rows_used, "complete analysis rows"),
"/missing_count" = exact(nrow(d) - rows_used, "rows dropped for a missing value"),
"/excluded_count" = exact(0L, "no excluded rows"),
"/units" = exact(rows_used, "row resampling: one unit per row"),
"/components/0/estimate" = tol(estimate, label),
"/components/0/attempted" = exact(resamples, "resamples requested"),
"/components/0/successful" = exact(resamples, "every replicate of this statistic is defined"),
"/components/0/failed" = exact(0L, "no failed replicates")
)
if (!is.null(name)) outputs[["/components/0/name"]] <- word(name, "component label")
outputs
}
v <- d$v[is.finite(d$v)]
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-001", "Bootstrap estimate mean", "nominal",
list(statistic = "mean", column = "v"),
counts(length(v), mean(v), "mean of the 19 finite values"),
"Mean: one row of v is missing, so 19 rows are used.")
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-002", "Bootstrap estimate median", "axis_coverage",
list(statistic = "median", column = "v"),
counts(length(v), stats::median(v), "median of the 19 finite values"),
"Median of an odd count of values is the middle order statistic.")
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-003", "Bootstrap estimate standard deviation", "axis_coverage",
list(statistic = "sd", column = "v"),
counts(length(v), stats::sd(v), "sample SD (n - 1) of the 19 finite values"),
"Standard deviation with the n - 1 divisor.")
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-004", "Bootstrap estimate 10 percent trimmed mean", "axis_coverage",
list(statistic = "trimmed_mean", column = "v", trim = 0.1),
counts(length(v), mean(v, trim = 0.1), "mean(trim = 0.1): floor(0.1 n) values cut from each end"),
"Trimmed mean: R's mean(trim = 0.1) and SciPy's trim_mean both drop floor(0.1 n) = 1 value from each end of the 19 sorted values.")
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-005", "Bootstrap estimate first quartile", "axis_coverage",
list(statistic = "quantile", column = "v", probability = 0.25),
counts(length(v), stats::quantile(v, 0.25, type = 7, names = FALSE), "quantile(0.25, type = 7)"),
"Quantile with R's type 7 linear interpolation (NumPy's default 'linear' method).")
complete <- is.finite(d$v) & is.finite(d$w)
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-006", "Bootstrap estimate Pearson correlation", "axis_coverage",
list(statistic = "pearson", column = "v", second = "w"),
counts(sum(complete), stats::cor(d$v[complete], d$w[complete]), "Pearson r on the complete pairs"),
"Pearson correlation of v and w on the 19 rows complete on both.")
a <- d$v[is.finite(d$v) & d$g == "A"]
b <- d$v[is.finite(d$v) & d$g == "B"]
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-007", "Bootstrap estimate mean difference B minus A", "axis_coverage",
list(statistic = "mean_difference", column = "v", group = "g", first_group = "B"),
counts(length(a) + length(b), mean(b) - mean(a), "mean of group B minus mean of group A", "Mean of v: B minus A"),
"Difference of two group means with the declared first group B: mean(B) - mean(A) on the finite rows.")
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-008", "Bootstrap estimate mean minus target", "axis_coverage",
list(statistic = "mean_minus_target", column = "v", target = 12),
counts(length(v), mean(v) - 12, "mean of v minus the fixed target 12"),
"Mean minus target: the target 12 is fixed in every replicate.")
emit("STAT-HYP-BOOTSTRAP-ESTIMATE-009", "Bootstrap estimate mean of paired differences", "axis_coverage",
list(statistic = "paired_mean_difference", column = "v", second = "w"),
counts(sum(complete), mean(d$v[complete] - d$w[complete]), "mean of v minus w over the complete pairs"),
"Mean of paired differences v - w; each complete row is one pair.")
cat("bootstrap estimate cases generated\n")
generators/r/hypothesis_boundaries.R
source("common.R", local = TRUE)
edge <- data.frame(x = c(1.0, 2.0, 1.2, 2.3), g = c("A", "A", "B", "B"),
stringsAsFactors = FALSE)
export_dataset(edge, "HYP_EDGE", "Constructed two-observation-per-group edge data",
"R stats::t.test, ptukey and finite permutation enumeration.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", g = "String"))
e <- read_dataset("HYP_EDGE")
blocks <- data.frame(
block = rep(c("B1", "B2"), each = 3), treatment = rep(c("A", "B", "C"), 2),
x = c(1.0, 2.0, 3.0, 2.0, 2.8, 4.0), stringsAsFactors = FALSE)
export_dataset(blocks, "HYP_EDGE_BLOCKS", "Constructed minimum complete-block design",
"R stats::friedman.test help; two blocks and three treatments.",
"Constructed test data; no third-party dataset license.",
c(block = "String", treatment = "String", x = "Float64"))
bl <- read_dataset("HYP_EDGE_BLOCKS")
constant <- data.frame(x = rep(1.0, 4), g = c("A", "A", "B", "B"),
stringsAsFactors = FALSE)
export_dataset(constant, "HYP_CONSTANT", "Constructed zero-variance edge data",
"Tabelz manual chapters 07, 16, 32-34: degenerate design boundaries.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", g = "String"))
zero_rate <- data.frame(count = rep(0L, 4), exposure = c(8, 10, 9, 11))
export_dataset(zero_rate, "HYP_ZERO_RATE", "Constructed zero-event exposure data",
"R stats::poisson.test help; exact zero-count rate interval.",
"Constructed test data; no third-party dataset license.",
c(count = "Int64", exposure = "Float64"))
z <- read_dataset("HYP_ZERO_RATE")
large <- data.frame(x = seq_len(22) / 10, g = rep(c("A", "B"), each = 11),
stringsAsFactors = FALSE)
export_dataset(large, "HYP_PERM_LIMIT", "Constructed 22-row exact-enumeration boundary",
"R utils::choose help; choose(22,11) exceeds the stated 20,000-assignment budget.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", g = "String"))
tiny <- read_dataset("HYP_TINY")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R boundary reference:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R boundary tail:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, entry, menu, dataset, config, outputs = list(),
error = NULL, notes, excluded = list()) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else stats::setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Boundary:", id), case_kind = "boundary",
dataset = if (is.null(dataset)) NULL else list(
path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = if (is.null(error)) "independent_implementation" else "analytic",
source = "R 4.6.1 stats and manual-defined design boundaries",
citation = "R stats help: t.test, poisson.test, friedman.test, ptukey, power.t.test; Tabelz manual chapters 07, 16, 31-34, 62.",
generator = list(path = "generators/r/hypothesis_boundaries.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R numerical values use the exported CSV; invalid designs have no estimator.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
# Hypothesis test: smallest accepted t sample and unestimable zero variance.
t_min <- stats::t.test(tiny$x, mu = 0)
emit("STAT-HYP-BOUNDARY-TEST-MIN-001", "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
"hypothesisTestsMenuItem", "HYP_TINY",
list(test = "one_sample_t", response = "x", target = 0, alpha = 0.1),
list("/statistic" = tol(t_min$statistic, "n=2 Student t"),
"/p_value" = prob(t_min$p.value, "n=2 Student t tail")),
notes = list("Two nonconstant finite observations are the minimum for a Student t statistic."))
emit("STAT-HYP-BOUNDARY-TEST-CONSTANT-001", "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
"hypothesisTestsMenuItem", "HYP_CONSTANT",
list(test = "one_sample_t", response = "x", target = 0),
error = list(error_class = "ValueError", message_contains = "no sample variation"),
notes = list("Zero sample variance makes a finite t statistic undefined."))
# Power: smallest planned n and almost-null positive standardized effect.
p_min <- stats::power.t.test(n = 2, delta = 0.5, sd = 1, sig.level = 0.1,
type = "one.sample", alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-BOUNDARY-POWER-MIN-001", "tabelz.analysis.hypothesis_tests:run_power_analysis",
"hypothesisTestsMenuItem", NULL,
list(analysis = "one_sample_t", solve_for = "power", sample_size = 2L,
effect_size = 0.5, alpha = 0.1),
list("/value" = tol(p_min$power, "n=2 noncentral t power")),
notes = list("Sample size two is the smallest accepted planned t sample."))
p_near <- stats::power.t.test(n = 30, delta = 1e-8, sd = 1, sig.level = 0.05,
type = "one.sample", alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-BOUNDARY-POWER-NEARNULL-001", "tabelz.analysis.hypothesis_tests:run_power_analysis",
"hypothesisTestsMenuItem", NULL,
list(analysis = "one_sample_t", solve_for = "power", sample_size = 30L,
effect_size = 1e-8),
list("/value" = tol(p_near$power, "tiny-positive-effect noncentral t power")),
notes = list("Effect size is positive but near zero, testing the no-effect boundary."))
# Two retained per group is the Welch and comparison minimum.
a <- e$x[e$g == "A"]
b <- e$x[e$g == "B"]
delta <- mean(a) - mean(b)
va <- var(a) / length(a)
vb <- var(b) / length(b)
se <- sqrt(va + vb)
df <- (va + vb)^2 / (va^2 / (length(a) - 1) + vb^2 / (length(b) - 1))
emit("STAT-HYP-BOUNDARY-EQUIV-MIN-001", "tabelz.executor.equivalence", "equivalenceMenuItem",
"HYP_EDGE",
list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = -3, upper_bound = 3, alpha = 0.1, max_rows = 100L),
list("/difference_a_minus_b" = tol(delta, "n=2/group A-B mean"),
"/p_lower" = prob(stats::pt((delta + 3) / se, df, lower.tail = FALSE), "Welch lower TOST")),
notes = list("Two finite observations per selected group; Welch-Satterthwaite df."))
emit("STAT-HYP-BOUNDARY-EQUIV-ZEROSE-001", "tabelz.executor.equivalence", "equivalenceMenuItem",
"HYP_CONSTANT",
list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = -1, upper_bound = 1),
error = list(error_class = "EquivalenceExecutionError", message_contains = "zero standard error"),
notes = list("Both groups are constant at the same value; Welch standard error is zero."))
# One-sample equivalence minimum and zero-variance boundary.
mini_effect <- mean(tiny$x - 1.5)
mini_se <- sd(tiny$x - 1.5) / sqrt(nrow(tiny))
emit("STAT-HYP-BOUNDARY-EQUIVEXT-MIN-001", "tabelz.executor.equivalence_extensions",
"equivalenceMenuItem", "HYP_TINY",
list(mode = "one_sample_difference", response_column = "x", target = 1.5,
lower_bound = -3, upper_bound = 3, alpha = 0.1, max_rows = 100L),
list("/estimate" = tol(mini_effect, "n=2 mean-target effect"),
"/p_lower" = prob(stats::pt((mini_effect + 3) / mini_se, 1,
lower.tail = FALSE), "n=2 lower TOST")),
notes = list("Two finite nonconstant responses are the one-sample TOST minimum."))
emit("STAT-HYP-BOUNDARY-EQUIVEXT-ZEROSE-001", "tabelz.executor.equivalence_extensions",
"equivalenceMenuItem", "HYP_CONSTANT",
list(mode = "one_sample_difference", response_column = "x", target = 1,
lower_bound = -1, upper_bound = 1),
error = list(error_class = "EquivalenceExtensionsExecutionError", message_contains = "zero standard error"),
notes = list("Four identical responses make the one-sample standard error zero."))
# Known sigma can use one observation; zero events retain a positive upper bound.
z_min <- (tiny$x[[2]] - 0) / 1
emit("STAT-HYP-BOUNDARY-BASIC-MIN-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem", "HYP_TINY",
list(method = "known_sigma_z", response_column = "x", known_sigma = 1,
null_mean = 0, alpha = 0.1, max_rows = 100L),
list("/statistic" = tol(z_min, "one-observation known-sigma z"),
"/p_value" = prob(2 * stats::pnorm(-abs(z_min)), "normal tail")),
notes = list("One finite response is sufficient when population sigma is supplied."),
excluded = list(0L))
count <- sum(z$count)
exposure <- sum(z$exposure)
rate_test <- stats::poisson.test(count, T = exposure, r = 0.1)
expected_count <- 0.1 * exposure
p_zero <- min(1, 2 * min(stats::ppois(count, expected_count),
stats::ppois(count - 1, expected_count, lower.tail = FALSE)))
emit("STAT-HYP-BOUNDARY-BASIC-ZEROEVENT-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem", "HYP_ZERO_RATE",
list(method = "one_poisson_rate", count_column = "count", exposure_column = "exposure",
null_rate = 0.1),
list("/estimate" = tol(0, "zero event rate"),
"/p_value" = prob(p_zero, "doubled minimum Poisson tail"),
"/upper" = tol(rate_test$conf.int[[2]], "positive exact upper rate bound")),
notes = list("Zero observed events; manual chapter 31 specifies doubled minimum-tail p-value."))
# Independent exact permutation has two-per-group minimum and bounded assignments.
idx <- utils::combn(seq_len(nrow(e)), 2)
sa <- colSums(matrix(e$x[idx], nrow = 2))
null <- sa / 2 - (sum(e$x) - sa) / 2
p_perm <- mean(abs(null) >= abs(delta) - 1e-12)
emit("STAT-HYP-BOUNDARY-PERM-MIN-001", "tabelz.executor.permutation_tests",
"permutationTestsMenuItem", "HYP_EDGE",
list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "exact", max_rows = 100L,
max_cells = 1000L),
list("/statistic" = tol(delta, "n=2/group A-B mean"),
"/p_value" = prob(p_perm, "all six assignments")),
notes = list("Two complete responses per group; exactly choose(4,2)=6 label assignments."))
emit("STAT-HYP-BOUNDARY-PERM-LIMIT-001", "tabelz.executor.permutation_tests",
"permutationTestsMenuItem", "HYP_PERM_LIMIT",
list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "exact"),
error = list(error_class = "PermutationTestsExecutionError", message_contains = "above the bounded budget"),
notes = list("choose(22,11) exact assignments exceed the documented 20,000 budget."))
# Friedman minimum complete design and all-tied runs boundary.
friedman <- stats::friedman.test(x ~ treatment | block, data = bl)
emit("STAT-HYP-BOUNDARY-RANK-MIN-001", "tabelz.executor.blocked_rank_tests",
"blockedRankTestsMenuItem", "HYP_EDGE_BLOCKS",
list(method = "friedman", response_column = "x", treatment_column = "treatment",
block_column = "block", max_rows = 100L, max_blocks = 10L),
list("/friedman_statistic" = tol(friedman$statistic, "two-block Friedman chi-square"),
"/p_value" = prob(friedman$p.value, "Friedman chi-square tail")),
notes = list("Two complete blocks with three treatments each are the minimum design."))
emit("STAT-HYP-BOUNDARY-RANK-TIES-001", "tabelz.executor.blocked_rank_tests",
"blockedRankTestsMenuItem", "HYP_CONSTANT",
list(method = "runs", response_column = "x"),
error = list(error_class = "BlockedRankTestsExecutionError", message_contains = "at least one observation in each category"),
notes = list("All numeric values equal the median and are omitted; no runs sequence remains."))
# Games-Howell minimum and zero-variance contrast boundary.
emit("STAT-HYP-BOUNDARY-COMP-MIN-001", "tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem", "HYP_EDGE",
list(response_column = "x", group_column = "g", family = "games_howell",
alpha = 0.1, max_rows = 100L, max_groups = 3L),
list("/comparisons/0/estimate" = tol(delta, "two-per-group A-B contrast")),
notes = list("Two finite observations per group permit a two-group Games-Howell contrast."))
emit("STAT-HYP-BOUNDARY-COMP-ZEROVAR-001", "tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem", "HYP_CONSTANT",
list(response_column = "x", group_column = "g", family = "games_howell"),
error = list(error_class = "AdditionalComparisonsExecutionError", message_contains = "zero-variance groups"),
notes = list("Both comparison groups have zero variance; the Welch pairwise SE is zero."))
# Bootstrap two-response minimum and degenerate constant resample distribution.
emit("STAT-HYP-BOUNDARY-BOOT-MIN-001", "tabelz.executor.bootstrap_estimates",
"bootstrapMenuItem", "HYP_TINY",
list(column = "x", statistic = "mean", resamples = 20L, seed = 17L,
confidence_level = 0.9, histogram_bins = 5L, max_rows = 100L),
list("/estimate" = tol(mean(tiny$x), "n=2 sample mean")),
notes = list("Two finite responses are the bootstrap minimum; only point statistic compared."))
emit("STAT-HYP-BOUNDARY-BOOT-CONSTANT-001", "tabelz.executor.bootstrap_estimates",
"bootstrapMenuItem", "HYP_CONSTANT",
list(column = "x", statistic = "mean", resamples = 20L, seed = 17L),
list("/estimate" = tol(1, "constant sample mean"),
"/degenerate" = exact(TRUE, "Every resample has the same point statistic.")),
notes = list("All finite responses equal one, so all bootstrap means are one."))
cat("hypothesis boundary cases generated\n")
generators/r/hypothesis_completion.R
source("common.R", local = TRUE)
d <- read_dataset("HYP_CORE")
b <- read_dataset("HYP_BLOCKS")
r <- read_dataset("HYP_RATES")
pairs <- read_dataset("HYP_PAIRS")
run <- generator_run(c("stats", "multcomp", "mvtnorm", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R independent completion reference:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R independent tail:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, entry, menu, dataset, config, outputs, kind, notes,
deferred = list(), repetitions = 1L) {
write_expected("hypothesis", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = deferred,
generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Additional configuration coverage:", id), case_kind = kind,
dataset = if (is.null(dataset)) NULL else list(
path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = repetitions,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R 4.6.1 stats/multcomp",
citation = "R stats::pnorm, pt, qt, binom.test, power.t.test; multcomp::glht and mvtnorm documentation.",
generator = list(path = "generators/r/hypothesis_completion.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Data-derived values use the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
# Alternative-axis values for the basic known-sigma Z method.
z <- (mean(d$x) - 6) / (2 / sqrt(nrow(d)))
for (alternative in c("less", "greater")) {
p <- if (alternative == "less") stats::pnorm(z) else stats::pnorm(z, lower.tail = FALSE)
emit(paste0("STAT-HYP-NOMINAL-BASIC-", toupper(alternative), "-001"),
"tabelz.executor.additional_basic_tests", "additionalBasicTestsMenuItem", "HYP_CORE",
list(method = "known_sigma_z", response_column = "x", known_sigma = 2,
null_mean = 6, alternative = alternative, alpha = 0.1,
confidence_level = 0.9, max_rows = 100L),
list("/statistic" = tol(z, "known-sigma z"),
"/p_value" = prob(p, paste(alternative, "normal tail"))),
"nominal", list("Directional known-sigma normal tail; confidence level equals 1-alpha."))
}
emit("STAT-HYP-NOMINAL-BOOT-MEAN-001", "tabelz.executor.bootstrap_estimates",
"bootstrapMenuItem", "HYP_CORE",
list(column = "x", statistic = "mean", resamples = 100L, seed = 42L),
list("/estimate" = tol(mean(d$x), "mean point statistic")), "nominal",
list("Mean is the default statistic; intervals remain in V27."))
# Default numeric runs inference from ordered signs about the observed median.
reference <- stats::median(d$x)
sequence <- d$x > reference
sequence <- sequence[d$x != reference]
runs <- 1L + sum(sequence[-1] != sequence[-length(sequence)])
n1 <- sum(sequence)
n2 <- length(sequence) - n1
expected <- 1 + 2 * n1 * n2 / length(sequence)
variance <- 2 * n1 * n2 * (2 * n1 * n2 - length(sequence)) /
(length(sequence)^2 * (length(sequence) - 1))
p_runs <- 2 * stats::pnorm(-abs((runs - expected) / sqrt(variance)))
emit("STAT-HYP-NOMINAL-RANK-RUNS-001", "tabelz.executor.blocked_rank_tests",
"blockedRankTestsMenuItem", "HYP_CORE",
list(method = "runs", response_column = "x", reference_mode = "median",
inference = "asymptotic", alternative = "two-sided", max_exact_n = 20L),
list("/runs" = exact(as.integer(runs), "Observed runs in row order."),
"/p_value" = prob(p_runs, "no-continuity-correction normal tail")),
"nominal", list("Observed median, asymptotic normal runs test without continuity correction."))
# Explicit non-default numerical bounds for the two TOST entry points.
a <- d$x[d$g == "A"]
bb <- d$x[d$g == "B"]
effect <- mean(a) - mean(bb)
va <- var(a) / length(a)
vb <- var(bb) / length(bb)
se <- sqrt(va + vb)
df <- (va + vb)^2 / (va^2 / (length(a) - 1) + vb^2 / (length(bb) - 1))
emit("STAT-HYP-NUMERIC-EQUIV-001", "tabelz.executor.equivalence", "equivalenceMenuItem",
"HYP_CORE",
list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = -3, upper_bound = 3, max_rows = 100L, max_cells = 1000L,
alpha = 0.1),
list("/difference_a_minus_b" = tol(effect, "A-B mean"),
"/p_lower" = prob(stats::pt((effect + 3) / se, df, lower.tail = FALSE), "Welch lower TOST")),
"axis_coverage", list("Non-default alpha and bounded source/cell limits do not alter Welch estimators."))
delta <- d$x - 6
one_effect <- mean(delta)
one_se <- sd(delta) / sqrt(length(delta))
emit("STAT-HYP-NUMERIC-EQUIVEXT-001", "tabelz.executor.equivalence_extensions",
"equivalenceMenuItem", "HYP_CORE",
list(mode = "one_sample_difference", response_column = "x", target = 6,
lower_bound = -2, upper_bound = 2, max_rows = 100L, max_cells = 1000L,
alpha = 0.1),
list("/estimate" = tol(one_effect, "mean-target effect"),
"/p_lower" = prob(stats::pt((one_effect + 2) / one_se, length(delta) - 1,
lower.tail = FALSE), "one-sample lower TOST")),
"axis_coverage", list("Non-default alpha and bounded source/cell limits do not alter one-sample estimators."))
# Paired Monte Carlo draws are deferred to V27; the observed statistic is exact.
paired <- pairs$x - pairs$y
emit("STAT-HYP-AXIS-PERM-PAIRED-MC-001", "tabelz.executor.permutation_tests",
"permutationTestsMenuItem", "HYP_PAIRS",
list(mode = "paired", response_column = "x", second_response_column = "y",
inference = "monte_carlo", resamples = 100L, seed = 42L,
histogram_bins = 5L, max_rows = 100L, max_cells = 1000L),
list("/statistic" = tol(mean(paired), "paired mean difference")), "axis_coverage",
list("Mode and inference axes interact; random p-value belongs to V27."),
deferred = list(list(path = "/p_value", reason = "Seeded Monte Carlo p-value is V27 simulation evidence.")),
repetitions = 2L)
# Summary conditional two-rate test: no source frame, entered totals from CSV.
count_a <- sum(r$count[r$g == "A"])
count_b <- sum(r$count[r$g == "B"])
exposure_a <- sum(r$exposure[r$g == "A"])
exposure_b <- sum(r$exposure[r$g == "B"])
null_probability <- exposure_a / (exposure_a + exposure_b)
conditional <- stats::binom.test(count_a, count_a + count_b,
p = null_probability, alternative = "greater")
emit("STAT-HYP-AXIS-BASIC-SUMMARY-TWORATE-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem", NULL,
list(method = "two_poisson_rate", input_kind = "summary", summary_count = count_a,
summary_exposure = exposure_a, summary_count_2 = count_b,
summary_exposure_2 = exposure_b, null_ratio = 1, alternative = "greater"),
list("/p_value" = prob(conditional$p.value, "conditional binomial right tail"),
"/estimate" = tol((count_a / exposure_a) / (count_b / exposure_b), "A/B rate ratio")),
"axis_coverage", list("Summary two-rate mode is source independent; totals derived from the R CSV."))
# Dunnett is an explicit nominal family axis; seed and accuracy are recorded.
b$treatment <- factor(b$treatment, levels = c("A", "B", "C"))
fit <- stats::lm(x ~ treatment, data = b)
contrast <- multcomp::glht(fit, linfct = multcomp::mcp(treatment = "Dunnett"))
set.seed(42)
adjusted <- summary(contrast, test = multcomp::adjusted("single-step", abseps = 1e-12))
emit("STAT-HYP-NOMINAL-DUNNETT-001", "tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem", "HYP_BLOCKS",
list(response_column = "x", group_column = "treatment", family = "dunnett",
control_level = "A", seed = 42L, alpha = 0.1,
max_rows = 100L, max_groups = 3L),
list("/comparisons/0/estimate" = tol(as.numeric(adjusted$test$coefficients)[[1]], "B-A mean"),
"/comparisons/0/standard_error" = tol(as.numeric(adjusted$test$sigma)[[1]], "Dunnett pooled SE")),
"nominal", list("Dunnett family with explicit control A, integration seed 42, abseps=1e-12.",
"Randomized adjusted p-value deferred because its error below the 1e-8 tier is not independently established."),
deferred = list(list(path = "/comparisons/0/p_value", reason = "Randomized multivariate-t integration tier unproven.")))
# Non-default target power changes the required integer sample size.
target <- 0.85
required <- stats::power.t.test(power = target, delta = 0.5, sd = 1,
sig.level = 0.05, type = "one.sample",
alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-NUMERIC-POWER-TARGET-001",
"tabelz.analysis.hypothesis_tests:run_power_analysis", "hypothesisTestsMenuItem",
NULL,
list(analysis = "one_sample_t", solve_for = "sample_size", effect_size = 0.5,
target_power = target),
list("/value" = exact(as.integer(ceiling(required$n)), "Round R solved n upward.")),
"axis_coverage", list("Target power 0.85 is non-default; strict two-sided noncentral t solution."))
cat("hypothesis completion cases generated\n")
generators/r/hypothesis_hard.R
source("common.R", local = TRUE)
base <- read_dataset("HYP_CORE")
hard <- base
hard$x <- base$x + 1e9
hard$y <- base$y + 1e9
export_dataset(
hard, "HYP_HARD", "Constructed large-offset version of HYP_CORE",
"Constructed by hypothesis_hard.R; R stats help pages for t.test, pnorm, ptukey and power.t.test.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64", g = "String", h = "String", outcome = "String")
)
d <- read_dataset("HYP_HARD")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R large-offset calculation:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R large-offset tail:", label))
emit <- function(id, entry, menu, config, outputs, notes, dataset = "HYP_HARD", version = "1.0.0") {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = version,
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Large-offset numerical case:", id), case_kind = "hard_numeric",
dataset = if (is.null(dataset)) NULL else list(
path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R 4.6.1 stats",
citation = "R stats package help: t.test, pnorm, ptukey, power.t.test; analytic TOST and runs formulae.",
generator = list(path = "generators/r/hypothesis_hard.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "All data-based expected values use the exported large-offset CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
center <- 1e9 + 6
t <- stats::t.test(d$x, mu = center)
emit("STAT-HYP-HARD-TEST-001", "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
"hypothesisTestsMenuItem", list(test = "one_sample_t", response = "x", target = center),
list("/statistic" = tol(t$statistic, "one-sample t statistic"),
"/p_value" = prob(t$p.value, "Student t tail")),
list("Large common offset, small sample spread; null mean uses the same offset."))
a <- d$x[d$g == "A"]
b <- d$x[d$g == "B"]
delta <- mean(a) - mean(b)
va <- var(a) / length(a)
vb <- var(b) / length(b)
se <- sqrt(va + vb)
df <- (va + vb)^2 / (va^2 / (length(a) - 1) + vb^2 / (length(b) - 1))
emit("STAT-HYP-HARD-EQUIV-001", "tabelz.executor.equivalence", "equivalenceMenuItem",
list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = -3, upper_bound = 3),
list("/difference_a_minus_b" = tol(delta, "small A-B difference"),
"/standard_error" = tol(se, "Welch SE"),
"/p_lower" = prob(stats::pt((delta + 3) / se, df, lower.tail = FALSE), "lower TOST")),
list("Welch TOST on differences of means near one billion."))
one_effect <- mean(d$x - center)
one_se <- sd(d$x - center) / sqrt(nrow(d))
emit("STAT-HYP-HARD-EQUIVEXT-001", "tabelz.executor.equivalence_extensions",
"equivalenceMenuItem",
list(mode = "one_sample_difference", response_column = "x", target = center,
lower_bound = -2, upper_bound = 2),
list("/estimate" = tol(one_effect, "small mean-target effect"),
"/standard_error" = tol(one_se, "one-sample SE")),
list("Subtract the large target from each observed response before computing the t standard error."))
z <- (mean(d$x) - center) / (2 / sqrt(nrow(d)))
emit("STAT-HYP-HARD-BASIC-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem",
list(method = "known_sigma_z", response_column = "x", known_sigma = 2, null_mean = center),
list("/statistic" = tol(z, "known-sigma z"),
"/p_value" = prob(2 * stats::pnorm(-abs(z)), "normal tail")),
list("Known sigma 2 and large mean offset."))
assign <- utils::combn(seq_len(nrow(d)), length(a))
# Measure the pooled responses from their median before enumerating: raw sums
# of values near 1e9 lose the low-order bits that decide near-ties, so the
# uncentred enumeration miscounted the extreme assignments.
centred <- d$x - stats::median(d$x)
perm_delta <- mean(centred[d$g == "A"]) - mean(centred[d$g == "B"])
sum_a <- colSums(matrix(centred[assign], nrow = length(a)))
null <- sum_a / length(a) - (sum(centred) - sum_a) / length(b)
p_exact <- mean(abs(null) >= abs(perm_delta))
emit("STAT-HYP-HARD-PERM-001", "tabelz.executor.permutation_tests",
"permutationTestsMenuItem",
list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "exact"),
list("/statistic" = tol(perm_delta, "centred large-offset mean difference"),
"/p_value" = prob(p_exact, "all exact assignments, centred enumeration")),
list("All choose(16,8) assignments enumerated with no Monte Carlo; the pooled responses are measured from their median so the exact tail is decided on the low-order bits; two-sided tail uses the absolute mean difference with no tie tolerance."),
version = "2.0.0")
sequence <- d$x > stats::median(d$x)
n1 <- sum(sequence)
n2 <- length(sequence) - n1
runs <- 1L + sum(sequence[-1] != sequence[-length(sequence)])
expected_runs <- 1 + 2 * n1 * n2 / length(sequence)
runs_var <- 2 * n1 * n2 * (2 * n1 * n2 - length(sequence)) /
(length(sequence)^2 * (length(sequence) - 1))
runs_p <- 2 * stats::pnorm(-abs((runs - expected_runs) / sqrt(runs_var)))
emit("STAT-HYP-HARD-RANK-001", "tabelz.executor.blocked_rank_tests",
"blockedRankTestsMenuItem",
list(method = "runs", response_column = "x", inference = "asymptotic"),
list("/expected_runs" = tol(expected_runs, "runs null expectation"),
"/p_value" = prob(runs_p, "asymptotic no-correction runs tail")),
list("Numeric median reference, source row order, no continuity correction."))
q <- abs(delta) / sqrt(0.5 * (va + vb))
emit("STAT-HYP-HARD-COMP-001", "tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem",
list(response_column = "x", group_column = "g", family = "games_howell"),
list("/comparisons/0/estimate" = tol(delta, "A-B contrast"),
"/comparisons/0/p_value" = prob(stats::ptukey(q, nmeans = 2, df = df,
lower.tail = FALSE), "Games-Howell tail")),
list("Two-group Games-Howell studentized-range tail."))
emit("STAT-HYP-HARD-BOOT-001", "tabelz.executor.bootstrap_estimates",
"bootstrapMenuItem", list(column = "x", statistic = "mean", seed = 12L, resamples = 200L),
list("/estimate" = tol(mean(d$x), "point mean near one billion")),
list("Only bootstrap point statistic compared; interval belongs to V27."))
power <- stats::power.t.test(n = 30, delta = 1e-4, sd = 1, sig.level = 0.05,
type = "one.sample", alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-HARD-POWER-001", "tabelz.analysis.hypothesis_tests:run_power_analysis",
"hypothesisTestsMenuItem",
list(analysis = "one_sample_t", solve_for = "power", effect_size = 1e-4,
sample_size = 30L),
list("/value" = tol(power$power, "near-null noncentral-t power")),
list("Strict two-sided power includes both rejection tails for a tiny effect."), dataset = NULL)
cat("hypothesis hard-numeric cases generated\n")
generators/r/hypothesis_interactions.R
source("common.R", local = TRUE)
d <- read_dataset("HYP_CORE")
r <- read_dataset("HYP_RATES")
run <- generator_run(c("stats", "pwr", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R interaction reference:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R interaction tail:", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
emit <- function(id, entry, menu, dataset, config, outputs, notes) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Interacting axes:", id), case_kind = "axis_coverage",
dataset = if (is.null(dataset)) NULL else list(
path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R stats and pwr",
citation = "R stats::power.t.test, pchisq, ppois, pbinom; pwr::pwr.chisq.test, pwr.2p.test, pwr.anova.test documentation.",
generator = list(path = "generators/r/hypothesis_interactions.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Entered summary values, when used, were computed from an exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
basic <- "tabelz.executor.additional_basic_tests"
menu <- "additionalBasicTestsMenuItem"
# The other summary-mode method/input combinations: one variance and one rate.
n <- nrow(d)
sample_var <- stats::var(d$x)
chi <- (n - 1) * sample_var / 4
two_tail_chi <- min(1, 2 * min(stats::pchisq(chi, n - 1),
stats::pchisq(chi, n - 1, lower.tail = FALSE)))
emit("STAT-HYP-INTERACT-BASIC-SUMMARY-VARIANCE-001", basic, menu, NULL,
list(method = "one_variance_chi_square", input_kind = "summary",
summary_variance = sample_var, sample_size = n, null_variance = 4),
list("/statistic" = tol(chi, "summary variance chi-square"),
"/p_value" = prob(two_tail_chi, "two-sided variance chi-square tail")),
list("Summary variance and n come from the R CSV; no source table is passed."))
count <- sum(r$count)
exposure <- sum(r$exposure)
expected <- 0.15 * exposure
p_rate <- min(1, 2 * min(stats::ppois(count, expected),
stats::ppois(count - 1, expected, lower.tail = FALSE)))
emit("STAT-HYP-INTERACT-BASIC-SUMMARY-ONERATE-001", basic, menu, NULL,
list(method = "one_poisson_rate", input_kind = "summary",
summary_count = count, summary_exposure = exposure, null_rate = 0.15),
list("/estimate" = tol(count / exposure, "summary event rate"),
"/p_value" = prob(p_rate, "doubled minimum Poisson tail")),
list("Summary count/exposure totals are from exported HYP_RATES; no table source is claimed."))
# Alternatives interact with variance, one-rate and two-rate methods.
for (alternative in c("less", "greater")) {
variance_p <- if (alternative == "less") stats::pchisq(chi, n - 1) else
stats::pchisq(chi, n - 1, lower.tail = FALSE)
emit(paste0("STAT-HYP-INTERACT-BASIC-VARIANCE-", toupper(alternative), "-001"),
basic, menu, "HYP_CORE",
list(method = "one_variance_chi_square", response_column = "x",
null_variance = 4, alternative = alternative),
list("/statistic" = tol(chi, "variance chi-square"),
"/p_value" = prob(variance_p, paste(alternative, "chi-square tail"))),
list("One-sided variance chi-square tail."))
rate_p <- if (alternative == "less") stats::ppois(count, expected) else
stats::ppois(count - 1, expected, lower.tail = FALSE)
emit(paste0("STAT-HYP-INTERACT-BASIC-ONERATE-", toupper(alternative), "-001"),
basic, menu, "HYP_RATES",
list(method = "one_poisson_rate", count_column = "count",
exposure_column = "exposure", null_rate = 0.15,
alternative = alternative),
list("/estimate" = tol(count / exposure, "raw event rate"),
"/p_value" = prob(rate_p, paste(alternative, "Poisson tail"))),
list("One-sided exact Poisson count tail at null rate times exposure."))
}
count_a <- sum(r$count[r$g == "A"])
count_b <- sum(r$count[r$g == "B"])
exposure_a <- sum(r$exposure[r$g == "A"])
exposure_b <- sum(r$exposure[r$g == "B"])
conditional_probability <- exposure_a / (exposure_a + exposure_b)
for (alternative in c("less", "greater")) {
two_rate_p <- if (alternative == "less")
stats::pbinom(count_a, count_a + count_b, conditional_probability) else
stats::pbinom(count_a - 1, count_a + count_b, conditional_probability,
lower.tail = FALSE)
emit(paste0("STAT-HYP-INTERACT-BASIC-TWORATE-", toupper(alternative), "-001"),
basic, menu, "HYP_RATES",
list(method = "two_poisson_rate", count_column = "count",
exposure_column = "exposure", group_column = "g", group_1 = "A", group_2 = "B",
null_ratio = 1, alternative = alternative),
list("/estimate" = tol((count_a / exposure_a) / (count_b / exposure_b), "A/B rate ratio"),
"/p_value" = prob(two_rate_p, paste(alternative, "conditional binomial tail"))),
list("Condition on total event count; null group-A probability follows exposure ratio."))
}
# Solve the five remaining power-analysis methods for minimum sample size.
power_entry <- "tabelz.analysis.hypothesis_tests:run_power_analysis"
power_menu <- "hypothesisTestsMenuItem"
paired <- stats::power.t.test(power = 0.8, delta = 0.5, sd = 1,
sig.level = 0.05, type = "paired",
alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-INTERACT-POWER-PAIRED-001", power_entry, power_menu, NULL,
list(analysis = "paired_t", solve_for = "sample_size", effect_size = 0.5,
target_power = 0.8),
list("/value" = exact(ceiling(paired$n), "Round required pairs upward.")),
list("R noncentral t power for paired differences, strict two-sided."))
two_t <- stats::power.t.test(power = 0.8, delta = 0.5, sd = 1,
sig.level = 0.05, type = "two.sample",
alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-INTERACT-POWER-TWO-T-001", power_entry, power_menu, NULL,
list(analysis = "two_sample_t", solve_for = "sample_size", effect_size = 0.5,
target_power = 0.8, ratio = 1),
list("/value" = exact(2 * ceiling(two_t$n), "Required total across equal groups.")),
list("R two-sample noncentral t with equal group sizes; round each group upward."))
one_prop <- pwr::pwr.chisq.test(w = 0.5, df = 1, sig.level = 0.05, power = 0.8)
emit("STAT-HYP-INTERACT-POWER-ONE-PROP-001", power_entry, power_menu, NULL,
list(analysis = "one_proportion", solve_for = "sample_size", effect_size = 0.5,
target_power = 0.8),
list("/value" = exact(ceiling(one_prop$N), "Required one-proportion chi-square n.")),
list("Cohen h=0.5 enters the one-degree chi-square approximation as w=0.5."))
two_prop <- pwr::pwr.2p.test(h = 0.5, sig.level = 0.05, power = 0.8,
alternative = "two.sided")
emit("STAT-HYP-INTERACT-POWER-TWO-PROP-001", power_entry, power_menu, NULL,
list(analysis = "two_proportion", solve_for = "sample_size", effect_size = 0.5,
target_power = 0.8, ratio = 1),
list("/value" = exact(2 * ceiling(two_prop$n), "Required total across equal groups.")),
list("Cohen h=0.5; normal two-group approximation, round each group upward."))
anova <- pwr::pwr.anova.test(k = 3, f = 0.5, sig.level = 0.05, power = 0.8)
emit("STAT-HYP-INTERACT-POWER-ANOVA-001", power_entry, power_menu, NULL,
list(analysis = "one_way_anova", solve_for = "sample_size", effect_size = 0.5,
target_power = 0.8, groups = 3L),
list("/value" = exact(3 * ceiling(anova$n), "Three equal groups rounded upward.")),
list("R noncentral F ANOVA with three equal-size groups; total is a multiple of three."))
cat("hypothesis interaction cases generated\n")
generators/r/hypothesis_invalid.R
source("common.R", local = TRUE)
# Contract-invalid configurations need no numerical oracle; each expected
# rejection is derived from the public request conditions in manual chapter 07
# and the corresponding feature chapter. The dataset is still an exported CSV.
d <- read_dataset("HYP_CORE")
stopifnot(nrow(d) > 1L)
run <- generator_run(c("jsonlite", "stats"))
entries <- list(
list(id = "TEST", entry = "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
menu = "hypothesisTestsMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(test = "one_sample_t", response = "x", alpha = 0),
match = "Alpha must be greater", rule = "Alpha must be strictly positive."),
list(config = list(test = "unsupported", response = "x"),
match = "Choose a supported hypothesis test", rule = "Only listed hypothesis tests are accepted."))),
list(id = "POWER", entry = "tabelz.analysis.hypothesis_tests:run_power_analysis",
menu = "hypothesisTestsMenuItem", dataset = NULL,
variants = list(
list(config = list(analysis = "unsupported"),
match = "Choose a supported power calculation", rule = "Planning analysis must be listed."),
list(config = list(analysis = "one_sample_t", effect_size = -1),
match = "Effect size must be a positive", rule = "Standardized effect size is positive."))),
list(id = "EQUIV", entry = "tabelz.executor.equivalence",
menu = "equivalenceMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(response_column = "x", group_column = "g", group_a = "A",
group_b = "B", lower_bound = 2, upper_bound = -2),
match = "lower equivalence bound", rule = "Lower bound must precede upper bound."),
list(config = list(response_column = "x", group_column = "g", group_a = "A",
group_b = "A", lower_bound = -2, upper_bound = 2),
match = "Group A and group B must be different", rule = "Compared groups must differ."))),
list(id = "EQUIVEXT", entry = "tabelz.executor.equivalence_extensions",
menu = "equivalenceMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(mode = "one_sample_difference", response_column = "x", target = 6,
lower_bound = 2, upper_bound = -2),
match = "lower equivalence bound", rule = "Lower bound must precede upper bound."),
list(config = list(mode = "paired_difference", lower_bound = -2, upper_bound = 2),
match = "Paired equivalence requires", rule = "Paired mode needs both columns."))),
list(id = "BASIC", entry = "tabelz.executor.additional_basic_tests",
menu = "additionalBasicTestsMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(method = "known_sigma_z", response_column = "x",
known_sigma = 0, null_mean = 6),
match = "Known sigma", rule = "Known sigma must be positive."),
list(config = list(method = "known_sigma_z", response_column = "x",
known_sigma = 2, null_mean = 6, alpha = 0),
match = "alpha", rule = "Alpha must be strictly positive."))),
list(id = "PERM", entry = "tabelz.executor.permutation_tests",
menu = "permutationTestsMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "A"),
match = "group labels must differ", rule = "Independent labels must differ."),
list(config = list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "unsupported"),
match = "inference", rule = "Inference is exact or Monte Carlo."))),
list(id = "RANK", entry = "tabelz.executor.blocked_rank_tests",
menu = "blockedRankTestsMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(method = "friedman", response_column = "x"),
match = "Friedman requires", rule = "Friedman needs treatment and block columns."),
list(config = list(method = "friedman", response_column = "x", treatment_column = "g",
block_column = "h", alternative = "less"),
match = "Friedman inference is two-sided", rule = "Friedman is two-sided."))),
list(id = "COMP", entry = "tabelz.executor.additional_comparisons",
menu = "additionalComparisonsMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(response_column = "x", group_column = "g", family = "dunnett"),
match = "Dunnett comparisons require control_level", rule = "Dunnett needs a control."),
list(config = list(response_column = "x", group_column = "x", family = "games_howell"),
match = "Response and group columns must be distinct", rule = "Response and group roles differ."))),
list(id = "BOOT", entry = "tabelz.executor.bootstrap_estimates",
menu = "bootstrapMenuItem", dataset = "HYP_CORE",
variants = list(
list(config = list(column = ""),
match = "non-empty", rule = "Analyzed column must have a name."),
list(config = list(column = "x", confidence_level = 1),
match = "confidence_level", rule = "Confidence level must be below one.")))
)
for (entry in entries) {
for (index in seq_along(entry$variants)) {
variant <- entry$variants[[index]]
id <- sprintf("STAT-HYP-INVALID-%s-%03d", entry$id, index)
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id,
outputs = stats::setNames(list(), character()),
expected_error = list(error_class = "ValueError", message_contains = variant$match),
deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = entry$entry, menu_items = list(entry$menu),
title = paste("Invalid request:", variant$rule), case_kind = "invalid_input",
dataset = if (is.null(entry$dataset)) NULL else list(
path = paste0("reference_data/datasets/", entry$dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = variant$config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "analytic", source = "Tabelz user manual: hypothesis and feature setup rules",
citation = "docs/manual/chapters/07-models-and-tests.tex and feature chapters 16, 31-34, 62",
generator = list(path = "generators/r/hypothesis_invalid.R", sha256 = ""),
independent = TRUE,
convention_notes = list(paste("Invalid condition:", variant$rule),
"No test statistic exists for an invalid request; rejection is the only valid result.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
}
cat("hypothesis invalid cases generated\n")
generators/r/hypothesis_meta_analysis.R
source("common.R", local = TRUE)
# Meta-analysis (Statistics > Meta-analysis): common-effect and random-effects pooling,
# heterogeneity, Hartung-Knapp inference, the prediction interval and the small-study tests,
# all written in base R from the formulas of docs/manual/chapters/78-meta-analysis.tex.
# Every reference is computed from the re-read exported CSV.
studies <- data.frame(
study = sprintf("S%02d", 1:8),
effect = c(0.31, 0.12, 0.55, -0.08, 0.42, 0.27, 0.68, 0.19),
se = c(0.11, 0.17, 0.14, 0.23, 0.09, 0.19, 0.21, 0.13),
n1 = c(24L, 18L, 31L, 15L, 40L, 22L, 17L, 28L),
mean1 = c(12.4, 11.8, 14.4, 10.3, 12.7, 12.2, 14.6, 12.0),
sd1 = c(2.1, 2.6, 1.9, 2.8, 2.2, 2.4, 2.0, 2.3),
n2 = c(25L, 20L, 29L, 16L, 38L, 21L, 19L, 27L),
mean2 = c(11.6, 11.5, 12.0, 11.1, 11.9, 11.5, 12.2, 11.6),
sd2 = c(2.0, 2.4, 2.2, 2.5, 2.1, 2.3, 2.6, 2.2),
events1 = c(5L, 3L, 9L, 0L, 12L, 4L, 6L, 7L),
total1 = c(24L, 18L, 31L, 15L, 40L, 22L, 17L, 28L),
events2 = c(14L, 4L, 16L, 6L, 9L, 13L, 9L, 16L),
total2 = c(25L, 20L, 29L, 16L, 38L, 21L, 19L, 27L),
stringsAsFactors = FALSE
)
export_dataset(
studies, "HYP_META_STUDIES", "Constructed by hypothesis_meta_analysis.R",
"Base R arithmetic on the inverse-variance formulas of the user manual (chapter 78).",
"Constructed test data; no third-party dataset license.",
c(study = "String", effect = "Float64", se = "Float64", n1 = "Int64", mean1 = "Float64",
sd1 = "Float64", n2 = "Int64", mean2 = "Float64", sd2 = "Float64", events1 = "Int64",
total1 = "Int64", events2 = "Int64", total2 = "Int64")
)
d <- read_dataset("HYP_META_STUDIES")
run <- generator_run(c("stats", "jsonlite"))
source_ref <- "Base R (stats) inverse-variance pooling written from the manual formulas"
citation <- paste(
"DerSimonian and Laird (1986) Controlled Clinical Trials 7:177-188;",
"Hartung and Knapp (2001) Statistics in Medicine 20:3875-3889;",
"Viechtbauer (2007) Statistics in Medicine 26:37-52 (Q-profile interval);",
"Egger et al. (1997) BMJ 315:629-634; Begg and Mazumdar (1994) Biometrics 50:1088-1101;",
"R stats: lm, cor.test (exact Kendall), uniroot, pt, pnorm, pchisq."
)
tol <- function(value, label, tier = "1e-9") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste("Base R", label, "from the re-read HYP_META_STUDIES CSV."))
}
prob <- function(value, label, tier = "1e-8") {
list(comparison = "tail_probability", value = dec(unname(value)), relative_tolerance = tier,
rationale = paste("Base R", label, "from the re-read HYP_META_STUDIES CSV."))
}
exact <- function(value, label) {
list(comparison = "exact", value = as.integer(value), rationale = label)
}
# --- model pieces -----------------------------------------------------------------------
common_pool <- function(y, v) {
w <- 1 / v
mu <- sum(w * y) / sum(w)
q <- sum(w * (y - mu)^2)
c0 <- sum(w) - sum(w^2) / sum(w) # tr(P_0)
list(mu = mu, se = sqrt(1 / sum(w)), q = q, df = length(y) - 1, c0 = c0,
s2 = (length(y) - 1) / c0, weights = 100 * w / sum(w))
}
gen_q <- function(y, v, t) {
w <- 1 / (v + t)
mu <- sum(w * y) / sum(w)
sum(w * (y - mu)^2)
}
tau2_dl <- function(y, v) {
cp <- common_pool(y, v)
max(0, (cp$q - cp$df) / cp$c0)
}
tau2_pm <- function(y, v) {
k <- length(y)
if (gen_q(y, v, 0) <= k - 1) return(0)
stats::uniroot(function(t) gen_q(y, v, t) - (k - 1), c(0, 100 * max(v) + stats::var(y) * 100),
tol = 1e-14, maxiter = 10000)$root
}
reml_score <- function(y, v, t) {
w <- 1 / (v + t)
mu <- sum(w * y) / sum(w)
-0.5 * (sum(w) - sum(w^2) / sum(w)) + 0.5 * sum(w^2 * (y - mu)^2)
}
reml_ll <- function(y, v, t) {
w <- 1 / (v + t)
mu <- sum(w * y) / sum(w)
-0.5 * (sum(log(v + t)) + log(sum(w)) + sum(w * (y - mu)^2))
}
tau2_reml <- function(y, v) {
# The restricted score has one sign change on a fine log grid (asserted), so the maximum is
# the single root; it is compared with the boundary 0 by the restricted likelihood.
grid <- exp(seq(log(1e-8 * min(v)), log(1e3 * max(v)), length.out = 4000))
scores <- vapply(grid, function(t) reml_score(y, v, t), 0)
changes <- which(scores[-length(scores)] > 0 & scores[-1] <= 0)
stopifnot(length(changes) == 1L)
root <- stats::uniroot(function(t) reml_score(y, v, t), c(grid[changes], grid[changes + 1]),
tol = 1e-15, maxiter = 10000)$root
if (reml_ll(y, v, root) > reml_ll(y, v, 0)) root else 0
}
qprofile <- function(y, v, level) {
df <- length(y) - 1
limit <- function(p) {
target <- stats::qchisq(p, df)
if (gen_q(y, v, 0) <= target) return(0)
stats::uniroot(function(t) gen_q(y, v, t) - target, c(0, 1e3 * max(v) + 100 * stats::var(y)),
tol = 1e-14, maxiter = 10000)$root
}
c(lower = limit((1 + level) / 2), upper = limit((1 - level) / 2))
}
random_pool <- function(y, v, tau2, inference, level) {
k <- length(y)
w <- 1 / (v + tau2)
mu <- sum(w * y) / sum(w)
var_mu <- 1 / sum(w)
if (inference == "normal") {
se <- sqrt(var_mu); crit <- stats::qnorm((1 + level) / 2)
stat <- mu / se; p <- 2 * stats::pnorm(-abs(stat)); scale <- NA
} else {
raw <- sum(w * (y - mu)^2) / (k - 1)
scale <- if (inference == "modified_hk") max(1, raw) else raw
se <- sqrt(var_mu * scale); crit <- stats::qt((1 + level) / 2, k - 1)
stat <- mu / se; p <- 2 * stats::pt(-abs(stat), k - 1)
}
pred <- stats::qt((1 + level) / 2, k - 2) * sqrt(tau2 + se^2)
cp <- common_pool(y, v)
list(mu = mu, se = se, stat = stat, p = p, lower = mu - crit * se, upper = mu + crit * se,
pred_lower = mu - pred, pred_upper = mu + pred, weights = 100 * w / sum(w),
i2 = 100 * tau2 / (tau2 + cp$s2), h2 = (tau2 + cp$s2) / cp$s2, hk_scale = scale)
}
egger <- function(y, v) {
se <- sqrt(v)
fit <- summary(stats::lm(I(y / se) ~ I(1 / se)))$coefficients
list(t = fit["(Intercept)", "t value"], p = fit["(Intercept)", "Pr(>|t|)"])
}
begg <- function(y, v) {
w <- 1 / v
mu <- sum(w * y) / sum(w)
z <- (y - mu) / sqrt(v - 1 / sum(w))
stopifnot(!anyDuplicated(z), !anyDuplicated(v))
test <- stats::cor.test(z, v, method = "kendall", exact = TRUE)
list(tau = unname(test$estimate), p = test$p.value)
}
emit <- function(id, title, kind, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("hypothesis", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = "tabelz.analysis.meta_analysis:compute_meta_analysis",
menu_items = list("metaAnalysisMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/HYP_META_STUDIES.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/hypothesis_meta_analysis.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes, "Expected computed from the exported CSV.",
"docs/manual/chapters/78-meta-analysis.tex:25-31: common effect w=1/v; random effects w=1/(v+tau^2); REML default, DL/PM/SJ alternatives.",
"docs/manual/chapters/78-meta-analysis.tex:33-37: modified Hartung-Knapp scales 1/sum(w) by q=max(1, sum w (y-mu)^2/(k-1)) on t(k-1); ordinary HK removes the floor.",
"docs/manual/chapters/78-meta-analysis.tex:70-79: Q, tau^2 with Q-profile interval, I^2 and H^2 relative to the typical within-study variance; prediction interval mu +- t(k-2) sqrt(tau^2 + Var(mu))."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-META_ANALYSIS-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
)
write_case("hypothesis", id, case)
}
common_outputs <- function(cp, level, index = 0) {
z <- stats::qnorm((1 + level) / 2)
base <- paste0("/pooled/", index)
out <- list()
out[[paste0(base, "/estimate")]] <- tol(cp$mu, "common-effect estimate")
out[[paste0(base, "/standard_error")]] <- tol(cp$se, "common-effect SE")
out[[paste0(base, "/statistic")]] <- tol(cp$mu / cp$se, "common-effect z")
out[[paste0(base, "/p_value")]] <- prob(2 * stats::pnorm(-abs(cp$mu / cp$se)), "common-effect normal p")
out[[paste0(base, "/lower")]] <- tol(cp$mu - z * cp$se, "common-effect CI lower")
out[[paste0(base, "/upper")]] <- tol(cp$mu + z * cp$se, "common-effect CI upper")
out[["/heterogeneity/q"]] <- tol(cp$q, "Cochran Q")
out[["/heterogeneity/df"]] <- exact(cp$df, "k - 1")
out[["/heterogeneity/p_value"]] <- prob(stats::pchisq(cp$q, cp$df, lower.tail = FALSE), "Q chi-square p")
out
}
random_outputs <- function(rp, tau2, prof, cp, level) {
out <- list(
"/pooled/1/estimate" = tol(rp$mu, "random-effects estimate", "1e-8"),
"/pooled/1/standard_error" = tol(rp$se, "random-effects SE", "1e-8"),
"/pooled/1/statistic" = tol(rp$stat, "random-effects statistic", "1e-8"),
"/pooled/1/p_value" = prob(rp$p, "random-effects p", "1e-7"),
"/pooled/1/lower" = tol(rp$lower, "random-effects CI lower", "1e-8"),
"/pooled/1/upper" = tol(rp$upper, "random-effects CI upper", "1e-8"),
"/pooled/1/prediction_lower" = tol(rp$pred_lower, "prediction interval lower", "1e-8"),
"/pooled/1/prediction_upper" = tol(rp$pred_upper, "prediction interval upper", "1e-8"),
"/heterogeneity/tau2" = tol(tau2, "tau^2", "1e-8"),
"/heterogeneity/i2" = tol(rp$i2, "I^2 = 100 tau^2/(tau^2 + s^2)", "1e-8"),
"/heterogeneity/h2" = tol(rp$h2, "H^2 = (tau^2 + s^2)/s^2", "1e-8"),
"/heterogeneity/tau2_upper" = tol(prof[["upper"]], "Q-profile upper tau^2 limit (uniroot)", "1e-8"),
"/studies/0/weight_percent" = tol(rp$weights[1], "random-effects weight of study 1 (%)", "1e-8")
)
if (prof[["lower"]] > 0) {
out[["/heterogeneity/tau2_lower"]] <- tol(prof[["lower"]], "Q-profile lower tau^2 limit (uniroot)", "1e-8")
}
if (!is.na(rp$hk_scale)) out[["/heterogeneity/hk_scale"]] <- tol(rp$hk_scale, "Hartung-Knapp scale", "1e-8")
out
}
# --- case 001: common effect, generic layout --------------------------------------------
y <- d$effect; v <- d$se^2; k <- length(y)
cp <- common_pool(y, v)
eg <- egger(y, v); bg <- begg(y, v)
outputs <- common_outputs(cp, 0.95)
outputs[["/k"]] <- exact(k, "eight studies")
outputs[["/heterogeneity/i2"]] <- tol(max(0, 100 * (cp$q - cp$df) / cp$q), "common-effect I^2 from Q")
outputs[["/heterogeneity/h2"]] <- tol(cp$q / cp$df, "common-effect H^2 = Q/df")
outputs[["/studies/0/weight_percent"]] <- tol(cp$weights[1], "inverse-variance weight of study 1 (%)")
outputs[["/studies/4/weight_percent"]] <- tol(cp$weights[5], "inverse-variance weight of study 5 (%)")
outputs[["/studies/0/lower"]] <- tol(y[1] - stats::qnorm(0.975) * sqrt(v[1]), "study 1 normal CI lower")
outputs[["/bias/0/statistic"]] <- tol(eg$t, "Egger intercept t (lm of y/SE on 1/SE)")
outputs[["/bias/0/p_value"]] <- prob(eg$p, "Egger intercept p on k - 2 df")
outputs[["/bias/1/statistic"]] <- tol(bg$tau, "Begg Kendall tau (cor.test)")
outputs[["/bias/1/p_value"]] <- prob(bg$p, "Begg exact Kendall p (cor.test exact=TRUE)")
outputs[["/cumulative/7/estimate"]] <- tol(cp$mu, "cumulative estimate after all studies = pooled")
loo <- common_pool(y[-1], v[-1])
outputs[["/leave_one_out/0/estimate"]] <- tol(loo$mu, "common-effect estimate without study 1")
emit("STAT-HYP-META-001", "Common-effect meta-analysis of generic effects", "nominal",
list(data = "generic", study = "study", effect = "effect", standard_error = "se",
model = "fixed", measure = "hedges_g"),
outputs,
list("Common effect: mu = sum(w y)/sum(w), w = 1/se^2, normal z inference; Q = sum w (y - mu)^2 on k - 1 df; I^2 = max(0, 100 (Q - df)/Q), H^2 = Q/df.",
"Egger: lm(y/se ~ 1/se), intercept t on k - 2 df. Begg: Kendall tau between (y - mu)/sqrt(v - 1/sum w) and v, exact p (no ties)."))
# --- case 002: random effects, DerSimonian-Laird, normal inference ----------------------
t_dl <- tau2_dl(y, v)
stopifnot(t_dl > 0)
rp <- random_pool(y, v, t_dl, "normal", 0.95)
prof <- qprofile(y, v, 0.95)
outputs <- c(common_outputs(cp, 0.95), random_outputs(rp, t_dl, prof, cp, 0.95))
loo <- random_pool(y[-1], v[-1], tau2_dl(y[-1], v[-1]), "normal", 0.95)
outputs[["/leave_one_out/0/estimate"]] <- tol(loo$mu, "DL random-effects estimate without study 1", "1e-8")
outputs[["/leave_one_out/0/tau2"]] <- tol(tau2_dl(y[-1], v[-1]), "DL tau^2 without study 1", "1e-8")
cum3 <- random_pool(y[1:3], v[1:3], tau2_dl(y[1:3], v[1:3]), "normal", 0.95)
outputs[["/cumulative/2/estimate"]] <- tol(cum3$mu, "DL cumulative estimate after three studies", "1e-8")
emit("STAT-HYP-META-002", "Random effects, DerSimonian-Laird, normal inference", "axis_coverage",
list(data = "generic", study = "study", effect = "effect", standard_error = "se",
model = "random", measure = "hedges_g", tau2 = "dl", inference = "normal"),
outputs,
list("DL tau^2 = max(0, (Q - (k - 1)) / (sum w - sum w^2 / sum w)); random-effects weights 1/(v + tau^2); normal interval.",
"Typical within-study variance s^2 = (k - 1)/(sum w - sum w^2/sum w); I^2 = 100 tau^2/(tau^2 + s^2); H^2 = (tau^2 + s^2)/s^2.",
"Q-profile limits: tau^2 where the generalized Q equals chi-square(k - 1) quantiles 0.975 (lower) and 0.025 (upper), by uniroot to 1e-14; 0 when Q(0) is below the quantile.",
"Prediction interval: mu +- t(k - 2, 0.975) sqrt(tau^2 + 1/sum w*); relative tolerance 1e-8 because the Q-profile limits come from a root search."))
# --- case 003: continuous data, Hedges' g, DL, modified Hartung-Knapp, 90% --------------
m <- d$n1 + d$n2 - 2
sp <- sqrt(((d$n1 - 1) * d$sd1^2 + (d$n2 - 1) * d$sd2^2) / m)
dd <- (d$mean1 - d$mean2) / sp
J <- exp(lgamma(m / 2) - 0.5 * log(m / 2) - lgamma((m - 1) / 2))
g <- J * dd
vg <- 1 / d$n1 + 1 / d$n2 + g^2 / (2 * (d$n1 + d$n2))
cpg <- common_pool(g, vg)
t_g <- tau2_dl(g, vg)
rpg <- random_pool(g, vg, t_g, "modified_hk", 0.90)
profg <- qprofile(g, vg, 0.90)
outputs <- c(common_outputs(cpg, 0.90), random_outputs(rpg, t_g, profg, cpg, 0.90))
outputs[["/studies/0/effect"]] <- tol(g[1], "Hedges' g of study 1 (exact J)")
outputs[["/studies/0/standard_error"]] <- tol(sqrt(vg[1]), "SE of Hedges' g of study 1")
outputs[["/studies/3/effect"]] <- tol(g[4], "Hedges' g of study 4")
outputs[["/pooled/1/distribution"]] <- list(comparison = "exact", value = "t(7)",
rationale = "Hartung-Knapp uses t on k - 1 = 7 df")
emit("STAT-HYP-META-003", "Hedges' g from two arms, DL, modified Hartung-Knapp, 90%", "axis_coverage",
list(data = "continuous", study = "study", n1 = "n1", mean1 = "mean1", sd1 = "sd1",
n2 = "n2", mean2 = "mean2", sd2 = "sd2", measure = "hedges_g", model = "random",
tau2 = "dl", inference = "modified_hk", confidence_level = 0.90),
outputs,
list("Hedges' g = J d with d = (m1 - m2)/s_pooled, J = Gamma(m/2)/(sqrt(m/2) Gamma((m - 1)/2)), m = n1 + n2 - 2; variance 1/n1 + 1/n2 + g^2/(2 (n1 + n2)) (metafor escalc SMD convention).",
"Modified Hartung-Knapp: Var(mu) = max(1, sum w (y - mu)^2/(k - 1)) / sum w with t(k - 1); the same scaled variance enters the prediction interval.",
"tau^2 = 0 would make the modified HK scale floor at 1; this data set has tau^2 > 0 (asserted in the generator)."))
stopifnot(t_g > 0)
# --- case 004: binary data, log odds ratio with a zero-cell correction, REML, HK ---------
a <- d$events1; b <- d$total1 - d$events1; cc <- d$events2; dd2 <- d$total2 - d$events2
zero <- (a == 0 | b == 0 | cc == 0 | dd2 == 0)
stopifnot(sum(zero) == 1L, !any((a == 0 & cc == 0) | (b == 0 & dd2 == 0)))
a[zero] <- a[zero] + 0.5; b[zero] <- b[zero] + 0.5; cc[zero] <- cc[zero] + 0.5; dd2[zero] <- dd2[zero] + 0.5
lor <- log(a * dd2 / (b * cc))
vlor <- 1 / a + 1 / b + 1 / cc + 1 / dd2
cpl <- common_pool(lor, vlor)
t_reml <- tau2_reml(lor, vlor)
stopifnot(t_reml > 0)
rpl <- random_pool(lor, vlor, t_reml, "hk", 0.95)
profl <- qprofile(lor, vlor, 0.95)
outputs <- c(common_outputs(cpl, 0.95), random_outputs(rpl, t_reml, profl, cpl, 0.95))
outputs[[paste0("/studies/", which(zero) - 1, "/effect")]] <- tol(lor[zero], "log OR of the zero-cell study after adding 0.5 to each cell")
outputs[[paste0("/studies/", which(zero) - 1, "/standard_error")]] <- tol(sqrt(vlor[zero]), "SE of the corrected log OR")
outputs[["/studies/0/effect"]] <- tol(lor[1], "log OR of study 1")
emit("STAT-HYP-META-004", "Log odds ratio from events, REML, ordinary Hartung-Knapp", "axis_coverage",
list(data = "binary", study = "study", events1 = "events1", total1 = "total1",
events2 = "events2", total2 = "total2", measure = "log_or", correction = 0.5,
model = "random", tau2 = "reml", inference = "hk"),
outputs,
list("Log OR = log(a d/(b c)) with variance 1/a + 1/b + 1/c + 1/d; the one study with a zero cell gets 0.5 added to all four cells (manual chapter 78 lines 17-20).",
"REML tau^2: the root of the restricted score -tr(P)/2 + y'PPy/2 (one sign change on a 4000-point log grid, asserted), found by uniroot to 1e-15 and compared with the boundary 0 by the restricted likelihood.",
"Ordinary Hartung-Knapp: Var(mu) = (sum w (y - mu)^2/(k - 1)) / sum w without the floor at 1, t(k - 1).",
"Relative tolerance 1e-8: tau^2 is an iterative root in both implementations."))
# --- case 005: generic, Paule-Mandel, normal inference, 99% ------------------------------
t_pm <- tau2_pm(y, v)
stopifnot(t_pm > 0)
rpp <- random_pool(y, v, t_pm, "normal", 0.99)
profp <- qprofile(y, v, 0.99)
outputs <- c(common_outputs(cp, 0.99), random_outputs(rpp, t_pm, profp, cp, 0.99))
emit("STAT-HYP-META-005", "Paule-Mandel tau^2 with normal inference, 99%", "axis_coverage",
list(data = "generic", study = "study", effect = "effect", standard_error = "se",
model = "random", measure = "hedges_g", tau2 = "pm", inference = "normal",
confidence_level = 0.99),
outputs,
list("Paule-Mandel tau^2: the root of the generalized Q(tau^2) = sum (y - mu*)^2/(v + tau^2) = k - 1, by uniroot to 1e-14 (0 when Q(0) <= k - 1).",
"Relative tolerance 1e-8: tau^2 and the Q-profile limits are iterative roots in both implementations."))
cat("meta-analysis cases generated\n")
generators/r/hypothesis_methods.R
source("common.R", local = TRUE)
d <- read_dataset("HYP_CORE")
run <- generator_run(c("stats", "car", "jsonlite"))
citation <- paste(
"R Core Team, stats help: oneway.test, chisq.test, binom.test, wilcox.test,",
"aov and kruskal.test; car::Anova and car::leveneTest help."
)
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R reference:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R reference:", label))
emit <- function(id, title, config, outputs, notes) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
menu_items = list("hypothesisTestsMenuItem"), title = title,
case_kind = "axis_coverage",
dataset = list(path = "reference_data/datasets/HYP_CORE.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation",
source = "R 4.6.1 stats and car",
citation = citation,
generator = list(path = "generators/r/hypothesis_methods.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Every statistic was computed after re-reading the exported HYP_CORE CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
welch_aov <- stats::oneway.test(x ~ g, data = d, var.equal = FALSE)
emit("STAT-HYP-ONE-ANOVA-WELCH-001", "Welch one-way ANOVA",
list(test = "one_way_anova", response = "x", group = "g", method = "welch", posthoc = FALSE),
list("/statistic" = tol(welch_aov$statistic, "Welch ANOVA F"),
"/p_value" = prob(welch_aov$p.value, "Welch ANOVA F tail"),
"/df2" = tol(welch_aov$parameter[[2]], "Welch denominator df")),
list("R oneway.test uses unequal-variance Welch correction."))
two_way <- car::Anova(stats::lm(x ~ g * h, data = d), type = 2)
interaction <- two_way["g:h", ]
emit("STAT-HYP-TWO-ANOVA-001", "Type II two-way interaction ANOVA",
list(test = "two_way_anova", response = "x", group = "g", second_group = "h",
posthoc = FALSE),
list("/statistic" = tol(interaction[["F value"]], "type II interaction F"),
"/p_value" = prob(interaction[["Pr(>F)"]], "type II interaction F tail")),
list("Type II sums of squares, g:h interaction is the primary statistic."))
observed <- as.integer(table(d$outcome))
gof <- stats::chisq.test(observed, p = rep(0.5, 2))
emit("STAT-HYP-CHI-GOF-001", "Two-category chi-square goodness of fit",
list(test = "chi_square_gof", response = "outcome"),
list("/statistic" = tol(gof$statistic, "Pearson GOF chi-square"),
"/p_value" = prob(gof$p.value, "GOF chi-square tail")),
list("Blank expected proportions means equal category probabilities."))
association <- stats::chisq.test(table(d$g, d$h), correct = FALSE)
emit("STAT-HYP-CHI-ASSOC-001", "Two-way categorical association",
list(test = "chi_square_association", group = "g", second_group = "h"),
list("/statistic" = tol(association$statistic, "Pearson association chi-square"),
"/p_value" = prob(association$p.value, "association chi-square tail")),
list("Yates continuity correction disabled, as the manual's Pearson test specifies."))
lev <- car::leveneTest(x ~ g, data = d, center = median)
emit("STAT-HYP-VARIANCE-LEVENE-001", "Brown-Forsythe median-centred variance test",
list(test = "equal_variance", response = "x", group = "g", method = "levene"),
list("/statistic" = tol(lev[["F value"]][[1]], "Brown-Forsythe F"),
"/p_value" = prob(lev[["Pr(>F)"]][[1]], "Brown-Forsythe F tail")),
list("Levene deviations centred on each group's median."))
paired_diff <- d$x - d$y
positive <- sum(paired_diff > 0)
nonzero <- sum(paired_diff != 0)
sign <- stats::binom.test(positive, nonzero, p = 0.5)
emit("STAT-HYP-SIGN-001", "Exact paired sign test",
list(test = "sign", response = "x", second_response = "y"),
list("/statistic" = tol(positive, "positive paired differences"),
"/p_value" = prob(sign$p.value, "exact two-sided binomial tail")),
list("Zero paired differences omitted; test positive among nonzero differences."))
# Statistician ruling, issue #12: W+ with the exact conditional distribution
# for m <= 50, ties by average ranks. R 4.6.1's exact test handles ties.
wilcox <- stats::wilcox.test(paired_diff, mu = 0, exact = TRUE)
stopifnot(sum(paired_diff != 0) <= 50L)
emit("STAT-HYP-WILCOXON-001", "Paired Wilcoxon signed ranks",
list(test = "wilcoxon", response = "x", second_response = "y"),
list("/statistic" = tol(wilcox$statistic, "W+, the positive signed-rank sum"),
"/p_value" = prob(wilcox$p.value, "exact conditional signed-rank distribution with tied ranks")),
list("Issue #12 ruling: W+ and exact conditional inference for m <= 50, including ties.",
"Until 2026-09-25 this case used R's explicit normal approximation (exact = FALSE, correct = TRUE)."))
grand_median <- stats::median(d$x)
below <- d$x <= grand_median
mood <- stats::chisq.test(table(d$g, below), correct = FALSE)
emit("STAT-HYP-MOOD-001", "Mood median with ties below",
list(test = "moods_median", response = "x", group = "g"),
list("/statistic" = tol(mood$statistic, "median contingency chi-square"),
"/p_value" = prob(mood$p.value, "median chi-square tail"),
"/estimate" = tol(grand_median, "grand median")),
list("Values equal to the pooled median count in the below cell; no Yates correction."))
# Explicit pooled z test (R prop.test correct=FALSE is related but reports chi-square).
g_a <- d$outcome[d$g == "A"]
g_b <- d$outcome[d$g == "B"]
p_a <- mean(g_a == "yes")
p_b <- mean(g_b == "yes")
pooled <- (sum(g_a == "yes") + sum(g_b == "yes")) / (length(g_a) + length(g_b))
z <- (p_a - p_b) / sqrt(pooled * (1 - pooled) * (1 / length(g_a) + 1 / length(g_b)))
emit("STAT-HYP-TWO-PROP-Z-001", "Pooled two-proportion Z",
list(test = "two_proportion", response = "outcome", group = "g",
success_value = "yes", method = "z"),
list("/statistic" = tol(z, "pooled two-proportion z"),
"/p_value" = prob(2 * stats::pnorm(-abs(z)), "uncorrected normal tail")),
list("Manual method z uses no continuity correction; pooled null SE shown analytically."))
cat("hypothesis method cases generated\n")
generators/r/hypothesis_minimum.R
source("common.R", local = TRUE)
tiny <- data.frame(x = c(1.0, 2.0), y = c(1.5, 2.5),
g = c("A", "B"), stringsAsFactors = FALSE)
export_dataset(
tiny, "HYP_TINY", "Constructed minimum-size request data",
"Tabelz manual chapters 07, 16, 31-34 and 62: minimum sample/design conditions.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64", g = "String")
)
d <- read_dataset("HYP_TINY")
stopifnot(nrow(d) == 2L)
run <- generator_run(c("jsonlite", "stats"))
entries <- list(
list(id = "TEST", entry = "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
menu = "hypothesisTestsMenuItem", config = list(test = "one_sample_t", response = "x"),
excluded = list(0L), error = "ValueError", match = "needs at least 2 complete observations",
rule = "One remaining response is below the one-sample t minimum."),
list(id = "POWER", entry = "tabelz.analysis.hypothesis_tests:run_power_analysis",
menu = "hypothesisTestsMenuItem", config = list(analysis = "one_sample_t",
solve_for = "power", sample_size = 1L),
excluded = list(), error = "ValueError", match = "Sample size must be at least 2",
rule = "Prospective t power needs at least two planned observations.", dataset = NULL),
list(id = "EQUIV", entry = "tabelz.executor.equivalence",
menu = "equivalenceMenuItem",
config = list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = -2, upper_bound = 2),
excluded = list(), error = "EquivalenceExecutionError",
match = "requires at least two retained finite observations",
rule = "One response in each group is below the Welch TOST minimum."),
list(id = "EQUIVEXT", entry = "tabelz.executor.equivalence_extensions",
menu = "equivalenceMenuItem",
config = list(mode = "one_sample_difference", response_column = "x", target = 0,
lower_bound = -2, upper_bound = 2),
excluded = list(0L), error = "EquivalenceExtensionsExecutionError",
match = "requires at least two finite retained observations",
rule = "One retained response is below the one-sample TOST minimum."),
list(id = "BASIC", entry = "tabelz.executor.additional_basic_tests",
menu = "additionalBasicTestsMenuItem",
config = list(method = "one_variance_chi_square", response_column = "x", null_variance = 1),
excluded = list(0L), error = "AdditionalBasicTestsExecutionError",
match = "Variance inference requires at least two finite observations",
rule = "One retained response is below the sample-variance minimum."),
list(id = "PERM", entry = "tabelz.executor.permutation_tests",
menu = "permutationTestsMenuItem",
config = list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "exact"),
excluded = list(), error = "PermutationTestsExecutionError",
match = "require at least 2 finite responses in each selected group",
rule = "One response per group is below the independent permutation minimum."),
list(id = "RANK", entry = "tabelz.executor.blocked_rank_tests",
menu = "blockedRankTestsMenuItem",
config = list(method = "runs", response_column = "x"),
excluded = list(0L), error = "BlockedRankTestsExecutionError",
match = "Runs test requires at least one observation in each category",
rule = "One retained response cannot populate both runs categories."),
list(id = "COMP", entry = "tabelz.executor.additional_comparisons",
menu = "additionalComparisonsMenuItem",
config = list(response_column = "x", group_column = "g", family = "games_howell"),
excluded = list(), error = "AdditionalComparisonsExecutionError",
match = "Every comparison group needs at least 2 observations",
rule = "One observation per group is below the comparison minimum."),
list(id = "BOOT", entry = "tabelz.executor.bootstrap_estimates",
menu = "bootstrapMenuItem", config = list(column = "x", resamples = 20L),
excluded = list(0L), error = "BootstrapExecutionError",
match = "Bootstrap intervals require at least 2 finite rows",
rule = "One retained response is below the bootstrap minimum.")
)
for (entry in entries) {
id <- paste0("STAT-HYP-MINIMUM-", entry$id, "-001")
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id,
outputs = stats::setNames(list(), character()),
expected_error = list(error_class = entry$error, message_contains = entry$match),
deferred_outputs = list(), generator_run = run))
dataset <- if (!is.null(entry$dataset) || entry$id != "POWER")
list(path = "reference_data/datasets/HYP_TINY.json", sha256 = "") else NULL
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = entry$entry, menu_items = list(entry$menu),
title = paste("Below minimum:", entry$rule), case_kind = "invalid_input",
dataset = dataset, excluded_rows = entry$excluded, configuration = entry$config,
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "analytic", source = "Tabelz manual sample/design requirements",
citation = "docs/manual/chapters/07-models-and-tests.tex; 16, 31-34 and 62 feature chapters.",
generator = list(path = "generators/r/hypothesis_minimum.R", sha256 = ""),
independent = TRUE,
convention_notes = list(entry$rule,
"No estimator is defined below the minimum; the request must fail explicitly.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
cat("hypothesis below-minimum cases generated\n")
generators/r/hypothesis_population.R
source("common.R", local = TRUE)
constructed <- read_dataset("HYP_CORE")
constructed$x[[2]] <- NA_real_
constructed$y[[4]] <- NA_real_
constructed$g[[6]] <- NA_character_
export_dataset(
constructed, "HYP_MISSING", "HYP_CORE with three deliberately missing cells",
"Constructed by hypothesis_population.R from the HYP_CORE CSV; R stats help pages for t.test, pnorm, ptukey.",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64", g = "String", h = "String", outcome = "String")
)
d <- read_dataset("HYP_MISSING")
included <- d[-1, , drop = FALSE] # Zero-based source row 0 is excluded.
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R filtered CSV:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R filtered CSV:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, entry, menu, config, outputs, notes, error = NULL) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Missing and excluded population:", id), case_kind = "missing_excluded",
dataset = list(path = "reference_data/datasets/HYP_MISSING.json", sha256 = ""),
excluded_rows = list(0L), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = if (is.null(error)) "independent_implementation" else "analytic",
source = "R 4.6.1 stats",
citation = "R stats package help: t.test, pnorm, ptukey, binom.test; complete-case population calculated in R.",
generator = list(path = "generators/r/hypothesis_population.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Exclude source row 0, then drop missing cells only in selected roles.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
x <- included$x[is.finite(included$x)]
t <- stats::t.test(x, mu = 6)
emit("STAT-HYP-MISSING-TEST-001", "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
"hypothesisTestsMenuItem", list(test = "one_sample_t", response = "x", target = 6),
list("/statistic" = tol(t$statistic, "one-sample t"),
"/p_value" = prob(t$p.value, "Student t tail"),
"/sample_count" = exact(length(x), "Finite selected responses after exclusion."),
"/dropped_rows" = exact(nrow(included) - length(x), "Missing selected response.")),
list("One-sample response x; R t.test removes the single missing response in included rows."))
grouped <- included[is.finite(included$x) & !is.na(included$g), , drop = FALSE]
a <- grouped$x[grouped$g == "A"]
b <- grouped$x[grouped$g == "B"]
delta <- mean(a) - mean(b)
va <- var(a) / length(a)
vb <- var(b) / length(b)
se <- sqrt(va + vb)
df <- (va + vb)^2 / (va^2 / (length(a) - 1) + vb^2 / (length(b) - 1))
emit("STAT-HYP-MISSING-EQUIV-001", "tabelz.executor.equivalence", "equivalenceMenuItem",
list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = -3, upper_bound = 3),
list("/difference_a_minus_b" = tol(delta, "A-B mean"),
"/p_lower" = prob(stats::pt((delta + 3) / se, df, lower.tail = FALSE), "lower TOST"),
"/analyzed_count" = exact(nrow(grouped), "Complete x/group rows.")),
list("Welch two-sample TOST, complete x/group rows."))
effect <- mean(x - 6)
se_one <- sd(x) / sqrt(length(x))
emit("STAT-HYP-MISSING-EQUIVEXT-001", "tabelz.executor.equivalence_extensions",
"equivalenceMenuItem",
list(mode = "one_sample_difference", response_column = "x", target = 6,
lower_bound = -2, upper_bound = 2),
list("/estimate" = tol(effect, "mean(x-6)"),
"/p_lower" = prob(stats::pt((effect + 2) / se_one, length(x) - 1,
lower.tail = FALSE), "lower one-sample TOST"),
"/analyzed_count" = exact(length(x), "Complete response count.")),
list("One-sample difference TOST on finite x after exclusion."))
z <- (mean(x) - 6) / (2 / sqrt(length(x)))
emit("STAT-HYP-MISSING-BASIC-001", "tabelz.executor.additional_basic_tests",
"additionalBasicTestsMenuItem",
list(method = "known_sigma_z", response_column = "x", known_sigma = 2, null_mean = 6),
list("/statistic" = tol(z, "known-sigma Z"),
"/p_value" = prob(2 * stats::pnorm(-abs(z)), "normal tail"),
"/sample_size" = exact(length(x), "Finite responses.")),
list("Known sigma 2; incomplete response removed."))
assignments <- utils::combn(seq_len(nrow(grouped)), length(a))
pooled <- grouped$x
sum_a <- colSums(matrix(pooled[assignments], nrow = length(a)))
null <- sum_a / length(a) - (sum(pooled) - sum_a) / length(b)
p_perm <- mean(abs(null) >= abs(delta) - 1e-12)
emit("STAT-HYP-MISSING-PERM-001", "tabelz.executor.permutation_tests",
"permutationTestsMenuItem",
list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "exact"),
list("/statistic" = tol(delta, "included A-B mean"),
"/p_value" = prob(p_perm, "all complete-case label assignments"),
"/analyzed_count" = exact(nrow(grouped), "Complete x/group rows.")),
list("All choose(n,nA) complete-case assignments enumerated."))
# Statistician ruling, issue #12: a runs test rejects a missing value inside
# the retained sequence rather than joining its neighbours. HYP_MISSING has a
# missing x after the explicit exclusion of row 0, so the request is refused.
stopifnot(any(!is.finite(included$x)))
emit("STAT-HYP-MISSING-RANK-001", "tabelz.executor.blocked_rank_tests",
"blockedRankTestsMenuItem",
list(method = "runs", response_column = "x", reference_mode = "median",
inference = "asymptotic"),
structure(list(), names = character()),
list("Chapter 32 and the issue #12 ruling: a missing value in the retained runs sequence is refused, not dropped.",
"Until 2026-09-25 this case expected the missing value to be dropped and the neighbours joined."),
error = list(error_class = "BlockedRankTestsExecutionError",
message_contains = "missing or non-finite required values"))
gh_estimate <- mean(a) - mean(b)
emit("STAT-HYP-MISSING-COMP-001", "tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem",
list(response_column = "x", group_column = "g", family = "games_howell"),
list("/comparisons/0/estimate" = tol(gh_estimate, "A-B mean difference"),
"/observations" = exact(nrow(grouped), "Complete x/group rows.")),
list("Two-group Games-Howell; first and only contrast is A-B."))
emit("STAT-HYP-MISSING-BOOT-001", "tabelz.executor.bootstrap_estimates",
"bootstrapMenuItem",
list(column = "x", statistic = "mean", seed = 12L, resamples = 200L),
list("/estimate" = tol(mean(x), "point sample mean"),
"/complete_count" = exact(length(x), "Finite included responses.")),
list("Bootstrap point estimate only; interval validation belongs to V27."))
cat("hypothesis missing/excluded cases generated\n")
generators/r/hypothesis_power_axes.R
source("common.R", local = TRUE)
run <- generator_run(c("stats", "pwr", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Independent R prospective power:", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
emit <- function(id, config, output, notes) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = list("/value" = output),
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis",
entry_point = "tabelz.analysis.hypothesis_tests:run_power_analysis",
menu_items = list("hypothesisTestsMenuItem"),
title = paste("Prospective power axis:", id), case_kind = "nominal",
dataset = NULL, excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R stats and pwr",
citation = "R stats::power.t.test and pwr::pwr.chisq.test / pwr.2p2n.test / pwr.anova.test documentation.",
generator = list(path = "generators/r/hypothesis_power_axes.R", sha256 = ""),
independent = TRUE,
convention_notes = notes),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
sample <- stats::power.t.test(power = 0.8, delta = 0.5, sd = 1, sig.level = 0.05,
type = "one.sample", alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-POWER-SAMPLE-SIZE-001",
list(analysis = "one_sample_t", solve_for = "sample_size", effect_size = 0.5,
target_power = 0.8, alpha = 0.05),
exact(ceiling(sample$n), "Smallest integer n meeting target power from R noncentral t."),
list("R power.t.test strict=TRUE includes both two-sided rejection tails; round solved n upward."))
for (alternative in c("less", "greater")) {
one <- stats::power.t.test(n = 30, delta = 0.5, sd = 1, sig.level = 0.05,
type = "one.sample", alternative = "one.sided")
emit(paste0("STAT-HYP-POWER-ALT-", toupper(alternative), "-001"),
list(analysis = "one_sample_t", solve_for = "power", effect_size = 0.5,
sample_size = 30L, alternative = alternative),
tol(one$power, paste(alternative, "one-sided t power")),
list("Positive standardized effect magnitude; one-sided noncentral t power.",
"The manual names less and greater alternatives but gives a positive effect-size input."))
}
pair <- stats::power.t.test(n = 30, delta = 0.5, sd = 1, sig.level = 0.05,
type = "paired", alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-POWER-PAIRED-001",
list(analysis = "paired_t", solve_for = "power", effect_size = 0.5, sample_size = 30L),
tol(pair$power, "paired t power for 30 pairs"),
list("Cohen dz = 0.5, n is number of pairs; strict two-sided t rejection."))
# Unequal-allocation independent t: n2 is ceil(ratio*n1).
n1 <- 30
n2 <- ceiling(1.5 * n1)
df <- n1 + n2 - 2
ncp <- 0.5 / sqrt(1 / n1 + 1 / n2)
critical <- stats::qt(0.975, df)
two_t_power <- stats::pt(-critical, df, ncp = ncp) +
stats::pt(critical, df, ncp = ncp, lower.tail = FALSE)
emit("STAT-HYP-POWER-TWO-T-001",
list(analysis = "two_sample_t", solve_for = "power", effect_size = 0.5,
sample_size = n1, ratio = 1.5),
tol(two_t_power, "unequal-allocation independent t noncentral tail"),
list("n1=30, n2=ceil(1.5*n1)=45; df=n1+n2-2; noncentrality d/sqrt(1/n1+1/n2)."))
one_prop <- pwr::pwr.chisq.test(w = 0.5, N = 30, df = 1, sig.level = 0.05)
emit("STAT-HYP-POWER-ONE-PROP-001",
list(analysis = "one_proportion", solve_for = "power", effect_size = 0.5,
sample_size = 30L),
tol(one_prop$power, "one-degree chi-square power"),
list("Cohen h=0.5 enters the one-degree chi-square approximation as w=0.5."))
two_prop <- pwr::pwr.2p2n.test(h = 0.5, n1 = 30, n2 = 30, sig.level = 0.05,
alternative = "two.sided")
emit("STAT-HYP-POWER-TWO-PROP-001",
list(analysis = "two_proportion", solve_for = "power", effect_size = 0.5,
sample_size = 30L, ratio = 1),
tol(two_prop$power, "two-independent-proportion normal power"),
list("Cohen h=0.5 and 30 observations per group; two-sided normal approximation."))
anova <- pwr::pwr.anova.test(k = 3, n = 10, f = 0.5, sig.level = 0.05)
emit("STAT-HYP-POWER-ANOVA-001",
list(analysis = "one_way_anova", solve_for = "power", effect_size = 0.5,
sample_size = 30L, groups = 3L),
tol(anova$power, "three-group ANOVA noncentral F power"),
list("Cohen f=0.5, three groups with ten observations each; N=30."))
cat("hypothesis power-axis cases generated\n")
generators/r/hypothesis_repeated_measures.R
source("common.R", local = TRUE)
# Repeated-measures ANOVA on wide data (Statistics > Compare means & ANOVA >
# Repeated-measures ANOVA). References: stats::aov with an Error stratum for the univariate
# within-subject table, stats::anova.mlm for the multivariate tests, stats:::sphericity (the
# helper anova.mlm uses) for the Greenhouse-Geisser and Huynh-Feldt epsilons,
# stats::mauchly.test for W, and t.test for the one-contrast designs where F = t^2. Every
# reference is computed from the re-read HYP_RM CSV.
constructed <- data.frame(
t1 = c(10.2, 11.5, 9.8, 12.1, 10.9, 13.4, 12.2, 14.8, 13.9, 12.6),
t2 = c(11.0, 12.9, 10.1, 12.4, 12.0, 13.1, 13.5, 15.9, 13.2, NA),
t3 = c(12.3, 13.6, 11.9, 14.0, 12.2, 15.3, 13.9, 17.2, 15.5, 14.1),
t4 = c(11.4, 12.2, 10.7, 13.5, 11.8, 14.6, 13.0, 16.1, 14.4, 13.3),
grp = c(rep("A", 5), rep("B", 5)),
stringsAsFactors = FALSE
)
export_dataset(
constructed, "HYP_RM", "Constructed by hypothesis_repeated_measures.R",
"R stats::aov (Error stratum), anova.mlm, stats:::sphericity, mauchly.test, t.test",
"Constructed test data; no third-party dataset license.",
c(t1 = "Float64", t2 = "Float64", t3 = "Float64", t4 = "Float64", grp = "String")
)
d <- read_dataset("HYP_RM")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R", label, "from re-read HYP_RM CSV."))
prob <- function(x, label)
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = "1e-8",
rationale = paste("R", label, "from re-read HYP_RM CSV."))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
word <- function(x, label) list(comparison = "exact", value = as.character(x), rationale = label)
absent <- function(label) list(comparison = "absent", rationale = label)
entry <- "tabelz.analysis.repeated_measures:compute_repeated_measures"
menu <- "repeatedMeasuresMenuItem"
notes_common <- c(
"A subject missing any cell column is dropped and counted as incomplete; nu = N - rank(X).",
"Within-subject effects use orthonormal contrasts: SS = tr(H) and error SS = tr(E_W) equal the classical univariate repeated-measures sums of squares (aov with Error(subject/factor)); F on (r q, nu q) df; the Greenhouse-Geisser and Huynh-Feldt (Lecoutre) epsilons multiply both df.",
"docs/manual/chapters/83-repeated-anova-and-contrasts.tex:33-47: the univariate F, Greenhouse-Geisser epsilon (sum lambda)^2 / (q sum lambda^2), corrected tests multiplying both df, the Huynh-Feldt/Lecoutre and lower-bound corrections, Mauchly's first-order approximation.",
"docs/manual/chapters/83-repeated-anova-and-contrasts.tex:48-52: 'For a two-occasion design without between-subject factors, the test equals the paired t test squared.'",
"docs/manual/chapters/83-repeated-anova-and-contrasts.tex:54-58: Pillai, Wilks, Hotelling-Lawley and Roy multivariate tests."
)
emit <- function(id, title, kind, config, outputs, notes, excluded = list()) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs, expected_error = NULL,
deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "hypothesis",
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/HYP_RM.json", sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R 4.6.1 stats",
citation = "R Core Team, stats package help: aov, anova.mlm, mauchly.test, SSD, t.test; Greenhouse SW, Geisser S (1959), Psychometrika 24, 95-112; Lecoutre B (1991), Journal of Educational Statistics 16, 371-372.",
generator = list(path = "generators/r/hypothesis_repeated_measures.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, notes_common, "Expected computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-REPEATED-MEASURES-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# Orthonormal contrast basis (rows) of the levels of one factor.
orthonormal <- function(p) t(qr.Q(qr(stats::contr.helmert(p))))
# Sphericity quantities of an effect with contrast rows T on the residual SSD of an mlm fit.
sphericity_outputs <- function(index, fit, T, nu) {
ssd <- stats::SSD(fit)
q <- nrow(T)
eps <- stats:::sphericity(ssd, T = T)
lambda <- eigen(T %*% ssd$SSD %*% t(T) / nu, symmetric = TRUE, only.values = TRUE)$values
w <- prod(lambda) / (mean(lambda))^q
scale <- nu - (2 * q^2 + q + 2) / (6 * q)
chi <- -scale * log(w)
prefix <- paste0("/sphericity/", index)
outputs <- list()
outputs[[paste0(prefix, "/dimensions")]] <- exact(q, "contrast dimensions")
outputs[[paste0(prefix, "/greenhouse_geisser")]] <- tol(eps$GG.eps, "stats:::sphericity Greenhouse-Geisser epsilon")
outputs[[paste0(prefix, "/huynh_feldt")]] <- tol(eps$HF.eps, "stats:::sphericity Huynh-Feldt (Lecoutre) epsilon")
outputs[[paste0(prefix, "/huynh_feldt_applied")]] <- tol(min(1, eps$HF.eps), "Huynh-Feldt epsilon capped at 1")
outputs[[paste0(prefix, "/lower_bound")]] <- tol(1 / q, "lower-bound epsilon 1/q")
outputs[[paste0(prefix, "/mauchly_w")]] <- tol(w, "Mauchly W = det(S) / (tr(S)/q)^q of the contrast covariance")
outputs[[paste0(prefix, "/chi_square")]] <- tol(chi, "first-order Mauchly chi-square -(nu - (2q^2+q+2)/(6q)) log W")
outputs[[paste0(prefix, "/df")]] <- exact(q * (q + 1) / 2 - 1, "Mauchly df")
outputs[[paste0(prefix, "/p_value")]] <- prob(stats::pchisq(chi, q * (q + 1) / 2 - 1, lower.tail = FALSE), "chi-square tail of the first-order Mauchly statistic")
list(outputs = outputs, gg = eps$GG.eps, hf = min(1, eps$HF.eps))
}
univariate_outputs <- function(index, ss, df, error_ss, error_df, gg, hf) {
f <- (ss / df) / (error_ss / error_df)
prefix <- paste0("/within_tests/", index)
outputs <- list()
outputs[[paste0(prefix, "/sum_of_squares")]] <- tol(ss, "within-effect SS")
outputs[[paste0(prefix, "/df")]] <- exact(df, "effect df")
outputs[[paste0(prefix, "/mean_square")]] <- tol(ss / df, "effect mean square")
outputs[[paste0(prefix, "/f_value")]] <- tol(f, "univariate F")
outputs[[paste0(prefix, "/error_sum_of_squares")]] <- tol(error_ss, "error SS of the effect's contrasts")
outputs[[paste0(prefix, "/error_df")]] <- exact(error_df, "error df")
outputs[[paste0(prefix, "/partial_eta_squared")]] <- tol(ss / (ss + error_ss), "partial eta squared")
corrections <- list(c("Sphericity assumed", 1), c("Greenhouse-Geisser", gg), c("Huynh-Feldt", hf), c("Lower bound", df / (df / 1)))
epsilons <- c(1, gg, hf, NA)
for (j in 1:4) {
cp <- paste0(prefix, "/tests/", j - 1)
e <- epsilons[j]
if (is.na(e)) next
outputs[[paste0(cp, "/correction")]] <- word(corrections[[j]][1], "correction label")
outputs[[paste0(cp, "/epsilon")]] <- tol(e, "epsilon applied")
outputs[[paste0(cp, "/df1")]] <- tol(df * e, "numerator df times epsilon")
outputs[[paste0(cp, "/df2")]] <- tol(error_df * e, "denominator df times epsilon")
outputs[[paste0(cp, "/p_value")]] <- prob(stats::pf(f, df * e, error_df * e, lower.tail = FALSE), "F tail on the corrected df")
}
outputs
}
multivariate_outputs <- function(start, fit, X, M, idata, term) {
tests <- c("Pillai", "Wilks", "Hotelling-Lawley", "Roy")
labels <- c("Pillai's trace", "Wilks' lambda", "Hotelling-Lawley trace", "Roy's root")
outputs <- list()
for (j in seq_along(tests)) {
a <- stats::anova(fit, X = X, M = M, idata = idata, test = tests[j])
row <- a[term, ]
cp <- paste0("/multivariate_tests/", start + j - 1)
outputs[[paste0(cp, "/statistic")]] <- word(labels[j], "statistic name")
outputs[[paste0(cp, "/value")]] <- tol(row[[2]], paste("anova.mlm", tests[j], "statistic"))
outputs[[paste0(cp, "/f_value")]] <- tol(row[["approx F"]], paste("anova.mlm", tests[j], "approximate F"))
outputs[[paste0(cp, "/df1")]] <- tol(row[["num Df"]], "numerator df")
outputs[[paste0(cp, "/df2")]] <- tol(row[["den Df"]], "denominator df")
outputs[[paste0(cp, "/p_value")]] <- prob(row[["Pr(>F)"]], paste("anova.mlm", tests[j], "F tail"))
}
outputs
}
# ---- 001: one within factor with three levels, no between-subject factor ---------------------
cc <- d[stats::complete.cases(d[c("t1", "t2", "t3")]), ]
Y <- as.matrix(cc[c("t1", "t2", "t3")])
n <- nrow(Y); p <- 3L; nu <- n - 1L
fit <- stats::lm(Y ~ 1)
idata <- data.frame(Time = factor(c("t1", "t2", "t3")))
long <- data.frame(y = as.vector(Y), subject = factor(rep(seq_len(n), p)), Time = factor(rep(1:p, each = n)))
strata <- summary(stats::aov(y ~ Time + Error(subject / Time), data = long))
within <- strata[["Error: subject:Time"]][[1]]
sph <- sphericity_outputs(0, fit, orthonormal(p), nu)
outputs <- c(sph$outputs,
univariate_outputs(0, within["Time", "Sum Sq"], within["Time", "Df"], within["Residuals", "Sum Sq"], within["Residuals", "Df"], sph$gg, sph$hf),
multivariate_outputs(0, fit, ~1, ~Time, idata, "(Intercept)"))
outputs[["/within_tests/0/effect"]] <- word("Time", "effect name")
total <- rowSums(Y) / sqrt(p)
outputs[["/between_tests/0/effect"]] <- word("(Intercept)", "between effect")
outputs[["/between_tests/0/sum_of_squares"]] <- tol(n * mean(total)^2, "intercept SS of the normalized subject total")
outputs[["/between_tests/0/f_value"]] <- tol(unname(stats::t.test(total)$statistic)^2, "squared one-sample t of the normalized total")
outputs[["/between_tests/0/p_value"]] <- prob(stats::t.test(total)$p.value, "two-sided t tail, equal to the F tail on (1, nu)")
outputs[["/between_error_ss"]] <- tol(sum((total - mean(total))^2), "residual SS of the normalized subject total")
outputs[["/design/subjects"]] <- exact(nrow(d), "rows in the table")
outputs[["/design/used"]] <- exact(n, "complete subjects")
outputs[["/design/incomplete"]] <- exact(nrow(d) - n, "subjects missing a cell")
outputs[["/design/residual_df"]] <- exact(nu, "N - rank(X)")
emit("STAT-HYP-REPEATED-MEASURES-001", "Repeated measures one within factor three levels", "nominal",
list(factor1_name = "Time", factor1_levels = list("t1", "t2", "t3"), cells = list("t1", "t2", "t3"),
contrast_family = "none", pairwise_procedure = "none"),
outputs, c("Subject 10 lacks t2, so 9 subjects are used; nu = 8.",
"Mauchly: W from the eigenvalues of the orthonormal-contrast residual covariance (equal to mauchly.test's W); the chi-square and p use the first-order factor nu - (2q^2 + q + 2)/(6q) rather than mauchly.test's second-order expansion, as the manual states.",
"Between-subject intercept test on the normalized subject total Y 1 / sqrt(p): F = t^2 of the one-sample t test of that total."))
# ---- 002: the same within factor with a balanced between-subject factor ----------------------
# Subject 10 (grp B) is incomplete; row 5 (grp A, index 4) is excluded so each group keeps four
# subjects and the sequential anova.mlm tests coincide with the Type III tests.
keep <- stats::complete.cases(d[c("t1", "t2", "t3")]) & seq_len(nrow(d)) != 5
cc <- d[keep, ]
Y <- as.matrix(cc[c("t1", "t2", "t3")])
n <- nrow(Y); p <- 3L
grp <- factor(cc$grp)
fit <- stats::lm(Y ~ grp, contrasts = list(grp = "contr.sum"))
nu <- n - 2L
long <- data.frame(y = as.vector(Y), subject = factor(rep(seq_len(n), p)), Time = factor(rep(1:p, each = n)), grp = rep(grp, p))
strata <- summary(stats::aov(y ~ grp * Time + Error(subject / Time), data = long))
within <- strata[["Error: subject:Time"]][[1]]
between <- strata[["Error: subject"]][[1]]
sph <- sphericity_outputs(0, fit, orthonormal(p), nu)
outputs <- c(sph$outputs,
univariate_outputs(0, within["Time", "Sum Sq"], within["Time", "Df"], within["Residuals", "Sum Sq"], within["Residuals", "Df"], sph$gg, sph$hf),
univariate_outputs(1, within["grp:Time", "Sum Sq"], within["grp:Time", "Df"], within["Residuals", "Sum Sq"], within["Residuals", "Df"], sph$gg, sph$hf),
multivariate_outputs(0, fit, ~1, ~Time, idata, "(Intercept)"),
multivariate_outputs(4, fit, ~1, ~Time, idata, "grp"))
rownames(within) <- trimws(rownames(within)); rownames(between) <- trimws(rownames(between))
outputs[["/within_tests/0/effect"]] <- word("Time", "effect name")
outputs[["/within_tests/1/effect"]] <- word("Time × grp", "interaction effect name")
total <- rowSums(Y) / sqrt(p)
bt <- stats::anova(stats::lm(total ~ grp))
outputs[["/between_tests/1/effect"]] <- word("grp", "between effect")
outputs[["/between_tests/1/sum_of_squares"]] <- tol(bt["grp", "Sum Sq"], "grp SS of the normalized subject total")
outputs[["/between_tests/1/df"]] <- exact(bt["grp", "Df"], "grp df")
outputs[["/between_tests/1/f_value"]] <- tol(bt["grp", "F value"], "grp F")
outputs[["/between_tests/1/p_value"]] <- prob(bt["grp", "Pr(>F)"], "grp F tail")
outputs[["/between_error_ss"]] <- tol(bt["Residuals", "Sum Sq"], "residual SS of the normalized subject total")
outputs[["/design/used"]] <- exact(n, "complete, non-excluded subjects")
outputs[["/design/incomplete"]] <- exact(1L, "subject 10 lacks t2")
outputs[["/design/excluded"]] <- exact(1L, "row 5 excluded")
outputs[["/design/residual_df"]] <- exact(nu, "N - rank(X) with the intercept and one sum-to-zero column")
emit("STAT-HYP-REPEATED-MEASURES-002", "Repeated measures with a balanced between-subject factor", "missing_excluded",
list(factor1_name = "Time", factor1_levels = list("t1", "t2", "t3"), cells = list("t1", "t2", "t3"),
between = list("grp"), contrast_family = "none", pairwise_procedure = "none"),
outputs, c("Within effects: aov(y ~ grp * Time + Error(subject / Time)) rows Time and grp:Time with the subject:Time residual; sphericity from the residual SSD of lm(Y ~ grp) on nu = 6 df.",
"Multivariate tests: anova.mlm rows (Intercept) (= Time) and grp (= Time x grp) with X = ~1, M = ~Time; balanced groups make the sequential tests the Type III tests.",
"Between tests on the normalized subject total: anova(lm(total ~ grp))."),
excluded = list(4L))
# ---- 003: two occasions: F equals the squared paired t --------------------------------------
cc <- d[stats::complete.cases(d[c("t1", "t2")]), ]
n <- nrow(cc); nu <- n - 1L
tt <- stats::t.test(cc$t1, cc$t2, paired = TRUE)
score <- (cc$t2 - cc$t1) / sqrt(2) # orthonormal contrast score
outputs <- list(
"/sphericity/0/dimensions" = exact(1L, "one contrast"),
"/sphericity/0/greenhouse_geisser" = tol(1, "epsilon is 1 with one contrast"),
"/sphericity/0/mauchly_w" = absent("no Mauchly test with one contrast"),
"/within_tests/0/effect" = word("Time", "effect name"),
"/within_tests/0/sum_of_squares" = tol(n * mean(score)^2, "n times the squared mean contrast score"),
"/within_tests/0/df" = exact(1L, "one contrast"),
"/within_tests/0/error_sum_of_squares" = tol(sum((score - mean(score))^2), "contrast score SS about its mean"),
"/within_tests/0/error_df" = exact(nu, "n - 1"),
"/within_tests/0/f_value" = tol(unname(tt$statistic)^2, "squared paired t"),
"/within_tests/0/tests/0/p_value" = prob(tt$p.value, "paired t two-sided tail"),
"/within_tests/0/tests/1/epsilon" = tol(1, "Greenhouse-Geisser epsilon 1"),
"/within_tests/0/tests/1/p_value" = prob(tt$p.value, "unchanged by epsilon 1"),
"/multivariate_tests/0/f_value" = tol(unname(tt$statistic)^2, "Pillai exact F with one contrast"),
"/multivariate_tests/0/p_value" = prob(tt$p.value, "exact F tail with one contrast"),
"/multivariate_tests/1/p_value" = prob(tt$p.value, "Wilks exact F tail with one contrast"),
"/design/used" = exact(n, "complete subjects"),
"/design/residual_df" = exact(nu, "n - 1")
)
emit("STAT-HYP-REPEATED-MEASURES-003", "Repeated measures two occasions equals paired t squared", "axis_coverage",
list(factor1_name = "Time", factor1_levels = list("t1", "t2"), cells = list("t1", "t2"),
contrast_family = "none", pairwise_procedure = "none"),
outputs, "Two occasions: the orthonormal contrast score (t2 - t1)/sqrt(2) gives F = t^2 of the paired t test and the same p; with one contrast every epsilon is 1, there is no Mauchly test and the multivariate F is exact.")
# ---- 004: two within factors, 2 x 2, each effect a single contrast --------------------------
cc <- d[stats::complete.cases(d[c("t1", "t2", "t3", "t4")]), ]
n <- nrow(cc); nu <- n - 1L
contrasts <- list(A = c(1, 1, -1, -1) / 2, B = c(1, -1, 1, -1) / 2, "A × B" = c(1, -1, -1, 1) / 2)
outputs <- list("/design/used" = exact(n, "complete subjects"), "/design/residual_df" = exact(nu, "n - 1"))
for (j in seq_along(contrasts)) {
score <- as.matrix(cc[c("t1", "t2", "t3", "t4")]) %*% contrasts[[j]]
tt <- stats::t.test(score)
prefix <- paste0("/within_tests/", j - 1)
outputs[[paste0(prefix, "/effect")]] <- word(names(contrasts)[j], "effect name")
outputs[[paste0(prefix, "/sum_of_squares")]] <- tol(n * mean(score)^2, "n times the squared mean orthonormal contrast score")
outputs[[paste0(prefix, "/df")]] <- exact(1L, "one contrast")
outputs[[paste0(prefix, "/error_sum_of_squares")]] <- tol(sum((score - mean(score))^2), "contrast score SS about its mean")
outputs[[paste0(prefix, "/error_df")]] <- exact(nu, "n - 1")
outputs[[paste0(prefix, "/f_value")]] <- tol(unname(tt$statistic)^2, "squared one-sample t of the contrast score")
outputs[[paste0(prefix, "/tests/0/p_value")]] <- prob(tt$p.value, "t tail = F tail on (1, nu)")
outputs[[paste0("/sphericity/", j - 1, "/greenhouse_geisser")]] <- tol(1, "epsilon 1 with one contrast")
outputs[[paste0("/multivariate_tests/", 4 * (j - 1), "/p_value")]] <- prob(tt$p.value, "Pillai exact F tail with one contrast")
}
emit("STAT-HYP-REPEATED-MEASURES-004", "Repeated measures two within factors 2 x 2", "axis_coverage",
list(factor1_name = "A", factor1_levels = list("a1", "a2"), factor2_name = "B", factor2_levels = list("b1", "b2"),
cells = list("t1", "t2", "t3", "t4"), contrast_family = "none", pairwise_procedure = "none"),
outputs, "Cells in first-factor-slowest order: t1 = a1 b1, t2 = a1 b2, t3 = a2 b1, t4 = a2 b2. Each effect is one orthonormal contrast (A: (t1 + t2 - t3 - t4)/2, B: (t1 - t2 + t3 - t4)/2, A x B: (t1 - t2 - t3 + t4)/2) whose one-sample t squared is the F; effects are ordered A, B, A x B with four multivariate rows each.")
cat("repeated measures cases generated\n")
generators/r/hypothesis_secondary.R
source("common.R", local = TRUE)
d <- read_dataset("HYP_CORE")
blocks <- data.frame(
block = rep(paste0("B", 1:4), each = 3),
treatment = rep(c("A", "B", "C"), 4),
x = c(4.1, 5.0, 6.2, 5.2, 6.4, 7.1, 4.8, 6.1, 7.4, 5.9, 6.8, 8.2),
stringsAsFactors = FALSE
)
export_dataset(
blocks, "HYP_BLOCKS", "Constructed by hypothesis_secondary.R",
"R stats::friedman.test and stats::ptukey help pages; balanced complete-block data.",
"Constructed test data; no third-party dataset license.",
c(block = "String", treatment = "String", x = "Float64")
)
b <- read_dataset("HYP_BLOCKS")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(value, label) {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Independent R calculation:", label))
}
prob <- function(value, label) {
list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = "1e-8", rationale = paste("Independent R tail:", label))
}
exact <- function(value, label) {
list(comparison = "exact", value = value, rationale = label)
}
emit <- function(id, entry, menu, title, kind, dataset, config, outputs, notes,
citation, excluded = list(), repetitions = 1L) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = if (is.null(dataset)) NULL else list(
path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = repetitions,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R 4.6.1 independent implementation",
citation = citation,
generator = list(path = "generators/r/hypothesis_secondary.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected values calculated after re-reading the exported CSV where data are present.")
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
# Independent Welch TOST: difference A-B, 90% two-sided interval at alpha=.05.
a <- d$x[d$g == "A"]
bb <- d$x[d$g == "B"]
diff <- mean(a) - mean(bb)
term_a <- var(a) / length(a)
term_b <- var(bb) / length(bb)
se <- sqrt(term_a + term_b)
df <- (term_a + term_b)^2 / (term_a^2 / (length(a) - 1) + term_b^2 / (length(bb) - 1))
lower <- -3
upper <- 3
p_lower <- stats::pt((diff - lower) / se, df, lower.tail = FALSE)
p_upper <- stats::pt((diff - upper) / se, df)
crit <- stats::qt(0.95, df)
emit("STAT-HYP-EQUIV-WELCH-001", "tabelz.executor.equivalence", "equivalenceMenuItem",
"Welch two-group TOST", "nominal", "HYP_CORE",
list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = lower, upper_bound = upper),
list("/difference_a_minus_b" = tol(diff, "A minus B"),
"/standard_error" = tol(se, "Welch standard error"),
"/degrees_of_freedom" = tol(df, "Welch-Satterthwaite df"),
"/p_lower" = prob(p_lower, "lower TOST"),
"/p_upper" = prob(p_upper, "upper TOST"),
"/interval_lower" = tol(diff - crit * se, "90% interval lower"),
"/interval_upper" = tol(diff + crit * se, "90% interval upper")),
list("Two one-sided Welch t tails; strict equivalence requires both p-values below alpha."),
"R stats::t.test documentation (Welch variance and Student t quantiles); analytic TOST from the two one-sided tests.")
# One-sample equivalence extension on x-target, using Student t tails.
target <- 6
delta <- d$x - target
effect <- mean(delta)
se1 <- sd(delta) / sqrt(length(delta))
df1 <- length(delta) - 1
lo1 <- -2
hi1 <- 2
emit("STAT-HYP-EQUIV-ONE-001", "tabelz.executor.equivalence_extensions", "equivalenceMenuItem",
"One-sample difference TOST", "nominal", "HYP_CORE",
list(mode = "one_sample_difference", response_column = "x", target = target,
lower_bound = lo1, upper_bound = hi1),
list("/estimate" = tol(effect, "mean(x-target)"),
"/standard_error" = tol(se1, "one-sample SE"),
"/p_lower" = prob(stats::pt((effect - lo1) / se1, df1, lower.tail = FALSE), "lower TOST"),
"/p_upper" = prob(stats::pt((effect - hi1) / se1, df1), "upper TOST")),
list("Student one-sample TOST on x-target."),
"R stats::t.test documentation; analytic one-sample TOST.")
# Basic known-sigma Z inference: exact normal tail, not estimated sample SD.
sigma <- 2
null_mean <- 6
z <- (mean(d$x) - null_mean) / (sigma / sqrt(length(d$x)))
emit("STAT-HYP-BASIC-Z-001", "tabelz.executor.additional_basic_tests", "additionalBasicTestsMenuItem",
"Known-sigma one-sample Z", "nominal", "HYP_CORE",
list(method = "known_sigma_z", input_kind = "raw", response_column = "x",
known_sigma = sigma, null_mean = null_mean),
list("/statistic" = tol(z, "known-sigma Z"),
"/p_value" = prob(2 * stats::pnorm(-abs(z)), "two-sided normal tail"),
"/estimate" = tol(mean(d$x), "sample mean")),
list("Z uses known sigma 2 and n=16; normal two-sided tail."),
"R stats::Normal documentation (pnorm and qnorm); closed-form Z statistic.")
# Enumerate every independent label assignment, without Monte Carlo draws.
pooled <- d$x
n_a <- length(a)
observed <- diff
all_a <- utils::combn(seq_along(pooled), n_a)
total_sum <- sum(pooled)
assigned_a <- colSums(matrix(pooled[all_a], nrow = n_a))
null <- assigned_a / n_a - (total_sum - assigned_a) / (length(pooled) - n_a)
p_exact <- mean(abs(null) >= abs(observed) - 1e-12)
emit("STAT-HYP-PERM-EXACT-001", "tabelz.executor.permutation_tests", "permutationTestsMenuItem",
"Exact independent mean permutation", "nominal", "HYP_CORE",
list(mode = "independent", response_column = "x", group_column = "g",
group_a = "A", group_b = "B", inference = "exact"),
list("/statistic" = tol(observed, "observed A-B mean"),
"/p_value" = prob(p_exact, "all choose(16,8) assignments"),
"/assignment_count" = exact(ncol(all_a), "All independent assignments enumerated.")),
list("combn enumerates every allocation of 8 labels to 16 values; two-sided absolute statistic."),
"R utils::combn documentation; exact finite-label randomization enumeration.")
friedman <- stats::friedman.test(x ~ treatment | block, data = b)
emit("STAT-HYP-FRIEDMAN-001", "tabelz.executor.blocked_rank_tests", "blockedRankTestsMenuItem",
"Complete-block Friedman", "nominal", "HYP_BLOCKS",
list(method = "friedman", response_column = "x", treatment_column = "treatment",
block_column = "block"),
list("/friedman_statistic" = tol(friedman$statistic, "Friedman chi-square"),
"/p_value" = prob(friedman$p.value, "Friedman chi-square tail"),
"/block_count" = exact(length(unique(b$block)), "Four complete blocks.")),
list("No within-block ties; R friedman.test chi-square approximation."),
"R stats::friedman.test documentation.")
# Games-Howell for three treatment groups. Compare the first A-B contrast.
ga <- b$x[b$treatment == "A"]
gb <- b$x[b$treatment == "B"]
gc <- b$x[b$treatment == "C"]
va <- var(ga) / length(ga)
vb <- var(gb) / length(gb)
gh_df <- (va + vb)^2 / (va^2 / (length(ga) - 1) + vb^2 / (length(gb) - 1))
gh_diff <- mean(ga) - mean(gb)
gh_q <- abs(gh_diff) / sqrt(0.5 * (va + vb))
emit("STAT-HYP-GAMES-HOWELL-001", "tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem", "Three-group Games-Howell", "nominal", "HYP_BLOCKS",
list(response_column = "x", group_column = "treatment", family = "games_howell"),
list("/comparisons/0/estimate" = tol(gh_diff, "A-B mean difference"),
"/comparisons/0/degrees_of_freedom" = tol(gh_df, "Welch pair df"),
"/comparisons/0/p_value" = prob(stats::ptukey(gh_q, nmeans = 3, df = gh_df,
lower.tail = FALSE), "Games-Howell ptukey tail")),
list("Pairwise Welch SE and df; R ptukey upper tail with three means."),
"R stats::ptukey documentation; analytic Games-Howell studentized-range tail.")
emit("STAT-HYP-BOOTSTRAP-POINT-001", "tabelz.executor.bootstrap_estimates",
"bootstrapMenuItem", "Mean point estimate with seeded resampling", "determinism", "HYP_CORE",
list(column = "x", statistic = "mean", seed = 12L, resamples = 200L),
list("/estimate" = tol(mean(d$x), "sample mean point statistic"),
"/complete_count" = exact(nrow(d), "All rows are complete.")),
list("Bootstrap interval and SE deliberately deferred to V27; only deterministic point estimate and population count compared."),
"R base::mean documentation; point statistic only (interval validation is V27).",
repetitions = 2L)
# Prospective one-sample t power from R's noncentral-t reference.
power <- stats::power.t.test(n = 30, delta = 0.5, sd = 1, sig.level = 0.05,
type = "one.sample", alternative = "two.sided", strict = TRUE)
emit("STAT-HYP-POWER-ONE-T-001", "tabelz.analysis.hypothesis_tests:run_power_analysis",
"hypothesisTestsMenuItem", "One-sample t achieved power", "nominal", NULL,
list(analysis = "one_sample_t", solve_for = "power", effect_size = 0.5,
sample_size = 30L, alpha = 0.05),
list("/value" = tol(power$power, "R noncentral-t achieved power"),
"/achieved_power" = tol(power$power, "R noncentral-t achieved power")),
list("R power.t.test uses standardized delta/sd = 0.5 and n=30.",
"strict=TRUE counts both rejection tails. docs/manual/chapters/07-models-and-tests.tex:864-866: 'two-sided alternatives.'"),
"R stats::power.t.test documentation.")
cat("hypothesis secondary cases generated\n")
generators/r/hypothesis_simultaneous.R
source("common.R", local = TRUE)
d <- read_dataset("HYP_BLOCKS")
d$treatment <- factor(d$treatment, levels = c("A", "B", "C"))
run <- generator_run(c("stats", "multcomp", "mvtnorm", "jsonlite"))
tol <- function(x, label) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R simultaneous-comparison reference:", label))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R F tail:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, entry, menu, config, outputs, deferred, notes) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = deferred, generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Simultaneous comparisons:", id), case_kind = "axis_coverage",
dataset = list(path = "reference_data/datasets/HYP_BLOCKS.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R stats and multcomp",
citation = "R stats::aov and TukeyHSD help; multcomp::glht and mvtnorm::pmvt documentation.",
generator = list(path = "generators/r/hypothesis_simultaneous.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "All fitted data were re-read from exported HYP_BLOCKS CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
fit <- stats::aov(x ~ treatment, data = d)
anova_row <- summary(fit)[[1]][1, ]
tukey <- stats::TukeyHSD(fit, "treatment", conf.level = 0.95)$treatment
emit("STAT-HYP-TUKEY-001", "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
"hypothesisTestsMenuItem",
list(test = "one_way_anova", response = "x", group = "treatment", posthoc = TRUE),
list("/statistic" = tol(anova_row[["F value"]], "one-way F"),
"/p_value" = prob(anova_row[["Pr(>F)"]], "one-way F tail"),
"/intervals/0/estimate" = tol(tukey["B-A", "diff"], "Tukey-Kramer B-A contrast"),
"/intervals/0/significant" = exact(tukey["B-A", "p adj"] < 0.05,
"R TukeyHSD adjusted p below alpha.")),
list(list(path = "/intervals/0/lower", reason = "Statsmodels rounds the displayed Tukey interval; full-tier numeric comparison is deferred."),
list(path = "/intervals/0/upper", reason = "Statsmodels rounds the displayed Tukey interval; full-tier numeric comparison is deferred.")),
list("R TukeyHSD is Tukey-Kramer for unequal group sizes; this balanced design uses the same convention.",
"The first ordered interval is B minus A; its rejection flag is compared without reducing the numerical tier."))
# The multivariate-t adjusted Dunnett p-value is numerically integrated. Set
# an explicit seed and request abseps=1e-12, but defer p unless the error tier
# can be independently established against a second integration implementation.
model <- stats::lm(x ~ treatment, data = d)
contrast <- multcomp::glht(model, linfct = multcomp::mcp(treatment = "Dunnett"))
set.seed(12345)
integrated <- summary(contrast, test = multcomp::adjusted("single-step", abseps = 1e-12))
estimates <- as.numeric(integrated$test$coefficients)
ses <- as.numeric(integrated$test$sigma)
statistics <- as.numeric(integrated$test$tstat)
df <- as.numeric(integrated$df)
emit("STAT-HYP-DUNNETT-R-001", "tabelz.executor.additional_comparisons",
"additionalComparisonsMenuItem",
list(response_column = "x", group_column = "treatment", family = "dunnett",
control_level = "A", seed = 12345L, alpha = 0.05),
list("/comparisons/0/estimate" = tol(estimates[[1]], "B-A mean contrast"),
"/comparisons/0/standard_error" = tol(ses[[1]], "pooled Dunnett SE"),
"/comparisons/0/degrees_of_freedom" = tol(df, "pooled residual df"),
"/comparisons/0/statistic" = tol(statistics[[1]], "B-A t statistic"),
"/comparisons/1/estimate" = tol(estimates[[2]], "C-A mean contrast"),
"/comparisons/1/standard_error" = tol(ses[[2]], "pooled Dunnett SE")),
list(list(path = "/comparisons/0/p_value", reason = "Randomized multivariate-t integration error is not independently bounded below the 1e-8 probability tier."),
list(path = "/comparisons/1/p_value", reason = "Randomized multivariate-t integration error is not independently bounded below the 1e-8 probability tier.")),
list("Explicit control A; each treatment mean minus A; pooled linear-model MSE.",
"R multcomp single-step Dunnett integration seed 12345 and mvtnorm abseps=1e-12; adjusted p-values deferred pending an independent error bound."))
cat("hypothesis simultaneous-comparison cases generated\n")
generators/r/hypothesis_tests.R
source("common.R", local = TRUE)
# Constructed, deliberately non-normal-looking small samples. Every oracle below
# reads the exported CSV rather than referring to these in-memory vectors.
constructed <- data.frame(
x = c(3.1, 4.2, 5.4, 5.8, 6.3, 7.2, 7.7, 8.5,
5.2, 5.7, 6.1, 7.4, 8.2, 8.8, 9.1, 10.3),
y = c(2.4, 3.8, 4.9, 5.1, 5.8, 6.8, 7.1, 8.0,
4.7, 5.1, 5.8, 6.6, 7.5, 8.1, 8.8, 9.5),
g = rep(c("A", "B"), each = 8),
h = rep(c("L", "R"), 8),
outcome = c("yes", "no", "yes", "yes", "no", "yes", "no", "yes",
"yes", "no", "no", "no", "yes", "no", "no", "yes"),
stringsAsFactors = FALSE
)
export_dataset(
constructed, "HYP_CORE", "Constructed by hypothesis_tests.R",
"R stats manual: t.test, var.test, aov, binom.test, chisq.test, fisher.test, wilcox.test, kruskal.test",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64", g = "String", h = "String", outcome = "String")
)
d <- read_dataset("HYP_CORE")
run <- generator_run(c("stats", "jsonlite"))
source_ref <- "R 4.6.1 stats package documentation"
citation <- "R Core Team, stats package help: t.test, var.test, aov, binom.test, chisq.test, fisher.test, wilcox.test, kruskal.test."
tol <- function(value, label) {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R stats", label, "from re-read HYP_CORE CSV."))
}
prob <- function(value, label) {
list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = "1e-8",
rationale = paste("R stats", label, "from re-read HYP_CORE CSV."))
}
emit <- function(id, title, kind, configuration, outputs, notes, excluded = list(),
error = NULL, repetitions = 1L) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = error, deferred_outputs = list(), generator_run = run)
write_expected("hypothesis", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis",
entry_point = "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
menu_items = list("hypothesisTestsMenuItem"), title = title,
case_kind = kind,
dataset = list(path = "reference_data/datasets/HYP_CORE.json", sha256 = ""),
excluded_rows = excluded, configuration = configuration, "repeat" = repetitions,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/hypothesis_tests.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes, "Expected computed from the exported CSV.",
"docs/manual/chapters/07-models-and-tests.tex:827-828: 'Tests a population mean against the declared target.'",
"docs/manual/chapters/07-models-and-tests.tex:855: 'Results lead with the estimate, confidence interval'"
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("hypothesis", id, case)
}
for (alternative in c("two-sided", "less", "greater")) {
index <- match(alternative, c("two-sided", "less", "greater"))
r_alternative <- if (alternative == "two-sided") "two.sided" else alternative
result <- stats::t.test(d$x, mu = 6.2, alternative = r_alternative, conf.level = 0.95)
outputs <- list("/statistic" = tol(result$statistic, "one-sample t"),
"/p_value" = prob(result$p.value, "one-sample t tail"),
"/estimate" = tol(mean(d$x) - 6.2, "mean minus target"),
"/df" = tol(result$parameter, "degrees of freedom"))
# The displayed estimate is mean - target, so centre R's mean CI likewise.
if (is.finite(result$conf.int[[1]])) outputs[["/ci_lower"]] <- tol(result$conf.int[[1]] - 6.2, "CI lower for mean difference")
if (is.finite(result$conf.int[[2]])) outputs[["/ci_upper"]] <- tol(result$conf.int[[2]] - 6.2, "CI upper for mean difference")
emit(sprintf("STAT-HYP-ONE-T-%03d", index), paste("One-sample t", alternative),
if (index == 1) "nominal" else "axis_coverage",
list(test = "one_sample_t", response = "x", target = 6.2, alternative = alternative),
outputs, list("t.test one-sample; response minus target; directional CI follows the alternative."))
}
for (method in c("welch", "pooled")) {
result <- stats::t.test(x ~ g, data = d, var.equal = method == "pooled", conf.level = 0.95)
emit(sprintf("STAT-HYP-TWO-T-%03d", match(method, c("welch", "pooled"))),
paste("Independent", method, "t"), "axis_coverage",
list(test = "two_sample_t", response = "x", group = "g", method = method),
list("/statistic" = tol(result$statistic, method),
"/df" = tol(result$parameter, method),
"/p_value" = prob(result$p.value, method),
"/estimate" = tol(mean(d$x[d$g == "A"]) - mean(d$x[d$g == "B"]), method)),
list("R t.test uses var.equal=FALSE for Welch and TRUE for pooled; A minus B."))
}
paired <- stats::t.test(d$x, d$y, paired = TRUE)
emit("STAT-HYP-PAIRED-T-001", "Paired differences", "nominal",
list(test = "paired_t", response = "x", second_response = "y"),
list("/statistic" = tol(paired$statistic, "paired t"),
"/p_value" = prob(paired$p.value, "paired t"),
"/estimate" = tol(mean(d$x - d$y), "paired mean difference")),
list("Pair rows by exported CSV order; difference is x minus y."))
binomial <- stats::binom.test(sum(d$outcome == "yes"), length(d$outcome), p = 0.4)
emit("STAT-HYP-ONE-PROP-001", "Exact one-proportion test", "nominal",
list(test = "one_proportion", response = "outcome", success_value = "yes", target = 0.4),
list("/statistic" = tol(sum(d$outcome == "yes"), "success count"),
"/p_value" = prob(binomial$p.value, "binomial exact"),
"/estimate" = tol(unname(binomial$estimate), "success proportion"),
"/ci_lower" = tol(binomial$conf.int[[1]], "Clopper-Pearson lower"),
"/ci_upper" = tol(binomial$conf.int[[2]], "Clopper-Pearson upper")),
list("binom.test exact two-sided p and Clopper-Pearson interval."))
tab <- table(d$g, d$outcome == "yes")
fisher <- stats::fisher.test(tab)
emit("STAT-HYP-TWO-PROP-001", "Sparse two-proportion Fisher test", "nominal",
list(test = "two_proportion", response = "outcome", group = "g", success_value = "yes",
method = "fisher"),
list("/p_value" = prob(fisher$p.value, "Fisher exact two-sided"),
"/estimate" = tol(mean(d$outcome[d$g == "A"] == "yes") -
mean(d$outcome[d$g == "B"] == "yes"), "proportion difference")),
list("Fisher p-value from 2x2 table; the reported estimate is A minus B proportion."))
anova <- stats::aov(x ~ g, data = d)
anova_row <- summary(anova)[[1]][1, ]
emit("STAT-HYP-ONE-ANOVA-001", "One-way classic ANOVA", "nominal",
list(test = "one_way_anova", response = "x", group = "g", method = "classic", posthoc = FALSE),
list("/statistic" = tol(anova_row[["F value"]], "one-way ANOVA F"),
"/p_value" = prob(anova_row[["Pr(>F)"]], "one-way ANOVA F tail")),
list("Classic fixed-effects one-way ANOVA; posthoc disabled."))
variance <- stats::var.test(x ~ g, data = d)
emit("STAT-HYP-VARIANCE-F-001", "Two-group variance ratio", "axis_coverage",
list(test = "equal_variance", response = "x", group = "g", method = "f"),
list("/statistic" = tol(variance$statistic, "variance ratio F"),
"/p_value" = prob(variance$p.value, "variance ratio F tail"),
"/ci_lower" = tol(variance$conf.int[[1]], "variance ratio CI lower"),
"/ci_upper" = tol(variance$conf.int[[2]], "variance ratio CI upper")),
list("var.test compares A/B sample variances in sorted group order."))
bartlett <- stats::bartlett.test(x ~ g, data = d)
emit("STAT-HYP-VARIANCE-BARTLETT-001", "Bartlett equal variances", "axis_coverage",
list(test = "equal_variance", response = "x", group = "g", method = "bartlett"),
list("/statistic" = tol(bartlett$statistic, "Bartlett K2"),
"/p_value" = prob(bartlett$p.value, "Bartlett chi-square tail")),
list("Bartlett test uses sample variances and chi-square approximation."))
mann <- stats::wilcox.test(x ~ g, data = d, exact = TRUE, correct = FALSE)
emit("STAT-HYP-MANN-WHITNEY-001", "Two-sample exact rank test", "nominal",
list(test = "mann_whitney", response = "x", group = "g"),
list("/statistic" = tol(mann$statistic, "Mann-Whitney U"),
"/p_value" = prob(mann$p.value, "exact rank tail")),
list("No ties and n=8/group: R wilcox.test exact p. R W equals U for the first group."))
kruskal <- stats::kruskal.test(x ~ g, data = d)
emit("STAT-HYP-KRUSKAL-001", "Kruskal-Wallis rank test", "nominal",
list(test = "kruskal_wallis", response = "x", group = "g"),
list("/statistic" = tol(kruskal$statistic, "Kruskal-Wallis H"),
"/p_value" = prob(kruskal$p.value, "Kruskal-Wallis chi-square tail")),
list("R kruskal.test tie-corrected chi-square."))
cat("hypothesis core cases generated\n")
generators/r/hypothesis_thresholds.R
source("common.R", local = TRUE)
d <- read_dataset("HYP_CORE")
run <- generator_run(c("stats", "jsonlite"))
prob <- function(x, label) list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8",
rationale = paste("R Student tail near alpha:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
contains <- function(value, label) list(comparison = "contains", values = list(value),
rationale = label)
emit <- function(id, entry, menu, config, outputs, notes) {
write_expected("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("hypothesis", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "hypothesis", entry_point = entry, menu_items = list(menu),
title = paste("Decision threshold:", id), case_kind = "threshold",
dataset = list(path = "reference_data/datasets/HYP_CORE.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R Student t tails",
citation = "R stats::t.test, pt and qt documentation; TOST analytic one-sided t tests.",
generator = list(path = "generators/r/hypothesis_thresholds.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "All critical values and observed tails use the exported HYP_CORE CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
# The null target is placed on opposite sides of a non-default alpha=.1 cutoff.
alpha <- 0.1
mean_x <- mean(d$x)
se_x <- sd(d$x) / sqrt(nrow(d))
critical <- stats::qt(1 - alpha / 2, nrow(d) - 1)
for (side in c("reject", "retain")) {
factor <- if (side == "reject") 1.01 else 0.99
target <- mean_x - factor * critical * se_x
result <- stats::t.test(d$x, mu = target, conf.level = 1 - alpha)
stopifnot((result$p.value < alpha) == (side == "reject"))
phrase <- if (side == "reject") "provide evidence" else "do not provide sufficient evidence"
emit(paste0("STAT-HYP-THRESHOLD-TEST-", toupper(side), "-001"),
"tabelz.analysis.hypothesis_tests:run_hypothesis_test", "hypothesisTestsMenuItem",
list(test = "one_sample_t", response = "x", target = target, alpha = alpha),
list("/p_value" = prob(result$p.value, "two-sided one-sample t"),
"/conclusion" = contains(phrase, "R p-value lies on the specified side of alpha.")),
list("Null target is mean minus factor times the alpha=.1 critical standard error.",
paste("factor", factor, "puts R p", if (side == "reject") "below" else "above", "alpha")))
}
# Welch independent TOST: interval half-width factor 1.01 or .99 of the
# one-sided critical radius. Bounds are symmetric around the observed effect.
a <- d$x[d$g == "A"]
b <- d$x[d$g == "B"]
effect <- mean(a) - mean(b)
va <- var(a) / length(a)
vb <- var(b) / length(b)
se <- sqrt(va + vb)
df <- (va + vb)^2 / (va^2 / (length(a) - 1) + vb^2 / (length(b) - 1))
radius <- stats::qt(1 - alpha, df) * se
for (side in c("established", "not-established")) {
factor <- if (side == "established") 1.01 else 0.99
lower <- effect - factor * radius
upper <- effect + factor * radius
p_lower <- stats::pt((effect - lower) / se, df, lower.tail = FALSE)
p_upper <- stats::pt((effect - upper) / se, df)
decision <- p_lower < alpha && p_upper < alpha
stopifnot(decision == (side == "established"))
emit(paste0("STAT-HYP-THRESHOLD-EQUIV-", toupper(side), "-001"),
"tabelz.executor.equivalence", "equivalenceMenuItem",
list(response_column = "x", group_column = "g", group_a = "A", group_b = "B",
lower_bound = lower, upper_bound = upper, alpha = alpha),
list("/p_lower" = prob(p_lower, "lower Welch TOST"),
"/p_upper" = prob(p_upper, "upper Welch TOST"),
"/equivalent" = exact(decision, "Both one-sided R p-values must be below alpha.")),
list("Bounds straddle the 1-alpha Welch interval endpoints by 1% of its half-width."))
}
# One-sample TOST extension around the same non-default alpha cutoff.
delta <- d$x - 6
effect_one <- mean(delta)
se_one <- sd(delta) / sqrt(length(delta))
radius_one <- stats::qt(1 - alpha, length(delta) - 1) * se_one
for (side in c("established", "not-established")) {
factor <- if (side == "established") 1.01 else 0.99
lower <- effect_one - factor * radius_one
upper <- effect_one + factor * radius_one
p_lower <- stats::pt((effect_one - lower) / se_one, length(delta) - 1,
lower.tail = FALSE)
p_upper <- stats::pt((effect_one - upper) / se_one, length(delta) - 1)
decision <- p_lower < alpha && p_upper < alpha
stopifnot(decision == (side == "established"))
emit(paste0("STAT-HYP-THRESHOLD-EQUIVEXT-", toupper(side), "-001"),
"tabelz.executor.equivalence_extensions", "equivalenceMenuItem",
list(mode = "one_sample_difference", response_column = "x", target = 6,
lower_bound = lower, upper_bound = upper, alpha = alpha),
list("/p_lower" = prob(p_lower, "lower one-sample TOST"),
"/p_upper" = prob(p_upper, "upper one-sample TOST"),
"/equivalent" = exact(decision, "Both one-sided R p-values must be below alpha.")),
list("Bounds straddle the 1-alpha one-sample t interval endpoints by 1%."))
}
cat("hypothesis threshold cases generated\n")
generators/r/measurement_attribute.R
source("common.R", local = TRUE)
# References for attribute measurement studies (manual chapter 10):
# tabelz.analysis.attribute_agreement:compute_attribute_agreement (lines 270-317)
# tabelz.analysis.attribute_gage_response:compute_attribute_gage_response (lines 323-415)
# Agreement counting units are the literal reading of lines 299-304: within an
# appraiser, an item agrees when all of that appraiser's trials agree; between
# appraisers, an item-and-trial agrees when all appraisers agree; versus the
# standard, each rating is compared with the item's standard. Intervals are
# Wilson 95% (prop.test, correct = FALSE); kappas come from irr (Cohen, with
# equal or squared weights for the ordinal scales, and Fleiss for > 2 raters).
# Gage response: grouped binomial probit MLE, Fieller intervals for
# x_p = (qnorm(p) - b0) / b1 using the normal quantile at the confidence level,
# spread and probit scale by inverting the Wald slope interval, Pearson
# chi-square with k - 2 df.
run <- generator_run(c("stats", "irr", "jsonlite"))
response_run <- generator_run(c("stats", "irr", "jsonlite", "twopartm"))
family <- "measurement"
generator <- "generators/r/measurement_attribute.R"
agree_entry <- "tabelz.analysis.attribute_agreement:compute_attribute_agreement"
resp_entry <- "tabelz.analysis.attribute_gage_response:compute_attribute_gage_response"
tol <- function(value, why, tier = "1e-9") {
if (any(!is.finite(value))) stop("non-finite reference output: ", why)
list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste(if (tier == "1e-9") "Closed-form reference; owner 1e-9 tier." else "Iterative probit fit; owner 1e-6 tier.", why))
}
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(id, entry, menu, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(), error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(),
generator_run = if (identical(entry, resp_entry)) response_run else run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = if (identical(entry, resp_entry)) "R stats::glm probit and twopartm::FiellerRatio 0.1.0 for bounded Fieller outputs" else "R irr::kappa2, irr::kappam.fleiss and stats::prop.test",
citation = if (identical(entry, resp_entry)) "R stats::glm documentation; twopartm 0.1.0 FiellerRatio; Franz (2007), equations (3)-(5)" else "R irr::kappa2, irr::kappam.fleiss and stats::prop.test documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes,
if (identical(entry, resp_entry)) "twopartm supplies only bounded Fieller ratio limits; GLM estimates, row policy, spread and diagnostics have separate R provenance." else NULL,
"All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-MSA-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
wilson <- function(x, n) prop.test(x, n, correct = FALSE)$conf.int
# ---- attribute agreement ----------------------------------------------------------------
set.seed(20261024)
items <- sprintf("I%02d", 1:12); appr <- c("Ann", "Bob", "Cy"); std <- sample(c("G", "NG"), 12, replace = TRUE)
flip <- c(Ann = 0.08, Bob = 0.18, Cy = 0.3)
rows <- expand.grid(trial = c("T1", "T2"), appraiser = appr, item = items, stringsAsFactors = FALSE)
rows$standard <- std[match(rows$item, items)]
rows$rating <- ifelse(runif(nrow(rows)) < flip[rows$appraiser], ifelse(rows$standard == "G", "NG", "G"), rows$standard)
export_dataset(rows[c("item", "appraiser", "trial", "rating", "standard")], "MSA_AGREE",
"Constructed attribute study: 12 items x 3 appraisers x 2 trials with a known standard, seed 20261024",
"R stats::runif documentation", "Constructed validation data; no third-party license.",
c(item = "String", appraiser = "String", trial = "String", rating = "String", standard = "String"))
a <- read_dataset("MSA_AGREE")
rating_of <- function(ap, tr) a$rating[a$appraiser == ap & a$trial == tr][order(a$item[a$appraiser == ap & a$trial == tr])]
out <- list()
for (k in seq_along(appr)) {
ap <- appr[k]; r1 <- rating_of(ap, "T1"); r2 <- rating_of(ap, "T2")
agree <- sum(r1 == r2); ci <- wilson(agree, 12)
node <- sprintf("/appraisers/[appraiser=%s]", ap)
out[[paste0(node, "/within/agreement_pct")]] <- tol(100 * agree / 12, "Items whose two trials agree.")
out[[paste0(node, "/within/ci_lower")]] <- tol(100 * ci[1], "Wilson lower limit, percent.")
out[[paste0(node, "/within/ci_upper")]] <- tol(100 * ci[2], "Wilson upper limit, percent.")
out[[paste0(node, "/within/kappa")]] <- tol(irr::kappa2(cbind(r1, r2))$value, "Cohen kappa between the two trials.")
sub <- a[a$appraiser == ap, ]
hit <- sum(sub$rating == sub$standard)
out[[paste0(node, "/versus_standard/agreement_pct")]] <- tol(100 * hit / nrow(sub), "Ratings equal to the standard.")
}
ratings <- sapply(appr, function(ap) c(rating_of(ap, "T1"), rating_of(ap, "T2")))
between <- sum(apply(ratings, 1, function(v) length(unique(v)) == 1))
out[["/between/agreement_pct"]] <- tol(100 * between / 24, "Item-and-trial pairs where all appraisers agree.")
out[["/between/kappa"]] <- tol(irr::kappam.fleiss(ratings)$value, "Fleiss kappa over three appraisers.")
out[["/n_trials"]] <- exact(2L, "Two trials.")
emit("STAT-MSA-AGREE-001", agree_entry, "attributeAgreementMenuItem", "Three appraisers, two trials and a known standard", "MSA_AGREE",
list(rating = "rating", part = "item", appraiser = "appraiser", trial = "trial", standard = "standard"), out,
c("Nominal scale (line 288); Fleiss kappa for more than two appraisers (line 302)."))
two <- read_dataset("MSA_AGREE"); two <- two[two$appraiser != "Cy", ]
export_dataset(two, "MSA_AGREE_TWO", "MSA_AGREE with appraisers Ann and Bob only", "R stats::runif documentation",
"Constructed validation data; no third-party license.",
c(item = "String", appraiser = "String", trial = "String", rating = "String", standard = "String"))
a <- read_dataset("MSA_AGREE_TWO")
ratings2 <- sapply(c("Ann", "Bob"), function(ap) c(rating_of(ap, "T1"), rating_of(ap, "T2")))
emit("STAT-MSA-AGREE-002", agree_entry, "attributeAgreementMenuItem", "Two appraisers use Cohen kappa between them", "MSA_AGREE_TWO",
list(rating = "rating", part = "item", appraiser = "appraiser", trial = "trial"),
list("/between/kappa" = tol(irr::kappa2(ratings2)$value, "Cohen kappa for two appraisers (line 302)."),
"/between/agreement_pct" = tol(100 * mean(ratings2[, 1] == ratings2[, 2]), "Item-and-trial agreement."),
"/overall_standard" = absent("No standard was assigned.")),
c("Two appraisers."), kind = "axis_coverage")
set.seed(20261025)
truth <- sample(1:3, 15, replace = TRUE)
ord <- expand.grid(appraiser = c("P", "Q"), item = sprintf("S%02d", 1:15), stringsAsFactors = FALSE)
ord$rating <- as.character(pmin(3, pmax(1, truth[match(ord$item, sprintf("S%02d", 1:15))] + sample(c(-1, 0, 0, 0, 1), nrow(ord), replace = TRUE))))
export_dataset(ord[c("item", "appraiser", "rating")], "MSA_AGREE_ORDINAL", "Constructed ordinal ratings 1-3 by two appraisers, one trial, seed 20261025",
"R base::sample documentation", "Constructed validation data; no third-party license.",
c(item = "String", appraiser = "String", rating = "String"))
o <- read_dataset("MSA_AGREE_ORDINAL")
pq <- cbind(o$rating[o$appraiser == "P"][order(o$item[o$appraiser == "P"])], o$rating[o$appraiser == "Q"][order(o$item[o$appraiser == "Q"])])
for (spec in list(c("ordinal_linear", "equal", "003"), c("ordinal_quadratic", "squared", "004"))) {
emit(paste0("STAT-MSA-AGREE-", spec[3]), agree_entry, "attributeAgreementMenuItem", paste("Weighted Cohen kappa:", spec[1]), "MSA_AGREE_ORDINAL",
list(rating = "rating", part = "item", appraiser = "appraiser", scale = spec[1]),
list("/between/kappa" = tol(irr::kappa2(pq, weight = spec[2])$value, sprintf("irr::kappa2 with weight = '%s'.", spec[2]))),
c("Numeric-looking categories are ordered numerically (line 309-310); one trial."), kind = "axis_coverage")
}
emit("STAT-MSA-AGREE-005", agree_entry, "attributeAgreementMenuItem", "Repeated columns are refused", "MSA_AGREE",
list(rating = "rating", part = "item", appraiser = "item"), list(),
c("Line 293: all assigned columns must be distinct."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "must use a different column"))
# ---- attribute gage response -----------------------------------------------------------------
set.seed(20261026)
ref <- seq(9.80, 10.20, by = 0.04)
trials <- rep(20L, length(ref))
passes <- rbinom(length(ref), trials, pnorm((ref - 10.01) / 0.07))
export_dataset(data.frame(reference = ref, passes = as.integer(passes), trials = trials), "MSA_GAGE_RESPONSE",
"Constructed: 11 reference values x 20 trials, probit centre 10.01 and scale 0.07, seed 20261026",
"R stats::rbinom documentation", "Constructed validation data; no third-party license.",
c(reference = "Float64", passes = "Int64", trials = "Int64"))
g <- read_dataset("MSA_GAGE_RESPONSE")
probit_ref <- function(x, y, n, level = 0.95, lower = 0.05, upper = 0.95) {
fit <- glm(cbind(y, n - y) ~ x, family = binomial(link = "probit"), control = glm.control(epsilon = 1e-14, maxit = 100))
b <- coef(fit); V <- vcov(fit); z <- qnorm(0.5 + level / 2)
point <- function(p) {
q <- qnorm(p); est <- (q - b[1]) / b[2]
# Solve (b0 + b1 x - q)^2 = z^2 (V00 + 2 x V01 + x^2 V11) for x.
a2 <- b[2]^2 - z^2 * V[2, 2]; a1 <- 2 * (b[1] - q) * b[2] - 2 * z^2 * V[1, 2]; a0 <- (b[1] - q)^2 - z^2 * V[1, 1]
if (a2 <= 0) return(list(est = unname(est), lo = NA, hi = NA, bounded = FALSE))
roots <- sort(c((-a1 - sqrt(a1^2 - 4 * a2 * a0)) / (2 * a2), (-a1 + sqrt(a1^2 - 4 * a2 * a0)) / (2 * a2)))
# twopartm's xest/yest ordering is numerator/denominator; Cov(q-b0,b1)=-Cov(b0,b1).
ratio_cov <- matrix(c(V[1, 1], -V[1, 2], -V[1, 2], V[2, 2]), nrow = 2)
package_limits <- twopartm::FiellerRatio(unname(q - b[1]), unname(b[2]),
ratio_cov, alpha = 1 - level)
if (max(abs(unname(package_limits) - c(est, roots))) > 1e-8)
stop("twopartm Fieller ratio disagrees with the independent quadratic")
list(est = unname(est), lo = unname(package_limits["min"]),
hi = unname(package_limits["max"]), bounded = TRUE)
}
width <- qnorm(upper) - qnorm(lower); se1 <- sqrt(V[2, 2])
mu <- fitted(fit); pearson <- sum((y - n * mu)^2 / (n * mu * (1 - mu)))
list(b = b, V = V, mid = point(0.5), low = point(lower), high = point(upper),
spread = width / abs(b[2]), spread_lo = width / (abs(b[2]) + z * se1),
spread_hi = if (abs(b[2]) > z * se1) width / (abs(b[2]) - z * se1) else NA,
scale = 1 / abs(b[2]), pearson = pearson, gof_p = pchisq(pearson, length(x) - 2, lower.tail = FALSE))
}
pr <- probit_ref(g$reference, g$passes, g$trials)
if (!pr$mid$bounded) stop("expected a bounded Fieller interval")
resp_outputs <- function(pr, threshold) list(
"/coefficients/1/estimate" = tol(pr$b[2], "Probit slope on the reference scale.", "1e-6"),
"/midpoint/estimate" = tol(pr$mid$est, "x_0.5 = -b0 / b1.", "1e-6"),
"/midpoint/lower" = tol(pr$mid$lo, "Fieller lower limit.", "1e-6"),
"/midpoint/upper" = tol(pr$mid$hi, "Fieller upper limit.", "1e-6"),
"/lower_point/estimate" = tol(pr$low$est, "x_0.05.", "1e-6"),
"/upper_point/estimate" = tol(pr$high$est, "x_0.95.", "1e-6"),
# The threshold is exact, so the bias carries x_0.5's error unchanged: its
# tolerance is x_0.5's 1e-6 tier as an absolute bound, not 1e-6 of the small
# difference (until 2026-09-25 the relative tier was applied to the difference).
"/bias" = list(comparison = "tolerance", value = dec(unname(pr$mid$est - threshold)),
absolute_tolerance = dec(1e-6 * abs(pr$mid$est)), relative_tolerance = "0",
rationale = "Iterative probit fit; owner 1e-6 tier on x_0.5, propagated to x_0.5 minus the exact threshold."),
"/spread" = tol(pr$spread, "(qnorm(0.95) - qnorm(0.05)) / |b1|.", "1e-6"),
"/spread_lower" = tol(pr$spread_lo, "Inverted Wald slope interval.", "1e-6"),
"/probit_scale" = tol(pr$scale, "1 / |b1|.", "1e-6"),
"/pearson_chi_square" = tol(pr$pearson, "Pearson chi-square over the pooled levels.", "1e-6"),
"/goodness_of_fit_df" = exact(9L, "k - 2 = 11 - 2."))
emit("STAT-MSA-RESP-001", resp_entry, "attributeGageResponseMenuItem", "Probit gage response with Fieller intervals", "MSA_GAGE_RESPONSE",
list(reference_column = "reference", pass_count_column = "passes", trials_column = "trials", threshold = 10),
resp_outputs(pr, 10), c("glm(binomial(link = 'probit')) with epsilon 1e-14; normal quantile in the Fieller equation."))
prf <- probit_ref(g$reference, g$trials - g$passes, g$trials)
emit("STAT-MSA-RESP-002", resp_entry, "attributeGageResponseMenuItem", "Modelling the fail event reverses the curve", "MSA_GAGE_RESPONSE",
list(reference_column = "reference", pass_count_column = "passes", trials_column = "trials", threshold = 10, modeled_event = "fail"),
list("/midpoint/estimate" = tol(prf$mid$est, "The 50% point is unchanged (line 364-365).", "1e-6"),
"/spread" = tol(prf$spread, "The spread is unchanged.", "1e-6"),
"/direction" = exact("decreasing", "The fail probability falls with the reference.")),
c("The fail count n - y is modelled."), kind = "axis_coverage")
pr90 <- probit_ref(g$reference, g$passes, g$trials, level = 0.9, lower = 0.1, upper = 0.9)
emit("STAT-MSA-RESP-003", resp_entry, "attributeGageResponseMenuItem", "10/90 transition points at 90% confidence", "MSA_GAGE_RESPONSE",
list(reference_column = "reference", pass_count_column = "passes", trials_column = "trials", threshold = 10,
lower_percentile = 0.1, upper_percentile = 0.9, confidence_level = 0.9),
list("/lower_point/estimate" = tol(pr90$low$est, "x_0.10.", "1e-6"), "/spread" = tol(pr90$spread, "Spread between 10% and 90%.", "1e-6"),
"/midpoint/lower" = tol(pr90$mid$lo, "90% Fieller limit.", "1e-6")),
c("Non-default transition points and confidence."), kind = "axis_coverage")
sep <- data.frame(reference = c(1, 2, 3, 4), passes = c(0L, 0L, 10L, 10L), trials = rep(10L, 4))
export_dataset(sep, "MSA_GAGE_RESPONSE_SEPARATED", "Constructed: outcomes separated along the reference axis", "Hand-constructed",
"Constructed validation data; no third-party license.", c(reference = "Float64", passes = "Int64", trials = "Int64"))
emit("STAT-MSA-RESP-004", resp_entry, "attributeGageResponseMenuItem", "Separated outcomes have no finite estimate", "MSA_GAGE_RESPONSE_SEPARATED",
list(reference_column = "reference", pass_count_column = "passes", trials_column = "trials", threshold = 2.5), list(),
c("Line 367-371: separated outcomes are an error."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "separat"))
generators/r/measurement_deming.R
source("common.R", local = TRUE)
# References for tabelz.executor.deming_regression (manual chapter 39 lines 50-82):
# lambda = Var(error Y) / Var(error X); D = Syy - lambda Sxx;
# b = (D + sqrt(D^2 + 4 lambda Sxy^2)) / (2 Sxy); a = ybar - b xbar;
# xhat_i = (lambda x_i + b (y_i - a)) / (lambda + b^2); yhat_i = a + b xhat_i.
# Bootstrap intervals are seeded resampling and are not reproduced here (V27).
run_base <- generator_run(c("stats", "jsonlite"))
run_mcr <- generator_run(c("stats", "jsonlite", "mcr"))
family <- "measurement"
generator <- "generators/r/measurement_deming.R"
entry <- "tabelz.executor.deming_regression"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form Deming estimate; owner 1e-9 tier.", why))
mcr_tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("mcr 1.3.3.1 independent Deming/Linnet implementation; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(id, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(), error = NULL,
oracle = "analytic") {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(),
generator_run = if (oracle == "mcr") run_mcr else run_base))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list("demingRegressionMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = if (oracle == "mcr") "independent_implementation" else if (kind == "invalid_input") "property" else "analytic",
source = if (oracle == "mcr") "mcr 1.3.3.1 Deming point estimates and Linnet jackknife intervals; independent R arithmetic for other fields" else "R closed-form Deming and specified policy checks",
citation = if (oracle == "mcr") "mcr 1.3.3.1 mcreg documentation; NCSS Deming Regression chapter 303, Program 1; NIST Dataplot reproduces that NCSS example in case 007" else "NCSS Deming Regression chapter 303; Tabelz manual chapter 39 for policy",
generator = list(path = generator, sha256 = ""), independent = oracle == "mcr",
convention_notes = as.list(c(notes, "All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-MSA-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
deming <- function(x, y, lambda) {
sxx <- sum((x - mean(x))^2); syy <- sum((y - mean(y))^2); sxy <- sum((x - mean(x)) * (y - mean(y)))
D <- syy - lambda * sxx; b <- (D + sqrt(D^2 + 4 * lambda * sxy^2)) / (2 * sxy); a <- mean(y) - b * mean(x)
xhat <- (lambda * x + b * (y - a)) / (lambda + b^2)
list(b = b, a = a, xhat = xhat, yhat = a + b * xhat, sxx = sxx, syy = syy, sxy = sxy)
}
mcr_fit <- function(x, y, lambda, jackknife = FALSE, confidence = 0.95) {
if (length(x) != length(y) || length(x) < 4L || anyNA(x) || anyNA(y))
stop("mcr oracle needs at least four matched complete pairs")
fit <- mcr::mcreg(x, y, method.reg = "Deming",
method.ci = if (jackknife) "jackknife" else "analytical",
alpha = 1 - confidence, error.ratio = 1 / lambda)
coef <- mcr::getCoefficients(fit)
if (!identical(rownames(coef), c("Intercept", "Slope")) ||
!all(c("EST", "SE", "LCI", "UCI") %in% colnames(coef)))
stop("Unexpected mcr coefficient layout")
coef
}
mcr_point <- function(coef, name) mcr_tol(coef[name, "EST"], paste(name, "point estimate; error.ratio = 1/lambda."))
outputs <- function(r, x, y, rows) {
out <- list("/slope" = tol(r$b, "b from D, Sxy and lambda."), "/intercept" = tol(r$a, "a = ybar - b xbar."),
"/sxy" = tol(r$sxy, "Centered cross product."))
for (i in rows) {
node <- sprintf("/rows/%d", i)
out[[paste0(node, "/xhat")]] <- tol(r$xhat[i + 1], "Latent x projection.")
out[[paste0(node, "/y_residual")]] <- tol(y[i + 1] - r$yhat[i + 1], "y - yhat.")
}
out
}
set.seed(20261027)
truth <- runif(40, 5, 25)
x <- round(truth + rnorm(40, 0, 0.4), 4); y <- round(0.3 + 1.04 * truth + rnorm(40, 0, 0.6), 4)
export_dataset(data.frame(method_a = x, method_b = y), "MSA_DEMING", "Constructed method comparison: 40 pairs, X error SD 0.4, Y error SD 0.6, seed 20261027",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(method_a = "Float64", method_b = "Float64"))
d <- read_dataset("MSA_DEMING")
fit1 <- mcr_fit(d$method_a, d$method_b, 1)
out1 <- outputs(deming(d$method_a, d$method_b, 1), d$method_a, d$method_b, c(0L, 17L, 39L))
out1[["/slope"]] <- mcr_point(fit1, "Slope"); out1[["/intercept"]] <- mcr_point(fit1, "Intercept")
emit("STAT-MSA-DEMING-001", "Deming regression with lambda 1", "MSA_DEMING", list(x_column = "method_a", y_column = "method_b"),
out1, c("Orthogonal regression at lambda = 1.", "mcr supplies slope/intercept; closed-form R supplies Sxy and latent row outputs."), oracle = "mcr")
fit2 <- mcr_fit(d$method_a, d$method_b, 2.25)
out2 <- outputs(deming(d$method_a, d$method_b, 2.25), d$method_a, d$method_b, c(0L, 39L))
out2[["/slope"]] <- mcr_point(fit2, "Slope"); out2[["/intercept"]] <- mcr_point(fit2, "Intercept")
emit("STAT-MSA-DEMING-002", "Deming regression with lambda 2.25", "MSA_DEMING",
list(x_column = "method_a", y_column = "method_b", lambda_ratio = 2.25, comparable_units = TRUE),
out2,
c("lambda = 0.6^2 / 0.4^2 = 2.25; mcr error.ratio = 1/2.25.",
"mcr supplies slope/intercept; closed-form R supplies Sxy and latent row outputs."),
kind = "axis_coverage", oracle = "mcr")
inv <- deming(d$method_b, d$method_a, 1 / 2.25)
fit3 <- mcr_fit(d$method_b, d$method_a, 1 / 2.25)
emit("STAT-MSA-DEMING-003", "Swapping X and Y with 1/lambda gives the inverse line", "MSA_DEMING",
list(x_column = "method_b", y_column = "method_a", lambda_ratio = 1 / 2.25),
list("/slope" = mcr_point(fit3, "Slope"), "/intercept" = mcr_point(fit3, "Intercept")),
c(sprintf("R: 1 / b(lambda 2.25) = %.15g and b(swapped, 1/2.25) = %.15g.", 1 / deming(d$method_a, d$method_b, 2.25)$b, inv$b)),
kind = "axis_coverage", oracle = "mcr")
m <- read_dataset("MSA_DEMING"); m$method_b[c(5, 30)] <- NA
export_dataset(m, "MSA_DEMING_MISSING", "MSA_DEMING with method_b missing in rows 5 and 30 (1-based)", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(method_a = "Float64", method_b = "Float64"))
mm <- read_dataset("MSA_DEMING_MISSING")
excluded <- c(0L, 20L)
keep <- setdiff(which(!is.na(mm$method_b)), excluded + 1L)
r <- deming(mm$method_a[keep], mm$method_b[keep], 1)
fit4 <- mcr_fit(mm$method_a[keep], mm$method_b[keep], 1)
emit("STAT-MSA-DEMING-004", "Missing and excluded pairs keep their rows without fitted values", "MSA_DEMING_MISSING",
list(x_column = "method_a", y_column = "method_b"),
list("/slope" = mcr_point(fit4, "Slope"),
"/rows/0/xhat" = absent("An excluded row keeps its identity with fitted fields unavailable (line 81-82)."),
"/rows/4/xhat" = absent("A missing pair has no projection."),
"/rows/1/xhat" = tol(r$xhat[1], "The first retained pair."),
"/excluded_count" = exact(2L, "Two excluded rows."), "/missing_count" = exact(2L, "Two missing pairs.")),
c("Rows 0 and 20 (0-based) are excluded; mcr fits the 36 matched eligible pairs only.",
"Row identities, exclusions and latent projection come from the Tabelz policy and closed form, not mcr."),
kind = "missing_excluded", excluded = excluded, oracle = "mcr")
h <- read_dataset("MSA_DEMING"); h$method_a <- h$method_a + 1e6; h$method_b <- h$method_b + 1e6
export_dataset(h, "MSA_DEMING_OFFSET", "MSA_DEMING with both methods shifted by 1e6", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(method_a = "Float64", method_b = "Float64"))
ho <- read_dataset("MSA_DEMING_OFFSET")
emit("STAT-MSA-DEMING-005", "A 1e6 offset in both methods", "MSA_DEMING_OFFSET", list(x_column = "method_a", y_column = "method_b"),
list("/slope" = tol(deming(ho$method_a, ho$method_b, 1)$b, "Centered sums make the slope offset-invariant.")),
c("R centres before forming the sums."), kind = "hard_numeric")
export_dataset(data.frame(method_a = c(1, 2), method_b = c(1.1, 2.3)), "MSA_DEMING_TWO", "Constructed: two pairs", "Hand-constructed",
"Constructed validation data; no third-party license.", c(method_a = "Float64", method_b = "Float64"))
emit("STAT-MSA-DEMING-006", "Two pairs are too few", "MSA_DEMING_TWO", list(x_column = "method_a", y_column = "method_b"), list(),
c("Line 68: at least three complete pairs."), kind = "invalid_input",
error = list(error_class = "DemingRegressionExecutionError", message_contains = "three"))
# ---- Jackknife t intervals (statistician ruling, issue #15) ------------------------------------
# mcr implements Linnet's leave-one-out SE and full-estimate-centred t interval.
# Its error.ratio is X-error/Y-error, so pass 1/lambda_Tabelz.
jk_outputs <- function(coef, n) list(
"/slope" = mcr_point(coef, "Slope"),
"/intercept" = mcr_point(coef, "Intercept"),
"/interval_method" = exact("jackknife_t", "The configured interval method."),
"/jackknife_degrees_of_freedom" = exact(n - 2L, "Tabelz/CLSI n - 2 interval convention; mcr uses the same degrees of freedom."),
"/jackknife_slope_se" = mcr_tol(coef["Slope", "SE"], "Linnet leave-one-out slope standard error."),
"/jackknife_slope_interval/0" = mcr_tol(coef["Slope", "LCI"], "Slope lower bound, centred on full-data estimate."),
"/jackknife_slope_interval/1" = mcr_tol(coef["Slope", "UCI"], "Slope upper bound."),
"/jackknife_intercept_se" = mcr_tol(coef["Intercept", "SE"], "Linnet leave-one-out intercept standard error."),
"/jackknife_intercept_interval/0" = mcr_tol(coef["Intercept", "LCI"], "Intercept lower bound."),
"/jackknife_intercept_interval/1" = mcr_tol(coef["Intercept", "UCI"], "Intercept upper bound."),
"/bootstrap_slope_interval" = absent("Jackknife bounds never appear in the bootstrap fields."))
export_dataset(data.frame(method_a = c(7, 8.3, 10.5, 9, 5.1, 8.2, 10.2, 10.3, 7.1, 5.9),
method_b = c(7.9, 8.2, 9.6, 9, 6.5, 7.3, 10.2, 10.6, 6.3, 5.2)),
"MSA_DEMING_NCSS", "NCSS Deming Regression Program 1: ten pairs; reproduced by NIST Dataplot",
"NCSS Deming Regression chapter 303, Program 1; https://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/demfit.htm",
"Small numeric example transcribed for validation; NCSS source and NIST reproduction credited.", c(method_a = "Float64", method_b = "Float64"))
dn <- read_dataset("MSA_DEMING_NCSS")
jn <- mcr_fit(dn$method_a, dn$method_b, 0.25, jackknife = TRUE)
stopifnot(abs(jn["Slope", "EST"] - 1.00119) < 5.1e-6,
abs(jn["Slope", "SE"] - 0.18718) < 5.1e-6,
abs(jn["Intercept", "SE"] - 1.72199) < 5.1e-6)
emit("STAT-MSA-DEMING-007", "Jackknife t intervals on the NCSS ten-pair example", "MSA_DEMING_NCSS",
list(x_column = "method_a", y_column = "method_b", lambda_ratio = 0.25, interval_method = "jackknife_t"),
jk_outputs(jn, nrow(dn)),
c("NCSS-origin example reproduced by NIST Dataplot; delta = Var(e_x)/Var(e_y) = 4 corresponds to lambda = 0.25 here.",
"mcr error.ratio = 4; its slope and Linnet jackknife SEs agree with NIST's reproduced five-decimal values within 5.1e-6.",
"Interval method, degrees of freedom and bootstrap-field absence are Tabelz contract checks."), oracle = "mcr")
jm <- mcr_fit(d$method_a, d$method_b, 2.25, jackknife = TRUE)
emit("STAT-MSA-DEMING-008", "Jackknife t intervals with lambda 2.25 on 40 pairs", "MSA_DEMING",
list(x_column = "method_a", y_column = "method_b", lambda_ratio = 2.25, interval_method = "jackknife_t"),
jk_outputs(jm, nrow(d)), c("mcr error.ratio = 1/2.25 on 40 matched pairs; 38 degrees of freedom.",
"Interval method, degrees of freedom and bootstrap-field absence are Tabelz contract checks."),
oracle = "mcr")
export_dataset(data.frame(method_a = c(1, 2, 3.5), method_b = c(1.1, 2.3, 3.2)), "MSA_DEMING_THREE", "Constructed: three pairs",
"Hand-constructed", "Constructed validation data; no third-party license.", c(method_a = "Float64", method_b = "Float64"))
d3 <- read_dataset("MSA_DEMING_THREE")
emit("STAT-MSA-DEMING-009", "Three pairs keep the fit and withhold the jackknife interval", "MSA_DEMING_THREE",
list(x_column = "method_a", y_column = "method_b", interval_method = "jackknife_t"),
list("/slope" = tol(deming(d3$method_a, d3$method_b, 1)$b, "The point fit needs only three pairs."),
"/jackknife_slope_interval" = absent("Jackknife intervals need at least four pairs."),
"/interval_unavailable_reason" = list(comparison = "contains", values = list("at least 4"),
rationale = "The withheld interval states its reason.")),
c("Issue #15 ruling: at least four eligible pairs for leave-one-out intervals; three-pair point fits remain."),
kind = "boundary")
generators/r/measurement_expanded.R
source("common.R", local = TRUE)
# References for tabelz.analysis.gage_expanded:compute_gage_expanded (manual
# chapter 10 lines 167-199). The classic crossed structure delegates to the
# crossed Gage R&R engine, so its reference is the balanced crossed ANOVA with
# the interaction pooled at alpha 0.05. Other structures fit REML variance
# components; repeatability is the residual, reproducibility sums the operator
# components, crossed site components form "Site (measurement system)" and count
# toward Gage R&R, and site-within-part counts toward part-to-part (lines 187-192).
# REML references use lme4::lmer (REML = TRUE, bobyqa, tight tolerances): the
# owner's 1e-6 tier for iterative fits.
run <- generator_run(c("stats", "lme4", "jsonlite"))
family <- "measurement"
generator <- "generators/r/measurement_expanded.R"
entry <- "tabelz.analysis.gage_expanded:compute_gage_expanded"
tol <- function(value, why, tier = "1e-6") list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste(if (tier == "1e-9") "Closed-form ANOVA; owner 1e-9 tier." else "REML fit; owner 1e-6 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, title, dataset, config, outputs, notes, kind = "nominal", error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list("gageExpandedMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats::anova and lme4::lmer REML",
citation = "R stats::anova.lm and lme4::lmer documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-MSA-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
component <- function(source, v, why, tier = "1e-6") {
out <- list(); out[[sprintf("/components/[source=%s]/variance", source)]] <- tol(v, why, tier); out
}
reml <- function(formula, data) {
fit <- lme4::lmer(formula, data = data, REML = TRUE,
control = lme4::lmerControl(optimizer = "bobyqa", optCtrl = list(rhobeg = 0.2, rhoend = 1e-12, maxfun = 1e6),
calc.derivs = FALSE))
vc <- as.data.frame(lme4::VarCorr(fit))
setNames(vc$vcov, vc$grp)
}
# ---- classic crossed ---------------------------------------------------------------------
g <- read_dataset("MSA_GRR_POOLED")
g$part <- factor(g$part); g$operator <- factor(g$operator)
full <- anova(lm(y ~ part * operator, data = g)); ms <- full[["Mean Sq"]]; df <- full[["Df"]]; ss <- full[["Sum Sq"]]
p_int <- pf(ms[3] / ms[4], df[3], df[4], lower.tail = FALSE); if (p_int < 0.05) stop("expected pooling")
ms_err <- (ss[3] + ss[4]) / (df[3] + df[4])
v_op <- max(0, (ms[2] - ms_err) / 30); v_part <- max(0, (ms[1] - ms_err) / 9)
emit("STAT-MSA-EXPANDED-001", "Classic crossed structure reproduces the crossed engine", "MSA_GRR_POOLED",
list(response_column = "y", part_column = "part", operator_column = "operator"),
c(component("Repeatability", ms_err, "Pooled error mean square.", "1e-9"),
component("Total Gage R&R", ms_err + v_op, "Repeatability + operator.", "1e-9"),
# The classic structure reproduces the crossed engine's components,
# labels included (tests/unit/analysis/test_gage_expanded.py), so the part
# component keeps the crossed spelling; until 2026-09-25 this used "Part-to-part".
component("Part-To-Part", v_part, "(MS_part - MS_error) / (o r).", "1e-9"),
list("/path" = exact("classic_crossed", "Delegated to the crossed engine (line 177-178)."))),
c("Dataset written by measurement_gage.R; 10 parts x 3 operators x 3 trials, interaction pooled."))
# ---- fully crossed with a site -------------------------------------------------------------
set.seed(20261028)
rows <- expand.grid(trial = 1:2, site = c("S1", "S2", "S3"), operator = c("A", "B", "C"), part = sprintf("P%d", 1:6), stringsAsFactors = FALSE)
pe <- rnorm(6, 0, 1.8); oe <- rnorm(3, 0, 0.5); se <- rnorm(3, 0, 0.4)
rows$y <- round(30 + pe[match(rows$part, sprintf("P%d", 1:6))] + oe[match(rows$operator, c("A", "B", "C"))] +
se[match(rows$site, c("S1", "S2", "S3"))] + rnorm(nrow(rows), 0, 0.3), 4)
export_dataset(rows[c("part", "operator", "site", "y")], "MSA_EXPANDED_CROSSED",
"Constructed: 6 parts x 3 operators x 3 sites x 2 trials, fully crossed, seed 20261028",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(part = "String", operator = "String", site = "String", y = "Float64"))
e <- read_dataset("MSA_EXPANDED_CROSSED")
vc <- reml(y ~ (1 | part) + (1 | operator) + (1 | site), e)
emit("STAT-MSA-EXPANDED-002", "Fully crossed part, operator and site", "MSA_EXPANDED_CROSSED",
list(response_column = "y", part_column = "part", operator_column = "operator", site_column = "site", structure = "fully_crossed"),
c(component("Repeatability", vc[["Residual"]], "REML residual variance."),
component("Reproducibility", vc[["operator"]], "The operator component."),
component("Site (measurement system)", vc[["site"]], "A crossed site counts toward Gage R&R (line 190-191)."),
component("Total Gage R&R", vc[["Residual"]] + vc[["operator"]] + vc[["site"]], "Repeatability + reproducibility + site."),
component("Part-to-part", vc[["part"]], "The part component."),
list("/path" = exact("reml_expanded", "REML path."))),
c("lmer(y ~ (1|part) + (1|operator) + (1|site), REML) with bobyqa rhoend 1e-12."), kind = "axis_coverage")
# ---- site nested in part ------------------------------------------------------------------
set.seed(20261029)
rows <- expand.grid(trial = 1:2, operator = c("A", "B", "C"), site = 1:2, part = sprintf("P%d", 1:8), stringsAsFactors = FALSE)
rows$site_id <- sprintf("%s-s%d", rows$part, rows$site)
pe <- setNames(rnorm(8, 0, 2), sprintf("P%d", 1:8)); sw <- setNames(rnorm(16, 0, 0.7), unique(rows$site_id)); oe <- c(A = 0.3, B = -0.2, C = 0.1)
rows$y <- round(12 + pe[rows$part] + sw[rows$site_id] + oe[rows$operator] + rnorm(nrow(rows), 0, 0.25), 4)
export_dataset(data.frame(part = rows$part, operator = rows$operator, site = rows$site_id, y = rows$y), "MSA_EXPANDED_NESTED",
"Constructed: 8 parts, 2 sites within each part, 3 operators, 2 trials, seed 20261029",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(part = "String", operator = "String", site = "String", y = "Float64"))
n <- read_dataset("MSA_EXPANDED_NESTED")
vn <- reml(y ~ (1 | part) + (1 | part:site) + (1 | operator), n)
emit("STAT-MSA-EXPANDED-003", "Site nested in part counts toward part-to-part", "MSA_EXPANDED_NESTED",
list(response_column = "y", part_column = "part", operator_column = "operator", site_column = "site", structure = "site_nested_in_part"),
c(component("Repeatability", vn[["Residual"]], "REML residual variance."),
component("Reproducibility", vn[["operator"]], "The operator component."),
component("Part-to-part", vn[["part"]] + vn[["part:site"]], "Part plus site-within-part (line 191-192)."),
component("Total Gage R&R", vn[["Residual"]] + vn[["operator"]], "Repeatability + reproducibility.")),
c("lmer(y ~ (1|part) + (1|part:site) + (1|operator), REML)."), kind = "axis_coverage")
emit("STAT-MSA-EXPANDED-004", "A part-by-site interaction is confounded when sites nest in parts", "MSA_EXPANDED_NESTED",
list(response_column = "y", part_column = "part", operator_column = "operator", site_column = "site",
structure = "site_nested_in_part", interactions = list("part_site")), list(),
c("Line 183-185: a declaration confounded with the nesting is rejected."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "confounded with the declared nesting"))
generators/r/measurement_gage.R
source("common.R", local = TRUE)
# References for crossed and extended gage studies (manual chapter 10):
# tabelz.analysis.gage_rr:compute_gage_rr (lines 95-165): balanced crossed
# random-effects ANOVA; the Operator*Part interaction is pooled into error
# when its p-value >= alpha; method-of-moments components truncated at 0;
# %contribution 100 v/v_total, %study variation 100 SD/SD_total,
# %tolerance 100 m SD / tolerance; ndc = floor(sqrt(2) SD_part / SD_GRR), min 1.
# gage_studies (lines 206-268): nested ANOVA; linearity and bias (bias on
# reference regression, average-bias t interval); type-1 Cg = 0.2 T / (6 s),
# Cgk = (0.1 T - |B|) / (3 s), sourced to Minitab's Type 1 image formulas
# with K=20 and SV=6s; stability centre, MR-bar/d2 sigma, +/- 3 sigma,
# drift slope on sequence position.
run <- generator_run(c("stats", "jsonlite"))
family <- "measurement"
generator <- "generators/r/measurement_gage.R"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form ANOVA or regression; owner 1e-9 tier.", why))
tail_p <- function(value, why) list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = "1e-9", rationale = paste("R p-value; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, entry, menu, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(),
error = NULL, deferred = list(), oracle_source = "R stats::aov, stats::lm and stats::t.test",
oracle_citation = "R stats::aov, stats::anova.lm, stats::lm and stats::t.test documentation",
version = "1.0.0", review_reference = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = deferred, generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = version, family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = oracle_source,
citation = oracle_citation,
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-MSA-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = review_reference))
}
# ---- crossed Gage R&R -----------------------------------------------------------------
crossed_data <- function(seed, interaction_sd) {
set.seed(seed)
parts <- sprintf("P%02d", 1:10); ops <- c("A", "B", "C")
pe <- rnorm(10, 0, 2); oe <- rnorm(3, 0, 0.4); ie <- matrix(rnorm(30, 0, interaction_sd), 10)
rows <- expand.grid(trial = 1:3, operator = ops, part = parts, stringsAsFactors = FALSE)
y <- 50 + pe[match(rows$part, parts)] + oe[match(rows$operator, ops)] +
ie[cbind(match(rows$part, parts), match(rows$operator, ops))] + rnorm(nrow(rows), 0, 0.5)
data.frame(part = rows$part, operator = rows$operator, y = round(y, 4), stringsAsFactors = FALSE)
}
crossed_ref <- function(d, alpha = 0.05, m = 6, tolerance = NULL) {
d$part <- factor(d$part); d$operator <- factor(d$operator)
p <- nlevels(d$part); o <- nlevels(d$operator); r <- nrow(d) / (p * o)
full <- anova(lm(y ~ part * operator, data = d))
ms <- full[["Mean Sq"]]; df <- full[["Df"]]; ss <- full[["Sum Sq"]]
ms_p <- ms[1]; ms_o <- ms[2]; ms_i <- ms[3]; ms_e <- ms[4]
p_int <- pf(ms_i / ms_e, df[3], df[4], lower.tail = FALSE)
pooled <- p_int >= alpha
if (pooled) {
ms_err <- (ss[3] + ss[4]) / (df[3] + df[4])
v_rep <- ms_err; v_int <- 0
v_op <- max(0, (ms_o - ms_err) / (p * r)); v_part <- max(0, (ms_p - ms_err) / (o * r))
} else {
v_rep <- ms_e; v_int <- max(0, (ms_i - ms_e) / r)
v_op <- max(0, (ms_o - ms_i) / (p * r)); v_part <- max(0, (ms_p - ms_i) / (o * r))
}
v_repro <- v_op + v_int; v_grr <- v_rep + v_repro; v_tot <- v_grr + v_part
list(pooled = pooled, p_int = p_int, v = c(grr = v_grr, rep = v_rep, repro = v_repro, op = v_op, int = v_int, part = v_part, total = v_tot),
ndc = if (v_grr > 0) max(1L, as.integer(floor(sqrt(2) * sqrt(v_part / v_grr)))) else 0L, m = m, tolerance = tolerance,
rep_ci = c(ms_e * df[4], if (pooled) (ss[3] + ss[4]) else ss[4]), df_err = if (pooled) df[3] + df[4] else df[4])
}
component_outputs <- function(r) {
v <- r$v; out <- list()
names_map <- c(grr = "Total Gage R&R", rep = "Repeatability", repro = "Reproducibility", op = "Operator",
part = "Part-To-Part", total = "Total Variation")
for (key in names(names_map)) {
node <- sprintf("/components/[source=%s]", names_map[[key]])
out[[paste0(node, "/variance")]] <- tol(v[[key]], "Method-of-moments component.")
out[[paste0(node, "/pct_contribution")]] <- tol(100 * v[[key]] / v[["total"]], "100 v / v_total.")
out[[paste0(node, "/pct_study_var")]] <- tol(100 * sqrt(v[[key]] / v[["total"]]), "100 SD / SD_total.")
out[[paste0(node, "/study_var")]] <- tol(r$m * sqrt(v[[key]]), "m SD.")
if (!is.null(r$tolerance)) out[[paste0(node, "/pct_tolerance")]] <- tol(100 * r$m * sqrt(v[[key]]) / r$tolerance, "100 m SD / tolerance.")
}
out[["/ndc"]] <- exact(r$ndc, "floor(sqrt(2) SD_part / SD_GRR), minimum 1.")
out[["/interaction_dropped"]] <- exact(r$pooled, "Pooled when the interaction p-value >= alpha.")
out
}
interval_deferral <- list(list(path = "/components/[source=Operator]/variance_lower",
reason = "The manual names component-specific F/chi-square intervals without a formula for non-repeatability components."))
export_dataset(crossed_data(20261018, 0.05), "MSA_GRR_POOLED", "Constructed crossed study: 10 parts x 3 operators x 3 trials, negligible interaction, seed 20261018",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(part = "String", operator = "String", y = "Float64"))
g1 <- read_dataset("MSA_GRR_POOLED")
r1 <- crossed_ref(g1, tolerance = 12)
if (!r1$pooled) stop("expected a pooled interaction")
emit("STAT-MSA-GRR-001", "tabelz.analysis.gage_rr:compute_gage_rr", "gageRrMenuItem", "Crossed Gage R&R with a pooled interaction and a tolerance",
"MSA_GRR_POOLED", list(measurement_column = "y", part_column = "part", operator_column = "operator", tolerance = 12),
c(component_outputs(r1),
list("/anova_with_interaction/[source=Operator*Part]/p_value" = tail_p(r1$p_int, "Interaction F test against repeatability."))),
c(sprintf("Interaction p = %.6g >= 0.05, so its SS and df join repeatability (line 126-127).", r1$p_int)),
deferred = interval_deferral)
export_dataset(crossed_data(20261019, 1.2), "MSA_GRR_INTERACTION", "Constructed crossed study with a strong operator-by-part interaction, seed 20261019",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(part = "String", operator = "String", y = "Float64"))
g2 <- read_dataset("MSA_GRR_INTERACTION")
r2 <- crossed_ref(g2)
if (r2$pooled) stop("expected a retained interaction")
emit("STAT-MSA-GRR-002", "tabelz.analysis.gage_rr:compute_gage_rr", "gageRrMenuItem", "Crossed Gage R&R with a retained interaction",
"MSA_GRR_INTERACTION", list(measurement_column = "y", part_column = "part", operator_column = "operator"),
c(component_outputs(r2), list("/components/[source=Operator*Part]/variance" = tol(r2$v[["int"]], "(MS_OP - MS_E) / r."))),
c(sprintf("Interaction p = %.6g < 0.05, so it stays in reproducibility (line 127-128).", r2$p_int)),
kind = "axis_coverage", deferred = interval_deferral)
r3 <- crossed_ref(g1, alpha = 0.99, m = 5.15)
emit("STAT-MSA-GRR-003", "tabelz.analysis.gage_rr:compute_gage_rr", "gageRrMenuItem", "Interaction alpha 0.99 and a 5.15-sigma study variation",
"MSA_GRR_POOLED", list(measurement_column = "y", part_column = "part", operator_column = "operator",
interaction_alpha = 0.99, study_var_multiplier = 5.15),
component_outputs(r3), c(sprintf("With alpha 0.99 the interaction p = %.6g decides pooling.", r3$p_int)),
kind = "threshold", deferred = interval_deferral)
# STAT-MSA-GRR-004: the simultaneous full-model repeatability interval (#185 rulings
# 6096972514 and 6130934313). The four full-model strata are kept whatever the interaction
# pooling decides; each chi-square tail is alpha/8 of the 95% joint confidence.
g4 <- read_dataset("MSA_GRR_POOLED")
g4$part <- factor(g4$part); g4$operator <- factor(g4$operator)
cells4 <- table(g4$part, g4$operator)
stopifnot(nlevels(g4$part) == 10L, nlevels(g4$operator) == 3L, all(cells4 == 3L))
full4 <- stats::anova(stats::lm(y ~ part * operator, data = g4))
stopifnot(identical(as.integer(full4$Df), c(9L, 2L, 18L, 60L)))
sse4 <- full4$`Sum Sq`[[4L]]
joint4 <- 0.95
tail4 <- (1 - joint4) / 8
emit("STAT-MSA-GRR-004", "tabelz.analysis.gage_rr:compute_gage_rr", "gageRrMenuItem",
"Simultaneous full-model repeatability interval", "MSA_GRR_POOLED",
list(measurement_column = "y", part_column = "part", operator_column = "operator"),
list("/components/[source=Repeatability]/variance_lower" = tol(sse4 / qchisq(1 - tail4, 60), "Full-model within-cell SS / chi2_(1 - alpha/8, 60)."),
"/components/[source=Repeatability]/variance_upper" = tol(sse4 / qchisq(tail4, 60), "Full-model within-cell SS / chi2_(alpha/8, 60)."),
"/confidence_method" = exact("Simultaneous Bonferroni intervals from full-model ANOVA (balanced crossed random effects)",
"The effective balanced interval method."),
"/interval_joint_confidence" = tol(joint4, "Joint confidence of the simultaneous variance family.")),
c("The repeatability interval of the Bonferroni simultaneous family: all four full-model ANOVA strata, each chi-square tail alpha/8 = 0.00625 at 95% joint confidence, using the within-cell SS on 60 df whatever the interaction pooling selects for point estimates.",
"The coverage claim is for the balanced Normal random-effects variance family, not for ratios or percentages."),
kind = "axis_coverage", version = "1.1.0",
review_reference = "https://github.com/Ali-Alsaqqa/Tabelz/issues/185#issuecomment-6130934313",
oracle_source = "R stats::lm full crossed ANOVA and stats::qchisq",
oracle_citation = "R stats::lm, stats::anova.lm and stats::qchisq documentation")
m <- read_dataset("MSA_GRR_POOLED"); m$y[7] <- NA
export_dataset(m, "MSA_GRR_MISSING", "MSA_GRR_POOLED with y missing in row 7 (1-based)", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(part = "String", operator = "String", y = "Float64"))
emit("STAT-MSA-GRR-005", "tabelz.analysis.gage_rr:compute_gage_rr", "gageRrMenuItem", "A missing value unbalances the design and is refused", "MSA_GRR_MISSING",
list(measurement_column = "y", part_column = "part", operator_column = "operator"), list(),
c("Line 99-102: every operator measures every part the same number of times unless unbalanced fitting is allowed."),
kind = "invalid_input", error = list(error_class = "ValueError", message_contains = "balanced"))
two <- read_dataset("MSA_GRR_POOLED"); two <- two[!duplicated(two[c("part", "operator")]), ]
export_dataset(two, "MSA_GRR_ONETRIAL", "MSA_GRR_POOLED with one trial per cell", "Hand-constructed",
"Constructed validation data; no third-party license.", c(part = "String", operator = "String", y = "Float64"))
emit("STAT-MSA-GRR-006", "tabelz.analysis.gage_rr:compute_gage_rr", "gageRrMenuItem", "One trial per cell is refused", "MSA_GRR_ONETRIAL",
list(measurement_column = "y", part_column = "part", operator_column = "operator"), list(),
c("Line 100-101: at least two trials in every cell."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "trial"))
# ---- nested -----------------------------------------------------------------------------
set.seed(20261020)
rows <- expand.grid(trial = 1:3, part = 1:4, operator = c("A", "B", "C"), stringsAsFactors = FALSE)
rows$part_id <- sprintf("%s-%d", rows$operator, rows$part)
pe <- setNames(rnorm(12, 0, 1.5), unique(rows$part_id)); oe <- c(A = -0.3, B = 0.2, C = 0.5)
rows$y <- round(20 + pe[rows$part_id] + oe[rows$operator] + rnorm(nrow(rows), 0, 0.4), 4)
export_dataset(data.frame(operator = rows$operator, part = rows$part_id, y = rows$y), "MSA_NESTED",
"Constructed nested study: 3 operators x 4 own parts x 3 trials, seed 20261020", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(operator = "String", part = "String", y = "Float64"))
dn <- read_dataset("MSA_NESTED")
fit <- anova(lm(y ~ operator + operator:part, data = transform(dn, operator = factor(operator), part = factor(part))))
ms_o <- fit[["Mean Sq"]][1]; ms_p <- fit[["Mean Sq"]][2]; ms_e <- fit[["Mean Sq"]][3]
v_o <- max(0, (ms_o - ms_p) / (4 * 3)); v_p <- max(0, (ms_p - ms_e) / 3); v_g <- ms_e + v_o
emit("STAT-MSA-NESTED-001", "tabelz.analysis.gage_studies:compute_nested_gage_rr", "extendedGageMenuItem", "Nested destructive Gage R&R", "MSA_NESTED",
list(measurement_column = "y", part_column = "part", operator_column = "operator"),
list("/metrics/[key=grr_pct]/value" = tol(100 * sqrt(v_g / (v_g + v_p)), "100 SD_GRR / SD_total."),
"/metrics/[key=ndc]/value" = exact(max(1L, as.integer(floor(sqrt(2) * sqrt(v_p / v_g)))), "floor(sqrt(2) SD_part / SD_GRR), minimum 1."),
"/tables/1/rows/1/3" = tol(ms_p, "Part(Operator) mean square."),
"/tables/0/rows/0/1" = tol(v_g, "Total Gage R&R variance = repeatability + operator.")),
c("lm(y ~ operator + operator:part): Operator, Part within Operator and Repeatability (line 215-217)."))
# ---- linearity and bias ------------------------------------------------------------------
set.seed(20261021)
ref <- rep(c(2, 4, 6, 8, 10), each = 6)
meas <- round(ref + 0.05 + 0.012 * ref + rnorm(30, 0, 0.08), 4)
export_dataset(data.frame(reference = ref, y = meas), "MSA_LINEARITY", "Constructed: five reference levels x six measurements, seed 20261021",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(reference = "Float64", y = "Float64"))
dl <- read_dataset("MSA_LINEARITY")
bias <- dl$y - dl$reference; lf <- summary(lm(bias ~ dl$reference)); tt <- t.test(bias)
emit("STAT-MSA-LIN-001", "tabelz.analysis.gage_studies:compute_linearity_bias", "extendedGageMenuItem", "Gage linearity and bias", "MSA_LINEARITY",
list(measurement_column = "y", reference_column = "reference", tolerance = 4),
list("/metrics/[key=average_bias]/value" = tol(mean(bias), "Mean of y - r."),
"/metrics/[key=bias_p]/value" = tail_p(tt$p.value, "One-sample t test of bias against zero."),
"/metrics/[key=slope]/value" = tol(coef(lf)[2, 1], "Slope of bias on reference."),
"/metrics/[key=linearity_p]/value" = tail_p(coef(lf)[2, 4], "Slope t test."),
"/metrics/[key=linearity]/value" = tol(coef(lf)[2, 1] * diff(range(dl$reference)), "Slope times the reference range (line 230)."),
"/metrics/[key=r_squared]/value" = tol(lf$r.squared, "R^2 of the bias regression.")),
c("Line 225-231."))
# ---- type-1 --------------------------------------------------------------------------------
set.seed(20261022)
export_dataset(data.frame(y = round(10.02 + rnorm(25, 0, 0.015), 5), run = seq_len(25)), "MSA_TYPE1",
"Constructed: 25 repeated measurements of a 10.00 standard, seed 20261022", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(y = "Float64", run = "Int64"))
d1 <- read_dataset("MSA_TYPE1")$y
s <- sd(d1); B <- mean(d1) - 10; W <- 0.2 * 0.6
emit("STAT-MSA-TYPE1-001", "tabelz.analysis.gage_studies:compute_type_1_gage", "extendedGageMenuItem", "Type-1 gage Cg and Cgk", "MSA_TYPE1",
list(measurement_column = "y", reference = 10, tolerance = 0.6),
list("/metrics/[key=bias]/value" = tol(B, "Mean minus reference."),
"/metrics/[key=std]/value" = tol(s, "Sample standard deviation."),
"/metrics/[key=cg]/value" = list(comparison = "tolerance", value = dec(W / (6 * s)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = "Minitab Type 1 Cg image formula (K/100*T)/SV with T=0.6, K=20, SV=6 sample SD; owner 1e-9 tier."),
"/metrics/[key=cgk]/value" = list(comparison = "tolerance", value = dec((W / 2 - abs(B)) / (3 * s)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = "Minitab Type 1 CgK image formula (K/200*T-|mean-reference|)/(SV/2), K=20 and SV=6 sample SD; owner 1e-9 tier."),
"/metrics/[key=bias_p]/value" = tail_p(t.test(d1, mu = 10)$p.value, "t test of the bias.")),
c("Type 1 Tabelz defaults K=20 percent and SV=6s: Minitab Cg=(K/100*T)/SV and Cgk=(K/200*T-|mean-reference|)/(SV/2). T=0.6 response units.",
"The numeric Cg/Cgk values are analytic applications of the Minitab image formulas to constructed measurements, not a published numerical example."),
oracle_source = "Minitab Type 1 Gage Study Cg/Cgk formulas; R stats::sd and stats::t.test for sample statistics and bias test",
oracle_citation = "Minitab Support, Methods and formulas for Type 1 Gage Study, Cg and CgK formula images; R stats::sd and stats::t.test documentation")
emit("STAT-MSA-TYPE1-002", "tabelz.analysis.gage_studies:compute_type_1_gage", "extendedGageMenuItem", "Fewer than ten measurements are refused", "MSA_TYPE1",
list(measurement_column = "y", reference = 10, tolerance = 0.6), list(),
c("Line 240: at least ten measurements; rows 0-15 (0-based) are excluded, leaving nine."), kind = "invalid_input",
excluded = 0:15, error = list(error_class = "ValueError", message_contains = "10"))
# ---- stability -------------------------------------------------------------------------------
set.seed(20261023)
yst <- round(5 + seq(0, 0.3, length.out = 30) + rnorm(30, 0, 0.1), 4)
ord <- sample(30)
export_dataset(data.frame(order = ord, y = yst[ord]), "MSA_STABILITY", "Constructed: a drifting check standard stored out of order, seed 20261023",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(order = "Int64", y = "Float64"))
ds <- read_dataset("MSA_STABILITY"); ys <- ds$y[order(ds$order)]
sig <- mean(abs(diff(ys))) / (2 / sqrt(pi)); lf <- summary(lm(ys ~ seq_along(ys)))
emit("STAT-MSA-STAB-001", "tabelz.analysis.gage_studies:compute_gage_stability", "extendedGageMenuItem", "Gage stability sorted by an order column", "MSA_STABILITY",
list(measurement_column = "y", order_column = "order", reference = 5.1),
list("/metrics/[key=mean]/value" = tol(mean(ys), "Centre."),
"/metrics/[key=sigma]/value" = tol(sig, "MR-bar / d2 on the sorted sequence."),
"/metrics/[key=upper]/value" = tol(mean(ys) + 3 * sig, "Mean + 3 sigma."),
"/metrics/[key=slope]/value" = tol(coef(lf)[2, 1], "Drift per observation."),
"/metrics/[key=drift_p]/value" = tail_p(coef(lf)[2, 4], "Slope t test."),
"/metrics/[key=bias]/value" = tol(mean(ys) - 5.1, "Mean minus the reference.")),
c("The order column actively sorts the sequence (line 257-260)."))
generators/r/measurement_manufacturing.R
source("common.R", local = TRUE)
# References for the manufacturing decision tools (Analyze > Quality >
# Manufacturing decision tools):
# tabelz.analysis.manufacturing_studies:acceptance_sampling_study
# tabelz.analysis.manufacturing_studies:tolerance_interval_study
# tabelz.analysis.manufacturing_studies:reliability_study
# The manual defines these methods in the manufacturing and precision chapters.
# The generators check the stated conventions and independent package outputs:
# * the binomial single-sampling plan with the smallest n (then smallest c)
# meeting Pa(AQL) >= 1 - alpha and Pa(LTPD) <= beta, and its Pa values;
# * the one-sided normal tolerance factor, which has one exact form,
# k = qt(confidence, n - 1, ncp = qnorm(content) sqrt(n)) / sqrt(n);
# * the two-sided Howe equation (2.13), named HE2 by tolerance 3.0.0;
# * the right-censored Weibull maximum-likelihood fit (survival::survreg).
# Plan search order, hypergeometric rounding, distribution-free order
# statistics and the life-model intervals have no stated convention.
run <- generator_run(c("stats", "survival", "jsonlite", "tolerance"))
family <- "measurement"
generator <- "generators/r/measurement_manufacturing.R"
menu <- "manufacturingDecisionMenuItem"
tol <- function(value, why, tier = "1e-9") list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste(if (tier == "1e-9") "Closed-form reference; owner 1e-9 tier." else "Iterative survreg fit; owner 1e-6 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, entry, title, dataset, config, outputs, notes, kind = "nominal", error = NULL, deferred = list()) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = deferred, generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = if (is.null(dataset)) NULL else list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = if (id %in% c("STAT-MSA-TOL-002", "STAT-MSA-TOL-004"))
"R tolerance::K.factor 3.0.0, explicit HE2" else
"R stats::pbinom, stats::qt (noncentral) and survival::survreg",
citation = if (id %in% c("STAT-MSA-TOL-002", "STAT-MSA-TOL-004"))
"Howe (1969), equation (2.13) and Table 1; R tolerance 3.0.0 K.factor documentation" else
"R stats::Binomial, stats::TDist and survival::survreg documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(notes)),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-MSA-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
# ---- acceptance sampling ---------------------------------------------------------------
acc <- "tabelz.analysis.manufacturing_studies:acceptance_sampling_study"
plan <- function(aql, ltpd, alpha, beta) {
for (n in 1:5000) for (c in 0:n) {
if (pbinom(c, n, aql) >= 1 - alpha && pbinom(c, n, ltpd) <= beta) return(c(n = n, c = c))
if (pbinom(c, n, ltpd) > beta) break
}
stop("no plan")
}
for (spec in list(c(0.01, 0.06, 0.05, 0.10, 1), c(0.02, 0.08, 0.10, 0.05, 2))) {
pl <- plan(spec[1], spec[2], spec[3], spec[4])
emit(sprintf("STAT-MSA-ACCEPT-%03d", spec[5]), acc, sprintf("Binomial single-sampling plan: AQL %g, LTPD %g", spec[1], spec[2]), NULL,
list(aql = spec[1], ltpd = spec[2], producer_risk = spec[3], consumer_risk = spec[4]),
list("/sample_size" = exact(as.integer(pl[["n"]]), "Smallest n meeting both risks."),
"/accept_number" = exact(as.integer(pl[["c"]]), "Smallest c for that n."),
"/probability_accept_aql" = tol(pbinom(pl[["c"]], pl[["n"]], spec[1]), "Binomial Pa at the AQL."),
"/probability_accept_ltpd" = tol(pbinom(pl[["c"]], pl[["n"]], spec[2]), "Binomial Pa at the LTPD.")),
c("Search over n = 1, 2, ... and, for each n, c = 0, 1, ...; the first plan meeting both risks."),
kind = if (spec[5] == 1) "nominal" else "axis_coverage")
}
emit("STAT-MSA-ACCEPT-003", acc, "An AQL at or above the LTPD is refused", NULL, list(aql = 0.05, ltpd = 0.05), list(),
c("A plan needs AQL < LTPD."), kind = "invalid_input", error = list(error_class = "ValueError", message_contains = "LTPD"))
# ---- tolerance intervals ------------------------------------------------------------------
set.seed(20261030)
export_dataset(data.frame(x = round(rnorm(30, 100, 4), 4)), "MSA_TOLERANCE", "Constructed: 30 values N(100, 4^2), seed 20261030",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(x = "Float64"))
x <- read_dataset("MSA_TOLERANCE")$x; n <- length(x); m <- mean(x); s <- sd(x)
tolx <- "tabelz.analysis.manufacturing_studies:tolerance_interval_study"
k1 <- qt(0.95, n - 1, ncp = qnorm(0.99) * sqrt(n)) / sqrt(n)
emit("STAT-MSA-TOL-001", tolx, "One-sided normal upper tolerance bound", "MSA_TOLERANCE",
list(column = "x", method = "normal", content = 0.99, confidence = 0.95, sided = "one_upper"),
list("/factor" = tol(k1, "Exact noncentral-t factor."), "/upper" = tol(m + k1 * s, "Mean + k s."), "/n" = exact(30L, "Thirty values.")),
c("99% content, 95% confidence, one-sided upper."))
kh <- tolerance::K.factor(n = n, f = n - 1, alpha = 1 - 0.99,
P = 0.95, side = 2, method = "HE2")
emit("STAT-MSA-TOL-002", tolx, "Two-sided normal tolerance interval (Howe)", "MSA_TOLERANCE",
list(column = "x", method = "normal", content = 0.95, confidence = 0.99, sided = "two_sided"),
list("/factor" = tol(kh, "tolerance::K.factor(method='HE2', f=n-1, alpha=1-C, P=content)."),
"/lower" = tol(m - kh * s, "Mean - k s."), "/upper" = tol(m + kh * s, "Mean + k s.")),
c("95% content, 99% confidence."), kind = "axis_coverage")
export_dataset(data.frame(x = 1:13), "MSA_HOWE_TABLE1",
"Constructed 13-value input to exercise Howe Table 1 factor at N=13; factor is scale invariant",
"Howe (1969), Table 1", "Original constructed input; no third-party license.",
c(x = "Float64"))
kh_table <- tolerance::K.factor(n = 13, f = 12, alpha = 0.05,
P = 0.95, side = 2, method = "HE2")
howe_x <- read_dataset("MSA_HOWE_TABLE1")$x
emit("STAT-MSA-TOL-004", tolx, "Howe Table 1 HE2 factor, N=13", "MSA_HOWE_TABLE1",
list(column = "x", method = "normal", content = 0.95, confidence = 0.95,
sided = "two_sided"),
list("/factor" = tol(kh_table, "Pinned R tolerance 3.0.0 HE2; Howe Table 1 prints 3.082."),
"/lower" = tol(mean(howe_x) - kh_table * sd(howe_x), "Constructed bounds from pinned HE2 factor."),
"/upper" = tol(mean(howe_x) + kh_table * sd(howe_x), "Constructed bounds from pinned HE2 factor."),
"/n" = exact(13L, "Howe Table 1 N=13.")),
c("Howe Table 1 lambda1 prints 3.082 and achieved probability 0.9486; lambda2 has 0.9485.",
"Only the factor is source-published; dataset and bounds are constructed."))
emit("STAT-MSA-TOL-003", tolx, "Distribution-free intervals are two-sided only", "MSA_TOLERANCE",
list(column = "x", method = "distribution_free", content = 0.9, confidence = 0.9, sided = "one_upper"), list(),
c("The distribution-free interval is two-sided."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "two-sided"))
# ---- censored Weibull reliability -----------------------------------------------------------
set.seed(20261031)
life <- round(rweibull(24, shape = 2.2, scale = 900), 2); cens <- life > 1100; life[cens] <- 1100
export_dataset(data.frame(hours = life, status = ifelse(cens, "suspended", "failed")), "MSA_RELIABILITY",
"Constructed: 24 Weibull(2.2, 900) lives, Type I censored at 1100 h, seed 20261031",
"R stats::rweibull documentation", "Constructed validation data; no third-party license.",
c(hours = "Float64", status = "String"))
rl <- read_dataset("MSA_RELIABILITY")
fit <- survival::survreg(survival::Surv(hours, status == "failed") ~ 1, data = rl, dist = "weibull",
control = survival::survreg.control(rel.tolerance = 1e-12, maxiter = 200))
shape <- 1 / fit$scale; scale <- exp(unname(coef(fit)))
emit("STAT-MSA-REL-001", "tabelz.analysis.manufacturing_studies:reliability_study", "Right-censored Weibull life model", "MSA_RELIABILITY",
list(time_column = "hours", event_column = "status", failure_value = "failed"),
list("/shape" = tol(shape, "1 / survreg scale.", "1e-6"), "/scale" = tol(scale, "exp(survreg intercept).", "1e-6"),
"/log_likelihood" = tol(fit$loglik[2], "Censored Weibull log likelihood.", "1e-6"),
"/median_life" = tol(scale * log(2)^(1 / shape), "scale (ln 2)^(1/shape).", "1e-6"),
"/failures" = exact(sum(rl$status == "failed"), "Observed failures."), "/censored" = exact(sum(rl$status != "failed"), "Suspensions.")),
c("survreg(Surv(hours, failed) ~ 1, dist = 'weibull'), rel.tolerance 1e-12; shape = 1/scale, scale = exp(intercept)."),
deferred = list(list(path = "/shape_lower", reason = "No stated interval convention for the life-model parameters.")))
generators/r/measurement_nist_oc.R
source("common.R", local = TRUE)
# NIST/SEMATECH e-Handbook 6.2.3.2, (n,c)=(52,3) binomial OC table.
# Expected values use independent full-precision R pbinom; the printed
# three-decimal table is preserved separately in the case provenance.
run <- generator_run(c("stats", "jsonlite"))
id <- "STAT-MSA-OC-NIST-001"
proportions <- c(0.01, 0.03, 0.05)
printed <- c(0.998, 0.930, 0.739)
export_dataset(data.frame(fraction_defective = proportions), "MSA_NIST_OC_52_3",
"NIST/SEMATECH e-Handbook 6.2.3.2 OC table p=.01,.03,.05",
"https://www.itl.nist.gov/div898/handbook/pmc/section2/pmc232.htm",
"Published numerical example; source attribution retained.",
c(fraction_defective = "Float64"))
d <- read_dataset("MSA_NIST_OC_52_3")
outputs <- list()
for (i in seq_along(proportions)) {
p <- d$fraction_defective[i]
outputs[[paste0("/probabilities/", i - 1L)]] <- list(
comparison = "tolerance", value = dec(pbinom(3, 52, p)),
absolute_tolerance = "1e-13", relative_tolerance = "1e-12",
rationale = paste("R stats::pbinom(3, 52,", p,
") recomputes the NIST binomial sum at full precision; printed table entry is", printed[i], "."))
}
write_expected("measurement", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("measurement", id, list(schema_version = "2.0.0", case_id = id,
version = "2.0.0", family = "measurement",
entry_point = "tabelz.analysis.manufacturing:probability_of_acceptance",
menu_items = list("manufacturingDecisionMenuItem"),
title = "NIST (52,3) binomial operating-characteristic example", case_kind = "nominal",
dataset = list(path = "reference_data/datasets/MSA_NIST_OC_52_3.json", sha256 = ""),
excluded_rows = list(),
configuration = list(fraction_column = "fraction_defective", sample_size = 52L,
accept_number = 3L),
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/measurement/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation",
source = "R stats::pbinom applied to NIST (52,3) binomial OC example",
citation = "NIST/SEMATECH e-Handbook 6.2.3.2, Choosing a Sampling Plan with a given OC Curve, https://www.itl.nist.gov/div898/handbook/pmc/section2/pmc232.htm; R stats::Binomial documentation",
generator = list(path = "generators/r/measurement_nist_oc.R", sha256 = ""),
independent = TRUE,
convention_notes = list(
"NIST prints Pa=0.998 at p=.01, 0.930 at p=.03, and 0.739 at p=.05 for n=52,c=3.",
"The binomial sum/R pbinom at p=.05 is about 0.7383174108, which rounds to .738 at three decimals; NIST's printed .739 is discrepant and is retained as published text, not the full-precision expected value.",
"Large or unspecified lot only; this case does not assert the finite-lot hypergeometric extension.",
"The public probability_of_acceptance helper evaluates each declared p at n=52,c=3; it does not search for a plan.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-MSA-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
cat("NIST (52,3) binomial OC case generated\n")
generators/r/multivariate_axes.R
source("common.R",local=TRUE)
d <- read_dataset("MV_CORE")
run <- generator_run(c("stats","jsonlite"))
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
emit <- function(id,entry,menu,config,outputs,notes) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Configuration axis",id),
case_kind="nominal",dataset=list(path="reference_data/datasets/MV_CORE.json",sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats and matrix arithmetic",
citation="R stats::hclust and base rowSums documentation",
generator=list(path="generators/r/multivariate_axes.R",sha256=""),independent=TRUE,
convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
ord <- order(as.character(seq_len(nrow(d))-1L))
hc <- stats::hclust(stats::dist(d[ord,c("x","y","z")]),method="ward.D2")
emit("STAT-MV-HCLUST-RAW-001","tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem",
list(columns=list("x","y","z"),mode="rows",linkage="ward",standardize=FALSE,cut_k=3L),
list("/merge_heights/0"=tol(hc$height[1],"R Ward.D2 first merge on original measurement scale")),
c("Row-mode standardize=false uses Euclidean distances in original units."))
id <- "STAT-MV-ALPHA-SCORE-SUM-001"
ids <- as.list(paste0(id,":",seq_len(nrow(d))-1L))
model <- list(item_analysis=list(items=list("i1","i2","i3"),reverse_items=list(),
scale_low=NULL,scale_high=NULL,row_ids=ids))
sums <- rowSums(d[c("i1","i2","i3")])
emit(id,"operation:tabelz.item_analysis_scores","itemAnalysisMenuItem",
list(names=list("scale_sum"),aggregation="sum",model=model),
list("/0/scale_sum"=tol(sums[1],"Analytic three-item sum for first row"),
"/11/scale_sum"=tol(sums[12],"Analytic three-item sum for last row")),
c("The sum aggregation changes only the retained score operation, not the item-analysis fit."))
cat("multivariate axis references generated\n")
generators/r/multivariate_boundaries.R
source("common.R",local=TRUE)
base <- read_dataset("MV_CORE")
run <- generator_run(c("stats","jsonlite"))
types <- c(x="Float64",y="Float64",z="Float64",i1="Int64",i2="Int64",
i3="Int64",group="String",cat="String")
tol <- function(x,note,tier="1e-9") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R boundary reference:",note))
emit <- function(id,entry,menu,dataset,config,outputs,notes) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Special boundary",id),
case_kind="boundary",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats and closed-form formulae",
citation="R stats::prcomp, hclust, kmeans, manova, chisq.test and cor documentation",
generator=list(path="generators/r/multivariate_boundaries.R",sha256=""),independent=TRUE,
convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
d <- base[c(1L,1L,5L,9L),]
d$i2 <- c(2L,2L,2L,2L)
export_dataset(d,"MV_TIES","Duplicate measurements and a constant item",
"R multivariate tie and zero-item-variance references",
"Constructed validation data; no third-party license.",types)
d <- read_dataset("MV_TIES")
pc <- stats::prcomp(d[c("x","y")],center=TRUE,scale.=TRUE)
emit("STAT-MV-PCA-TIES-001","tabelz.executor.principal_components","principalComponentsMenuItem",
"MV_TIES",list(columns=list("x","y"),scaling="correlation",n_components=2L),
list("/eigenvalues/1"=tol(pc$sdev[2]^2,"second eigenvalue with one repeated row")),
c("One exact duplicate row tests covariance computation without assuming a sign for loadings."))
ord <- order(as.character(seq_len(nrow(d))-1L))
hc <- stats::hclust(stats::dist(scale(d[ord,c("x","y")])),method="ward.D2")
emit("STAT-MV-HCLUST-TIES-001","tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem","MV_TIES",
list(columns=list("x","y"),linkage="ward",cut_k=2L),
list("/merge_heights/0"=tol(hc$height[1],"zero-height duplicate-row merge")),
c("Exact duplicate measurements force first Ward.D2 merge height zero."))
set.seed(11)
km <- stats::kmeans(scale(d[ord,c("x","y")]),centers=2L,nstart=50L,algorithm="Hartigan-Wong")
emit("STAT-MV-KMEANS-TIES-001","tabelz.executor.clustering","kmeansClusteringMenuItem",
"MV_TIES",list(columns=list("x","y"),k=2L,seed=11L,restarts=50L),
list("/inertia"=tol(km$tot.withinss,"within SS with exact duplicate observations")),
c("set.seed(11), nstart=50; duplicate points remain together in the unique two-way optimum."))
items <- as.matrix(d[c("i1","i2")]); k <- ncol(items)
alpha <- k/(k-1)*(1-sum(apply(items,2,stats::var))/stats::var(rowSums(items)))
emit("STAT-MV-ALPHA-CONSTANT-001","tabelz.executor.item_analysis","itemAnalysisMenuItem",
"MV_TIES",list(items=list("i1","i2")),
list("/cronbach_alpha"=tol(alpha,"raw alpha with one zero-variance item")),
c("One item is constant; total-score variance remains positive, so raw alpha is defined."))
emit("STAT-MV-CORR-TIES-001","tabelz.analysis.correlation:compute_correlation",
"correlationMenuItem","MV_TIES",
list(columns=list("x","y"),metric="spearman",max_scatter_columns=0L),
list("/cells/1/value"=tol(stats::cor(d$x,d$y,method="spearman"),"midrank Spearman correlation with ties")),
c("Tied ranks use R average/midrank convention."))
f <- read_dataset("MV_FACTOR")[seq_len(6L),]
export_dataset(f,"MV_FA_SIX","Six complete observations on five factor variables",
"R sample-correlation eigenvalue at n=p+1",
"Constructed validation data; no third-party license.",
setNames(rep("Float64",ncol(f)),names(f)))
f <- read_dataset("MV_FA_SIX")
eig <- eigen(stats::cor(f),symmetric=TRUE)$values[1]
emit("STAT-MV-FA-SIX-001","tabelz.executor.factor_analysis","factorAnalysisMenuItem",
"MV_FA_SIX",list(columns=as.list(names(f)),n_factors=1L,rotation="none"),
list("/correlation_eigenvalues/0"=tol(eig,"leading correlation eigenvalue at n=p+1")),
c("Five-variable factor analysis on six complete rows is the n=p+1 boundary; ML uniqueness is not compared on this small sample."))
m <- base[c(1L,2L,5L,6L),]
export_dataset(m,"MV_MANOVA_FOUR","Two groups with two observations each",
"R manova small residual-SSCP boundary",
"Constructed validation data; no third-party license.",types)
m <- read_dataset("MV_MANOVA_FOUR")
mv <- stats::manova(cbind(x,y)~group,data=m)
pillai <- summary(mv,test="Pillai")$stats[1,"Pillai"]
emit("STAT-MV-MANOVA-SMALL-001","tabelz.executor.manova","manovaMenuItem",
"MV_MANOVA_FOUR",list(response_columns=list("x","y"),factor_columns=list("group")),
list("/joint_tests/0/value"=tol(pillai,"Pillai trace with two residual degrees of freedom")),
c("Two groups of two observations leave residual df=2 for two responses."))
ct <- data.frame(label=c("A","B"),U=c(0L,3L),V=c(2L,1L))
export_dataset(ct,"MV_CA_ZERO","Two-by-two table with one zero cell and nonzero margins",
"R chisq.test zero-cell correspondence-analysis boundary",
"Constructed validation data; no third-party license.",
c(label="String",U="Int64",V="Int64"))
ct <- read_dataset("MV_CA_ZERO")
chi <- suppressWarnings(stats::chisq.test(as.matrix(ct[c("U","V")]),correct=FALSE))$statistic
emit("STAT-MV-CA-ZERO-001","tabelz.executor.correspondence_analysis",
"correspondenceAnalysisMenuItem","MV_CA_ZERO",
list(mode="count_table",row_label_column="label",count_columns=list("U","V")),
list("/chi_square"=tol(chi,"Pearson chi-square with one zero observed cell")),
c("A zero observed cell is valid because both row and column margins remain positive."))
cat("special multivariate boundaries generated\n")
generators/r/multivariate_ca_coordinates.R
source("common.R",local=TRUE)
d <- data.frame(label=c("A","B","C"),U=c(8L,1L,2L),
V=c(2L,5L,1L),W=c(1L,2L,4L))
export_dataset(d,"MV_CA_COORD","Asymmetric positive three-by-three count table",
"R ca::ca principal-coordinate map with distinct inertias",
"Constructed validation data; no third-party license.",
c(label="String",U="Int64",V="Int64",W="Int64"))
d <- read_dataset("MV_CA_COORD")
tab <- as.matrix(d[c("U","V","W")])
fit <- ca::ca(tab)
stopifnot(fit$sv[1L]>fit$sv[2L],fit$sv[2L]>0)
principal_row <- fit$rowcoord[1L,1L]*fit$sv[1L]
principal_col <- fit$colcoord[1L,1L]*fit$sv[1L]
sign_one <- function(x,note) list(comparison="sign_invariant_sequence",
values=list(dec(unname(x))),absolute_tolerance="0",relative_tolerance="1e-9",
rationale=note)
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
id <- "STAT-MV-CA-COORD-001"
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/singular_values/0"=tol(fit$sv[1L],"R ca first singular value"),
"/row_categories/0/coordinates"=sign_one(principal_row,
"R ca standard row coordinate times singular value gives principal coordinate; sign is arbitrary"),
"/column_categories/0/coordinates"=sign_one(principal_col,
"R ca standard column coordinate times singular value gives principal coordinate; sign is arbitrary")),
expected_error=NULL,deferred_outputs=list(),
generator_run=generator_run(c("ca","stats","jsonlite"))))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point="tabelz.executor.correspondence_analysis",
menu_items=list("correspondenceAnalysisMenuItem"),title="CA principal-coordinate sign convention",
case_kind="axis_coverage",
dataset=list(path="reference_data/datasets/MV_CA_COORD.json",sha256=""),
excluded_rows=list(),configuration=list(mode="count_table",row_label_column="label",
count_columns=list("U","V","W"),dimensions=1L),"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R ca::ca",
citation="R ca package simple correspondence-analysis documentation",
generator=list(path="generators/r/multivariate_ca_coordinates.R",sha256=""),
independent=TRUE,convention_notes=list("R ca rowcoord/colcoord are standard coordinates; multiply by singular value for principal coordinates.",
"The two positive singular values are distinct, so axis ordering is unique; each axis sign may flip.",
"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
cat("correspondence principal-coordinate reference generated\n")
generators/r/multivariate_canonical.R
source("common.R", local = TRUE)
# Canonical correlation (Statistics > Multivariate > Canonical correlation) against
# stats::cancor plus the closed-form Bartlett sequential chi-square, structure correlations
# and redundancies computed from the cancor variates. Every oracle reads the exported CSV.
run <- generator_run(c("stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/92-dimensions-and-categories.tex:52-58: linear combinations U = a'X and V = b'Y with maximal correlation, successive pairs uncorrelated with the preceding ones; complete independent rows.",
"docs/manual/chapters/92-dimensions-and-categories.tex:60-64: structure correlations are each variable with its own canonical variate, cross-structure with the other set's; redundancy = mean squared structure correlation times the squared canonical correlation.",
"docs/manual/chapters/92-dimensions-and-categories.tex:73-79: Bartlett's sequential test -(N - 1 - (p + q + 1)/2) sum_{i >= k} log(1 - r_i^2) ~ chi-square with (p - k + 1)(q - k + 1) df; the sign of a canonical pair is arbitrary."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
prob <- function(value, label) {
list(comparison = "tail_probability", value = dec(unname(value)), relative_tolerance = "1e-8",
rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
seq_tol <- function(values, label, rel = "1e-9", abs = "0") {
list(comparison = "sequence", values = as.list(dec(unname(values))),
absolute_tolerance = abs, relative_tolerance = rel, rationale = label)
}
emit <- function(id, title, kind, configuration, outputs, notes, excluded = list()) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("multivariate", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "multivariate",
entry_point = "tabelz.analysis.canonical_correlation:compute_canonical_correlation",
menu_items = list("canonicalCorrelationMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/MV_CANCOR.json", sha256 = ""),
excluded_rows = excluded, configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/multivariate/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "independent_implementation",
source = "R stats::cancor plus closed-form Bartlett test, structure correlations and redundancy",
citation = "R Core Team, stats package help: cancor; Bartlett (1941) sequential chi-square; Stewart and Love (1968) redundancy index.",
generator = list(path = "generators/r/multivariate_canonical.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-MV-CANCOR-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("multivariate", id, case)
}
set.seed(48)
n <- 20
z1 <- rnorm(n); z2 <- rnorm(n)
frame <- data.frame(
x1 = round(2 * z1 + rnorm(n), 3),
x2 = round(z2 - z1 + rnorm(n), 3),
x3 = round(rnorm(n, 5, 2), 3),
y1 = round(z1 + 0.5 * z2 + rnorm(n, sd = 0.8), 3),
y2 = round(-z2 + rnorm(n), 3)
)
frame$x2[5] <- NA
export_dataset(frame, "MV_CANCOR", "Twenty rows on two correlated variable sets with one missing x2 (seed 48)",
"R stats cancor",
"Constructed validation data; no third-party license.",
c(x1 = "Float64", x2 = "Float64", x3 = "Float64", y1 = "Float64", y2 = "Float64"))
d <- read_dataset("MV_CANCOR")
# Everything Tabelz reports, from cancor's variates; the sign of each pair is fixed so that
# the first set's structure correlations with its own variate sum to a positive value.
reference <- function(data, first, second) {
rows <- complete.cases(data[c(first, second)])
x <- as.matrix(data[rows, first, drop = FALSE])
y <- as.matrix(data[rows, second, drop = FALSE])
N <- nrow(x); p <- ncol(x); q <- ncol(y); k <- min(p, q)
fit <- cancor(x, y)
r <- fit$cor[seq_len(k)]
# cancor coefficients give variates of unit sum of squares; unit variance needs sqrt(N - 1).
a <- fit$xcoef[, seq_len(k), drop = FALSE] * sqrt(N - 1)
b <- fit$ycoef[, seq_len(k), drop = FALSE] * sqrt(N - 1)
u <- scale(x, scale = FALSE) %*% a
v <- scale(y, scale = FALSE) %*% b
for (i in seq_len(k)) {
if (sum(cor(x, u[, i])) < 0) {
a[, i] <- -a[, i]; b[, i] <- -b[, i]; u[, i] <- -u[, i]; v[, i] <- -v[, i]
}
}
stopifnot(max(abs(apply(u, 2, var) - 1)) < 1e-9, max(abs(diag(cor(u, v)) - r)) < 1e-9)
sdx <- apply(x, 2, sd); sdy <- apply(y, 2, sd)
own_x <- cor(x, u); cross_x <- cor(x, v)
own_y <- cor(y, v); cross_y <- cor(y, u)
multiplier <- N - 1 - (p + q + 1) / 2
logs <- log(1 - r^2)
roots <- lapply(seq_len(k), function(i) {
chi <- -multiplier * sum(logs[i:k])
df <- (p - i + 1) * (q - i + 1)
list(correlation = r[i], squared = r[i]^2, wilks = exp(sum(logs[i:k])), chi = chi, df = df,
p = pchisq(chi, df, lower.tail = FALSE))
})
redundancy <- lapply(seq_len(k), function(i) {
fo <- mean(own_x[, i]^2); so <- mean(own_y[, i]^2)
list(first_own = fo, first_red = fo * r[i]^2, second_own = so, second_red = so * r[i]^2)
})
list(N = N, p = p, q = q, k = k, roots = roots, redundancy = redundancy,
a = a, b = b, std_a = a * sdx, std_b = b * sdy, multiplier = multiplier,
own_x = own_x, cross_x = cross_x, own_y = own_y, cross_y = cross_y,
first = first, second = second)
}
outputs_for <- function(ref, missing, excluded) {
out <- list(
"/n" = exact(ref$N, "complete rows"),
"/missing_count" = exact(missing, "rows with a missing value among the selected variables"),
"/excluded_count" = exact(excluded, "rows excluded by identity"),
"/bartlett_multiplier" = tol(ref$multiplier, "N - 1 - (p + q + 1)/2")
)
for (i in seq_len(ref$k)) {
root <- ref$roots[[i]]
base <- sprintf("/roots/%d", i - 1L)
out[[paste0(base, "/correlation")]] <- tol(root$correlation, "cancor canonical correlation")
out[[paste0(base, "/squared")]] <- tol(root$squared, "squared canonical correlation")
out[[paste0(base, "/wilks_lambda")]] <- tol(root$wilks, "product of (1 - r_i^2) from this root on")
out[[paste0(base, "/chi_square")]] <- tol(root$chi, "Bartlett chi-square of the remaining roots")
out[[paste0(base, "/df")]] <- exact(as.integer(root$df), "(p - k + 1)(q - k + 1)")
out[[paste0(base, "/p_value")]] <- prob(root$p, "upper chi-square tail")
red <- ref$redundancy[[i]]
base <- sprintf("/redundancy/%d", i - 1L)
out[[paste0(base, "/first_own_variance")]] <- tol(red$first_own, "mean squared structure correlation of the first set with its own variate")
out[[paste0(base, "/first_redundancy")]] <- tol(red$first_red, "first own variance times r_i^2")
out[[paste0(base, "/second_own_variance")]] <- tol(red$second_own, "mean squared structure correlation of the second set with its own variate")
out[[paste0(base, "/second_redundancy")]] <- tol(red$second_red, "second own variance times r_i^2")
}
for (j in seq_along(ref$first)) {
base <- sprintf("/loadings/%d", j - 1L)
out[[paste0(base, "/variable")]] <- exact(ref$first[[j]], "first-set variable in selection order")
out[[paste0(base, "/raw")]] <- seq_tol(ref$a[j, ], "cancor xcoef times sqrt(N - 1): unit-variance variates; pair sign fixed by the structure-sum convention", rel = "1e-8")
out[[paste0(base, "/standardized")]] <- seq_tol(ref$std_a[j, ], "raw coefficient times the variable's sample SD", rel = "1e-8")
out[[paste0(base, "/own_structure")]] <- seq_tol(ref$own_x[j, ], "correlation of the variable with its own set's variates", rel = "1e-8")
out[[paste0(base, "/cross_structure")]] <- seq_tol(ref$cross_x[j, ], "correlation of the variable with the other set's variates", rel = "1e-8")
}
for (j in seq_along(ref$second)) {
base <- sprintf("/loadings/%d", ref$p + j - 1L)
out[[paste0(base, "/variable")]] <- exact(ref$second[[j]], "second-set variable after the first set")
out[[paste0(base, "/raw")]] <- seq_tol(ref$b[j, ], "cancor ycoef times sqrt(N - 1)", rel = "1e-8")
out[[paste0(base, "/standardized")]] <- seq_tol(ref$std_b[j, ], "raw coefficient times the variable's sample SD", rel = "1e-8")
out[[paste0(base, "/own_structure")]] <- seq_tol(ref$own_y[j, ], "correlation of the variable with its own set's variates", rel = "1e-8")
out[[paste0(base, "/cross_structure")]] <- seq_tol(ref$cross_y[j, ], "correlation of the variable with the other set's variates", rel = "1e-8")
}
out
}
sign_note <- "Sign convention replicated from the product: each pair of variates is reflected so that the sum of the first set's structure correlations with its own variate is positive (the manual calls the sign arbitrary)."
ref <- reference(d, c("x1", "x2", "x3"), c("y1", "y2"))
stopifnot(ref$N == 19, ref$k == 2)
emit("STAT-MV-CANCOR-001", "Canonical correlation of three and two variables", "nominal",
list(first = list("x1", "x2", "x3"), second = list("y1", "y2")),
outputs_for(ref, 1L, 0L),
list("stats::cancor on the 19 complete rows (one x2 is missing); p = 3, q = 2, k = 2.",
"Bartlett's chi-square: -(N - 1 - (p + q + 1)/2) sum_{i >= k} log(1 - r_i^2) on (p - k + 1)(q - k + 1) df, upper tail.",
sign_note))
ref <- reference(d[-1, ], c("x1", "x3"), c("y1", "y2"))
stopifnot(ref$N == 19, ref$k == 2)
emit("STAT-MV-CANCOR-SQUARE-001", "Canonical correlation of two and two variables with an excluded row", "axis_coverage",
list(first = list("x1", "x3"), second = list("y1", "y2")),
outputs_for(ref, 0L, 1L),
list("stats::cancor on rows 2-20 (row 1 is excluded by identity; no selected value is missing): p = q = 2, so both roots have a square set of coefficients.",
sign_note),
excluded = list(0L))
ref <- reference(d, "x1", "y1")
stopifnot(ref$N == 20, ref$k == 1, abs(ref$roots[[1]]$correlation - abs(cor(d$x1, d$y1))) < 1e-12)
emit("STAT-MV-CANCOR-SINGLE-001", "Canonical correlation of one variable against one", "boundary",
list(first = list("x1"), second = list("y1")),
outputs_for(ref, 0L, 0L),
list("With one variable in each set the canonical correlation is |cor(x1, y1)| (checked in the generator), the structure correlations are +1 and +/-r, and Bartlett's test has one degree of freedom.",
sign_note))
cat("canonical correlation cases generated\n")
generators/r/multivariate_catpca.R
source("common.R", local = TRUE)
# Categorical principal components (Statistics > Multivariate > Categorical principal
# components). The product fits the Gifi single-quantification (princals) loss by alternating
# least squares. For numeric variables the quantification is the linear standardization, and
# for a two-category nominal or ordinal variable the centred, normalized quantification is
# unique up to sign (ordinal: the declared order fixes the sign), so in these cases the
# solution is the principal component analysis of the correlation matrix of the quantified
# variables, computed here with base R eigen(cor(.)) and prcomp. Every oracle reads the
# exported CSV.
run <- generator_run(c("stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/92-dimensions-and-categories.tex:83-91: nominal variables permit unrestricted category values, ordinal ones a monotone order, numeric ones a linear transformation; one quantification per variable shared across dimensions, complete rows, equal weights.",
"docs/manual/chapters/92-dimensions-and-categories.tex:93-98: read the achieved loss and convergence, component eigenvalues, loadings and category values; numeric-only inputs reduce to correlation-based PCA; the CATPCA alpha of a component is a function of its eigenvalue."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
seq_tol <- function(values, label, rel = "1e-9", abs = "0") {
list(comparison = "sequence", values = as.list(dec(unname(values))),
absolute_tolerance = abs, relative_tolerance = rel, rationale = label)
}
sign_seq <- function(values, label, rel = "1e-9", abs = "0") {
list(comparison = "sign_invariant_sequence", values = as.list(dec(unname(values))),
absolute_tolerance = abs, relative_tolerance = rel, rationale = label)
}
emit <- function(id, title, kind, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("multivariate", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "multivariate",
entry_point = "tabelz.analysis.categorical_pca:compute_categorical_pca",
menu_items = list("categoricalPcaMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/MV_CATPCA.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/multivariate/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "independent_implementation",
source = "R stats::prcomp / eigen(cor(.)) on the uniquely quantified variables",
citation = "Gifi (1990) Nonlinear Multivariate Analysis, ch. 4 (princals single quantification); Linting, Meulman, Groenen and van der Kooij (2007) Nonlinear principal components analysis, Psychological Methods 12(3) (Cronbach's alpha of a CATPCA dimension); R Core Team, stats package help: prcomp.",
generator = list(path = "generators/r/multivariate_catpca.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-MV-CATPCA-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("multivariate", id, case)
}
set.seed(92)
n <- 20
base <- rnorm(n)
frame <- data.frame(
x1 = round(base + rnorm(n, sd = 0.6), 3),
x2 = round(-0.7 * base + rnorm(n, sd = 0.8), 3),
x3 = round(rnorm(n, 3, 1.5), 3),
rating = ifelse(base + rnorm(n, sd = 0.5) > 0.2, "high", "low"),
site = sample(c("east", "west"), n, TRUE),
stringsAsFactors = FALSE
)
frame$x3[4] <- NA
frame$site[11] <- NA
export_dataset(frame, "MV_CATPCA", "Twenty rows: three scale variables, a binary ordinal rating and a binary nominal site, two missing values (seed 92)",
"R stats prcomp on uniquely quantified variables",
"Constructed validation data; no third-party license.",
c(x1 = "Float64", x2 = "Float64", x3 = "Float64", rating = "String", site = "String"))
d <- read_dataset("MV_CATPCA")
# PCA of the correlation matrix with the product's loading sign convention (the loadings of a
# dimension sum to a positive value; the largest absolute loading is positive on a zero sum).
pca_reference <- function(q, dims) {
m <- ncol(q)
e <- eigen(cor(q), symmetric = TRUE)
values <- e$values[seq_len(dims)]
vectors <- e$vectors[, seq_len(dims), drop = FALSE]
check <- prcomp(q, scale. = TRUE)
stopifnot(max(abs(check$sdev[seq_len(dims)]^2 - values)) < 1e-9)
for (s in seq_len(dims)) {
total <- sum(vectors[, s])
anchor <- vectors[which.max(abs(vectors[, s])), s]
if (total < 0 || (total == 0 && anchor < 0)) vectors[, s] <- -vectors[, s]
}
loadings <- vectors %*% diag(sqrt(values), nrow = dims)
list(m = m, values = values, loadings = loadings, percent = 100 * values / m,
alpha = m / (m - 1) * (1 - 1 / values), loss = dims - sum(values) / m)
}
dimension_outputs <- function(ref, n_rows, missing) {
out <- list(
"/n" = exact(n_rows, "complete rows"),
"/missing_count" = exact(missing, "rows with a missing selected value"),
"/converged" = exact(TRUE, "the alternating least squares converges on this fixed-quantification problem"),
"/loss" = tol(ref$loss, "princals loss p - sum of the retained eigenvalues / m at the optimum", rel = "1e-8", abs = "1e-10")
)
for (s in seq_along(ref$values)) {
base <- sprintf("/dimensions/%d", s - 1L)
out[[paste0(base, "/eigenvalue")]] <- tol(ref$values[s], "eigenvalue of the correlation matrix of the quantified variables (prcomp sdev^2)")
out[[paste0(base, "/percent")]] <- tol(ref$percent[s], "100 eigenvalue / m")
out[[paste0(base, "/alpha")]] <- tol(ref$alpha[s], "Cronbach's alpha (m/(m-1))(1 - 1/eigenvalue)")
}
out
}
# Case 1: numeric variables only -> ordinary correlation PCA.
rows <- complete.cases(d[c("x1", "x2", "x3")])
q <- as.matrix(d[rows, c("x1", "x2", "x3")])
ref <- pca_reference(q, 2L)
stopifnot(sum(rows) == 19, ref$values[1] > ref$values[2] + 1e-6, ref$values[2] > 1e-6)
outputs <- dimension_outputs(ref, 19L, 1L)
for (j in 1:3) {
outputs[[sprintf("/variables/%d/variable", j - 1L)]] <- exact(c("x1", "x2", "x3")[j], "numeric variables in selection order")
outputs[[sprintf("/variables/%d/level", j - 1L)]] <- exact("numeric", "scaling level")
outputs[[sprintf("/variables/%d/loadings", j - 1L)]] <- seq_tol(ref$loadings[j, ], "eigenvector times sqrt(eigenvalue): correlation of the standardized variable with the component; dimension sign by the loading-sum convention", rel = "1e-8", abs = "1e-10")
}
sd_pop <- function(v) sqrt(mean((v - mean(v))^2))
stopifnot(!anyDuplicated(q[, "x1"]))
first_x1 <- sort(unique(q[, "x1"]))[1]
outputs[["/categories/0/variable"]] <- exact("x1", "the first category row belongs to x1")
outputs[["/categories/0/quantification"]] <- tol((first_x1 - mean(q[, "x1"])) / sd_pop(q[, "x1"]), "numeric quantification: (x - mean)/population SD of the smallest x1 value", rel = "1e-8")
outputs[["/categories/0/count"]] <- exact(1L, "distinct x1 values occur once")
emit("STAT-MV-CATPCA-NUMERIC-001", "Categorical PCA of numeric variables equals correlation PCA", "nominal",
list(numeric = list("x1", "x2", "x3"), dimensions = 2L),
outputs,
list("With numeric scaling levels only, each quantification is the linear standardization, so the princals optimum is the PCA of the Pearson correlation matrix: eigen(cor(x)) cross-checked with prcomp(scale. = TRUE) on the 19 complete rows.",
"Loss at the optimum: dimensions - (lambda_1 + lambda_2)/m. Sign convention replicated from the product: the loadings of each dimension sum to a positive value.",
"The eigenvalues are distinct, so the loadings are unique up to the stated sign."))
# Case 2: a binary ordinal variable (declared order low < high) and two numeric variables.
rows <- complete.cases(d[c("rating", "x1", "x2")])
sub <- d[rows, ]
n2 <- nrow(sub)
counts <- table(factor(sub$rating, levels = c("low", "high")))
# Centred, unit-variance (population) quantification, non-decreasing in the declared order.
y_low <- -sqrt(counts[["high"]] / counts[["low"]])
y_high <- sqrt(counts[["low"]] / counts[["high"]])
quant <- ifelse(sub$rating == "low", y_low, y_high)
stopifnot(abs(mean(quant)) < 1e-12, abs(mean(quant^2) - 1) < 1e-12)
q <- cbind(rating = quant, x1 = sub$x1, x2 = sub$x2)
ref <- pca_reference(q, 2L)
stopifnot(n2 == 20, ref$values[1] > ref$values[2] + 1e-6)
outputs <- dimension_outputs(ref, 20L, 0L)
outputs[["/variables/0/variable"]] <- exact("rating", "ordinal variables come first in the variable list")
outputs[["/variables/0/level"]] <- exact("ordinal", "scaling level")
outputs[["/variables/0/categories"]] <- exact(2L, "two categories")
for (j in 1:3) {
outputs[[sprintf("/variables/%d/loadings", j - 1L)]] <- seq_tol(ref$loadings[j, ], "PCA loadings of the quantified variables; dimension sign by the loading-sum convention", rel = "1e-8", abs = "1e-10")
}
outputs[["/categories/0/category"]] <- exact("low", "categories in the declared order")
outputs[["/categories/0/count"]] <- exact(as.integer(counts[["low"]]), "rows rated low")
outputs[["/categories/0/quantification"]] <- tol(y_low, "-sqrt(n_high/n_low): the centred, normalized non-decreasing quantification of a binary ordinal variable", rel = "1e-8")
outputs[["/categories/1/category"]] <- exact("high", "categories in the declared order")
outputs[["/categories/1/quantification"]] <- tol(y_high, "sqrt(n_low/n_high)", rel = "1e-8")
emit("STAT-MV-CATPCA-ORDINAL-001", "Categorical PCA with a binary ordinal variable", "axis_coverage",
list(ordinal = list("rating"), orders = list("rating: low < high"), numeric = list("x1", "x2"),
dimensions = 2L),
outputs,
list("A two-category variable has exactly one centred quantification with sum of squared values n (up to sign); the ordinal restriction low < high fixes the sign, so the quantification is -sqrt(n_high/n_low) for low and sqrt(n_low/n_high) for high and the princals optimum is the PCA of cor(quantified rating, x1, x2).",
"Sign convention replicated from the product: the loadings of each dimension sum to a positive value."))
# Case 3: a binary nominal variable and three numeric variables, one dimension.
rows <- complete.cases(d[c("site", "x1", "x2", "x3")])
sub <- d[rows, ]
counts <- table(sub$site)
y_east <- -sqrt(counts[["west"]] / counts[["east"]])
y_west <- sqrt(counts[["east"]] / counts[["west"]])
quant <- ifelse(sub$site == "east", y_east, y_west)
q <- cbind(site = quant, x1 = sub$x1, x2 = sub$x2, x3 = sub$x3)
ref <- pca_reference(q, 1L)
stopifnot(nrow(sub) == 18)
outputs <- dimension_outputs(ref, 18L, 2L)
outputs[["/variables/0/variable"]] <- exact("site", "nominal variables come first in the variable list")
outputs[["/variables/0/level"]] <- exact("nominal", "scaling level")
for (j in 1:4) {
outputs[[sprintf("/variables/%d/loadings", j - 1L)]] <- sign_seq(ref$loadings[j, ], "PCA loading magnitude; the nominal quantification's sign is free, so each loading is compared up to sign", rel = "1e-8", abs = "1e-10")
}
outputs[["/categories/0/category"]] <- exact("east", "categories in code-point order")
outputs[["/categories/0/count"]] <- exact(as.integer(counts[["east"]]), "rows at east")
outputs[["/artifact/variables/0/quantifications"]] <- sign_seq(c(y_east, y_west), "(-sqrt(n_west/n_east), sqrt(n_east/n_west)) up to a common sign: the pair flips together", rel = "1e-8")
emit("STAT-MV-CATPCA-NOMINAL-001", "Categorical PCA with a binary nominal variable, one dimension", "axis_coverage",
list(nominal = list("site"), numeric = list("x1", "x2", "x3"), dimensions = 1L),
outputs,
list("A binary nominal variable has one centred normalized quantification up to sign, so the optimum is the first principal component of cor(quantified site, x1, x2, x3) on the 18 rows complete on all four variables.",
"Because the nominal sign is free, the site quantifications and every loading (one per variable in one dimension) are compared sign-invariantly; eigenvalue, percent, alpha and loss are sign-free."))
cat("categorical principal components cases generated\n")
generators/r/multivariate_core.R
source("common.R", local = TRUE)
raw <- data.frame(
x = c(1.0, 1.2, 0.8, 1.1, 4.9, 5.2, 5.0, 4.8, 9.1, 8.8, 9.2, 9.0),
y = c(2.0, 2.1, 1.8, 2.2, 7.1, 6.8, 7.2, 6.9, 3.0, 3.2, 2.9, 3.1),
z = c(3.1, 2.8, 3.0, 3.3, 5.2, 5.0, 4.8, 5.1, 8.9, 9.1, 8.8, 9.2),
i1 = c(1, 2, 1, 2, 3, 4, 3, 4, 5, 4, 5, 4),
i2 = c(1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 4),
i3 = c(2, 1, 1, 2, 4, 3, 3, 4, 5, 4, 4, 5),
group = rep(c("A", "B", "C"), each = 4L),
cat = c("U", "U", "V", "U", "V", "W", "V", "V", "W", "W", "U", "W")
)
export_dataset(raw, "MV_CORE", "Constructed three-cluster and item-response matrix",
"R stats::prcomp, hclust, kmeans, manova, cor and analytic alpha",
"Constructed validation data; no third-party license.",
c(x="Float64", y="Float64", z="Float64", i1="Int64",
i2="Int64", i3="Int64", group="String", cat="String"))
d <- read_dataset("MV_CORE")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, note, tier="1e-9")
list(comparison="tolerance", value=dec(unname(x)), absolute_tolerance="0",
relative_tolerance=tier, rationale=paste("R closed-form reference:", note))
seqtol <- function(x, note, sign=FALSE)
list(comparison=if (sign) "sign_invariant_sequence" else "sequence",
values=as.list(dec(unname(x))), absolute_tolerance="0", relative_tolerance="1e-9",
rationale=paste("R reference:", note))
exact <- function(x, note) list(comparison="exact", value=x, rationale=note)
emit <- function(id, entry, menu, config, outputs, notes, kind="nominal", excluded=list(), repeat_count=1L) {
write_expected("multivariate", id, list(schema_version="2.0.0", case_id=id,
outputs=outputs, expected_error=NULL, deferred_outputs=list(), generator_run=run))
write_case("multivariate", id, list(
schema_version="2.0.0", case_id=id, version="1.0.0", family="multivariate",
entry_point=entry, menu_items=list(menu), title=paste("Multivariate", id),
case_kind=kind, dataset=list(path="reference_data/datasets/MV_CORE.json", sha256=""),
excluded_rows=excluded, configuration=config, "repeat"=repeat_count,
expected=list(path=paste0("reference_data/expected/multivariate/", id, ".json"), sha256=""),
oracle=list(oracle_class="independent_implementation", source="R stats and analytic formulae",
citation="R stats::prcomp/hclust/kmeans/manova/cor documentation",
generator=list(path="generators/r/multivariate_core.R", sha256=""),
independent=TRUE, convention_notes=c(notes, "R re-reads the exported CSV.")),
supported=TRUE, unsupported_reason=NULL,
requirement_ids=list("REQ-STAT-MV-001"), risk_ids=list("RISK-REFERENCE-001"),
review_reference=NULL))
}
pc <- stats::prcomp(d[c("x","y","z")], center=TRUE, scale.=TRUE)
emit("STAT-MV-PCA-001", "tabelz.executor.principal_components", "principalComponentsMenuItem",
list(columns=list("x","y","z"), scaling="correlation", n_components=2L),
list("/eigenvalues/0"=tol(pc$sdev[1]^2, "sample-correlation first eigenvalue"),
"/means"=seqtol(pc$center, "column means"),
"/loadings/0"=seqtol(pc$rotation[1,1:2], "first variable loadings", TRUE),
"/source_count"=exact(nrow(d), "all source rows")),
c("prcomp uses sample SD (n-1); each component may flip sign."))
scaled <- scale(d[c("x","y","z")])
hc <- stats::hclust(stats::dist(scaled), method="ward.D2")
# Labels align with the result's item_ids: complete-case row identities
# "<case>:<index>" in ascending string order (hierarchical_clustering and
# clustering result contracts), i.e. 0, 1, 10, 11, 2, ... for 12 rows. Until
# 2026-09-25 the partitions were listed in CSV order.
id_order <- function(id, n) order(paste0(id, ":", seq_len(n) - 1L), method = "radix")
emit("STAT-MV-HCLUST-001", "tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem",
list(columns=list("x","y","z"), linkage="ward", cut_k=3L),
list("/merge_heights"=seqtol(hc$height, "ward.D2 linkage heights"),
"/cluster_labels"=list(comparison="partition", values=as.list(as.integer(stats::cutree(hc,3L))[id_order("STAT-MV-HCLUST-001", nrow(d))]),
rationale="R Ward.D2 three-way partition")),
c("Rows standardized with sample SD; Ward.D2 uses squared Euclidean update."))
set.seed(11)
km <- stats::kmeans(scaled, centers=3L, nstart=50L, algorithm="Hartigan-Wong")
emit("STAT-MV-KMEANS-001", "tabelz.executor.clustering", "kmeansClusteringMenuItem",
list(columns=list("x","y","z"), k=3L, seed=11L, restarts=50L),
list("/inertia"=tol(km$tot.withinss, "unique three-cluster optimum"),
"/cluster_labels"=list(comparison="partition", values=as.list(as.integer(km$cluster)[id_order("STAT-MV-KMEANS-001", nrow(d))]),
rationale="Well-separated cluster partition independent of numeric labels")),
c("R Hartigan-Wong and Tabelz Lloyd agree on this unique separated optimum.") ,
kind="determinism", repeat_count=2L)
items <- as.matrix(d[c("i1","i2","i3")])
k <- ncol(items)
alpha <- k/(k-1) * (1-sum(apply(items,2,stats::var))/stats::var(rowSums(items)))
rbar <- mean(stats::cor(items)[upper.tri(stats::cor(items))])
standard_alpha <- k*rbar/(1+(k-1)*rbar)
emit("STAT-MV-ALPHA-001", "tabelz.executor.item_analysis", "itemAnalysisMenuItem",
list(items=list("i1","i2","i3")),
list("/cronbach_alpha"=tol(alpha, "sample-variance raw Cronbach alpha"),
"/standardized_alpha"=tol(standard_alpha, "mean-correlation standardized alpha")),
c("Raw alpha is k/(k-1)*(1-sum sample item variances/sample total variance)."))
emit("STAT-MV-CORR-001", "tabelz.analysis.correlation:compute_correlation",
"correlationMenuItem", list(columns=list("x","y","z"), metric="pearson", max_scatter_columns=0L),
list("/cells/0/value"=tol(1, "diagonal correlation"),
"/cells/1/value"=tol(stats::cor(d$x,d$y), "Pearson x-y correlation")),
c("Pearson product-moment correlation, complete pairwise finite observations."))
tab <- table(factor(d$group, levels=c("A","B","C")),
factor(d$cat, levels=c("U","V","W")))
chi <- suppressWarnings(stats::chisq.test(tab, correct=FALSE))$statistic
emit("STAT-MV-CA-001", "tabelz.executor.correspondence_analysis",
"correspondenceAnalysisMenuItem",
list(mode="categorical", row_column="group", column_column="cat"),
list("/chi_square"=tol(chi, "Pearson chi-square for contingency table"),
"/total_inertia"=tol(chi/sum(tab), "CA total inertia = chi-square / n")),
c("First-appearance row/column levels; inertia is chi-square divided by total count."))
mv <- stats::manova(cbind(x,y) ~ group, data=d)
pillai <- summary(mv, test="Pillai")$stats[1,]
wilks <- summary(mv, test="Wilks")$stats[1,]
emit("STAT-MV-MANOVA-001", "tabelz.executor.manova", "manovaMenuItem",
list(response_columns=list("x","y"), factor_columns=list("group")),
list("/joint_tests/0/value"=tol(unname(pillai["Pillai"]), "Pillai trace"),
"/joint_tests/1/value"=tol(unname(wilks["Wilks"]), "Wilks lambda")),
c("R manova one-way factor; Tabelz emits Pillai and Wilks only; no Hotelling-Lawley or Roy."))
cat("multivariate core references generated\n")
generators/r/multivariate_correlation_extensions.R
source("common.R", local = TRUE)
# Kendall's tau-b and partial correlation (Statistics > Correlation & regression).
# Every reference below is computed from the re-read MV_CORREXT CSV, never from the
# in-memory vectors.
i <- seq_len(16L)
constructed <- data.frame(
x = c(2.1, 3.4, 1.8, 5.2, 4.4, 6.1, 3.9, 7.3, 5.8, 8.2, 6.9, 9.4, 8.8, 10.1, 9.9, 11.6),
y = c(1.2, 2.9, 2.2, 4.1, 5.0, 4.6, 3.1, 6.8, 6.2, 7.0, 7.9, 8.1, 9.5, 9.2, 10.7, 10.3),
w = c(3.0, 2.2, 4.1, NA, 5.5, 5.1, 6.4, 6.0, 7.7, 7.1, 8.6, 8.0, 9.9, 9.1, 10.4, 11.0),
z = c(1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7),
c1 = c(0.5, 1.5, 0.9, 2.4, 2.0, 3.1, 1.7, 3.8, 2.9, 4.2, 3.3, 4.9, 4.4, 5.3, 5.0, 6.1),
stringsAsFactors = FALSE
)
constructed$c2 <- 2 * constructed$c1 + 3
constructed$c3 <- c(0.7, 0.2, 1.4, 0.9, 1.9, 1.1, 2.6, 1.8, NA, 2.4, 3.3, 2.9, 3.6, 3.1, 4.2, 3.8)
export_dataset(
constructed, "MV_CORREXT", "Constructed by multivariate_correlation_extensions.R",
"R stats::cor.test (method = 'kendall'), stats::lm residuals, stats::cor, stats::pt",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64", w = "Float64", z = "Float64", c1 = "Float64",
c2 = "Float64", c3 = "Float64")
)
d <- read_dataset("MV_CORREXT")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R stats", label, "from re-read MV_CORREXT CSV."))
prob <- function(x, label)
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = "1e-8",
rationale = paste("R stats", label, "from re-read MV_CORREXT CSV."))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
absent <- function(label) list(comparison = "absent", rationale = label)
emit <- function(id, entry, menu, title, kind, config, outputs, notes, citation, source_ref) {
write_expected("multivariate", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs, expected_error = NULL,
deferred_outputs = list(), generator_run = run))
write_case("multivariate", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "multivariate",
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/MV_CORREXT.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/multivariate/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/multivariate_correlation_extensions.R",
sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(if (grepl("KENDALL", id)) "REQ-STAT-MV-KENDALL-CORRELATION-001"
else "REQ-STAT-MV-PARTIAL-CORRELATION-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# ---- Kendall's tau-b --------------------------------------------------------------------------
kendall_entry <- "tabelz.analysis.correlation_extensions:compute_kendall"
kendall_menu <- "kendallCorrelationMenuItem"
kendall_cite <- "R Core Team, stats package help: cor.test (method = 'kendall'); Kendall (1970), Rank Correlation Methods, for S and the tie-corrected variance."
kendall_notes <- c(
"cor.test(method = 'kendall', exact = FALSE, continuity = FALSE): tau-b with ties and z = S / sqrt(var S) with the tie-corrected variance; S is concordant minus discordant pairs enumerated directly.",
"docs/manual/chapters/82-descriptive-reports-and-association.tex:97-101: 'accounts for ties in both variables. Select pairwise or listwise deletion. Exact inference requires no ties; the tie-corrected normal approximation'"
)
kendall_s <- function(x, y) {
s <- 0L
n <- length(x)
for (a in seq_len(n - 1)) for (b in (a + 1):n) {
s <- s + as.integer(sign(x[a] - x[b]) * sign(y[a] - y[b]))
}
s
}
kendall_pair <- function(x, y, prefix, exact_p = FALSE) {
keep <- is.finite(x) & is.finite(y)
x <- x[keep]
y <- y[keep]
asym <- stats::cor.test(x, y, method = "kendall", exact = FALSE, continuity = FALSE)
outputs <- list()
outputs[[paste0(prefix, "/n")]] <- exact(length(x), "complete pairs after deletion")
outputs[[paste0(prefix, "/tau_b")]] <- tol(asym$estimate, "cor.test Kendall tau-b")
outputs[[paste0(prefix, "/s")]] <- exact(kendall_s(x, y), "concordant minus discordant pairs by enumeration")
if (exact_p) {
ties <- any(duplicated(x)) || any(duplicated(y))
if (ties) {
outputs[[paste0(prefix, "/p_value")]] <- absent("Exact inference is unavailable with ties: p is withheld.")
} else {
ex <- stats::cor.test(x, y, method = "kendall", exact = TRUE)
outputs[[paste0(prefix, "/p_value")]] <- prob(ex$p.value, "cor.test exact Kendall p (no ties)")
}
} else {
outputs[[paste0(prefix, "/z")]] <- tol(asym$statistic, "cor.test tie-corrected z")
outputs[[paste0(prefix, "/p_value")]] <- prob(asym$p.value, "two-sided normal tail of the tie-corrected z")
}
outputs
}
# 001: three columns, pairwise deletion, asymptotic inference (z has ties).
outputs <- c(kendall_pair(d$x, d$y, "/pairs/0"), kendall_pair(d$x, d$z, "/pairs/1"),
kendall_pair(d$y, d$z, "/pairs/2"))
outputs[["/rows_analysed"]] <- exact(nrow(d), "all rows are analysed; deletion is per pair")
emit("STAT-MV-KENDALL-001", kendall_entry, kendall_menu, "Kendall tau-b tie-corrected normal",
"nominal", list(columns = list("x", "y", "z"), deletion = "pairwise", inference = "asymptotic"),
outputs, kendall_notes, kendall_cite, "R 4.6.1 stats::cor.test")
# 002: pairwise deletion with a missing value in w: (x, y) keeps 16 rows, pairs with w keep 15.
outputs <- c(kendall_pair(d$x, d$y, "/pairs/0"), kendall_pair(d$x, d$w, "/pairs/1"),
kendall_pair(d$y, d$w, "/pairs/2"))
emit("STAT-MV-KENDALL-002", kendall_entry, kendall_menu, "Kendall tau-b pairwise deletion",
"missing_excluded", list(columns = list("x", "y", "w"), deletion = "pairwise", inference = "asymptotic"),
outputs, c(kendall_notes, "Pairwise deletion: each pair drops only its own incomplete rows."),
kendall_cite, "R 4.6.1 stats::cor.test")
# 003: listwise deletion: every pair uses the 15 rows complete on x, y and w.
complete <- is.finite(d$x) & is.finite(d$y) & is.finite(d$w)
outputs <- c(kendall_pair(d$x[complete], d$y[complete], "/pairs/0"),
kendall_pair(d$x[complete], d$w[complete], "/pairs/1"),
kendall_pair(d$y[complete], d$w[complete], "/pairs/2"))
emit("STAT-MV-KENDALL-003", kendall_entry, kendall_menu, "Kendall tau-b listwise deletion",
"axis_coverage", list(columns = list("x", "y", "w"), deletion = "listwise", inference = "asymptotic"),
outputs, c(kendall_notes, "Listwise deletion: rows incomplete on any chosen column are dropped from every pair."),
kendall_cite, "R 4.6.1 stats::cor.test")
# 004: exact inference: (x, y) has no ties (exact p); pairs with z have ties (p withheld).
outputs <- c(kendall_pair(d$x, d$y, "/pairs/0", exact_p = TRUE),
kendall_pair(d$x, d$z, "/pairs/1", exact_p = TRUE))
emit("STAT-MV-KENDALL-004", kendall_entry, kendall_menu, "Kendall tau-b exact inference",
"axis_coverage", list(columns = list("x", "y", "z"), deletion = "pairwise", inference = "exact"),
outputs, c(kendall_notes, "cor.test(exact = TRUE) enumerates the permutation distribution of S for n < 50 without ties; with ties the p-value is withheld."),
kendall_cite, "R 4.6.1 stats::cor.test")
# ---- Partial correlation ---------------------------------------------------------------------
partial_entry <- "tabelz.analysis.correlation_extensions:compute_partial_correlation"
partial_menu <- "partialCorrelationMenuItem"
partial_cite <- "R Core Team, stats package help: lm, residuals, cor, pt."
partial_notes <- c(
"Residuals of lm(x ~ controls) and lm(y ~ controls) on the complete cases; r = cor of the residuals; df = n - k - 2 with k the rank of the controls beyond the intercept (lm's rank minus one); t = r sqrt(df / (1 - r^2)); two-sided t tail.",
"docs/manual/chapters/82-descriptive-reports-and-association.tex:103-107: 'correlates the residuals after regressing each response on an intercept and the declared controls, using complete cases. If the controls' numerical rank is k, the usual test has n-k-2 degrees of freedom.'"
)
partial_case <- function(id, title, kind, controls, extra_notes = character()) {
columns <- c("x", "y", controls)
complete <- Reduce(`&`, lapply(columns, function(name) is.finite(d[[name]])))
cc <- d[complete, columns]
formula_x <- stats::as.formula(paste("x ~", paste(controls, collapse = " + ")))
formula_y <- stats::as.formula(paste("y ~", paste(controls, collapse = " + ")))
fit_x <- stats::lm(formula_x, data = cc)
fit_y <- stats::lm(formula_y, data = cc)
k <- fit_x$rank - 1L
n <- nrow(cc)
df <- n - k - 2L
r <- stats::cor(stats::residuals(fit_x), stats::residuals(fit_y))
t <- r * sqrt(df / (1 - r^2))
p <- 2 * stats::pt(-abs(t), df)
outputs <- list(
"/n" = exact(n, "complete cases on x, y and the controls"),
"/incomplete_count" = exact(nrow(d) - n, "rows missing any chosen column"),
"/control_rank" = exact(k, "rank of the controls beyond the intercept (lm rank - 1)"),
"/df" = exact(df, "n - k - 2"),
"/zero_order_r" = tol(stats::cor(cc$x, cc$y), "Pearson r of x and y on the complete cases"),
"/partial_r" = tol(r, "correlation of the lm residuals"),
"/t_value" = tol(t, "t = r sqrt(df / (1 - r^2))"),
"/p_value" = prob(p, "two-sided t tail on df")
)
emit(id, partial_entry, partial_menu, title, kind,
list(x = "x", y = "y", controls = as.list(controls)), outputs,
c(partial_notes, extra_notes), partial_cite, "R 4.6.1 stats::lm, stats::cor, stats::pt")
}
partial_case("STAT-MV-PARTIAL-001", "Partial correlation one control", "nominal", "c1")
partial_case("STAT-MV-PARTIAL-002", "Partial correlation two controls with a missing control value",
"missing_excluded", c("c1", "c3"),
"c3 is missing on one row, so n = 15 complete cases and incomplete_count = 1.")
partial_case("STAT-MV-PARTIAL-003", "Partial correlation redundant control", "axis_coverage",
c("c1", "c2"),
"c2 = 2 c1 + 3 is collinear with c1 and the intercept: the controls have rank 1, df = n - 3 and the residuals equal those on c1 alone.")
cat("correlation extension cases generated\n")
generators/r/multivariate_factor.R
source("common.R", local = TRUE)
d <- read_dataset("MV_CORE")
# Statistician ruling, issue #14: r_xy r_xz / r_yz > 1, so the one-factor ML
# optimum lies on the boundary psi_x = 0 with lambda = (1, r_xy, r_xz) and
# psi = (0, 1 - r_xy^2, 1 - r_xz^2). Check it independently: with
# Sigma = lambda lambda' + diag(psi) and G = Sigma^-1 - Sigma^-1 R Sigma^-1, the
# free gradients 2 G lambda and G_jj (j = y, z) vanish and the derivative into
# positive psi_x, G_xx, is positive. R's factanal stops at its own lower = 0.005
# floor, a different optimization problem when the floor binds, so it is not used.
r <- stats::cor(d[c("x","y","z")])
stopifnot(r[1,2] * r[1,3] / r[2,3] > 1)
lambda <- c(1, r[1,2], r[1,3]); psi <- c(0, 1 - r[1,2]^2, 1 - r[1,3]^2)
Sigma <- outer(lambda, lambda) + diag(psi); Si <- solve(Sigma); G <- Si - Si %*% r %*% Si
stopifnot(max(abs(2 * G %*% lambda)) < 1e-10, max(abs(diag(G)[2:3])) < 1e-10, G[1,1] > 0)
eig <- eigen(r, symmetric=TRUE)$values
id <- "STAT-MV-FA-001"
tol <- function(x, note, tier="1e-6")
list(comparison="tolerance", value=dec(unname(x)), absolute_tolerance="0",
relative_tolerance=tier, rationale=paste("R maximum-likelihood factor reference:",note))
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/correlation_eigenvalues/0"=tol(eig[1],"sample-correlation largest eigenvalue","1e-9"),
"/status"=list(comparison="exact",value="heywood",rationale="Verified zero-uniqueness boundary optimum."),
"/uniquenesses/0"=list(comparison="tolerance",value="0",absolute_tolerance="1e-8",relative_tolerance="0",
rationale="psi_x = 0 at the boundary optimum."),
"/uniquenesses/1"=tol(psi[2],"1 - r_xy^2 at the boundary"),
"/uniquenesses/2"=tol(psi[3],"1 - r_xz^2 at the boundary"),
"/source_count"=list(comparison="exact",value=as.integer(nrow(d)),rationale="All complete rows")),
expected_error=NULL,deferred_outputs=list(),generator_run=generator_run(c("stats","jsonlite"))))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point="tabelz.executor.factor_analysis",
menu_items=list("factorAnalysisMenuItem"),title="One-factor maximum-likelihood fit",
case_kind="nominal",dataset=list(path="reference_data/datasets/MV_CORE.json",sha256=""),
excluded_rows=list(),configuration=list(columns=list("x","y","z"),n_factors=1L,
rotation="none"),"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats::factanal",
citation="R stats::factanal maximum-likelihood factor analysis documentation",
generator=list(path="generators/r/multivariate_factor.R",sha256=""),independent=TRUE,
convention_notes=list("Issue #14 ruling: the ML optimum is a verified zero-uniqueness boundary, derived in closed form and checked by its KKT conditions in R.",
"Until 2026-09-25 this case compared R factanal's lower = 0.005 floor value.",
"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
cat("factor reference generated\n")
generators/r/multivariate_factor_loadings.R
source("common.R",local=TRUE)
d <- read_dataset("MV_FACTOR")
set.seed(1959)
fit <- stats::factanal(d,factors=1L,rotation="none",
control=list(nstart=20L,opt=list(factr=1,pgtol=0,maxit=10000)))
sign_one <- function(x,note) list(comparison="sign_invariant_sequence",
values=list(dec(unname(x))),absolute_tolerance="0",relative_tolerance="1e-6",
rationale=note)
id <- "STAT-MV-FA-LOADINGS-001"
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/loadings/0"=sign_one(fit$loadings[1L,1L],"R ML first-variable loading, arbitrary factor sign"),
"/loadings/2"=sign_one(fit$loadings[3L,1L],"R ML third-variable loading, arbitrary factor sign")),
expected_error=NULL,deferred_outputs=list(),generator_run=generator_run(c("stats","jsonlite"))))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point="tabelz.executor.factor_analysis",
menu_items=list("factorAnalysisMenuItem"),title="Seeded ML factor-loading sign convention",
case_kind="axis_coverage",dataset=list(path="reference_data/datasets/MV_FACTOR.json",sha256=""),
excluded_rows=list(),configuration=list(columns=as.list(names(d)),n_factors=1L,rotation="none"),
"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="Seeded R stats::factanal",
citation="R stats::factanal maximum-likelihood factor analysis documentation",
generator=list(path="generators/r/multivariate_factor_loadings.R",sha256=""),
independent=TRUE,convention_notes=list("set.seed(1959) before ML fit; 20 starts, L-BFGS-B factr=1 (reltol does not apply to L-BFGS-B).",
"One factor avoids factor-order ambiguity; its common sign may flip.",
"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
cat("factor-loading sign reference generated\n")
generators/r/multivariate_factor_population.R
source("common.R",local=TRUE)
raw <- read_dataset("MV_FACTOR")
raw$a[4] <- NA_real_
export_dataset(raw,"MV_FACTOR_POP","Constructed factor matrix with one missing measurement",
"R stats::cor/eigen for complete-case factor preprocessing",
"Constructed validation data; no third-party license.",
c(a="Float64",b="Float64",c="Float64",d="Float64",e="Float64"))
d <- read_dataset("MV_FACTOR_POP")
selected <- d[-11L,,drop=FALSE]
selected <- selected[stats::complete.cases(selected),,drop=FALSE]
eig <- eigen(stats::cor(selected),symmetric=TRUE)$values[1]
id <- "STAT-MV-FA-POP-001"
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/source_count"=exact(nrow(d),"All CSV rows"),
"/excluded_count"=exact(1L,"One stable identity excluded"),
"/missing_count"=exact(1L,"One selected measurement missing"),
"/correlation_eigenvalues/0"=list(comparison="tolerance",value=dec(eig),
absolute_tolerance="0",relative_tolerance="1e-9",
rationale="R complete-case sample-correlation eigenvalue")),
expected_error=NULL,deferred_outputs=list(),generator_run=generator_run(c("stats","jsonlite"))))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point="tabelz.executor.factor_analysis",
menu_items=list("factorAnalysisMenuItem"),title="Factor preprocessing after missing and exclusion",
case_kind="missing_excluded",dataset=list(path="reference_data/datasets/MV_FACTOR_POP.json",sha256=""),
excluded_rows=list(10L),configuration=list(columns=as.list(names(d)),n_factors=1L),"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats::cor/eigen",
citation="R stats::cor and eigen documentation",
generator=list(path="generators/r/multivariate_factor_population.R",sha256=""),
independent=TRUE,convention_notes=list("R re-reads the exported CSV, excludes zero-based ordinal 10, then drops incomplete cases.",
"The eigenvalue checks factor preprocessing; separate seeded cases compare ML uniquenesses.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
cat("factor population reference generated\n")
generators/r/multivariate_factor_seeded.R
source("common.R", local=TRUE)
d <- read_dataset("MV_FACTOR")
run <- generator_run(c("stats","jsonlite"))
tol <- function(x,note,tier="1e-6")
list(comparison="tolerance",value=dec(unname(x)),absolute_tolerance="0",
relative_tolerance=tier,rationale=paste("Seeded R factanal reference:",note))
emit <- function(id,k,rotation,fit) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/uniquenesses/0"=tol(fit$uniquenesses[1],"first ML uniqueness"),
"/uniquenesses/2"=tol(fit$uniquenesses[3],"third ML uniqueness"),
"/correlation_eigenvalues/0"=tol(eigen(stats::cor(d),symmetric=TRUE)$values[1],
"sample correlation eigenvalue","1e-9")),
expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point="tabelz.executor.factor_analysis",
menu_items=list("factorAnalysisMenuItem"),title=paste("Seeded ML factor fit",rotation),
case_kind="nominal",dataset=list(path="reference_data/datasets/MV_FACTOR.json",sha256=""),
excluded_rows=list(),configuration=list(columns=as.list(names(d)),n_factors=as.integer(k),rotation=rotation),
"repeat"=1L,expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="Seeded R stats::factanal",
citation="R stats::factanal maximum-likelihood factor analysis documentation",
generator=list(path="generators/r/multivariate_factor_seeded.R",sha256=""),
independent=TRUE,convention_notes=list("set.seed(1959) before each fit; 20 starts; L-BFGS-B factr=1 (reltol does not apply to L-BFGS-B).",
"R uniqueness lower bound 0.005 is not active for these interior fits.",
"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
set.seed(1959)
fit1 <- stats::factanal(d,factors=1L,rotation="none",
control=list(nstart=20L,opt=list(factr=1,pgtol=0,maxit=10000)))
set.seed(1959)
fit2 <- stats::factanal(d,factors=2L,rotation="varimax",
control=list(nstart=20L,opt=list(factr=1,pgtol=0,maxit=10000)))
emit("STAT-MV-FA-003",1L,"none",fit1)
emit("STAT-MV-FA-VARIMAX-002",2L,"varimax",fit2)
cat("seeded factor references generated\n")
generators/r/multivariate_factor_stable.R
source("common.R", local=TRUE)
t <- seq_len(48L)
raw <- data.frame(
a = 2*sin(t*0.37)+0.24*cos(t*1.31),
b = 1.6*sin(t*0.37)+0.31*sin(t*1.07),
c = 1.5*cos(t*0.23)+0.28*cos(t*1.67),
d = 1.2*cos(t*0.23)+0.35*sin(t*1.47),
e = 0.8*sin(t*0.37)+0.7*cos(t*0.23)+0.42*cos(t*1.91)
)
export_dataset(raw,"MV_FACTOR","Constructed two-factor oscillatory measurements",
"R stats::factanal maximum-likelihood factor model",
"Constructed validation data; no third-party license.",
c(a="Float64",b="Float64",c="Float64",d="Float64",e="Float64"))
d <- read_dataset("MV_FACTOR")
run <- generator_run(c("stats","jsonlite"))
tol <- function(x,note,tier="1e-6")
list(comparison="tolerance",value=dec(unname(x)),absolute_tolerance="0",
relative_tolerance=tier,rationale=paste("R factanal reference:",note))
emit <- function(id,k,rotation,fit) {
expected <- list("/uniquenesses/0"=tol(fit$uniquenesses[1],"first ML uniqueness"),
"/uniquenesses/2"=tol(fit$uniquenesses[3],"third ML uniqueness"),
"/correlation_eigenvalues/0"=tol(eigen(stats::cor(d),symmetric=TRUE)$values[1],
"sample correlation eigenvalue","1e-9"))
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=expected,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point="tabelz.executor.factor_analysis",
menu_items=list("factorAnalysisMenuItem"),title=paste("Stable ML factor fit",rotation),
case_kind="nominal",dataset=list(path="reference_data/datasets/MV_FACTOR.json",sha256=""),
excluded_rows=list(),configuration=list(columns=as.list(names(d)),n_factors=as.integer(k),rotation=rotation),
"repeat"=1L,expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats::factanal",
citation="R stats::factanal maximum-likelihood factor analysis documentation",
generator=list(path="generators/r/multivariate_factor_stable.R",sha256=""),
independent=TRUE,convention_notes=list("R maximum likelihood with 20 starts and lower uniqueness 0.005; fitted uniquenesses are interior.",
"Orthogonal rotation cannot change uniquenesses.",
"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
set.seed(1959L)
fit1 <- stats::factanal(d,factors=1L,rotation="none",control=list(nstart=20L,opt=list(factr=1,pgtol=0,maxit=10000)))
set.seed(1959L)
fit2 <- stats::factanal(d,factors=2L,rotation="varimax",control=list(nstart=20L,opt=list(factr=1,pgtol=0,maxit=10000)))
emit("STAT-MV-FA-002",1L,"none",fit1)
emit("STAT-MV-FA-VARIMAX-001",2L,"varimax",fit2)
cat("stable factor references generated\n")
generators/r/multivariate_hard.R
source("common.R", local=TRUE)
base <- read_dataset("MV_CORE")
hard <- base
hard$x <- 1e8 + base$x
hard$y <- 1e8 + base$y
hard$z <- 1e8 + base$z
hard$i1 <- 1000000 + base$i1
hard$i2 <- 1000000 + base$i2
hard$i3 <- 1000000 + base$i3
export_dataset(hard,"MV_HARD","Large-offset multivariate and item-response data",
"R stats multivariate hard-numeric references",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64",z="Float64",i1="Int64",i2="Int64",
i3="Int64",group="String",cat="String"))
d <- read_dataset("MV_HARD")
fb <- read_dataset("MV_FACTOR")
fh <- as.data.frame(lapply(fb,function(x) 1e7+x))
names(fh) <- names(fb)
export_dataset(fh,"MV_FACTOR_HARD","Large-offset factor-analysis measurements",
"Seeded R stats::factanal hard-numeric reference",
"Constructed validation data; no third-party license.",
setNames(rep("Float64",ncol(fh)),names(fh)))
f <- read_dataset("MV_FACTOR_HARD")
cb <- read_dataset("MV_CA_COUNTS")
cb[c("U","V","W")] <- lapply(cb[c("U","V","W")],function(x) 1000000L+x)
export_dataset(cb,"MV_CA_HARD","Large-offset balanced contingency counts",
"R chisq.test correspondence-analysis identity",
"Constructed validation data; no third-party license.",
c(label="String",U="Int64",V="Int64",W="Int64"))
ca <- read_dataset("MV_CA_HARD")
run <- generator_run(c("stats","jsonlite"))
tol <- function(x,note,tier="1e-9") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R hard-numeric reference:",note))
emit <- function(id,entry,menu,dataset,config,outputs,notes) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Hard-numeric",id),
case_kind="hard_numeric",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats",
citation="R stats::prcomp, factanal, hclust, kmeans, manova and chisq.test documentation",
generator=list(path="generators/r/multivariate_hard.R",sha256=""),independent=TRUE,
convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-NUMERIC-001"),review_reference=NULL))
}
xyz <- list("x","y","z")
pc <- stats::prcomp(d[c("x","y","z")],center=TRUE,scale.=TRUE)
emit("STAT-MV-PCA-HARD-001","tabelz.executor.principal_components","principalComponentsMenuItem",
"MV_HARD",list(columns=xyz,scaling="correlation",n_components=2L),
list("/eigenvalues/0"=tol(pc$sdev[1]^2,"sample-correlation leading eigenvalue")),
c("Large common offset must cancel before computing covariance; prcomp uses n-1."))
set.seed(1959)
fa <- stats::factanal(f,factors=1L,rotation="none",
control=list(nstart=20L,opt=list(factr=1,pgtol=0,maxit=10000)))
emit("STAT-MV-FA-HARD-001","tabelz.executor.factor_analysis","factorAnalysisMenuItem",
"MV_FACTOR_HARD",list(columns=as.list(names(f)),n_factors=1L,rotation="none"),
list("/uniquenesses/0"=tol(fa$uniquenesses[1],"seeded ML uniqueness","1e-6")),
c("set.seed(1959), 20 starts, L-BFGS-B factr=1; R ML factor analysis on offset data."))
ord <- order(as.character(seq_len(nrow(d))-1L))
scaled <- scale(d[ord,c("x","y","z")])
hc <- stats::hclust(stats::dist(scaled),method="ward.D2")
emit("STAT-MV-HCLUST-HARD-001","tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem","MV_HARD",
list(columns=xyz,linkage="ward",cut_k=3L),
list("/merge_heights/0"=tol(hc$height[1],"standardized first Ward.D2 merge")),
c("Rows are in stable row-ID order and standardized with sample SD."))
set.seed(11)
km <- stats::kmeans(scaled,3L,nstart=50L,algorithm="Hartigan-Wong")
emit("STAT-MV-KMEANS-HARD-001","tabelz.executor.clustering","kmeansClusteringMenuItem",
"MV_HARD",list(columns=xyz,k=3L,seed=11L,restarts=50L),
list("/inertia"=tol(km$tot.withinss,"unique separated three-cluster within SS")),
c("set.seed(11), nstart=50; unique separated optimum, independent of label names."))
mv <- stats::manova(cbind(x,y)~group,data=d)
pillai <- summary(mv,test="Pillai")$stats[1,"Pillai"]
emit("STAT-MV-MANOVA-HARD-001","tabelz.executor.manova","manovaMenuItem",
"MV_HARD",list(response_columns=list("x","y"),factor_columns=list("group")),
list("/joint_tests/0/value"=tol(pillai,"Pillai trace after cancelling common offset")),
c("One-way MANOVA; Pillai trace computed from R hypothesis/error SSCP."))
items <- as.matrix(d[c("i1","i2","i3")]); k <- ncol(items)
alpha <- k/(k-1)*(1-sum(apply(items,2,stats::var))/stats::var(rowSums(items)))
emit("STAT-MV-ALPHA-HARD-001","tabelz.executor.item_analysis","itemAnalysisMenuItem",
"MV_HARD",list(items=list("i1","i2","i3")),
list("/cronbach_alpha"=tol(alpha,"analytic raw alpha from sample variances")),
c("Large identical item offsets must cancel in sample-variance alpha formula."))
mat <- as.matrix(ca[c("U","V","W")])
chi <- suppressWarnings(stats::chisq.test(mat,correct=FALSE))$statistic
emit("STAT-MV-CA-HARD-001","tabelz.executor.correspondence_analysis",
"correspondenceAnalysisMenuItem","MV_CA_HARD",
list(mode="count_table",row_label_column="label",count_columns=list("U","V","W")),
list("/chi_square"=tol(chi,"Pearson chi square with million-count baseline")),
c("Large balanced cells with small departures; chi-square uses observed-expected squared/expected."))
emit("STAT-MV-CORR-HARD-001","tabelz.analysis.correlation:compute_correlation",
"correlationMenuItem","MV_HARD",
list(columns=xyz,metric="covariance",max_scatter_columns=0L),
list("/cells/1/value"=tol(stats::cov(d$x,d$y),"sample covariance after common offsets")),
c("R sample covariance uses n-1 and centered products at 1e8 offset."))
cat("hard multivariate references generated\n")
generators/r/multivariate_mca.R
source("common.R", local = TRUE)
# Multiple correspondence analysis (Statistics > Multivariate > Multiple correspondence
# analysis): ca::mjca(lambda = "indicator") for the indicator-matrix inertias, masses, principal
# coordinates, contributions and squared cosines, ca::mjca(lambda = "adjusted") for Greenacre's
# adjusted percentages, and the Benzecri formula of the manual. Supplementary categories are
# checked against the textbook projection (column profile times the standard row coordinates).
# Every oracle reads the exported CSV.
library(ca)
run <- generator_run(c("ca", "stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/92-dimensions-and-categories.tex:100-107: simple CA of the complete indicator matrix; with Q variables and J active categories the raw inertias sum to (J - Q)/Q; contributions and representation quality follow simple CA; supplementary variables are projected after fitting.",
"docs/manual/chapters/92-dimensions-and-categories.tex:109-112: missing values are excluded or form a distinct category by the declared option; empty active categories are removed.",
"docs/manual/chapters/92-dimensions-and-categories.tex:115-124: Benzecri keeps lambda_s > 1/Q and reports a_s = [Q/(Q-1) (lambda_s - 1/Q)]^2; Greenacre's percentages use a different adjusted total; the map uses the raw principal coordinates."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
sign_one <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "sign_invariant_sequence", values = list(dec(unname(value))),
absolute_tolerance = abs, relative_tolerance = rel, rationale = label)
}
emit <- function(id, title, kind, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("multivariate", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "multivariate",
entry_point = "tabelz.analysis.multiple_correspondence_analysis:compute_mca",
menu_items = list("multipleCorrespondenceMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/MV_MCA.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/multivariate/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "independent_implementation",
source = "R ca::mjca (lambda = 'indicator' and 'adjusted') with the textbook supplementary projection",
citation = "Nenadic and Greenacre (2007), Correspondence analysis in R, with two- and three-dimensional graphics: the ca package, Journal of Statistical Software 20(3); Greenacre (2007) Correspondence Analysis in Practice, 2nd ed., chapters 18-19.",
generator = list(path = "generators/r/multivariate_mca.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-MV-MCA-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("multivariate", id, case)
}
set.seed(129)
n <- 24
frame <- data.frame(
region = sample(c("north", "south", "west"), n, TRUE, prob = c(0.4, 0.35, 0.25)),
size = sample(c("large", "small"), n, TRUE),
grade = sample(c("A", "B", "C"), n, TRUE, prob = c(0.3, 0.4, 0.3)),
sector = sample(c("public", "private", "mixed"), n, TRUE),
stringsAsFactors = FALSE
)
# Induce association so the leading inertia is distinct and interpretable.
frame$grade[frame$region == "north" & frame$size == "large"] <- "A"
frame$grade[frame$region == "west"] <- "C"
frame$region[3] <- NA
export_dataset(frame, "MV_MCA", "Twenty-four cases on four categorical variables with one missing region (seed 129)",
"R ca::mjca indicator-matrix MCA",
"Constructed validation data; no third-party license.",
c(region = "String", size = "String", grade = "String", sector = "String"))
d <- read_dataset("MV_MCA")
# Categories in code-point order with the recoded missing category last, as the product orders them.
level_order <- function(values) {
levels <- sort(unique(values[!is.na(values)]), method = "radix")
c(setdiff(levels, "(missing value)"), intersect(levels, "(missing value)"))
}
# Indicator MCA from first principles (Greenacre 2007, chapter 18), used to cross-check mjca
# and to project supplementary categories.
indicator <- function(data, active) {
blocks <- lapply(active, function(name) {
f <- factor(data[[name]], levels = level_order(data[[name]]))
z <- model.matrix(~ f - 1)
colnames(z) <- paste(name, levels(f), sep = ":")
z
})
do.call(cbind, blocks)
}
mca_reference <- function(data, active, supplementary = character()) {
rows <- complete.cases(data[active])
data <- data[rows, , drop = FALSE]
z <- indicator(data, active)
N <- nrow(z); Q <- length(active); J <- ncol(z)
P <- z / (N * Q)
r <- rep(1 / N, N); c <- colSums(P)
S <- diag(1 / sqrt(r)) %*% (P - r %o% c) %*% diag(1 / sqrt(c))
sv <- svd(S)
keep <- sv$d > 1e-12
lambda <- sv$d[keep]^2
row_standard <- diag(1 / sqrt(r)) %*% sv$u[, keep, drop = FALSE]
col_principal <- diag(1 / sqrt(c)) %*% sv$v[, keep, drop = FALSE] %*% diag(sqrt(lambda), nrow = sum(keep))
total <- (J - Q) / Q
stopifnot(abs(sum(lambda) - total) < 1e-9)
adjusted <- ifelse(lambda > 1 / Q, (Q / (Q - 1) * (lambda - 1 / Q))^2, 0)
greenacre_total <- Q / (Q - 1) * (sum(lambda^2) - (J - Q) / Q^2)
# Cross-check against ca::mjca on the same complete rows.
factors <- as.data.frame(lapply(data[active], function(v) factor(v, levels = level_order(v))))
fit <- mjca(factors, lambda = "indicator", nd = NA)
stopifnot(max(abs(fit$sv[seq_along(lambda)]^2 - lambda)) < 1e-9,
max(abs(fit$colmass - c)) < 1e-12,
max(abs(abs(fit$colpcoord[, seq_along(lambda)]) - abs(col_principal))) < 1e-7)
adjusted_fit <- mjca(factors, lambda = "adjusted", nd = NA)
stopifnot(abs(adjusted_fit$inertia.t - greenacre_total) < 1e-9,
max(abs(adjusted_fit$sv^2 - adjusted[adjusted > 0])) < 1e-9)
# Squared chi-square distance of an indicator column profile to the centroid: 1/(Q c_j) - 1.
distance2 <- rowSums(col_principal^2)
stopifnot(max(abs(distance2 - (1 / (Q * c) - 1))) < 1e-9)
counts <- colSums(z)
supp <- list()
for (name in supplementary) {
f <- factor(data[[name]], levels = level_order(data[[name]]))
for (level in levels(f)) {
members <- !is.na(f) & f == level
supp[[paste(name, level, sep = ":")]] <- list(
count = sum(members),
coordinates = colMeans(row_standard[members, , drop = FALSE]))
}
}
list(N = N, Q = Q, J = J, lambda = lambda, total = total, adjusted = adjusted,
adjusted_total = sum(adjusted), greenacre_total = greenacre_total, mass = c,
counts = counts, principal = col_principal, distance2 = distance2,
names = colnames(z), supp = supp)
}
outputs_for <- function(ref, dims, categories = seq_along(ref$names)) {
out <- list(
"/rows_analysed" = exact(ref$N, "complete rows on the active variables"),
"/variables" = exact(ref$Q, "active variables"),
"/categories" = exact(ref$J, "active categories"),
"/total_inertia" = tol(ref$total, "(J - Q)/Q"),
"/greenacre_denominator" = tol(ref$greenacre_total, "mjca adjusted inertia.t: Q/(Q-1) (sum lambda^2 - (J-Q)/Q^2)")
)
cumulative <- cumsum(ref$lambda) / ref$total
for (s in seq_along(ref$lambda)) {
base <- sprintf("/dimensions/%d", s - 1L)
out[[paste0(base, "/inertia")]] <- tol(ref$lambda[s], "squared singular value of the indicator CA (mjca sv^2)")
out[[paste0(base, "/raw_percent")]] <- tol(100 * ref$lambda[s] / ref$total, "100 lambda_s / total")
out[[paste0(base, "/cumulative_percent")]] <- tol(100 * cumulative[s], "cumulative raw percent")
out[[paste0(base, "/adjusted")]] <- tol(ref$adjusted[s], "Benzecri adjusted inertia [Q/(Q-1)(lambda - 1/Q)]^2, zero at or below 1/Q", abs = "1e-12")
out[[paste0(base, "/benzecri_percent")]] <- tol(100 * ref$adjusted[s] / ref$adjusted_total, "adjusted inertia over the sum of adjusted inertias", abs = "1e-12")
out[[paste0(base, "/greenacre_percent")]] <- tol(100 * ref$adjusted[s] / ref$greenacre_total, "adjusted inertia over Greenacre's average off-diagonal Burt inertia (mjca adjusted inertia.e)", abs = "1e-12")
}
for (j in categories) {
base <- sprintf("/categories_table/%d", j - 1L)
parts <- strsplit(ref$names[j], ":", fixed = TRUE)[[1]]
out[[paste0(base, "/variable")]] <- exact(parts[1], "active variable in selection order")
out[[paste0(base, "/category")]] <- exact(parts[2], "categories in sorted order within the variable")
out[[paste0(base, "/count")]] <- exact(as.integer(ref$counts[j]), "rows in the category")
out[[paste0(base, "/mass")]] <- tol(ref$mass[j], "column mass count/(NQ)")
if (dims == 1L) {
# One axis: the coordinate list has one element, compared up to the axis sign. With more
# axes each sign is independent, so only the sign-free contributions and cosines are
# compared (they fix the coordinate magnitudes).
out[[paste0(base, "/coordinates")]] <- sign_one(
ref$principal[j, 1], "principal coordinate (standard coordinate times singular value); axis sign arbitrary", rel = "1e-8")
}
for (s in seq_len(dims)) {
out[[sprintf("%s/contributions/%d", base, s - 1L)]] <- tol(
ref$mass[j] * ref$principal[j, s]^2 / ref$lambda[s], "mass times squared principal coordinate over the inertia", rel = "1e-8")
out[[sprintf("%s/cosine_squared/%d", base, s - 1L)]] <- tol(
ref$principal[j, s]^2 / ref$distance2[j], "squared coordinate over the squared chi-square distance to the centroid", rel = "1e-8")
}
}
out
}
active <- c("region", "size", "grade")
ref <- mca_reference(d, active)
stopifnot(ref$N == 23, ref$lambda[1] > ref$lambda[2] + 1e-6, ref$lambda[2] > ref$lambda[3] + 1e-6)
outputs <- outputs_for(ref, 2L)
outputs[["/incomplete_count"]] <- exact(1L, "one row has a missing region")
emit("STAT-MV-MCA-001", "Indicator MCA of three variables, complete rows", "nominal",
list(columns = list("region", "size", "grade"), missing = "complete", dimensions = 2L),
outputs,
list("ca::mjca(lambda = 'indicator', nd = NA) on the 23 complete rows: inertias are sv^2, principal coordinates are colpcoord; contributions mass g^2/lambda and squared cosines g^2/d^2 follow Greenacre (2007); d^2 is the chi-square distance of the indicator column profile to the centroid, 1/(Q c_j) - 1 (asserted in the generator).",
"Adjusted inertias by the Benzecri formula; Greenacre's denominator equals mjca(lambda = 'adjusted')$inertia.t (asserted in the generator).",
"The first three inertias are distinct, so each axis is unique up to sign; with two axes the signs are independent, so the sign-free contributions and squared cosines are compared here and the signed first-axis coordinates in STAT-MV-MCA-DIM1-001."))
outputs <- outputs_for(ref, 1L)
emit("STAT-MV-MCA-DIM1-001", "Indicator MCA reporting one dimension", "axis_coverage",
list(columns = list("region", "size", "grade"), missing = "complete", dimensions = 1L),
outputs,
list("Same fit as STAT-MV-MCA-001 with one requested dimension: every inertia is still reported; coordinates (compared up to the axis sign), contributions and cosines are reported for the first axis only."))
d_missing <- d
d_missing$region[is.na(d_missing$region)] <- "(missing value)"
ref <- mca_reference(d_missing, active)
stopifnot(ref$N == 24, any(grepl("(missing value)", ref$names, fixed = TRUE)))
missing_index <- which(ref$names == "region:(missing value)")
outputs <- outputs_for(ref, 2L)
outputs[["/incomplete_count"]] <- exact(0L, "the missing region is a category, so no row is incomplete")
outputs[[sprintf("/categories_table/%d/is_missing", missing_index - 1L)]] <- exact(TRUE, "the typed missing category")
emit("STAT-MV-MCA-MISSING-001", "Indicator MCA with missing values as a category", "missing_excluded",
list(columns = list("region", "size", "grade"), missing = "category", dimensions = 2L),
outputs,
list("The missing region is recoded as the category '(missing value)' before ca::mjca, so all 24 rows are active and region has four categories (J = 9).",
"The product lists the missing category last within its variable; the recoded label sorts after the text categories in R as well (asserted by the index used)."))
ref <- mca_reference(d, active, "sector")
outputs <- outputs_for(ref, 1L, categories = 1:3)
j <- length(ref$names)
for (name in names(ref$supp)) {
parts <- strsplit(name, ":", fixed = TRUE)[[1]]
base <- sprintf("/categories_table/%d", j)
outputs[[paste0(base, "/variable")]] <- exact(parts[1], "supplementary variable after the active categories")
outputs[[paste0(base, "/category")]] <- exact(parts[2], "supplementary categories in sorted order")
outputs[[paste0(base, "/count")]] <- exact(as.integer(ref$supp[[name]]$count), "complete rows in the supplementary category")
outputs[[paste0(base, "/supplementary")]] <- exact(TRUE, "flagged supplementary")
outputs[[paste0(base, "/coordinates")]] <- sign_one(
ref$supp[[name]]$coordinates[1], "supplementary principal coordinate on the single axis: mean of the standard row coordinates of its rows (Greenacre 2007, ch. 12 transition formula); axis sign arbitrary", rel = "1e-8")
j <- j + 1L
}
emit("STAT-MV-MCA-SUPP-001", "Indicator MCA with a supplementary variable", "axis_coverage",
list(columns = list("region", "size", "grade"), supplementary = list("sector"),
missing = "complete", dimensions = 1L),
outputs,
list("The active fit is that of STAT-MV-MCA-001; each sector category is projected as the average of the standard row coordinates (D_r^-1/2 U) of the complete rows holding it, which is the simple-CA supplementary-column principal coordinate.",
"Supplementary categories follow the active ones in the categories table, variable by variable, categories sorted; one dimension is requested so the single coordinate is compared up to the axis sign."))
cat("multiple correspondence analysis cases generated\n")
generators/r/multivariate_mds.R
source("common.R", local = TRUE)
# Multidimensional scaling (Statistics > Multivariate > Multidimensional scaling).
# Classical scaling is checked against stats::cmdscale (eig = TRUE); the ordinal SMACOF
# model is checked on an exactly Euclidean matrix, where the monotone fit is perfect: the
# stress is zero after the first Guttman transform and the configuration is the classical
# one rescaled to the normalized disparities. Every oracle reads the exported CSV.
run <- generator_run(c("stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/92-dimensions-and-categories.tex:20-24: classical scaling eigendecomposes B = -J D^(2) J / 2; coordinates use eigenvectors times square roots of positive eigenvalues; the goodness-of-fit ratios use the absolute and the positive eigenvalue totals.",
"docs/manual/chapters/92-dimensions-and-categories.tex:12-17: computed distances use complete rows, standardization to unit SD by default, Euclidean, city-block or correlation sqrt(2(1 - r)) distance; a supplied matrix is never standardized.",
"docs/manual/chapters/92-dimensions-and-categories.tex:45-51: ordinal scaling fits monotone disparities minimizing stress; raw stress, normalized stress and Kruskal stress-1 have different denominators."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
seq_tol <- function(values, label, rel = "1e-9", abs = "0") {
list(comparison = "sequence", values = as.list(dec(unname(values))),
absolute_tolerance = abs, relative_tolerance = rel, rationale = label)
}
# Tabelz sign convention for a classical axis: the largest absolute coordinate is positive.
sign_fix <- function(points) {
for (s in seq_len(ncol(points))) {
if (points[which.max(abs(points[, s])), s] < 0) points[, s] <- -points[, s]
}
points
}
emit <- function(id, title, kind, dataset, configuration, outputs, notes, source, citation,
excluded = list()) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("multivariate", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "multivariate",
entry_point = "tabelz.analysis.multidimensional_scaling:compute_mds",
menu_items = list("multidimensionalScalingMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/multivariate/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source,
citation = citation,
generator = list(path = "generators/r/multivariate_mds.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.",
manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-MV-MDS-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("multivariate", id, case)
}
cmd_source <- "R stats::cmdscale(d, k, eig = TRUE)"
cmd_citation <- "R Core Team, stats package help: cmdscale (classical multidimensional scaling, Torgerson 1958; Mardia 1978 goodness-of-fit ratios)."
classical_outputs <- function(fit, k, objects, prefix = "") {
points <- sign_fix(fit$points)
out <- list(
"/dimensions" = exact(k, "requested dimensions, all with positive eigenvalues"),
"/gof_absolute" = tol(fit$GOF[[1]], "cmdscale GOF[1]: retained eigenvalues over the sum of absolute eigenvalues"),
"/gof_positive" = tol(fit$GOF[[2]], "cmdscale GOF[2]: retained eigenvalues over the sum of positive eigenvalues")
)
for (i in seq_along(fit$eig)) {
out[[sprintf("/eigenvalues/%d/eigenvalue", i - 1L)]] <- tol(
fit$eig[[i]], "cmdscale eigenvalue of the double-centred matrix (roundoff-level ones allowed 1e-9 absolute)",
abs = "1e-9")
out[[sprintf("/eigenvalues/%d/retained", i - 1L)]] <- exact(i <= k, "the first k eigenvalues are retained")
}
for (i in objects) {
out[[sprintf("/objects/%d/coordinates", i - 1L)]] <- seq_tol(
points[i, ], "cmdscale points (sqrt(eigenvalue) scaling) with the largest absolute coordinate of each axis made positive",
abs = "1e-12")
}
out
}
# ---- Supplied matrices -------------------------------------------------------------------
conveyor <- data.frame(A = c(0, 1, 3), B = c(1, 0, 2), C = c(3, 2, 0))
export_dataset(conveyor, "MV_MDS_CONVEYOR", "Three conveyor stations 1, 2 and 3 m apart (manual example)",
"R stats cmdscale; Tabelz manual chapter 92 conveyor example",
"Constructed validation data; no third-party license.",
c(A = "Float64", B = "Float64", C = "Float64"))
d <- read_dataset("MV_MDS_CONVEYOR")
m <- as.matrix(d[c("A", "B", "C")])
fit <- cmdscale(m, k = 1, eig = TRUE)
stopifnot(abs(fit$GOF[[1]] - 1) < 1e-12)
outputs <- classical_outputs(fit, 1L, 1:3)
outputs[["/objects/0/label"]] <- exact("A", "matrix columns name the objects when no label column is given")
outputs[["/rows_used"]] <- exact(3L, "three objects")
outputs[["/missing_count"]] <- exact(0L, "no missing entry")
emit("STAT-MV-MDS-MATRIX-001", "Classical scaling of a one-dimensional supplied matrix", "nominal",
"MV_MDS_CONVEYOR",
list(source = "matrix", columns = list("A", "B", "C"), model = "classical", dimensions = 1L),
outputs,
list("stats::cmdscale on the supplied 3 x 3 matrix with k = 1; the distances 1, 2, 3 are exactly collinear so the fit is perfect (coordinates -4/3, -1/3, 5/3 after the sign convention, GOF 1).",
"Objects are labelled by the matrix column names in the absence of a label column."),
cmd_source, cmd_citation)
# A non-Euclidean 5 x 5 dissimilarity matrix (triangle inequality violated for 1-2-3).
non_euclid <- matrix(c(
0.0, 1.0, 1.0, 4.0, 5.0,
1.0, 0.0, 3.0, 2.5, 4.5,
1.0, 3.0, 0.0, 3.5, 2.0,
4.0, 2.5, 3.5, 0.0, 1.5,
5.0, 4.5, 2.0, 1.5, 0.0), 5, byrow = TRUE)
colnames(non_euclid) <- c("P", "Q", "R", "S", "T")
sites <- data.frame(site = c("north", "east", "south", "west", "centre"), non_euclid,
stringsAsFactors = FALSE)
export_dataset(sites, "MV_MDS_SITES", "Five sites with a non-Euclidean dissimilarity matrix and a label column",
"R stats cmdscale with negative eigenvalues",
"Constructed validation data; no third-party license.",
c(site = "String", P = "Float64", Q = "Float64", R = "Float64", S = "Float64",
T = "Float64"))
d <- read_dataset("MV_MDS_SITES")
m <- as.matrix(d[c("P", "Q", "R", "S", "T")])
fit <- cmdscale(m, k = 2, eig = TRUE)
stopifnot(any(fit$eig < -1e-6), fit$eig[[1]] > fit$eig[[2]] + 1e-6, fit$eig[[2]] > 1e-6)
outputs <- classical_outputs(fit, 2L, 1:5)
outputs[["/objects/0/label"]] <- exact("north", "labels from the label column")
outputs[["/objects/4/label"]] <- exact("centre", "labels from the label column")
emit("STAT-MV-MDS-MATRIX-002", "Classical scaling of a non-Euclidean matrix with labels", "axis_coverage",
"MV_MDS_SITES",
list(source = "matrix", columns = list("P", "Q", "R", "S", "T"), labels = "site",
model = "classical", dimensions = 2L),
outputs,
list("stats::cmdscale with k = 2 on a matrix that violates the triangle inequality: negative eigenvalues are reported and the two goodness-of-fit ratios differ.",
"Eigenvalues are distinct, so each axis is unique up to sign; the sign convention (largest absolute coordinate positive) is applied to the R points."),
cmd_source, cmd_citation)
# ---- Ordinal SMACOF on an exactly Euclidean matrix ---------------------------------------
config <- matrix(c(0, 0, 4, 0.5, 3, 3, -1, 2.5, 1, 1), ncol = 2, byrow = TRUE)
exact_d <- as.matrix(dist(config))
colnames(exact_d) <- c("a", "b", "c", "d", "e")
plane <- data.frame(exact_d)
export_dataset(plane, "MV_MDS_PLANE", "Euclidean distances of five points in the plane (perfect two-dimensional fit)",
"R stats dist and cmdscale",
"Constructed validation data; no third-party license.",
c(a = "Float64", b = "Float64", c = "Float64", d = "Float64", e = "Float64"))
d <- read_dataset("MV_MDS_PLANE")
m <- as.matrix(d[c("a", "b", "c", "d", "e")])
fit <- cmdscale(m, k = 2, eig = TRUE)
stopifnot(fit$eig[[1]] > fit$eig[[2]] + 1e-6, abs(fit$eig[[3]]) < 1e-9)
n <- nrow(m)
pairs <- n * (n - 1) / 2
upper <- m[upper.tri(m)]
# Disparities normalized to sum of squares n(n-1)/2; the perfect-fit configuration is the
# classical one rescaled by sqrt(pairs / sum d^2) and centred (it already is).
scale <- sqrt(pairs / sum(upper^2))
points <- sign_fix(fit$points * scale)
for (ties in c("secondary", "primary")) {
outputs <- list(
"/dimensions" = exact(2L, "two requested dimensions"),
"/converged" = exact(TRUE, "zero stress: the first Guttman transform reproduces the start"),
"/iterations" = exact(1L, "convergence is declared at the first iteration (stress change 0 < 1e-6)"),
"/raw_stress" = tol(0, "perfect monotone fit", abs = "1e-20"),
"/normalized_stress" = tol(0, "perfect monotone fit", abs = "1e-20"),
"/smacof_stress" = tol(0, "sqrt of zero normalized stress", abs = "1e-10"),
"/stress_1" = tol(0, "Kruskal stress-1 of a perfect fit", abs = "1e-10"),
"/starts/0/start" = exact("classical", "the only start is the classical configuration"),
"/starts/0/normalized_stress" = tol(0, "perfect monotone fit", abs = "1e-20"),
"/eigenvalues/0/eigenvalue" = tol(fit$eig[[1]], "cmdscale eigenvalue"),
"/eigenvalues/1/eigenvalue" = tol(fit$eig[[2]], "cmdscale eigenvalue")
)
for (i in 1:n) {
outputs[[sprintf("/objects/%d/coordinates", i - 1L)]] <- seq_tol(
points[i, ], "classical coordinates rescaled so the fitted distances equal the normalized disparities; sign convention applied",
rel = "1e-8", abs = "1e-10")
}
emit(sprintf("STAT-MV-MDS-ORDINAL-%03d", match(ties, c("secondary", "primary"))),
paste("Ordinal SMACOF on an exactly Euclidean matrix,", ties, "ties"),
if (ties == "secondary") "nominal" else "axis_coverage", "MV_MDS_PLANE",
list(source = "matrix", columns = list("a", "b", "c", "d", "e"), model = "ordinal",
dimensions = 2L, ties = ties),
outputs,
list("The matrix holds the exact Euclidean distances of five plane points: the classical start reproduces every distance, the monotone regression of distances on dissimilarities is the identity, and the raw stress is 0 at the first Guttman transform.",
"Disparities are normalized to sum of squares n(n-1)/2, so the final configuration equals the classical solution times sqrt(n(n-1)/2 / sum d_ij^2); no tie is present in the matrix so the tie rule cannot change the result.",
"Oracle: stats::cmdscale for the classical solution plus the stated analytic rescaling."),
"R stats::cmdscale and analytic perfect-fit stress", cmd_citation)
}
# ---- Dissimilarities computed from variables ----------------------------------------------
set.seed(146)
vars <- data.frame(
unit = sprintf("u%02d", 1:12),
v1 = round(rnorm(12, 10, 2), 2), v2 = round(rnorm(12, 50, 8), 1),
v3 = round(runif(12, 0, 5), 2), v4 = round(rnorm(12, 0, 1), 3),
stringsAsFactors = FALSE)
vars$v3[7] <- NA
export_dataset(vars, "MV_MDS_VARS", "Twelve units on four scale variables with one missing value (seed 146)",
"R stats dist, cor and cmdscale",
"Constructed validation data; no third-party license.",
c(unit = "String", v1 = "Float64", v2 = "Float64", v3 = "Float64", v4 = "Float64"))
d <- read_dataset("MV_MDS_VARS")
complete <- d[complete.cases(d[c("v1", "v2", "v3", "v4")]), ]
stopifnot(nrow(complete) == 11)
x <- as.matrix(complete[c("v1", "v2", "v3", "v4")])
fit <- cmdscale(dist(scale(x)), k = 2, eig = TRUE)
stopifnot(fit$eig[[1]] > fit$eig[[2]] + 1e-6, fit$eig[[2]] > fit$eig[[3]] + 1e-6)
outputs <- classical_outputs(fit, 2L, 1:11)
outputs[["/rows_used"]] <- exact(11L, "one row has a missing v3")
outputs[["/missing_count"]] <- exact(1L, "one row has a missing v3")
outputs[["/objects/0/label"]] <- exact("u01", "labels from the unit column")
outputs[["/objects/6/label"]] <- exact("u08", "the incomplete row u07 is left out, so the seventh object is u08")
emit("STAT-MV-MDS-VARIABLES-001", "Classical scaling of standardized Euclidean distances between rows", "nominal",
"MV_MDS_VARS",
list(source = "variables", columns = list("v1", "v2", "v3", "v4"), labels = "unit",
distance = "euclidean", objects = "rows", standardize = TRUE, model = "classical",
dimensions = 2L),
outputs,
list("dist(scale(x)) on the 11 complete rows (scale uses the sample SD, ddof 1, as the product does), then stats::cmdscale with k = 2.",
"Roundoff-level eigenvalues (n - 1 - 4 of them) are compared with an absolute tolerance of 1e-9."),
cmd_source, cmd_citation)
fit <- cmdscale(dist(x, method = "manhattan"), k = 2, eig = TRUE)
stopifnot(fit$eig[[1]] > fit$eig[[2]] + 1e-6, fit$eig[[2]] > fit$eig[[3]] + 1e-6)
outputs <- classical_outputs(fit, 2L, 1:11)
outputs[["/rows_used"]] <- exact(11L, "one row has a missing v3")
emit("STAT-MV-MDS-CITYBLOCK-001", "Classical scaling of unstandardized city-block distances", "axis_coverage",
"MV_MDS_VARS",
list(source = "variables", columns = list("v1", "v2", "v3", "v4"), labels = "unit",
distance = "cityblock", objects = "rows", standardize = FALSE, model = "classical",
dimensions = 2L),
outputs,
list("dist(x, method = 'manhattan') on the raw complete rows (standardize off), then stats::cmdscale with k = 2; city-block distances are not Euclidean so negative eigenvalues appear."),
cmd_source, cmd_citation)
r <- cor(x)
corr_d <- sqrt(2 * (1 - r))
diag(corr_d) <- 0
fit <- cmdscale(as.dist(corr_d), k = 2, eig = TRUE)
stopifnot(fit$eig[[1]] > fit$eig[[2]] + 1e-6, fit$eig[[2]] > fit$eig[[3]] + 1e-6)
outputs <- classical_outputs(fit, 2L, 1:4)
outputs[["/rows_used"]] <- exact(11L, "one row has a missing v3")
outputs[["/objects/0/label"]] <- exact("v1", "variables are the objects")
outputs[["/objects/3/label"]] <- exact("v4", "variables are the objects")
emit("STAT-MV-MDS-CORRELATION-001", "Classical scaling of correlation distances between variables", "axis_coverage",
"MV_MDS_VARS",
list(source = "variables", columns = list("v1", "v2", "v3", "v4"), labels = "unit",
distance = "correlation", objects = "variables", model = "classical", dimensions = 2L),
outputs,
list("d_ij = sqrt(2 (1 - r_ij)) with r the Pearson correlation between variables over the 11 complete rows (cor is scale-invariant, so the standardize-for-correlation option is immaterial here), then stats::cmdscale with k = 2 on the four variables."),
cmd_source, cmd_citation)
cat("multidimensional scaling cases generated\n")
generators/r/multivariate_minimum.R
source("common.R",local=TRUE)
core <- read_dataset("MV_CORE")
factor <- read_dataset("MV_FACTOR")
run <- generator_run(c("stats","jsonlite"))
types <- c(x="Float64",y="Float64",z="Float64",i1="Int64",i2="Int64",
i3="Int64",group="String",cat="String")
tol <- function(x,note,tier="1e-9") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R boundary reference:",note))
emit <- function(id,entry,menu,dataset,config,outputs,notes) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Minimum-sample",id),
case_kind="boundary",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats and closed-form formulae",
citation="R stats::prcomp, factanal, hclust, kmeans, manova, chisq.test and cor documentation",
generator=list(path="generators/r/multivariate_minimum.R",sha256=""),independent=TRUE,
convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
two <- core[c(1L,5L),]
export_dataset(two,"MV_MIN_TWO","Two distinct observations",
"R closed-form two-row multivariate boundary",
"Constructed validation data; no third-party license.",types)
d <- read_dataset("MV_MIN_TWO")
pc <- stats::prcomp(d[c("x","y")],center=TRUE,scale.=TRUE)
emit("STAT-MV-PCA-MIN-001","tabelz.executor.principal_components","principalComponentsMenuItem",
"MV_MIN_TWO",list(columns=list("x","y"),scaling="correlation",n_components=1L),
list("/eigenvalues/0"=tol(pc$sdev[1]^2,"two-point sample-correlation eigenvalue")),
c("Two complete rows are the minimum accepted for PCA."))
hc <- stats::hclust(stats::dist(scale(d[c("x","y")])),method="ward.D2")
emit("STAT-MV-HCLUST-MIN-001","tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem","MV_MIN_TWO",
list(columns=list("x","y"),linkage="ward",cut_k=2L),
list("/merge_heights/0"=tol(hc$height[1],"only Ward.D2 merge height")),
c("Two distinct rows give one unambiguous merge."))
emit("STAT-MV-KMEANS-MIN-001","tabelz.executor.clustering","kmeansClusteringMenuItem",
"MV_MIN_TWO",list(columns=list("x","y"),k=2L,seed=11L,restarts=50L),
list("/inertia"=tol(0,"two singleton clusters have zero within SS")),
c("With k=n=2 every observation forms a singleton, independent of initialization."))
items <- as.matrix(d[c("i1","i3")]); k <- ncol(items)
alpha <- k/(k-1)*(1-sum(apply(items,2,stats::var))/stats::var(rowSums(items)))
emit("STAT-MV-ALPHA-MIN-001","tabelz.executor.item_analysis","itemAnalysisMenuItem",
"MV_MIN_TWO",list(items=list("i1","i3")),
list("/cronbach_alpha"=tol(alpha,"two-row two-item sample-variance alpha")),
c("Two rows and two items are the documented minimum for item analysis."))
emit("STAT-MV-CORR-MIN-001","tabelz.analysis.correlation:compute_correlation",
"correlationMenuItem","MV_MIN_TWO",
list(columns=list("x","y"),metric="pearson",max_scatter_columns=0L),
list("/cells/1/value"=tol(stats::cor(d$x,d$y),"two-row Pearson r is exactly signed one")),
c("Two distinct pairs are the smallest nonconstant sample for Pearson r."))
f <- factor[c(1L,6L,12L,18L),c("a","b","c")]
export_dataset(f,"MV_MIN_FACTOR","Four complete observations on three variables",
"R seeded ML factor-analysis minimum-row boundary",
"Constructed validation data; no third-party license.",
setNames(rep("Float64",ncol(f)),names(f)))
f <- read_dataset("MV_MIN_FACTOR")
eig <- eigen(stats::cor(f),symmetric=TRUE)$values[1]
emit("STAT-MV-FA-MIN-001","tabelz.executor.factor_analysis","factorAnalysisMenuItem",
"MV_MIN_FACTOR",list(columns=as.list(names(f)),n_factors=1L,rotation="none"),
list("/correlation_eigenvalues/0"=tol(eig,"sample-correlation eigenvalue at n=p+1")),
c("Four complete rows exceed the three selected columns by one; only the closed-form correlation eigenvalue is compared because a just-identified ML uniqueness fit is nonunique."))
m <- core[c(1:3,5:7),]
export_dataset(m,"MV_MIN_MANOVA","Two groups with three observations each",
"R manova minimum-estimable one-way boundary",
"Constructed validation data; no third-party license.",types)
m <- read_dataset("MV_MIN_MANOVA")
mv <- stats::manova(cbind(x,y)~group,data=m)
pillai <- summary(mv,test="Pillai")$stats[1,"Pillai"]
emit("STAT-MV-MANOVA-MIN-001","tabelz.executor.manova","manovaMenuItem",
"MV_MIN_MANOVA",list(response_columns=list("x","y"),factor_columns=list("group")),
list("/joint_tests/0/value"=tol(pillai,"two-group Pillai trace")),
c("Each group has three observations for two responses; residual SSCP is estimable."))
ct <- data.frame(label=c("A","B"),U=c(1L,2L),V=c(2L,1L))
export_dataset(ct,"MV_MIN_CA","Two-by-two positive count table",
"R chisq.test correspondence-analysis boundary",
"Constructed validation data; no third-party license.",
c(label="String",U="Int64",V="Int64"))
ct <- read_dataset("MV_MIN_CA")
chi <- suppressWarnings(stats::chisq.test(as.matrix(ct[c("U","V")]),correct=FALSE))$statistic
emit("STAT-MV-CA-MIN-001","tabelz.executor.correspondence_analysis",
"correspondenceAnalysisMenuItem","MV_MIN_CA",
list(mode="count_table",row_label_column="label",count_columns=list("U","V")),
list("/chi_square"=tol(chi,"Pearson chi-square of a two-by-two table")),
c("Two rows and two columns give one correspondence-analysis dimension."))
cat("minimum multivariate references generated\n")
generators/r/multivariate_model_clustering.R
source("common.R", local = TRUE)
# Model-based clustering (Statistics > Multivariate > Model-based clustering): Gaussian
# mixtures against mclust (VVI = diagonal, VVV = full covariance, tightened EM tolerance) on
# two well-separated clusters, the one-component fits in closed form, the silhouette of the
# hard partition against cluster::silhouette, and a one-class latent-class fit in closed
# form. Every oracle reads the exported CSV.
suppressMessages(library(mclust))
library(cluster)
run <- generator_run(c("mclust", "cluster", "stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/90-learning-and-mixtures.tex:93-99: a Gaussian mixture uses diagonal covariance by default with a full-covariance option; a latent class model uses conditional independence of categorical variables within each class.",
"docs/manual/chapters/90-learning-and-mixtures.tex:101-112: L = prod_i sum_g pi_g f_g(y_i); the default search fits K from 10 seeded starts per K and chooses the smallest BIC = -2 log L + m log n, m the number of free parameters; the selected fit is the best converged start, not a proven global maximum."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
emit <- function(id, title, kind, dataset, configuration, outputs, notes, source, citation) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("multivariate", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "multivariate",
entry_point = "tabelz.analysis.model_based_clustering:compute_model_clustering",
menu_items = list("modelClusteringMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/multivariate/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source,
citation = citation,
generator = list(path = "generators/r/multivariate_model_clustering.R",
sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-MV-MCLUST-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("multivariate", id, case)
}
set.seed(145)
group <- rep(1:2, each = 15)
frame <- data.frame(
length = round(ifelse(group == 1, rnorm(30, 10, 1.0), rnorm(30, 20, 1.5)), 3),
width = round(ifelse(group == 1, rnorm(30, 5, 0.8), rnorm(30, 2, 0.6)), 3)
)
frame$width[9] <- NA
export_dataset(frame, "MV_MIXTURE", "Thirty parts from two well-separated bivariate normal clusters, one missing width (seed 145)",
"R mclust Mclust",
"Constructed validation data; no third-party license.",
c(length = "Float64", width = "Float64"))
d <- read_dataset("MV_MIXTURE")
x <- as.matrix(d[complete.cases(d), ])
n <- nrow(x); p <- ncol(x)
stopifnot(n == 29)
mclust_source <- "R mclust::Mclust (VVI/VVV) with emControl(tol = 1e-12); cluster::silhouette"
mclust_citation <- "Scrucca, Fop, Murphy and Raftery (2016) mclust 5: clustering, classification and density estimation using Gaussian finite mixture models, The R Journal 8(1); Rousseeuw (1987) Silhouettes, J. Comput. Appl. Math. 20."
control <- emControl(tol = c(1e-12, 1e-12), itmax = c(10000, 10000))
# One-component closed form: MLE mean and (population) variances per variable.
one_loglik <- function(x) {
sum(vapply(seq_len(ncol(x)), function(j) {
v <- x[, j]
sum(dnorm(v, mean(v), sqrt(mean((v - mean(v))^2)), log = TRUE))
}, 0))
}
bic_of <- function(loglik, m, n) -2 * loglik + m * log(n)
parameters <- function(k, p, full) k - 1 + k * p + k * (if (full) p * (p + 1) / 2 else p)
fit1 <- one_loglik(x)
fit2 <- Mclust(x, G = 2, modelNames = "VVI", control = control, verbose = FALSE)
fit2_full <- Mclust(x, G = 2, modelNames = "VVV", control = control, verbose = FALSE)
stopifnot(!is.null(fit2), !is.null(fit2_full), length(unique(fit2$classification)) == 2)
# The hard partition is the two true clusters (well separated); silhouette on standardized
# Euclidean distances.
sil <- silhouette(fit2$classification, dist(scale(x)))
m1 <- parameters(1, p, FALSE); m2 <- parameters(2, p, FALSE); m2f <- parameters(2, p, TRUE)
stopifnot(bic_of(fit2$loglik, m2, n) < bic_of(fit1, m1, n))
# mclust's BIC is 2 log L - m log n; check the parameter count agrees with the product's m.
stopifnot(abs(fit2$bic - (2 * fit2$loglik - m2 * log(n))) < 1e-6,
abs(fit2_full$bic - (2 * fit2_full$loglik - m2f * log(n))) < 1e-6)
em_note <- "Log-likelihoods and BIC are compared at 1e-7 relative: both EM runs converge to the same optimum on well-separated clusters (mclust tol 1e-12; the product stops at a 1e-8 change per observation and regularizes each component variance by 1e-6 times the sample variance, a second-order effect at the maximum)."
outputs <- list(
"/n" = exact(n, "complete rows"),
"/missing_count" = exact(1L, "one row has a missing width"),
"/chosen_k" = exact(2L, "BIC chooses two clusters (asserted smaller than the one-cluster BIC; the three-cluster fit is not compared)"),
"/fits/[k=1]/status" = exact("fitted", "one component always fits"),
"/fits/[k=1]/parameters" = exact(m1, "K - 1 + K p means + K p variances"),
"/fits/[k=1]/log_likelihood" = tol(fit1, "closed-form one-component Gaussian log-likelihood with MLE (population) variances", rel = "1e-7"),
"/fits/[k=1]/bic" = tol(bic_of(fit1, m1, n), "-2 log L + m log n", rel = "1e-7"),
"/fits/[k=2]/status" = exact("fitted", "two components fit"),
"/fits/[k=2]/parameters" = exact(m2, "K - 1 + K p means + K p variances"),
"/fits/[k=2]/log_likelihood" = tol(fit2$loglik, "mclust VVI (diagonal, varying) log-likelihood", rel = "1e-7"),
"/fits/[k=2]/bic" = tol(bic_of(fit2$loglik, m2, n), "-2 log L + m log n (mclust bic negated)", rel = "1e-7"),
"/silhouette" = tol(mean(sil[, "sil_width"]), "cluster::silhouette mean width of the hard two-cluster partition on standardized Euclidean distances", rel = "1e-6")
)
# Cluster numbering is arbitrary (the product's own note), so the per-cluster sizes, weights
# and means cannot be addressed by index; the silhouette of the hard partition covers the
# assignment as a whole.
emit("STAT-MV-MCLUST-001", "Gaussian mixture with diagonal covariance, K chosen by BIC", "nominal",
"MV_MIXTURE",
list(variables = list("length", "width"), kind = "gaussian", covariance = "diagonal",
k_min = 1L, k_max = 3L),
outputs,
list("mclust::Mclust(G = 2, modelNames = 'VVI') on the 29 complete rows; VVI is the per-component diagonal covariance model. K = 1 in closed form. The K = 3 fit is a local optimum question and is not compared; the BIC choice of K = 2 is.",
"Cluster numbering is arbitrary, so per-cluster profiles are not addressed by index; the hard partition is checked through its mean silhouette (cluster::silhouette on the mclust classification, which is the two true clusters).",
em_note),
mclust_source, mclust_citation)
outputs <- list(
"/n" = exact(n, "complete rows"),
"/chosen_k" = exact(2L, "the only K in the range"),
"/fits/[k=2]/parameters" = exact(m2f, "K - 1 + K p means + K p(p+1)/2 covariances"),
"/fits/[k=2]/log_likelihood" = tol(fit2_full$loglik, "mclust VVV (full, varying) log-likelihood", rel = "1e-7"),
"/fits/[k=2]/bic" = tol(bic_of(fit2_full$loglik, m2f, n), "-2 log L + m log n", rel = "1e-7")
)
emit("STAT-MV-MCLUST-FULL-001", "Gaussian mixture with full covariance, two components", "axis_coverage",
"MV_MIXTURE",
list(variables = list("length", "width"), kind = "gaussian", covariance = "full",
k_min = 2L, k_max = 2L),
outputs,
list("mclust::Mclust(G = 2, modelNames = 'VVV') on the 29 complete rows; VVV is the unrestricted per-component covariance model.", em_note),
mclust_source, mclust_citation)
outputs <- list(
"/n" = exact(n, "complete rows"),
"/chosen_k" = exact(1L, "the only K in the range"),
"/fits/[k=1]/parameters" = exact(m1, "p means and p variances"),
"/fits/[k=1]/log_likelihood" = tol(fit1, "closed-form Gaussian log-likelihood with MLE variances", rel = "1e-9"),
"/fits/[k=1]/bic" = tol(bic_of(fit1, m1, n), "-2 log L + m log n", rel = "1e-9"),
"/profiles/0/size" = exact(n, "every row in the single cluster"),
"/profiles/0/weight" = tol(1, "single component weight"),
"/profiles/0/means" = list(comparison = "sequence", values = as.list(dec(colMeans(x))),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = "sample means"),
"/profiles/0/variances" = list(comparison = "sequence",
values = as.list(dec(apply(x, 2, function(v) mean((v - mean(v))^2)))),
absolute_tolerance = "0", relative_tolerance = "1e-5",
rationale = "MLE variances; the product adds 1e-6 times the sample variance as a floor, hence 1e-5 relative")
)
emit("STAT-MV-MCLUST-ONE-001", "One-component Gaussian fit in closed form", "boundary",
"MV_MIXTURE",
list(variables = list("length", "width"), kind = "gaussian", covariance = "diagonal",
k_min = 1L, k_max = 1L),
outputs,
list("With one component EM is the Gaussian MLE: means are the sample means and variances the population (1/n) variances; log L = sum_j sum_i log N(x_ij; mean_j, var_j).",
"The product's variance floor (1e-6 times the sample variance) perturbs the variances at 1e-6 relative and the log-likelihood only to second order."),
"closed-form Gaussian maximum likelihood (base R dnorm)", mclust_citation)
# Latent class, one class: class-conditional probabilities are the observed proportions.
set.seed(146)
frame <- data.frame(
finish = sample(c("matte", "gloss", "satin"), 24, TRUE),
defect = sample(c("no", "yes"), 24, TRUE, prob = c(0.7, 0.3)),
line = sample(c("L1", "L2", "L3"), 24, TRUE),
stringsAsFactors = FALSE
)
frame$line[5] <- NA
export_dataset(frame, "MV_LATENT", "Twenty-four items on three categorical variables with one missing line (seed 146)",
"closed-form one-class latent class log-likelihood",
"Constructed validation data; no third-party license.",
c(finish = "String", defect = "String", line = "String"))
d <- read_dataset("MV_LATENT")
complete <- d[complete.cases(d), ]
nl <- nrow(complete)
stopifnot(nl == 23)
loglik <- 0; m <- 0
for (name in names(complete)) {
counts <- table(complete[[name]])
loglik <- loglik + sum(counts * log(counts / nl))
m <- m + length(counts) - 1
}
finish_probs <- table(factor(complete$finish, levels = sort(unique(complete$finish), method = "radix"))) / nl
outputs <- list(
"/n" = exact(nl, "complete rows"),
"/missing_count" = exact(1L, "one row has a missing line"),
"/chosen_k" = exact(1L, "the only K in the range"),
"/fits/[k=1]/parameters" = exact(as.integer(m), "sum over variables of (categories - 1)"),
"/fits/[k=1]/log_likelihood" = tol(loglik, "sum_j sum_c n_jc log(n_jc / n): the multinomial log-likelihood at the observed proportions", rel = "1e-9"),
"/fits/[k=1]/bic" = tol(bic_of(loglik, m, nl), "-2 log L + m log n", rel = "1e-9"),
"/profiles/0/size" = exact(nl, "every row in the single class"),
"/profiles/0/probabilities/0/0/0" = exact(names(finish_probs)[1], "first finish category in code-point order"),
"/profiles/0/probabilities/0/0/1" = tol(finish_probs[[1]], "observed proportion of that category", rel = "1e-9"),
"/profiles/0/probabilities/0/1/1" = tol(finish_probs[[2]], "observed proportion of the second category", rel = "1e-9")
)
emit("STAT-MV-LATENT-ONE-001", "One-class latent class fit in closed form", "boundary",
"MV_LATENT",
list(variables = list("finish", "defect", "line"), kind = "latent_class", k_min = 1L,
k_max = 1L),
outputs,
list("With one class the conditional-independence likelihood factorizes into one multinomial per variable, maximized at the observed category proportions; complete rows only (missing_items complete).",
"Categories are listed in code-point order within each variable."),
"closed-form one-class multinomial maximum likelihood (base R)",
"Lazarsfeld and Henry (1968) Latent Structure Analysis; McCutcheon (1987) Latent Class Analysis, Sage.")
cat("model-based clustering cases generated\n")
generators/r/multivariate_options.R
source("common.R",local=TRUE)
d <- read_dataset("MV_CORE")
run <- generator_run(c("stats","jsonlite"))
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
part <- function(x,note) list(comparison="partition",values=as.list(as.integer(x)),rationale=note)
emit <- function(id,entry,menu,config,outputs,notes,dataset="MV_CORE",kind="nominal",repeat_count=1L) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Multivariate option",id),
case_kind=kind,dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=repeat_count,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats and analytic formulae",
citation="R stats::prcomp/hclust/kmeans/manova/cor documentation",
generator=list(path="generators/r/multivariate_options.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
cols <- list("x","y","z")
pc <- stats::prcomp(d[c("x","y","z")],center=TRUE,scale.=FALSE)
emit("STAT-MV-PCA-COV-001","tabelz.executor.principal_components","principalComponentsMenuItem",
list(columns=cols,scaling="covariance",n_components=2L),
list("/eigenvalues/0"=tol(pc$sdev[1]^2,"R prcomp sample-covariance first eigenvalue"),
"/scales/0"=tol(1,"Covariance PCA retains raw measurement scale")),
c("prcomp sample covariance divides by n-1 and does not standardize."))
ord <- order(as.character(seq_len(nrow(d))-1L))
scaled <- scale(d[ord,c("x","y","z")])
for (link in c("average","complete")) {
hc <- stats::hclust(stats::dist(scaled),method=link)
emit(paste0("STAT-MV-HCLUST-",toupper(link),"-001"),
"tabelz.executor.hierarchical_clustering","hierarchicalClusteringMenuItem",
list(columns=cols,linkage=link,cut_k=3L),
list("/merge_heights/0"=tol(hc$height[1],paste("R",link,"first merge height")),
"/cluster_labels"=part(stats::cutree(hc,3L),paste("R",link,"three-way cut"))),
c("Rows are sample-SD standardized then ordered by stable row ID before clustering."))
}
var_dist <- as.dist(1-stats::cor(d[c("x","y","z")]))
var_hc <- stats::hclust(var_dist,method="average")
emit("STAT-MV-HCLUST-VARIABLES-001","tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem",
list(columns=cols,mode="variables",linkage="average",standardize=TRUE,cut_k=2L),
list("/merge_heights/0"=tol(var_hc$height[1],"R average linkage on 1-Pearson correlation"),
"/cluster_labels"=part(stats::cutree(var_hc,2L),"R variable partition")),
c("Variable mode distance is 1-r on complete cases; scaling is immaterial to correlation.",
"Variable mode requires standardize=true (strict request contract); until 2026-09-25 this case sent false."))
set.seed(29)
km <- stats::kmeans(d[ord,c("x","y","z")],3L,nstart=50L,algorithm="Hartigan-Wong")
emit("STAT-MV-KMEANS-RAW-001","tabelz.executor.clustering","kmeansClusteringMenuItem",
list(columns=cols,k=3L,standardize=FALSE,seed=29L,restarts=50L),
list("/inertia"=tol(km$tot.withinss,"R unique raw-scale minimum within SS"),
"/cluster_labels"=part(km$cluster,"R raw-scale three-way partition")),
c("Well-separated optimum shared by R Hartigan-Wong and scikit-learn Lloyd; no standardization."),
kind="determinism",repeat_count=2L)
for (metric in c("spearman","covariance")) {
value <- if (metric=="spearman") stats::cor(d$x,d$y,method="spearman") else stats::cov(d$x,d$y)
emit(paste0("STAT-MV-CORR-",toupper(metric),"-001"),
"tabelz.analysis.correlation:compute_correlation","correlationMenuItem",
list(columns=cols,metric=metric,max_scatter_columns=0L),
list("/cells/1/value"=tol(value,paste("R pairwise",metric,"x-y"))),
c("All rows finite; R cor or cov uses the complete pair and sample covariance."))
}
items <- as.matrix(d[c("i1","i2","i3")]); items[,3] <- 6-items[,3]
k <- ncol(items)
alpha <- k/(k-1)*(1-sum(apply(items,2,stats::var))/stats::var(rowSums(items)))
emit("STAT-MV-ALPHA-REVERSE-001","tabelz.executor.item_analysis","itemAnalysisMenuItem",
list(items=list("i1","i2","i3"),reverse_items=list("i3"),scale_low=1,scale_high=5),
list("/cronbach_alpha"=tol(alpha,"Analytic sample-variance alpha after reflecting i3 as 6-i3")),
c("Reverse coding uses low+high-value once; raw alpha uses sample variances."))
mv <- stats::manova(cbind(x,y)~group,data=d)
pillai <- summary(mv,test="Pillai")$stats[1,"Pillai"]
wilks <- summary(mv,test="Wilks")$stats[1,"Wilks"]
emit("STAT-MV-MANOVA-II-001","tabelz.executor.manova","manovaMenuItem",
list(response_columns=list("x","y"),factor_columns=list("group"),test_type="II"),
list("/joint_tests/0/value"=tol(pillai,"R one-way Pillai trace; Type II equals Type III"),
"/joint_tests/1/value"=tol(wilks,"R one-way Wilks lambda; Type II equals Type III")),
c("One-factor design has identical Type-II and Type-III hypotheses."))
ca <- data.frame(label=c("A","B","C"),U=c(3L,0L,1L),V=c(1L,3L,0L),W=c(0L,1L,3L))
export_dataset(ca,"MV_CA_COUNTS","Constructed three-by-three count table",
"R stats::chisq.test and correspondence-analysis inertia identity",
"Constructed validation data; no third-party license.",
c(label="String",U="Int64",V="Int64",W="Int64"))
counts <- read_dataset("MV_CA_COUNTS")
table <- as.matrix(counts[c("U","V","W")])
chi <- suppressWarnings(stats::chisq.test(table,correct=FALSE))$statistic
emit("STAT-MV-CA-COUNTS-001","tabelz.executor.correspondence_analysis",
"correspondenceAnalysisMenuItem",
list(mode="count_table",row_label_column="label",count_columns=list("U","V","W")),
list("/chi_square"=tol(chi,"Pearson chi-square from R count table"),
"/total_inertia"=tol(chi/sum(table),"CA total inertia equals chi-square/n")),
c("Count-table mode uses literal nonnegative integer cell counts."),dataset="MV_CA_COUNTS")
cat("multivariate option references generated\n")
generators/r/multivariate_ordered.R
source("common.R", local = TRUE)
d <- read_dataset("MV_CORE")
order_ids <- order(as.character(seq_len(nrow(d)) - 1L))
run <- generator_run(c("stats", "jsonlite"))
emit <- function(id, entry, menu, config, outputs, notes, kind="row_alignment", repeat_count=1L) {
write_expected("multivariate", id, list(schema_version="2.0.0", case_id=id,
outputs=outputs, expected_error=NULL, deferred_outputs=list(), generator_run=run))
write_case("multivariate", id, list(
schema_version="2.0.0", case_id=id, version="1.0.0", family="multivariate",
entry_point=entry, menu_items=list(menu), title=paste("Ordered multivariate", id),
case_kind=kind, dataset=list(path="reference_data/datasets/MV_CORE.json", sha256=""),
excluded_rows=list(), configuration=config, "repeat"=repeat_count,
expected=list(path=paste0("reference_data/expected/multivariate/", id, ".json"), sha256=""),
oracle=list(oracle_class="independent_implementation", source="R stats",
citation="R stats::hclust/cutree/kmeans documentation",
generator=list(path="generators/r/multivariate_ordered.R", sha256=""),
independent=TRUE, convention_notes=c(notes,
"Engine reports rows sorted lexicographically by stable row ID: ordinal strings 0,1,10,11,2,...",
"R re-reads the exported CSV and applies that documented row-ID order before fitting.")),
supported=TRUE, unsupported_reason=NULL,
requirement_ids=list("REQ-STAT-MV-001"), risk_ids=list("RISK-REFERENCE-001"),
review_reference=NULL))
}
part <- function(x, note) list(comparison="partition", values=as.list(as.integer(x)), rationale=note)
tol <- function(x, note) list(comparison="tolerance", value=dec(unname(x)),
absolute_tolerance="0", relative_tolerance="1e-9", rationale=note)
scaled <- scale(d[order_ids,c("x","y","z")])
hc <- stats::hclust(stats::dist(scaled), method="ward.D2")
emit("STAT-MV-HCLUST-002", "tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem",
list(columns=list("x","y","z"), linkage="ward", cut_k=3L),
list("/cluster_labels"=part(stats::cutree(hc,3L), "R Ward.D2 partition in retained row-ID order")),
c("Sample-SD standardization and Euclidean Ward.D2 linkage."))
set.seed(11)
km <- stats::kmeans(scaled, centers=3L, nstart=50L, algorithm="Hartigan-Wong")
emit("STAT-MV-KMEANS-002", "tabelz.executor.clustering", "kmeansClusteringMenuItem",
list(columns=list("x","y","z"), k=3L, seed=11L, restarts=50L),
list("/inertia"=tol(km$tot.withinss, "R unique optimum total within-cluster SS"),
"/cluster_labels"=part(km$cluster, "R unique optimum partition in retained row-ID order")),
c("Well-separated optimum is shared by R Hartigan-Wong and scikit-learn Lloyd."),
kind="determinism", repeat_count=2L)
cat("ordered multivariate references generated\n")
generators/r/multivariate_population.R
source("common.R",local=TRUE)
raw <- read_dataset("MV_CORE")
raw$x[3] <- NA_real_; raw$i1[3] <- NA_integer_
raw$group[3] <- NA_character_; raw$cat[3] <- NA_character_
export_dataset(raw,"MV_POP","Constructed multivariate population with one missing and one excluded row",
"R stats::prcomp/hclust/kmeans/manova/cor and analytic alpha/CA identities",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64",z="Float64",i1="Int64",i2="Int64",i3="Int64",
group="String",cat="String"))
d <- read_dataset("MV_POP")
run <- generator_run(c("stats","jsonlite"))
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
part <- function(x,note) list(comparison="partition",values=as.list(as.integer(x)),rationale=note)
emit <- function(id,entry,menu,config,outputs,notes) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),
title=paste("Missing and excluded multivariate population",id),case_kind="missing_excluded",
dataset=list(path="reference_data/datasets/MV_POP.json",sha256=""),
excluded_rows=list(9L),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats and analytic identities",
citation="R stats::prcomp/hclust/kmeans/manova/cor documentation",
generator=list(path="generators/r/multivariate_population.R",sha256=""),
independent=TRUE,convention_notes=c(notes,
"R re-reads exported CSV, drops ordinal 9 by stable-row-ID exclusion, then uses complete cases.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
eligible <- d[-10L,,drop=FALSE]
xyzd <- eligible[stats::complete.cases(eligible[c("x","y","z")]),,drop=FALSE]
pc <- stats::prcomp(xyzd[c("x","y","z")],center=TRUE,scale.=TRUE)
emit("STAT-MV-PCA-POP-001","tabelz.executor.principal_components","principalComponentsMenuItem",
list(columns=list("x","y","z"),n_components=2L),
list("/source_count"=exact(nrow(d),"CSV source population"),
"/excluded_count"=exact(1L,"One stable row ID excluded"),
"/missing_count"=exact(1L,"One selected-column missing row"),
"/eigenvalues/0"=tol(pc$sdev[1]^2,"R complete-case sample-correlation eigenvalue")),
c("Complete-case standardization divides by n-1."))
ord <- which(seq_len(nrow(d)) != 10L & stats::complete.cases(d[c("x","y","z")]))
ord <- ord[order(as.character(ord-1L))]
scaled <- scale(d[ord,c("x","y","z")])
hc <- stats::hclust(stats::dist(scaled),method="ward.D2")
emit("STAT-MV-HCLUST-POP-001","tabelz.executor.hierarchical_clustering",
"hierarchicalClusteringMenuItem",list(columns=list("x","y","z"),cut_k=3L),
list("/excluded_count"=exact(1L,"One stable row ID excluded"),
"/missing_count"=exact(1L,"One selected-column missing row"),
"/cluster_labels"=part(stats::cutree(hc,3L),"R Ward.D2 partition in sorted row-ID order")),
c("R uses sample-SD standardization and Ward.D2 on the same sorted complete rows."))
set.seed(37)
km <- stats::kmeans(scaled,3L,nstart=50L,algorithm="Hartigan-Wong")
emit("STAT-MV-KMEANS-POP-001","tabelz.executor.clustering","kmeansClusteringMenuItem",
list(columns=list("x","y","z"),k=3L,seed=37L,restarts=50L),
list("/excluded_count"=exact(1L,"One stable row ID excluded"),
"/missing_count"=exact(1L,"One selected-column missing row"),
"/inertia"=tol(km$tot.withinss,"R unique optimum complete-case within SS"),
"/cluster_labels"=part(km$cluster,"R complete-case partition in sorted row-ID order")),
c("Well-separated optimum shared by R Hartigan-Wong and scikit-learn Lloyd."))
itemd <- eligible[stats::complete.cases(eligible[c("i1","i2","i3")]),c("i1","i2","i3")]
itemm <- as.matrix(itemd); k <- ncol(itemm)
alpha <- k/(k-1)*(1-sum(apply(itemm,2,stats::var))/stats::var(rowSums(itemm)))
emit("STAT-MV-ALPHA-POP-001","tabelz.executor.item_analysis","itemAnalysisMenuItem",
list(items=list("i1","i2","i3")),
list("/excluded_count"=exact(1L,"One stable row ID excluded"),
"/missing_count"=exact(1L,"One item-missing row"),
"/cronbach_alpha"=tol(alpha,"Analytic raw alpha on complete selected items")),
c("Raw alpha uses complete rows and sample variances after exclusions."))
cad <- eligible[stats::complete.cases(eligible[c("group","cat")]),,drop=FALSE]
tab <- table(factor(cad$group,levels=c("A","B","C")),
factor(cad$cat,levels=c("U","V","W")))
chi <- suppressWarnings(stats::chisq.test(tab,correct=FALSE))$statistic
emit("STAT-MV-CA-POP-001","tabelz.executor.correspondence_analysis",
"correspondenceAnalysisMenuItem",list(mode="categorical",row_column="group",column_column="cat"),
list("/excluded_count"=exact(1L,"One stable row ID excluded"),
"/missing_count"=exact(1L,"One category-missing row"),
"/chi_square"=tol(chi,"R complete-case contingency chi-square")),
c("Event rows with either category missing are omitted after exclusion."))
md <- eligible[stats::complete.cases(eligible[c("x","y","group")]),,drop=FALSE]
mv <- stats::manova(cbind(x,y)~group,data=md)
pillai <- summary(mv,test="Pillai")$stats[1,"Pillai"]
emit("STAT-MV-MANOVA-POP-001","tabelz.executor.manova","manovaMenuItem",
list(response_columns=list("x","y"),factor_columns=list("group")),
list("/excluded_count"=exact(1L,"One stable row ID excluded"),
"/missing_count"=exact(1L,"One response/factor-missing row"),
"/joint_tests/0/value"=tol(pillai,"R complete-case one-way Pillai trace")),
c("One-way factor Type-III Pillai trace on complete response/factor rows."))
pair <- eligible[stats::complete.cases(eligible[c("x","y")]),,drop=FALSE]
emit("STAT-MV-CORR-POP-001","tabelz.analysis.correlation:compute_correlation",
"correlationMenuItem",list(columns=list("x","y"),max_scatter_columns=0L),
list("/cells/1/value"=tol(stats::cor(pair$x,pair$y),"R pairwise-complete Pearson x-y"),
"/cells/1/pair_count"=exact(nrow(pair),"Pairwise finite rows after exclusion")),
c("Correlation uses pairwise finite rows rather than all-variable complete cases."))
cat("multivariate population references generated\n")
generators/r/multivariate_scoring.R
source("common.R", local = TRUE)
d <- read_dataset("MV_CORE")
run <- generator_run(c("stats", "jsonlite"))
ids <- function(case_id) paste0(case_id, ":", seq_len(nrow(d))-1L)
dec_list <- function(x) lapply(as.double(x), identity)
matrix_rows <- function(x) lapply(seq_len(nrow(x)), function(i) dec_list(x[i,]))
tol <- function(x, note) list(comparison="tolerance", value=dec(x), absolute_tolerance="0",
relative_tolerance="1e-9", rationale=note)
exact <- function(x, note) list(comparison="exact", value=as.integer(x), rationale=note)
absent <- function(note) list(comparison="absent", rationale=note)
emit <- function(id, entry, menu, config, output, notes) {
write_expected("multivariate", id, list(schema_version="2.0.0", case_id=id,
outputs=output, expected_error=NULL, deferred_outputs=list(), generator_run=run))
write_case("multivariate", id, list(schema_version="2.0.0", case_id=id,
version="1.0.0", family="multivariate", entry_point=entry,
menu_items=list(menu), title=paste("Retained scoring", id), case_kind="row_alignment",
dataset=list(path="reference_data/datasets/MV_CORE.json", sha256=""),
excluded_rows=list(), configuration=config, "repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"), sha256=""),
oracle=list(oracle_class="independent_implementation", source="R matrix arithmetic",
citation="R stats::prcomp, kmeans, hclust and matrix multiplication documentation",
generator=list(path="generators/r/multivariate_scoring.R", sha256=""),
independent=TRUE, convention_notes=c(notes, "R re-reads the exported CSV.")),
supported=TRUE, unsupported_reason=NULL,
requirement_ids=list("REQ-STAT-MV-001"), risk_ids=list("RISK-REFERENCE-001"),
review_reference=NULL))
}
outputs <- function(values, name, note, omitted=integer(), numeric=TRUE) {
out <- list()
for (i in seq_along(values)) {
path <- paste0("/", i-1L, "/", name)
out[[path]] <- if (i %in% omitted) absent(paste(note, "row excluded by retained membership"))
else if (numeric) tol(values[[i]], note)
else exact(values[[i]], note)
}
out
}
id <- "STAT-MV-PCA-SCORE-001"
pc <- stats::prcomp(d[c("x","y","z")], center=TRUE, scale.=TRUE)
keep <- setdiff(seq_len(nrow(d)), 3L)
model <- list(principal_components=list(columns=list("x","y","z"),
means=dec_list(pc$center), scales=dec_list(pc$scale),
loadings=matrix_rows(pc$rotation[,1,drop=FALSE]), row_ids=as.list(ids(id)[keep])))
emit(id,"operation:tabelz.principal_component_scores","principalComponentsMenuItem",
list(names=list("pc1"),model=model),
outputs(pc$x[,1],"pc1","R standardized data times retained prcomp loading", omitted=3L),
c("One row is absent from the retained population and must receive null; all other scores retain CSV order."))
id <- "STAT-MV-FA-SCORE-001"
weights <- matrix(c(0.2,0.3,0.5), ncol=1)
mu <- colMeans(d[c("x","y","z")]); sd <- apply(d[c("x","y","z")],2,stats::sd)
score <- as.vector(scale(d[c("x","y","z")], center=mu, scale=sd) %*% weights)
model <- list(factor_analysis=list(columns=list("x","y","z"), means=dec_list(mu),
scales=dec_list(sd), weights=matrix_rows(weights), row_ids=as.list(ids(id)[keep])))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem",
list(names=list("factor1"),model=model),
outputs(score,"factor1","R standardized data times explicitly retained factor-score weights",omitted=3L),
c("The fixed weight vector is an analytic retained basis; this tests replay, not factor fitting."))
sorted <- order(as.character(seq_len(nrow(d))-1L))
scaled <- scale(d[sorted,c("x","y","z")])
id <- "STAT-MV-HCLUST-LABEL-001"
hc <- stats::hclust(stats::dist(scaled),method="ward.D2")
hc_labels <- as.integer(stats::cutree(hc,3L))
membership <- integer(nrow(d)); membership[sorted] <- hc_labels
model <- list(hierarchical_clustering=list(mode="rows",linkage="ward",cut_k=3L,
row_ids=as.list(ids(id)[sorted]),labels=as.list(hc_labels)))
emit(id,"operation:tabelz.hierarchical_cluster_labels","hierarchicalClusteringMenuItem",
list(name="hcluster",model=model),
outputs(membership,"hcluster","Retained R Ward.D2 cut joined by stable row identity",numeric=FALSE),
c("R cluster labels are retained in engine row-ID order, then joined into source order."))
id <- "STAT-MV-KMEANS-LABEL-001"
set.seed(11)
km <- stats::kmeans(scaled,3L,nstart=50L,algorithm="Hartigan-Wong")
km_labels <- as.integer(km$cluster)
membership <- integer(nrow(d)); membership[sorted] <- km_labels
model <- list(kmeans=list(k=3L,seed=11L,restarts=50L,standardize=TRUE,
row_ids=as.list(ids(id)[sorted]),labels=as.list(km_labels)))
emit(id,"operation:tabelz.kmeans_cluster_labels","kmeansClusteringMenuItem",
list(name="kcluster",model=model),
outputs(membership,"kcluster","Retained R kmeans partition joined by stable row identity",numeric=FALSE),
c("R cluster labels are retained in engine row-ID order, then joined into source order."))
id <- "STAT-MV-ALPHA-SCORE-001"
model <- list(item_analysis=list(items=list("i1","i2","i3"),reverse_items=list(),
scale_low=NULL,scale_high=NULL,row_ids=as.list(ids(id)[keep])))
emit(id,"operation:tabelz.item_analysis_scores","itemAnalysisMenuItem",
list(names=list("scale_score"),aggregation="mean",model=model),
outputs(rowMeans(d[c("i1","i2","i3")]),"scale_score",
"Arithmetic mean of three retained item responses",omitted=3L),
c("The item score is the arithmetic row mean; one row is outside retained membership."))
cat("multivariate scoring references generated\n")
generators/r/multivariate_scoring_boundaries.R
source("common.R",local=TRUE)
d <- read_dataset("MV_MIN_TWO")
run <- generator_run(c("stats","jsonlite"))
nums <- function(x) lapply(as.double(x),identity)
rows <- function(x) lapply(seq_len(nrow(x)),function(i) nums(x[i,]))
tol <- function(x,note) list(comparison="tolerance",value=dec(x),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
absent <- function(note) list(comparison="absent",rationale=note)
emit <- function(id,entry,menu,config,values,name,notes,partial=FALSE,numeric=TRUE) {
outputs <- list()
for (i in seq_along(values)) {
path <- paste0("/",i-1L,"/",name)
outputs[[path]] <- if (partial && i==2L) absent("Second row outside retained membership")
else if (numeric) tol(values[[i]],"Independent R matrix arithmetic")
else exact(values[[i]],"Retained label joined by row identity")
}
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Retained boundary",id),
case_kind="boundary",dataset=list(path="reference_data/datasets/MV_MIN_TWO.json",sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R matrix arithmetic and retained identity join",
citation="R stats::prcomp and base matrix arithmetic documentation",
generator=list(path="generators/r/multivariate_scoring_boundaries.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-ROW-IDENTITY-001"),review_reference=NULL))
}
for (suffix in c("MIN","OMIT")) {
partial <- suffix=="OMIT"
make_ids <- function(id) as.list(paste0(id,":",if(partial) 0L else 0:1))
id <- paste0("STAT-MV-PCA-SCORE-",suffix,"-001")
pc <- stats::prcomp(d[c("x","y")],center=TRUE,scale.=TRUE)
model <- list(principal_components=list(columns=list("x","y"),means=nums(pc$center),
scales=nums(pc$scale),loadings=rows(pc$rotation[,1,drop=FALSE]),row_ids=make_ids(id)))
emit(id,"operation:tabelz.principal_component_scores","principalComponentsMenuItem",
list(names=list("pc1"),model=model),pc$x[,1],"pc1",
c("Two source rows; R standardized matrix times retained loading."),partial)
id <- paste0("STAT-MV-FA-SCORE-",suffix,"-001")
x <- as.matrix(d[c("x","y","z")]); mu <- colMeans(x); sd <- apply(x,2,stats::sd)
weights <- matrix(c(0.2,0.3,0.5),ncol=1)
scores <- as.vector(scale(x,center=mu,scale=sd)%*%weights)
model <- list(factor_analysis=list(columns=list("x","y","z"),means=nums(mu),
scales=nums(sd),weights=rows(weights),row_ids=make_ids(id)))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem",
list(names=list("factor1"),model=model),scores,"factor1",
c("Two source rows; fixed retained weights test scoring without re-fitting FA."),partial)
id <- paste0("STAT-MV-HCLUST-LABEL-",suffix,"-001")
model <- list(hierarchical_clustering=list(mode="rows",linkage="ward",cut_k=2L,
row_ids=make_ids(id),labels=as.list(if(partial) 1L else c(1L,2L))))
emit(id,"operation:tabelz.hierarchical_cluster_labels","hierarchicalClusteringMenuItem",
list(name="hcluster",model=model),c(1L,2L),"hcluster",
c("Two-row retained partition; labels are joined by stable row ID."),partial,FALSE)
id <- paste0("STAT-MV-KMEANS-LABEL-",suffix,"-001")
model <- list(kmeans=list(k=2L,seed=11L,restarts=50L,standardize=TRUE,
row_ids=make_ids(id),labels=as.list(if(partial) 1L else c(1L,2L))))
emit(id,"operation:tabelz.kmeans_cluster_labels","kmeansClusteringMenuItem",
list(name="kcluster",model=model),c(1L,2L),"kcluster",
c("Two-row retained partition; labels are joined by stable row ID."),partial,FALSE)
id <- paste0("STAT-MV-ALPHA-SCORE-",suffix,"-001")
model <- list(item_analysis=list(items=list("i1","i3"),reverse_items=list(),
scale_low=NULL,scale_high=NULL,row_ids=make_ids(id)))
scores <- rowMeans(d[c("i1","i3")])
emit(id,"operation:tabelz.item_analysis_scores","itemAnalysisMenuItem",
list(names=list("scale_score"),aggregation="mean",model=model),scores,"scale_score",
c("Two-item arithmetic row mean; one retained row in OMIT case."),partial)
}
cat("retained scoring boundaries generated\n")
generators/r/multivariate_scoring_corrections.R
source("common.R",local=TRUE)
base <- read_dataset("MV_CORE")
three <- base[c(1L,5L,9L),]
four <- base[c(1L,5L,9L,12L),]
types <- c(x="Float64",y="Float64",z="Float64",i1="Int64",i2="Int64",
i3="Int64",group="String",cat="String")
export_dataset(three,"MV_SCORE_THREE","Three distinct retained scoring rows",
"R retained-model matrix arithmetic boundary",
"Constructed validation data; no third-party license.",types)
export_dataset(four,"MV_SCORE_FOUR","Three retained rows and one nonmember scoring row",
"R retained-model row-identity boundary",
"Constructed validation data; no third-party license.",types)
three <- read_dataset("MV_SCORE_THREE")
four <- read_dataset("MV_SCORE_FOUR")
hard <- read_dataset("MV_HARD")
run <- generator_run(c("stats","jsonlite"))
nums <- function(x) lapply(as.double(x),identity)
rows <- function(x) lapply(seq_len(nrow(x)),function(i) nums(x[i,]))
tol <- function(x,note) list(comparison="tolerance",value=dec(x),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
absent <- function(note) list(comparison="absent",rationale=note)
emit <- function(id,entry,menu,dataset,config,values,name,notes,kind="boundary",
omitted=integer(),numeric=TRUE) {
outputs <- list()
for (i in seq_along(values)) {
path <- paste0("/",i-1L,"/",name)
outputs[[path]] <- if (i %in% omitted) absent("Row outside the retained population")
else if (numeric) tol(values[[i]],"Independent R retained matrix arithmetic")
else exact(values[[i]],"Retained label joined by stable row ID")
}
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Retained correction",id),
case_kind=kind,dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R matrix arithmetic and analytic row-identity join",
citation="R stats::prcomp, scale and base matrix multiplication documentation",
generator=list(path="generators/r/multivariate_scoring_corrections.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-ROW-IDENTITY-001"),review_reference=NULL))
}
id <- "STAT-MV-FA-SCORE-MIN-CORRECTED-001"
x <- as.matrix(three[c("x","y","z")]); mu <- colMeans(x); sd <- apply(x,2,stats::sd)
weights <- matrix(c(0.2,0.3,0.5),ncol=1)
scores <- as.vector(scale(x,center=mu,scale=sd)%*%weights)
ids <- function(id,n) as.list(paste0(id,":",seq_len(n)-1L))
model <- list(factor_analysis=list(columns=list("x","y","z"),means=nums(mu),
scales=nums(sd),weights=rows(weights),row_ids=ids(id,3L)))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem","MV_SCORE_THREE",
list(names=list("factor1"),model=model),scores,"factor1",
c("Three retained rows meet the FA score model minimum; fixed weights avoid refitting."))
d <- four
ret <- three
id <- "STAT-MV-PCA-SCORE-OMIT-CORRECTED-001"
pc <- stats::prcomp(ret[c("x","y")],center=TRUE,scale.=TRUE)
scores <- as.vector(scale(as.matrix(d[c("x","y")]),center=pc$center,scale=pc$scale)%*%
pc$rotation[,1,drop=FALSE])
model <- list(principal_components=list(columns=list("x","y"),means=nums(pc$center),
scales=nums(pc$scale),loadings=rows(pc$rotation[,1,drop=FALSE]),row_ids=ids(id,3L)))
emit(id,"operation:tabelz.principal_component_scores","principalComponentsMenuItem",
"MV_SCORE_FOUR",list(names=list("pc1"),model=model),scores,"pc1",
c("Three retained rows and one nonmember; stable row IDs control where null is written."),omitted=4L)
id <- "STAT-MV-FA-SCORE-OMIT-CORRECTED-001"
x <- as.matrix(ret[c("x","y","z")]); mu <- colMeans(x); sd <- apply(x,2,stats::sd)
scores <- as.vector(scale(as.matrix(d[c("x","y","z")]),center=mu,scale=sd)%*%weights)
model <- list(factor_analysis=list(columns=list("x","y","z"),means=nums(mu),
scales=nums(sd),weights=rows(weights),row_ids=ids(id,3L)))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem",
"MV_SCORE_FOUR",list(names=list("factor1"),model=model),scores,"factor1",
c("Three retained FA score rows plus one nonmember."),omitted=4L)
id <- "STAT-MV-HCLUST-LABEL-OMIT-CORRECTED-001"
model <- list(hierarchical_clustering=list(mode="rows",linkage="ward",cut_k=2L,
row_ids=ids(id,3L),labels=as.list(c(1L,2L,2L))))
emit(id,"operation:tabelz.hierarchical_cluster_labels","hierarchicalClusteringMenuItem",
"MV_SCORE_FOUR",list(name="hcluster",model=model),c(1L,2L,2L,1L),"hcluster",
c("Three retained labels, one nonmember; the test concerns replay identity, not refitting linkage."),
omitted=4L,numeric=FALSE)
id <- "STAT-MV-KMEANS-LABEL-OMIT-CORRECTED-001"
model <- list(kmeans=list(k=2L,seed=11L,restarts=50L,standardize=TRUE,
row_ids=ids(id,3L),labels=as.list(c(1L,2L,2L))))
emit(id,"operation:tabelz.kmeans_cluster_labels","kmeansClusteringMenuItem",
"MV_SCORE_FOUR",list(name="kcluster",model=model),c(1L,2L,2L,1L),"kcluster",
c("Three retained labels, one nonmember; the test concerns replay identity, not refitting k-means."),
omitted=4L,numeric=FALSE)
id <- "STAT-MV-ALPHA-SCORE-OMIT-CORRECTED-001"
model <- list(item_analysis=list(items=list("i1","i3"),reverse_items=list(),
scale_low=NULL,scale_high=NULL,row_ids=ids(id,3L)))
scores <- rowMeans(d[c("i1","i3")])
emit(id,"operation:tabelz.item_analysis_scores","itemAnalysisMenuItem",
"MV_SCORE_FOUR",list(names=list("scale_score"),aggregation="mean",model=model),
scores,"scale_score",c("Two-item row mean for three retained rows; one nonmember."),omitted=4L)
# The artifact carries the model as JSON numbers, which write_json(digits = NA)
# writes with 15 significant digits. Score in R with exactly those values: at
# a 1e8 offset the 15-digit mean differs from the full-precision one by up to
# 5e-7, which the scores inherit. Until 2026-09-25 R scored with full precision.
as_serialized <- function(x) array(as.double(sprintf("%.15g", x)), dim = dim(as.array(x)))
id <- "STAT-MV-PCA-SCORE-HARD-001"
pc <- stats::prcomp(hard[c("x","y")],center=TRUE,scale.=TRUE)
pc_center <- as.vector(as_serialized(pc$center)); pc_scale <- as.vector(as_serialized(pc$scale))
pc_loading <- as.vector(as_serialized(pc$rotation[,1]))
scores <- as.vector(scale(as.matrix(hard[c("x","y")]),center=pc_center,scale=pc_scale)%*%pc_loading)
model <- list(principal_components=list(columns=list("x","y"),means=nums(pc_center),
scales=nums(pc_scale),loadings=rows(matrix(pc_loading,ncol=1)),row_ids=ids(id,nrow(hard))))
emit(id,"operation:tabelz.principal_component_scores","principalComponentsMenuItem",
"MV_HARD",list(names=list("pc1"),model=model),scores,"pc1",
c("Scoring subtracts 1e8-scale means before applying retained PCA loading."),kind="hard_numeric")
id <- "STAT-MV-FA-SCORE-HARD-001"
x <- as.matrix(hard[c("x","y","z")])
mu <- as.vector(as_serialized(colMeans(x))); sd <- as.vector(as_serialized(apply(x,2,stats::sd)))
scores <- as.vector(scale(x,center=mu,scale=sd)%*%as_serialized(weights))
model <- list(factor_analysis=list(columns=list("x","y","z"),means=nums(mu),
scales=nums(sd),weights=rows(weights),row_ids=ids(id,nrow(hard))))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem",
"MV_HARD",list(names=list("factor1"),model=model),scores,"factor1",
c("Scoring subtracts 1e8-scale means before applying explicit factor weights."),kind="hard_numeric")
id <- "STAT-MV-ALPHA-SCORE-HARD-001"
model <- list(item_analysis=list(items=list("i1","i3"),reverse_items=list(),
scale_low=NULL,scale_high=NULL,row_ids=ids(id,nrow(hard))))
scores <- rowMeans(hard[c("i1","i3")])
emit(id,"operation:tabelz.item_analysis_scores","itemAnalysisMenuItem",
"MV_HARD",list(names=list("scale_score"),aggregation="mean",model=model),
scores,"scale_score",c("Retained two-item score is the mean of million-offset integer responses."),
kind="hard_numeric")
cat("corrected retained scoring references generated\n")
generators/r/multivariate_scoring_final.R
source("common.R",local=TRUE)
base <- read_dataset("MV_CORE")
four <- read_dataset("MV_SCORE_FOUR")
five <- base[c(1L,5L,9L,12L,3L),]
types <- c(x="Float64",y="Float64",z="Float64",i1="Int64",i2="Int64",
i3="Int64",group="String",cat="String")
export_dataset(five,"MV_SCORE_FIVE","Four retained rows and one nonmember factor-scoring row",
"R retained matrix arithmetic and stable row identity",
"Constructed validation data; no third-party license.",types)
five <- read_dataset("MV_SCORE_FIVE")
hard <- base
hard[c("x","y","z")] <- lapply(base[c("x","y","z")],function(v) 1e5+v)
export_dataset(hard,"MV_SCORE_HARD","Large-offset retained scoring measurements",
"R centered matrix arithmetic hard-numeric reference",
"Constructed validation data; no third-party license.",types)
hard <- read_dataset("MV_SCORE_HARD")
run <- generator_run(c("stats","jsonlite"))
nums <- function(x) lapply(as.double(x),identity)
rows <- function(x) lapply(seq_len(nrow(x)),function(i) nums(x[i,]))
ids <- function(id,n) as.list(paste0(id,":",seq_len(n)-1L))
tol <- function(x,note) list(comparison="tolerance",value=dec(x),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
absent <- function(note) list(comparison="absent",rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,kind,notes) {
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point=entry,menu_items=list(menu),title=paste("Retained scoring",id),
case_kind=kind,dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats and centered matrix arithmetic",
citation="R stats::prcomp and scale documentation",
generator=list(path="generators/r/multivariate_scoring_final.R",sha256=""),independent=TRUE,
convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-ROW-IDENTITY-001"),review_reference=NULL))
}
weights <- matrix(c(0.2,0.3,0.5),ncol=1)
id <- "STAT-MV-FA-SCORE-MIN-VALID-001"
x <- as.matrix(four[c("x","y","z")]); mu <- colMeans(x); sd <- apply(x,2,stats::sd)
scores <- as.vector(scale(x,center=mu,scale=sd)%*%weights)
model <- list(factor_analysis=list(columns=list("x","y","z"),means=nums(mu),
scales=nums(sd),weights=rows(weights),row_ids=ids(id,4L)))
out <- list("/0/factor1"=tol(scores[1],"Four-row retained FA score"),
"/3/factor1"=tol(scores[4],"Fourth retained FA score"))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem","MV_SCORE_FOUR",
list(names=list("factor1"),model=model),out,"boundary",
c("Four retained rows are the smallest population strictly exceeding three scoring columns."))
id <- "STAT-MV-FA-SCORE-OMIT-VALID-001"
x <- as.matrix(four[c("x","y","z")]); mu <- colMeans(x); sd <- apply(x,2,stats::sd)
scores <- as.vector(scale(as.matrix(five[c("x","y","z")]),center=mu,scale=sd)%*%weights)
model <- list(factor_analysis=list(columns=list("x","y","z"),means=nums(mu),
scales=nums(sd),weights=rows(weights),row_ids=ids(id,4L)))
out <- list("/0/factor1"=tol(scores[1],"First retained FA score"),
"/3/factor1"=tol(scores[4],"Fourth retained FA score"),
"/4/factor1"=absent("Fifth source row is outside retained model population"))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem","MV_SCORE_FIVE",
list(names=list("factor1"),model=model),out,"boundary",
c("Four retained rows satisfy the model schema; fifth source row must remain missing."))
id <- "STAT-MV-PCA-SCORE-HARD-VALID-001"
pc <- stats::prcomp(hard[c("x","y")],center=TRUE,scale.=TRUE)
model <- list(principal_components=list(columns=list("x","y"),means=nums(pc$center),
scales=nums(pc$scale),loadings=rows(pc$rotation[,1,drop=FALSE]),row_ids=ids(id,nrow(hard))))
out <- list("/0/pc1"=tol(pc$x[1,1],"Centered PCA score at 1e5 offset"))
emit(id,"operation:tabelz.principal_component_scores","principalComponentsMenuItem",
"MV_SCORE_HARD",list(names=list("pc1"),model=model),out,"hard_numeric",
c("Measurements have a 1e5 offset with O(1) spread; R centers before projecting."))
id <- "STAT-MV-FA-SCORE-HARD-VALID-001"
x <- as.matrix(hard[c("x","y","z")]); mu <- colMeans(x); sd <- apply(x,2,stats::sd)
scores <- as.vector(scale(x,center=mu,scale=sd)%*%weights)
model <- list(factor_analysis=list(columns=list("x","y","z"),means=nums(mu),
scales=nums(sd),weights=rows(weights),row_ids=ids(id,nrow(hard))))
out <- list("/0/factor1"=tol(scores[1],"Centered factor score at 1e5 offset"))
emit(id,"operation:tabelz.factor_scores","factorAnalysisMenuItem","MV_SCORE_HARD",
list(names=list("factor1"),model=model),out,"hard_numeric",
c("Measurements have a 1e5 offset with O(1) spread; explicit retained weights test scoring, not fitting."))
cat("final retained scoring references generated\n")
generators/r/multivariate_variables.R
source("common.R",local=TRUE)
d <- read_dataset("MV_CORE")
distance <- as.dist(1-stats::cor(d[c("x","y","z")]))
fit <- stats::hclust(distance,method="average")
id <- "STAT-MV-HCLUST-VARIABLES-002"
write_expected("multivariate",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/merge_heights/0"=list(comparison="tolerance",value=dec(fit$height[1]),
absolute_tolerance="0",relative_tolerance="1e-9",
rationale="R average linkage on 1-Pearson correlation"),
"/cluster_labels"=list(comparison="partition",values=as.list(as.integer(stats::cutree(fit,2L))),
rationale="R variable partition at two clusters")),
expected_error=NULL,deferred_outputs=list(),generator_run=generator_run(c("stats","jsonlite"))))
write_case("multivariate",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="multivariate",entry_point="tabelz.executor.hierarchical_clustering",
menu_items=list("hierarchicalClusteringMenuItem"),title="Variable-mode average linkage",
case_kind="nominal",dataset=list(path="reference_data/datasets/MV_CORE.json",sha256=""),
excluded_rows=list(),configuration=list(columns=list("x","y","z"),mode="variables",
linkage="average",standardize=TRUE,cut_k=2L),"repeat"=1L,
expected=list(path=paste0("reference_data/expected/multivariate/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats::hclust/cutree",
citation="R stats::hclust and cutree documentation",
generator=list(path="generators/r/multivariate_variables.R",sha256=""),
independent=TRUE,convention_notes=list("Variable mode uses 1-Pearson correlation; standardize=true is required by the strict request contract.",
"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-MV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
cat("variable-mode reference generated\n")
generators/r/quality_bissell_capability.R
source("common.R", local = TRUE)
# Bissell (1990), Applied Statistics 39(3), 331-340, Tables 3-4 (pp. 338-339).
# The paper uses index +/- 2 SE; the application reports normal 95% intervals.
run <- generator_run(c("stats", "jsonlite"))
values <- matrix(c(
5.66,5.50,5.65,5.59,5.64, 5.61,5.60,5.55,5.68,5.63,
5.58,5.62,5.57,5.58,5.63, 5.54,5.59,5.59,5.59,5.65,
5.60,5.67,5.55,5.57,5.61, 5.62,5.63,5.62,5.66,5.67,
5.59,5.67,5.66,5.54,5.55, 5.57,5.54,5.55,5.59,5.66,
5.61,5.65,5.55,5.66,5.62, 5.60,5.62,5.65,5.67,5.61,
5.58,5.65,5.65,5.63,5.51, 5.58,5.51,5.60,5.68,5.58,
5.67,5.59,5.65,5.69,5.67, 5.66,5.59,5.72,5.63,5.57,
5.59,5.58,5.67,5.67,5.69
), ncol = 5, byrow = TRUE)
export_dataset(data.frame(subgroup = rep(seq_len(15), each = 5), value = as.vector(t(values))),
"QUAL_BISSELL_TABLE3", "Published Table 3: 15 rational subgroups of five",
"Bissell (1990), How Reliable is Your Capability Index?, Applied Statistics 39(3), 331-340, Table 3, p. 338",
"Published numerical example; source attribution retained.", c(subgroup = "Int64", value = "Float64"))
d <- read_dataset("QUAL_BISSELL_TABLE3")
groups <- split(d$value, d$subgroup)
x <- d$value
mu <- mean(x); s <- sd(x); n <- length(x)
# Independently integrate the CDF of the normal range for subgroup size five.
# This supplies full-precision d2 and d3 rather than treating the paper's
# rounded effective degrees of freedom (54 and 57) as exact inputs.
range_cdf <- function(w) vapply(w, function(t) 5 * integrate(
function(v) dnorm(v) * (pnorm(v + t) - pnorm(v))^4,
-9, 9, rel.tol = 1e-10)$value, 0.0)
d2 <- integrate(function(w) pmax(0, 1 - range_cdf(w)), 0, 16, rel.tol = 1e-9)$value
range_second <- integrate(function(w) 2 * w * pmax(0, 1 - range_cdf(w)),
0, 16, rel.tol = 1e-9)$value
d3 <- sqrt(range_second - d2^2)
c4 <- sqrt(2 / 4) * gamma(5 / 2) / gamma(4 / 2)
sigmas <- c(overall = s,
range = mean(vapply(groups, function(g) diff(range(g)), 0.0)) / d2,
stddev = mean(vapply(groups, sd, 0.0)) / c4,
pooled = sqrt(mean(vapply(groups, var, 0.0))))
degrees <- c(overall = 74, range = 15 / (2 * (d3 / d2)^2),
stddev = 15 / (2 * (1 / c4^2 - 1)), pooled = 60)
printed_degrees <- c(overall = 74, range = 54, stddev = 57, pooled = 60)
published_cpk <- c(overall = 0.853, range = 0.831, stddev = 0.831, pooled = 0.853)
published_se <- c(overall = 0.080, range = 0.089, stddev = 0.087, pooled = 0.086)
published_limits <- list(overall = c(0.693, 1.013), range = c(0.653, 1.008),
stddev = c(0.657, 1.005), pooled = c(0.679, 1.027))
tol <- function(v, why, relative = "1e-9") list(comparison = "tolerance", value = dec(unname(v)),
absolute_tolerance = "0", relative_tolerance = relative, rationale = why)
for (i in seq_along(sigmas)) {
method <- names(sigmas)[i]
id <- sprintf("STAT-QUAL-BISSELL-%03d", i)
sigma <- unname(sigmas[i])
index <- min(5.79 - mu, mu - 5.49) / (3 * sigma)
se <- sqrt(index^2 / (2 * degrees[i]) + 1 / (9 * n))
ci <- index + c(-1, 1) * qnorm(.975) * se
overall_index <- min(5.79 - mu, mu - 5.49) / (3 * s)
overall_se <- sqrt(overall_index^2 / (2 * (n - 1)) + 1 / (9 * n))
overall_ci <- overall_index + c(-1, 1) * qnorm(.975) * overall_se
# Decimal Table 4 entries are a publication-level comparison; retain full
# precision separately for the engine. The pooled printed .086 differs from
# the .0868 computed with Table 4's f=60 and its stated SE formula.
outputs <- list("/rows/0/n" = list(comparison = "exact", value = n,
rationale = "Table 3 has 15 complete subgroups of five."),
"/rows/0/mean" = tol(mu, "R mean of the published Table 3 measurements."),
"/rows/0/std" = tol(s, "R sample SD of all 75 Table 3 measurements."),
"/rows/0/ppk" = tol(overall_index,
"R recomputation of overall index; called Cpk in the paper's overall-s method."),
"/rows/0/ppk_ci_lower" = tol(overall_ci[1],
paste("R normal 95% lower endpoint from nu=74 and SE", dec(overall_se),
"; the paper instead prints index minus 2 SE."), "1e-7"),
"/rows/0/ppk_ci_upper" = tol(overall_ci[2],
paste("R normal 95% upper endpoint from nu=74 and SE", dec(overall_se),
"; the paper instead prints index plus 2 SE."), "1e-7"),
"/rows/0/uncertainty_method" = list(comparison = "contains",
values = list("Ppk nu=N-1"),
rationale = "Public uncertainty disclosure identifies the overall-s degrees of freedom."))
if (method != "overall") {
outputs[["/rows/0/within_sigma"]] <- tol(sigma, paste("Table 3", method,
"within-sigma estimator recomputed from subgroup observations."))
outputs[["/rows/0/cpk"]] <- tol(index,
"R recomputation of Cpk using the stated subgroup sigma estimator.")
outputs[["/rows/0/cpk_ci_lower"]] <- tol(ci[1],
paste("R normal 95% lower endpoint, effective nu", dec(degrees[i]),
"and SE", dec(se), "; the paper prints a +/-2 SE interval."), "1e-7")
outputs[["/rows/0/cpk_ci_upper"]] <- tol(ci[2],
paste("R normal 95% upper endpoint, effective nu", dec(degrees[i]),
"and SE", dec(se), "; the paper prints a +/-2 SE interval."), "1e-7")
outputs[["/rows/0/uncertainty_method"]]$values <- list(
paste0("Cpk nu=", trimws(formatC(degrees[i], digits = 10, format = "fg"))),
"Ppk nu=N-1")
}
write_expected("quality", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("quality", id, list(schema_version = "2.0.0", case_id = id,
version = "2.0.0", family = "quality",
entry_point = "tabelz.analysis.spec_sheet:compute_capability",
menu_items = list("capabilityMenuItem"),
title = paste("Bissell Table 3/4 capability:", method, "sigma"), case_kind = "nominal",
dataset = list(path = "reference_data/datasets/QUAL_BISSELL_TABLE3.json", sha256 = ""),
excluded_rows = list(), configuration = list(limits = list(value = list(5.49, 5.79)),
method = "normal", study = if (method == "overall")
list(subgroup_mode = "individuals", within_method = "moving_range",
require_stability = FALSE, require_msa = FALSE) else
list(subgroup_mode = "constant", subgroup_size = 5, within_method = method,
require_stability = FALSE, require_msa = FALSE)),
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/quality/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R recomputation from Bissell (1990), Table 3",
citation = "Bissell (1990), How Reliable is Your Capability Index?, Applied Statistics 39(3), 331-340, Tables 3-4, pp. 338-339",
generator = list(path = "generators/r/quality_bissell_capability.R", sha256 = ""),
independent = TRUE, convention_notes = list(
paste("Table 4 printed effective f", printed_degrees[i], "Cpk", published_cpk[i], "and SE", published_se[i],
"with +/-2 SE limits", paste(published_limits[[method]], collapse = " to "),
"; independently computed effective nu", dec(degrees[i]),
"and full-precision SE", dec(se), "."),
"The paper calls the overall-s index Cpk; Tabelz calls the same estimator Ppk.",
"The paper's +/-2 SE limits are not asserted against Tabelz's z=1.95996 intervals.",
"The pooled printed SE .086 is discrepant with .0868 from the stated formula and f=60; no rounding equivalence is claimed.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-QUAL-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
cat("Bissell Table 3/4 capability cases generated\n")
generators/r/quality_capability_counts_batches.R
source("common.R", local = TRUE)
# References for two capability entry points:
# tabelz.analysis.spec_sheet:compute_attribute_capability (manual chapter 08
# lines 150-177): binomial rate = sum d / sum n with a Wilson interval,
# PPM = 1e6 rate, yield 100 (1 - p); Poisson rate = sum c / sum exposure
# with an exact Poisson interval, yield 100 exp(-u); sigma level where defined.
# tabelz.executor.batch_capability (chapter 43): one-way Gaussian random
# intercept; sigma_within = sqrt(s2_e), sigma_overall = sqrt(s2_e + s2_b);
# Cp, CPL, CPU, Cpk from the analyzed grand mean; conservative Gaussian
# simultaneous bounds for within and total Cp and Cpk (chapter 43, issue #5).
# Variance components are nonnegative constrained REML (chapter 43, statistician's
# ruling on GitHub issue #2). For balanced batches of size n the exact solution is
# s2_e = MSW and s2_b = (MSB - MSW) / n when MSB > MSW; otherwise s2_b = 0 and
# s2_e = SST / (N - 1), the sample variance of the retained observations. That
# differs from truncated ANOVA, which keeps MSW at the boundary.
run <- generator_run(c("stats", "jsonlite"))
family <- "quality"
generator <- "generators/r/quality_capability_counts_batches.R"
attr_entry <- "tabelz.analysis.spec_sheet:compute_attribute_capability"
batch_entry <- "tabelz.executor.batch_capability"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form capability reference; owner 1e-9 tier.", why))
interval <- function(lo, hi, why) list(
comparison = "interval", lower = dec(unname(lo)), upper = dec(unname(hi)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form interval; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, entry, menu, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(), error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats::prop.test, stats::poisson.test and one-way ANOVA mean squares",
citation = "R stats::prop.test (correct = FALSE gives the Wilson interval), stats::poisson.test and stats::aov documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-QUAL-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
# ---- attribute capability ------------------------------------------------------------
set.seed(20261015)
k <- 25L
inspected <- sample(150:250, k, replace = TRUE)
defectives <- rbinom(k, inspected, 0.012)
exposure <- round(runif(k, 1, 4), 3)
defects <- rpois(k, 2.2 * exposure)
export_dataset(data.frame(defectives = as.integer(defectives), inspected = as.integer(inspected),
defects = as.integer(defects), exposure = exposure),
"QUAL_ATTR_CAP", "Constructed count data, seed 20261015 (sample, rbinom, runif, rpois)",
"R stats::rbinom and stats::rpois documentation", "Constructed validation data; no third-party license.",
c(defectives = "Int64", inspected = "Int64", defects = "Int64", exposure = "Float64"))
a <- read_dataset("QUAL_ATTR_CAP")
binomial_outputs <- function(d, n, level) {
x <- sum(d); m <- sum(n); p <- x / m
ci <- prop.test(x, m, conf.level = level, correct = FALSE)$conf.int
list("/rate" = tol(p, "Total defectives / total inspected."),
"/ppm" = tol(1e6 * p, "1e6 times the rate."),
"/yield_percent" = tol(100 * (1 - p), "100 (1 - p)."),
"/ci_lower" = tol(ci[1], "Wilson lower limit (prop.test, correct = FALSE)."),
"/ci_upper" = tol(ci[2], "Wilson upper limit."),
"/sigma_level" = tol(qnorm(1 - p), "Standard normal quantile of the yield fraction."),
"/total_events" = exact(as.integer(x), "Total defectives."))
}
emit("STAT-QUAL-ATTRCAP-001", attr_entry, "capabilityMenuItem", "Binomial defectives with a Wilson interval", "QUAL_ATTR_CAP",
list(family = "binomial", event_column = "defectives", denominator_column = "inspected"),
binomial_outputs(a$defectives, a$inspected, 0.95),
c("The sigma level is taken as the normal quantile of the yield without a 1.5 shift; the manual says only 'where defined'."))
emit("STAT-QUAL-ATTRCAP-002", attr_entry, "capabilityMenuItem", "Binomial at a 90% confidence level", "QUAL_ATTR_CAP",
list(family = "binomial", event_column = "defectives", denominator_column = "inspected", confidence_level = 0.9),
binomial_outputs(a$defectives, a$inspected, 0.9)[c("/ci_lower", "/ci_upper")],
c("A non-default confidence level (the engine API accepts it; the dialog fixes 95%)."), kind = "axis_coverage")
poisson_outputs <- function(c, e, level) {
x <- sum(c); t <- sum(e); u <- x / t
ci <- poisson.test(x, t, conf.level = level)$conf.int
list("/rate" = tol(u, "Total defects / total exposure."),
"/yield_percent" = tol(100 * exp(-u), "100 exp(-u)."),
"/ci_lower" = tol(ci[1], "Exact Poisson lower limit (poisson.test)."),
"/ci_upper" = tol(ci[2], "Exact Poisson upper limit."))
}
emit("STAT-QUAL-ATTRCAP-003", attr_entry, "capabilityMenuItem", "Poisson defects with an exposure column", "QUAL_ATTR_CAP",
list(family = "poisson", event_column = "defects", denominator_column = "exposure"),
poisson_outputs(a$defects, a$exposure, 0.95), c("Line 173-176."), kind = "axis_coverage")
emit("STAT-QUAL-ATTRCAP-004", attr_entry, "capabilityMenuItem", "Poisson defects with unit exposure", "QUAL_ATTR_CAP",
list(family = "poisson", event_column = "defects"),
poisson_outputs(a$defects, rep(1, k), 0.95)[c("/rate", "/ci_lower", "/ci_upper")],
c("With the exposure column off, each row has unit exposure (line 161-162)."), kind = "axis_coverage")
m <- read_dataset("QUAL_ATTR_CAP"); m$defectives[c(4, 19)] <- NA
export_dataset(m, "QUAL_ATTR_CAP_MISSING", "QUAL_ATTR_CAP with defectives missing in rows 4 and 19 (1-based)",
"R stats::rbinom documentation", "Constructed validation data; no third-party license.",
c(defectives = "Int64", inspected = "Int64", defects = "Int64", exposure = "Float64"))
mm <- read_dataset("QUAL_ATTR_CAP_MISSING")
excluded <- c(0L, 10L)
keep <- setdiff(which(!is.na(mm$defectives)), excluded + 1L)
emit("STAT-QUAL-ATTRCAP-005", attr_entry, "capabilityMenuItem", "Missing and excluded rows are omitted", "QUAL_ATTR_CAP_MISSING",
list(family = "binomial", event_column = "defectives", denominator_column = "inspected"),
c(binomial_outputs(mm$defectives[keep], mm$inspected[keep], 0.95)[c("/rate", "/ci_lower", "/ci_upper")],
list("/dropped_rows" = exact(2L, "Two rows with a missing count are disclosed and omitted."))),
c("Rows 0 and 10 (0-based) are excluded before the call."), kind = "missing_excluded", excluded = excluded)
over <- read_dataset("QUAL_ATTR_CAP"); over$defectives[2] <- over$inspected[2] + 1L
export_dataset(over, "QUAL_ATTR_CAP_OVER", "QUAL_ATTR_CAP with more defectives than inspected in row 2 (1-based)",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(defectives = "Int64", inspected = "Int64", defects = "Int64", exposure = "Float64"))
emit("STAT-QUAL-ATTRCAP-006", attr_entry, "capabilityMenuItem", "Defectives above inspected are refused", "QUAL_ATTR_CAP_OVER",
list(family = "binomial", event_column = "defectives", denominator_column = "inspected"), list(),
c("Line 159-160."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "cannot exceed inspected counts"))
emit("STAT-QUAL-ATTRCAP-007", attr_entry, "capabilityMenuItem", "Binomial without an inspected column is refused", "QUAL_ATTR_CAP",
list(family = "binomial", event_column = "defectives"), list(),
c("Line 159: inspected counts are required for Binomial."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "inspected-count column"))
# ---- batch capability ------------------------------------------------------------------
batch_ref <- function(y, g, lsl, usl, level = 0.95) {
g <- factor(g, levels = unique(g)); G <- nlevels(g); N <- length(y); n <- N / G
fit <- anova(lm(y ~ g)); msw <- fit[["Mean Sq"]][2]; msb <- fit[["Mean Sq"]][1]
if (msb > msw) { s2e <- msw; s2b <- (msb - msw) / n } else { s2e <- var(y); s2b <- 0 }
within <- sqrt(s2e); overall <- sqrt(s2e + s2b); mu <- mean(y)
# Conservative Gaussian simultaneous bounds (statistician's ruling on issue #5):
# alpha/3 to each of e, b and mu, two-sided tails alpha/6, union bound, and each
# index at its extremes over the resulting box.
sizes <- as.numeric(table(g)); m <- tapply(y, g, mean)[levels(g)]
W <- sum((y - m[as.integer(g)])^2); Q <- sum((m - mean(m))^2); d <- N - G
a6 <- (1 - level) / 6
eL <- W / qchisq(1 - a6, d); eU <- W / qchisq(a6, d)
bL <- max(0, Q / qchisq(1 - a6, G - 1) - eU / min(sizes))
bU <- max(0, Q / qchisq(a6, G - 1) - eL / max(sizes))
mu_hat <- sum(sizes * m) / N
half <- qnorm(1 - a6) * sqrt(eU / N + bU * sum(sizes^2) / N^2)
muL <- mu_hat - half; muU <- mu_hat + half
margin <- function(v) min(c(if (!is.null(lsl)) v - lsl, if (!is.null(usl)) usl - v))
bounds <- function(vL, vU) {
cp <- if (!is.null(lsl) && !is.null(usl)) c((usl - lsl) / (6 * sqrt(vU)), (usl - lsl) / (6 * sqrt(vL))) else NA
hmin <- min(margin(muL), margin(muU))
hmax <- if (!is.null(lsl) && !is.null(usl)) margin(min(max((lsl + usl) / 2, muL), muU)) else max(margin(muL), margin(muU))
lo <- if (hmin >= 0) hmin / (3 * sqrt(vU)) else hmin / (3 * sqrt(vL))
hi <- if (hmax >= 0) hmax / (3 * sqrt(vL)) else hmax / (3 * sqrt(vU))
list(cp = cp, cpk = c(lo, hi))
}
idx <- function(s, b) {
cp <- if (!is.null(lsl) && !is.null(usl)) (usl - lsl) / (6 * s) else NA
cpl <- if (!is.null(lsl)) (mu - lsl) / (3 * s) else NA; cpu <- if (!is.null(usl)) (usl - mu) / (3 * s) else NA
cpk <- min(c(cpl, cpu), na.rm = TRUE)
list(sigma = s, cp = cp, cpl = cpl, cpu = cpu, cpk = cpk, cp_ci = b$cp, cpk_ci = b$cpk)
}
list(mu = mu, within = idx(within, bounds(eL, eU)), overall = idx(overall, bounds(eL + bL, eU + bU)), s2b = s2b)
}
batch_outputs <- function(r, prefix, two_sided = TRUE) {
x <- r[[prefix]]; node <- paste0("/", prefix, "_capability")
out <- list()
out[[paste0(node, "/sigma")]] <- tol(x$sigma, paste(prefix, "sigma from the variance components."))
out[[paste0(node, "/cpk")]] <- tol(x$cpk, "min(CPL, CPU).")
out[[paste0(node, "/cpk_interval")]] <- interval(x$cpk_ci[1], x$cpk_ci[2], "Conservative Gaussian simultaneous bound (issue #5).")
if (two_sided) {
out[[paste0(node, "/cp")]] <- tol(x$cp, "(USL - LSL) / (6 sigma).")
out[[paste0(node, "/cp_interval")]] <- interval(x$cp_ci[1], x$cp_ci[2], "Conservative Gaussian simultaneous bound (issue #5).")
}
out
}
set.seed(20261016)
G <- 8L; nb <- 6L
batch <- rep(sprintf("B%d", seq_len(G)), each = nb)
yv <- round(50 + rep(rnorm(G, 0, 0.6), each = nb) + rnorm(G * nb, 0, 0.9), 4)
export_dataset(data.frame(batch = batch, y = yv), "QUAL_BATCH", "Constructed: 8 balanced batches of 6, between SD 0.6, within SD 0.9, seed 20261016",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(batch = "String", y = "Float64"))
b <- read_dataset("QUAL_BATCH")
rb <- batch_ref(b$y, b$batch, 46, 54)
if (rb$s2b <= 0) stop("expected a positive between-batch component")
emit("STAT-QUAL-BATCH-001", batch_entry, "batchCapabilityMenuItem", "Within and overall capability of balanced batches", "QUAL_BATCH",
list(response_column = "y", batch_column = "batch", lower_limit = 46, upper_limit = 54),
c(batch_outputs(rb, "within"), batch_outputs(rb, "overall"),
list("/overall_mean" = tol(rb$mu, "Analyzed grand mean."), "/total_n" = exact(48L, "48 analyzed rows."))),
c("Balanced batches: REML equals the ANOVA estimator, s2_e = MSW and s2_b = (MSB - MSW) / n."))
emit("STAT-QUAL-BATCH-002", batch_entry, "batchCapabilityMenuItem", "Upper limit only at a 90% level", "QUAL_BATCH",
list(response_column = "y", batch_column = "batch", upper_limit = 54, confidence_level = 0.9),
c(batch_outputs(batch_ref(b$y, b$batch, NULL, 54, 0.9), "within", FALSE),
list("/within_capability/cp" = list(comparison = "absent", rationale = "Cp needs both limits."))),
c("One-sided upper specification; Cpk = CPU."), kind = "axis_coverage")
set.seed(20261017)
flat <- round(50 + rnorm(G * nb, 0, 0.9), 4)
flat <- flat - ave(flat, batch) + mean(flat) + rep(seq(-0.02, 0.02, length.out = G), each = nb)
export_dataset(data.frame(batch = batch, y = round(flat, 6)), "QUAL_BATCH_FLAT",
"Constructed: batch means forced nearly equal, seed 20261017", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(batch = "String", y = "Float64"))
bf <- read_dataset("QUAL_BATCH_FLAT")
rf <- batch_ref(bf$y, bf$batch, 46, 54)
if (rf$s2b != 0) stop("expected MSB < MSW")
emit("STAT-QUAL-BATCH-003", batch_entry, "batchCapabilityMenuItem", "A zero between-batch component makes within and overall coincide", "QUAL_BATCH_FLAT",
list(response_column = "y", batch_column = "batch", lower_limit = 46, upper_limit = 54),
list("/within_capability/sigma" = tol(rf$within$sigma, "Constrained REML at the zero boundary: sqrt(SST / (N - 1)) = sd(y)."),
"/overall_capability/sigma" = tol(rf$overall$sigma, "Chapter 43: at a zero optimum total and within capability coincide.")),
c("MSB < MSW, so the constrained REML optimum has zero between-batch variance (issue #2 ruling).",
"Until 2026-09-24 this case expected sqrt(MSW), the truncated-ANOVA convention; the statistician ruled for REML."),
kind = "boundary")
emit("STAT-QUAL-BATCH-004", batch_entry, "batchCapabilityMenuItem", "No specification limit is refused", "QUAL_BATCH",
list(response_column = "y", batch_column = "batch"), list(),
c("Line 6: at least one specification limit."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "At least one specification limit"))
generators/r/quality_johnson_supplied_parameters.R
source("common.R", local = TRUE)
# Slifker & Shapiro (1980), Technometrics 22(2), 239-246, p. 243.
# Published percentile coordinates and rounded system parameters test only
# the forward transform. They do not validate MLE or automatic family choice.
run <- generator_run(c("stats", "jsonlite"))
examples <- list(
list(id = "STAT-QUAL-JOHNSON-PARAM-001", dataset = "QUAL_JOHNSON_SU_PUBLISHED",
family = "SU", x = c(16.689, 15.242, 13.581, 10.409),
a = 1.402, b = 2.333, loc = 15.516, scale = 1.585,
printed = c(3, 1, -1, -3)),
list(id = "STAT-QUAL-JOHNSON-PARAM-002", dataset = "QUAL_JOHNSON_SB_PUBLISHED",
family = "SB", x = c(0.786, 0.635, 0.516, 0.432),
a = 2.373, b = 1.959, loc = 0.295, scale = 1.203,
printed = c(1.645, 0.5483, -0.5483, -1.645)))
for (e in examples) {
export_dataset(data.frame(value = e$x), e$dataset,
paste("Slifker-Shapiro published", e$family, "percentile coordinates"),
"Slifker and Shapiro (1980), The Johnson System: Selection and Parameter Estimation, Technometrics 22(2), 239-246, p. 243",
"Published numerical example; source attribution retained.", c(value = "Float64"))
d <- read_dataset(e$dataset)$value
z <- if (e$family == "SU") e$a + e$b * asinh((d - e$loc) / e$scale) else {
u <- (d - e$loc) / e$scale
e$a + e$b * log(u / (1 - u))
}
outputs <- setNames(lapply(seq_along(z), function(i) list(
comparison = "tolerance", value = dec(z[i]), absolute_tolerance = "1e-12",
relative_tolerance = "1e-12",
rationale = paste("Forward Johnson", e$family, "score from the published rounded parameters;",
"paper's intended normal coordinate", e$printed[i], "is approximate."))),
paste0("/scores/", seq_along(z) - 1L))
write_expected("quality", e$id, list(schema_version = "2.0.0", case_id = e$id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("quality", e$id, list(schema_version = "2.0.0", case_id = e$id,
version = "2.0.0", family = "quality",
entry_point = "tabelz.analysis.johnson_transform:johnson_transform_value",
menu_items = list("capabilityMenuItem"),
title = paste("Slifker-Shapiro supplied-parameter Johnson", e$family, "transform"),
case_kind = "nominal",
dataset = list(path = paste0("reference_data/datasets/", e$dataset, ".json"), sha256 = ""),
excluded_rows = list(),
configuration = list(measurement_column = "value", family = e$family,
a = e$a, b = e$b, loc = e$loc, scale = e$scale), "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/quality/", e$id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R recomputation from Slifker and Shapiro (1980), p. 243",
citation = "Slifker and Shapiro (1980), The Johnson System: Selection and Parameter Estimation, Technometrics 22(2), 239-246, p. 243",
generator = list(path = "generators/r/quality_johnson_supplied_parameters.R", sha256 = ""),
independent = TRUE,
convention_notes = list("The paper prints rounded parameters and percentile coordinates; expected scores apply the formula to exactly those rounded values.",
"This exercises only supplied-parameter transformation; it does not qualify Tabelz Johnson MLE or automatic family selection.",
"The retained binding sends each supplied value through the public scalar helper in the analysis worker; no parameter fitting is performed.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-QUAL-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
cat("Johnson supplied-parameter cases generated\n")
generators/r/quality_ma_shortrun.R
source("common.R", local = TRUE)
# References for two SPC executors:
# tabelz.executor.spc_moving_average (Montgomery, 6th ed., Example 9.3): M_t is the mean of
# the active window of k observations (k = 1..w under the expanding policy),
# limits xbar0 +/- 3 sigma0 / sqrt(k); sigma0 is MR-bar / (2/sqrt(pi)) or the
# sample standard deviation of the baseline; missing and excluded rows are
# omitted and the retained rows become adjacent; a stage column resets the
# window; the full-window policy marks the first w - 1 limits unavailable.
# tabelz.executor.spc_short_run (Minitab Z-MR methods and formulas): z_i =
# (x_i - mu_p) / sigma_p with the known product parameters, MR_i = |z_i -
# z_(i-1)| between adjacent valid rows (missing or excluded rows break
# adjacency; a product change breaks it only when reset is on), Z limits
# +/- 3, MR limit d2 + 3 d3 with d2 = 2/sqrt(pi), d3 = sqrt(2 - 4/pi);
# signals are strict z < -3, z > 3 or MR > UCL.
run <- generator_run(c("stats", "jsonlite"))
family <- "quality"
generator <- "generators/r/quality_ma_shortrun.R"
ma_entry <- "tabelz.executor.spc_moving_average"
sr_entry <- "tabelz.executor.spc_short_run"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "1e-12",
relative_tolerance = "1e-9", rationale = paste("Closed-form chart statistic; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(id, entry, menu, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(), error = NULL) {
citation <- if (entry == ma_entry)
"Montgomery, Introduction to Statistical Quality Control, 6th ed., Example 9.3, pp. 428-429; NIST/SEMATECH e-Handbook 6.3.2.1 for d2 moving-range factor"
else
"Minitab Z-MR Chart Methods and Formulas, https://support.minitab.com/en-us/minitab/help-and-how-to/quality-and-process-improvement/control-charts/how-to/variables-charts-for-individuals/z-mr-chart/methods-and-formulas/methods-and-formulas/"
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "Hand-coded chart formulas in R",
citation = citation,
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes,
if (entry == ma_entry) "Montgomery's Example 9.3 uses known mean and sigma; estimated-baseline and full-window cases here are independent Tabelz-convention calculations." else
"Minitab prints rounded Z-MR factors; the generator uses d2=2/sqrt(pi), d3=sqrt(2-4/pi), with full-precision MR UCL d2+3d3.",
"All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-QUAL-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
d2 <- 2 / sqrt(pi); d3 <- sqrt(2 - 4 / pi)
# ---- moving average ----------------------------------------------------------------
ma_menu <- "spcMovingAverageMenuItem"
moving <- function(x, w, center, sigma, stage = rep("", length(x)), full = FALSE) {
m <- numeric(length(x)); k <- integer(length(x)); start <- 1L
for (i in seq_along(x)) {
if (i > 1 && stage[i] != stage[i - 1]) start <- i
lo <- max(start, i - w + 1L); k[i] <- i - lo + 1L; m[i] <- mean(x[lo:i])
}
ucl <- center + 3 * sigma / sqrt(k); lcl <- center - 3 * sigma / sqrt(k)
available <- if (full) k == w else rep(TRUE, length(x))
list(m = m, k = k, ucl = ucl, lcl = lcl, breach = available & (m > ucl | m < lcl), available = available)
}
ma_outputs <- function(r, indexes = c(0L, 1L, 2L, 10L, 29L)) {
out <- list()
for (i in indexes) {
node <- sprintf("/points/%d", i)
out[[paste0(node, "/moving_average")]] <- tol(r$m[i + 1], "Mean of the active window.")
out[[paste0(node, "/window_size")]] <- exact(as.integer(r$k[i + 1]), "Active window size.")
out[[paste0(node, "/ucl")]] <- tol(r$ucl[i + 1], "Centre + 3 sigma / sqrt(k).")
out[[paste0(node, "/lcl")]] <- tol(r$lcl[i + 1], "Centre - 3 sigma / sqrt(k).")
}
out
}
set.seed(20261010)
x <- round(c(rnorm(30, 12, 0.8), rnorm(20, 12.9, 0.8)), 4)
export_dataset(data.frame(x = x, phase1 = c(rep(TRUE, 30), rep(FALSE, 20)),
stage = c(rep("S1", 25), rep("S2", 25))),
"QUAL_MA", "Constructed: 30 points N(12, 0.8^2) then 20 points N(12.9, 0.8^2), seed 20261010",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(x = "Float64", phase1 = "Boolean", stage = "String"))
d <- read_dataset("QUAL_MA")
base <- d$x[d$phase1]
s_mr <- mean(abs(diff(base))) / d2
r <- moving(d$x, 3, mean(base), s_mr)
emit("STAT-QUAL-MA-001", ma_entry, ma_menu, "Width 3 with a Phase I marker and the moving-range sigma", "QUAL_MA",
list(value_column = "x", baseline_column = "phase1"),
c(ma_outputs(r), list("/baseline_mean" = tol(mean(base), "Mean of the 30 Phase I rows."),
"/baseline_sigma" = tol(s_mr, "MR-bar / d2 over the Phase I rows."),
"/baseline_count" = exact(30L, "Thirty Phase I rows."))),
c("Expanding startup: k = 1, 2, 3 (line 28-29)."))
s_sd <- sd(base)
r <- moving(d$x, 5, mean(base), s_sd)
emit("STAT-QUAL-MA-002", ma_entry, ma_menu, "Width 5 with the sample standard deviation", "QUAL_MA",
list(value_column = "x", baseline_column = "phase1", window_width = 5L, sigma_estimator = "stddev"),
c(ma_outputs(r), list("/baseline_sigma" = tol(s_sd, "Sample standard deviation of the Phase I rows."))),
c("sigma_estimator = stddev (line 29-31)."), kind = "axis_coverage")
r <- moving(d$x, 4, 12, 0.8, full = TRUE)
emit("STAT-QUAL-MA-003", ma_entry, ma_menu, "Full-window startup with historical parameters", "QUAL_MA",
list(value_column = "x", window_width = 4L, partial_window_policy = "full_window", historical_mean = 12, historical_sigma = 0.8),
list("/points/0/limits_available" = exact(FALSE, "The first w - 1 rows have no limits (line 34-36)."),
"/points/2/limits_available" = exact(FALSE, "Still a partial window."),
"/points/3/limits_available" = exact(TRUE, "The first complete window starts monitoring."),
"/points/3/ucl" = tol(12 + 3 * 0.8 / 2, "Historical centre + 3 sigma / sqrt(4)."),
"/points/0/moving_average" = tol(r$m[1], "The expanding partial statistic is still reported.")),
c("Historical mean 12 and sigma 0.8 are used exactly (line 12-13)."), kind = "axis_coverage")
r <- moving(d$x, 3, 12, 0.8, stage = d$stage)
emit("STAT-QUAL-MA-004", ma_entry, ma_menu, "A stage column resets the window", "QUAL_MA",
list(value_column = "x", window_width = 3L, stage_column = "stage", historical_mean = 12, historical_sigma = 0.8),
list("/points/25/window_size" = exact(1L, "The first row of stage S2 starts a new window (line 39-47)."),
"/points/26/moving_average" = tol(r$m[27], "Mean of the first two S2 rows."),
"/points/24/window_size" = exact(3L, "A full window at the end of S1.")),
c("Stages S1 (rows 1-25) and S2 (rows 26-50)."), kind = "axis_coverage")
r <- moving(d$x, 1, mean(base), s_mr)
emit("STAT-QUAL-MA-005", ma_entry, ma_menu, "Width one is the individuals specification", "QUAL_MA",
list(value_column = "x", window_width = 1L, baseline_column = "phase1"),
list("/points/40/moving_average" = tol(d$x[41], "Each average is the observation itself (line 6-8)."),
"/points/40/ucl" = tol(mean(base) + 3 * s_mr, "Centre + 3 sigma.")),
c("Width 1."), kind = "boundary")
m <- read_dataset("QUAL_MA"); m$x[c(6, 33)] <- NA
export_dataset(m, "QUAL_MA_MISSING", "QUAL_MA with x missing in rows 6 and 33 (1-based)", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(x = "Float64", phase1 = "Boolean", stage = "String"))
mm <- read_dataset("QUAL_MA_MISSING")
excluded <- c(9L, 40L)
keep <- setdiff(which(!is.na(mm$x)), excluded + 1L)
xk <- mm$x[keep]; bk <- mm$phase1[keep]
r <- moving(xk, 3, mean(xk[bk]), mean(abs(diff(xk[bk]))) / d2)
emit("STAT-QUAL-MA-006", ma_entry, ma_menu, "Missing and excluded rows are omitted before the windows", "QUAL_MA_MISSING",
list(value_column = "x", baseline_column = "phase1"),
c(ma_outputs(r, c(0L, 5L, 10L, 30L)), list("/excluded_count" = exact(2L, "Two excluded rows."))),
c("Line 41-43: the retained rows become adjacent; excluded rows 9 and 40 (0-based) and two missing values are omitted."),
kind = "missing_excluded", excluded = excluded)
export_dataset(data.frame(x = rep(5, 8), phase1 = rep(TRUE, 8)), "QUAL_MA_CONSTANT", "Constructed: eight equal values",
"Hand-constructed", "Constructed validation data; no third-party license.", c(x = "Float64", phase1 = "Boolean"))
emit("STAT-QUAL-MA-007", ma_entry, ma_menu, "A constant baseline is rejected", "QUAL_MA_CONSTANT",
list(value_column = "x", baseline_column = "phase1"), list(),
c("Line 32: constant estimated baseline data are rejected; the fragment is the product's wording."), kind = "invalid_input",
error = list(error_class = "SpcMovingAverageExecutionError", message_contains = "no varying measurements"))
emit("STAT-QUAL-MA-008", ma_entry, ma_menu, "A historical mean without a sigma is refused", "QUAL_MA",
list(value_column = "x", historical_mean = 12), list(),
c("Line 12-13: the two historical values must be supplied together."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "supplied together"))
# ---- short-run Z-MR --------------------------------------------------------------------
sr_menu <- "spcShortRunMenuItem"
set.seed(20261011)
prod <- rep(c("A", "A", "B", "B", "B", "C"), length.out = 36)
params <- list(A = c(10, 0.5), B = c(25, 1.2), C = c(3, 0.1))
x <- round(vapply(prod, function(p) rnorm(1, params[[p]][1], params[[p]][2]), 0), 4)
x[14] <- params[[prod[14]]][1] + 3.4 * params[[prod[14]]][2]
export_dataset(data.frame(x = x, product = prod, stage = c(rep("I", 20), rep("II", 16))), "QUAL_SHORTRUN",
"Constructed: three products with known parameters, seed 20261011; row 14 set 3.4 sigma above its centre",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(x = "Float64", product = "String", stage = "String"))
s <- read_dataset("QUAL_SHORTRUN")
products <- list(list(product = "A", center = 10, sigma = 0.5), list(product = "B", center = 25, sigma = 1.2),
list(product = "C", center = 3, sigma = 0.1))
short_run <- function(x, prod, valid, reset = FALSE, stage = NULL) {
mu <- vapply(prod, function(p) params[[p]][1], 0); sg <- vapply(prod, function(p) params[[p]][2], 0)
z <- (x - mu) / sg; mr <- rep(NA_real_, length(x))
for (i in seq_along(x)[-1]) {
if (!valid[i] || !valid[i - 1]) next
if (reset && prod[i] != prod[i - 1]) next
if (!is.null(stage) && stage[i] != stage[i - 1]) next
mr[i] <- abs(z[i] - z[i - 1])
}
list(z = ifelse(valid, z, NA), mr = mr)
}
sr_outputs <- function(r, rows, status = NULL) {
out <- list(); ucl <- d2 + 3 * d3
for (i in rows) {
node <- sprintf("/observations/%d", i)
out[[paste0(node, "/z")]] <- if (is.na(r$z[i + 1])) absent("No z for an ineligible row.") else tol(r$z[i + 1], "(x - mu_p) / sigma_p.")
out[[paste0(node, "/moving_range")]] <- if (is.na(r$mr[i + 1])) absent("Adjacency is broken here.") else tol(r$mr[i + 1], "|z_i - z_(i-1)|.")
}
out
}
all_valid <- rep(TRUE, 36)
r <- short_run(s$x, s$product, all_valid)
emit("STAT-QUAL-SHORTRUN-001", sr_entry, sr_menu, "Standardized Z-MR across three products", "QUAL_SHORTRUN",
list(measurement_column = "x", product_column = "product", products = products),
c(sr_outputs(r, c(0L, 1L, 2L, 13L, 14L, 35L)),
list("/observations/13/signal_reasons" = list(comparison = "contains", values = list("Z"),
rationale = "Row 14 (1-based) lies 3.4 sigma above its product centre: z > 3."))),
c("Reset at product change is off, so cross-product pairs contribute moving ranges (line 28-31).",
sprintf("MR upper limit d2 + 3 d3 = %.15g.", d2 + 3 * d3)))
r <- short_run(s$x, s$product, all_valid, reset = TRUE)
emit("STAT-QUAL-SHORTRUN-002", sr_entry, sr_menu, "Reset the moving range at each product change", "QUAL_SHORTRUN",
list(measurement_column = "x", product_column = "product", products = products, reset_on_product_change = TRUE),
sr_outputs(r, c(1L, 2L, 3L, 5L, 6L)), c("A product transition starts a new moving-range sequence (line 30-31)."),
kind = "axis_coverage")
r <- short_run(s$x, s$product, all_valid, stage = s$stage)
emit("STAT-QUAL-SHORTRUN-003", sr_entry, sr_menu, "A stage column breaks adjacency", "QUAL_SHORTRUN",
list(measurement_column = "x", product_column = "product", stage_column = "stage", products = products),
sr_outputs(r, c(19L, 20L, 21L)), c("Stage I is rows 1-20 and stage II rows 21-36 (1-based)."), kind = "axis_coverage")
m <- read_dataset("QUAL_SHORTRUN"); m$x[8] <- NA
export_dataset(m, "QUAL_SHORTRUN_MISSING", "QUAL_SHORTRUN with x missing in row 8 (1-based)", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(x = "Float64", product = "String", stage = "String"))
mm <- read_dataset("QUAL_SHORTRUN_MISSING")
valid <- !is.na(mm$x); valid[c(3, 25) + 1] <- FALSE
r <- short_run(mm$x, mm$product, valid)
emit("STAT-QUAL-SHORTRUN-004", sr_entry, sr_menu, "Missing and excluded rows break adjacency but stay in the table", "QUAL_SHORTRUN_MISSING",
list(measurement_column = "x", product_column = "product", products = products),
c(sr_outputs(r, c(3L, 4L, 7L, 8L, 25L, 26L)),
list("/observations/3/status" = exact("excluded", "Excluded rows are retained in the detail table (line 47-48)."),
"/observations/7/status" = exact("missing", "A missing measurement."))),
c("Rows 3 and 25 (0-based) are excluded; row 7 (0-based) is missing."), kind = "missing_excluded", excluded = c(3L, 25L))
emit("STAT-QUAL-SHORTRUN-005", sr_entry, sr_menu, "A product without parameters is an input error", "QUAL_SHORTRUN",
list(measurement_column = "x", product_column = "product", products = products[1:2]), list(),
c("Line 13-14: every active product must have a record."), kind = "invalid_input",
error = list(error_class = "ShortRunExecutionError", message_contains = "parameters were supplied for product"))
emit("STAT-QUAL-SHORTRUN-006", sr_entry, sr_menu, "A zero sigma is refused", "QUAL_SHORTRUN",
list(measurement_column = "x", product_column = "product",
products = list(list(product = "A", center = 10, sigma = 0), products[[2]], products[[3]])), list(),
c("Line 14: sigma must be strictly positive."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "must be finite and positive"))
generators/r/quality_multivariate_spc.R
source("common.R", local = TRUE)
# References for the multivariate control charts:
# tabelz.analysis.spc_multivariate:compute_hotelling_t2 (individual Phase II)
# tabelz.analysis.spc_multivariate_subgroups:compute_hotelling_subgroups (chapter 46)
# tabelz.analysis.spc_mewma:compute_mewma (chapter 47)
# Individual chart: baseline mean and sample covariance S (denominator m - 1)
# from the reference rows; T^2 = (x - xbar)' S^-1 (x - xbar). The manual has no
# chapter for this chart and states no limit; the standard Phase II limit for a
# future individual observation, p (m + 1)(m - 1) / (m (m - p)) F_(1-alpha)(p, m - p)
# (Montgomery, Introduction to Statistical Quality Control, section 11.3), is used.
# Subgroup chart (chapter 46 lines 47-81): pooled covariance is the mean of the
# subgroup covariances; T^2_j = n (xbar_j - xbarbar)' Sbar^-1 (xbar_j - xbarbar);
# UCL = p (m + 1)(n - 1) / (mn - m - p + 1) F_(1-alpha)(p, mn - m - p + 1);
# generalized variance |S_j| with Alt's b1, b2 and |Sigma| = mean |S_i| / b1.
# MEWMA (chapter 47 lines 53-65): Z_0 = xbar, Z_i = lambda x_i + (1 - lambda) Z_(i-1),
# Sigma_Zi = lambda (1 - (1 - lambda)^(2i)) / (2 - lambda) S.
run <- generator_run(c("stats", "jsonlite"))
family <- "quality"
generator <- "generators/r/quality_multivariate_spc.R"
t2_entry <- "tabelz.analysis.spc_multivariate:compute_hotelling_t2"
sub_entry <- "tabelz.analysis.spc_multivariate_subgroups:compute_hotelling_subgroups"
mewma_entry <- "tabelz.analysis.spc_mewma:compute_mewma"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form multivariate statistic; owner 1e-9 tier.", why))
seqtol <- function(values, why) list(
comparison = "sequence", values = lapply(unname(values), dec), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form multivariate statistic; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, entry, menu, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(), error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R base linear algebra (solve, det, cov) and stats::qf",
citation = paste("Montgomery, Introduction to Statistical Quality Control, 6th ed., sec. 11.3 and Example 11.1;",
"Alt, Multivariate Quality Control, Encyclopedia of Statistical Sciences 6, 1985, pp. 110-122;",
"Lowry et al., Technometrics 34(1), 1992, pp. 46-53; R base::solve, base::det, stats::cov and stats::qf"),
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes,
if (entry == mewma_entry) "Lowry finite-time MEWMA covariance is used; user-set h is not an independently calibrated ARL claim." else
if (entry == sub_entry) "Montgomery's Phase-II subgroup T-squared F limit and Alt's b1/b2 determinant formulas use the configured m, n and p." else
"Montgomery's Phase-II individual T-squared F limit applies to a future observation against the estimated baseline.",
"All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-QUAL-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
quad <- function(v, inv) as.numeric(t(v) %*% inv %*% v)
# ---- individual observations -------------------------------------------------------
set.seed(20261012)
L <- matrix(c(1, 0, 0, 0.6, 0.8, 0, 0.3, -0.2, 0.9), 3, byrow = TRUE)
ref <- matrix(rnorm(40 * 3), 40) %*% t(L)
mon <- matrix(rnorm(20 * 3), 20) %*% t(L); mon[11:20, 1] <- mon[11:20, 1] + 2.2
X <- round(rbind(ref, mon) + matrix(rep(c(10, 20, 5), each = 60), 60), 5)
export_dataset(data.frame(a = X[, 1], b = X[, 2], c = X[, 3], role = c(rep("ref", 40), rep("mon", 20)),
stage = c(rep("ref", 40), rep("S1", 10), rep("S2", 10))),
"QUAL_T2", "Constructed: 40 reference and 20 monitoring rows of three correlated variables, seed 20261012; a +2.2 shift in a from monitoring row 11",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(a = "Float64", b = "Float64", c = "Float64", role = "String", stage = "String"))
d <- read_dataset("QUAL_T2")
vars <- c("a", "b", "c")
R0 <- as.matrix(d[d$role == "ref", vars]); M0 <- as.matrix(d[d$role == "mon", vars])
m <- nrow(R0); p <- 3L; xbar <- colMeans(R0); S <- cov(R0); Si <- solve(S)
t2 <- apply(M0, 1, function(x) quad(x - xbar, Si))
ucl_ind <- function(alpha) p * (m + 1) * (m - 1) / (m * (m - p)) * qf(1 - alpha, p, m - p)
for (alpha in c(0.0027, 0.01)) {
id <- if (alpha == 0.0027) "STAT-QUAL-T2-001" else "STAT-QUAL-T2-002"
emit(id, t2_entry, "hotellingMenuItem", sprintf("Individual Phase II T-squared at alpha %g", alpha), "QUAL_T2",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref", alpha = alpha),
list("/baseline/mean" = seqtol(xbar, "Reference mean vector."),
"/baseline/covariance/0" = seqtol(S[1, ], "First row of S, denominator m - 1."),
"/baseline/upper_control_limit" = tol(ucl_ind(alpha), "p(m+1)(m-1)/(m(m-p)) F_(1-alpha)(p, m-p)."),
"/monitoring_points/0/t_squared" = tol(t2[1], "First monitoring T^2."),
"/monitoring_points/15/t_squared" = tol(t2[16], "A shifted monitoring row."),
"/breach_count" = exact(sum(t2 > ucl_ind(alpha)), "Rows with T^2 above the limit.")),
c("The standard Phase-II individual-observation limit is used (Montgomery, 6th ed., section 11.3)."),
kind = if (alpha == 0.0027) "nominal" else "threshold")
}
mm <- read_dataset("QUAL_T2"); mm$b[45] <- NA
export_dataset(mm, "QUAL_T2_MISSING", "QUAL_T2 with b missing in row 45 (1-based)", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.",
c(a = "Float64", b = "Float64", c = "Float64", role = "String", stage = "String"))
dm <- read_dataset("QUAL_T2_MISSING")
excluded <- c(3L, 52L)
keep <- setdiff(which(stats::complete.cases(dm[vars])), excluded + 1L)
Rk <- as.matrix(dm[intersect(keep, which(dm$role == "ref")), vars]); Mk <- as.matrix(dm[intersect(keep, which(dm$role == "mon")), vars])
xk <- colMeans(Rk); Sk <- solve(cov(Rk)); mk <- nrow(Rk)
emit("STAT-QUAL-T2-003", t2_entry, "hotellingMenuItem", "Excluded and incomplete rows leave the baseline and the monitoring", "QUAL_T2_MISSING",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref", alpha = 0.0027),
list("/baseline/mean" = seqtol(xk, "Mean of the 39 remaining reference rows."),
"/baseline/upper_control_limit" = tol(p * (mk + 1) * (mk - 1) / (mk * (mk - p)) * qf(1 - 0.0027, p, mk - p), "Limit at m = 39."),
"/monitoring_points/0/t_squared" = tol(quad(Mk[1, ] - xk, Sk), "First remaining monitoring row."),
"/incomplete_count" = exact(1L, "Row 45 (1-based) has a missing value."),
"/excluded_count" = exact(2L, "Two excluded rows.")),
c("Reference row 3 and monitoring row 52 (0-based) are excluded; monitoring row 44 (0-based) is incomplete."),
kind = "missing_excluded", excluded = excluded)
small <- read_dataset("QUAL_T2"); small$role[4:40] <- "mon"
export_dataset(small, "QUAL_T2_FEWREF", "QUAL_T2 with only three reference rows", "Hand-constructed",
"Constructed validation data; no third-party license.",
c(a = "Float64", b = "Float64", c = "Float64", role = "String", stage = "String"))
emit("STAT-QUAL-T2-004", t2_entry, "hotellingMenuItem", "Three reference rows for three variables are too few", "QUAL_T2_FEWREF",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref"), list(),
c("The baseline needs more reference rows than variables (m > p)."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "reference rows"))
# ---- MEWMA -------------------------------------------------------------------------
mewma <- function(M, lambda, xbar, S, stage = NULL) {
out <- numeric(nrow(M)); z <- xbar; step <- 0L
for (i in seq_len(nrow(M))) {
if (!is.null(stage) && i > 1 && stage[i] != stage[i - 1]) { z <- xbar; step <- 0L }
step <- step + 1L
z <- lambda * M[i, ] + (1 - lambda) * z
factor <- lambda * (1 - (1 - lambda)^(2 * step)) / (2 - lambda)
out[i] <- quad(z - xbar, solve(factor * S))
}
out
}
mw <- mewma(M0, 0.2, xbar, S)
emit("STAT-QUAL-MEWMA-001", mewma_entry, "mewmaMenuItem", "MEWMA with lambda 0.2 and a trial limit of 11", "QUAL_T2",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref", decision_limit = 11),
list("/points/0/statistic" = tol(mw[1], "Exact finite-time covariance at step 1."),
"/points/9/statistic" = tol(mw[10], "Step 10."),
"/points/19/statistic" = tol(mw[20], "Step 20."),
"/signal_count" = exact(sum(mw > 11), "Points with T^2 > h."),
"/asymptotic_factor" = tol(0.2 / 1.8, "Steady-state lambda / (2 - lambda).")),
c("Z_0 = xbar; no reset after a signal (line 77-78)."))
m1 <- mewma(M0, 1, xbar, S)
emit("STAT-QUAL-MEWMA-002", mewma_entry, "mewmaMenuItem", "Lambda 1 reproduces the individual Hotelling statistic", "QUAL_T2",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref", smoothing = 1, decision_limit = 11),
list("/points/0/statistic" = tol(t2[1], "Line 63-65: with lambda = 1 the statistic equals the individual T^2."),
"/points/15/statistic" = tol(t2[16], "Same identity at a shifted row.")),
c(sprintf("R check: max |MEWMA(lambda = 1) - T^2| = %.3g.", max(abs(m1 - t2)))), kind = "boundary")
ms <- mewma(M0, 0.3, xbar, S, stage = d$stage[d$role == "mon"])
emit("STAT-QUAL-MEWMA-003", mewma_entry, "mewmaMenuItem", "A stage column restarts the recursion", "QUAL_T2",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref", smoothing = 0.3,
decision_limit = 12, stage_column = "stage"),
list("/points/10/step" = exact(1L, "The first S2 row restarts at step 1 (line 69-71)."),
"/points/10/statistic" = tol(ms[11], "Restarted from Z = xbar."),
"/segment_count" = exact(2L, "Two stages.")),
c("Monitoring stages S1 (rows 41-50) and S2 (rows 51-60), 1-based."), kind = "axis_coverage")
emit("STAT-QUAL-MEWMA-004", mewma_entry, "mewmaMenuItem", "A user-calibrated limit needs a source note", "QUAL_T2",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref", decision_limit = 11,
limit_provenance = "user_calibrated"), list(),
c("Line 25-28: a user-calibrated limit requires a source note."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "source"))
emit("STAT-QUAL-MEWMA-005", mewma_entry, "mewmaMenuItem", "A smoothing weight above 1 is refused", "QUAL_T2",
list(variables = as.list(vars), reference_column = "role", reference_value = "ref", smoothing = 1.2, decision_limit = 11), list(),
c("Line 19: 0 < lambda <= 1."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "less than or equal to 1"))
# ---- subgroups ---------------------------------------------------------------------
subgroup_data <- function(seed, p, n, m_ref, m_mon, shift) {
set.seed(seed)
rows <- list(); k <- 0L
for (g in seq_len(m_ref + m_mon)) {
block <- matrix(rnorm(n * p), n) %*% t(diag(p) + 0.4 * (row(diag(p)) > col(diag(p))))
if (g > m_ref + m_mon / 2) block[, 1] <- block[, 1] + shift
for (r in seq_len(n)) {
k <- k + 1L
rows[[k]] <- c(block[r, ] + 5, g, if (g <= m_ref) 1 else 0)
}
}
mat <- do.call(rbind, rows)
out <- data.frame(round(mat[, seq_len(p)], 5))
names(out) <- paste0("v", seq_len(p))
out$lot <- sprintf("G%02d", as.integer(mat[, p + 1])); out$role <- ifelse(mat[, p + 2] == 1, "ref", "mon")
out
}
gv_constants <- function(n, p) {
b1 <- prod(n - seq_len(p)) / (n - 1)^p
b2 <- prod(n - seq_len(p)) * (prod(n - seq_len(p) + 2) - prod(n - seq_len(p))) / (n - 1)^(2 * p)
c(b1 = b1, b2 = b2)
}
sub_chart <- function(dd, vars, n) {
groups <- split(dd, factor(dd$lot, levels = unique(dd$lot)))
refs <- Filter(function(g) g$role[1] == "ref", groups); mons <- Filter(function(g) g$role[1] == "mon", groups)
m <- length(refs); p <- length(vars)
means <- t(sapply(refs, function(g) colMeans(as.matrix(g[vars]))))
gm <- colMeans(means); Sbar <- Reduce(`+`, lapply(refs, function(g) cov(as.matrix(g[vars])))) / m
inv <- solve(Sbar)
t2 <- sapply(mons, function(g) n * quad(colMeans(as.matrix(g[vars])) - gm, inv))
df2 <- m * n - m - p + 1
ucl <- p * (m + 1) * (n - 1) / df2 * qf(1 - 0.0027, p, df2)
out <- list(gm = gm, Sbar = Sbar, t2 = t2, ucl = ucl, df2 = df2)
if (n > p) {
k <- gv_constants(n, p); ref_det <- sapply(refs, function(g) det(cov(as.matrix(g[vars]))))
sigma_det <- mean(ref_det) / k[["b1"]]
out$gv <- sapply(mons, function(g) det(cov(as.matrix(g[vars]))))
out$center <- mean(ref_det); out$gv_ucl <- sigma_det * (k[["b1"]] + 3 * sqrt(k[["b2"]]))
out$gv_lcl <- max(0, sigma_det * (k[["b1"]] - 3 * sqrt(k[["b2"]])))
}
out
}
export_dataset(subgroup_data(20261013, 2, 5, 15, 8, 1.6), "QUAL_T2_SUB", "Constructed: p = 2, n = 5, 15 reference and 8 monitoring subgroups, seed 20261013; +1.6 shift in v1 in the last four",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(v1 = "Float64", v2 = "Float64", lot = "String", role = "String"))
ds <- read_dataset("QUAL_T2_SUB")
sc <- sub_chart(ds, c("v1", "v2"), 5)
sub_config <- list(variables = list("v1", "v2"), subgroup_column = "lot", subgroup_size = 5L,
reference_column = "role", reference_value = "ref")
emit("STAT-QUAL-T2SUB-001", sub_entry, "hotellingSubgroupsMenuItem", "Subgroup T-squared and generalized variance", "QUAL_T2_SUB",
sub_config,
list("/baseline/grand_mean" = seqtol(sc$gm, "Mean of the reference subgroup means."),
"/baseline/pooled_covariance/0" = seqtol(sc$Sbar[1, ], "Mean of the reference subgroup covariances."),
"/baseline/t_squared_ucl" = tol(sc$ucl, "p(m+1)(n-1)/(mn-m-p+1) F_(1-alpha)(p, mn-m-p+1)."),
"/baseline/f_denominator_df" = exact(as.integer(sc$df2), "mn - m - p + 1."),
"/monitoring_points/0/t_squared" = tol(sc$t2[[1]], "n (xbar_j - xbarbar)' Sbar^-1 (xbar_j - xbarbar)."),
"/monitoring_points/7/t_squared" = tol(sc$t2[[8]], "A shifted subgroup."),
"/monitoring_points/0/generalized_variance" = tol(sc$gv[[1]], "|S_j|."),
"/baseline/generalized_variance_center" = tol(sc$center, "Mean reference |S_i|."),
"/baseline/generalized_variance_ucl" = tol(sc$gv_ucl, "|Sigma| (b1 + 3 sqrt(b2))."),
"/t_squared_breach_count" = exact(sum(sc$t2 > sc$ucl), "Subgroups above the limit.")),
c("alpha defaults to 0.0027 (line 24-25).", "Alt's b1 and b2 for n = 5, p = 2 (line 70-73)."))
export_dataset(subgroup_data(20261014, 3, 3, 12, 6, 0), "QUAL_T2_SUB_SMALLN", "Constructed: p = 3, n = 3, seed 20261014",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(v1 = "Float64", v2 = "Float64", v3 = "Float64", lot = "String", role = "String"))
dn <- read_dataset("QUAL_T2_SUB_SMALLN")
sn <- sub_chart(dn, c("v1", "v2", "v3"), 3)
emit("STAT-QUAL-T2SUB-002", sub_entry, "hotellingSubgroupsMenuItem", "n <= p keeps the location chart and withholds the spread chart", "QUAL_T2_SUB_SMALLN",
list(variables = list("v1", "v2", "v3"), subgroup_column = "lot", subgroup_size = 3L, reference_column = "role", reference_value = "ref"),
list("/baseline/t_squared_ucl" = tol(sn$ucl, "Location limit still computed."),
"/monitoring_points/0/t_squared" = tol(sn$t2[[1]], "Location statistic."),
"/baseline/generalized_variance_available" = exact(FALSE, "Every subgroup covariance is singular when n <= p (line 83-85).")),
c("m(n - 1) = 24 >= p = 3, so the location chart is defined."), kind = "boundary")
emit("STAT-QUAL-T2SUB-003", sub_entry, "hotellingSubgroupsMenuItem", "A subgroup size of 1 is refused", "QUAL_T2_SUB",
modifyList(sub_config, list(subgroup_size = 1L)), list(),
c("Line 22-24: a size of 1 points to the individual-observation chart."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "individual"))
emit("STAT-QUAL-T2SUB-004", sub_entry, "hotellingSubgroupsMenuItem", "Subgroups larger than n stop the run", "QUAL_T2_SUB",
modifyList(sub_config, list(subgroup_size = 4L)), list(),
c("Line 36-38: a subgroup with more than n rows stops the run."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "G01"))
generators/r/quality_normal_capability.R
source("common.R", local = TRUE)
run <- generator_run(c("stats", "jsonlite"))
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Analytic normal-capability formula:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
index <- seq_len(30L)
raw <- data.frame(value = 10 + 0.41 * sin(index * 0.61) +
0.24 * cos(index * 1.19) + 0.07 * sin(index * 2.31))
export_dataset(raw, "QUAL_CAPABILITY_NORMAL_01", "Constructed ordered continuous measurements",
"R stats::mean and stats::sd documentation; manual chapter 08 capability formulas",
"Constructed validation data; no third-party license.", c(value = "Float64"))
d <- read_dataset("QUAL_CAPABILITY_NORMAL_01")
x <- d$value
lsl <- 8.7
usl <- 11.3
d2 <- 2 / sqrt(pi)
mu <- mean(x)
overall <- stats::sd(x)
mr_bar <- mean(abs(diff(x)))
within <- mr_bar / d2
cp <- (usl - lsl) / (6 * within)
cpk <- min((mu - lsl) / (3 * within), (usl - mu) / (3 * within))
pp <- (usl - lsl) / (6 * overall)
ppk <- min((mu - lsl) / (3 * overall), (usl - mu) / (3 * overall))
id <- "STAT-QUAL-CAPABILITY-001"
write_expected("quality", id, list(
schema_version = "2.0.0", case_id = id,
outputs = list(
"/rows/0/n" = exact(length(x), "Thirty finite ordered measurements."),
"/rows/0/mean" = tol(mu, "Arithmetic mean; owner 1e-9 tier."),
"/rows/0/std" = tol(overall, "Sample SD with n-1 denominator; owner 1e-9 tier."),
"/rows/0/within_sigma" = tol(within, "Mean absolute successive difference / d2 with d2 = 2/sqrt(pi) computed at full precision; owner 1e-9 tier."),
"/rows/0/cp" = tol(cp, "(USL-LSL)/(6*s_w); owner 1e-9 tier."),
"/rows/0/cpk" = tol(cpk, "Minimum one-sided distance / (3*s_w); owner 1e-9 tier."),
"/rows/0/pp" = tol(pp, "(USL-LSL)/(6*s_o); owner 1e-9 tier."),
"/rows/0/ppk" = tol(ppk, "Minimum one-sided distance / (3*s_o); owner 1e-9 tier."),
"/rows/0/oos" = exact(sum(x < lsl | x > usl), "Only values strictly outside the specifications are counted.")
), expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("quality", id, list(
schema_version = "2.0.0", case_id = id, version = "2.0.0", family = "quality",
entry_point = "tabelz.analysis.spec_sheet:compute_capability",
menu_items = list("capabilityMenuItem"), title = "Normal capability with individual observations",
case_kind = "nominal",
dataset = list(path = "reference_data/datasets/QUAL_CAPABILITY_NORMAL_01.json", sha256 = ""),
excluded_rows = list(),
configuration = list(limits = list(value = list(lsl, usl)), method = "normal",
study = list(subgroup_mode = "individuals", within_method = "moving_range",
require_stability = FALSE, require_msa = FALSE)),
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/quality/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats and manual formulas",
citation = "R stats::mean and stats::sd documentation; Tabelz manual chapter 08, Capability continuous data",
generator = list(path = "generators/r/quality_normal_capability.R", sha256 = ""),
independent = TRUE,
convention_notes = list(
"Manual chapter 08: individuals use s_w=mean(abs(diff(x)))/d2 with d2=2/sqrt(pi) computed at full precision (about 1.128379167); the manual's 1.128 is a rounded display form. Overall s_o=sample SD.",
"LSL=8.7 and USL=11.3 are response units. Cp and Cpk use s_w; Pp and Ppk use s_o.",
"Rows are production-ordered as exported; R re-reads the CSV before every calculation."
)
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-QUAL-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
cat("normal capability reference generated\n")
generators/r/quality_spc.R
source("common.R", local = TRUE)
# References for tabelz.analysis.spc:compute_spc: NIST/SEMATECH
# e-Handbook, 6.3.2.1 (Shewhart variables chart factors), and Nelson (1984)
# for supplementary run rules. The manual documents Tabelz's chosen pairing
# and varying-size conventions, but is not an independent numerical source.
# Constants (lines 66-71, 99-104, 119-121): d2(n) is the mean and d3(n) the
# standard deviation of the range of n standard normal observations, both from
# their defining integrals; c4(n) = sqrt(2/(n-1)) Gamma(n/2) / Gamma((n-1)/2).
# A2 = 3/(d2 sqrt(n)), D3 = max(0, 1 - 3 d3/d2), D4 = 1 + 3 d3/d2,
# A3 = 3/(c4 sqrt(n)), B3 = max(0, 1 - 3 sqrt(1-c4^2)/c4), B4 = 1 + 3 sqrt(1-c4^2)/c4.
# Varying subgroup sizes (lines 93-97, 119-120): sigma is the mean of R_i/d2(n_i)
# (or s_i/c4(n_i)), with point-specific limits. Rules (lines 213-245) are
# implemented here independently; a window rule signals at the point that
# completes the window, and zones use the plotted statistic's own sigma.
run <- generator_run(c("stats", "jsonlite"))
family <- "quality"
entry <- "tabelz.analysis.spc:compute_spc"
generator <- "generators/r/quality_spc.R"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form chart statistic; owner 1e-9 tier.", why))
tol_abs <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "1e-12",
relative_tolerance = "1e-9", rationale = paste("Closed-form value that can be zero; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(), error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list("spcMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats::integrate constants and hand-coded chart formulas and rules",
citation = paste("NIST/SEMATECH e-Handbook 6.3.2.1, https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc321.htm;",
"Nelson, Journal of Quality Technology 16(4), 1984, pp. 237-239; R stats::integrate and base::lgamma"),
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "NIST tabulates rounded factors; the generator integrates d2/d3 at full precision.",
"Phase-I selection, varying subgroup sizes and row adjacency are Tabelz conventions independently implemented here.",
"All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-QUAL-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
# ---- constants ----------------------------------------------------------------
d2 <- function(n) {
if (n == 2) return(2 / sqrt(pi))
integrate(function(x) 1 - pnorm(x)^n - pnorm(-x)^n, -Inf, Inf, rel.tol = 1e-13)$value
}
range_cdf <- function(r, n) {
n * integrate(function(x) dnorm(x) * (pnorm(x + r) - pnorm(x))^(n - 1), -Inf, Inf, rel.tol = 1e-13)$value
}
d3 <- function(n) {
if (n == 2) return(sqrt(2 - 4 / pi))
second <- 2 * integrate(Vectorize(function(r) r * (1 - range_cdf(r, n))), 0, Inf, rel.tol = 1e-12)$value
sqrt(second - d2(n)^2)
}
c4 <- function(n) exp(0.5 * log(2 / (n - 1)) + lgamma(n / 2) - lgamma((n - 1) / 2))
# ---- rules --------------------------------------------------------------------
rule_keys <- function(values, center, zone_sigma, ucl, lcl, rules) {
n <- length(values); z <- (values - center) / zone_sigma; keys <- character()
add <- function(i, rule) keys <<- c(keys, sprintf("primary:%s:%d", rule, i - 1L))
for (i in seq_len(n)) {
if (values[i] > ucl[i] || values[i] < lcl[i]) add(i, "rule_1")
if ("rule_2" %in% rules && i >= 9) { w <- z[(i - 8):i]; if (all(w > 0) || all(w < 0)) add(i, "rule_2") }
if ("rule_3" %in% rules && i >= 6) { dd <- diff(values[(i - 5):i]); if (all(dd > 0) || all(dd < 0)) add(i, "rule_3") }
if ("rule_4" %in% rules && i >= 14) {
dd <- sign(diff(values[(i - 13):i])); if (all(dd != 0) && all(dd[-1] == -dd[-length(dd)])) add(i, "rule_4")
}
if ("rule_5" %in% rules && i >= 3) { w <- z[(i - 2):i]; if (sum(w > 2) >= 2 || sum(w < -2) >= 2) add(i, "rule_5") }
if ("rule_6" %in% rules && i >= 5) { w <- z[(i - 4):i]; if (sum(w > 1) >= 4 || sum(w < -1) >= 4) add(i, "rule_6") }
if ("rule_7" %in% rules && i >= 15) { w <- z[(i - 14):i]; if (all(abs(w) < 1)) add(i, "rule_7") }
if ("rule_8" %in% rules && i >= 8) { w <- z[(i - 7):i]; if (all(abs(w) > 1) && any(w > 0) && any(w < 0)) add(i, "rule_8") }
}
keys
}
secondary_keys <- function(values, ucl, lcl) {
i <- which(values > ucl | values < lcl)
sprintf("secondary:rule_1:%d", i - 1L)
}
set_of <- function(keys, why) list(comparison = "set", values = as.list(sort(keys)), rationale = why)
# ---- individuals and moving range ----------------------------------------------
imr <- function(x, rules, baseline = rep(TRUE, length(x))) {
b <- x[baseline]; mr_b <- abs(diff(b))
center <- mean(b); mrbar <- mean(mr_b); sigma <- mrbar / d2(2)
ucl <- center + 3 * sigma; lcl <- center - 3 * sigma
mr <- abs(diff(x)); mr_ucl <- (1 + 3 * d3(2) / d2(2)) * mrbar
list(center = center, ucl = ucl, lcl = lcl, sigma = sigma, mrbar = mrbar, mr_ucl = mr_ucl, mr = mr,
keys = c(rule_keys(x, rep(center, length(x)), rep(sigma, length(x)), rep(ucl, length(x)), rep(lcl, length(x)), rules),
secondary_keys(mr, rep(mr_ucl, length(mr)), rep(0, length(mr)))))
}
imr_outputs <- function(r, x, why_keys) list(
"/primary/center_line" = tol(r$center, "Mean of the baseline observations."),
"/primary/upper_control_limit" = tol(r$ucl, "Mean + (3/d2) MR-bar, d2 = 2/sqrt(pi)."),
"/primary/lower_control_limit" = tol(r$lcl, "Mean - (3/d2) MR-bar."),
"/within_sigma" = tol(r$sigma, "MR-bar / d2."),
"/secondary/center_line" = tol(r$mrbar, "Average moving range."),
"/secondary/upper_control_limit" = tol(r$mr_ucl, "(1 + 3 d3/d2) MR-bar with d3 = sqrt(2 - 4/pi)."),
"/secondary/lower_control_limit" = tol_abs(0, "The moving-range chart has lower limit 0."),
"/primary/points/0/value" = tol(x[1], "The first plotted observation."),
"/secondary/points/0/value" = tol(r$mr[1], "The first moving range |x2 - x1|."),
"/violation_keys" = set_of(r$keys, why_keys))
set.seed(20261001)
x <- round(rnorm(70, 50, 2), 4)
x[20] <- 62 # beyond the limits
x[30:38] <- round(51 + abs(rnorm(9, 1.2, 0.3)), 4) # nine above the centre
x[45:50] <- round(seq(46, 52, length.out = 6), 4) # six rising
x[55:57] <- c(55.5, 49.8, 55.9) # two of three beyond 2 sigma
export_dataset(data.frame(t = seq_along(x), x = x, phase = ifelse(seq_along(x) <= 40, 1L, 2L)),
"QUAL_SPC_IMR", "Constructed individuals series, seed 20261001, with an outlier, a shift, a trend and a two-of-three pattern",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(t = "Int64", x = "Float64", phase = "Int64"))
d <- read_dataset("QUAL_SPC_IMR")
default_rules <- c("rule_2", "rule_3", "rule_4")
all_rules <- c(default_rules, "rule_5", "rule_6", "rule_7", "rule_8")
r <- imr(d$x, default_rules)
emit("STAT-QUAL-SPC-001", "Individuals and moving range with the default rules", "QUAL_SPC_IMR",
list(y_column = "x"), imr_outputs(r, d$x, "Rules 1-4 on the I chart and rule 1 on the MR chart."),
c("Rules 2-4 are on by default (line 216-217)."))
r <- imr(d$x, all_rules)
emit("STAT-QUAL-SPC-002", "Individuals chart with all eight rules", "QUAL_SPC_IMR",
list(y_column = "x", run_rules = as.list(all_rules)), list("/violation_keys" = set_of(r$keys, "Rules 1-8 evaluated independently in R.")),
c("Zones use the I chart's sigma = MR-bar/d2 (line 238-240)."), kind = "axis_coverage")
rb <- imr(d$x, default_rules, baseline = d$phase <= 1)
emit("STAT-QUAL-SPC-003", "Phase I baseline freezes the limits from the first 40 points", "QUAL_SPC_IMR",
list(y_column = "x", baseline = list(column = "phase", op = "le", value = 1)),
c(imr_outputs(rb, d$x, "All 70 points are evaluated against the baseline limits."),
list("/baseline_count" = exact(40L, "Forty baseline points."))),
c("Line 25-27: the baseline estimates the limits and every point is still plotted and evaluated."), kind = "axis_coverage")
# missing values and excluded rows: the retained neighbours become successive
m <- read_dataset("QUAL_SPC_IMR")
m$x[c(8, 61)] <- NA
export_dataset(m, "QUAL_SPC_IMR_MISSING", "QUAL_SPC_IMR with x missing in rows 8 and 61 (1-based)",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(t = "Int64", x = "Float64", phase = "Int64"))
mm <- read_dataset("QUAL_SPC_IMR_MISSING")
excluded <- c(2L, 40L)
xm <- mm$x[-(excluded + 1L)]; xm <- xm[!is.na(xm)]
emit("STAT-QUAL-SPC-004", "Missing and excluded rows are omitted before the moving ranges", "QUAL_SPC_IMR_MISSING",
list(y_column = "x"), imr_outputs(imr(xm, default_rules), xm, "Rules on the retained sequence."),
c("Line 75-77: missing observations are omitted and the adjacent retained values become successive.",
"Excluded rows (0-based 2 and 40) are removed from the plotted source."),
kind = "missing_excluded", excluded = excluded)
h <- read_dataset("QUAL_SPC_IMR"); h$x <- h$x + 1e7
export_dataset(h, "QUAL_SPC_IMR_OFFSET", "QUAL_SPC_IMR shifted by 1e7", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(t = "Int64", x = "Float64", phase = "Int64"))
ho <- read_dataset("QUAL_SPC_IMR_OFFSET")
ro <- imr(ho$x, default_rules)
emit("STAT-QUAL-SPC-005", "Individuals chart at a 1e7 offset", "QUAL_SPC_IMR_OFFSET", list(y_column = "x"),
imr_outputs(ro, ho$x, "The offset changes no signal."), c("A 1e7 shift moves the centre and limits only."), kind = "hard_numeric")
# ---- X-bar/R and X-bar/S with constant sizes --------------------------------------
xbar_chart <- function(groups, family_name, rules) {
n_i <- vapply(groups, length, 1L); means <- vapply(groups, mean, 0)
spread <- if (family_name == "R") vapply(groups, function(g) diff(range(g)), 0) else vapply(groups, sd, 0)
gm <- sum(means * n_i) / sum(n_i)
if (length(unique(n_i)) == 1) {
n <- n_i[1]
if (family_name == "R") {
bar <- mean(spread); sigma <- bar / d2(n); A <- 3 / (d2(n) * sqrt(n))
lo_f <- max(0, 1 - 3 * d3(n) / d2(n)); hi_f <- 1 + 3 * d3(n) / d2(n)
} else {
bar <- mean(spread); sigma <- bar / c4(n); A <- 3 / (c4(n) * sqrt(n))
k <- sqrt(1 - c4(n)^2) / c4(n); lo_f <- max(0, 1 - 3 * k); hi_f <- 1 + 3 * k
}
ucl <- rep(gm + A * bar, length(n_i)); lcl <- rep(gm - A * bar, length(n_i))
s_center <- rep(bar, length(n_i)); s_ucl <- rep(hi_f * bar, length(n_i)); s_lcl <- rep(lo_f * bar, length(n_i))
} else {
unit <- if (family_name == "R") vapply(n_i, d2, 0) else vapply(n_i, c4, 0)
sigma <- mean(spread / unit)
ucl <- gm + 3 * sigma / sqrt(n_i); lcl <- gm - 3 * sigma / sqrt(n_i)
if (family_name == "R") {
dd3 <- vapply(n_i, d3, 0)
s_center <- unit * sigma; s_ucl <- (unit + 3 * dd3) * sigma; s_lcl <- pmax(0, unit - 3 * dd3) * sigma
} else {
s_center <- unit * sigma; s_ucl <- unit * sigma + 3 * sigma * sqrt(1 - unit^2)
s_lcl <- pmax(0, unit * sigma - 3 * sigma * sqrt(1 - unit^2))
}
}
keys <- c(rule_keys(means, rep(gm, length(means)), sigma / sqrt(n_i), ucl, lcl, rules),
secondary_keys(spread, s_ucl, s_lcl))
list(means = means, spread = spread, gm = gm, sigma = sigma, ucl = ucl, lcl = lcl,
s_center = s_center, s_ucl = s_ucl, s_lcl = s_lcl, keys = keys, n = n_i)
}
xbar_outputs <- function(ch, varying = FALSE) {
out <- list(
"/primary/center_line" = tol(ch$gm, "Observation-weighted grand mean."),
"/within_sigma" = tol(ch$sigma, "Within-subgroup sigma."),
"/primary/points/0/value" = tol(ch$means[1], "First subgroup mean."),
"/secondary/points/0/value" = tol(ch$spread[1], "First subgroup range or standard deviation."),
"/violation_keys" = set_of(ch$keys, "Rules 1-4 on the mean chart and rule 1 on the dispersion chart."),
"/subgroup_count" = exact(length(ch$means), "Retained subgroups."))
if (!varying) {
out[["/primary/upper_control_limit"]] <- tol(ch$ucl[1], "Grand mean + A2 R-bar (or A3 S-bar).")
out[["/primary/lower_control_limit"]] <- tol(ch$lcl[1], "Grand mean - A2 R-bar (or A3 S-bar).")
out[["/secondary/center_line"]] <- tol(ch$s_center[1], "Average range or standard deviation.")
out[["/secondary/upper_control_limit"]] <- tol(ch$s_ucl[1], "D4 R-bar or B4 S-bar.")
out[["/secondary/lower_control_limit"]] <- tol_abs(ch$s_lcl[1], "D3 R-bar or B3 S-bar (zero when the factor is zero).")
} else {
out[["/primary/point_upper_limits"]] <- list(comparison = "sequence", values = lapply(ch$ucl, dec),
absolute_tolerance = "0", relative_tolerance = "1e-9", rationale = "Grand mean + 3 sigma / sqrt(n_i).")
out[["/primary/point_lower_limits"]] <- list(comparison = "sequence", values = lapply(ch$lcl, dec),
absolute_tolerance = "0", relative_tolerance = "1e-9", rationale = "Grand mean - 3 sigma / sqrt(n_i).")
out[["/secondary/point_upper_limits"]] <- list(comparison = "sequence", values = lapply(ch$s_ucl, dec),
absolute_tolerance = "0", relative_tolerance = "1e-9", rationale = "Dispersion upper limit for n_i.")
out[["/secondary/point_center_lines"]] <- list(comparison = "sequence", values = lapply(ch$s_center, dec),
absolute_tolerance = "0", relative_tolerance = "1e-9", rationale = "d2(n_i) sigma or c4(n_i) sigma.")
}
out
}
set.seed(20261002)
xs <- round(rnorm(52, 20, 1.5), 4)
xs[13] <- NA # makes subgroup 3 incomplete
export_dataset(data.frame(row = seq_along(xs), x = xs), "QUAL_SPC_SUB5",
"Constructed: 52 rows, seed 20261002, x missing at row 13 (1-based)", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(row = "Int64", x = "Float64"))
s5 <- read_dataset("QUAL_SPC_SUB5")$x
blocks <- split(s5[1:50], rep(1:10, each = 5))
kept <- Filter(function(g) !anyNA(g), blocks)
ch <- xbar_chart(unname(kept), "R", default_rules)
emit("STAT-QUAL-SPC-006", "X-bar and R with constant subgroups of 5", "QUAL_SPC_SUB5",
list(y_column = "x", subgroup_mode = "constant", subgroup_size = 5L),
c(xbar_outputs(ch), list("/dropped_subgroups" = exact(2L, "Subgroup 3 has a missing value and rows 51-52 are an incomplete final subgroup."))),
c("Line 44-48: subgroups are formed from consecutive row positions before missing values are assessed; incomplete ones are dropped and counted.",
sprintf("A2(5) = %.15g, D4(5) = %.15g from the integrals.", 3 / (d2(5) * sqrt(5)), 1 + 3 * d3(5) / d2(5))))
set.seed(20261003)
x12 <- round(rnorm(96, 7, 0.4), 5)
export_dataset(data.frame(x = x12, row = seq_along(x12)), "QUAL_SPC_SUB12", "Constructed: 96 rows, seed 20261003",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(x = "Float64", row = "Int64"))
s12 <- read_dataset("QUAL_SPC_SUB12")$x
ch <- xbar_chart(unname(split(s12, rep(1:8, each = 12))), "S", default_rules)
emit("STAT-QUAL-SPC-007", "X-bar and S with constant subgroups of 12", "QUAL_SPC_SUB12",
list(y_column = "x", subgroup_mode = "constant", subgroup_size = 12L), xbar_outputs(ch),
c("Sizes 11-25 use X-bar/S (line 46).", sprintf("c4(12) = %.15g.", c4(12))), kind = "axis_coverage")
x2 <- read_dataset("QUAL_SPC_SUB12")$x
ch <- xbar_chart(unname(split(x2, rep(1:48, each = 2))), "R", default_rules)
emit("STAT-QUAL-SPC-008", "X-bar and R with the smallest subgroups of 2", "QUAL_SPC_SUB12",
list(y_column = "x", subgroup_mode = "constant", subgroup_size = 2L),
c(xbar_outputs(ch)[c("/primary/center_line", "/within_sigma")],
list("/primary/upper_control_limit" = tol(ch$ucl[1], "Grand mean + A2(2) R-bar."),
"/secondary/upper_control_limit" = tol(ch$s_ucl[1], "D4(2) R-bar."))),
c("All 96 rows form 48 subgroups of 2, the smallest subgroup size (line 44)."),
kind = "boundary")
# ---- varying sizes from a grouping column ----------------------------------------
set.seed(20261004)
sizes <- c(4, 6, 5, 3, 7, 5, 4, 6, 5, 1, 5)
lot <- rep(sprintf("L%02d", seq_along(sizes)), sizes)
vals <- round(rnorm(length(lot), 100, 3), 4)
export_dataset(data.frame(lot = lot, x = vals), "QUAL_SPC_BYLOT", "Constructed: lots of sizes 4,6,5,3,7,5,4,6,5,1,5, seed 20261004",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(lot = "String", x = "Float64"))
bl <- read_dataset("QUAL_SPC_BYLOT")
groups <- split(bl$x, factor(bl$lot, levels = unique(bl$lot)))
groups <- groups[vapply(groups, length, 1L) >= 2]
ch <- xbar_chart(unname(groups), "R", default_rules)
emit("STAT-QUAL-SPC-009", "Grouping column with varying sizes (X-bar/R, point-specific limits)", "QUAL_SPC_BYLOT",
list(y_column = "x", subgroup_mode = "by_column", group_column = "lot"),
c(xbar_outputs(ch, varying = TRUE), list("/dropped_subgroups" = exact(1L, "Lot L10 has one measurement."))),
c("Line 49-52 and 93-97: first-appearance order; sigma averages R_i/d2(n_i); the median size is 5, so X-bar/R."),
kind = "axis_coverage")
# ---- the S family through a grouping column ----------------------------------------
set.seed(20261005)
sizes <- c(12, 14, 11, 13, 12, 15)
lot <- rep(sprintf("B%d", seq_along(sizes)), sizes)
export_dataset(data.frame(lot = lot, x = round(rnorm(length(lot), 3, 0.2), 5)), "QUAL_SPC_BYLOT_S",
"Constructed: batches of sizes 12,14,11,13,12,15, seed 20261005", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(lot = "String", x = "Float64"))
bs <- read_dataset("QUAL_SPC_BYLOT_S")
ch <- xbar_chart(unname(split(bs$x, factor(bs$lot, levels = unique(bs$lot)))), "S", default_rules)
emit("STAT-QUAL-SPC-010", "Grouping column with a median size above 10 uses X-bar/S", "QUAL_SPC_BYLOT_S",
list(y_column = "x", subgroup_mode = "by_column", group_column = "lot"), xbar_outputs(ch, varying = TRUE),
c("Median usable size 12.5 > 10 selects X-bar/S; sigma averages s_i/c4(n_i) (line 119-120)."), kind = "axis_coverage")
emit("STAT-QUAL-SPC-011", "Spec limits are overlays only", "QUAL_SPC_IMR",
list(y_column = "x", lower_spec = 40, upper_spec = 60),
list("/lower_spec_limit" = tol(40, "Passed through as an overlay."), "/upper_spec_limit" = tol(60, "Passed through as an overlay."),
"/violation_keys" = set_of(imr(d$x, default_rules)$keys, "Spec limits never create signals (line 30-31).")),
c("Line 30-31: applied specifications never replace limits or create signals."), kind = "axis_coverage")
emit("STAT-QUAL-SPC-012", "A subgroup size of 26 is refused", "QUAL_SPC_SUB12",
list(y_column = "x", subgroup_mode = "constant", subgroup_size = 26L), list(),
c("Line 44: sizes 2-25."), kind = "invalid_input", error = list(error_class = "ValueError", message_contains = "25"))
emit("STAT-QUAL-SPC-013", "An unknown run rule is refused", "QUAL_SPC_IMR",
list(y_column = "x", run_rules = list("rule_9")), list(),
c("Only rules 2-8 are optional (line 219-236)."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "Unknown run rules"))
generators/r/quality_spc_attributes.R
source("common.R", local = TRUE)
# References for the Attributes and Time-weighted SPC families: NIST/SEMATECH
# e-Handbook 6.3.3 and 6.3.2.3-4, Laney (2002), and Benneyan (2001).
# tabelz.analysis.spc_attributes:compute_attribute_spc
# tabelz.analysis.spc_timeweighted:compute_time_weighted
# p: p_i = d_i/n_i, pbar = sum d / sum n, limits pbar +/- 3 sqrt(pbar(1-pbar)/n_i)
# clipped to [0, 1]; np: n pbar +/- 3 sqrt(n pbar (1-pbar)), clipped at 0;
# c: cbar +/- 3 sqrt(cbar), clipped at 0; u: ubar = sum c / sum n, limits
# ubar +/- 3 sqrt(ubar/n_i), clipped at 0. Laney p'/u': standardized
# z_i = (p_i - pbar)/sigma_i, sigma_z = MRbar(z)/d2(2) with d2(2) = 2/sqrt(pi),
# limits pbar +/- 3 sigma_i sigma_z (the standard Laney construction the table
# on lines 149-151 describes). CUSUM (lines 180-191) and EWMA (lines 195-211)
# with target and sigma = MRbar/d2 from the baseline unless supplied. Rules
# 2-4 on attribute charts use the plotted statistic's own sigma (line 238-241).
run <- generator_run(c("stats", "jsonlite"))
family <- "quality"
generator <- "generators/r/quality_spc_attributes.R"
attr_entry <- "tabelz.analysis.spc_attributes:compute_attribute_spc"
tw_entry <- "tabelz.analysis.spc_timeweighted:compute_time_weighted"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "1e-12",
relative_tolerance = "1e-9", rationale = paste("Closed-form chart statistic; owner 1e-9 tier (1e-12 absolute for clipped zeros).", why))
seqtol <- function(values, why) list(
comparison = "sequence", values = lapply(unname(values), dec), absolute_tolerance = "1e-12",
relative_tolerance = "1e-9", rationale = paste("Closed-form chart statistic; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
set_of <- function(keys, why) list(comparison = "set", values = as.list(sort(keys)), rationale = why)
emit <- function(id, entry, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(),
error = NULL, deferred = list()) {
citation <- if (entry == tw_entry) {
"NIST/SEMATECH e-Handbook 6.3.2.3-4, https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc323.htm and https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc324.htm; Montgomery, Introduction to Statistical Quality Control, 6th ed., ch. 9"
} else if (config$chart %in% c("laney_p", "laney_u")) {
"Laney, Improved Control Charts for Attributes, Quality Engineering 14(4), 2002, pp. 531-537; NIST/SEMATECH e-Handbook 6.3.3"
} else if (config$chart %in% c("g", "t")) {
"Benneyan, Number-between g-type statistical quality control charts for monitoring adverse events, Health Care Management Science 4(4), 2001, pp. 305-318; NIST/SEMATECH e-Handbook 6.3.3"
} else {
"NIST/SEMATECH e-Handbook 6.3.3.1-2, https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc331.htm and https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc332.htm; R base arithmetic"
}
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = deferred, generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list("spcMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "Hand-coded chart formulas in R",
citation = citation,
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes,
if (entry == tw_entry) "CUSUM/EWMA targets and sigma are known when supplied, otherwise estimated from the selected Phase-I baseline by the stated Tabelz convention." else
if (config$chart %in% c("p", "c")) "NIST published p/c forms; varying-n, clipped-limit and exclusion choices remain explicit in this case." else
if (config$chart %in% c("np", "u")) "np/u formulas are independently derived from binomial/Poisson models with the configured denominator convention." else
if (config$chart %in% c("laney_p", "laney_u")) "Laney standardized moving ranges adjust the corresponding p/u limits; sparse or degenerate baselines need separate failure cases." else
"Rare-event geometric support or t-chart fitting conventions must be aligned before comparing published examples.",
"All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-QUAL-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
d2_2 <- 2 / sqrt(pi)
window_keys <- function(values, center, zone, ucl, lcl, rules = c("rule_2", "rule_3", "rule_4")) {
n <- length(values); z <- ifelse(zone > 0, (values - center) / zone, 0); keys <- character()
add <- function(i, rule) keys <<- c(keys, sprintf("primary:%s:%d", rule, i - 1L))
for (i in seq_len(n)) {
if (values[i] > ucl[i] || values[i] < lcl[i]) add(i, "rule_1")
if ("rule_2" %in% rules && i >= 9) { w <- z[(i - 8):i]; if (all(w > 0) || all(w < 0)) add(i, "rule_2") }
if ("rule_3" %in% rules && i >= 6) { dd <- diff(values[(i - 5):i]); if (all(dd > 0) || all(dd < 0)) add(i, "rule_3") }
if ("rule_4" %in% rules && i >= 14) {
dd <- sign(diff(values[(i - 13):i])); if (all(dd != 0) && all(dd[-1] == -dd[-length(dd)])) add(i, "rule_4")
}
}
keys
}
chart_outputs <- function(values, center, ucl, lcl, zone, why) list(
"/chart/center_line" = tol(center[1], "Centre line."),
"/chart/point_upper_limits" = seqtol(ucl, paste("Point-specific upper limits.", why)),
"/chart/point_lower_limits" = seqtol(lcl, "Point-specific lower limits, clipped."),
"/chart/points/0/value" = tol(values[1], "First plotted statistic."),
"/chart/points/5/value" = tol(values[6], "Sixth plotted statistic."),
"/violation_keys" = set_of(window_keys(values, center, zone, ucl, lcl), "Rules 1-4 evaluated independently in R."))
# ---- attribute data ----------------------------------------------------------------
set.seed(20261006)
k <- 30L
n_i <- sample(80:120, k, replace = TRUE)
d_i <- rbinom(k, n_i, 0.06); d_i[17] <- 22
area <- round(runif(k, 0.8, 2.5), 3)
c_i <- rpois(k, 4 * area); c_i[9] <- 25
export_dataset(data.frame(lot = seq_len(k), defectives = as.integer(d_i), inspected = as.integer(n_i),
defects = as.integer(c_i), area = area, phase = ifelse(seq_len(k) <= 20, 1L, 2L)),
"QUAL_ATTR", "Constructed attribute data, seed 20261006 (sample, rbinom, runif, rpois)",
"R stats::rbinom and stats::rpois documentation", "Constructed validation data; no third-party license.",
c(lot = "Int64", defectives = "Int64", inspected = "Int64", defects = "Int64", area = "Float64", phase = "Int64"))
a <- read_dataset("QUAL_ATTR")
p_chart <- function(d, n, laney = FALSE, base = rep(TRUE, length(d))) {
pbar <- sum(d[base]) / sum(n[base]); p <- d / n; s <- sqrt(pbar * (1 - pbar) / n); factor <- 1
if (laney) { z <- (p[base] - pbar) / s[base]; factor <- mean(abs(diff(z))) / d2_2 }
list(values = p, center = rep(pbar, length(p)), ucl = pmin(1, pbar + 3 * s * factor),
lcl = pmax(0, pbar - 3 * s * factor), zone = s * factor, factor = factor)
}
u_chart <- function(c, n, laney = FALSE) {
ubar <- sum(c) / sum(n); u <- c / n; s <- sqrt(ubar / n); factor <- 1
if (laney) { z <- (u - ubar) / s; factor <- mean(abs(diff(z))) / d2_2 }
list(values = u, center = rep(ubar, length(u)), ucl = ubar + 3 * s * factor,
lcl = pmax(0, ubar - 3 * s * factor), zone = s * factor, factor = factor)
}
emit_chart <- function(id, title, config, ch, notes, kind = "nominal", extra = list(), deferred = list()) {
emit(id, attr_entry, title, "QUAL_ATTR", config,
c(chart_outputs(ch$values, ch$center, ch$ucl, ch$lcl, ch$zone, ""), extra), notes, kind = kind,
deferred = deferred)
}
laney_deferred <- list(list(path = "/dispersion_ratio",
reason = "The manual does not define the reported dispersion ratio; sigma_z is checked through the adjusted limits."))
emit_chart("STAT-QUAL-ATTR-001", "p chart with varying inspected counts",
list(count_column = "defectives", chart = "p", sample_size_column = "inspected"),
p_chart(a$defectives, a$inspected), c("Line 139-141; stepped limits for varying n_i (line 160-161)."))
ch <- p_chart(a$defectives, a$inspected, laney = TRUE)
emit_chart("STAT-QUAL-ATTR-002", "Laney p' chart", list(count_column = "defectives", chart = "laney_p", sample_size_column = "inspected"),
ch, c("sigma_z = mean |z_i - z_(i-1)| / d2(2) over standardized p values."), kind = "axis_coverage",
deferred = laney_deferred)
cbar <- mean(a$defects)
emit("STAT-QUAL-ATTR-003", attr_entry, "c chart", "QUAL_ATTR", list(count_column = "defects", chart = "c"),
list("/chart/center_line" = tol(cbar, "cbar."),
"/chart/upper_control_limit" = tol(cbar + 3 * sqrt(cbar), "cbar + 3 sqrt(cbar)."),
"/chart/lower_control_limit" = tol(max(0, cbar - 3 * sqrt(cbar)), "Clipped at 0."),
"/chart/points/8/value" = tol(a$defects[9], "The ninth count."),
"/violation_keys" = set_of(window_keys(a$defects, rep(cbar, k), rep(sqrt(cbar), k), rep(cbar + 3 * sqrt(cbar), k),
rep(max(0, cbar - 3 * sqrt(cbar)), k)), "Rules 1-4 in R.")),
c("Line 144-145: cbar +/- 3 sqrt(cbar), clipped at 0."), kind = "axis_coverage")
emit_chart("STAT-QUAL-ATTR-004", "u chart with an area column", list(count_column = "defects", chart = "u", sample_size_column = "area"),
u_chart(a$defects, a$area), c("Line 146-148."), kind = "axis_coverage")
ch <- u_chart(a$defects, a$area, laney = TRUE)
emit_chart("STAT-QUAL-ATTR-005", "Laney u' chart", list(count_column = "defects", chart = "laney_u", sample_size_column = "area"),
ch, c("sigma_z from standardized u values."), kind = "axis_coverage",
deferred = laney_deferred)
# np needs a constant size: 100 inspected per lot
set.seed(20261007)
dn <- rbinom(k, 100, 0.08)
export_dataset(data.frame(lot = seq_len(k), defectives = as.integer(dn)), "QUAL_ATTR_NP",
"Constructed: rbinom(30, 100, 0.08), seed 20261007", "R stats::rbinom documentation",
"Constructed validation data; no third-party license.", c(lot = "Int64", defectives = "Int64"))
an <- read_dataset("QUAL_ATTR_NP")
pbar <- sum(an$defectives) / (100 * k); s <- sqrt(100 * pbar * (1 - pbar))
emit("STAT-QUAL-ATTR-006", attr_entry, "np chart with a constant size of 100", "QUAL_ATTR_NP",
list(count_column = "defectives", chart = "np", sample_size = 100),
list("/chart/center_line" = tol(100 * pbar, "n pbar."),
"/chart/upper_control_limit" = tol(100 * pbar + 3 * s, "n pbar + 3 sqrt(n pbar (1 - pbar))."),
"/chart/lower_control_limit" = tol(max(0, 100 * pbar - 3 * s), "Clipped at 0."),
"/violation_keys" = set_of(window_keys(an$defectives, rep(100 * pbar, k), rep(s, k), rep(100 * pbar + 3 * s, k),
rep(max(0, 100 * pbar - 3 * s), k)), "Rules 1-4 in R.")),
c("Line 142-143."), kind = "axis_coverage")
bp <- p_chart(a$defectives, a$inspected, base = a$phase <= 1)
emit("STAT-QUAL-ATTR-007", attr_entry, "p chart with a Phase I baseline of 20 lots", "QUAL_ATTR",
list(count_column = "defectives", chart = "p", sample_size_column = "inspected",
baseline = list(column = "phase", op = "le", value = 1)),
c(chart_outputs(bp$values, bp$center, bp$ucl, bp$lcl, bp$zone, "From the baseline pbar."),
list("/baseline_count" = exact(20L, "Twenty baseline lots."))),
c("pbar from the first 20 lots; all 30 are evaluated (line 167-168)."), kind = "axis_coverage")
m <- read_dataset("QUAL_ATTR"); m$defectives[c(5, 23)] <- NA
export_dataset(m, "QUAL_ATTR_MISSING", "QUAL_ATTR with defectives missing in rows 5 and 23 (1-based)",
"R stats::rbinom documentation", "Constructed validation data; no third-party license.",
c(lot = "Int64", defectives = "Int64", inspected = "Int64", defects = "Int64", area = "Float64", phase = "Int64"))
mm <- read_dataset("QUAL_ATTR_MISSING")
excluded <- c(0L, 11L)
keep <- setdiff(which(!is.na(mm$defectives)), excluded + 1L)
mp <- p_chart(mm$defectives[keep], mm$inspected[keep])
emit("STAT-QUAL-ATTR-008", attr_entry, "Missing counts are omitted and excluded lots removed", "QUAL_ATTR_MISSING",
list(count_column = "defectives", chart = "p", sample_size_column = "inspected"),
c(chart_outputs(mp$values, mp$center, mp$ucl, mp$lcl, mp$zone, ""),
list("/subgroup_count" = exact(length(keep), "Retained lots."))),
c("Line 164-165: rows with null counts are omitted and counted; excluded lots 0 and 11 (0-based) are removed first."),
kind = "missing_excluded", excluded = excluded)
emit("STAT-QUAL-ATTR-009", attr_entry, "g chart plots the counts between events", "QUAL_ATTR",
list(count_column = "defects", chart = "g"),
list("/chart/points/0/value" = tol(a$defects[1], "The plotted statistic is the count itself."),
"/chart/points/8/value" = tol(a$defects[9], "The plotted statistic is the count itself.")),
c("Line 152-153 say only that g limits are probability based under a geometric model."), kind = "axis_coverage",
deferred = list(list(path = "/chart/center_line", reason = "The manual does not define the g chart's centre or probability limits."),
list(path = "/chart/upper_control_limit", reason = "The manual does not define the g chart's probability limits.")))
big <- read_dataset("QUAL_ATTR"); big$defectives[3] <- big$inspected[3] + 1L
export_dataset(big, "QUAL_ATTR_OVER", "QUAL_ATTR with defectives exceeding the inspected count in row 3 (1-based)",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(lot = "Int64", defectives = "Int64", inspected = "Int64", defects = "Int64", area = "Float64", phase = "Int64"))
emit("STAT-QUAL-ATTR-010", attr_entry, "Defectives above the sample size are refused", "QUAL_ATTR_OVER",
list(count_column = "defectives", chart = "p", sample_size_column = "inspected"), list(),
c("Line 166: binomial counts cannot exceed sample size."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "exceed"))
emit("STAT-QUAL-ATTR-011", attr_entry, "np with a varying size column is refused", "QUAL_ATTR",
list(count_column = "defectives", chart = "np", sample_size_column = "inspected"), list(),
c("Line 142 and 167: np requires a constant size."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "constant"))
# ---- CUSUM and EWMA ----------------------------------------------------------------
set.seed(20261008)
y <- round(c(rnorm(25, 10, 1), rnorm(25, 10.8, 1)), 4)
export_dataset(data.frame(t = seq_along(y), y = y), "QUAL_TW", "Constructed: 25 points N(10,1) then 25 points N(10.8,1), seed 20261008",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.", c(t = "Int64", y = "Float64"))
tw <- read_dataset("QUAL_TW")$y
cusum <- function(x, target, sigma, k, h) {
K <- k * sigma; H <- h * sigma; up <- numeric(length(x)); lo <- numeric(length(x)); a <- 0; b <- 0
for (i in seq_along(x)) { a <- max(0, a + (x[i] - target) - K); b <- max(0, b + (target - x[i]) - K); up[i] <- a; lo[i] <- b }
keys <- c(sprintf("primary:rule_1:%d", which(up > H) - 1L), sprintf("secondary:rule_1:%d", which(lo > H) - 1L))
list(up = up, lo = lo, H = H, keys = keys)
}
ewma <- function(x, target, sigma, lambda, L) {
z <- numeric(length(x)); prev <- target
for (i in seq_along(x)) { prev <- lambda * x[i] + (1 - lambda) * prev; z[i] <- prev }
half <- L * sigma * sqrt(lambda / (2 - lambda) * (1 - (1 - lambda)^(2 * seq_along(x))))
list(z = z, ucl = target + half, lcl = target - half,
keys = sprintf("primary:rule_1:%d", which(z > target + half | z < target - half) - 1L))
}
t0 <- mean(tw); s0 <- mean(abs(diff(tw))) / d2_2
cu <- cusum(tw, t0, s0, 0.5, 5)
emit("STAT-QUAL-TW-001", tw_entry, "CUSUM with target and sigma from the data", "QUAL_TW", list(y_column = "y"),
list("/target" = tol(t0, "Baseline mean."), "/sigma" = tol(s0, "MR-bar / d2(2)."),
"/primary/points/49/value" = tol(cu$up[50], "Final upper accumulator C+."),
"/secondary/points/49/value" = tol(cu$lo[50], "Final lower accumulator C-."),
"/primary/upper_control_limit" = tol(cu$H, "Decision interval H = h sigma."),
"/violation_keys" = set_of(cu$keys, "Points where an accumulator exceeds H.")),
c("Defaults k = 0.5 and h = 5 (line 180-182); accumulators start at zero."))
cu2 <- cusum(tw, 10, 1.1, 0.25, 4)
emit("STAT-QUAL-TW-002", tw_entry, "CUSUM with a supplied target and sigma", "QUAL_TW",
list(y_column = "y", target = 10, sigma = 1.1, cusum_k = 0.25, cusum_h = 4),
list("/primary/points/30/value" = tol(cu2$up[31], "C+ at point 31."),
"/violation_keys" = set_of(cu2$keys, "Points where an accumulator exceeds H.")),
c("Supplied target 10 and sigma 1.1 override the baseline estimates (line 173-176)."), kind = "axis_coverage")
ew <- ewma(tw, t0, s0, 0.2, 3)
emit("STAT-QUAL-TW-003", tw_entry, "EWMA with finite-time limits", "QUAL_TW", list(y_column = "y", method = "ewma"),
list("/primary/points/0/value" = tol(ew$z[1], "z_1 = lambda x_1 + (1 - lambda) T."),
"/primary/points/49/value" = tol(ew$z[50], "z_50."),
"/primary/point_upper_limits" = seqtol(ew$ucl, "T + L sigma sqrt(lambda/(2-lambda) (1 - (1-lambda)^(2i)))."),
"/primary/point_lower_limits" = seqtol(ew$lcl, "Mirror of the upper limits."),
"/violation_keys" = set_of(ew$keys, "Points beyond their limits.")),
c("Defaults lambda = 0.2 and L = 3 (line 195-196)."), kind = "axis_coverage")
ew2 <- ewma(tw, 10, s0, 0.1, 2.7)
emit("STAT-QUAL-TW-004", tw_entry, "EWMA with lambda 0.1, L 2.7 and a supplied target", "QUAL_TW",
list(y_column = "y", method = "ewma", target = 10, ewma_lambda = 0.1, ewma_l = 2.7),
list("/primary/points/49/value" = tol(ew2$z[50], "z_50."),
"/primary/point_upper_limits" = seqtol(ew2$ucl, "Finite-time limits."),
"/violation_keys" = set_of(ew2$keys, "Points beyond their limits.")),
c("Sigma still comes from the data (MR-bar / d2)."), kind = "axis_coverage")
emit("STAT-QUAL-TW-005", tw_entry, "A non-positive sigma is refused", "QUAL_TW",
list(y_column = "y", sigma = 0), list(), c("Line 176: a supplied sigma must be positive."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "igma must be positive"))
emit("STAT-QUAL-TW-006", tw_entry, "An EWMA lambda above 1 is refused", "QUAL_TW",
list(y_column = "y", method = "ewma", ewma_lambda = 1.5), list(), c("Line 195: 0 < lambda <= 1."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "ambda"))
generators/r/quality_spc_stages.R
source("common.R", local = TRUE)
# References for process stages and approved limits: NIST/SEMATECH
# e-Handbook 6.3.2.1 for I-MR factors and Nelson (1984) for rules. Stage
# boundaries and approved-limit application are Tabelz product conventions.
# tabelz.analysis.spc_studies:compute_process_stage_study
# tabelz.analysis.spc_studies:apply_approved_limit_study
# Each stage gets an independent centre and variation estimate and every rule
# window restarts at the stage boundary (lines 267-271), so the reference is one
# independent I-MR chart per stage. Approved fixed limits replace the chart's
# centre and limits; rules 2-4 then use the approved centre (lines 261-265).
run <- generator_run(c("stats", "jsonlite"))
family <- "quality"
generator <- "generators/r/quality_spc_stages.R"
stage_entry <- "tabelz.analysis.spc_studies:compute_process_stage_study"
approved_entry <- "tabelz.analysis.spc_studies:apply_approved_limit_study"
seqtol <- function(values, why) list(
comparison = "sequence", values = lapply(unname(values), dec), absolute_tolerance = "1e-12",
relative_tolerance = "1e-9", rationale = paste("Closed-form chart statistic; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
set_of <- function(keys, why) list(comparison = "set", values = as.list(sort(keys)), rationale = why)
emit <- function(id, entry, title, config, outputs, notes, kind = "nominal", error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list("spcMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/QUAL_SPC_STAGES.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "Hand-coded I-MR charts and rules in R",
citation = paste("NIST/SEMATECH e-Handbook 6.3.2.1, https://www.itl.nist.gov/div898/handbook/pmc/section3/pmc321.htm;",
"Nelson, Journal of Quality Technology 16(4), 1984, pp. 237-239; R base arithmetic"),
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes,
if (entry == stage_entry) "Independent Phase-I estimates and restarted rule windows at stage boundaries are explicit Tabelz conventions." else
"Approved fixed limits are user-supplied; NIST/Nelson support chart factors and rule definitions, not those values.",
"All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-QUAL-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
d2 <- 2 / sqrt(pi); d3 <- sqrt(2 - 4 / pi)
rules_for <- function(values, center, sigma, ucl, lcl, offset, role = "primary", window = TRUE) {
keys <- character(); z <- (values - center) / sigma
add <- function(i, rule) keys <<- c(keys, sprintf("%s:%s:%d", role, rule, offset + i - 1L))
for (i in seq_along(values)) {
if (values[i] > ucl[i] || values[i] < lcl[i]) add(i, "rule_1")
if (!window) next
if (i >= 9) { w <- z[(i - 8):i]; if (all(w > 0) || all(w < 0)) add(i, "rule_2") }
if (i >= 6) { dd <- diff(values[(i - 5):i]); if (all(dd > 0) || all(dd < 0)) add(i, "rule_3") }
if (i >= 14) { dd <- sign(diff(values[(i - 13):i])); if (all(dd != 0) && all(dd[-1] == -dd[-length(dd)])) add(i, "rule_4") }
}
keys
}
set.seed(20261009)
x <- round(c(rnorm(30, 20, 1), rnorm(30, 23, 0.6)), 4)
x[12] <- 25.5
x[47] <- 19.9
labels <- sprintf("P%02d", seq_along(x))
export_dataset(data.frame(label = labels, x = x), "QUAL_SPC_STAGES",
"Constructed: 30 points N(20,1) then 30 points N(23,0.6), seed 20261009, with one outlier in each stage",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(label = "String", x = "Float64"))
d <- read_dataset("QUAL_SPC_STAGES")
stage <- function(v) {
center <- mean(v); mr <- abs(diff(v)); mrbar <- mean(mr); sigma <- mrbar / d2
list(values = v, mr = mr, center = rep(center, length(v)), ucl = rep(center + 3 * sigma, length(v)),
lcl = rep(center - 3 * sigma, length(v)), sigma = sigma, mr_ucl = rep((1 + 3 * d3 / d2) * mrbar, length(mr)))
}
a <- stage(d$x[1:30]); b <- stage(d$x[31:60])
keys <- c(rules_for(a$values, a$center, a$sigma, a$ucl, a$lcl, 0L),
rules_for(b$values, b$center, b$sigma, b$ucl, b$lcl, 30L),
rules_for(a$mr, rep(0, 29), 1, a$mr_ucl, rep(0, 29), 0L, "secondary", FALSE),
rules_for(b$mr, rep(0, 29), 1, b$mr_ucl, rep(0, 29), 29L, "secondary", FALSE))
emit("STAT-QUAL-STAGES-001", stage_entry, "Two process stages recalculated independently",
list(y_column = "x", x_column = "label",
stages = list(list(name = "Before", start_label = "P01", end_label = "P30"),
list(name = "After", start_label = "P31"))),
list("/primary/point_center_lines" = seqtol(c(a$center, b$center), "Each stage's own mean."),
"/primary/point_upper_limits" = seqtol(c(a$ucl, b$ucl), "Each stage's mean + 3 MR-bar/d2."),
"/primary/point_lower_limits" = seqtol(c(a$lcl, b$lcl), "Each stage's mean - 3 MR-bar/d2."),
"/violation_keys" = set_of(keys, "Rules restart at the stage boundary; moving ranges stay within a stage."),
"/stage_count" = exact(2L, "Two calculated stages.")),
c("Stage 'After' starts at label P31 with no end label, so it runs to the last point.",
"Moving ranges are formed within each stage: 29 + 29 points on the MR chart."))
whole <- stage(d$x)
approved <- list(list(chart = "individuals", center = 21.5, lower = 18.4, upper = 24.6),
list(chart = "moving_range", center = 1.0, lower = 0, upper = 3.3))
keys <- c(rules_for(d$x, rep(21.5, 60), 1, rep(24.6, 60), rep(18.4, 60), 0L),
rules_for(whole$mr, rep(1, 59), 1, rep(3.3, 59), rep(0, 59), 0L, "secondary", FALSE))
emit("STAT-QUAL-APPROVED-001", approved_entry, "Fixed approved limits replace the calculated ones",
list(y_column = "x", x_column = "label", approved = approved),
list("/primary/upper_control_limit" = list(comparison = "tolerance", value = dec(24.6), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = "The approved upper limit applies to every point."),
"/primary/lower_control_limit" = list(comparison = "tolerance", value = dec(18.4), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = "The approved lower limit applies to every point."),
"/primary/center_line" = list(comparison = "tolerance", value = dec(21.5), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = "The approved centre."),
"/violation_keys" = set_of(keys, "Rule 1 against the approved limits, rules 2-4 against the approved centre; rule 1 on the MR chart.")),
c("Approved values (individuals 18.4/21.5/24.6, moving range 0/1.0/3.3) are hand-chosen round numbers.",
"Rules 2-4 need only the approved centre, so the fixed basis needs no process sigma."),
kind = "axis_coverage")
emit("STAT-QUAL-APPROVED-002", approved_entry, "Approved limits for the wrong chart are refused",
list(y_column = "x", x_column = "label", approved = list(list(chart = "xbar", center = 21, lower = 18, upper = 24))),
list(), c("A variables result needs approvals for the charts it plots."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "No approved limits are available"))
generators/r/quality_spec_import.R
source("common.R", local = TRUE)
# Analytic references for the Apply spec import (manual chapter 08 lines 10-58):
# tabelz.analysis.spec_import:prepare_spec_sheet
# tabelz.analysis.spec_import:match_spec_columns
# Transpose treats the first source column as the new headings and rotates the
# rest (line 20-22); ignore-whitespace removes surrounding and internal
# whitespace and case-insensitive ignores letter case for name matching
# (line 24-26); a blank limit is an absent side, a malformed populated limit
# leaves the whole row unresolved, and one bad row does not cancel the others
# (line 47-52). Every expected value below is read off the constructed sheet.
run <- generator_run(c("jsonlite"))
family <- "quality"
generator <- "generators/r/quality_spec_import.R"
prep_entry <- "tabelz.analysis.spec_import:prepare_spec_sheet"
match_entry <- "tabelz.analysis.spec_import:match_spec_columns"
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
num <- function(value, why) list(comparison = "tolerance", value = dec(value), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Parsed limit; owner 1e-9 tier.", why))
absent <- function(why) list(comparison = "absent", rationale = why)
set_of <- function(values, why) list(comparison = "set", values = as.list(values), rationale = why)
emit <- function(id, entry, title, dataset, config, outputs, notes, kind = "nominal", error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list("applySpecMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "analytic", source = "Hand derivation from the constructed spec sheet",
citation = "", generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(notes)),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-QUAL-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
sheet <- data.frame(
variable = c(" Diameter A", "length", "Width", "Height", "Mass", "Unknown"),
LSL = c("9.9", "", "3", "abc", "", "1"),
USL = c("10.1", "50.5", "2", "7", "", "2"),
Target = c("10", "", "", "", "", ""),
Units = c("mm", "mm", "mm", "mm", "g", "mm"),
stringsAsFactors = FALSE)
export_dataset(sheet, "QUAL_SPEC_SHEET", "Constructed specification sheet with whitespace, case, one-sided, inverted, malformed, empty and unmatched rows",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(variable = "String", LSL = "String", USL = "String", Target = "String", Units = "String"))
targets <- list("DiameterA", "Length", "Width", "Height", "Mass", "Other")
base <- list(target_columns = targets, name_column = "variable", lsl_column = "LSL", usl_column = "USL",
target_column = "Target", units_column = "Units")
emit("STAT-QUAL-SPECMATCH-001", match_entry, "Default normalization matches through whitespace and case", "QUAL_SPEC_SHEET", base,
list("/matches/0/variable" = exact(" Diameter A", "The first resolved row."),
"/matches/0/column" = exact("DiameterA", "' Diameter A' without whitespace and case equals 'DiameterA'."),
"/matches/0/lower" = num(9.9, "LSL 9.9."), "/matches/0/upper" = num(10.1, "USL 10.1."),
"/matches/0/target" = num(10, "Target 10."), "/matches/0/units" = exact("mm", "Units retained."),
"/matches/1/column" = exact("Length", "'length' matches 'Length' case-insensitively."),
"/matches/1/lower" = absent("A blank LSL is an absent side (line 47-48)."),
"/matches/1/upper" = num(50.5, "USL 50.5, a one-sided specification."),
"/matches/2" = absent("Only two rows resolve.")),
c("Width has LSL 3 > USL 2 (inverted); Height has the malformed LSL 'abc' and is unresolved even though USL 7 parses (line 48-50);",
"Mass has neither limit; Unknown names no target column. None of these cancels the two valid rows (line 51-52)."))
emit("STAT-QUAL-SPECMATCH-002", match_entry, "Case-sensitive matching leaves 'length' unmatched", "QUAL_SPEC_SHEET",
modifyList(base, list(case_insensitive = FALSE)),
list("/matches/0/column" = exact("DiameterA", "Whitespace is still removed and the letters match exactly."),
"/matches/1" = absent("'length' does not match 'Length' when case matters.")),
c("case_insensitive = false (line 26)."), kind = "axis_coverage")
emit("STAT-QUAL-SPECMATCH-003", match_entry, "Keeping whitespace leaves ' Diameter A' unmatched", "QUAL_SPEC_SHEET",
modifyList(base, list(ignore_whitespace = FALSE)),
list("/matches/0/column" = exact("Length", "Only 'length' still matches."),
"/matches/1" = absent("' Diameter A' keeps its spaces and matches nothing.")),
c("ignore_whitespace = false (line 24-25)."), kind = "axis_coverage")
emit("STAT-QUAL-SPECMATCH-004", match_entry, "Neither limit role assigned is refused", "QUAL_SPEC_SHEET",
list(target_columns = targets, name_column = "variable"), list(),
c("Line 32-33: at least one of LSL or USL must be assigned."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "lsl_column or usl_column"))
wide <- data.frame(field = c("variable", "LSL", "USL"), c1 = c("Alpha", "1", "2"), c2 = c("Beta", "", "9"),
stringsAsFactors = FALSE)
export_dataset(wide, "QUAL_SPEC_WIDE", "Constructed specification sheet laid out across columns", "Hand-constructed",
"Constructed validation data; no third-party license.", c(field = "String", c1 = "String", c2 = "String"))
emit("STAT-QUAL-SPECPREP-001", prep_entry, "Transposing a sheet laid out across columns", "QUAL_SPEC_WIDE",
list(transposed = TRUE),
list("/0/variable" = exact("Alpha", "The first characteristic becomes the first row."),
"/0/LSL" = exact("1", "Its LSL."), "/1/variable" = exact("Beta", "The second characteristic."),
"/1/USL" = exact("9", "Its USL."), "/2" = absent("Two characteristics become two rows.")),
c("The first source column (variable, LSL, USL) supplies the new headings (line 20-22)."))
emit("STAT-QUAL-SPECPREP-002", prep_entry, "Without transposing the sheet is unchanged", "QUAL_SPEC_WIDE",
list(transposed = FALSE),
list("/0/field" = exact("variable", "The original first row."), "/2/c2" = exact("9", "The original cell.")),
c("transposed = false is the default (line 20)."), kind = "axis_coverage")
emit("STAT-QUAL-SPECMATCH-005", match_entry, "Matching a transposed sheet", "QUAL_SPEC_WIDE",
list(transposed = TRUE, target_columns = list("alpha", "beta"), name_column = "variable", lsl_column = "LSL", usl_column = "USL"),
list("/matches/0/column" = exact("alpha", "'Alpha' matches 'alpha' case-insensitively."),
"/matches/0/lower" = num(1, "LSL 1."), "/matches/1/lower" = absent("Beta has a blank LSL."),
"/matches/1/upper" = num(9, "USL 9.")),
c("Prepare then match, as the desktop does."), kind = "axis_coverage")
generators/r/quality_subgroup_capability.R
source("common.R", local = TRUE)
# References for Quality > Statistical process control > Between/within I-MR-R/S chart and
# Zone chart (manual chapter docs/manual/chapters/84-subgroup-monitoring.tex and
# 103-raw-zone.tex) and Quality > Capability > Capability sixpack, Compare capability of
# populations and Capability of several characteristics (docs/manual/chapters/85-capability-views.tex
# and 08-specifications-and-capability.tex). Chart constants d2(n), d3(n) and c4(n) are
# integrated/evaluated at full precision as in quality_spc.R. Variance components of balanced
# subgroups use the one-way ANOVA closed form that the REML fit equals for a positive between
# component (chapter 84 lines 55-57); unbalanced components use nlme::lme(method = "REML").
# Zone scoring is hand-coded from the rule in chapter 84 lines 112-120.
suppressPackageStartupMessages(library(nlme))
run <- generator_run(c("stats", "jsonlite", "nlme"))
family <- "quality"
generator <- "generators/r/quality_subgroup_capability.R"
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
num <- function(value, why, tier = "1e-9") list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = tier, rationale = why)
prob <- function(value, why, tier = "1e-8") list(comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = tier, rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(id, ep, menu, title, dataset, config, outputs, notes, source, citation, requirement,
kind = "nominal", excluded = integer()) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = ep,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source, citation = citation,
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, paste("Expected computed from the re-read", dataset, "CSV.")))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list(requirement),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# ---- constants (integrated at full precision; see quality_spc.R) -----------------------------
d2 <- function(n) {
if (n == 2) return(2 / sqrt(pi))
integrate(function(x) 1 - pnorm(x)^n - pnorm(-x)^n, -Inf, Inf, rel.tol = 1e-13)$value
}
range_cdf <- function(r, n) {
n * integrate(function(x) dnorm(x) * (pnorm(x + r) - pnorm(x))^(n - 1), -Inf, Inf, rel.tol = 1e-13)$value
}
d3 <- function(n) {
if (n == 2) return(sqrt(2 - 4 / pi))
second <- 2 * integrate(Vectorize(function(r) r * (1 - range_cdf(r, n))), 0, Inf, rel.tol = 1e-12)$value
sqrt(second - d2(n)^2)
}
c4 <- function(n) unname(exp(0.5 * log(2 / (n - 1)) + lgamma(n / 2) - lgamma((n - 1) / 2)))
d2_2 <- d2(2); d3_2 <- d3(2)
# ---- zone scoring rule (chapter 84 lines 112-120) ----------------------------------------------
zone_of <- function(z) { a <- abs(z); if (a <= 1) 1L else if (a <= 2) 2L else if (a <= 3) 3L else 4L }
zone_scores <- function(z, weights = c(0, 2, 4, 8), signal = 8, reset = FALSE) {
score <- 0; side <- 0L
out <- data.frame(zone = integer(), side = integer(), weight = numeric(), score = numeric(), signal = logical())
for (value in z) {
zone <- zone_of(value)
point_side <- if (value > 0) 1L else if (value < 0) -1L else 0L
if (point_side == 0L) {
weight <- 0
} else {
weight <- weights[zone]
if (side != 0L && point_side != side) score <- weight else score <- score + weight
side <- point_side
}
flagged <- score >= signal
out[nrow(out) + 1L, ] <- list(zone, point_side, weight, score, flagged)
if (flagged && reset) score <- 0
}
out
}
zone_outputs <- function(steps, z, offset = 0L) {
out <- list()
for (i in seq_along(z)) {
p <- offset + i - 1L
out[[sprintf("/points/%d/z", p)]] <- num(z[i], "(statistic - centre) / scale.")
out[[sprintf("/points/%d/zone", p)]] <- exact(steps$zone[i], "Zone 1-4 by |z| with boundary points in the inner zone.")
out[[sprintf("/points/%d/side", p)]] <- exact(steps$side[i], "Sign of z.")
out[[sprintf("/points/%d/score", p)]] <- num(steps$score[i], "Cumulative zone score under the accumulation rule.")
out[[sprintf("/points/%d/signal", p)]] <- exact(steps$signal[i], "Score reaches the signal value.")
}
out
}
# ==============================================================================================
# Between/within I-MR-R/S chart
# ==============================================================================================
set.seed(20261010)
batch_means <- c(10.4, 11.9, 9.1, 10.8, 12.6, 9.7)
bw_value <- round(rep(batch_means, each = 5) + rnorm(30, 0, 0.5), 2)
lot_sizes <- c(4, 5, 6, 5, 4, 6)
bw <- data.frame(value = bw_value, batch = rep(1:6, each = 5), stage = rep(c("A", "B"), each = 15),
lot = rep(sprintf("L%d", 1:6), times = lot_sizes), stringsAsFactors = FALSE)
export_dataset(bw, "QUAL_BW_01",
"Simulated (R 4.6.1, set.seed 20261010): six batches of five coating measurements with batch-to-batch shifts; lot gives unequal subgroup sizes 4-6; 30 rows",
"Simulated with stats::rnorm", "Constructed validation data; no third-party license.",
c(value = "Float64", batch = "Int64", stage = "String", lot = "String"))
d <- read_dataset("QUAL_BW_01")
bw_ep <- "tabelz.analysis.spc_between_within:compute_between_within"
bw_menu <- "betweenWithinChartMenuItem"
bw_source <- "Hand-coded chart formulas with stats::integrate constants; nlme::lme REML components"
bw_cite <- "Montgomery, Introduction to Statistical Quality Control, 7th ed., ch. 6 (I-MR, R and S chart constants); Pinheiro & Bates (2000) nlme; R stats::integrate"
bw_req <- "REQ-STAT-QUAL-BETWEEN-WITHIN-001"
groups_of <- function(values, keys) split(values, factor(keys, levels = unique(keys)))
bw_stage <- function(groups, dispersion) {
means <- vapply(groups, mean, 0); sizes <- vapply(groups, length, 0L)
center <- mean(means); grand <- mean(unlist(groups))
mr <- abs(diff(means)); mr_bar <- mean(mr)
within <- if (dispersion == "range") mean(vapply(groups, function(g) (max(g) - min(g)) / d2(length(g)), 0))
else mean(vapply(groups, function(g) sd(g) / c4(length(g)), 0))
list(means = means, sizes = sizes, center = center, grand = grand, mr_bar = mr_bar,
i_scale = mr_bar / d2_2, i_upper = center + 3 * mr_bar / d2_2, i_lower = center - 3 * mr_bar / d2_2,
mr_upper = (1 + 3 * d3_2 / d2_2) * mr_bar, within = within)
}
w_limits <- function(n, sigma, dispersion) {
if (dispersion == "range") c(center = d2(n) * sigma, lower = max(0, d2(n) - 3 * d3(n)) * sigma, upper = (d2(n) + 3 * d3(n)) * sigma)
else { spread <- sqrt(1 - c4(n)^2); c(center = c4(n) * sigma, lower = max(0, c4(n) - 3 * spread) * sigma, upper = (c4(n) + 3 * spread) * sigma) }
}
anova_components <- function(groups) {
sizes <- vapply(groups, length, 0L); k <- length(groups); N <- sum(sizes)
means <- vapply(groups, mean, 0); grand <- mean(unlist(groups))
ss_b <- sum(sizes * (means - grand)^2); ss_w <- sum(vapply(groups, function(g) sum((g - mean(g))^2), 0))
ms_b <- ss_b / (k - 1); ms_w <- ss_w / (N - k); n0 <- (N - sum(sizes^2) / N) / (k - 1)
list(df_b = k - 1L, df_w = as.integer(N - k), ms_b = ms_b, ms_w = ms_w, n0 = n0,
between = max(0, (ms_b - ms_w) / n0), within = ms_w)
}
stage_outputs <- function(s, index, dispersion, first_point, components, comp_tier) {
out <- list()
pre <- sprintf("/stages/%d", index)
out[[paste0(pre, "/subgroups")]] <- exact(length(s$means), "Retained subgroups in the stage.")
out[[paste0(pre, "/observations")]] <- exact(as.integer(sum(s$sizes)), "Observations in the stage.")
out[[paste0(pre, "/grand_mean")]] <- num(s$grand, "Size-weighted mean of every observation.")
out[[paste0(pre, "/subgroup_mean_center")]] <- num(s$center, "Unweighted mean of the subgroup means (I-chart centre).")
out[[paste0(pre, "/mean_moving_range")]] <- num(s$mr_bar, "Mean |xbar_i - xbar_(i-1)| of consecutive subgroup means.")
out[[paste0(pre, "/i_chart_scale")]] <- num(s$i_scale, "MR-bar / d2(2), d2(2) = 2/sqrt(pi).")
out[[paste0(pre, "/within_sigma")]] <- num(s$within, if (dispersion == "range") "Mean of R_i / d2(n_i)." else "Mean of s_i / c4(n_i).")
out[[sprintf("/individuals/points/%d/center", first_point)]] <- num(s$center, "I chart centre.")
out[[sprintf("/individuals/points/%d/upper", first_point)]] <- num(s$i_upper, "centre + 3 MR-bar / d2(2).")
out[[sprintf("/individuals/points/%d/lower", first_point)]] <- num(s$i_lower, "centre - 3 MR-bar / d2(2).")
out[[sprintf("/individuals/points/%d/value", first_point)]] <- num(s$means[1], "First subgroup mean of the stage.")
wl <- w_limits(s$sizes[1], s$within, dispersion)
out[[sprintf("/within/points/%d/center", first_point)]] <- num(wl[["center"]], if (dispersion == "range") "d2(n) sigma_w." else "c4(n) sigma_w.")
out[[sprintf("/within/points/%d/upper", first_point)]] <- num(wl[["upper"]], if (dispersion == "range") "(d2(n) + 3 d3(n)) sigma_w." else "(c4(n) + 3 sqrt(1 - c4^2)) sigma_w.")
out[[sprintf("/within/points/%d/lower", first_point)]] <- num(wl[["lower"]], "max(0, centre factor - 3 spread factor) sigma_w.")
out[[paste0(pre, "/components/df_between")]] <- exact(components$df_b, "k - 1.")
out[[paste0(pre, "/components/df_within")]] <- exact(components$df_w, "N - k.")
out[[paste0(pre, "/components/ms_between")]] <- num(components$ms_b, "One-way ANOVA MS between (descriptive).")
out[[paste0(pre, "/components/ms_within")]] <- num(components$ms_w, "One-way ANOVA MS within (descriptive).")
out[[paste0(pre, "/components/n0")]] <- num(components$n0, "(N - sum n_i^2 / N) / (k - 1).")
out[[paste0(pre, "/components/between_sigma")]] <- num(sqrt(components$between), "sqrt of the REML between-subgroup variance.", comp_tier)
out[[paste0(pre, "/components/within_component_sigma")]] <- num(sqrt(components$within), "sqrt of the REML within variance.", comp_tier)
out[[paste0(pre, "/components/total_sigma")]] <- num(sqrt(components$between + components$within), "sqrt(between + within).", comp_tier)
out
}
bw_notes <- c(
"docs/manual/chapters/84-subgroup-monitoring.tex:19-26: I chart of subgroup means around their unweighted mean with limits c +/- 3 MR-bar/d2(2); MR upper limit D4(2) MR-bar; R or S chart uses the mean of R_i/d2(n_i) or s_i/c4(n_i) with limits that change with n_i.",
"docs/manual/chapters/84-subgroup-monitoring.tex:46-57: between, within and total SDs from the constrained random-intercept REML fit; for balanced groups with a positive between component sigma_u^2 = (MS_B - MS_W)/n and sigma_e^2 = MS_W.",
"d2(n), d3(n) by stats::integrate at full precision and c4(n) by lgamma; D4(2) = 1 + 3 d3(2)/d2(2).")
# ---- 001 nominal: R chart by batch column ---------------------------------------------------
groups <- groups_of(d$value, d$batch)
s <- bw_stage(groups, "range"); comp <- anova_components(groups)
if (comp$between <= 0) stop("Between component must be positive for the balanced closed form")
mr_values <- abs(diff(s$means))
out <- c(stage_outputs(s, 0L, "range", 0L, comp, "1e-6"),
list("/moving_range/points/0/value" = num(mr_values[1], "|xbar_2 - xbar_1|."),
"/moving_range/points/0/center" = num(s$mr_bar, "MR-bar."),
"/moving_range/points/0/upper" = num(s$mr_upper, "D4(2) MR-bar with D4(2) = 1 + 3 d3(2)/d2(2)."),
"/moving_range/points/0/subgroup" = exact(1L, "The first moving range belongs to the second subgroup."),
"/subgroups/2/mean" = num(s$means[3], "Mean of batch 3."),
"/subgroups/2/range" = num(max(groups[[3]]) - min(groups[[3]]), "Range of batch 3."),
"/subgroups/2/standard_deviation" = num(sd(groups[[3]]), "Sample SD of batch 3."),
"/subgroups/2/size" = exact(5L, "Five measurements."),
"/source_count" = exact(30L, "Rows in the table."), "/excluded_count" = exact(0L, "No exclusions."),
"/missing_count" = exact(0L, "No missing values."), "/dropped_subgroup_count" = exact(0L, "Every batch has five values."),
"/individuals/points/4/beyond_limits" = exact(s$means[5] > s$i_upper || s$means[5] < s$i_lower, "Rule 1 on the highest batch mean.")))
emit("STAT-QUAL-BETWEEN-WITHIN-001", bw_ep, bw_menu, "Between/within I-MR-R chart by subgroup column", "QUAL_BW_01",
list(response = "value", subgroup_mode = "column", subgroup_column = "batch", dispersion = "range"), out,
c(bw_notes, "Balanced design with a positive between component: the REML components equal the ANOVA closed form; tolerance 1e-6 on the component SDs because HypoNum's REML optimum is numerical (nlme::lme agrees to that order)."),
bw_source, bw_cite, bw_req)
lme_fit <- nlme::lme(value ~ 1, random = ~ 1 | batch, data = data.frame(value = d$value, batch = factor(d$batch)), method = "REML")
vc <- nlme::VarCorr(lme_fit)
if (abs(as.numeric(vc[1, 1]) - comp$between) > 1e-6 * comp$between) stop("nlme REML disagrees with the balanced closed form")
# ---- 002 S chart --------------------------------------------------------------------------------
s2 <- bw_stage(groups, "stddev")
out <- c(stage_outputs(s2, 0L, "stddev", 0L, comp, "1e-6"),
list("/within/points/2/value" = num(sd(groups[[3]]), "Sample SD of batch 3 on the S chart."),
"/moving_range/points/0/upper" = num(s2$mr_upper, "D4(2) MR-bar.")))
emit("STAT-QUAL-BETWEEN-WITHIN-002", bw_ep, bw_menu, "Between/within I-MR-S chart", "QUAL_BW_01",
list(response = "value", subgroup_mode = "column", subgroup_column = "batch", dispersion = "stddev"), out,
c(bw_notes, "S chart: within sigma is the mean of s_i/c4(n_i); limits (c4 +/- 3 sqrt(1 - c4^2)) sigma_w; the I and MR charts are unchanged."),
bw_source, bw_cite, bw_req, kind = "axis_coverage")
# ---- 003 fixed-size subgroups with two stages ----------------------------------------------------
stage_groups <- list(A = groups[1:3], B = groups[4:6])
out <- list("/stages/1/label" = exact("B", "Second stage label."), "/stages/1/first_subgroup" = exact(3L, "Stage B starts at the fourth subgroup."),
"/subgroups/3/label" = exact("4", "Fixed-size subgroups are numbered from 1."),
"/moving_range/points/2/subgroup" = exact(4L, "Stage B's first moving range belongs to its second subgroup; no range spans the stage boundary."))
for (i in 1:2) {
g <- stage_groups[[i]]; si <- bw_stage(g, "range"); ci <- anova_components(g)
out <- c(out, stage_outputs(si, i - 1L, "range", (i - 1L) * 3L, ci, "1e-6"))
out[[sprintf("/moving_range/points/%d/upper", (i - 1L) * 2L)]] <- num(si$mr_upper, "D4(2) MR-bar of the stage.")
}
emit("STAT-QUAL-BETWEEN-WITHIN-003", bw_ep, bw_menu, "Between/within chart with consecutive subgroups of five and two stages", "QUAL_BW_01",
list(response = "value", subgroup_mode = "size", subgroup_size = 5L, stage_column = "stage", dispersion = "range"), out,
c(bw_notes, "Consecutive rows of five form subgroups 1-6; stage A holds subgroups 1-3 and stage B 4-6; every stage estimates its own centre, scales and components (chapter 84 line 35: stage boundaries restart the calculations).",
"Both stages are balanced with positive between components (checked), so the ANOVA closed form is the REML solution."),
bw_source, bw_cite, bw_req, kind = "axis_coverage")
for (g in stage_groups) if (anova_components(g)$between <= 0) stop("stage between component not positive")
# ---- 004 unequal subgroup sizes with an excluded row --------------------------------------------
keep <- rep(TRUE, nrow(d)); keep[11] <- FALSE # 0-based row 10 (lot L3) excluded
groups4 <- groups_of(d$value[keep], d$lot[keep])
s4 <- bw_stage(groups4, "range")
lme4 <- nlme::lme(value ~ 1, random = ~ 1 | lot, data = data.frame(value = d$value[keep], lot = factor(d$lot[keep], levels = unique(d$lot))),
method = "REML", control = nlme::lmeControl(tolerance = 1e-10, msTol = 1e-12, niterEM = 100, msMaxIter = 500, opt = "optim"))
vc4 <- nlme::VarCorr(lme4)
comp4 <- anova_components(groups4)
comp4$between <- as.numeric(vc4[1, 1]); comp4$within <- as.numeric(vc4[2, 1])
out <- stage_outputs(s4, 0L, "range", 0L, comp4, "1e-5")
wl6 <- w_limits(6, s4$within, "range")
out <- c(out, list("/within/points/5/upper" = num(wl6[["upper"]], "(d2(6) + 3 d3(6)) sigma_w for the six-value lot."),
"/within/points/5/center" = num(wl6[["center"]], "d2(6) sigma_w."),
"/within/points/5/lower" = num(wl6[["lower"]], "max(0, d2(6) - 3 d3(6)) sigma_w = 0 for n = 6."),
"/subgroups/2/size" = exact(5L, "Lot L3 keeps five values after the exclusion."),
"/excluded_count" = exact(1L, "One excluded row."), "/source_count" = exact(30L, "Rows in the table."),
"/stages/0/subgroup_size_min" = exact(4L, "Smallest lot."), "/stages/0/subgroup_size_max" = exact(6L, "Largest lot.")))
emit("STAT-QUAL-BETWEEN-WITHIN-004", bw_ep, bw_menu, "Between/within chart with unequal lot sizes and an excluded row", "QUAL_BW_01",
list(response = "value", subgroup_mode = "column", subgroup_column = "lot", dispersion = "range"), out,
c(bw_notes, "Unequal sizes 4-6: within sigma averages R_i/d2(n_i) and the R-chart limits change with n_i (chapter 84 lines 25-26, 31-33).",
"Unbalanced components come from nlme::lme(value ~ 1, random = ~1 | lot, method = 'REML'); tolerance 1e-5 on the component SDs because both programs stop at a numerical REML optimum. MS between/within and n0 are the descriptive ANOVA quantities.",
"Row index 10 (0-based) is excluded, so lot L3 has five values; the I chart is centred at the unweighted mean of the six lot means."),
bw_source, bw_cite, bw_req, kind = "axis_coverage", excluded = 10L)
# ==============================================================================================
# Zone chart
# ==============================================================================================
set.seed(20261011)
zone_means <- c(50.0, 49.5, 50.4, 49.8, 50.2, 51.6, 52.0, 51.8, 52.4, 52.1)
zone_value <- round(rep(zone_means, each = 4) + rnorm(40, 0, 2), 2)
zone_gap <- zone_value; zone_gap[15] <- NA
zd <- data.frame(value = zone_value, sample = rep(1:10, each = 4), stage = rep(c("P", "Q"), each = 20),
value_gap = zone_gap, stringsAsFactors = FALSE)
export_dataset(zd, "QUAL_ZONE_01",
"Simulated (R 4.6.1, set.seed 20261011): ten samples of four fill weights whose mean shifts upward in the second half; value_gap is value with row 15 missing; 40 rows",
"Simulated with stats::rnorm", "Constructed validation data; no third-party license.",
c(value = "Float64", sample = "Int64", stage = "String", value_gap = "Float64"))
z <- read_dataset("QUAL_ZONE_01")
zone_ep <- "tabelz.analysis.spc_zone:compute_zone_chart"
zone_menu <- "zoneChartMenuItem"
zone_source <- "Hand-coded zone scoring rule (Jaehn 1987) with stats::integrate chart constants"
zone_cite <- "Jaehn, A. H. (1987) Zone control charts: SPC made easy, Quality 26(10), 51-53; Montgomery, Introduction to Statistical Quality Control, 7th ed., ch. 6 constants"
zone_req <- "REQ-STAT-QUAL-ZONE-001"
zone_notes <- c(
"docs/manual/chapters/84-subgroup-monitoring.tex:103-107: z_i = (xbar_i - c)/(sigma_w/sqrt(n_i)); the centre is the size-weighted mean of the baseline observations; the I chart of subgroup means uses its MR scale.",
"docs/manual/chapters/84-subgroup-monitoring.tex:112-120: zones at 1, 2, 3 with boundary points in the inner zone; weights 0, 2, 4, 8 and signal score 8; scores accumulate on one side, a strict side change starts a new score with that point's weight; a centre-line point adds zero; reset after signal restarts the next point from zero.")
zgroups <- groups_of(z$value, z$sample)
zmeans <- vapply(zgroups, mean, 0); zsizes <- vapply(zgroups, length, 0L)
zcenter <- mean(unlist(zgroups))
sigma_r <- mean(vapply(zgroups, function(g) (max(g) - min(g)) / d2(length(g)), 0))
sigma_s <- mean(vapply(zgroups, function(g) sd(g) / c4(length(g)), 0))
# ---- 001 nominal subgroup means, R-bar within sigma --------------------------------------------
zz <- (zmeans - zcenter) / (sigma_r / sqrt(zsizes))
steps <- zone_scores(zz)
if (!any(steps$signal)) stop("zone data must produce at least one signal")
out <- c(list("/stages/0/center" = num(zcenter, "Size-weighted mean of all 40 observations."),
"/stages/0/sigma" = num(sigma_r, "Mean of R_i/d2(4)."),
"/points/0/scale" = num(sigma_r / 2, "sigma_w / sqrt(4)."),
"/stages/0/signals" = exact(as.integer(sum(steps$signal)), "Signalling points in the stage."),
"/signal_count" = exact(as.integer(sum(steps$signal)), "Signalling points overall."),
"/customized_signal" = exact(FALSE, "Signal score 8 equals the outer-zone weight."),
"/source_count" = exact(40L, "Rows."), "/dropped_subgroup_count" = exact(0L, "Every sample has four values.")),
zone_outputs(steps, zz))
emit("STAT-QUAL-ZONE-001", zone_ep, zone_menu, "Zone chart of subgroup means with R-bar within sigma", "QUAL_ZONE_01",
list(response = "value", statistic = "subgroup_mean", subgroup_mode = "column", subgroup_column = "sample", within_sigma = "rbar"),
out, zone_notes, zone_source, zone_cite, zone_req)
# ---- 002 S-bar within sigma --------------------------------------------------------------------
zz <- (zmeans - zcenter) / (sigma_s / sqrt(zsizes))
steps <- zone_scores(zz)
out <- c(list("/stages/0/sigma" = num(sigma_s, "Mean of s_i/c4(4)."), "/signal_count" = exact(as.integer(sum(steps$signal)), "Signalling points.")),
zone_outputs(steps, zz))
emit("STAT-QUAL-ZONE-002", zone_ep, zone_menu, "Zone chart of subgroup means with S-bar within sigma", "QUAL_ZONE_01",
list(response = "value", statistic = "subgroup_mean", subgroup_mode = "column", subgroup_column = "sample", within_sigma = "sbar"),
out, c(zone_notes, "within_sigma = sbar: the scale is S-bar/c4(n) divided by sqrt(n_i)."), zone_source, zone_cite, zone_req, kind = "axis_coverage")
# ---- 003 individuals chart of subgroup means --------------------------------------------------
sigma_i <- mean(abs(diff(zmeans))) / d2_2
zz <- (zmeans - zcenter) / sigma_i
steps <- zone_scores(zz)
out <- c(list("/stages/0/sigma" = num(sigma_i, "MR-bar of the subgroup means / d2(2)."),
"/points/0/scale" = num(sigma_i, "The MR scale is not divided by sqrt(n)."),
"/signal_count" = exact(as.integer(sum(steps$signal)), "Signalling points.")),
zone_outputs(steps, zz))
emit("STAT-QUAL-ZONE-003", zone_ep, zone_menu, "Zone chart as an individuals chart of subgroup means", "QUAL_ZONE_01",
list(response = "value", statistic = "individuals_of_means", subgroup_mode = "size", subgroup_size = 4L),
out, c(zone_notes, "statistic = individuals_of_means: the scale is MR-bar/d2(2) of the consecutive subgroup means (chapter 84 line 106); the centre stays the size-weighted mean, which equals the mean of the means for equal sizes."),
zone_source, zone_cite, zone_req, kind = "axis_coverage")
# ---- 004 custom weights, reset after signal, two stages -----------------------------------------
weights4 <- c(0, 1, 3, 6); signal4 <- 6
out <- list("/customized_signal" = exact(FALSE, "Signal score equals the outer-zone weight 6."),
"/weights/1" = num(1, "Second zone weight as configured."), "/reset_after_signal" = exact(TRUE, "Reset mode recorded."))
total_signals <- 0L
for (i in 1:2) {
g <- zgroups[(i - 1) * 5 + 1:5]; m <- vapply(g, mean, 0); ns <- vapply(g, length, 0L)
centre <- mean(unlist(g)); sig <- mean(vapply(g, function(x) (max(x) - min(x)) / d2(length(x)), 0))
zz <- (m - centre) / (sig / sqrt(ns)); steps <- zone_scores(zz, weights4, signal4, reset = TRUE)
total_signals <- total_signals + sum(steps$signal)
out[[sprintf("/stages/%d/center", i - 1L)]] <- num(centre, "Stage centre: size-weighted mean of its observations.")
out[[sprintf("/stages/%d/sigma", i - 1L)]] <- num(sig, "Stage within sigma, mean of R_i/d2(4).")
out[[sprintf("/stages/%d/signals", i - 1L)]] <- exact(as.integer(sum(steps$signal)), "Signals in the stage.")
out <- c(out, zone_outputs(steps, zz, (i - 1L) * 5L))
}
out[["/signal_count"]] <- exact(as.integer(total_signals), "Signals over both stages.")
emit("STAT-QUAL-ZONE-004", zone_ep, zone_menu, "Zone chart with custom weights, reset after signal and two stages", "QUAL_ZONE_01",
list(response = "value", statistic = "subgroup_mean", subgroup_mode = "column", subgroup_column = "sample", stage_column = "stage",
within_sigma = "rbar", weights = list(0, 1, 3, 6), signal_score = 6, reset_after_signal = TRUE),
out, c(zone_notes, "Weights 0, 1, 3, 6 with signal 6: a signalling point keeps its score and the next point starts from zero (chapter 84 lines 118-120); each stage (P: samples 1-5, Q: samples 6-10) estimates its own centre and sigma and starts a new accumulation."),
zone_source, zone_cite, zone_req, kind = "axis_coverage")
# ---- 005 raw individual observations with a missing value (gap) ----------------------------------
x <- z$value_gap
present <- which(!is.na(x)); xv <- x[present]
centre <- mean(xv)
adjacent <- diff(present) == 1
mr <- abs(diff(xv))[adjacent]
sigma_raw <- mean(mr) / d2_2
zz <- (xv - centre) / sigma_raw
runs <- cumsum(c(TRUE, !adjacent))
steps <- do.call(rbind, lapply(split(seq_along(zz), runs), function(idx) zone_scores(zz[idx])))
out <- c(list("/stages/0/center" = num(centre, "Mean of the 39 finite observations."),
"/stages/0/sigma" = num(sigma_raw, "MR-bar of adjacent observations / d2(2); no sqrt(n)."),
"/stages/0/mean_moving_range" = num(mean(mr), "Mean of the 37 adjacent moving ranges; none spans the missing row."),
"/stages/0/moving_ranges" = exact(length(mr), "Adjacent pairs."),
"/stages/0/subgroups" = exact(length(xv), "Observations in the stage."),
"/gap_count" = exact(1L, "The missing row breaks adjacency once."),
"/missing_count" = exact(1L, "One missing observation."),
"/units" = exact("g", "Units recorded with the raw chart."),
"/signal_count" = exact(as.integer(sum(steps$signal)), "Signals over both runs."),
"/points/14/event" = exact("after_gap", "The observation after the missing row starts a new run.")),
zone_outputs(steps, zz))
emit("STAT-QUAL-ZONE-005", zone_ep, zone_menu, "Raw-individual zone chart with a missing observation", "QUAL_ZONE_01",
list(response = "value_gap", statistic = "raw_individuals", subgroup_mode = "individual", units = "g"),
out, c("docs/manual/chapters/103-raw-zone.tex:9-20: centre is the mean of the finite observations; sigma = MR-bar/(2/sqrt(pi)) over consecutive eligible pairs; z = (x - xbar)/sigma with no sqrt(n); missing rows break adjacency and restart score accumulation.",
"docs/manual/chapters/103-raw-zone.tex:22-24: same zones, weights 0, 2, 4, 8 and signal 8; the score restarts after the gap, so the two runs are scored separately.",
zone_notes[2]),
zone_source, zone_cite, zone_req, kind = "axis_coverage")
# ==============================================================================================
# Capability sixpack, comparison and several characteristics
# ==============================================================================================
set.seed(20261012)
cap_value <- round(10 + rnorm(40, 0, 0.35) + rep(c(0, 0.25), each = 20), 3)
cap_thick <- round(2.0 + rnorm(40, 0, 0.08), 3)
cd <- data.frame(value = cap_value, thickness = cap_thick, lot = rep(sprintf("L%02d", 1:8), each = 5),
line = rep(c("A", "B"), each = 20), index = 1:40, stringsAsFactors = FALSE)
export_dataset(cd, "QUAL_SIXPACK_01",
"Simulated (R 4.6.1, set.seed 20261012): forty fill weights (line B offset +0.25) and coating thickness, eight lots of five, production index; 40 rows",
"Simulated with stats::rnorm", "Constructed validation data; no third-party license.",
c(value = "Float64", thickness = "Float64", lot = "String", line = "String", index = "Int64"))
cp_d <- read_dataset("QUAL_SIXPACK_01")
cap_source <- "Hand-coded manual capability formulas with stats::pnorm, qnorm, shapiro.test and stats::integrate constants"
cap_cite <- "Montgomery, Introduction to Statistical Quality Control, 7th ed., ch. 8 (Cp, Cpk, Pp, Ppk); NIST Dataplot Cpm; Royston (1995) shapiro.test; R stats documentation"
cap_notes <- c(
"docs/manual/chapters/08-specifications-and-capability.tex:146-160: within sigma s_w = MR-bar/d2 for ordered individuals (d2 = 2/sqrt(pi) at full precision); with rational subgroups ranges are divided by their own d2(n_i) and averaged; overall s_o is the sample SD.",
"docs/manual/chapters/08-specifications-and-capability.tex:268-290: index intervals use SE = sqrt(C^2/(2 nu) + 1/(9 N)) with z_(1 - alpha/2); nu for MR-bar/d2 from A = pi/2 - 1, B = sqrt(3)/2 + pi/12 - 1, v = (mA + 2(m - 1)B)/m^2; for averaged R_i/d2(n_i), v = g^-2 sum (d3/d2)^2; Ppk uses nu = N - 1.",
"docs/manual/chapters/85-capability-views.tex:12-17: normal overall spread is mean +/- 3 overall SD and within spread uses within sigma.",
"docs/manual/chapters/85-capability-views.tex:19-32: Cpm = (USL - LSL)/(6 sqrt(s^2 + (xbar - T)^2)) with the overall sample SD; normal within PPM is a model projection using within sigma.",
"Overall predicted PPM = 1e6 [Phi((LSL - mean)/s_o) + Phi((mean - USL)/s_o)]; within PPM uses s_w; normality check is Shapiro-Wilk (stats::shapiro.test).")
capability <- function(x, lsl, usl, target = NULL, level = 0.95, groups = NULL) {
n <- length(x); mu <- mean(x); s <- sd(x)
if (is.null(groups)) {
sw <- mean(abs(diff(x))) / d2_2
m <- n - 1; A <- pi / 2 - 1; B <- sqrt(3) / 2 + pi / 12 - 1
nu_w <- 1 / (2 * (m * A + 2 * (m - 1) * B) / m^2)
} else {
sw <- mean(vapply(groups, function(g) (max(g) - min(g)) / d2(length(g)), 0))
g <- length(groups)
nu_w <- 1 / (2 * sum(vapply(groups, function(gg) (d3(length(gg)) / d2(length(gg)))^2, 0)) / g^2)
}
side <- function(limit, sigma, upper) if (is.null(limit)) NULL else if (upper) (limit - mu) / (3 * sigma) else (mu - limit) / (3 * sigma)
two <- function(sigma) if (is.null(lsl) || is.null(usl)) NULL else (usl - lsl) / (6 * sigma)
cpl <- side(lsl, sw, FALSE); cpu <- side(usl, sw, TRUE); cpk <- min(unlist(list(cpl, cpu)))
ppl <- side(lsl, s, FALSE); ppu <- side(usl, s, TRUE); ppk <- min(unlist(list(ppl, ppu)))
zq <- qnorm(0.5 + level / 2)
ci <- function(index, nu) { se <- sqrt(index^2 / (2 * nu) + 1 / (9 * n)); c(index - zq * se, index + zq * se) }
tails <- function(sigma) 1e6 * ((if (is.null(lsl)) 0 else pnorm((lsl - mu) / sigma)) + (if (is.null(usl)) 0 else pnorm((mu - usl) / sigma)))
cpm <- if (is.null(lsl) || is.null(usl) || is.null(target)) NULL else (usl - lsl) / (6 * sqrt(s^2 + (mu - target)^2))
list(n = n, mean = mu, std = s, within = sw, cp = two(sw), cpk = cpk, pp = two(s), ppk = ppk, cpm = cpm,
cpk_ci = ci(cpk, nu_w), ppk_ci = ci(ppk, n - 1), dpmo = tails(s), within_ppm = tails(sw),
shapiro = shapiro.test(x)$p.value, oos = sum((if (is.null(lsl)) FALSE else x < lsl) | (if (is.null(usl)) FALSE else x > usl)))
}
cap_outputs <- function(r, prefix) {
out <- list()
p <- function(name) paste0(prefix, "/", name)
out[[p("n")]] <- exact(as.integer(r$n), "Finite observations.")
out[[p("mean")]] <- num(r$mean, "Arithmetic mean.")
out[[p("std")]] <- num(r$std, "Sample SD (n - 1).")
out[[p("within_sigma")]] <- num(r$within, "Within sigma by the manual estimator for the subgrouping.")
if (!is.null(r$cp)) out[[p("cp")]] <- num(r$cp, "(USL - LSL)/(6 s_w).") else out[[p("cp")]] <- absent("Cp needs both limits.")
out[[p("cpk")]] <- num(r$cpk, "min(CPL, CPU) with s_w.")
if (!is.null(r$pp)) out[[p("pp")]] <- num(r$pp, "(USL - LSL)/(6 s_o).") else out[[p("pp")]] <- absent("Pp needs both limits.")
out[[p("ppk")]] <- num(r$ppk, "min(PPL, PPU) with s_o.")
if (!is.null(r$cpm)) out[[p("cpm")]] <- num(r$cpm, "(USL - LSL)/(6 sqrt(s_o^2 + (mean - T)^2).") else out[[p("cpm")]] <- absent("Cpm needs both limits and a target.")
out[[p("cpk_ci_lower")]] <- num(r$cpk_ci[1], "Cpk - z SE with the within-estimator degrees of freedom.")
out[[p("cpk_ci_upper")]] <- num(r$cpk_ci[2], "Cpk + z SE.")
out[[p("ppk_ci_lower")]] <- num(r$ppk_ci[1], "Ppk - z SE with nu = N - 1.")
out[[p("ppk_ci_upper")]] <- num(r$ppk_ci[2], "Ppk + z SE.")
out[[p("dpmo")]] <- num(r$dpmo, "Overall predicted PPM from the normal model with s_o.")
out[[p("within_ppm")]] <- num(r$within_ppm, "Within-model PPM with s_w.")
out[[p("normality_p")]] <- prob(r$shapiro, "shapiro.test p (Royston 1995 in both implementations; loosened for the SWILK single-precision constants).", "1e-5")
out[[p("oos")]] <- exact(as.integer(r$oos), "Observations strictly outside the limits.")
out
}
# ---- sixpack 001 nominal individuals, both limits and target --------------------------------------
six_ep <- "tabelz.analysis.capability_sixpack:compute_capability_sixpack"
six_req <- "REQ-STAT-QUAL-CAPABILITY-SIXPACK-001"
x <- cp_d$value; lsl <- 8.8; usl <- 11.2; target <- 10
r <- capability(x, lsl, usl, target)
mr_bar <- mean(abs(diff(x)))
out <- c(cap_outputs(r, "/capability"),
list("/within_spread/0" = num(r$mean - 3 * r$within, "mean - 3 s_w."), "/within_spread/1" = num(r$mean + 3 * r$within, "mean + 3 s_w."),
"/overall_spread/0" = num(r$mean - 3 * r$std, "mean - 3 s_o."), "/overall_spread/1" = num(r$mean + 3 * r$std, "mean + 3 s_o."),
"/overall_spread_label" = exact("mean +/- 3 overall SD", "Normal model label."),
"/chart_family" = exact("I-MR", "Individuals and moving-range charts match individual observations."),
"/primary/0/center" = num(r$mean, "I chart centre = mean."),
"/primary/0/upper" = num(r$mean + 3 * mr_bar / d2_2, "mean + 3 MR-bar/d2(2)."),
"/primary/0/lower" = num(r$mean - 3 * mr_bar / d2_2, "mean - 3 MR-bar/d2(2)."),
"/secondary/0/center" = num(mr_bar, "MR chart centre = MR-bar."),
"/secondary/0/upper" = num((1 + 3 * d3_2 / d2_2) * mr_bar, "D4(2) MR-bar."),
"/run_plot_subgroups" = exact(25L, "The run plot shows the last 25 points."),
"/excluded_count" = exact(0L, "No exclusions.")))
emit("STAT-QUAL-CAPABILITY-SIXPACK-001", six_ep, "capabilitySixpackMenuItem", "Capability sixpack of ordered individuals with a target", "QUAL_SIXPACK_01",
list(column = "value", lsl = lsl, usl = usl, target = target, method = "normal", subgroup_mode = "individuals", confidence_level = 0.95),
out, c(cap_notes, "Individuals: s_w = MR-bar/d2(2) over the 40 ordered values; the I chart uses the same MR-bar; 95% intervals with z = qnorm(0.975)."),
cap_source, cap_cite, six_req)
# ---- sixpack 002 consecutive subgroups of five, 90% confidence --------------------------------------
groups5 <- split(x, rep(1:8, each = 5))
r <- capability(x, lsl, usl, target, level = 0.90, groups = groups5)
means5 <- vapply(groups5, mean, 0); ranges5 <- vapply(groups5, function(g) max(g) - min(g), 0)
out <- c(cap_outputs(r, "/capability"),
list("/primary/0/center" = num(mean(means5), "X-bar centre: mean of the eight subgroup means."),
"/primary/0/upper" = num(mean(means5) + 3 * r$within / sqrt(5), "centre + 3 s_w/sqrt(5)."),
"/primary/0/lower" = num(mean(means5) - 3 * r$within / sqrt(5), "centre - 3 s_w/sqrt(5)."),
"/primary/0/y" = num(means5[1], "First subgroup mean."),
"/secondary/0/center" = num(d2(5) * r$within, "R chart centre d2(5) s_w."),
"/secondary/0/upper" = num((d2(5) + 3 * d3(5)) * r$within, "(d2(5) + 3 d3(5)) s_w."),
"/secondary/0/y" = num(ranges5[1], "First subgroup range."),
"/run_plot_subgroups" = exact(8L, "Eight subgroups, fewer than 25."),
"/capability/subgroup_count" = exact(8L, "Eight complete subgroups."),
"/capability/confidence_level" = num(0.90, "Configured level.")))
emit("STAT-QUAL-CAPABILITY-SIXPACK-002", six_ep, "capabilitySixpackMenuItem", "Capability sixpack with consecutive subgroups of five at 90% confidence", "QUAL_SIXPACK_01",
list(column = "value", lsl = lsl, usl = usl, target = target, method = "normal", subgroup_mode = "constant", subgroup_size = 5L, confidence_level = 0.90),
out, c(cap_notes, "Subgroups of five: s_w = mean(R_i/d2(5)) (the default range estimator for sizes up to ten); Cpk nu = 1/(2 v) with v = g^-2 sum (d3(5)/d2(5))^2; 90% intervals with z = qnorm(0.95)."),
cap_source, cap_cite, six_req, kind = "axis_coverage")
# ---- sixpack 003 subgroups from a column, upper limit only, excluded rows ---------------------------
keep <- rep(TRUE, 40); keep[c(3, 22)] <- FALSE # 0-based rows 2 and 21 excluded
xk <- cp_d$thickness[keep]; lots <- cp_d$lot[keep]
groups_lot <- groups_of(xk, lots)
r <- capability(xk, NULL, 2.25, NULL, groups = groups_lot)
out <- c(cap_outputs(r, "/capability"),
list("/capability/cpm_note" = exact("Cpm needs both specification limits and a target.", "One-sided specification."),
"/within_spread/1" = num(r$mean + 3 * r$within, "mean + 3 s_w."),
"/excluded_count" = exact(2L, "Two excluded rows."),
"/capability/subgroup_count" = exact(8L, "Eight lots, two of them with four values.")))
# Lot L01 contains excluded row index 2 and lot L05 excluded row index 21, so each keeps four values.
out[["/primary/0/center"]] <- num(mean(xk), "X-bar centre: size-weighted mean of the 38 retained observations.")
out[["/primary/0/upper"]] <- num(mean(xk) + 3 * r$within / sqrt(4), "Size-weighted centre + 3 s_w/sqrt(4): lot L01 has four values after the exclusion.")
out[["/primary/1/upper"]] <- num(mean(xk) + 3 * r$within / sqrt(5), "Size-weighted centre + 3 s_w/sqrt(5) for the complete lot L02.")
emit("STAT-QUAL-CAPABILITY-SIXPACK-003", six_ep, "capabilitySixpackMenuItem", "Capability sixpack by lot column with an upper limit only and excluded rows", "QUAL_SIXPACK_01",
list(column = "thickness", usl = 2.25, method = "normal", subgroup_mode = "by_column", group_column = "lot", confidence_level = 0.95),
out, c(cap_notes, "Only USL: Cp, Pp and Cpm are unavailable; Cpk = CPU and Ppk = PPU. Lots L01 and L05 lose one row each to the exclusions, so s_w averages R_i/d2(n_i) over sizes 4 and 5 and the X-bar limits change with n_i.",
"The X-bar centre is the size-weighted mean of all retained observations."),
cap_source, cap_cite, six_req, kind = "axis_coverage", excluded = c(2L, 21L))
# ---- comparison 001 by group column -------------------------------------------------------------------
cmp_ep <- "tabelz.analysis.capability_sixpack:compute_capability_comparison"
cmp_req <- "REQ-STAT-QUAL-CAPABILITY-COMPARISON-001"
out <- list()
for (i in 1:2) {
level <- c("A", "B")[i]; xi <- cp_d$value[cp_d$line == level]
r <- capability(xi, lsl, usl, target)
out[[sprintf("/populations/%d/population", i - 1L)]] <- exact(level, "Sorted level label.")
out[[sprintf("/populations/%d/rows", i - 1L)]] <- exact(as.integer(length(xi)), "Rows in the population.")
out[[sprintf("/populations/%d/excluded", i - 1L)]] <- exact(0L, "No exclusions.")
out <- c(out, cap_outputs(r, sprintf("/populations/%d/capability", i - 1L)))
}
emit("STAT-QUAL-CAPABILITY-COMPARISON-001", cmp_ep, "capabilityComparisonMenuItem", "Compare capability of two production lines", "QUAL_SIXPACK_01",
list(column = "value", lsl = lsl, usl = usl, target = target, split_kind = "group", split_column = "line", method = "normal", subgroup_mode = "individuals", confidence_level = 0.95),
out, c(cap_notes, "docs/manual/chapters/85-capability-views.tex:34-37: each population gets the identical computation with the same specifications, model and subgrouping; populations are the sorted levels of the grouping column.",
"Each line is analysed as ordered individuals within the line: s_w = MR-bar/d2(2) over that line's 20 values."),
cap_source, cap_cite, cmp_req)
# ---- comparison 002 before/after a split point with subgroups of five ----------------------------------
out <- list()
parts <- list(cp_d$value[cp_d$index < 21], cp_d$value[cp_d$index >= 21])
labels <- c("before 21", "from 21")
for (i in 1:2) {
xi <- parts[[i]]; gi <- split(xi, rep(seq_len(length(xi) / 5), each = 5))
r <- capability(xi, lsl, usl, NULL, level = 0.95, groups = gi)
out[[sprintf("/populations/%d/population", i - 1L)]] <- exact(labels[i], "Split-point label.")
out[[sprintf("/populations/%d/rows", i - 1L)]] <- exact(as.integer(length(xi)), "Rows in the population.")
out <- c(out, cap_outputs(r, sprintf("/populations/%d/capability", i - 1L)))
out[[sprintf("/populations/%d/capability/subgroup_count", i - 1L)]] <- exact(4L, "Four subgroups of five.")
}
emit("STAT-QUAL-CAPABILITY-COMPARISON-002", cmp_ep, "capabilityComparisonMenuItem", "Compare capability before and after a split point with subgroups of five", "QUAL_SIXPACK_01",
list(column = "value", lsl = lsl, usl = usl, split_kind = "point", split_column = "index", split_value = 21, method = "normal", subgroup_mode = "constant", subgroup_size = 5L, confidence_level = 0.95),
out, c(cap_notes, "docs/manual/chapters/85-capability-views.tex:34-37: populations before and from the declared split point 21 on the index column; no target so Cpm is unavailable.",
"Within each population consecutive rows of five form subgroups: s_w = mean(R_i/d2(5))."),
cap_source, cap_cite, cmp_req, kind = "axis_coverage")
# ---- multiple 001 two characteristics, individuals -------------------------------------------------------
mul_ep <- "tabelz.analysis.capability_sixpack:compute_capability_multiple"
mul_req <- "REQ-STAT-QUAL-CAPABILITY-MULTIPLE-001"
r1 <- capability(cp_d$value, lsl, usl, target)
r2 <- capability(cp_d$thickness, 1.75, 2.25, NULL)
out <- c(cap_outputs(r1, "/rows/0"), cap_outputs(r2, "/rows/1"),
list("/rows/0/column" = exact("value", "First characteristic."), "/rows/1/column" = exact("thickness", "Second characteristic."),
"/rows/1/cpm_note" = exact("Cpm needs both specification limits and a target.", "Blank target for thickness.")))
emit("STAT-QUAL-CAPABILITY-MULTIPLE-001", mul_ep, "capabilityMultipleMenuItem", "Capability of two characteristics with their own limits", "QUAL_SIXPACK_01",
list(columns = list("value", "thickness"), lsls = list("8.8", "1.75"), usls = list("11.2", "2.25"), targets = list("10", ""),
method = "normal", subgroup_mode = "individuals", confidence_level = 0.95),
out, c(cap_notes, "docs/manual/chapters/85-capability-views.tex:39-44: the single-characteristic analysis is applied separately to each column with its own limits and target; nothing is pooled.",
"Limits are entered as text; a blank target leaves Cpm unavailable."),
cap_source, cap_cite, mul_req)
# ---- multiple 002 by lot, one-sided limits, 99% confidence ---------------------------------------------
g_value <- groups_of(cp_d$value, cp_d$lot); g_thick <- groups_of(cp_d$thickness, cp_d$lot)
r1 <- capability(cp_d$value, NULL, usl, NULL, level = 0.99, groups = g_value)
r2 <- capability(cp_d$thickness, 1.75, NULL, NULL, level = 0.99, groups = g_thick)
out <- c(cap_outputs(r1, "/rows/0"), cap_outputs(r2, "/rows/1"),
list("/rows/0/subgroup_count" = exact(8L, "Eight lots."), "/rows/1/subgroup_count" = exact(8L, "Eight lots."),
"/rows/0/confidence_level" = num(0.99, "Configured level.")))
emit("STAT-QUAL-CAPABILITY-MULTIPLE-002", mul_ep, "capabilityMultipleMenuItem", "Capability of two characteristics by lot with one-sided limits at 99% confidence", "QUAL_SIXPACK_01",
list(columns = list("value", "thickness"), lsls = list("", "1.75"), usls = list("11.2", ""),
method = "normal", subgroup_mode = "by_column", group_column = "lot", confidence_level = 0.99),
out, c(cap_notes, "One-sided limits per column: value has only USL (Cpk = CPU) and thickness only LSL (Cpk = CPL); Cp, Pp and Cpm are unavailable; lots of five give s_w = mean(R_i/d2(5)); 99% intervals with z = qnorm(0.995)."),
cap_source, cap_cite, mul_req, kind = "axis_coverage")
cat("subgroup chart and capability references generated\n")
generators/r/quality_variables_acceptance.R
source("common.R", local = TRUE)
run <- generator_run(c("stats", "jsonlite"))
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("Analytic normal-process plan:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
emit <- function(id, title, config, outputs, notes, deferred = list()) {
write_expected("quality", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = deferred, generator_run = run
))
write_case("quality", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "quality",
entry_point = "tabelz.analysis.variables_acceptance:plan_variables_acceptance",
menu_items = list("variablesAcceptanceMenuItem"), title = title,
case_kind = "nominal", dataset = NULL, excluded_rows = list(),
configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/quality/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats analytic normal/noncentral-t formulas",
citation = "R stats::pnorm, stats::qnorm, stats::pt and stats::uniroot documentation",
generator = list(path = "generators/r/quality_variables_acceptance.R", sha256 = ""),
independent = TRUE, convention_notes = notes
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-QUAL-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
pa <- function(p, n, k, status) {
z <- stats::qnorm(1 - p)
if (status == "known") stats::pnorm(sqrt(n) * (z - k))
else stats::pt(sqrt(n) * k, df = n - 1L, ncp = sqrt(n) * z, lower.tail = FALSE)
}
interval <- function(n, config) {
upper_target <- 1 - config$producer_risk
lower_target <- config$consumer_risk
if (config$sigma_status == "known") {
upper <- stats::qnorm(1 - config$aql) - stats::qnorm(upper_target) / sqrt(n)
lower <- stats::qnorm(1 - config$ltpd) - stats::qnorm(lower_target) / sqrt(n)
} else {
upper <- stats::uniroot(function(k) pa(config$aql, n, k, "unknown") - upper_target,
c(-20, 20), tol = 1e-13)$root
lower <- stats::uniroot(function(k) pa(config$ltpd, n, k, "unknown") - lower_target,
c(-20, 20), tol = 1e-13)$root
}
c(lower = lower, upper = upper)
}
plan <- function(config) {
for (n in 2L:config$max_sample_size) {
bounds <- interval(n, config)
if (bounds[["lower"]] > bounds[["upper"]]) next
midpoint <- mean(bounds)
k <- floor(midpoint * 1e4) / 1e4
if (k < bounds[["lower"]]) k <- ceiling(bounds[["lower"]] * 1e4) / 1e4
if (k > bounds[["upper"]]) k <- midpoint
if (pa(config$aql, n, k, config$sigma_status) < 1 - config$producer_risk ||
pa(config$ltpd, n, k, config$sigma_status) > config$consumer_risk) next
return(list(n = n, k = k, aql = pa(config$aql, n, k, config$sigma_status),
ltpd = pa(config$ltpd, n, k, config$sigma_status)))
}
stop("The chosen request did not admit a plan within the cap")
}
for (status in c("known", "unknown")) {
config <- list(spec_side = if (status == "known") "upper" else "lower",
spec_limit = if (status == "known") 87.5 else 12.5,
sigma_status = status, aql = 0.02, ltpd = 0.10,
producer_risk = 0.05, consumer_risk = 0.10,
max_sample_size = 500L, oc_points = 11L)
if (status == "known") config$sigma <- 2.25
result <- plan(config)
id <- if (status == "known") "STAT-QUAL-VARACC-001" else "STAT-QUAL-VARACC-002"
outputs <- list("/sample_size" = exact(result$n,
"First n with a feasible k interval; one fewer n fails a risk constraint."))
deferred <- list()
if (status == "known") {
outputs[["/acceptance_constant"]] <- tol(result$k,
"Midpoint k quantized down to four decimals; owner 1e-9 tier.")
outputs[["/probability_accept_aql"]] <- tol(result$aql,
"Normal AQL acceptance probability; owner 1e-9 tier.")
outputs[["/probability_accept_ltpd"]] <- tol(result$ltpd,
"Normal LTPD acceptance probability; owner 1e-9 tier.")
} else {
deferred <- lapply(c("/acceptance_constant", "/probability_accept_aql",
"/probability_accept_ltpd"), function(path)
list(path = path, reason = "reference precision below tier"))
}
emit(id, paste(status, "sigma one-sided variables plan"), config,
outputs,
c("Manual chapter 64: Pa(p)=Phi(sqrt(n)*(z_p-k)) for known sigma, with z_p=qnorm(1-p).",
"For unknown sigma Pa(p)=P(T>=sqrt(n)*k), T noncentral-t(df=n-1,ncp=sqrt(n)*z_p).",
"R scans n from 2 to 500, solves the two k bounds, takes their midpoint and quantizes down to four decimals.",
"Upper and lower specification sides share the same one-sided OC probability by symmetry.",
if (status == "unknown") "R pt warned that full precision may not be achieved; continuous outputs are deferred at the owner 1e-9 tier."),
deferred = deferred)
}
cat("variables acceptance references generated\n")
generators/r/reference_stephens_ad.R
source("common.R", local = TRUE)
id <- "STAT-DESC-AD-STEPHENS-001"
dataset <- "DESC_STEPHENS_1974_WEIGHTS"
weights <- data.frame(value = c(148, 154, 158, 160, 161, 162, 166, 170, 182, 195, 236))
export_dataset(weights, dataset,
"Stephens (1974), Illustration 2: eleven garment weights",
"Stephens (1974), Journal of the American Statistical Association 69, 730-737, p. 735",
"Eleven published numeric inputs transcribed for a validation example; paper not bundled.",
c(value = "Float64"))
values <- read_dataset(dataset)$value
reference <- nortest::ad.test(values)
run <- generator_run(c("nortest", "jsonlite"))
write_expected("descriptives", id, list(
schema_version = "2.0.0", case_id = id,
outputs = list(
"/statistic" = list(comparison = "tolerance", value = dec(unname(reference$statistic)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = "Pinned nortest 1.0-4 raw fitted-normal A2 on Stephens' printed inputs."),
"/p_value" = list(comparison = "tail_probability", value = dec(unname(reference$p.value)),
relative_tolerance = "1e-9",
rationale = "Pinned nortest 1.0-4 later finite-n/polynomial calibration; n=11."),
"/observations" = list(comparison = "exact", value = 11L,
rationale = "Eleven published weights.")),
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("descriptives", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "descriptives", entry_point = "tabelz.executor.distribution_diagnostics",
menu_items = list("distributionDiagnosticsMenuItem"),
title = "Stephens 1974 weights with later Anderson-Darling calibration",
case_kind = "nominal",
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = list(column = "value", method = "anderson_darling"),
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/descriptives/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "Stephens (1974) printed inputs; R nortest 1.0-4 statistic and later p calibration",
citation = "Stephens (1974), Illustration 2/Table 1.3; nortest 1.0-4 ad.test documentation",
generator = list(path = "generators/r/reference_stephens_ad.R", sha256 = ""),
independent = TRUE,
convention_notes = list(
"The raw A2 is computed from source printed inputs and fitted mean/sample SD.",
"The source's 1974 modified A2=1.095 and one-percent critical 1.092 reject; the later nortest p=0.010454 does not reject at one percent.",
"Published and later calibration decisions are distinct; source does not publish this later p-value.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-DESC-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
generators/r/regression_advanced_axes.R
source("common.R", local = TRUE)
orth <- read_dataset("REG_ORTHODONT")
tree <- read_dataset("REG_TREE")
da <- read_dataset("REG_DISCRIMINANT")
pls_data <- read_dataset("REG_PLS")
curve <- data.frame(x = seq(0, 5, length.out = 30L))
curve$growth <- 1 + 4 * (1 - exp(-0.8 * curve$x)) + 0.02 * cos(2 * curve$x)
export_dataset(curve, "REG_GROWTH", "Constructed bounded asymptotic-growth curve",
"R stats::nls; Tabelz nonlinear regression manual",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", growth = "Float64"))
curve <- read_dataset("REG_GROWTH")
logistic <- data.frame(x = seq(0.5, 10, length.out = 40L))
logistic$y <- 1 + 4 / (1 + (logistic$x / 4)^2) + 0.01 * sin(logistic$x)
export_dataset(logistic, "REG_4PL", "Constructed bounded four-parameter logistic curve",
"R stats::nls; Tabelz nonlinear regression manual",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64"))
logistic <- read_dataset("REG_4PL")
crossed <- expand.grid(a = c("A1", "A2", "A3", "A4"),
b = c("B1", "B2", "B3", "B4"), t = seq_len(4L),
stringsAsFactors = FALSE)
crossed$x <- as.double(crossed$t + as.integer(factor(crossed$a)) / 5 +
as.integer(factor(crossed$b)) / 7)
crossed$y <- 3 + 0.8 * crossed$x +
c(-1.2, -0.3, 0.6, 1.3)[match(crossed$a, c("A1", "A2", "A3", "A4"))] +
c(0.7, -0.9, 0.4, -0.2)[match(crossed$b, c("B1", "B2", "B3", "B4"))] +
0.05 * sin(seq_len(nrow(crossed)))
crossed <- crossed[c("a", "b", "t", "x", "y")]
export_dataset(crossed, "REG_CROSSED", "Constructed crossed random-intercept design",
"R lme4::lmer crossed variance components",
"Constructed test data; no third-party dataset license.",
c(a = "String", b = "String", t = "Int64", x = "Float64", y = "Float64"))
crossed <- read_dataset("REG_CROSSED")
run <- generator_run(c("stats", "nlme", "lme4", "plm", "rpart", "MASS", "pls", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R regression II:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
set <- function(x, label) list(comparison = "set", values = as.list(as.character(x)), rationale = label)
emit <- function(id, base, dataset, config, outputs, notes, kind = "axis_coverage",
deferred = list(), repeat_count = 1L) {
prior <- jsonlite::fromJSON(file.path(ref_root(), "cases", "regression",
paste0(base, ".json")), simplifyVector = FALSE)
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = deferred,
generator_run = run))
write_case("regression", id, list(schema_version = "2.0.0", case_id = id,
version = "1.0.0", family = "regression", entry_point = prior$entry_point,
menu_items = prior$menu_items, title = paste("Regression II option axis", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = repeat_count,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R statistical packages",
citation = "R nlme::lme, lme4::lmer, plm::plm, stats::nls/step, MASS::lda, pls::plsr",
generator = list(path = "generators/r/regression_advanced_axes.R", sha256 = ""),
independent = TRUE, convention_notes = c(notes, "R re-reads the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
mixed <- nlme::lme(distance ~ age + sex, random = ~1 | subject, data = orth,
method = "REML", control = nlme::lmeControl(opt = "optim"))
emit("STAT-REG-AXIS-ADV-MIXED-CATEGORICAL-001", "STAT-REG-MIXED-REML-001",
"REG_ORTHODONT", list(response_column = "distance", group_column = "subject",
fixed_numeric_columns = list("age"),
fixed_categorical_columns = list("sex"), estimation = "reml"),
list("/coefficients/1/estimate" = tol(nlme::fixef(mixed)[["age"]], "adjusted age slope"),
"/analyzed_count" = exact(as.integer(nrow(orth)), "complete observations")),
list("Female/Male factor levels are alphabetic; nlme random intercept is REML."))
crossed_fit <- lme4::lmer(y ~ x + (1 | a) + (1 | b), data = crossed, REML = TRUE,
control = lme4::lmerControl(optimizer = "bobyqa",
optCtrl = list(maxfun = 100000L)))
crossed_config <- list(response_column = "y", fixed_numeric_columns = list("x"),
model_mode = "variance_components",
components = list(list(name = "a", level_column = "a", relation = "crossed"),
list(name = "b", level_column = "b", relation = "crossed")),
estimation = "reml", max_rows = 1000L)
emit("STAT-REG-AXIS-ADV-STRUCT-CROSSED-001", "STAT-REG-MIXED-STRUCT-SLOPE-001",
"REG_CROSSED", crossed_config,
list("/coefficients/1/estimate" = tol(lme4::fixef(crossed_fit)[["x"]],
"crossed random-intercept fixed slope"),
"/analyzed_count" = exact(as.integer(nrow(crossed)), "balanced crossed cells")),
list("Two independent crossed grouping factors; lmer REML; bobyqa maxfun 100000."),
kind = "nominal")
crossed_fit_ml <- lme4::lmer(y ~ x + (1 | a) + (1 | b), data = crossed, REML = FALSE,
control = lme4::lmerControl(optimizer = "bobyqa",
optCtrl = list(maxfun = 100000L)))
crossed_config_ml <- crossed_config
crossed_config_ml$estimation <- "ml"
emit("STAT-REG-AXIS-ADV-STRUCT-CROSSED-ML-001", "STAT-REG-MIXED-STRUCT-SLOPE-001",
"REG_CROSSED", crossed_config_ml,
list("/coefficients/1/estimate" = tol(lme4::fixef(crossed_fit_ml)[["x"]],
"ML crossed-model fixed slope")),
list("Variance-component mode with ML explicitly; do not compare REML likelihoods."),
kind = "axis_coverage")
panel <- plm::plm(distance ~ age, data = orth, index = c("subject", "time"),
model = "within", effect = "individual")
emit("STAT-REG-AXIS-ADV-PANEL-90-001", "STAT-REG-PANEL-001", "REG_ORTHODONT",
list(response_column = "distance", predictor_columns = list("age"),
entity_column = "subject", time_column = "time", confidence_level = 0.9),
list("/coefficients/0/estimate" = tol(coef(panel)[["age"]], "within age slope", "1e-9")),
list("90% intervals change quantile only; plm within slope is invariant."))
growth_fit <- stats::nls(growth ~ c + a * (1 - exp(-k * x)), data = curve,
start = list(a = 4, k = 0.8, c = 1), algorithm = "port",
lower = c(a = 0, k = 0.01, c = 0),
upper = c(a = 10, k = 3, c = 5),
control = stats::nls.control(maxiter = 1000, tol = 1e-12))
emit("STAT-REG-AXIS-ADV-NONLINEAR-GROWTH-001", "STAT-REG-NONLINEAR-DECAY-001",
"REG_GROWTH",
list(x_column = "x", response = "growth", curve = "asymptotic_growth",
parameters = list(list(name = "a", start = 4, lower = 0, upper = 10),
list(name = "k", start = 0.8, lower = 0.01, upper = 3),
list(name = "c", start = 1, lower = 0, upper = 5))),
list("/artifact/estimates/0" = tol(coef(growth_fit)[["a"]], "growth amplitude"),
"/artifact/estimates/1" = tol(coef(growth_fit)[["k"]], "growth rate")),
list("NLS port bounded fit on original response scale; same parameter order as manual."),
kind = "nominal")
logistic_fit <- stats::nls(y ~ lo + (hi - lo) / (1 + (x / mid)^slope), data = logistic,
start = list(lo = 1, hi = 5, mid = 4, slope = 2),
algorithm = "port",
lower = c(lo = 0, hi = 2, mid = 0.1, slope = 0.1),
upper = c(lo = 3, hi = 8, mid = 10, slope = 5),
control = stats::nls.control(maxiter = 1000, tol = 1e-12))
emit("STAT-REG-AXIS-ADV-NONLINEAR-4PL-001", "STAT-REG-NONLINEAR-DECAY-001",
"REG_4PL", list(x_column = "x", response = "y", curve = "four_parameter_logistic",
parameters = list(list(name = "lo", start = 1, lower = 0, upper = 3),
list(name = "hi", start = 5, lower = 2, upper = 8),
list(name = "mid", start = 4, lower = 0.1, upper = 10),
list(name = "slope", start = 2, lower = 0.1, upper = 5))),
list("/artifact/estimates/2" = tol(coef(logistic_fit)[["mid"]], "four-parameter midpoint"),
"/artifact/estimates/3" = tol(coef(logistic_fit)[["slope"]], "four-parameter slope")),
list("R nls port curve matches manual 4PL direction and positive midpoint/slope."),
kind = "nominal")
# Split-dependent model coefficients cannot be reproduced in R when the
# validated workflow assigns rows with NumPy. Count and determinism remain
# independent; selected penalties are explicitly deferred.
for (algorithm in c("ridge", "lasso")) {
id <- paste0("STAT-REG-AXIS-ADV-VALIDATED-", toupper(algorithm), "-001")
emit(id, "STAT-REG-VALIDATED-COUNT-001", "REG_ORTHODONT",
list(response = "distance", numeric_predictors = list("age"),
algorithm = algorithm, imputation = "drop", cv_folds = 3L,
test_fraction = 0.25, seed = 7L),
list("/source_count" = exact(as.integer(nrow(orth)), "full validated source"),
"/training_metrics/row_count" = exact(81L, "75% training rows")),
list("R verifies population counts; NumPy split and CV folds are not R reproducible."),
kind = "determinism", repeat_count = 2L,
deferred = list(list(path = "/recipe/coefficients",
reason = "NumPy split and CV-selected penalty cannot be reproduced with R RNG.")))
}
for (prior_mode in c("observed", "custom")) {
id <- paste0("STAT-REG-AXIS-ADV-DA-PRIOR-", toupper(prior_mode), "-001")
cfg <- list(response = "class", predictors = list("x"), model = "lda",
prior_mode = prior_mode, standardize = TRUE)
if (prior_mode == "custom") cfg$custom_priors <- list(0.7, 0.3)
prior_a <- if (prior_mode == "custom") 0.7 else 0.5
emit(id, "STAT-REG-DISCRIMINANT-LDA-001", "REG_DISCRIMINANT", cfg,
list("/source_count" = exact(as.integer(nrow(da)), "complete class source"),
"/artifact/priors/0" = tol(prior_a, "selected prior for class A", "1e-12")),
list("MASS::lda prior chosen explicitly; balanced design has observed prior 1/2."),
kind = "nominal")
}
emit("STAT-REG-AXIS-ADV-PLS-SCALE-001", "STAT-REG-PLS-FIT-001", "REG_PLS",
list(response = "y", predictors = list("x"), scale = TRUE, cv_folds = 3L,
seed = 3L),
list("/source_count" = exact(as.integer(nrow(pls_data)), "complete PLS source"),
"/training_metrics/row_count" = exact(23L, "floor(0.25*30) held out")),
list("R pls::plsr scale=TRUE; split-dependent coefficients deferred."),
kind = "determinism", repeat_count = 2L,
deferred = list(list(path = "/artifact/coefficients",
reason = "NumPy-selected train rows and CV folds differ from R RNG.")))
base_sel <- list(response = "distance", candidate_predictors = list("age", "age_sq"))
full <- stats::lm(distance ~ age + age_sq, data = orth)
null <- stats::lm(distance ~ 1, data = orth)
for (criterion in c("aic", "bic")) for (direction in c("forward", "backward", "both")) {
k <- if (criterion == "aic") 2 else log(nrow(orth))
ref <- stats::step(if (direction == "backward") full else null,
scope = list(lower = ~1, upper = ~age + age_sq),
direction = direction, k = k, trace = 0)
selected <- names(stats::coef(ref))[-1L]
cfg <- base_sel
cfg$method <- "stepwise"
cfg$criterion <- criterion
cfg$direction <- direction
id <- paste0("STAT-REG-AXIS-ADV-SELECTION-", toupper(criterion), "-",
toupper(direction), "-001")
emit(id, "STAT-REG-SELECTION-001", "REG_ORTHODONT", cfg,
list("/selected" = set(selected, "R step selected predictor names"),
"/n" = exact(as.integer(nrow(orth)), "complete source rows")),
list(paste("R step", direction, "criterion", criterion, "with k=", dec(k),
"and explicit null/full scope.")), kind = "axis_coverage")
}
for (criterion in c("aic", "bic")) {
k <- if (criterion == "aic") 2 else log(nrow(orth))
candidates <- list(age = stats::lm(distance ~ age, data = orth),
age_sq = stats::lm(distance ~ age_sq, data = orth),
both = full)
scores <- vapply(candidates, function(m) stats::AIC(m, k = k), 0.0)
selected <- switch(names(which.min(scores)), age = "age", age_sq = "age_sq",
both = c("age", "age_sq"))
cfg <- base_sel
cfg$method <- "best_subsets"
cfg$criterion <- criterion
id <- paste0("STAT-REG-AXIS-ADV-SELECTION-BEST-", toupper(criterion), "-001")
emit(id, "STAT-REG-SELECTION-001", "REG_ORTHODONT", cfg,
list("/selected" = set(selected, "R exhaustive nonempty subsets")),
list("R AIC enumerates all three nonempty two-predictor subsets; BIC uses k=log(n)."),
kind = "nominal")
}
cat("advanced regression option-axis references generated\n")
generators/r/regression_advanced_core.R
source("common.R", local = TRUE)
orth <- nlme::Orthodont
constructed <- data.frame(
distance = as.double(orth$distance),
age = as.double(orth$age),
subject = as.character(orth$Subject),
sex = as.character(orth$Sex),
time = as.integer(orth$age),
age_sq = as.double(orth$age)^2
)
export_dataset(constructed, "REG_ORTHODONT", "nlme::Orthodont built-in repeated-measures dataset",
"R nlme::Orthodont help page; nlme::lme; plm::plm; stats::lm/step",
"R nlme package dataset; see installed package licensing and documentation.",
c(distance = "Float64", age = "Float64", subject = "String",
sex = "String", time = "Int64", age_sq = "Float64"))
d <- read_dataset("REG_ORTHODONT")
curve_data <- data.frame(x = seq(0, 5, length.out = 30L))
curve_data$y <- 1 + 4 * exp(-0.8 * curve_data$x) + 0.03 * cos(3 * curve_data$x)
export_dataset(curve_data, "REG_DECAY", "Constructed bounded exponential-decay curve",
"R stats::nls help page; Tabelz manual chapter 38 exponential-decay definition",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64"))
curve_data <- read_dataset("REG_DECAY")
run <- generator_run(c("stats", "nlme", "plm", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R advanced-regression reference:", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
emit <- function(id, entry, menu, dataset, config, outputs, notes, kind = "nominal",
deferred = list(), repeat_count = 1L) {
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = deferred, generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = paste("Regression II", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = repeat_count,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R nlme/plm/stats",
citation = "R nlme::Orthodont, nlme::lme, plm::plm, stats::nls/lm/step documentation",
generator = list(path = "generators/r/regression_advanced_core.R", sha256 = ""),
independent = TRUE, convention_notes = c(notes, "R reference re-reads exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
mixed <- nlme::lme(distance ~ age, random = ~1 | subject, data = d,
method = "REML", control = nlme::lmeControl(opt = "optim"))
emit("STAT-REG-MIXED-REML-001", "tabelz.executor.mixed_models",
"mixedModelsMenuItem", "REG_ORTHODONT",
list(response_column = "distance", group_column = "subject",
fixed_numeric_columns = list("age"), estimation = "reml"),
list("/coefficients/1/estimate" = tol(nlme::fixef(mixed)[["age"]], "REML age slope"),
"/analyzed_count" = exact(nrow(d), "complete repeated-measures rows")),
list("R nlme random intercept; REML is the manual default. No fallback estimator."))
mixed_ml <- nlme::lme(distance ~ age, random = ~1 | subject, data = d,
method = "ML", control = nlme::lmeControl(opt = "optim"))
emit("STAT-REG-MIXED-ML-001", "tabelz.executor.mixed_models",
"mixedModelsMenuItem", "REG_ORTHODONT",
list(response_column = "distance", group_column = "subject",
fixed_numeric_columns = list("age"), estimation = "ml"),
list("/coefficients/1/estimate" = tol(nlme::fixef(mixed_ml)[["age"]], "ML age slope")),
list("R nlme maximum likelihood selected explicitly; compare only fixed slope."),
kind = "nominal")
random_slope <- nlme::lme(distance ~ age, random = ~age | subject, data = d,
method = "REML", control = nlme::lmeControl(opt = "optim"))
emit("STAT-REG-MIXED-STRUCT-SLOPE-001", "tabelz.executor.mixed_model_structures",
"mixedModelStructuresMenuItem", "REG_ORTHODONT",
list(response_column = "distance", fixed_numeric_columns = list("age"),
model_mode = "random_slope", group_column = "subject",
random_slope_column = "age", estimation = "reml"),
list("/coefficients/1/estimate" = tol(nlme::fixef(random_slope)[["age"]],
"random-slope fixed age effect")),
list("R nlme correlated random intercept and age slope per subject, REML."))
panel <- plm::plm(distance ~ age, data = d, index = c("subject", "time"),
model = "within", effect = "individual")
emit("STAT-REG-PANEL-001", "tabelz.executor.panel_fixed_effects",
"panelFixedEffectsMenuItem", "REG_ORTHODONT",
list(response_column = "distance", predictor_columns = list("age"),
entity_column = "subject", time_column = "time"),
list("/coefficients/0/estimate" = tol(coef(panel)[["age"]], "within-entity age slope", "1e-9"),
"/observations" = exact(nrow(d), "balanced complete panel rows")),
list("R plm within estimator uses entity demeaning; slope does not depend on covariance correction."))
decay <- stats::nls(y ~ c + a * exp(-k * x), data = curve_data,
start = list(a = 4, k = 0.8, c = 1),
algorithm = "port", lower = c(a = 0, k = 0.01, c = 0),
upper = c(a = 10, k = 3, c = 5),
control = stats::nls.control(maxiter = 1000, tol = 1e-12))
emit("STAT-REG-NONLINEAR-DECAY-001", "tabelz.executor.nonlinear_regression",
"nonlinearRegressionMenuItem", "REG_DECAY",
list(x_column = "x", response = "y", curve = "exponential_decay",
parameters = list(list(name = "a", start = 4, lower = 0, upper = 10),
list(name = "k", start = 0.8, lower = 0.01, upper = 3),
list(name = "c", start = 1, lower = 0, upper = 5))),
list("/artifact/estimates/0" = tol(coef(decay)[["a"]], "decay amplitude"),
"/artifact/estimates/1" = tol(coef(decay)[["k"]], "decay rate")),
list("R nls port bounds match the manual's strictly positive rate and original-response-scale loss."))
selection_data <- d
selection_fit <- stats::lm(distance ~ age, data = selection_data)
emit("STAT-REG-SELECTION-001", "tabelz.analysis.model_selection:compute_model_selection",
"modelSelectionMenuItem", "REG_ORTHODONT",
list(response = "distance", candidate_predictors = list("age")),
list("/coefficients/1/coefficient" = tol(coef(selection_fit)[["age"]],
"single candidate selected slope", "1e-9"),
"/n" = exact(nrow(selection_data), "all complete rows")),
list("With one strong numeric candidate, R step and best-subset search both select age; refit is R lm."))
# This API has no explicit row split. Split-dependent coefficients are deferred;
# independent evidence covers its complete-population arithmetic and repeatability.
emit("STAT-REG-VALIDATED-COUNT-001",
"tabelz.analysis.predictive_validation:compute_validated_regression",
"validatedRegressionMenuItem", "REG_ORTHODONT",
list(response = "distance", numeric_predictors = list("age"),
algorithm = "ols", imputation = "drop", test_fraction = 0.25, seed = 0),
list("/source_count" = exact(nrow(d), "complete source population"),
"/training_metrics/row_count" = exact(nrow(d) - floor(nrow(d) * 0.25),
"training share after 25 percent holdout"),
"/test_metrics/row_count" = exact(floor(nrow(d) * 0.25),
"test share after 25 percent holdout")),
list("R checks population and deterministic split sizes only; Python NumPy RNG assigns row identities."),
kind = "determinism",
deferred = list(list(path = "/recipe/coefficients",
reason = "No explicit split assignment exists; R cannot reproduce NumPy's row permutation.")),
repeat_count = 2L)
cat("advanced regression core references generated\n")
generators/r/regression_advanced_hard.R
source("common.R", local = TRUE)
orth <- read_dataset("REG_ORTHODONT")
orth$age <- orth$age + 10000
orth$distance <- orth$distance + 1000
export_dataset(orth, "REG_ADV_HARD_ORTH", "Large-offset Orthodont repeated-measures design",
"R nlme::Orthodont, nlme::lme, plm::plm, stats::lm",
"Derived from R nlme::Orthodont; retain its attribution.",
c(distance = "Float64", age = "Float64", subject = "String",
sex = "String", time = "Int64", age_sq = "Float64"))
orth <- read_dataset("REG_ADV_HARD_ORTH")
decay <- read_dataset("REG_DECAY")
decay$y <- decay$y + 10000
export_dataset(decay, "REG_ADV_HARD_DECAY", "Large-baseline exponential-decay curve",
"R stats::nls; Tabelz nonlinear curve definition",
"Constructed reference data; no third-party dataset license.",
c(x = "Float64", y = "Float64"))
decay <- read_dataset("REG_ADV_HARD_DECAY")
da <- read_dataset("REG_DISCRIMINANT")
da$x <- da$x + 1000000
export_dataset(da, "REG_ADV_HARD_DA", "Separated classes near a million-unit predictor offset",
"R MASS::lda; Tabelz discriminant scoring manual",
"Constructed reference data; no third-party dataset license.",
c(x = "Float64", class = "String"))
da <- read_dataset("REG_ADV_HARD_DA")
pls_data <- read_dataset("REG_PLS")
pls_data$x <- pls_data$x + 10000000
export_dataset(pls_data, "REG_ADV_HARD_PLS", "One-predictor PLS with a ten-million-unit offset",
"R pls::plsr; Tabelz PLS scoring manual",
"Constructed reference data; no third-party dataset license.",
c(x = "Float64", y = "Float64"))
pls_data <- read_dataset("REG_ADV_HARD_PLS")
run <- generator_run(c("stats", "nlme", "plm", "MASS", "pls", "jsonlite"))
prior <- function(id) jsonlite::fromJSON(file.path(ref_root(), "cases", "regression",
paste0(id, ".json")), simplifyVector = FALSE)
tol <- function(x, note, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = note)
exact <- function(x, note) list(comparison = "exact", value = x, rationale = note)
emit <- function(id, base, dataset, config, outputs, note) {
source <- prior(base)
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(schema_version = "2.0.0", case_id = id,
version = "1.0.0", family = "regression", entry_point = source$entry_point,
menu_items = source$menu_items, title = paste("Regression II large-offset numeric", id),
case_kind = "hard_numeric",
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R nlme/plm/stats/MASS/pls",
citation = "R nlme::lme, plm::plm, stats::lm/nls, MASS::lda, pls::plsr documentation",
generator = list(path = "generators/r/regression_advanced_hard.R", sha256 = ""),
independent = TRUE, convention_notes = c(note, "R re-reads exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-NUMERIC-001"),
review_reference = NULL))
}
mixed <- nlme::lme(distance ~ age, random = ~1 | subject, data = orth,
method = "REML", control = nlme::lmeControl(opt = "optim"))
emit("STAT-REG-HARD-ADV-MIXED-001", "STAT-REG-MIXED-REML-001", "REG_ADV_HARD_ORTH",
list(response_column = "distance", group_column = "subject",
fixed_numeric_columns = list("age"), estimation = "reml"),
list("/coefficients/1/estimate" = tol(nlme::fixef(mixed)[["age"]],
"REML fixed slope after large offsets")),
"Both response and age are translated; fixed slope should remain stable.")
panel <- plm::plm(distance ~ age, data = orth, index = c("subject", "time"),
model = "within", effect = "individual")
emit("STAT-REG-HARD-ADV-PANEL-001", "STAT-REG-PANEL-001", "REG_ADV_HARD_ORTH",
list(response_column = "distance", predictor_columns = list("age"),
entity_column = "subject", time_column = "time"),
list("/coefficients/0/estimate" = tol(coef(panel)[["age"]],
"within slope after large offsets", "1e-9")),
"Entity demeaning should cancel large age/response offsets before OLS.")
ols <- stats::lm(distance ~ age, data = orth)
emit("STAT-REG-HARD-ADV-SELECTION-001", "STAT-REG-SELECTION-001",
"REG_ADV_HARD_ORTH", list(response = "distance",
candidate_predictors = list("age")),
list("/coefficients/1/coefficient" = tol(coef(ols)[["age"]],
"large-offset OLS slope", "1e-9")),
"One strong candidate is retained; R lm is the independent refit.")
nls_fit <- stats::nls(y ~ c + a * exp(-k * x), data = decay,
start = list(a = 4, k = 0.8, c = 10001), algorithm = "port",
lower = c(a = 0, k = 0.01, c = 9990),
upper = c(a = 10, k = 3, c = 10010),
control = stats::nls.control(maxiter = 1000, tol = 1e-12))
emit("STAT-REG-HARD-ADV-NONLINEAR-001", "STAT-REG-NONLINEAR-DECAY-001",
"REG_ADV_HARD_DECAY",
list(x_column = "x", response = "y", curve = "exponential_decay",
parameters = list(list(name = "a", start = 4, lower = 0, upper = 10),
list(name = "k", start = 0.8, lower = 0.01, upper = 3),
list(name = "c", start = 10001, lower = 9990, upper = 10010))),
list("/artifact/estimates/1" = tol(coef(nls_fit)[["k"]],
"decay rate with ten-thousand-unit baseline")),
"R nls port estimates rate on the original response scale with a large fitted c.")
lda <- MASS::lda(class ~ x, data = da, prior = c(0.5, 0.5))
base_da <- prior("STAT-REG-DISCRIMINANT-SCORE-LDA-001")$configuration
base_da$artifact$class_means <- list(list(as.double(lda$means[1L, 1L])),
list(as.double(lda$means[2L, 1L])))
posterior <- stats::predict(lda, newdata = da)$posterior
emit("STAT-REG-HARD-ADV-DA-SCORE-001", "STAT-REG-DISCRIMINANT-SCORE-LDA-001",
"REG_ADV_HARD_DA", base_da,
list("/0/probabilities/0" = tol(posterior[1L, 1L],
"LDA posterior under million-unit offset", "1e-9"),
"/12/probabilities/0" = tol(posterior[13L, 1L],
"opposite-class LDA posterior", "1e-9")),
"LDA class means carry the same million-unit translation as the CSV; pooled covariance is unchanged.")
pls_fit <- pls::plsr(y ~ x, data = pls_data, ncomp = 1L, validation = "none", scale = FALSE)
pls_cfg <- prior("STAT-REG-PLS-SCORE-001")$configuration
pls_cfg$artifact$x_means <- list(mean(pls_data$x))
pls_cfg$artifact$coefficients <- list(as.double(stats::coef(pls_fit, ncomp = 1L))[1L])
pls_cfg$artifact$intercept <- mean(pls_data$y)
pls_pred <- as.double(stats::predict(pls_fit, newdata = pls_data, ncomp = 1L))
emit("STAT-REG-HARD-ADV-PLS-SCORE-001", "STAT-REG-PLS-SCORE-001",
"REG_ADV_HARD_PLS", pls_cfg,
list("/0/predicted" = tol(pls_pred[[1L]],
"PLS prediction after ten-million-unit x offset", "1e-9"),
"/29/predicted" = tol(pls_pred[[30L]],
"last PLS prediction after large offset", "1e-9")),
"R pls::plsr centers the large predictor before extracting the original-unit coefficient.")
cat("advanced regression large-offset references generated\n")
generators/r/regression_advanced_hard_closure.R
source("common.R",local=TRUE)
run <- generator_run(c("stats","rpart","jsonlite"))
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
score <- data.frame(x=c(1e8-1,1e8,1e8+1),y=c(1e6-0.125,1e6-0.125,1e6+0.125))
export_dataset(score,"REG_TREE_SCORE_HARD","Large-offset predictor and leaf values",
"R rpart unique threshold and analytic retained tree scoring",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
score <- read_dataset("REG_TREE_SCORE_HARD")
training <- data.frame(x=rep(c(1e8-1,1e8+1),each=4L),
y=rep(c(1e6-0.125,1e6+0.125),each=4L))
tree <- rpart::rpart(y~x,data=training,method="anova",
control=rpart::rpart.control(cp=0,xval=0,minsplit=2,
minbucket=1,maxdepth=1))
stopifnot(nrow(tree$frame)==3L,abs(tree$splits[1L,"index"]-1e8)<1e-12)
id <- "STAT-REG-TREE-SCORE-REG-HARD-001"
artifact <- list(feature_order=list("x"),children_left=list(1L,-1L,-1L),
children_right=list(2L,-1L,-1L),feature=list(0L,-2L,-2L),
threshold=list(as.double(tree$splits[1L,"index"]),0,0),
value=list(1e6,as.double(tree$frame$yval[2L]),as.double(tree$frame$yval[3L])),
n_node_samples=list(8L,4L,4L))
# Manual chapter 15: numeric predictors use the tree estimator's single-precision
# input convention. Round each score row to IEEE float32 (R's 4-byte writeBin),
# then route it against the stored double threshold. Float32 spacing near 1e8
# is 8, so 1e8-1 and 1e8+1 both become 1e8 and take the left leaf.
float32 <- function(v) readBin(writeBin(as.double(v), raw(), size = 4L), "double",
n = length(v), size = 4L)
routed <- ifelse(float32(score$x) <= as.double(tree$splits[1L,"index"]),
tree$frame$yval[2L], tree$frame$yval[3L])
stopifnot(all(float32(score$x) == 1e8))
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/0/tree_score"=tol(routed[1L],"1e8-1 rounds to float32 1e8: left leaf"),
"/1/tree_score"=tol(routed[2L],"Threshold-equal row goes left"),
"/2/tree_score"=tol(routed[3L],
"1e8+1 rounds to float32 1e8: left leaf (until 2026-09-25 wrongly expected right)")),
expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point="operation:tabelz.regression_tree_scores",
menu_items=list("regressionTreeMenuItem"),title="Large-offset retained tree scoring",
case_kind="hard_numeric",dataset=list(path="reference_data/datasets/REG_TREE_SCORE_HARD.json",sha256=""),
excluded_rows=list(),configuration=list(artifact=artifact,
row_ids=as.list(paste0(id,":",0:2))),"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R rpart and analytic tree traversal",
citation="R rpart::rpart documentation and Tabelz retained tree scoring contract",
generator=list(path="generators/r/regression_advanced_hard_closure.R",sha256=""),
independent=TRUE,convention_notes=list("R rpart on eight pure training rows yields a unique split at 1e8.",
"Leaf responses are 1e6 plus/minus 0.125; stored artifact uses the R leaf means.",
"Manual chapter 15: score rows are rounded to IEEE float32 before routing, as the fit was.",
"R re-reads the exported scoring CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-NUMERIC-001"),review_reference=NULL))
cat("advanced regression tree-scoring hard-numeric reference generated\n")
generators/r/regression_advanced_invalid.R
source("common.R", local = TRUE)
orth <- read_dataset("REG_ORTHODONT")
tree <- read_dataset("REG_TREE")
one <- data.frame(distance = orth$distance[1L], age = orth$age[1L],
subject = orth$subject[1L], sex = orth$sex[1L],
time = orth$time[1L], age_sq = orth$age_sq[1L],
x = tree$x[1L], y = tree$y[1L], class = tree$class[1L])
export_dataset(one, "REG_ADV_ONE", "One complete row for model-minimum checks",
"Tabelz manual model design conditions; R stats model minimum ranks",
"Rows derive partly from nlme::Orthodont; retain that R dataset's attribution.",
c(distance = "Float64", age = "Float64", subject = "String",
sex = "String", time = "Int64", age_sq = "Float64",
x = "Float64", y = "Float64", class = "String"))
one <- read_dataset("REG_ADV_ONE")
stopifnot(nrow(one) == 1L)
two <- orth[1:2, , drop = FALSE]
export_dataset(two, "REG_ADV_TWO", "Two Orthodont rows with varying age and distance",
"R nlme::Orthodont help page; model-selection residual degrees of freedom",
"Derived from nlme::Orthodont; retain that R dataset's attribution.",
c(distance = "Float64", age = "Float64", subject = "String",
sex = "String", time = "Int64", age_sq = "Float64"))
two <- read_dataset("REG_ADV_TWO")
stopifnot(nrow(two) == 2L, length(unique(two$age)) == 2L)
run <- generator_run(c("stats", "jsonlite"))
case_root <- file.path(ref_root(), "cases", "regression")
prior <- function(id) jsonlite::fromJSON(file.path(case_root, paste0(id, ".json")),
simplifyVector = FALSE)
emit <- function(id, base, config, dataset, message, note,
error_class = "ValueError", kind = "invalid_input") {
case <- prior(base)
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = structure(list(), names = character()),
expected_error = list(error_class = error_class, message_contains = message),
deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(schema_version = "2.0.0", case_id = id,
version = "1.0.0", family = "regression", entry_point = case$entry_point,
menu_items = case$menu_items, title = paste("Regression II invalid design", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "analytic", source = "Manual input/design condition",
citation = "Tabelz manual chapters for mixed, panel, nonlinear, validated and tree regression",
generator = list(path = "generators/r/regression_advanced_invalid.R", sha256 = ""),
independent = TRUE, convention_notes = c(note, "Input is from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
bases <- c(
"STAT-REG-MIXED-REML-001", "STAT-REG-MIXED-STRUCT-SLOPE-001",
"STAT-REG-PANEL-001", "STAT-REG-NONLINEAR-DECAY-001",
"STAT-REG-VALIDATED-COUNT-001", "STAT-REG-TREE-CLASS-001",
"STAT-REG-TREE-REG-001", "STAT-REG-DISCRIMINANT-LDA-001",
"STAT-REG-PLS-FIT-001", "STAT-REG-SELECTION-001",
"STAT-REG-DISCRIMINANT-SCORE-LDA-001", "STAT-REG-PLS-SCORE-001",
"STAT-REG-TREE-SCORE-CLASS-001", "STAT-REG-TREE-SCORE-REG-001"
)
labels <- c("MIXED", "MIXED-STRUCT", "PANEL", "NONLINEAR", "VALIDATED",
"TREE-CLASS", "TREE-REG", "DISCRIMINANT", "PLS", "SELECTION",
"DISCRIMINANT-SCORE", "PLS-SCORE", "TREE-SCORE-CLASS", "TREE-SCORE-REG")
for (i in seq_along(bases)) {
case <- prior(bases[[i]])
config <- case$configuration
config$bad_setting <- 1L
emit(paste0("STAT-REG-INVALID-ADV-", labels[[i]], "-CONFIG-001"), bases[[i]],
config, basename(sub("\\.json$", "", case$dataset$path)),
"bad_setting", "An unknown setting must not be silently ignored.")
}
# The engine analyses require a fitted population; a one-row source cannot
# identify their requested models. The scoring routes have different input
# conditions and receive separate invalid artifacts below.
fit_indices <- seq_len(10L)
for (i in fit_indices) {
case <- prior(bases[[i]])
config <- case$configuration
if (identical(labels[[i]], "TREE-CLASS")) config$split_assignment <- NULL
wrapped <- labels[[i]] %in% c("MIXED", "MIXED-STRUCT", "PANEL", "NONLINEAR",
"TREE-CLASS", "TREE-REG")
message <- if (identical(labels[[i]], "SELECTION")) "More complete rows" else
if (identical(labels[[i]], "NONLINEAR")) "eligible rows" else
if (identical(labels[[i]], "PANEL")) "singletons" else
if (labels[[i]] %in% c("MIXED", "MIXED-STRUCT")) "observations" else "row"
emit(paste0("STAT-REG-INVALID-ADV-", labels[[i]], "-MIN-001"), bases[[i]],
config, if (identical(labels[[i]], "SELECTION")) "REG_ADV_TWO" else "REG_ADV_ONE",
message,
"One complete row has insufficient independent observations for the model.",
error_class = if (wrapped) "RuntimeError" else "ValueError")
}
lda <- prior("STAT-REG-DISCRIMINANT-SCORE-LDA-001")$configuration
lda$artifact$predictors <- list("absent_predictor")
emit("STAT-REG-INVALID-ADV-DISCRIMINANT-SCORE-ROLE-001",
"STAT-REG-DISCRIMINANT-SCORE-LDA-001", lda, "REG_DISCRIMINANT",
"Unknown scoring predictor", "Stored artifact requires a predictor absent from the table.")
pls <- prior("STAT-REG-PLS-SCORE-001")$configuration
pls$artifact$predictors <- list("absent_predictor")
emit("STAT-REG-INVALID-ADV-PLS-SCORE-ROLE-001", "STAT-REG-PLS-SCORE-001",
pls, "REG_PLS", "Unknown scoring predictor",
"Stored artifact requires a predictor absent from the table.")
class_op <- prior("STAT-REG-TREE-SCORE-CLASS-001")$configuration
class_op$score_name <- "__tabelz_reserved"
emit("STAT-REG-INVALID-ADV-TREE-SCORE-CLASS-ROLE-001",
"STAT-REG-TREE-SCORE-CLASS-001", class_op, "REG_TREE", "score-column",
"Reserved internal score-column names are not user columns.")
reg_op <- prior("STAT-REG-TREE-SCORE-REG-001")$configuration
reg_op$score_name <- "__tabelz_reserved"
emit("STAT-REG-INVALID-ADV-TREE-SCORE-REG-ROLE-001",
"STAT-REG-TREE-SCORE-REG-001", reg_op, "REG_TREE", "score-column",
"Reserved internal score-column names are not user columns.")
cat("advanced regression invalid references generated\n")
generators/r/regression_advanced_minimum_a.R
source("common.R",local=TRUE)
run <- generator_run(c("stats","plm","rpart","jsonlite"))
tol <- function(x,note,tier="1e-9") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R small-sample reference:",note))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,notes,deferred=list()) {
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=deferred,generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point=entry,menu_items=list(menu),title=paste("Small-sample boundary",id),
case_kind="boundary",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats, plm, rpart",
citation="R stats::nls/lm, plm::plm, rpart::rpart documentation",
generator=list(path="generators/r/regression_advanced_minimum_a.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
panel <- data.frame(y=c(1,3,2,5),x=c(1,2,1,2),entity=c("A","A","B","B"),
time=c(1L,2L,1L,2L))
export_dataset(panel,"REG_MIN_PANEL","Two entities each observed twice",
"R plm within-estimator small-panel boundary",
"Constructed validation data; no third-party license.",
c(y="Float64",x="Float64",entity="String",time="Int64"))
p <- read_dataset("REG_MIN_PANEL")
fit <- plm::plm(y~x,data=p,index=c("entity","time"),model="within",effect="individual")
emit("STAT-REG-PANEL-MIN-001","tabelz.executor.panel_fixed_effects","panelFixedEffectsMenuItem",
"REG_MIN_PANEL",list(response_column="y",predictor_columns=list("x"),
entity_column="entity",time_column="time"),
list("/coefficients/0/estimate"=tol(coef(fit)[["x"]],"within slope for two two-period entities"),
"/observations"=exact(nrow(p),"all four panel rows")),
c("Two retained entities have two time points each; plm removes entity means."))
curve <- data.frame(x=c(0,1,2,3))
curve$y <- 1+4*exp(-0.8*curve$x)
export_dataset(curve,"REG_MIN_DECAY","Four exact exponential-decay observations",
"R nls port three-parameter small-sample boundary",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
c <- read_dataset("REG_MIN_DECAY")
fit <- stats::nls(y~c0+a*exp(-k*x),data=c,start=list(a=4,k=0.8,c0=1),
algorithm="port",lower=c(a=0,k=0.01,c0=0),
upper=c(a=10,k=3,c0=5),
control=stats::nls.control(maxiter=1000,tol=1e-12))
emit("STAT-REG-NONLINEAR-MIN-001","tabelz.executor.nonlinear_regression",
"nonlinearRegressionMenuItem","REG_MIN_DECAY",
list(x_column="x",response="y",curve="exponential_decay",
parameters=list(list(name="a",start=4,lower=0,upper=10),
list(name="k",start=0.8,lower=0.01,upper=3),
list(name="c",start=1,lower=0,upper=5))),
list("/artifact/estimates/0"=tol(coef(fit)[["a"]],"exact decay amplitude","1e-6"),
"/eligible_count"=exact(nrow(c),"all four fit rows")),
c("Four observations identify a three-parameter exponential-decay curve; R port uses matching bounds."))
sel <- data.frame(x=c(1,2,3,4,5),y=c(2.1,4.0,6.2,8.1,10.0))
export_dataset(sel,"REG_MIN_SELECTION","Five-row one-predictor selection design",
"R lm and step AIC small-sample boundary",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
s <- read_dataset("REG_MIN_SELECTION")
fit <- stats::lm(y~x,data=s)
emit("STAT-REG-SELECTION-MIN-001","tabelz.analysis.model_selection:compute_model_selection",
"modelSelectionMenuItem","REG_MIN_SELECTION",
list(response="y",candidate_predictors=list("x")),
list("/coefficients/1/coefficient"=tol(coef(fit)[["x"]],"single selected OLS slope"),
"/n"=exact(nrow(s),"five selected rows")),
c("One strong candidate gives the unique one-term AIC choice."))
tree <- data.frame(x=rep(c(-1,1),each=4L),class=rep(c("A","B"),each=4L),
y=rep(c(-2,2),each=4L))
export_dataset(tree,"REG_MIN_TREE","Eight rows in two pure CART classes",
"R rpart unique-split classification and regression tree",
"Constructed validation data; no third-party license.",
c(x="Float64",class="String",y="Float64"))
t <- read_dataset("REG_MIN_TREE")
train <- c(1L,2L,3L,5L,6L,7L)
fit <- rpart::rpart(class~x,data=t[train,],method="class",
control=rpart::rpart.control(cp=0,xval=0,minsplit=2,
minbucket=1,maxdepth=1))
id <- "STAT-REG-TREE-CLASS-MIN-001"
make_ids <- function(idx) as.list(sort(paste0(id,":",idx-1L)))
emit(id,"tabelz.executor.classification_tree","classificationTreeMenuItem",
"REG_MIN_TREE",
list(response="class",numeric_predictors=list("x"),positive_class="B",cv_folds=2L,
max_depth=1L,min_samples_leaf=1L,
split_assignment=list(train_row_ids=make_ids(train),
test_row_ids=make_ids(c(4L,8L)),test_fraction=0.25,seed=0L)),
list("/artifact/threshold/0"=tol(fit$splits[1L,"index"],"unique split at zero"),
"/source_count"=exact(nrow(t),"eight complete rows")),
c("Six explicit training rows contain three per class; two held-out rows contain one per class.",
"rpart uses cp=0, xval=0, minsplit=2, minbucket=1, maxdepth=1."))
fit <- rpart::rpart(y~x,data=t,method="anova",
control=rpart::rpart.control(cp=0,xval=0,minsplit=2,
minbucket=1,maxdepth=1))
emit("STAT-REG-TREE-REG-MIN-001","tabelz.executor.regression_tree",
"regressionTreeMenuItem","REG_MIN_TREE",
list(response="y",numeric_predictors=list("x"),cv_folds=2L,max_depth=1L,
depth_grid=list(1L),min_leaf_grid=list(1L)),
list("/source_count"=exact(nrow(t),"eight complete rows")),
c("R full-sample split is uniquely at zero, but the engine's split and CV folds use NumPy RNG."),
deferred=list(list(path="/artifact/threshold/0",
reason="The regression-tree API does not expose an explicit split assignment.")))
cat("advanced regression small-sample group A generated\n")
generators/r/regression_advanced_minimum_b.R
source("common.R",local=TRUE)
orth <- read_dataset("REG_ORTHODONT")
run <- generator_run(c("stats","nlme","MASS","pls","jsonlite"))
tol <- function(x,note,tier="1e-6") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R small-sample reference:",note))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,notes,deferred=list()) {
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=deferred,generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point=entry,menu_items=list(menu),title=paste("Small-sample boundary",id),
case_kind="boundary",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R nlme/MASS/pls/stats",
citation="R nlme::lme, MASS::lda, pls::plsr and stats::lm documentation",
generator=list(path="generators/r/regression_advanced_minimum_b.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
types <- c(distance="Float64",age="Float64",subject="String",sex="String",
time="Int64",age_sq="Float64")
four <- orth[c(1L,2L,5L,6L),]
export_dataset(four,"REG_MIN_MIXED","Two repeated subjects with two observations each",
"R nlme random-intercept small-sample boundary",
"Derived from R nlme::Orthodont; retain attribution.",types)
m <- read_dataset("REG_MIN_MIXED")
fit <- nlme::lme(distance~age,random=~1|subject,data=m,method="REML",
control=nlme::lmeControl(opt="optim"))
emit("STAT-REG-MIXED-MIN-001","tabelz.executor.mixed_models","mixedModelsMenuItem",
"REG_MIN_MIXED",
list(response_column="distance",group_column="subject",
fixed_numeric_columns=list("age"),estimation="reml"),
list("/coefficients/1/estimate"=list(comparison = "tolerance", value = "0", absolute_tolerance = "1e-12", relative_tolerance = "0",
rationale = paste("Exactly zero by the design's symmetry; any implementation returns rounding",
"noise of order 1e-15, so an absolute 1e-12 replaces a relative tier that",
"cannot compare with zero. R gives", format(nlme::fixef(fit)[["age"]], digits = 17))),
"/analyzed_count"=exact(nrow(m),"four analyzed rows")),
c("Two groups have two complete observations each, meeting the random-intercept minimum."))
six <- orth[c(1:3,5:7),]
export_dataset(six,"REG_MIN_SLOPE","Two repeated subjects with three observations each",
"R nlme random-slope minimum-group-size boundary",
"Derived from R nlme::Orthodont; retain attribution.",types)
m <- read_dataset("REG_MIN_SLOPE")
fit <- nlme::lme(distance~age,random=~age|subject,data=m,method="REML",
control=nlme::lmeControl(opt="optim"))
emit("STAT-REG-STRUCT-MIN-001","tabelz.executor.mixed_model_structures",
"mixedModelStructuresMenuItem","REG_MIN_SLOPE",
list(response_column="distance",fixed_numeric_columns=list("age"),
model_mode="random_slope",group_column="subject",random_slope_column="age",
estimation="reml"),
list("/coefficients/1/estimate"=tol(nlme::fixef(fit)[["age"]],"random-slope fixed age effect")),
c("Two groups each have three complete rows, the declared random-slope minimum."))
v <- read_dataset("REG_MIN_SELECTION")
emit("STAT-REG-VALIDATED-MIN-001",
"tabelz.analysis.predictive_validation:compute_validated_regression",
"validatedRegressionMenuItem","REG_MIN_SELECTION",
list(response="y",numeric_predictors=list("x"),algorithm="ols",
imputation="drop",test_fraction=0.2,seed=0L),
list("/source_count"=exact(nrow(v),"five complete rows"),
"/training_metrics/row_count"=exact(4L,"four training rows"),
"/test_metrics/row_count"=exact(1L,"one held-out row")),
c("R checks population and split sizes; NumPy determines the held-out identity."),
deferred=list(list(path="/recipe/coefficients",
reason="No explicit row split exists to replay the NumPy permutation in R.")))
da <- read_dataset("REG_DISCRIMINANT")[c(1L,5L,9L,13L,17L,21L),]
export_dataset(da,"REG_MIN_DA","Two classes with three distinct predictor values each",
"R MASS lda small stratified-split boundary",
"Constructed validation data; no third-party license.",
c(x="Float64",class="String"))
da <- read_dataset("REG_MIN_DA")
fit <- MASS::lda(class~x,data=da,prior=c(0.5,0.5))
emit("STAT-REG-DA-MIN-001",
"tabelz.analysis.discriminant_analysis:compute_discriminant_analysis",
"discriminantAnalysisMenuItem","REG_MIN_DA",
list(response="class",predictors=list("x"),model="lda",standardize=FALSE,
prior_mode="equal",test_fraction=0.25,seed=0L),
list("/source_count"=exact(nrow(da),"six usable rows"),
"/class_order/0"=list(comparison="exact",value="A",rationale="R factor class order")),
c("Three rows per class allow one stratified holdout and two training values per class.",
"R lda fits the full CSV; split-dependent fitted moments are deferred."),
deferred=list(list(path="/artifact/class_means",
reason="The training split uses a NumPy stratified permutation not replayable in R.")))
pls_data <- read_dataset("REG_PLS")[seq_len(8L),]
export_dataset(pls_data,"REG_MIN_PLS","Eight-row one-predictor PLS design",
"R pls plsr small training/CV boundary",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
pls_data <- read_dataset("REG_MIN_PLS")
fit <- pls::plsr(y~x,data=pls_data,ncomp=1L,validation="none",scale=FALSE)
emit("STAT-REG-PLS-MIN-001",
"tabelz.analysis.partial_least_squares:compute_partial_least_squares",
"partialLeastSquaresMenuItem","REG_MIN_PLS",
list(response="y",predictors=list("x"),scale=FALSE,cv_folds=2L,
test_fraction=0.25,seed=0L),
list("/source_count"=exact(nrow(pls_data),"eight usable PLS rows")),
c("Two-fold CV has six training rows; R full-sample plsr confirms one-component estimability.",
"NumPy holdout and fold assignments are not reproduced by R."),
deferred=list(list(path="/artifact/coefficients",
reason="NumPy split and CV folds cannot be independently replayed in R.")))
cat("advanced regression small-sample group B generated\n")
generators/r/regression_advanced_population.R
source("common.R", local = TRUE)
make_missing <- function(source_id, target_id, changes, dtypes, citation) {
data <- read_dataset(source_id)
for (change in changes) data[[change[[1L]]]][change[[2L]]] <- NA
export_dataset(data, target_id, paste("Missing-value variant of", source_id), citation,
"Derived from the cited source dataset; retain its attribution.", dtypes)
read_dataset(target_id)
}
orth <- make_missing("REG_ORTHODONT", "REG_ORTHODONT_MISSING",
list(list("distance", 1L), list("age", 5L)),
c(distance = "Float64", age = "Float64", subject = "String",
sex = "String", time = "Int64", age_sq = "Float64"),
"R nlme::Orthodont; nlme::lme; plm::plm")
tree <- make_missing("REG_TREE", "REG_TREE_MISSING",
list(list("class", 1L), list("x", 2L), list("y", 3L)),
c(x = "Float64", class = "String", y = "Float64"),
"Constructed REG_TREE; R rpart::rpart")
da <- make_missing("REG_DISCRIMINANT", "REG_DISCRIMINANT_MISSING",
list(list("class", 1L), list("x", 2L)),
c(x = "Float64", class = "String"),
"Constructed REG_DISCRIMINANT; R MASS::lda")
pls_data <- make_missing("REG_PLS", "REG_PLS_MISSING",
list(list("y", 1L), list("x", 2L)),
c(x = "Float64", y = "Float64"),
"Constructed REG_PLS; R pls::plsr")
decay <- make_missing("REG_DECAY", "REG_DECAY_MISSING",
list(list("y", 1L), list("x", 2L)),
c(x = "Float64", y = "Float64"),
"Constructed REG_DECAY; R stats::nls")
run <- generator_run(c("stats", "nlme", "plm", "rpart", "MASS", "pls", "jsonlite"))
tol <- function(x, note, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = note)
exact <- function(x, note) list(comparison = "exact", value = x, rationale = note)
absent <- function(note) list(comparison = "absent", rationale = note)
emit <- function(id, base, dataset, config, outputs, notes,
kind = "missing_excluded", excluded = list(), deferred = list()) {
prior <- jsonlite::fromJSON(file.path(ref_root(), "cases", "regression",
paste0(base, ".json")), simplifyVector = FALSE)
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = deferred,
generator_run = run))
write_case("regression", id, list(schema_version = "2.0.0", case_id = id,
version = "1.0.0", family = "regression", entry_point = prior$entry_point,
menu_items = prior$menu_items, title = paste("Regression II population", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R nlme/plm/rpart/MASS/pls",
citation = "R nlme::lme, plm::plm, rpart::rpart, MASS::lda, pls::plsr, stats::nls/lm",
generator = list(path = "generators/r/regression_advanced_population.R", sha256 = ""),
independent = TRUE, convention_notes = c(notes, "R re-reads the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
orth_keep <- orth[-9L, , drop = FALSE]
orth_fit <- orth_keep[stats::complete.cases(orth_keep[c("distance", "age", "subject")]),,
drop = FALSE]
random_intercept <- nlme::lme(distance ~ age, random = ~1 | subject, data = orth_fit,
method = "REML", control = nlme::lmeControl(opt = "optim"))
emit("STAT-REG-POP-ADV-MIXED-001", "STAT-REG-MIXED-REML-001",
"REG_ORTHODONT_MISSING", list(response_column = "distance", group_column = "subject",
fixed_numeric_columns = list("age"), estimation = "reml"),
list("/coefficients/1/estimate" = tol(nlme::fixef(random_intercept)[["age"]],
"R lme REML after omissions"),
"/analyzed_count" = exact(as.integer(nrow(orth_fit)), "complete nonexcluded rows"),
"/excluded_count" = exact(1L, "explicitly excluded source row")),
list("Drop missing distance/age in different subjects, then source row 9; REML."),
excluded = list(8L))
# lme4 with tight tolerances: nlme with opt = "optim" stopped 1.2e-6 below the REML
# maximum here, leaving the age effect 3.1e-6 relative off (discrepancy of 2026-09-25).
random_slope <- lme4::lmer(distance ~ age + (age | subject), data = orth_fit, REML = TRUE,
control = lme4::lmerControl(
optCtrl = list(ftol_abs = 1e-15, xtol_abs = 1e-15),
check.conv.singular = "ignore"))
emit("STAT-REG-POP-ADV-STRUCT-001", "STAT-REG-MIXED-STRUCT-SLOPE-001",
"REG_ORTHODONT_MISSING",
list(response_column = "distance", fixed_numeric_columns = list("age"),
model_mode = "random_slope", group_column = "subject",
random_slope_column = "age", estimation = "reml"),
list("/coefficients/1/estimate" = tol(lme4::fixef(random_slope)[["age"]],
"R random-slope age effect (lme4, converged)"),
"/analyzed_count" = exact(as.integer(nrow(orth_fit)), "complete nonexcluded rows")),
list("Subject random slope plus intercept; omit original rows 1, 5, 9."),
excluded = list(8L))
panel <- plm::plm(distance ~ age, data = orth_fit,
index = c("subject", "time"), model = "within", effect = "individual")
emit("STAT-REG-POP-ADV-PANEL-001", "STAT-REG-PANEL-001",
"REG_ORTHODONT_MISSING",
list(response_column = "distance", predictor_columns = list("age"),
entity_column = "subject", time_column = "time"),
list("/coefficients/0/estimate" = tol(coef(panel)[["age"]],
"R plm within slope", "1e-9"),
"/excluded_count" = exact(1L, "explicitly excluded source row")),
list("R plm within estimator on complete nonexcluded rows."),
excluded = list(8L))
decay_keep <- decay[-3L, , drop = FALSE]
decay_fit <- decay_keep[stats::complete.cases(decay_keep[c("x", "y")]), , drop = FALSE]
nls_fit <- stats::nls(y ~ c + a * exp(-k * x), data = decay_fit,
start = list(a = 4, k = 0.8, c = 1), algorithm = "port",
lower = c(a = 0, k = 0.01, c = 0),
upper = c(a = 10, k = 3, c = 5),
control = stats::nls.control(maxiter = 1000, tol = 1e-12))
emit("STAT-REG-POP-ADV-NONLINEAR-001", "STAT-REG-NONLINEAR-DECAY-001",
"REG_DECAY_MISSING",
list(x_column = "x", response = "y", curve = "exponential_decay",
parameters = list(list(name = "a", start = 4, lower = 0, upper = 10),
list(name = "k", start = 0.8, lower = 0.01, upper = 3),
list(name = "c", start = 1, lower = 0, upper = 5))),
list("/artifact/estimates/0" = tol(coef(nls_fit)[["a"]], "R nls amplitude"),
"/artifact/estimates/1" = tol(coef(nls_fit)[["k"]], "R nls rate")),
list("R nls port on complete nonexcluded rows; missing rows 1/2, excluded row 3."),
excluded = list(2L))
tree_class <- tree[-4L, , drop = FALSE]
tree_class_fit <- tree_class[stats::complete.cases(tree_class[c("x", "class")]),,
drop = FALSE]
rpart_class <- rpart::rpart(class ~ x, data = tree_class_fit, method = "class",
control = rpart::rpart.control(cp = 0, xval = 0,
minsplit = 2, minbucket = 1,
maxdepth = 1))
stopifnot(nrow(rpart_class$frame) == 3L)
emit("STAT-REG-POP-ADV-TREE-CLASS-001", "STAT-REG-TREE-CLASS-001",
"REG_TREE_MISSING", list(response = "class", numeric_predictors = list("x"),
positive_class = "B", cv_folds = 2L,
max_depth = 1L, min_samples_leaf = 1L),
list("/artifact/threshold/0" = tol(rpart_class$splits[1L, "index"],
"unique split despite omissions", "1e-12"),
"/source_count" = exact(as.integer(nrow(tree)), "source row count"),
"/excluded_count" = exact(1L, "excluded row"),
"/missing_response_count" = exact(1L, "missing class label"),
"/incomplete_predictor_count" = exact(1L, "missing numeric predictor")),
list("Omit source class row 1, x row 2 and excluded row 4; leaves remain pure."),
excluded = list(3L))
tree_reg <- tree[-4L, , drop = FALSE]
tree_reg_fit <- tree_reg[stats::complete.cases(tree_reg[c("x", "y")]),, drop = FALSE]
rpart_reg <- rpart::rpart(y ~ x, data = tree_reg_fit, method = "anova",
control = rpart::rpart.control(cp = 0, xval = 0,
minsplit = 2, minbucket = 1,
maxdepth = 1))
stopifnot(nrow(rpart_reg$frame) == 3L)
emit("STAT-REG-POP-ADV-TREE-REG-001", "STAT-REG-TREE-REG-001",
"REG_TREE_MISSING", list(response = "y", numeric_predictors = list("x"),
cv_folds = 2L, max_depth = 1L,
depth_grid = list(1L), min_leaf_grid = list(1L)),
list("/artifact/threshold/0" = tol(rpart_reg$splits[1L, "index"],
"unique regression split", "1e-12"),
"/source_count" = exact(as.integer(nrow(tree)), "source row count"),
"/excluded_count" = exact(1L, "excluded row"),
"/missing_response_count" = exact(1L, "missing numeric response"),
"/incomplete_numeric_count" = exact(1L, "missing numeric predictor")),
list("Omit source x row 2, y row 3 and excluded row 4; leaves remain pure."),
excluded = list(3L))
da_config <- list(response = "class", predictors = list("x"), model = "lda",
imputation = "drop", standardize = FALSE, prior_mode = "equal")
emit("STAT-REG-POP-ADV-DA-001", "STAT-REG-DISCRIMINANT-LDA-001",
"REG_DISCRIMINANT_MISSING", da_config,
list("/source_count" = exact(as.integer(nrow(da)), "source class rows"),
"/excluded_count" = exact(1L, "excluded row"),
"/missing_response_count" = exact(1L, "missing class"),
"/incomplete_predictor_count" = exact(1L, "missing x")),
list("Class and predictor missing in different rows; original row 3 excluded."),
excluded = list(2L),
deferred = list(list(path = "/artifact/class_means",
reason = "NumPy stratified train split cannot be reproduced with R RNG.")))
emit("STAT-REG-POP-ADV-PLS-001", "STAT-REG-PLS-FIT-001",
"REG_PLS_MISSING", list(response = "y", predictors = list("x"),
imputation = "drop", scale = FALSE, cv_folds = 3L),
list("/source_count" = exact(as.integer(nrow(pls_data)), "source PLS rows"),
"/excluded_count" = exact(1L, "excluded row"),
"/missing_response_count" = exact(1L, "missing response"),
"/incomplete_predictor_count" = exact(1L, "missing predictor")),
list("R complete cases after original row 3 exclusion; split moments deferred."),
excluded = list(2L),
deferred = list(list(path = "/artifact/coefficients",
reason = "NumPy split and selected component count cannot be reproduced with R RNG.")))
emit("STAT-REG-POP-ADV-VALIDATED-001", "STAT-REG-VALIDATED-COUNT-001",
"REG_PLS_MISSING", list(response = "y", numeric_predictors = list("x"),
algorithm = "ols", imputation = "drop", cv_folds = 3L),
list("/source_count" = exact(as.integer(nrow(pls_data)), "source regression rows"),
"/excluded_count" = exact(1L, "excluded row"),
"/missing_response_count" = exact(1L, "missing response"),
"/incomplete_numeric_count" = exact(1L, "missing predictor")),
list("Complete-case OLS after excluding original row 3; split coefficients deferred."),
excluded = list(2L),
deferred = list(list(path = "/recipe/coefficients",
reason = "No explicit split assignment; NumPy chooses training identities.")))
selection_clean <- orth_fit
selection_ref <- stats::lm(distance ~ age, data = selection_clean)
emit("STAT-REG-POP-ADV-SELECTION-001", "STAT-REG-SELECTION-001",
"REG_ORTHODONT_MISSING", list(response = "distance",
candidate_predictors = list("age")),
list("/coefficients/1/coefficient" = tol(coef(selection_ref)[["age"]],
"complete-case OLS slope", "1e-9"),
"/n" = exact(as.integer(nrow(selection_clean)), "complete nonexcluded rows"),
"/dropped" = exact(2L, "two missing rows after exclusion")),
list("R lm on complete rows after original row 9 is excluded; one predictor wins."),
excluded = list(8L))
cat("advanced regression missing/exclusion references generated\n")
generators/r/regression_advanced_remaining_axes.R
source("common.R", local = TRUE)
crossed <- read_dataset("REG_CROSSED")
nested <- crossed
nested$ab <- paste(nested$a, nested$b, sep = ":")
nested <- nested[c("a", "ab", "t", "x", "y")]
export_dataset(nested, "REG_ADV_NESTED", "Constructed nested grouping hierarchy",
"R lme4::lmer nested random effects; Tabelz mixed structures manual",
"Constructed from REG_CROSSED; no third-party dataset license.",
c(a = "String", ab = "String", t = "Int64", x = "Float64", y = "Float64"))
nested <- read_dataset("REG_ADV_NESTED")
tree <- read_dataset("REG_TREE")
da <- read_dataset("REG_DISCRIMINANT_MISSING")
pls_data <- read_dataset("REG_PLS_MISSING")
run <- generator_run(c("stats", "lme4", "rpart", "MASS", "pls", "jsonlite"))
prior <- function(id) jsonlite::fromJSON(file.path(ref_root(), "cases", "regression",
paste0(id, ".json")), simplifyVector = FALSE)
exact <- function(x, note) list(comparison = "exact", value = x, rationale = note)
tol <- function(x, note, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = note)
absent <- function(note) list(comparison = "absent", rationale = note)
emit <- function(id, base, dataset, config, outputs, note,
kind = "axis_coverage", deferred = list(), repeat_count = 1L) {
source <- prior(base)
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = deferred,
generator_run = run))
write_case("regression", id, list(schema_version = "2.0.0", case_id = id,
version = "1.0.0", family = "regression", entry_point = source$entry_point,
menu_items = source$menu_items, title = paste("Regression II remaining axis", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = repeat_count,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R lme4/rpart/MASS/pls",
citation = "R lme4::lmer, rpart::rpart, MASS::lda, pls::plsr documentation",
generator = list(path = "generators/r/regression_advanced_remaining_axes.R", sha256 = ""),
independent = TRUE, convention_notes = c(note, "R re-reads exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
nested_fit <- lme4::lmer(y ~ x + (1 | a) + (1 | a:ab), data = nested, REML = TRUE,
control = lme4::lmerControl(optimizer = "bobyqa",
optCtrl = list(maxfun = 100000L)))
emit("STAT-REG-AXIS-ADV-STRUCT-NESTED-001", "STAT-REG-MIXED-STRUCT-SLOPE-001",
"REG_ADV_NESTED", list(response_column = "y", fixed_numeric_columns = list("x"),
model_mode = "variance_components", estimation = "reml",
max_rows = 1000L,
components = list(list(name = "a", level_column = "a",
relation = "crossed"),
list(name = "ab", level_column = "ab",
relation = "nested", parent_column = "a"))),
list("/coefficients/1/estimate" = tol(lme4::fixef(nested_fit)[["x"]],
"nested fixed slope"),
"/analyzed_count" = exact(as.integer(nrow(nested)), "all nested observations")),
"R lmer a and a:ab random intercepts; ab labels are unique within parent a.")
class_cfg <- prior("STAT-REG-TREE-CLASS-001")$configuration
class_cfg$class_weight <- "balanced"
class_cfg$split_assignment <- NULL
emit("STAT-REG-AXIS-ADV-TREE-BALANCED-001", "STAT-REG-TREE-CLASS-001",
"REG_TREE", class_cfg,
list("/artifact/threshold/0" = tol(0, "unambiguous class split", "1e-12"),
"/source_count" = exact(as.integer(nrow(tree)), "complete source rows")),
"Balanced weights on an exactly balanced two-class table preserve the pure split.",
kind = "determinism", repeat_count = 2L)
reg_cfg <- prior("STAT-REG-TREE-REG-001")$configuration
reg_cfg$seed <- 7L
emit("STAT-REG-AXIS-ADV-TREE-REG-SEED-001", "STAT-REG-TREE-REG-001",
"REG_TREE", reg_cfg,
list("/artifact/threshold/0" = tol(0, "unambiguous numeric split", "1e-12"),
"/source_count" = exact(as.integer(nrow(tree)), "complete source rows")),
"Seed changes the holdout, not the pure two-leaf split; repeat verifies replay.",
kind = "determinism", repeat_count = 2L)
for (model in c("lda", "qda")) {
id <- paste0("STAT-REG-AXIS-ADV-DA-MEAN-", toupper(model), "-001")
config <- list(response = "class", predictors = list("x"), model = model,
imputation = "mean", standardize = FALSE, prior_mode = "equal",
cv_folds = NULL)
config$cv_folds <- NULL
emit(id, "STAT-REG-DISCRIMINANT-LDA-001", "REG_DISCRIMINANT_MISSING",
config,
list("/source_count" = exact(as.integer(nrow(da)), "all source rows"),
"/missing_response_count" = exact(1L, "one missing class"),
"/incomplete_predictor_count" = exact(0L, "mean imputation retains predictor row")),
"R class means and pooled covariance are fitted after mean filling on training rows; NumPy split moments deferred.",
kind = "determinism", repeat_count = 2L,
deferred = list(list(path = "/artifact/class_means",
reason = "NumPy stratified split determines training imputation mean.")))
}
emit("STAT-REG-AXIS-ADV-PLS-MEAN-001", "STAT-REG-PLS-FIT-001",
"REG_PLS_MISSING", list(response = "y", predictors = list("x"),
imputation = "mean", scale = FALSE, cv_folds = 3L),
list("/source_count" = exact(as.integer(nrow(pls_data)), "source rows"),
"/missing_response_count" = exact(1L, "one missing y"),
"/incomplete_predictor_count" = exact(0L, "mean-filled x retained")),
"PLS mean filling uses training-only mean; random train split prevents R coefficient oracle.",
kind = "determinism", repeat_count = 2L,
deferred = list(list(path = "/artifact/coefficients",
reason = "NumPy training split/CV folds are not reproducible with R RNG.")))
for (imputation in c("mean", "knn")) {
id <- paste0("STAT-REG-AXIS-ADV-VALIDATED-", toupper(imputation), "-001")
emit(id, "STAT-REG-VALIDATED-COUNT-001", "REG_PLS_MISSING",
list(response = "y", numeric_predictors = list("x"), algorithm = "ols",
imputation = imputation, cv_folds = 3L, seed = 2L),
list("/source_count" = exact(as.integer(nrow(pls_data)), "all source rows"),
"/missing_response_count" = exact(1L, "one missing response"),
"/incomplete_numeric_count" = exact(0L, "imputation retains missing x")),
"R verifies population arithmetic; imputer is trained after NumPy split.",
kind = "determinism", repeat_count = 2L,
deferred = list(list(path = "/recipe/coefficients",
reason = "NumPy split and training-fitted imputation cannot be reproduced in R.")))
}
lda_score <- prior("STAT-REG-DISCRIMINANT-SCORE-LDA-001")$configuration
lda_score$artifact$imputation <- "mean"
lda_score$artifact$imputation_values <- list(0)
filled_x <- da$x
filled_x[is.na(filled_x)] <- 0
lda_ref <- MASS::lda(class ~ x, data = read_dataset("REG_DISCRIMINANT"),
prior = c(0.5, 0.5))
lda_pred <- stats::predict(lda_ref, newdata = data.frame(x = filled_x))
emit("STAT-REG-AXIS-ADV-DA-SCORE-MEAN-001", "STAT-REG-DISCRIMINANT-SCORE-LDA-001",
"REG_DISCRIMINANT_MISSING", lda_score,
list("/1/probabilities/0" = tol(lda_pred$posterior[2L, 1L],
"R LDA posterior after mean filling", "1e-9"),
"/1/row_id" = exact("STAT-REG-AXIS-ADV-DA-SCORE-MEAN-001:1",
"missing-x source row keeps identity")),
"Stored full-population LDA artifact uses zero training mean; the 50/50 class tie has no unique label.")
pls_score <- prior("STAT-REG-PLS-SCORE-001")$configuration
pls_score$artifact$imputation <- "mean"
pls_score$artifact$imputation_values <- list(pls_score$artifact$x_means[[1L]])
expected_mean <- as.double(pls_score$artifact$intercept)
emit("STAT-REG-AXIS-ADV-PLS-SCORE-MEAN-001", "STAT-REG-PLS-SCORE-001",
"REG_PLS_MISSING", pls_score,
list("/1/predicted" = tol(expected_mean,
"centered PLS prediction at training mean", "1e-9"),
"/1/row_id" = exact("STAT-REG-AXIS-ADV-PLS-SCORE-MEAN-001:1",
"missing-x source row keeps identity")),
"Mean imputation inserts stored training x_mean; centered PLS prediction equals intercept.")
cat("advanced regression remaining axes generated\n")
generators/r/regression_advanced_rows.R
source("common.R", local = TRUE)
tree <- read_dataset("REG_TREE")
da <- read_dataset("REG_DISCRIMINANT")
pls_data <- read_dataset("REG_PLS")
run <- generator_run(c("stats", "rpart", "MASS", "pls", "jsonlite"))
case_root <- file.path(ref_root(), "cases", "regression")
prior <- function(id) jsonlite::fromJSON(file.path(case_root, paste0(id, ".json")),
simplifyVector = FALSE)
exact <- function(x, note) list(comparison = "exact", value = x, rationale = note)
tol <- function(x, note) list(comparison = "tolerance", value = dec(unname(x)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = note)
absent <- function(note) list(comparison = "absent", rationale = note)
emit <- function(id, base, dataset, config, outputs, note, excluded = list()) {
source <- prior(base)
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(schema_version = "2.0.0", case_id = id,
version = "1.0.0", family = "regression", entry_point = source$entry_point,
menu_items = source$menu_items, title = paste("Regression II complete row alignment", id),
case_kind = "row_alignment",
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R rpart/MASS/pls",
citation = "R rpart::rpart, MASS::lda/qda, pls::plsr documentation",
generator = list(path = "generators/r/regression_advanced_rows.R", sha256 = ""),
independent = TRUE, convention_notes = c(note, "R re-reads exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
row_ids <- function(id, n) as.list(paste0(id, ":", seq_len(n) - 1L))
class_id <- "STAT-REG-ROWS-ADV-TREE-CLASS-001"
class_source <- prior("STAT-REG-TREE-SCORE-CLASS-001")
class_config <- class_source$configuration
class_config$row_ids <- row_ids(class_id, nrow(tree))
class_outputs <- list()
for (i in seq_len(nrow(tree))) {
path <- paste0("/", i - 1L)
class_outputs[[paste0(path, "/__tabelz_row_id")]] <- exact(
class_config$row_ids[[i]], "original ordered row identity")
class_outputs[[paste0(path, "/tree_score")]] <- if (i == 2L)
absent("active excluded row has no score") else
exact(tree$class[[i]], "R rpart pure two-leaf class")
}
emit(class_id, "STAT-REG-TREE-SCORE-CLASS-001", "REG_TREE", class_config,
class_outputs, "All 40 ordered row IDs and class scores; source row 2 excluded.",
excluded = list(1L))
reg_id <- "STAT-REG-ROWS-ADV-TREE-REG-001"
reg_source <- prior("STAT-REG-TREE-SCORE-REG-001")
reg_config <- reg_source$configuration
reg_config$row_ids <- row_ids(reg_id, nrow(tree) - 1L)
reg_outputs <- list()
for (i in seq_len(nrow(tree))) {
path <- paste0("/", i - 1L)
reg_outputs[[paste0(path, "/__tabelz_row_id")]] <- exact(
paste0(reg_id, ":", i - 1L), "original ordered row identity")
reg_outputs[[paste0(path, "/tree_score")]] <- if (i == nrow(tree))
absent("outside fitted row population") else
tol(tree$y[[i]], "R rpart pure two-leaf fitted value")
}
emit(reg_id, "STAT-REG-TREE-SCORE-REG-001", "REG_TREE", reg_config,
reg_outputs, "All 40 ordered row IDs and predictions; final row outside artifact population.")
for (model in c("lda", "qda")) {
id <- paste0("STAT-REG-ROWS-ADV-DA-", toupper(model), "-001")
base <- paste0("STAT-REG-DISCRIMINANT-SCORE-", toupper(model), "-001")
config <- prior(base)$configuration
reference <- if (model == "lda") MASS::lda(class ~ x, data = da, prior = c(0.5, 0.5)) else
MASS::qda(class ~ x, data = da, prior = c(0.5, 0.5))
predicted <- stats::predict(reference, newdata = da)
outputs <- list()
for (i in seq_len(nrow(da))) {
path <- paste0("/", i - 1L)
outputs[[paste0(path, "/row_id")]] <- exact(paste0(id, ":", i - 1L),
"original ordered row identity")
outputs[[paste0(path, "/predicted")]] <- exact(as.character(predicted$class[[i]]),
"R MASS discriminant class")
outputs[[paste0(path, "/probabilities/0")]] <- tol(predicted$posterior[i, 1L],
"R MASS posterior for class A")
}
emit(id, base, "REG_DISCRIMINANT", config, outputs,
paste("MASS", model, "whole-table scoring, including ordered probabilities."))
}
pls_id <- "STAT-REG-ROWS-ADV-PLS-001"
pls_config <- prior("STAT-REG-PLS-SCORE-001")$configuration
pls_fit <- pls::plsr(y ~ x, data = pls_data, ncomp = 1L, validation = "none", scale = FALSE)
predicted <- as.double(stats::predict(pls_fit, newdata = pls_data, ncomp = 1L))
pls_outputs <- list()
for (i in seq_len(nrow(pls_data))) {
path <- paste0("/", i - 1L)
pls_outputs[[paste0(path, "/row_id")]] <- exact(paste0(pls_id, ":", i - 1L),
"original ordered row identity")
pls_outputs[[paste0(path, "/predicted")]] <- tol(predicted[[i]],
"R one-component PLS fitted value")
}
emit(pls_id, "STAT-REG-PLS-SCORE-001", "REG_PLS", pls_config, pls_outputs,
"R pls::plsr predicts every row from the exported table in source order.")
cat("advanced regression full-row references generated\n")
generators/r/regression_advanced_score_boundaries.R
source("common.R",local=TRUE)
prior <- function(id) jsonlite::fromJSON(file.path(ref_root(),"cases","regression",
paste0(id,".json")),simplifyVector=FALSE)
tree <- data.frame(x=c(0,NA_real_,1),y=c(0,NA_real_,2),class=c("A","A","B"))
export_dataset(tree,"REG_SCORE_BOUND_TREE","Threshold-equal and missing tree-scoring predictors",
"Analytic retained CART threshold and missing-score convention",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64",class="String"))
tree <- read_dataset("REG_SCORE_BOUND_TREE")
da <- data.frame(x=c(0,NA_real_,1),class=c("A","A","B"))
export_dataset(da,"REG_SCORE_BOUND_DA","LDA equal-posterior midpoint and missing predictor",
"R MASS lda posterior midpoint; analytic missing-score convention",
"Constructed validation data; no third-party license.",
c(x="Float64",class="String"))
da <- read_dataset("REG_SCORE_BOUND_DA")
base <- read_dataset("REG_PLS")
center <- mean(base$x)
pls_data <- data.frame(x=c(center,NA_real_,1),y=c(0,0,0))
export_dataset(pls_data,"REG_SCORE_BOUND_PLS","PLS training-mean predictor and missing predictor",
"R pls centered-intercept identity; analytic missing-score convention",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
pls_data <- read_dataset("REG_SCORE_BOUND_PLS")
run <- generator_run(c("stats","MASS","pls","jsonlite"))
exact <- function(x,note) list(comparison="exact",value=x,rationale=note)
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
absent <- function(note) list(comparison="absent",rationale=note)
emit <- function(id,base,dataset,config,outputs,notes) {
original <- prior(base)
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point=original$entry_point,menu_items=original$menu_items,
title=paste("Retained scoring boundary",id),case_kind="boundary",
dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R MASS/pls and analytic retained tree",
citation="R MASS::lda, pls::plsr and rpart::rpart documentation; Tabelz retained-artifact contract",
generator=list(path="generators/r/regression_advanced_score_boundaries.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
row_ids <- function(id) as.list(paste0(id,":",0:2))
for (suffix in c("THRESHOLD","MISSING")) {
id <- paste0("STAT-REG-TREE-SCORE-CLASS-",suffix,"-001")
cfg <- prior("STAT-REG-TREE-SCORE-CLASS-001")$configuration
cfg$row_ids <- row_ids(id)
outputs <- if(suffix=="THRESHOLD")
list("/0/tree_score"=exact("A","At stored threshold zero, the <= branch leads left")) else
list("/1/tree_score"=absent("Missing split predictor cannot be scored"))
emit(id,"STAT-REG-TREE-SCORE-CLASS-001","REG_SCORE_BOUND_TREE",cfg,outputs,
c("Stored two-leaf CART artifact has threshold zero and pure A/B leaves.",
"No tree is refitted for scoring."))
id <- paste0("STAT-REG-TREE-SCORE-REG-",suffix,"-001")
cfg <- prior("STAT-REG-TREE-SCORE-REG-001")$configuration
cfg$row_ids <- row_ids(id)
outputs <- if(suffix=="THRESHOLD")
list("/0/tree_score"=tol(-2,"At stored threshold zero, the <= branch leads left")) else
list("/1/tree_score"=absent("Missing split predictor cannot be scored"))
emit(id,"STAT-REG-TREE-SCORE-REG-001","REG_SCORE_BOUND_TREE",cfg,outputs,
c("Stored two-leaf regression tree has threshold zero and leaf means -2 and 2.",
"No tree is refitted for scoring."))
}
lda <- MASS::lda(class~x,data=read_dataset("REG_DISCRIMINANT"),prior=c(0.5,0.5))
posterior <- stats::predict(lda,newdata=data.frame(x=0))$posterior[1L,1L]
for (suffix in c("MIDPOINT","MISSING")) {
id <- paste0("STAT-REG-DA-SCORE-",suffix,"-001")
cfg <- prior("STAT-REG-DISCRIMINANT-SCORE-LDA-001")$configuration
outputs <- if(suffix=="MIDPOINT")
list("/0/probabilities/0"=tol(posterior,"R MASS LDA equal-prior midpoint posterior")) else
list("/1/predicted"=absent("Drop-imputation leaves missing predictor unscored"))
emit(id,"STAT-REG-DISCRIMINANT-SCORE-LDA-001","REG_SCORE_BOUND_DA",cfg,outputs,
c("The stored class means are -2 and 2 with equal priors; x=0 has equal posterior mass.",
"The missing row is not imputed under drop mode."))
}
pls_cfg <- prior("STAT-REG-PLS-SCORE-001")$configuration
for (suffix in c("CENTER","MISSING")) {
id <- paste0("STAT-REG-PLS-SCORE-",suffix,"-001")
outputs <- if(suffix=="CENTER")
list("/0/predicted"=tol(pls_cfg$artifact$intercept,"At x_mean, centered PLS prediction equals intercept")) else
list("/1/predicted"=absent("Drop-imputation leaves missing predictor unscored"))
emit(id,"STAT-REG-PLS-SCORE-001","REG_SCORE_BOUND_PLS",pls_cfg,outputs,
c("The retained one-component PLS artifact applies intercept + coefficient*(x-x_mean).",
"The missing row is not imputed under drop mode."))
}
cat("advanced regression scoring boundaries generated\n")
generators/r/regression_advanced_score_corrections.R
source("common.R",local=TRUE)
prior <- function(id) jsonlite::fromJSON(file.path(ref_root(),"cases","regression",
paste0(id,".json")),simplifyVector=FALSE)
da <- data.frame(x=0,class="A")
export_dataset(da,"REG_SCORE_MID_DA","Fully observed LDA equal-posterior midpoint",
"R MASS lda midpoint posterior",
"Constructed validation data; no third-party license.",
c(x="Float64",class="String"))
da <- read_dataset("REG_SCORE_MID_DA")
center <- mean(read_dataset("REG_PLS")$x)
pls_data <- data.frame(x=center,y=0)
export_dataset(pls_data,"REG_SCORE_CENTER_PLS","Fully observed PLS training-mean predictor",
"R pls centered-intercept identity",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
pls_data <- read_dataset("REG_SCORE_CENTER_PLS")
run <- generator_run(c("stats","MASS","pls","jsonlite"))
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
emit <- function(id,base,dataset,outputs,error,note) {
original <- prior(base)
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=error,deferred_outputs=list(),generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point=original$entry_point,menu_items=original$menu_items,
title=paste("Corrected retained scoring boundary",id),case_kind="boundary",
dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=original$configuration,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class=if(is.null(error)) "independent_implementation" else "analytic",
source="R MASS/pls or strict scoring contract",
citation="R MASS::lda, pls::plsr and Tabelz manual retained-scoring semantics",
generator=list(path="generators/r/regression_advanced_score_corrections.R",sha256=""),
independent=TRUE,convention_notes=list(note,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
empty <- structure(list(),names=character())
lda <- MASS::lda(class~x,data=read_dataset("REG_DISCRIMINANT"),prior=c(0.5,0.5))
posterior <- stats::predict(lda,newdata=da)$posterior[1L,1L]
emit("STAT-REG-DA-SCORE-MIDPOINT-CORRECTED-001",
"STAT-REG-DISCRIMINANT-SCORE-LDA-001","REG_SCORE_MID_DA",
list("/0/probabilities/0"=tol(posterior,"R MASS LDA equal-prior midpoint posterior")),
NULL,"A one-row, fully observed scoring table lets the midpoint posterior be evaluated.")
# Statistician ruling, issue #11: drop-row retained scoring is row-wise, so the
# former DA-SCORE-MISSING-CORRECTED-001 and PLS-SCORE-MISSING-CORRECTED-001,
# which expected the whole table to be rejected, are retired in favour of the
# original row-wise cases STAT-REG-DA-SCORE-MISSING-001 and -PLS-SCORE-MISSING-001.
pls_cfg <- prior("STAT-REG-PLS-SCORE-001")$configuration
emit("STAT-REG-PLS-SCORE-CENTER-CORRECTED-001",
"STAT-REG-PLS-SCORE-001","REG_SCORE_CENTER_PLS",
list("/0/predicted"=tol(pls_cfg$artifact$intercept,
"At retained x_mean, centered PLS prediction equals intercept")),
NULL,"A one-row, fully observed scoring table tests the retained centered intercept.")
cat("corrected advanced regression scoring boundaries generated\n")
generators/r/regression_advanced_scoring.R
source("common.R", local = TRUE)
tree <- data.frame(x = rep(c(-1, 1), 20L),
class = rep(c("A", "B"), 20L),
y = rep(c(-2, 2), 20L))
export_dataset(tree, "REG_TREE", "Constructed two-leaf CART with a unique split",
"R rpart::rpart manual; Tabelz classification and regression tree manuals",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", class = "String", y = "Float64"))
tree <- read_dataset("REG_TREE")
da <- data.frame(x = rep(c(-3, -2, -1, 1, 2, 3), each = 4L),
class = rep(c("A", "B"), each = 12L))
export_dataset(da, "REG_DISCRIMINANT", "Constructed balanced separated classes",
"R MASS::lda and MASS::qda documentation",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", class = "String"))
da <- read_dataset("REG_DISCRIMINANT")
pls_data <- data.frame(x = as.double(seq_len(30L)))
pls_data$y <- 3 + 2 * pls_data$x + 0.05 * sin(pls_data$x)
export_dataset(pls_data, "REG_PLS", "Constructed one-predictor linear PLS1 design",
"R pls::plsr documentation",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", y = "Float64"))
pls_data <- read_dataset("REG_PLS")
run <- generator_run(c("stats", "rpart", "MASS", "pls", "jsonlite"))
tol <- function(x, note, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = note)
exact <- function(x, note) list(comparison = "exact", value = x, rationale = note)
absent <- function(note) list(comparison = "absent", rationale = note)
emit <- function(id, entry, menu, dataset, config, outputs, notes,
kind = "nominal", excluded = list(), deferred = list(), repeat_count = 1L) {
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = deferred,
generator_run = run))
write_case("regression", id, list(schema_version = "2.0.0", case_id = id,
version = "1.0.0", family = "regression", entry_point = entry,
menu_items = list(menu), title = paste("Regression II", id), case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = repeat_count,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R rpart/MASS/pls",
citation = "R rpart::rpart, MASS::lda/qda, pls::plsr documentation",
generator = list(path = "generators/r/regression_advanced_scoring.R", sha256 = ""),
independent = TRUE, convention_notes = c(notes, "R reference re-reads exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
tree_class <- rpart::rpart(class ~ x, data = tree, method = "class",
control = rpart::rpart.control(cp = 0, xval = 0,
minsplit = 2, minbucket = 1,
maxdepth = 1))
tree_reg <- rpart::rpart(y ~ x, data = tree, method = "anova",
control = rpart::rpart.control(cp = 0, xval = 0,
minsplit = 2, minbucket = 1,
maxdepth = 1))
stopifnot(nrow(tree_class$frame) == 3L, nrow(tree_reg$frame) == 3L,
abs(tree_class$splits[1L, "index"]) < 1e-12,
abs(tree_reg$splits[1L, "index"]) < 1e-12)
tree_id <- function(id, i) paste0(id, ":", i - 1L)
test_positions <- which((seq_len(nrow(tree)) - 1L) %% 8L %in% c(2L, 3L))
train <- sort(vapply(setdiff(seq_len(nrow(tree)), test_positions),
function(i) tree_id("STAT-REG-TREE-CLASS-001", i), ""))
test <- sort(vapply(test_positions,
function(i) tree_id("STAT-REG-TREE-CLASS-001", i), ""))
emit("STAT-REG-TREE-CLASS-001", "tabelz.executor.classification_tree",
"classificationTreeMenuItem", "REG_TREE",
list(response = "class", numeric_predictors = list("x"), positive_class = "B",
cv_folds = 2L, max_depth = 1L, min_samples_leaf = 1L,
split_assignment = list(train_row_ids = as.list(train), test_row_ids = as.list(test),
test_fraction = 0.25, seed = 0L)),
list("/artifact/threshold/0" = tol(tree_class$splits[1L, "index"], "unique CART split", "1e-12"),
"/training_metrics/accuracy" = tol(1, "pure training leaves", "1e-12"),
"/test_metrics/accuracy" = tol(1, "pure held-out leaves", "1e-12"),
"/source_count" = exact(as.integer(nrow(tree)), "complete source rows")),
list("A unique numeric split at zero separates both classes; explicit row split is replayable."),
kind = "hard_numeric")
emit("STAT-REG-TREE-REG-001", "tabelz.executor.regression_tree",
"regressionTreeMenuItem", "REG_TREE",
list(response = "y", numeric_predictors = list("x"), cv_folds = 2L,
max_depth = 1L, depth_grid = list(1L), min_leaf_grid = list(1L)),
list("/artifact/threshold/0" = tol(tree_reg$splits[1L, "index"], "unique CART split", "1e-12"),
"/artifact/value/1" = tol(-2, "left-leaf mean", "1e-12"),
"/artifact/value/2" = tol(2, "right-leaf mean", "1e-12"),
"/source_count" = exact(as.integer(nrow(tree)), "complete source rows")),
list("Response is exactly constant on each side; random holdout cannot change leaf means."),
kind = "hard_numeric", deferred = list(list(path = "/split",
reason = "No explicit split assignment exists for regression trees; NumPy chooses held-out identities.")))
reg_op_id <- "STAT-REG-TREE-SCORE-REG-001"
reg_rows <- as.list(vapply(seq_len(nrow(tree) - 1L), function(i) tree_id(reg_op_id, i), ""))
reg_art <- list(feature_order = list("x"), children_left = list(1L, -1L, -1L),
children_right = list(2L, -1L, -1L), feature = list(0L, -2L, -2L),
threshold = list(0, 0, 0), value = list(0, -2, 2),
n_node_samples = list(40L, 20L, 20L))
emit(reg_op_id, "operation:tabelz.regression_tree_scores", "regressionTreeMenuItem",
"REG_TREE", list(artifact = reg_art, row_ids = reg_rows),
list("/0/tree_score" = tol(-2, "first row follows left leaf", "1e-12"),
"/1/tree_score" = tol(2, "second row follows right leaf", "1e-12"),
"/0/__tabelz_row_id" = exact(tree_id(reg_op_id, 1L), "retained row identity"),
"/39/tree_score" = absent("unfitted final row must receive null")),
list("R rpart two-leaf model stored as the documented immutable numeric artifact."),
kind = "row_alignment")
class_op_id <- "STAT-REG-TREE-SCORE-CLASS-001"
class_rows <- as.list(vapply(seq_len(nrow(tree)), function(i) tree_id(class_op_id, i), ""))
class_art <- list(feature_order = list("x"), class_order = list("A", "B"),
children_left = list(1L, -1L, -1L), children_right = list(2L, -1L, -1L),
feature = list(0L, -2L, -2L), threshold = list(0, 0, 0),
class_probabilities = list(list(0.5, 0.5), list(1, 0), list(0, 1)),
n_node_samples = list(40L, 20L, 20L),
encoding = list(numeric_predictors = list("x"), categorical_predictors = list(),
categorical_levels = list(), feature_names = list("x")))
emit(class_op_id, "operation:tabelz.classification_tree_scores", "classificationTreeMenuItem",
"REG_TREE", list(artifact = class_art, row_ids = class_rows),
list("/0/tree_score" = exact("A", "left class"),
"/1/tree_score" = absent("actively excluded row must receive null"),
"/2/tree_score" = exact("A", "next unexcluded row retains class"),
"/2/__tabelz_row_id" = exact(tree_id(class_op_id, 3L), "stable row identity")),
list("R rpart pure split; active exclusion affects only the selected target row."),
kind = "row_alignment", excluded = list(1L))
lda <- MASS::lda(class ~ x, data = da, prior = c(0.5, 0.5))
qda <- MASS::qda(class ~ x, data = da, prior = c(0.5, 0.5))
score_x <- da$x
pooled_var <- sum(tapply(da$x, da$class, function(x) sum((x - mean(x))^2))) / (nrow(da) - 2L)
class_var <- unname(vapply(split(da$x, da$class), stats::var, 0.0))
lda_art <- list(response = "class", predictors = list("x"), model = "lda",
imputation = "drop", standardize = FALSE, class_order = list("A", "B"),
priors = list(0.5, 0.5), imputation_values = list(NULL),
transform_means = list(0), transform_scales = list(1),
class_means = list(list(-2), list(2)), covariance = list(list(pooled_var)))
qda_art <- lda_art
qda_art$model <- "qda"
qda_art$class_covariances <- lapply(class_var, function(x) list(list(unname(x))))
for (model in c("lda", "qda")) {
fit_id <- paste0("STAT-REG-DISCRIMINANT-", toupper(model), "-001")
emit(fit_id, "tabelz.analysis.discriminant_analysis:compute_discriminant_analysis",
"discriminantAnalysisMenuItem", "REG_DISCRIMINANT",
list(response = "class", predictors = list("x"), model = model,
standardize = FALSE, prior_mode = "equal"),
list("/source_count" = exact(as.integer(nrow(da)), "complete class population"),
"/class_order/0" = exact("A", "alphabetic first class"),
"/class_order/1" = exact("B", "alphabetic second class"),
"/artifact/priors/0" = tol(0.5, "equal prior for A", "1e-12")),
list("MASS lda/qda on full CSV; random split precludes exact fitted moments."),
kind = "determinism", deferred = list(list(path = "/artifact/class_means",
reason = "Training subset depends on NumPy stratified permutation, not R RNG.")),
repeat_count = 2L)
score_id <- paste0("STAT-REG-DISCRIMINANT-SCORE-", toupper(model), "-001")
pred <- if (model == "lda") predict(lda, data.frame(x = score_x)) else
predict(qda, data.frame(x = score_x))
artifact <- if (model == "lda") lda_art else qda_art
emit(score_id, "tabelz.analysis.discriminant_analysis:score_discriminant_table",
"discriminantAnalysisMenuItem", "REG_DISCRIMINANT", list(artifact = artifact),
list("/0/predicted" = exact(as.character(pred$class[1L]), "R discriminant first score"),
"/12/predicted" = exact(as.character(pred$class[13L]), "R discriminant opposite class"),
"/0/row_id" = exact(tree_id(score_id, 1L), "score preserves source row")),
list("Artifact moments computed from full CSV; MASS discriminant predictions are the oracle."),
kind = "row_alignment")
}
pls_fit <- pls::plsr(y ~ x, data = pls_data, ncomp = 1L, validation = "none", scale = FALSE)
pls_slope <- as.double(stats::coef(pls_fit, ncomp = 1L))[1L]
pls_mean_x <- mean(pls_data$x)
pls_mean_y <- mean(pls_data$y)
pls_art <- list(response = "y", predictors = list("x"), imputation = "drop", scale = FALSE,
component_count = 1L, imputation_values = list(NULL),
x_means = list(pls_mean_x), coefficients = list(pls_slope),
intercept = pls_mean_y)
emit("STAT-REG-PLS-FIT-001", "tabelz.analysis.partial_least_squares:compute_partial_least_squares",
"partialLeastSquaresMenuItem", "REG_PLS",
list(response = "y", predictors = list("x"), scale = FALSE, cv_folds = 3L),
list("/source_count" = exact(as.integer(nrow(pls_data)), "complete PLS population"),
"/training_metrics/row_count" = exact(23L, "three-quarter training share")),
list("R pls::plsr reference is full-population; NumPy random split prevents coefficient comparison."),
kind = "determinism", deferred = list(list(path = "/artifact/coefficients",
reason = "PLS training split and cross-validation folds are NumPy-specific.")),
repeat_count = 2L)
pls_pred <- as.double(stats::predict(pls_fit, newdata = pls_data, ncomp = 1L))
pls_score_id <- "STAT-REG-PLS-SCORE-001"
emit(pls_score_id, "tabelz.analysis.partial_least_squares:score_partial_least_squares_table",
"partialLeastSquaresMenuItem", "REG_PLS", list(artifact = pls_art),
list("/0/predicted" = tol(pls_pred[1L], "R pls first-row prediction", "1e-9"),
"/29/predicted" = tol(pls_pred[30L], "R pls last-row prediction", "1e-9"),
"/0/row_id" = exact(tree_id(pls_score_id, 1L), "stable scoring row identity")),
list("One-component PLS1 original-unit coefficient and centered intercept from R pls::plsr."),
kind = "row_alignment")
cat("advanced regression tree and scoring references generated\n")
generators/r/regression_advanced_special_a.R
source("common.R",local=TRUE)
run <- generator_run(c("stats","nlme","plm","rpart","jsonlite"))
tol <- function(x,note,tier="1e-6") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R edge-condition reference:",note))
exact <- function(x,note) list(comparison="exact",value=x,rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,notes,deferred=list()) {
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=deferred,generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point=entry,menu_items=list(menu),title=paste("Regression edge",id),
case_kind="boundary",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R nlme/plm/stats/rpart",
citation="R nlme::lme, plm::plm, stats::nls/lm/step, rpart::rpart documentation",
generator=list(path="generators/r/regression_advanced_special_a.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
orth <- read_dataset("REG_ORTHODONT")
u <- orth[c(1L,2L,5L,6L,7L),]
export_dataset(u,"REG_EDGE_MIXED_UNBALANCED","Two repeated groups of sizes two and three",
"R nlme random-intercept unbalanced minimum-group boundary",
"Derived from R nlme::Orthodont; retain attribution.",
c(distance="Float64",age="Float64",subject="String",sex="String",
time="Int64",age_sq="Float64"))
u <- read_dataset("REG_EDGE_MIXED_UNBALANCED")
# lme4 with tight tolerances: nlme with opt = "optim" stopped 2e-10 below the REML
# maximum here, leaving the slope 3.6e-6 relative off (discrepancy of 2026-09-25).
fit <- lme4::lmer(distance~age+(1|subject),data=u,REML=TRUE,
control=lme4::lmerControl(optCtrl=list(ftol_abs=1e-15,xtol_abs=1e-15),
check.conv.singular="ignore"))
emit("STAT-REG-MIXED-UNBALANCED-001","tabelz.executor.mixed_models","mixedModelsMenuItem",
"REG_EDGE_MIXED_UNBALANCED",
list(response_column="distance",group_column="subject",
fixed_numeric_columns=list("age"),estimation="reml"),
list("/coefficients/1/estimate"=tol(lme4::fixef(fit)[["age"]],"REML fixed age slope (lme4, converged)"),
"/analyzed_count"=exact(as.integer(nrow(u)),"five complete rows")),
c("One group is exactly at the two-observation minimum while the other has three rows."))
p <- data.frame(y=c(1,3,2,5,4),x=c(1,2,1,2,3),
entity=c("A","A","B","B","C"),time=c(1L,2L,1L,2L,1L))
export_dataset(p,"REG_EDGE_PANEL_SINGLETON","Two two-period entities plus one singleton",
"R plm within estimator singleton-entity boundary",
"Constructed validation data; no third-party license.",
c(y="Float64",x="Float64",entity="String",time="Int64"))
p <- read_dataset("REG_EDGE_PANEL_SINGLETON")
fit <- plm::plm(y~x,data=p,index=c("entity","time"),model="within",effect="individual")
emit("STAT-REG-PANEL-SINGLETON-001","tabelz.executor.panel_fixed_effects",
"panelFixedEffectsMenuItem","REG_EDGE_PANEL_SINGLETON",
list(response_column="y",predictor_columns=list("x"),entity_column="entity",
time_column="time"),
list("/coefficients/0/estimate"=tol(coef(fit)[["x"]],"within slope with singleton entity","1e-9"),
"/observations"=exact(as.integer(nrow(p)),"five complete source rows")),
c("Singleton C contributes no within variation; A and B identify the common slope."))
curve <- data.frame(x=seq(0,5,length.out=12L))
curve$y <- 1+10*exp(-0.8*curve$x)
export_dataset(curve,"REG_EDGE_DECAY_BOUND","Exact exponential decay with amplitude at upper bound",
"R nls port bound-active fit",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
c <- read_dataset("REG_EDGE_DECAY_BOUND")
fit <- stats::nls(y~c0+a*exp(-k*x),data=c,
start=list(a=9.5,k=0.8,c0=1),algorithm="port",
lower=c(a=0,k=0.01,c0=0),upper=c(a=10,k=3,c0=5),
control=stats::nls.control(maxiter=1000,tol=1e-12))
emit("STAT-REG-NONLINEAR-BOUND-001","tabelz.executor.nonlinear_regression",
"nonlinearRegressionMenuItem","REG_EDGE_DECAY_BOUND",
list(x_column="x",response="y",curve="exponential_decay",
parameters=list(list(name="a",start=9.5,lower=0,upper=10),
list(name="k",start=0.8,lower=0.01,upper=3),
list(name="c",start=1,lower=0,upper=5))),
list("/artifact/estimates/0"=tol(coef(fit)[["a"]],"bound-active amplitude"),
"/eligible_count"=exact(as.integer(nrow(c)),"all twelve rows eligible")),
c("True amplitude equals upper bound 10; R port and Tabelz use original response scale."))
tree <- data.frame(x=c(-2,-1,0,1,2,3,4,5),y=rep(7,8L))
export_dataset(tree,"REG_EDGE_TREE_CONSTANT","Constant-response regression tree",
"R rpart no-split leaf mean boundary",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
t <- read_dataset("REG_EDGE_TREE_CONSTANT")
fit <- rpart::rpart(y~x,data=t,method="anova",
control=rpart::rpart.control(cp=0,xval=0,minsplit=2,minbucket=1))
emit("STAT-REG-TREE-REG-CONSTANT-001","tabelz.executor.regression_tree",
"regressionTreeMenuItem","REG_EDGE_TREE_CONSTANT",
list(response="y",numeric_predictors=list("x"),cv_folds=2L,max_depth=1L,
depth_grid=list(1L),min_leaf_grid=list(1L)),
list("/source_count"=exact(as.integer(nrow(t)),"all eight constant-response rows"),
"/artifact/value/0"=tol(fit$frame$yval[1L],"constant-response root leaf mean","1e-9")),
c("With zero response variance, R rpart produces one leaf of value seven.",
"The random holdout cannot change the constant training response."))
sel <- read_dataset("REG_MIN_SELECTION")
sel$z <- sel$x
export_dataset(sel,"REG_EDGE_SELECTION_ALIAS","Two exactly aliased candidate predictors",
"R stats lm/step AIC alias boundary",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64",z="Float64"))
s <- read_dataset("REG_EDGE_SELECTION_ALIAS")
fit <- stats::step(stats::lm(y~1,data=s),scope=list(lower=~1,upper=~x+z),
direction="forward",trace=0)
emit("STAT-REG-SELECTION-ALIAS-001",
"tabelz.analysis.model_selection:compute_model_selection",
"modelSelectionMenuItem","REG_EDGE_SELECTION_ALIAS",
list(response="y",candidate_predictors=list("x","z"),direction="forward"),
list("/n"=exact(as.integer(nrow(s)),"five source rows")),
c("x and z are exactly aliased; R step breaks the tie by candidate order.",
"Only population size is asserted until Tabelz and R alias-selection conventions are aligned."),
deferred=list(list(path="/selected",
reason="Exact-alias tie breaking is not specified in the manual.")))
cat("advanced regression special group A generated\n")
generators/r/regression_advanced_special_b.R
source("common.R",local=TRUE)
run <- generator_run(c("stats","plm","jsonlite"))
empty <- structure(list(),names=character())
prior <- function(id) jsonlite::fromJSON(file.path(ref_root(),"cases","regression",
paste0(id,".json")),simplifyVector=FALSE)
emit <- function(id,base,dataset,config,outputs=list(),error=NULL,note="",source="Analytic design condition") {
original <- prior(base)
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=if(is.null(error)) outputs else empty,expected_error=error,
deferred_outputs=list(),generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point=original$entry_point,menu_items=original$menu_items,
title=paste("Regression edge",id),case_kind="boundary",
dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class=if(is.null(error)) "independent_implementation" else "analytic",
source=source,citation="Tabelz manual regression model design conditions; R stats/plm references",
generator=list(path="generators/r/regression_advanced_special_b.R",sha256=""),
independent=TRUE,convention_notes=list(note,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
err <- function(cls,message) list(error_class=cls,message_contains=message)
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-9",rationale=note)
orth <- read_dataset("REG_ORTHODONT")[1:4,]
export_dataset(orth,"REG_EDGE_STRUCT_ONE_GROUP","One repeated group with four observations",
"Analytic random-slope group-count design condition",
"Derived from R nlme::Orthodont; retain attribution.",
c(distance="Float64",age="Float64",subject="String",sex="String",
time="Int64",age_sq="Float64"))
emit("STAT-REG-STRUCT-ONEGROUP-001","STAT-REG-MIXED-STRUCT-SLOPE-001",
"REG_EDGE_STRUCT_ONE_GROUP",
list(response_column="distance",fixed_numeric_columns=list("age"),
model_mode="random_slope",group_column="subject",random_slope_column="age",
estimation="reml"),error=err("RuntimeError","at least two groups"),
note="A random-slope variance cannot be identified from one repeated group.")
v <- read_dataset("REG_MIN_SELECTION")[1:4,]
export_dataset(v,"REG_EDGE_VALIDATED_SMALLTRAIN","Four-row OLS population with two-row holdout",
"Analytic train-rank boundary for one-predictor validated regression",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
emit("STAT-REG-VALIDATED-SMALLTRAIN-001","STAT-REG-VALIDATED-COUNT-001",
"REG_EDGE_VALIDATED_SMALLTRAIN",
list(response="y",numeric_predictors=list("x"),algorithm="ols",
imputation="drop",test_fraction=0.5,seed=0L),
error=err("ValueError","cannot estimate"),
note="Two training rows cannot estimate the one-predictor model plus the required residual degrees of freedom.")
tree <- read_dataset("REG_MIN_TREE")
tree$class <- rep("A",nrow(tree))
export_dataset(tree,"REG_EDGE_TREE_ONECLASS","Constant class response for a classification tree",
"Analytic two-class requirement",
"Constructed validation data; no third-party license.",
c(x="Float64",class="String",y="Float64"))
emit("STAT-REG-TREE-CLASS-ONECLASS-001","STAT-REG-TREE-CLASS-001",
"REG_EDGE_TREE_ONECLASS",
list(response="class",numeric_predictors=list("x"),positive_class="B",
cv_folds=2L,max_depth=1L,min_samples_leaf=1L),
error=err("RuntimeError","at least two classes"),
note="A classification tree requires two observed response classes before splitting.")
da <- read_dataset("REG_MIN_DA")
da$class <- rep("A",nrow(da))
export_dataset(da,"REG_EDGE_DA_ONECLASS","One-class discriminant response",
"Analytic two-class stratified-discriminant requirement",
"Constructed validation data; no third-party license.",
c(x="Float64",class="String"))
emit("STAT-REG-DA-ONECLASS-001","STAT-REG-DISCRIMINANT-LDA-001",
"REG_EDGE_DA_ONECLASS",
list(response="class",predictors=list("x"),model="lda",standardize=FALSE,
prior_mode="equal",test_fraction=0.25,seed=0L),
error=err("ValueError","at least two classes"),
note="LDA requires at least two observed classes; no stratified split exists for one class.")
pls_data <- read_dataset("REG_MIN_PLS")
pls_data$x <- rep(7,nrow(pls_data))
export_dataset(pls_data,"REG_EDGE_PLS_CONSTANT","Zero-variance PLS predictor",
"Analytic positive training-variance requirement",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
emit("STAT-REG-PLS-CONSTANT-001","STAT-REG-PLS-FIT-001",
"REG_EDGE_PLS_CONSTANT",
list(response="y",predictors=list("x"),scale=FALSE,cv_folds=2L,
test_fraction=0.25,seed=0L),
error=err("ValueError","positive training variance"),
note="A constant predictor has zero variance in every training split, so no PLS component exists.")
p <- read_dataset("REG_EDGE_PANEL_SINGLETON")
fit <- plm::plm(y~x,data=p,index=c("entity","time"),model="within",effect="individual")
emit("STAT-REG-PANEL-SINGLETON-CORRECTED-001","STAT-REG-PANEL-001",
"REG_EDGE_PANEL_SINGLETON",
list(response_column="y",predictor_columns=list("x"),entity_column="entity",
time_column="time"),
outputs=list("/coefficients/0/estimate"=tol(coef(fit)[["x"]],"R within slope after singleton removal"),
"/observations"=exact(4L,"two retained two-period entities"),
"/singleton_observation_count"=exact(1L,"one singleton source row")),
note="The manual excludes singleton C from the within fit and reports it separately.",
source="R plm::plm and analytic singleton partition")
cat("advanced regression special group B generated\n")
generators/r/regression_advanced_structure_hard.R
source("common.R",local=TRUE)
d <- read_dataset("REG_ADV_HARD_ORTH")
fit <- nlme::lme(distance~age,random=~age|subject,data=d,method="REML",
control=nlme::lmeControl(opt="optim"))
id <- "STAT-REG-STRUCT-HARD-001"
run <- generator_run(c("nlme","stats","jsonlite"))
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=list("/coefficients/1/estimate"=list(comparison="tolerance",
value=dec(nlme::fixef(fit)[["age"]]),absolute_tolerance="0",
relative_tolerance="1e-6",
rationale="R nlme REML random-slope fixed effect at large age and response offsets")),
expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point="tabelz.executor.mixed_model_structures",
menu_items=list("mixedModelStructuresMenuItem"),title="Large-offset random-slope mixed model",
case_kind="hard_numeric",
dataset=list(path="reference_data/datasets/REG_ADV_HARD_ORTH.json",sha256=""),
excluded_rows=list(),configuration=list(response_column="distance",
fixed_numeric_columns=list("age"),model_mode="random_slope",
group_column="subject",random_slope_column="age",estimation="reml"),"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R nlme::lme",
citation="R nlme::lme REML random-slope documentation",
generator=list(path="generators/r/regression_advanced_structure_hard.R",sha256=""),
independent=TRUE,convention_notes=list("Both age and response have large common offsets; fixed age slope should be translation-invariant.",
"nlme::lme uses REML, random=~age|subject, control opt=optim.",
"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-NUMERIC-001"),review_reference=NULL))
cat("random-slope hard numeric reference generated\n")
generators/r/regression_advanced_true_min.R
source("common.R",local=TRUE)
prior <- function(id) jsonlite::fromJSON(file.path(ref_root(),"cases","regression",
paste0(id,".json")),simplifyVector=FALSE)
run <- generator_run(c("stats","rpart","pls","jsonlite"))
tol <- function(x,note,tier="1e-9") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=note)
exact <- function(x,note) list(comparison="exact",value=x,rationale=note)
emit <- function(id,base,dataset,config,outputs,note,deferred=list()) {
source <- prior(base)
write_expected("regression",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=deferred,generator_run=run))
write_case("regression",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="regression",entry_point=source$entry_point,menu_items=source$menu_items,
title=paste("True minimum sample",id),case_kind="boundary",
dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/regression/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R stats/rpart/pls and analytic sample conditions",
citation="R stats::lm, rpart::rpart, pls::plsr documentation",
generator=list(path="generators/r/regression_advanced_true_min.R",sha256=""),
independent=TRUE,convention_notes=list(note,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-REG-001"),
risk_ids=list("RISK-BOUNDARY-001"),review_reference=NULL))
}
sel <- read_dataset("REG_MIN_SELECTION")[1:3,]
export_dataset(sel,"REG_TRUE_MIN_SELECTION","Three rows for one candidate predictor",
"R lm/step AIC minimal one-predictor model",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
s <- read_dataset("REG_TRUE_MIN_SELECTION")
fit <- stats::lm(y~x,data=s)
emit("STAT-REG-SELECTION-TRUE-MIN-001","STAT-REG-SELECTION-001",
"REG_TRUE_MIN_SELECTION",list(response="y",candidate_predictors=list("x")),
list("/n"=exact(as.integer(nrow(s)),"three rows exceed one predictor plus intercept"),
"/coefficients/1/coefficient"=tol(coef(fit)[["x"]],"three-point OLS slope")),
"Three complete rows are the smallest accepted for one candidate predictor.")
pls_data <- read_dataset("REG_PLS")[1:5,]
export_dataset(pls_data,"REG_TRUE_MIN_PLS","Five-row one-predictor PLS CV design",
"R pls plsr estimability with one component",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
pls_data <- read_dataset("REG_TRUE_MIN_PLS")
fit <- pls::plsr(y~x,data=pls_data,ncomp=1L,validation="none",scale=FALSE)
emit("STAT-REG-PLS-TRUE-MIN-001","STAT-REG-PLS-FIT-001",
"REG_TRUE_MIN_PLS",
list(response="y",predictors=list("x"),scale=FALSE,cv_folds=2L,
test_fraction=0.2,seed=0L),
list("/source_count"=exact(as.integer(nrow(pls_data)),"five analyzed rows")),
"Four training rows leave two rows per two-fold training partition, enough for one component.",
deferred=list(list(path="/artifact/coefficients",
reason="NumPy holdout and CV fold identities are not replayable in R.")))
tree <- data.frame(x=c(-1,-1,-1,1,1,1),class=c("A","A","A","B","B","B"),
y=c(-2,-2,-2,2,2,2))
export_dataset(tree,"REG_TRUE_MIN_CLASS_TREE","Three rows in each pure class",
"R rpart unique two-leaf split at the smallest CV-valid stratified sample",
"Constructed validation data; no third-party license.",
c(x="Float64",class="String",y="Float64"))
t <- read_dataset("REG_TRUE_MIN_CLASS_TREE")
train <- c(1L,2L,4L,5L); test <- c(3L,6L)
fit <- rpart::rpart(class~x,data=t[train,],method="class",
control=rpart::rpart.control(cp=0,xval=0,minsplit=2,
minbucket=1,maxdepth=1))
id <- "STAT-REG-TREE-CLASS-TRUE-MIN-001"
ids <- function(i) as.list(sort(paste0(id,":",i-1L)))
emit(id,"STAT-REG-TREE-CLASS-001","REG_TRUE_MIN_CLASS_TREE",
list(response="class",numeric_predictors=list("x"),positive_class="B",cv_folds=2L,
max_depth=1L,min_samples_leaf=1L,
split_assignment=list(train_row_ids=ids(train),test_row_ids=ids(test),
test_fraction=1/3,seed=0L)),
list("/artifact/threshold/0"=tol(fit$splits[1L,"index"],"unique CART split at zero"),
"/source_count"=exact(as.integer(nrow(t)),"six complete rows")),
"Each class supplies two training rows for two-fold CV and one test row; n=6 is minimal.")
reg <- data.frame(x=c(-1,1,2),y=c(-2,2,3))
export_dataset(reg,"REG_TRUE_MIN_REG_TREE","Three finite regression-tree rows",
"R rpart small-sample regression-tree estimability",
"Constructed validation data; no third-party license.",
c(x="Float64",y="Float64"))
r <- read_dataset("REG_TRUE_MIN_REG_TREE")
emit("STAT-REG-TREE-REG-TRUE-MIN-001","STAT-REG-TREE-REG-001",
"REG_TRUE_MIN_REG_TREE",
list(response="y",numeric_predictors=list("x"),cv_folds=2L,max_depth=1L,
depth_grid=list(1L),min_leaf_grid=list(1L)),
list("/source_count"=exact(as.integer(nrow(r)),"three finite tree rows")),
"One held-out row leaves two training rows for two-fold CV; fitted split depends on NumPy holdout.",
deferred=list(list(path="/artifact/threshold/0",
reason="The API does not expose the regression-tree training split.")))
id <- "STAT-REG-TREE-SCORE-CLASS-TRUE-MIN-001"
config <- prior("STAT-REG-TREE-SCORE-CLASS-001")$configuration
config$row_ids <- list(paste0(id,":0"))
emit(id,"STAT-REG-TREE-SCORE-CLASS-001","REG_SCORE_MID_DA",config,
list("/0/tree_score"=exact("A","One retained x=0 row follows the left leaf")),
"The retained classification-tree operation accepts one row ID; x equal to threshold zero follows <= left.")
cat("advanced regression true-minimum references generated\n")
generators/r/regression_blocks.R
source("common.R", local = TRUE)
# Blockwise (hierarchical) linear regression (Statistics > Correlation & regression >
# Blockwise linear regression). Every reference is lm() on the re-read REG_BLOCKS CSV.
i <- seq_len(24L)
constructed <- data.frame(
x1 = (i - 12) / 5 + sin(1.3 * i) / 3,
x2 = cos(0.7 * i) + (i %% 5) / 4,
x3 = sin(2.1 * i) + (i %% 3) / 2,
x4 = cos(1.9 * i) / 2 + (i %% 4) / 5,
x5 = sin(0.4 * i) - (i %% 6) / 7,
stringsAsFactors = FALSE
)
constructed$y <- 2 + 1.5 * constructed$x1 - 0.8 * constructed$x2 + 1.1 * constructed$x3 +
sin(3.7 * i) / 2 + cos(5.3 * i) / 3
constructed$x3[7] <- NA
constructed <- constructed[c("y", "x1", "x2", "x3", "x4", "x5")]
export_dataset(
constructed, "REG_BLOCKS", "Constructed by regression_blocks.R",
"R stats::lm, anova (nested models), confint, step, AIC, BIC, rstandard, rstudent, hatvalues, cooks.distance, dfbetas",
"Constructed test data; no third-party dataset license.",
c(y = "Float64", x1 = "Float64", x2 = "Float64", x3 = "Float64", x4 = "Float64", x5 = "Float64")
)
d <- read_dataset("REG_BLOCKS")
run <- generator_run(c("stats", "jsonlite"))
cc <- d[stats::complete.cases(d), ]
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R stats", label, "from re-read REG_BLOCKS CSV."))
prob <- function(x, label)
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = "1e-8",
rationale = paste("R stats", label, "from re-read REG_BLOCKS CSV."))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
setof <- function(values, label) list(comparison = "set", values = as.list(values), rationale = label)
entry <- "tabelz.analysis.regression_blocks:compute_regression_blocks"
menu <- "regressionBlocksMenuItem"
notes_common <- c(
"Every model is lm() on the rows complete on y and every block's predictor (listwise over all blocks, 23 of 24 rows).",
"R-squared change F from anova(model k-1, model k); overall F and p from summary.lm; coefficient intervals from confint.",
"Durbin-Watson = sum(diff(e)^2) / sum(e^2) of the residuals in row order; VIF = 1 / (1 - R^2 of each predictor on the others); partial r = cor of the residuals of y and x_j on the other predictors; part r = cor(y, residual of x_j on the others).",
"Collinearity: singular values of the design (intercept included) with each column scaled to unit length, no centering; eigenvalue = singular value squared; condition index = sqrt(max eigenvalue / eigenvalue).",
"Casewise: standardized residual e / s; studentized = rstandard; deleted studentized = rstudent; leverage = hatvalues; Cook's distance = cooks.distance; DFBETAS = dfbetas.",
"docs/manual/chapters/86-regression-and-count-models.tex:9-14: 'fits cumulative blocks on one common complete-case population. ... selection uses Gaussian AIC or BIC, including the error-variance parameter.'",
"docs/manual/chapters/86-regression-and-count-models.tex:16-19: the change statistic F=[dR^2/(p_k-p_{k-1})]/[(1-R_k^2)/(n-p_k)]",
"docs/manual/chapters/86-regression-and-count-models.tex:24-26: 'Collinearity diagnostics include the intercept in an uncentred design with columns scaled to unit length.'"
)
emit <- function(id, title, kind, config, outputs, notes) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs, expected_error = NULL,
deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_BLOCKS.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R 4.6.1 stats::lm and step",
citation = "R Core Team, stats package help: lm, anova.lm, confint.lm, step, AIC, BIC, rstandard, rstudent, hatvalues, cooks.distance, dfbetas; Belsley, Kuh and Welsch (1980), Regression Diagnostics, ch. 3.",
generator = list(path = "generators/r/regression_blocks.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, notes_common, "Expected computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-REGRESSION-BLOCKS-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
fit_of <- function(predictors) {
rhs <- if (length(predictors)) paste(predictors, collapse = " + ") else "1"
stats::lm(stats::as.formula(paste("y ~", rhs)), data = cc)
}
model_outputs <- function(index, predictors, previous, level = 0.95, with_coefficients = TRUE) {
fit <- fit_of(predictors)
s <- summary(fit)
prefix <- paste0("/models/", index)
n <- nrow(cc)
p <- length(predictors) + 1L
outputs <- list()
outputs[[paste0(prefix, "/r_squared")]] <- tol(s$r.squared, "R-squared")
outputs[[paste0(prefix, "/adjusted_r_squared")]] <- tol(s$adj.r.squared, "adjusted R-squared")
outputs[[paste0(prefix, "/standard_error")]] <- tol(s$sigma, "residual standard error")
outputs[[paste0(prefix, "/df2")]] <- exact(n - p, "residual df")
outputs[[paste0(prefix, "/residual_ss")]] <- tol(sum(stats::residuals(fit)^2), "residual SS")
outputs[[paste0(prefix, "/regression_ss")]] <- tol(sum((cc$y - mean(cc$y))^2) - sum(stats::residuals(fit)^2), "regression SS")
f <- unname(s$fstatistic)
outputs[[paste0(prefix, "/f_value")]] <- tol(f[1], "overall F")
outputs[[paste0(prefix, "/p_value")]] <- prob(stats::pf(f[1], f[2], f[3], lower.tail = FALSE), "overall F tail")
e <- stats::residuals(fit)
outputs[[paste0(prefix, "/durbin_watson")]] <- tol(sum(diff(e)^2) / sum(e^2), "Durbin-Watson of the residuals in row order")
if (is.null(previous)) {
outputs[[paste0(prefix, "/r_squared_change")]] <- tol(s$r.squared, "R-squared change from the empty model")
outputs[[paste0(prefix, "/df1")]] <- exact(p - 1L, "coefficients added")
outputs[[paste0(prefix, "/f_change")]] <- tol(f[1], "F change equals the overall F of the first model")
outputs[[paste0(prefix, "/p_change")]] <- prob(stats::pf(f[1], f[2], f[3], lower.tail = FALSE), "F change tail")
} else {
before <- fit_of(previous)
change <- stats::anova(before, fit)
outputs[[paste0(prefix, "/r_squared_change")]] <- tol(s$r.squared - summary(before)$r.squared, "R-squared change")
outputs[[paste0(prefix, "/df1")]] <- exact(change$Df[2], "coefficients added")
outputs[[paste0(prefix, "/f_change")]] <- tol(change$F[2], "anova() F for the added block")
outputs[[paste0(prefix, "/p_change")]] <- prob(change[["Pr(>F)"]][2], "anova() F tail")
}
outputs[[paste0(prefix, "/predictors")]] <- setof(predictors, "predictors in the cumulative model")
if (!with_coefficients) return(outputs)
coef <- s$coefficients
ci <- stats::confint(fit, level = level)
sd_y <- stats::sd(cc$y)
for (j in seq_len(nrow(coef))) {
cp <- paste0(prefix, "/coefficients/", j - 1)
term <- rownames(coef)[j]
outputs[[paste0(cp, "/estimate")]] <- tol(coef[j, 1], paste(term, "estimate"))
outputs[[paste0(cp, "/standard_error")]] <- tol(coef[j, 2], paste(term, "standard error"))
outputs[[paste0(cp, "/t_value")]] <- tol(coef[j, 3], paste(term, "t"))
outputs[[paste0(cp, "/p_value")]] <- prob(coef[j, 4], paste(term, "two-sided t tail"))
outputs[[paste0(cp, "/lower")]] <- tol(ci[j, 1], paste(term, "confint lower"))
outputs[[paste0(cp, "/upper")]] <- tol(ci[j, 2], paste(term, "confint upper"))
if (j > 1) {
others <- setdiff(predictors, term)
rhs <- if (length(others)) paste(others, collapse = " + ") else "1"
x_on_others <- stats::lm(stats::as.formula(paste(term, "~", rhs)), data = cc)
y_on_others <- stats::lm(stats::as.formula(paste("y ~", rhs)), data = cc)
vif <- 1 / (1 - summary(x_on_others)$r.squared)
outputs[[paste0(cp, "/standardized")]] <- tol(coef[j, 1] * stats::sd(cc[[term]]) / sd_y, paste(term, "beta"))
outputs[[paste0(cp, "/zero_order")]] <- tol(stats::cor(cc[[term]], cc$y), paste(term, "zero-order r"))
outputs[[paste0(cp, "/partial")]] <- tol(stats::cor(stats::residuals(y_on_others), stats::residuals(x_on_others)), paste(term, "partial r"))
outputs[[paste0(cp, "/part")]] <- tol(stats::cor(cc$y, stats::residuals(x_on_others)), paste(term, "part r"))
outputs[[paste0(cp, "/tolerance")]] <- tol(1 / vif, paste(term, "tolerance"))
outputs[[paste0(cp, "/vif")]] <- tol(vif, paste(term, "VIF"))
}
}
design <- stats::model.matrix(fit)
scaled <- sweep(design, 2, sqrt(colSums(design^2)), "/")
sv <- svd(scaled)$d
for (j in seq_along(sv)) {
cp <- paste0(prefix, "/collinearity/", j - 1)
outputs[[paste0(cp, "/dimension")]] <- exact(j, "dimension number")
outputs[[paste0(cp, "/eigenvalue")]] <- tol(sv[j]^2, "eigenvalue of the unit-length scaled cross-product")
outputs[[paste0(cp, "/condition_index")]] <- tol(sqrt(sv[1]^2 / sv[j]^2), "condition index", "1e-8")
}
outputs
}
casewise_outputs <- function(predictors, rows) {
fit <- fit_of(predictors)
s <- summary(fit)
e <- stats::residuals(fit)
rs <- stats::rstandard(fit)
rt <- stats::rstudent(fit)
h <- stats::hatvalues(fit)
ck <- stats::cooks.distance(fit)
db <- stats::dfbetas(fit)
outputs <- list()
for (r in rows) {
cp <- paste0("/casewise/", r - 1)
outputs[[paste0(cp, "/observed")]] <- tol(cc$y[r], "observed y")
outputs[[paste0(cp, "/predicted")]] <- tol(stats::fitted(fit)[r], "fitted value")
outputs[[paste0(cp, "/residual")]] <- tol(e[r], "residual")
outputs[[paste0(cp, "/standardized_residual")]] <- tol(e[r] / s$sigma, "e / s")
outputs[[paste0(cp, "/studentized_residual")]] <- tol(rs[r], "rstandard")
outputs[[paste0(cp, "/deleted_studentized_residual")]] <- tol(rt[r], "rstudent")
outputs[[paste0(cp, "/leverage")]] <- tol(h[r], "hatvalues")
outputs[[paste0(cp, "/cooks_distance")]] <- tol(ck[r], "cooks.distance")
for (j in seq_len(ncol(db))) {
outputs[[paste0(cp, "/dfbetas/", j - 1)]] <- tol(db[r, j], paste("dfbetas", colnames(db)[j]))
}
}
outputs
}
n_complete <- nrow(cc)
# 001: enter all, two blocks, casewise table of every row.
outputs <- c(model_outputs(0, c("x1", "x2"), NULL, with_coefficients = FALSE),
model_outputs(1, c("x1", "x2", "x3", "x4"), c("x1", "x2")),
casewise_outputs(c("x1", "x2", "x3", "x4"), c(1, 2, 5, 11, 23)))
outputs[["/n"]] <- exact(n_complete, "rows complete on y and every block predictor")
outputs[["/missing_count"]] <- exact(nrow(d) - n_complete, "rows missing x3")
emit("STAT-REG-REGRESSION-BLOCKS-001", "Blockwise regression enter all with casewise table", "nominal",
list(response = "y", blocks = list(list("x1", "x2"), list("x3", "x4")), method = "enter",
criterion = "aic", confidence_level = 0.95, casewise = "all", casewise_threshold = 3.0),
outputs, "Enter all: model 1 = x1 + x2, model 2 adds x3 + x4; the casewise table lists every row in retained order (index 0 is the first complete row).")
# 002: forward selection by AIC in block 2 (x3, x4, x5 candidates) with block 1 forced.
forced <- stats::lm(y ~ x1 + x2, data = cc)
fwd <- stats::step(forced, scope = list(lower = ~ x1 + x2, upper = ~ x1 + x2 + x3 + x4 + x5),
direction = "forward", k = 2, trace = 0)
chosen_fwd <- setdiff(attr(stats::terms(fwd), "term.labels"), c("x1", "x2"))
outputs <- c(model_outputs(0, c("x1", "x2"), NULL, with_coefficients = FALSE),
model_outputs(1, c("x1", "x2", chosen_fwd), c("x1", "x2"), with_coefficients = FALSE))
outputs[["/models/1/entered"]] <- setof(chosen_fwd, "predictors step() adds by AIC")
outputs[["/models/1/selection/0/criterion"]] <- tol(stats::AIC(forced), "AIC of the forced model (R's AIC with the variance counted)")
outputs[["/models/1/selection/0/action"]] <- list(comparison = "exact", value = "start", rationale = "the first move records the forced model")
first_added <- sub("^[+] ", "", as.character(fwd$anova$Step[2]))
stopifnot(length(chosen_fwd) >= 1, first_added %in% chosen_fwd)
outputs[["/models/1/selection/1/action"]] <- list(comparison = "exact", value = "add", rationale = "forward entry adds")
outputs[["/models/1/selection/1/predictor"]] <- list(comparison = "exact", value = first_added, rationale = "the first predictor step() adds")
outputs[["/models/1/selection/1/criterion"]] <- tol(stats::AIC(fit_of(c("x1", "x2", first_added))), "AIC after the first addition")
emit("STAT-REG-REGRESSION-BLOCKS-002", "Blockwise regression forward AIC", "axis_coverage",
list(response = "y", blocks = list(list("x1", "x2"), list("x3", "x4", "x5")), method = "forward",
criterion = "aic", confidence_level = 0.95, casewise = "none", casewise_threshold = 3.0),
outputs, "step(direction = 'forward', k = 2) from y ~ x1 + x2 with scope x3 + x4 + x5; the reported criterion is AIC() of the fitted lm, which differs from extractAIC by a constant and selects the same moves.")
# 003: backward elimination by BIC in block 2.
full <- stats::lm(y ~ x1 + x2 + x3 + x4 + x5, data = cc)
bwd <- stats::step(full, scope = list(lower = ~ x1 + x2, upper = ~ x1 + x2 + x3 + x4 + x5),
direction = "backward", k = log(n_complete), trace = 0)
chosen_bwd <- setdiff(attr(stats::terms(bwd), "term.labels"), c("x1", "x2"))
outputs <- c(model_outputs(0, c("x1", "x2"), NULL, with_coefficients = FALSE),
model_outputs(1, c("x1", "x2", chosen_bwd), c("x1", "x2"), with_coefficients = FALSE))
outputs[["/models/1/entered"]] <- setof(chosen_bwd, "predictors step() keeps by BIC")
outputs[["/models/1/selection/0/criterion"]] <- tol(stats::BIC(full), "BIC of the full model (R's BIC with the variance counted)")
emit("STAT-REG-REGRESSION-BLOCKS-003", "Blockwise regression backward BIC", "axis_coverage",
list(response = "y", blocks = list(list("x1", "x2"), list("x3", "x4", "x5")), method = "backward",
criterion = "bic", confidence_level = 0.95, casewise = "none", casewise_threshold = 3.0),
outputs, "step(direction = 'backward', k = log(n)) from the full model with x1 + x2 forced; the start criterion is BIC() of the full block-2 model.")
# 004: stepwise by AIC at a 90% confidence level, casewise outliers beyond 1.5 s.
both <- stats::step(forced, scope = list(lower = ~ x1 + x2, upper = ~ x1 + x2 + x3 + x4 + x5),
direction = "both", k = 2, trace = 0)
chosen_both <- setdiff(attr(stats::terms(both), "term.labels"), c("x1", "x2"))
final <- c("x1", "x2", chosen_both)
outputs <- c(model_outputs(0, c("x1", "x2"), NULL, level = 0.90),
model_outputs(1, final, c("x1", "x2"), level = 0.90))
outputs[["/models/1/entered"]] <- setof(chosen_both, "predictors step(direction = 'both') keeps by AIC")
fit_final <- fit_of(final)
flagged <- which(abs(stats::residuals(fit_final) / summary(fit_final)$sigma) > 1.5)
outputs[["/casewise_truncated"]] <- list(comparison = "exact", value = FALSE, rationale = "far below the casewise row limit")
for (k in seq_along(flagged)) {
outputs[[paste0("/casewise/", k - 1, "/observed")]] <- tol(cc$y[flagged[k]], "observed y of a flagged row")
outputs[[paste0("/casewise/", k - 1, "/standardized_residual")]] <- tol(stats::residuals(fit_final)[flagged[k]] / summary(fit_final)$sigma, "e / s beyond 1.5")
}
outputs[[paste0("/casewise/", length(flagged))]] <- list(comparison = "absent", rationale = paste("exactly", length(flagged), "rows exceed |e / s| > 1.5"))
emit("STAT-REG-REGRESSION-BLOCKS-004", "Blockwise regression stepwise AIC 90 percent outliers", "axis_coverage",
list(response = "y", blocks = list(list("x1", "x2"), list("x3", "x4", "x5")), method = "stepwise",
criterion = "aic", confidence_level = 0.90, casewise = "outliers", casewise_threshold = 1.5),
outputs, "step(direction = 'both', k = 2) from y ~ x1 + x2; confint(level = 0.90); the casewise table lists rows with |e / s| > 1.5 in retained order.")
cat("regression blocks cases generated\n")
generators/r/regression_boundaries.R
source("common.R", local = TRUE)
base <- read_dataset("REG_CORE")
types <- c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64")
for (n in c(3L, 4L, 5L, 8L))
export_dataset(base[seq_len(n), , drop = FALSE], paste0("REG_MIN", n),
paste("First", n, "rows of exported REG_CORE"),
"R stats::lm/glm; MASS::polr/glm.nb; nnet::multinom help pages",
"Constructed test data; no third-party dataset license.", types)
edge <- base
edge$binary <- "event"
edge$ordinal <- "low"
edge$nominal <- "A"
edge$group <- "A"
edge$exposure[[1]] <- 0
edge$z <- edge$x
export_dataset(edge, "REG_DEGENERATE", "REG_CORE with collapsed levels, collinear x/z and zero exposure",
"Tabelz manual, regression design validity conditions",
"Constructed test data; no third-party dataset license.", types)
run <- generator_run(c("stats", "MASS", "nnet", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R minimal-design", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
emit <- function(id, entry, menu, dataset, config, outputs, notes, error = NULL) {
if (!is.null(error)) outputs <- structure(list(), names = character())
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = error, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = paste("Design boundary", id),
case_kind = "boundary",
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = if (is.null(error)) "independent_implementation" else "analytic",
source = "R minimal-design fit and manual design conditions",
citation = "R stats::lm/glm, MASS::polr/glm.nb, nnet::multinom help pages; Tabelz manual chapter 07",
generator = list(path = "generators/r/regression_boundaries.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Reference data are re-read from the exported minimal-design CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-NUMERIC-001"),
review_reference = NULL))
}
d3 <- read_dataset("REG_MIN3")
d4 <- read_dataset("REG_MIN4")
d5 <- read_dataset("REG_MIN5")
d8 <- read_dataset("REG_MIN8")
fit <- stats::lm(y ~ x, data = d3)
emit("STAT-REG-BOUNDARY-OLS-MIN-001", "tabelz.analysis.fit_y_by_x:compute_fits",
"fitYByXMenuItem", "REG_MIN3",
list(y_columns = list("y"), x_columns = list("x")),
list("/responses/0/parameters/1/estimate" = tol(coef(fit)[["x"]], "n=3 slope", "1e-9"),
"/responses/0/observations" = exact(3, "three observations")),
list("Three rows give one residual degree of freedom for intercept plus one slope."))
emit("STAT-REG-BOUNDARY-OLS-DOMAIN-001", "tabelz.analysis.fit_y_by_x:compute_fits",
"fitYByXMenuItem", "REG_CORE",
list(y_columns = list("y"), x_columns = list("x"), model_type = "logarithmic"),
list(), list("Logarithmic regression requires strictly positive x; REG_CORE contains negative x."),
list(error_class = "ValueError", message_contains = "positive"))
vif_fit <- stats::lm(x ~ z, data = d3)
emit("STAT-REG-BOUNDARY-VIF-MIN-001", "tabelz.analysis.fit_y_by_x:compute_fit_vif",
"fitYByXMenuItem", "REG_MIN3",
list(y_columns = list("y"), x_columns = list("x", "z"), missing = "drop",
model_type = "linear", degree = 2L, interactions = FALSE, split_percent = 100L),
list("/0" = tol(1 / (1 - summary(vif_fit)$r.squared), "n=3 auxiliary VIF", "1e-9")),
list("Three rows are the smallest design for an auxiliary intercept-plus-slope fit."))
emit("STAT-REG-BOUNDARY-VIF-COLLINEAR-001", "tabelz.analysis.fit_y_by_x:compute_fit_vif",
"fitYByXMenuItem", "REG_DEGENERATE",
list(y_columns = list("y"), x_columns = list("x", "z"), missing = "drop",
model_type = "linear", degree = 2L, interactions = FALSE, split_percent = 100L),
list("/0" = list(comparison = "absent", rationale = "Perfect collinearity makes VIF undefined.")),
list("z equals x exactly; R auxiliary regression has R-squared one, so VIF is undefined."))
bfit <- stats::glm(I(binary == "event") ~ x, data = d3, family = stats::binomial())
emit("STAT-REG-BOUNDARY-BINARY-MIN-001",
"tabelz.analysis.logistic_regression:compute_binary_logistic_regression",
"logisticRegressionMenuItem", "REG_MIN3",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
list(),
list("REG_MIN3's three rows all have the same outcome, so a binary logit is undefined and must be refused.",
"Until 2026-09-25 this case wrongly asserted both outcomes occur; STAT-REG-BOUNDARY-BINARY-CORRECTED-001 covers the valid minimum design."),
error = list(error_class = "ValueError", message_contains = "exactly two outcome values"))
emit("STAT-REG-BOUNDARY-BINARY-ONELEVEL-001",
"tabelz.analysis.logistic_regression:compute_binary_logistic_regression",
"logisticRegressionMenuItem", "REG_DEGENERATE",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
list(), list("Only one observed binary level is present."),
list(error_class = "ValueError", message_contains = "exactly two"))
d4$ord <- ordered(d4$ordinal, levels = c("low", "middle", "high"))
ofit <- MASS::polr(ord ~ x, data = d4, method = "logistic", Hess = TRUE,
control = list(maxit = 1000, reltol = 1e-15))
# With four rows the likelihood is nearly flat along the slope and polr's BFGS
# stops about 1e-12 below the maximum. Refine on the same proportional-odds
# likelihood with Nelder-Mead and keep the refinement only if log L rises.
ord_loglik <- function(p) {
eta <- p[1L] * d4$x
lower <- stats::plogis(p[2L] - eta)
upper <- stats::plogis(p[2L] + exp(p[3L]) - eta)
sum(log(ifelse(d4$ord == "low", lower, ifelse(d4$ord == "middle", upper - lower, 1 - upper))))
}
ord_start <- c(coef(ofit)[["x"]], ofit$zeta[[1L]], log(ofit$zeta[[2L]] - ofit$zeta[[1L]]))
ord_refined <- stats::optim(ord_start, function(p) -ord_loglik(p), method = "Nelder-Mead",
control = list(reltol = 1e-16, maxit = 100000L))
stopifnot(-ord_refined$value >= ord_loglik(ord_start))
ord_min_slope <- ord_refined$par[[1L]]
emit("STAT-REG-BOUNDARY-ORDINAL-MIN-001",
"tabelz.analysis.ordinal_logistic:compute_ordinal_logistic_regression",
"ordinalRegressionMenuItem", "REG_MIN4",
list(outcome_column = "ordinal", predictors = list("x"),
level_order = list("low", "middle", "high")),
list("/coefficients/0/coefficient" = tol(ord_min_slope, "n=4 ordered slope, polr refined by Nelder-Mead"),
"/n" = exact(4, "four observations")),
list("Four rows exceed one slope plus two thresholds."))
emit("STAT-REG-BOUNDARY-ORDINAL-ONELEVEL-001",
"tabelz.analysis.ordinal_logistic:compute_ordinal_logistic_regression",
"ordinalRegressionMenuItem", "REG_DEGENERATE",
list(outcome_column = "ordinal", predictors = list("x")),
list(), list("Only one ordered level is present."),
list(error_class = "ValueError", message_contains = "three"))
# Statistician ruling, issue #11: REG_MIN5's single C row has the smallest x, so
# C versus A is completely separated and the multinomial MLE does not exist
# (moving the C coefficients along the separating direction raises every C
# contribution without bound). The fit is refused. Verify the separation independently: C's x lies strictly below
# every other row.
stopifnot(max(d5$x[d5$nominal == "C"]) < min(d5$x[d5$nominal != "C"]))
emit("STAT-REG-BOUNDARY-NOMINAL-MIN-001",
"tabelz.analysis.nominal_logistic:compute_nominal_logistic_regression",
"nominalRegressionMenuItem", "REG_MIN5",
list(outcome_column = "nominal", reference_value = "A", predictors = list("x")),
list(),
list("Issue #11 ruling: complete separation of C establishes that no finite MLE exists; the fit is refused.",
"Until 2026-09-25 this case compared nnet::multinom's arbitrary stopping point."),
list(error_class = "ValueError", message_contains = "No finite maximum-likelihood estimate"))
emit("STAT-REG-BOUNDARY-NOMINAL-ONELEVEL-001",
"tabelz.analysis.nominal_logistic:compute_nominal_logistic_regression",
"nominalRegressionMenuItem", "REG_DEGENERATE",
list(outcome_column = "nominal", predictors = list("x")),
list(), list("Only one nominal category is present."),
list(error_class = "ValueError", message_contains = "three"))
pfit <- stats::glm(count ~ x, data = d3, family = stats::poisson())
emit("STAT-REG-BOUNDARY-POISSON-MIN-001",
"tabelz.analysis.poisson_regression:compute_poisson_regression",
"poissonRegressionMenuItem", "REG_MIN3",
list(count_column = "count", predictors = list("x")),
list("/coefficients/1/coefficient" = tol(coef(pfit)[["x"]], "n=3 Poisson slope"),
"/n" = exact(3, "three observations")),
list("Three rows exceed intercept and slope count."))
emit("STAT-REG-BOUNDARY-POISSON-ZEROEXPOSURE-001",
"tabelz.analysis.poisson_regression:compute_poisson_regression",
"poissonRegressionMenuItem", "REG_DEGENERATE",
list(count_column = "count", predictors = list("x"), exposure_column = "exposure"),
list(), list("One exposure is zero; manual requires positive exposure."),
list(error_class = "ValueError", message_contains = "exposure"))
nbfit <- MASS::glm.nb(nb_count ~ x, data = d8,
control = stats::glm.control(maxit = 1000, epsilon = 1e-12))
emit("STAT-REG-BOUNDARY-NB-MIN-001",
"tabelz.analysis.negative_binomial:compute_negative_binomial",
"nbRegressionMenuItem", "REG_MIN8",
list(count = "nb_count", predictors = list("x")),
list("/coefficients/1/coefficient" = tol(coef(nbfit)[["x"]], "small-design NB2 slope"),
"/n" = exact(8, "eight observations")),
list("Eight-row design is the smallest constructed subset on which the R NB2 fit has finite dispersion."))
emit("STAT-REG-BOUNDARY-NB-ZEROEXPOSURE-001",
"tabelz.analysis.negative_binomial:compute_negative_binomial",
"nbRegressionMenuItem", "REG_DEGENERATE",
list(count = "nb_count", predictors = list("x"), exposure = "exposure"),
list(), list("One exposure is zero; manual requires positive exposure."),
list(error_class = "ValueError", message_contains = "exposure"))
gfit <- stats::lm(y ~ x, data = d3)
emit("STAT-REG-BOUNDARY-GENERAL-MIN-001", "tabelz.executor.general_linear",
"generalLinearMenuItem", "REG_MIN3",
list(response_column = "y", covariate_columns = list("x")),
list("/coefficients/x" = tol(coef(gfit)[["x"]], "n=3 general-linear slope", "1e-9"),
"/residual_df" = exact(1, "one residual degree of freedom")),
list("Three rows, intercept and one covariate give residual df one."))
emit("STAT-REG-BOUNDARY-GENERAL-ONELEVEL-001", "tabelz.executor.general_linear",
"generalLinearMenuItem", "REG_DEGENERATE",
list(response_column = "y", factor_columns = list("group")),
list(), list("A one-level factor has no estimable contrast."),
list(error_class = "GeneralLinearExecutionError", message_contains = "two"))
emit("STAT-REG-BOUNDARY-EFFECTS-MIN-001", "tabelz.executor.linear_effects",
"linearEffectsMenuItem", "REG_MIN3",
list(response_column = "y", covariate_columns = list("x")),
list("/coefficients/x" = tol(coef(gfit)[["x"]], "n=3 effects slope", "1e-9"),
"/residual_df" = exact(1, "one residual degree of freedom")),
list("Three rows, intercept and one covariate give residual df one."))
emit("STAT-REG-BOUNDARY-EFFECTS-ONELEVEL-001", "tabelz.executor.linear_effects",
"linearEffectsMenuItem", "REG_DEGENERATE",
list(response_column = "y", factor_columns = list("group")),
list(), list("A one-level factor has no estimable contrast."),
list(error_class = "LinearEffectsExecutionError", message_contains = "two"))
cat("regression boundary references generated\n")
generators/r/regression_chaid.R
source("common.R", local = TRUE)
# CHAID trees (Statistics > Multivariate > CHAID tree). The product grows the tree on a seeded
# identity split (numpy PCG64) that R cannot reproduce, so the reference data make the root
# split invariant to the subset: a nominal predictor that determines the target exactly. For a
# perfectly associated c x c table the Pearson chi-square equals (c - 1) N whatever the
# margins, no pair of categories can merge (every pairwise 2 x 2 table is perfect with at
# least four rows), so the Bonferroni multiplier is 1 and the adjusted p is the chi-square
# upper tail at (c - 1)^2 df evaluated at the training size N = n - floor(0.3 n). Both children
# are pure and the training and held-out risks are 0. stats::chisq.test confirms the identity
# on the full table. Every oracle reads the exported CSV.
run <- generator_run(c("stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/90-learning-and-mixtures.tex:17-22: categories with similar target distributions are merged using Pearson chi-square tests; nominal categories may merge freely, ordered ones only with neighbours.",
"docs/manual/chapters/90-learning-and-mixtures.tex:44-49: the overall split test includes a Bonferroni multiplier for the candidate category reduction; the smallest adjusted p selects the split if the thresholds and size rules permit.",
"docs/manual/chapters/90-learning-and-mixtures.tex:51-58: merge and split thresholds 0.05; a seeded random split reserves 30% for assessment; read held-out misclassification separately from training error."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
prob <- function(value, label) {
list(comparison = "tail_probability", value = dec(unname(value)), relative_tolerance = "1e-8",
rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
emit <- function(id, title, kind, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("regression", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.chaid:compute_chaid",
menu_items = list("chaidMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_CHAID.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "analytic",
source = "perfect-association chi-square identity (c - 1) N, checked with stats::chisq.test; Kass (1980) Bonferroni multiplier",
citation = "Kass (1980) An exploratory technique for investigating large quantities of categorical data, Applied Statistics 29(2), 119-127; R Core Team, stats package help: chisq.test.",
generator = list(path = "generators/r/regression_chaid.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-CHAID-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("regression", id, case)
}
set.seed(119)
n <- 42
shift <- sample(c("day", "night"), n, TRUE)
machine <- rep(c("m1", "m2", "m3"), each = 14)[sample(n)]
frame <- data.frame(
shift = shift,
machine = machine,
late = ifelse(shift == "night", "yes", "no"),
grade = c(m1 = "A", m2 = "B", m3 = "C")[machine],
noise = round(rnorm(n, 50, 5), 1),
stringsAsFactors = FALSE
)
export_dataset(frame, "REG_CHAID", "Forty-two jobs: shift determines lateness, machine determines grade (seed 119)",
"analytic perfect-association chi-square; stats chisq.test",
"Constructed validation data; no third-party license.",
c(shift = "String", machine = "String", late = "String", grade = "String",
noise = "Float64"))
d <- read_dataset("REG_CHAID")
stopifnot(all(table(d$shift) >= 13), all(table(d$machine) == 14),
all((d$shift == "night") == (d$late == "yes")),
all(c(m1 = "A", m2 = "B", m3 = "C")[d$machine] == d$grade))
training <- n - floor(0.3 * n)
test <- n - training
stopifnot(training == 30, test == 12)
# Every category keeps at least 13 - 12 = 1 (shift) or 14 - 12 = 2 (machine) training rows;
# a pairwise 2 x 2 table of two machine categories therefore has at least 4 rows, chi-square
# at least 4 and p at most 0.0455 < 0.05, so no merge happens.
stopifnot(pchisq(4, 1, lower.tail = FALSE) < 0.05)
# Identity check on the full tables: chi-square = (c - 1) N for a perfectly associated table.
full2 <- suppressWarnings(chisq.test(table(d$shift, d$late), correct = FALSE))
full3 <- suppressWarnings(chisq.test(table(d$machine, d$grade), correct = FALSE))
stopifnot(abs(full2$statistic - n) < 1e-9, abs(full3$statistic - 2 * n) < 1e-9)
root_outputs <- function(c_categories, classes, predictor, groups) {
chi <- (c_categories - 1) * training
df <- (c_categories - 1)^2
p <- pchisq(chi, df, lower.tail = FALSE)
out <- list(
"/training_rows" = exact(training, "n - floor(0.3 n)"),
"/test_rows" = exact(test, "floor(0.3 n) held out"),
"/missing_target" = exact(0L, "no missing target"),
"/excluded_count" = exact(0L, "no excluded row"),
"/classes" = list(comparison = "sequence", values = as.list(classes),
rationale = "classes in code-point order"),
"/risk_name" = exact("misclassification rate", "categorical target"),
"/training_risk" = tol(0, "pure children reproduce every training row", abs = "1e-15"),
"/test_risk" = tol(0, "the deterministic rule classifies every held-out row", abs = "1e-15"),
"/nodes/0/n" = exact(training, "root holds the training rows"),
"/nodes/0/split/predictor" = exact(predictor, "the only predictor"),
"/nodes/0/split/statistic" = tol(chi, "Pearson chi-square of a perfectly associated c x c table: (c - 1) N", rel = "1e-9"),
"/nodes/0/split/df/0" = exact(as.integer(df), "(c - 1)^2"),
"/nodes/0/split/p_value" = prob(p, "upper chi-square tail"),
"/nodes/0/split/multiplier" = tol(1, "no category was merged: c groups of c categories"),
"/nodes/0/split/adjusted_p" = prob(p, "p times the multiplier 1"),
"/nodes/0/split/eligible" = exact(TRUE, "every child has at least one row")
)
for (g in seq_along(groups)) {
out[[sprintf("/nodes/0/split/groups/%d", g - 1L)]] <- list(
comparison = "sequence", values = as.list(groups[[g]]),
rationale = "unmerged categories in code-point order, one per group")
out[[sprintf("/nodes/%d/stop_reason", g)]] <- exact("pure node", "each child holds one class")
out[[sprintf("/nodes/%d/parent", g)]] <- exact(0L, "child of the root")
}
out
}
invariance <- "Analytic oracle: the predictor determines the target, so whichever 30 rows form the training set the root table is perfectly associated, its chi-square is (c - 1) N = (c - 1) x 30 with (c - 1)^2 df, nothing merges (each pairwise 2 x 2 has at least 4 rows: chi-square >= 4, p <= 0.0455 < 0.05), the multiplier is 1 and both risks are 0. stats::chisq.test confirms the identity on the full 42-row tables."
sizes <- "Minimum parent 2 and child 1 so the 30-row root may split; depth 3 leaves the pure children to stop as pure nodes."
emit("STAT-REG-CHAID-001", "CHAID root split on a binary nominal predictor", "nominal",
list(target = "late", target_kind = "categorical", predictors = list("shift"),
min_parent = 2L, min_child = 1L, max_depth = 3L),
root_outputs(2L, c("no", "yes"), "shift", list("day", "night")),
list(invariance, sizes))
emit("STAT-REG-CHAID-THREE-001", "CHAID root split on a three-category nominal predictor", "axis_coverage",
list(target = "grade", target_kind = "categorical", predictors = list("machine"),
min_parent = 2L, min_child = 1L, max_depth = 3L),
root_outputs(3L, c("A", "B", "C"), "machine", list("m1", "m2", "m3")),
list(invariance, "Three categories and three classes: chi-square 2N on 4 df; the nominal Kass multiplier for c = r = 3 is 1.", sizes))
emit("STAT-REG-CHAID-ORDINAL-001", "CHAID root split on an ordered three-category predictor", "axis_coverage",
list(target = "grade", target_kind = "categorical", predictors = list("machine"),
ordinal = list("machine"), min_parent = 2L, min_child = 1L, max_depth = 3L),
root_outputs(3L, c("A", "B", "C"), "machine", list("m1", "m2", "m3")),
list(invariance, "Declared ordinal: only adjacent categories may merge, and none does; the ordinal multiplier C(c - 1, r - 1) for r = c is 1.", sizes))
emit("STAT-REG-CHAID-RESPLIT-001", "CHAID root split with Kass merge/re-split", "axis_coverage",
list(target = "grade", target_kind = "categorical", predictors = list("machine"),
algorithm = "kass_resplit", resplit_alpha = 0.01, min_parent = 2L, min_child = 1L,
max_depth = 3L),
root_outputs(3L, c("A", "B", "C"), "machine", list("m1", "m2", "m3")),
list(invariance, "Kass merge/re-split mode: no compound forms, so no re-split is attempted and the partition is the three single categories.", sizes))
cat("CHAID cases generated\n")
generators/r/regression_core.R
source("common.R", local = TRUE)
# The independent calculations below always re-read the exported CSV. The
# constructed design avoids separation and has balanced, non-aliased terms.
i <- seq_len(72L)
constructed <- data.frame(
x = (i - 36) / 13 + sin(1.7 * i) / 4,
z = cos(0.91 * i) + (i %% 7) / 10,
group = rep(c("A", "B", "C"), length.out = length(i)),
binary = ifelse((7 * i %% 13) < 6, "event", "other"),
ordinal = rep(c("low", "middle", "high", "middle"), length.out = length(i)),
nominal = rep(c("A", "B", "C", "B", "A", "C"), length.out = length(i)),
exposure = 1 + (i %% 4) / 3,
cluster = rep(sprintf("g%02d", 1:12), each = 6),
weight = 1 + (i %% 5) / 5,
stringsAsFactors = FALSE
)
constructed$y <- 3 + 1.4 * constructed$x - 0.6 * constructed$z +
c(A = 0, B = 0.7, C = -0.4)[constructed$group] + sin(i * 2.3) / 4
constructed$count <- pmax(0, round(constructed$exposure *
exp(0.3 + 0.2 * constructed$x - 0.15 * constructed$z) +
(i %% 5) - 2))
constructed$nb_count <- pmax(0, round(constructed$exposure *
exp(0.5 + 0.18 * constructed$x) * c(0.2, 1, 2.5, 0.5, 1.5)[(i %% 5) + 1]))
constructed <- constructed[c("x", "z", "group", "binary", "ordinal", "nominal",
"exposure", "cluster", "weight", "y", "count", "nb_count")]
export_dataset(constructed, "REG_CORE", "Constructed by regression_core.R",
"R stats::lm, stats::glm, MASS::polr, nnet::multinom, MASS::glm.nb documentation",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64"))
d <- read_dataset("REG_CORE")
run <- generator_run(c("stats", "MASS", "nnet", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("Independent R", label))
prob <- function(x, label)
list(comparison = "tail_probability", value = dec(unname(x)),
relative_tolerance = "1e-8", rationale = paste("Independent R", label))
exact <- function(x, label)
list(comparison = "exact", value = as.integer(x), rationale = label)
emit <- function(id, entry, menu, kind, config, outputs, notes, excluded = list(),
error = NULL, dataset = "REG_CORE") {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = gsub("-", " ", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R stats, MASS and nnet",
citation = "R stats::lm/glm, MASS::polr/glm.nb, nnet::multinom help pages",
generator = list(path = "generators/r/regression_core.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "All reference fits use the re-read exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
fit <- stats::lm(y ~ x + z, data = d)
fit_entry <- "tabelz.analysis.fit_y_by_x:compute_fits"
fit_menu <- "fitYByXMenuItem"
emit("STAT-REG-OLS-001", fit_entry, fit_menu, "nominal",
list(y_columns = list("y"), x_columns = list("x", "z")),
list("/responses/0/parameters/1/estimate" = tol(coef(fit)[["x"]], "OLS x slope", "1e-9"),
"/responses/0/metrics/sse" = tol(sum(resid(fit)^2), "OLS SSE", "1e-9")),
list("Classical OLS with intercept; R lm coefficients and residual sum of squares."))
vif_entry <- "tabelz.analysis.fit_y_by_x:compute_fit_vif"
r2_x <- summary(stats::lm(x ~ z, data = d))$r.squared
r2_z <- summary(stats::lm(z ~ x, data = d))$r.squared
emit("STAT-REG-VIF-001", vif_entry, fit_menu, "nominal",
list(y_columns = list("y"), x_columns = list("x", "z"),
missing = "drop", model_type = "linear", degree = 2L,
interactions = FALSE, split_percent = 100L),
list("/0" = tol(1 / (1 - r2_x), "x VIF", "1e-9"),
"/1" = tol(1 / (1 - r2_z), "z VIF", "1e-9")),
list("Each VIF = 1/(1-R-squared) from the auxiliary R lm regression."))
binary_entry <- "tabelz.analysis.logistic_regression:compute_binary_logistic_regression"
binary_fit <- stats::glm(I(binary == "event") ~ x, family = stats::binomial(), data = d)
emit("STAT-REG-BINARY-001", binary_entry, "logisticRegressionMenuItem", "nominal",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
list("/coefficients/1/coefficient" = tol(coef(binary_fit)[["x"]], "binary logit slope"),
"/log_likelihood" = tol(as.numeric(stats::logLik(binary_fit)), "binomial log likelihood")),
list("R glm binomial logit; event explicitly coded TRUE; Wald inference per manual."))
ordinal_entry <- "tabelz.analysis.ordinal_logistic:compute_ordinal_logistic_regression"
d$ordinal_ordered <- ordered(d$ordinal, levels = c("low", "middle", "high"))
ordinal_fit <- MASS::polr(ordinal_ordered ~ x, data = d, method = "logistic",
Hess = TRUE, control = list(maxit = 1000, reltol = 1e-15))
emit("STAT-REG-ORDINAL-001", ordinal_entry, "ordinalRegressionMenuItem", "nominal",
list(outcome_column = "ordinal", predictors = list("x"),
level_order = list("low", "middle", "high")),
list("/coefficients/0/coefficient" = tol(coef(ordinal_fit)[["x"]], "proportional-odds slope")),
list("MASS polr convention logit P(Y <= k) = zeta[k] - eta; positive x slope raises higher-level odds."))
nominal_entry <- "tabelz.analysis.nominal_logistic:compute_nominal_logistic_regression"
d$nominal_factor <- factor(d$nominal, levels = c("A", "B", "C"))
nominal_fit <- nnet::multinom(nominal_factor ~ x, data = d, trace = FALSE,
maxit = 1000, reltol = 1e-15)
emit("STAT-REG-NOMINAL-001", nominal_entry, "nominalRegressionMenuItem", "nominal",
list(outcome_column = "nominal", reference_value = "A", predictors = list("x")),
list("/coefficients/0/coefficient" = tol(coef(nominal_fit)["B", "(Intercept)"],
"B vs A intercept")),
list("R nnet multinom uses A as first factor level and reference; tight convergence."))
poisson_entry <- "tabelz.analysis.poisson_regression:compute_poisson_regression"
poisson_fit <- stats::glm(count ~ x + offset(log(exposure)),
family = stats::poisson(), data = d)
emit("STAT-REG-POISSON-001", poisson_entry, "poissonRegressionMenuItem", "nominal",
list(count_column = "count", predictors = list("x"), exposure_column = "exposure"),
list("/coefficients/1/coefficient" = tol(coef(poisson_fit)[["x"]], "Poisson log-rate slope"),
"/deviance" = tol(stats::deviance(poisson_fit), "Poisson deviance")),
list("Exposure enters only as log offset; glm Poisson uses log link."))
nb_entry <- "tabelz.analysis.negative_binomial:compute_negative_binomial"
nb_fit <- MASS::glm.nb(nb_count ~ x + offset(log(exposure)), data = d,
control = stats::glm.control(maxit = 1000, epsilon = 1e-12))
emit("STAT-REG-NB-001", nb_entry, "nbRegressionMenuItem", "nominal",
list(count = "nb_count", predictors = list("x"), exposure = "exposure"),
list("/coefficients/1/coefficient" = tol(coef(nb_fit)[["x"]], "NB2 log-rate slope"),
"/alpha" = tol(1 / nb_fit$theta, "NB2 dispersion alpha")),
list("MASS glm.nb theta is inverse of Tabelz NB2 alpha; offset is log exposure."))
glm_entry <- "tabelz.executor.general_linear"
glm_fit <- stats::lm(y ~ x + group, data = d,
contrasts = list(group = stats::contr.sum(3)))
emit("STAT-REG-GENERAL-001", glm_entry, "generalLinearMenuItem", "nominal",
list(response_column = "y", factor_columns = list("group"), covariate_columns = list("x"),
test_type = "III"),
list("/residual_variance" = tol(summary(glm_fit)$sigma^2, "GLM residual variance", "1e-9"),
"/residual_df" = exact(stats::df.residual(glm_fit), "full model residual df")),
list("Sum-to-zero factor contrasts and Type III partial tests per manual; residual variance is SSE/df."))
effects_entry <- "tabelz.executor.linear_effects"
effects_fit <- stats::lm(y ~ x + group, data = d)
emit("STAT-REG-EFFECTS-001", effects_entry, "linearEffectsMenuItem", "nominal",
list(response_column = "y", factor_columns = list("group"), covariate_columns = list("x")),
list("/residual_variance" = tol(summary(effects_fit)$sigma^2, "linear-effects residual variance", "1e-9"),
"/residual_df" = exact(stats::df.residual(effects_fit), "full model residual df")),
list("Treatment contrasts use alphabetically first factor level A as reference; Type II tests."))
cat("regression core references generated\n")
generators/r/regression_curve_estimation.R
source("common.R", local = TRUE)
# Curve estimation (Statistics > Correlation & regression > Curve estimation): each curve is
# ordinary least squares of a transformation of Y on a function of t. Every reference is
# lm() on the re-read REG_CURVE CSV.
t <- as.double(1:12)
y <- c(4.2, 6.1, 8.9, 11.4, 15.8, 19.1, 24.6, 29.3, 34.2, 38.4, 42.9, 46.1)
y_gap <- y
y_gap[6] <- NA
constructed <- data.frame(t = t, y = y, y_gap = y_gap)
export_dataset(
constructed, "REG_CURVE", "Constructed by regression_curve_estimation.R",
"R stats::lm, summary.lm, confint on transformed responses",
"Constructed test data; no third-party dataset license.",
c(t = "Float64", y = "Float64", y_gap = "Float64")
)
d <- read_dataset("REG_CURVE")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R stats::lm", label, "from re-read REG_CURVE CSV."))
prob <- function(x, label)
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = "1e-8",
rationale = paste("R stats::lm", label, "from re-read REG_CURVE CSV."))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
entry <- "tabelz.analysis.curve_estimation:compute_curve_estimation"
menu <- "curveEstimationMenuItem"
notes_common <- c(
"lm() of the curve's fitted-scale response on its design columns with an intercept; R-squared, F, residual SE, coefficient SE, t, p and confint are on that fitted scale.",
"Curve parameters: b0 = exp(coefficient) for power, compound, exponential and logistic; b1 = exp(coefficient) for compound and logistic; a parameter interval is the exp of the fitted interval (bounds sorted).",
"docs/manual/chapters/86-regression-and-count-models.tex:31-37: 'offers named linearized curves. Linear, quadratic and cubic models fit the original response; power, exponential and growth forms fit a logarithmic response. The logistic growth curve requires a declared upper bound.'",
"docs/manual/chapters/86-regression-and-count-models.tex:65-67: 'The reported R^2, tests and intervals use the named fitting scale'"
)
emit <- function(id, title, kind, config, outputs, notes) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs, expected_error = NULL,
deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_CURVE.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R 4.6.1 stats::lm",
citation = "R Core Team, stats package help: lm, summary.lm, confint.lm, pf, pt.",
generator = list(path = "generators/r/regression_curve_estimation.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, notes_common, "Expected computed from the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-CURVE-ESTIMATION-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# The fitted-scale design of each curve, as the window defines them.
curve_design <- function(curve, t, y, upper = NULL) {
response <- switch(curve,
power = , compound = , s = , growth = , exponential = log(y),
logistic = log(1 / y - 1 / upper),
y)
columns <- switch(curve,
logarithmic = , power = list(log(t)),
inverse = , s = list(1 / t),
quadratic = list(t, t^2),
cubic = list(t, t^2, t^3),
list(t))
list(response = response, columns = columns)
}
parameter <- function(curve, index, value) {
if (index == 1 && curve %in% c("power", "compound", "exponential", "logistic")) return(exp(value))
if (index == 2 && curve %in% c("compound", "logistic")) return(exp(value))
value
}
curve_outputs <- function(curve, t, y, upper = NULL, level = 0.95) {
design <- curve_design(curve, t, y, upper)
frame <- data.frame(response = design$response)
for (j in seq_along(design$columns)) frame[[paste0("c", j)]] <- design$columns[[j]]
formula <- stats::as.formula(paste("response ~", paste(paste0("c", seq_along(design$columns)), collapse = " + ")))
fit <- stats::lm(formula, data = frame)
s <- summary(fit)
ci <- stats::confint(fit, level = level)
f <- unname(s$fstatistic)
prefix <- paste0("/fits/[curve=", curve, "]")
outputs <- list()
outputs[[paste0(prefix, "/r_squared")]] <- tol(s$r.squared, paste(curve, "R-squared on the fitted scale"))
outputs[[paste0(prefix, "/adjusted_r_squared")]] <- tol(s$adj.r.squared, paste(curve, "adjusted R-squared"))
outputs[[paste0(prefix, "/standard_error")]] <- tol(s$sigma, paste(curve, "residual standard error"))
outputs[[paste0(prefix, "/f_value")]] <- tol(f[1], paste(curve, "overall F"))
outputs[[paste0(prefix, "/df1")]] <- exact(f[2], "regression df")
outputs[[paste0(prefix, "/df2")]] <- exact(f[3], "residual df")
outputs[[paste0(prefix, "/p_value")]] <- prob(stats::pf(f[1], f[2], f[3], lower.tail = FALSE), paste(curve, "F tail"))
coef <- s$coefficients
for (j in seq_len(nrow(coef))) {
cp <- paste0(prefix, "/coefficients/", j - 1)
outputs[[paste0(cp, "/estimate")]] <- tol(coef[j, 1], paste(curve, "b", j - 1, "fitted-scale estimate"))
outputs[[paste0(cp, "/standard_error")]] <- tol(coef[j, 2], paste(curve, "b", j - 1, "standard error"))
outputs[[paste0(cp, "/t_value")]] <- tol(coef[j, 3], paste(curve, "b", j - 1, "t"))
outputs[[paste0(cp, "/p_value")]] <- prob(coef[j, 4], paste(curve, "b", j - 1, "two-sided t tail"))
outputs[[paste0(cp, "/lower")]] <- tol(ci[j, 1], paste(curve, "b", j - 1, "confint lower"))
outputs[[paste0(cp, "/upper")]] <- tol(ci[j, 2], paste(curve, "b", j - 1, "confint upper"))
outputs[[paste0(cp, "/parameter")]] <- tol(parameter(curve, j, coef[j, 1]), paste(curve, "b", j - 1, "on the curve's scale"))
bounds <- sort(c(parameter(curve, j, ci[j, 1]), parameter(curve, j, ci[j, 2])))
outputs[[paste0(cp, "/parameter_lower")]] <- tol(bounds[1], paste(curve, "b", j - 1, "parameter interval lower"))
outputs[[paste0(cp, "/parameter_upper")]] <- tol(bounds[2], paste(curve, "b", j - 1, "parameter interval upper"))
}
outputs
}
# 001: polynomial and reciprocal curves on the original response scale.
curves <- c("linear", "quadratic", "cubic", "logarithmic", "inverse")
outputs <- do.call(c, lapply(curves, function(curve) curve_outputs(curve, d$t, d$y)))
outputs[["/n"]] <- exact(nrow(d), "complete rows")
outputs[["/missing_count"]] <- exact(0L, "no missing rows")
emit("STAT-REG-CURVE-ESTIMATION-001", "Curve estimation original-scale curves", "nominal",
list(response = "y", predictor = "t", curves = as.list(curves), confidence_level = 0.95),
outputs, "Original-scale curves: linear, quadratic, cubic (powers of t), logarithmic (ln t) and inverse (1 / t).")
# 002: log-scale curves at a 90% confidence level.
curves <- c("power", "compound", "s", "growth", "exponential")
outputs <- do.call(c, lapply(curves, function(curve) curve_outputs(curve, d$t, d$y, level = 0.90)))
emit("STAT-REG-CURVE-ESTIMATION-002", "Curve estimation log-scale curves 90 percent", "axis_coverage",
list(response = "y", predictor = "t", curves = as.list(curves), confidence_level = 0.90),
outputs, "Curves fitted to ln Y: power (on ln t), compound, growth and exponential (on t), S (on 1 / t); confint(level = 0.90).")
# 003: the bounded logistic curve with upper bound U = 50 (every Y lies in (0, U)).
outputs <- curve_outputs("logistic", d$t, d$y, upper = 50)
emit("STAT-REG-CURVE-ESTIMATION-003", "Curve estimation logistic with upper bound", "axis_coverage",
list(response = "y", predictor = "t", curves = list("logistic"), upper_bound = 50, confidence_level = 0.95),
outputs, "Logistic: lm of ln(1 / Y - 1 / U) on t with U = 50; b0 and b1 are exp of the fitted coefficients.")
# 004/005: no predictor, a missing response on row 6. Input positions keep the gap (t = 1..12
# without 6); retained order renumbers the 11 complete rows 1..11.
keep <- is.finite(d$y_gap)
outputs <- curve_outputs("linear", d$t[keep], d$y_gap[keep])
outputs <- c(outputs, curve_outputs("quadratic", d$t[keep], d$y_gap[keep]))
outputs[["/n"]] <- exact(sum(keep), "complete rows")
outputs[["/missing_count"]] <- exact(sum(!keep), "rows with a missing response")
emit("STAT-REG-CURVE-ESTIMATION-004", "Curve estimation sequence with input positions", "missing_excluded",
list(response = "y_gap", predictor = "", sequence = "input_position", curves = list("linear", "quadratic"), confidence_level = 0.95),
outputs, "Blank predictor with input positions: t is the row's position 1..N in the table, so the missing row leaves the gap t = 6 (the t column of REG_CURVE equals the positions).")
outputs <- curve_outputs("linear", as.double(seq_len(sum(keep))), d$y_gap[keep])
outputs <- c(outputs, curve_outputs("quadratic", as.double(seq_len(sum(keep))), d$y_gap[keep]))
outputs[["/n"]] <- exact(sum(keep), "complete rows")
emit("STAT-REG-CURVE-ESTIMATION-005", "Curve estimation sequence with retained order", "axis_coverage",
list(response = "y_gap", predictor = "", sequence = "retained_order", curves = list("linear", "quadratic"), confidence_level = 0.95),
outputs, "Blank predictor with retained order: the 11 complete rows are renumbered t = 1..11 in table order.")
cat("curve estimation cases generated\n")
generators/r/regression_fit_axes.R
source("common.R", local = TRUE)
base <- read_dataset("REG_CORE")
positive <- base
positive$x <- positive$x + 4
positive$z <- positive$z + 3
positive$y <- positive$y + 10
types <- c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64")
export_dataset(positive, "REG_POSITIVE", "REG_CORE shifted to positive x, z and y",
"R stats::lm, sandwich::vcovHC; manual transformed-regression definitions",
"Constructed test data; no third-party dataset license.", types)
centered <- base
centered$x <- rep(c(-3, -2, -1, 1, 2, 3), length.out = nrow(centered))
centered$y <- 5 + 2 * centered$x + rep(c(0.2, -0.1, 0.3, -0.2, 0.1, -0.3),
length.out = nrow(centered))
export_dataset(centered, "REG_CENTERED", "REG_CORE with centered one-factor penalized design",
"R mean-centering identity for penalized linear regression with an unpenalized intercept",
"Constructed test data; no third-party dataset license.", types)
d <- read_dataset("REG_CORE")
p <- read_dataset("REG_POSITIVE")
c <- read_dataset("REG_CENTERED")
run <- generator_run(c("stats", "sandwich", "jsonlite"))
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("Independent R fit-axis reference:", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
emit <- function(id, entry, dataset, config, outputs, notes, kind = "nominal", deferred = list()) {
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = deferred, generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list("fitYByXMenuItem"),
title = paste("Fit axis", id), case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R lm and matrix algebra",
citation = "R stats::lm, sandwich::vcovHC documentation; Tabelz manual chapter 07",
generator = list(path = "generators/r/regression_fit_axes.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "All numeric references use re-read exported CSV values.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
fit_entry <- "tabelz.analysis.fit_y_by_x:compute_fits"
vif_entry <- "tabelz.analysis.fit_y_by_x:compute_fit_vif"
# Model-type Literal axis: every transformation matches the manual's response/X scale.
poly <- stats::lm(y ~ x + I(x^2), data = p)
emit("STAT-REG-AXIS-FIT-POLYNOMIAL-001", fit_entry, "REG_POSITIVE",
list(y_columns = list("y"), x_columns = list("x"), model_type = "polynomial"),
list("/responses/0/parameters/2/estimate" = tol(coef(poly)[[3]], "quadratic coefficient")),
list("Polynomial degree two adds the square of numeric x."))
exp_fit <- stats::lm(log(y) ~ x, data = p)
emit("STAT-REG-AXIS-FIT-EXPONENTIAL-001", fit_entry, "REG_POSITIVE",
list(y_columns = list("y"), x_columns = list("x"), model_type = "exponential"),
list("/responses/0/parameters/1/estimate" = tol(coef(exp_fit)[["x"]], "log-response slope")),
list("Exponential fits log(y) against untransformed x; the reported coefficient is on log scale."))
log_fit <- stats::lm(y ~ log(x), data = p)
emit("STAT-REG-AXIS-FIT-LOGARITHMIC-001", fit_entry, "REG_POSITIVE",
list(y_columns = list("y"), x_columns = list("x"), model_type = "logarithmic"),
list("/responses/0/parameters/1/estimate" = tol(coef(log_fit)[[2]], "log-x slope")),
list("Logarithmic model fits untransformed y against log(x)."))
power_fit <- stats::lm(log(y) ~ log(x), data = p)
emit("STAT-REG-AXIS-FIT-POWER-001", fit_entry, "REG_POSITIVE",
list(y_columns = list("y"), x_columns = list("x"), model_type = "power"),
list("/responses/0/parameters/1/estimate" = tol(coef(power_fit)[[2]], "log-log slope")),
list("Power model fits log(y) against log(x)."))
poly3 <- stats::lm(y ~ x + I(x^2) + I(x^3), data = p)
emit("STAT-REG-NUMERIC-FIT-DEGREE3-001", fit_entry, "REG_POSITIVE",
list(y_columns = list("y"), x_columns = list("x"), model_type = "polynomial", degree = 3L),
list("/responses/0/parameters/3/estimate" = tol(coef(poly3)[[4]], "cubic coefficient")),
list("Non-default polynomial degree three, with powers x, x-squared and x-cubed."),
kind = "axis_coverage")
interaction <- stats::lm(y ~ x * z, data = d)
emit("STAT-REG-AXIS-FIT-INTERACTIONS-001", fit_entry, "REG_CORE",
list(y_columns = list("y"), x_columns = list("x", "z"), interactions = TRUE),
list("/responses/0/parameters/3/estimate" = tol(coef(interaction)[["x:z"]], "x:z slope")),
list("Pairwise interaction is the product x*z, added after main effects."))
default_fit <- stats::lm(y ~ x + z, data = d)
for (strategy in c("neighbor_average", "knn"))
emit(paste0("STAT-REG-AXIS-FIT-MISSING-", gsub("_", "-", toupper(strategy)), "-001"),
fit_entry, "REG_CORE",
list(y_columns = list("y"), x_columns = list("x", "z"), missing = strategy),
list("/responses/0/parameters/1/estimate" = tol(coef(default_fit)[["x"]],
paste(strategy, "complete-data slope"))),
list("REG_CORE has no missing selected values, so this strategy's fitted model equals R complete-data OLS."))
# Ridge/lasso penalty selection has a Python-specific shuffled CV split. The
# unpenalized intercept is nevertheless an independent exact invariant here:
# mean(x)=0, so for any selected lambda its original-scale intercept is mean(y).
for (algorithm in c("ridge", "lasso"))
emit(paste0("STAT-REG-AXIS-FIT-", toupper(algorithm), "-001"), fit_entry,
"REG_CENTERED",
list(y_columns = list("y"), x_columns = list("x"), algorithm = algorithm,
cv_folds = 3L),
list("/responses/0/parameters/0/estimate" = tol(mean(c$y),
paste(algorithm, "centred-design intercept")),
"/responses/0/observations" = exact(nrow(c), "full centred dataset")),
list("For an unpenalized intercept, mean(x)=0 makes intercept=mean(y) for every penalty.",
"The selected penalty itself depends on Python's seeded K-fold assignment and is deferred."),
deferred = list(list(path = "/responses/0/optimal_lambda",
reason = "R and Python seeded K-fold assignments differ; selected penalty is not reproducible independently here.")))
hc3 <- sandwich::vcovHC(default_fit, type = "HC3")
emit("STAT-REG-AXIS-FIT-HC3-001", fit_entry, "REG_CORE",
list(y_columns = list("y"), x_columns = list("x", "z"), covariance = "hc3"),
list("/responses/0/parameters/1/std_error" = tol(sqrt(diag(hc3))[["x"]], "HC3 x SE")),
list("HC3 sandwich covariance, t inference with N-K residual degrees of freedom."))
X <- stats::model.matrix(default_fit)
e <- stats::residuals(default_fit)
score <- rowsum(X * as.vector(e), group = d$cluster)
B <- solve(crossprod(X))
G <- nrow(score)
N <- nrow(X)
K <- ncol(X)
cluster_cov <- (G / (G - 1)) * ((N - 1) / (N - K)) * B %*% crossprod(score) %*% B
emit("STAT-REG-AXIS-FIT-CLUSTER-001", fit_entry, "REG_CORE",
list(y_columns = list("y"), x_columns = list("x", "z"),
covariance = "cluster", cluster_column = "cluster"),
list("/responses/0/parameters/1/std_error" = tol(sqrt(diag(cluster_cov))[["x"]],
"CR1 x SE")),
list("Manual CR1 factor is G/(G-1)*(N-1)/(N-K); cluster scores are row sums of x_i*e_i.",
"No random split is applied; G=12 clusters."))
alpha_fit <- stats::lm(y ~ x + z, data = d)
ci <- stats::confint(alpha_fit, level = 0.9)
emit("STAT-REG-NUMERIC-FIT-ALPHA-001", fit_entry, "REG_CORE",
list(y_columns = list("y"), x_columns = list("x", "z"), alpha_level = 0.1),
list("/responses/0/parameters/1/lower_ci" = tol(ci["x", 1], "90 percent x lower CI")),
list("The manual passes alpha=1-confidence to the OLS t interval; 0.1 gives 90 percent."),
kind = "axis_coverage")
emit("STAT-REG-NUMERIC-FIT-HOLDOUT-001", fit_entry, "REG_CORE",
list(y_columns = list("y"), x_columns = list("x", "z"), split_percent = 80L),
list("/responses/0/training_observations" = exact(floor(0.8 * nrow(d)),
"80 percent training share")),
list("The holdout split is Python-seeded; only its deterministic training count is referenceable in R."),
kind = "axis_coverage",
deferred = list(list(path = "/responses/0/parameters/1/estimate",
reason = "Python's seeded row split is not the same as an R RNG split.")))
# VIF model and missing-strategy axes on complete data. One-factor VIF is one;
# two-factor transformed VIFs follow auxiliary OLS regressions.
vif_config <- function(model, missing = "drop", interactions = FALSE, degree = 2L)
list(y_columns = list("y"), x_columns = list("x", "z"), model_type = model,
missing = missing, degree = degree, interactions = interactions,
split_percent = 100L)
vif_r <- function(x, z) 1 / (1 - summary(stats::lm(x ~ z))$r.squared)
emit("STAT-REG-AXIS-VIF-EXPONENTIAL-001", vif_entry, "REG_POSITIVE",
vif_config("exponential"), list("/0" = tol(vif_r(p$x, p$z), "exponential X VIF")),
list("Exponential response transformation leaves the X design untransformed."))
for (model in c("logarithmic", "power"))
emit(paste0("STAT-REG-AXIS-VIF-", toupper(model), "-001"), vif_entry,
"REG_POSITIVE", vif_config(model),
list("/0" = tol(vif_r(log(p$x), log(p$z)), paste(model, "log-X VIF"))),
list("Both x and z are log transformed before computing auxiliary R-squared."))
poly_design <- data.frame(x = p$x, z = p$z, x2 = p$x^2, z2 = p$z^2)
poly_vif <- 1 / (1 - summary(stats::lm(x ~ z + x2 + z2, data = poly_design))$r.squared)
emit("STAT-REG-AXIS-VIF-POLYNOMIAL-001", vif_entry, "REG_POSITIVE",
vif_config("polynomial"), list("/0" = tol(poly_vif, "quadratic-design x VIF")),
list("Quadratic design includes x, z, x-squared and z-squared; R auxiliary regression conditions on the other three."))
for (strategy in c("neighbor_average", "knn"))
emit(paste0("STAT-REG-AXIS-VIF-MISSING-", gsub("_", "-", toupper(strategy)), "-001"),
vif_entry, "REG_CORE", vif_config("linear", missing = strategy),
list("/0" = tol(vif_r(d$x, d$z), paste(strategy, "complete-data VIF"))),
list("No selected field is missing, so imputation strategy cannot change the design."))
interaction_design <- data.frame(x = d$x, z = d$z, xz = d$x * d$z)
interaction_vif <- 1 / (1 - summary(stats::lm(x ~ z + xz, data = interaction_design))$r.squared)
emit("STAT-REG-AXIS-VIF-INTERACTIONS-001", vif_entry, "REG_CORE",
vif_config("linear", interactions = TRUE),
list("/0" = tol(interaction_vif, "x VIF with x:z interaction")),
list("The base X interaction x*z is an additional VIF design term."))
cat("regression fit-axis references generated\n")
generators/r/regression_forest.R
source("common.R", local = TRUE)
# Random forest regression (Statistics > Multivariate > Random forest regression). The forest
# is seeded resampling (bootstrap trees, random feature subsets) on a seeded split that R
# cannot reproduce, so only the deterministic outputs are compared: population counts, the
# documented split sizes, the tuning grid, predictor names and encoding sizes, the tree count
# and the permutation count. Every fitted metric is deferred with its reason. A second case
# checks the refusal of dependent rows. Every oracle reads the exported CSV.
run <- generator_run(c("stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/76-forest-penalty-quantiles.tex:13-20: one numeric response, numeric and/or categorical predictors, independent observations declared; missing responses are omitted and counted; at least 25 usable responses.",
"docs/manual/chapters/76-forest-penalty-quantiles.tex:22-27: the forest averages 500 regression trees on bootstrap samples with a random feature subset at each split.",
"docs/manual/chapters/76-forest-penalty-quantiles.tex:30-37: a seeded split reserves 20% of the usable rows, rounded up; five-fold cross-validation selects among 12 combinations of leaf sizes 1, 3, 5, feature fractions 0.5 and 1, and depth 10 or unlimited.",
"docs/manual/chapters/76-forest-penalty-quantiles.tex:54-59: permutation importance over ten permutations per predictor."
)
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
tol <- function(value, label) {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-12", rationale = label)
}
emit <- function(id, title, kind, configuration, outputs, deferred, notes, error = NULL) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = error, deferred_outputs = deferred, generator_run = run)
write_expected("regression", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.regression_forest:compute_regression_forest",
menu_items = list("regressionForestMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_FOREST.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "analytic",
source = "documented study design counts (manual chapter 76) evaluated by hand",
citation = "Breiman (2001) Random forests, Machine Learning 45, 5-32; Tabelz manual chapter 76 (study design: 20% test reservation rounded up, 5-fold CV, 12-candidate grid, 500 trees, 10 permutations).",
generator = list(path = "generators/r/regression_forest.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-FOREST-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("regression", id, case)
}
set.seed(106)
n <- 32
frame <- data.frame(
load = round(runif(n, 10, 50), 2),
speed = round(rnorm(n, 100, 15), 1),
tool = sample(c("carbide", "ceramic", "steel"), n, TRUE),
stringsAsFactors = FALSE
)
frame$wear <- round(0.05 * frame$load + 0.02 * frame$speed +
c(carbide = -0.5, ceramic = 0.3, steel = 0.8)[frame$tool] + rnorm(n, sd = 0.4), 3)
frame$wear[c(7, 20)] <- NA
export_dataset(frame, "REG_FOREST", "Thirty-two machining runs: tool wear from load, speed and tool material, two missing responses (seed 106)",
"documented forest study design counts",
"Constructed validation data; no third-party license.",
c(load = "Float64", speed = "Float64", tool = "String", wear = "Float64"))
d <- read_dataset("REG_FOREST")
usable <- sum(!is.na(d$wear))
test <- ceiling(0.2 * usable)
training <- usable - test
stopifnot(usable == 30, test == 6, training == 24)
# Encoded features: two numeric predictors plus one indicator per tool level seen in the
# training rows; every level has more rows than the 6 held out, so all three are in training.
stopifnot(all(table(d$tool[!is.na(d$wear)]) > test))
encoded <- 2L + length(unique(d$tool))
grid <- expand.grid(depth = c(10L, NA), features = c(0.5, 1), leaf = c(1L, 3L, 5L))
outputs <- list(
"/response" = exact("wear", "the response"),
"/numeric_predictors" = list(comparison = "sequence", values = list("load", "speed"),
rationale = "numeric predictors in declared order"),
"/categorical_predictors" = list(comparison = "sequence", values = list("tool"),
rationale = "categorical predictors in declared order"),
"/seed" = exact(7L, "the declared seed"),
"/encoded_feature_count" = exact(encoded, "two numeric columns plus one indicator per tool level, all seen in training"),
"/trees" = exact(500L, "the forest always grows 500 trees"),
"/source_count" = exact(as.integer(n), "rows in the table"),
"/excluded_count" = exact(0L, "no excluded row"),
"/missing_response_count" = exact(as.integer(n - usable), "rows without a response are omitted and counted"),
"/split/test_fraction" = tol(0.2, "the documented 20% reservation"),
"/split/seed" = exact(7L, "the split uses the declared seed"),
"/test_metrics/row_count" = exact(as.integer(test), "ceil(0.2 x 30) held-out rows"),
"/baseline_test_metrics/row_count" = exact(as.integer(test), "the baseline is scored on the same held-out rows"),
"/importance/0/predictor" = exact("load", "importance rows follow the predictors"),
"/importance/1/predictor" = exact("speed", "importance rows follow the predictors"),
"/importance/2/predictor" = exact("tool", "a categorical predictor is permuted as one variable"),
"/importance/0/permutations" = exact(10L, "ten permutations per predictor"),
"/artifact/fit" = exact("training", "the reported artifact is the training fit"),
"/artifact/fit_row_count" = exact(as.integer(training), "24 training rows"),
"/deployment_artifact/fit" = exact("deployment", "the deployment artifact is refitted on every usable row"),
"/deployment_artifact/fit_row_count" = exact(as.integer(usable), "all 30 usable rows")
)
for (i in seq_len(nrow(grid))) {
base <- sprintf("/candidates/%d/settings", i - 1L)
outputs[[paste0(base, "/min_samples_leaf")]] <- exact(grid$leaf[i], "grid: leaf sizes 1, 3, 5 (slowest)")
outputs[[paste0(base, "/max_features")]] <- tol(grid$features[i], "grid: feature fractions 0.5 and 1")
if (is.na(grid$depth[i])) {
outputs[[paste0(base, "/max_depth")]] <- list(comparison = "absent", rationale = "unlimited depth is null")
} else {
outputs[[paste0(base, "/max_depth")]] <- exact(grid$depth[i], "grid: depth 10 or unlimited (fastest)")
}
}
deferred <- list(
list(path = "/test_metrics/rmse", reason = "Seeded bootstrap forest on a numpy-seeded split: no independent R reproduction of the fitted metric; the study design is compared instead."),
list(path = "/test_metrics/mae", reason = "Seeded bootstrap forest; see /test_metrics/rmse."),
list(path = "/test_metrics/r_squared", reason = "Seeded bootstrap forest; see /test_metrics/rmse."),
list(path = "/baseline_cv_rmse", reason = "Depends on the numpy-seeded training/test split and fold assignment, which R cannot reproduce."),
list(path = "/baseline_test_metrics/rmse", reason = "Depends on the numpy-seeded split; the training mean is otherwise deterministic."),
list(path = "/selected_index", reason = "Selected by cross-validated forest RMSE of seeded trees."),
list(path = "/importance/0/mean_rmse_increase", reason = "Permutation importance of seeded trees.")
)
emit("STAT-REG-FOREST-001", "Regression forest study design on independent rows", "nominal",
list(response = "wear", numeric_predictors = list("load", "speed"),
categorical_predictors = list("tool"), dependence = "independent", seed = 7L),
outputs, deferred,
list("Deterministic study-design quantities only: 32 rows, 30 with a response, ceil(0.2 x 30) = 6 held out and 24 training rows; the 12-candidate grid is enumerated in the product's order (leaf size slowest, then feature fraction, then depth); 500 trees; 10 permutations.",
"Every fitted or resampled quantity is deferred with its reason: the forest's bootstrap and feature sampling and the numpy-seeded split have no independent R reproduction."))
emit("STAT-REG-FOREST-DEPENDENT-001", "Regression forest refuses dependent rows", "invalid_input",
list(response = "wear", numeric_predictors = list("load", "speed"),
categorical_predictors = list("tool"), dependence = "clustered", seed = 7L),
setNames(list(), character(0)), list(),
list("The manual requires independent observations for the random split; declaring clustered rows is refused before any fitting."),
error = list(error_class = "ValueError", message_contains = "needs independent observations"))
cat("regression forest cases generated\n")
generators/r/regression_gee.R
# Independent base-R implementation of population-average GEE (F100).
# Self-contained: no packages beyond base R and stats, JSON written by hand.
#
# Model (manual chapter 67): binomial-logit or Poisson-log marginal means,
# working variances m pi (1 - pi) or mu with the scale fixed at 1,
# V_i = A^(1/2) R_i A^(1/2), R_i = I or (1 - rho) I + rho 11'. rho is the
# pooled within-cluster Pearson-residual pair-product sum divided by M phi,
# M = sum n_i (n_i - 1) / 2 and phi = sum r^2 / N (statistician ruling #30),
# clamped to [-1/(n_max - 1) + 1e-8, 1 - 1e-8]. Fisher scoring on
# sum D' V^-1 (y - mu) = 0 with rho updated after each step until the maximum
# scaled change |d theta| / max(1, |theta|) <= 1e-8. Covariance: Mancl-DeRouen
# B^-1 [sum D' V^-1 (I - H)^-1 r r' (I - H)^-T V^-1 D] B^-1 with dense
# (I - H_i)^-1; t inference on G - p degrees of freedom.
# Run from this directory: Rscript regression_gee.R
ref <- normalizePath("../../reference_data", winslash = "/", mustWork = TRUE)
family_dir <- "regression"
entry <- "tabelz.analysis.generalized_estimating_equations:compute_generalized_estimating_equations"
generator <- "generators/r/regression_gee.R"
num <- function(x) sprintf("%.17g", x)
q <- function(x) paste0("\"", gsub("\"", "\\\\\"", x), "\"")
write_text <- function(lines, path) {
dir.create(dirname(path), recursive = TRUE, showWarnings = FALSE)
con <- file(path, open = "wb")
writeLines(lines, con, sep = "\n")
close(con)
}
set.seed(20260927)
rows <- list()
for (g in 1:24) {
effect <- rnorm(1, 0, 0.6)
for (j in seq_len(sample(2:5, 1))) {
x <- round(rnorm(1), 4)
group <- if (runif(1) < 0.5) "a" else "b"
p <- 1 / (1 + exp(-(-0.2 + 0.7 * x + 0.5 * (group == "b") + effect)))
exposure <- round(runif(1, 0.5, 2.5), 4)
rows[[length(rows) + 1]] <- data.frame(
cluster = sprintf("k%02d", g), x = x, group = group,
event = as.integer(runif(1) < p),
count = as.integer(rpois(1, exposure * exp(0.3 + 0.4 * x + 0.3 * effect))),
exposure = exposure, stringsAsFactors = FALSE)
}
}
d <- do.call(rbind, rows)
dataset_id <- "REG_GEE_CLUSTERED"
csv <- file.path(ref, "datasets", paste0(dataset_id, ".csv"))
write_text(c("cluster,x,group,event,count,exposure",
sprintf("%s,%s,%s,%d,%d,%s", d$cluster, num(d$x), d$group, d$event, d$count,
num(d$exposure))), csv)
write_text(c(
"{",
" \"schema_version\": \"2.0.0\",",
paste0(" \"dataset_id\": \"", dataset_id, "\","),
" \"source\": \"Constructed clustered study: 24 clusters of 2-5 rows with a Gaussian cluster effect, seed 20260927\",",
" \"citation\": \"R stats::rnorm, stats::rpois documentation\",",
" \"license_note\": \"Constructed validation data; no third-party license.\",",
" \"columns\": [",
" {\"name\": \"cluster\", \"dtype\": \"String\"},",
" {\"name\": \"x\", \"dtype\": \"Float64\"},",
" {\"name\": \"group\", \"dtype\": \"String\"},",
" {\"name\": \"event\", \"dtype\": \"Int64\"},",
" {\"name\": \"count\", \"dtype\": \"Int64\"},",
" {\"name\": \"exposure\", \"dtype\": \"Float64\"}",
" ],",
paste0(" \"row_count\": ", nrow(d), ","),
paste0(" \"csv\": {\"path\": \"reference_data/datasets/", dataset_id, ".csv\", \"sha256\": \"\"}"),
"}"), file.path(ref, "datasets", paste0(dataset_id, ".json")))
d <- read.csv(csv, stringsAsFactors = FALSE)
X <- cbind(1, d$x, as.numeric(d$group == "b"))
terms <- c("Intercept", "x", "group = b (vs a)")
clusters <- split(seq_len(nrow(d)), factor(d$cluster, levels = unique(d$cluster)))
gee <- function(y, m, off, family, working) {
p <- ncol(X)
beta <- rep(0, p)
beta[1] <- if (family == "binomial") qlogis(sum(y) / sum(m)) else log(sum(y) / sum(exp(off)))
nmax <- max(lengths(clusters))
lo <- -1 / (nmax - 1) + 1e-8; hi <- 1 - 1e-8
rho <- 0
moments <- function(beta) {
eta <- drop(X %*% beta)
if (family == "binomial") {
pi <- plogis(eta); list(mu = m * pi, v = m * pi * (1 - pi), dm = m * pi * (1 - pi))
} else {
mu <- exp(eta + off); list(mu = mu, v = mu, dm = mu)
}
}
vinv <- function(v, rho) {
n <- length(v); R <- (1 - rho) * diag(n) + rho * matrix(1, n, n)
S <- diag(sqrt(v), n); solve(S %*% R %*% S)
}
for (it in 1:100) {
s <- moments(beta); U <- rep(0, p); B <- matrix(0, p, p)
for (k in clusters) {
D <- s$dm[k] * X[k, , drop = FALSE]; W <- vinv(s$v[k], rho)
U <- U + t(D) %*% W %*% (y[k] - s$mu[k]); B <- B + t(D) %*% W %*% D
}
nb <- beta + drop(solve(B, U)); nr <- rho
if (working == "exchangeable") {
s2 <- moments(nb); e <- (y - s2$mu) / sqrt(s2$v); num <- 0; pairs <- 0
phi <- sum(e^2) / length(e)
for (k in clusters) {
n <- length(k); if (n < 2) next
num <- num + (sum(e[k])^2 - sum(e[k]^2)) / 2; pairs <- pairs + n * (n - 1) / 2
}
nr <- min(max(num / (pairs * phi), lo), hi)
}
old <- c(beta, rho); new <- c(nb, nr)
change <- max(abs(new - old) / pmax(1, abs(old)))
beta <- nb; rho <- nr
if (change <= 1e-8) break
}
s <- moments(beta); B <- matrix(0, p, p); parts <- list()
for (k in clusters) {
D <- s$dm[k] * X[k, , drop = FALSE]; W <- vinv(s$v[k], rho)
B <- B + t(D) %*% W %*% D; parts[[length(parts) + 1]] <- list(D = D, W = W, r = y[k] - s$mu[k])
}
Bi <- solve(B); M <- matrix(0, p, p)
for (part in parts) {
H <- part$D %*% Bi %*% t(part$D) %*% part$W
ra <- solve(diag(nrow(H)) - H, part$r)
g <- t(part$D) %*% part$W %*% ra; M <- M + g %*% t(g)
}
V <- Bi %*% M %*% Bi
df <- length(clusters) - p
se <- sqrt(diag(V)); tval <- beta / se; crit <- qt(0.975, df)
list(beta = beta, se = se, t = tval, p = 2 * pt(-abs(tval), df), lower = beta - crit * se,
upper = beta + crit * se, rho = rho, df = df, iterations = it)
}
tol <- function(value, rel, why) paste0(
"{\"comparison\": \"tolerance\", \"value\": \"", num(value), "\", \"absolute_tolerance\": \"0\", ",
"\"relative_tolerance\": \"", rel, "\", \"rationale\": ", q(why), "}")
exact <- function(value, why) paste0(
"{\"comparison\": \"exact\", \"value\": ", value, ", \"rationale\": ", q(why), "}")
emit <- function(id, title, config, fit, notes, kind = "nominal") {
out <- character()
for (i in seq_along(terms)) {
node <- paste0("/coefficients/[term=", terms[i], "]")
out <- c(out,
paste0(" ", q(paste0(node, "/estimate")), ": ", tol(fit$beta[i], "1e-7", "Fisher-scoring root of the estimating equations (stops at scaled change 1e-8).")),
paste0(" ", q(paste0(node, "/std_error")), ": ", tol(fit$se[i], "1e-6", "Mancl-DeRouen sandwich from dense (I - H_i)^-1.")),
paste0(" ", q(paste0(node, "/p_value")), ": ", tol(fit$p[i], "1e-5", "Two-sided t on G - p df.")),
paste0(" ", q(paste0(node, "/lower")), ": ", tol(fit$lower[i], "1e-6", "estimate - t(0.975, G - p) SE.")),
paste0(" ", q(paste0(node, "/upper")), ": ", tol(fit$upper[i], "1e-6", "estimate + t(0.975, G - p) SE.")))
}
out <- c(out, paste0(" \"/degrees_of_freedom\": ", exact(fit$df, "G - p = 24 - 3.")))
if (config$working == "exchangeable") {
out <- c(out, paste0(" \"/rho\": ", tol(fit$rho, "1e-6", "Pearson pair-product sum / (M phi), phi = sum r^2 / N (ruling #30).")))
}
write_text(c("{", " \"schema_version\": \"2.0.0\",", paste0(" \"case_id\": \"", id, "\","),
" \"outputs\": {", paste(out, collapse = ",\n"), " },",
paste0(" \"generator_run\": {\"r_version\": ", q(R.version.string), ", \"packages\": {\"stats\": ",
q(as.character(packageVersion("stats"))), "}}"), "}"),
file.path(ref, "expected", family_dir, paste0(id, ".json")))
cfg <- c(paste0("\"family\": ", q(config$family)), "\"response_column\": \"event\"",
"\"cluster_column\": \"cluster\"", "\"predictors\": [\"x\", \"group\"]",
paste0("\"working_correlation\": ", q(config$working)))
if (config$family == "poisson") {
cfg[2] <- "\"response_column\": \"count\""; cfg <- c(cfg, "\"exposure_column\": \"exposure\"")
}
write_text(c(
"{", " \"schema_version\": \"2.0.0\",", paste0(" \"case_id\": \"", id, "\","),
" \"version\": \"1.0.0\",", paste0(" \"family\": \"", family_dir, "\","),
paste0(" \"entry_point\": \"", entry, "\","), " \"menu_items\": [\"geeMenuItem\"],",
paste0(" \"title\": ", q(title), ","), paste0(" \"case_kind\": \"", kind, "\","),
paste0(" \"dataset\": {\"path\": \"reference_data/datasets/", dataset_id, ".json\", \"sha256\": \"\"},"),
" \"excluded_rows\": [],", paste0(" \"configuration\": {", paste(cfg, collapse = ", "), "},"),
" \"repeat\": 1,",
paste0(" \"expected\": {\"path\": \"reference_data/expected/", family_dir, "/", id, ".json\", \"sha256\": \"\"},"),
" \"oracle\": {", " \"oracle_class\": \"independent_implementation\",",
" \"source\": \"Base-R GEE with explicit dense block matrices\",",
" \"citation\": \"Liang and Zeger (1986); Mancl and DeRouen (2001); formulas as stated in manual chapter 67\",",
paste0(" \"generator\": {\"path\": \"", generator, "\", \"sha256\": \"\"},"),
" \"independent\": true,",
paste0(" \"convention_notes\": [", paste(q(c(notes, "All reference calculations re-read the exported CSV.")), collapse = ", "), "]"),
" },", " \"supported\": true,", " \"unsupported_reason\": null,",
" \"requirement_ids\": [\"REQ-STAT-REG-001\"],", " \"risk_ids\": [\"RISK-NUMERIC-001\"],",
" \"review_reference\": null", "}"), file.path(ref, "cases", family_dir, paste0(id, ".json")))
}
b <- gee(d$event, rep(1, nrow(d)), rep(0, nrow(d)), "binomial", "exchangeable")
emit("STAT-REG-GEE-001", "Binomial GEE with an exchangeable working correlation",
list(family = "binomial", working = "exchangeable"), b,
c(sprintf("Converged in %d iterations; rho = %.10g.", b$iterations, b$rho)))
pz <- gee(d$count, rep(1, nrow(d)), log(d$exposure), "poisson", "independence")
emit("STAT-REG-GEE-002", "Poisson GEE with exposure and independence working correlation",
list(family = "poisson", working = "independence"), pz,
c("Independence coefficients equal the Poisson GLM; the covariance is the bias-reduced sandwich."),
kind = "axis_coverage")
cat("wrote STAT-REG-GEE-001 and STAT-REG-GEE-002\n")
generators/r/regression_glm_general.R
source("common.R", local = TRUE)
library(MASS)
library(sandwich)
# The general GLM window (sum-to-zero factor coding, IRLS by expected information). R's
# stats::glm with contr.sum on sorted factor levels is the oracle for the fits; Type III Wald
# tests, marginal means, Holm comparisons, HC0 sandwich, maximum-likelihood dispersions and
# information criteria are written out from the fitted coefficients and covariance. Every
# oracle re-reads the exported CSV.
set.seed(20261012)
n <- 40L
g <- rep(c("A", "B", "C"), length.out = n)
x <- round(runif(n, -1.5, 2.5), 3)
exposure <- round(runif(n, 0.5, 3), 3)
eta <- 0.4 + c(A = -0.3, B = 0.1, C = 0.2)[g] + 0.35 * x
count <- rpois(n, exposure * exp(eta))
trials <- sample(5:12, n, replace = TRUE)
events <- rbinom(n, trials, stats::pnorm(-0.3 + c(A = -0.5, B = 0.2, C = 0.3)[g] + 0.4 * x))
ypos <- round(rgamma(n, shape = 6, scale = exp(0.5 + c(A = -0.2, B = 0.1, C = 0.1)[g] + 0.25 * x) / 6), 3)
nbcount <- rnbinom(n, mu = exp(1.0 + c(A = -0.4, B = 0.2, C = 0.2)[g] + 0.3 * x), size = 2.5)
constructed <- data.frame(g = g, x = x, exposure = exposure, count = count, trials = trials,
events = events, ypos = ypos, nbcount = nbcount,
stringsAsFactors = FALSE)
constructed$log_exposure <- round(log(constructed$exposure), 6)
constructed <- constructed[c("g", "x", "exposure", "log_exposure", "count", "trials",
"events", "ypos", "nbcount")]
export_dataset(
constructed, "REG_GLM_01", "Constructed by regression_glm_general.R",
"R stats::glm, MASS::glm.nb/theta.ml/gamma.dispersion, sandwich::vcovHC help pages",
"Constructed test data; no third-party dataset license.",
c(g = "String", x = "Float64", exposure = "Float64", log_exposure = "Float64",
count = "Int64", trials = "Int64", events = "Int64", ypos = "Float64", nbcount = "Int64")
)
d <- read_dataset("REG_GLM_01")
d$g <- factor(d$g, levels = sort(unique(d$g)))
run <- generator_run(c("stats", "MASS", "sandwich", "jsonlite"))
control <- stats::glm.control(epsilon = 1e-14, maxit = 200)
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste("Independent R glm (IRLS to 1e-14) versus HypoNum IRLS (deviance criterion 1e-10), an iterative fit whose derived quantities (link-scale means, theta-conditional limits) can sit near zero:", label))
prob <- function(x, label, tier = "1e-7")
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = tier,
rationale = paste("Independent R tail probability from an iterative fit:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, kind, config, outputs, notes) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.generalized_linear_models:compute_generalized_linear",
menu_items = list("generalizedLinearMenuItem"), title = gsub("-", " ", id),
case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_GLM_01.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R stats::glm with contr.sum, MASS::glm.nb/theta.ml, MASS::gamma.dispersion, sandwich::vcovHC(type = 'HC0'); Wald/F/LR tests and marginal means from the fitted coefficients",
citation = "R Core Team, stats package help: glm, summary.glm, logLik.glm; Venables & Ripley (2002) MASS: glm.nb, theta.ml, gamma.dispersion; Zeileis (2006) sandwich.",
generator = list(path = "generators/r/regression_glm_general.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes,
"Factors are sum-to-zero coded on sorted levels (last level = -1), covariates enter as recorded; coefficient tests are t with Type III F when the dispersion is a moment estimate (normal: deviance/df; gamma: Pearson/df) and z with chi-square otherwise (docs/manual/chapters/86-regression-and-count-models.tex:107-170).",
"Log-likelihood, AIC and BIC of true-likelihood families are evaluated at the maximum-likelihood dispersion and count the dispersion (and theta) as parameters.",
"Marginal means average the linear predictor over the equal-weight factor grid at covariate means and the mean offset, then invert the link; pairwise differences are on the link scale with Holm-adjusted p-values and unadjusted intervals (manual lines 160-169).",
"Expected computed from the exported CSV."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-GENERALIZED-LINEAR-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
holm <- function(p) {
o <- order(p); m <- length(p); out <- numeric(m); running <- 0
for (r in seq_len(m)) {
running <- max(running, min(1, (m - r + 1) * p[o[r]]))
out[o[r]] <- running
}
out
}
# Outputs shared by every case: coefficients, Type III Wald tests, fit statistics.
glm_outputs <- function(fit, V, moment, level, terms, type3_terms, exp_link = FALSE) {
beta <- stats::coef(fit)
X <- stats::model.matrix(fit)
n <- nrow(X); p <- ncol(X); df <- n - p
se <- sqrt(diag(V))
stat <- beta / se
if (moment) {
crit <- stats::qt(0.5 + level / 2, df)
pv <- 2 * stats::pt(abs(stat), df, lower.tail = FALSE)
} else {
crit <- stats::qnorm(0.5 + level / 2)
pv <- 2 * stats::pnorm(abs(stat), lower.tail = FALSE)
}
out <- list("/n" = exact(as.integer(n), "complete rows"),
"/statistic_kind" = exact(if (moment) "t" else "z", "reference distribution"),
"/type3_kind" = exact(if (moment) "F" else "chi-square", "Type III reference"),
"/fit/df_residual" = exact(as.integer(df), "residual df"),
"/fit/df_null" = exact(as.integer(n - 1L), "null df"),
"/fit/deviance" = tol(stats::deviance(fit), "deviance"),
"/fit/null_deviance" = tol(fit$null.deviance, "null deviance"),
"/fit/pearson_chi_square" = tol(sum(stats::residuals(fit, type = "pearson")^2), "Pearson chi-square"))
for (j in seq_along(terms)) {
base <- sprintf("/coefficients/%d/", j - 1L)
out[[paste0(base, "term")]] <- exact(terms[[j]], "coefficient label")
out[[paste0(base, "estimate")]] <- tol(beta[[j]], "estimate")
out[[paste0(base, "standard_error")]] <- tol(se[[j]], "standard error")
out[[paste0(base, "statistic")]] <- tol(stat[[j]], "test statistic")
out[[paste0(base, "p_value")]] <- prob(pv[[j]], "two-sided tail")
out[[paste0(base, "lower")]] <- tol(beta[[j]] - crit * se[[j]], "confidence limit")
out[[paste0(base, "upper")]] <- tol(beta[[j]] + crit * se[[j]], "confidence limit")
if (exp_link) {
out[[paste0(base, "exp_estimate")]] <- tol(exp(beta[[j]]), "exp(estimate)")
out[[paste0(base, "exp_lower")]] <- tol(exp(beta[[j]] - crit * se[[j]]), "exp(limit)")
out[[paste0(base, "exp_upper")]] <- tol(exp(beta[[j]] + crit * se[[j]]), "exp(limit)")
}
}
for (k in seq_along(type3_terms)) {
idx <- type3_terms[[k]]$columns
b <- beta[idx]
wald <- drop(t(b) %*% solve(V[idx, idx, drop = FALSE], b))
q <- length(idx)
base <- sprintf("/type3/%d/", k - 1L)
out[[paste0(base, "term")]] <- exact(type3_terms[[k]]$name, "Type III term")
out[[paste0(base, "df")]] <- exact(as.integer(q), "Type III df")
if (moment) {
out[[paste0(base, "statistic")]] <- tol(wald / q, "Type III F")
out[[paste0(base, "denominator_df")]] <- exact(as.integer(df), "Type III denominator df")
out[[paste0(base, "p_value")]] <- prob(stats::pf(wald / q, q, df, lower.tail = FALSE), "Type III F tail")
} else {
out[[paste0(base, "statistic")]] <- tol(wald, "Type III Wald chi-square")
out[[paste0(base, "p_value")]] <- prob(stats::pchisq(wald, q, lower.tail = FALSE), "Type III chi-square tail")
}
}
out
}
# Marginal means of factor g (levels A, B, C) at the covariate mean and link-scale offset base.
means_outputs <- function(fit, V, moment, level, inverse, mu_eta, base = 0, ratio = FALSE) {
beta <- stats::coef(fit)
n <- length(stats::fitted(fit)); df <- n - length(beta)
crit <- if (moment) stats::qt(0.5 + level / 2, df) else stats::qnorm(0.5 + level / 2)
xbar <- mean(d$x)
rows <- list(A = c(1, 1, 0, xbar), B = c(1, 0, 1, xbar), C = c(1, -1, -1, xbar))
out <- list()
levels <- names(rows)
for (i in seq_along(rows)) {
eta <- base + sum(rows[[i]] * beta)
se <- sqrt(drop(t(rows[[i]]) %*% V %*% rows[[i]]))
pre <- sprintf("/means/%d/", i - 1L)
out[[paste0(pre, "level")]] <- exact(levels[[i]], "factor level")
out[[paste0(pre, "link_mean")]] <- tol(eta, "link-scale marginal mean")
out[[paste0(pre, "link_se")]] <- tol(se, "link-scale SE")
out[[paste0(pre, "link_lower")]] <- tol(eta - crit * se, "link-scale limit")
out[[paste0(pre, "link_upper")]] <- tol(eta + crit * se, "link-scale limit")
out[[paste0(pre, "mean")]] <- tol(inverse(eta), "response-scale marginal mean")
out[[paste0(pre, "mean_se")]] <- tol(abs(mu_eta(eta)) * se, "delta-method SE")
}
pairs <- list(c("A", "B"), c("A", "C"), c("B", "C"))
diffs <- lapply(pairs, function(pair) {
cvec <- rows[[pair[[1]]]] - rows[[pair[[2]]]]
diff <- sum(cvec * beta)
se <- sqrt(drop(t(cvec) %*% V %*% cvec))
stat <- diff / se
pv <- if (moment) 2 * stats::pt(abs(stat), df, lower.tail = FALSE) else 2 * stats::pnorm(abs(stat), lower.tail = FALSE)
list(diff = diff, se = se, stat = stat, p = pv)
})
adjusted <- holm(vapply(diffs, function(item) item$p, 0))
for (i in seq_along(pairs)) {
pre <- sprintf("/comparisons/%d/", i - 1L)
item <- diffs[[i]]
out[[paste0(pre, "first")]] <- exact(pairs[[i]][[1]], "first level")
out[[paste0(pre, "second")]] <- exact(pairs[[i]][[2]], "second level")
out[[paste0(pre, "difference")]] <- tol(item$diff, "link-scale difference")
out[[paste0(pre, "standard_error")]] <- tol(item$se, "difference SE")
out[[paste0(pre, "statistic")]] <- tol(item$stat, "difference statistic")
out[[paste0(pre, "p_value")]] <- prob(item$p, "unadjusted two-sided tail")
out[[paste0(pre, "holm_p_value")]] <- prob(adjusted[[i]], "Holm-adjusted tail")
out[[paste0(pre, "lower")]] <- tol(item$diff - crit * item$se, "difference limit")
out[[paste0(pre, "upper")]] <- tol(item$diff + crit * item$se, "difference limit")
if (ratio) out[[paste0(pre, "ratio")]] <- tol(exp(item$diff), "exp(difference)")
}
out
}
coef_terms <- c("Intercept", "g[sum:A]", "g[sum:B]", "x")
type3 <- list(list(name = "g", columns = 2:3), list(name = "x", columns = 4L))
sum_contrasts <- list(g = "contr.sum")
# 001: normal identity, deviance/df dispersion, t and F inference, ML-dispersion likelihood.
fit <- stats::glm(ypos ~ g + x, family = stats::gaussian(), data = d, contrasts = sum_contrasts,
control = control)
p <- length(stats::coef(fit)); df <- n - p
scale <- stats::deviance(fit) / df
V <- scale * summary(fit)$cov.unscaled
ll <- as.numeric(stats::logLik(fit)) # Gaussian logLik uses the ML dispersion deviance/n
stopifnot(abs(ll - (-0.5 * n * log(2 * pi * stats::deviance(fit) / n) - n / 2)) < 1e-9)
emit("STAT-REG-GENERALIZED-LINEAR-001", "nominal",
list(response = "ypos", family = "gaussian", factors = list("g"), covariates = list("x"),
link = "canonical", scale = "default", covariance = "model", confidence_level = 0.95,
comparisons = "holm", lr_tests = FALSE),
c(glm_outputs(fit, V, TRUE, 0.95, coef_terms, type3),
list("/fit/scale" = tol(scale, "deviance / residual df"),
"/fit/ml_scale" = tol(stats::deviance(fit) / n, "ML dispersion deviance / n"),
"/fit/log_likelihood" = tol(ll, "Gaussian log-likelihood at the ML dispersion"),
"/fit/aic" = tol(-2 * ll + 2 * (p + 1), "AIC counting the dispersion"),
"/fit/bic" = tol(-2 * ll + log(n) * (p + 1), "BIC counting the dispersion"),
"/fit/parameters_counted" = exact(as.integer(p + 1L), "coefficients plus dispersion")),
means_outputs(fit, V, TRUE, 0.95, identity, function(eta) 1)),
list("stats::glm(ypos ~ g + x, gaussian) with contr.sum; dispersion deviance/df gives t and F inference."))
# 002: Poisson log link with a log-exposure offset, likelihood-ratio term tests, 90% intervals.
fit <- stats::glm(count ~ g + x + offset(log_exposure), family = stats::poisson(), data = d,
contrasts = sum_contrasts, control = control)
p <- length(stats::coef(fit))
V <- summary(fit)$cov.unscaled
ll <- as.numeric(stats::logLik(fit))
drop_g <- stats::glm(count ~ x + offset(log_exposure), family = stats::poisson(), data = d, control = control)
drop_x <- stats::glm(count ~ g + offset(log_exposure), family = stats::poisson(), data = d,
contrasts = sum_contrasts, control = control)
lr_g <- 2 * (ll - as.numeric(stats::logLik(drop_g)))
lr_x <- 2 * (ll - as.numeric(stats::logLik(drop_x)))
emit("STAT-REG-GENERALIZED-LINEAR-002", "axis_coverage",
list(response = "count", family = "poisson", factors = list("g"), covariates = list("x"),
offset = "log_exposure", link = "canonical", scale = "default", covariance = "model",
confidence_level = 0.90, comparisons = "holm", lr_tests = TRUE),
c(glm_outputs(fit, V, FALSE, 0.90, coef_terms, type3, exp_link = TRUE),
list("/fit/scale" = tol(1, "fixed dispersion", "1e-12"),
"/fit/log_likelihood" = tol(ll, "Poisson log-likelihood"),
"/fit/aic" = tol(-2 * ll + 2 * p, "AIC"),
"/fit/bic" = tol(-2 * ll + log(n) * p, "BIC"),
"/fit/parameters_counted" = exact(as.integer(p), "coefficients only"),
"/lr_tests/0/term" = exact("g", "LR test term"),
"/lr_tests/0/df" = exact(2L, "LR df"),
"/lr_tests/0/statistic" = tol(lr_g, "LR chi-square dropping g"),
"/lr_tests/0/p_value" = prob(stats::pchisq(lr_g, 2, lower.tail = FALSE), "LR tail"),
"/lr_tests/1/term" = exact("x", "LR test term"),
"/lr_tests/1/df" = exact(1L, "LR df"),
"/lr_tests/1/statistic" = tol(lr_x, "LR chi-square dropping x"),
"/lr_tests/1/p_value" = prob(stats::pchisq(lr_x, 1, lower.tail = FALSE), "LR tail")),
means_outputs(fit, V, FALSE, 0.90, exp, exp, base = mean(d$log_exposure), ratio = TRUE)),
list("stats::glm(count ~ g + x + offset(log_exposure), poisson); LR tests refit without each term; marginal means use the mean offset."))
# 003: binomial events/trials with the probit link and the HC0 sandwich covariance.
fit <- stats::glm(cbind(events, trials - events) ~ g + x, family = stats::binomial(link = "probit"),
data = d, contrasts = sum_contrasts, control = control)
X <- stats::model.matrix(fit)
u <- stats::residuals(fit, type = "working") * stats::weights(fit, type = "working")
bread <- summary(fit)$cov.unscaled
V <- bread %*% crossprod(X * u) %*% bread
stopifnot(max(abs(V - sandwich::vcovHC(fit, type = "HC0"))) < 1e-10)
ll <- as.numeric(stats::logLik(fit))
p <- length(stats::coef(fit))
emit("STAT-REG-GENERALIZED-LINEAR-003", "axis_coverage",
list(response = "events", trials = "trials", family = "binomial", factors = list("g"),
covariates = list("x"), link = "probit", scale = "default", covariance = "hc0",
confidence_level = 0.95, comparisons = "holm", lr_tests = FALSE),
c(glm_outputs(fit, V, FALSE, 0.95, coef_terms, type3),
list("/fit/log_likelihood" = tol(ll, "binomial log-likelihood with the combinatorial term"),
"/fit/aic" = tol(-2 * ll + 2 * p, "AIC"),
"/fit/parameters_counted" = exact(as.integer(p), "coefficients only")),
means_outputs(fit, V, FALSE, 0.95, stats::pnorm, stats::dnorm)),
list("stats::glm(cbind(events, trials - events) ~ g + x, binomial(probit)); HC0 = (X'WX)^-1 [sum u_i^2 x_i x_i'] (X'WX)^-1 with u_i = m_i (y_i - mu_i) mu_eta_i / V(mu_i), equal to sandwich::vcovHC(type = 'HC0')."))
# 004: gamma with the canonical inverse link; Pearson/df dispersion for inference, ML dispersion
# for the likelihood and information criteria.
fit <- stats::glm(ypos ~ g + x, family = stats::Gamma(link = "inverse"), data = d,
contrasts = sum_contrasts, control = control)
p <- length(stats::coef(fit)); df <- n - p
pearson <- sum(stats::residuals(fit, type = "pearson")^2)
scale <- pearson / df
stopifnot(abs(scale - summary(fit)$dispersion) < 1e-12 * scale)
V <- scale * summary(fit)$cov.unscaled
mu <- stats::fitted(fit)
target <- stats::deviance(fit) / (2 * n)
shape <- stats::uniroot(function(s) log(s) - digamma(s) - target, c(1e-3, 1e6), tol = 1e-14)$root
ml_scale <- 1 / shape
stopifnot(abs(ml_scale - MASS::gamma.dispersion(fit)) < 1e-6)
ll <- sum(stats::dgamma(d$ypos, shape = shape, scale = mu * ml_scale, log = TRUE))
emit("STAT-REG-GENERALIZED-LINEAR-004", "axis_coverage",
list(response = "ypos", family = "gamma", factors = list("g"), covariates = list("x"),
link = "canonical", scale = "default", covariance = "model", confidence_level = 0.95,
comparisons = "holm", lr_tests = FALSE),
c(glm_outputs(fit, V, TRUE, 0.95, coef_terms, type3),
list("/fit/scale" = tol(scale, "Pearson chi-square / residual df"),
"/fit/ml_scale" = tol(ml_scale, "ML dispersion solving log(nu) - digamma(nu) = D/(2n)"),
"/fit/log_likelihood" = tol(ll, "gamma log-likelihood at the ML dispersion"),
"/fit/aic" = tol(-2 * ll + 2 * (p + 1), "AIC counting the dispersion"),
"/fit/bic" = tol(-2 * ll + log(n) * (p + 1), "BIC counting the dispersion"),
"/fit/parameters_counted" = exact(as.integer(p + 1L), "coefficients plus dispersion")),
means_outputs(fit, V, TRUE, 0.95, function(eta) 1 / eta, function(eta) -1 / eta^2)),
list("stats::glm(ypos ~ g + x, Gamma(inverse)); the moment dispersion is Pearson/df (summary.glm) and the ML dispersion solves log(nu) - digamma(nu) = D/(2n) (MASS::gamma.dispersion)."))
# 005: negative binomial with theta estimated by maximum likelihood (log link, conditional-theta
# expected-information coefficient covariance). MASS::glm.nb is polished by alternating exact
# theta.ml and glm steps until theta is stable to 1e-12.
nb <- MASS::glm.nb(nbcount ~ g + x, data = d, contrasts = sum_contrasts)
theta <- nb$theta
for (iteration in 1:100) {
fit <- stats::glm(nbcount ~ g + x, family = MASS::negative.binomial(theta), data = d,
contrasts = sum_contrasts, control = control)
updated <- as.numeric(MASS::theta.ml(d$nbcount, stats::fitted(fit), limit = 200, eps = 1e-14))
if (abs(log(updated) - log(theta)) < 1e-13) { theta <- updated; break }
theta <- updated
}
fit <- stats::glm(nbcount ~ g + x, family = MASS::negative.binomial(theta), data = d,
contrasts = sum_contrasts, control = control)
mu <- stats::fitted(fit)
y <- d$nbcount
p <- length(stats::coef(fit))
V <- summary(fit, dispersion = 1)$cov.unscaled
ll <- sum(stats::dnbinom(y, size = theta, mu = mu, log = TRUE))
info <- sum(-trigamma(y + theta) + trigamma(theta) - 1 / theta + 2 / (mu + theta) - (y + theta) / (mu + theta)^2)
emit("STAT-REG-GENERALIZED-LINEAR-005", "axis_coverage",
list(response = "nbcount", family = "negative_binomial", factors = list("g"),
covariates = list("x"), link = "canonical", theta = "estimate", scale = "default",
covariance = "model", confidence_level = 0.95, comparisons = "holm", lr_tests = FALSE),
c(glm_outputs(fit, V, FALSE, 0.95, coef_terms, type3, exp_link = TRUE),
list("/fit/theta" = tol(theta, "ML theta", "1e-7"),
"/fit/theta_se" = tol(1 / sqrt(info), "theta SE from the observed information", "1e-6"),
"/fit/log_likelihood" = tol(ll, "negative binomial log-likelihood"),
"/fit/aic" = tol(-2 * ll + 2 * (p + 1), "AIC counting theta"),
"/fit/bic" = tol(-2 * ll + log(n) * (p + 1), "BIC counting theta"),
"/fit/parameters_counted" = exact(as.integer(p + 1L), "coefficients plus theta"))),
list("MASS::glm.nb polished by alternating MASS::theta.ml (eps 1e-14) and glm(negative.binomial(theta)) steps; theta SE = 1/sqrt(observed information) as in theta.ml; coefficient covariance is the expected information at fixed theta with dispersion 1.",
"Theta and its SE carry 1e-7 and 1e-6 relative tolerances because both sides reach theta by an alternating search rather than in closed form."))
generators/r/regression_hard.R
source("common.R", local = TRUE)
base <- read_dataset("REG_CORE")
base$x <- base$x / 10000
base$y <- base$y * 1000000
export_dataset(base, "REG_SCALE", "REG_CORE rescaled in predictor and response units",
"R stats::lm/glm; MASS::polr/glm.nb; nnet::multinom documentation",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64"))
d <- read_dataset("REG_SCALE")
run <- generator_run(c("stats", "MASS", "nnet", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R rescaled-design", label))
emit <- function(id, entry, menu, config, outputs, notes) {
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = paste("Extreme scale", id),
case_kind = "hard_numeric",
dataset = list(path = "reference_data/datasets/REG_SCALE.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R scaled-data models",
citation = "R stats::lm/glm, MASS::polr/glm.nb, nnet::multinom help pages",
generator = list(path = "generators/r/regression_hard.R", sha256 = ""), independent = TRUE,
convention_notes = c(notes, "R re-reads REG_SCALE; x is REG_CORE x / 10000 and y is REG_CORE y * 1000000.",
"This is an extreme-unit-scale numerical challenge, not a change of statistical model.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-NUMERIC-001"),
review_reference = NULL))
}
fit <- stats::lm(y ~ x + z, data = d)
emit("STAT-REG-HARD-OLS-001", "tabelz.analysis.fit_y_by_x:compute_fits",
"fitYByXMenuItem", list(y_columns = list("y"), x_columns = list("x", "z")),
list("/responses/0/parameters/1/estimate" = tol(coef(fit)[["x"]], "x slope", "1e-9")),
list("OLS slope changes by 1e10 under the opposing x/y unit scales."))
vif <- 1 / (1 - summary(stats::lm(x ~ z, data = d))$r.squared)
emit("STAT-REG-HARD-VIF-001", "tabelz.analysis.fit_y_by_x:compute_fit_vif",
"fitYByXMenuItem",
list(y_columns = list("y"), x_columns = list("x", "z"), missing = "drop",
model_type = "linear", degree = 2L, interactions = FALSE, split_percent = 100L),
list("/0" = tol(vif, "scale-invariant VIF", "1e-9")),
list("Auxiliary R lm R-squared is unchanged by linear rescaling of x."))
b <- stats::glm(I(binary == "event") ~ x, family = stats::binomial(), data = d)
emit("STAT-REG-HARD-BINARY-001",
"tabelz.analysis.logistic_regression:compute_binary_logistic_regression",
"logisticRegressionMenuItem",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
list("/coefficients/1/coefficient" = tol(coef(b)[["x"]], "rescaled logit slope")),
list("The binomial logit slope changes inversely with predictor units."))
# polr and multinom do not converge on the rescaled design itself (on REG_SCALE they
# stop at lower likelihoods than the equivalent REG_CORE fits), so these two
# references fit REG_CORE and apply the exact reparameterization: x / 1e4 multiplies
# the x coefficient by 1e4 and leaves the likelihood unchanged.
core <- read_dataset("REG_CORE")
core$ord <- ordered(core$ordinal, levels = c("low", "middle", "high"))
core$nom <- factor(core$nominal, levels = c("A", "B", "C"))
o <- MASS::polr(ord ~ x, data = core, method = "logistic", Hess = TRUE,
control = list(maxit = 1000, reltol = 1e-15))
emit("STAT-REG-HARD-ORDINAL-001",
"tabelz.analysis.ordinal_logistic:compute_ordinal_logistic_regression",
"ordinalRegressionMenuItem",
list(outcome_column = "ordinal", predictors = list("x"),
level_order = list("low", "middle", "high")),
list("/coefficients/0/coefficient" = tol(1e4 * coef(o)[["x"]], "REG_CORE ordered-logit slope times 1e4 (exact rescaling)")),
list("MASS polr uses the same low-to-high slope sign."))
n <- nnet::multinom(nom ~ x, data = core, trace = FALSE, maxit = 1000, reltol = 1e-15)
emit("STAT-REG-HARD-NOMINAL-001",
"tabelz.analysis.nominal_logistic:compute_nominal_logistic_regression",
"nominalRegressionMenuItem",
list(outcome_column = "nominal", reference_value = "A", predictors = list("x")),
list("/coefficients/1/coefficient" = tol(1e4 * coef(n)["B", "x"], "REG_CORE nominal slope times 1e4 (exact rescaling)")),
list("R nnet B-versus-A log relative-risk slope; reference category A."))
p <- stats::glm(count ~ x + offset(log(exposure)), data = d, family = stats::poisson())
emit("STAT-REG-HARD-POISSON-001",
"tabelz.analysis.poisson_regression:compute_poisson_regression",
"poissonRegressionMenuItem",
list(count_column = "count", predictors = list("x"), exposure_column = "exposure"),
list("/coefficients/1/coefficient" = tol(coef(p)[["x"]], "rescaled Poisson slope")),
list("Log exposure is an offset; x slope changes inversely with x units."))
nb <- MASS::glm.nb(nb_count ~ x + offset(log(exposure)), data = d,
control = stats::glm.control(maxit = 1000, epsilon = 1e-12))
emit("STAT-REG-HARD-NB-001", "tabelz.analysis.negative_binomial:compute_negative_binomial",
"nbRegressionMenuItem",
list(count = "nb_count", predictors = list("x"), exposure = "exposure"),
list("/coefficients/1/coefficient" = tol(coef(nb)[["x"]], "rescaled NB2 slope")),
list("MASS glm.nb theta is inverse alpha; compare only coefficient here."))
g <- stats::lm(y ~ x + group, data = d,
contrasts = list(group = stats::contr.sum(3)))
emit("STAT-REG-HARD-GENERAL-001", "tabelz.executor.general_linear",
"generalLinearMenuItem",
list(response_column = "y", factor_columns = list("group"), covariate_columns = list("x"),
test_type = "III"),
list("/coefficients/x" = tol(coef(g)[["x"]], "rescaled general-linear slope", "1e-9")),
list("Sum-to-zero factor contrasts; large response and small predictor units."))
e <- stats::lm(y ~ x + group, data = d)
emit("STAT-REG-HARD-EFFECTS-001", "tabelz.executor.linear_effects",
"linearEffectsMenuItem",
list(response_column = "y", factor_columns = list("group"), covariate_columns = list("x")),
list("/coefficients/x" = tol(coef(e)[["x"]], "rescaled effects slope", "1e-9")),
list("Treatment factor contrasts; large response and small predictor units."))
cat("regression hard-numeric references generated\n")
generators/r/regression_invalid.R
source("common.R", local = TRUE)
core <- read_dataset("REG_CORE")
one <- core[1L, , drop = FALSE]
export_dataset(one, "REG_ONE", "First row of exported REG_CORE",
"R stats::lm/glm and the Tabelz manual's minimum-design conditions",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64"))
# Two rows whose x, count and nb_count all vary: only the row-count gate
# (n <= fitted coefficients) can refuse an intercept-plus-slope count model.
two <- core[1:2, , drop = FALSE]
stopifnot(length(unique(two$x)) == 2L, length(unique(two$count)) == 2L,
length(unique(two$nb_count)) == 2L)
export_dataset(two, "REG_TWO", "First two rows of exported REG_CORE",
"R stats::glm and the Tabelz manual's minimum-design conditions",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64"))
run <- generator_run(c("stats", "jsonlite"))
emit <- function(id, entry, menu, dataset, config, error_class, message, notes) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id,
outputs = structure(list(), names = character()),
expected_error = list(error_class = error_class, message_contains = message),
deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = paste("Invalid design:", id),
case_kind = "invalid_input",
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "analytic", source = "Manual design condition",
citation = "Tabelz user manual, chapters/07-models-and-tests.tex, 07-linear-effects.tex and 08-general-linear.tex",
generator = list(path = "generators/r/regression_invalid.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, if (dataset == "REG_TWO")
"REG_TWO is two exported source rows whose x and counts vary: no more rows than coefficients."
else "REG_ONE is a single exported source row: below the model's residual-df minimum.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
fit <- "tabelz.analysis.fit_y_by_x:compute_fits"
vif <- "tabelz.analysis.fit_y_by_x:compute_fit_vif"
binary <- "tabelz.analysis.logistic_regression:compute_binary_logistic_regression"
ordinal <- "tabelz.analysis.ordinal_logistic:compute_ordinal_logistic_regression"
nominal <- "tabelz.analysis.nominal_logistic:compute_nominal_logistic_regression"
poisson <- "tabelz.analysis.poisson_regression:compute_poisson_regression"
nb <- "tabelz.analysis.negative_binomial:compute_negative_binomial"
general <- "tabelz.executor.general_linear"
effects <- "tabelz.executor.linear_effects"
fit_menu <- "fitYByXMenuItem"
emit("STAT-REG-INVALID-OLS-MIN-001", fit, fit_menu, "REG_ONE",
list(y_columns = list("y"), x_columns = list("x")), "ValueError", "row",
list("One source row cannot supply positive residual degrees of freedom for intercept and slope."))
emit("STAT-REG-INVALID-OLS-CONFIG-001", fit, fit_menu, "REG_CORE",
list(y_columns = list("y"), x_columns = list("x"), alpha_level = 2),
"ValueError", "alpha level",
list("A significance level of 2 lies outside the open unit interval."))
vif_config <- list(y_columns = list("y"), x_columns = list("x", "z"),
missing = "drop", model_type = "linear", degree = 2L,
interactions = FALSE, split_percent = 100L)
emit("STAT-REG-INVALID-VIF-MIN-001", vif, fit_menu, "REG_ONE",
vif_config, "ValueError", "row",
list("One row cannot estimate the auxiliary regression for a VIF."))
vif_invalid <- vif_config
vif_invalid$missing <- "bad"
emit("STAT-REG-INVALID-VIF-CONFIG-001", vif, fit_menu, "REG_CORE",
vif_invalid, "ValueError", "missing",
list("A nonexistent missing-value strategy is not an estimator."))
emit("STAT-REG-INVALID-BINARY-MIN-001", binary, "logisticRegressionMenuItem", "REG_ONE",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
"ValueError", "exactly two",
list("A one-row population cannot contain two observed outcome categories."))
emit("STAT-REG-INVALID-BINARY-CONFIG-001", binary, "logisticRegressionMenuItem", "REG_CORE",
list(outcome_column = "binary", event_value = "event", predictors = list("x"), threshold = 1),
"ValueError", "threshold",
list("Manual: classification threshold is strictly between zero and one."))
emit("STAT-REG-INVALID-ORDINAL-MIN-001", ordinal, "ordinalRegressionMenuItem", "REG_ONE",
list(outcome_column = "ordinal", predictors = list("x")),
"ValueError", "three",
list("A one-row population cannot supply three ordered outcome levels."))
emit("STAT-REG-INVALID-ORDINAL-CONFIG-001", ordinal, "ordinalRegressionMenuItem", "REG_CORE",
list(outcome_column = "ordinal", predictors = list("x"), confidence_level = 1),
"ValueError", "Confidence level",
list("Confidence level one lies outside the supported half-open interval."))
emit("STAT-REG-INVALID-NOMINAL-MIN-001", nominal, "nominalRegressionMenuItem", "REG_ONE",
list(outcome_column = "nominal", predictors = list("x")),
"ValueError", "three",
list("A one-row population cannot supply three nominal categories."))
emit("STAT-REG-INVALID-NOMINAL-CONFIG-001", nominal, "nominalRegressionMenuItem", "REG_CORE",
list(outcome_column = "nominal", predictors = list("x"), confidence_level = 1),
"ValueError", "Confidence level",
list("Confidence level one lies outside the supported half-open interval."))
emit("STAT-REG-INVALID-POISSON-MIN-001", poisson, "poissonRegressionMenuItem", "REG_TWO",
list(count_column = "count", predictors = list("x")),
"ValueError", "More complete rows",
list("Two rows cannot fit an intercept and a predictor with positive residual degrees of freedom.",
"Until 2026-09-25 this case used REG_ONE, where a constant predictor is refused first."))
emit("STAT-REG-INVALID-POISSON-CONFIG-001", poisson, "poissonRegressionMenuItem", "REG_CORE",
list(count_column = "count", predictors = list("x"), confidence_level = 1),
"ValueError", "Confidence level",
list("Confidence level one lies outside the supported half-open interval."))
emit("STAT-REG-INVALID-NB-MIN-001", nb, "nbRegressionMenuItem", "REG_TWO",
list(count = "nb_count", predictors = list("x")),
"ValueError", "More complete rows",
list("Two rows cannot identify NB2 intercept, slope and dispersion.",
"Until 2026-09-25 this case used REG_ONE, where a constant response is refused first."))
emit("STAT-REG-INVALID-NB-CONFIG-001", nb, "nbRegressionMenuItem", "REG_CORE",
list(count = "nb_count", predictors = list("x"), confidence_level = 1),
"ValueError", "confidence_level",
list("Confidence level one violates the strict NB2 configuration bound."))
emit("STAT-REG-INVALID-GENERAL-MIN-001", general, "generalLinearMenuItem", "REG_ONE",
list(response_column = "y", covariate_columns = list("x")),
"GeneralLinearExecutionError", "at least two",
list("The manual requires positive residual df; one row is below even the two-observation gate."))
emit("STAT-REG-INVALID-GENERAL-CONFIG-001", general, "generalLinearMenuItem", "REG_CORE",
list(response_column = "y", covariate_columns = list("x"), confidence_level = 1),
"ValidationError", "confidence_level",
list("Confidence level one violates the strict general-linear configuration bound."))
emit("STAT-REG-INVALID-EFFECTS-MIN-001", effects, "linearEffectsMenuItem", "REG_ONE",
list(response_column = "y", covariate_columns = list("x")),
"LinearEffectsExecutionError", "at least two",
list("The manual requires positive residual df; one row is below even the two-observation gate."))
emit("STAT-REG-INVALID-EFFECTS-CONFIG-001", effects, "linearEffectsMenuItem", "REG_CORE",
list(response_column = "y", covariate_columns = list("x"), confidence_level = 1),
"ValidationError", "confidence_level",
list("Confidence level one violates the strict linear-effects configuration bound."))
cat("regression invalid references generated\n")
generators/r/regression_lastmile.R
source("common.R", local = TRUE)
d <- read_dataset("REG_CORE")
small <- read_dataset("REG_BIN_MIN")
run <- generator_run(c("stats", "car", "jsonlite"))
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R last-mile reference:", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
emit <- function(id, entry, menu, kind, dataset, config, outputs, notes) {
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = paste("Regression completion", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R regression fit",
citation = "R stats::glm/lm and car::Anova help pages; Tabelz manual chapters 07 and 08",
generator = list(path = "generators/r/regression_lastmile.R", sha256 = ""),
independent = TRUE, convention_notes = c(notes, "The fit uses only the exported CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
binary_entry <- "tabelz.analysis.logistic_regression:compute_binary_logistic_regression"
binary_menu <- "logisticRegressionMenuItem"
small_fit <- stats::glm(I(binary == "event") ~ x, data = small,
family = stats::binomial())
emit("STAT-REG-BOUNDARY-BINARY-MIN-PASS-001", binary_entry, binary_menu,
"boundary", "REG_BIN_MIN",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
list("/n" = exact(3, "three-row accepted minimum"),
"/coefficients/0/coefficient" = tol(coef(small_fit)[[1]], "nonzero intercept")),
list("Symmetric events at x=-1 and +1 make the slope mathematically zero; compare the nonzero intercept and n instead."))
binary_fit <- stats::glm(I(binary == "event") ~ x, data = d,
family = stats::binomial())
pred <- as.vector(stats::predict(binary_fit, type = "response"))
index <- which.min(abs(pred - 0.5))
spacing <- min(abs(pred[-index] - pred[[index]]))
if (!is.finite(spacing) || spacing <= 0) stop("Threshold design needs isolated prediction")
delta <- spacing / 3
for (side in c("below", "above")) {
threshold <- pred[[index]] + if (side == "below") -delta else delta
if (!(threshold > 0 && threshold < 1)) stop("Threshold not in open unit interval")
accuracy <- mean((pred >= threshold) == (d$binary == "event"))
emit(paste0("STAT-REG-THRESHOLD-BINARY-", toupper(side), "-001"),
binary_entry, binary_menu, "threshold", "REG_CORE",
list(outcome_column = "binary", event_value = "event", predictors = list("x"),
threshold = threshold),
list("/accuracy" = tol(accuracy, paste("accuracy", side, "isolated prediction"))),
list("Threshold is one-third of the nearest probability gap below/above the same R-fitted row.",
"Exactly one row's class changes between the paired thresholds."))
}
general_entry <- "tabelz.executor.general_linear"
general_fit <- stats::lm(y ~ group + x, data = d,
contrasts = list(group = stats::contr.sum(3)))
emit("STAT-REG-NUMERIC-GENERAL-MAXROWS-001", general_entry,
"generalLinearMenuItem", "axis_coverage", "REG_CORE",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"), max_rows = 100L),
list("/observations" = exact(nrow(d), "72 below explicit row cap 100"),
"/residual_variance" = tol(summary(general_fit)$sigma^2,
"general-linear residual variance")),
list("Manual fixes individual intervals at 95%; max_rows=100 is the valid non-default numeric setting."))
effects_fit <- stats::lm(y ~ group + x, data = d)
emit("STAT-REG-NUMERIC-EFFECTS-MAXROWS-001", "tabelz.executor.linear_effects",
"linearEffectsMenuItem", "axis_coverage", "REG_CORE",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"), max_rows = 100L),
list("/observations" = exact(nrow(d), "72 below explicit row cap 100"),
"/residual_variance" = tol(summary(effects_fit)$sigma^2,
"linear-effects residual variance")),
list("Manual fixes individual intervals at 95%; max_rows=100 is the valid non-default numeric setting."))
interacting <- stats::lm(y ~ group * x, data = d,
contrasts = list(group = stats::contr.sum(3)))
type2 <- car::Anova(interacting, type = 2)
emit("STAT-REG-INTERACT-GENERAL-TYPEII-001", general_entry,
"generalLinearMenuItem", "axis_coverage", "REG_CORE",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"), interactions = list(list("group", "x")),
test_type = "II"),
list("/term_tests/0/f_statistic" = tol(type2["group", "F value"],
"Type II group effect in interaction model")),
list("Manual Type II preserves marginality: group main effect is not adjusted for group:x interaction.",
"R car::Anova(type=2) supplies the comparison on the same sum-coded design."))
cat("regression last-mile references generated\n")
generators/r/regression_loglinear.R
source("common.R", local = TRUE)
# Loglinear models for a 2 x 3 x 2 count table (General and Logit windows). The oracle is
# stats::glm(poisson) on the cell table with treatment contrasts whose reference is the last
# (or first, or a recorded) level, exactly HypoNum's coding; multinomial and product-
# multinomial log-likelihoods, profile-likelihood limits and the logit contrasts are written
# out from the Poisson fit. Every oracle re-reads the exported CSV.
cells <- expand.grid(C = c("c1", "c2"), B = c("b1", "b2", "b3"), A = c("a1", "a2"),
stringsAsFactors = FALSE)[c("A", "B", "C")]
cells$n <- c(18L, 7L, 12L, 9L, 6L, 0L, # a1: (b1,c1) (b1,c2) (b2,c1) (b2,c2) (b3,c1) (b3,c2)
10L, 11L, 8L, 14L, 5L, 9L) # a2
cells$weight <- c(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1.5, 1.5)
export_dataset(
cells, "REG_LOGLINEAR_01", "Constructed by regression_loglinear.R",
"R stats::glm (poisson) help page; Agresti (2013) Categorical Data Analysis ch. 9-10",
"Constructed test data; no third-party dataset license.",
c(A = "String", B = "String", C = "String", n = "Int64", weight = "Float64")
)
d <- read_dataset("REG_LOGLINEAR_01")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste("Independent R glm(poisson) on the cell table (IRLS to 1e-14) versus HypoNum (coefficient criterion 1e-10), an iterative fit whose Wald limits subtract nearly equal quantities:", label))
zero <- function(label)
list(comparison = "tolerance", value = "0", absolute_tolerance = "1e-9",
relative_tolerance = "0",
rationale = paste("Analytically zero for a cell the model fits exactly (leverage 1):", label))
prob <- function(x, label, tier = "1e-7")
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = tier,
rationale = paste("Independent R tail probability from an iterative fit:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, menu, kind, config, outputs, notes) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.loglinear:compute_loglinear",
menu_items = list(menu), title = gsub("-", " ", id),
case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_LOGLINEAR_01.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R stats::glm(family = poisson) on the cell table with contr.treatment at the declared reference; rstandard/residuals for adjusted and deviance residuals; profile limits by uniroot on the constrained deviance; logit contrasts from the Poisson coefficients",
citation = "R Core Team, stats package help: glm, rstandard.glm, residuals.glm, uniroot; Agresti (2013) Categorical Data Analysis, ch. 9 (loglinear models, sampling schemes) and 10 (logit-loglinear equivalence).",
generator = list(path = "generators/r/regression_loglinear.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes,
"log mu_c = o_c + x_c' lambda over the allowable cells in reference coding (last level by default); G^2 and X^2 on df = allowable cells - rank; the Poisson, multinomial and product-multinomial fitted counts coincide and each scheme's own log-likelihood is reported (docs/manual/chapters/86-regression-and-count-models.tex:171-197).",
"Adjusted residual (y - mu)/sqrt(mu (1 - h)) with the Poisson leverage (R rstandard type pearson); deviance residual sign(y - mu) sqrt(d_c).",
"The logit model fixes the explanatory factors' interaction; each logit parameter equals the loglinear parameter whose term contains the dependent factor (manual lines 193-197; chapters/106-loglinear-extensions.tex:74-118).",
"Expected computed from the exported CSV."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(if (menu == "loglinearLogitMenuItem") "REQ-STAT-REG-LOGLINEAR-LOGIT-001" else "REQ-STAT-REG-LOGLINEAR-GENERAL-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
control <- stats::glm.control(epsilon = 1e-14, maxit = 200)
levels_of <- list(A = c("a1", "a2"), B = c("b1", "b2", "b3"), C = c("c1", "c2"))
# Fit the Poisson loglinear model with the given references; returns the glm and the
# HypoNum-style parameter table (term, label, index into coef).
loglinear <- function(data, formula, references, offset = NULL, pseudocount = FALSE) {
data <- data
for (f in c("A", "B", "C")) {
lev <- levels_of[[f]]
ref <- references[[f]]
data[[f]] <- factor(data[[f]], levels = c(ref, setdiff(lev, ref))) # treatment reference first
}
if (pseudocount) data$n <- data$n + 0.5
if (!is.null(offset)) data$o <- log(offset) else data$o <- 0
fit <- suppressWarnings(stats::glm(formula, family = stats::poisson(), data = data,
offset = o, control = control))
list(fit = fit, data = data)
}
# HypoNum's parameter order: intercept, then every implied term lowest order first (factors
# in table order), and within a term the product of the kept levels in level order.
parameter_table <- function(fit, terms, references) {
coefs <- stats::coef(fit)
rows <- list(list(term = "(intercept)", label = "", name = "(Intercept)"))
for (term in terms) {
kept <- lapply(term, function(f) setdiff(levels_of[[f]], references[[f]]))
grid <- expand.grid(rev(kept), stringsAsFactors = FALSE)[rev(seq_along(term))]
for (i in seq_len(nrow(grid))) {
combo <- unlist(grid[i, ])
names(combo) <- term
rname <- paste0(paste0(term, combo), collapse = ":")
stopifnot(rname %in% names(coefs))
rows[[length(rows) + 1L]] <- list(
term = paste(term, collapse = "*"),
label = paste0(term, "=", combo, collapse = ", "), name = rname)
}
}
rows
}
fit_outputs <- function(fit, rows, level, loglik, support = rep(TRUE, 12L)) {
V <- summary(fit)$cov.unscaled
coefs <- stats::coef(fit)
crit <- stats::qnorm(0.5 + level / 2)
df <- fit$df.residual
dev <- stats::deviance(fit)
pearson <- sum(stats::residuals(fit, type = "pearson")^2)
out <- list("/fit/df" = exact(as.integer(df), "allowable cells minus rank"),
"/fit/rank" = exact(as.integer(fit$rank), "design rank on the support"),
"/fit/allowable_cells" = exact(as.integer(sum(support)), "allowable cells"),
"/fit/structural_zero_cells" = exact(as.integer(sum(!support)), "structural zeros"),
"/fit/deviance" = tol(dev, "G^2"),
"/fit/pearson" = tol(pearson, "X^2"),
"/fit/deviance_p" = prob(stats::pchisq(dev, df, lower.tail = FALSE), "G^2 tail"),
"/fit/pearson_p" = prob(stats::pchisq(pearson, df, lower.tail = FALSE), "X^2 tail"),
"/fit/log_likelihood" = tol(loglik, "sampling-scheme log-likelihood"),
"/fit/boundary" = exact(FALSE, "interior maximum-likelihood fit"),
"/total" = tol(sum(d$n), "table total", "1e-12"))
for (j in seq_along(rows)) {
r <- rows[[j]]
est <- coefs[[r$name]]
se <- sqrt(V[r$name, r$name])
base <- sprintf("/fit/parameters/%d/", j - 1L)
out[[paste0(base, "term")]] <- exact(r$term, "parameter term")
out[[paste0(base, "label")]] <- exact(r$label, "parameter levels")
out[[paste0(base, "estimate")]] <- tol(est, "loglinear parameter")
out[[paste0(base, "standard_error")]] <- tol(se, "Wald standard error")
out[[paste0(base, "z_value")]] <- tol(est / se, "Wald z")
out[[paste0(base, "p_value")]] <- prob(2 * stats::pnorm(abs(est / se), lower.tail = FALSE), "Wald two-sided tail")
out[[paste0(base, "lower")]] <- tol(est - crit * se, "Wald limit")
out[[paste0(base, "upper")]] <- tol(est + crit * se, "Wald limit")
}
mu <- stats::fitted(fit)
adjusted <- stats::rstandard(fit, type = "pearson")
leverage <- stats::hatvalues(fit)
devres <- stats::residuals(fit, type = "deviance")
position <- 0L
for (c in seq_len(12L)) {
base <- sprintf("/fit/cells/%d/", c - 1L)
out[[paste0(base, "observed")]] <- tol(d$n[[c]], "observed count", "1e-12")
out[[paste0(base, "structural")]] <- exact(!support[[c]], "structural-zero flag")
if (!support[[c]]) next
position <- position + 1L
out[[paste0(base, "expected")]] <- tol(mu[[position]], "fitted count")
if (leverage[[position]] < 1 - 1e-12) {
out[[paste0(base, "residual")]] <- tol((fit$y[[position]] - mu[[position]]) / sqrt(mu[[position]]), "Pearson residual")
out[[paste0(base, "adjusted_residual")]] <- tol(adjusted[[position]], "adjusted residual")
out[[paste0(base, "deviance_residual")]] <- tol(devres[[position]], "deviance residual")
} else {
# A cell fitted exactly (leverage 1): residuals are zero and no adjusted residual exists.
out[[paste0(base, "residual")]] <- zero("Pearson residual")
out[[paste0(base, "deviance_residual")]] <- zero("deviance residual")
}
}
out
}
poisson_loglik <- function(fit) {
y <- fit$y; mu <- stats::fitted(fit)
sum(ifelse(y > 0, y * log(mu), 0) - mu - lgamma(y + 1))
}
multinomial_loglik <- function(fit) {
y <- fit$y; mu <- stats::fitted(fit); N <- sum(y)
sum(ifelse(y > 0, y * log(mu), 0)) - sum(lgamma(y + 1)) - N * log(N) + lgamma(N + 1)
}
product_multinomial_loglik <- function(fit, strata) {
y <- fit$y; mu <- stats::fitted(fit)
base <- sum(ifelse(y > 0, y * log(mu), 0)) - sum(lgamma(y + 1))
for (key in unique(strata)) {
total <- sum(y[strata == key])
if (total > 0) base <- base + lgamma(total + 1) - total * log(total)
}
base
}
last <- list(A = "a2", B = "b3", C = "c2")
first <- list(A = "a1", B = "b1", C = "c1")
# GENERAL 001: Poisson sampling, generating class A*B; C, reference = last level.
m <- loglinear(d, n ~ A * B + C, last)
rows <- parameter_table(m$fit, list("A", "B", "C", c("A", "B")), last)
stopifnot(all(stats::fitted(m$fit) > 0))
emit("STAT-REG-LOGLINEAR-GENERAL-001", "loglinearGeneralMenuItem", "nominal",
list(factors = list("A", "B", "C"), count = "n", terms = list("A*B", "C"),
sampling = "poisson", coding = "last", confidence_level = 0.95,
conditional_test = "off"),
c(fit_outputs(m$fit, rows, 0.95, poisson_loglik(m$fit)),
list("/fit/generators/0" = exact("A*B", "generating term"),
"/fit/generators/1" = exact("C", "generating term"),
"/reference_levels/0/1" = exact("a2", "reference of A"),
"/reference_levels/1/1" = exact("b3", "reference of B"))),
list("glm(n ~ A*B + C, poisson) with contr.treatment(base = last level); the sampling zero at (a1, b3, c2) stays an observation and the fit is interior."))
# GENERAL 002: multinomial sampling, first-level coding, 90% Wald intervals.
m <- loglinear(d, n ~ A * B + C, first)
rows <- parameter_table(m$fit, list("A", "B", "C", c("A", "B")), first)
emit("STAT-REG-LOGLINEAR-GENERAL-002", "loglinearGeneralMenuItem", "axis_coverage",
list(factors = list("A", "B", "C"), count = "n", terms = list("A*B", "C"),
sampling = "multinomial", coding = "first", confidence_level = 0.90,
conditional_test = "off"),
c(fit_outputs(m$fit, rows, 0.90, multinomial_loglik(m$fit)),
list("/reference_levels/0/1" = exact("a1", "reference of A"),
"/reference_levels/1/1" = exact("b1", "reference of B"))),
list("Multinomial log-likelihood = Poisson kernel - N log N + lgamma(N + 1) with N the table total; first-level reference coding."))
# GENERAL 003: product-multinomial with the totals of A fixed; generating class B*C (A is added).
m <- loglinear(d, n ~ B * C + A, last)
rows <- parameter_table(m$fit, list("A", "B", "C", c("B", "C")), last)
emit("STAT-REG-LOGLINEAR-GENERAL-003", "loglinearGeneralMenuItem", "axis_coverage",
list(factors = list("A", "B", "C"), count = "n", terms = list("B*C"),
sampling = "product_multinomial", fixed_margins = list("A"), coding = "last",
confidence_level = 0.95, conditional_test = "off"),
c(fit_outputs(m$fit, rows, 0.95, product_multinomial_loglik(m$fit, d$A)),
list("/fit/generators/0" = exact("B*C", "generating term"),
"/fit/generators/1" = exact("A", "the fixed stratum factor"))),
list("Product-multinomial: the stratum factor A enters the generating class; log-likelihood adds lgamma(n_a + 1) - n_a log n_a per stratum."))
# GENERAL 004: pseudocount 0.5, a cell-structure offset, a recorded reference B=b2 and profile
# intervals; the profile limit of each parameter is the root of deviance(v) - deviance_min =
# qchisq(0.95, 1) with the parameter fixed by an offset.
m <- loglinear(d, n ~ A * B + C, list(A = "a2", B = "b2", C = "c2"), offset = d$weight, pseudocount = TRUE)
refs <- list(A = "a2", B = "b2", C = "c2")
rows <- parameter_table(m$fit, list("A", "B", "C", c("A", "B")), refs)
X <- stats::model.matrix(m$fit)
profile_limit <- function(j, direction) {
est <- stats::coef(m$fit)[[j]]
se <- sqrt(summary(m$fit)$cov.unscaled[j, j])
dmin <- stats::deviance(m$fit)
target <- stats::qchisq(0.95, 1)
gap <- function(v) {
fixed <- X[, j] * v + m$data$o
refit <- suppressWarnings(stats::glm(m$data$n ~ X[, -j] - 1, family = stats::poisson(),
offset = fixed, control = control))
stats::deviance(refit) - dmin - target
}
lo <- est + direction * 1e-6 * se
hi <- est + direction * 10 * se
stats::uniroot(gap, sort(c(lo, hi)), tol = 1e-12)$root
}
out <- fit_outputs(m$fit, rows, 0.95, poisson_loglik(m$fit))
for (j in c(2L, 5L)) {
base <- sprintf("/fit/parameters/%d/", j - 1L)
out[[paste0(base, "profile_lower")]] <- tol(profile_limit(j, -1), "profile-likelihood limit (uniroot, tol 1e-12)", "1e-7")
out[[paste0(base, "profile_upper")]] <- tol(profile_limit(j, +1), "profile-likelihood limit (uniroot, tol 1e-12)", "1e-7")
}
out[["/reference_levels/1/1"]] <- exact("b2", "recorded reference of B")
emit("STAT-REG-LOGLINEAR-GENERAL-004", "loglinearGeneralMenuItem", "axis_coverage",
list(factors = list("A", "B", "C"), count = "n", terms = list("A*B", "C"),
sampling = "poisson", coding = "last", confidence_level = 0.95,
cell_structure = "weight", pseudocount = TRUE, reference_levels = list("B=b2"),
intervals = "wald_and_profile", conditional_test = "off"),
out,
list("Counts + 0.5 in every cell (named pseudocount analysis), offset log(weight) from the cell structure, B's reference recorded as b2 (A and C keep the last level).",
"Profile limits for A [A=a1] and C [C=c1]: the constrained deviance rises by chi-square(1, 0.95) = 3.841; found by uniroot on refits with the parameter fixed through the offset (manual chapters/86 lines 202-212)."))
# GENERAL 005: the zero cell (a1, b3, c2) declared a structural zero leaves the support.
support <- !(d$A == "a1" & d$B == "b3" & d$C == "c2")
m <- loglinear(d[support, ], n ~ A * B + C, last)
rows <- parameter_table(m$fit, list("A", "B", "C", c("A", "B")), last)
emit("STAT-REG-LOGLINEAR-GENERAL-005", "loglinearGeneralMenuItem", "axis_coverage",
list(factors = list("A", "B", "C"), count = "n", terms = list("A*B", "C"),
sampling = "poisson", coding = "last", confidence_level = 0.95,
structural_zeros = list("A=a1; B=b3; C=c2"), conditional_test = "off"),
fit_outputs(m$fit, rows, 0.95, poisson_loglik(m$fit), support),
list("The structural zero is dropped from the fit: 11 allowable cells, rank 7, df 4; every parameter stays estimable on the support (manual lines 178-183)."))
# LOGIT 001: dependent C with explanatory associations A*C and B*C (A*B fixed).
m <- loglinear(d, n ~ A * B + A:C + B:C + C, last)
rows <- parameter_table(m$fit, list("A", "B", "C", c("A", "B"), c("A", "C"), c("B", "C")), last)
V <- summary(m$fit)$cov.unscaled
coefs <- stats::coef(m$fit)
logit_rows <- Filter(function(r) grepl("C", r$term, fixed = TRUE), rows)
baseline <- loglinear(d, n ~ A * B + C, last)$fit
step1 <- loglinear(d, n ~ A * B + C + A:C, last)$fit
out <- fit_outputs(m$fit, rows, 0.95, poisson_loglik(m$fit))
for (j in seq_along(logit_rows)) {
r <- logit_rows[[j]]
base <- sprintf("/logit/%d/", j - 1L)
out[[paste0(base, "logit")]] <- exact("log(C=c1 / C=c2)", "the logit against the reference")
explanatory <- setdiff(strsplit(r$term, "*", fixed = TRUE)[[1]], "C")
out[[paste0(base, "term")]] <- exact(if (length(explanatory)) paste(explanatory, collapse = "*") else "(intercept)", "explanatory term")
out[[paste0(base, "estimate")]] <- tol(coefs[[r$name]], "logit parameter = loglinear parameter")
out[[paste0(base, "standard_error")]] <- tol(sqrt(V[r$name, r$name]), "logit parameter SE")
}
out[["/joint/total_association"]] <- tol(stats::deviance(baseline) - stats::deviance(m$fit), "G^2 of A*B + C minus G^2 of the requested model")
out[["/joint/total_df"]] <- exact(as.integer(baseline$df.residual - m$fit$df.residual), "rank gained from the baseline")
out[["/joint/partition/0/added"]] <- exact("A", "first explanatory set")
out[["/joint/partition/0/lr_chi_square"]] <- tol(stats::deviance(baseline) - stats::deviance(step1), "G^2 decrease adding A*C")
out[["/joint/partition/0/df"]] <- exact(as.integer(baseline$df.residual - step1$df.residual), "rank gained adding A*C")
out[["/joint/partition/0/p_value"]] <- prob(stats::pchisq(stats::deviance(baseline) - stats::deviance(step1), baseline$df.residual - step1$df.residual, lower.tail = FALSE), "chi-square tail")
out[["/joint/partition/1/added"]] <- exact("B", "second explanatory set")
out[["/joint/partition/1/lr_chi_square"]] <- tol(stats::deviance(step1) - stats::deviance(m$fit), "G^2 decrease adding B*C")
out[["/joint/partition/1/df"]] <- exact(as.integer(step1$df.residual - m$fit$df.residual), "rank gained adding B*C")
out[["/joint/partition/1/p_value"]] <- prob(stats::pchisq(stats::deviance(step1) - stats::deviance(m$fit), step1$df.residual - m$fit$df.residual, lower.tail = FALSE), "chi-square tail")
emit("STAT-REG-LOGLINEAR-LOGIT-001", "loglinearLogitMenuItem", "nominal",
list(factors = list("A", "B", "C"), count = "n", dependent = list("C"),
terms = list("A*C", "B*C"), sampling = "poisson", coding = "last",
confidence_level = 0.95, conditional_test = "off"),
out,
list("glm(n ~ A*B + C + A:C + B:C, poisson): the logit parameters for log(C=c1/C=c2) are the loglinear parameters of C, A*C and B*C; the sequential association partition adds A*C then B*C to the baseline A*B + C (chapters/106-loglinear-extensions.tex:74-118)."))
# LOGIT 002: multinomial sampling, first-level coding, only the A*C association.
m <- loglinear(d, n ~ A * B + C + A:C, first)
rows <- parameter_table(m$fit, list("A", "B", "C", c("A", "B"), c("A", "C")), first)
V <- summary(m$fit)$cov.unscaled
coefs <- stats::coef(m$fit)
logit_rows <- Filter(function(r) grepl("C", r$term, fixed = TRUE), rows)
out <- fit_outputs(m$fit, rows, 0.95, multinomial_loglik(m$fit))
for (j in seq_along(logit_rows)) {
r <- logit_rows[[j]]
base <- sprintf("/logit/%d/", j - 1L)
out[[paste0(base, "logit")]] <- exact("log(C=c2 / C=c1)", "the logit against the first level")
out[[paste0(base, "estimate")]] <- tol(coefs[[r$name]], "logit parameter = loglinear parameter")
out[[paste0(base, "standard_error")]] <- tol(sqrt(V[r$name, r$name]), "logit parameter SE")
}
emit("STAT-REG-LOGLINEAR-LOGIT-002", "loglinearLogitMenuItem", "axis_coverage",
list(factors = list("A", "B", "C"), count = "n", dependent = list("C"),
terms = list("A*C"), sampling = "multinomial", coding = "first",
confidence_level = 0.95, conditional_test = "off"),
out,
list("glm(n ~ A*B + C + A:C, poisson) with first-level references; the fitted counts are those of the multinomial model and the logit is log(C=c2 / C=c1)."))
generators/r/regression_loglinear_selection.R
source("common.R", local = TRUE)
# Hierarchical backward elimination for a 2 x 3 x 2 count table, written out in base R with
# stats::glm(poisson) fits of every candidate model: from the saturated model (plus the terms
# the sampling design fixes), every removable maximal term is tested by G^2(reduced) -
# G^2(current) on the df difference; the largest p above alpha is removed (ties: higher
# order, then earlier in table order); the search stops when no candidate exceeds alpha.
# Every oracle re-reads the exported CSV.
cells <- expand.grid(C = c("c1", "c2"), B = c("b1", "b2", "b3"), A = c("a1", "a2"),
stringsAsFactors = FALSE)[c("A", "B", "C")]
cells$n <- c(31L, 19L, 12L, 9L, 8L, 6L, # a1: (b1,c1) (b1,c2) (b2,c1) (b2,c2) (b3,c1) (b3,c2)
8L, 17L, 15L, 28L, 21L, 39L) # a2
export_dataset(
cells, "REG_LOGLINEAR_02", "Constructed by regression_loglinear_selection.R",
"R stats::glm (poisson) help page; Agresti (2013) Categorical Data Analysis section 9.4 (model selection)",
"Constructed test data; no third-party dataset license.",
c(A = "String", B = "String", C = "String", n = "Int64")
)
d <- read_dataset("REG_LOGLINEAR_02")
for (f in c("A", "B", "C")) d[[f]] <- factor(d[[f]])
run <- generator_run(c("stats", "jsonlite"))
control <- stats::glm.control(epsilon = 1e-12, maxit = 200)
factors <- c("A", "B", "C")
tol <- function(x, label, tier = "1e-8")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste("Independent R glm(poisson) fits of the current and reduced models (IRLS to 1e-12):", label))
prob <- function(x, label, tier = "1e-7")
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = tier,
rationale = paste("Independent R chi-square tail of the likelihood-ratio change:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, kind, config, outputs, notes) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.loglinear_selection:compute_loglinear_selection",
menu_items = list("loglinearSelectionMenuItem"), title = gsub("-", " ", id),
case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_LOGLINEAR_02.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "Base R implementation of hierarchical backward elimination over stats::glm(family = poisson) fits of each generating class; stats::pchisq",
citation = "R Core Team, stats package help: glm, pchisq; Agresti (2013) Categorical Data Analysis, section 9.4 (hierarchical loglinear model selection by likelihood-ratio comparisons).",
generator = list(path = "generators/r/regression_loglinear_selection.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes,
"Backward elimination from the saturated model: each maximal term whose removal keeps the model hierarchical (and that is not inside a term fixed by the sampling design) is tested by G^2(reduced) - G^2(current) on the df difference; the candidate with the largest p above alpha is removed, ties going to the higher-order term and then the term earlier in table order; the search stops when no candidate exceeds alpha (docs/manual/chapters/86-regression-and-count-models.tex:193-197).",
"Candidates are listed by order (highest first) and then table order; df and deviances come from Poisson fits on the full positive table, so every fit is interior and the chi-square reference applies.",
"Expected computed from the exported CSV."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-LOGLINEAR-SELECTION-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
term_name <- function(term) paste(factors[factors %in% term], collapse = "*")
order_key <- function(terms) {
# HypoNum's order: higher order first, then the factor-index list lexicographically.
order(-lengths(terms), vapply(terms, function(t) paste(sprintf("%02d", sort(match(t, factors))), collapse = ""), ""))
}
maximal <- function(terms) {
terms <- unique(lapply(terms, function(t) sort(t)))
keep <- vapply(terms, function(t) !any(vapply(terms, function(o) length(o) > length(t) && all(t %in% o), TRUE)), TRUE)
terms <- terms[keep]
terms[order_key(terms)]
}
fit_class <- function(terms) {
# "*" expands each generating term to every term it implies (a hierarchical model).
rhs <- paste(vapply(terms, function(t) paste(factors[factors %in% t], collapse = "*"), ""), collapse = " + ")
fit <- stats::glm(stats::as.formula(paste("n ~", rhs)), family = stats::poisson(), data = d,
control = control)
stopifnot(all(stats::fitted(fit) > 0))
list(deviance = stats::deviance(fit), df = fit$df.residual, fit = fit)
}
backward <- function(fixed, alpha, highest_only = FALSE) {
current <- maximal(c(list(factors), fixed))
steps <- list()
repeat {
now <- fit_class(current)
eligible <- Filter(function(t) !any(vapply(fixed, function(f) all(t %in% f), TRUE)), current)
if (highest_only && length(eligible)) {
top <- max(lengths(eligible))
eligible <- Filter(function(t) length(t) == top, eligible)
}
candidates <- list()
for (term in eligible[order_key(eligible)]) {
subterms <- if (length(term) > 1) utils::combn(term, length(term) - 1, simplify = FALSE) else list()
reduced <- maximal(c(Filter(function(t) !identical(t, term), current), subterms))
red <- fit_class(reduced)
df <- red$df - now$df
chi <- max(0, red$deviance - now$deviance)
candidates[[length(candidates) + 1L]] <- list(
term = term, name = term_name(term), order = length(term), df = df, chi = chi,
p = if (df > 0) stats::pchisq(chi, df, lower.tail = FALSE) else NA, reduced = reduced)
}
best <- NULL
for (c in candidates) {
if (is.na(c$p) || c$p <= alpha) next
if (is.null(best) || c$p > best$p + 1e-12) best <- c
}
steps[[length(steps) + 1L]] <- list(model = vapply(current, term_name, ""),
candidates = candidates,
removed = if (is.null(best)) NULL else best$name)
if (is.null(best)) break
current <- best$reduced
}
list(steps = steps, final = fit_class(current), model = vapply(current, term_name, ""))
}
selection_outputs <- function(r) {
out <- list("/final/deviance" = tol(r$final$deviance, "G^2 of the final model"),
"/final/df" = exact(as.integer(r$final$df), "df of the final model"),
"/final/deviance_p" = prob(stats::pchisq(r$final$deviance, r$final$df, lower.tail = FALSE), "final G^2 tail"),
"/final/boundary" = exact(FALSE, "interior final fit"))
for (g in seq_along(r$model)) {
out[[sprintf("/final/generators/%d", g - 1L)]] <- exact(r$model[[g]], "final generating term")
}
for (s in seq_along(r$steps)) {
step <- r$steps[[s]]
base <- sprintf("/steps/%d/", s - 1L)
out[[paste0(base, "step")]] <- exact(s, "step number")
for (m in seq_along(step$model)) {
out[[sprintf("%smodel/%d", base, m - 1L)]] <- exact(step$model[[m]], "current generating term")
}
if (!is.null(step$removed)) out[[paste0(base, "removed")]] <- exact(step$removed, "the removed term")
for (c in seq_along(step$candidates)) {
cand <- step$candidates[[c]]
pre <- sprintf("%scandidates/%d/", base, c - 1L)
out[[paste0(pre, "term")]] <- exact(cand$name, "candidate term")
out[[paste0(pre, "order")]] <- exact(as.integer(cand$order), "candidate order")
out[[paste0(pre, "df")]] <- exact(as.integer(cand$df), "df gained by removal")
out[[paste0(pre, "lr_chi_square")]] <- tol(cand$chi, "G^2(reduced) - G^2(current)")
if (!is.na(cand$p)) out[[paste0(pre, "p_value")]] <- prob(cand$p, "chi-square tail")
}
}
out
}
common <- list(factors = list("A", "B", "C"), count = "n", sampling = "poisson",
coding = "last", confidence_level = 0.95, conditional_test = "off")
# 001: Poisson sampling, alpha 0.05: the three-way term and B*C leave, A*B and A*C stay.
r <- backward(list(), 0.05)
stopifnot(length(r$steps) == 3L, identical(r$model, c("A*B", "A*C")))
emit("STAT-REG-LOGLINEAR-SELECTION-001", "nominal", c(common, list(alpha = 0.05, highest_order_only = FALSE)),
selection_outputs(r),
list("Alpha 0.05 from the saturated A*B*C model; expected path: remove A*B*C, then B*C, then stop with A*B; A*C."))
# 002: a stricter alpha (0.30) removes less or stops earlier; the same algorithm.
r <- backward(list(), 0.30)
emit("STAT-REG-LOGLINEAR-SELECTION-002", "axis_coverage", c(common, list(alpha = 0.30, highest_order_only = FALSE)),
selection_outputs(r),
list("Alpha 0.30: a candidate is removed only when its likelihood-ratio p exceeds 0.30."))
# 003: the compatibility option tests only the highest-order terms present at each step.
r <- backward(list(), 0.05, highest_only = TRUE)
emit("STAT-REG-LOGLINEAR-SELECTION-003", "axis_coverage", c(common, list(alpha = 0.05, highest_order_only = TRUE)),
selection_outputs(r),
list("highest_order_only: at each step only terms of the highest order present are candidates."))
# 004: product-multinomial sampling with the totals of A and B fixed: A*B is never removed.
r <- backward(list(c("A", "B")), 0.05)
emit("STAT-REG-LOGLINEAR-SELECTION-004", "axis_coverage",
list(factors = list("A", "B", "C"), count = "n", sampling = "product_multinomial",
fixed_margins = list("A", "B"), coding = "last", confidence_level = 0.95,
conditional_test = "off", alpha = 0.05, highest_order_only = FALSE),
selection_outputs(r),
list("Product-multinomial with A, B totals fixed: A*B and its subterms are never candidates; the Poisson fitted counts and G^2 are unchanged."))
generators/r/regression_nearest_neighbour.R
source("common.R", local = TRUE)
# Nearest neighbours (Statistics > Multivariate > Nearest neighbours). The product splits the
# rows with a seeded permutation (numpy PCG64) that R cannot reproduce, so the reference data
# are two clusters separated by far more than their spread: for every admissible split, every
# k on the grid and both distances, every validation and held-out prediction is correct
# (classification) or exact (regression, constant targets within a cluster), so the errors,
# the chosen k and the importance changes are analytic, and the sample sizes follow the
# documented split fractions. FNN::knn is used to confirm the zero error on the whole data.
# Every oracle reads the exported CSV.
library(FNN)
run <- generator_run(c("FNN", "stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/90-learning-and-mixtures.tex:63-69: features are standardized with training means and SDs; Euclidean and city-block distances; a constant feature cannot contribute.",
"docs/manual/chapters/90-learning-and-mixtures.tex:71-79: regression predicts the mean of the k nearest training targets, classification their majority class; read held-out test error."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
emit <- function(id, title, kind, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("regression", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.nearest_neighbour:compute_nearest_neighbour",
menu_items = list("nearestNeighbourMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_KNN.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "analytic",
source = "separable-cluster argument checked with FNN::knn / FNN::knn.reg",
citation = "Cover and Hart (1967) Nearest neighbor pattern classification, IEEE Trans. Inf. Theory 13; R FNN package documentation (knn, knn.reg).",
generator = list(path = "generators/r/regression_nearest_neighbour.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-KNN-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("regression", id, case)
}
set.seed(47)
cluster <- rep(c("pass", "fail"), each = 15)
frame <- data.frame(
f1 = round(ifelse(cluster == "pass", rnorm(30, 0, 0.3), rnorm(30, 10, 0.3)), 3),
f2 = round(ifelse(cluster == "pass", rnorm(30, 0, 0.3), rnorm(30, 10, 0.3)), 3),
outcome = cluster,
hours = ifelse(cluster == "pass", 0, 6),
stringsAsFactors = FALSE
)
export_dataset(frame, "REG_KNN", "Thirty jobs in two clusters 10 units apart with spread 0.3 (seed 47); outcome and repair hours are constant within a cluster",
"analytic separable clusters; FNN knn",
"Constructed validation data; no third-party license.",
c(f1 = "Float64", f2 = "Float64", outcome = "String", hours = "Float64"))
d <- read_dataset("REG_KNN")
x <- as.matrix(d[c("f1", "f2")])
# Separation check: the largest within-cluster distance on either feature alone is far below
# the smallest between-cluster distance, so every neighbour set of size <= 3 is within-cluster
# for any training subset holding at least three rows of each cluster (guaranteed: a cluster
# has 15 rows and at most 12 rows are outside training).
within <- max(sapply(split(as.data.frame(x), d$outcome), function(g) max(dist(g))))
between <- min(as.matrix(dist(x))[d$outcome == "pass", d$outcome == "fail"])
within_1d <- max(sapply(c("f1", "f2"), function(f) max(sapply(split(d[[f]], d$outcome), function(v) diff(range(v))))))
between_1d <- min(sapply(c("f1", "f2"), function(f) abs(diff(range(tapply(d[[f]], d$outcome, mean)))) - within_1d))
stopifnot(between > 3 * within, between_1d > 3 * within_1d)
# Whole-data leave-one-out confirmation with FNN.
stopifnot(all(as.character(knn.cv(x, d$outcome, k = 3)) == d$outcome),
max(abs(knn.reg(x, y = d$hours, k = 3)$pred - d$hours)) < 1e-12)
n <- nrow(d)
test_rows <- floor(n * 0.2)
validation_rows <- floor((n - test_rows) * 0.25)
training_rows <- n - test_rows - validation_rows
stopifnot(test_rows == 6, validation_rows == 6, training_rows == 18)
common <- function(error_name) {
list(
"/training_rows" = exact(training_rows, "n - floor(0.2 n) - floor(0.25 (n - floor(0.2 n)))"),
"/validation_rows" = exact(validation_rows, "floor(0.25 of the non-test rows)"),
"/test_rows" = exact(test_rows, "floor(0.2 n)"),
"/missing_count" = exact(0L, "no missing value"),
"/excluded_count" = exact(0L, "no excluded row"),
"/chosen_k" = exact(1L, "every k has validation error 0; ties go to the smaller k"),
"/validation_error" = tol(0, "no validation row is misclassified / mispredicted", abs = "1e-15"),
"/test_error" = tol(0, "no held-out row is misclassified / mispredicted", abs = "1e-15"),
"/error_name" = exact(error_name, "the error measure of the task"),
"/grid/0/k" = exact(1L, "grid in declared order"),
"/grid/0/validation_error" = tol(0, "k = 1 validation error", abs = "1e-15"),
"/grid/1/k" = exact(3L, "grid in declared order"),
"/grid/1/validation_error" = tol(0, "k = 3 validation error", abs = "1e-15"),
"/importance/0/feature" = exact("f1", "features in declared order"),
"/importance/0/reduced_validation_error" = tol(0, "f2 alone separates the clusters", abs = "1e-15"),
"/importance/0/change" = tol(0, "reduced minus full validation error", abs = "1e-15"),
"/importance/1/feature" = exact("f2", "features in declared order"),
"/importance/1/change" = tol(0, "f1 alone separates the clusters", abs = "1e-15")
)
}
separation_note <- "Analytic oracle: the two clusters are at least three times farther apart than any within-cluster spread (also on each feature alone, asserted in the generator), so for any training subset with at least three rows per cluster the 1 and 3 nearest training rows of every validation or test row lie in its own cluster; FNN::knn.cv and knn.reg confirm a zero leave-one-out error on the full data."
split_note <- "The product's seeded identity split (test floor(0.2 n) = 6 rows, then validation floor(0.25 of 24) = 6 rows) is reproduced only in its counts; which rows are held out does not change the compared outputs."
outputs <- common("misclassification rate")
outputs[["/classes"]] <- list(comparison = "sequence", values = list("fail", "pass"),
rationale = "classes in code-point order")
emit("STAT-REG-KNN-CLASS-001", "Nearest-neighbour classification of separable clusters", "nominal",
list(target = "outcome", task = "classification", features = list("f1", "f2"),
distance = "euclidean", k_values = list(1L, 3L)),
outputs, list(separation_note, split_note))
outputs <- common("misclassification rate")
emit("STAT-REG-KNN-CITYBLOCK-001", "Nearest-neighbour classification with city-block distance", "axis_coverage",
list(target = "outcome", task = "classification", features = list("f1", "f2"),
distance = "cityblock", k_values = list(1L, 3L)),
outputs, list(separation_note, "City-block neighbourhoods coincide with the Euclidean ones here because the between-cluster gap dominates on each feature.", split_note))
outputs <- common("mean squared error")
emit("STAT-REG-KNN-REG-001", "Nearest-neighbour regression with constant within-cluster targets", "axis_coverage",
list(target = "hours", task = "regression", features = list("f1", "f2"),
distance = "euclidean", k_values = list(1L, 3L)),
outputs, list(separation_note, "Repair hours are 0 in one cluster and 6 in the other, so the mean of within-cluster neighbours reproduces the target exactly and the mean squared error is 0.", split_note))
cat("nearest neighbour cases generated\n")
generators/r/regression_options.R
source("common.R", local = TRUE)
d <- read_dataset("REG_CORE")
m <- read_dataset("REG_MISSING")
run <- generator_run(c("stats", "MASS", "nnet", "car", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R option reference:", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
sequence <- function(x, label, tier = "1e-9")
list(comparison = "sequence", values = as.list(dec(unname(x))), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R row-order reference:", label))
emit <- function(id, entry, menu, kind, config, outputs, notes,
dataset = "REG_CORE", excluded = list(), error = NULL) {
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = error, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = paste("Regression setting", id),
case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = if (is.null(error)) "independent_implementation" else "analytic",
source = "R regression fits",
citation = "R stats::lm/glm/predict.lm, MASS::polr/glm.nb, nnet::multinom and car::Anova help pages",
generator = list(path = "generators/r/regression_options.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "All fit inputs come from re-read exported CSVs.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
# Explicit, independent row-level fitted sequence after listwise missing and
# active population exclusion. The plot cap exceeds this entire small table.
remaining <- m[-c(3L, 8L), , drop = FALSE]
usable <- remaining[stats::complete.cases(remaining[c("y", "x", "z")]), , drop = FALSE]
fit <- stats::lm(y ~ x + z, data = usable)
emit("STAT-REG-ROWS-OLS-001", "tabelz.analysis.fit_y_by_x:compute_fits",
"fitYByXMenuItem", "row_alignment",
list(y_columns = list("y"), x_columns = list("x", "z")),
list("/responses/0/actual_predicted/x" = sequence(stats::predict(fit),
"all fitted means in retained row order"),
"/responses/0/actual_predicted/y" = sequence(usable$y,
"all observed responses in retained row order")),
list("The Actual by Predicted payload has x=fitted and y=observed; all retained rows are below the plot cap."),
dataset = "REG_MISSING", excluded = list(2L, 7L))
# Non-default Wald confidence, classification threshold and reference category.
binary <- stats::glm(I(binary == "event") ~ x, family = stats::binomial(), data = d)
binary_p <- as.vector(stats::predict(binary, type = "response"))
binary_actual <- d$binary == "event"
binary_estimate <- coef(binary)[["x"]]
binary_se <- summary(binary)$coefficients["x", "Std. Error"]
emit("STAT-REG-NUMERIC-BINARY-001",
"tabelz.analysis.logistic_regression:compute_binary_logistic_regression",
"logisticRegressionMenuItem", "axis_coverage",
list(outcome_column = "binary", event_value = "event", predictors = list("x"),
threshold = 0.3, confidence_level = 0.9),
list("/accuracy" = tol(mean((binary_p >= 0.3) == binary_actual), "0.3-threshold accuracy"),
"/coefficients/1/odds_ratio_ci_lower" = tol(exp(binary_estimate -
stats::qnorm(0.95) * binary_se), "90 percent Wald OR lower")),
list("Threshold 0.3 changes only classification; Wald interval uses z[0.95]."))
ord <- d
ord$response <- ordered(ord$ordinal, levels = c("low", "middle", "high"))
ord_fit <- MASS::polr(response ~ x, data = ord, method = "logistic", Hess = TRUE,
control = list(maxit = 1000, reltol = 1e-15))
ord_se <- summary(ord_fit)$coefficients["x", "Std. Error"]
emit("STAT-REG-NUMERIC-ORDINAL-001",
"tabelz.analysis.ordinal_logistic:compute_ordinal_logistic_regression",
"ordinalRegressionMenuItem", "axis_coverage",
list(outcome_column = "ordinal", predictors = list("x"),
level_order = list("low", "middle", "high"), confidence_level = 0.9),
list("/coefficients/0/odds_ratio_ci_lower" = tol(exp(coef(ord_fit)[["x"]] -
stats::qnorm(0.95) * ord_se), "90 percent ordered-logit Wald OR lower")),
list("MASS polr uses the manual's positive-at-higher-level slope sign."))
nom <- d
nom$response <- factor(nom$nominal, levels = c("C", "A", "B"))
nom_fit <- nnet::multinom(response ~ x, data = nom, trace = FALSE,
maxit = 1000, reltol = 1e-15)
nom_se <- summary(nom_fit)$standard.errors["A", "x"]
emit("STAT-REG-NUMERIC-NOMINAL-001",
"tabelz.analysis.nominal_logistic:compute_nominal_logistic_regression",
"nominalRegressionMenuItem", "axis_coverage",
list(outcome_column = "nominal", reference_value = "C", predictors = list("x"),
confidence_level = 0.9),
list("/coefficients/1/rrr_ci_lower" = tol(exp(coef(nom_fit)["A", "x"] -
stats::qnorm(0.95) * nom_se), "A-vs-C 90 percent Wald RRR lower")),
list("R first level C is explicit reference; numeric confidence setting is non-default."))
pois <- stats::glm(count ~ x + offset(log(exposure)), data = d,
family = stats::poisson())
pois_se <- summary(pois)$coefficients["x", "Std. Error"]
emit("STAT-REG-NUMERIC-POISSON-001",
"tabelz.analysis.poisson_regression:compute_poisson_regression",
"poissonRegressionMenuItem", "axis_coverage",
list(count_column = "count", predictors = list("x"), exposure_column = "exposure",
confidence_level = 0.9),
list("/coefficients/1/rate_ratio_ci_lower" = tol(exp(coef(pois)[["x"]] -
stats::qnorm(0.95) * pois_se), "90 percent Poisson Wald rate-ratio lower")),
list("Poisson uses a log-exposure offset and a Wald rather than profile-likelihood interval."))
no_offset <- stats::glm(count ~ x, data = d, family = stats::poisson())
emit("STAT-REG-AXIS-POISSON-NOOFFSET-001",
"tabelz.analysis.poisson_regression:compute_poisson_regression",
"poissonRegressionMenuItem", "nominal",
list(count_column = "count", predictors = list("x")),
list("/coefficients/1/coefficient" = tol(coef(no_offset)[["x"]], "no-offset log-count slope")),
list("Without exposure, the model is count intensity per row, not per unit exposure."))
nb <- MASS::glm.nb(nb_count ~ x + offset(log(exposure)), data = d,
control = stats::glm.control(maxit = 1000, epsilon = 1e-12))
# Statistician ruling, issue #11: the joint observed-information covariance is
# retained. glm.nb's summary reports the conditional expected-information
# (IRLS) covariance, so the SE comes from the analytic observed NB2 Hessian over
# (beta, theta) at glm.nb's MLE: with mu = exp(eta), per observation
# d2l/deta2 = -theta mu (theta + y) / (theta + mu)^2,
# d2l/deta dtheta = mu (y - mu) / (theta + mu)^2,
# d2l/dtheta2 = trigamma(y + theta) - trigamma(theta) + 1/theta
# - 2/(theta + mu) + (y + theta)/(theta + mu)^2.
# The beta block of the inverse does not depend on how theta is parameterized.
nb_joint_covariance <- function(fit) {
X <- stats::model.matrix(fit); y <- fit$y; mu <- fit$fitted.values; th <- fit$theta
w_bb <- -th * mu * (th + y) / (th + mu)^2
w_bt <- mu * (y - mu) / (th + mu)^2
h_tt <- sum(trigamma(y + th) - trigamma(th) + 1 / th - 2 / (th + mu) + (y + th) / (th + mu)^2)
H <- rbind(cbind(crossprod(X, X * w_bb), crossprod(X, w_bt)),
cbind(t(crossprod(X, w_bt)), h_tt))
solve(-H)[seq_len(ncol(X)), seq_len(ncol(X))]
}
nb_se <- sqrt(diag(nb_joint_covariance(nb)))[["x"]]
stopifnot(abs(nb_se - 0.05583151840027438) < 1e-12)
emit("STAT-REG-NUMERIC-NB-001", "tabelz.analysis.negative_binomial:compute_negative_binomial",
"nbRegressionMenuItem", "axis_coverage",
list(count = "nb_count", predictors = list("x"), exposure = "exposure",
confidence_level = 0.9, max_rows = 100L),
list("/coefficients/1/rate_ratio_ci_lower" = tol(exp(coef(nb)[["x"]] -
stats::qnorm(0.95) * nb_se), "90 percent NB2 Wald rate-ratio lower, joint observed information")),
list("MASS theta=1/alpha; confidence 0.9 and row cap 100 are explicit.",
"Issue #11 ruling: joint observed-information SE from the analytic NB2 Hessian; until 2026-09-25 this case used glm.nb's conditional expected-information SE."))
# A second case kind makes the population guarantee explicit for the three
# row-level engines; the row-alignment cases compare identities separately.
nb_pop <- remaining[stats::complete.cases(remaining[c("nb_count", "x", "exposure")]), , drop = FALSE]
nb_pop_fit <- MASS::glm.nb(nb_count ~ x + offset(log(exposure)), data = nb_pop,
control = stats::glm.control(maxit = 1000, epsilon = 1e-12))
emit("STAT-REG-MISSING-NB-001", "tabelz.analysis.negative_binomial:compute_negative_binomial",
"nbRegressionMenuItem", "missing_excluded",
list(count = "nb_count", predictors = list("x"), exposure = "exposure"),
list("/n" = exact(nrow(nb_pop), "retained NB2 rows"),
"/coefficients/1/coefficient" = tol(coef(nb_pop_fit)[["x"]], "retained NB2 slope")),
list("Two active exclusions are applied before complete-case NB2 fitting."),
dataset = "REG_MISSING", excluded = list(2L, 7L))
linear_pop <- remaining[stats::complete.cases(remaining[c("y", "x", "group")]), , drop = FALSE]
general_pop <- stats::lm(y ~ x + group, data = linear_pop,
contrasts = list(group = stats::contr.sum(3)))
emit("STAT-REG-MISSING-GENERAL-001", "tabelz.executor.general_linear",
"generalLinearMenuItem", "missing_excluded",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"), test_type = "III"),
list("/observations" = exact(nrow(linear_pop), "retained general-linear rows"),
"/residual_variance" = tol(summary(general_pop)$sigma^2, "retained GLM residual variance")),
list("Missing selected values are omitted listwise after active exclusions."),
dataset = "REG_MISSING", excluded = list(2L, 7L))
effects_pop <- stats::lm(y ~ x + group, data = linear_pop)
emit("STAT-REG-MISSING-EFFECTS-001", "tabelz.executor.linear_effects",
"linearEffectsMenuItem", "missing_excluded",
list(response_column = "y", factor_columns = list("group"), covariate_columns = list("x")),
list("/observations" = exact(nrow(linear_pop), "retained effects rows"),
"/residual_variance" = tol(summary(effects_pop)$sigma^2, "retained effects residual variance")),
list("Missing selected values are omitted listwise after active exclusions."),
dataset = "REG_MISSING", excluded = list(2L, 7L))
# Repair the independently constructed three-row boundary without touching the
# already indexed failed REG_MIN3 case, whose binary outcome has only one level.
binary_min <- d[1:3, , drop = FALSE]
binary_min$x <- c(-1, 0, 1)
binary_min$binary <- c("event", "other", "event")
export_dataset(binary_min, "REG_BIN_MIN", "Three deliberately non-separated binary rows",
"R stats::glm binomial manual; three rows exceed intercept-plus-slope count",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64"))
binary_min <- read_dataset("REG_BIN_MIN")
binary_min_fit <- stats::glm(I(binary == "event") ~ x, data = binary_min,
family = stats::binomial())
emit("STAT-REG-BOUNDARY-BINARY-CORRECTED-001",
"tabelz.analysis.logistic_regression:compute_binary_logistic_regression",
"logisticRegressionMenuItem", "boundary",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
list("/n" = exact(3, "three complete rows"),
"/coefficients/1/coefficient" = list(comparison = "tolerance", value = "0", absolute_tolerance = "1e-12", relative_tolerance = "0",
rationale = paste("Exactly zero by the design's symmetry; any implementation returns rounding",
"noise of order 1e-15, so an absolute 1e-12 replaces a relative tier that",
"cannot compare with zero. R gives", format(coef(binary_min_fit)[["x"]], digits = 17)))),
list("Events occur at x=-1 and +1 and non-event at x=0: both outcomes, finite MLE and residual df one.",
"This corrects coverage lost by the separately retained failed REG_MIN3 draft."),
dataset = "REG_BIN_MIN")
vif_entry <- "tabelz.analysis.fit_y_by_x:compute_fit_vif"
emit("STAT-REG-NUMERIC-VIF-HOLDOUT-001", vif_entry, "fitYByXMenuItem", "axis_coverage",
list(y_columns = list("y"), x_columns = list("x"), missing = "drop",
model_type = "linear", degree = 2L, interactions = FALSE, split_percent = 80L),
list("/0" = tol(1, "single-predictor VIF invariant", "1e-9")),
list("For one selected X, the auxiliary regression has no other predictors, so VIF=1 under any training-row split."))
positive <- read_dataset("REG_POSITIVE")
poly3_design <- data.frame(x = positive$x, z = positive$z,
x2 = positive$x^2, z2 = positive$z^2,
x3 = positive$x^3, z3 = positive$z^3)
poly3_vif <- 1 / (1 - summary(stats::lm(x ~ z + x2 + z2 + x3 + z3,
data = poly3_design))$r.squared)
emit("STAT-REG-NUMERIC-VIF-DEGREE3-001", vif_entry, "fitYByXMenuItem", "axis_coverage",
list(y_columns = list("y"), x_columns = list("x", "z"), missing = "drop",
model_type = "polynomial", degree = 3L, interactions = FALSE,
split_percent = 100L),
list("/0" = tol(poly3_vif, "cubic-design x VIF")),
list("R auxiliary OLS conditions x on z, x^2, z^2, x^3 and z^3."),
dataset = "REG_POSITIVE")
general_entry <- "tabelz.executor.general_linear"
general_config <- list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"))
general_fit <- stats::lm(y ~ group + x, data = d,
contrasts = list(group = stats::contr.sum(3)))
type2 <- car::Anova(general_fit, type = 2)
config_ii <- general_config
config_ii$test_type <- "II"
emit("STAT-REG-AXIS-GENERAL-TYPEII-001", general_entry, "generalLinearMenuItem",
"nominal", config_ii,
list("/term_tests/0/f_statistic" = tol(type2["group", "F value"],
"Type II whole-factor F", "1e-9")),
list("car::Anova type=2 tests group conditional on x while preserving marginality."))
weighted_fit <- stats::lm(y ~ group + x, data = d, weights = weight,
contrasts = list(group = stats::contr.sum(3)))
weighted_config <- general_config
weighted_config$weight_column <- "weight"
weighted_config$weight_kind <- "precision"
emit("STAT-REG-AXIS-GENERAL-WEIGHTED-001", general_entry, "generalLinearMenuItem",
"axis_coverage", weighted_config,
list("/residual_variance" = tol(summary(weighted_fit)$sigma^2,
"precision-weighted residual variance", "1e-9")),
list("Manual precision weights are inverse relative variances; R lm weights use the same WLS SSE/df."))
config_main <- list(response_column = "y", covariate_columns = list("x"),
terms = list(list(kind = "main", columns = list("x"))))
main_fit <- stats::lm(y ~ x, data = d)
emit("STAT-REG-AXIS-GENERAL-MAIN-001", general_entry, "generalLinearMenuItem",
"nominal", config_main,
list("/coefficients/x" = tol(coef(main_fit)[["x"]], "explicit main-term slope", "1e-9")),
list("An explicit main term does not duplicate the automatically included covariate main effect."))
config_interaction <- list(response_column = "y", covariate_columns = list("x", "z"),
terms = list(list(kind = "interaction", columns = list("x", "z"))))
interaction_fit <- stats::lm(y ~ x * z, data = d)
emit("STAT-REG-AXIS-GENERAL-INTERACTION-001", general_entry, "generalLinearMenuItem",
"nominal", config_interaction,
list("/residual_variance" = tol(summary(interaction_fit)$sigma^2,
"numeric interaction model MSE", "1e-9")),
list("The explicit x:z term adds one product column to the already included main effects."))
config_quadratic <- list(response_column = "y", covariate_columns = list("x"),
terms = list(list(kind = "quadratic", columns = list("x"))))
quadratic_fit <- stats::lm(y ~ x + I(x^2), data = d)
emit("STAT-REG-AXIS-GENERAL-QUADRATIC-001", general_entry, "generalLinearMenuItem",
"nominal", config_quadratic,
list("/coefficients/x^2" = tol(coef(quadratic_fit)[[3]], "quadratic x coefficient", "1e-9")),
list("A quadratic term is x squared and remains an adjustment term in Type II inference."))
nested_data <- d
nested_data$child <- rep(c("L", "R"), length.out = nrow(nested_data))
export_dataset(nested_data, "REG_NESTED", "REG_CORE with balanced within-parent child factor",
"R stats::lm factor nesting and Tabelz manual chapter 08",
"Constructed test data; no third-party dataset license.",
c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64", child = "String"))
nested_data <- read_dataset("REG_NESTED")
nested_fit <- stats::lm(y ~ x + group / child, data = nested_data,
contrasts = list(group = stats::contr.sum(3),
child = stats::contr.sum(2)))
config_nested <- list(response_column = "y", factor_columns = list("group", "child"),
covariate_columns = list("x"),
terms = list(list(kind = "nested", columns = list("group", "child"))))
emit("STAT-REG-AXIS-GENERAL-NESTED-001", general_entry, "generalLinearMenuItem",
"nominal", config_nested,
list("/residual_variance" = tol(summary(nested_fit)$sigma^2,
"nested-factor residual variance", "1e-9")),
list("R group/child spans group and child-within-group contrasts; the child has no crossed main effect."),
dataset = "REG_NESTED")
general_prediction <- stats::predict(general_fit,
newdata = data.frame(group = "A", x = mean(d$x)), se.fit = TRUE)
general_ci <- as.numeric(general_prediction$fit -
stats::qt(0.975, stats::df.residual(general_fit)) * general_prediction$se.fit)
config_general_conf <- general_config
config_general_conf$confidence_level <- 0.9
config_general_conf$max_rows <- 100L
# Manual chapter 08 fixes general-linear intervals at 95%; another level is refused.
emit("STAT-REG-NUMERIC-GENERAL-CONFIDENCE-001", general_entry, "generalLinearMenuItem",
"invalid_input", config_general_conf, structure(list(), names = character()),
list("Manual chapter 08: individual 95% t intervals; a 90% request must be refused.",
"Until 2026-09-25 this case wrongly expected a 90% interval; -095-001 checks the 95% interval."),
error = list(error_class = "ValidationError", message_contains = "fixed at 95%"))
config_general_conf$confidence_level <- 0.95
emit("STAT-REG-NUMERIC-GENERAL-CONFIDENCE-095-001", general_entry, "generalLinearMenuItem",
"axis_coverage", config_general_conf,
list("/adjusted_means/0/lower" = tol(general_ci, "95 percent adjusted mean A lower")),
list("Individual t interval from R predict.lm fit and se.fit at complete-case mean x; no simultaneous adjustment."))
effects_entry <- "tabelz.executor.linear_effects"
effects_fit <- stats::lm(y ~ group + x, data = d)
effects_prediction <- stats::predict(effects_fit,
newdata = data.frame(group = "A", x = mean(d$x)), se.fit = TRUE)
effects_ci <- as.numeric(effects_prediction$fit -
stats::qt(0.975, stats::df.residual(effects_fit)) * effects_prediction$se.fit)
# Manual chapter 07 fixes linear-effects intervals at 95%; another level is refused.
emit("STAT-REG-NUMERIC-EFFECTS-CONFIDENCE-001", effects_entry, "linearEffectsMenuItem",
"invalid_input",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"), confidence_level = 0.9, max_rows = 100L),
structure(list(), names = character()),
list("Manual chapter 07: individual 95% t intervals; a 90% request must be refused.",
"Until 2026-09-25 this case wrongly expected a 90% interval; -095-001 checks the 95% interval."),
error = list(error_class = "ValidationError", message_contains = "fixed at 95%"))
emit("STAT-REG-NUMERIC-EFFECTS-CONFIDENCE-095-001", effects_entry, "linearEffectsMenuItem",
"axis_coverage",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"), confidence_level = 0.95, max_rows = 100L),
list("/adjusted_means/0/lower" = tol(effects_ci, "95 percent effects mean A lower")),
list("Individual t interval from R predict.lm fit and se.fit at complete-case mean x."))
cat("regression option references generated\n")
generators/r/regression_penalized_logistic.R
source("common.R", local = TRUE)
# Elastic-net logistic regression (F107) checked analytically. glmnet is not in the lock
# file, so the contracted objective
# (1/n) sum log(1 + exp(eta_i)) - y_i eta_i + lambda [(1 - rho)/2 beta^2 + rho |beta|]
# is minimised in base R by Newton's method to machine precision. The seeded split and
# fold assignment cannot be reproduced in R, so the table is built to make every split
# irrelevant: within each outcome class every row has the same predictor value. Only the
# per-class counts then matter: 12 rows per class give floor(0.2 * 12) = 2 test rows and 10
# training rows per class, and sklearn's StratifiedKFold allocation puts exactly 2 rows of
# each class into every one of the 5 validation folds, so each fold fits on 8 + 8 rows with
# the same standardized design and the aggregate cross-validated log loss, the selection,
# the final fit on 10 + 10 rows and the 2 + 2 test evaluation are all closed-form
# consequences of the contracted procedure. Every oracle re-reads the exported CSV.
constructed <- data.frame(
outcome = rep(c("fail", "pass"), each = 12L),
load = rep(c(13, 10), each = 12L),
stringsAsFactors = FALSE
)
export_dataset(
constructed, "REG_ENET_01", "Constructed by regression_penalized_logistic.R",
"Hastie, Tibshirani & Friedman (2009) The Elements of Statistical Learning, section 18.4 (elastic net); base R optimisation of the stated objective",
"Constructed test data; no third-party dataset license.",
c(outcome = "String", load = "Float64")
)
d <- read_dataset("REG_ENET_01")
run <- generator_run(c("stats", "jsonlite"))
lambda_grid <- c(1e-4, 1e-3, 1e-2, 0.1, 1, 10, 100)
rho_grid <- c(0, 0.25, 0.5, 0.75, 1)
softplus <- function(v) ifelse(v > 30, v, log1p(exp(v)))
loss_sum <- function(y, eta) sum(softplus((1 - 2 * y) * eta))
sigmoid <- function(v) 1 / (1 + exp(-v))
# The standardized design of a fitting set: z = ((x - median) - mean(x - median)) / sd0.
standardize <- function(x) {
centre <- stats::median(x)
dev <- x - centre
offset <- mean(dev)
sd0 <- sqrt(mean((dev - offset)^2))
list(z = (dev - offset) / sd0, mean = centre + offset, sd = sd0)
}
# Minimise the objective in (b0, beta). The class with the larger load is the event, so the
# unpenalized gradient in beta at beta = 0 is negative and the optimum has beta >= 0: either
# beta = 0 when lambda * rho dominates that gradient (soft threshold), or the smooth problem
# with penalty lambda [(1 - rho)/2 beta^2 + rho beta] solved by Newton's method.
fit_objective <- function(z, y, lam, rho) {
n <- length(y)
grad_loss <- function(b0, beta) {
eta <- b0 + beta * z
r <- sigmoid(eta) - y
c(sum(r), sum(r * z)) / n
}
hess_loss <- function(b0, beta) {
eta <- b0 + beta * z
w <- sigmoid(eta) * (1 - sigmoid(eta))
matrix(c(sum(w), sum(w * z), sum(w * z), sum(w * z * z)), 2) / n
}
# Candidate 1: beta = 0 with b0 the prevalence logit.
p <- mean(y)
b0_zero <- log(p / (1 - p))
g <- grad_loss(b0_zero, 0)
if (abs(g[[2]]) <= lam * rho) {
return(list(b0 = b0_zero, beta = 0, objective = loss_sum(y, rep(b0_zero, n)) / n))
}
theta <- c(b0_zero, 0.5)
for (iteration in 1:500) {
g <- grad_loss(theta[[1]], theta[[2]]) + c(0, lam * (1 - rho) * theta[[2]] + lam * rho)
H <- hess_loss(theta[[1]], theta[[2]]) + diag(c(0, lam * (1 - rho)))
step <- solve(H, g)
# Keep beta positive on the smooth branch (damped Newton).
scale <- 1
while (theta[[2]] - scale * step[[2]] <= 0) scale <- scale / 2
theta <- theta - scale * step
if (max(abs(g)) < 1e-14) break
}
stopifnot(max(abs(g)) < 1e-12)
eta <- theta[[1]] + theta[[2]] * z
list(b0 = theta[[1]], beta = theta[[2]],
objective = loss_sum(y, eta) / n + lam * ((1 - rho) / 2 * theta[[2]]^2 + rho * theta[[2]]))
}
classes <- sort(unique(d$outcome)) # "fail" < "pass"
load_of <- c(fail = unique(d$load[d$outcome == "fail"]), pass = unique(d$load[d$outcome == "pass"]))
stopifnot(length(load_of) == 2L, sum(d$outcome == "fail") == 12L, sum(d$outcome == "pass") == 12L)
test_per_class <- floor(0.2 * 12) # 2 of each class
train_per_class <- 12L - test_per_class # 10 of each class
fold_valid_per_class <- 2L # StratifiedKFold(5) over 10 + 10 rows
fold_fit_per_class <- train_per_class - fold_valid_per_class
# One training fold: 8 fail + 8 pass rows; the validation fold has 2 + 2.
fold_x <- rep(c(load_of[["fail"]], load_of[["pass"]]), each = fold_fit_per_class)
fold_y <- rep(c(1, 0), each = fold_fit_per_class)
fold_std <- standardize(fold_x)
valid_z <- (c(load_of[["fail"]], load_of[["pass"]]) - fold_std$mean) / fold_std$sd
valid_y <- c(1, 0)
n_train <- 2L * train_per_class
candidates <- list()
for (lam in lambda_grid) for (rho in rho_grid) {
fit <- fit_objective(fold_std$z, fold_y, lam, rho)
fold_loss <- fold_valid_per_class * loss_sum(valid_y, fit$b0 + fit$beta * valid_z)
candidates[[length(candidates) + 1L]] <- list(lam = lam, rho = rho, cv = 5 * fold_loss / n_train)
}
cv <- vapply(candidates, function(c) c$cv, 0)
best <- min(cv)
tied <- which(cv - best <= 1e-12 * abs(best))
selected <- candidates[[tied[which.max(vapply(candidates[tied], function(c) c$lam * 1e3 + c$rho, 0))]]]
baseline_cv <- log(2) # training-fold prevalence 1/2 on every fold
# The final fit on all 20 training rows (same two load values, 10 + 10).
train_x <- rep(c(load_of[["fail"]], load_of[["pass"]]), each = train_per_class)
train_y <- rep(c(1, 0), each = train_per_class)
train_std <- standardize(train_x)
final <- fit_objective(train_std$z, train_y, selected$lam, selected$rho)
raw_beta <- final$beta / train_std$sd
raw_intercept <- final$b0 - final$beta * train_std$mean / train_std$sd
# The reserved test rows: 2 fail + 2 pass.
test_z <- (c(load_of[["fail"]], load_of[["pass"]]) - train_std$mean) / train_std$sd
test_eta <- rep(final$b0 + final$beta * test_z, each = test_per_class)
test_y <- rep(c(1, 0), each = test_per_class)
test_p <- sigmoid(test_eta)
model_scores <- list(log_loss = loss_sum(test_y, test_eta) / length(test_y),
brier = mean((test_p - test_y)^2),
auc = if (final$beta > 0) 1 else 0.5)
baseline_scores <- list(log_loss = log(2), brier = 0.25)
confusion <- list(tp = sum(test_p >= 0.5 & test_y == 1), fp = sum(test_p >= 0.5 & test_y == 0),
tn = sum(test_p < 0.5 & test_y == 0), fn = sum(test_p < 0.5 & test_y == 1))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste("Base-R Newton minimisation of the contracted objective (gradient 1e-14) versus HypoNum's proximal Newton (KKT 1e-10):", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
outputs <- list(
"/analyzed_count" = exact(24L, "rows with an observed outcome"),
"/train_events" = exact(as.integer(train_per_class), "fail rows kept for training"),
"/train_non_events" = exact(as.integer(train_per_class), "pass rows kept for training"),
"/test_events" = exact(as.integer(test_per_class), "fail rows reserved for testing"),
"/test_non_events" = exact(as.integer(test_per_class), "pass rows reserved for testing"),
"/train_prevalence" = tol(0.5, "training prevalence", "1e-12"),
"/baseline_cv_log_loss" = tol(baseline_cv, "prevalence-baseline cross-validated log loss log 2", "1e-12"),
"/selected_lambda" = tol(selected$lam, "selected lambda", "1e-12"),
"/selected_rho" = tol(selected$rho, "selected rho", "1e-12"),
"/final_objective" = tol(final$objective, "objective at the final fit"),
"/final_converged" = exact(TRUE, "the final fit converged"),
"/coefficients/0/term" = exact("Intercept", "intercept row"),
"/coefficients/0/fitted_scale" = list(comparison = "tolerance", value = "0", absolute_tolerance = "1e-8",
relative_tolerance = "0",
rationale = "Analytically zero: balanced classes with symmetric standardized loads (+1/-1) make the objective even in b0, so the fitted intercept is 0 (Newton gives |b0| < 1e-12)."),
"/coefficients/0/raw_scale" = tol(raw_intercept, "raw-scale intercept"),
"/coefficients/1/term" = exact("load", "the numeric predictor"),
"/coefficients/1/fitted_scale" = tol(final$beta, "fitted-scale coefficient"),
"/coefficients/1/raw_scale" = tol(raw_beta, "raw-scale coefficient = beta / sd"),
"/coefficients/1/status" = exact(if (final$beta == 0) "zero_by_penalty" else "estimated", "coefficient status"),
"/artifact/numeric/0/mean" = tol(train_std$mean, "training mean of load", "1e-12"),
"/artifact/numeric/0/sd" = tol(train_std$sd, "training population SD of load", "1e-12"),
"/evaluation/test_count" = exact(4L, "test rows"),
"/evaluation/model/log_loss" = tol(model_scores$log_loss, "test log loss"),
"/evaluation/model/brier" = tol(model_scores$brier, "test Brier score"),
"/evaluation/model/auc" = tol(model_scores$auc, "test ROC AUC", "1e-12"),
"/evaluation/baseline/log_loss" = tol(baseline_scores$log_loss, "baseline test log loss", "1e-12"),
"/evaluation/baseline/brier" = tol(baseline_scores$brier, "baseline test Brier score", "1e-12"),
"/evaluation/confusion/true_positive" = exact(as.integer(confusion$tp), "confusion at 0.5"),
"/evaluation/confusion/false_positive" = exact(as.integer(confusion$fp), "confusion at 0.5"),
"/evaluation/confusion/true_negative" = exact(as.integer(confusion$tn), "confusion at 0.5"),
"/evaluation/confusion/false_negative" = exact(as.integer(confusion$fn), "confusion at 0.5")
)
for (k in seq_along(candidates)) {
c <- candidates[[k]]
outputs[[sprintf("/candidates/%d/lam", k - 1L)]] <- tol(c$lam, "grid lambda", "1e-12")
outputs[[sprintf("/candidates/%d/rho", k - 1L)]] <- tol(c$rho, "grid rho", "1e-12")
outputs[[sprintf("/candidates/%d/eligible", k - 1L)]] <- exact(TRUE, "every fold converged")
outputs[[sprintf("/candidates/%d/cv_log_loss", k - 1L)]] <- tol(c$cv, sprintf("aggregate CV log loss at lambda %g, rho %g", c$lam, c$rho))
}
id <- "STAT-REG-PENALIZED-LOGISTIC-001"
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.penalized_logistic:compute_penalized_logistic",
menu_items = list("penalizedLogisticMenuItem"), title = gsub("-", " ", id),
case_kind = "nominal",
dataset = list(path = "reference_data/datasets/REG_ENET_01.json", sha256 = ""),
excluded_rows = list(),
configuration = list(response = "outcome", event = "fail", numeric_predictors = list("load"),
seed = 20260923L, threshold = 0.5, independence_declared = TRUE),
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "analytic",
source = "Base R Newton minimisation of the contracted elastic-net objective on a table whose split and folds are determined by class counts alone",
citation = "Zou & Hastie (2005) Regularization and variable selection via the elastic net, JRSS-B 67, 301-320; Hastie, Tibshirani & Friedman (2009) ESL section 18.4.",
generator = list(path = "generators/r/regression_penalized_logistic.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
"Objective (1/n) sum {log(1 + e^eta_i) - y_i eta_i} + lambda {(1 - rho)/2 ||beta||^2 + rho ||beta||_1}, unpenalized intercept, grid lambda in {1e-4, ..., 100} x rho in {0, 0.25, 0.5, 0.75, 1}; a stratified 20% test sample first, then five stratified folds select the lowest pooled log loss with near-ties (1e-12 relative) preferring the larger lambda then larger rho (docs/manual/chapters/76-forest-penalty-quantiles.tex:72-105).",
"Numeric predictors are median-imputed (no missing values here) and standardized as z = ((x - median) - mean(x - median)) / population SD on the fitting rows; raw coefficient = beta / sd, raw intercept = b0 - beta * mean / sd.",
"Within each outcome class every row has the same load, so the seeded choice of which rows are reserved or fold-assigned cannot change any fit: per class floor(0.2 * 12) = 2 test rows and 10 training rows; StratifiedKFold's allocation puts 2 rows of each class into every validation fold (per-class fold counts follow from the class counts alone), so every fold fits the same 8 + 8 design. The oracle therefore depends on those count rules (split and fold labels recorded by HypoNum in split_procedure), not on the random generator.",
"Beta >= 0 at every optimum because the event class has the larger load; the soft-threshold condition |dL/dbeta(b0*, 0)| <= lambda rho gives beta = 0, otherwise damped Newton on the smooth branch to gradient 1e-14.",
"Test ROC AUC is 1 when beta > 0 (every fail probability exceeds every pass probability), the baseline test log loss is log 2 and its Brier score 0.25 at training prevalence 1/2.",
"Expected computed from the exported CSV."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-PENALIZED-LOGISTIC-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
cat("selected", selected$lam, selected$rho, "beta", final$beta, "b0", final$b0, "\n")
generators/r/regression_population.R
source("common.R", local = TRUE)
source_data <- read_dataset("REG_CORE")
source_data$x[[5]] <- NA_real_
source_data$y[[11]] <- NA_real_
source_data$binary[[17]] <- NA_character_
source_data$ordinal[[19]] <- NA_character_
source_data$nominal[[21]] <- NA_character_
source_data$count[[23]] <- NA_integer_
source_data$nb_count[[25]] <- NA_integer_
export_dataset(source_data, "REG_MISSING", "REG_CORE with seven declared missing cells",
"R stats::lm/glm; MASS::polr/glm.nb; nnet::multinom documentation",
"Constructed from REG_CORE; no third-party dataset license.",
c(x = "Float64", z = "Float64", group = "String", binary = "String",
ordinal = "String", nominal = "String", exposure = "Float64",
cluster = "String", weight = "Float64", y = "Float64",
count = "Int64", nb_count = "Int64"))
d <- read_dataset("REG_MISSING")
excluded <- c(2L, 7L) # Zero-based case indexes; remove rows 3 and 8 in R.
kept <- d[-(excluded + 1L), , drop = FALSE]
run <- generator_run(c("stats", "MASS", "nnet", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("R complete-case fit:", label))
exact <- function(x, label) list(comparison = "exact", value = as.integer(x), rationale = label)
seq_ids <- function(id, indexes) list(comparison = "sequence",
values = as.list(paste0(id, ":", indexes - 1L)),
rationale = "Source-row ordinals survive exclusion and complete-case filtering.")
emit <- function(id, entry, menu, kind, configuration, outputs, notes) {
write_expected("regression", id, list(schema_version = "2.0.0", case_id = id,
outputs = outputs, expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = entry, menu_items = list(menu), title = paste("Missing and excluded", id),
case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_MISSING.json", sha256 = ""),
excluded_rows = as.list(excluded), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = "R complete-case models",
citation = "R stats::lm/glm, MASS::polr/glm.nb, nnet::multinom help pages",
generator = list(path = "generators/r/regression_population.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R removes zero-based excluded rows 2 and 7 first, then selected-field missing rows.",
"All estimates are computed from the re-read REG_MISSING CSV.")),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL))
}
fit_rows <- which(stats::complete.cases(kept[c("y", "x", "z")]))
fit_data <- kept[fit_rows, , drop = FALSE]
fit <- stats::lm(y ~ x + z, data = fit_data)
emit("STAT-REG-POP-OLS-001", "tabelz.analysis.fit_y_by_x:compute_fits",
"fitYByXMenuItem", "missing_excluded",
list(y_columns = list("y"), x_columns = list("x", "z")),
list("/responses/0/observations" = exact(nrow(fit_data), "complete, non-excluded rows"),
"/responses/0/parameters/1/estimate" = tol(coef(fit)[["x"]], "x slope")),
list("Listwise OLS after population exclusion."))
vif <- 1 / (1 - summary(stats::lm(x ~ z, data = fit_data))$r.squared)
emit("STAT-REG-POP-VIF-001", "tabelz.analysis.fit_y_by_x:compute_fit_vif",
"fitYByXMenuItem", "missing_excluded",
list(y_columns = list("y"), x_columns = list("x", "z"), missing = "drop",
model_type = "linear", degree = 2L, interactions = FALSE, split_percent = 100L),
list("/0" = tol(vif, "x variance inflation", "1e-9")),
list("VIF uses the same complete design rows as the OLS fit."))
binary_data <- kept[stats::complete.cases(kept[c("binary", "x")]), , drop = FALSE]
binary_fit <- stats::glm(I(binary == "event") ~ x, data = binary_data,
family = stats::binomial())
emit("STAT-REG-POP-BINARY-001",
"tabelz.analysis.logistic_regression:compute_binary_logistic_regression",
"logisticRegressionMenuItem", "missing_excluded",
list(outcome_column = "binary", event_value = "event", predictors = list("x")),
list("/n" = exact(nrow(binary_data), "complete non-excluded binary rows"),
"/coefficients/1/coefficient" = tol(coef(binary_fit)[["x"]], "binary slope")),
list("Event is explicit; no imputation of missing outcome or predictor."))
ord_data <- kept[stats::complete.cases(kept[c("ordinal", "x")]), , drop = FALSE]
ord_data$response <- ordered(ord_data$ordinal, levels = c("low", "middle", "high"))
ord_fit <- MASS::polr(response ~ x, data = ord_data, method = "logistic", Hess = TRUE,
control = list(maxit = 1000, reltol = 1e-15))
emit("STAT-REG-POP-ORDINAL-001",
"tabelz.analysis.ordinal_logistic:compute_ordinal_logistic_regression",
"ordinalRegressionMenuItem", "missing_excluded",
list(outcome_column = "ordinal", predictors = list("x"),
level_order = list("low", "middle", "high")),
list("/n" = exact(nrow(ord_data), "complete non-excluded ordinal rows"),
"/coefficients/0/coefficient" = tol(coef(ord_fit)[["x"]], "ordered slope")),
list("Low-to-high outcome order is explicit in R and Tabelz."))
nom_data <- kept[stats::complete.cases(kept[c("nominal", "x")]), , drop = FALSE]
nom_data$response <- factor(nom_data$nominal, levels = c("A", "B", "C"))
nom_fit <- nnet::multinom(response ~ x, data = nom_data, trace = FALSE,
maxit = 1000, reltol = 1e-15)
emit("STAT-REG-POP-NOMINAL-001",
"tabelz.analysis.nominal_logistic:compute_nominal_logistic_regression",
"nominalRegressionMenuItem", "missing_excluded",
list(outcome_column = "nominal", reference_value = "A", predictors = list("x")),
list("/n" = exact(nrow(nom_data), "complete non-excluded nominal rows"),
"/coefficients/1/coefficient" = tol(coef(nom_fit)["B", "x"], "B versus A x slope")),
list("R multinom reference A and tight convergence."))
pois_data <- kept[stats::complete.cases(kept[c("count", "x", "exposure")]), , drop = FALSE]
pois_fit <- stats::glm(count ~ x + offset(log(exposure)), data = pois_data,
family = stats::poisson())
emit("STAT-REG-POP-POISSON-001",
"tabelz.analysis.poisson_regression:compute_poisson_regression",
"poissonRegressionMenuItem", "missing_excluded",
list(count_column = "count", predictors = list("x"), exposure_column = "exposure"),
list("/n" = exact(nrow(pois_data), "complete non-excluded Poisson rows"),
"/coefficients/1/coefficient" = tol(coef(pois_fit)[["x"]], "Poisson rate slope")),
list("Positive exposure enters as log offset."))
nb_data <- kept[stats::complete.cases(kept[c("nb_count", "x", "exposure")]), , drop = FALSE]
nb_fit <- MASS::glm.nb(nb_count ~ x + offset(log(exposure)), data = nb_data,
control = stats::glm.control(maxit = 1000, epsilon = 1e-12))
nb_indexes <- as.integer(rownames(nb_data))
emit("STAT-REG-POP-NB-001", "tabelz.analysis.negative_binomial:compute_negative_binomial",
"nbRegressionMenuItem", "row_alignment",
list(count = "nb_count", predictors = list("x"), exposure = "exposure"),
list("/n" = exact(nrow(nb_data), "complete non-excluded NB2 rows"),
"/analyzed_row_ids" = seq_ids("STAT-REG-POP-NB-001", nb_indexes),
"/coefficients/1/coefficient" = tol(coef(nb_fit)[["x"]], "NB2 slope")),
list("An explicit row-ID sequence checks population and prediction alignment."))
linear_data <- kept[stats::complete.cases(kept[c("y", "x", "group")]), , drop = FALSE]
linear_fit <- stats::lm(y ~ x + group, data = linear_data,
contrasts = list(group = stats::contr.sum(3)))
linear_indexes <- as.integer(rownames(linear_data))
emit("STAT-REG-POP-GENERAL-001", "tabelz.executor.general_linear",
"generalLinearMenuItem", "row_alignment",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x"), test_type = "III"),
list("/observations" = exact(nrow(linear_data), "complete non-excluded GLM rows"),
"/retained_row_ids" = seq_ids("STAT-REG-POP-GENERAL-001", linear_indexes),
"/residual_variance" = tol(summary(linear_fit)$sigma^2, "GLM residual variance")),
list("General linear precision weights absent; sum contrasts per manual."))
effects_fit <- stats::lm(y ~ x + group, data = linear_data)
emit("STAT-REG-POP-EFFECTS-001", "tabelz.executor.linear_effects",
"linearEffectsMenuItem", "row_alignment",
list(response_column = "y", factor_columns = list("group"),
covariate_columns = list("x")),
list("/observations" = exact(nrow(linear_data), "complete non-excluded effects rows"),
"/retained_row_ids" = seq_ids("STAT-REG-POP-EFFECTS-001", linear_indexes),
"/residual_variance" = tol(summary(effects_fit)$sigma^2, "effects residual variance")),
list("Linear effects treatment contrasts; first alphabetical level reference."))
cat("regression population references generated\n")
generators/r/regression_quantile.R
source("common.R", local = TRUE)
library(quantreg)
# Linear conditional-quantile regression: quantreg::rq (Barrodale-Roberts simplex) is the
# oracle for the point fits and their check loss; the intercept-only fit is the declared
# lower empirical quantile (order statistic ceil(n tau)). Bootstrap intervals, the held-out
# split and the sensitivity ranges depend on NumPy's seeded generator or on HypoNum's own
# tolerance rules and are not compared. Every oracle re-reads the exported CSV.
set.seed(20261011)
n <- 30L
x1 <- round(runif(n, 0, 10), 3)
x2 <- round(rnorm(n, 2, 1.5), 3)
g <- rep(c("A", "B", "C"), c(11L, 9L, 10L))
y <- round(1 + 0.6 * x1 - 0.9 * x2 + c(A = 0, B = 1.5, C = -1)[g] +
(0.4 + 0.15 * x1) * rt(n, df = 4), 3)
constructed <- data.frame(y = y, x1 = x1, x2 = x2, g = g, stringsAsFactors = FALSE)
export_dataset(
constructed, "REG_QUANT_01", "Constructed by regression_quantile.R",
"Koenker, R. quantreg package help: rq (method = 'br').",
"Constructed test data; no third-party dataset license.",
c(y = "Float64", x1 = "Float64", x2 = "Float64", g = "String")
)
d <- read_dataset("REG_QUANT_01")
run <- generator_run(c("stats", "quantreg", "jsonlite"))
tol <- function(x, label, tier = "1e-6")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste("quantreg::rq simplex solution versus HypoNum's HiGHS program (feasibility 1e-10): the two linear programs agree at a unique vertex to far better than 1e-6;", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, kind, config, outputs, notes) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.quantile_regression:compute_quantile_regression",
menu_items = list("quantileRegressionMenuItem"), title = gsub("-", " ", id),
case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_QUANT_01.json", sha256 = ""),
excluded_rows = list(), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R quantreg::rq(method = 'br') and the check loss sum rho_tau(y - X beta); base R sort() for the intercept-only order statistic",
citation = "Koenker, R. (2005) Quantile Regression, CUP; quantreg package help: rq, rq.fit.br.",
generator = list(path = "generators/r/regression_quantile.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes,
"Check loss rho_tau(u) = u (tau - 1(u < 0)) with an intercept, no weights, complete cases (docs/manual/chapters/76-forest-penalty-quantiles.tex:121-146); categorical predictors are reference-cell indicators with the first sorted level as reference, as in rq's default treatment contrasts.",
"Intercept-only fits report the declared lower empirical quantile y_(ceil(n tau)) and, when n tau is an integer, the minimizing interval [y_(n tau), y_(n tau + 1)] as the sensitivity range (manual lines 133-146).",
"Uniqueness of each compared vertex was checked in R by agreement of the simplex (br) and interior-point (fn) solutions to 1e-5.",
"Bootstrap percentile intervals (999 NumPy-seeded resamples), the held-out split and the near-optimal sensitivity probes are not reproduced in R; only the interval status rule min(n tau, n(1 - tau)) >= 10 is checked.",
"Expected computed from the exported CSV."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-QUANTILE-REGRESSION-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
check_loss <- function(y, fitted, tau) {
u <- y - fitted
sum(u * (tau - (u < 0)))
}
fit_outputs <- function(index, tau, formula, terms, resamples_supported) {
warned <- FALSE
fit <- withCallingHandlers(
quantreg::rq(formula, tau = tau, data = d, method = "br"),
warning = function(w) { warned <<- TRUE; invokeRestart("muffleWarning") })
stopifnot(!warned) # rq.fit.br warns when the solution may be nonunique
alt <- quantreg::rq(formula, tau = tau, data = d, method = "fn")
stopifnot(max(abs(stats::coef(fit) - stats::coef(alt))) < 1e-5)
coef <- stats::coef(fit)
stopifnot(length(coef) == length(terms))
base <- sprintf("/fits/%d/", index)
out <- list()
out[[paste0(base, "quantile")]] <- tol(tau, "the requested quantile", "1e-12")
out[[paste0(base, "check_loss")]] <- tol(check_loss(d$y, stats::fitted(fit), tau),
"check loss at the optimum", "1e-8")
out[[paste0(base, "tail_information")]] <- tol(min(n * tau, n * (1 - tau)), "min(n tau, n (1 - tau))", "1e-12")
out[[paste0(base, "interval_status")]] <- exact(
if (resamples_supported) "available" else "tail_withheld",
"the tail-information rule min(n tau, n(1 - tau)) >= 10 (manual lines 148-152)")
out[[paste0(base, "resamples")]] <- exact(if (resamples_supported) 999L else 0L,
"999 declared resamples when intervals are supported")
for (j in seq_along(terms)) {
out[[sprintf("%scoefficients/%d/term", base, j - 1L)]] <- exact(terms[[j]], "term label")
out[[sprintf("%scoefficients/%d/estimate", base, j - 1L)]] <- tol(coef[[j]], paste("coefficient", terms[[j]]))
}
out
}
common <- list("/fit_rows" = exact(n, "complete rows fitted"),
"/source_count" = exact(n, "source rows"),
"/excluded_count" = exact(0L, "no excluded rows"),
"/incomplete_count" = exact(0L, "no incomplete rows"),
"/crossing_rows" = exact(0L, "a single quantile cannot cross"))
# 001: the median with two numeric predictors (bootstrap intervals supported, not compared).
emit("STAT-REG-QUANTILE-REGRESSION-001", "nominal",
list(response = "y", numeric_predictors = list("x1", "x2"), quantiles = list(0.5),
confidence_level = 0.95, dependence = "independent", held_out_evaluation = FALSE,
seed = 0L),
c(common, fit_outputs(0L, 0.5, y ~ x1 + x2, c("(Intercept)", "x1", "x2"), TRUE)),
list("Median regression on x1 and x2; rq(tau = 0.5)."))
# 002: two quantiles entered out of order are sorted; tau = 0.25 has 7.5 < 10 tail rows.
out <- c(fit_outputs(0L, 0.25, y ~ x1 + x2, c("(Intercept)", "x1", "x2"), FALSE),
fit_outputs(1L, 0.75, y ~ x1 + x2, c("(Intercept)", "x1", "x2"), FALSE))
out[["/quantiles/0"]] <- tol(0.25, "sorted quantiles", "1e-12")
out[["/quantiles/1"]] <- tol(0.75, "sorted quantiles", "1e-12")
emit("STAT-REG-QUANTILE-REGRESSION-002", "axis_coverage",
list(response = "y", numeric_predictors = list("x1", "x2"), quantiles = list(0.75, 0.25),
confidence_level = 0.90, dependence = "independent", held_out_evaluation = FALSE,
seed = 3L),
c(common[c("/fit_rows", "/source_count", "/excluded_count", "/incomplete_count")], out),
list("Quantiles 0.75 and 0.25 are fitted separately (rq per tau) and reported in increasing order; both tails have min(n tau, n(1 - tau)) = 7.5 < 10 so intervals are withheld."))
# 003: a categorical predictor beside a numeric one (reference A, indicators for B and C).
emit("STAT-REG-QUANTILE-REGRESSION-003", "axis_coverage",
list(response = "y", numeric_predictors = list("x1"), categorical_predictors = list("g"),
quantiles = list(0.5), confidence_level = 0.95, dependence = "independent",
held_out_evaluation = FALSE, seed = 0L),
c(common, fit_outputs(0L, 0.5, y ~ x1 + g, c("(Intercept)", "x1", "g=B (vs A)", "g=C (vs A)"), TRUE)),
list("rq(y ~ x1 + g) with R's default treatment contrasts (first sorted level A as reference)."))
# 004: intercept-only median: the lower empirical quantile y_(15) with range [y_(15), y_(16)].
sorted <- sort(d$y)
estimate <- sorted[[15]]
stopifnot(sorted[[16]] > estimate)
emit("STAT-REG-QUANTILE-REGRESSION-004", "axis_coverage",
list(response = "y", quantiles = list(0.5), confidence_level = 0.95,
dependence = "independent", held_out_evaluation = FALSE, seed = 0L),
c(common,
list("/fits/0/coefficients/0/term" = exact("(Intercept)", "term label"),
"/fits/0/coefficients/0/estimate" = tol(estimate, "order statistic y_(15) = y_(ceil(30 * 0.5))", "1e-12"),
"/fits/0/coefficients/0/range_low" = tol(estimate, "lower end of the minimizing interval", "1e-12"),
"/fits/0/coefficients/0/range_high" = tol(sorted[[16]], "upper end of the minimizing interval y_(16)", "1e-12"),
"/fits/0/coefficients/0/ambiguous" = exact(TRUE, "an interval of minimizers is flagged ambiguous"),
"/fits/0/check_loss" = tol(check_loss(d$y, rep(estimate, n), 0.5), "check loss of the median", "1e-12"),
"/fits/0/interval_status" = exact("available", "15 >= 10 tail rows"),
"/fits/0/tail_information" = tol(15, "min(n tau, n (1 - tau))", "1e-12"))),
list("Intercept-only median: n tau = 15 is an integer and y_(15) < y_(16), so the declared estimate is y_(15) and every value up to y_(16) minimizes the loss (analytic, base R sort)."))
generators/r/regression_quest.R
source("common.R", local = TRUE)
# QUEST trees (Statistics > Multivariate > QUEST tree). The product grows the tree on a seeded
# identity split (numpy PCG64) that R cannot reproduce, so the reference data make the root
# split invariant to the subset: two classes whose single numeric predictor takes one value
# per class (1 for low, 3 for high). Then, for any training subset holding both classes, the
# class-mean ANOVA has zero within-class variation (p = 0, the Bonferroni stage selects the
# predictor), both superclass variances are zero so Loh and Shih's equal-variance (linear)
# rule applies and the split point is the midpoint 2 whatever the priors, and the training
# and held-out errors are 0. Every oracle reads the exported CSV.
run <- generator_run(c("stats", "jsonlite"))
manual <- c(
"docs/manual/chapters/104-tree-extensions.tex:70-76: QUEST separates choosing a predictor from choosing its binary split.",
"docs/manual/chapters/104-tree-extensions.tex:79-86: numeric predictors receive class-mean ANOVA tests; the smallest first-pass p is selected if below alpha/K.",
"docs/manual/chapters/104-tree-extensions.tex:91-97: the split compares the two prior-weighted normal densities; equal variances use the linear discriminant rule.",
"docs/manual/chapters/104-tree-extensions.tex:106-110: minimum parent 20, child 7 and depth 5 are adjustable defaults; priors can follow training frequencies or be equal.",
"docs/manual/chapters/104-tree-extensions.tex:118-123: a seeded split reserves 30% of rows; turning pruning off is an explicit choice."
)
tol <- function(value, label, rel = "1e-9", abs = "0") {
list(comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = abs,
relative_tolerance = rel, rationale = label)
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
emit <- function(id, title, kind, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("regression", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.quest:compute_quest",
menu_items = list("questMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_QUEST.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"),
sha256 = ""),
oracle = list(oracle_class = "analytic",
source = "Loh and Shih (1997) split rule evaluated by hand for two point-mass classes",
citation = "Loh and Shih (1997) Split selection methods for classification trees, Statistica Sinica 7, 815-840, Algorithm 1 (equal-variance case).",
generator = list(path = "generators/r/regression_quest.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "Expected computed from the exported CSV.", manual)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-QUEST-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("regression", id, case)
}
set.seed(1006)
n <- 30
class <- rep(c("low", "high"), each = 15)[sample(n)]
frame <- data.frame(
vibration = ifelse(class == "low", 1, 3),
temperature = round(rnorm(n, 60, 4), 1),
class = class,
stringsAsFactors = FALSE
)
export_dataset(frame, "REG_QUEST", "Thirty components: vibration is 1 for the low class and 3 for the high class (seed 1006)",
"analytic QUEST split for point-mass classes",
"Constructed validation data; no third-party license.",
c(vibration = "Float64", temperature = "Float64", class = "String"))
d <- read_dataset("REG_QUEST")
stopifnot(all(d$vibration[d$class == "low"] == 1), all(d$vibration[d$class == "high"] == 3))
training <- n - floor(0.3 * n)
test <- n - training
stopifnot(training == 21, test == 9)
# The midpoint rule: with equal (zero) variances d = (xA + xB)/2 - s^2 log(pA/pB)/(xA - xB) = 2.
midpoint <- (1 + 3) / 2
root_outputs <- function() {
list(
"/training_rows" = exact(training, "n - floor(0.3 n)"),
"/test_rows" = exact(test, "floor(0.3 n) held out"),
"/missing_target" = exact(0L, "no missing target"),
"/excluded_count" = exact(0L, "no excluded row"),
"/classes" = list(comparison = "sequence", values = list("high", "low"),
rationale = "classes in code-point order"),
"/grown_leaves" = exact(2L, "the root splits once into two pure leaves"),
"/training_error" = tol(0, "the midpoint separates the point-mass classes", abs = "1e-15"),
"/test_error" = tol(0, "every held-out row is on its class's side of the midpoint", abs = "1e-15"),
"/training_rows_routed" = exact(0L, "no training row is missing the split variable"),
"/test_rows_routed" = exact(0L, "no held-out row is missing the split variable"),
"/nodes/0/n" = exact(training, "root holds the training rows"),
"/nodes/0/split/predictor" = exact("vibration", "the only predictor"),
"/nodes/0/split/kind" = exact("numeric", "numeric predictor"),
"/nodes/0/split/stage" = exact("bonferroni", "the ANOVA p-value 0 is below alpha/K"),
"/nodes/0/split/threshold" = tol(midpoint, "(xA + xB)/2: the linear rule with zero variances and any prior ratio", abs = "1e-12"),
"/nodes/0/split/method" = exact("linear", "equal (zero) class variances: the linear discriminant rule"),
"/nodes/0/split/superclasses/0/0" = exact("low", "superclass A starts at the smaller class mean (1)"),
"/nodes/0/split/superclasses/1/0" = exact("high", "superclass B holds the larger mean (3)"),
"/nodes/0/split/missing_rows" = exact(0L, "no missing predictor value"),
"/nodes/0/tests/0/predictor" = exact("vibration", "the first-pass test"),
"/nodes/0/tests/0/test" = exact("F", "class-mean ANOVA"),
"/nodes/0/tests/0/p_value" = tol(0, "zero within-class variation: p = 0", abs = "1e-300"),
"/nodes/0/tests/0/rows" = exact(training, "all training rows have a vibration value"),
"/nodes/1/stop_reason" = exact("pure node", "left child holds the low class only"),
"/nodes/2/stop_reason" = exact("pure node", "right child holds the high class only"),
"/nodes/1/prediction" = exact("low", "left child predicts low"),
"/nodes/2/prediction" = exact("high", "right child predicts high")
)
}
invariance <- "Analytic oracle: vibration is 1 for every low and 3 for every high row, so for any training subset holding both classes the ANOVA within-class sum of squares is 0 (p = 0, Bonferroni stage), superclass A is the low class (smaller mean) and B the high class, both variances are 0 so the equal-variance rule gives d = (1 + 3)/2 - 0 = 2 for any prior ratio, the children are pure, and no training or held-out row is misclassified."
emit("STAT-REG-QUEST-001", "QUEST root split between two point-mass classes, no pruning", "nominal",
list(target = "class", predictors = list("vibration"), min_parent = 2L, min_child = 1L,
max_depth = 5L, prune = FALSE),
c(root_outputs(), list("/selected_alpha" = list(comparison = "absent", rationale = "pruning is off"),
"/cv_folds_used" = list(comparison = "absent", rationale = "pruning is off"))),
list(invariance, "Minimum parent 2 and child 1 so the 21-row root may split whatever the class balance of the subset; pruning off."))
emit("STAT-REG-QUEST-EQUAL-001", "QUEST root split with equal priors", "axis_coverage",
list(target = "class", predictors = list("vibration"), min_parent = 2L, min_child = 1L,
max_depth = 5L, prune = FALSE, priors = "equal"),
root_outputs(),
list(invariance, "Equal priors change the prior ratio in the linear rule, whose variance factor is 0 here, so the threshold stays 2."))
emit("STAT-REG-QUEST-PRUNED-001", "QUEST root split kept by cost-complexity pruning", "axis_coverage",
list(target = "class", predictors = list("vibration"), min_parent = 2L, min_child = 1L,
max_depth = 5L, prune = TRUE, cv_folds = 5L),
c(root_outputs(), list("/cv_folds_used" = exact(5L, "five folds requested on 21 training rows"))),
list(invariance, "Cross-validated pruning with the 1-SE rule keeps the split: the two-leaf tree has cross-validated error 0 (every fold refits the same midpoint rule) and the root alone misclassifies the minority class, so the smallest tree within one SE of the minimum is the two-leaf tree."))
cat("QUEST cases generated\n")
generators/r/regression_survey_models.R
source("common.R", local = TRUE)
# Design-based regression models (Survey > Linear, Logistic, Ordinal and Cox regression)
# against R survey 4.5: svyglm, svyolr, svycoxph and regTermTest. Every reference reads the
# exported CSV back before computing.
suppressMessages({library(survey); library(survival)})
run <- generator_run(c("stats", "survey", "survival", "jsonlite"))
options(survey.lonely.psu = "fail")
tol <- function(value, why, rel = "1e-9", atol = "0") list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = atol, relative_tolerance = rel,
rationale = paste("R survey reference:", why)
)
tail <- function(value, why, rel = "1e-9") list(
comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = rel, rationale = paste("R survey p-value reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
MANUAL <- "docs/manual/chapters/87-complex-surveys.tex"
emit <- function(id, title, entry, menu, requirement, configuration, outputs, notes,
kind = "nominal") {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "regression", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_SURVEY_B7.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R survey package: svydesign, svrepdesign, svyglm, svyolr, svycoxph, regTermTest",
citation = "Lumley, T. (2024). survey: analysis of complex survey samples. R package version 4.5; Binder (1983, 1992); Lumley (2010) Complex Surveys, Wiley.",
generator = list(path = "generators/r/regression_survey_models.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.",
paste0(MANUAL, ":77-86: linear/logistic coefficient tests use t with D - rank(X) + 1 df and term tests F with the same denominator; normal/chi-square inference is an explicit alternative."),
paste0(MANUAL, ":86-89: ordinal proportional-odds and survey Cox models use asymptotic normal/chi-square inference; a positive ordinal coefficient shifts probability toward higher levels; a Cox coefficient exponentiates to a hazard ratio."))
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(requirement), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
# ---------------------------------------------------------------------------------------
# Dataset: two strata, five PSUs each, three rows per PSU (30 rows); four supplied replicate
# weight columns; outcomes simulated from x and the factor grp.
set.seed(20261010)
h <- rep(c("1", "2"), each = 15)
psu <- rep(sprintf("c%02d", 1:10), each = 3)
w <- round(rep(c(1.4, 2.1, 1.7, 2.6, 1.9, 3.1, 2.4, 1.6, 2.8, 2.2), each = 3) *
runif(30, 0.85, 1.15), 4)
N <- rep(c(25L, 40L), each = 15)
x <- round(rnorm(30, 0, 1), 3)
grp <- c("a", "b", "c", "b", "a", "c", "a", "c", "b", "a", "b", "c", "c", "a", "b",
"b", "c", "a", "a", "b", "c", "c", "a", "b", "b", "a", "c", "a", "c", "b")
gx <- ifelse(grp == "b", 0.6, ifelse(grp == "c", -0.4, 0))
y <- round(5 + 1.2 * x + 1.5 * gx + rnorm(30, 0, 1.1), 3)
eta <- -0.2 + 1.1 * x + gx
b <- ifelse(runif(30) < plogis(eta), "yes", "no")
latent <- 0.9 * x + gx + rlogis(30)
ord <- ifelse(latent < -0.6, "low", ifelse(latent < 0.7, "mid", "high"))
time <- round(rexp(30, rate = exp(-1.5 + 0.7 * x + 0.5 * gx)), 3)
status <- ifelse(runif(30) < 0.75, "event", "censored")
dom <- c("A", "A", "B", "A", "B", "A", "B", "A", "A", "B", "A", "B", "A", "B", "A",
"B", "A", "A", "B", "A", "B", "A", "B", "A", "A", "B", "A", "B", "A", "B")
rw <- round(w * matrix(runif(30 * 4, 0.6, 1.4), 30, 4), 4)
raw <- data.frame(h = h, psu = psu, w = w, N = N, x = x, grp = grp, y = y, b = b, ord = ord,
time = time, status = status, dom = dom,
rw1 = rw[, 1], rw2 = rw[, 2], rw3 = rw[, 3], rw4 = rw[, 4],
stringsAsFactors = FALSE)
export_dataset(raw, "REG_SURVEY_B7", "Constructed two-stratum clustered survey for regression (B7)",
"R survey package documentation (Lumley 2024)",
"Constructed validation data; no third-party license.",
c(h = "String", psu = "String", w = "Float64", N = "Int64", x = "Float64",
grp = "String", y = "Float64", b = "String", ord = "String", time = "Float64",
status = "String", dom = "String",
rw1 = "Float64", rw2 = "Float64", rw3 = "Float64", rw4 = "Float64"))
d <- read_dataset("REG_SURVEY_B7")
stopifnot(sum(d$b == "yes") >= 8, sum(d$b == "no") >= 8, min(table(d$ord)) >= 5,
sum(d$status == "event") >= 15, length(unique(d$time)) == nrow(d))
taylor <- svydesign(ids = ~psu, strata = ~h, weights = ~w, data = d)
with_fpc <- svydesign(ids = ~psu, strata = ~h, weights = ~w, fpc = ~N, data = d)
REP_SCALE <- 0.75
replicate <- svrepdesign(weights = ~w, repweights = d[, c("rw1", "rw2", "rw3", "rw4")],
type = "other", scale = REP_SCALE, rscales = rep(1, 4),
mse = TRUE, data = d, combined.weights = TRUE)
design_taylor <- list(weight = "w", variance = "linearization",
stage1 = list(strata = "h", units = "psu"))
design_fpc <- list(weight = "w", variance = "linearization",
stage1 = list(strata = "h", units = "psu", fpc = "N", fpc_kind = "count"))
design_rep <- list(weight = "w", variance = "replicate",
replicates = list(columns = list("rw1", "rw2", "rw3", "rw4"),
method = "other", weight_type = "full",
scale = REP_SCALE, rscales = NULL, fay_rho = NULL,
centering = "full_sample"))
D_T <- as.integer(degf(taylor))
D_F <- as.integer(degf(with_fpc))
D_R <- as.integer(degf(replicate))
# Coefficient rows: estimate, SE, statistic, p, interval (t with df, or normal when df is
# NULL) and, when asked, the exponentiated ratio with its interval.
coefficient_outputs <- function(prefix, names, theta, covariance, df, level, rel, ratio,
label, atol = "0", tail_rel = rel) {
out <- list()
q <- 0.5 + level / 2
critical <- if (is.null(df)) qnorm(q) else qt(q, df)
for (index in seq_along(names)) {
name <- names[[index]]
est <- as.double(theta[[name]])
se <- sqrt(as.double(covariance[name, name]))
stat <- est / se
p <- if (is.null(df)) 2 * pnorm(-abs(stat)) else 2 * pt(-abs(stat), df)
base <- paste0(prefix, "/", index - 1)
what <- paste(label, "coefficient", name)
out[[paste0(base, "/estimate")]] <- tol(est, paste(what, "estimate"), rel, atol)
out[[paste0(base, "/standard_error")]] <- tol(se, paste(what, "design SE"), rel, atol)
out[[paste0(base, "/statistic")]] <- tol(stat, paste(what, "estimate / SE"), rel, atol)
out[[paste0(base, "/p_value")]] <- tail(p, paste(what, "two-sided p"), tail_rel)
out[[paste0(base, "/lower")]] <- tol(est - critical * se, paste(what, "interval lower"), rel, atol)
out[[paste0(base, "/upper")]] <- tol(est + critical * se, paste(what, "interval upper"), rel, atol)
if (!is.null(df)) out[[paste0(base, "/df")]] <- exact(df, "D - rank(X) + 1")
if (ratio && name != "(Intercept)") {
out[[paste0(base, "/ratio")]] <- tol(exp(est), paste(what, "exponentiated"), rel, atol)
out[[paste0(base, "/ratio_lower")]] <- tol(exp(est - critical * se), paste(what, "ratio lower"), rel, atol)
out[[paste0(base, "/ratio_upper")]] <- tol(exp(est + critical * se), paste(what, "ratio upper"), rel, atol)
}
}
out
}
# Wald term tests from the coefficient covariance: F(q, df) or chi-square(q).
wald_outputs <- function(term, names, theta, covariance, df, rel, atol = "0", tail_rel = rel) {
b <- as.double(theta[names])
v <- covariance[names, names, drop = FALSE]
wald <- as.double(t(b) %*% solve(v) %*% b)
q <- length(names)
prefix <- paste0("/fit/term_tests/[term=", term, "]")
out <- list()
out[[paste0(prefix, "/df")]] <- exact(q, "coefficients in the term")
if (is.null(df)) {
out[[paste0(prefix, "/statistic")]] <- tol(wald, paste("Wald chi-square b' V^-1 b for", term), rel, atol)
out[[paste0(prefix, "/p_value")]] <- tail(pchisq(wald, q, lower.tail = FALSE), paste("chi-square(q) p for", term), tail_rel)
} else {
out[[paste0(prefix, "/statistic")]] <- tol(wald / q, paste("Wald F for", term), rel)
out[[paste0(prefix, "/denominator_df")]] <- exact(df, "denominator df D - rank(X) + 1")
out[[paste0(prefix, "/p_value")]] <- tail(pf(wald / q, q, df, lower.tail = FALSE), paste("F(q, df) p for", term), rel)
}
out
}
fit_outputs <- function(rows_used, rank, design_df, test_df) {
out <- list("/fit/rows_used" = exact(rows_used, "complete analysed rows"),
"/fit/rank" = exact(rank, "model matrix columns"),
"/fit/design_df" = exact(design_df, "PSUs minus strata (or replicate rank minus 1)"))
if (!is.null(test_df)) out[["/fit/test_df"]] <- exact(test_df, "D - rank(X) + 1")
out
}
G_NAMES <- c("grpb", "grpc")
# --- Linear regression ----------------------------------------------------------------
LINEAR <- "tabelz.analysis.survey_models:compute_survey_linear"
LIN_REQ <- "REQ-STAT-REG-SURVEY-LINEAR-001"
linear_case <- function(id, title, design, design_json, D, inference, level, extra_config,
formula, notes, kind, weighted_r2_design = design) {
fit <- svyglm(formula, design = design)
names <- names(coef(fit))
rank <- length(names)
df <- if (inference == "normal") NULL else D - rank + 1
if (!is.null(df)) stopifnot(fit$df.residual == df)
V <- vcov(fit)
summary_p <- summary(fit)$coefficients[, "Pr(>|t|)"]
outputs <- coefficient_outputs("/fit/coefficients", names, coef(fit), V, df, level, "1e-9",
FALSE, "linear")
if (!is.null(df)) {
outputs[["/fit/coefficients/1/p_value"]] <- tail(summary_p[[2]], "summary.svyglm t p-value for grpb (df.residual = D - rank + 1)")
g_test <- regTermTest(fit, ~grp, method = "Wald")
stopifnot(all.equal(as.double(g_test$ddf), df))
outputs[["/fit/term_tests/[term=grp]/statistic"]] <- tol(as.double(g_test$Ftest), "regTermTest Wald F for grp")
outputs[["/fit/term_tests/[term=grp]/p_value"]] <- tail(as.double(g_test$p), "regTermTest F(q, ddf) p for grp")
outputs[["/fit/term_tests/[term=grp]/denominator_df"]] <- exact(df, "regTermTest ddf")
x_test <- regTermTest(fit, ~x, method = "Wald")
outputs[["/fit/term_tests/[term=x]/statistic"]] <- tol(as.double(x_test$Ftest), "regTermTest Wald F for x")
outputs[["/fit/term_tests/[term=x]/p_value"]] <- tail(as.double(x_test$p), "regTermTest F(q, ddf) p for x")
if (any(grepl(":", names))) {
i_test <- regTermTest(fit, ~grp:x, method = "Wald")
outputs[["/fit/term_tests/[term=grp:x]/statistic"]] <- tol(as.double(i_test$Ftest), "regTermTest Wald F for grp:x")
outputs[["/fit/term_tests/[term=grp:x]/p_value"]] <- tail(as.double(i_test$p), "regTermTest F(q, ddf) p for grp:x")
}
} else {
outputs <- c(outputs, wald_outputs("grp", G_NAMES, coef(fit), V, NULL, "1e-9"),
wald_outputs("x", "x", coef(fit), V, NULL, "1e-9"))
g_chi <- regTermTest(fit, ~grp, method = "Wald", df = Inf)
outputs[["/fit/term_tests/[term=grp]/statistic"]] <- tol(as.double(g_chi$chisq), "regTermTest Wald chi-square for grp (df = Inf)")
outputs[["/fit/term_tests/[term=grp]/p_value"]] <- tail(as.double(g_chi$p), "regTermTest chi-square p for grp")
}
all_test <- if (is.null(df)) regTermTest(fit, ~grp + x, method = "Wald", df = Inf) else regTermTest(fit, ~grp + x, method = "Wald")
if (!any(grepl(":", names))) {
outputs[["/fit/term_tests/[term=All predictors]/statistic"]] <-
tol(as.double(if (is.null(df)) all_test$chisq else all_test$Ftest), "regTermTest for all predictors")
outputs[["/fit/term_tests/[term=All predictors]/p_value"]] <- tail(as.double(all_test$p), "regTermTest p for all predictors")
}
wts <- weights(design, "sampling")
used <- wts > 0
r <- residuals(fit, type = "response")[used]
yv <- fit$y[used]
ybar <- sum(wts[used] * yv) / sum(wts[used])
r2 <- 1 - sum(wts[used] * r^2) / sum(wts[used] * (yv - ybar)^2)
outputs[["/fit/statistics/[label=Weighted R-squared]/value"]] <- tol(r2, "1 - sum w r^2 / sum w (y - weighted mean)^2 over the analysed rows")
outputs[["/fit/statistics/[label=Sum of weights (analysed rows)]/value"]] <- tol(sum(wts[used]), "sum of the sampling weights of the analysed rows")
outputs <- c(outputs, fit_outputs(sum(used), rank, D, df))
config <- c(list(design = design_json, response = "y", factors = list("grp"),
covariates = list("x"), inference = inference, confidence_level = level),
extra_config)
emit(id, title, LINEAR, "surveyLinearMenuItem", LIN_REQ, config, outputs, notes, kind)
}
linear_case("STAT-REG-SURVEY-LINEAR-001", "Survey linear regression with design-df t and F inference",
taylor, design_taylor, D_T, "design_df", 0.95, list(), y ~ grp + x,
c("svyglm(y ~ grp + x): coefficients (Intercept), grp = b (vs a), grp = c (vs a), x in that order (factors before covariates).",
"D = 10 PSUs - 2 strata = 8; coefficient t and term-test F use D - rank(X) + 1 = 5 (svyglm df.residual)."),
"nominal")
linear_case("STAT-REG-SURVEY-LINEAR-002", "Survey linear regression with normal inference",
taylor, design_taylor, D_T, "normal", 0.95, list(), y ~ grp + x,
c("Normal inference: z = estimate / SE, 2 * pnorm(-|z|), qnorm intervals; term tests are Wald chi-square(q) (regTermTest df = Inf)."),
"axis_coverage")
domain_a <- subset(taylor, dom == "A")
linear_case("STAT-REG-SURVEY-LINEAR-003", "Survey linear regression with an interaction and 90% intervals",
taylor, design_taylor, D_T, "design_df", 0.90,
list(interactions = list("grp:x")), y ~ grp + x + grp:x,
c("svyglm(y ~ grp + x + grp:x): interaction columns grp = b (vs a):x and grp = c (vs a):x follow the main effects (indices 4 and 5).",
"Test df = D - rank(X) + 1 = 8 - 6 + 1 = 3; 90% intervals use qt(0.95, 3)."),
"axis_coverage")
# --- Logistic regression ----------------------------------------------------------------
LOGISTIC <- "tabelz.analysis.survey_models:compute_survey_logistic"
LOG_REQ <- "REQ-STAT-REG-SURVEY-LOGISTIC-001"
logistic_case <- function(id, title, design, design_json, D, inference, level, notes, kind) {
fit <- svyglm(I(b == "yes") ~ grp + x, design = design, family = quasibinomial(),
control = glm.control(epsilon = 1e-14, maxit = 200))
stopifnot(fit$converged)
names <- names(coef(fit))
rank <- length(names)
df <- if (inference == "normal") NULL else D - rank + 1
if (!is.null(df)) stopifnot(fit$df.residual == df)
V <- vcov(fit)
outputs <- coefficient_outputs("/fit/coefficients", names, coef(fit), V, df, level, "1e-6",
TRUE, "logistic", atol = "1e-6", tail_rel = "1e-5")
if (is.null(df)) {
g_test <- regTermTest(fit, ~grp, method = "Wald", df = Inf)
outputs[["/fit/term_tests/[term=grp]/statistic"]] <- tol(as.double(g_test$chisq), "regTermTest Wald chi-square for grp", "1e-6", "1e-6")
outputs[["/fit/term_tests/[term=grp]/p_value"]] <- tail(as.double(g_test$p), "chi-square(2) p for grp", "1e-5")
} else {
g_test <- regTermTest(fit, ~grp, method = "Wald")
outputs[["/fit/term_tests/[term=grp]/statistic"]] <- tol(as.double(g_test$Ftest), "regTermTest Wald F for grp", "1e-6", "1e-6")
outputs[["/fit/term_tests/[term=grp]/p_value"]] <- tail(as.double(g_test$p), "F(2, ddf) p for grp", "1e-5")
outputs[["/fit/term_tests/[term=grp]/denominator_df"]] <- exact(df, "regTermTest ddf")
}
outputs[["/fit/statistics/[label=Event rows]/value"]] <- tol(sum(d$b == "yes"), "rows with b = yes")
outputs <- c(outputs, fit_outputs(nrow(d), rank, D, df))
emit(id, title, LOGISTIC, "surveyLogisticMenuItem", LOG_REQ,
list(design = design_json, response = "b", event_value = "yes", factors = list("grp"),
covariates = list("x"), inference = inference, confidence_level = level),
outputs, c(notes, "svyglm(family = quasibinomial()) pseudo-maximum likelihood; odds ratios are exp(coefficient) with the interval exponentiated.",
"Iterative fit: the reference IRLS is converged to 1e-14 in deviance; outputs allow 1e-6 absolute or relative (p-values 1e-5 relative) for the engine's own stopping tolerance."),
kind)
}
logistic_case("STAT-REG-SURVEY-LOGIT-001", "Survey logistic regression with design-df inference",
taylor, design_taylor, D_T, "design_df", 0.95,
c("Coefficient t with D - rank(X) + 1 = 8 - 4 + 1 = 5 df; term F(2, 5)."), "nominal")
logistic_case("STAT-REG-SURVEY-LOGIT-002", "Survey logistic regression with normal inference",
taylor, design_taylor, D_T, "normal", 0.95,
c("Normal inference: z tests, qnorm intervals, chi-square term tests."), "axis_coverage")
logistic_case("STAT-REG-SURVEY-LOGIT-003", "Survey logistic regression with a first-stage FPC and 90% intervals",
with_fpc, design_fpc, D_F, "design_df", 0.90,
c("svydesign(fpc = ~N) with stratum PSU population counts 25 and 40; 90% intervals use qt(0.95, 5)."),
"axis_coverage")
# --- Ordinal (proportional odds) regression ---------------------------------------------
ORDINAL <- "tabelz.analysis.survey_models:compute_survey_ordinal"
ORD_REQ <- "REQ-STAT-REG-SURVEY-ORDINAL-001"
LEVELS <- c("low", "mid", "high")
# Newton refinement of the survey-weighted proportional-odds log-likelihood, written from
# its definition logit P(Y <= j) = zeta_j - eta. svyolr's BFGS stops on the objective value,
# which leaves the flattest direction (grpb, |z| about 0.1) resolved only to roughly 1e-4,
# while the engine's Newton fit stops on the parameter step itself (1e-10). Polishing
# svyolr's solution with Newton steps until the parameter step is below 1e-12 resolves every
# direction the engine resolves; the design variance is rebuilt at the polished parameters
# with survey's own svyrecvar, exactly the way svyolr builds it (score rows %*% solve(H)).
ordinal_grads <- function(par, x, codes, levels, w) {
q <- levels - 1L
n <- nrow(x)
p_slope <- ncol(x)
zeta <- par[seq_len(q)]
eta <- as.vector(x %*% par[q + seq_len(p_slope)])
upper <- zeta[pmin(codes + 1L, q)] - eta
lower <- zeta[pmax(codes, 1L)] - eta
top <- codes == levels - 1L
bottom <- codes == 0L
safe_u <- ifelse(top, 0, upper)
safe_l <- ifelse(bottom, 0, lower)
s_u <- ifelse(top, 0, stats::dlogis(safe_u))
s_l <- ifelse(bottom, 0, stats::dlogis(safe_l))
p <- ifelse(top, 1, stats::plogis(safe_u)) - ifelse(bottom, 0, stats::plogis(safe_l))
if (any(p <= 0)) return(NULL)
u_dir <- cbind(matrix(0, n, q), -x)
l_dir <- cbind(matrix(0, n, q), -x)
u_dir[cbind(which(!top), codes[!top] + 1L)] <- 1
l_dir[cbind(which(!bottom), codes[!bottom])] <- 1
grad_p <- s_u * u_dir - s_l * l_dir
score_rows <- (w / p) * grad_p
dd_u <- s_u * (1 - 2 * stats::plogis(safe_u))
dd_l <- s_l * (1 - 2 * stats::plogis(safe_l))
curvature <- crossprod(u_dir * (w * dd_u / p), u_dir) - crossprod(l_dir * (w * dd_l / p), l_dir)
hessian <- curvature - crossprod(score_rows, grad_p / p)
list(ll = sum(w * log(p)), score = colSums(score_rows), score_rows = score_rows,
information = -hessian)
}
ordinal_newton <- function(par, x, codes, levels, w) {
for (step in 1:100) {
parts <- ordinal_grads(par, x, codes, levels, w)
if (is.null(parts)) stop("ordinal refinement left the parameter space")
direction <- solve(parts$information, parts$score)
scale <- 1
repeat {
candidate <- par + scale * direction
moved_parts <- ordinal_grads(candidate, x, codes, levels, w)
if (!is.null(moved_parts) &&
moved_parts$ll >= parts$ll - 1e-10 * (1 + abs(parts$ll))) break
scale <- scale / 2
if (scale < 1e-12) stop("ordinal refinement made no progress")
}
moved <- max(abs(candidate - par))
par <- candidate
if (moved <= 1e-12 * (1 + max(abs(par)))) {
moved_parts$par <- par
return(moved_parts)
}
}
stop("ordinal refinement did not converge")
}
ordinal_refine <- function(fit, design) {
w <- weights(design, "sampling")
keep <- w > 0
data <- design$variables
mm <- model.matrix(ordf ~ grp + x, data = data)[, -1, drop = FALSE]
codes <- as.integer(data$ordf) - 1L
n_levels <- length(levels(data$ordf))
par0 <- c(fit$zeta, fit$coefficients)
start <- ordinal_grads(par0, mm, codes, n_levels, w)
mean_wt <- mean(w[keep])
stopifnot(abs(start$ll + fit$deviance * mean_wt / 2) <= 1e-7 * (1 + abs(start$ll)))
refined <- ordinal_newton(par0, mm, codes, n_levels, w)
stopifnot(refined$ll >= start$ll - 1e-9 * (1 + abs(start$ll)),
max(abs(refined$par - par0)) < 1e-3)
vcov_refined <- svyrecvar(refined$score_rows %*% solve(refined$information),
design$cluster, design$strata, design$fpc,
postStrata = design$postStrata)
vcov_refined <- (vcov_refined + t(vcov_refined)) / 2
theta <- refined$par
names(theta) <- c(names(fit$zeta), names(fit$coefficients))
dimnames(vcov_refined) <- list(names(theta), names(theta))
# svyolr orders its covariance slopes first, thresholds last; compare like for like.
svyolr_vcov <- vcov(fit)[names(theta), names(theta)]
stopifnot(max(abs(vcov_refined - svyolr_vcov)) < 1e-5)
list(theta = theta, vcov = vcov_refined, ll = refined$ll, svyolr_ll = start$ll)
}
ordinal_case <- function(id, title, design, design_json, D, level, extra_config, notes, kind) {
data_levels <- factor(design$variables$ord, levels = LEVELS)
design$variables$ordf <- data_levels
fit <- svyolr(ordf ~ grp + x, design = design, control = list(reltol = 1e-15, maxit = 5000))
refined <- ordinal_refine(fit, design)
V <- refined$vcov
zeta <- fit$zeta
theta <- refined$theta
slope_names <- setdiff(names(theta), names(zeta))
stopifnot(identical(slope_names, c(G_NAMES, "x")))
outputs <- coefficient_outputs("/fit/coefficients", slope_names, theta, V, NULL, level,
"1e-6", TRUE, "ordinal", atol = "1e-7", tail_rel = "1e-5")
outputs <- c(outputs, coefficient_outputs("/fit/thresholds", names(zeta), theta, V, NULL,
level, "1e-6", FALSE, "ordinal threshold",
atol = "1e-7", tail_rel = "1e-5"))
outputs <- c(outputs, wald_outputs("grp", G_NAMES, theta, V, NULL, "1e-6", "1e-7", "1e-5"),
wald_outputs("x", "x", theta, V, NULL, "1e-6", "1e-7", "1e-5"),
wald_outputs("All predictors", c(G_NAMES, "x"), theta, V, NULL, "1e-6", "1e-7", "1e-5"))
used <- sum(weights(design, "sampling") > 0)
outputs <- c(outputs, fit_outputs(used, length(slope_names), D, NULL))
config <- c(list(design = design_json, response = "ord", level_order = as.list(LEVELS),
factors = list("grp"), covariates = list("x"), confidence_level = level),
extra_config)
emit(id, title, ORDINAL, "surveyOrdinalMenuItem", ORD_REQ, config, outputs,
c(notes, "svyolr(factor(ord, levels = c('low', 'mid', 'high')) ~ grp + x): logit P(Y <= j) = zeta_j - eta, the engine's threshold_j - x'b.",
"Thresholds low|mid and mid|high are listed before the coefficients; term Wald chi-square b' V^-1 b from the design covariance.",
paste("Reference: svyolr (BFGS, reltol 1e-15) cross-checked against an R Newton refinement of the same weighted",
"log-likelihood (parameter step below 1e-12, likelihood not below svyolr's, parameters within 1e-3,",
"svyrecvar design covariance within 1e-3 of svyolr's own); the refined values are compared because svyolr's",
"function-value stopping leaves the near-null grpb direction resolved only to about 1e-4, while the engine's",
"Newton stops on the parameter step (1e-10). Agreement expected between the two parameter-converged fits:",
"outputs allow 1e-7 absolute or 1e-6 relative (p-values 1e-5 relative).")),
kind)
}
ordinal_case("STAT-REG-SURVEY-ORD-001", "Survey proportional-odds regression (Taylor)",
taylor, design_taylor, D_T, 0.95, list(),
c("Normal reference for every coefficient; D = 8 shown for reference only."), "nominal")
ordinal_case("STAT-REG-SURVEY-ORD-002", "Survey proportional-odds regression in a domain with 90% intervals",
domain_a, design_taylor, D_T, 0.90,
list(domain_column = "dom", domain_values = list("A")),
c("svyolr on subset(design, dom == 'A'); qnorm(0.95) intervals."), "axis_coverage")
# --- Cox regression ------------------------------------------------------------------------
COX <- "tabelz.analysis.survey_models:compute_survey_cox"
COX_REQ <- "REQ-STAT-REG-SURVEY-COX-001"
cox_case <- function(id, title, design, design_json, D, level, notes, kind) {
design$variables$event <- as.integer(design$variables$status == "event")
fit <- svycoxph(Surv(time, event) ~ grp + x, design = design)
V <- vcov(fit)
theta <- coef(fit)
outputs <- coefficient_outputs("/fit/coefficients", names(theta), theta, V, NULL, level,
"1e-6", TRUE, "Cox")
outputs <- c(outputs, wald_outputs("grp", G_NAMES, theta, V, NULL, "1e-6"),
wald_outputs("x", "x", theta, V, NULL, "1e-6"),
wald_outputs("All predictors", c(G_NAMES, "x"), theta, V, NULL, "1e-6"))
outputs[["/events"]] <- exact(sum(d$status == "event"), "rows with status = event")
outputs[["/fit/statistics/[label=Weighted events]/value"]] <- tol(sum(d$w[d$status == "event"]), "sum of weights over event rows")
outputs <- c(outputs, fit_outputs(nrow(d), length(theta), D, NULL))
emit(id, title, COX, "surveyCoxMenuItem", COX_REQ,
list(design = design_json, time = "time", status = "status", event_value = "event",
factors = list("grp"), covariates = list("x"), confidence_level = level),
outputs, c(notes, "svycoxph(Surv(time, event) ~ grp + x): Efron ties (none here, times are distinct), design-based (Binder 1992) variance.",
"Hazard ratios exp(coefficient) with normal intervals; term Wald chi-square from vcov(svycoxph).",
"Iterative fit: 1e-6 relative tolerance."),
kind)
}
cox_case("STAT-REG-SURVEY-COX-001", "Survey Cox regression (Taylor)", taylor, design_taylor, D_T, 0.95,
c("Linearized design variance of the weighted score residuals."), "nominal")
cox_case("STAT-REG-SURVEY-COX-002", "Survey Cox regression from supplied replicate weights with 90% intervals",
replicate, design_rep, D_R, 0.90,
c("svycoxph on svrepdesign(type = 'other', scale = 0.75, mse = TRUE): the model is refitted under each replicate weight column.",
"degf = qr(replicate weights)$rank - 1 = 3 (reference only; normal inference)."), "axis_coverage")
cat("regression survey model references generated\n")
generators/r/regression_two_stage.R
source("common.R", local = TRUE)
# Two-stage least squares written out in base R matrix algebra (no IV package is in the
# lock file): X_hat = P_Z X, beta = (X_hat'X_hat)^-1 X_hat'y, structural residuals
# u = y - X beta, s^2 = u'u/(n - k), classical or HC1 covariance, first-stage partial F,
# Sargan n R^2. Every oracle re-reads the exported CSV.
set.seed(20261010)
n <- 30L
z1 <- round(rnorm(n), 4)
z2 <- round(runif(n, -1, 2), 4)
w <- round(rnorm(n, 1, 0.8), 4)
shared <- rnorm(n)
x <- round(0.5 + 0.9 * z1 + 0.7 * z2 + 0.3 * w + 0.6 * shared + rnorm(n, sd = 0.4), 4)
y <- round(1.5 + 1.2 * x - 0.8 * w + 0.5 * shared + rnorm(n, sd = 0.5), 4)
w[12] <- NA
constructed <- data.frame(y = y, x = x, w = w, z1 = z1, z2 = z2)
export_dataset(
constructed, "REG_IV_01", "Constructed by regression_two_stage.R",
"Base R matrix algebra for 2SLS; stats::lm for the first stages and the Sargan regression",
"Constructed test data; no third-party dataset license.",
c(y = "Float64", x = "Float64", w = "Float64", z1 = "Float64", z2 = "Float64")
)
d <- read_dataset("REG_IV_01")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier, rationale = paste("Independent base-R 2SLS algebra:", label))
prob <- function(x, label)
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = "1e-8",
rationale = paste("Independent R tail probability:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, kind, config, outputs, notes, excluded = list()) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.two_stage_least_squares:compute_two_stage",
menu_items = list("twoStageLeastSquaresMenuItem"), title = gsub("-", " ", id),
case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_IV_01.json", sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "Base R: solve/crossprod for the IV estimator, stats::lm for first stages and the Sargan auxiliary regression, stats::pt/pf/pchisq",
citation = "R Core Team, stats package help: lm, summary.lm, pt, pf, pchisq. Wooldridge, Introductory Econometrics, ch. 15 (2SLS, first-stage F, Sargan n R^2).",
generator = list(path = "generators/r/regression_two_stage.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes,
"Instruments Z = [1, exogenous, excluded instruments]; X = [1, exogenous, endogenous]; beta = (X'P_Z X)^-1 X'P_Z y; residual variance from u = y - X beta on n - k df; t intervals (docs/manual/chapters/86-regression-and-count-models.tex:90-105).",
"HC1 covariance: (X_hat'X_hat)^-1 [sum u_i^2 x_hat_i x_hat_i'] (X_hat'X_hat)^-1 n/(n-k); the Sargan statistic n R^2 of u on Z is unchanged by it; robust first-stage F is the HC1 joint Wald statistic of the excluded instruments divided by their number.",
"First-stage F compares the first-stage regression on Z with the regression on the included exogenous regressors; partial R^2 = (RSS_restricted - RSS_full)/RSS_restricted.",
"Rows with a missing value in any role are dropped; excluded rows are removed first.",
"Expected computed from the exported CSV."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-TWO-STAGE-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
two_stage <- function(data, endogenous, exogenous, instruments, covariance, level) {
rows <- stats::complete.cases(data[c("y", endogenous, exogenous, instruments)])
data <- data[rows, ]
n <- nrow(data)
y <- data$y
included <- cbind(1, as.matrix(data[exogenous]))
X <- cbind(included, as.matrix(data[endogenous]))
Z <- cbind(included, as.matrix(data[instruments]))
k <- ncol(X)
kz <- ncol(Z)
Xhat <- Z %*% solve(crossprod(Z), crossprod(Z, X))
gram_inv <- solve(crossprod(Xhat))
beta <- drop(gram_inv %*% crossprod(Xhat, y))
# Sanity check: the second-stage lm on the fitted regressors gives the same coefficients.
check <- stats::lm(y ~ Xhat - 1)
stopifnot(max(abs(stats::coef(check) - beta)) < 1e-9)
u <- drop(y - X %*% beta)
df <- n - k
s2 <- sum(u^2) / df
if (covariance == "hc1") {
meat <- crossprod(Xhat * u)
V <- gram_inv %*% meat %*% gram_inv * n / df
} else {
V <- s2 * gram_inv
}
se <- sqrt(diag(V))
tcrit <- stats::qt(0.5 + level / 2, df)
tv <- beta / se
coefs <- lapply(seq_len(k), function(j) list(
estimate = beta[[j]], standard_error = se[[j]], t_value = tv[[j]],
p_value = 2 * stats::pt(abs(tv[[j]]), df, lower.tail = FALSE),
lower = beta[[j]] - tcrit * se[[j]], upper = beta[[j]] + tcrit * se[[j]]))
q <- length(instruments)
stages <- lapply(endogenous, function(name) {
target <- data[[name]]
full <- stats::lm(target ~ Z - 1)
restricted <- stats::lm(target ~ included - 1)
rss_full <- sum(stats::residuals(full)^2)
rss_restricted <- sum(stats::residuals(restricted)^2)
f <- ((rss_restricted - rss_full) / q) / (rss_full / (n - kz))
out <- list(r_squared = 1 - rss_full / sum((target - mean(target))^2),
partial_r_squared = (rss_restricted - rss_full) / rss_restricted,
f_value = f, df1 = q, df2 = n - kz,
p_value = stats::pf(f, q, n - kz, lower.tail = FALSE))
if (covariance == "hc1") {
e <- stats::residuals(full)
b <- stats::coef(full)
bread <- solve(crossprod(Z))
Vz <- bread %*% crossprod(Z * e) %*% bread * n / (n - kz)
idx <- (kz - q + 1):kz
wald <- drop(t(b[idx]) %*% solve(Vz[idx, idx, drop = FALSE], b[idx]))
out$robust_f <- wald / q
out$robust_p <- stats::pf(wald / q, q, n - kz, lower.tail = FALSE)
}
out
})
overid <- q - length(endogenous)
sargan <- NULL
if (overid > 0) {
aux <- stats::lm(u ~ Z - 1)
statistic <- n * summary(stats::lm(u ~ Z[, -1]))$r.squared
centered <- u - mean(u)
stopifnot(abs(statistic - n * (sum(centered^2) - sum(stats::residuals(aux)^2)) /
sum(centered^2)) < 1e-9)
sargan <- list(statistic = statistic, df = overid,
p_value = stats::pchisq(statistic, overid, lower.tail = FALSE))
}
slopes <- 2:k
b <- beta[slopes]
wald_f <- drop(t(b) %*% solve(V[slopes, slopes, drop = FALSE], b)) / length(slopes)
list(n = n, coefficients = coefs, first_stages = stages, sargan = sargan,
residual_standard_error = sqrt(s2),
r_squared = 1 - sum(u^2) / sum((y - mean(y))^2),
wald_f = wald_f, wald_df1 = length(slopes), wald_df2 = df,
wald_p = stats::pf(wald_f, length(slopes), df, lower.tail = FALSE))
}
outputs_for <- function(r, terms, roles) {
out <- list("/n" = exact(as.integer(r$n), "complete rows"),
"/residual_standard_error" = tol(r$residual_standard_error, "structural residual SE"),
"/r_squared" = tol(r$r_squared, "structural-residual R-squared"),
"/wald_f" = tol(r$wald_f, "joint Wald F of the slopes"),
"/wald_df1" = exact(as.integer(r$wald_df1), "Wald numerator df"),
"/wald_df2" = exact(as.integer(r$wald_df2), "Wald denominator df"),
"/wald_p" = prob(r$wald_p, "joint Wald F tail"))
for (j in seq_along(r$coefficients)) {
c <- r$coefficients[[j]]
base <- sprintf("/coefficients/%d/", j - 1L)
out[[paste0(base, "term")]] <- exact(terms[[j]], "coefficient label")
out[[paste0(base, "role")]] <- exact(roles[[j]], "coefficient role")
out[[paste0(base, "estimate")]] <- tol(c$estimate, "2SLS estimate")
out[[paste0(base, "standard_error")]] <- tol(c$standard_error, "2SLS standard error")
out[[paste0(base, "t_value")]] <- tol(c$t_value, "t statistic")
out[[paste0(base, "p_value")]] <- prob(c$p_value, "two-sided t tail")
out[[paste0(base, "lower")]] <- tol(c$lower, "confidence limit")
out[[paste0(base, "upper")]] <- tol(c$upper, "confidence limit")
}
for (j in seq_along(r$first_stages)) {
s <- r$first_stages[[j]]
base <- sprintf("/first_stages/%d/", j - 1L)
out[[paste0(base, "r_squared")]] <- tol(s$r_squared, "first-stage R-squared")
out[[paste0(base, "partial_r_squared")]] <- tol(s$partial_r_squared, "first-stage partial R-squared")
out[[paste0(base, "f_value")]] <- tol(s$f_value, "first-stage partial F")
out[[paste0(base, "df1")]] <- exact(as.integer(s$df1), "first-stage numerator df")
out[[paste0(base, "df2")]] <- exact(as.integer(s$df2), "first-stage denominator df")
out[[paste0(base, "p_value")]] <- prob(s$p_value, "first-stage F tail")
if (!is.null(s$robust_f)) {
out[[paste0(base, "robust_f")]] <- tol(s$robust_f, "HC1 first-stage Wald / q")
out[[paste0(base, "robust_p")]] <- prob(s$robust_p, "HC1 first-stage F tail")
}
}
if (!is.null(r$sargan)) {
out[["/sargan/statistic"]] <- tol(r$sargan$statistic, "Sargan n R-squared")
out[["/sargan/df"]] <- exact(as.integer(r$sargan$df), "over-identification df")
out[["/sargan/p_value"]] <- prob(r$sargan$p_value, "Sargan chi-square tail")
}
out
}
# 001: over-identified, one included exogenous regressor, classical covariance.
r <- two_stage(d, "x", "w", c("z1", "z2"), "classical", 0.95)
emit("STAT-REG-TWO-STAGE-001", "nominal",
list(response = "y", endogenous = list("x"), exogenous = list("w"),
instruments = list("z1", "z2"), covariance = "classical", confidence_level = 0.95),
c(outputs_for(r, c("(Intercept)", "w", "x"), c("constant", "exogenous", "endogenous")),
list("/missing_count" = exact(1L, "one row has a missing w"),
"/excluded_count" = exact(0L, "no excluded rows"))),
list("Classical covariance s^2 (X_hat'X_hat)^-1; two excluded instruments for one endogenous regressor give a 1-df Sargan test."))
# 002: the same model with the HC1 sandwich.
r <- two_stage(d, "x", "w", c("z1", "z2"), "hc1", 0.95)
emit("STAT-REG-TWO-STAGE-002", "axis_coverage",
list(response = "y", endogenous = list("x"), exogenous = list("w"),
instruments = list("z1", "z2"), covariance = "hc1", confidence_level = 0.95),
outputs_for(r, c("(Intercept)", "w", "x"), c("constant", "exogenous", "endogenous")),
list("HC1 coefficient covariance and the robust first-stage F; the Sargan statistic is the homoskedastic n R^2."))
# 003: exactly identified, no included exogenous regressor, 90% intervals: no Sargan test.
r <- two_stage(d, "x", character(0), "z1", "classical", 0.90)
stopifnot(is.null(r$sargan))
emit("STAT-REG-TWO-STAGE-003", "axis_coverage",
list(response = "y", endogenous = list("x"), exogenous = list(),
instruments = list("z1"), covariance = "classical", confidence_level = 0.90),
outputs_for(r, c("(Intercept)", "x"), c("constant", "endogenous")),
list("Exactly identified (one instrument, one endogenous regressor): the result carries no Sargan test, which the expected outputs therefore omit; 90% t intervals."))
# 004: excluded rows (0-based 0 and 5) with both instruments and the exogenous regressor.
r <- two_stage(d[-c(1, 6), ], "x", "w", c("z1", "z2"), "hc1", 0.99)
emit("STAT-REG-TWO-STAGE-004", "axis_coverage",
list(response = "y", endogenous = list("x"), exogenous = list("w"),
instruments = list("z1", "z2"), covariance = "hc1", confidence_level = 0.99),
c(outputs_for(r, c("(Intercept)", "w", "x"), c("constant", "exogenous", "endogenous")),
list("/missing_count" = exact(1L, "one row has a missing w"),
"/excluded_count" = exact(2L, "two rows excluded by identity"))),
list("Rows 0 and 5 (0-based) are excluded before the fit; HC1 covariance with 99% intervals."),
excluded = list(0L, 5L))
generators/r/regression_weight_estimation.R
source("common.R", local = TRUE)
# Weight estimation (Var(e_i) = sigma^2 z_i^p, power chosen by the Gaussian profile
# likelihood on a grid). Every oracle below re-reads the exported CSV and fits the
# weighted least squares at each grid power with stats::lm(weights = z^-p).
set.seed(11)
n <- 24L
z <- round(exp(seq(0, 2.2, length.out = n)) + (seq_len(n) %% 3) / 2, 4)
z <- z[sample(n)]
x1 <- round(seq(1, 8, length.out = n) + sin(seq_len(n)) / 3, 4)
x2 <- round(cos(0.7 * seq_len(n)) * 2 + (seq_len(n) %% 4) / 3, 4)
y <- round(2 + 0.8 * x1 - 1.1 * x2 + rnorm(n, sd = 0.3) * z, 4)
x2[20] <- NA
constructed <- data.frame(y = y, x1 = x1, x2 = x2, z = z)
export_dataset(
constructed, "REG_WEIGHT_01", "Constructed by regression_weight_estimation.R",
"R stats::lm help page (weighted least squares)",
"Constructed test data; no third-party dataset license.",
c(y = "Float64", x1 = "Float64", x2 = "Float64", z = "Float64")
)
d <- read_dataset("REG_WEIGHT_01")
run <- generator_run(c("stats", "jsonlite"))
tol <- function(x, label, tier = "1e-9")
list(comparison = "tolerance", value = dec(unname(x)), absolute_tolerance = "0",
relative_tolerance = tier,
rationale = paste("Independent R weighted least squares:", label))
prob <- function(x, label)
list(comparison = "tail_probability", value = dec(unname(x)), relative_tolerance = "1e-8",
rationale = paste("Independent R tail probability:", label))
exact <- function(x, label) list(comparison = "exact", value = x, rationale = label)
emit <- function(id, kind, config, outputs, notes, excluded = list()) {
write_expected("regression", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case("regression", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "regression",
entry_point = "tabelz.analysis.weight_estimation:compute_weight_estimation",
menu_items = list("weightEstimationMenuItem"), title = gsub("-", " ", id),
case_kind = kind,
dataset = list(path = "reference_data/datasets/REG_WEIGHT_01.json", sha256 = ""),
excluded_rows = excluded, configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/regression/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R stats::lm with weights = z^-p, stats::qt/pt/pf",
citation = "R Core Team, stats package help: lm, summary.lm, confint.",
generator = list(path = "generators/r/regression_weight_estimation.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes,
"Weights are z^(-p); for each grid power p the profile log-likelihood is -n/2 [ln(2 pi) + ln(WSSE/n) + 1] + 1/2 sum ln w_i and the largest value selects p (docs/manual/chapters/86-regression-and-count-models.tex:71-84).",
"The final fit is stats::lm(y ~ predictors, weights = z^-p_selected); s^2 = WSSE/(n-k); t intervals on n-k df; R-squared and F from weighted sums of squares about the weighted mean, as summary.lm reports for a weighted fit.",
"Rows with a missing value in any role are dropped (R na.omit); excluded rows are removed before anything is computed.",
"Expected computed from the exported CSV."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-REG-WEIGHT-ESTIMATION-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
profile <- function(data, predictors, powers, level) {
formula <- stats::as.formula(paste("y ~", paste(predictors, collapse = " + ")))
rows <- stats::complete.cases(data[c("y", predictors, "z")])
data <- data[rows, ]
n <- nrow(data)
grid <- lapply(powers, function(p) {
data$w <- data$z^(-p)
w <- data$w
fit <- stats::lm(formula, data = data, weights = w)
wsse <- sum(w * stats::residuals(fit)^2)
list(power = p, weighted_sse = wsse,
log_likelihood = -0.5 * n * (log(2 * pi) + log(wsse / n) + 1) + 0.5 * sum(log(w)))
})
ll <- vapply(grid, function(g) g$log_likelihood, 0)
best <- which.max(ll)
p <- powers[[best]]
data$w <- data$z^(-p)
w <- data$w
fit <- stats::lm(formula, data = data, weights = w)
s <- summary(fit)
k <- length(stats::coef(fit))
ci <- stats::confint(fit, level = level)
coefs <- lapply(seq_len(k), function(j) list(
estimate = s$coefficients[j, 1], standard_error = s$coefficients[j, 2],
t_value = s$coefficients[j, 3], p_value = s$coefficients[j, 4],
lower = ci[j, 1], upper = ci[j, 2]))
wsse <- sum(w * stats::residuals(fit)^2)
wmean <- sum(w * data$y) / sum(w)
wsst <- sum(w * (data$y - wmean)^2)
r2 <- 1 - wsse / wsst
stopifnot(abs(r2 - s$r.squared) < 1e-12)
f <- ((wsst - wsse) / (k - 1)) / (wsse / (n - k))
stopifnot(abs(f - s$fstatistic[["value"]]) < 1e-9 * f)
list(n = n, grid = grid, selected_power = p,
at_boundary = length(powers) > 1 && best %in% c(1L, length(powers)),
coefficients = coefs, r_squared = r2,
adjusted_r_squared = 1 - (1 - r2) * (n - 1) / (n - k),
standard_error = sqrt(wsse / (n - k)), f_value = f, df1 = k - 1L, df2 = n - k,
p_value = stats::pf(f, k - 1, n - k, lower.tail = FALSE))
}
outputs_for <- function(r, terms) {
out <- list("/n" = exact(as.integer(r$n), "complete rows"),
"/selected_power" = tol(r$selected_power, "selected power"),
"/at_boundary" = exact(r$at_boundary, "grid-edge flag"),
"/r_squared" = tol(r$r_squared, "weighted R-squared"),
"/adjusted_r_squared" = tol(r$adjusted_r_squared, "adjusted weighted R-squared"),
"/standard_error" = tol(r$standard_error, "root weighted residual variance"),
"/f_value" = tol(r$f_value, "weighted F"),
"/df1" = exact(as.integer(r$df1), "numerator df"),
"/df2" = exact(as.integer(r$df2), "denominator df"),
"/p_value" = prob(r$p_value, "weighted F tail"))
for (i in seq_along(r$grid)) {
out[[sprintf("/grid/%d/log_likelihood", i - 1L)]] <- tol(r$grid[[i]]$log_likelihood,
sprintf("profile log-likelihood at power %g", r$grid[[i]]$power))
out[[sprintf("/grid/%d/weighted_sse", i - 1L)]] <- tol(r$grid[[i]]$weighted_sse,
sprintf("weighted SSE at power %g", r$grid[[i]]$power))
}
for (j in seq_along(r$coefficients)) {
c <- r$coefficients[[j]]
base <- sprintf("/coefficients/%d/", j - 1L)
out[[paste0(base, "term")]] <- exact(terms[[j]], "coefficient label")
out[[paste0(base, "estimate")]] <- tol(c$estimate, "WLS estimate")
out[[paste0(base, "standard_error")]] <- tol(c$standard_error, "WLS standard error")
out[[paste0(base, "t_value")]] <- tol(c$t_value, "t statistic")
out[[paste0(base, "p_value")]] <- prob(c$p_value, "two-sided t tail")
out[[paste0(base, "lower")]] <- tol(c$lower, "confidence limit")
out[[paste0(base, "upper")]] <- tol(c$upper, "confidence limit")
}
out
}
grid_of <- function(from, to, step) round(from + step * seq(0, floor((to - from) / step + 1e-9)), 10)
# 001: the default grid, two predictors, 95% intervals.
r <- profile(d, c("x1", "x2"), grid_of(-2, 2, 0.5), 0.95)
emit("STAT-REG-WEIGHT-ESTIMATION-001", "nominal",
list(response = "y", predictors = list("x1", "x2"), weight_variable = "z",
power_from = -2, power_to = 2, power_step = 0.5, confidence_level = 0.95),
c(outputs_for(r, c("(Intercept)", "x1", "x2")),
list("/missing_count" = exact(1L, "one row has a missing x2"),
"/excluded_count" = exact(0L, "no excluded rows"))),
list("Default grid -2 to 2 by 0.5; the interior maximum is checked by R at every grid point."))
# 002: a finer, narrower grid with 90% intervals and one predictor.
r <- profile(d, "x1", grid_of(-1, 2, 0.25), 0.90)
emit("STAT-REG-WEIGHT-ESTIMATION-002", "axis_coverage",
list(response = "y", predictors = list("x1"), weight_variable = "z",
power_from = -1, power_to = 2, power_step = 0.25, confidence_level = 0.90),
outputs_for(r, c("(Intercept)", "x1")),
list("Grid -1 to 2 by 0.25 with 90% t intervals; one predictor."))
# 003: a grid whose maximum lies on its edge is reported as a constrained-grid result.
r <- profile(d, c("x1", "x2"), grid_of(-2, 0, 1), 0.95)
stopifnot(isTRUE(r$at_boundary))
emit("STAT-REG-WEIGHT-ESTIMATION-003", "axis_coverage",
list(response = "y", predictors = list("x1", "x2"), weight_variable = "z",
power_from = -2, power_to = 0, power_step = 1, confidence_level = 0.95),
outputs_for(r, c("(Intercept)", "x1", "x2")),
list("Grid -2 to 0 by 1: the likelihood keeps rising to the upper edge, so at_boundary is true and the edge power is used for the final fit."))
# 004: excluded rows (0-based indexes 2 and 7) leave the analysed population before fitting.
r <- profile(d[-c(3, 8), ], c("x1", "x2"), grid_of(-1, 2, 0.5), 0.99)
emit("STAT-REG-WEIGHT-ESTIMATION-004", "axis_coverage",
list(response = "y", predictors = list("x1", "x2"), weight_variable = "z",
power_from = -1, power_to = 2, power_step = 0.5, confidence_level = 0.99),
c(outputs_for(r, c("(Intercept)", "x1", "x2")),
list("/missing_count" = exact(1L, "one row has a missing x2"),
"/excluded_count" = exact(2L, "two rows excluded by identity"))),
list("Rows 2 and 7 (0-based) are excluded; the grid -1 to 2 by 0.5 and 99% intervals."),
excluded = list(2L, 7L))
generators/r/smoke.R
source("common.R", local = TRUE)
data("sleep", package = "datasets")
citation <- paste(
"Cushny AR, Peebles AR (1905). The Action of Optical Isomers: II. Hyoscines.",
"The Journal of Physiology 32(5-6), 501-510. doi:10.1113/jphysiol.1905.sp001097"
)
dataset <- data.frame(extra = as.double(sleep$extra), group = as.character(sleep$group))
export_dataset(
dataset, "SLEEP", "R 4.6.1 datasets::sleep", citation,
"Published data distributed with R; retain the citation.",
c(extra = "Float64", group = "String")
)
reloaded <- read_dataset("SLEEP")
group_one <- reloaded$extra[reloaded$group == "1"]
reference <- stats::t.test(group_one, mu = 0, alternative = "two.sided", conf.level = 0.95)
case_id <- "STAT-HYP-ONESAMPLE-T-001"
run <- generator_run(c("stats", "jsonlite"))
tolerance <- function(value, label) {
list(
comparison = "tolerance", value = dec(value),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R stats::t.test closed-form", label, "on re-read SLEEP CSV.")
)
}
probability <- list(
comparison = "tail_probability", value = dec(reference$p.value),
relative_tolerance = "1e-9",
rationale = "R stats::t.test two-sided Student t tail on re-read SLEEP CSV."
)
expected <- list(
schema_version = "2.0.0", case_id = case_id,
outputs = list(
"/statistic" = tolerance(unname(reference$statistic), "statistic"),
"/df" = tolerance(unname(reference$parameter), "degrees of freedom"),
"/p_value" = probability,
"/ci_lower" = tolerance(reference$conf.int[[1]], "95% CI lower"),
"/ci_upper" = tolerance(reference$conf.int[[2]], "95% CI upper")
),
expected_error = NULL, deferred_outputs = list(), generator_run = run
)
write_expected("hypothesis", case_id, expected)
case <- list(
schema_version = "2.0.0", case_id = case_id, version = "1.0.0",
family = "hypothesis",
entry_point = "tabelz.analysis.hypothesis_tests:run_hypothesis_test",
menu_items = list("hypothesisTestsMenuItem"),
title = "R sleep group 1 one-sample t-test against zero",
case_kind = "nominal",
dataset = list(path = "reference_data/datasets/SLEEP.json", sha256 = ""),
excluded_rows = as.list(10:19),
configuration = list(test = "one_sample_t", response = "extra", target = 0.0),
"repeat" = 1L,
expected = list(path = paste0("reference_data/expected/hypothesis/", case_id, ".json"),
sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R 4.6.1 stats::t.test", citation = citation,
generator = list(path = "generators/r/smoke.R", sha256 = ""),
independent = TRUE,
convention_notes = list(
"Use only group 1's extra; exclude zero-based dataset rows 10 through 19.",
"Two-sided Student one-sample t-test, mu=0, 95% confidence interval.",
"Expected values computed from the exported CSV, not R's in-memory sleep object."
)
),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-HYP-001"),
risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
)
write_case("hypothesis", case_id, case)
cat("smoke case generated: ", case_id, "\n", sep = "")
generators/r/survival_boundaries.R
source("common.R",local=TRUE)
base <- read_dataset("SURV_MIN")
censored <- base; censored$status[] <- 0L
export_dataset(censored,"SURV_ALL_CENS","Five right-censored observations with no failures",
"R survival::survfit all-censored boundary",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64",response="Float64"))
censored <- read_dataset("SURV_ALL_CENS")
one <- base; one$status <- c(1L,0L,0L,0L,0L)
export_dataset(one,"SURV_ONE_FAIL","Five observations with only one failure",
"R survival::Surv failure-count boundary",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64",response="Float64"))
constant <- base; constant$stress[] <- 40
export_dataset(constant,"SURV_CONST_STRESS","Five observations at a constant stress",
"R survival::survreg identifiability condition",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64",response="Float64"))
large <- read_dataset("SURV_CORE")
large$time <- as.double(round(large$time))
export_dataset(large,"SURV_TIES","Constructed tied event and censoring times",
"R survival::coxph Efron tie-handling reference",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64"))
ties <- read_dataset("SURV_TIES")
interval <- read_dataset("SURV_INTERVAL")
interval$upper[4] <- interval$lower[4]+1e-8
export_dataset(interval,"SURV_NARROW","Mixed censoring with a narrow nonzero interval",
"R survival::survreg interval2 likelihood reference",
"Constructed validation data; no third-party license.",
c(lower="Float64",upper="Float64"))
interval <- read_dataset("SURV_NARROW")
run <- generator_run(c("survival","stats","jsonlite"))
tol <- function(x,note,tier="1e-6") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=note)
exact <- function(x,note) list(comparison="exact",value=x,rationale=note)
empty_object <- structure(list(),names=character())
emit <- function(id,entry,menu,dataset,config,outputs,notes,error=NULL) {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=if(is.null(error)) outputs else empty_object,
expected_error=error,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Survival boundary",id),
case_kind="boundary",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R survival and analytic boundary",
citation="R survival::survfit/coxph/survreg documentation",
generator=list(path="generators/r/survival_boundaries.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
cfg <- list(time_column="time",event_column="status",event_value="1",censored_value="0")
emit("STAT-SURV-KM-ALL-CENS-001","tabelz.executor.survival","survivalMenuItem",
"SURV_ALL_CENS",cfg,
list("/curves/0/survival/0"=tol(1,"R all-censored KM remains at one","1e-9"),
"/curves/0/inference_available"=exact(FALSE,"No event means no Greenwood inference")),
c("R survfit(Surv(time,status)~1) stays at survival 1 when all rows are censored."))
emit("STAT-SURV-RISK-ALL-CENS-001","tabelz.analysis.survival_nonparametric:number_at_risk",
"survivalMenuItem","SURV_ALL_CENS",c(cfg,list(times=list(1,5,6))),
list("/0"=exact(5L,"Five at risk at first time"),
"/1"=exact(1L,"One censored row still observed at last time"),
"/2"=exact(0L,"None beyond last time")),
c("Number at risk counts censoring rows through their recorded time."))
cox <- survival::coxph(survival::Surv(time,status)~x,data=ties,ties="efron")
emit("STAT-SURV-COX-TIES-001","tabelz.executor.cox_regression","coxRegressionMenuItem",
"SURV_TIES",list(duration_column="time",event_column="status",event_value="1",
censored_value="0",predictors=list("x")),
list("/coefficients/0/log_hazard"=tol(coef(cox)[[1]],"R Efron tied-event log hazard"),
"/event_count"=exact(as.integer(sum(ties$status)),"All failures retained")),
c("Integer-rounded durations create exact ties; R coxph ties=efron matches manual."))
emit("STAT-SURV-FAMILIES-ONE-FAIL-001","tabelz.executor.reliability",
"reliabilityFamiliesMenuItem","SURV_ONE_FAIL",
list(mode="families",time_column="time",event_column="status"),list(),
c("Censored life families require at least two failures for parameter estimation."),
error=list(error_class="ReliabilityExecutionError",message_contains="At least two observed failures"))
emit("STAT-SURV-ALT-CONSTANT-001","tabelz.executor.reliability",
"acceleratedLifeMenuItem","SURV_CONST_STRESS",
list(mode="accelerated",time_column="time",event_column="status",stress_column="stress",
stress_type="linear",stress_unit="native",use_stress=40,high_stress=60),list(),
c("Constant stress cannot identify an accelerated-life stress slope."),
error=list(error_class="ReliabilityExecutionError",message_contains="Varying stress levels"))
emit("STAT-SURV-WEIBULL-CONSTANT-001","tabelz.analysis.weibull_aft:compute_weibull_aft",
"weibullAftMenuItem","SURV_CONST_STRESS",
list(time_column="time",event_column="status",stress_column="stress"),list(),
c("Constant stress makes the Weibull AFT stress coefficient unidentified."),
error=list(error_class="ValueError",message_contains="stress covariate is constant"))
# survreg forms exp(-L/s) - exp(-U/s) from the two endpoints, which loses about
# eight digits of a 1e-8 interval and moves its optimum by ~4e-6. Solve the
# exponential score equation instead, with each interval term written through
# its exact width: d/ds log(e^{-L/s} - e^{-U/s}) = (L - U e^{-w/s}) / (s^2 (1 - e^{-w/s})),
# w = U - L, rewritten as L/s^2 + w e^{-w/s} / (s^2 expm1(-w/s)).
exp_score <- function(s) {
lo <- interval$lower; up <- interval$upper
exact <- !is.na(lo) & !is.na(up) & lo == up
right <- !is.na(lo) & is.na(up)
left <- (is.na(lo) | lo == 0) & !is.na(up)
width <- !is.na(lo) & !is.na(up) & lo > 0 & lo < up
w <- up[width] - lo[width]
sum(-1 / s + lo[exact] / s^2) + sum(lo[right] / s^2) +
sum(-up[left] * exp(-up[left] / s) / (s^2 * (-expm1(-up[left] / s)))) +
sum(lo[width] / s^2 - w * exp(-w / s) / (s^2 * (-expm1(-w / s))))
}
narrow_scale <- stats::uniroot(exp_score, c(1, 20), tol = 1e-15)$root
emit("STAT-SURV-INTERVAL-NARROW-001","tabelz.executor.reliability",
"intervalLifeMenuItem","SURV_NARROW",
list(mode="interval",lower_column="lower",upper_column="upper"),
list("/interval_count"=exact(5L,"Narrow positive interval remains interval-censored"),
"/fits/[family=exponential]/param1_val"=tol(narrow_scale,
"Exponential MLE from the width-first score equation (uniroot)")),
c("A 1e-8 interval width is not rounded to an exact failure."))
emit("STAT-SURV-STABILITY-OUTSIDE-001",
"tabelz.analysis.product_stability:compute_product_stability",
"productStabilityMenuItem","SURV_STABILITY",
list(time_column="time",response_column="response",lsl=1000),
list("/overall_shelf_life_status"=exact("out_of_specification_at_zero",
"One-sided lower mean bound is already below LSL=1000 at time zero")),
c("R pooled lm intercept is near 100, so its one-sided lower 95% bound is certainly below 1000."))
cat("survival boundary references generated\n")
generators/r/survival_core.R
source("common.R",local=TRUE)
run <- generator_run(c("stats","survival","MASS","jsonlite"))
tol <- function(x,note,tier="1e-9")
list(comparison="tolerance",value=dec(unname(x)),absolute_tolerance="0",
relative_tolerance=tier,rationale=paste("R survival reference:",note))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
seqexact <- function(x,note) list(comparison="sequence",values=as.list(as.integer(x)),rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,notes,kind="nominal") {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Survival",id),
case_kind=kind,dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R survival/stats",
citation="R survival::survfit/survdiff/coxph and stats::lm documentation",
generator=list(path="generators/r/survival_core.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the referenced CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
mp <- read_dataset("HARVEST_GEHAN_MP")
both <- read_dataset("HARVEST_GEHAN_BOTH")
stopifnot(nrow(mp)==21L,nrow(both)==42L)
km <- survival::survfit(survival::Surv(time,status)~1,data=mp,conf.type="log-log")
config <- list(time_column="time",event_column="status",event_value="1",censored_value="0")
emit("STAT-SURV-KM-001","tabelz.executor.survival","survivalMenuItem",
"HARVEST_GEHAN_MP",config,
list("/curves/0/n"=exact(nrow(mp),"Gehan 6-MP row count"),
"/curves/0/events"=exact(sum(mp$status),"Gehan 6-MP failures"),
"/curves/0/survival/0"=tol(km$surv[1],"Kaplan-Meier first failure survival"),
"/curves/0/lower/0"=tol(km$lower[1],"Greenwood log-log lower pointwise limit"),
"/curves/0/upper/0"=tol(km$upper[1],"Greenwood log-log upper pointwise limit")),
c("R survfit conf.type=log-log matches Tabelz; this independently checks V14 Gehan values."))
ticks <- c(6,7,10,23)
risk <- vapply(ticks,function(t) sum(mp$time>=t),0L)
emit("STAT-SURV-RISK-001","tabelz.analysis.survival_nonparametric:number_at_risk",
"survivalMenuItem","HARVEST_GEHAN_MP",c(config,list(times=as.list(ticks))),
list("/0"=exact(risk[1],"At risk at time 6"),
"/1"=exact(risk[2],"At risk at time 7"),
"/2"=exact(risk[3],"At risk at time 10"),
"/3"=exact(risk[4],"At risk at time 23")),
c("number_at_risk is an in-process payload step after an engine survival fit; ties remain at risk at t."))
group_fit <- survival::survdiff(survival::Surv(time,status)~arm,data=both)
emit("STAT-SURV-LOGRANK-001","tabelz.executor.survival","survivalMenuItem",
"HARVEST_GEHAN_BOTH",c(config,list(group_column="arm")),
list("/log_rank/statistic"=tol(group_fit$chisq,"R two-group log-rank chi-square"),
"/log_rank/observed"=seqexact(group_fit$obs,"R observed failures by arm")),
c("R survdiff rho=0 gives the log-rank statistic; checks V14 Gehan grouping."))
i <- seq_len(32L)
raw <- data.frame(time=exp(2.2+0.025*i+0.18*sin(i*1.17)-0.31*cos(i*0.41)),
status=as.integer(i%%5L!=0L),
stress=as.double(rep(c(20,40,60,80),each=8L)),
x=as.double(sin(i*0.39)+0.1*cos(i*1.21)))
export_dataset(raw,"SURV_CORE","Constructed censored duration and stress population",
"R survival::coxph/survreg documentation",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64"))
d <- read_dataset("SURV_CORE")
cox <- survival::coxph(survival::Surv(time,status)~x,data=d,ties="efron",x=TRUE,
control=survival::coxph.control(eps=1e-12,iter.max=200L))
emit("STAT-SURV-COX-001","tabelz.executor.cox_regression","coxRegressionMenuItem",
"SURV_CORE",list(duration_column="time",event_column="status",event_value="1",
censored_value="0",predictors=list("x")),
list("/coefficients/0/log_hazard"=tol(coef(cox)[[1]],"Efron-tie log hazard", "1e-6"),
"/coefficients/0/hazard_ratio"=tol(exp(coef(cox)[[1]]),"Exponentiated Efron hazard ratio","1e-6"),
"/event_count"=exact(sum(d$status),"Observed failures")),
c("R coxph uses Efron ties, eps=1e-12 and iter.max=200; independent subjects."))
j <- seq_len(30L)
stability <- data.frame(time=as.double(rep(0:9,3L)),
response=100-0.37*rep(0:9,3L)+0.08*sin(j*1.31),
batch=rep(c("A","B","C"),each=10L))
export_dataset(stability,"SURV_STABILITY","Constructed linear shelf-life measurements",
"R stats::lm pooled regression",
"Constructed validation data; no third-party license.",
c(time="Float64",response="Float64",batch="String"))
s <- read_dataset("SURV_STABILITY")
lmfit <- stats::lm(response~time,data=s)
emit("STAT-SURV-STABILITY-001","tabelz.analysis.product_stability:compute_product_stability",
"productStabilityMenuItem","SURV_STABILITY",
list(time_column="time",response_column="response",model_type="pooled",lsl=95),
list("/coefficients/0/estimate"=tol(coef(lmfit)[[1]],"Pooled OLS intercept"),
"/coefficients/1/estimate"=tol(coef(lmfit)[[2]],"Pooled OLS time slope"),
"/n"=exact(nrow(s),"All complete shelf-life measurements")),
c("Pooled OLS uses all batches without batch terms; lower spec limit is 95 response units."))
cat("survival core references generated\n")
generators/r/survival_corrections.R
source("common.R",local=TRUE)
d <- read_dataset("SURV_CORE")
run <- generator_run(c("survival","stats","jsonlite"))
tol <- function(x,note) list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance="1e-6",rationale=note)
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
emit <- function(id,entry,menu,config,outputs,notes) {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Survival corrected option",id),
case_kind="nominal",dataset=list(path="reference_data/datasets/SURV_CORE.json",sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R survival",
citation="R survival::survreg and coxph documentation",
generator=list(path="generators/r/survival_corrections.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
x <- log(d$stress)
fit <- survival::survreg(survival::Surv(d$time,d$status)~x,dist="weibull")
median50 <- exp(coef(fit)[[1]]+coef(fit)[[2]]*log(50))*(log(2))^fit$scale
emit("STAT-SURV-ALT-INVERSE-001","tabelz.executor.reliability",
"acceleratedLifeMenuItem",
list(mode="accelerated",time_column="time",event_column="status",stress_column="stress",
stress_type="inverse_power",stress_unit="native",use_stress=50,high_stress=80,
confidence_level=0.9),
list("/fits/[family=weibull]/median_use"=tol(median50,"R inverse-power use-stress Weibull median")),
c("R stress covariate is log(S); Weibull survreg scale=1/shape.",
"Median=exp(beta0+beta1*log(50))*(log 2)^(survreg scale)."))
cox <- survival::coxph(survival::Surv(time,status)~x,data=d,ties="efron",
control=survival::coxph.control(iter.max=300L))
emit("STAT-SURV-COX-OPTIONS-001","tabelz.executor.cox_regression",
"coxRegressionMenuItem",
list(duration_column="time",event_column="status",event_value="1",censored_value="0",
predictors=list("x"),confidence_level=0.95,max_iterations=300L,max_diagnostic_rows=100L),
list("/coefficients/0/log_hazard"=tol(coef(cox)[[1]],"R Efron Cox log hazard"),
"/analyzed_count"=exact(nrow(d),"All finite rows")),
c("Cox intervals are fixed at 95% by the manual; max_iterations and diagnostic bound are nondefaults."))
cat("corrected survival references generated\n")
generators/r/survival_cox_time_dependent.R
source("common.R", local = TRUE)
library(survival)
# Cox regression with time-dependent covariates (Statistics > Survival > Cox regression with
# time-dependent covariates): survival::coxph on counting-process episodes (start, stop,
# status), with Efron or Breslow ties, the subject-clustered robust variance, strata and the
# tt() time functions. docs/manual/chapters/93-grouped-and-changing-survival.tex:62-104.
episodes <- data.frame(
id = c(1L, 1L, 2L, 3L, 3L, 3L, 4L, 4L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 10L, 10L, 11L, 11L,
12L, 13L, 13L, 13L, 14L, 15L, 15L, 16L),
start = c(0, 3, 0, 0, 2, 6, 0, 4, 0, 0, 3, 0, 5, 0, 6, 0, 0, 2, 0, 5, 0, 0, 3, 6, 0, 0, 4, 0),
stop = c(3, 7, 5, 2, 6, 9, 4, 8, 7, 3, 9, 4, 9, 6, 11, 10, 2, 5, 5, 12, 8, 3, 6, 12, 7, 4, 11, 9),
status = c(0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L,
1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L),
x = c(1.2, 1.8, 0.4, 2.1, 2.5, 1.9, 0.9, 1.1, 1.5, 0.2, 0.6, 1.7, 2.2, 1.0, 1.3, 0.7, 2.4, 2.9,
1.1, 0.8, 1.6, 0.5, 0.9, 1.4, 2.0, 1.3, 1.0, 0.3),
trt = c("A", "A", "B", "A", "A", "A", "B", "B", "A", "B", "B", "A", "A", "B", "B", "A", "B", "B",
"A", "A", "B", "A", "A", "A", "B", "A", "A", "B"),
sex = c("F", "F", "M", "M", "M", "M", "F", "F", "F", "M", "M", "M", "M", "F", "F", "F", "M", "M",
"M", "M", "F", "F", "F", "F", "M", "M", "M", "F"),
stringsAsFactors = FALSE
)
export_dataset(
episodes, "SURV_COXTD_EPISODES", "Constructed by survival_cox_time_dependent.R",
"Therneau and Grambsch (2000) Modeling Survival Data, ch. 3 and 6; R survival::coxph documentation.",
"Constructed test data; no third-party dataset license.",
c(id = "Int64", start = "Float64", stop = "Float64", status = "Int64", x = "Float64",
trt = "String", sex = "String")
)
one_row <- data.frame(
time = c(7, 5, 9, 8, 7, 9, 9, 11, 10, 5, 12, 8, 12, 7, 11, 9),
status = c(1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L),
x = c(1.5, 0.4, 2.2, 1.0, 1.5, 0.4, 2.0, 1.2, 0.7, 2.7, 0.9, 1.6, 1.0, 2.0, 1.1, 0.3),
trt = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B"),
stringsAsFactors = FALSE
)
export_dataset(
one_row, "SURV_COXTD_ONE", "Constructed by survival_cox_time_dependent.R",
"Therneau, Crowson and Atkinson, Using time dependent covariates and time dependent coefficients in the Cox model (survival package vignette).",
"Constructed test data; no third-party dataset license.",
c(time = "Float64", status = "Int64", x = "Float64", trt = "String")
)
d <- read_dataset("SURV_COXTD_EPISODES")
d1 <- read_dataset("SURV_COXTD_ONE")
run <- generator_run(c("stats", "survival", "jsonlite"))
source_ref <- "R survival::coxph (counting process, cluster, strata, tt) and cox.zph"
citation <- "R survival package documentation: coxph, coxph.control, cox.zph; Therneau and Grambsch (2000)."
control <- survival::coxph.control(eps = 1e-10, iter.max = 100)
tol <- function(value, label, tier = "1e-6") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste("R survival::coxph", label, "from the re-read CSV; both fits are Newton iterations (coxph eps = 1e-10)."))
}
prob <- function(value, label, tier = "1e-5") {
list(comparison = "tail_probability", value = dec(unname(value)), relative_tolerance = tier,
rationale = paste("R survival::coxph", label, "from the re-read CSV; iterative fit."))
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
emit <- function(id, title, kind, dataset, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("survival", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "survival",
entry_point = "tabelz.analysis.cox_time_dependent:compute_cox_time_dependent",
menu_items = list("coxTimeDependentMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/survival/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/survival_cox_time_dependent.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes, "Expected computed from the exported CSV.",
"docs/manual/chapters/93-grouped-and-changing-survival.tex:64-67: h(t) = h_0s(t) exp(z(t)'beta) on (start, stop] episodes; an event at t uses the covariates in force just before t.",
"docs/manual/chapters/93-grouped-and-changing-survival.tex:87-96: Efron or Breslow ties; partial likelihood; model-based covariance from the information matrix, robust covariance over whole subjects; Wald intervals on the log scale.",
"docs/manual/chapters/93-grouped-and-changing-survival.tex:97-101: the proportional-hazards score diagnostic with the KM transform; withheld for a model with a declared time interaction.",
"Relative tolerance 1e-6 (1e-5 on tail probabilities): both implementations maximize the partial likelihood iteratively."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-SURV-COX_TIME_DEPENDENT-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
)
write_case("survival", id, case)
}
coefficient_outputs <- function(fit, level, robust = FALSE, names = NULL) {
beta <- stats::coef(fit)
se <- if (robust) sqrt(diag(fit$var)) else sqrt(diag(fit$var))
naive <- if (robust) sqrt(diag(fit$naive.var)) else se
crit <- stats::qnorm((1 + level) / 2)
out <- list()
for (j in seq_along(beta)) {
at <- paste0("/coefficients/", j - 1)
label <- if (is.null(names)) names(beta)[j] else names[j]
out[[paste0(at, "/coefficient")]] <- tol(beta[j], paste("coefficient of", label))
out[[paste0(at, "/standard_error")]] <- tol(naive[j], paste("model-based SE of", label))
if (robust) out[[paste0(at, "/robust_standard_error")]] <- tol(se[j], paste("robust (subject-clustered) SE of", label))
out[[paste0(at, "/z")]] <- tol(beta[j] / se[j], paste("z of", label))
out[[paste0(at, "/p_value")]] <- prob(2 * stats::pnorm(-abs(beta[j] / se[j])), paste("two-sided normal p of", label))
out[[paste0(at, "/hazard_ratio")]] <- tol(exp(beta[j]), paste("hazard ratio of", label))
out[[paste0(at, "/lower")]] <- tol(exp(beta[j] - crit * se[j]), paste("hazard ratio lower limit of", label))
out[[paste0(at, "/upper")]] <- tol(exp(beta[j] + crit * se[j]), paste("hazard ratio upper limit of", label))
}
lr <- 2 * (fit$loglik[2] - fit$loglik[1])
out[["/tests/0/chisq"]] <- tol(lr, "likelihood-ratio chi-square")
out[["/tests/0/df"]] <- exact(length(beta), "model df")
out[["/tests/0/p_value"]] <- prob(stats::pchisq(lr, length(beta), lower.tail = FALSE), "likelihood-ratio p")
out[["/tests/1/chisq"]] <- tol(unname(fit$wald.test), if (robust) "robust Wald chi-square" else "Wald chi-square")
out[["/log_likelihood"]] <- tol(fit$loglik[2], "partial log-likelihood at the estimate")
out[["/null_log_likelihood"]] <- tol(fit$loglik[1], "partial log-likelihood at beta = 0")
out
}
zph_outputs <- function(fit) {
z <- survival::cox.zph(fit, transform = "km", terms = TRUE, global = TRUE)$table
out <- list("/ph_test/status" = exact("computed", "Grambsch-Therneau test computed"))
for (i in seq_len(nrow(z))) {
at <- paste0("/ph_test/rows/", i - 1)
out[[paste0(at, "/chisq")]] <- tol(z[i, "chisq"], paste("cox.zph KM-transform chi-square for", rownames(z)[i]))
out[[paste0(at, "/df")]] <- exact(as.integer(z[i, "df"]), paste("cox.zph df for", rownames(z)[i]))
out[[paste0(at, "/p_value")]] <- prob(z[i, "p"], paste("cox.zph p for", rownames(z)[i]))
}
out
}
counts <- function(data) {
list("/subjects" = exact(length(unique(data$id)), "distinct subjects"),
"/episodes" = exact(nrow(data), "episodes"),
"/events" = exact(as.integer(sum(data$status)), "events"),
"/gaps" = exact(1L, "subject 7 has a gap between stop 4 and start 5"))
}
episode_config <- list(layout = "counting_process", subject = "id", start = "start", stop = "stop",
status = "status", event_value = "1", censored_value = "0",
numeric_covariates = list("x"), categorical_covariates = list("trt"))
# --- case 001: Efron ties, model-based variance ------------------------------------------
fit <- survival::coxph(survival::Surv(start, stop, status) ~ x + trt, data = d, ties = "efron", control = control)
emit("STAT-SURV-COXTD-001", "Counting-process Cox model with Efron ties", "nominal",
"SURV_COXTD_EPISODES", episode_config,
c(coefficient_outputs(fit, 0.95), zph_outputs(fit), counts(d)),
list("coxph(Surv(start, stop, status) ~ x + trt, ties = 'efron'); terms in order x, trt = B (vs A).",
"cox.zph(transform = 'km', terms = TRUE): rows x, trt, GLOBAL."))
# --- case 002: Breslow ties, robust subject-clustered variance ---------------------------
fit2 <- survival::coxph(survival::Surv(start, stop, status) ~ x + trt, data = d, ties = "breslow",
cluster = id, control = control)
emit("STAT-SURV-COXTD-002", "Breslow ties with the robust sandwich variance", "axis_coverage",
"SURV_COXTD_EPISODES", c(episode_config, list(ties = "breslow", robust = TRUE)),
c(coefficient_outputs(fit2, 0.95, robust = TRUE), counts(d)),
list("coxph(..., ties = 'breslow', cluster = id): fit$var is the robust sandwich with the subject as the cluster, fit$naive.var the model-based covariance; z, p, the interval and the Wald test use the robust SE."))
# --- case 003: strata by sex, 90% intervals ----------------------------------------------
fit3 <- survival::coxph(survival::Surv(start, stop, status) ~ x + trt + survival::strata(sex), data = d,
ties = "efron", control = control)
emit("STAT-SURV-COXTD-003", "Stratified baseline hazards by sex with 90% intervals", "axis_coverage",
"SURV_COXTD_EPISODES", c(episode_config, list(strata = "sex", confidence_level = 0.90)),
c(coefficient_outputs(fit3, 0.90), zph_outputs(fit3), counts(d),
list("/strata_levels/0" = exact("F", "strata levels sorted naturally"),
"/strata_levels/1" = exact("M", "strata levels sorted naturally"))),
list("coxph(... + strata(sex)): separate baseline hazards, common coefficients; 90% Wald limits on the log scale."))
# --- case 004: one row per subject, x times t ---------------------------------------------
fit4 <- survival::coxph(survival::Surv(time, status) ~ x + trt + tt(x), data = d1, ties = "efron",
tt = function(x, t, ...) x * t, control = control)
one_config <- list(layout = "one_row", stop = "time", status = "status", event_value = "1",
censored_value = "0", numeric_covariates = list("x"),
categorical_covariates = list("trt"))
cuts <- length(unique(d1$time[d1$status == 1L]))
emit("STAT-SURV-COXTD-004", "One row per subject with the covariate-by-time term x t", "axis_coverage",
"SURV_COXTD_ONE", c(one_config, list(time_function = "time", time_covariates = list("x"))),
c(coefficient_outputs(fit4, 0.95, names = c("x", "trt = B (vs A)", "x * t")),
list("/ph_test/status" = exact("withheld", "the model already has a declared time interaction"),
"/split/cut_count" = exact(cuts, "distinct event times"),
"/split/function" = exact("time", "g(t) = t"),
"/subjects" = exact(nrow(d1), "16 subjects"),
"/events" = exact(as.integer(sum(d1$status)), "events"))),
list("coxph(Surv(time, status) ~ x + trt + tt(x), tt = function(x, t, ...) x * t): survival expands the data at each event time, the same expansion the product performs; terms x, trt = B (vs A), x x t."))
# --- case 005: one row per subject, step at t > 8 ------------------------------------------
fit5 <- survival::coxph(survival::Surv(time, status) ~ x + trt + tt(x), data = d1, ties = "efron",
tt = function(x, t, ...) x * (t > 8), control = control)
emit("STAT-SURV-COXTD-005", "One row per subject with the step function x 1{t > 8}", "axis_coverage",
"SURV_COXTD_ONE", c(one_config, list(time_function = "step", time_covariates = list("x"), step_time = 8)),
c(coefficient_outputs(fit5, 0.95, names = c("x", "trt = B (vs A)", "x * 1{t > 8}")),
list("/ph_test/status" = exact("withheld", "the model already has a declared time interaction"),
"/split/cut_count" = exact(1L, "one cut at the step time"))),
list("coxph(..., tt = function(x, t, ...) x * (t > 8)): the step is in force strictly after 8, so an event at exactly 8 uses the pre-step value; the product splits each episode once at the step time."))
cat("cox time-dependent cases generated\n")
generators/r/survival_degradation.R
source("common.R", local = TRUE)
# Degradation-based lifetime references (B9): repeated-measurement degradation data
# with a temperature axis. Oracle: the classical pseudo-failure analysis. With a
# very small measurement error (data noise SD 0.001 against a log-rate spread of
# about 0.3), the engine's direct marginal maximum likelihood equals, up to
# O(sigma_e^2 / (tau^2 S_tt r^2)) ~ 1e-8, the two-stage analysis: per-unit
# least-squares rates r_ls,i = sum(t z) / sum(t^2), pseudo-failure times
# t~_i = D / r_ls,i, then survreg(Surv(t~) ~ 1 or ~ x, dist = "lognormal").
# The engine's repeated-measurement mode estimates sigma_e from the data (the
# configuration contract refuses a supplied SD in this mode), so the small
# measurement error lives in the data and the ML sigma_e is referenced by its
# pseudo-failure limit sqrt(RSS / (N - g)). Every reference reads the exported
# CSV back before computing.
suppressMessages(library(survival))
run <- generator_run(c("stats", "survival", "jsonlite"))
tol <- function(value, why, rel = "1e-6", atol = "1e-7") list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = atol, relative_tolerance = rel,
rationale = paste("R reference:", why)
)
strict <- function(value, why) tol(value, why, rel = "1e-9", atol = "0")
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
MANUAL <- "docs/manual/chapters/96-repairable-degradation-warranty.tex"
CITATION <- paste0("Meeker, Hahn and Escobar (1998) Statistical Methods for Reliability Data, ",
"chapter 21 (pseudo-failure limits); Therneau T (2024), A Package for ",
"Survival Analysis in R.")
emit <- function(id, title, configuration, outputs, notes, kind = "nominal") {
entry <- "tabelz.analysis.degradation_reliability:compute_degradation_reliability"
write_expected("survival", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("survival", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "survival", entry_point = entry, menu_items = list("degradationMenuItem"),
title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/SURV_DEG_B9.json", sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/survival/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation",
source = "R survival::survreg lognormal pseudo-failure fits and base R delta-method algebra",
citation = CITATION,
generator = list(path = "generators/r/survival_degradation.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.")
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-SURV-DEGRADATION-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
# =======================================================================================
# Dataset: 10 units, four readings each at times 4, 8, 12, 16; baseline 0, threshold 25,
# direction increasing, so D = 25. True log rates are log(1.25) + 4000 (1/348 - 1/T_i)
# + N(0, 0.3^2); five units were tested at 338 K and five at 358 K; measurement noise
# SD is exactly 0.001, far below the log-rate spread, which is the pseudo-failure limit.
set.seed(96041)
units <- sprintf("u%02d", 1:10)
temp_of_unit <- c(rep(338, 5), rep(358, 5))
log_rate <- log(1.25) + 4000 * (1 / 348 - 1 / temp_of_unit) + rnorm(10, 0, 0.3)
times <- c(4, 8, 12, 16)
response <- unlist(lapply(seq_along(units), function(i) {
exp(log_rate[i]) * times + rnorm(4, 0, 0.001)
}))
deg <- data.frame(
unit = rep(units, each = 4),
temp_k = rep(temp_of_unit, each = 4),
time = rep(times, 10),
response = response,
stringsAsFactors = FALSE
)
export_dataset(deg, "SURV_DEG_B9",
"Constructed repeated-measurement degradation readings (B9)",
"Meeker, Hahn and Escobar (1998) Statistical Methods for Reliability Data",
"Constructed validation data; no third-party license.",
c(unit = "String", temp_k = "Float64", time = "Float64",
response = "Float64"))
dg <- read_dataset("SURV_DEG_B9")
D <- 25
g <- length(units)
n_readings <- nrow(dg)
unit_ids <- sort(unique(dg$unit))
unit_stats <- lapply(unit_ids, function(u) {
s <- dg[dg$unit == u, ]
stt <- sum(s$time^2)
r_ls <- sum(s$time * s$response) / stt
list(stt = stt, r_ls = r_ls, rss = sum((s$response - r_ls * s$time)^2),
temp = s$temp_k[1])
})
r_ls <- vapply(unit_stats, `[[`, 0, "r_ls")
rss_by_unit <- vapply(unit_stats, `[[`, 0, "rss")
temp_by_unit <- vapply(unit_stats, `[[`, 0, "temp")
pseudo_failure <- D / r_ls
sigma_ref <- sqrt(sum(rss_by_unit) / (n_readings - g))
z95 <- stats::qnorm(0.975)
# Pseudo-failure references from survreg. survreg's lognormal is a location-scale
# model on log T, so the intercept a, optional slope b and Log(scale) map to the
# engine's eta_use = beta0 = log D - a, beta1 = -b (per inverse kelvin) and
# log tau. The engine's lifetime covariance of (eta_use, log tau) is the survreg
# covariance of (a, Log(scale)) with the intercept signs flipped.
fit1 <- survival::survreg(Surv(pseudo_failure) ~ 1, dist = "lognormal")
V1 <- stats::vcov(fit1)
eta1 <- log(D) - unname(stats::coef(fit1)[1])
tau1 <- fit1$scale
cov1 <- matrix(c(V1[1, 1], -V1[1, 2], -V1[1, 2], V1[2, 2]), 2, 2)
x_use <- 1 / 348 - 1 / temp_by_unit
fit2 <- survival::survreg(Surv(pseudo_failure) ~ x_use, dist = "lognormal")
V2 <- stats::vcov(fit2)
eta2 <- log(D) - unname(stats::coef(fit2)[1])
beta1_2 <- -unname(stats::coef(fit2)[2])
tau2 <- fit2$scale
cov2 <- matrix(c(V2[1, 1], -V2[1, 3], -V2[1, 3], V2[3, 3]), 2, 2)
# Delta-method lifetime references, implementing the engine's documented formulas:
# Q_p = D exp(-eta + tau z_p) with the log-quantile gradient (-1, tau z_p), and
# R(t) = Phi bar((log t - log D + eta) / tau) with the logit-scale gradient
# (-phi(w) / (tau R (1 - R)), phi(w) w / (R (1 - R))).
quantile_ref <- function(p, eta, tau, cov) {
zp <- stats::qnorm(p)
log_q <- log(D) - eta + tau * zp
gradient <- c(-1, tau * zp)
variance <- as.numeric(t(gradient) %*% cov %*% gradient)
c(estimate = exp(log_q),
lower = exp(log_q - z95 * sqrt(variance)),
upper = exp(log_q + z95 * sqrt(variance)))
}
reliability_ref <- function(t, eta, tau, cov) {
w <- (log(t) - log(D) + eta) / tau
value <- stats::pnorm(w, lower.tail = FALSE)
spread <- value * (1 - value)
dens <- stats::dnorm(w)
gradient <- c(-dens / (tau * spread), dens * w / spread)
variance <- as.numeric(t(gradient) %*% cov %*% gradient)
logit <- log(value) - log(1 - value)
expit <- function(u) if (u >= 0) 1 / (1 + exp(-u)) else exp(u) / (1 + exp(u))
c(time = t, reliability = value,
lower = expit(logit - z95 * sqrt(variance)),
upper = expit(logit + z95 * sqrt(variance)))
}
probabilities <- c(0.1, 0.5, 0.9)
r_times <- c(10, 15, 20)
counts_outputs <- list(
"/distance" = tol(D, "threshold distance D = s (h - y0)", "1e-9"),
"/unit_count" = exact(g, "units with readings"),
"/analyzed_count" = exact(n_readings, "observed readings analyzed"),
"/source_count" = exact(n_readings, "source rows"),
"/excluded_count" = exact(0, "no excluded rows"),
"/unobserved_count" = exact(0, "every reading observed")
)
units_outputs <- function(with_temperature) {
out <- list()
for (i in seq_along(unit_ids)) {
base <- paste0("/units/", i - 1)
out[[paste0(base, "/readings")]] <- exact(4, "four readings per unit")
out[[paste0(base, "/distinct_positive_times")]] <- exact(4, "times 4, 8, 12 and 16")
out[[paste0(base, "/least_squares_rate")]] <-
strict(r_ls[i], "sum(t z) / sum(t^2) through the baseline")
out[[paste0(base, "/empirical_bayes_rate")]] <-
tol(r_ls[i], "posterior-mode rate; the LS rate because the posterior shrinkage weight S_tt / sigma_e^2 exceeds 1 / tau^2 by eight orders of magnitude")
if (with_temperature) {
out[[paste0(base, "/temperature")]] <- strict(temp_by_unit[i], "constant unit test temperature")
}
}
out
}
readings_outputs <- function() {
out <- list()
for (i in seq_len(n_readings)) {
base <- paste0("/readings/", i - 1)
row <- dg[i, ]
fitted <- r_ls[match(row$unit, unit_ids)] * row$time
out[[paste0(base, "/time")]] <- strict(row$time, "echoed measurement time")
out[[paste0(base, "/response")]] <- strict(row$response, "echoed reading")
out[[paste0(base, "/fitted")]] <- tol(fitted, "baseline + LS-rate time; the empirical-Bayes rate equals the LS rate to below 3e-8 in log rate, about 2e-7 in fitted value", rel = "1e-6", atol = "1e-6")
out[[paste0(base, "/residual")]] <- tol(row$response - fitted, "response minus the LS trajectory; the engine's empirical-Bayes residual differs by under 3e-7", rel = "1e-6", atol = "1e-6")
}
out
}
lifetime_outputs <- function(eta, tau, cov) {
out <- list(
"/lifetime/distance" = tol(D, "distance echoed in the lifetime summary", "1e-9"),
"/lifetime/eta_use" = tol(eta, "use-condition log rate"),
"/lifetime/tau" = tol(tau, "unit-to-unit SD of the log rate")
)
med <- quantile_ref(0.5, eta, tau, cov)
out[["/lifetime/median/probability"]] <- strict(0.5, "median probability")
out[["/lifetime/median/estimate"]] <- tol(med[["estimate"]], "D exp(-eta)")
out[["/lifetime/median/lower"]] <- tol(med[["lower"]], "delta method on log Q with gradient (-1, 0)")
out[["/lifetime/median/upper"]] <- tol(med[["upper"]], "delta method on log Q with gradient (-1, 0)")
for (k in seq_along(probabilities)) {
p <- probabilities[k]
q <- quantile_ref(p, eta, tau, cov)
base <- paste0("/lifetime/quantiles/", k - 1)
out[[paste0(base, "/probability")]] <- strict(p, "requested probability")
out[[paste0(base, "/estimate")]] <- tol(q[["estimate"]], "D exp(-eta + tau z_p)")
out[[paste0(base, "/lower")]] <- tol(q[["lower"]], "delta method on log Q_p")
out[[paste0(base, "/upper")]] <- tol(q[["upper"]], "delta method on log Q_p")
}
for (k in seq_along(r_times)) {
point <- reliability_ref(r_times[k], eta, tau, cov)
base <- paste0("/lifetime/reliability/", k - 1)
out[[paste0(base, "/time")]] <- strict(r_times[k], "requested reliability time")
out[[paste0(base, "/reliability")]] <- tol(point[["reliability"]], "1 - Phi((log t - log D + eta) / tau)")
out[[paste0(base, "/lower")]] <- tol(point[["lower"]], "delta method on logit R(t)")
out[[paste0(base, "/upper")]] <- tol(point[["upper"]], "delta method on logit R(t)")
}
out
}
parameter_outputs <- function(has_slope, a_se, b_se, eta, tau) {
out <- list(
"/parameters/[name=beta0]/estimate" = tol(eta, "lognormal pseudo-failure intercept mapped to eta_use = log D - a"),
"/parameters/[name=beta0]/standard_error" = tol(a_se, "survreg intercept SE"),
"/parameters/[name=beta0]/lower" = tol(eta - z95 * a_se, "natural-scale Wald interval"),
"/parameters/[name=beta0]/upper" = tol(eta + z95 * a_se, "natural-scale Wald interval")
)
if (has_slope) {
out[["/parameters/[name=beta1]/estimate"]] <- tol(beta1_2, "negated survreg slope on 1/T_use - 1/T")
out[["/parameters/[name=beta1]/standard_error"]] <- tol(b_se, "survreg slope SE")
out[["/parameters/[name=beta1]/lower"]] <- tol(beta1_2 - z95 * b_se, "natural-scale Wald interval")
out[["/parameters/[name=beta1]/upper"]] <- tol(beta1_2 + z95 * b_se, "natural-scale Wald interval")
}
se_log_tau <- if (has_slope) sqrt(V2[3, 3]) else sqrt(V1[2, 2])
out[["/parameters/[name=tau]/estimate"]] <- tol(tau, "survreg Log(scale) exponentiated")
out[["/parameters/[name=tau]/standard_error"]] <- tol(se_log_tau, "survreg Log(scale) SE")
out[["/parameters/[name=tau]/lower"]] <- tol(tau * exp(-z95 * se_log_tau), "log-scale Wald interval")
out[["/parameters/[name=tau]/upper"]] <- tol(tau * exp(z95 * se_log_tau), "log-scale Wald interval")
out[["/parameters/[name=sigma_e]/estimate"]] <-
tol(sigma_ref, "pseudo-failure ML limit sqrt(RSS / (N - g)) of the estimated measurement SD")
out
}
BASE_NOTES <- c(
paste0(MANUAL, ":97-104: y_ij = y0 + s exp(eta_i + u_i) t_ij + e_ij with u ~ N(0, tau^2) and ",
"log T ~ N(log D - eta, tau^2); failure is threshold crossing of the noise-free trajectory."),
paste0(MANUAL, ":110-122: inverse-kelvin temperature acceleration and delta-method lifetime intervals ",
"for population quantities."),
paste0("Oracle: per-unit least-squares rates r_ls,i = sum(t z) / sum(t^2) with z = s (y - y0), ",
"pseudo-failure times t~_i = D / r_ls,i, then R survival::survreg(Surv(t~) ~ 1, dist = \"lognormal\") ",
"for STAT-SURV-DEG-001. survreg's intercept a and Log(scale) map to the engine's ",
"beta0 = log D - a and log tau; the lifetime covariance of (eta_use, log tau) is survreg's ",
"(Intercept, Log(scale)) covariance with the intercept signs flipped. survreg fits the ",
"observed information of the uncensored lognormal, which at the ML point equals the ",
"engine's marginal-likelihood information in this limit."),
paste0("Pseudo-failure limit: the data carry measurement noise SD 0.001 while the log rates spread ",
"over about 0.3, so sigma_e^2 / (tau^2 S_tt r^2) is below 2e-8 and the engine's direct ",
"marginal ML, its covariance, the empirical-Bayes rates and the fitted trajectories agree with ",
"the two-stage reference to well under 1e-6 relative (1e-7 absolute); the per-unit LS rates are ",
"closed-form algebra and allow 1e-9."),
paste0("Engine contract note: repeated-measurement mode estimates sigma_e and refuses a supplied ",
"measurement_sd (degradation_reliability_contracts), so the small calibrated error is carried by ",
"the data and the ML sigma_e is compared with its pseudo-failure limit sqrt(RSS / (N - g))."),
paste0("Not compared: the quadrature log-likelihood and its diagnostics (integration constants and ",
"search internals without a two-stage counterpart), the raw covariance matrix (its entries are ",
"covered through the parameter and lifetime intervals), the reliability curve (plot data) and ",
"text/boolean labels.")
)
# 001: nominal repeated-measurement fit at use conditions without the temperature axis.
emit("STAT-SURV-DEG-001",
"Degradation-based lifetime: repeated measurements, lognormal rates, no temperature axis",
list(
mode = "repeated", response_column = "response", time_column = "time",
unit_column = "unit", direction = "increasing", baseline = 0, threshold = 25,
time_unit = "hours", temperature_column = NULL, use_temperature = NULL,
dropout_ignorable_declared = TRUE,
lifetime_probabilities = as.list(probabilities),
reliability_times = as.list(r_times),
confidence_level = 0.95
),
c(counts_outputs,
parameter_outputs(FALSE, sqrt(V1[1, 1]), NULL, eta1, tau1),
lifetime_outputs(eta1, tau1, cov1),
units_outputs(FALSE),
readings_outputs()),
BASE_NOTES)
# 002: the temperature axis with inverse-kelvin acceleration to a 348 K use condition.
emit("STAT-SURV-DEG-002",
"Degradation-based lifetime: inverse-kelvin temperature acceleration to a use temperature",
list(
mode = "repeated", response_column = "response", time_column = "time",
unit_column = "unit", direction = "increasing", baseline = 0, threshold = 25,
time_unit = "hours", temperature_column = "temp_k", use_temperature = 348,
dropout_ignorable_declared = TRUE,
lifetime_probabilities = as.list(probabilities),
reliability_times = as.list(r_times),
confidence_level = 0.95
),
c(counts_outputs,
parameter_outputs(TRUE, sqrt(V2[1, 1]), sqrt(V2[2, 2]), eta2, tau2),
lifetime_outputs(eta2, tau2, cov2),
units_outputs(TRUE),
readings_outputs()),
c(BASE_NOTES,
paste0("Temperature oracle: x_i = 1/348 - 1/temp_k_i and R survival::survreg(Surv(t~) ~ x_i, ",
"dist = \"lognormal\"); the engine's beta0 = log D - a and beta1 = -b, with the ",
"covariance of (eta_use, log tau) taken from the (Intercept, Log(scale)) block with the ",
"intercept signs flipped; beta1's own covariance enters only through the parameter table.")),
kind = "axis_coverage")
cat("degradation references generated\n")
generators/r/survival_hard.R
source("common.R",local=TRUE)
i <- seq_len(40L)
hard <- data.frame(time=1e6+137*i+4*sin(i*0.87),
status=as.integer(i%%5L!=0L),
stress=as.double(rep(c(101000,102000,103000,104000),each=10L)),
x=1e7+2*cos(i*0.47)+0.05*i)
export_dataset(hard,"SURV_HARD","Constructed large-offset lifetimes and predictors",
"R survival::survfit/coxph/survreg hard-numeric reference",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64"))
d <- read_dataset("SURV_HARD")
j <- seq_len(30L)
stable <- data.frame(time=1e6+j,response=100-0.03*j+0.004*sin(j*0.91))
export_dataset(stable,"SURV_STABILITY_HARD","Constructed large-offset time with small response slope",
"R stats::lm QR regression hard-numeric reference",
"Constructed validation data; no third-party license.",
c(time="Float64",response="Float64"))
s <- read_dataset("SURV_STABILITY_HARD")
interval <- read_dataset("SURV_INTERVAL")
interval$lower <- ifelse(is.na(interval$lower),NA_real_,1e6+interval$lower)
interval$upper <- ifelse(is.na(interval$upper),NA_real_,1e6+interval$upper)
export_dataset(interval,"SURV_INTERVAL_HARD","Constructed large-offset exact and mixed censoring endpoints",
"R survival::survreg interval2 hard-numeric reference",
"Constructed validation data; no third-party license.",
c(lower="Float64",upper="Float64"))
interval <- read_dataset("SURV_INTERVAL_HARD")
run <- generator_run(c("survival","stats","jsonlite"))
tol <- function(x,note,tier="1e-6") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R hard-numeric reference:",note))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,notes) {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Hard-numeric survival",id),
case_kind="hard_numeric",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R survival/stats",
citation="R survival::survfit/coxph/survreg and stats::lm documentation",
generator=list(path="generators/r/survival_hard.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-NUMERIC-001"),review_reference=NULL))
}
km <- survival::survfit(survival::Surv(time,status)~1,data=d,conf.type="log-log")
cfg <- list(time_column="time",event_column="status",event_value="1",censored_value="0")
emit("STAT-SURV-KM-HARD-001","tabelz.executor.survival","survivalMenuItem",
"SURV_HARD",cfg,
list("/curves/0/survival/0"=tol(km$surv[1],"Large-offset first KM step","1e-9"),
"/curves/0/median/time"=tol(summary(km)$table[["median"]],
"First crossing of one-half survival","1e-9")),
c("Lifetimes are near 1e6 with small relative spread; events remain distinct."))
ticks <- c(1e6+137,1e6+1370,1e6+5500)
risk <- vapply(ticks,function(t) sum(d$time>=t),0L)
emit("STAT-SURV-RISK-HARD-001","tabelz.analysis.survival_nonparametric:number_at_risk",
"survivalMenuItem","SURV_HARD",c(cfg,list(times=as.list(ticks))),
list("/0"=exact(risk[1],"Risk at first large time"),
"/1"=exact(risk[2],"Risk at interior large time"),
"/2"=exact(risk[3],"Risk near largest time")),
c("At-risk comparisons use exact stored double times, not rounded display labels."))
cox <- survival::coxph(survival::Surv(time,status)~x,data=d,ties="efron")
emit("STAT-SURV-COX-HARD-001","tabelz.executor.cox_regression","coxRegressionMenuItem",
"SURV_HARD",list(duration_column="time",event_column="status",event_value="1",
censored_value="0",predictors=list("x")),
list("/coefficients/0/log_hazard"=tol(coef(cox)[[1]],"Large-offset covariate Efron log hazard")),
c("Covariate is near 1e7 with O(1) spread; Cox partial likelihood is shift-invariant."))
emit("STAT-SURV-FAMILIES-HARD-001","tabelz.executor.reliability",
"reliabilityFamiliesMenuItem","SURV_HARD",
list(mode="families",time_column="time",event_column="status"),
list("/fits/[family=exponential]/param1_val"=tol(sum(d$time)/sum(d$status),
"Large-offset exponential time-on-test scale","1e-9")),
c("Exponential scale=sum time on test/failures remains analytic at large lifetime offset."))
fit_alt <- survival::survreg(survival::Surv(time,status)~stress,data=d,dist="weibull",
control=survival::survreg.control(rel.tolerance=1e-14,maxiter=1000))
use_stress <- 102500
median_use <- exp(coef(fit_alt)[[1]]+coef(fit_alt)[[2]]*use_stress)*(log(2))^fit_alt$scale
emit("STAT-SURV-ALT-HARD-001","tabelz.executor.reliability","acceleratedLifeMenuItem",
"SURV_HARD",list(mode="accelerated",time_column="time",event_column="status",
stress_column="stress",stress_type="linear",stress_unit="native",
use_stress=use_stress,high_stress=104000),
list("/fits/[family=weibull]/median_use"=tol(median_use,"Large-offset linear-stress Weibull median")),
c("Stress is near 1e5 with a 3000-unit span; R survreg Weibull scale=1/shape."))
emit("STAT-SURV-WEIBULL-HARD-001","tabelz.analysis.weibull_aft:compute_weibull_aft",
"weibullAftMenuItem","SURV_HARD",
list(time_column="time",event_column="status",stress_column="stress",
prediction_stresses=list(use_stress)),
list("/coefficients/2/estimate"=tol(1/fit_alt$scale,"Large-offset Weibull shape=1/survreg scale")),
c("The independent reference uses the same censored Weibull likelihood and converted shape."))
fit_stable <- stats::lm(response~time,data=s)
emit("STAT-SURV-STABILITY-HARD-001","tabelz.analysis.product_stability:compute_product_stability",
"productStabilityMenuItem","SURV_STABILITY_HARD",
list(time_column="time",response_column="response",lsl=95),
list("/coefficients/1/estimate"=tol(coef(fit_stable)[[2]],
"Large-offset time OLS slope","1e-9")),
c("Time is near 1e6 with unit increments; QR regression resolves a small slope."))
fit_int <- survival::survreg(survival::Surv(lower,upper,type="interval2")~1,
data=interval,dist="exponential",control=survival::survreg.control(rel.tolerance=1e-14,maxiter=1000))
emit("STAT-SURV-INTERVAL-HARD-001","tabelz.executor.reliability","intervalLifeMenuItem",
"SURV_INTERVAL_HARD",list(mode="interval",lower_column="lower",upper_column="upper"),
list("/fits/[family=exponential]/param1_val"=tol(exp(coef(fit_int)[[1]]),
"Large-offset mixed-censoring exponential scale")),
c("All positive endpoints have a 1e6 offset but retain their censoring interval widths."))
cat("hard survival references generated\n")
generators/r/survival_life_families.R
source("common.R", local = TRUE)
# Reliability-engineering life analyses (B9) against R survival's survreg/survfit:
# Quality > Reliability engineering > Parametric life distribution, Distribution
# identification plot, Nonparametric life estimation and Regression with life data.
# Every reference reads the exported CSV back before computing.
suppressMessages(library(survival))
run <- generator_run(c("stats", "survival", "jsonlite"))
tol <- function(value, why, rel = "1e-6", atol = "1e-7") list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = atol, relative_tolerance = rel,
rationale = paste("R survival reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
# survreg distribution names for the engine families.
SURV_DIST <- c(weibull = "weibull", lognormal = "lognormal", exponential = "exponential",
sev = "extreme", logistic = "logistic", loglogistic = "loglogistic",
normal = "gaussian")
POSITIVE <- c("exponential", "weibull", "lognormal", "loglogistic")
MANUAL <- "docs/manual/chapters/98-life-model-extensions.tex"
CITATION <- "Therneau T (2024). A Package for Survival Analysis in R. R package version 3.6; Meeker, Hahn and Escobar (1998) Statistical Methods for Reliability Data, Wiley; Kalbfleisch and Prentice (2002)."
SOURCE <- "R survival package: survreg, survfit, predict.survreg"
emit <- function(id, title, entry, menu, requirement, dataset, configuration, outputs, notes,
kind = "nominal") {
write_expected("survival", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("survival", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "survival", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/survival/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = SOURCE, citation = CITATION,
generator = list(path = "generators/r/survival_life_families.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.")
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(requirement), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
counts_outputs <- function(d, layout) {
if (layout == "arbitrary") {
kinds <- ifelse(is.na(d$start), "left", ifelse(is.na(d$end), "right",
ifelse(d$start == d$end, "exact", "interval")))
} else {
kinds <- ifelse(d$status == 1, "exact", "right")
}
w <- d$freq
list(
"/counts/source_count" = exact(nrow(d), "source rows"),
"/counts/missing_count" = exact(0, "all rows usable in this layout"),
"/counts/zero_frequency_rows" = exact(sum(w == 0), "rows with zero frequency"),
"/counts/rows_used" = exact(sum(w > 0), "records with positive frequency"),
"/counts/exact" = tol(sum(w[kinds == "exact"]), "weighted exact records"),
"/counts/right" = tol(sum(w[kinds == "right"]), "weighted right-censored records"),
"/counts/left" = tol(sum(w[kinds == "left"]), "weighted left-censored records"),
"/counts/interval" = tol(sum(w[kinds == "interval"]), "weighted interval records")
)
}
survreg_fit <- function(start, end, weights, family, threshold) {
data <- data.frame(start = start - threshold, end = end - threshold)
do.call(survreg, list(formula = Surv(start, end, type = "interval2") ~ 1, data = data,
weights = weights, dist = SURV_DIST[[family]]))
}
# The engine's family_parameters rows, in order, from the survreg fit.
family_params_outputs <- function(fit, family, threshold, level, prefix = "/parameters") {
z <- stats::qnorm(0.5 + level / 2)
mu <- unname(coef(fit))
sigma <- fit$scale
v <- vcov(fit)
se_mu <- sqrt(v[1, 1])
se_log_sigma <- if (family != "exponential") sqrt(v[2, 2]) else 0
rows <- list()
add <- function(estimate, error, lower, upper) {
rows[[length(rows) + 1]] <<- c(estimate, error, lower, upper)
}
if (family == "exponential") {
add(exp(mu), exp(mu) * se_mu, exp(mu - z * se_mu), exp(mu + z * se_mu))
} else if (family == "weibull") {
add(1 / sigma, se_log_sigma / sigma,
exp(-(log(sigma) + z * se_log_sigma)), exp(-(log(sigma) - z * se_log_sigma)))
add(exp(mu), exp(mu) * se_mu, exp(mu - z * se_mu), exp(mu + z * se_mu))
} else {
add(mu, se_mu, mu - z * se_mu, mu + z * se_mu)
add(sigma, sigma * se_log_sigma,
exp(log(sigma) - z * se_log_sigma), exp(log(sigma) + z * se_log_sigma))
}
out <- list()
for (i in seq_along(rows)) {
base <- paste0(prefix, "/", i - 1)
r <- rows[[i]]
out[[paste0(base, "/estimate")]] <- tol(r[1], "survreg maximum likelihood")
out[[paste0(base, "/standard_error")]] <- tol(r[2], "delta method from survreg vcov")
out[[paste0(base, "/lower")]] <- tol(r[3], "Wald limit")
out[[paste0(base, "/upper")]] <- tol(r[4], "Wald limit")
}
if (family %in% POSITIVE) {
out[[paste0(prefix, "/", length(rows), "/estimate")]] <-
tol(threshold, "declared fixed threshold (no interval)")
}
out
}
percentile_outputs <- function(fit, family, percents, threshold, level, prefix = "/percentiles") {
z <- stats::qnorm(0.5 + level / 2)
percents <- sort(unique(percents))
q <- predict(fit, type = "quantile", p = percents / 100, se.fit = TRUE)
values <- as.matrix(q$fit)[1, ]
errors <- as.matrix(q$se.fit)[1, ]
out <- list()
for (i in seq_along(percents)) {
base <- paste0(prefix, "/", i - 1)
estimate <- values[i] + threshold
error <- errors[i]
if (family %in% POSITIVE) {
# The engine transforms the log-scale Wald limits: gamma + exp(y +/- z se_y),
# with se_y = se_t / t. survreg's se.fit is the t-scale delta SE.
spread <- z * errors[i] / values[i]
lower <- threshold + exp(log(values[i]) - spread)
upper <- threshold + exp(log(values[i]) + spread)
} else {
lower <- estimate - z * error
upper <- estimate + z * error
}
out[[paste0(base, "/estimate")]] <- tol(estimate, "survreg predict type=quantile")
out[[paste0(base, "/standard_error")]] <- tol(error, "survreg predict delta-method SE")
out[[paste0(base, "/lower")]] <- tol(lower, "transformed log-scale Wald limit")
out[[paste0(base, "/upper")]] <- tol(upper, "transformed log-scale Wald limit")
}
out
}
fit_info_outputs <- function(fit, freq, threshold = 0) {
ll <- fit$loglik[2]
k <- if (fit$dist == "exponential") 1 else 2
units <- sum(freq)
v <- vcov(fit)
out <- list(
"/log_likelihood" = tol(ll, "weighted interval2 log-likelihood"),
"/aic" = tol(2 * k - 2 * ll, "2k - 2 log L"),
"/bic" = tol(k * log(units) - 2 * ll, "k log(sum of frequencies) - 2 log L"),
"/covariance/0/0" = tol(v[1, 1], "observed information (location)")
)
if (k == 2) {
out[["/covariance/0/1"]] <- tol(v[1, 2], "observed information cross term")
out[["/covariance/1/1"]] <- tol(v[2, 2], "observed information (log dispersion)")
}
out
}
# =======================================================================================
# Dataset 1: mixed arbitrary censoring (exact, right, left, interval), frequencies and two
# predictors for the regression window. Exact/right rows also carry a status code.
set.seed(20261021)
n <- 24
t_fail <- round(stats::rweibull(n, 1.7, 110), 1)
censor_at <- round(stats::runif(n, 40, 260), 1)
obs <- pmin(t_fail, censor_at)
failed <- t_fail <= censor_at
start <- obs
end <- ifelse(failed, obs, NA)
ii <- which(failed)[1:4]
start[ii] <- pmax(round(obs[ii] - stats::runif(4, 2, 20), 1), 0.1)
end[ii] <- obs[ii]
ll <- which(failed)[5:6]
start[ll] <- NA
end[ll] <- obs[ll]
freq <- rep(1L, n)
freq[c(2, 7, 13)] <- c(2L, 3L, 2L)
temp_c <- round(stats::runif(n, 40, 80), 1)
stress <- round(stats::runif(n, 50, 200), 1)
temp3 <- ifelse(temp_c < 55, "low", ifelse(temp_c < 68, "mid", "high"))
status <- ifelse(!is.na(start) & !is.na(end) & start == end, 1L,
ifelse(!is.na(start) & is.na(end), 0L, NA))
life <- data.frame(start = start, end = end, status = status, freq = freq,
temp_c = temp_c, stress = stress, temp3 = temp3)
export_dataset(life, "SURV_LIFE_B9", "Constructed censored life data (B9)",
"Meeker, Hahn and Escobar (1998); R survival package documentation",
"Constructed validation data; no third-party license.",
c(start = "Float64", end = "Float64", status = "Int64", freq = "Int64",
temp_c = "Float64", stress = "Float64", temp3 = "String"))
d <- read_dataset("SURV_LIFE_B9")
stopifnot(all(!is.na(d$temp3)), all(!is.na(d$temp_c)), all(!is.na(d$stress)))
LIFE <- "tabelz.analysis.life_distribution_analysis:compute_life_distribution"
LIFE_REQ <- "REQ-STAT-SURV-LIFE-DISTRIBUTION-001"
ARBITRARY <- list(layout = "arbitrary", start = "start", end = "end", frequency = "freq")
LIFE_NOTES <- c(
paste0(MANUAL, ":4-18: the likelihood uses f(t), S(L), F(U) and F(U)-F(L) for exact, right-, left- and interval-censored records; R reads the same records as Surv(start, end, type = 'interval2') with frequency weights."),
"survreg's location is the engine's mu, survreg's scale the engine's sigma; Weibull shape is 1/scale and the characteristic life exp(mu); the exponential fixes sigma = 1.",
"Percentiles and their standard errors are survreg predict(type = 'quantile', se.fit = TRUE); intervals are estimate +/- z * SE as the engine reports.",
"Iterative maximum likelihood: outputs allow 1e-7 absolute or 1e-6 relative."
)
life_case <- function(id, title, family, threshold, level, percents, notes, kind) {
fit <- survreg_fit(d$start, d$end, d$freq, family, threshold)
outputs <- c(
counts_outputs(d, "arbitrary"),
fit_info_outputs(fit, d$freq),
family_params_outputs(fit, family, threshold, level),
percentile_outputs(fit, family, percents, threshold, level)
)
config <- c(ARBITRARY, list(family = family, threshold = threshold,
confidence_level = level, percents = as.list(percents)))
emit(id, title, LIFE, "lifeDistributionMenuItem", LIFE_REQ, "SURV_LIFE_B9", config, outputs,
notes, kind)
}
life_case("STAT-SURV-LIFE-001", "Parametric Weibull life distribution under mixed censoring",
"weibull", 0.0, 0.95, c(1, 5, 10, 50), LIFE_NOTES, "nominal")
life_case("STAT-SURV-LIFE-002", "Lognormal life distribution with a declared threshold at 90%",
"lognormal", 5.0, 0.90, c(5, 50), c(LIFE_NOTES,
"Threshold 5 shifts the data before fitting; survreg fits the shifted records."), "axis_coverage")
life_case("STAT-SURV-LIFE-004", "Smallest-extreme-value life distribution with interval records",
"sev", 0.0, 0.95, c(1, 10, 50), c(LIFE_NOTES,
"The SEV family has real support: survreg dist = 'extreme' on the unshifted records."), "axis_coverage")
# 003: exponential in the time-and-status layout on the exact/right rows.
dk <- d[!is.na(d$status), ]
fit3 <- survreg(Surv(start, status) ~ 1, data = dk, weights = dk$freq, dist = "exponential")
outputs3 <- c(
list("/counts/source_count" = exact(nrow(d), "source rows"),
"/counts/missing_count" = exact(sum(is.na(d$status)), "rows without a status code"),
"/counts/zero_frequency_rows" = exact(0, "no zero-frequency rows"),
"/counts/rows_used" = exact(nrow(dk), "rows with a status code"),
"/counts/exact" = tol(sum(dk$freq[dk$status == 1]), "weighted failures"),
"/counts/right" = tol(sum(dk$freq[dk$status == 0]), "weighted right-censored rows"),
"/counts/left" = tol(0, "no left censoring in this layout"),
"/counts/interval" = tol(0, "no interval records in this layout")),
fit_info_outputs(fit3, dk$freq),
family_params_outputs(fit3, "exponential", 0.0, 0.95),
percentile_outputs(fit3, "exponential", c(1, 5, 50), 0.0, 0.95)
)
emit("STAT-SURV-LIFE-003", "Exponential life distribution from time-and-status data",
LIFE, "lifeDistributionMenuItem", LIFE_REQ, "SURV_LIFE_B9",
list(layout = "right_censored", start = "start", status = "status", frequency = "freq",
family = "exponential", threshold = 0.0, confidence_level = 0.95,
percents = list(1, 5, 50)),
outputs3, c(LIFE_NOTES,
"The time-and-status layout reads only rows with a status code; the interval and left-censored rows count as missing."), "axis_coverage")
# =======================================================================================
# Dataset 2: exact/right-censored rows only (Kaplan-Meier).
set.seed(20261022)
n2 <- 22
t2 <- round(stats::rweibull(n2, 1.9, 150), 1)
c2 <- round(stats::runif(n2, 50, 300), 1)
fail2 <- t2 <= c2
time2 <- pmin(t2, c2)
# a tied time with one failure and one censoring: the failure precedes the censoring
tie <- round(min(t2[3], t2[9], c2[3], c2[9]), 1)
time2[3] <- tie
time2[9] <- tie
fail2[3] <- TRUE
fail2[9] <- FALSE
# the largest observed time stays censored so the log-log bands remain defined
top <- which.max(time2)
fail2[top] <- FALSE
time2[top] <- round(max(time2) + 37.3, 1)
freq2 <- rep(1L, n2)
freq2[c(5, 14)] <- c(2L, 3L)
km_data <- data.frame(time = time2, status = ifelse(fail2, 1L, 0L), freq = freq2,
stress = round(stats::runif(n2, 50, 200), 1))
export_dataset(km_data, "SURV_KM_B9", "Constructed exact and right-censored life data (B9)",
"Kaplan and Meier (1958); R survival package documentation",
"Constructed validation data; no third-party license.",
c(time = "Float64", status = "Int64", freq = "Int64", stress = "Float64"))
kmd <- read_dataset("SURV_KM_B9")
NP <- "tabelz.analysis.life_distribution_analysis:compute_nonparametric_life"
NP_REQ <- "REQ-STAT-SURV-NONPARAMETRIC-LIFE-001"
NP_NOTES <- c(
paste0(MANUAL, ":44-51: Kaplan-Meier for exact and right-censored records; failures precede censorings at tied times; log-minus-log intervals are unavailable at survival 0 or 1."),
"R: survfit(Surv(time, status) ~ 1, weights = freq, conf.type = 'log-log'); steps are compared at failure times only, as the engine reports them."
)
km_frame <- function(fit) {
s <- summary(fit, censored = TRUE)
data.frame(time = s$time, n.risk = s$n.risk, n.event = s$n.event, n.censor = s$n.censor,
surv = s$surv, lower = s$lower, upper = s$upper)
}
km_case <- function(id, title, level, notes, kind) {
fit <- survfit(Surv(time, status) ~ 1, data = kmd, weights = kmd$freq,
conf.type = "log-log", conf.int = level)
s <- km_frame(fit)
keep <- s$n.event > 0
s <- s[keep, ]
stopifnot(all(s$surv > 0 & s$surv < 1))
outputs <- c(
list("/counts/source_count" = exact(nrow(kmd), "source rows"),
"/counts/missing_count" = exact(0, "no missing values"),
"/counts/zero_frequency_rows" = exact(0, "no zero-frequency rows"),
"/counts/rows_used" = exact(nrow(kmd), "records used"),
"/counts/exact" = tol(sum(kmd$freq[kmd$status == 1]), "weighted failures"),
"/counts/right" = tol(sum(kmd$freq[kmd$status == 0]), "weighted censored"),
"/counts/left" = tol(0, "none"), "/counts/interval" = tol(0, "none"))
)
for (i in seq_len(nrow(s))) {
base <- paste0("/estimate/kaplan_meier/", i - 1)
outputs[[paste0(base, "/time")]] <- tol(s$time[i], "failure time")
outputs[[paste0(base, "/at_risk")]] <- tol(s$n.risk[i], "weighted units at risk")
outputs[[paste0(base, "/failures")]] <- tol(s$n.event[i], "weighted failures")
outputs[[paste0(base, "/censored")]] <- tol(s$n.censor[i], "weighted censorings")
outputs[[paste0(base, "/survival")]] <- tol(s$surv[i], "Kaplan-Meier survival")
outputs[[paste0(base, "/lower")]] <- tol(s$lower[i], "log-minus-log band")
outputs[[paste0(base, "/upper")]] <- tol(s$upper[i], "log-minus-log band")
}
emit(id, title, NP, "nonparametricLifeMenuItem", NP_REQ, "SURV_KM_B9",
list(layout = "right_censored", start = "time", status = "status", frequency = "freq",
confidence_level = level),
outputs, notes, kind)
}
km_case("STAT-SURV-NP-001", "Kaplan-Meier life estimation with frequencies at 95%",
0.95, NP_NOTES, "nominal")
km_case("STAT-SURV-NP-003", "Kaplan-Meier life estimation with a tied failure and censoring at 90%",
0.90, c(NP_NOTES, "Rows 3 and 9 share a time; the failure precedes the censoring in both implementations."), "axis_coverage")
# =======================================================================================
# Dataset 3: interval data with a unique Turnbull NPMLE and no open tail: the largest
# observation is an exact failure beyond every interval and right-censoring bound.
int_data <- data.frame(
start = c(8, 15, 24, 55, NA, 5, 12, 0, 20, 28, NA, 30, 40),
end = c(8, 15, 24, 55, 9, 18, 30, 14, 40, 50, NA, NA, NA),
freq = c(2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)
)
int_data$end[c(1:4)] <- int_data$start[c(1:4)]
int_data$end[5] <- 9
int_data$end[11] <- NA
int_rows <- data.frame(start = c(NA, 5, 12, 0, 20, 28), end = c(9, 18, 30, 14, 40, 50))
int_data <- rbind(
data.frame(start = c(8, 15, 24, 55), end = c(8, 15, 24, 55), freq = c(2L, 1L, 3L, 1L)),
data.frame(start = c(NA, 5, 12, 0, 20, 28), end = c(9, 18, 30, 14, 40, 50),
freq = c(1L, 1L, 1L, 1L, 1L, 1L)),
data.frame(start = c(30, 40), end = c(NA, NA), freq = c(1L, 1L))
)
export_dataset(int_data, "SURV_INTERVAL_B9", "Constructed interval-censored life data (B9)",
"Turnbull (1976); R survival package documentation",
"Constructed validation data; no third-party license.",
c(start = "Float64", end = "Float64", freq = "Int64"))
idt <- read_dataset("SURV_INTERVAL_B9")
# Turnbull NPMLE by the EM self-consistency algorithm on the innermost intervals,
# implemented from Turnbull (1976); cross-checked against survival::survfit.
turnbull_reference <- function(d) {
kinds <- ifelse(is.na(d$start), "left", ifelse(is.na(d$end), "right",
ifelse(d$start == d$end, "exact", "interval")))
left_value <- ifelse(kinds == "left", -Inf, d$start)
right_value <- ifelse(kinds == "right", Inf, ifelse(kinds == "exact", d$start, d$end))
left_open <- kinds != "exact"
events <- rbind(
data.frame(value = left_value, kind = ifelse(left_open, 2, 0), open = left_open),
data.frame(value = right_value, kind = 1, open = FALSE)
)
events <- events[order(events$value, events$kind), ]
intervals <- list()
for (i in seq_len(nrow(events) - 1)) {
if (events$kind[i] %in% c(0, 2) && events$kind[i + 1] == 1 && is.finite(events$value[i + 1])) {
intervals[[length(intervals) + 1]] <- c(events$value[i], events$value[i + 1],
events$open[i])
}
}
intervals <- intervals[!duplicated(intervals)]
intervals <- intervals[order(vapply(intervals, function(x) x[1], 0),
vapply(intervals, function(x) x[2], 0))]
membership <- matrix(FALSE, nrow(d), length(intervals))
for (j in seq_along(intervals)) {
low <- intervals[[j]][1]
high <- intervals[[j]][2]
low_open <- intervals[[j]][3]
starts_inside <- (left_value < low) | ((left_value == low) & (!left_open | low_open))
membership[, j] <- starts_inside & (high <= right_value)
}
stopifnot(all(rowSums(membership) >= 1))
weight <- d$freq
mass <- rep(1 / length(intervals), length(intervals))
for (step in 1:100000) {
probability <- as.vector(membership %*% mass)
gradient <- as.vector((weight / probability) %*% membership) / sum(weight)
new_mass <- mass * gradient
new_mass <- new_mass / sum(new_mass)
if (max(abs(new_mass - mass)) < 1e-14) {
mass <- new_mass
break
}
mass <- new_mass
}
list(intervals = intervals, mass = mass,
high = vapply(intervals, function(x) x[2], 0),
open = vapply(intervals, function(x) as.logical(x[3]), TRUE),
survival_after = 1 - cumsum(mass))
}
turn <- turnbull_reference(idt)
turn_outputs <- list()
for (j in seq_along(turn$intervals)) {
low <- turn$intervals[[j]][1]
stopifnot(is.finite(low), is.finite(turn$high[j]))
base <- paste0("/estimate/turnbull/", j - 1)
turn_outputs[[paste0(base, "/low")]] <- tol(low, "innermost interval lower end")
turn_outputs[[paste0(base, "/high")]] <- tol(turn$high[j], "innermost interval upper end")
turn_outputs[[paste0(base, "/mass")]] <- tol(turn$mass[j], "Turnbull EM mass")
turn_outputs[[paste0(base, "/survival_before")]] <- tol(1 - cumsum(turn$mass)[j] + turn$mass[j], "survival above the interval")
turn_outputs[[paste0(base, "/survival_after")]] <- tol(turn$survival_after[j], "survival below the interval")
}
NP2_NOTES <- c(
paste0(MANUAL, ":48-51: Turnbull masses on maximal intersection intervals; no Greenwood band; likelihood optimality is checked, not assumed."),
"Reference: an independent R implementation of Turnbull's (1976) EM self-consistency algorithm on the innermost intervals, iterated until the masses change by less than 1e-14. survival::survfit's interval-censored estimate places its drops at interval midpoints and a different support enumeration, so it cannot be compared row by row; the total mass and the shared exact-time steps were checked against it during development.",
"The dataset has a unique NPMLE (no overlapping-support ambiguity) and no open tail: the largest observation is an exact failure beyond every interval and censoring bound.",
"Every innermost interval (zero-mass rows included) is compared positionally in lower- then upper-end order, as the engine reports them."
)
emit("STAT-SURV-NP-002", "Turnbull nonparametric life estimation from interval data",
NP, "nonparametricLifeMenuItem", NP_REQ, "SURV_INTERVAL_B9",
list(layout = "arbitrary", start = "start", end = "end", frequency = "freq",
confidence_level = 0.95),
c(list("/counts/source_count" = exact(nrow(idt), "source rows"),
"/counts/missing_count" = exact(0, "no missing values"),
"/counts/zero_frequency_rows" = exact(0, "none"),
"/counts/rows_used" = exact(nrow(idt), "records used"),
"/counts/exact" = tol(sum(idt$freq[idt$start == idt$end & !is.na(idt$start) & !is.na(idt$end)]), "weighted exact"),
"/counts/right" = tol(sum(idt$freq[is.na(idt$end)]), "weighted right-censored"),
"/counts/left" = tol(sum(idt$freq[is.na(idt$start)]), "weighted left-censored"),
"/counts/interval" = tol(sum(idt$freq[!is.na(idt$start) & !is.na(idt$end) & idt$start != idt$end]), "weighted interval")),
turn_outputs),
NP2_NOTES, "axis_coverage")
# =======================================================================================
# Dataset 4: complete exact data for distribution identification (classical AD).
set.seed(20261023)
n4 <- 26
t4 <- round(stats::rlnorm(n4, log(95), 0.55), 1)
freq4 <- rep(1L, n4)
freq4[c(4, 11, 19)] <- 2L
complete <- data.frame(time = t4, freq = freq4)
export_dataset(complete, "SURV_COMPLETE_B9", "Constructed complete life data (B9)",
"Anderson and Darling (1954); Benard and Bos-Levenbach (1953)",
"Constructed validation data; no third-party license.",
c(time = "Float64", freq = "Int64"))
cd <- read_dataset("SURV_COMPLETE_B9")
# Dataset 4 also serves the censored identification case: censor the KM dataset's copy?
# No: the censored identification case uses SURV_KM_B9 (right censoring).
standard_quantile <- function(family, p) {
if (family %in% c("weibull", "exponential", "sev")) return(log(-log1p(-p)))
if (family %in% c("lognormal", "normal")) return(stats::qnorm(p))
log(p / (1 - p))
}
family_cdf <- function(family, fit, times, threshold) {
mu <- unname(coef(fit))
sigma <- fit$scale
if (family %in% POSITIVE) {
shifted <- times - threshold
z <- ifelse(shifted > 0, (log(ifelse(shifted > 0, shifted, 1)) - mu) / sigma, -Inf)
} else {
z <- (times - mu) / sigma
}
if (family %in% c("weibull", "exponential", "sev")) return(1 - exp(-exp(z)))
if (family %in% c("lognormal", "normal")) return(stats::pnorm(z))
stats::plogis(z)
}
# The piece integral of (F_n - F)^2 / (F (1 - F)) dF over (u1, u2] at level c.
ad_piece <- function(level, u1, u2) {
if (u2 <= u1) return(0)
total <- -(u2 - u1)
if (level > 0) {
if (u1 <= 0) return(Inf)
total <- total + level * level * log(u2 / u1)
}
if (level < 1) {
if (u2 >= 1) return(Inf)
total <- total - (1 - level)^2 * log((1 - u2) / (1 - u1))
}
total
}
# Complete-data identification on cd: Benard positions and the classical Anderson-Darling.
ident_complete <- function(families, threshold, level) {
times <- rep(cd$time, cd$freq)
sorted <- sort(times)
n <- length(sorted)
probabilities <- (seq_len(n) - 0.3) / (n + 0.4)
out <- list()
for (index in seq_along(families)) {
family <- families[index]
fit <- survreg_fit(cd$time, cd$time, cd$freq, family, threshold)
base <- paste0("/families/", index - 1)
u <- family_cdf(family, fit, sorted, threshold)
# closed form of n * integral (F_n - F)^2 / (F (1 - F)) dF from the definition:
# F_n is constant between order statistics (level (i-1)/n over (t_(i-1), t_(i)])
pieces <- ad_piece(0, 0, u[1])
for (i in 2:n) {
pieces <- pieces + ad_piece((i - 1) / n, u[i - 1], u[i])
}
pieces <- pieces + ad_piece(1, u[n], 1) # the final piece to F = 1 (complete data)
statistic <- n * pieces
y <- if (family %in% POSITIVE) log(sorted - threshold) else sorted
w <- vapply(probabilities, function(p) standard_quantile(family, p), 0)
out <- c(out, family_params_outputs(fit, family, threshold, level,
prefix = paste0(base, "/parameters")))
out[[paste0(base, "/log_likelihood")]] <- tol(fit$loglik[2], "weighted log-likelihood")
out[[paste0(base, "/anderson_darling")]] <- tol(statistic, "Anderson-Darling A2 from the definition n*integral (F_n - F)^2/(F(1-F)) dF")
out[[paste0(base, "/correlation")]] <- tol(stats::cor(y, w), "correlation of the plotted Benard positions against the standard quantiles")
out <- c(out, percentile_outputs(fit, family, c(1, 5, 10, 50), threshold, level,
prefix = paste0(base, "/percentiles")))
}
out
}
DISTID <- "tabelz.analysis.life_distribution_id:compute_life_distribution_id"
DISTID_REQ <- "REQ-STAT-SURV-LIFE-DISTRIBUTION-ID-001"
ALL7 <- c("weibull", "lognormal", "exponential", "loglogistic", "logistic", "normal", "sev")
DISTID_NOTES <- c(
paste0(MANUAL, ":53-62: probability-plot positions follow Benard median ranks (i - 0.3)/(n + 0.4) for complete data, with frequencies expanding ranks; the Anderson-Darling statistic is the complete-data form; no family is selected automatically."),
"R: survreg per family; A2 computed from its definition by the exact piecewise closed form c^2 log(u2/u1) - (1-c)^2 log((1-u2)/(1-u1)) - (u2-u1); the correlation is exact arithmetic on the plotted points (1e-9 tolerance).",
"Iterative maximum likelihood: 1e-7 absolute or 1e-6 relative elsewhere."
)
emit("STAT-SURV-DISTID-001",
"Distribution identification of complete data across the seven families",
DISTID, "lifeDistributionIdMenuItem", DISTID_REQ, "SURV_COMPLETE_B9",
list(layout = "arbitrary", start = "time", end = "time", frequency = "freq",
families = as.list(ALL7), threshold = 0.0, confidence_level = 0.95),
c(list("/plotted_points" = exact(sum(cd$freq), "frequencies expand the ranks")),
ident_complete(ALL7, 0.0, 0.95)),
DISTID_NOTES, "nominal")
# Censored identification on SURV_KM_B9: KM midpoints and the restricted-domain AD.
ident_censored <- function(families, threshold, level) {
km <- survfit(Surv(time, status) ~ 1, data = kmd, weights = kmd$freq,
conf.type = "log-log", conf.int = level)
s <- km_frame(km)
s <- s[s$n.event > 0, ]
times <- s$time
before <- c(1, s$surv[-length(s$surv)])
probabilities <- 1 - (before + s$surv) / 2
last_obs <- max(kmd$time)
out <- list()
for (index in seq_along(families)) {
family <- families[index]
fit <- survreg(Surv(time, status) ~ 1, data = kmd, weights = kmd$freq,
dist = SURV_DIST[[family]])
base <- paste0("/families/", index - 1)
u <- family_cdf(family, fit, times, threshold)
# identified pieces: F_n = 1 - S(t_{i-1}) is constant over (t_{i-1}, t_i]; the
# integration stops at the largest observation (right censoring)
before_levels <- c(0, 1 - s$surv[-length(s$surv)])
statistic <- 0
for (i in seq_along(u)) {
u1 <- if (i == 1) 0 else u[i - 1]
statistic <- statistic + ad_piece(before_levels[i], u1, u[i])
}
u_end <- family_cdf(family, fit, last_obs, threshold)
statistic <- statistic + ad_piece(1 - s$surv[length(s$surv)], u[length(u)], u_end)
statistic <- sum(kmd$freq) * statistic
domain_probability <- u_end # the domain runs to the largest observation
y <- if (family %in% POSITIVE) log(times - threshold) else times
w <- vapply(probabilities, function(p) standard_quantile(family, p), 0)
out <- c(out, family_params_outputs(fit, family, threshold, level,
prefix = paste0(base, "/parameters")))
out[[paste0(base, "/log_likelihood")]] <- tol(fit$loglik[2], "weighted log-likelihood")
out[[paste0(base, "/anderson_darling")]] <- tol(statistic, "restricted-domain AD discrepancy: the exact piecewise integral of (F_n - F)^2/(F(1-F)) dF over the identified region (up to the largest observation)")
out[[paste0(base, "/ad_domain_probability")]] <- tol(domain_probability, "fitted probability the domain covers")
out[[paste0(base, "/correlation")]] <- tol(stats::cor(y, w), "correlation of the KM midpoint positions against the standard quantiles")
out <- c(out, percentile_outputs(fit, family, c(1, 5, 10, 50), threshold, level,
prefix = paste0(base, "/percentiles")))
}
out
}
km_failure_times <- function() {
fit <- survfit(Surv(time, status) ~ 1, data = kmd, weights = kmd$freq, conf.type = "log-log")
sum(km_frame(fit)$n.event > 0)
}
emit("STAT-SURV-DISTID-002",
"Distribution identification of right-censored data with the restricted-domain AD",
DISTID, "lifeDistributionIdMenuItem", DISTID_REQ, "SURV_KM_B9",
list(layout = "right_censored", start = "time", status = "status", frequency = "freq",
families = as.list(ALL7), threshold = 0.0, confidence_level = 0.95),
c(list("/plotted_points" = exact(km_failure_times(), "one point per failure time")),
ident_censored(ALL7, 0.0, 0.95)),
c(DISTID_NOTES,
paste0(MANUAL, ":56-62: under right censoring the discrepancy integrates only to the largest observation; the covered fitted probability is reported and no p-value is given."),
"Plotting positions are the Kaplan-Meier midpoints (F(t-) + F(t))/2 at failure times."), "axis_coverage")
# =======================================================================================
# Regression with life data.
REG <- "tabelz.analysis.life_regression:compute_life_regression"
REG_REQ <- "REQ-STAT-SURV-LIFE-REGRESSION-001"
REG_NOTES <- c(
paste0(MANUAL, ":64-79: log(T - gamma) = x'beta + sigma W with common dispersion; Weibull shape 1/sigma; percentile intervals use the full fitted covariance including the dispersion cross terms."),
"R: survreg(Surv(start, end, type = 'interval2') ~ predictors, weights = freq); factor columns use contr.sum on sorted levels, matching the engine's sum-coded design (level order is alphabetical; the last level is the -1 reference).",
"Iterative maximum likelihood: 1e-7 absolute or 1e-6 relative."
)
regression_case <- function(id, title, family, threshold, numerics, factors, transform,
transform_column, temperature_unit, prediction_rows, percents,
level, notes, kind, ci_atol = "1e-7") {
formula_rhs <- paste(c(factors, numerics), collapse = " + ")
formula <- stats::as.formula(paste("Surv(start, end, type = 'interval2') ~", formula_rhs))
data <- d
if (transform != "none") {
if (transform == "arrhenius") {
kelvin <- data[[transform_column]] + if (temperature_unit == "celsius") 273.15 else 0
data[[transform_column]] <- 1 / kelvin
} else {
data[[transform_column]] <- log(data[[transform_column]])
}
}
old <- options(contrasts = c(unordered = "contr.sum", ordered = "contr.sum"))
on.exit(options(old), add = TRUE)
for (name in factors) {
data[[name]] <- factor(data[[name]], levels = sort(unique(data[[name]])))
}
fit <- survreg(formula, data = data, weights = d$freq, dist = SURV_DIST[[family]])
v <- vcov(fit)
z <- stats::qnorm(0.5 + level / 2)
coef_names <- names(coef(fit))
outputs <- list()
for (i in seq_along(coef_names)) {
base <- paste0("/coefficients/", i - 1)
estimate <- unname(coef(fit)[i])
error <- sqrt(v[i, i])
outputs[[paste0(base, "/estimate")]] <- tol(estimate, paste("survreg coefficient", coef_names[i]))
outputs[[paste0(base, "/standard_error")]] <- tol(error, "observed information")
outputs[[paste0(base, "/z_value")]] <- tol(estimate / error, "estimate / SE")
outputs[[paste0(base, "/lower")]] <- tol(estimate - z * error, "Wald limit", "1e-6", ci_atol)
outputs[[paste0(base, "/upper")]] <- tol(estimate + z * error, "Wald limit", "1e-6", ci_atol)
}
if (family != "exponential") {
sigma <- fit$scale
error <- sqrt(v[ncol(v), ncol(v)])
outputs <- c(outputs, list(
"/dispersion/estimate" = tol(sigma, "survreg scale"),
"/dispersion/standard_error" = tol(sigma * error, "sigma * SE(log sigma)"),
"/dispersion/z_value" = tol(log(sigma) / error, "log sigma / SE(log sigma)"),
"/dispersion/lower" = tol(exp(log(sigma) - z * error), "log-sigma interval"),
"/dispersion/upper" = tol(exp(log(sigma) + z * error), "log-sigma interval")))
if (family == "weibull") {
outputs <- c(outputs, list(
"/shape/estimate" = tol(1 / sigma, "1 / sigma"),
"/shape/standard_error" = tol(error / sigma, "SE(log sigma) / sigma"),
"/shape/z_value" = tol(-log(sigma) / error, "-log sigma / SE(log sigma)"),
"/shape/lower" = tol(exp(-log(sigma) - z * error), "shape interval"),
"/shape/upper" = tol(exp(-log(sigma) + z * error), "shape interval")))
}
}
outputs[["/log_likelihood"]] <- tol(fit$loglik[2], "weighted log-likelihood")
for (i in seq_along(prediction_rows)) {
setting <- prediction_rows[i]
values <- strsplit(setting, " +")[[1]]
pairs <- strsplit(values, "=")
newdata <- d[1, c("start", "end", numerics, factors), drop = FALSE]
for (name in names(newdata)) newdata[[name]] <- newdata[[name]][1]
row_index <- 1
for (pair in pairs) {
if (pair[1] %in% factors) {
newdata[[pair[1]]] <- factor(pair[2], levels = levels(data[[pair[1]]]))
} else {
newdata[[pair[1]]] <- as.numeric(pair[2])
}
}
for (name in factors) {
if (!any(vapply(pairs, function(p) p[1] == name, TRUE))) {
newdata[[name]] <- factor(data[[name]][row_index], levels = levels(data[[name]]))
}
}
if (transform != "none" && transform_column %in% names(newdata)) {
if (transform == "arrhenius") {
newdata[[transform_column]] <- 1 / (newdata[[transform_column]] +
if (temperature_unit == "celsius") 273.15 else 0)
} else {
newdata[[transform_column]] <- log(newdata[[transform_column]])
}
}
q <- predict(fit, newdata = newdata, type = "quantile", p = sort(percents) / 100,
se.fit = TRUE)
for (j in seq_along(percents)) {
base <- paste0("/predictions/", (i - 1) * length(percents) + (j - 1))
estimate <- q$fit[j] + threshold
error <- q$se.fit[j]
if (family %in% POSITIVE) {
spread <- z * q$se.fit[j] / q$fit[j]
lower <- threshold + exp(log(q$fit[j]) - spread)
upper <- threshold + exp(log(q$fit[j]) + spread)
} else {
lower <- estimate - z * error
upper <- estimate + z * error
}
outputs[[paste0(base, "/estimate")]] <- tol(estimate, "survreg quantile prediction")
outputs[[paste0(base, "/standard_error")]] <- tol(error, "delta-method SE")
outputs[[paste0(base, "/lower")]] <- tol(lower, "transformed log-scale Wald limit")
outputs[[paste0(base, "/upper")]] <- tol(upper, "transformed log-scale Wald limit")
}
}
outputs <- c(counts_outputs(d, "arbitrary"), outputs)
config <- c(ARBITRARY, list(
family = family, threshold = threshold,
numeric_predictors = as.list(numerics), categorical_predictors = as.list(factors),
transform = transform, transform_column = transform_column,
temperature_unit = temperature_unit,
prediction_rows = as.list(prediction_rows),
percents = as.list(percents), confidence_level = level))
emit(id, title, REG, "lifeRegressionMenuItem", REG_REQ, "SURV_LIFE_B9", config, outputs,
notes, kind)
}
regression_case("STAT-SURV-LIFEREG-001",
"Weibull life regression on a numeric stress predictor with predictions",
"weibull", 0.0, "stress", character(0), "none", NULL, "celsius",
c("stress=80", "stress=150"), c(10, 50), 0.95, REG_NOTES, "nominal")
regression_case("STAT-SURV-LIFEREG-002",
"Lognormal life regression with a sum-coded three-level factor",
"lognormal", 0.0, character(0), "temp3", "none", NULL, "celsius",
c("temp3=low", "temp3=high"), c(10, 50), 0.95,
c(REG_NOTES,
"contr.sum on sorted levels high, low, mid: the engine's grp[sum:high] and grp[sum:low] are R's temp31 and temp32 (mid is the -1 reference)."), "axis_coverage")
regression_case("STAT-SURV-LIFEREG-003",
"SEV life regression with the Arrhenius temperature preset at 90%",
"sev", 0.0, "temp_c", character(0), "arrhenius", "temp_c", "celsius",
c("temp_c=45", "temp_c=75"), c(10, 90), 0.90,
c(REG_NOTES,
"The Arrhenius preset maps Celsius to 1/(temp_c + 273.15); predictions are entered in Celsius and transformed identically.",
"The Arrhenius coefficient is about -2.1e5 on the 1/K covariate and its z is -1.65, so each Wald bound is the small difference of two O(2e5) quantities: last-digit round-off in the estimate or its SE (each within 1e-6 relative of itself) moves the bound by up to a few 1e-3 absolute. The coefficient interval outputs therefore carry a 5e-2 absolute tolerance alongside 1e-6 relative; estimate, SE and z stay at the default."),
"axis_coverage", ci_atol = "5e-2")
cat("survival life-family references generated\n")
generators/r/survival_life_tables.R
source("common.R", local = TRUE)
# Actuarial life tables (Statistics > Survival > Life tables): the Cutler-Ederer table, the
# Greenwood standard errors, the midpoint hazard and density, the actuarial median, the grouped
# Gehan (Lee-Desu) comparison and the Prentice-Gloeckler grouped PH score test, written by
# hand in base R from docs/manual/chapters/93-grouped-and-changing-survival.tex and
# docs/manual/chapters/107-grouped-ph.tex.
exact_times <- data.frame(
time = c(0.8, 1.5, 2.0, 2.4, 3.1, 3.7, 4.0, 4.6, 5.2, 5.9, 6.3, 7.0, 7.8, 8.5, 9.4,
1.2, 1.9, 2.7, 3.3, 4.0, 4.9, 5.5, 6.0, 6.8, 7.5, 8.2, 9.0, 9.9, 10.6, 12.5),
status = c(1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L,
1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L),
group = rep(c("A", "B"), each = 15),
stringsAsFactors = FALSE
)
export_dataset(
exact_times, "SURV_LIFETAB", "Constructed by survival_life_tables.R",
"Cutler and Ederer (1958) J Chronic Dis 8:699-712; Lee and Desu (1972) Computer Programs in Biomedicine 2:315-321.",
"Constructed test data; no third-party dataset license.",
c(time = "Float64", status = "Int64", group = "String")
)
visits <- data.frame(
time = c(1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 2, 4, 6,
0, 1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 2, 3, 5),
status = c("event", "event", "event", "censored", "event", "event", "event", "event",
"censored", "event", "censored", "censored", "event", "event", "censored",
"censored", "event", "event", "event", "censored", "event", "censored", "event",
"event", "event", "censored", "censored", "censored", "event", "censored"),
group = rep(c("A", "B"), each = 15),
stringsAsFactors = FALSE
)
export_dataset(
visits, "SURV_LIFETAB_VISITS", "Constructed by survival_life_tables.R",
"Prentice and Gloeckler (1978) Biometrics 34:57-67 (grouped proportional hazards).",
"Constructed test data; no third-party dataset license.",
c(time = "Float64", status = "String", group = "String")
)
d <- read_dataset("SURV_LIFETAB")
dv <- read_dataset("SURV_LIFETAB_VISITS")
run <- generator_run(c("stats", "jsonlite"))
source_ref <- "Base R arithmetic of the actuarial, Gehan and grouped PH formulas"
citation <- paste(
"Cutler and Ederer (1958) J Chronic Dis 8:699-712; Greenwood (1926);",
"Lee and Desu (1972) Computer Programs in Biomedicine 2:315-321; Gehan (1965) Biometrika 52:203-223;",
"Prentice and Gloeckler (1978) Biometrics 34:57-67; R stats: pchisq, glm (cloglog cross-check), anova(test = 'Rao')."
)
tol <- function(value, label, tier = "1e-9") {
list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = tier,
rationale = paste("Base R", label, "from the re-read CSV."))
}
prob <- function(value, label, tier = "1e-8") {
list(comparison = "tail_probability", value = dec(unname(value)), relative_tolerance = tier,
rationale = paste("Base R", label, "from the re-read CSV."))
}
exact <- function(value, label) list(comparison = "exact", value = value, rationale = label)
# --- actuarial table ----------------------------------------------------------------------
life_table <- function(time, event, edges) {
k <- length(edges) - 1
index <- findInterval(time, edges) # i: edges[i] <= t < edges[i + 1]; k + 1 is the open end
rows <- list()
entering <- length(time)
survival <- 1
greenwood <- 0
for (i in seq_len(k + 1)) {
if (entering == 0) break
d_i <- sum(index == i & event)
w_i <- sum(index == i & !event)
at_risk <- entering - w_i / 2
q <- d_i / at_risk
p <- 1 - q
terminal <- i == k + 1
width <- if (terminal) NA else edges[i + 1] - edges[i]
row <- list(entering = entering, withdrawn = w_i, events = d_i, at_risk = at_risk, q = q, p = p,
survival_start = survival, survival_start_se = survival * sqrt(greenwood),
survival_end = if (terminal) NA else survival * p,
survival_end_se = if (terminal || p <= 0) NA else survival * p * sqrt(greenwood + q / (at_risk * p)),
hazard = if (terminal) NA else 2 * q / (width * (1 + p)),
hazard_se = NA, density = if (terminal) NA else survival * q / width, density_se = NA,
start = edges[i], width = width)
if (!terminal && q > 0 && q < 1) {
row$hazard_se <- row$hazard * sqrt((1 - (width * row$hazard / 2)^2) / (at_risk * q))
row$density_se <- row$density * sqrt(greenwood + p / (at_risk * q))
}
rows[[i]] <- row
survival <- survival * p
greenwood <- if (p > 0) greenwood + q / (at_risk * p) else NA
entering <- entering - d_i - w_i
}
rows
}
actuarial_median <- function(rows) {
for (row in rows) {
if (is.na(row$survival_end) || row$survival_end > 0.5) next
return(row$start + row$width * (row$survival_start - 0.5) / (row$survival_start - row$survival_end))
}
NA
}
gehan <- function(grouped, event, strata) {
n <- length(grouped)
score <- numeric(n)
for (a in seq_len(n)) for (b in seq_len(n)) {
if (a == b) next
if (grouped[a] > grouped[b] && event[b]) score[a] <- score[a] + 1
else if (grouped[a] < grouped[b] && event[a]) score[a] <- score[a] - 1
else if (grouped[a] == grouped[b]) {
if (event[b] && !event[a]) score[a] <- score[a] + 1
else if (event[a] && !event[b]) score[a] <- score[a] - 1
}
}
stopifnot(sum(score) == 0)
labels <- unique(strata)
sums <- vapply(labels, function(l) sum(score[strata == l]), 0)
sizes <- vapply(labels, function(l) sum(strata == l), 0)
statistic <- (n - 1) * sum(sums^2 / sizes) / sum(score^2)
list(statistic = statistic, df = length(labels) - 1,
p = stats::pchisq(statistic, length(labels) - 1, lower.tail = FALSE),
sums = sums, means = sums / sizes, labels = labels)
}
grouped_ph <- function(visit, event, group, k) {
labels <- unique(group)
G <- length(labels)
r <- matrix(0, G, k); dd <- matrix(0, G, k)
for (g in seq_len(G)) for (j in seq_len(k)) {
r[g, j] <- sum(group == labels[g] & visit >= j)
dd[g, j] <- sum(group == labels[g] & visit == j & event)
}
U <- numeric(G); I <- matrix(0, G, G)
for (j in seq_len(k)) {
rj <- sum(r[, j]); q <- sum(dd[, j]) / rj
stopifnot(q > 0, q < 1)
a <- -log(1 - q)
U <- U + (a / q) * (dd[, j] - r[, j] * q)
I <- I + a^2 * (1 - q) / q * (diag(r[, j], G) - outer(r[, j], r[, j]) / rj)
}
reduced <- I[seq_len(G - 1), seq_len(G - 1), drop = FALSE]
statistic <- drop(t(U[seq_len(G - 1)]) %*% solve(reduced, U[seq_len(G - 1)]))
# Independent cross-check: the Rao score test of the group term in the complementary
# log-log binomial model with one intercept per visit interval (Prentice-Gloeckler 1978).
long <- do.call(rbind, lapply(seq_len(G), function(g) data.frame(
j = factor(seq_len(k)), group = labels[g], d = dd[g, ], n = r[g, ] - dd[g, ])))
null <- stats::glm(cbind(d, n) ~ j, family = stats::binomial("cloglog"), data = long)
full <- stats::glm(cbind(d, n) ~ j + group, family = stats::binomial("cloglog"), data = long)
X <- stats::model.matrix(full)
mu <- stats::fitted(null)
dmu <- stats::binomial("cloglog")$mu.eta(stats::predict(null))
variance <- mu * (1 - mu) / null$prior.weights
score <- t(X) %*% ((long$d / (long$d + long$n) - mu) * dmu / variance)
information <- t(X) %*% (X * as.vector(dmu^2 / variance))
glm_score <- drop(t(score) %*% solve(information, score))
stopifnot(abs(glm_score - statistic) < 1e-9 * statistic)
rao <- stats::anova(null, full, test = "Rao")$Rao[2]
stopifnot(abs(rao - statistic) < 1e-3 * statistic) # anova's one-step Rao agrees to display precision
list(statistic = statistic, df = G - 1, p = stats::pchisq(statistic, G - 1, lower.tail = FALSE),
scores = U, information = reduced, labels = labels, r = r, d = dd)
}
emit <- function(id, title, kind, dataset, configuration, outputs, notes) {
expected <- list(schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run)
write_expected("survival", id, expected)
case <- list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = "survival",
entry_point = "tabelz.analysis.life_tables:compute_life_table",
menu_items = list("lifeTableMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/survival/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation", source = source_ref,
citation = citation,
generator = list(path = "generators/r/survival_life_tables.R", sha256 = ""),
independent = TRUE,
convention_notes = c(
notes, "Expected computed from the exported CSV.",
"docs/manual/chapters/93-grouped-and-changing-survival.tex:9-22: intervals closed on the left, open on the right; n' = n - w/2, q = d/n', p = 1 - q, S = prod p with Greenwood SE; midpoint hazard 2q/[b(1 + p)] and density S_{i-1} q/b.",
"docs/manual/chapters/93-grouped-and-changing-survival.tex:38-51: open last interval has no hazard or density; Gehan (Lee-Desu) on interval starts, event before withdrawal at a tie, (N - 1) sum U_j^2/n_j / sum S_i^2 on g - 1 df.",
"docs/manual/chapters/93-grouped-and-changing-survival.tex:53-58: actuarial median by linear interpolation in the first finite interval whose end survival reaches one half."
)),
supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-SURV-LIFE_TABLE-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL
)
write_case("survival", id, case)
}
row_outputs <- function(rows, base, indices, tier = "1e-9") {
out <- list()
for (i in indices) {
row <- rows[[i]]
at <- paste0(base, "/rows/", i - 1)
label <- sprintf("interval starting at %g", row$start)
out[[paste0(at, "/entering")]] <- exact(as.integer(row$entering), paste("entering,", label))
out[[paste0(at, "/withdrawn")]] <- exact(as.integer(row$withdrawn), paste("withdrawn,", label))
out[[paste0(at, "/events")]] <- exact(as.integer(row$events), paste("events,", label))
out[[paste0(at, "/at_risk")]] <- tol(row$at_risk, paste("n - w/2,", label), tier)
out[[paste0(at, "/q")]] <- tol(row$q, paste("conditional failure probability,", label), tier)
out[[paste0(at, "/survival_start")]] <- tol(row$survival_start, paste("survival at the start,", label), tier)
if (row$survival_start_se > 0) out[[paste0(at, "/survival_start_se")]] <- tol(row$survival_start_se, paste("Greenwood SE at the start,", label), tier)
if (!is.na(row$survival_end)) {
out[[paste0(at, "/survival_end")]] <- tol(row$survival_end, paste("survival at the end,", label), tier)
out[[paste0(at, "/survival_end_se")]] <- tol(row$survival_end_se, paste("Greenwood SE at the end,", label), tier)
out[[paste0(at, "/hazard")]] <- tol(row$hazard, paste("midpoint hazard,", label), tier)
out[[paste0(at, "/density")]] <- tol(row$density, paste("midpoint density,", label), tier)
if (!is.na(row$hazard_se)) {
out[[paste0(at, "/hazard_se")]] <- tol(row$hazard_se, paste("hazard SE,", label), tier)
out[[paste0(at, "/density_se")]] <- tol(row$density_se, paste("density SE,", label), tier)
}
}
}
out
}
# --- case 001: equal widths, one stratum ------------------------------------------------
edges1 <- seq(0, 10, by = 2)
event <- d$status == 1L
rows1 <- life_table(d$time, event, edges1)
stopifnot(length(rows1) == 6L)
outputs <- c(
list("/analyzed_count" = exact(nrow(d), "30 rows analysed"),
"/strata/0/subjects" = exact(nrow(d), "30 subjects"),
"/strata/0/events" = exact(as.integer(sum(event)), "events"),
"/strata/0/withdrawn" = exact(as.integer(sum(!event)), "withdrawals"),
"/strata/0/median/status" = exact("estimated", "the median lies in a finite interval"),
"/strata/0/median/value" = tol(actuarial_median(rows1), "actuarial median by linear interpolation")),
row_outputs(rows1, "/strata/0", 1:6)
)
emit("STAT-SURV-LIFETAB-001", "Actuarial life table with 2-unit intervals to 10", "nominal",
"SURV_LIFETAB", list(time = "time", status = "status", event_value = "1", censored_value = "0",
interval_width = 2, last_boundary = 10),
outputs,
list("Edges 0, 2, ..., 10 and the open interval [10, inf): findInterval places an event at exactly 2.0 and 4.0 in the interval that starts there.",
"The open terminal row reports entering, withdrawn, events, n - w/2, q and the survival at its start only."))
# --- case 002: unequal boundaries, two strata, Gehan comparison -------------------------
edges2 <- c(0, 1, 3, 6, 12)
outputs <- list()
for (level in c("A", "B")) {
rows <- life_table(d$time[d$group == level], event[d$group == level], edges2)
base <- paste0("/strata/[stratum=", level, "]")
outputs[[paste0(base, "/subjects")]] <- exact(as.integer(sum(d$group == level)), paste("subjects in", level))
outputs[[paste0(base, "/events")]] <- exact(as.integer(sum(event[d$group == level])), paste("events in", level))
med <- actuarial_median(rows)
if (!is.na(med)) outputs[[paste0(base, "/median/value")]] <- tol(med, paste("actuarial median of", level))
outputs <- c(outputs, row_outputs(rows, base, seq_along(rows)))
}
grouped <- edges2[findInterval(d$time, edges2)]
cmp <- gehan(grouped, event, d$group)
outputs[["/comparison/statistic"]] <- tol(cmp$statistic, "Lee-Desu chi-square on grouped times")
outputs[["/comparison/df"]] <- exact(as.integer(cmp$df), "strata minus one")
outputs[["/comparison/p_value"]] <- prob(cmp$p, "Lee-Desu chi-square tail")
outputs[["/comparison/strata/[stratum=A]/score_sum"]] <- tol(cmp$sums[["A"]], "Gehan score sum of stratum A")
outputs[["/comparison/strata/[stratum=B]/mean_score"]] <- tol(cmp$means[["B"]], "mean Gehan score of stratum B")
emit("STAT-SURV-LIFETAB-002", "Unequal boundaries with two strata and the grouped Gehan test", "axis_coverage",
"SURV_LIFETAB", list(time = "time", status = "status", event_value = "1", censored_value = "0",
boundaries = list(0, 1, 3, 6, 12), strata = "group"),
outputs,
list("Explicit boundaries 0, 1, 3, 6, 12; each stratum gets its own table and median.",
"Gehan scores by the pairwise definition on interval starts (open interval -> 12): case a scores +1 against an uncensored b with an earlier grouped time, or an uncensored b at the same grouped time when a is censored; -1 symmetrically; the scores sum to zero (asserted)."))
# --- case 003: scheduled visits, text status values, grouped PH score test ---------------
edges3 <- 0:6
eventv <- dv$status == "event"
outputs <- list()
rows_a <- life_table(dv$time[dv$group == "A"], eventv[dv$group == "A"], edges3)
outputs <- c(outputs, row_outputs(rows_a, "/strata/[stratum=A]", 1:3))
outputs[["/strata/[stratum=B]/rows/0/withdrawn"]] <- exact(1L, "the subject censored at the origin is withdrawn in [0, 1)")
ph <- grouped_ph(dv$time, eventv, dv$group, length(edges3) - 1)
outputs[["/grouped_ph/statistic"]] <- tol(ph$statistic, "Prentice-Gloeckler score statistic U' I^-1 U")
outputs[["/grouped_ph/df"]] <- exact(as.integer(ph$df), "groups minus one")
outputs[["/grouped_ph/p_value"]] <- prob(ph$p, "grouped PH chi-square tail")
outputs[["/grouped_ph/groups/[group=A]/score"]] <- tol(ph$scores[1], "score of group A")
outputs[["/grouped_ph/groups/[group=B]/lost_before_first_visit"]] <- exact(1L, "one subject of B censored at the origin")
outputs[["/grouped_ph/information/0/0"]] <- tol(ph$information[1, 1], "information of the first group coordinate")
outputs[["/grouped_ph/zero_information_intervals"]] <- exact(0L, "every visit interval has 0 < q < 1")
outputs[["/grouped_ph/intervals/0/at_risk/1"]] <- exact(as.integer(ph$r[2, 1]), "B at risk in (0, 1]: 14 (the origin-censored subject is not)")
outputs[["/grouped_ph/intervals/2/events/0"]] <- exact(as.integer(ph$d[1, 3]), "A events detected at visit 3")
cmp3 <- gehan(edges3[findInterval(dv$time, edges3)], eventv, dv$group)
outputs[["/comparison/statistic"]] <- tol(cmp3$statistic, "Lee-Desu chi-square on the visit times")
emit("STAT-SURV-LIFETAB-003", "Scheduled visits with text status values and the grouped PH score test", "axis_coverage",
"SURV_LIFETAB_VISITS", list(time = "time", status = "status", event_value = "event",
censored_value = "censored", boundaries = as.list(as.numeric(edges3)),
strata = "group", observation_model = "scheduled_visits",
grouped_ph_test = TRUE),
outputs,
list("Status values are the texts 'event' and 'censored'.",
"Grouped PH score test (docs/manual/chapters/107-grouped-ph.tex): r[g, j] subjects of group g with visit index m >= j, d[g, j] events detected at visit j; under H0 q_j = d_j/r_j, a_j = -log(1 - q_j); U[g] = sum_j (a_j/q_j)(d[g, j] - r[g, j] q_j), I[g, h] = sum_j a_j^2 (1 - q_j)/q_j (delta r[g, j] - r[g, j] r[h, j]/r_j); Q = U' I^-1 U on the first G - 1 coordinates.",
"The by-hand statistic is cross-checked in the generator against the expected-information score test of the group term computed from the null complementary log-log binomial GLM fit (one intercept per visit interval; glm fitted values, mu.eta and model matrix) to 1e-9 relative, and against anova(test = 'Rao') to 1e-3; the generator stops if either differs more."))
cat("life table cases generated\n")
generators/r/survival_minimum.R
source("common.R",local=TRUE)
raw <- data.frame(time=c(1,2,3,4,5),status=c(1L,0L,1L,1L,0L),
stress=c(20,30,40,50,60),x=c(0,1,1,0,0.5),
response=c(100.1,99.55,99.09,98.61,98.12))
export_dataset(raw,"SURV_MIN","Constructed small censored-design boundary matrix",
"R survival::survfit/coxph and analytic minimum-count checks",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64",response="Float64"))
d <- read_dataset("SURV_MIN")
inter <- read_dataset("SURV_INTERVAL")
run <- generator_run(c("survival","stats","jsonlite"))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
tol <- function(x,note,tier="1e-6")
list(comparison="tolerance",value=dec(unname(x)),absolute_tolerance="0",relative_tolerance=tier,rationale=note)
emit <- function(id,entry,menu,dataset,excluded,config,outputs,notes) {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Small-sample survival boundary",id),
case_kind="boundary",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=as.list(as.integer(excluded)),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R survival/stats and analytic counts",
citation="R survival::survfit/coxph and Surv(type=interval2) documentation",
generator=list(path="generators/r/survival_minimum.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV and drops excluded 0-based ordinals.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
cfg <- list(time_column="time",event_column="status",event_value="1",censored_value="0")
emit("STAT-SURV-KM-MIN-001","tabelz.executor.survival","survivalMenuItem",
"SURV_MIN",1:4,cfg,
list("/analyzed_count"=exact(1L,"One observed failure is the smallest KM sample"),
"/curves/0/survival/0"=tol(0,"Single event reduces survival to zero","1e-9")),
c("R survfit on the first row has one event at time 1 and S(1)=0."))
emit("STAT-SURV-RISK-MIN-001","tabelz.analysis.survival_nonparametric:number_at_risk",
"survivalMenuItem","SURV_MIN",1:4,c(cfg,list(times=list(0,1,2))),
list("/0"=exact(1L,"One row at risk before time 1"),
"/1"=exact(1L,"Event row is still at risk at time 1"),
"/2"=exact(0L,"No observation at or after time 2")),
c("At-risk count includes a failure at the requested instant."))
cox <- survival::coxph(survival::Surv(time,status)~x,data=d,ties="efron")
emit("STAT-SURV-COX-MIN-001","tabelz.executor.cox_regression","coxRegressionMenuItem",
"SURV_MIN",integer(),
list(duration_column="time",event_column="status",event_value="1",censored_value="0",
predictors=list("x")),
list("/analyzed_count"=exact(nrow(d),"Five identifiable rows"),
"/coefficients/0/log_hazard"=tol(coef(cox)[[1]],"R Efron small-sample log hazard")),
c("Five rows with three failures avoid complete separation; Efron ties pinned."))
emit("STAT-SURV-FAMILIES-MIN-001","tabelz.executor.reliability",
"reliabilityFamiliesMenuItem","SURV_MIN",3:4,
list(mode="families",time_column="time",event_column="status"),
list("/total_n"=exact(3L,"Minimum three retained lifetimes"),
"/failures"=exact(2L,"Minimum two observed failures"),
"/fits/[family=exponential]/param1_val"=tol(sum(d$time[1:3])/sum(d$status[1:3]),
"Analytic exponential total-time/failure scale","1e-9")),
c("Three rows and two failures meet censored-families minimum."))
emit("STAT-SURV-ALT-MIN-001","tabelz.executor.reliability","acceleratedLifeMenuItem",
"SURV_MIN",integer(),
list(mode="accelerated",time_column="time",event_column="status",stress_column="stress",
stress_type="linear",stress_unit="native",use_stress=40,high_stress=60),
list("/total_n"=exact(5L,"Minimum five stress/lifetime rows"),
"/failures"=exact(3L,"Minimum three failures")),
c("Five rows, three events and five stress levels meet physical ALT minimum."))
emit("STAT-SURV-WEIBULL-MIN-001","tabelz.analysis.weibull_aft:compute_weibull_aft",
"weibullAftMenuItem","SURV_MIN",integer(),
list(time_column="time",event_column="status",stress_column="stress"),
list("/failure_count"=exact(3L,"Minimum three observed failures"),
"/analyzed_count"=exact(5L,"Five complete rows")),
c("Three failures and varying stress meet Weibull AFT sample condition."))
emit("STAT-SURV-STABILITY-MIN-001","tabelz.analysis.product_stability:compute_product_stability",
"productStabilityMenuItem","SURV_MIN",3:4,
list(time_column="time",response_column="response",lsl=95),
list("/n"=exact(3L,"Three rows leave one residual degree of freedom"),
"/coefficients/1/estimate"=tol(coef(stats::lm(response~time,data=d[1:3,]))[[2]],
"Three-row OLS slope","1e-9")),
c("Three distinct times are the smallest two-parameter OLS sample with residual df."))
valid_int <- inter[1:3,,drop=FALSE]
emit("STAT-SURV-INTERVAL-MIN-001","tabelz.executor.reliability","intervalLifeMenuItem",
"SURV_INTERVAL",3:19,
list(mode="interval",lower_column="lower",upper_column="upper"),
list("/total_n"=exact(nrow(valid_int),"Minimum three retained interval records"),
"/exact_count"=exact(1L,"One exact failure"),
"/left_count"=exact(1L,"One left-censored failure-indicative row"),
"/right_count"=exact(1L,"One right-censored row")),
c("Exact plus left censoring supplies two failure-indicative records."))
cat("survival minimum references generated\n")
generators/r/survival_options.R
source("common.R",local=TRUE)
run <- generator_run(c("survival","stats","jsonlite"))
tol <- function(x,note,tier="1e-6")
list(comparison="tolerance",value=dec(unname(x)),absolute_tolerance="0",
relative_tolerance=tier,rationale=paste("R survival option reference:",note))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,notes,error=NULL,kind="nominal") {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=error,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Survival option",id),
case_kind=kind,dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class=if(is.null(error)) "independent_implementation" else "analytic",
source="R survival/stats",
citation="R survival::survfit/coxph/survreg and stats::lm documentation",
generator=list(path="generators/r/survival_options.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the referenced CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
mp <- read_dataset("HARVEST_GEHAN_MP")
km90 <- survival::survfit(survival::Surv(time,status)~1,data=mp,
conf.type="log-log",conf.int=0.9)
emit("STAT-SURV-KM-CONFIDENCE-001","tabelz.executor.survival","survivalMenuItem",
"HARVEST_GEHAN_MP",list(time_column="time",event_column="status",event_value="1",
censored_value="0",confidence_level=0.9),
list("/curves/0/lower/0"=tol(km90$lower[1],"90% Greenwood log-log lower", "1e-9"),
"/curves/0/upper/0"=tol(km90$upper[1],"90% Greenwood log-log upper", "1e-9")),
c("R survfit conf.type=log-log and conf.int=0.9; pointwise, not simultaneous."))
d <- read_dataset("SURV_CORE")
cox <- survival::coxph(survival::Surv(time,status)~x,data=d,ties="efron")
lower95 <- exp(coef(cox)[[1]]-stats::qnorm(0.975)*sqrt(vcov(cox)[1,1]))
cox_config <- list(duration_column="time",event_column="status",event_value="1",censored_value="0",
predictors=list("x"),confidence_level=0.9,max_iterations=300L,max_diagnostic_rows=100L)
# Manual chapter 14 fixes Cox intervals at 95%; another level is refused.
emit("STAT-SURV-COX-CONFIDENCE-001","tabelz.executor.cox_regression",
"coxRegressionMenuItem","SURV_CORE",cox_config,structure(list(),names=character()),
c("Manual chapter 14: fixed 95% asymptotic intervals; a 90% request must be refused.",
"Until 2026-09-25 this case wrongly expected a 90% interval; -095-001 checks the 95% interval."),
error=list(error_class="ValidationError",message_contains="fixed at 95%"),kind="invalid_input")
cox_config$confidence_level <- 0.95
emit("STAT-SURV-COX-CONFIDENCE-095-001","tabelz.executor.cox_regression",
"coxRegressionMenuItem","SURV_CORE",cox_config,
list("/coefficients/0/hazard_ratio_ci_lower"=tol(lower95,"95% Wald hazard-ratio lower"),
"/analyzed_count"=exact(nrow(d),"All finite rows")),
c("R coxph Efron ties; 95% Wald interval exp(beta-z_0.975*SE)."))
stress <- d$stress
for (mode in c("arrhenius","inverse_power")) {
x <- if(mode=="arrhenius") 1/(stress+273.15) else log(stress)
fitted <- survival::survreg(survival::Surv(d$time,d$status)~x,dist="weibull",
control=survival::survreg.control(rel.tolerance=1e-14,maxiter=1000))
use <- if(mode=="arrhenius") 1/(25+273.15) else log(50)
median <- exp(coef(fitted)[[1]]+coef(fitted)[[2]]*use)*(log(2))^fitted$scale
emit(paste0("STAT-SURV-ALT-",gsub("_","-",toupper(mode)),"-001"),
"tabelz.executor.reliability","acceleratedLifeMenuItem","SURV_CORE",
list(mode="accelerated",time_column="time",event_column="status",stress_column="stress",
stress_type=mode,stress_unit=if(mode=="arrhenius") "celsius" else "native",
use_stress=if(mode=="arrhenius") 25 else 50,high_stress=80,confidence_level=0.9),
list("/fits/[family=weibull]/median_use"=tol(median,paste(mode,"Weibull use-stress median"))),
c("R Weibull survreg scale=1/shape; stress transform is 1/(C+273.15) or log(S).",
"Median=exp(beta0+beta1*transformed use)*(log 2)^(survreg scale)."))
}
s <- read_dataset("SURV_STABILITY")
intercepts <- stats::lm(response~0+batch+time,data=s)
emit("STAT-SURV-STABILITY-INTERCEPTS-001",
"tabelz.analysis.product_stability:compute_product_stability",
"productStabilityMenuItem","SURV_STABILITY",
list(time_column="time",response_column="response",batch_column="batch",
model_type="separate_intercepts_pooled_slope",confidence_level=0.9,lsl=95),
list("/coefficients/3/estimate"=tol(coef(intercepts)[[4]],"Pooled time slope","1e-9"),
"/n"=exact(nrow(s),"All stability measurements")),
c("R 0+batch+time has separate batch intercepts and one pooled slope."))
slopes <- stats::lm(response~0+batch+batch:time,data=s)
emit("STAT-SURV-STABILITY-SLOPES-001",
"tabelz.analysis.product_stability:compute_product_stability",
"productStabilityMenuItem","SURV_STABILITY",
list(time_column="time",response_column="response",batch_column="batch",
model_type="separate_intercepts_separate_slopes",lsl=95),
list("/coefficients/3/estimate"=tol(coef(slopes)[[4]],"Batch A time slope","1e-9"),
"/n"=exact(nrow(s),"All stability measurements")),
c("R 0+batch+batch:time has separate intercepts and slopes per batch."))
cat("survival option references generated\n")
generators/r/survival_parametric.R
source("common.R",local=TRUE)
d <- read_dataset("SURV_CORE")
run <- generator_run(c("survival","stats","jsonlite"))
tol <- function(x,note,tier="1e-6")
list(comparison="tolerance",value=dec(unname(x)),absolute_tolerance="0",
relative_tolerance=tier,rationale=paste("R survreg reference:",note))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
emit <- function(id,entry,menu,dataset,config,outputs,notes) {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Parametric survival",id),
case_kind="nominal",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R survival::survreg",
citation="R survival::survreg and Surv(type=interval2) documentation",
generator=list(path="generators/r/survival_parametric.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
exp_scale <- sum(d$time)/sum(d$status)
emit("STAT-SURV-FAMILIES-001","tabelz.executor.reliability","reliabilityFamiliesMenuItem",
"SURV_CORE",list(mode="families",time_column="time",event_column="status"),
list("/total_n"=exact(nrow(d),"All finite right-censored observations"),
"/failures"=exact(sum(d$status),"Observed failures"),
"/fits/[family=exponential]/param1_val"=tol(exp_scale,
"Right-censored exponential scale=sum time on test/failures","1e-9")),
c("The exponential MLE is analytic; right-censored records contribute survival probability."))
i <- seq_len(20L)
life <- 1.4+0.29*i+0.12*sin(i*0.83)
lower <- upper <- life
for (index in i) {
mode <- (index-1L)%%4L
if (mode==1L) upper[index] <- NA_real_
if (mode==2L) lower[index] <- NA_real_
if (mode==3L) { lower[index] <- life[index]-0.22; upper[index] <- life[index]+0.22 }
}
interval <- data.frame(lower=lower,upper=upper)
export_dataset(interval,"SURV_INTERVAL","Constructed exact/right/left/interval lifetime matrix",
"R survival::Surv(type=interval2) and survreg exponential model",
"Constructed validation data; no third-party license.",
c(lower="Float64",upper="Float64"))
interval <- read_dataset("SURV_INTERVAL")
fit_int <- survival::survreg(survival::Surv(lower,upper,type="interval2")~1,
data=interval,dist="exponential",control=survival::survreg.control(maxiter=200L))
emit("STAT-SURV-INTERVAL-001","tabelz.executor.reliability","intervalLifeMenuItem",
"SURV_INTERVAL",list(mode="interval",lower_column="lower",upper_column="upper"),
list("/total_n"=exact(nrow(interval),"All four censoring types retained"),
"/exact_count"=exact(5L,"Five exact failures"),
"/right_count"=exact(5L,"Five right-censored rows"),
"/left_count"=exact(5L,"Five left-censored rows"),
"/interval_count"=exact(5L,"Five interval-censored rows"),
"/fits/[family=exponential]/param1_val"=tol(exp(coef(fit_int)[[1]]),
"Interval2 exponential scale MLE")),
c("R Surv(type=interval2): NA lower is left censoring, NA upper is right censoring.",
"survreg exponential intercept is log scale, so exp(intercept) is Tabelz scale."))
fit_aft <- survival::survreg(survival::Surv(time,status)~stress,data=d,dist="weibull",
control=survival::survreg.control(maxiter=200L))
median50 <- exp(coef(fit_aft)[[1]]+coef(fit_aft)[[2]]*50)*(log(2))^fit_aft$scale
emit("STAT-SURV-ALT-001","tabelz.executor.reliability","acceleratedLifeMenuItem",
"SURV_CORE",list(mode="accelerated",time_column="time",event_column="status",
stress_column="stress",stress_type="linear",stress_unit="native",
use_stress=50,high_stress=80),
list("/total_n"=exact(nrow(d),"All finite stress observations"),
"/fits/[family=weibull]/median_use"=tol(median50,"Weibull median at stress 50")),
c("R Weibull survreg scale=1/shape; intercept+slope*stress is log scale.",
"Median at stress s is exp(beta0+beta1*s)*(log 2)^(survreg scale)."))
emit("STAT-SURV-WEIBULL-AFT-001","tabelz.analysis.weibull_aft:compute_weibull_aft",
"weibullAftMenuItem","SURV_CORE",
list(time_column="time",event_column="status",stress_column="stress",
prediction_stresses=list(50)),
list("/coefficients/0/estimate"=tol(coef(fit_aft)[[1]],"Log scale intercept"),
"/coefficients/1/estimate"=tol(coef(fit_aft)[[2]],"Log scale stress slope"),
"/coefficients/2/estimate"=tol(1/fit_aft$scale,"Weibull shape=1/survreg scale"),
"/analyzed_count"=exact(nrow(d),"All complete rows")),
c("R survreg Weibull scale is inverse shape; Tabelz k=1/scale and beta0 is log scale.",
"R maxiter=200; no predictor standardization."))
cat("parametric survival references generated\n")
generators/r/survival_population.R
source("common.R",local=TRUE)
raw <- read_dataset("SURV_CORE")
raw$time[3] <- NA_real_; raw$x[4] <- NA_real_
raw$stress[5] <- NA_real_; raw$status[6] <- NA_integer_
export_dataset(raw,"SURV_POP","Constructed censored population with role-specific missingness",
"R survival::survfit/coxph/survreg and analytic exponential MLE",
"Constructed validation data; no third-party license.",
c(time="Float64",status="Int64",stress="Float64",x="Float64"))
d <- read_dataset("SURV_POP")
sraw <- read_dataset("SURV_STABILITY")
sraw$response[3] <- NA_real_; sraw$time[6] <- NA_real_
export_dataset(sraw,"SURV_STABILITY_POP","Constructed stability population with missing response and time",
"R stats::lm complete-case regression",
"Constructed validation data; no third-party license.",
c(time="Float64",response="Float64",batch="String"))
s <- read_dataset("SURV_STABILITY_POP")
iraw <- read_dataset("SURV_INTERVAL")
iraw$lower[3] <- NA_real_; iraw$upper[3] <- NA_real_
export_dataset(iraw,"SURV_INTERVAL_POP","Constructed mixed-censoring population with an omitted row",
"R survival::Surv(type=interval2) and censoring counts",
"Constructed validation data; no third-party license.",
c(lower="Float64",upper="Float64"))
inter <- read_dataset("SURV_INTERVAL_POP")
run <- generator_run(c("survival","stats","jsonlite"))
tol <- function(x,note,tier="1e-9") list(comparison="tolerance",value=dec(unname(x)),
absolute_tolerance="0",relative_tolerance=tier,rationale=paste("R complete-case reference:",note))
exact <- function(x,note) list(comparison="exact",value=as.integer(x),rationale=note)
emit <- function(id,entry,menu,dataset,excluded,config,outputs,notes) {
write_expected("survival",id,list(schema_version="2.0.0",case_id=id,
outputs=outputs,expected_error=NULL,deferred_outputs=list(),generator_run=run))
write_case("survival",id,list(schema_version="2.0.0",case_id=id,version="1.0.0",
family="survival",entry_point=entry,menu_items=list(menu),title=paste("Survival population",id),
case_kind="missing_excluded",dataset=list(path=paste0("reference_data/datasets/",dataset,".json"),sha256=""),
excluded_rows=list(as.integer(excluded)),configuration=config,"repeat"=1L,
expected=list(path=paste0("reference_data/expected/survival/",id,".json"),sha256=""),
oracle=list(oracle_class="independent_implementation",source="R survival/stats and analytic counts",
citation="R survival::Surv/survfit/coxph/survreg and stats::lm documentation",
generator=list(path="generators/r/survival_population.R",sha256=""),
independent=TRUE,convention_notes=c(notes,"R re-reads the exported CSV and removes the declared ordinal before complete-case selection.")),
supported=TRUE,unsupported_reason=NULL,requirement_ids=list("REQ-STAT-SURV-001"),
risk_ids=list("RISK-REFERENCE-001"),review_reference=NULL))
}
eligible <- d[-8L,,drop=FALSE]
kmdata <- eligible[stats::complete.cases(eligible[c("time","status")]),,drop=FALSE]
km <- survival::survfit(survival::Surv(time,status)~1,data=kmdata,conf.type="log-log")
cfg <- list(time_column="time",event_column="status",event_value="1",censored_value="0")
emit("STAT-SURV-KM-POP-001","tabelz.executor.survival","survivalMenuItem",
"SURV_POP",7L,cfg,
list("/source_count"=exact(nrow(d),"CSV population"),
"/excluded_count"=exact(1L,"One source row excluded"),
"/missing_count"=exact(2L,"Missing time and status rows"),
"/curves/0/survival/0"=tol(km$surv[1],"First complete-case Kaplan-Meier step")),
c("Kaplan-Meier removes missing time/status after exclusion; log-log Greenwood bands."))
ticks <- c(7,10,15,20)
risk <- vapply(ticks,function(t) sum(kmdata$time>=t),0L)
emit("STAT-SURV-RISK-POP-001","tabelz.analysis.survival_nonparametric:number_at_risk",
"survivalMenuItem","SURV_POP",7L,c(cfg,list(times=as.list(ticks))),
list("/0"=exact(risk[1],"Risk at 7"),"/1"=exact(risk[2],"Risk at 10"),
"/2"=exact(risk[3],"Risk at 15"),"/3"=exact(risk[4],"Risk at 20")),
c("Number at risk counts rows still observed at or beyond each query time."))
coxdata <- eligible[stats::complete.cases(eligible[c("time","status","x")]),,drop=FALSE]
cox <- survival::coxph(survival::Surv(time,status)~x,data=coxdata,ties="efron")
emit("STAT-SURV-COX-POP-001","tabelz.executor.cox_regression","coxRegressionMenuItem",
"SURV_POP",7L,list(duration_column="time",event_column="status",event_value="1",
censored_value="0",predictors=list("x")),
list("/source_count"=exact(nrow(d),"CSV population"),
"/excluded_count"=exact(1L,"One source row excluded"),
"/missing_count"=exact(3L,"Missing time, status, and predictor rows"),
"/coefficients/0/log_hazard"=tol(coef(cox)[[1]],"Efron-tie complete-case log hazard","1e-6")),
c("R coxph Efron fit uses the same complete predictor/time/event rows."))
exp_scale <- sum(kmdata$time)/sum(kmdata$status)
emit("STAT-SURV-FAMILIES-POP-001","tabelz.executor.reliability",
"reliabilityFamiliesMenuItem","SURV_POP",7L,
list(mode="families",time_column="time",event_column="status"),
list("/input_row_count"=exact(nrow(eligible),"Rows sent to worker after exclusion"),
"/omitted_row_count"=exact(2L,"Rows with missing lifetime or status"),
"/total_n"=exact(nrow(kmdata),"Finite complete lifetimes"),
"/fits/[family=exponential]/param1_val"=tol(exp_scale,
"Exponential total time/failures","1e-9")),
c("The exponential right-censored MLE is total time on test divided by observed failures."))
altdata <- eligible[stats::complete.cases(eligible[c("time","status","stress")]),,drop=FALSE]
emit("STAT-SURV-ALT-POP-001","tabelz.executor.reliability",
"acceleratedLifeMenuItem","SURV_POP",7L,
list(mode="accelerated",time_column="time",event_column="status",stress_column="stress",
stress_type="linear",stress_unit="native",use_stress=50,high_stress=80),
list("/input_row_count"=exact(nrow(eligible),"Rows sent to worker after exclusion"),
"/omitted_row_count"=exact(3L,"Missing time, status or stress"),
"/total_n"=exact(nrow(altdata),"Complete AFT rows")),
c("Physical ALT omits incomplete time/event/stress rows after exclusion."))
emit("STAT-SURV-WEIBULL-POP-001","tabelz.analysis.weibull_aft:compute_weibull_aft",
"weibullAftMenuItem","SURV_POP",7L,
list(time_column="time",event_column="status",stress_column="stress",prediction_stresses=list(50)),
list("/source_count"=exact(nrow(d),"CSV population"),
"/excluded_count"=exact(1L,"One stable identity excluded"),
"/missing_response_count"=exact(2L,"Missing time and status"),
"/incomplete_covariate_count"=exact(1L,"Missing stress"),
"/analyzed_count"=exact(nrow(altdata),"Complete AFT rows")),
c("This checks Weibull AFT population partition; seeded ML coefficients are compared in a separate case."))
stability_eligible <- s[-10L,,drop=FALSE]
stability_complete <- stability_eligible[stats::complete.cases(stability_eligible[c("time","response")]),,drop=FALSE]
lmfit <- stats::lm(response~time,data=stability_complete)
emit("STAT-SURV-STABILITY-POP-001","tabelz.analysis.product_stability:compute_product_stability",
"productStabilityMenuItem","SURV_STABILITY_POP",9L,
list(time_column="time",response_column="response",lsl=95),
list("/source_count"=exact(nrow(s),"CSV population"),
"/excluded_count"=exact(1L,"One source row excluded"),
"/missing_count"=exact(2L,"Missing response and time"),
"/coefficients/1/estimate"=tol(coef(lmfit)[[2]],"Complete-case pooled slope")),
c("Pooled OLS excludes missing time/response and declared source exclusion."))
inter_eligible <- inter[-10L,,drop=FALSE]
valid <- !(is.na(inter_eligible$lower)&is.na(inter_eligible$upper))
inter_complete <- inter_eligible[valid,,drop=FALSE]
exact_n <- sum(!is.na(inter_complete$lower)&!is.na(inter_complete$upper)&inter_complete$lower==inter_complete$upper)
right_n <- sum(!is.na(inter_complete$lower)&is.na(inter_complete$upper))
left_n <- sum(is.na(inter_complete$lower)&!is.na(inter_complete$upper))
interval_n <- nrow(inter_complete)-exact_n-right_n-left_n
emit("STAT-SURV-INTERVAL-POP-001","tabelz.executor.reliability","intervalLifeMenuItem",
"SURV_INTERVAL_POP",9L,
list(mode="interval",lower_column="lower",upper_column="upper"),
list("/input_row_count"=exact(nrow(inter_eligible),"Rows after exclusion"),
"/omitted_row_count"=exact(1L,"Both endpoints missing"),
"/exact_count"=exact(exact_n,"Exact endpoint pairs"),
"/right_count"=exact(right_n,"Finite lower, absent upper"),
"/left_count"=exact(left_n,"Absent lower, finite upper"),
"/interval_count"=exact(interval_n,"Finite ordered interval pairs")),
c("R interval2 endpoint types match the worker's missing-bound decoding."))
cat("survival population references generated\n")
generators/r/survival_repairable.R
source("common.R", local = TRUE)
# Reliability-engineering process analyses (B9): Repairable systems, Degradation-based
# lifetime, Prepare warranty data, the warranty censoring table and Warranty prediction.
# References: base R implementations of the documented formulas (MCF, Crow-AMSAA power
# law, warranty conversion), survreg for the fitted life families and the pseudo-failure
# limit for degradation. Every reference reads the exported CSV back before computing.
suppressMessages(library(survival))
run <- generator_run(c("stats", "survival", "jsonlite"))
tol <- function(value, why, rel = "1e-6", atol = "1e-7") list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = atol, relative_tolerance = rel,
rationale = paste("R reference:", why)
)
tail <- function(value, why, rel = "1e-6") list(
comparison = "tail_probability", value = dec(unname(value)),
relative_tolerance = rel, rationale = paste("R p-value reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
MANUAL <- "docs/manual/chapters/96-repairable-degradation-warranty.tex"
CITATION <- "Turnbull (1976); Nelson (1988); Crow (1975); Meeker, Hahn and Escobar (1998); Therneau T (2024), A Package for Survival Analysis in R."
emit <- function(id, title, entry, menu, requirement, dataset, configuration, outputs, notes,
source, kind = "nominal") {
write_expected("survival", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("survival", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "survival", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/survival/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = source, citation = CITATION,
generator = list(path = "generators/r/survival_repairable.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.")
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(requirement), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
# =======================================================================================
# Dataset 1: repairable-system events and observation windows stacked behind a tab column.
event_rows <- data.frame(
table = "events",
unit = c("u1", "u1", "u1", "u1", "u2", "u2", "u3", "u3", "u3", "u4", "u4", "u3"),
age = c(1.5, 4.0, 4.0, 9.5, 2.5, 7.0, 6.0, 6.5, 11.0, 5.0, 10.5, 6.0),
start = NA_real_, end = NA_real_, stringsAsFactors = FALSE
)
window_rows <- data.frame(
table = "windows",
unit = c("u1", "u2", "u3", "u4", "u5"),
age = NA_real_,
start = c(0, 0, 0, 2, 0),
end = c(12, 12, 12, 12, 12),
stringsAsFactors = FALSE
)
repair <- rbind(event_rows, window_rows)
export_dataset(repair, "SURV_REPAIR_B9",
"Constructed repairable-system events and windows (B9)",
"Nelson (1988) Applied Life Data Analysis; Crow (1975)",
"Constructed validation data; no third-party license.",
c(table = "String", unit = "String", age = "Float64",
start = "Float64", end = "Float64"))
rp <- read_dataset("SURV_REPAIR_B9")
rp_events <- rp[rp$table == "events", c("unit", "age")]
rp_windows <- rp[rp$table == "windows", c("unit", "start", "end")]
# Mean cumulative function with the robust unit-cluster variance, from the definition.
mcf_reference <- function(events, windows, level) {
z <- stats::qnorm(0.5 + level / 2)
g <- nrow(windows)
ages <- sort(unique(events$age))
steps <- list()
psi <- rep(0, g)
m <- 0
for (u in ages) {
at_risk <- sum(windows$start < u & u <= windows$end)
d_n <- sum(events$age == u)
for (i in seq_len(g)) {
d_n_i <- sum(events$age == u & events$unit == windows$unit[i])
y_i <- as.numeric(windows$start[i] < u & u <= windows$end[i])
psi[i] <- psi[i] + (d_n_i - y_i * d_n / at_risk) / at_risk
}
m <- m + d_n / at_risk
variance <- g / (g - 1) * sum(psi^2)
se <- sqrt(variance)
steps[[length(steps) + 1]] <- list(
age = u, at_risk = at_risk, events = d_n, increment = d_n / at_risk, mcf = m,
variance = variance, standard_error = se,
lower = m * exp(-z * se / m), upper = m * exp(z * se / m)
)
}
list(steps = steps, z = z)
}
mcf_outputs <- function(reference, evaluation_ages, level) {
out <- list()
for (i in seq_along(reference$steps)) {
s <- reference$steps[[i]]
base <- paste0("/mcf_steps/", i - 1)
out[[paste0(base, "/segment")]] <- exact(0, "one support segment")
out[[paste0(base, "/age")]] <- tol(s$age, "distinct event age")
out[[paste0(base, "/events")]] <- exact(s$events, "events at the age")
out[[paste0(base, "/at_risk")]] <- exact(s$at_risk, "units with entry < age <= end")
out[[paste0(base, "/increment")]] <- tol(s$increment, "dN / Y")
out[[paste0(base, "/mcf")]] <- tol(s$mcf, "M(t) = sum dN / Y")
out[[paste0(base, "/variance")]] <- tol(s$variance, "G/(G-1) sum psi_i^2")
out[[paste0(base, "/standard_error")]] <- tol(s$standard_error, "sqrt of the variance")
out[[paste0(base, "/lower")]] <- tol(s$lower, "M exp(-z se / M)")
out[[paste0(base, "/upper")]] <- tol(s$upper, "M exp(+z se / M)")
}
z <- stats::qnorm(0.5 + level / 2)
for (k in seq_along(evaluation_ages)) {
age <- evaluation_ages[k]
hit <- 0
value <- 0
se <- NA_real_
for (s in reference$steps) {
if (s$age <= age) {
hit <- 1
value <- s$mcf
se <- s$standard_error
}
}
base <- paste0("/mcf_evaluations/", k - 1)
out[[paste0(base, "/age")]] <- tol(age, "requested evaluation age")
out[[paste0(base, "/mcf")]] <- tol(value, "M at the age")
if (hit) {
out[[paste0(base, "/standard_error")]] <- tol(se, "sqrt of the variance")
out[[paste0(base, "/lower")]] <- tol(value * exp(-z * se / value), "log-scale interval")
out[[paste0(base, "/upper")]] <- tol(value * exp(+z * se / value), "log-scale interval")
}
}
out
}
REPAIR <- "tabelz.analysis.repairable_reliability:compute_repairable_reliability"
REPAIR_REQ <- "REQ-STAT-SURV-REPAIRABLE-001"
REPAIR_TABS <- list(tab_column = "table", events_tab = "events", windows_tab = "windows")
REPAIR_BASE <- list(event_unit_column = "unit", event_age_column = "age",
window_unit_column = "unit", window_entry_column = "start",
window_end_column = "end", time_unit = "months")
REPAIR_NOTES <- c(
paste0(MANUAL, ":13-23: M(t) = sum dN(u) / Y(u) over units at risk entry < u <= end; the robust variance aggregates psi_i = sum [dN_i - Y_i dN / Y] / Y over independent units with the G/(G-1) factor; log-scale pointwise intervals."),
"Reference: an independent base-R implementation of those formulas. HPP limits are the exact central Poisson (Garwood) interval chi2(2N) / (2 exposure).",
"The events and windows share one dataset split by the tab column; the binding passes both frames to the engine as the desktop service does."
)
mref95 <- mcf_reference(rp_events, rp_windows, 0.95)
mref90 <- mcf_reference(rp_events, rp_windows, 0.90)
hpp_outputs <- function(level) {
events <- nrow(rp_events)
exposure <- sum(rp_windows$end - rp_windows$start)
rate <- events / exposure
alpha <- 1 - level
lower <- stats::qchisq(alpha / 2, 2 * events) / 2 / exposure
upper <- stats::qchisq(1 - alpha / 2, 2 * events + 2) / 2 / exposure
list(
"/hpp/rate" = tol(rate, "events / exposure"),
"/hpp/rate_lower" = tol(lower, "qchisq(alpha/2, 2N) / (2 exposure)"),
"/hpp/rate_upper" = tol(upper, "qchisq(1-alpha/2, 2N+2) / (2 exposure)"),
"/hpp/events" = exact(events, "events"),
"/hpp/exposure" = tol(exposure, "sum of window lengths"),
"/hpp/log_likelihood" = tol(events * log(rate) - events, "N log rate - N")
)
}
emit("STAT-SURV-REPAIR-001", "Repairable-system MCF with a constant-rate HPP overlay",
REPAIR, "repairableMenuItem", REPAIR_REQ, "SURV_REPAIR_B9",
c(REPAIR_TABS, REPAIR_BASE,
list(models = list("mcf", "hpp"), evaluation_ages = list(3, 6, 12),
confidence_level = 0.95)),
c(list("/unit_count" = exact(5, "units"),
"/zero_event_unit_count" = exact(1, "u5 has no events"),
"/event_count" = exact(nrow(rp_events), "events"),
"/distinct_event_age_count" = exact(length(mref95$steps), "distinct event ages"),
"/total_exposure" = tol(sum(rp_windows$end - rp_windows$start), "exposure"),
"/earliest_entry" = tol(min(rp_windows$start), "earliest entry"),
"/latest_end" = tol(max(rp_windows$end), "latest end"),
"/event_source_count" = exact(nrow(rp_events), "source events"),
"/event_excluded_count" = exact(0, "none excluded"),
"/window_source_count" = exact(5, "source windows"),
"/window_excluded_count" = exact(0, "none excluded")),
mcf_outputs(mref95, c(3, 6, 12), 0.95),
hpp_outputs(0.95)),
REPAIR_NOTES, "base R MCF and Poisson rate algebra")
# 002: power-law NHPP (Crow-AMSAA) with a forecast horizon at 90%.
power_reference <- function(events, windows, level, evaluation_ages, horizon) {
n <- nrow(events)
sum_log_t <- sum(log(events$age))
loglik <- function(a, b) {
s <- sum(windows$end^b - windows$start^b)
n * a + n * log(b) + (b - 1) * sum_log_t - exp(a) * s
}
profile <- function(log_b) {
b <- exp(log_b)
s <- sum(windows$end^b - windows$start^b)
loglik(log(n / s), b)
}
best <- optimize(profile, c(log(1e-3), log(1e3)), maximum = TRUE)
log_b <- best$maximum
# Newton polish on the profile score so the reference optimum matches the engine's
# grid + Brent + Newton search to well below the output tolerances.
for (k in 1:8) {
h <- 1e-6
d1 <- (profile(log_b + h) - profile(log_b - h)) / (2 * h)
d2 <- (profile(log_b + h) - 2 * profile(log_b) + profile(log_b - h)) / h^2
step <- d1 / d2
log_b <- log_b - step
if (abs(step) < 1e-13) break
}
b <- exp(log_b)
s <- sum(windows$end^b - windows$start^b)
a <- log(n / s)
peak <- profile(log_b)
# numeric Hessian of the log-likelihood in (a, log b)
h <- 1e-4
f <- function(da, db) loglik(a + da, exp(log_b + db))
h11 <- -(f(h, 0) - 2 * f(0, 0) + f(-h, 0)) / h^2
h22 <- -(f(0, h) - 2 * f(0, 0) + f(0, -h)) / h^2
h12 <- -(f(h, h) - f(h, -h) - f(-h, h) + f(-h, -h)) / (4 * h^2)
information <- matrix(c(h11, h12, h12, h22), 2, 2)
covariance <- solve(information)
cutoff <- stats::qchisq(level, 1) / 2
target <- peak - cutoff
side <- function(which_side) {
# The engine searches b in [1e-6, 1e3] with stable scaled moments; naive end^b
# overflows well below 1e3, and this dataset's cutoff lies far inside b = 50.
edge <- log(if (which_side == "lower") 1e-3 else 50)
value <- profile(edge)
if (!is.finite(value) || value >= target) return(NA_real_)
exp(uniroot(function(x) profile(x) - target,
c(edge, log_b), tol = 1e-12)$root)
}
lrt <- max(0, 2 * (peak - profile(0)))
list(a = a, b = b, loglik = peak, covariance = covariance,
profile_lower = side("lower"), profile_upper = side("upper"),
lrt = lrt, lrt_p = stats::pchisq(lrt, 1, lower.tail = FALSE),
expected = function(age) exp(a) * age^b,
increment = function(age, horizon) exp(a) * ((age + horizon)^b - age^b))
}
pw <- power_reference(rp_events, rp_windows, 0.9, c(4, 8), 2)
# delta-method interval of a log-scale value with a numerically differentiated gradient
log_delta <- function(log_value_function, covariance, level) {
z <- stats::qnorm(0.5 + level / 2)
centre <- c(0, 0) # evaluated at (a, log b) directly
value <- log_value_function(pw$a, log(pw$b))
gradient <- numeric(2)
for (k in 1:2) {
step <- 1e-6 * max(1, abs(c(pw$a, log(pw$b))[k]))
up <- c(pw$a, log(pw$b)); up[k] <- up[k] + step
down <- c(pw$a, log(pw$b)); down[k] <- down[k] - step
gradient[k] <- (log_value_function(up[1], up[2]) - log_value_function(down[1], down[2])) / (2 * step)
}
variance <- as.numeric(t(gradient) %*% covariance %*% gradient)
c(exp(value), exp(value - z * sqrt(variance)), exp(value + z * sqrt(variance)))
}
power_outputs <- list(
"/power_law/log_scale" = tol(pw$a, "a = log(N / S(b)) at the profile maximum"),
"/power_law/shape" = tol(pw$b, "profile maximum over log b (optimize)"),
"/power_law/log_likelihood" = tol(pw$loglik, "profile log-likelihood"),
"/power_law/log_scale_standard_error" = tol(sqrt(pw$covariance[1, 1]), "observed information"),
"/power_law/log_shape_standard_error" = tol(sqrt(pw$covariance[2, 2]), "observed information"),
"/power_law/covariance_log_scale_log_shape/0/0" = tol(pw$covariance[1, 1], "observed information"),
"/power_law/covariance_log_scale_log_shape/0/1" = tol(pw$covariance[1, 2], "observed information"),
"/power_law/covariance_log_scale_log_shape/1/1" = tol(pw$covariance[2, 2], "observed information"),
"/power_law/shape_profile_lower" = tol(pw$profile_lower, "profile likelihood cutoff chi2_1(level)/2"),
"/power_law/shape_profile_upper" = tol(pw$profile_upper, "profile likelihood cutoff chi2_1(level)/2"),
"/power_law/lrt_statistic" = tol(pw$lrt, "2 (log L(b_hat) - log L(b = 1))"),
"/power_law/lrt_p_value" = tail(pw$lrt_p, "chi-square(1) p for b = 1")
)
for (k in seq_along(c(4, 8))) {
age <- c(4, 8)[k]
cum <- log_delta(function(a, lb) a + exp(lb) * log(age), pw$covariance, 0.9)
inc <- log_delta(function(a, lb) a + exp(lb) * log(age + 2) +
log(1 - (age / (age + 2))^exp(lb)), pw$covariance, 0.9)
base <- paste0("/power_law/expected_counts/", 2 * (k - 1))
power_outputs[[paste0(base, "/estimate")]] <- tol(cum[1], "Lambda(t) = e^a t^b")
power_outputs[[paste0(base, "/lower")]] <- tol(cum[2], "delta method on log Lambda")
power_outputs[[paste0(base, "/upper")]] <- tol(cum[3], "delta method on log Lambda")
base2 <- paste0("/power_law/expected_counts/", 2 * (k - 1) + 1)
power_outputs[[paste0(base2, "/estimate")]] <- tol(inc[1], "Lambda(t + h) - Lambda(t)")
power_outputs[[paste0(base2, "/lower")]] <- tol(inc[2], "delta method")
power_outputs[[paste0(base2, "/upper")]] <- tol(inc[3], "delta method")
}
emit("STAT-SURV-REPAIR-002", "Repairable-system MCF with a power-law NHPP fit and forecast",
REPAIR, "repairableMenuItem", REPAIR_REQ, "SURV_REPAIR_B9",
c(REPAIR_TABS, REPAIR_BASE,
list(models = list("mcf", "power_law"), evaluation_ages = list(4, 8),
forecast_horizon = 2, confidence_level = 0.90)),
c(list("/unit_count" = exact(5, "units"),
"/event_count" = exact(nrow(rp_events), "events")),
mcf_outputs(mref90, c(4, 8), 0.90),
power_outputs),
c(REPAIR_NOTES,
paste0(MANUAL, ":34-41: Lambda(t) = e^a t^b fitted by profile maximum likelihood (a = log(N / S(b)), S(b) = sum (end^b - entry^b)); profile-likelihood interval for b with cutoff chi2_1(level)/2; likelihood-ratio test of b = 1."),
"Iterative searches (optimize, uniroot to 1e-12) and numeric second derivatives with step 1e-4: outputs allow 1e-7 absolute or 1e-6 relative."),
"base R Crow-AMSAA profile likelihood and MCF algebra")
cat("repairable references generated\n")
generators/r/survival_warranty.R
source("common.R", local = TRUE)
# Warranty validation references (B9): the shipment-by-return triangle conversion
# (Quality > Reliability engineering > Prepare warranty data), the derived censoring
# table and Warranty prediction. The conversion is re-implemented independently in
# base R from the documented conventions; the life family of the prediction cases is
# fitted with survival::survreg on interval2 records with frequency weights and the
# expected counts, gradients and logit intervals are computed from the documented
# formulas. Every reference reads the exported CSV back before computing.
suppressMessages(library(survival))
run <- generator_run(c("stats", "survival", "jsonlite"))
tol <- function(value, why, rel = "1e-6", atol = "1e-7") list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = atol, relative_tolerance = rel,
rationale = paste("R reference:", why)
)
algebra <- function(value, why) tol(value, why, rel = "1e-9", atol = "1e-9")
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
flag <- function(value, why) list(comparison = "exact", value = value, rationale = why)
MANUAL <- "docs/manual/chapters/96-repairable-degradation-warranty.tex"
CITATION <- "Therneau T (2024). A Package for Survival Analysis in R. R package version 3.6; Meeker, Hahn and Escobar (1998) Statistical Methods for Reliability Data, Wiley."
SOURCE <- "R survival package survreg on interval2 records with frequency weights; independent base-R implementations of the warranty triangle conversion and the expected-count algebra"
emit <- function(id, title, entry, menu, requirement, dataset, configuration, outputs, notes,
kind = "nominal") {
write_expected("survival", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("survival", id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0",
family = "survival", entry_point = entry, menu_items = list(menu),
title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/survival/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = SOURCE, citation = CITATION,
generator = list(path = "generators/r/survival_warranty.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculating the reference.")
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list(requirement), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
PREP <- "tabelz.analysis.warranty:compute_warranty_preprocess"
TABLE <- "tabelz.analysis.warranty:warranty_censoring_table"
PRED <- "tabelz.analysis.warranty_prediction:compute_warranty_prediction"
PREP_REQ <- "REQ-STAT-SURV-WARRANTY-PREP-001"
PRED_REQ <- "REQ-STAT-SURV-WARRANTY-PRED-001"
# =======================================================================================
# Independent re-implementation of the triangle conversion: cohorts in shipment order,
# records per cohort in return-column order and then the right-censored survivors.
convert_reference <- function(d, columns, axis, width, last) {
n <- nrow(d)
last_calendar <- if (axis == "calendar") length(columns) else n
as_of <- if (last == "incomplete") last_calendar - 1 else last_calendar
cohorts <- list()
records <- list()
shipped_total <- 0L
excluded_failures <- 0L
excluded_shipments <- 0L
for (i in seq_len(n)) {
period <- i
label <- as.character(d$label[i])
shipped <- as.integer(d$shipped[i])
shipped_total <- shipped_total + shipped
if (period > as_of) {
excluded_shipments <- excluded_shipments + shipped
for (name in columns) {
value <- d[[name]][i]
if (!is.na(value)) excluded_failures <- excluded_failures + as.integer(value)
}
next
}
failures <- 0L
late <- 0L
for (k in seq_along(columns)) {
value <- d[[columns[k]]][i]
calendar <- if (axis == "calendar") k else period + k - 1
where <- paste0(label, ", return column ", columns[k])
if (calendar < period) {
if (!is.na(value) && value != 0) stop(where, ": returns before the shipment period")
next
}
if (calendar > last_calendar) {
if (!is.na(value) && value != 0) stop(where, ": returns after the last period")
next
}
if (calendar > as_of) {
late <- late + if (is.na(value)) 0L else as.integer(value)
next
}
if (is.na(value)) stop(where, ": blank cell inside the observed region")
count <- as.integer(value)
if (count > 0) {
age <- calendar - period
records[[length(records) + 1]] <- list(
start = age * width, end = (age + 1) * width,
frequency = count, kind = "interval", cohort = label
)
}
failures <- failures + count
}
survivors <- shipped - failures
survivor_age <- (as_of - period + 1) * width
if (survivors > 0) {
records[[length(records) + 1]] <- list(
start = survivor_age, end = NA_real_,
frequency = survivors, kind = "right", cohort = label
)
}
excluded_failures <- excluded_failures + late
cohorts[[length(cohorts) + 1]] <- list(
label = label, shipment_period = period, shipped = shipped,
failures = failures, survivors = survivors, age = survivor_age,
excluded_failures = late
)
}
list(
as_of = as_of, shipped_total = shipped_total,
failures_used = sum(vapply(cohorts, function(x) x$failures, 0L)),
survivors_total = sum(vapply(cohorts, function(x) x$survivors, 0L)),
excluded_failures = excluded_failures, excluded_shipments = excluded_shipments,
cohorts = cohorts, records = records, source_count = n
)
}
conversion_outputs <- function(conv) {
out <- list(
"/conversion/as_of_period" = exact(conv$as_of, "J*: the last included calendar period"),
"/conversion/shipped_total" = exact(conv$shipped_total, "units shipped in the cohort rows"),
"/conversion/failures_used" = exact(conv$failures_used, "returns inside the included periods"),
"/conversion/survivors_total" = exact(conv$survivors_total, "unreturned units right censored"),
"/conversion/excluded_failures" = exact(conv$excluded_failures, "returns after the as-of date"),
"/conversion/excluded_shipments" = exact(conv$excluded_shipments, "units shipped after the as-of date"),
"/conversion/source_count" = exact(conv$source_count, "shipment rows read"),
"/conversion/excluded_row_count" = exact(0, "no rows excluded")
)
for (i in seq_along(conv$cohorts)) {
ch <- conv$cohorts[[i]]
base <- paste0("/conversion/cohorts/", i - 1)
out[[paste0(base, "/shipment_period")]] <- exact(ch$shipment_period, "row position in shipment order")
out[[paste0(base, "/shipped")]] <- exact(ch$shipped, "units shipped")
out[[paste0(base, "/failures")]] <- exact(ch$failures, "returns inside the included periods")
out[[paste0(base, "/survivors")]] <- exact(ch$survivors, "unreturned units")
out[[paste0(base, "/age")]] <- algebra(ch$age, "(J* - i + 1) w")
out[[paste0(base, "/excluded_failures")]] <- exact(ch$excluded_failures, "returns after the as-of date")
}
for (j in seq_along(conv$records)) {
rec <- conv$records[[j]]
base <- paste0("/conversion/records/", j - 1)
out[[paste0(base, "/start")]] <- algebra(rec$start, "interval start or right-censoring age")
if (!is.na(rec$end)) {
out[[paste0(base, "/end")]] <- algebra(rec$end, "interval end")
}
out[[paste0(base, "/frequency")]] <- exact(rec$frequency, "units in the record")
}
out
}
table_outputs <- function(conv) {
out <- list()
for (j in seq_along(conv$records)) {
rec <- conv$records[[j]]
base <- paste0("/", j - 1)
out[[paste0(base, "/start")]] <- algebra(rec$start, "interval start or right-censoring age")
if (!is.na(rec$end)) {
out[[paste0(base, "/end")]] <- algebra(rec$end, "interval end")
}
out[[paste0(base, "/frequency")]] <- exact(rec$frequency, "units in the record")
}
out
}
# =======================================================================================
# Warranty prediction: survreg fits the family, then the documented expected-count and
# logit delta-method algebra computed from (mu, log sigma).
SURV_DIST <- c(weibull = "weibull", lognormal = "lognormal")
warranty_log_survival <- function(family, theta, times) {
mu <- theta[1]
sigma <- exp(theta[2])
out <- rep(0, length(times)) # log S(0) = 0
positive <- times > 0
if (any(positive)) {
z <- (log(times[positive]) - mu) / sigma
out[positive] <- if (family == "weibull") -exp(z)
else stats::pnorm(z, lower.tail = FALSE, log = TRUE)
}
out
}
warranty_prediction_reference <- function(records, family, survivors, ages, future_units,
future_times, horizon, limit, level, period_width) {
fit <- survreg(Surv(start, end, type = "interval2") ~ 1,
data = records, weights = records$frequency, dist = SURV_DIST[[family]])
theta <- c(unname(coef(fit)), log(fit$scale))
covariance <- stats::vcov(fit)
cap <- if (is.null(limit)) Inf else limit
z <- stats::qnorm(0.5 + level / 2)
field_at <- function(at, th) {
eligible <- ages < cap
if (!any(eligible)) return(0)
a <- ages[eligible]
ends <- pmin(a + at, cap)
sum(survivors[eligible] * -expm1(
warranty_log_survival(family, th, ends) - warranty_log_survival(family, th, a)
))
}
future_at <- function(at, th) {
active <- future_times < at
if (!any(active)) return(0)
spans <- pmin(at - future_times[active], cap)
sum(future_units[active] * -expm1(warranty_log_survival(family, th, spans)))
}
summarize_at <- function(at) {
total <- field_at(at, theta) + future_at(at, theta)
units <- sum(survivors[ages < cap]) + sum(future_units[future_times < at])
gradient <- vapply(seq_along(theta), function(k) {
step <- 1e-6 * max(1, abs(theta[k]))
up <- theta
up[k] <- up[k] + step
down <- theta
down[k] <- down[k] - step
(field_at(at, up) + future_at(at, up) - field_at(at, down) - future_at(at, down)) / (2 * step)
}, 0)
variance <- as.numeric(t(gradient) %*% covariance %*% gradient)
fraction <- total / units
spread <- sqrt(variance) / (total * (1 - fraction))
centre <- log(fraction / (1 - fraction))
list(total = total, units = units,
lower = units / (1 + exp(-(centre - z * spread))),
upper = units / (1 + exp(-(centre + z * spread))))
}
steps <- if (horizon > 0) ceiling(horizon / period_width - 1e-12) else 0
periods <- list()
previous <- 0
for (step in seq_len(steps)) {
at <- min(step * period_width, horizon)
row <- summarize_at(at)
row$at <- at
row$increment <- row$total - previous
previous <- row$total
periods[[length(periods) + 1]] <- row
}
list(
fit = fit, theta = theta,
cohorts = survivors * -expm1(
warranty_log_survival(family, theta, pmin(ages + horizon, cap)) -
warranty_log_survival(family, theta, ages)
),
future = ifelse(future_times < horizon,
future_units * -expm1(warranty_log_survival(
family, theta, pmin(horizon - future_times, cap)
)),
0),
summary = summarize_at(horizon),
field = field_at(horizon, theta),
future_total = future_at(horizon, theta),
periods = periods
)
}
prediction_parameters <- function(fit, family, level) {
z <- stats::qnorm(0.5 + level / 2)
mu <- unname(coef(fit))
sigma <- fit$scale
v <- stats::vcov(fit)
se_mu <- sqrt(v[1, 1])
se_lsigma <- sqrt(v[2, 2])
rows <- list()
if (family == "weibull") {
rows[[1]] <- c(1 / sigma, se_lsigma / sigma,
exp(-(log(sigma) + z * se_lsigma)), exp(-(log(sigma) - z * se_lsigma)))
rows[[2]] <- c(exp(mu), exp(mu) * se_mu, exp(mu - z * se_mu), exp(mu + z * se_mu))
} else {
rows[[1]] <- c(mu, se_mu, mu - z * se_mu, mu + z * se_mu)
rows[[2]] <- c(sigma, sigma * se_lsigma,
exp(log(sigma) - z * se_lsigma), exp(log(sigma) + z * se_lsigma))
}
out <- list()
for (i in seq_along(rows)) {
base <- paste0("/parameters/", i - 1)
r <- rows[[i]]
out[[paste0(base, "/estimate")]] <- tol(r[1], "survreg maximum likelihood")
out[[paste0(base, "/standard_error")]] <- tol(r[2], "delta method from survreg vcov")
out[[paste0(base, "/lower")]] <- tol(r[3], "Wald limit")
out[[paste0(base, "/upper")]] <- tol(r[4], "Wald limit")
}
out[[paste0("/parameters/", length(rows), "/estimate")]] <-
algebra(0, "declared fixed threshold (no interval)")
out
}
prediction_outputs <- function(reference, family, survivors, ages, future_units,
future_times, level) {
out <- list(
"/log_likelihood" = tol(reference$fit$loglik[2], "weighted interval2 log-likelihood"),
"/field_expected" = tol(reference$field, "sum over cohorts of n_j [1 - S(min(a_j+h,L))/S(a_j)]"),
"/future_expected" = tol(reference$future_total, "sum over shipments of d F(min(h-s,L))"),
"/expected" = tol(reference$summary$total, "field plus future expected failures"),
"/eligible_units" = tol(reference$summary$units,
"survivors of eligible cohorts plus future units before the horizon"),
"/lower" = tol(reference$summary$lower, "logit(mu / N eligible) delta-method lower bound"),
"/upper" = tol(reference$summary$upper, "logit(mu / N eligible) delta-method upper bound")
)
for (i in seq_along(survivors)) {
base <- paste0("/cohorts/", i - 1)
out[[paste0(base, "/survivors")]] <- algebra(survivors[i], "right-censored units at the as-of date")
out[[paste0(base, "/age")]] <- algebra(ages[i], "cohort age")
out[[paste0(base, "/eligible")]] <- flag(TRUE, "below the warranty limit with a positive horizon")
out[[paste0(base, "/expected")]] <- tol(reference$cohorts[i],
"n_j [1 - S(min(a_j+h,L))/S(a_j)]")
}
for (i in seq_along(future_units)) {
base <- paste0("/future/", i - 1)
out[[paste0(base, "/units")]] <- algebra(future_units[i], "units of the future shipment")
out[[paste0(base, "/time")]] <- algebra(future_times[i], "time after the as-of date")
out[[paste0(base, "/expected")]] <- tol(reference$future[i], "d F(min(h-s,L))")
}
for (p in seq_along(reference$periods)) {
row <- reference$periods[[p]]
base <- paste0("/periods/", p - 1)
out[[paste0(base, "/horizon")]] <- algebra(row$at, "min(step * width, horizon)")
out[[paste0(base, "/expected")]] <- tol(row$total, "cumulative expectation through the horizon")
out[[paste0(base, "/increment")]] <- tol(row$increment, "difference of cumulative expectations")
out[[paste0(base, "/lower")]] <- tol(row$lower, "logit delta-method lower bound")
out[[paste0(base, "/upper")]] <- tol(row$upper, "logit delta-method upper bound")
}
c(out, prediction_parameters(reference$fit, family, level))
}
CONVERSION_NOTES <- c(
paste0(MANUAL, ":143-157: units are shipped at the start of their shipment period; the as-of date is the end of the last included calendar period J*; a return in calendar period j from shipment i is interval censored over ((j-i)w, (j-i+1)w]; units unreturned at J* are right censored at (J*-i+1)w; blank cells inside the observed region are refused, and cells before shipment or after the last period must be blank or zero."),
"Reference: an independent base-R re-implementation of the conversion loops; cohorts and records follow the engine's row and return-column order, so the positional output paths align."
)
PREDICTION_NOTES <- c(
paste0(MANUAL, ":159-170: a positive-support life family is fitted by maximum likelihood to the converted censored data; expected failures for n_j survivors of age a_j through horizon h before limit L are n_j [1 - S(min(a_j+h,L))/S(a_j)]; a future shipment of d units at time s contributes d F(min(h-s,L)); the interval is a delta-method confidence interval for the expected count formed on logit(mu / N_eligible) with the shared fitted-parameter covariance, not a prediction interval."),
"R: survreg(Surv(start, end, type = 'interval2') ~ 1, weights = frequency) fits the family; S(t) is computed analytically from (mu, sigma) (Weibull S = exp(-exp((log t - mu)/sigma)); lognormal S = 1 - pnorm((log t - mu)/sigma)); the gradient of the total expected count uses central differences with step 1e-6 * max(1, |theta|) in (mu, log sigma), and the periods table runs at multiples of the period width capped at the horizon.",
"Iterative maximum likelihood: survreg-dependent outputs allow 1e-7 absolute or 1e-6 relative; integer counts use exact and closed-form algebra 1e-9."
)
# =======================================================================================
# Dataset 1: calendar-axis shipment triangle. Blank cells sit before the shipment
# period or after the as-of date; column r7 is the still-in-progress period.
RCOLS <- paste0("r", 1:7)
warr <- data.frame(
label = c("2025-01", "2025-02", "2025-03", "2025-04", "2025-05", "2025-06"),
shipped = c(120L, 135L, 150L, 160L, 145L, 110L),
r1 = c(2L, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_),
r2 = c(3L, 3L, NA_integer_, NA_integer_, NA_integer_, NA_integer_),
r3 = c(4L, 5L, 4L, NA_integer_, NA_integer_, NA_integer_),
r4 = c(2L, 3L, 6L, 5L, NA_integer_, NA_integer_),
r5 = c(1L, 2L, 3L, 7L, 6L, NA_integer_),
r6 = c(1L, 2L, 2L, 4L, 5L, 3L),
r7 = c(1L, 1L, 2L, 0L, 1L, 2L)
)
export_dataset(warr, "SURV_WARR_B9",
"Constructed warranty shipment-by-return triangle, calendar return periods (B9)",
"Meeker, Hahn and Escobar (1998) Statistical Methods for Reliability Data, Wiley",
"Constructed validation data; no third-party license.",
c(label = "String", shipped = "Int64",
setNames(rep("Int64", length(RCOLS)), RCOLS)))
wp <- read_dataset("SURV_WARR_B9")
conv_calendar <- convert_reference(wp, RCOLS, "calendar", 1, "incomplete")
stopifnot(
conv_calendar$failures_used + conv_calendar$survivors_total +
conv_calendar$excluded_shipments == conv_calendar$shipped_total,
length(conv_calendar$records) == 27
)
PREP_CALENDAR <- list(shipped = "shipped", returns = as.list(RCOLS), return_axis = "calendar",
period_width = 1, period_unit = "month", last_period = "incomplete",
shipment_label = "label")
emit("STAT-SURV-WARRPREP-001",
"Warranty triangle conversion on the calendar axis with the final period incomplete",
PREP, "warrantyPreprocessMenuItem", PREP_REQ, "SURV_WARR_B9", PREP_CALENDAR,
conversion_outputs(conv_calendar),
c(CONVERSION_NOTES,
"The default 'incomplete' setting excludes calendar period 7: its 7 returns are reported as excluded failures, and units unreturned at the end of period 6 stay survivors right censored at (6 - i + 1) months."),
"nominal")
emit("STAT-SURV-WARRTAB-001",
"Derived warranty censoring table with interval records and right-censored survivors",
TABLE, "warrantyPreprocessMenuItem", PREP_REQ, "SURV_WARR_B9", PREP_CALENDAR,
table_outputs(conv_calendar),
c(CONVERSION_NOTES,
"The derived frequency table keeps one row per cohort and period with a frequency and never expands units into rows (manual :155-157); the result is a frame of row dicts, so numeric fields are compared positionally, the null end of right-censored rows and the string cohort and censoring columns are skipped."),
"nominal")
# =======================================================================================
# Dataset 2: age-axis triangle with the final period treated as complete.
ACOLS <- paste0("r", 1:5)
warr_age <- data.frame(
label = c("2026-01", "2026-02", "2026-03", "2026-04", "2026-05", "2026-06"),
shipped = c(200L, 180L, 190L, 170L, 160L, 140L),
r1 = c(4L, 5L, 6L, 3L, 4L, 2L),
r2 = c(6L, 4L, 5L, 5L, 2L, NA_integer_),
r3 = c(5L, 6L, 4L, 4L, NA_integer_, NA_integer_),
r4 = c(3L, 3L, 2L, NA_integer_, NA_integer_, NA_integer_),
r5 = c(2L, 3L, NA_integer_, NA_integer_, NA_integer_, NA_integer_)
)
export_dataset(warr_age, "SURV_WARR_AGE_B9",
"Constructed warranty shipment-by-return triangle, age return periods (B9)",
"Meeker, Hahn and Escobar (1998) Statistical Methods for Reliability Data, Wiley",
"Constructed validation data; no third-party license.",
c(label = "String", shipped = "Int64",
setNames(rep("Int64", length(ACOLS)), ACOLS)))
wa <- read_dataset("SURV_WARR_AGE_B9")
conv_age <- convert_reference(wa, ACOLS, "age", 1, "complete")
stopifnot(
conv_age$failures_used + conv_age$survivors_total == conv_age$shipped_total,
conv_age$excluded_failures == 0, conv_age$excluded_shipments == 0
)
emit("STAT-SURV-WARRPREP-002",
"Warranty triangle conversion on the age axis with the final period complete",
PREP, "warrantyPreprocessMenuItem", PREP_REQ, "SURV_WARR_AGE_B9",
list(shipped = "shipped", returns = as.list(ACOLS), return_axis = "age",
period_width = 1, period_unit = "month", last_period = "complete",
shipment_label = "label"),
conversion_outputs(conv_age),
c(CONVERSION_NOTES,
"Age-axis reading: column k counts returns in age period k (calendar period i + k - 1), so the last calendar period equals the number of shipment rows; the 'complete' setting includes it as a full period of exposure, and blank cells lie after the last calendar period."),
"axis_coverage")
# =======================================================================================
# Case 4: Weibull warranty prediction from the calendar triangle.
triangle_records <- data.frame(
start = vapply(conv_calendar$records, function(x) x$start, 0),
end = vapply(conv_calendar$records, function(x) if (is.na(x$end)) NA_real_ else x$end, 0),
frequency = vapply(conv_calendar$records, function(x) x$frequency, 0L)
)
triangle_survivors <- vapply(conv_calendar$cohorts, function(x) x$survivors, 0)
triangle_ages <- vapply(conv_calendar$cohorts, function(x) x$age, 0)
future_units_1 <- c(500, 300)
future_times_1 <- c(1.5, 6)
pred_weibull <- warranty_prediction_reference(
triangle_records, "weibull", triangle_survivors, triangle_ages,
future_units_1, future_times_1, 12, 24, 0.95, 1
)
emit("STAT-SURV-WARRPRED-001",
"Weibull warranty prediction from a calendar triangle with future shipments and a limit",
PRED, "warrantyPredictionMenuItem", PRED_REQ, "SURV_WARR_B9",
list(source = "triangle", shipped = "shipped", returns = as.list(RCOLS),
return_axis = "calendar", period_width = 1, period_unit = "month",
last_period = "incomplete", family = "weibull", horizon = 12,
warranty_limit = 24, future_shipments = list("500@1.5", "300@6"),
unit_cost = 3.2, confidence_level = 0.95),
prediction_outputs(pred_weibull, "weibull", triangle_survivors, triangle_ages,
future_units_1, future_times_1, 0.95),
c(PREDICTION_NOTES,
"The exposure cohorts are the converted shipment rows with survivors; the cap min(a_j+h, L) binds through the 24-month warranty duration and the future shipments contribute d F(min(h-s, L))."),
"nominal")
# =======================================================================================
# Dataset 3: arbitrary censoring table (exact, interval and right-censored rows).
censor <- data.frame(
start = c(8.3, 6, 12, 15.2, 12, 18, 18, 21.5, 24, 30, 24, 27.8),
end = c(8.3, 9, NA_real_, 15.2, 18, NA_real_, 24, 21.5, NA_real_, NA_real_, 30, 27.8),
freq = c(3L, 4L, 40L, 2L, 5L, 30L, 6L, 1L, 45L, 25L, 5L, 2L)
)
export_dataset(censor, "SURV_CENSOR_B9",
"Constructed warranty censoring table with exact, interval and right-censored records (B9)",
"Meeker, Hahn and Escobar (1998) Statistical Methods for Reliability Data, Wiley",
"Constructed validation data; no third-party license.",
c(start = "Float64", end = "Float64", freq = "Int64"))
cn <- read_dataset("SURV_CENSOR_B9")
right <- is.na(cn$end)
censor_ages <- sort(unique(cn$start[right]))
censor_survivors <- vapply(censor_ages, function(a) sum(cn$freq[right & cn$start == a]), 0)
censor_records <- data.frame(start = cn$start, end = cn$end, frequency = cn$freq)
future_units_2 <- 400
future_times_2 <- 2.5
pred_lognormal <- warranty_prediction_reference(
censor_records, "lognormal", censor_survivors, censor_ages,
future_units_2, future_times_2, 8, NULL, 0.90, 1
)
emit("STAT-SURV-WARRPRED-002",
"Lognormal warranty prediction from a censoring table without a warranty limit",
PRED, "warrantyPredictionMenuItem", PRED_REQ, "SURV_CENSOR_B9",
list(source = "censoring_table", start = "start", end = "end", frequency = "freq",
family = "lognormal", horizon = 8, future_shipments = list("400@2.5"),
confidence_level = 0.90, period_width = 1, period_unit = "month"),
prediction_outputs(pred_lognormal, "lognormal", censor_survivors, censor_ages,
future_units_2, future_times_2, 0.90),
c(PREDICTION_NOTES,
"The censoring-table source reads start, end and frequency columns directly; the risk-set cohorts are the distinct right-censoring ages with their surviving weights; no warranty limit means the cap is infinite."),
"axis_coverage")
cat("warranty references generated\n")
generators/r/timeseries_correlations.R
source("common.R", local = TRUE)
run <- generator_run(c("stats", "jsonlite", "urca"))
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = "1e-9",
rationale = paste("R closed-form reference:", why)
)
exact <- function(value, why) list(comparison = "exact", value = as.integer(value), rationale = why)
absent <- function(why) list(comparison = "absent", rationale = why)
label <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, title, menu, configuration, outputs, notes, kind = "nominal",
version = "1.0.0", citation = "R stats::acf, stats::pacf and stats::diff documentation") {
write_expected("timeseries", id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run
))
write_case("timeseries", id, list(
schema_version = "2.0.0", case_id = id, version = version,
family = "timeseries", entry_point = "tabelz.executor.time_series",
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset_id, ".json"), sha256 = ""),
excluded_rows = list(), configuration = configuration, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/timeseries/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats",
citation = citation,
generator = list(path = "generators/r/timeseries_correlations.R", sha256 = ""),
independent = TRUE,
convention_notes = c(notes, "R re-reads the exported CSV before calculation.")
), supported = TRUE, unsupported_reason = NULL,
requirement_ids = list("REQ-STAT-TS-001"), risk_ids = list("RISK-REFERENCE-001"),
review_reference = NULL
))
}
# Guided-differencing diagnostics replication. The workflow evaluates every
# stage with ADF and KPSS under a constant-only deterministic specification
# (the workflow's default stationarity term) and selects the smallest stage
# order that both tests call stationary at 5 percent. The functions below
# re-implement the conventions of statsmodels.tsa.stattools.adfuller
# (regression "c", autolag "AIC") and kpss (nlags "auto") in R with lm():
#
# ADF: augmented regression of d[y] on the lagged level, lagged differences
# and a constant. Candidate lags 0..ceil(12*(n/100)^0.25), capped at
# n/2 - 2, are scored by AIC on the common sample trimmed to the maximum
# lag; the final regression refits on the larger sample that the selected
# lag allows. The statistic is the t-value of the lagged level.
#
# KPSS: level-stationarity residuals, Bartlett long-run variance with the
# data-dependent lag of Hobijn, Franses and Ooms (1998), statistic per
# Kwiatkowski et al. (1992), equations 10-11.
#
# Decisions at 5 percent use the MacKinnon critical value for the constant
# case (urca::ur.df tables, keyed by the estimation sample size only) for
# ADF and the published critical value 0.463 for KPSS. Test p-values are
# not compared: published response-surface and table implementations differ
# in digits while agreeing on these decisions.
adf_maxlag <- function(n) {
candidate <- as.integer(ceiling(12.0 * (n / 100.0)^0.25))
min(n %/% 2 - 2L, candidate)
}
search_aic <- function(d, level, lags, maxlag) {
rows <- (maxlag + 1L):(length(d))
frame <- data.frame(y = d[rows], level = level[rows])
for (i in seq_len(lags)) frame[[paste0("dl", i)]] <- d[rows - i]
fit <- stats::lm(y ~ ., data = frame)
nobs_fit <- length(rows)
ssr <- sum(stats::residuals(fit)^2)
p <- 2L + lags
nobs_fit * (log(2 * pi) + 1 + log(ssr / nobs_fit)) + 2 * p
}
adf_rep <- function(x) {
n <- length(x)
d <- diff(x)
maxlag <- adf_maxlag(n)
level <- x[-n]
aics <- vapply(seq.int(0L, maxlag), function(l) search_aic(d, level, l, maxlag), 0)
best <- which.min(aics) - 1L
rows <- (best + 1L):(n - 1L)
frame <- data.frame(y = d[rows], level = x[rows])
for (i in seq_len(best)) frame[[paste0("dl", i)]] <- d[rows - i]
fit <- stats::lm(y ~ ., data = frame)
nobs <- length(rows)
set.seed(1L)
cv5 <- slot(urca::ur.df(stats::rnorm(nobs), type = "drift",
selectlags = "Fixed", lags = 0L), "cval")["tau2", "5pct"]
list(
statistic = summary(fit)$coefficients["level", "t value"],
lags = best, nobs = nobs, critical_5pct = cv5, rejects_unit_root = summary(fit)$coefficients["level", "t value"] < cv5
)
}
kpss_rep <- function(x) {
n <- length(x)
e <- x - mean(x)
covlags <- as.integer(n^(2.0 / 9.0))
s0 <- sum(e^2) / n
s1 <- 0
for (i in seq_len(covlags)) {
prod_i <- sum(e[(i + 1):n] * e[1:(n - i)]) / (n / 2.0)
s0 <- s0 + prod_i
s1 <- s1 + i * prod_i
}
s_ratio <- s1 / s0
nlags <- min(as.integer(1.1447 * ((s_ratio^2)^(1.0 / 3.0)) * n^(1.0 / 3.0)), n - 1L)
eta <- sum(cumsum(e)^2) / (n^2)
s_hat <- sum(e^2)
for (i in seq_len(nlags)) {
s_hat <- s_hat + 2 * sum(e[(i + 1):n] * e[1:(n - i)]) * (1 - i / (nlags + 1))
}
s_hat <- s_hat / n
stat <- eta / s_hat
list(
statistic = stat, lags = nlags,
critical_5pct = 0.463, rejects_stationarity = stat > 0.463
)
}
# One guided-differencing stage, matching the workflow's combined criterion.
stage_diagnostics <- function(values) {
adf <- adf_rep(values)
kpss <- kpss_rep(values)
assessment <- if (adf$rejects_unit_root && !kpss$rejects_stationarity) "stationary" else if (!adf$rejects_unit_root && kpss$rejects_stationarity) "difference_candidate" else "inconclusive"
list(adf = adf, kpss = kpss, assessment = assessment)
}
# Stage outputs shared by the selection cases; p-values are deliberately not
# compared (see the header comment above).
stage_outputs <- function(prefix, values) {
diag <- stage_diagnostics(values)
out <- list()
out[[paste0(prefix, "/sample_count")]] <- exact(length(values), "Stage sample count is exact.")
out[[paste0(prefix, "/stationarity/assessment")]] <- label(diag$assessment, "Combined ADF/KPSS stage decision at 5 percent.")
out[[paste0(prefix, "/stationarity/adf_statistic")]] <- tol(diag$adf$statistic, "ADF t-value on the lagged level; owner 1e-9 tier.")
out[[paste0(prefix, "/stationarity/adf_lags")]] <- exact(diag$adf$lags, "AIC-selected ADF lag order on the common search sample.")
out[[paste0(prefix, "/stationarity/kpss_statistic")]] <- tol(diag$kpss$statistic, "KPSS level-stationarity statistic; owner 1e-9 tier.")
out[[paste0(prefix, "/stationarity/kpss_lags")]] <- exact(diag$kpss$lags, "Hobijn-Franses-Ooms automatic Bartlett lag.")
list(outputs = out, diagnostics = diag)
}
selection_notes <- c(
"Deterministic term: constant only, the workflow's default stationarity term (ADF regression 'c', KPSS level stationarity).",
"ADF lag rule: AIC over 0..ceil(12*(n/100)^0.25) capped at n/2-2 on the common trimmed sample; final fit on the larger sample the selected lag allows.",
"KPSS lag rule: automatic data-dependent Bartlett lag (Hobijn, Franes and Ooms 1998).",
"Stage rule: the smallest ordinary order 0..2 whose ADF rejects a unit root and whose KPSS does not reject level stationarity at 5 percent; if none qualifies, no order is recommended and order zero is displayed.",
"Stage p-values are not compared across implementations: response-surface and table versions of the published critical values differ in digits while agreeing on these decisions."
)
index <- seq_len(36L)
dataset_id <- "TS_CORRELATIONS_01"
raw <- data.frame(value = as.double(20 + 0.17 * index + sin(index * 0.39) +
0.37 * cos(index * 1.13)))
export_dataset(raw, "TS_CORRELATIONS_01", "Constructed ordered series with trend and oscillation",
"R stats::acf and stats::diff documentation",
"Constructed validation data; no third-party license.", c(value = "Float64"))
d <- read_dataset("TS_CORRELATIONS_01")
series <- d$value
acf <- stats::acf(series, lag.max = 5L, plot = FALSE, type = "correlation")$acf[, 1, 1]
pacf <- stats::pacf(series, lag.max = 5L, plot = FALSE)$acf[, 1, 1]
emit("STAT-TS-ACF-001", "Biased sample ACF and lag-one PACF", "timeSeriesAcfPacfMenuItem",
list(response_column = "value", task = "correlations", maximum_lag = 5L),
list("/sample_count" = exact(length(series), "All 36 values are finite."),
"/correlations/acf/0" = tol(acf[1], "Lag-zero ACF equals one; owner 1e-9 tier."),
"/correlations/acf/1" = tol(acf[2], "Biased lag-one ACF divides by n; owner 1e-9 tier."),
"/correlations/acf/2" = tol(acf[3], "Biased lag-two ACF divides by n; owner 1e-9 tier."),
"/correlations/pacf/1" = tol(pacf[1], "Yule-Walker lag-one PACF; owner 1e-9 tier.")),
c("Manual chapter 12 ACF/PACF task: autocorrelation uses a fixed n denominator.",
"R acf(type=correlation) uses the biased n denominator and R pacf uses Yule-Walker."))
# Guided differencing: transformation arithmetic, checked independently of the
# stage-selection oracle (STAT-TS-DIFF-002). The old version of this case
# applied two unconditional differences; manual chapter 12 instead tests
# through the configured maximum and uses the first stationary stage.
first <- diff(series, differences = 1L)
emit("STAT-TS-DIFF-001", "First ordinary difference of the guided workflow", "timeSeriesStationarityMenuItem",
list(response_column = "value", task = "difference", difference_order = 2L,
maximum_lag = 5L),
list("/sample_count" = exact(length(first), "One leading row is unavailable after first differencing."),
"/points/0/transformed" = tol(first[1], "x2 - x1; owner 1e-9 tier."),
"/points/1/transformed" = tol(first[2], "x3 - x2; owner 1e-9 tier.")),
c("Manual chapter 12 guided differencing retains leading unavailable values as missing in a created column.",
"Transformation arithmetic only: the reference differences once because the guided workflow selects order one on this series (selection checked by STAT-TS-DIFF-002 on a well-conditioned dataset; this deterministic construction makes the ADF regression near-singular, so lag counts and t-values here are not reproducibly defined across independent least-squares implementations)."),
kind = "row_alignment", version = "2.0.0")
# Selection case 1: trend-bearing local-level series; the first stationary
# stage is order one.
dataset_id <- "TS_DIFF_GUIDED_01"
set.seed(7L)
n_guided <- 60L
guided_raw <- data.frame(value = cumsum(stats::rnorm(n_guided, sd = 2.0)) +
stats::rnorm(n_guided, sd = 1.0))
export_dataset(guided_raw, dataset_id,
"Constructed local-level series: random walk plus measurement noise",
"R stats::rnorm with seed 7; manual chapter 12 guided differencing",
"Constructed validation data; no third-party license.", c(value = "Float64"))
guided <- read_dataset(dataset_id)$value
stages_guided <- list(
stage_outputs("/differencing_stages/0", guided),
stage_outputs("/differencing_stages/1", diff(guided, differences = 1L)),
stage_outputs("/differencing_stages/2", diff(guided, differences = 2L))
)
selected_guided <- Position(function(item) item$diagnostics$assessment == "stationary", stages_guided) - 1L
outputs_guided <- list(
"/recommended_difference_order" = exact(selected_guided, "Smallest ordinary order whose stage passes both tests."),
"/difference_order" = exact(selected_guided, "The workflow uses the recommended order for the transformed column."),
"/sample_count" = exact(length(guided) - selected_guided, "Transformed sample count at the selected stage."),
"/stationarity/assessment" = label(stages_guided[[1]]$diagnostics$assessment, "Source-level combined decision."),
"/points/0/transformed" = tol(diff(guided, differences = 1L)[1], "First transformed value at the selected stage; owner 1e-9 tier.")
)
for (item in stages_guided) outputs_guided <- c(outputs_guided, item$outputs)
emit("STAT-TS-DIFF-002", "Guided selection of the first stationary difference order",
"timeSeriesStationarityMenuItem",
list(response_column = "value", task = "difference", difference_order = 2L, maximum_lag = 5L),
outputs_guided,
c(selection_notes,
"Constructed with R seed 7: a random walk plus independent measurement noise is difference-stationary at order one."))
# Selection case 2: original series already stationary; order zero is the
# first tested stationary stage and no differenced column is recommended.
dataset_id <- "TS_DIFF_STATIONARY_01"
set.seed(7L)
stationary_raw <- data.frame(value = as.numeric(stats::arima.sim(list(ar = 0.55), n = 60L, sd = 1.8)))
export_dataset(stationary_raw, dataset_id, "Constructed stationary AR(1) series",
"R stats::arima.sim with seed 7; manual chapter 12 guided differencing",
"Constructed validation data; no third-party license.", c(value = "Float64"))
stationary_series <- read_dataset(dataset_id)$value
stages_stationary <- list(
stage_outputs("/differencing_stages/0", stationary_series),
stage_outputs("/differencing_stages/1", diff(stationary_series, differences = 1L)),
stage_outputs("/differencing_stages/2", diff(stationary_series, differences = 2L))
)
selected_stationary <- Position(function(item) item$diagnostics$assessment == "stationary", stages_stationary) - 1L
outputs_stationary <- list(
"/recommended_difference_order" = exact(selected_stationary, "The original series is the first tested stationary stage."),
"/difference_order" = exact(selected_stationary, "Order zero keeps the original values as the transformed column."),
"/sample_count" = exact(length(stationary_series) - selected_stationary, "No observations are lost at order zero."),
"/stationarity/assessment" = label(stages_stationary[[1]]$diagnostics$assessment, "Source-level combined decision."),
"/points/0/transformed" = tol(stationary_series[1], "At order zero the transformed column equals the original series; owner 1e-9 tier.")
)
for (item in stages_stationary) outputs_stationary <- c(outputs_stationary, item$outputs)
emit("STAT-TS-DIFF-003", "Original series selected: no differencing recommended",
"timeSeriesStationarityMenuItem",
list(response_column = "value", task = "stationarity", difference_order = 2L, maximum_lag = 5L),
outputs_stationary,
c(selection_notes,
"Constructed with R seed 7: an AR(1) series with modest persistence is level-stationary at order zero."),
kind = "boundary")
# Selection case 3: no tested order qualifies; nothing is recommended and
# order zero is displayed.
dataset_id <- "TS_DIFF_NONE_01"
set.seed(7L)
none_raw <- data.frame(value = cumsum(cumsum(cumsum(stats::rnorm(60L, sd = 1.0)))))
export_dataset(none_raw, dataset_id,
"Constructed triple-integrated series; no tested order is stationary",
"R stats::rnorm with seed 7; manual chapter 12 guided differencing",
"Constructed validation data; no third-party license.", c(value = "Float64"))
none_series <- read_dataset(dataset_id)$value
stages_none <- list(
stage_outputs("/differencing_stages/0", none_series),
stage_outputs("/differencing_stages/1", diff(none_series, differences = 1L)),
stage_outputs("/differencing_stages/2", diff(none_series, differences = 2L))
)
eligible_none <- Filter(function(item) item$diagnostics$assessment == "stationary", stages_none)
stopifnot(length(eligible_none) == 0L)
outputs_none <- list(
"/recommended_difference_order" = absent("No order from zero through the configured maximum met the combined criteria."),
"/difference_order" = exact(0L, "Without a recommended order the workflow displays order zero."),
"/sample_count" = exact(length(none_series), "The transformed column is the original series at order zero."),
"/stationarity/assessment" = label(stages_none[[1]]$diagnostics$assessment, "Source-level combined decision."),
"/points/0/transformed" = tol(none_series[1], "At order zero the transformed column equals the original series; owner 1e-9 tier.")
)
for (item in stages_none) outputs_none <- c(outputs_none, item$outputs)
emit("STAT-TS-DIFF-004", "No eligible differencing stage", "timeSeriesStationarityMenuItem",
list(response_column = "value", task = "difference", difference_order = 2L, maximum_lag = 5L),
outputs_none,
c(selection_notes,
"Constructed with R seed 7: a triple-integrated random walk stays non-stationary through ordinary difference order two."),
kind = "boundary")
cat("time-series correlation and differencing references generated\n")
generators/r/timeseries_cross_correlation.R
source("common.R", local = TRUE)
# References for tabelz.analysis.cross_correlation:compute_cross_correlation
# (manual chapter 60). For a signed lag k, x(t) is paired with y(t + k) over
# the overlapping valid positions and the cross-correlation is the Pearson
# correlation of those pairs (lines 29-31); lags with fewer than 3 pairs are
# omitted (lines 35-36); the band is +/- 1 / sqrt(m) for overlap m (line 42);
# differencing makes the leading position and any position next to an
# unavailable one unavailable, and excluded or missing rows keep their
# positions (lines 58-65); prewhitening uses OLS AR(p) residuals (lines 46-49).
# R's ccf() is NOT used: it centres with global means, divides by n and pairs
# x(t + k) with y(t), so its lag sign is the opposite.
run <- generator_run(c("stats", "jsonlite"))
family <- "timeseries"
entry <- "tabelz.analysis.cross_correlation:compute_cross_correlation"
generator <- "generators/r/timeseries_cross_correlation.R"
tol <- function(value, why) list(
comparison = "tolerance", value = dec(unname(value)), absolute_tolerance = "0",
relative_tolerance = "1e-9", rationale = paste("Closed-form Pearson correlation; owner 1e-9 tier.", why))
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
emit <- function(id, title, dataset, config, outputs, notes, kind = "nominal", excluded = integer(), error = NULL) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id,
outputs = if (is.null(error)) outputs else setNames(list(), character()),
expected_error = error, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = entry,
menu_items = list("crossCorrelationMenuItem"), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(
oracle_class = "independent_implementation", source = "R stats::cor on explicitly aligned lag pairs",
citation = "R stats::cor and stats::lm documentation",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes, "Manual chapter 60 lines 29-65 define the lag pairing, band, differencing and positions.",
"All reference calculations re-read the exported CSV."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-TS-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
difference <- function(v, order) if (order == 0) v else c(NA, diff(v))
ar_residuals <- function(v, p) {
# OLS AR(p) with an intercept over the one gap-free block, NA elsewhere.
ok <- which(!is.na(v)); start <- min(ok); stop <- max(ok); b <- v[start:stop]
m <- length(b)
design <- sapply(seq_len(p), function(i) b[(p - i + 1):(m - i)])
fit <- lm(b[(p + 1):m] ~ design)
out <- rep(NA_real_, length(v)); out[(start + p):stop] <- residuals(fit)
out
}
lag_table <- function(x, y, max_lag) {
n <- length(x); rows <- list()
for (k in -max_lag:max_lag) {
if (k >= 0) { a <- x[1:(n - k)]; b <- y[(1 + k):n] } else { a <- x[(1 - k):n]; b <- y[1:(n + k)] }
ok <- !is.na(a) & !is.na(b)
if (sum(ok) < 3) { rows[[length(rows) + 1]] <- list(lag = k, omitted = TRUE); next }
r <- cor(a[ok], b[ok])
rows[[length(rows) + 1]] <- list(lag = k, omitted = FALSE, r = r, m = sum(ok))
}
rows
}
lag_outputs <- function(rows) {
kept <- Filter(function(row) !row$omitted, rows)
out <- list()
for (i in seq_along(kept)) {
row <- kept[[i]]; node <- sprintf("/lag_rows/%d", i - 1L)
out[[paste0(node, "/lag")]] <- exact(as.integer(row$lag), "Signed lag in ascending order.")
out[[paste0(node, "/correlation")]] <- tol(row$r, "Pearson correlation of x(t) with y(t + k).")
out[[paste0(node, "/overlap")]] <- exact(as.integer(row$m), "Number of valid aligned pairs.")
out[[paste0(node, "/inside_band")]] <- exact(abs(row$r) <= 1 / sqrt(row$m), "|r| <= 1 / sqrt(m).")
}
out[["/omitted_lag_count"]] <- exact(as.integer(sum(vapply(rows, `[[`, TRUE, "omitted"))), "Lags below 3 pairs.")
peak <- kept[[which.max(vapply(kept, function(row) abs(row$r), 0))]]
out[["/peak_lag"]] <- exact(as.integer(peak$lag), "Lag with the largest absolute correlation.")
out
}
# ---- data: y is x delayed by 3 plus small noise ------------------------------
set.seed(20260930)
n <- 60L
x <- round(cumsum(rnorm(n + 3)) * 0.5 + rnorm(n + 3), 6)
y <- round(c(rep(0, 3), x[1:n])[1:n] + rnorm(n, 0, 0.3), 6)
x <- x[1:n]
export_dataset(data.frame(t = seq_len(n), x = x, y = y), "TS_XCORR_SHIFT",
"Constructed: y(t) = x(t - 3) + N(0, 0.3^2) noise, seed 20260930, rounded to 6 decimals",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(t = "Int64", x = "Float64", y = "Float64"))
d <- read_dataset("TS_XCORR_SHIFT")
rows <- lag_table(d$x, d$y, 8)
out <- c(lag_outputs(rows), list(
"/peak_label" = exact("x leads y by 3", "A positive peak is labelled 'X leads Y by d' (line 32-34)."),
"/max_lag_used" = exact(8L, "The requested maximum lag."),
"/band_basis" = exact("nominal", "No prewhitening.")))
emit("STAT-TS-XCORR-001", "A delayed copy peaks at its delay", "TS_XCORR_SHIFT",
list(series_x = "x", series_y = "y", max_lag = 8L), out,
c("Manual line 81-83: a synthetic shifted pair y(t) = x(t - d) peaks at d with the 'X leads Y' label."))
emit("STAT-TS-XCORR-002", "First differences of both series", "TS_XCORR_SHIFT",
list(series_x = "x", series_y = "y", max_lag = 5L, difference_order_x = 1L, difference_order_y = 1L),
lag_outputs(lag_table(difference(d$x, 1), difference(d$y, 1), 5)),
c("Both series are differenced; the leading position is unavailable (line 58-60)."), kind = "axis_coverage")
wx <- ar_residuals(difference(d$x, 0), 2); wy <- ar_residuals(difference(d$y, 0), 2)
out <- lag_outputs(lag_table(wx, wy, 5))
out[["/band_basis"]] <- exact("prewhitened", "Bands are computed on the AR residuals (line 48-50).")
out[["/prewhiten_order"]] <- exact(2L, "The declared AR order.")
emit("STAT-TS-XCORR-003", "Prewhitening with an OLS AR(2) fit", "TS_XCORR_SHIFT",
list(series_x = "x", series_y = "y", max_lag = 5L, prewhiten = TRUE, prewhiten_order = 2L), out,
c("Each series is replaced by the residuals of lm(x_t ~ x_{t-1} + x_{t-2}) with an intercept over its gap-free block.",
"The manual says 'a deterministic OLS AR(p) fit' without stating the intercept; this reference includes one."),
kind = "axis_coverage")
# ---- missing values and excluded rows keep their positions ------------------
m <- read_dataset("TS_XCORR_SHIFT")
m$x[c(10, 31)] <- NA
m$y[45] <- NA
export_dataset(m, "TS_XCORR_MISSING", "TS_XCORR_SHIFT with x missing at rows 10 and 31 and y at row 45 (1-based)",
"R stats::rnorm documentation", "Constructed validation data; no third-party license.",
c(t = "Int64", x = "Float64", y = "Float64"))
mm <- read_dataset("TS_XCORR_MISSING")
excluded <- c(4L, 20L)
xa <- mm$x; ya <- mm$y; xa[excluded + 1L] <- NA; ya[excluded + 1L] <- NA
out <- c(lag_outputs(lag_table(xa, ya, 6)), list(
"/excluded_count" = exact(2L, "Two excluded rows."),
"/source_count" = exact(n, "Every source row is counted.")))
emit("STAT-TS-XCORR-004", "Missing and excluded rows stay as unavailable positions", "TS_XCORR_MISSING",
list(series_x = "x", series_y = "y", max_lag = 6L), out,
c("Rows 4 and 20 (0-based) are excluded and three values are missing; the series never closes up (line 61-65)."),
kind = "missing_excluded", excluded = excluded)
# ---- boundary: few rows leave lags without three pairs ---------------------
export_dataset(data.frame(x = c(1.2, 3.4, 2.2, 5.1, 4.0, 6.3), y = c(2.0, 1.1, 3.9, 2.7, 5.5, 4.2)),
"TS_XCORR_SHORT", "Constructed: six paired values", "Hand-constructed",
"Constructed validation data; no third-party license.", c(x = "Float64", y = "Float64"))
s <- read_dataset("TS_XCORR_SHORT")
out <- c(lag_outputs(lag_table(s$x, s$y, 5)),
list("/max_lag_used" = exact(5L, "The lag range is capped at n - 1 = 5.")))
emit("STAT-TS-XCORR-005", "Lags with fewer than three pairs are omitted", "TS_XCORR_SHORT",
list(series_x = "x", series_y = "y", max_lag = 30L), out,
c("With n = 6 the lags |k| >= 4 have at most 2 pairs and are omitted (line 35-36); max_lag 30 is reduced to 5."),
kind = "boundary")
export_dataset(data.frame(x = c(1, 2), y = c(3, 1)), "TS_XCORR_TWO", "Constructed: two paired values",
"Hand-constructed", "Constructed validation data; no third-party license.", c(x = "Float64", y = "Float64"))
emit("STAT-TS-XCORR-006", "Two rows give an insufficient-data outcome", "TS_XCORR_TWO",
list(series_x = "x", series_y = "y", max_lag = 1L),
list("/status" = exact("insufficient_data", "No lag reaches 3 pairs (line 77-78)."),
"/peak_lag" = list(comparison = "absent", rationale = "No lags are invented.")),
c("Two rows give at most two pairs at every lag."), kind = "boundary")
# ---- hard numeric: a large offset --------------------------------------------
h <- read_dataset("TS_XCORR_SHIFT")
h$x <- h$x + 1e8
export_dataset(h, "TS_XCORR_OFFSET", "TS_XCORR_SHIFT with x shifted by 1e8", "R stats::rnorm documentation",
"Constructed validation data; no third-party license.", c(t = "Int64", x = "Float64", y = "Float64"))
ho <- read_dataset("TS_XCORR_OFFSET")
emit("STAT-TS-XCORR-007", "A 1e8 offset leaves the correlations unchanged", "TS_XCORR_OFFSET",
list(series_x = "x", series_y = "y", max_lag = 4L), lag_outputs(lag_table(ho$x, ho$y, 4)),
c("Pearson correlation is shift invariant; R centres each lag window before multiplying."), kind = "hard_numeric")
# ---- invalid inputs --------------------------------------------------------
dup <- read_dataset("TS_XCORR_SHIFT")
dup$t[12] <- dup$t[11]
export_dataset(dup, "TS_XCORR_DUPKEY", "TS_XCORR_SHIFT with a duplicated time key at row 12 (1-based)",
"Hand-constructed", "Constructed validation data; no third-party license.",
c(t = "Int64", x = "Float64", y = "Float64"))
emit("STAT-TS-XCORR-008", "A duplicated time key is refused", "TS_XCORR_DUPKEY",
list(series_x = "x", series_y = "y", time_column = "t", max_lag = 4L), list(),
c("Line 18-21: duplicate keys are rejected; keys are never interpolated."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "uplicate"))
emit("STAT-TS-XCORR-009", "A maximum lag above 250 is refused", "TS_XCORR_SHIFT",
list(series_x = "x", series_y = "y", max_lag = 251L), list(),
c("Line 22: a fixed ceiling of 250."), kind = "invalid_input",
error = list(error_class = "ValueError", message_contains = "250"))
generators/r/timeseries_models.R
source("common.R", local = TRUE)
# References for Fit ARMA / ARIMA, Forecast, Dynamic Regression, VAR / VECM,
# GARCH, Zivot-Andrews, Compare & Validate and the stationarity verdict (manual
# chapter 12). Point forecasts of the naive, seasonal-naive, mean and drift
# benchmarks are analytic; their intervals come from the forecast package,
# whose innovation-variance divisor the manual does not define. ARIMA and
# regression with ARIMA errors use stats::arima(method = "ML") and compare
# parameters that have one meaning in both programs; parameter standard errors
# are deferred because the manual does not say how the information matrix is
# estimated. GARCH and Zivot-Andrews follow the conventions ruled on in
# statistician issue #9 (the arch backcast; Baum's AIC lag selection), each
# reproduced independently in R below. VAR forecasts use vars::VAR; Johansen
# traces use urca::ca.jo.
suppressPackageStartupMessages({ library(forecast); library(vars); library(urca) })
run <- generator_run(c("stats", "jsonlite", "forecast", "vars", "urca"))
family <- "timeseries"
generator <- "generators/r/timeseries_models.R"
ep <- "tabelz.executor.time_series"
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
num <- function(value, why, tier = "1e-9") list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = tier, rationale = why)
fit6 <- function(value, why) num(value, why, "1e-6")
absent <- function(why) list(comparison = "absent", rationale = why)
emit <- function(id, menu, title, dataset, config, outputs, notes, source, citation, kind = "nominal",
deferred = list(), oracle_class = "independent_implementation", excluded = integer()) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = deferred, generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = ep,
menu_items = list(menu), title = title, case_kind = kind,
dataset = list(path = paste0("reference_data/datasets/", dataset, ".json"), sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = oracle_class, source = source, citation = citation,
generator = list(path = generator, sha256 = ""), independent = TRUE, convention_notes = as.list(notes)),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-TS-001"),
risk_ids = list("RISK-NUMERIC-001"), review_reference = NULL))
}
defer <- function(path, reason) list(path = path, reason = reason)
rows <- function(prefix, index, field) paste0("/", prefix, "/", index, "/", field)
# ---- datasets --------------------------------------------------------------------------------
set.seed(20260924)
n <- 60
ar <- as.numeric(10 + arima.sim(list(ar = 0.6), n = n))
rw <- 50 + cumsum(rnorm(n))
x <- rnorm(n, 5, 2)
dyn <- as.numeric(2 + 1.5 * x + arima.sim(list(ar = 0.5), n = n))
s4 <- rep(c(3, -1, 2, -4), n / 4) + 20 + rnorm(n, 0, 0.5)
export_dataset(data.frame(ar = ar, rw = rw, x = x, dyn = dyn, s4 = s4), "TS_MODELS_01",
"Simulated (R 4.6.1, set.seed 20260924): AR(1) about 10, random walk, predictor x, regression with AR(1) errors, period-4 seasonal series; 60 rows",
"Simulated with stats::arima.sim and stats::rnorm", "Constructed validation data; no third-party license.",
c(ar = "Float64", rw = "Float64", x = "Float64", dyn = "Float64", s4 = "Float64"))
d <- read_dataset("TS_MODELS_01")
set.seed(20260925)
m <- 100
v <- matrix(0, m, 2)
A <- matrix(c(0.5, 0.2, -0.1, 0.4), 2, 2)
for (i in 2:m) v[i, ] <- c(1, -0.5) + A %*% v[i - 1, ] + rnorm(2, 0, c(1, 0.7))
c1 <- cumsum(rnorm(m))
c2 <- 0.8 * c1 + rnorm(m, 0, 0.5)
export_dataset(data.frame(v1 = v[, 1], v2 = v[, 2], c1 = c1, c2 = c2), "TS_SYSTEM_01",
"Simulated (R 4.6.1, set.seed 20260925): stationary VAR(1) pair v1, v2 and cointegrated pair c1, c2; 100 rows",
"Simulated with stats::rnorm", "Constructed validation data; no third-party license.",
c(v1 = "Float64", v2 = "Float64", c1 = "Float64", c2 = "Float64"))
s <- read_dataset("TS_SYSTEM_01")
set.seed(20260926)
g <- 600
e <- numeric(g); h <- numeric(g); h[1] <- 0.1 / (1 - 0.1 - 0.8)
for (i in seq_len(g)) {
if (i > 1) h[i] <- 0.1 + 0.1 * e[i - 1]^2 + 0.8 * h[i - 1]
e[i] <- sqrt(h[i]) * rnorm(1)
}
tt <- 1:g
za <- 0.5 + 0.02 * tt + ifelse(tt > 300, 3 + 0.01 * (tt - 300), 0) + rnorm(g)
export_dataset(data.frame(ret = 0.05 + e, level = 100 + cumsum(0.05 + e), za = za), "TS_LONG_01",
"Simulated (R 4.6.1, set.seed 20260926): GARCH(1,1) returns (mu 0.05, omega 0.1, alpha 0.1, beta 0.8), their cumulative level, and a trend-stationary series with a level and slope break after row 300; 600 rows",
"Simulated with stats::rnorm", "Constructed validation data; no third-party license.",
c(ret = "Float64", level = "Float64", za = "Float64"))
l <- read_dataset("TS_LONG_01")
# stats::arima's default optim tolerance stopped short of the ML estimate (AR 8e-6
# relative off; a tighter run raises the log-likelihood), so the references use this.
tight_optim <- list(reltol = 1e-15, maxit = 100000)
# Stationarity verdict data (replacing TS_LONG_01's drifting level and GARCH returns,
# whose verdicts are legitimately "inconclusive"): a driftless random walk and iid
# normal noise, each checked to give the same verdict at every lag choice below.
set.seed(20260927)
export_dataset(data.frame(walk = cumsum(rnorm(600)), noise = rnorm(600)), "TS_STATIONARITY_01",
"Simulated (R 4.6.1, set.seed 20260927): a driftless Gaussian random walk and iid N(0, 1) noise; 600 rows",
"Simulated with stats::rnorm", "Constructed validation data; no third-party license.",
c(walk = "Float64", noise = "Float64"))
st <- read_dataset("TS_STATIONARITY_01")
verdict_is_robust <- function(x, adf_rejects, kpss_rejects) {
adf <- sapply(0:12, function(k) {
test <- ur.df(x, type = "drift", lags = k)
test@teststat[1] < test@cval[1, "5pct"]
})
kpss <- sapply(c("short", "long"), function(k) {
test <- ur.kpss(x, type = "mu", lags = k)
test@teststat > test@cval[1, "5pct"]
})
all(adf == adf_rejects) && all(kpss == kpss_rejects)
}
if (!verdict_is_robust(st$walk, FALSE, TRUE)) stop("the random walk verdict depends on the lag choice")
if (!verdict_is_robust(st$noise, TRUE, FALSE)) stop("the noise verdict depends on the lag choice")
# ---- benchmark forecasts ---------------------------------------------------------------------
fc_cite <- "forecast::naive, snaive, meanf and rwf documentation; Hyndman and Athanasopoulos, Forecasting: Principles and Practice, section 5.5"
bench <- function(id, title, model, series, extra, point, reference, notes) {
out <- list()
for (k in 0:2) {
out[[rows("forecast_rows", k, "forecast")]] <- num(point[k + 1], "Analytic point forecast.")
out[[rows("forecast_rows", k, "lower")]] <- num(reference$lower[k + 1, 1], "forecast package 95 percent interval.")
out[[rows("forecast_rows", k, "upper")]] <- num(reference$upper[k + 1, 1], "forecast package 95 percent interval.")
}
out[["/forecast_rows/3"]] <- absent("Horizon 3.")
emit(id, "timeSeriesForecastMenuItem", title, "TS_MODELS_01",
c(list(method = "forecast", response_column = series, model = model, horizon = 3L), extra), out,
c("Point forecasts follow the benchmark definitions; intervals are Gaussian.", notes),
"R forecast package", fc_cite)
}
y <- d$rw; T <- length(y)
bench("STAT-TS-FC-NAIVE-001", "Naive forecast", "naive", "rw", list(), rep(y[T], 3),
naive(y, h = 3, level = 95), "Naive: the last value, interval scale sqrt(h).")
bench("STAT-TS-FC-MEAN-001", "Mean forecast", "mean", "rw", list(), rep(mean(y), 3),
meanf(y, h = 3, level = 95), "Mean: the sample mean; forecast::meanf uses a t quantile, which the manual does not settle.")
drift <- (y[T] - y[1]) / (T - 1)
bench("STAT-TS-FC-DRIFT-001", "Drift forecast with the manual's interval scale", "drift", "rw", list(),
y[T] + drift * 1:3, rwf(y, h = 3, drift = TRUE, level = 95),
"Chapter 12: drift fitted values are one-step forecasts plus the drift; interval scale sqrt(h(1 + h/(T-1))).")
z4 <- d$s4
bench("STAT-TS-FC-SNAIVE-001", "Seasonal naive forecast", "seasonal_naive", "s4", list(season_length = 4L),
z4[T - 4 + 1:3], snaive(ts(z4, frequency = 4), h = 3, level = 95),
"Seasonal naive: the value one season earlier.")
# ---- ARIMA -----------------------------------------------------------------------------------
arima_case <- function(id, title, series, order, term, fit, notes) {
pr <- predict(fit, n.ahead = 3)
z <- qnorm(0.975)
out <- list()
for (k in 0:2) {
out[[rows("forecast_rows", k, "forecast")]] <- fit6(pr$pred[k + 1], "stats::predict.Arima.")
out[[rows("forecast_rows", k, "lower")]] <- fit6(pr$pred[k + 1] - z * pr$se[k + 1], "Gaussian 95 percent interval.")
out[[rows("forecast_rows", k, "upper")]] <- fit6(pr$pred[k + 1] + z * pr$se[k + 1], "Gaussian 95 percent interval.")
}
cf <- coef(fit)
np <- length(cf)
out[[paste0("/parameters/", np - 1, "/term")]] <- exact("ar.L1", "The AR coefficient follows the constant.")
out[[paste0("/parameters/", np - 1, "/estimate")]] <- fit6(cf[["ar1"]], "stats::arima ML AR coefficient.")
out[[paste0("/parameters/", np, "/term")]] <- exact("sigma2", "Innovation variance last.")
out[[paste0("/parameters/", np, "/estimate")]] <- fit6(fit$sigma2, "stats::arima ML innovation variance.")
if (term == "constant") {
out[["/parameters/0/term"]] <- exact("intercept", "The constant comes first.")
out[["/parameters/0/estimate"]] <- fit6(cf[["intercept"]] * (1 - cf[["ar1"]]),
"The equation constant c = mu (1 - phi) of R's mean mu.")
}
out[["/diagnostics/ljung_box_model_df"]] <- exact(sum(order[c(1, 3)]), "Chapter 12: subtract the fitted AR and MA terms.")
out[["/converged"]] <- exact(TRUE, "The optimizer converges on these data.")
deferred <- list(defer("/parameters/0/standard_error", "The manual does not state how parameter standard errors are estimated (observed Hessian or outer product of gradients)."))
emit(id, "timeSeriesArimaMenuItem", title, "TS_MODELS_01",
list(method = "forecast", response_column = series, model = "arima", arima_order = as.list(as.integer(order)),
deterministic_term = term, horizon = 3L), out,
c("Exact Gaussian maximum likelihood (R method = 'ML'); iterative-fit tier 1e-6.", notes),
"R stats::arima", "R stats::arima and stats::predict.Arima documentation", deferred = deferred)
}
arima_case("STAT-TS-ARIMA-001", "ARMA(1,0) with a constant", "ar", c(1, 0, 0), "constant",
arima(d$ar, order = c(1, 0, 0), method = "ML", optim.control = tight_optim),
"R parameterises the constant as the mean; the product labels the equation constant 'intercept'.")
# Statistician ruling, issue #10: integrated states use exact diffuse
# initialization and the reported likelihood drops the parameter-free diffuse
# constants, which for complete data is the exact Gaussian likelihood of the
# differenced series. R's arima uses an approximate diffuse prior (kappa = 1e6)
# instead, so this reference is certified independently: the exact stationary
# AR(1) likelihood of w = diff(rw) profiles sigma^2 = S(phi) / n in closed form,
# leaving a one-dimensional maximization in phi. Original-scale forecasts are
# closed form: y_T + sum_{j<=h} phi^j w_T, with error variance
# sigma^2 sum_{j<h} (sum_{i<=j} phi^i)^2.
rw_w <- diff(d$rw); rw_n <- length(rw_w)
rw_ss <- function(phi) (1 - phi^2) * rw_w[1]^2 + sum((rw_w[-1] - phi * rw_w[-rw_n])^2)
rw_profile <- function(phi) -0.5 * rw_n * (log(2 * pi * rw_ss(phi) / rw_n) + 1) + 0.5 * log(1 - phi^2)
rw_phi <- stats::optimize(function(phi) -rw_profile(phi), c(-0.99, 0.99), tol = 1e-15)$minimum
rw_sigma2 <- rw_ss(rw_phi) / rw_n
stopifnot(abs(rw_profile(rw_phi) - (-88.94964906893236)) < 1e-9) # statistician's certificate
local({
last <- d$rw[length(d$rw)]; w_last <- rw_w[rw_n]; z <- qnorm(0.975)
out <- list()
for (k in 0:2) {
h <- k + 1
point <- last + sum(rw_phi^(1:h)) * w_last
se <- sqrt(rw_sigma2 * sum(sapply(0:(h - 1), function(j) sum(rw_phi^(0:j))^2)))
out[[rows("forecast_rows", k, "forecast")]] <- fit6(point, "Closed-form ARIMA(1,1,0) forecast.")
out[[rows("forecast_rows", k, "lower")]] <- fit6(point - z * se, "Gaussian 95 percent interval.")
out[[rows("forecast_rows", k, "upper")]] <- fit6(point + z * se, "Gaussian 95 percent interval.")
}
out[["/parameters/0/term"]] <- exact("ar.L1", "The AR coefficient comes first without a constant.")
out[["/parameters/0/estimate"]] <- fit6(rw_phi, "Exact ML AR coefficient of the differenced series.")
out[["/parameters/1/term"]] <- exact("sigma2", "Innovation variance last.")
out[["/parameters/1/estimate"]] <- fit6(rw_sigma2, "Exact ML innovation variance of the differenced series.")
out[["/diagnostics/ljung_box_model_df"]] <- exact(1L, "Chapter 12: subtract the fitted AR and MA terms.")
out[["/converged"]] <- exact(TRUE, "The optimizer converges on these data.")
deferred <- list(defer("/parameters/0/standard_error", "The manual does not state how parameter standard errors are estimated (observed Hessian or outer product of gradients)."))
emit("STAT-TS-ARIMA-002", "timeSeriesArimaMenuItem", "ARIMA(1,1,0) without a deterministic term", "TS_MODELS_01",
list(method = "forecast", response_column = "rw", model = "arima", arima_order = list(1L, 1L, 0L),
deterministic_term = "none", horizon = 3L), out,
c("Exact diffuse initialization of the integrated state (statistician ruling, issue #10): for complete data the likelihood of the differenced series.",
"Certified by the closed-form profile likelihood of AR(1) on diff(rw), solved to machine precision; iterative-fit tier 1e-6.",
"R stats::arima (approximate diffuse, kappa = 1e6) gave phi -0.016609067949116645, the reference before this method change.",
"One ordinary difference and no drift."),
"Closed-form exact AR(1) profile likelihood of the differenced series in R",
"Box, Jenkins and Reinsel, Time Series Analysis, exact AR(1) likelihood; statistician ruling on issue #10", deferred = deferred)
})
# ---- dynamic regression ----------------------------------------------------------------------
future_x <- c(4, 5, 6)
# Statistician ruling, issue #10: certify the reference optimum independently.
# For regression with stationary AR(1) errors, the exact Gaussian likelihood
# profiles in closed form: for fixed phi the Prais-Winsten transform gives beta by
# least squares and sigma^2 = RSS / n, leaving a one-dimensional maximization in
# phi, solved to machine precision. R's arima(method = "ML") stopped about 6e-12
# log-likelihood units short even at reltol 1e-15, leaving c = mu (1 - phi) 1e-6
# relative off; its forecasts are taken at the certified parameters via fixed =.
dyn_y <- d$dyn; dyn_X <- cbind(1, d$x); dyn_n <- length(dyn_y)
dyn_profile <- function(phi) {
s <- sqrt(1 - phi^2)
ty <- c(s * dyn_y[1], dyn_y[-1] - phi * dyn_y[-dyn_n])
tX <- rbind(s * dyn_X[1, ], dyn_X[-1, ] - phi * dyn_X[-dyn_n, ])
b <- qr.coef(qr(tX), ty); rss <- sum((ty - tX %*% b)^2)
list(ll = -0.5 * dyn_n * (log(2 * pi * rss / dyn_n) + 1) + log(s), beta = b)
}
dyn_phi <- stats::optimize(function(phi) -dyn_profile(phi)$ll, c(-0.99, 0.99), tol = 1e-15)$minimum
dyn_beta <- dyn_profile(dyn_phi)$beta
dfit <- arima(d$dyn, order = c(1, 0, 0), xreg = d$x, method = "ML",
fixed = c(dyn_phi, dyn_beta[1], dyn_beta[2]), transform.pars = FALSE)
stopifnot(abs(dfit$loglik - dyn_profile(dyn_phi)$ll) < 1e-9)
dpr <- predict(dfit, n.ahead = 3, newxreg = future_x)
dout <- list("/parameters/1/estimate" = fit6(dyn_beta[2], "Regression slope on the level feature."),
"/parameters/2/estimate" = fit6(dyn_phi, "AR(1) error coefficient."),
"/parameters/0/estimate" = fit6(dyn_beta[1] * (1 - dyn_phi),
"Constant of the AR error equation, c = mu (1 - phi)."),
"/conditional_on_predictors" = exact(TRUE, "Conditional on the supplied future path."))
for (k in 0:2) {
dout[[rows("forecast_rows", k, "forecast")]] <- fit6(dpr$pred[k + 1], "Forecast on the supplied predictor path.")
dout[[rows("forecast_rows", k, "upper")]] <- fit6(dpr$pred[k + 1] + qnorm(0.975) * dpr$se[k + 1], "Gaussian 95 percent interval.")
}
emit("STAT-TS-DYNREG-001", "timeSeriesDynamicRegressionMenuItem", "Regression with AR(1) errors on a future predictor path",
"TS_MODELS_01",
list(method = "dynamic_regression", response_column = "dyn", features = list(list(column = "x", kind = "level")),
future_scenarios = list(list(column = "x", values = as.list(future_x))), arima_order = list(1L, 0L, 0L),
deterministic_term = "constant"),
dout, c("Chapter 12: the forecast is conditional on the explicit future predictor path.",
"R's arima with xreg is the same regression with ARMA errors; the product's constant sits in the error equation."),
"R stats::arima with xreg", "R stats::arima documentation (xreg)",
deferred = list(defer("/parameters/0/standard_error", "Standard-error method not stated in the manual.")))
# ---- VAR / VECM ------------------------------------------------------------------------------
vfit <- VAR(data.frame(v1 = s$v1, v2 = s$v2), p = 1, type = "const")
vpr <- predict(vfit, n.ahead = 2, ci = 0.95)
vo <- list("/model_label" = exact("Level VAR (1 lag(s))", "Full cointegration rank is a VAR in levels."))
for (k in 0:1) {
vo[[rows("forecasts", k, "v1")]] <- num(vpr$fcst$v1[k + 1, "fcst"], "vars::predict point forecast (equation-wise OLS).")
vo[[rows("forecasts", k, "v2")]] <- num(vpr$fcst$v2[k + 1, "fcst"], "vars::predict point forecast.")
vo[[rows("forecasts", k, "v1_lower")]] <- num(vpr$fcst$v1[k + 1, "lower"], "vars::predict 95 percent interval.")
vo[[rows("forecasts", k, "v2_upper")]] <- num(vpr$fcst$v2[k + 1, "upper"], "vars::predict 95 percent interval.")
}
ir <- irf(vfit, impulse = "v1", response = "v1", n.ahead = 2, ortho = TRUE, boot = FALSE)$irf$v1
vo[["/impulse_responses/0/shock"]] <- exact("v1", "First impulse.")
vo[["/impulse_responses/0/response"]] <- exact("v1", "First response.")
vo[["/impulse_responses/0/value"]] <- num(ir[1, 1], "vars::irf orthogonalised response at horizon 0 (Cholesky factor).")
emit("STAT-TS-VAR-001", "timeSeriesVarVecmMenuItem", "Level VAR(1) forecasts and impulse response", "TS_SYSTEM_01",
list(method = "advanced", kind = "var_vecm", columns = list("v1", "v2"), lag_order = 1L, cointegration_rank = 2L,
horizon = 2L, deterministic = "constant"),
vo, c("VAR(1) with an unrestricted constant, fitted equation by equation by OLS.",
"Interval and impulse scale use the residual covariance; vars divides by T - Kp - 1 as the degrees-of-freedom-adjusted estimate."),
"R vars package", "vars::VAR, predict.varest and irf documentation (Pfaff 2008)")
dv <- apply(cbind(s$v1, s$v2), 2, diff)
mu <- colMeans(dv)
last <- c(tail(s$v1, 1), tail(s$v2, 1))
emit("STAT-TS-VAR-002", "timeSeriesVarVecmMenuItem", "Rank zero: a constant-only VAR in differences", "TS_SYSTEM_01",
list(method = "advanced", kind = "var_vecm", columns = list("v1", "v2"), lag_order = 1L, cointegration_rank = 0L,
horizon = 2L, deterministic = "constant"),
list("/forecasts/0/v1" = num(last[1] + mu[1], "Last level plus the mean difference."),
"/forecasts/1/v2" = num(last[2] + 2 * mu[2], "Two steps of the mean difference.")),
c("Chapter 12: the lag control counts level lags, so one level lag at rank zero fits zero lags of differences, leaving only the constant."),
"Analytic", "Luetkepohl (2005), New Introduction to Multiple Time Series Analysis, section 6.3",
kind = "boundary", oracle_class = "analytic")
cj <- ca.jo(data.frame(c1 = s$c1, c2 = s$c2), type = "trace", ecdet = "none", K = 2, spec = "transitory")
trace <- rev(cj@teststat) # ca.jo lists r <= 1 before r = 0
emit("STAT-TS-JOHANSEN-001", "timeSeriesVarVecmMenuItem", "Johansen trace statistics for a cointegrated pair", "TS_SYSTEM_01",
list(method = "advanced", kind = "var_vecm", columns = list("c1", "c2"), lag_order = 2L, cointegration_rank = 1L,
horizon = 2L, deterministic = "constant"),
list("/johansen_trace/0/null_rank" = exact(0L, "Null r = 0 first."),
"/johansen_trace/0/statistic" = num(trace[1], "urca::ca.jo trace statistic, r = 0."),
"/johansen_trace/1/statistic" = num(trace[2], "urca::ca.jo trace statistic, r <= 1."),
"/johansen_trace/0/reject_5pct" = exact(unname(cj@teststat[2] > cj@cval[2, "5pct"]), "The pair is cointegrated.")),
c("K = 2 level lags; unrestricted constant outside the cointegration relation (ca.jo ecdet = 'none')."),
"R urca package", "urca::ca.jo documentation; Johansen (1988)")
# ---- GARCH -----------------------------------------------------------------------------------
# Statistician ruling, issue #9: keep the locked arch 8.0.0 convention and
# reproduce its objective independently. Returns are rescaled by powers of 10
# until the preliminary residual variance lies in [0.1, 10000); the backcast
# b = sum 0.94^i e_i^2 / sum 0.94^i over the first min(75, n) preliminary
# (sample-mean) residuals is held fixed; sigma_1^2 = omega + (alpha + beta) b;
# the Gaussian log likelihood uses every observation.
arch_garch11 <- function(y) {
e_pre <- y - mean(y); v0 <- mean(e_pre^2)
rescale <- 1; sc <- v0
while (!(sc >= 0.1 && sc < 10000) && sc > 0) {
rescale <- if (sc < 1) rescale * 10 else rescale / 10
sc <- v0 * rescale^2
}
ys <- y * rescale; e0 <- ys - mean(ys)
tau <- min(75L, length(ys)); w <- 0.94^(0:(tau - 1L)); w <- w / sum(w)
b <- sum(e0[seq_len(tau)]^2 * w)
recursion <- function(p) {
e <- ys - p[1]; n <- length(e); s2 <- numeric(n)
s2[1] <- p[2] + (p[3] + p[4]) * b
for (t in 2:n) s2[t] <- p[2] + p[3] * e[t - 1]^2 + p[4] * s2[t - 1]
list(e = e, s2 = s2)
}
nll <- function(p) {
if (p[2] <= 0 || p[3] < 0 || p[4] < 0 || p[3] + p[4] >= 1) return(Inf)
r <- recursion(p); 0.5 * sum(log(2 * pi) + log(r$s2) + r$e^2 / r$s2)
}
fit <- stats::optim(c(mean(ys), 0.1 * var(ys), 0.1, 0.8), nll, method = "Nelder-Mead",
control = list(reltol = 1e-16, maxit = 100000))
for (i in 1:5) fit <- stats::optim(fit$par, nll, method = "BFGS",
control = list(reltol = 1e-16, maxit = 10000, parscale = abs(fit$par) + 1e-3))
fit <- stats::optim(fit$par, nll, method = "Nelder-Mead", control = list(reltol = 1e-16, maxit = 100000))
r <- recursion(fit$par); n <- length(ys)
list(backcast = b, rescale = rescale, loglik = -fit$value,
mu = fit$par[1] / rescale, omega = fit$par[2] / rescale^2, alpha = fit$par[3], beta = fit$par[4],
volatility = sqrt(r$s2) / rescale,
forecast_vol = sqrt(fit$par[2] + fit$par[3] * r$e[n]^2 + fit$par[4] * r$s2[n]) / rescale)
}
ga <- arch_garch11(l$ret)
stopifnot(abs(ga$backcast - 1.0179017970706792) < 1e-12)
emit("STAT-TS-GARCH-001", "timeSeriesGarchMenuItem", "Gaussian GARCH(1,1) with a constant mean", "TS_LONG_01",
list(method = "advanced", kind = "garch", columns = list("ret"), distribution = "normal", mean = "constant",
garch_p = 1L, garch_q = 1L, horizon = 1L),
list("/parameters/0/estimate" = fit6(ga$mu, "Constant mean."),
"/parameters/1/estimate" = fit6(ga$omega, "omega, in squared return units."),
"/parameters/2/estimate" = fit6(ga$alpha, "alpha1."),
"/parameters/3/estimate" = fit6(ga$beta, "beta1."),
"/persistence" = fit6(ga$alpha + ga$beta, "alpha + beta."),
"/conditional_volatility/0" = fit6(ga$volatility[1], "sqrt(omega + (alpha + beta) b): the backcast start."),
"/conditional_volatility/299" = fit6(ga$volatility[300], "Recursion at row 300."),
"/forecast_volatility/0" = fit6(ga$forecast_vol, "One-step conditional volatility.")),
c("Iterative fit, tier 1e-6.",
"Statistician ruling, issue #9: the arch weighted backcast is retained; this reference reproduces its objective (rescale, fixed 0.94-weighted backcast over min(75, n) residuals, sigma_1^2 = omega + (alpha + beta) b, Gaussian likelihood) independently in R.",
sprintf("Backcast b = %.17g; maximized log L = %.17g.", ga$backcast, ga$loglik),
"Until 2026-09-25 the reference was rugarch, whose variance start is the mean squared residual."),
"Independent R implementation of the arch 8.0.0 GARCH(1,1) objective",
"arch GARCH.backcast documentation (Sheppard); Bollerslev (1986)")
# ---- Zivot-Andrews ---------------------------------------------------------------------------
# Statistician ruling, issue #9: keep the locked statsmodels 0.15.0 procedure,
# Baum's approximation to Zivot-Andrews. The augmentation lag is chosen once by
# AIC in the no-break ADF regression (constant and trend) on a common sample,
# over 0..min(ceiling(12 (n/100)^(1/4)), n %/% 2 - 3) lags (19 at n = 600), and
# then used at every candidate break; breaks cover the central 1 - 2(0.15) of
# the sample; the statistic is the minimum t on the lagged level. Critical
# values and p-values come from statsmodels' simulated table and are not
# compared here.
za_baum <- function(x, trim = 0.15) {
n <- length(x); dx <- diff(x)
maxlag <- min(ceiling(12 * (n / 100)^(1 / 4)), n %/% 2 - 3)
k <- (maxlag + 1):(n - 1)
aic <- sapply(0:maxlag, function(L) {
X <- cbind(1, k, x[k], if (L) sapply(seq_len(L), function(j) dx[k - j]))
ssr <- sum(stats::lm.fit(X, dx[k])$residuals^2); m <- length(k)
m * log(2 * pi) + m * log(ssr / m) + m + 2 * ncol(X)
})
L <- which.min(aic) - 1L
rows <- (L + 1):(n - 1); trimcnt <- floor(n * trim)
breaks <- (trimcnt + 1):(n - trimcnt)
tstat <- sapply(breaks, function(bp) {
du <- as.numeric(rows >= bp)
X <- cbind(1, du, rows, (rows - bp + 1) * du, x[rows],
if (L) sapply(seq_len(L), function(j) dx[rows - j]))
f <- stats::lm.fit(X, dx[rows]); s2 <- sum(f$residuals^2) / (length(rows) - ncol(X))
V <- chol2inv(qr.R(f$qr)) * s2
f$coefficients[5] / sqrt(V[5, 5])
})
j <- which.min(tstat)
list(maxlag = maxlag, lag = L, statistic = unname(tstat[j]), break_label = breaks[j])
}
zb <- za_baum(l$za)
stopifnot(zb$maxlag == 19L)
emit("STAT-TS-ZA-001", "timeSeriesZivotAndrewsMenuItem", "Zivot-Andrews with a level and slope break", "TS_LONG_01",
list(method = "advanced", kind = "zivot_andrews", columns = list("za"), break_deterministic = "both"),
list("/statistic" = num(zb$statistic, "Minimum t on the lagged level over the candidate breaks."),
"/break_label" = exact(as.character(zb$break_label), "The last row before the break dummies switch on (1-based)."),
"/selected_lags" = exact(as.integer(zb$lag), "AIC in the no-break ADF regression, maximum lag 19.")),
c("Statistician ruling, issue #9: Baum's approximation (lag chosen once by AIC in the no-break constant-and-trend ADF regression, maximum 19 at n = 600, trim 0.15), reproduced independently in R.",
"Until 2026-09-25 the reference was urca::ur.za at lag 0, a different lag procedure."),
"Independent R implementation of the statsmodels 0.15.0 Zivot-Andrews procedure", "Zivot and Andrews (1992); Baum (2004)")
# ---- stationarity verdicts -------------------------------------------------------------------
emit("STAT-TS-STATIONARITY-001", "timeSeriesStationarityMenuItem", "A random walk is a differencing candidate", "TS_STATIONARITY_01",
list(response_column = "walk", task = "stationarity"),
list("/stationarity/assessment" = exact("difference_candidate", "ADF cannot reject a unit root and KPSS rejects stationarity for a 600-step driftless random walk, at every ADF lag 0-12 and both KPSS lag rules.")),
c("Chapter 12: a differencing candidate is labelled only when ADF does not reject and KPSS rejects at 5 percent.",
"Verdict only: the manual does not define the lag selection, so the statistics themselves are not asserted; the verdict is checked to hold at every lag choice.",
"Until 2026-09-25 this case used TS_LONG_01's drifting level, whose correct verdict is inconclusive."),
"Analytic", "Dickey and Fuller (1979); Kwiatkowski et al. (1992)", oracle_class = "analytic")
emit("STAT-TS-STATIONARITY-002", "timeSeriesStationarityMenuItem", "White noise is stationary", "TS_STATIONARITY_01",
list(response_column = "noise", task = "stationarity"),
list("/stationarity/assessment" = exact("stationary", "iid noise rejects a unit root and keeps level stationarity at every ADF lag 0-12 and both KPSS lag rules.")),
c("Chapter 12 combined criterion; verdict only, checked at every lag choice.",
"Until 2026-09-25 this case used GARCH(1,1) returns, which KPSS legitimately rejects."),
"Analytic", "Dickey and Fuller (1979); Kwiatkowski et al. (1992)", oracle_class = "analytic")
# ---- Compare & Validate ----------------------------------------------------------------------
yy <- d$rw
origins <- 40:51 # 0-based origins; the last 6 rows are the final evaluation
err <- sapply(1:3, function(k) sapply(origins, function(o) yy[o + k] - yy[o]))
final_err <- yy[55:60] - yy[54]
emit("STAT-TS-VALIDATE-001", "timeSeriesValidateMenuItem", "Rolling-origin naive accuracy", "TS_MODELS_01",
list(method = "study", forecast = list(response_column = "rw", model = "naive", horizon = 3L),
validation = list(initial_window = 40L, horizon = 3L, final_evaluation = 6L)),
list("/accuracy_by_horizon/0/count" = exact(12L, "Origins 40 to 51 fit before the six final rows."),
"/accuracy_by_horizon/0/metrics/mae" = num(mean(abs(err[, 1])), "Mean absolute one-step error."),
"/accuracy_by_horizon/2/metrics/mae" = num(mean(abs(err[, 3])), "Mean absolute three-step error."),
"/accuracy_by_horizon/1/metrics/rmse" = num(sqrt(mean(err[, 2]^2)), "Root mean squared two-step error."),
"/overall_metrics/mae" = num(mean(abs(err)), "Every rolling error."),
"/final_evaluation_metrics/mae" = num(mean(abs(final_err)), "Six-step forecast from the last training row."),
"/final_evaluation_metrics/rmse" = num(sqrt(mean(final_err^2)), "Final evaluation RMSE.")),
c("Chapter 12: an expanding window from 40 initial observations, step 1, horizon 3, and an untouched final 6.",
"MASE, sMAPE, bias sign and interval coverage depend on conventions the manual leaves open and are not asserted."),
"Analytic", "Hyndman and Athanasopoulos, Forecasting: Principles and Practice, section 5.10", oracle_class = "analytic")
generators/r/timeseries_trend.R
source("common.R", local = TRUE)
# References for Time Series > Trend analysis (manual chapter
# docs/manual/chapters/102-deterministic-trends.tex). The series is indexed t = 1..n in
# table order; a missing or excluded response keeps its position. Linear and quadratic
# trends are ordinary least squares (stats::lm); the log-linear exponential trend is
# lm(log(y) ~ t) back-transformed without bias adjustment (c = exp(a), SE(c) = c SE(a) by the
# delta method); the original-scale exponential and the three-parameter logistic S-curve
# are stats::nls least-squares fits from the same linearized starting values the manual
# describes. MAE, MSE, RMSE and MAPE are in-sample original-scale errors of the fitted
# values (chapter 102 lines 23-29).
run <- generator_run(c("stats", "jsonlite"))
family <- "timeseries"
generator <- "generators/r/timeseries_trend.R"
ep <- "tabelz.analysis.time_series_trend:compute_trend"
exact <- function(value, why) list(comparison = "exact", value = value, rationale = why)
num <- function(value, why, tier = "1e-9") list(comparison = "tolerance", value = dec(unname(value)),
absolute_tolerance = "0", relative_tolerance = tier, rationale = why)
fit6 <- function(value, why) num(value, why, "1e-6")
fit5 <- function(value, why) num(value, why, "1e-5")
emit <- function(id, title, config, outputs, notes, kind = "nominal", excluded = integer()) {
write_expected(family, id, list(
schema_version = "2.0.0", case_id = id, outputs = outputs,
expected_error = NULL, deferred_outputs = list(), generator_run = run))
write_case(family, id, list(
schema_version = "2.0.0", case_id = id, version = "1.0.0", family = family, entry_point = ep,
menu_items = list("trendAnalysisMenuItem"), title = title, case_kind = kind,
dataset = list(path = "reference_data/datasets/TS_TREND_01.json", sha256 = ""),
excluded_rows = as.list(as.integer(excluded)), configuration = config, "repeat" = 1L,
expected = list(path = paste0("reference_data/expected/", family, "/", id, ".json"), sha256 = ""),
oracle = list(oracle_class = "independent_implementation",
source = "R stats::lm and stats::nls on t = 1..n",
citation = "R Core Team, stats package help: lm, nls, summary.lm, vcov",
generator = list(path = generator, sha256 = ""), independent = TRUE,
convention_notes = as.list(c(notes,
"Expected computed from the re-read TS_TREND_01 CSV with positions t = 1..n in table order.",
"docs/manual/chapters/102-deterministic-trends.tex:3-6: positions t = 1..n in table order; excluded or missing responses retain their positions.",
"docs/manual/chapters/102-deterministic-trends.tex:23-24: MAE, MSE, RMSE and MAPE describe in-sample original-scale errors."))),
supported = TRUE, unsupported_reason = NULL, requirement_ids = list("REQ-STAT-TS-TREND-001"),
risk_ids = list("RISK-REFERENCE-001"), review_reference = NULL))
}
# ---- dataset -------------------------------------------------------------------------------
set.seed(20261009)
n <- 24L
t_all <- seq_len(n)
y_lin <- round(5 + 0.8 * t_all + rnorm(n, 0, 0.6), 3)
y_quad <- round(20 - 1.5 * t_all + 0.12 * t_all^2 + rnorm(n, 0, 0.8), 3)
y_exp <- round(3 * exp(0.09 * t_all) * exp(rnorm(n, 0, 0.05)), 3)
y_logis <- round(100 / (1 + 20 * exp(-0.35 * t_all)) + rnorm(n, 0, 1.2), 3)
y_gap <- y_lin
y_gap[7] <- NA
export_dataset(
data.frame(y_lin = y_lin, y_quad = y_quad, y_exp = y_exp, y_logis = y_logis, y_gap = y_gap),
"TS_TREND_01",
"Simulated (R 4.6.1, set.seed 20261009): linear, quadratic, exponential and logistic trends with normal noise; y_gap is y_lin with row 7 missing; 24 rows",
"Simulated with stats::rnorm", "Constructed validation data; no third-party license.",
c(y_lin = "Float64", y_quad = "Float64", y_exp = "Float64", y_logis = "Float64", y_gap = "Float64"))
d <- read_dataset("TS_TREND_01")
accuracy <- function(y, fitted, tier = "1e-9") {
e <- y - fitted
list("/mae" = num(mean(abs(e)), "MAE = mean |y - fitted| on the original scale.", tier),
"/mse" = num(mean(e^2), "MSE = mean (y - fitted)^2.", tier),
"/rmse" = num(sqrt(mean(e^2)), "RMSE = sqrt(MSE).", tier),
"/mape" = num(100 * mean(abs(e / y)), "MAPE = 100 mean |e / y| (no zero responses).", tier))
}
param <- function(index, est, se, label, tier_est = "1e-9", tier_se = "1e-9") {
out <- list()
out[[sprintf("/parameters/%d/estimate", index)]] <- num(est, paste(label, "estimate."), tier_est)
out[[sprintf("/parameters/%d/standard_error", index)]] <- num(se, paste(label, "standard error."), tier_se)
out
}
counts <- function(fitted, length, missing = 0L, excluded = 0L) list(
"/fitted_count" = exact(as.integer(fitted), "Finite, non-excluded responses that were fitted."),
"/series_length" = exact(as.integer(length), "Rows of the series including missing and excluded positions."),
"/missing_count" = exact(as.integer(missing), "Missing responses that keep their position."),
"/excluded_count" = exact(as.integer(excluded), "Excluded rows that keep their position."))
# ---- 001 linear (nominal), horizon 2 ---------------------------------------------------------
tt <- seq_len(nrow(d))
fit <- stats::lm(y_lin ~ tt, data = data.frame(y_lin = d$y_lin, tt = tt))
cf <- coef(fit); se <- sqrt(diag(vcov(fit)))
future <- (nrow(d) + 1):(nrow(d) + 2)
emit("STAT-TS-TREND-001", "Linear trend with a two-step extrapolation",
list(response = "y_lin", model = "linear", horizon = 2L),
c(param(0L, cf[[1]], se[[1]], "Intercept b0"), param(1L, cf[[2]], se[[2]], "Slope b1"),
accuracy(d$y_lin, fitted(fit)), counts(nrow(d), nrow(d)),
list("/extrapolation/0/0" = num(future[1], "First extrapolated position t = n + 1."),
"/extrapolation/0/1" = num(cf[[1]] + cf[[2]] * future[1], "b0 + b1 (n + 1)."),
"/extrapolation/1/1" = num(cf[[1]] + cf[[2]] * future[2], "b0 + b1 (n + 2)."))),
c("lm(y ~ t): ordinary least squares with intercept; standard errors from summary.lm (residual df n - 2).",
"docs/manual/chapters/102-deterministic-trends.tex:8: linear and quadratic models use original-scale least squares.",
"docs/manual/chapters/102-deterministic-trends.tex:31-32: extrapolation extends the deterministic curve without prediction intervals."))
# ---- 002 quadratic ---------------------------------------------------------------------------
fit <- stats::lm(y ~ tt + I(tt^2), data = data.frame(y = d$y_quad, tt = tt))
cf <- coef(fit); se <- sqrt(diag(vcov(fit)))
emit("STAT-TS-TREND-002", "Quadratic trend", list(response = "y_quad", model = "quadratic", horizon = 0L),
c(param(0L, cf[[1]], se[[1]], "b0"), param(1L, cf[[2]], se[[2]], "b1"), param(2L, cf[[3]], se[[3]], "b2"),
accuracy(d$y_quad, fitted(fit)), counts(nrow(d), nrow(d))),
c("lm(y ~ t + I(t^2)): ordinary least squares on 1, t, t^2; standard errors with residual df n - 3.",
"docs/manual/chapters/102-deterministic-trends.tex:8: linear and quadratic models use original-scale least squares."),
kind = "axis_coverage")
# ---- 003 exponential growth, log-linear back-transformed -------------------------------------
fit <- stats::lm(ly ~ tt, data = data.frame(ly = log(d$y_exp), tt = tt))
cf <- coef(fit); se <- sqrt(diag(vcov(fit)))
c_hat <- exp(cf[[1]]); d_hat <- cf[[2]]
fitted_exp <- c_hat * exp(d_hat * tt)
emit("STAT-TS-TREND-003", "Exponential growth, back-transformed log-linear fit",
list(response = "y_exp", model = "exponential", horizon = 3L),
c(param(0L, c_hat, c_hat * se[[1]], "c = exp(a)"), param(1L, d_hat, se[[2]], "d = b"),
accuracy(d$y_exp, fitted_exp), counts(nrow(d), nrow(d)),
list("/extrapolation/2/1" = num(c_hat * exp(d_hat * (nrow(d) + 3)), "c exp(d (n + 3)) from the log-linear fit."))),
c("lm(log(y) ~ t); c = exp(a) and d = b; SE(c) = exp(a) SE(a) (delta method) and SE(d) = SE(b); no bias adjustment.",
"Accuracy measures use the back-transformed fitted values c exp(d t) on the original scale.",
"docs/manual/chapters/102-deterministic-trends.tex:8-10: log-linear exponential growth fits log y = a + b t and displays e^a (e^b)^t, a median trend without bias adjustment."),
kind = "axis_coverage")
# ---- 004 exponential growth, original-scale least squares -------------------------------------
start_exp <- list(c = exp(cf[[1]]), d = cf[[2]])
fit <- stats::nls(y ~ c * exp(d * tt), data = data.frame(y = d$y_exp, tt = tt), start = start_exp,
control = stats::nls.control(maxiter = 1000, tol = 1e-8, minFactor = 1e-14))
cf4 <- coef(fit); se4 <- sqrt(diag(vcov(fit)))
emit("STAT-TS-TREND-004", "Exponential growth, original-scale least squares",
list(response = "y_exp", model = "exponential_nls", horizon = 0L),
c(param(0L, cf4[["c"]], se4[["c"]], "c", "1e-6", "1e-5"), param(1L, cf4[["d"]], se4[["d"]], "d", "1e-6", "1e-5"),
accuracy(d$y_exp, fitted(fit), "1e-6"), counts(nrow(d), nrow(d))),
c("nls(y ~ c exp(d t)) Gauss-Newton from the log-linear start exp(a), b; tolerance 1e-6 (1e-5 for the standard errors) because both programs stop at an iterative optimum.",
"docs/manual/chapters/102-deterministic-trends.tex:11-12: the original-scale exponential minimizes squared response residuals for y = c e^(d t)."),
kind = "axis_coverage")
# ---- 005 S-curve (three-parameter logistic) ---------------------------------------------------
k0 <- 1.05 * max(d$y_logis)
z <- log(k0 / d$y_logis - 1)
lin <- coef(stats::lm(z ~ tt))
start_s <- list(K = k0, A = exp(lin[[1]]), r = -lin[[2]])
fit <- stats::nls(y ~ K / (1 + A * exp(-r * tt)), data = data.frame(y = d$y_logis, tt = tt), start = start_s,
control = stats::nls.control(maxiter = 1000, tol = 1e-8, minFactor = 1e-14))
cf5 <- coef(fit); se5 <- sqrt(diag(vcov(fit)))
relative_k <- se5[["K"]] / cf5[["K"]]
if (relative_k > 0.25) stop("S-curve asymptote is weakly determined; choose different data")
outputs5 <- c(param(0L, cf5[["K"]], se5[["K"]], "K", "1e-6", "1e-5"),
param(1L, cf5[["A"]], se5[["A"]], "A", "1e-6", "1e-5"),
param(2L, cf5[["r"]], se5[["r"]], "r", "1e-6", "1e-5"),
accuracy(d$y_logis, fitted(fit), "1e-6"), counts(nrow(d), nrow(d)),
list("/extrapolation/0/1" = fit6(cf5[["K"]] / (1 + cf5[["A"]] * exp(-cf5[["r"]] * (nrow(d) + 1))),
"K / (1 + A exp(-r (n + 1))); SE(K)/K below 0.25 so the extrapolation is supplied."),
"/pearl_reed/2/estimate" = fit6(cf5[["A"]], "Pearl-Reed beta1 = A."),
"/pearl_reed/3/estimate" = fit6(exp(-cf5[["r"]]), "Pearl-Reed beta2 = exp(-r).")))
emit("STAT-TS-TREND-005", "S-curve three-parameter logistic trend",
list(response = "y_logis", model = "scurve", horizon = 1L), outputs5,
c("nls(y ~ K / (1 + A exp(-r t))) Gauss-Newton from the linearized start K0 = 1.05 max(y), log(K0/y - 1) ~ t; tolerance 1e-6 (1e-5 for the standard errors).",
sprintf("SE(K)/K = %.4g from vcov(nls), below the 0.25 screen, so the one-step extrapolation is expected.", relative_k),
"docs/manual/chapters/102-deterministic-trends.tex:15-20: logistic S-curve y = K/(1 + A e^(-rt)); extrapolation withheld when asymptote relative SE exceeds 0.25."),
kind = "axis_coverage")
# ---- 006 linear with a missing value and an excluded row: positions are kept ------------------
keep <- !is.na(d$y_gap)
keep[12] <- FALSE # 0-based row index 11 is excluded by the case
fit <- stats::lm(y ~ tt, data = data.frame(y = d$y_gap[keep], tt = tt[keep]))
cf <- coef(fit); se <- sqrt(diag(vcov(fit)))
emit("STAT-TS-TREND-006", "Linear trend with a missing response and an excluded row",
list(response = "y_gap", model = "linear", horizon = 1L),
c(param(0L, cf[[1]], se[[1]], "Intercept b0"), param(1L, cf[[2]], se[[2]], "Slope b1"),
accuracy(d$y_gap[keep], fitted(fit)), counts(sum(keep), nrow(d), 1L, 1L),
list("/extrapolation/0/0" = num(nrow(d) + 1, "Extrapolation starts at n + 1 counting the kept positions."),
"/extrapolation/0/1" = num(cf[[1]] + cf[[2]] * (nrow(d) + 1), "b0 + b1 (n + 1)."))),
c("lm(y ~ t) on the 22 fitted rows with t at their original positions (row 7 missing, row 12 excluded); the series length stays 24.",
"docs/manual/chapters/102-deterministic-trends.tex:4-6: excluded or missing responses retain their positions, so removing a value does not compress elapsed index time."),
kind = "axis_coverage", excluded = 11L)
cat("trend references generated\n")