Version 0.4.7 brings a fix for a bug that could prevent the use of weights passed through aesthetic weight
in some of the model-fitting statistics. Several enhancements to the model fitting statistics make it easier to fit different models to different groups or panels, and make it possible/easier to select among methods supported by a model fit function.
Here is a simple example of fitting different models to different panels. Instead of using lm()
as method, we define a wrapper function that tests for the significance of the slope in linear regression, and if not significant, fits the mean instead.
poly_or_mean <- function(formula, data, ...) { mf <- lm(formula = formula, data = data, ...) if (anova(mf)[["Pr(>F)"]][1] > 0.1) { lm(formula = y ~ 1, data = data, ...) } else { mf } }
We pass the name of this function as argument to method
overriding the default argument "lm"
.
ggplot(mpg, aes(displ, hwy)) + geom_point() + stat_poly_line(method = "poly_or_mean") + stat_poly_eq(method = poly_or_mean, aes(label = after_stat(eq.label)), label.x = "right") + theme(legend.position = "bottom") + facet_wrap(~class, ncol = 2)
Which produces the figure below, showing the mean in panels 2seater and minivan, and a linear regression in others.
Aditional examples are available in the free supplementary chapters of my book Learn R: As a Language.
Changes compared to version 0.4.6, the previous version in CRAN are:
- Fix bug in the handling of the
weight
aesthetic in some of the model fitting statistics. - The model formula is in calls to
stat_poly_eq()
andstat_quant_eq()
now retrieved from the returned fitted model object. This makes it possible model selection within the function passed as argument tomethod
. (Inspired by an answer read in Stackoverflow.) - Statistics now search for a matching function when an arbitrary name is supplied as a character string argument to parameter
method
. - The character string passed as argument to parameter
method
is now parsed so that it can contain both the name of a model fit function and the argument to be passed to this function’s ownmethod
parameter. (Backward compatibility is maintained.) - The stats that create equation labels now include a variable
method
in the returneddata
containing a character string with the method used in the model fit.
Documentation web site at http://docs.r4photobiology.info/ggpmisc/ includes all help pages, with output from all examples, vignettes as well as a changelog in HTML format.
NOTE: Version 0.4.7 is on its way to CRAN.
Please raise issues concerning bugs or enhancements to this package through GitHub https://github.com/aphalo/ggpmisc/issues