boxplot on an Rle object?
0
0
Entering edit mode
Johannes Rainer ★ 2.0k
@johannes-rainer-6987
Last seen 9 weeks ago
Italy

dear all!

 

I was wondering if there is somewhere a function already implemented to create a boxplot on an Rle object. In the end it shouldn't be that difficult given that there are all required tools (quantile function etc) implemented for Rle objects.

cheers, jo

Rle boxplot • 810 views
ADD COMMENT
0
Entering edit mode

actually, as said, it's pretty simple since all the stuff is already implemented for Rle:

## that's like the boxplot.stats function... just for Rle...
rleboxplot.stats <- function( x, coef=1.5, do.conf=TRUE, do.out=FALSE ){
    quants <- quantile( x, probs=c( 0.25, 0.5, 0.75 ) )
    iqr <- quants[ 3 ] - quants[ 1 ]
    if( coef > 0 ){
        Ranges <- range( x[ x <= ( quants[ 3 ] + coef * iqr ) & x >= ( quants[ 1 ] - coef * iqr ) ] )
    }else{
        Ranges <- range( x )
    }
    n <- length( x )
    conf <- if( do.conf )
                quants[ 2 ] + c( -1.58, 1.58 ) * iqr/sqrt( n )
    Vals <- list( stats=c( Ranges[ 1 ], quants, Ranges[ 2 ] ),
                 n=n, conf=conf, out=numeric() )
    return( Vals )
}

## primitive boxplot function; works with Rle's and also numeric vectors.
rleboxplot <- function( x, range=1.5, ... ){
    ## have to make a matrix of stats...
    Vals <- rleboxplot.stats( x, coef=range )
    stats <- matrix( 0, nrow=5L, ncol=1 )
    conf <- matrix( 0, nrow=2L, ncol=1 )
    stats[ , 1 ] <- Vals$stats
    conf[ , 1 ] <- Vals$conf
    z <- list( stats=stats,
              n=Vals$n,
              conf=conf,
              out=Vals$out,
              group=numeric(0L),
              names=NA )
    bxp( z=z, ... )
}

## define a method boxplot for the Rle
setMethod( "boxplot", "Rle", function( x, range=1.5, ... ){
    rleboxplot( x, range=range, ... )
} )

 

ADD REPLY
0
Entering edit mode

Hi Johannes,

Thanks for the suggestion and for providing the code. I will look at how to best integrate this in the S4Vectors package.

Cheers,

H.

ADD REPLY

Login before adding your answer.

Traffic: 302 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6