diff --git a/docs/404.html b/docs/404.html index 754e85e..03ea8ad 100644 --- a/docs/404.html +++ b/docs/404.html @@ -1,66 +1,27 @@ - - -
- + + + + -DESCRIPTION
+ Grayson White. Maintainer, author. -
-White G (2022). +gglm: Grammar of Graphics for Linear Model Diagnostic Plots. +R package version 1.0.0, https://github.com/graysonwhite/gglm. +
+@Manual{, + title = {gglm: Grammar of Graphics for Linear Model Diagnostic Plots}, + author = {Grayson White}, + year = {2022}, + note = {R package version 1.0.0}, + url = {https://github.com/graysonwhite/gglm}, +}
gglm
, The Grammar of Graphics for Linear Model Diagnostics, is an R package and official ggplot2
extension that creates beautiful ggplot2
diagonostic plots for linear models that are easy to use and adhere to The Grammar of Graphics. The purpose of this package is to provide a sensible alternative to using the base-R plot()
function to produce diagnostic plots for linear models.
You can now install gglm
from CRAN, do so with this code:
-install.packages("gglm")
Or, you can install the developmental version from GitHub:
--devtools::install_github("graysonwhite/gglm")
gglm
has two main types of functions. First, the gglm()
function for quickly creating the four main diagnostic plots, similar to when you call plot()
on an lm
type object. Second, the stat_*()
functions, which produce diagnostic plots the align with The Grammar of Graphics by creating ggplot2
layers that allow for easy plotting of particular model diagnostic plots.
gglm()
+gglm
, The Grammar of Graphics for Linear Model Diagnostics, is an R package and official ggplot2
extension that creates beautiful diagnostic plots using ggplot2
for a variety of model objects. These diagnostic plots are easy to use and adhere to the Grammar of Graphics. The purpose of this package is to provide a sensible alternative to using the base-R plot()
function to produce diagnostic plots for model objects. Currently, gglm
supports all model objects that are supported by broom::augment()
, broom.mixed::augment()
, or ggplot2::fortify()
. For example, objects that are outputted from stats::lm()
, lme4::lmer()
, brms::brm()
, and many other common modeling functions will work with gglm
. The function gglm::list_model_classes()
provides a full list of model classes supported by gglm
.
gglm
can be installed from CRAN:
+install.packages("gglm")
Or, the developmental version of gglm
can be installed from GitHub:
+devtools::install_github("graysonwhite/gglm")
gglm
has two main types of functions. First, the gglm()
function is used for quickly creating the four main diagnostic plots, and behaves similarly to how plot()
works on an lm
type object. Second, the stat_*()
functions are used to produce diagnostic plots by creating ggplot2
layers. These layers allow for plotting of particular model diagnostic plots within the ggplot2
framework.
gglm()
+
-library(gglm) # Load the package -data(mtcars) # Load example data -model <- lm(mpg ~ ., data = mtcars) # Create your model - -gglm(model) # Plot the four main diagnostic plots
Consider a simple linear model used to predict miles per gallon with weight. We can fit this model with lm()
, and then diagnose it easily by using gglm()
.
+library(gglm) # Load gglm
+
+model <- lm(mpg ~ wt, data = mtcars) # Fit the simple linear model
+
+gglm(model) # Plot the four main diagnostic plots
stat_*()
functions-library(ggplot2) # Need to load ggplot2 - -ggplot(data = model) + - stat_fitted_resid()
Now, one may be interested in a more complicated model, such as a mixed model with a varying intercept on cyl
, fit with the lme4
package. Luckily, gglm
accommodates a variety of models and modeling packages, so the diagnostic plots for the mixed model can be created in the same way as they were for the simple linear model.
+library(lme4) # Load lme4 to fit the mixed model
+
+mixed_model <- lmer(mpg ~ wt + (1 | cyl), data = mtcars) # Fit the mixed model
+
+gglm(mixed_model) # Plot the four main diagnostic plots.
-# We can also add layers such as themes to these `ggplot`s and adjust features of the plot: -ggplot(data = model) + - stat_cooks_leverage(alpha = 1) + - theme_minimal()
gglm()
plots the four default diagnostic plots when supplied an lm
object. This function works similarly to plot.lm()
, except that it displays the four diagnostic plots at once.
stat_*()
functions
+gglm
also provides functionality to stay within the Grammar of Graphics by providing functions that can be used as ggplot2
layers. An example of one of these functions is the stat_fitted_resid()
function. With this function, we can take a closer look at just the fitted vs. residual plot from the mixed model fit in Example 1.
+ggplot(data = mixed_model) +
+ stat_fitted_resid()
After taking a closer look, we may want to clean up the look of the plot for a presentation or a project. This can be done by adding other layers from ggplot2
to the plot. Note that any ggplot2
layers can be added on to any of the stat_*()
functions provided by gglm
.
+ggplot(data = mixed_model) +
+ stat_fitted_resid(alpha = 1) +
+ theme_bw() + # add a clean theme
+ labs(title = "Residual vs fitted values for the mixed model") + # change the title
+ theme(plot.title = element_text(hjust = 0.5)) # center the title
Wow! What a beautiful and production-ready diagnostic plot!
+gglm()
plots the four default diagnostic plots when supplied a model object (this is similar to plot.lm()
in the case of an object generated by lm()
). Note that can gglm()
take many types of model object classes as its input, and possible model object classes can be seen with list_model_classes()
stat_normal_qq()
, stat_fitted_resid()
, stat_resid_hist()
, stat_scale_location()
, stat_cooks_leverage()
, stat_cooks_obs()
, and stat_resid_leverage()
all are ggplot2
layers used to create individual diagnostic plots. To use these, follow Example 2.
list_model_classes()
lists the model classes compatible with gglm
.
Developed by Grayson White.
+ +Developed by Grayson White.
Provides four standard visual model diagnostic plots with `ggplot2`.
gglm(data, theme = ggplot2::theme_gray(), ...)- -
data | -A model object of type `lm` or `glm`. |
-
---|---|
theme | -The theme of the `ggplot`s to be produced. |
-
... | -Currently ignored. For extendability. |
-
A a `ggplot2` object for visual diagnostic of model validity.
- -+
gglm(data, theme = ggplot2::theme_gray(), ...)
A model object of type `lm` or `glm`.
The theme of the `ggplot`s to be produced.
Currently ignored. For extendability.
A a `ggplot2` object for visual diagnostic of model validity.
+Returns the possible model classes that `gglm` works with.
+list_model_classes(...)
Currently ignored. For extendability.
A character vector containing the possible model classes that `gglm` +works with.
+Note that these are not always the exact name of the class that that +can be used. This is due to how some methods are written in the packages +`gglm` imports. For example, the model class "merMod" refers to a variety of +model outputs from `lme4`, even when the outputted class is not called +"merMod".
+list_model_classes()
+#> [1] "mlogit" "rma" "poissonmfx" "default"
+#> [5] "lmrob" "rqs" "prcomp" "rq"
+#> [9] "glmRob" "polr" "mjoint" "stl"
+#> [13] "glmmadmb" "survreg" "drc" "kmeans"
+#> [17] "fixest" "merMod" "nlrq" "factanal"
+#> [21] "htest" "lm" "glmrob" "smooth.spline"
+#> [25] "ivreg" "gam" "sarlm" "mfx"
+#> [29] "poLCA" "felm" "negbinmfx" "glmmTMB"
+#> [33] "glm" "clm" "NULL" "gamm4"
+#> [37] "speedlm" "Sarlm" "gls" "pam"
+#> [41] "decomposed.ts" "brmsfit" "speedglm" "logitmfx"
+#> [45] "betamfx" "data.frame" "plm" "Mclust"
+#> [49] "loess" "probitmfx" "coxph" "ranef.mer"
+#> [53] "lmRob" "betareg" "nls" "rlm"
+#> [57] "margins" "lme"
+
Developed by Grayson White.
+Site built with pkgdown 2.0.6.
+These objects are imported from other packages. Follow the links +below to see their documentation.
+Developed by Grayson White.
+Site built with pkgdown 2.0.6.
+R/stat_cooks_leverage.R
+ Source: R/stat_cooks_leverage.R
stat_cooks_leverage.Rd
Cook's Distance vs. Leverage
stat_cooks_leverage( - alpha = 0.5, - method = "loess", - color = "steelblue", - se = FALSE, - ... -)- -
alpha | -Adjust transparency of points. |
-
---|---|
method | -Method for fitting the line to the points. |
-
color | -Color of the line. |
-
se | -Keep standard error bands around line? |
-
... | -Currently ignored. For extendability. |
-
A `ggplot2` layer for plotting Cook's Distance vs. Leverage.
- -+data(mtcars) -model <- lm(mpg ~ cyl + disp + hp, data = mtcars) -ggplot2::ggplot(data = model) + stat_cooks_leverage() -
stat_cooks_leverage(
+ alpha = 0.5,
+ method = "loess",
+ color = "steelblue",
+ se = FALSE,
+ ...
+)
Adjust transparency of points.
Method for fitting the line to the points.
Color of the line.
Keep standard error bands around line?
Currently ignored. For extendability.
A `ggplot2` layer for plotting Cook's Distance vs. Leverage.
+`ggplot2` layer for plotting cook's distance by observation number.
stat_cooks_obs(...)+
stat_cooks_obs(...)
... | -Currently ignored. For extendability. |
-
---|
A `ggplot2` layer for plotting cook's distance by observation number.
+A `ggplot2` layer for plotting cook's distance by observation number.
++data(mtcars) -model <- lm(mpg ~ cyl + disp + hp, data = mtcars) -ggplot2::ggplot(data = model) + stat_cooks_obs() -
R/stat_fitted_resid.R
+ Source: R/stat_fitted_resid.R
stat_fitted_resid.Rd
`ggplot2` layer for plotting a fitted vs. residual scatter plot.
stat_fitted_resid(alpha = 0.5, ...)- -
alpha | -Adjust transparency of points. |
-
---|---|
... | -Currently ignored. For extendability. |
-
A `ggplot2` layer for plotting a fitted vs. residual scatter plot.
- -+data(mtcars) -model <- lm(mpg ~ cyl + disp + hp, data = mtcars) -ggplot2::ggplot(data = model) + stat_fitted_resid() -
stat_fitted_resid(alpha = 0.5, ...)
Adjust transparency of points.
Currently ignored. For extendability.
A `ggplot2` layer for plotting a fitted vs. residual scatter plot.
+Normal QQ plot.
stat_normal_qq(alpha = 0.5, ...)- -
alpha | -Adjust transparency of points. |
-
---|---|
... | -Currently ignored. For extendability. |
-
A `ggplot2` layer for plotting a Normal Q-Q plot.
- -+data(mtcars) -model <- lm(mpg ~ cyl + disp + hp, data = mtcars) -ggplot2::ggplot(data = model) + stat_normal_qq() -
stat_normal_qq(alpha = 0.5, ...)
Adjust transparency of points.
Currently ignored. For extendability.
A `ggplot2` layer for plotting a Normal Q-Q plot.
+Visualize the distribution of the residuals of a model.
stat_resid_hist(bins = 30, ...)- -
bins | -Adjust the number of bins. |
-
---|---|
... | -Currently ignored. For extendability. |
-
A `ggplot2` layer for plotting a histogram of residuals.
- -+data(mtcars) -model <- lm(mpg ~ cyl + disp + hp, data = mtcars) -ggplot2::ggplot(data = model) + stat_resid_hist() -
stat_resid_hist(bins = 30, ...)
Adjust the number of bins.
Currently ignored. For extendability.
A `ggplot2` layer for plotting a histogram of residuals.
+R/stat_resid_leverage.R
+ Source: R/stat_resid_leverage.R
stat_resid_leverage.Rd
Residual vs. leverage plot.
stat_resid_leverage( - alpha = 0.5, - method = "loess", - se = FALSE, - color = "steelblue", - ... -)- -
alpha | -Adjust transparency of points. |
-
---|---|
method | -Method for fitting the line to the points. |
-
se | -Keep standard error bands around line? |
-
color | -Color of the line. |
-
... | -Currently ignored. For extendability. |
-
A `ggplot2` layer for plotting a fitted vs. residual scatter plot.
- -+data(mtcars) -model <- lm(mpg ~ cyl + disp + hp, data = mtcars) -ggplot2::ggplot(data = model) + stat_resid_leverage() -
stat_resid_leverage(
+ alpha = 0.5,
+ method = "loess",
+ se = FALSE,
+ color = "steelblue",
+ ...
+)
Adjust transparency of points.
Method for fitting the line to the points.
Keep standard error bands around line?
Color of the line.
Currently ignored. For extendability.
A `ggplot2` layer for plotting a fitted vs. residual scatter plot.
+R/stat_scale_location.R
+ Source: R/stat_scale_location.R
stat_scale_location.Rd
Scale location diagnostic plot.
stat_scale_location( - alpha = 0.5, - na.rm = TRUE, - se = FALSE, - method = "loess", - color = "steelblue", - ... -)- -
alpha | -Adjust the transparency of points. |
-
---|---|
na.rm | -Remove points with value NA? |
-
se | -Keep standard error bands around line? |
-
method | -Method for fitting the line to the points. |
-
color | -Color of the line. |
-
... | -Currently ignored. For extendability. |
-
A `ggplot2` layer for plotting the scale location diagnostic plot.
- -+data(mtcars) -model <- lm(mpg ~ cyl + disp + hp, data = mtcars) -ggplot2::ggplot(data = model) + stat_scale_location() -
stat_scale_location(
+ alpha = 0.5,
+ na.rm = TRUE,
+ se = FALSE,
+ method = "loess",
+ color = "steelblue",
+ ...
+)
Adjust the transparency of points.
Remove points with value NA?
Keep standard error bands around line?
Method for fitting the line to the points.
Color of the line.
Currently ignored. For extendability.
A `ggplot2` layer for plotting the scale location diagnostic plot.
+