knitr::opts_chunk$set(echo = TRUE, message=FALSE,warning=FALSE,warnings=FALSE,messages=FALSE)

Note

Diagnostic plot comparisons from base and this package.

Some notes on plot comparisons. axis scale/ aspect slightly different Text annotations logic ~ highlighting "Wigglyness" of line different ~ slightly different parameter used.

Data & Libs

library(tidyverse)
library(broom)
library(gridExtra)

# Traditional Model
lm.1 <- lm(mpg ~ wt,data=mtcars)

# Generic name
My.Mod <- lm.1
# Tidy output
Tidy.Mod <- augment(My.Mod)

# Attributes
class(My.Mod)
attributes(My.Mod)
head(Tidy.Mod,4)

\newpage

Plot1: Residules v Fitted

# base diagnostic
plot(My.Mod,which=1)

# ggDiagnostic plot
D1  <- augment(My.Mod) %>% 
  ggplot(aes(x=.fitted,y=.std.resid)) +
  geom_point() +
  geom_smooth(se=FALSE,colour="red",size=.25) +
  geom_hline(yintercept=0,linetype=3,colour="blue") +
  geom_text(aes(label=ifelse((.std.resid>1*IQR(.std.resid)),.rownames,"")),
             hjust=-0.1,vjust=-0.1,size=2.5) +
  labs(title="Residuls vs Fitted",subtitle=My.Mod$call)

D1

\newpage

Plot2: Normal QQ

# base diagnostic
plot(My.Mod,which=2)

# ggDiagnostic plot
D2  <- augment(My.Mod) %>% 
  ggplot(aes(sample=.fitted)) +
  stat_qq() +
  labs(title="Normal QQ",subtitle=My.Mod$call)


D2 

\newpage

Plot3: Scale Location

# base diagnostic
plot(My.Mod,which=3)

# ggDiagnostic plot
D3  <- augment(My.Mod) %>% 
  ggplot(aes(x=.fitted, y=sqrt(abs(.std.resid)))) +
  geom_point()+
  labs(title="Scale Location",subtitle=My.Mod$call) + 
  geom_smooth(se=FALSE,colour="red",size=.25) 

D3

\newpage

Plot4: Cooks Distance

# Cooks Distance
# base diagnostic
plot(My.Mod,which=4)

# ggDiagnostic plot
D4 <- augment(My.Mod) %>% 
  ggplot(aes(x=seq_along(.cooksd), y=.cooksd)) +
  geom_point(size = .75) +
  geom_col(width = .1) +
  geom_hline(yintercept=.2,linetype=4,colour="orange") +
  labs(title="Cooks Dist",subtitle=My.Mod$call) +
  geom_text(aes(label=ifelse((.cooksd>.2),.rownames,"")),
              hjust=-0.1,vjust=-0.1,size=2.5)

D4

\newpage

Plot5: Resid vs Leverage

# Resid vs Leverage

# base diagnostic
plot(My.Mod,which=5)

# ggDiagnostic plot
D5 <- augment(My.Mod) %>% 
  ggplot(aes(x=.hat, y=.std.resid)) +
  geom_point(size=.75) +
  geom_hline(yintercept=0,linetype=3,colour="blue") +
  labs(title="Resid vs Leverage",subtitle=My.Mod$call)

D5

\newpage

Plot6: CooksSD vs Leverage

# base diagnostic
plot(My.Mod,which=6)

# ggDiagnostic plot
D6 <- augment(My.Mod) %>% 
  ggplot(aes(x=.hat, y=.cooksd)) +
  geom_point() +
  labs(title="CooksD vs Leverage",subtitle=My.Mod$call) 

D6

\newpage

Panel

#library(gridExtra)

D.Panel <- grid.arrange(D1, D2, D3, D4, D5, D6, ncol=2)
D.Panel

\newpage

par(mfrow=c(2,3))
plot(My.Mod,which=1:6)


jcastagna/ggDiagnostic documentation built on May 30, 2019, 4:34 p.m.