Skip to content

statsjunk.regression

simple

RegressionResult

Bases: BaseModel

Result of a simple linear regression, with slope diagnostics.

compute_linear_regression

compute_linear_regression(
    x: Sequence[float], y: Sequence[float], ci: float = 0.95
) -> RegressionResult

Fit a simple linear regression model and its slope diagnostics.

What this solves

You have one variable you think predicts or explains another (e.g. years of experience predicting salary), and want the straight line that best fits the data, plus a sense of how reliable that line is. This fits the best-fitting line, tells you how much of the variation in y it explains (r_squared), and gives a confidence interval and p-value for the slope — so you can judge whether the relationship is likely real (slope clearly different from zero) or could just be noise.

Parameters:

Name Type Description Default
x Sequence[float]

Independent variable.

required
y Sequence[float]

Dependent variable.

required
ci float

Confidence level for the slope interval. Must be in (0, 1).

0.95

Returns:

Type Description
RegressionResult

Fitted slope and intercept; r_squared (share of the variance in y explained by the fit); slope_pvalue (two-sided Wald test of slope == 0 — identical to Pearson's p-value in a simple regression); and slope_low/slope_high, the ci confidence interval for the slope.

Raises:

Type Description
ValueError

If: - x and y have different lengths - fewer than 3 observations are provided - ci is not in (0, 1) - either input is constant

References
  • Draper, N.R. & Smith, H. (1998). Applied Regression Analysis (3rd ed.). Wiley.

multiple

MultipleRegressionResult

Bases: BaseModel

Result of an ordinary-least-squares multiple linear regression.

Coefficient

Bases: BaseModel

One term of a fitted multiple linear regression.

coef is on the regressor's own scale; coef_std is the standardized (beta) coefficient — the effect in standard deviations of y per standard deviation of the regressor — so magnitudes are comparable across regressors on different scales. Both are None for the intercept, as is vif.

compute_multiple_regression

compute_multiple_regression(
    x: Sequence[Sequence[float]],
    y: Sequence[float],
    names: Sequence[str] | None = None,
    ci: float = 0.95,
) -> MultipleRegressionResult

Fit a multiple linear regression by ordinary least squares.

What this solves

You have several variables you think together predict or explain an outcome (e.g. education, experience, and age predicting salary), and want to know each one's individual effect while accounting for the others. This fits the best-fitting linear combination, reports each predictor's effect size, standard error, and significance, and flags predictors that are too similar to each other to separate reliably (vif, variance inflation factor — a large value there is a warning sign that two or more of your predictors carry redundant information).

Parameters:

Name Type Description Default
x Sequence[Sequence[float]]

Design matrix of shape (n_observations, n_regressors) — one row per observation, one column per independent variable (no intercept column).

required
y Sequence[float]

Dependent variable, one value per observation.

required
names Sequence[str]

Label for each regressor column; defaults to x1, x2, ...

None
ci float

Confidence level for the per-coefficient intervals. Must be in (0, 1).

0.95

Returns:

Type Description
MultipleRegressionResult

Per-coefficient estimates (raw and standardized), standard errors, two-sided Wald p-values, ci confidence intervals and VIFs — the intercept first, then one row per regressor — plus r_squared, adj_r_squared, the overall-model F-test f_pvalue, n, df_residual and the residuals/fitted vectors.

Raises:

Type Description
ValueError

If: - x is not 2-D, or x and y disagree on the number of observations - there are no regressors, or fewer than k + 2 observations - ci is not in (0, 1) - x or y contains a non-finite value - y or any regressor column is constant - the regressors are collinear (design matrix not full rank) - names is given with the wrong length

References
  • Draper, N.R. & Smith, H. (1998). Applied Regression Analysis (3rd ed.). Wiley.
  • Marquardt, D.W. (1970). "Generalized Inverses, Ridge Regression, Biased Linear Estimation, and Nonlinear Estimation." Technometrics, 12(3), 591-612. (variance inflation factor)