Multiple regression through one weighted least squares core: OLS, weighted least squares (row weights) and GLS for AR(1) errors all reduce to the same algebra, so the coefficients, t tests and R² come from the same well-tested path.
Robust standard errors follow the Stata conventions: HC1 is White's heteroskedasticity-consistent estimator with the small-sample factor, HC2/HC3 add leverage corrections (HC3 is the usual recommendation for small samples), Newey–West HAC handles serial correlation with Bartlett weights, and the cluster estimator sums scores within groups before the outer product.
Every routine is cross-validated against statsmodels on fixed seed data — coefficients, standard errors and p-values agree to six or more significant digits.
Frequently asked questions
When do I need robust standard errors?
When residuals are heteroskedastic (variance grows with a regressor) use HC1–HC3; when they are autocorrelated (time series) use HAC; when observations share a group (students in schools, months in years) use cluster.
Why did it call my design singular?
One column is constant or a perfect multiple of another (a common accident: including both a percentage and its parts). Remove the duplicate column.
中文说明
通过同一个加权最小二乘核心做多元回归:OLS、加权最小二乘(按行权重)与处理 AR(1) 误差的 GLS 在代数上同源,系数、t 检验与 R² 走的是同一条经过验证的路径。
稳健标准误遵循 Stata 惯例:HC1 是带小样本修正的 White 异方差稳健估计,HC2/HC3 加入杠杆校正(小样本通常推荐 HC3),Newey–West HAC 用 Bartlett 权重处理序列相关,聚类估计则先在组内求和得分再做外积。
所有例程都已在固定种子数据上与 statsmodels 交叉验证——系数、标准误与 p 值在六位以上有效数字一致。
常见问题
什么时候需要稳健标准误?
残差异方差(方差随某个自变量增大)用 HC1–HC3;残差自相关(时间序列)用 HAC;观测共享分组(同校学生、同年月份)用聚类稳健。
为什么提示设计矩阵奇异?
某一列是常数或与另一列完全成比例(常见事故:同时放入百分比及其分量)。删掉重复列即可。