首页 > 解决方案 > 在面板数据集 R 中取出值的顺序

问题描述

我在 R plm 包中使用不平衡的面板数据集。由于 2010 年缺少一个变量,并且某些变量的值为零,因此我分两步进行:

#puts the panel data into a pdata.frame
dd <- pdata.frame(panel, index = c ('UF', 'year'))

#Takes out the year 2010
year <- dd$year
dd <- dd[year!=2010, ]

#Takes out values where population equals zero
pop <- dd$pop
dd_1 <- dd[pop!= 0,]

#Renames all variables
PIB <- dd_1$PIB
DT <- dd_1$despesa
RT <- dd_1$receita
#.....

#Runs an OLS model
ols_model <- plm(log(PIB) ~ mortinf + log(prod) + op  + log(DT) + Gini + I(log(DT)*Gini) + log(RT) + log(pop),  data = dd_1, model = "pooling")
summary (ols_model)

但是,当我按照上述方式进行操作时,我无法在图表中绘制变量的值(例如,因为 dd_1$GDP 不被视为向量)。所以我改变了数据操作顺序:我没有将数据放入 data.frame,而是先从面板中取出值,然后在 OLS 模型中指出年份和单位的索引是什么。

year <- panel$year
dd <- panel[year!=2010,]

#Takes out observations where pop == 0
pop <- dd$pop
dd_1 <- dd[pop!=0, ]

#Renames variables

GDP <- dd_1$GDP
DT <- dd_1$DT
#...
#This way I could plot, for example, GDP x DT in a graph
#Then I ran an OLS model:
ols_model <- plm(log(PIB) ~ mortinf + log(prod) + op  + log(DT) + Gini + I(log(DT)*Gini) + log(RT) + log(pop),  
data = dd_1, model = "pooling", index = c ('UF', 'year'))
summary (ols_model)

#But it gave different results than the first OLS!

据我了解,这两个模型应该输出相同的结果,但它们却大不相同。有人可以帮我吗?什么是正确的方法?提前致谢

标签: rdatabaseplm

解决方案


推荐阅读