【问题标题】:How to correctly take out zero observations in panel data in R如何正确取出 R 中面板数据中的零观测值
【发布时间】:2020-05-04 20:15:12
【问题描述】:

我在面板数据库中运行 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 回归中产生相同的输出。现在恐怕我的整个分析都是错误的,因为我是按照第一种方式做的。谁能解释我发生了什么? 提前致谢!

【问题讨论】:

    标签: r plm


    【解决方案1】:

    您正在运行两个不同的模型。我不知道为什么你会期望结果是一样的。

    您的第一个模型是:

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

    而第二个是:

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

    正如您从模型摘要中看到的,两者都是“模型内的单向(个体)效应”。在第一个中,您没有指定索引,因为 dd 是一个 pdata.frame 对象。在第二个中,您确实指定了索引,因为面板是一个简单的 data.frame。然而,这根本没有区别。

    区别是使用大写的对数还是不使用对数的大写。

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

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 1970-01-01
      • 2021-12-20
      • 2014-08-17
      • 1970-01-01
      • 2021-05-09
      • 2017-11-19
      • 2016-07-04
      • 1970-01-01
      相关资源
      最近更新 更多