GLS / FGLS — Generalized Least Squares
GLS (Generalized Least Squares) generalizes OLS to handle heteroskedasticity and/or autocorrelation through the error covariance matrix . When is unknown and must be estimated from data, we use FGLS (Feasible GLS).
When to use
Use GLS/FGLS when errors have a complex variance/correlation structure (e.g., AR(1) autocorrelation, grouping). WLS is the special case of GLS when is diagonal.
Model specification
GLS estimates:
where is the error covariance matrix. If , GLS reduces to OLS.
GLS vs FGLS
| GLS | FGLS | |
|---|---|---|
| Known | Estimated from data | |
| Property | Efficient (if correct) | Asymptotically efficient (large sample) |
| In practice | Rarely know | More common |
Running in EcoLab
- Modeling module → Classical linear regression family → GLS or FGLS.
- Select , the variables, and the covariance structure (e.g., AR(1), grouping).
- Run and read the Estimation and Diagnostics tabs; export the replication code.
Replication code
- Stata
- R
- Python
* ---- GLS / FGLS ----
* Load panel data (illustrative)
use "panel_data.dta", clear
xtset id time
* FGLS with heteroskedastic panels and AR(1) correlation
xtgls y x1 x2, panels(hetero) corr(ar1)
# ---- GLS / FGLS ----
library(nlme)
# Load data (illustrative)
df <- read.csv("panel_data.csv")
# GLS with AR(1) correlation and heteroskedastic variance
model_gls <- gls(y ~ x1 + x2,
correlation = corAR1(),
weights = varPower(),
data = df)
summary(model_gls)
# ---- GLS ----
import numpy as np
import statsmodels.api as sm
# Load data (illustrative)
import pandas as pd
df = pd.read_csv("panel_data.csv")
X = sm.add_constant(df[["x1", "x2"]])
y = df["y"]
# GLS with a known covariance matrix Omega
# (In practice, estimate Omega from OLS residuals for FGLS)
Omega = np.eye(len(y)) # placeholder — replace with estimated Omega
model_gls = sm.GLS(y, X, sigma=Omega).fit()
print(model_gls.summary())
Limitations
- FGLS can be biased in small samples if is poorly estimated.
- If you only need robust inference, OLS + robust/clustered standard errors is often simpler and safer.
Video tutorial
Video Tutorial: Running GLS in EcoLab
See also
- OLS · WLS · Model catalog