Last active
May 24, 2018 02:12
-
-
Save DoktorMike/6278065 to your computer and use it in GitHub Desktop.
This is my influence plot function. It uses ggplot to visualize leverage and studentized errors in a balloon plot where the balloon size is scaled by Cook's distance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plotInfluence <- function (model, fill="white", | |
| outline="black", size=30) { | |
| require(ggplot2) | |
| if(!inherits(model, "lm")) | |
| stop("You need to supply an lm object.") | |
| df<-data.frame(Residual=rstudent(model), | |
| Leverage=hatvalues(model), | |
| Cooks=cooks.distance(model), | |
| Observation=names(hatvalues(model)), | |
| stringsAsFactors=FALSE) | |
| myxint<-c(2*mean(df$Leverage), 3*mean(df$Leverage)) | |
| inds<-intersect(which(abs(df$Residual) < 2), | |
| which( df$Leverage < myxint[1])) | |
| if(length(inds) > 0) df$Observation[inds]<-"" | |
| ggplot(df, aes_string(x='Leverage', y='Residual', | |
| size='Cooks', label='Observation'), | |
| legend=FALSE) + | |
| geom_point(colour=outline, fill=fill, shape=21) + | |
| scale_size_area(max_size=size) + | |
| theme_bw(base_size=16) + geom_text(size=4) + | |
| geom_hline(yintercept=c(2,-2), linetype="dashed") + | |
| geom_vline(xintercept=myxint, linetype="dashed") + | |
| ylab("Studentized Residuals") + | |
| xlab("Hat-Values") + labs(size="Cook's distance") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment