0

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 NAs 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 NAs. I would like the fitted values padded with NAs where the original dataset has NAs. There must be a smart and simple way to retain pad the NAs from dt to my fitted values, but I can't see it. Any help much appreciated!

landroni
  • 2,902
  • 1
  • 32
  • 39
Mace
  • 1,259
  • 4
  • 16
  • 35

1 Answers1

0

I found the following solution using complete.cases. It works, but there are probably better ways:

fited.values <- rep(NA,nrow(dt))
fited.values[complete.cases(dt)] <- fit.plm$model[[1]]-fit.plm$residuals

fited.values
 [1]           NA  0.044116999 -0.001511951  0.182792055 -0.136758888
 [6] -0.009162091  0.220851814  0.221807764  0.228046083  0.297558446
[11]           NA           NA  0.130133821  0.211737223  0.339328498
[16]  0.379826505  0.102156480  0.024129950  0.213088736  0.235454141
[21]  0.321319682  0.420673101  0.474030175  0.497573470  0.205056353
[26]  0.168080225  0.309537308  0.010202845  0.082264514  0.260143856
Mace
  • 1,259
  • 4
  • 16
  • 35
  • I see this is selected as an answer already, but in the post linked to in the question Millo Giovanni (one of the `plm` package authors) says "... the input order is not always preserved...". This code works if you only want a vector with the fitted values, but one should be careful if the intention is to merge back into the original data. See for example my proposed answer in this more [general question](http://stackoverflow.com/questions/23143428/merge-plm-fitted-values-to-dataset) . – Peter Nov 13 '15 at 18:52