Use lmr from LMScript

lmr is a native statistics host for LMScript. It runs a fixed set of statistical operations in a C++17 runtime with no R runtime, package installation, or network access required after the binary is installed.

Purpose

Use lmr to calculate distributions, seeded random draws, hypothesis tests, regression and generalized linear models, time-series analyses, clustering, smoothing, and numerical optimization from an LMScript program.

When to use it

Use lmr when a task needs the supported statistical operations and benefits from reproducible numeric checks in a native LMScript host. Use an R environment when the task depends on arbitrary R code, packages, callbacks, or operations outside lmr's published schema.

Install

Install the LMScript compiler with npm install -g @lmctl-ai/lmctl. A released lmr binary supports Linux x64 and arm64, macOS x64 and arm64, and Windows x64. The compiler can download the matching release asset and verify its SHA-256 when the host declaration includes install.

lmscript --version
lmr --version
lmr --lmcode-schema --json

Each release embeds the lmr-specific schema from native/schema/lmr.dsl.json: one context object out, 166 functions and 50 types. Standard types such as StrList come from the lmcode standard library and are not repeated in the host schema. The lmcode-cpp SDK that lmr vendors carries lmcode_std_v3 with 206 signatures; the current upstream catalog has 226, including the standard REST and CLI interfaces, which lmr gains only when it syncs the SDK and releases.

Commands

Declare the host in an _lmcode_ block and execute it with run_lmcode, which compiles the block against the host schema, runs the host and returns ok and result.

p = _lmcode_ cli="lmr" install="github:lmctl-ai/lmr@latest";
let z = norm().q_vec(parse_vec("0.975")).at(0);
out.check_close(z, 1.959963984540054, 0.00000000000001, "normal 97.5% quantile");
out.set("z", z);
end _lmcode_
r = run_lmcode(p);
require(r.ok, "run failed: " + r.get("message", "unknown"));
println(sprintf("Normal 97.5%% quantile: %.10f", r.result.values.z));

Compile without running using lmscript --check FILE.lms. The host protocol underneath is lmr --lmcode-schema --json, lmr --lmcode-signatures and lmr --lmcode FILE.dslb; the reference lists every flag.

Run the normal quantile example for a complete program with an expected-value check.

Inputs and outputs

The host accepts typed LMScript values, such as numeric vectors, frames, formulas, and operation options. Results use typed objects with status, estimates, diagnostics, metadata, warnings, and output vectors appropriate to each operation.

Use parse_vec to read numeric text, including NA, NaN, Inf, and -Inf. The DSL uses floating-point NaN for missing values. Values exported through out.set appear under result.values of the run_lmcode result, next to the checks and failures counts; non-finite numbers serialize as JSON null. Random operations require a seed when deterministic output is needed.

Input limits depend on the native operation; see the API guide for operation-specific limits and result fields.

Safety rules

lmr exposes a fixed operation catalog. Optimization, integration, root finding, and nonlinear models use reverse communication: write the objective or model in the DSL and supply values through the solver object. Formula text uses the supported native formula parser; it is not evaluated as R code. The host does not run arbitrary R scripts or load user packages.

Review an LMScript file before running it. The compiler and native host execute with your local file permissions, an LMScript program can invoke other local programs through system, and an install declaration downloads a release binary.

Worked examples

The lmr repository examples include longer tutorials and analysis flows.

Errors and exit codes

The host exits with code 0 when execution succeeds and all out.check assertions pass. Code 2 indicates command-line usage errors, code 3 indicates a load or execution error, and code 4 indicates an application failure such as a failed check. From LMScript, test r.ok after run_lmcode instead of reading the process code.

When installation fails, verify the platform has a published release asset and that the tool cache can be written. When a schema mismatch appears, compare the embedded JSON and canonical signatures against the released schema using the compiler version required by the project.

How to verify

Check the version, schema bytes, and canonical signatures. Then run a fresh-cache example so lmscript downloads the released host and validates its checksum.

lmr --version
lmr --lmcode-schema --json > /tmp/lmr-schema.json
cmp /tmp/lmr-schema.json native/schema/lmr.dsl.json
lmr --lmcode-signatures > /tmp/lmr-host-signatures.txt
lmscript --lmcode-signatures native/schema/lmr.dsl.json > /tmp/lmr-compiler-signatures.txt
diff -u /tmp/lmr-compiler-signatures.txt /tmp/lmr-host-signatures.txt

For an end-to-end install check, run one of the lmr book examples with LMSCRIPT_TOOL_CACHE set to a new temporary directory and lmr absent from PATH. A host already on PATH bypasses installation.