lmtest :: bptest测试OLS残差,也用于FE作为输入

时间:2016-08-14 15:32:09

标签: r statistics regression plm

我想用lmtest::bptest测试异方差性的固定效应回归。我从bptest获得的测试结果对于固定效果回归和OLS是相同的。

似乎bptest会自动从输入对象中提取formula并使用这些变量运行OLS,而不管输入的回归模型如何。来自?bptest

  

Breusch-Pagan检验拟合线性回归模型与线性回归模型的残差(默认情况下,相同的解释变量与主回归模型相同),如果太多的方差由附加解释,则拒绝解释变量。

没有出现错误或警告,告诉您该功能的输出不是基于您用作输入的型号。

问题

首先,有没有办法用pbtest测试我的plm-object的异方差性?由于我假设没有,是否有办法对固定效应回归进行Breusch Pagan测试?

这是一个可重复的例子:

# load the data:
df <- structure(list(country = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), year = c(2010, 
         2015, 2010, 2015, 2010, 2015, 2010, 2015, 2010, 2015), dv1 = c(28.61, 
         31.13, 38.87, 39.46, 68.42, 70.39, 79.36, 80.55, 70.14, 71.48
         ), iv1 = c(-20.68, 0, NA, NA, -19.41, -18.73, 24.98, 25.23, 21.23, 
         -21.06), iv2 = c(-4.23, NA, NA, NA, -4.92, -4.22, 9.19, 9.37, 
         4.15, -3.92)), .Names = c("country", "year", "dv1", "iv1", "iv2"
         ), row.names = c(2L, 3L, 5L, 6L, 8L, 9L, 11L, 12L, 14L, 15L),class    
         ="data.frame")
library(plm)
library(lmtest)


# Run the fixed effects regression
FEoutput <- plm(dv1 ~ iv1 + iv2, data = df, 
             model = "within", index = c("country", "year"))
bptest(FEoutput)
#   studentized Breusch-Pagan test
#
#    data:  FEoutput
#    BP = 1.9052, df = 2, p-value = 0.3857


# Run the OLS regression
OLSoutput <- lm(dv1 ~ iv1 + iv2, data = df)
bptest(OLSoutput)
#   studentized Breusch-Pagan test
#
#    data:  OLSoutput
#    BP = 1.9052, df = 2, p-value = 0.3857
包括国家假人进入OLS回归创建固定效应回归也没有用,因为国家假人增加了测试的自由度:

OLSctry <- lm(dv1 ~ iv1 + iv2 + factor(country), data = df)
bptest(OLSctry)
# studentized Breusch-Pagan test
#
# data:  OLSctry
# BP = 7, df = 5, p-value = 0.2206 

提前致谢!

1 个答案:

答案 0 :(得分:1)

你需要一个围绕lmtest::bptest的包装器来在plm对象的(准)数据上运行它。这是一个,我称之为pbptest

pbptest <-function(x, ...) {
  ## residual heteroskedasticity test based on the residuals of the demeaned
  ## model and the regular bptest() in {lmtest}

  ## structure:
  ## 1: take demeaned data from 'plm' object
  ## 2: est. auxiliary model by OLS on demeaned data
  ## 3: apply bptest() to auxiliary model and return the result

  if (!inherits(x, "plm")) stop("need to supply a panelmodel estimated with plm()")
  model <- plm:::describe(x, "model")
  effect <- plm:::describe(x, "effect")
  theta <- x$ercomp$theta

  ## retrieve demeaned data
  demX <- model.matrix(x, model = model, effect = effect, theta = theta)
  demy <- pmodel.response(model.frame(x), model = model, effect = effect, theta = theta)

  Ti <- pdim(x)$Tint$Ti

  if (is.null(order)) order <- min(Ti)

  ## bgtest on the demeaned model:

  ## check package availability and load if necessary
  lm.ok <- require("lmtest")
  if(!lm.ok) stop("package lmtest is needed but not available")

  ## pbptest is the bptest, exception made for the method attribute
  dots <- match.call(expand.dots=FALSE)[["..."]]
  if (!is.null(dots$type)) type <- dots$type else type <- "Chisq"
  if (!is.null(dots$order.by)) order.by <- dots$order.by else order.by <- NULL

  auxformula <- demy~demX-1
  lm.mod <- lm(auxformula)
  return(lmtest::bptest(lm.mod, ...)) # call and return lmtest::bptest
} # END pbptest()