read.graph {INLA} | R Documentation

## Read and write a graph-object

### Description

Construct a graph-object from a file or a matrix; write graph-object to file

### Usage

    inla.read.graph(..., size.only = FALSE)

    inla.write.graph(
      graph,
      filename = "graph.dat",
      mode = c("binary", "ascii"),
      ...
    )

    ## S3 method for class 'inla.graph'
    plot(x, y, ...)

    ## S3 method for class 'inla.graph'
    summary(object, ...)

    ## S3 method for class 'inla.graph.summary'
    print(x, ...)

### Arguments

`...` | Additional arguments. In `inla.read.graph`, then it is the graph definition (object, matrix, character, filename), plus extra arguments. In `inla.write.graph` it is extra arguments to `inla.read.graph`.
`size.only` | Only read the size of the graph
`graph` | An `inla.graph`-object, a (sparse) symmetric matrix, a filename containing the graph, a list or collection of characters and/or numbers defining the graph, or a neighbours list with class `nb` (see `spdep::card` and `spdep::poly2nb` for for details of `nb` and an example a function returning an `nb` object
`filename` | The filename of the graph.
`mode` | The mode of the file; 'ascii' for ascii-file or 'binary' for a binary-file (default).
`x` | An `inla.graph` -object
`y` | Not used
`object` | An `inla.graph` -object

### Value

The output of `inla.read.graph`, is an `inla.graph` object, with elements

`n` | is the size of the graph
`nnbs` | is a vector with the number of neigbours
`nbs` | is a list-list with the neigbours
`cc` | list with connected component information `id`is a vector with the connected component id for each node (starting from 1) `n`is the number of connected components `nodes`is a list-list of nodes belonging to each connected component `mean`is a factor with one level for each connected component of size larger than one, otherwise `NA`

Methods implemented for `inla.graph` are `summary` and `plot`. The method `plot` require the libraries `Rgraphviz` and `graph` from the Bioconductor-project, see https://www.bioconductor.org.

### Author(s)

Havard Rue hrue@r-inla.org

### See Also

`inla.spy()`

### Examples

    ## a graph from a file
    g.file1 <- tempfile() # E.g. "g.dat"
    cat("3 1 1 2 2 1 1 3 0\n", file = g.file1)
    g = inla.read.graph(g.file1)
    ## writing an inla.graph-object to file
    g.file2 = inla.write.graph(g, mode="binary", filename = tempfile())
    ## re-reading it from that file
    gg = inla.read.graph(g.file2)
    summary(g)
    summary(gg)

    ## Not run:
    plot(g)
    inla.spy(g)
    ## when defining the graph directly in the call,
    ## we can use a mix of character and numbers
    g = inla.read.graph(c(3, 1, "1 2 2 1 1 3", 0))
    inla.spy(c(3, 1, "1 2 2 1 1 3 0"))
    inla.spy(c(3, 1, "1 2 2 1 1 3 0"),  reordering=3:1)
    inla.write.graph(c(3, 1, "1 2 2 1 1 3 0"))

    ## building a graph from adjacency matrix
    adjacent = matrix(0, nrow = 4, ncol = 4)
    adjacent[1,4] = adjacent[4,1] = 1
    adjacent[2,4] = adjacent[4,2] = 1
    adjacent[2,3] = adjacent[3,2] = 1
    adjacent[3,4] = adjacent[4,3] = 1
    g = inla.read.graph(adjacent)
    plot(g)
    summary(g)

    ## End(Not run)

[Package INLA version 26.08.22 Index]