来自ivreg {AER}对象的拟合值与手动2SLS结果

时间:2015-08-07 09:22:44

标签: r linear-regression prediction lm

我试图找出为什么来自ivreg估计{AER}的拟合值与手动执行的2阶段最小二乘(以及来自适当的简化形式方程)不同...对ivreg和ivreg的帮助。适合说它重复调用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.

最有可能的是,我错过了一些明显的东西,但我还是被卡住了。非常感谢任何评论。

2 个答案:

答案 0 :(得分:2)

predict()对象的fitted()ivreg方法只计算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 

答案 1 :(得分:1)

使用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会自动应用适当的调整。