R 使用plm前需要声明面板数据吗?
简单的问题,但在使用plm估计FE回归之前–我是否需要使用plm.data(类似于Stata中的xtset)将df“设置”为面板数据R 使用plm前需要声明面板数据吗?,r,regression,plm,economics,R,Regression,Plm,Economics,简单的问题,但在使用plm估计FE回归之前–我是否需要使用plm.data(类似于Stata中的xtset)将df“设置”为面板数据 pdata您可以在使用pdata.frame()(plm.data()已过时)执行plm命令之前执行此操作,或者在plm()调用自身时执行更简单的操作(与Stata中不同)。例如: plm要求前两列为组和时间数据。因此,当您使用上面的Grunfeld示例数据进行FE回归时,如果不指定索引,它将起作用 wi1 <- plm(inv ~ value + capi
pdata您可以在使用pdata.frame()
(plm.data()
已过时)执行plm
命令之前执行此操作,或者在plm()
调用自身时执行更简单的操作(与Stata中不同)。例如:
plm
要求前两列为组和时间数据。因此,当您使用上面的Grunfeld
示例数据进行FE回归时,如果不指定索引,它将起作用
wi1 <- plm(inv ~ value + capital,
data=Grunfeld, model="within", effect="twoways")
wi1$coe
# value capital
# 0.1177159 0.3579163
或者通过生成“pdata.frame”
结果wi1
、wi2
和wi3
是相同的。但是,也有一些后果,因为“pdata.frame”
的行名对应于组时间:
head(Grunfeld3, 3)
# inv value capital year firm
# 1-1935 317.6 3078.5 2.8 1935 1
# 1-1936 391.8 4661.7 52.6 1936 1
# 1-1937 410.6 5387.1 156.9 1937 1
因此,all.equal
抛出字符串不匹配
all.equal(wi2, wi3)
# [1] "Component “residuals”: Names: 200 string mismatches"
# [2] "Component “model”: Attributes: < Component “row.names”: 200 string mismatches >"
# [3] "Component “call”: target, current do not match when deparsed"
wi1 <- plm(inv ~ value + capital,
data=Grunfeld, model="within", effect="twoways")
wi1$coe
# value capital
# 0.1177159 0.3579163
## confuse columns
Grunfeld2 <- Grunfeld[c(3:5, 2,1)]
head(Grunfeld2, 3)
# inv value capital year firm
# 1 317.6 3078.5 2.8 1935 1
# 2 391.8 4661.7 52.6 1936 1
# 3 410.6 5387.1 156.9 1937 1
plm(inv ~ value + capital,
data=Grunfeld2, model="within", effect="twoways")
# Error in plm.fit [...]
wi2 <- plm(inv ~ value + capital, index=c("firm", "year"),
data=Grunfeld2, model="within", effect="twoways")
wi2$coe
# value capital
# 0.1177159 0.3579163
Grunfeld3 <- pdata.frame(Grunfeld2, index=c("firm", "year"))
class(Grunfeld3)
# [1] "pdata.frame" "data.frame"
wi3 <- plm(inv ~ value + capital,
data=Grunfeld3, model="within", effect="twoways")
wi3$coe
# value capital
# 0.1177159 0.3579163
head(Grunfeld3, 3)
# inv value capital year firm
# 1-1935 317.6 3078.5 2.8 1935 1
# 1-1936 391.8 4661.7 52.6 1936 1
# 1-1937 410.6 5387.1 156.9 1937 1
all.equal(wi2, wi3)
# [1] "Component “residuals”: Names: 200 string mismatches"
# [2] "Component “model”: Attributes: < Component “row.names”: 200 string mismatches >"
# [3] "Component “call”: target, current do not match when deparsed"
head(wi2$residuals)
# 1 2 3 4 5 6
# 41.10980 -69.68476 -152.11391 -19.73566 -93.36168 -28.48560
head(wi3$residuals)
# 1-1935 1-1936 1-1937 1-1938 1-1939 1-1940
# 41.10980 -69.68476 -152.11391 -19.73566 -93.36168 -28.48560
head(wi2$model, 3)
# inv value capital
# 1 317.6 3078.5 2.8
# 2 391.8 4661.7 52.6
# 3 410.6 5387.1 156.9
head(wi3$model, 3)
# inv value capital
# 1-1935 317.6 3078.5 2.8
# 1-1936 391.8 4661.7 52.6
# 1-1937 410.6 5387.1 156.9
wi2$call
# plm(formula = inv ~ value + capital, data = Grunfeld2, effect = "twoways",
# model = "within", index = c("firm", "year"))
wi3$call
# plm(formula = inv ~ value + capital, data = Grunfeld3, effect = "twoways",
# model = "within")