Skip to content

Commit

Permalink
Replace some deprecated functions from INLA.
Browse files Browse the repository at this point in the history
  • Loading branch information
famuvie committed Aug 2, 2024
1 parent 1f9df62 commit 123e49f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Imports:
Suggests:
doParallel,
fields,
fmesher,
GGally,
grid,
INLA,
Expand Down
30 changes: 25 additions & 5 deletions R/metagene-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,21 @@ sim.spatial.metagene <- function(meta, variance = 0.5, range = 0.5, ...) {

# Randomize individuals
spatial.order <- sample(meta$self[-founders.idx], N)
spatial.coord <- head(as.data.frame(INLA::inla.node2lattice.mapping(nr, nc)), N)
spatial.coord <- head(as.data.frame(node2lattice_mapping(nr, nc)), N)
SP <- sp::SpatialPoints(spatial.coord[order(spatial.order), ])

#### Simulate spatial field

# The spatial unit will be the distance between consecutive trees

# Generate INLA mesh
mesh <- INLA::inla.mesh.2d(loc = coordinates(SP),
cutoff = floor(.04*nr),
offset = floor(c(.05, .2)*nr),
max.edge = floor(c(.05, .1)*nr))
mesh <- fmesher::fm_mesh_2d_inla(
loc = coordinates(SP),
cutoff = floor(.04*nr),
offset = floor(c(.05, .2)*nr),
max.edge = floor(c(.05, .1)*nr)
)

# plot(mesh)

# SPDE structure The variance of the spatial effect is a proportion of the
Expand Down Expand Up @@ -475,3 +478,20 @@ setMethod('coordinates<-', signature = 'metagene',
NULL
}
)

#' Define mapping between a lattice and nodes
#'
#' Borrowed from {INLA}
node2lattice_mapping <- function (nrow, ncol)
{
stopifnot(nrow > 0 && ncol > 0)
return(inla.node2lattice(seq.int(nrow * ncol), nrow, ncol))
}
node2lattice <- function (node, nrow, ncol)
{
stopifnot(nrow > 0 && ncol > 0)
icol <- (node - 1) %/% nrow
irow <- (node - 1) %% nrow
return(list(irow = irow + 1, icol = icol + 1))
}

0 comments on commit 123e49f

Please sign in to comment.