Quantcast
Viewing latest article 1
Browse Latest Browse All 2

Visually weighted regression in R (à la Solomon Hsiang)

[Update 1: Sep 5, 2012: Explore the Magical Data Enhancer by IRES, using this visualization technique]

[Update 2: Sep 6, 2012: See new improved plots, and new R code!

Solomon Hsiang proposed an appealing method for visually displaying the uncertainty in regressions (see his blog [1][2], and also the discussions on the Statistical Modeling, Causal Inference, and Social Science Blog [1][2]).

I implemented the method in R (using ggplot2), and used an additional method of determining the shading (especially concerning Andrew Gelman’s comment that traditional statistical summaries (such as 95% intervals) give too much weight to the edges. In the following I will show how to produce plots like that:

Image may be NSFW.
Clik here to view.

I used following procedure:

  1. Compute smoothers from 1000 bootstrap samples of the original sample (this results in a spaghetti plot)
  2. Calculate a density estimate for each vertical cut through the bootstrapped smoothers. The area under the density curve always is 1, so the ink is constant for each y-slice.
  3. Shade the figure according to these density estimates.

 

Now let’s construct some plots!

The basic scatter plot:

 

Image may be NSFW.
Clik here to view.

 

 

No we show the bootstrapped smoothers (a “spaghetti plot”). Each spaghetti has a low alpha. That means that overlapping spaghettis produce a darker color and already give weight to highly populated regions.

Image may be NSFW.
Clik here to view.

Here is the shading according to the smoother’s density:

Image may be NSFW.
Clik here to view.

 

Now, we can overplot the median smoother estimate for each x value (the “median smoother”):

Image may be NSFW.
Clik here to view.

Or, a visually weighted smoother:

Image may be NSFW.
Clik here to view.

 

 

Finally, we can add the plain linear regression line (which obviously does not refelct the data points very well):

 

Image may be NSFW.
Clik here to view.

At the end of this post is the function that produces all of these plots. The function returns a ggplot object, so you can modify it afterwards, e.g.:

vwReg(y~x, df, shade=FALSE, spag=TRUE) + xlab("Implicit power motive") + ylab("Corrugator activity during preparation")[/cc]
 

Here are two plots with actual data I am working on:

The correlation of both variables is .22 (p = .003).

A) As a heat map (note: the vertical breaks at the left and right end occur due to single data points that get either sampled or not during the bootstrap):

 

Image may be NSFW.
Clik here to view.

 

B) As a spaghetti plot:

 

Image may be NSFW.
Clik here to view.

 

 

Finally, here's the code (sometimes the code box is collapsed - click the arrow on the top right of the box to open it). Comments and additions are welcome.
[cc lang="rsplus" escaped="true"][Update: I removed the code, as an updated version has been published here (see at the end of the post)][/cc]


Viewing latest article 1
Browse Latest Browse All 2

Trending Articles