如何处理面板数据回归中的NA值?

10

我正在尝试在包含NA的数据上预测拟合值,这是基于plm生成的模型。以下是一些示例代码:

require(plm)
test.data <- data.frame(id=c(1,1,2,2,3), time=c(1,2,1,2,1), 
   y=c(1,3,5,10,8), x=c(1, NA, 3,4,5))
model <- plm(y ~ x, data=test.data, index=c("id", "time"), 
       model="pooling", na.action=na.exclude)
yhat <- predict(model, test.data, na.action=na.pass)
test.data$yhat <- yhat
当我运行最后一行时,出现错误,提示替换有4行,而数据有5行。
我不知道如何让预测返回长度为5的向量...
如果我运行lm(如下所示),而不是运行plm,我将得到预期的结果。
model <- lm(y ~ x, data=test.data, na.action=na.exclude)

这个问题在plm的2.6-2版本中得到了解决,详见https://dev59.com/7r_pa4cB1Zd3GeqP-wQC#71904457。 - Helix123
1个回答

9
截至2022年8月16日,plm版本2.6.2,这个功能应该可以直接使用:Predict out of sample on fixed effects model(见NEWS文件)。其中固定效应模型的预测实现包括支持参数newdata以及样本外预测。特别添加了帮助页面(?predict.plm),专门解释固定效应模型的预测和样本外情况。我认为这是predict.plm应该为您处理的事情——似乎是软件包作者的疏忽,但您可以使用?napredict自己实现它。
 pp <- predict(model, test.data)
 na.stuff <- attr(model$model,"na.action")
 (yhat <- napredict(na.stuff,pp))
 ## [1] 1.371429       NA 5.485714 7.542857 9.600000

这个问题在plm的2.6-2版本中得到了解决,详见https://dev59.com/7r_pa4cB1Zd3GeqP-wQC#71904457。 - Helix123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接