I have the following dataset
seed(1)
dt <- data.frame(name= rep(c("A", "B", "C"), c(9,11,10)),
year=c(2001:2009,2000,2002:2011,2001:2010),
var1=c(NA,rnorm(10),NA,rnorm(18)),
var2=c(rnorm(10),NA,NA,rnorm(18)), var3=rnorm(30))
containing NA
s in some of the variables. I use the plm
package to estimate the following model:
fit.plm <- plm(var1 ~ var2 + var3, data=dt, model = "within", index=c("name","year"), na.action = na.exclude)
Now, I would like to extract the fitted values. As I understand the best way to do this is subtract the residuals from my response variable var1
(since there is not function to get fitted values in pml
(See here):
fit.plm$model[[1]]-fit.plm$residuals
2 3 4 5 6
-0.18364082 0.36118823 0.02070257 0.78060817 0.05237859
7 8 9 10 13
0.12783116 0.03599601 -0.17847569 0.11584831 0.21904021
14 15 16 17 18
0.75298182 0.18605829 -0.15536450 0.30810595 -0.13921289
19 20 21 22 23
-0.35047492 0.08139121 -0.02019619 0.14397486 0.07854582
24 25 26 27 28
-0.01082184 -0.05211639 -0.02904097 0.43262570 -0.46925312
29 30
0.37524551 0.35541691
but it excludes the rows with the NA
s. I would like the fitted values padded with NA
s where the original dataset has NA
s. There must be a smart and simple way to retain pad the NA
s from dt
to my fitted values, but I can't see it. Any help much appreciated!