Skip to main content
Tweeted twitter.com/#!/StackStats/status/143062462445522944
edited tags
Link
Tal Galili
  • 21.9k
  • 36
  • 147
  • 208
added 332 characters in body
Source Link
Tal Galili
  • 21.9k
  • 36
  • 147
  • 208

I started digging a bit into the plot.lm function, this function gives six plots for lm, they are:

  1. a plot of residuals against fitted values
  2. a Scale-Location plot of sqrt(| residuals |) against fitted values
  3. a Normal Q-Q plot, a plot of Cook's distances versus row labels
  4. a plot of residuals against leverages
  5. a plot of Cook's distances against leverage/(1-leverage)

And I am wondering what other useful diagnostic plots (orcommon/useful extensions of current plots) exists for linear models, and how can they be done in R? (links to articles of packages are also welcomed)

So herethe boxcox function (from {MASS}) is an example of another useful diagnostic plot (and such an answer would be great), however, I am more curious about variations/extensions on existing default diagnostic plots for lm in R (although general other remarks on the topic are always welcomed).

Here are some simple examples of what I mean:

#Some example code for all of us to refer to
set.seed(2542)
x1 <- rnorm(100)
x2 <- runif(100, -2,2)
eps <- rnorm(100,0,2)
y <- 1 + 2*x1 + 3*x2 + eps
y[1:4] <- 14 # adding some contaminated points
fit <- lm(y~x1+x2)

#plot(y~x1+x2)
#summary(fit)

To plot the residuals vs each of the potential x

plot(resid(fit)~x1); abline (h = 0)
plot(resid(fit)~x2); abline (h = 0)
# plot(resid(fit)~x1+x2) # you can also use this, but then you wouldn't be able to use the abline on any plot but the last one

To add the the 0-1 line (how is this line called in English?!) to the qqplot so to see how much the qqline deviates from it

plot(fit, which = 2); abline(0,1, col = "green")

To plot the qq-plot using externally studentized residuals

# plot(fit, which = 2); abline(0,1, col = "green") # The next command is just like this one
qqnorm(rstandard(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
qqnorm(rstudent(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
# We can note how the "bad" points are more extreme when using the rstudent

I started digging a bit into the plot.lm function, this function gives six plots for lm, they are:

  1. a plot of residuals against fitted values
  2. a Scale-Location plot of sqrt(| residuals |) against fitted values
  3. a Normal Q-Q plot, a plot of Cook's distances versus row labels
  4. a plot of residuals against leverages
  5. a plot of Cook's distances against leverage/(1-leverage)

And I am wondering what other useful diagnostic plots (or extensions of current plots) exists for linear models, and how can they be done in R?

So here are some simple examples of what I mean:

#Some example code for all of us to refer to
set.seed(2542)
x1 <- rnorm(100)
x2 <- runif(100, -2,2)
eps <- rnorm(100,0,2)
y <- 1 + 2*x1 + 3*x2 + eps
y[1:4] <- 14 # adding some contaminated points
fit <- lm(y~x1+x2)

#plot(y~x1+x2)
#summary(fit)

To plot the residuals vs each of the potential x

plot(resid(fit)~x1); abline (h = 0)
plot(resid(fit)~x2); abline (h = 0)
# plot(resid(fit)~x1+x2) # you can also use this, but then you wouldn't be able to use the abline on any plot but the last one

To add the the 0-1 line (how is this line called in English?!) to the qqplot so to see how much the qqline deviates from it

plot(fit, which = 2); abline(0,1, col = "green")

To plot the qq-plot using externally studentized residuals

# plot(fit, which = 2); abline(0,1, col = "green") # The next command is just like this one
qqnorm(rstandard(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
qqnorm(rstudent(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
# We can note how the "bad" points are more extreme when using the rstudent

I started digging a bit into the plot.lm function, this function gives six plots for lm, they are:

  1. a plot of residuals against fitted values
  2. a Scale-Location plot of sqrt(| residuals |) against fitted values
  3. a Normal Q-Q plot, a plot of Cook's distances versus row labels
  4. a plot of residuals against leverages
  5. a plot of Cook's distances against leverage/(1-leverage)

And I am wondering what other common/useful extensions of current plots exists for linear models, and how can they be done in R? (links to articles of packages are also welcomed)

So the boxcox function (from {MASS}) is an example of another useful diagnostic plot (and such an answer would be great), however, I am more curious about variations/extensions on existing default diagnostic plots for lm in R (although general other remarks on the topic are always welcomed).

Here are some simple examples of what I mean:

#Some example code for all of us to refer to
set.seed(2542)
x1 <- rnorm(100)
x2 <- runif(100, -2,2)
eps <- rnorm(100,0,2)
y <- 1 + 2*x1 + 3*x2 + eps
y[1:4] <- 14 # adding some contaminated points
fit <- lm(y~x1+x2)

#plot(y~x1+x2)
#summary(fit)

To plot the residuals vs each of the potential x

plot(resid(fit)~x1); abline (h = 0)
plot(resid(fit)~x2); abline (h = 0)
# plot(resid(fit)~x1+x2) # you can also use this, but then you wouldn't be able to use the abline on any plot but the last one

To add the the 0-1 line (how is this line called in English?!) to the qqplot so to see how much the qqline deviates from it

plot(fit, which = 2); abline(0,1, col = "green")

To plot the qq-plot using externally studentized residuals

# plot(fit, which = 2); abline(0,1, col = "green") # The next command is just like this one
qqnorm(rstandard(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
qqnorm(rstudent(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
# We can note how the "bad" points are more extreme when using the rstudent
Source Link
Tal Galili
  • 21.9k
  • 36
  • 147
  • 208

Possible extensions to the default diagnostic plots for lm (in R and in general)?

I started digging a bit into the plot.lm function, this function gives six plots for lm, they are:

  1. a plot of residuals against fitted values
  2. a Scale-Location plot of sqrt(| residuals |) against fitted values
  3. a Normal Q-Q plot, a plot of Cook's distances versus row labels
  4. a plot of residuals against leverages
  5. a plot of Cook's distances against leverage/(1-leverage)

And I am wondering what other useful diagnostic plots (or extensions of current plots) exists for linear models, and how can they be done in R?

So here are some simple examples of what I mean:

#Some example code for all of us to refer to
set.seed(2542)
x1 <- rnorm(100)
x2 <- runif(100, -2,2)
eps <- rnorm(100,0,2)
y <- 1 + 2*x1 + 3*x2 + eps
y[1:4] <- 14 # adding some contaminated points
fit <- lm(y~x1+x2)

#plot(y~x1+x2)
#summary(fit)

To plot the residuals vs each of the potential x

plot(resid(fit)~x1); abline (h = 0)
plot(resid(fit)~x2); abline (h = 0)
# plot(resid(fit)~x1+x2) # you can also use this, but then you wouldn't be able to use the abline on any plot but the last one

To add the the 0-1 line (how is this line called in English?!) to the qqplot so to see how much the qqline deviates from it

plot(fit, which = 2); abline(0,1, col = "green")

To plot the qq-plot using externally studentized residuals

# plot(fit, which = 2); abline(0,1, col = "green") # The next command is just like this one
qqnorm(rstandard(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
qqnorm(rstudent(fit), ylim = c(-2.2,4.2)); qqline(rstudent(fit), lty = 2) ;abline(0,1, col = "green")
# We can note how the "bad" points are more extreme when using the rstudent