Skip to content

Instantly share code, notes, and snippets.

@akastrin
Created October 2, 2012 19:47
Show Gist options
  • Save akastrin/3822848 to your computer and use it in GitHub Desktop.
Save akastrin/3822848 to your computer and use it in GitHub Desktop.
Residual plots for detecting possible anomalies in the case of linear regression.
# Setup
library(ggplot2)
library(grid)
library(gridExtra)
# Rectangle
n <- 500
x = runif(n, -1, 1)
y = runif(n, -0.25, 0.25)
df <- data.frame(x = x, y = y)
plt1 <- ggplot(df, aes(x = x, y = y)) + geom_point(size = 1) +
theme(axis.title.x = element_text(size=10),
axis.title.y = element_text(size=10),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = unit(rep(1, 4), "lines")) +
xlab(expression(hat(y))) + ylab(expression(epsilon)) +
scale_y_continuous(limits = c(-1, 1))
# Half disc
R <- 4
theta <- seq(0, pi, by = 0.005)
x <- R * cos(theta);
y <- R * sin(theta);
mult <- 2;
x <- x + mult * runif(length(x))
y <- y + mult * runif(length(y))
df <- data.frame(x = x, y = y)
plt2 <- ggplot(df, aes(x = x, y = y)) + geom_point(size = 1) +
theme(axis.title.x = element_text(size=10),
axis.title.y = element_text(size=10),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = unit(rep(1, 4), "lines")) +
xlab(expression(hat(y))) + ylab(expression(epsilon))
# Triangle
u <- runif(800, 0, 1)
v <- runif(800, -1, 1)
tmp <- sqrt(u)
x <- 1 - tmp
y <- v * tmp
df <- data.frame(x = x, y = y)
plt3 <- ggplot(df, aes(x = x, y = y)) + geom_point(size = 1) +
theme(axis.title.x = element_text(size=10),
axis.title.y = element_text(size=10),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = unit(rep(1, 4), "lines")) +
xlab(expression(hat(y))) + ylab(expression(epsilon))
# Linear
n <- 500
x = runif(n, -1, 1)
y = x*1 + runif(n, -1, 0)
df <- data.frame(x = x, y = y)
plt4 <- ggplot(df, aes(x = x, y = y)) + geom_point(size = 1) +
theme(axis.title.x = element_text(size=10),
axis.title.y = element_text(size=10),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
plot.margin = unit(rep(1, 4), "lines")) +
xlab(expression(hat(y))) + ylab(expression(epsilon))
# Plot 2 by 2 figure
pdf("test.pdf")
sidebysideplot <- grid.arrange(plt1, plt2, plt3, plt4, ncol = 2)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment