来自 ivreg {AER} 对象的拟合值与手动 2SLS 结果不匹配
Fitted values from the ivreg {AER} object do not match manual 2SLS results
我试图找出为什么 ivreg 估计 {AER} 的拟合值不同于手动执行的 2 阶段最小二乘法(以及适当的简化形式方程)... ivreg 和 [= 的帮助15=] 声明它重复调用 lm()。我提供了来自 {AER} 包的示例,其中计算了拟合值。
rm(list = ls())
require('AER') # install.packages('AER')
## data and example adapted from the AER package
data("CigarettesSW")
CigarettesSW$rprice <- with(CigarettesSW, price/cpi)
CigarettesSW$rincome <- with(CigarettesSW, income/population/cpi)
CigarettesSW$tdiff <- with(CigarettesSW, (taxs - tax)/cpi)
## Estimation by IV: log(rprice) is endogenous, tdiff is IV for log(rprice):
fm <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff,
data = CigarettesSW)
##
##
# Reduced form for log(rprice)
rf.rprice <- lm(log(rprice) ~ log(rincome) + tdiff,
data = CigarettesSW)
# Reduced form for log(packs)
rf.lpacks <- lm(log(packs) ~ log(rincome) + tdiff,
data = CigarettesSW)
# "Manual" 2SLS estimation of the "fm" equation
m2sls <- lm(log(packs) ~ rf.rprice$fitted.values + log(rincome),
data = CigarettesSW)
# Coefficients of "m2sls" are matched to "fm" object:
summary(m2sls)
summary(fm)
#
# It is my understanding, that fitted values from ivreg-fitted object "fm",
# manually performed 2SLS (in "m2sls") and from the reduced form rf.lpacks
# should be the same:
#
head(fm$fitted.values, 10)
head(m2sls$fitted.values, 10)
head(rf.lpacks$fitted.values, 10)
#
# However, fitted values from ivreg are different.
很可能,我遗漏了一些明显的东西,但我还是卡住了。非常感谢任何评论。
ivreg
对象的 predict()
和 fitted()
方法只计算 x %*% b
,其中 x
是原始回归矩阵,b
是系数向量(由 IV 估计)。因此:
x <- model.matrix(~ log(rprice) + log(rincome), data = CigarettesSW)
b <- coef(m2sls)
然后您手动计算的拟合值为:
head(drop(x %*% b))
## 1 2 3 4 5 6
## 4.750353 4.751864 4.720216 4.778866 4.919258 4.596331
与 ivreg
:
的计算完全匹配
head(fitted(fm))
## 1 2 3 4 5 6
## 4.750353 4.751864 4.720216 4.778866 4.919258 4.596331
使用 ivreg,您必须在“|”前面包含其他 RHS 变量之后。
例如:
stage1<-predict(lm(endogenous.var~x1+x2+instrument,data=data))
data$stage1<-stage1
stage2<-lm(dependent~x1+x2+stage1,data=data)
不会给你与
相同的系数
iv.model<-ivreg(dependent~x1+x2+endogenous|instrument,data=data)
但是,它会给你与
相同的答案
iv.model<-ivreg(dependent~x1+x2+endogenous|instrument+x1+x2,data=data.
实例:
rm(list=ls())
library(AER)
data(mtcars)
#let cyl be an instrument for hp
stage1<-predict(lm(hp~cyl+disp,data=mtcars))
mtcars$stage1<-stage1
stage2<-lm(mpg~stage1+disp,data=mtcars)
#this is not the same as
ivreg.bad<-ivreg(mpg~hp|cyl+disp,data=mtcars)
#and this doesn't even work
ivreg.bad2<-ivreg(mpg~disp+hp|cyl,data=mtcars)
#but it is the same as
ivreg.good<-ivreg(mpg~disp+hp|cyl+disp,data=mtcars)
summary(stage2)
summary(ivreg.good)
summary(ivreg.bad)
请注意,当然这只是指系数。标准误差会有所不同,因为 ivreg 会自动应用适当的调整。
我试图找出为什么 ivreg 估计 {AER} 的拟合值不同于手动执行的 2 阶段最小二乘法(以及适当的简化形式方程)... ivreg 和 [= 的帮助15=] 声明它重复调用 lm()。我提供了来自 {AER} 包的示例,其中计算了拟合值。
rm(list = ls())
require('AER') # install.packages('AER')
## data and example adapted from the AER package
data("CigarettesSW")
CigarettesSW$rprice <- with(CigarettesSW, price/cpi)
CigarettesSW$rincome <- with(CigarettesSW, income/population/cpi)
CigarettesSW$tdiff <- with(CigarettesSW, (taxs - tax)/cpi)
## Estimation by IV: log(rprice) is endogenous, tdiff is IV for log(rprice):
fm <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff,
data = CigarettesSW)
##
##
# Reduced form for log(rprice)
rf.rprice <- lm(log(rprice) ~ log(rincome) + tdiff,
data = CigarettesSW)
# Reduced form for log(packs)
rf.lpacks <- lm(log(packs) ~ log(rincome) + tdiff,
data = CigarettesSW)
# "Manual" 2SLS estimation of the "fm" equation
m2sls <- lm(log(packs) ~ rf.rprice$fitted.values + log(rincome),
data = CigarettesSW)
# Coefficients of "m2sls" are matched to "fm" object:
summary(m2sls)
summary(fm)
#
# It is my understanding, that fitted values from ivreg-fitted object "fm",
# manually performed 2SLS (in "m2sls") and from the reduced form rf.lpacks
# should be the same:
#
head(fm$fitted.values, 10)
head(m2sls$fitted.values, 10)
head(rf.lpacks$fitted.values, 10)
#
# However, fitted values from ivreg are different.
很可能,我遗漏了一些明显的东西,但我还是卡住了。非常感谢任何评论。
ivreg
对象的 predict()
和 fitted()
方法只计算 x %*% b
,其中 x
是原始回归矩阵,b
是系数向量(由 IV 估计)。因此:
x <- model.matrix(~ log(rprice) + log(rincome), data = CigarettesSW)
b <- coef(m2sls)
然后您手动计算的拟合值为:
head(drop(x %*% b))
## 1 2 3 4 5 6
## 4.750353 4.751864 4.720216 4.778866 4.919258 4.596331
与 ivreg
:
head(fitted(fm))
## 1 2 3 4 5 6
## 4.750353 4.751864 4.720216 4.778866 4.919258 4.596331
使用 ivreg,您必须在“|”前面包含其他 RHS 变量之后。
例如:
stage1<-predict(lm(endogenous.var~x1+x2+instrument,data=data))
data$stage1<-stage1
stage2<-lm(dependent~x1+x2+stage1,data=data)
不会给你与
相同的系数iv.model<-ivreg(dependent~x1+x2+endogenous|instrument,data=data)
但是,它会给你与
相同的答案iv.model<-ivreg(dependent~x1+x2+endogenous|instrument+x1+x2,data=data.
实例:
rm(list=ls())
library(AER)
data(mtcars)
#let cyl be an instrument for hp
stage1<-predict(lm(hp~cyl+disp,data=mtcars))
mtcars$stage1<-stage1
stage2<-lm(mpg~stage1+disp,data=mtcars)
#this is not the same as
ivreg.bad<-ivreg(mpg~hp|cyl+disp,data=mtcars)
#and this doesn't even work
ivreg.bad2<-ivreg(mpg~disp+hp|cyl,data=mtcars)
#but it is the same as
ivreg.good<-ivreg(mpg~disp+hp|cyl+disp,data=mtcars)
summary(stage2)
summary(ivreg.good)
summary(ivreg.bad)
请注意,当然这只是指系数。标准误差会有所不同,因为 ivreg 会自动应用适当的调整。