r - ggplot2: How to curve small gaussian densities on a regression line? -
i want graphically show assumptions of linear (and later other type) regression. how can add plot small gaussian densities (or type of densities) on regression line in figure:
you can compute empirical densities of residuals sections along fitted line. then, matter of drawing lines @ positions of choosing in each interval using geom_path. add theoretical distribution, generate densities along range of residuals each section (here using normal density). normal densities below, standard deviation each 1 determined each section residuals, choose standard deviation of them , use instead.
## sample data set.seed(0) dat <- data.frame(x=(x=runif(100, 0, 50)), y=rnorm(100, 10*x, 100)) ## breaks: want compute densities breaks <- seq(0, max(dat$x), len=5) dat$section <- cut(dat$x, breaks) ## residuals dat$res <- residuals(lm(y ~ x, data=dat)) ## compute densities each section, , flip axes, , add means of sections ## note: densities need scaled in relation section size (2000 here) dens <- do.call(rbind, lapply(split(dat, dat$section), function(x) { d <- density(x$res, n=50) res <- data.frame(x=max(x$x)- d$y*2000, y=d$x+mean(x$y)) res <- res[order(res$y), ] ## data normal lines xs <- seq(min(x$res), max(x$res), len=50) res <- rbind(res, data.frame(y=xs + mean(x$y), x=max(x$x) - 2000*dnorm(xs, 0, sd(x$res)))) res$type <- rep(c("empirical", "normal"), each=50) res })) dens$section <- rep(levels(dat$section), each=100) ## plot both empirical , theoretical ggplot(dat, aes(x, y)) + geom_point() + geom_smooth(method="lm", fill=na, lwd=2) + geom_path(data=dens, aes(x, y, group=interaction(section,type), color=type), lwd=1.1) + theme_bw() + geom_vline(xintercept=breaks, lty=2) or, gaussian curves
## normal ggplot(dat, aes(x, y)) + geom_point() + geom_smooth(method="lm", fill=na, lwd=2) + geom_path(data=dens[dens$type=="normal",], aes(x, y, group=section), color="salmon", lwd=1.1) + theme_bw() + geom_vline(xintercept=breaks, lty=2) 


Comments
Post a Comment