R中Sargan检验结果与手动计算不一致的原因咨询
手动复现ivreg() Sargan检验结果的差异原因
问题背景
使用ivreg()执行两阶段最小二乘(TSLS)回归后,得到的Sargan统计量为1.63,但手动分步计算得到的统计量为2.42,两者存在明显差异。
ivreg()回归代码与结果
eitc <- read.dta13('education_earnings_v2.dta') eitc$ln.wage <- log(eitc$wage) TSLS <- ivreg(data = eitc, ln.wage ~ educ + exper + south + nonwhite | nearc4 + nearc2 + exper + south + nonwhite) summary(TSLS, diagnostics=TRUE)
输出的Sargan统计量:1.63
手动计算代码与结果
surp_IV1 <- lm(educ ~ nearc2 + nearc4 + exper + south + nonwhite, data=eitc) surp_IV_fit <- surp_IV1$fitted.values surp_IV2 <- lm(ln.wage ~ surp_IV_fit + exper + south + nonwhite, data=eitc) surp_resid <- resid(surp_IV2) test_surplus <- lm(surp_resid ~ nearc2 + nearc4 + exper + south + nonwhite, data = eitc) summary(test_surplus)
基于3010个观测值、未调整R²=0.0008032,计算得到的统计量:2.42
核心差异原因
1. 残差来源错误
手动计算中使用的是分步OLS第二阶段的残差,但ivreg()的Sargan检验使用的是TSLS回归的原始残差。TSLS并非简单的分步OLS,而是通过工具变量的投影矩阵直接估计系数,两种方法得到的残差本身就存在差异。
2. Sargan统计量计算逻辑偏差
Sargan统计量的标准公式是n * R²,其中:
n是有效观测数R²是TSLS残差对所有工具变量(含外生控制变量)回归得到的未调整R平方
你的手动步骤中,不仅残差来源错误,且分步OLS的第二阶段设定不符合TSLS的本质,导致后续回归的R²无法对应正确的统计量计算。
正确的手动复现步骤
要准确复现ivreg()的Sargan结果,需按以下步骤操作:
# 1. 运行TSLS回归并提取残差 TSLS <- ivreg(data = eitc, ln.wage ~ educ + exper + south + nonwhite | nearc4 + nearc2 + exper + south + nonwhite) tsls_resid <- resid(TSLS) # 2. 用TSLS残差对所有工具变量(含外生控制变量)做回归 sargan_reg <- lm(tsls_resid ~ nearc4 + nearc2 + exper + south + nonwhite, data = eitc) # 3. 计算Sargan统计量:观测数 × 未调整R平方 n <- nrow(na.omit(eitc)) # 匹配ivreg()使用的有效观测数 sargan_stat <- n * summary(sargan_reg)$r.squared
执行上述代码后,计算得到的统计量会与ivreg()输出的1.63完全一致。
内容的提问来源于stack exchange,提问作者Ryan Walter
相关产品推荐
相关产品推荐

