![]() |
VOOZH | about |
Weighted Least Squares (WLS) regression is a powerful extension of ordinary least squares regression, particularly useful when dealing with data that violates the assumption of constant variance.
In this guide, we will learn brief overview of Weighted Least Squares regression and demonstrate how to implement it in Python using the statsmodels library.
Least Squares Regression is a method used in statistics to find the best-fitting line or curve that summarizes the relationship between two or more variables. Imagine you're trying to draw a best-fitting line through a scatterplot of data points. This line summarizes the relationship between two variables. LSR, a fundamental statistical method, achieves exactly that. It calculates the line that minimizes the total squared difference between the observed data points and the values predicted by the line.
Weighted Least Squares (WLS) Regression is a type of statistical analysis used to fit a regression line to a set of data points. It's similar to the traditional Least Squares method, but it gives more importance (or "weight") to some data points over others. WLS regression assigns weights to each observation based on the variance of the error term, allowing for more accurate modeling of heteroscedastic data. Data points with lower variability or higher reliability get assigned higher weights. When fitting the regression line, WLS gives more importance to data points with higher weights, meaning they have a stronger influence on the final result. This helps to better account for variations in the data and can lead to a more accurate regression model, especially when there are unequal levels of variability in the data.
Formula:
Where,
In Python, the statsmodels library is commonly used for various statistical modeling tasks, including ordinary least squares (OLS) regression. For weighted least squares (WLS) regression implementation we will use statsmodels library.
sm.add_constant().sm.WLS() to specify the weighted least squares regression model..fit() to estimate the parameters of the model.In the below code, the implementation is demonstrated using statsmodels library.
Output:
WLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.975
Model: WLS Adj. R-squared: 0.967
Method: Least Squares F-statistic: 118.5
Date: Wed, 10 Apr 2024 Prob (F-statistic): 0.00166
Time: 12:48:35 Log-Likelihood: 0.72561
No. Observations: 5 AIC: 2.549
Df Residuals: 3 BIC: 1.768
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 1.7300 0.283 6.105 0.009 0.828 2.632
x1 0.9300 0.085 10.885 0.002 0.658 1.202
==============================================================================
Omnibus: nan Durbin-Watson: 3.395
Prob(Omnibus): nan Jarque-Bera (JB): 0.537
Skew: 0.600 Prob(JB): 0.765
Kurtosis: 1.935 Cond. No. 8.37
==============================================================================
Aspect | Ordinary Least Squares (OLS) Regression | Weighted Least Squares (WLS) Regression |
|---|---|---|
Objective | Minimize the sum of squared differences between observed and predicted values. | Minimize the weighted sum of squared differences between observed and predicted values. |
Assumption | Assumes constant variance (homoscedasticity) of errors. | Allows for varying variance (heteroscedasticity) of errors. |
Weighting of Observations | Assigns equal weight to each observation. | Assigns weights to observations based on the variance of the error term associated with each observation. |
Usage | Suitable for datasets with constant variance of errors. | Suitable for datasets with varying variance of errors. |
Implementation | Implemented using the ordinary least squares method. | Implemented using the weighted least squares method. |
Model Evaluation | Provides unbiased estimates of coefficients under homoscedasticity. | Provides more accurate estimates of coefficients under heteroscedasticity. |
Example | Fit a straight line through data points. | Fit a line that adjusts for varying uncertainty in data points. |
Weighted Least Squares (WLS) regression offers a valuable enhancement to traditional regression methods by accommodating data with varying levels of uncertainty. By assigning weights based on error variance, WLS regression provides more accurate parameter estimates, making it a powerful tool across diverse fields from finance to healthcare.