Below we set some global options for all code chunks in this document.
# Set seed for reproducibility
set.seed(593)
# Set global options for all code chunks
knitr::opts_chunk$set(
# Disable messages printed by R code chunks
message = FALSE,
# Disable warnings printed by R code chunks
warning = FALSE,
# Show R code within code chunks in output
echo = TRUE,
# Include both R code and its results in output
include = TRUE,
# Evaluate R code chunks
eval = TRUE,
# Enable caching of R code chunks for faster rendering
cache = FALSE,
# Align figures in the center of the output
fig.align = "center",
# Enable retina display for high-resolution figures
retina = 2,
# Show errors in the output instead of stopping rendering
error = TRUE,
# Do not collapse code and output into a single block
collapse = FALSE
)
# Start the figure counter
fig_count <- 0
# Define the captioner function
captioner <- function(caption) {
fig_count <<- fig_count + 1
paste0("Figure ", fig_count, ": ", caption)
}
# Define the function to truncate a number to two decimal places
truncate_to_two <- function(x) {
floor(x * 100) / 100
}
gets_f_in_dist_mesh <- function(graph, f_values){
VtE <- graph$mesh$VtE
E <- graph$E
nE <- graph$nE
nV <- graph$nV
E_ext <- data.frame(edge_number = 1:nE, vertex_start = E[,1], vertex_end = E[,2])
original_vertex <- VtE[1:nV,]
f <- f_values[1:nV]
no_vertices <- cbind(VtE[(nV+1):length(f_values),], f_values[(nV+1):length(f_values)])
same_vertex_list <- list()
for (i in 1:nE) {
same_vertex_list[[i]] <- E_ext %>%
filter(vertex_start == i | vertex_end == i) %>%
mutate(lor = case_when(vertex_start == i ~ 0,vertex_end == i ~ 1)) %>%
dplyr::select(edge_number, lor) %>%
as.matrix()
}
for (i in seq_len(nrow(original_vertex))) {
current_row <- original_vertex[i, ]
# Find which matrix in same_vertex_list contains this row
for (j in seq_along(same_vertex_list)) {
if (any(apply(same_vertex_list[[j]], 1, function(x) all(x == current_row)))) {
# Add a new column with the corresponding value from f
same_vertex_list[[j]] <- cbind(same_vertex_list[[j]], f[i])
break
}
}
}
result_matrix <- do.call(rbind, same_vertex_list)
return(rbind(no_vertices, result_matrix))
}
Below we load the necessary libraries and define the auxiliary functions.
library(rSPDE)
library(MetricGraph)
library(dplyr)
library(plotly)
library(scales)
library(patchwork)
library(tidyr)
library(here)
library(rmarkdown)
# Cite all loaded packages
library(grateful)
Let’s build a small graph to illustrate the effect of \(\tau\) on the graph.
edge1 <- rbind(c(0,0),c(1,0))
edge2 <- rbind(c(0,0),c(0,1))
edge3 <- rbind(c(0,1),c(-1,1))
theta <- seq(from = pi,to = 3*pi/2,length.out = 50)
edge4 <- cbind(sin(theta),1+ cos(theta))
edges <- list(edge1, edge2, edge3, edge4)
h = 0.01
graph <- metric_graph$new(edges = edges)
aux_graph <- graph$clone()
graph$build_mesh(h=h, continuous = TRUE)
edge_number <- graph$mesh$VtE
XY_graph <- graph$mesh$V
aux_graph$build_mesh(h=h, continuous = FALSE)
aux_edge_number <- aux_graph$mesh$VtE
aux_XY_graph <- aux_graph$mesh$V
f <- edge_number[,1]/4 # discontinuous covariate
g <- 0.5*(XY_graph[, 1]^2 - XY_graph[, 2]^2) + 0.5 # continuous covariate
aux_f <- aux_edge_number[,1]/4# discontinuous covariate
aux_g <- 0.5*(aux_XY_graph[, 1]^2 - aux_XY_graph[, 2]^2) + 0.5 # continuous covariate
# Define the matrices B.tau and B.kappa
B.tau = cbind(0, 1, 0, g, 0)
B.kappa = cbind(0, 0, 1, 0, g)
# Log-regression coefficients
theta <- c(0, 0, 1, 1)
# Choose alpha
nu = 2.5
alpha = nu + 1/2
# Compute the operator
op <- rSPDE::spde.matern.operators(graph = graph,
B.tau = B.tau,
B.kappa = B.kappa,
parameterization = "spde",
theta = theta,
alpha = alpha)
# Simulate the non-stationary field
Sigma <- precision(op)
R <- chol(Sigma)
u = solve(R, normal_sample)
model_for_tau <- exp(B.tau[,-1]%*%theta)
model_for_kappa <- exp(B.kappa[,-1]%*%theta)
graph$plot_function(model_for_tau, vertex_size = 0, type = "plotly", line_color = "blue", line_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\tau(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 3: Model for \(\tau\).
graph$plot_function(model_for_kappa, vertex_size = 0, type = "plotly", line_color = "red", line_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\kappa(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 4: Model for \(\kappa\).
graph$plot_function(X = u, vertex_size = 0, type = "plotly", line_color = "darkgreen", line_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 5: Simulated field.
tau <- 1
# Define the matrices B.tau and B.kappa
B.tau <- cbind(0, 1, 0, log(tau)*rep(1, length(f)), 0)
realB.tau <- cbind(0, 1, 0, f, 0)
B.kappa = cbind(0, 0, 1, 0, f)
# Log-regression coefficients
theta <- c(0, 0, 1, 1)
# Compute the operator
op1 <- rSPDE::spde.matern.operators(graph = graph,
B.tau = B.tau,
B.kappa = B.kappa,
parameterization = "spde",
theta = theta,
alpha = alpha)
realmodel_for_tau <- exp(realB.tau[,-1]%*%theta)
normal_sample1 <- normal_sample #rnorm(length(g))
Sigma1 <- precision(op1)
R1 <- chol(Sigma1)
u1cont <- solve(R1, normal_sample1)
u1 <-u1cont*tau/realmodel_for_tau
op2 <- rSPDE::spde.matern.operators(graph = graph,
B.tau = realB.tau,
B.kappa = B.kappa,
parameterization = "spde",
theta = theta,
alpha = alpha)
Sigma2 <- precision(op2)
R2 <- chol(Sigma2)
u2 = solve(R2, normal_sample1)
# To plot the models for tau and kappa
aux_B.tau = cbind(0, 1, 0, aux_f, 0)
aux_B.kappa = cbind(0, 0, 1, 0, aux_f)
model_for_tau <- exp(aux_B.tau[,-1]%*%theta)
model_for_kappa <- exp(aux_B.kappa[,-1]%*%theta)
gg <- gets_f_in_dist_mesh(graph, u1cont)
aux_df <- data.frame(.edge_number = gg[,1], .distance_on_edge = gg[,2], f = gg[,3])
another_df <- cbind(tau/model_for_tau, aux_graph$mesh$VtE)
colnames(another_df) <- c("tau_rel", ".edge_number", ".distance_on_edge")
another_df <- as.data.frame(another_df)
# Step 1: Sort aux_df by the four columns
aux_df_sorted <- aux_df %>%
arrange(.edge_number, .distance_on_edge)
another_df_sorted <- another_df %>%
arrange(.edge_number, .distance_on_edge)
# Merge
merged_df <- cbind(aux_df_sorted, another_df_sorted)[c(1:4)] %>% mutate(final_f = f*tau_rel) %>% dplyr::select(-tau_rel, -f) %>% rename(edge_number = .edge_number, distance_on_edge = .distance_on_edge, f = final_f)
prod <- aux_graph$process_data(data = merged_df, normalized = TRUE)
aux_graph$plot_function(model_for_tau, vertex_size = 0, type = "plotly", line_color = "blue", line_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\tau(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 6: Model for \(\tau\).
aux_graph$plot_function(model_for_kappa, vertex_size = 0, type = "plotly", line_color = "red", line_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\kappa(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 7: Model for \(\kappa\).
aux_graph$plot_function(data = "f", newdata= prod, vertex_size = 0, type = "plotly", line_color = "darkgreen", line_width = 3, continuous = FALSE) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 8: Simulated field.
# Define the matrices B.tau and B.kappa
B.tau <- cbind(0, 1, 0, log(tau)*rep(1, length(f)), 0)
realB.tau <- cbind(0, 1, 0, f, 0)
B.kappa = cbind(0, 0, 1, 0, g)
# Log-regression coefficients
theta <- c(0, 0, 1, 1)
# Compute the operator
op1 <- rSPDE::spde.matern.operators(graph = graph,
B.tau = B.tau,
B.kappa = B.kappa,
parameterization = "spde",
theta = theta,
alpha = alpha)
realmodel_for_tau <- exp(realB.tau[,-1]%*%theta)
normal_sample2 <- normal_sample#rnorm(length(g))
Sigma1 <- precision(op1)
R1 <- chol(Sigma1)
u1cont <- solve(R1, normal_sample2)
u1 <-u1cont*tau/realmodel_for_tau
op2 <- rSPDE::spde.matern.operators(graph = graph,
B.tau = realB.tau,
B.kappa = B.kappa,
parameterization = "spde",
theta = theta,
alpha = alpha)
Sigma2 <- precision(op2)
R2 <- chol(Sigma2)
u2 = solve(R2, normal_sample2)
# To plot the models for tau and kappa
aux_B.tau = cbind(0, 1, 0, aux_f, 0)
model_for_tau <- exp(aux_B.tau[,-1]%*%theta)
model_for_kappa <- exp(B.kappa[,-1]%*%theta)
gg <- gets_f_in_dist_mesh(graph, u1cont)
aux_df <- data.frame(.edge_number = gg[,1], .distance_on_edge = gg[,2], f = gg[,3])
another_df <- cbind(tau/model_for_tau, aux_graph$mesh$VtE)
colnames(another_df) <- c("tau_rel", ".edge_number", ".distance_on_edge")
another_df <- as.data.frame(another_df)
# Step 1: Sort aux_df by the four columns
aux_df_sorted <- aux_df %>%
arrange(.edge_number, .distance_on_edge)
another_df_sorted <- another_df %>%
arrange(.edge_number, .distance_on_edge)
# Merge
merged_df <- cbind(aux_df_sorted, another_df_sorted)[c(1:4)] %>% mutate(final_f = f*tau_rel) %>% dplyr::select(-tau_rel, -f) %>% rename(edge_number = .edge_number, distance_on_edge = .distance_on_edge, f = final_f)
prod <- aux_graph$process_data(data = merged_df, normalized = TRUE)
aux_graph$plot_function(model_for_tau, vertex_size = 1, type = "plotly", line_color = "blue", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\tau(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 9: Model for \(\tau\).
graph$plot_function(model_for_kappa, vertex_size = 1, type = "plotly", line_color = "red", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\kappa(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 10: Model for \(\kappa\).
aux_graph$plot_function(data = "f", newdata= prod, vertex_size = 1, type = "plotly", line_color = "darkgreen", line_width = 3, continuous = FALSE, interpolate_plot = FALSE, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 11: Simulated field.
# Define the matrices B.tau and B.kappa
B.tau = cbind(0, 1, 0, g, 0) #rep(1, length(f))
B.kappa = cbind(0, 0, 1, 0, f)
# Log-regression coefficients
theta <- c(0, 0, 1, 1)
# Compute the operator
op <- rSPDE::spde.matern.operators(graph = graph,
B.tau = B.tau,
B.kappa = B.kappa,
parameterization = "spde",
theta = theta,
alpha = alpha)
# Simulate the non-stationary field
Sigma <- precision(op)
R <- chol(Sigma)
u = solve(R, normal_sample)
aux_B.kappa = cbind(0, 0, 1, 0, aux_f)
model_for_tau <- exp(B.tau[,-1]%*%theta)
model_for_kappa <- exp(aux_B.kappa[,-1]%*%theta)
graph$plot_function(model_for_tau, vertex_size = 1, type = "plotly", line_color = "blue", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\tau(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 12: Model for \(\tau\).
aux_graph$plot_function(model_for_kappa, vertex_size = 1, type = "plotly", line_color = "red", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\kappa(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 13: Model for \(\kappa\).
graph$plot_function(X = u, vertex_size = 1, type = "plotly", line_color = "darkgreen", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1/2, y = 1, z = 1),
camera = list(
eye = list(x = -1*a, y = 0.5*a, z = 0.7*a))))
Figure 14: Simulated field.
We used R version 4.4.1 (R Core Team 2024) and the following R packages: cowplot v. 1.1.3 (Wilke 2024), ggmap v. 4.0.0.900 (Kahle and Wickham 2013), ggpubr v. 0.6.0 (Kassambara 2023), ggtext v. 0.1.2 (Wilke and Wiernik 2022), here v. 1.0.1 (Müller 2020), htmltools v. 0.5.8.1 (Cheng et al. 2024), INLA v. 24.12.11 (Rue, Martino, and Chopin 2009; Lindgren, Rue, and Lindström 2011; Martins et al. 2013; Lindgren and Rue 2015; De Coninck et al. 2016; Rue et al. 2017; Verbosio et al. 2017; Bakka et al. 2018; Kourounis, Fuchs, and Schenk 2018), inlabru v. 2.12.0.9002 (Yuan et al. 2017; Bachl et al. 2019), knitr v. 1.48 (Xie 2014, 2015, 2024), latex2exp v. 0.9.6 (Meschiari 2022), Matrix v. 1.6.5 (Bates, Maechler, and Jagan 2024), MetricGraph v. 1.4.0.9000 (Bolin, Simas, and Wallin 2023b, 2023a, 2023c, 2024; Bolin et al. 2024), OpenStreetMap v. 0.4.0 (Fellows and JMapViewer library by Jan Peter Stotz 2023), osmdata v. 0.2.5 (Mark Padgham et al. 2017), patchwork v. 1.2.0 (Pedersen 2024), plotly v. 4.10.4 (Sievert 2020), plotrix v. 3.8.4 (J 2006), reshape2 v. 1.4.4 (Wickham 2007), rmarkdown v. 2.28 (Xie, Allaire, and Grolemund 2018; Xie, Dervieux, and Riederer 2020; Allaire et al. 2024), rSPDE v. 2.4.0.9000 (Bolin and Kirchner 2020; Bolin and Simas 2023; Bolin, Simas, and Xiong 2024), scales v. 1.3.0 (Wickham, Pedersen, and Seidel 2023), sf v. 1.0.19 (E. Pebesma 2018; E. Pebesma and Bivand 2023), sp v. 2.1.4 (E. J. Pebesma and Bivand 2005; Bivand, Pebesma, and Gomez-Rubio 2013), tidyverse v. 2.0.0 (Wickham et al. 2019), viridis v. 0.6.4 (Garnier et al. 2023), xaringanExtra v. 0.8.0 (Aden-Buie and Warkentin 2024).