4

I was wondering if it is possible to predict with the plm function from the plm package in R for a new dataset of predicting variables. I have create a model object using:

model <- plm(formula, data, index, model = 'pooling')

Now I'm hoping to predict a dependent variable from a new dataset which has not been used in the estimation of the model. I can do it through using the coefficients from the model object like this:

col_idx <- c(...)
df <- cbind(rep(1, nrow(df)), df[(1:ncol(df))[-col_idx]])
fitted_values <- as.matrix(df) %*% as.matrix(model_object$coefficients)

Such that I first define index columns used in the model and dropped columns due to collinearity in col_idx and subsequently construct a matrix of data which needs to be multiplied by the coefficients from the model. However, I can see errors occuring much easier with the manual dropping of columns.

A function designed to do this would make the code a lot more readable I guess. I have also found the pmodel.response() function but I can only get this to work for the dataset which has been used in predicting the actual model object.

Any help would be appreciated!

Michael
  • 1,281
  • 1
  • 17
  • 32
  • What do you need help with? It seems like a statistical question rather than a coding one... What do you mean with " I can see errors occuring much easier with the manual dropping of columns."? – llrs Dec 21 '16 at 12:02
  • See the `?pmodel.response` – m-dz Dec 21 '16 at 12:09
  • I checked `?pmodel.responde` but for the class of my model output object, which is 'plm' and 'panelmodel', using a dataframe containing predictive features as an argument for the parameter 'data' returns the original predictive values for the dataset on which the model is trained. – Michael Dec 21 '16 at 12:15
  • If you want to predict from a pooled model, just go with estimating your model by `lm()` and then use `predict.lm()` with argument `newdata`. – Helix123 Dec 21 '16 at 13:43
  • I think this should answer the question: https://stackoverflow.com/a/71904457/4640346 – Helix123 Aug 21 '22 at 21:25

1 Answers1

1

I wrote a function (predict.out.plm) to do out of sample predictions after estimating First Differences or Fixed Effects models with plm.

The function is posted here:

https://stackoverflow.com/a/44185441/2409896

eliascis
  • 317
  • 2
  • 10