aphalo / ggpmisc

R package ggpmisc is an extension to ggplot2 and the Grammar of Graphics
https://docs.r4photobiology.info/ggpmisc
94 stars 6 forks source link

Add validation of model formulas as polynomials in increasing order #63

Closed aleungplants closed 3 months ago

aleungplants commented 3 months ago

This is the result of running an lm with a quadratic formula.

coef(lm(formula = mpg ~ I(wt^2) + wt, data = mtcars)) (Intercept) I(wt^2) wt 49.930811 1.171087 -13.380337

However, running this code returns a figure that says y = 49.9+1.17x+13.4x^2, when it should say 49.9+13.4x+1.17x^2.

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + ggpmisc::stat_poly_eq( formula = y ~ I(x^2)+I(x), ggpmisc::use_label(labels = c("eq")), label.x = "right") image

aphalo commented 3 months ago

@aleungplants

The expected input is: y ~ x + I(x^2)

I recently added a warning, to the development version, highlighting that when writing the model formula the coefficients must be in order of increasing powers, or alternatively the model formula should be written using poly() and passing raw = TRUE. In the development version, based on an earlier request I have also added the possibility of having an equation label with the terms in order of decreasing powers by passing "decreasing = TRUE". However, even in this case the order in the model formula must remain increasing. I hope this solves the problem.

Ideally, the stats would check the syntax of the model formulas and reject those not matching the expectations with an error message. Writing code for this check I expect not to be simple, so it will take some time for me to write it, unless someone steps in to help with a pull request, or I find some example. Meanwhile, I hope the warning section I added in the help pages, will help.

Thanks for the report! I will keep this issue open but on hold, and edit the title.

aphalo commented 3 months ago

Added code to validate the model formula as an increasing polynomial. For the time being it only triggers a warning as I am not sure it recognises all valid formulas. After some checking, I will disable the generation of an equation when a model formula fails its validation.

aphalo commented 3 months ago

@aleungplants The new development version will be available at the R-Universe in about one hour. If you want to try the updated version of äggpmisc' you can install it from https://aphalo.r-universe.dev/ggpmisc#. I will appreciate feedback if you can give it. Thanks again for the bug report.