r - 用plm方法预测

标签 r predict panel-data plm

我正在使用 plm 包来估计面板数据的随机效应模型。 读书this question关于 plm 包中的预测让我有些怀疑。它究竟是如何工作的?我尝试了 3 种替代方法,它们给出了不同的解决方案。为什么?

library(data.table); library(plm)

set.seed(100)
DT <- data.table(CJ(id=c(1,2,3,4), time=c(1:10)))
DT[, x1:=rnorm(40)]
DT[, x2:=rnorm(40)]
DT[, y:=x1 + 2*x2 + rnorm(40)/10 + id]
DT <- DT[!(id=="a" & time==4)] # just to make it an unbalanced panel
setkey(DT, id, time)    

summary(plmFEit <- plm(data=DT, id=c("id","time"), formula=y ~ x1 + x2, model="random"))    
    ###################
    #method 1
    ###################
    # Extract the fitted values from the plm object
    FV <- data.table(plmFEit$model, residuals=as.numeric(plmFEit$residuals))
    FV[, y := as.numeric(y)]
    FV[, x1 := as.numeric(x1)]
    FV[, x2 := as.numeric(x2)]

    DT <- merge(x=DT, y=FV, by=c("y","x1","x2"), all=TRUE)
    DT[, fitted.plm_1 := as.numeric(y) - as.numeric(residuals)]                
    ###################
    #method 2
    ###################        
    # calculate the fitted values 
    DT[, fitted.plm_2 := as.numeric(coef(plmFEit)[1]+coef(plmFEit)[2] * x1 + coef(plmFEit)[3]*x2)]                
    ###################
    #method 3
    ###################
    # using pmodel.response 
    DT$fitted.plm_3 <-pmodel.response(plmFEit,model='random') 

最佳答案

方法一: 这可以更容易地完成:

FV <- data.table(plmFEit$model, residuals=as.numeric(plmFEit$residuals))
FV[ , fitted := y - residuals]

但是,它给出的拟合值与您使用 merge()

的方法相同

方法二: 您正在拟合一个随机效应模型(尽管您将其命名为 plmFEit,其中 FE 通常表示固定效应)。与方法 1 相比,您缺少特殊错误项。

方法三: pmodel.response() 为您提供响应变量(在本例中为 y),但应用了指定的转换(随机效应转换(“准贬值”))到它(参见 pmodel.response())。

我想,你想要的是方法1给出的。

关于r - 用plm方法预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28547614/

相关文章:

r - 了解 R data.table 中非标准评估的范围

r - 从 flexmix 对象预测 (R)

r - R中面板数据的相关矩阵

python - Pandas 具有多索引的一阶差分面板数据

r - 在R Shiny中显示/隐藏整个框元素

r - 不使用正则表达式分割字符串

mysql - RMySQL dbWriteTable 与 field.types

r - 黄土线绘制不正确

r - R中的并行预测

r - 固定效应工具变量 (IV) 回归与可用的诊断测试