在R

时间:2017-07-04 04:26:04

标签: r regression plm

我正在尝试使用plm包对R中的不平衡面板运行面板回归。我正在使用“Hedonic”数据来运行相同的数据。

我试图复制以下文章中的相似内容:http://ftp.uni-bayreuth.de/math/statlib/R/CRAN/doc/vignettes/plm/plmEN.pdf(第14页,3.2.5不平衡面板)。

我的代码看起来像这样:

form = mv ~ crim + zn + indus + chas + nox + rm + age + dis + rad + tax + ptratio + blacks + lstat
ba = plm(form, data = Hedonic)

但是,我在执行时遇到以下错误:

Error in names(y) <- namesy : 
  'names' attribute [506] must be the same length as the vector [0]

traceback()产生以下结果:

4: pmodel.response.pFormula(formula, data, model = model, effect = effect, 
       theta = theta)
3: pmodel.response(formula, data, model = model, effect = effect, 
       theta = theta)
2: plm.fit(formula, data, model, effect, random.method, random.dfcor, 
       inst.method)
1: plm(form, data = Hedonic)

我是小组回归的新手,如果有人可以帮我解决这个问题,我将非常感激。

感谢。

1 个答案:

答案 0 :(得分:1)

那篇论文已经十年了,我不确定plm是这样的。最新的文档在https://cran.r-project.org/web/packages/plm/vignettes/plm.pdf

你的问题出现了,因为在文档中:

  

当前版本的plm能够与常规版本一起使用   data.frame没有任何进一步的转换,前提是   个人和时间索引位于前两列,

Hedonic数据集在前两列中没有个别和时间索引。我不确定数据中的个人和时间索引在哪里,但是如果我为索引指定townid,我至少会得到一些运行的东西:

> p <- plm(mv~crim,data=Hedonic)
Error in names(y) <- namesy : 
  'names' attribute [506] must be the same length as the vector [0]

> p <- plm(mv~crim,data=Hedonic, index="townid")
> p

Model Formula: mv ~ crim

Coefficients:
      crim 
-0.0097455 

因为当你没有指定id和时间索引时,它将尝试使用前两列,而在Hedonic中为id提供唯一的数字,所以整个模型都崩溃了。

如果查看help(plm)中的示例,您可能会注意到所有数据集中的前两列定义了id和时间。