4
$\begingroup$

I came across this nice illustration of the "Coefficient of determination" (source):

enter image description here

Which leads me to ask two questions:

  1. How to do it with R? (I guess my main question would be how to deal with the opacity)
  2. Are there other interesting/useful visualizations of the "Coefficient of determination"? (and again, is there an easy way to make them in R?)

Thanks.

$\endgroup$
3
  • $\begingroup$ Just curious, where did you find that illustration? $\endgroup$
    – user5594
    Feb 25, 2012 at 16:30
  • $\begingroup$ I forgot to link, here is the source: en.wikipedia.org/wiki/File:Coefficient_of_Determination.svg $\endgroup$
    – Tal Galili
    Feb 25, 2012 at 16:52
  • 2
    $\begingroup$ re #2: A different graphical method to explain covariance (and therefore, in an obvious way, the correlation coefficient) appears at stats.stackexchange.com/a/18200. However, I used Mathematica to make the illustration, not R, and even Mathematica could not quite do what I wanted (which is to get complementary overlapping colors to cancel rather than add). $\endgroup$
    – whuber
    Feb 25, 2012 at 18:10

1 Answer 1

7
$\begingroup$

In R, use the alpha argument to hsv():

# Create test data.
# (These parameters produce a bunch of multiple overlaps.)
set.seed(17)
x <- (1:24)*2
y <- 24 + x/8 + 8 * rnorm(length(x))

# Draw the figure.
plot(x, y, pch=19, cex=0.8)   # Plot the points
fit <- lm(y ~ x)              # Find the least squares line
abline(fit)                   # Plot the line
u <- mapply(function(x,y,r) rect(x, y, x-r, y-r, col=hsv(1,alpha=0.1), border=NA), 
     x, y, fit$residuals)     # Plot the squares

Covariance plot

$\endgroup$
2
  • $\begingroup$ ggplot2 also has an alpha parameter that would do the same thing. $\endgroup$ Feb 25, 2012 at 21:20
  • $\begingroup$ @PaulHurleyuk lattice too :) $\endgroup$
    – chl
    Feb 26, 2012 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.