算法手册/ 计量经济研究/ OLS回归

OLS回归

计算公式

符号

符号含义
y因变量
X解释变量矩阵(含常数项列)
β回归系数向量
ei第 i 个观测的残差
n、k样本量与参数个数
R2aux辅助回归的决定系数

核心公式

公式 (1.1):最小二乘估计
\[ \hat{\beta}=\left(X^{\prime}X\right)^{-1}X^{\prime}y,\qquad e_i=y_i-x_i^{\prime}\hat{\beta} \]
公式 (1.2):HC1 稳健协方差
\[ \hat{V}_{HC1}=\frac{n}{n-k}\left(X^{\prime}X\right)^{-1}\left(\sum_{i=1}^{n}e_i^2x_ix_i^{\prime}\right)\left(X^{\prime}X\right)^{-1} \]
公式 (1.3):Breusch-Pagan 与 RESET
\[ \mathrm{BP}=nR^2_{aux},\qquad F_{RESET}=\frac{\left(\mathrm{SSE}_r-\mathrm{SSE}_{ur}\right)/q}{\mathrm{SSE}_{ur}/\left(n-k-q\right)} \]

参考文献

  1. White, H. (1980). A heteroskedasticity-consistent covariance matrix estimator and a direct test for heteroskedasticity. Econometrica, 48(4), 817–838.
  2. Breusch, T. S., & Pagan, A. R. (1979). A simple test for heteroscedasticity and random coefficient variation. Econometrica, 47(5), 1287–1294.
  3. Ramsey, J. B. (1969). Tests for specification errors in classical linear least-squares regression analysis. Journal of the Royal Statistical Society: Series B, 31(2), 350–371.

算法参数

参数取值默认影响
稳健标准误 不校正 / HC0 / HC1 / HC2 / HC3 HC1 改变标准误、t、p、置信区间与整体 F,不改变系数。

验证报告

数据与 R 包

数据文件 ols_reg.csv
样本与变量 N = 100;因变量 Y,自变量 X1、X2
R 包 sandwich 3.1.3、lmtest 0.9.40

提示词

以 Y 为因变量、X1 与 X2 为自变量做计量 OLS,使用 HC1 稳健标准误,并报告 Breusch-Pagan 异方差检验与 Ramsey RESET。

R 代码

library(sandwich); library(lmtest)
d <- read.csv("ols_reg.csv")
fit <- lm(Y ~ X1 + X2, data = d)
print(coeftest(fit, vcov = vcovHC(fit, type = "HC1")), digits = 15)
cat("R2 =", summary(fit)$r.squared, "\n")
cat("adjR2 =", summary(fit)$adj.r.squared, "\n")
print(waldtest(fit, vcov = vcovHC(fit, type = "HC1"), test = "F"), digits = 15)
print(bptest(fit), digits = 15)
print(resettest(fit, power = 2:4, type = "fitted"), digits = 15)

对比结果

指标SPSSAUR差值
常数4.44822425704.44822425704.66e−11
X1 系数1.54311729871.54311729874.97e−11
X2 系数−0.8248726237−0.82487262371.84e−11
常数标准误(HC1)1.44623699741.44623699741.43e−11
X1 标准误(HC1)0.15080816390.15080816394.81e−11
X2 标准误(HC1)0.10171716780.10171716781.62e−11
X1 t 值10.232319380110.23231938014.53e−11
X2 t 值−8.1094729797−8.10947297973.72e−11
R²0.56480088370.56480088374.16e−11
调整 R²0.55582770610.55582770611.32e−11
稳健整体 F78.503365414178.50336541410
Breusch-Pagan χ²7.96535071947.96535071942.32e−11
Breusch-Pagan p0.01863571530.01863571534.32e−11
RESET F0.66835001860.66835001861.28e−11
RESET p0.57355878270.57355878278e−12

结论:两个斜率及其 HC1 标准误、t 值、R²、稳健整体 F、Breusch-Pagan 与 RESET 均与 R 一致,最大差 2.4e−11(来自 10 位小数输出)。稳健系数表的 p 值与置信区间按 t(n−k) 计算,与 R 的 coeftest 一致:常数项 p = 0.0027285,斜率项 p 均远小于 1e−10。Breusch-Pagan 拒绝同方差,应使用稳健标准误。

操作手册 问题反馈

引用SPSSAU. OLS回归:计算公式与参数[EB/OL]. SPSSAU算法手册, 2026-11-20. https://www.spssau.com/algorithm/econometrics/ols_reg.html.