Reputation: 2147
I have plotted Cook's distance via the car package:
library(car)
influenceIndexPlot(model, id.n = 5, vars = c("Cook"))
http://gyazo.com/4b15671a0ee35944c4b5b273205f1b72
I would like to draw all points and the lines of these points in red, which are above 0.01.
Is there a way how I could do that? Thank you very much in advance!
Upvotes: 0
Views: 718
Reputation: 14192
I'm not sure how to use that particular function from the car
package to do what you are asking. It is just a wrapper to make a base-r plot.
here, I'm showing you how to make the same sort of plot in ggplot2
.
Example: make some sample data and run a linear model:
set.seed(84)
df <- data.frame(x = rnorm(100, 10, 5), y = rnorm(100, 12, 5))
model <- lm(y ~ x, df)
now we get the Cook's distances, create a dataframe and assign groups - either 0 (below 0.01) or 1 (above 0.01):
N <- nrow(model$model) #this is just to get the number of observations - it's obvious it's 100 as we chose that, but put it here for automation
df <- data.frame(Index = 1:N, Cook = cooks.distance(model))
df$group <- factor(ifelse(df$Cook > 0.01, 1, 0))
Now we plot it:
library(ggplot2)
ggplot(df, aes(Index, Cook, color=group, group=group)) +
geom_point(size=3) +
geom_segment(aes(Index, xend=Index, 0, yend=Cook, color=group), data=df) +
theme_bw() +
scale_color_manual(values=c("black", "red1")) +
ylab("Cook's Distance") +
ggtitle("Diagnostic Plots") +
theme(legend.position = "none")
It looks like this:
Upvotes: 1