Package 'rc'

Title: Helper for Fitting GMRF Models on River Networks in MGCV
Description: A set of functions to help with the fitting of gmrf models for river networks in mgcv. The package is intended for the fitting 1st and 2nd order smoothers on river networks. The code is written as simply as possible to make is easy to see how GMRFs are constructed.
Authors: Colin Millar [aut, cre]
Maintainer: Colin Millar <[email protected]>
License: MIT + file LICENSE
Version: 0.0.1
Built: 2024-10-30 04:45:36 UTC
Source: https://github.com/faskally/rc

Help Index


Add a new node in between two nodes in an igraph graph

Description

This increases the size of the graph by the number of new nodes

Usage

add.node(g, from, to)

Arguments

g

an igraph graph

from

a vector of node IDs

to

a vector of node IDs

Details

Description - This function does stuff.

Value

an igraph graph


Create an igraph graph from a spatial lines data.frame network

Description

Details

Usage

buildTopo(lines, from = "FNODE_", to = "TNODE_")

Arguments

lines

a spatial lines data.frame containing a river network

from

the column name containing the 'from' node IDs

to

the column name containing the 'to' node IDs

Details

Description - This function does stuff.

Value

what does it return


Differences defining a river network

Description

Returns the incidence matrix of a graph

Usage

Dgraph(g)

Arguments

g

an igraph graph specifying the dependence structure

Details

Description - This function does stuff.

Value

a Matrix with a column for each node and a row for each connection / edge


Compute RW1 precision matrix on a river network

Description

Returns the laplacian matrix of a graph. If weights are supplied then the weighted lacplacian is returned. Sensible weights are based on the flows of the incoming upstream graph edges.

Usage

getQgraph(g, weights = NULL)

Arguments

g

an igraph graph specifying the dependence structure

weights

weights to be applied to the edges of the graph (see details)

Details

Weights argument is passed onto the graph.laplacian function from the igraph package. If weights is NULL (default) and the graph has an edge attribute called weight, then it will be used automatically. Set this to NA if you want the unweighted Laplacian on a graph that has a weight edge attribute.

Value

matrix

Examples

# make a simple river netork
M <- rbind(c( 1, -1,  0,  0,  0,  0,  0,  0),
           c( 0,  1, -1,  0,  0,  0,  0,  0),
           c( 0,  1,  0, -1,  0,  0,  0,  0),
           c( 0,  0,  1,  0, -1,  0,  0,  0),
           c( 0,  0,  1,  0,  0, -1,  0,  0),
           c( 0,  0,  0,  1,  0,  0, -1,  0),
           c( 0,  0,  0,  1,  0,  0,  0, -1))
A <- -1 * t(M) %*% M
diag(A) <- 0
g <- graph.adjacency(A, mode = "undirected")
# plot graph
plot(g)
# add a node in between all other nodes
g <- add.node(g, c(1,2,2,3,3,4,4), c(2,3,4,5,6,7,8))
# get precision matrix for river network
Q <- getQgraph(g)
# simulate river network effect
x <- simQ(Q)
# simulate observations, 1 per region in this case
y <- x + rnorm(length(x))*0.5
# get node ids for observations
# note node ID should not be character
# it can either be numeric, or a factor.
nid <- factor(rownames(Q))
# collect data in a list
dat <- data.frame(y = y, nid = nid)
# provide rank to avoid calculating this at every fit
xtr <- list(penalty = Q, rank = nrow(Q) - 1)
# plot simulation
breaks <- seq(min(y)-0.001, max(y)+0.001, length = 11)
par(mfrow = c(1,2))
plot(g, vertex.color = heat.colors(length(breaks)-1)[as.numeric(cut(x, breaks))])
# fit a model
g1 <- gam(y ~ s(nid, bs = "gmrf", xt = xtr), method="REML", data = dat)
summary(g1)
# plot fitted values
plot(g, vertex.color = heat.colors(length(breaks)-1)[as.numeric(cut(fitted(g1), breaks))])

Compute RW1 precision matrix on a river network

Description

Returns the laplacian matrix of a graph. If weights are supplied then the weighted lacplacian is returned. Sensible weights are based on the flows of the incoming upstream graph edges.

Usage

getWRW1Mat(g, weights = NULL)

Arguments

g

an igraph graph specifying the dependence structure

weights

weights to be applied to the edges of the graph (see details)

Details

Weights argument is passed onto the graph.laplacian function from the igraph package. If weights is NULL (default) and the graph has an edge attribute called weight, then it will be used automatically. Set this to NA if you want the unweighted Laplacian on a graph that has a weight edge attribute.

Value

what does it return


Lightweight add-on for mgcv to allow slighly easier use of GMRF smoothing models

Description

Lightweight add-on for mgcv to allow slighly easier use of GMRF smoothing models