﻿ Newey-West标准误差与Mean Group/Fama-MacBeth估计器 - 代码日志

#### Newey-West标准误差与Mean Group/Fama-MacBeth估计器

here为例：

``````require(foreign)
require(plm)
require(lmtest)

fpmg <- pmg(y~x, test, index=c("firmid", "year")) # Time index in second position, unlike the example
``````

``````# Regular “Fama-MacBeth” standard errors
coeftest(fpmg)

# t test of coefficients:
#
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.032470   0.071671   0.453   0.6505
# x           0.969212   0.034782  27.866   <2e-16 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
``````

``````# Newey-West standard-errors
coeftest(fpmg, vcov = NeweyWest(fpmg, lag=3))

# Error in UseMethod("estfun") :
#   no applicable method for 'estfun' applied to an object of class "c('pmg', 'panelmodel')"
``````

``````fpmg <- pmg(y~x, test, index=c("year", "firmid"))
fpmg.coefficients <- fpmg\$
fpmg.coefficients
# (Intercept)            x
#  0.03127797   1.03558610

coeftest(fpmg)
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.031278   0.023356  1.3392   0.1806
# x           1.035586   0.033342 31.0599   <2e-16 ***
``````

``````the.years <- unique(test\$year)
a.formula <- y ~ x

first.step <-  lapply(the.years, function(a.year) {
temp.data <- test[test\$year == a.year, ]
an.lm <- lm(a.formula, data = temp.data)
the.coefficients <- an.lm\$coef
the.results <- as.data.frame(cbind(a.year, t(the.coefficients)))
the.results
})

first.step.df <- do.call('rbind', first.step)

second.step.coefficients <- apply(first.step.df[, -1], 2, mean)
second.step.coefficients
# (Intercept)           x
#  0.03127797  1.03558610

identical(fpmg.coefficients, second.step.coefficients)
# [1] TRUE
``````

``````second.step.NW.sigma.sq <- apply(first.step.df[, -1], 2,
function(x) sqrt(NeweyWest(lm(x ~ 1),
lag = 1, prewhite = FALSE)['(Intercept)',
'(Intercept)']))
t.statistics.NW.lag.1 <- second.step.coefficients / second.step.NW.sigma.sq

t.statistics.NW.lag.1
# (Intercept)           x
#    1.282726   36.216301
``````