如何在面板数据回归中处理NA? [英] How to deal with NA in a panel data regression?
问题描述
我试图基于包含NA
的数据并基于由plm
生成的模型来预测拟合值.这是一些示例代码:
I am trying to predict fitted values over data containing NA
s, and based on a model generated by plm
. Here's some sample code:
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行.
When I run the last line I get an error stating that the replacement has 4 rows while data has 5 rows.
我不知道如何获得预测返回长度为5的向量...
I have no idea how to get predict return a vector of length 5...
如果不是运行plm
,而是运行lm
(如下行),则会得到预期的结果.
If instead of running a plm
I run an lm
(as in the line below) I get the expected result.
model <- lm(y ~ x, data=test.data, na.action=na.exclude)
推荐答案
我认为这是predict.plm
应为您处理的事情-似乎是程序包作者的疏忽-但您可以使用?napredict
自己实现:
I think this is something that predict.plm
ought to handle for you -- seems like an oversight on the package authors' part -- but you can use ?napredict
to implement it for yourself:
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
这篇关于如何在面板数据回归中处理NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!