Learn R Programming

NeuralEstimators (version 0.2.0)

spatialgraph: spatialgraph

Description

Constructs a graph object for use in a graph neural network (GNN).

Usage

spatialgraph(S, Z, isotropic = TRUE, stationary = TRUE, ...)

Value

A GNNGraph (JuliaProxy object) or, if multiple data sets are provided, a vector of GNNGraph objects which can be indexed in the usual manner using [[ or converted to an R list using a combination of indexing and lapply.

Arguments

S

Spatial locations, provided as:

  • An \(n\times 2\) matrix when locations are fixed across replicates, where \(n\) is the number of spatial locations.

  • A list of \(n_i \times 2\) matrices when locations vary across replicates.

  • A list of the above elements (i.e., a list of matrices or a list of lists of matrices) when constructing graphs from multiple data sets.

Z

Spatial data, provided as:

  • An \(n\times m\) matrix when locations are fixed, where \(m\) is the number of replicates.

  • A list of \(n_i\)-vectors when locations vary across replicates.

  • A list of the above elements (i.e., a list of matrices or a list of lists of vectors) when constructing graphs from multiple data sets.

isotropic

Logical. If TRUE, edge features store the spatial distance (magnitude) between nodes. If FALSE, the spatial displacement or spatial location is stored, depending on the value of stationary.

stationary

Logical. If TRUE, edge features store the spatial displacement (vector difference) between nodes, capturing both magnitude and direction. If FALSE, edge features include the full spatial locations of both nodes.

...

Additional keyword arguments from the Julia function adjacencymatrix() that define the neighborhood of each node, with the default being a randomly selected set of k=30 neighbors within a radius of r=0.15 spatial distance units.

Examples

Run this code
if (FALSE) {
library("NeuralEstimators")

# Number of replicates
m <- 5

# Spatial locations fixed for all replicates
n <- 100 
S <- matrix(runif(n * 2), n, 2)
Z <- matrix(runif(n * m), n, m)
g <- spatialgraph(S, Z)

# Spatial locations varying between replicates
n <- sample(50:100, m, replace = TRUE)
S <- lapply(n, function(ni) matrix(runif(ni * 2), ni, 2))
Z <- lapply(n, function(ni) runif(ni))
g <- spatialgraph(S, Z)

# Multiple data sets: Spatial locations fixed for all replicates within a given data set
K <- 15 # number of data sets
n <- sample(50:100, K, replace = TRUE) # number of spatial locations can vary between data sets
S <- lapply(1:K, function(k) matrix(runif(n[k] * 2), n[k], 2))
Z <- lapply(1:K, function(k) matrix(runif(n[k] * m), n[k], m))
g <- spatialgraph(S, Z)

# Multiple data sets: Spatial locations varying between replicates within a given data set
S <- lapply(1:K, function(k) {
  lapply(1:m, function(i) {
  ni <- sample(50:100, 1)       # randomly generate the number of locations for each replicate
  matrix(runif(ni * 2), ni, 2)  # generate the spatial locations
  })
})
Z <- lapply(1:K, function(k) {
  lapply(1:m, function(i) {
    n <- nrow(S[[k]][[i]])
    runif(n)  
  })
})
g <- spatialgraph(S, Z)
}

Run the code above in your browser using DataLab