我对R中的面板数据几乎没有经验,我试图用plm包运行一个简单的面板回归。但是,在将我的数据帧转换为pdata.frame时,我的时间索引变量被转换为因子变量。这意味着,如果我想将一个因变量作为时间的函数进行回归,那么回归会生成一长串时间的虚拟变量,并为每个变量计算单独的系数。我只想要每个时间单位的平均效果。月平均增加/减少点数)。
dataframe例子:
ID Date Points
1 1/11/2014 2
1 1/12/2014 4
1 1/1/2015 6
1 1/2/2015 8
2 1/11/2014 1
2 1/12/2014 2
2 1/1/2015 3
2 1/2/2015 4
假设示例数据帧结构为ID = int, Date = POSIXct, Points = int。然后将其转换为索引ID和日期的pdata.frame:
panel <- pdata.frame(dataframe, c("ID", "Date"))
并运行plm固定效应回归:
fixed <- plm(Points ~ Date, data=panel, model="within")
summary(fixed)
然后将所得系数按月分解为假数。我想把我的时间变量当作一个连续变量,所以我只得到Date的一个系数。我该怎么做呢?是否有一种方法可以避免将时间索引变量格式化为面板数据框中的一个因素? 我认为您需要从panel$Date
创建一个单独的时钟或时间计数器以在您的模型中使用。例如:
library(dplyr)
dataframe <- dataframe %>%
group_by(ID) %>%
mutate(clock = seq_along(ID))
panel <- pdata.frame(dataframe, c("ID", "Date"))
生成这些数据:
ID Date Points clock
1-2014-11-01 1 2014-11-01 2 1
1-2014-12-01 1 2014-12-01 4 2
1-2015-01-01 1 2015-01-01 6 3
1-2015-02-01 1 2015-02-01 8 4
2-2014-11-01 2 2014-11-01 1 1
2-2014-12-01 2 2014-12-01 2 2
2-2015-01-01 2 2015-01-01 3 3
2-2015-02-01 2 2015-02-01 4 4
产生如下输出:
> fixed <- plm(Points ~ clock, data=panel, model="within")
> summary(fixed)
Oneway (individual) effect Within Model
Call:
plm(formula = points ~ clock, data = panel, model = "within")
Balanced Panel: n=2, T=4, N=8
Residuals :
Min. 1st Qu. Median 3rd Qu. Max.
-0.750 -0.375 0.000 0.375 0.750
Coefficients :
Estimate Std. Error t-value Pr(>|t|)
clock 1.50000 0.22361 6.7082 0.001114 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Total Sum of Squares: 25
Residual Sum of Squares: 2.5
R-Squared : 0.9
Adj. R-Squared : 0.5625
F-statistic: 45 on 1 and 5 DF, p-value: 0.0011144