← All procedures

statistics.leastSquares

The ordinary least-squares fit of `y` on the rows of `x`, with `k` coefficients. Each row has the same number of predictors. The fit solves the problem with a Householder QR decomposition. When the design does not have full rank, or when `n <= k`, the coefficients, `residualSd` and `r2` are NaN. When the total sum of squares is 0, `r2` is NaN.

CLI
Package
@procedures/statistics · README
Path
["statistics","leastSquares"]
CLI
mark statistics leastSquares --x <number[][]> --y <number[]>
As data
{"$proc":["statistics","leastSquares"],"input":{}}

Use it

In TypeScript

import { Client, LocalTransport, PROCEDURE_REGISTRY } from "@procedures/client";
import "@procedures/statistics/register"; // registers its procedures

const client = new Client(new LocalTransport({ registry: PROCEDURE_REGISTRY }));
const result = await client.exec(["statistics","leastSquares"], { x: [], y: [] });
console.log(JSON.stringify(result));

From the command line

$ mark statistics leastSquares --x <number[][]> --y <number[]>

As data, in a program

{
  "$proc": ["statistics", "leastSquares"],
  "input": { "x": [], "y": [] }
}

Input

FieldTypeRequiredDefaultDescription
xnumber[][]yes
ynumber[]yes
optionsobjectno

Output

FieldTypeRequiredDefaultDescription
coefficientsnumber[]yesThe coefficients. With an intercept, coefficient 0 is the intercept and coefficient `j + 1` is predictor `j`.
residualSdnumberyesThe residual standard deviation: the square root of the residual sum of squares divided by `n - k`.
r2numberyesThe coefficient of determination: 1 minus the residual sum of squares divided by the total sum of squares. With an intercept, the total sum uses the distances from the mean of `y`. Without an intercept, it uses the values.
nnumberyesThe number of rows.
knumberyesThe number of coefficients, with the intercept.
JSON Schemas
{
  "input": {
    "type": "object",
    "properties": {
      "x": {
        "type": "array",
        "items": {
          "type": "array",
          "items": {
            "type": "number"
          }
        }
      },
      "y": {
        "type": "array",
        "items": {
          "type": "number"
        }
      },
      "options": {
        "type": "object",
        "properties": {
          "intercept": {
            "type": "boolean",
            "description": "When true, the fit adds an intercept as coefficient 0. The default is true."
          }
        },
        "additionalProperties": {}
      }
    },
    "required": [
      "x",
      "y"
    ],
    "additionalProperties": false
  },
  "output": {
    "type": "object",
    "properties": {
      "coefficients": {
        "type": "array",
        "items": {
          "type": "number"
        },
        "description": "The coefficients. With an intercept, coefficient 0 is the intercept and coefficient `j + 1` is predictor `j`."
      },
      "residualSd": {
        "type": "number",
        "description": "The residual standard deviation: the square root of the residual sum of squares divided by `n - k`."
      },
      "r2": {
        "type": "number",
        "description": "The coefficient of determination: 1 minus the residual sum of squares divided by the total sum of squares. With an intercept, the total sum uses the distances from the mean of `y`. Without an intercept, it uses the values."
      },
      "n": {
        "type": "number",
        "description": "The number of rows."
      },
      "k": {
        "type": "number",
        "description": "The number of coefficients, with the intercept."
      }
    },
    "required": [
      "coefficients",
      "residualSd",
      "r2",
      "n",
      "k"
    ],
    "additionalProperties": {}
  }
}