rpredictplm

Predict out of sample on fixed effects model


Let's consider model :

library(plm)
data("Produc", package = "plm")
model <-  plm(pcap ~ hwy + water, data = Produc, model = 'within')

To calculate fitted value of the model we just need to use :

predict(model)

However, when trying to do this out of sample :

predict(model, newdata = data.frame('hwy' = 1, 'water' = 1))

Will get error :

Error in crossprod(beta, t(X)) : non-conformable arguments

Which is quite strange for me because this code will work for any model expect 'within'. I search that there is a function fixef which do predictions on fixed effect model but unfortunately - only in sample. So : Is there any solution how can we predict out of sample on fixed effect model ?


Solution

  • Just delete intercept for model :

    model <-  plm(pcap ~ 0 + hwy + water, data = Produc, model = 'within')
    predict(model, newdata = data.frame('hwy' = 1, 'water' = 1))
    3.980911