Skip to main content

Linear regression, with p-values and AIC for the full model, p-values per coefficient, and tests of linear constraints on regression coefficients. The formulas are based on Bingham & Fry.

The module uses only the modules numpy and math (built-in).

Basic usage

By default, do not add an explicit intercept to the predictors. The intercept will be appended as an additional predictor, unless the argument explicit_intercept is given and set to True (for if you want to define constraints involving the intercept).

Example, using simulated data:

import numpy as np

import teg_regression

nObs = 300

nPred = 5

fix_coeffs = {0: 1, 3: 2} # Set to empty dict to simulate the null model.

X, y = teg_regression.sim_data(nObs, nPred, fix_coeffs=fix_coeffs, fix_intercept=20)

O = teg_regression.run_regression(X, y)

The function returns a dictionary O with statistical values and prints out (unless report=False is set as an argument):

Test of the model:

F(5,294) = 0.849, p = 0.516

AIC constrained - free = -1.28e+03 (negative supports constraints).

Tests per coefficient.

Predictor 0: b = 0.00522, F(1,294) = 0.00842, p = 0.927

Predictor 1: b = 0.00324, F(1,294) = 0.00373, p = 0.951

Predictor 2: b = -0.0759, F(1,294) = 1.88, p = 0.172

Predictor 3: b = -0.093, F(1,294) = 2.69, p = 0.102

Predictor 4: b = 0.0182, F(1,294) = 0.115, p = 0.735

Intercept: b = 20.6, F(1,294) = 1.04e+05, p = 1.11e-16

Linear constraints

Linear constraints specify hypotheses in terms of weighted sums of the coefficients, to be tested against the free model. E.g., assuming we have four predictors, the matrix equation

[1 0 0 0] [0]

[0 1 0 0] * betas = [0]

[0 0 1 0] [0]

would constrain the first three predictors to be zero.

Similarly,

[1 -1 0 0] * betas = 0

Would constrain the first and second predictor to be equal.

The Constraints argument, specifying the desired hypothesis, is a dictionary with the "coefficients" matrix, as a 2D array, and the "constants" vector, also as an array. The example below shows the Constraints setup to set two specific predictor-coefficients to 0.

pred_to_test = [1, 2]

Constraints = {}

Constraints['coefficients'] = np.array([[0 for a in range(X.shape[1])] for newrow in range(2)]).reshape(2, X.shape[1])

Constraints['coefficients'][0][pred_to_test[0]] = 1

Constraints['coefficients'][1][pred_to_test[1]] = 1

Constraints['constants'] = np.array([0, 0])

O = teg_regression.run_regression(X, y, Constraints)

Metadata

Release files for teg-regression 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for teg-regression 0.1.3
File Size Uploaded
teg_regression-0.1.3.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for teg-regression 0.1.3
File Interpreter ABI Platform
teg_regression-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / teg_regression-0.1.3.tar.gz

Download URL teg_regression-0.1.3.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
aa2ba7e776c79609fa39b42c20be699d2c09e4d6888f43c96b769367116336e3
BLAKE2b-256 checksum
How to use checksums
2ee280207e926368ebbcc6ab08761edda370aa48fe0878da58d30cc457e90c4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.5

Release files / teg_regression-0.1.3-py3-none-any.whl

Download URL teg_regression-0.1.3-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02ff0420cce38e79b1d1876c4fb91dc90d62456dfa862a8a5d13723492c0106a
BLAKE2b-256 checksum
How to use checksums
1fd210a66939a349ff39ff0716365f79e046b0e67cc146be068670c44c931e4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page