当我运行面板回归时,缺少虚拟变量。如何显示虚拟变量?

时间:2019-05-28 13:03:16

标签: r plm

我正在尝试使用固定效果估算器进行面板数据回归。示例数据如下所示:

structure(list(DOILN = c(4.3207, 4.1675, 4.0718, 3.8239, 3.6247, 
2.044, 1.3759, 1.4596, 1.486, 4.3136), ROSLN = c(-2.0178, -2.2647, 
-4.0632, -3.9933, -3.441, -3.6077, -2.8291, -2.6271, -2.4051, 
-1.7239), IRATE = c(-0.0295, -0.1228, 0.00288, 0.03388, -0.0295, 
0.00288, 0.03849, 0.03388, 0.07165, 0.04809), GDPGROW = c(0.11731, 
0.07891, 0.05072, 0.05745, 0.11731, 0.05072, 0.02142, 0.05745, 
0.06645, -0.01765), ISOCode = structure(c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 3L), .Label = c("BRA", "CHN", "IND", "RUS"), class = "factor"), 
    ISOCodeBRA = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ISOCodeRUS = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 0), ISOCodeIND = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 1), ISOCodeCHN = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0)), .Names = c("DOILN", "ROSLN", "IRATE", "GDPGROW", "ISOCode", 
"ISOCodeBRA", "ISOCodeRUS", "ISOCodeIND", "ISOCodeCHN"), row.names = c("120453-2010", 
"120453-2011", "120453-2012", "120453-2014", "133431-2010", "133431-2012", 
"133431-2013", "133431-2014", "133431-2015", "200448-2009"), class = c("pdata.frame", 
"data.frame"), index = structure(list(GCKey = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("120453", "133431", 
"200448"), class = "factor"), FiscalY = structure(c(2L, 3L, 4L, 
6L, 2L, 4L, 5L, 6L, 7L, 1L), .Label = c("2009", "2010", "2011", 
"2012", "2013", "2014", "2015"), class = "factor")), .Names = c("GCKey", 
"FiscalY"), row.names = c(915L, 647L, 35L, 41L, 83L, 68L, 220L, 
330L, 497L, 1219L), class = c("pindex", "data.frame")))

但是,当我为国家/地区引入虚拟变量(ISOCode)时,摘要中将其丢失。而且,我仍然有N个虚拟变量,而不是N-1。

我使用model.matrix创建虚拟变量。首先,我这样做是为了创建虚拟变量并将其包含到我的数据框中

dBRICna <-cbind(dBRICna, model.matrix(~ -1+ISOCode, data = dBRICna))

然后,基于初始数据框创建一个面板数据框。面板回归如下:

fix.cdum <-Plm(ROSLN~DOILN+GDPGROW+IRATE+ISOCodeBRA+ISOCodeRUS+ISOCodeIND+ISOCodeCHN, 
data = pbric,model = "within")

结果回归如下:

Oneway (individual) effect Within Model

Call:
plm(formula = ROSLN ~ DOILN + IRATE + GDPGROW + ISOCodeBRA + 
    ISOCodeCHN + ISOCodeIND + ISOCodeRUS, data = pbric, model = "within")

Unbalanced Panel: n = 308, T = 1-7, N = 1574

Residuals:
      Min.    1st Qu.     Median    3rd Qu.       Max. 
-6.4169648 -0.1066602  0.0075008  0.1344821  2.7955477 

Coefficients:
         Estimate Std. Error t-value Pr(>|t|)  
DOILN    0.031935   0.055124  0.5793  0.56247  
IRATE   -1.194691   0.486961 -2.4534  0.01429 *
GDPGROW -0.041300   0.767433 -0.0538  0.95709  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Total Sum of Squares:    240.19
Residual Sum of Squares: 238.52
R-Squared:      0.006922
Adj. R-Squared: -0.23683
F-statistic: 2.93449 on 3 and 1263 DF, p-value: 0.032416

如所观察到的,虚拟变量未显示在回归结果上。如果有人可以就此事向我提出建议,我将非常感谢!

1 个答案:

答案 0 :(得分:0)

首先,我建议您不要自己生成虚拟变量。在模型估计期间,让R为您代劳,方法是使用变量的一个因数,您要从中得出虚拟变量:只需在公式中使用factor(ISOCode)。手动方式容易出错。

然后,在模型估计后使用model.matrix来查看固定效果模型固有的内部转换后的数据:

mod <-plm(ROSLN ~ DOILN + GDPGROW + IRATE + factor(ISOCode), 
                         data = dat, model = "within")
model.matrix(mod)

    DOILN    GDPGROW     IRATE factor(ISOCode)RUS
   0.224725  0.0412125 -0.000615                  0
   0.071525  0.0028125 -0.093915                  0
  -0.024175 -0.0253775  0.031765                  0
  -0.272075 -0.0186475  0.062765                  0
   1.626660  0.0546400 -0.052980                  0
   0.045960 -0.0119500 -0.020600                  0
  -0.622140 -0.0412500  0.015010                  0
  -0.538440 -0.0052200  0.010400                  0
  -0.512040  0.0037800  0.048170                  0
   0.000000  0.0000000  0.000000                  0

两个问题变得很明显:一行全为零,因为个人只有一个观察值。该行将在模型估计中删除。 ISO代码列全为零,也将被删除。

这就是为什么模型摘要看起来像这样。