aphalo / ggpmisc

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

Implement model-fitting statistics using compute-panel functions #30

Open tillrose opened 1 year ago

tillrose commented 1 year ago

Is it right that ggpmisc does not support lm models without interactions? Any easy way to handle it?

aphalo commented 1 year ago

I am not sure to understand your question. Within ggplot2 one can only define a model using as variables the name of aesthetics, as statistics do not have accesses to the user's data. There more than one statistic that supports lm, so please explain in more detail what you are trying to do.

tillrose commented 1 year ago

Within ggplot2 all grouping variables are always added with full interaction to the statistical model: y ~ x * group

but in an analysis it is sometimes wanted to add the grouping variable without interaction: y ~ x + group

It is easy to add model predictions of such a predefined model but to add the model equations is hard. It would be great if ggpmisc could handle these cases but I fully understand that it is against the framework.

aphalo commented 1 year ago

I guess you are using stat_poly_eq(). There are two ways in which statistics can be defined. Many are defined using computations per group. So, what you are seeing is not that the interaction is included in the model, but that a simple y ~ x model is being fit separately to each group. It is in possible to define stats using computations by panel, so I could in principle write a new statistic for the behaviour you wish, but the difficulty is that building the equations automatically becomes much more complex. So, the formatting of the equation is difficult to automate unless some restriction is imposed. At the moment the only stat in 'ggpmisc' defined to work by panel is stat_fit_tb() but its output is formatted to make it easy to add ANOVA and summary tables as insets. It should be possible, but not too easy to use the coefficient estimates from the summary table to assemble the model equation within the call to aes(). Anyway, it should be easy for me to create a variation of stat_fit_tb() that makes it easier to build equations or a new version of stat_poly_eq() that handles specifically the kind of models you want to use.

aphalo commented 1 year ago

@tillrose Thanks for suggesting this enhancement!

tillrose commented 1 year ago

Thanks for your positive reply. I think, you already got what I mean. But because I have a figure at hand, I just add an example. example

aphalo commented 1 year ago

Thanks! o.k, got it now. You fit a single model but still want to have one equation for each group.

aphalo commented 1 year ago

Moved to a later milestone. Requires implementing a new set of stats based on compute_panel() functions.

aphalo commented 2 months ago

A possible way of handling this request is to implement new statistics using compute panel functions and include the grouping in the model formula. This can be fairly tricky with multiple groupings unless we use an approach similar to that used for grp.label and ignore the groupings created by mappings to true aesthetics.