graph.matrix {INLA} | R Documentation

## Construct a neighbour-matrix from a `graph`

### Description

Construct a neighbour-matrix from a `graph` and disaply it

### Usage

    inla.matrix2graph(graph, ...)

    inla.graph2matrix(graph, ...)

    inla.spy(graph, ..., reordering = NULL, add = FALSE)

### Arguments

`graph` | An `inla.graph`-object, a (sparse) symmetric matrix, a filename containing the graph, or a list or collection of characters and/or numbers defining the graph.
`...` | Additional arguments to `inla.read.graph()`
`reordering` | A possible reordering. Typical the one obtained from a `inla`-call, `result$misc$reordering`, or the result of `inla.qreordering`.
`add` | Add to existing plot instead of creating a new one

### Value

`inla.graph2matrix` returns a sparse symmetric matrix where the non-zero pattern is defined by the `graph`. The `inla.spy` function, plots the associated sparse matrix defined by the `graph`. The `reordering` argument is typically the reordering found by `inla.qreordering()`.

### Author(s)

Havard Rue hrue@r-inla.org

### See Also

`inla.read.graph()`, `inla.qreordering()`

### Examples

    n = 50
    Q = matrix(0, n, n)
    idx = sample(1:n, 2*n, replace=TRUE)
    Q[idx, idx] = 1
    diag(Q) = 1
    g = inla.read.graph(Q)
    QQ = inla.graph2matrix(g)
    inla.spy(QQ)
    print(all.equal(as.matrix(Q), as.matrix(QQ)))

    g.file = inla.write.graph(g, filename = tempfile())
    inla.dev.new()
    inla.spy(g.file)
    inla.spy(g.file,  reordering = inla.qreordering(g))

    g = inla.read.graph(g.file)
    inla.dev.new()
    inla.spy(g)

    ## Old examples that don't work with the inla.spy call syntax:
    # inla.dev.new()
    # inla.spy(3, 1, "1 2 2 1 1 3 0")
    # inla.dev.new()
    # inla.spy(3, 1, "1 2 2 1 1 3 0", reordering = 3:1)

[Package INLA version 26.08.22 Index]