jtools icon indicating copy to clipboard operation
jtools copied to clipboard

Unable to plot interaction of plm output, jtools error message

Open soominoh1 opened this issue 5 years ago • 4 comments

Hello. I'm trying to plot the interaction of a plm output, but it keeps giving me the following error. I've tried uninstalling and reinstalling R and the packages therein, but to no avail. I googled the error and there seems to be another person who has the same issue (StackOverflow post). Would you be so kind as to help figure out what is happening? Your help is much appreciated. Thank you!

Model run & error:

plm(data = data, formula = y ~ x1 + x2 + x1*x2, model = "within", index = c("grp", "year")))

Error: ~does not appear to be a one- or two-sided formula. y does not appear to be a one- or two-sided formula. x1 + x2 + x1 * x2 does not appear to be a one- or two-sided formula. Backtrace:

interactions::interact_plot(...) jtools::get_data(model, warn = TRUE, ...) jtools:::get_lhs(formula)

soominoh1 avatar Nov 23 '20 11:11 soominoh1

Well I am going to fix the thing that gives the error seen here, but that unfortunately does not get us plotted predictions. As best as I can tell, the predict.plm() function is rather half-baked and people are consistently getting errors trying to use it with within models. Two other packages that do similar things to this one, prediction and ggeffects, report the same issues and neither have resolved the problem.

Going to paste some links here in case I (or someone else) decide to revisit: https://github.com/leeper/prediction/issues/19 https://github.com/strengejacke/ggeffects/issues/121 https://stackoverflow.com/questions/31954045/r-plotting-panel-model-predictions-using-plm-pglm https://stackoverflow.com/questions/64892622/plotting-interaction-terms-from-fixed-effects-models-plm https://stackoverflow.com/questions/71366468/predict-on-test-data-using-plm-package-in-r-and-calculate-rmse-for-test-data

I suspect the solution here is to define a prediction function for plm models within jtools, but that's not something I'm willing to do right now. I don't understand plm (or the generation of model predictions in general) well enough to be confident that I would produce accurate outputs.

jacob-long avatar Apr 08 '22 03:04 jacob-long

There is now support for predict for plm's within models in the development version available here: https://github.com/ycroissant/plm

As the within case is special, one likely wants to input a pdata.frame and not a plain data frame; the help page ?predict.plm has the details.

tappek avatar Apr 17 '22 16:04 tappek

Thank you @tappek for letting me know! I'll give some thought to the best way to implement.

jacob-long avatar Apr 19 '22 18:04 jacob-long

For models produced by plm::plm(), there is a predict method available since plm CRAN release version 2.6-2.

tappek avatar Aug 20 '22 10:08 tappek