conversions {apcluster} | R Documentation

## Conversions Between Dense and Sparse Similarity Matrices

### Description

Converts a dense similarity matrix into a sparse one or vice versa

### Usage

    ## S4 method for signature 'matrix'
    as.SparseSimilarityMatrix(s, lower=-Inf)
    ## S4 method for signature 'Matrix'
    as.SparseSimilarityMatrix(s, lower=-Inf)
    ## S4 method for signature 'sparseMatrix'
    as.SparseSimilarityMatrix(s, lower=-Inf)
    ## S4 method for signature 'matrix'
    as.DenseSimilarityMatrix(s, fill=-Inf)
    ## S4 method for signature 'Matrix'
    as.DenseSimilarityMatrix(s, fill=-Inf)
    ## S4 method for signature 'sparseMatrix'
    as.DenseSimilarityMatrix(s, fill=-Inf)

### Arguments

`s` | a similarity matrix in sparse or dense format (see details below)
`lower` | cut-off threshold to apply when converting similarity matrices into sparse format. All similarities lower than or equal to `lower` will be omitted from the result. The default is `-Inf`), i.e. only `-Inf` values are removed.
`fill` | value to fill in for entries that are missing from sparse similarity matrix 's' (defaults to `-Inf`).

### Details

The function `as.SparseSimilarityMatrix` takes a matrix argument, removes all diagonal elements and all values that are lower than or equal to the cut-off threshold `lower` and returns a sparse matrix of class `dgTMatrix`.

If the function `as.DenseSimilarityMatrix` is called for a sparse matrix (class `sparseMatrix` or any class derived from this class), a dense matrix is returned, where all values that were missing in the sparse matrix are replaced with `fill`.

`as.DenseSimilarityMatrix` can also be called for dense `matrix` and `Matrix` objects. In this case, `as.DenseSimilarityMatrix` assumes that the matrices have three columns that encode for a sparse matrix in the same way as the Matlab implementation of Frey's and Dueck's sparse affinity propagation accepts it: the first column contains 1-based row indices, the second column contains 1-based column indices, and the third column contains the similarity values. The same format is also accepted by `as.SparseSimilarityMatrix` to convert a sparse similarity matrix of this format into a `dgTMatrix` object. Note that, for matrices of this format, `as.DenseSimilarityMatrix` replaces the deprectated function `sparseToFull` (see `apcluster-deprecated`).

Note that `as.SparseSimilarityMatrix` and `as.DenseSimilarityMatrix` are no S4 coercion methods. There are no classes named `SparseSimilarityMatrix` or `DenseSimilarityMatrix`.

### Value

returns a square similarity matrix in sparse format (class `dgTMatrix` or in dense format (standard class `matrix`).

### Author(s)

Ulrich Bodenhofer & Andreas Kothmeier apcluster@bioinf.jku.at

### References

http://www.bioinf.jku.at/software/apcluster

Frey, B. J. and Dueck, D. (2007) Clustering by passing messages between data points. Science 315, 972-976. DOI: 10.1126/science.1136800.

Bodenhofer, U., Kothmeier, A., and Hochreiter, S. (2011) APCluster: an R package for affinity propagation clustering. Bioinformatics 27, 2463-2464. DOI: 10.1093/bioinformatics/btr406.

### Examples

    ## create similarity matrix in sparse format according to Frey and Dueck
    sp <- matrix(c(1, 2, 0.5, 3, 1, 0.2, 5, 4, -0.2, 3, 4, 1.2), 4, 3, byrow=TRUE)
    sp

    ## perform conversions
    as.DenseSimilarityMatrix(sp, fill=0)
    as.SparseSimilarityMatrix(sp)

    ## create dense similarity matrix
    cl1 <- cbind(rnorm(20, 0.2, 0.05), rnorm(20, 0.8, 0.06))
    cl2 <- cbind(rnorm(20, 0.7, 0.08), rnorm(20, 0.3, 0.05))
    x <- rbind(cl1, cl2)

    sim <- negDistMat(x, r=2)
    ssim <- as.SparseSimilarityMatrix(sim, lower=-0.2)

    ## run apcluster() on the sparse similarity matrix
    apres <- apcluster(ssim, q=0)
    apres

[Package apcluster version 1.4.7 Index]