Installing ggDiagnose

The follow code is how to install the package. The package "requires" you to start out with few packages. If you don't have the correct ones it will prompt you to load them when you run specific ggDiagnose functions.

#library(devtools)
#devtools::install_github("benjaminleroy/ggDiagnose")
library(ggDiagnose)
library(ggplot2)
library(tidyverse)
library(gridExtra)

ggDiagnose

ggDiagnose.lm

This example is for an lm object, function works for glm and rlm objects as well.

lm.object <- lm(Sepal.Length ~., data = iris)

The original visualization:

par(mfrow = c(2,3))
plot(lm.object, which = 1:6)

The updated visualization (note we supressed warnings):

ggDiagnose(lm.object, which = 1:6)
noleg <- ggplot2::theme(legend.position = "none")
gglist <- ggDiagnose(lm.object, which = 1:6,return = TRUE,show.plot = FALSE)$gglist
gglist_update <- gglist %>% lapply(function(p){p + aes_string(color = "Species") + noleg})

grid.arrange(grobs = gglist_update, nrow = 2)  

ggDiagnose.lm allows for similar parameter inputs as plot.lm but also includes additional ones. This may changes as the package evolves.

ggDiagnose.glmnet

library(glmnet)
glmnet.object <- glmnet(y = iris$Sepal.Length, 
                           x = model.matrix(Sepal.Length~., data = iris))

The original visualization:

plot(glmnet.object)

The updated visualization:

ggDiagnose(glmnet.object)

ggDiagnose.cv.glmnet

cv.glmnet.object <- cv.glmnet(y = iris$Sepal.Length, 
                              x = model.matrix(Sepal.Length~., data = iris))

The original visualization:

plot(cv.glmnet.object)

The updated visualization:

ggDiagnose(cv.glmnet.object)

ggDiagnose.Gam

library(gam)
gam.object <- gam::gam(Sepal.Length ~ gam::s(Sepal.Width) + Species,
                  data = iris)

The original visualization:

par(mfrow = c(1,2))
plot(gam.object, se = TRUE, residuals = TRUE)

The updated visualization:

ggDiagnose(gam.object, residuals = TRUE) # se = TRUE by default

ggDiagnose.tree

Note, for more perfect replication of the base plot function add + ggdendro::theme_dendro() which drops all ggplot background elements.

library(tree)

tree.object <- tree(Sepal.Length ~., data = iris)

The original visualization:

plot(tree.object)
text(tree.object)

The updated visualization (followed by quick improvement):

ggDiagnose(tree.object, split.labels = FALSE)
ggDiagnose(tree.object, split.labels = TRUE,
           leaf.labels = TRUE)

dfCompile

Note this section uses functionality that can be found in the dplyr library.

library(dplyr)

dfCompile.lm

lm.object <- lm(Sepal.Length ~., data = iris)
dfCompile(lm.object) %>% names
dfCompile(lm.object) %>% head(2) # needs package dplyr for "%>%"

dfCompile.glmnet

library(glmnet)
glmnet.object <- glmnet(y = iris$Sepal.Length,
                        x = model.matrix(Sepal.Length~., data = iris))
dfCompile(glmnet.object) %>% names # needs package dplyr for "%>%"
dfCompile(glmnet.object) %>% head(2) # needs package dplyr for "%>%"

dfCompile.cv.glmnet

library(glmnet)
cv.glmnet.object <- cv.glmnet(y = iris$Sepal.Length,
                              x = model.matrix(Sepal.Length~., data = iris))
dfCompile(cv.glmnet.object) %>% names # needs package dplyr for "%>%"
dfCompile(cv.glmnet.object) %>% head(2) # needs package dplyr for "%>%"

dfCompile.Gam

library(gam)
gam.object <- gam::gam(Sepal.Length ~ gam::s(Sepal.Width) + Species,
                       data = iris)
dfCompile(gam.object) %>% names # needs package dplyr for "%>%"
dfCompile(gam.object) %>% head(2) # needs package dplyr for "%>%"

dfCompile.tree

library(gam)
gam.object <- gam::gam(Sepal.Length ~ gam::s(Sepal.Width) + Species,
                       data = iris)
dfCompile(tree.object) %>% length # needs package dplyr for "%>%"
dfCompile(tree.object)$segments %>% head # needs package dplyr for "%>%"

dfCompile(tree.object)$labels %>% head # needs package dplyr for "%>%"

dfCompile(tree.object)$leaf_labels %>% head # needs package dplyr for "%>%"


benjaminleroy/ggDiagnose documentation built on May 4, 2019, 3:07 a.m.