rStats.js v1.3.0
    Preparing search index...

    Function lm

    • Fit a linear model from a formula. Equivalent to R's lm(formula, data). With weights and contrasts, and the quantities of summary().

      The model matrix is that of modelMatrix (but the levels given by levels that have no row left are dropped, as R's drop.unused.levels; a boolean column keeps both levels FALSE and TRUE); the fit is that of the matrix form (same pivoting and tolerance), its sums of squares computed in double-double so that data with a large mean keep their accuracy. Rows with a missing value in a variable of the formula or in the weights are dropped (rows lists those used).

      Parameters

      • formula: string

        A formula such as "y ~ A * B + x".

      • data: DataFrame

        The data frame: an object of columns.

      • Optionaloptions: LmFormulaOptions

      Returns LmFormulaResult

      const fit = lm("y ~ g", { y: [2.1, 3.9, 6.2, 7.8, 10.1, 11.8], g: ["a", "a", "b", "b", "c", "c"] });
      fit.coefNames; // ["(Intercept)", "gb", "gc"]
      fit.coefficients; // [3, 4, 7.95]
    • Fit a linear model by least squares, the predictors given as a matrix: R's lm(y ~ x) (or y ~ x - 1 without intercept, with weights for weighted least squares) together with summary(). Rows with a missing value in x, y or the weights are dropped.

      Parameters

      • x: Sample | MatrixInput

        Predictors: a matrix (array of rows, one column per predictor) or a vector (one predictor).

      • y: Sample

        Response.

      • Optionaloptions: LmOptions

      Returns LmResult

      const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
      fit.coefficients; // [0.05, 1.99]
      fit.rSquared; // 0.99731