PLM回归中的错误

6

同事们!我有面板数据:

    Company year       Beta     NI   Sales  Export Hedge      FL     QR     AT Foreign
1       1 2010 -2.2052800 293000 1881000 78.6816     0 23.5158  1.289 0.6554    3000
2       1 2011 -2.2536069 316000 2647000 81.4885     0 21.7945 1.1787 0.8282   22000
3       1 2012  0.3258693 363000 2987000 82.4908     0 24.5782 1.2428  0.813  -11000
4       1 2013  0.4006030 549000 4546000 79.4325     0 31.4168 0.6038 0.7905   71000
5       1 2014 -0.4508811 348000 5376000 79.2411     0 37.1451 0.6563  0.661  -64000
6       1 2015  0.1494696 355000 5038000 77.1735     0 33.3852 0.9798 0.5483   37000

但是当我尝试使用plm软件包进行回归时,R会显示错误:

panel <- read.csv("Panel.csv",  header=T, sep=";")
p=plm(data=panel,Beta~NI, model="within",index=c("id","year"))


Error in pdim.default(index[[1]], index[[2]]) : 
  duplicate couples (id-time)
In addition: Warning messages:
1: In pdata.frame(data, index) :
  duplicate couples (id-time) in resulting pdata.frame
 to find out which, use e.g. table(index(your_pdataframe), useNA = "ifany")
2: In is.pbalanced.default(index[[1]], index[[2]]) :
  duplicate couples (id-time)

3: In is.pbalanced.default(index[[1]], index[[2]]) :
  duplicate couples (id-time)

我在互联网上搜索了这个错误,发现它与公司ID和年份有关。但我没有找到避免这个问题的方法。此外,当我执行na.omit(panel)时,R不会显示错误,但保留NA数据和公司数据对于数据分析是重要的。请告诉我如何解决这个问题。谢谢。

2个回答

13

让我们考虑plm软件包中的Produc数据集。

data("Produc", package = "plm")
head(Produc)

    state year region     pcap     hwy   water    util       pc   gsp    emp unemp
1 ALABAMA 1970      6 15032.67 7325.80 1655.68 6051.20 35793.80 28418 1010.5   4.7
2 ALABAMA 1971      6 15501.94 7525.94 1721.02 6254.98 37299.91 29375 1021.9   5.2
3 ALABAMA 1972      6 15972.41 7765.42 1764.75 6442.23 38670.30 31303 1072.3   4.7
4 ALABAMA 1973      6 16406.26 7907.66 1742.41 6756.19 40084.01 33430 1135.5   3.9
5 ALABAMA 1974      6 16762.67 8025.52 1734.85 7002.29 42057.31 33749 1169.8   5.5
6 ALABAMA 1975      6 17316.26 8158.23 1752.27 7405.76 43971.71 33604 1155.4   7.7

在这个数据集中,信息是在时间(17年)和相同的样本单位(48个美国州)上收集的。
table(Produc$state, Produc$year)
                 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
  ALABAMA           1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1
  ARIZONA           1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1
  ARKANSAS          1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1
  CALIFORNIA        1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1
  ...

plm 要求每个(状态,年份)对都是唯一的。

any(table(Produc$state, Produc$year)!=1)
[1] FALSE

plm 命令在这个数据集上表现良好:

plmFit1 <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
          data = Produc, index = c("state","year"))
summary(plmFit1)


Oneway (individual) effect Within Model
Call:
plm(formula = log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, 
    data = Produc, index = c("state", "year"))

Balanced Panel: n=48, T=17, N=816

Residuals :
    Min.  1st Qu.   Median  3rd Qu.     Max. 
-0.12000 -0.02370 -0.00204  0.01810  0.17500 

Coefficients :
             Estimate  Std. Error t-value  Pr(>|t|)    
log(pcap) -0.02614965  0.02900158 -0.9017    0.3675    
log(pc)    0.29200693  0.02511967 11.6246 < 2.2e-16 ***
log(emp)   0.76815947  0.03009174 25.5273 < 2.2e-16 ***
unemp     -0.00529774  0.00098873 -5.3582 1.114e-07 ***
---
Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1

