簡體   English   中英

如何正確取出 R 面板數據中的零觀測值

[英]How to correctly take out zero observations in panel data in R

在我的面板數據庫中運行 plm 回歸時遇到了一些問題。 基本上,我必須從我的基礎中取出一年,還要從某個零變量中取出所有觀察值。 我嘗試使用來自 AER package 的數據集制作一個可重現的示例。


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 回歸中產生相同的輸出。 現在恐怕我的整個分析都是錯誤的,因為我是按照第一種方式做的。 誰能解釋我發生了什么? 提前致謝!

你是一個運行兩個不同的模型。 我不知道為什么你會期望結果是一樣的。

您的第一個 model 是:

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 object。 在第二個中,您確實指定了索引,因為面板是一個簡單的 data.frame。 然而,這根本沒有區別。

區別在於使用資本的對數或不使用對數的資本。

附帶說明一下,忽略 0 個觀察值通常是非常有問題的。 如果您這樣做,請確保您還嘗試了處理零的其他方法,並查看您的結果有多大變化。 您可以從這里開始https://stats.stackexchange.com/questions/1444/how-should-i-transform-non-negative-data-including-zeros

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM