shade: Shade density intervals

Description Usage Arguments Details Author(s) See Also Examples

Description

Adds a shaded interval to an existing density plot or regression plot.

Usage

1
2
shade(object, lim, label = NULL, col = col.alpha("black",
                 0.15), border = NA, ...)

Arguments

object

A density or formula object that defines the density OR a matrix object that defines the plot region. See details.

lim

For a density, a vector of two values, indicating upper and lower bounds of interval, on the horizontal axis. For a plot region, a list of x-axis values corresponding to y-axis points in the matrix object.

label

Optional label to center in interval.

col

Color to shade the interval. Default is transparent gray.

border

Border of shaded region. Default is no border, NA.

...

Other parameters to pass to polygon, which actually draws the shaded region.

Details

This function uses polygon to draw a shaded region under a density curve or on a regression plot. The function assumes the plot already exists. See the examples below.

There are two plotting interfaces, for densities. First, if the density is plotted from kernal estimation, using perhaps density, then the same density estimate should be passed as the first parameter. See example. Second, if the density is plotted from a series of x and y values, from perhaps a grid approximation, then a formula can be passed to define the curve. See example.

For plotting confidence regions on a regression plot, the matrix object should contain y-axis values defining the border of the region. The first row of the matrix should be the bottom of the region, and the second row should be the top. Each column should correspond to the x-axis values in lim. See example.

Author(s)

Richard McElreath

See Also

density, dens

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
models <- seq( from=0 , to=1 , length.out=100 )
prior <- rep( 1 , 100 )
likelihood <- dbinom( 6 , size=9 , prob=models )
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)

# using formula interface
plot( posterior ~ models , type="l" )
shade( posterior ~ models , c(0,0.5) )

# using density interface
samples <- sample( models , size=1e4 , replace=TRUE , prob=posterior )
plot( density(samples) )
shade( density(samples) , PCI(samples) )

# plotting a shaded confidence interval on a regression plot
data(cars)
m <- lm( dist ~ speed , cars )
p <- extract.samples( m )
x.seq <- seq( from=min(cars$speed)-1 , to=max(cars$speed)+1 , length.out=30 )
mu.ci <- sapply( x.seq , function(x) PI( p[,1] + p[,2]*x ) )
plot( dist ~ speed , cars )
abline( m )
shade( mu.ci , x.seq )

joepowers16/rethinking documentation built on June 2, 2019, 6:52 p.m.