# Linear Regression¶

Linear models with independently and identically distributed errors, and for errors with heteroscedasticity or autocorrelation. This module allows estimation by ordinary least squares (OLS), weighted least squares (WLS), generalized least squares (GLS), and feasible generalized least squares with autocorrelated AR(p) errors.

See Module Reference for commands and arguments.

## Examples¶

# Load modules and data
In : import numpy as np

In : import statsmodels.api as sm

In : spector_data.exog = sm.add_constant(spector_data.exog, prepend=False)

# Fit and summarize OLS model
In : mod = sm.OLS(spector_data.endog, spector_data.exog)

In : res = mod.fit()

In : print(res.summary())
OLS Regression Results
==============================================================================
Dep. Variable:                      y   R-squared:                       0.416
Method:                 Least Squares   F-statistic:                     6.646
Date:                Sun, 24 Nov 2019   Prob (F-statistic):            0.00157
Time:                        07:54:33   Log-Likelihood:                -12.978
No. Observations:                  32   AIC:                             33.96
Df Residuals:                      28   BIC:                             39.82
Df Model:                           3
Covariance Type:            nonrobust
==============================================================================
coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
x1             0.4639      0.162      2.864      0.008       0.132       0.796
x2             0.0105      0.019      0.539      0.594      -0.029       0.050
x3             0.3786      0.139      2.720      0.011       0.093       0.664
const         -1.4980      0.524     -2.859      0.008      -2.571      -0.425
==============================================================================
Omnibus:                        0.176   Durbin-Watson:                   2.346
Prob(Omnibus):                  0.916   Jarque-Bera (JB):                0.167
Skew:                           0.141   Prob(JB):                        0.920
Kurtosis:                       2.786   Cond. No.                         176.
==============================================================================

Warnings:
 Standard Errors assume that the covariance matrix of the errors is correctly specified.


Detailed examples can be found here:

## Technical Documentation¶

The statistical model is assumed to be

$$Y = X\beta + \mu$$, where $$\mu\sim N\left(0,\Sigma\right).$$

Depending on the properties of $$\Sigma$$, we have currently four classes available:

• GLS : generalized least squares for arbitrary covariance $$\Sigma$$

• OLS : ordinary least squares for i.i.d. errors $$\Sigma=\textbf{I}$$

• WLS : weighted least squares for heteroskedastic errors $$\text{diag}\left (\Sigma\right)$$

• GLSAR : feasible generalized least squares with autocorrelated AR(p) errors $$\Sigma=\Sigma\left(\rho\right)$$

All regression models define the same methods and follow the same structure, and can be used in a similar fashion. Some of them contain additional model specific methods and attributes.

GLS is the superclass of the other regression classes except for RecursiveLS.

### Attributes¶

The following is more verbose description of the attributes which is mostly common to all regression classes

pinv_wexogarray

The p x n Moore-Penrose pseudoinverse of the whitened design matrix. It is approximately equal to $$\left(X^{T}\Sigma^{-1}X\right)^{-1}X^{T}\Psi$$, where $$\Psi$$ is defined such that $$\Psi\Psi^{T}=\Sigma^{-1}$$.

cholsimgainvarray

The n x n upper triangular matrix $$\Psi^{T}$$ that satisfies $$\Psi\Psi^{T}=\Sigma^{-1}$$.

df_modelfloat

The model degrees of freedom. This is equal to p - 1, where p is the number of regressors. Note that the intercept is not counted as using a degree of freedom here.

df_residfloat

The residual degrees of freedom. This is equal n - p where n is the number of observations and p is the number of parameters. Note that the intercept is counted as using a degree of freedom here.

llffloat

The value of the likelihood function of the fitted model.

nobsfloat

The number of observations n

normalized_cov_paramsarray

A p x p array equal to $$(X^{T}\Sigma^{-1}X)^{-1}$$.

sigmaarray

The n x n covariance matrix of the error terms: $$\mu\sim N\left(0,\Sigma\right)$$.

wexogarray

The whitened design matrix $$\Psi^{T}X$$.

wendogarray

The whitened response variable $$\Psi^{T}Y$$.