jacob-long / jtools

Tools for summarizing/visualizing regressions and other helpful stuff
https://jtools.jacob-long.com
GNU General Public License v3.0
165 stars 22 forks source link

Unable to plot interaction of plm output, jtools error message #97

Open soominoh1 opened 4 years ago

soominoh1 commented 4 years ago

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)

jacob-long commented 2 years ago

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.

tappek commented 2 years ago

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.

jacob-long commented 2 years ago

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

tappek commented 2 years ago

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