如何正确地取出 R 面板数据中的零观测值

How to correctly take out zero observations in panel data in R

我 运行 在我的面板数据库中 运行 plm 回归时遇到了一些问题。基本上,我必须从我的基础中减去一年,以及来自某个变量的所有零观察值。我尝试使用 AER 包中的数据集制作一个可重现的示例。


require (AER)
library (AER)
require(plm)
library("plm")

data("Grunfeld", package = "AER")
View(Grunfeld)
#Here I randomize some observations of the third variable (capital) as zero, to reproduce my dataset
for (i in 1:220) {
  x <- rnorm(10,0,1)
  if (mean(x) >=0) {
    Grunfeld[i,3] <- 0
  }
}
View(Grunfeld)


panel <- Grunfeld

#First Method
#This is how I was originally manipulating my data and running my regression 

panel <- Grunfeld

dd <-pdata.frame(panel, index = c('firm', 'year'))

dd <- dd[dd$year!=1935, ]

dd <- dd[dd$capital !=0, ]

ols_model_2 <- plm(log(value) ~ (capital), data=dd)
summary(ols_model_2)
#However, I couuldn't plot the variables of the datasets in graphs, because they weren't vectors. So I tried another way:

#Second Method

panel <- panel[panel$year!= 1935, ]

panel <- panel[panel$capital != 0,]

ols_model <- plm(log(value) ~ log(capital), data=panel, index = c('firm','year'))
summary(ols_model)

#But this gave extremely different results for the ols regression!

据我了解,这两种方法在 OLS 回归中应该会产生相同的输出。现在恐怕我的整个分析都是错误的,因为我是按照第一种方式做的。谁能向我解释发生了什么事? 提前致谢!

你们是运行两个不同的模特。我不确定为什么您会期望结果相同。

您的第一个模型是:

ols_model_2 <- plm(log(value) ~ (capital), data=dd)

而第二个是:

ols_model <- plm(log(value) ~ log(capital), data=panel, index = c('firm','year'))

正如您从模型摘要中看到的那样,两者都是 "Oneway (individual) effect Within Model"。在第一个中你没有指定索引,因为 dd 是一个 pdata.frame 对象。在第二个中,您确实指定了索引,因为面板是一个简单的 data.frame。然而,这根本没有区别。

区别在于使用资本对数或资本不对数。

作为旁注,遗漏 0 个观察值通常是非常有问题的。如果这样做,请确保您还尝试了处理零的其他方法,并查看您的结果有多大变化。您可以从这里开始 https://stats.stackexchange.com/questions/1444/how-should-i-transform-non-negative-data-including-zeros