Total Sum of Squares:    18.941
Residual Sum of Squares: 1.1112
R-Squared:      0.94134
Adj. R-Squared: 0.93742
F-statistic: 3064.81 on 4 and 764 DF, p-value: < 2.22e-16

现在我们要复制其中一个(状态,年份)对:
 Produc[2,2] <- 1970
 any(table(Produc$state, Produc$year)>1)
 [1] TRUE

现在plm生成与您上述描述相同的错误消息:

zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
      data = Produc, index = c("state","year"))

Error in pdim.default(index[[1]], index[[2]]) : 
  duplicate couples (id-time)
Inoltre: Warning messages:
1: In pdata.frame(data, index) :
  duplicate couples (id-time) in resulting pdata.frame
 to find out which, use e.g. table(index(your_pdataframe), useNA = "ifany")
2: In is.pbalanced.default(index[[1]], index[[2]]) :
  duplicate couples (id-time)

3: In is.pbalanced.default(index[[1]], index[[2]]) :
  duplicate couples (id-time)

希望这可以帮助你。


一个没有重复的非平衡面板表格有零值,因此我建议使用 any(table(Produc$state, Produc$year) > 1) - jay.sf
那么我该如何在数据框中识别重复行? - Susan

0
刚刚发现另一个警告重复的配对(id-time),尽管没有这样的情况,这可能值得在这里分享。
也就是说,如果出于某种原因尝试将时间变量命名为"id"
library(plm)
data(Produc)

## duplicate time variable and name it "id"
Produc <- transform(Produc, id=year)

## check duplicate couples (id-time)
stopifnot(!any(table(Produc[, "state"], Produc[, "id"]) > 1))

f1 <- plm(gsp ~ pcap, Produc, index=c("state", "year"), model="within", effect="twoways")
## OK

f2 <- plm(gsp ~ pcap, Produc, index=c("state", "id"), model="within", effect="twoways")
# Warning messages:
# 1: In pdata.frame(x, index) :
#   duplicate couples (id-time) in resulting pdata.frame
#  to find out which, use e.g. table(index(your_pdataframe), useNA = "ifany")
# 2: In is.pbalanced.default(id, time) : duplicate couples (id-time)

当我们显式创建plm面板数据框时,原因就会变得可见,而如果您没有提供,则plm:plm内部会自动创建。
## create pdata.frames
p1 <- pdata.frame(Produc, index=c("state", "year"))
p2 <- pdata.frame(Produc, index=c("state", "id"))

head(index(p1))
#     state year
# 1 ALABAMA 1970
# 2 ALABAMA 1971
# 3 ALABAMA 1972
# 4 ALABAMA 1973
# 5 ALABAMA 1974
# 6 ALABAMA 1975

head(index(p2))
#     state state.1
# 1 ALABAMA ALABAMA
# 2 ALABAMA ALABAMA
# 3 ALABAMA ALABAMA
# 4 ALABAMA ALABAMA
# 5 ALABAMA ALABAMA
# 6 ALABAMA ALABAMA

正如我们所看到的,"id"并没有被用作变量,但是它与列"state"有某种关联。虽然我不确定出了什么问题,因为all.equal(str(p1), str(p2))返回TRUE


"id"、"time"和"group"是用于索引的列名。如果它们指向的索引与名称所示的含义不同,那么情况会变得混乱。开发版本对此有警告,请参见NEWS.md:"index:如果参数'which'包含“混淆”的值,则会发出警告。 “混淆”:用户调用的索引变量名为'id'、'time'或'group',如果它不是相应的索引(例如,在用户的数据框中,时间索引变量被称为'id')。 - Helix123
@Helix123 感谢您的提醒,我应该将此作为Github上的一个问题提交。请在这个 great 包的新版本发布时告诉我,我会删除答案。 - jay.sf
1
自从一段时间以来,新版本已经发布。 - Helix123
@Helix123 谢谢,现在至少会抛出所宣布的警告。 - jay.sf

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接