tests/testthat/test_cgeneric.R

# test_that("Check cgeneric precision matrices for alpha = 1", {
# set.seed(1)
# testthat::skip_on_cran()
#   if (!requireNamespace("INLA", quietly=TRUE))
#     testthat::skip(message = 'INLA package is not installed. (see www.r-inla.org/download-install)')
  
#   old_threads <- INLA::inla.getOption("num.threads")
#   INLA::inla.setOption(num.threads = "16:1")


# 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 = 20)
# edge4 <- cbind(sin(theta),1+ cos(theta))
# edges = list(edge1, edge2, edge3, edge4)
# graph <- metric_graph$new(edges = edges)

# obs.per.edge <- 250
# obs.loc <- NULL
# for(i in 1:(graph$nE)) {
#   obs.loc <- rbind(obs.loc,
#                    cbind(rep(i,obs.per.edge), runif(obs.per.edge) * 
#                           graph$edge_lengths[i]))
# }

# n.obs <- nrow(obs.loc)

# y <- rep(NA, obs.per.edge * graph$nE)

# df_temp <- data.frame(y = y, edge_number = obs.loc[,1], distance_on_edge = obs.loc[,2])

# graph$add_observations(data = df_temp, normalized = FALSE)

# graph$observation_to_vertex()

# A <- graph$.__enclos_env__$private$A()

# tau <- 0.05

# sigma.e <- 0.1

# nu <- 0.5

# r <- 0.3

# kappa <- sqrt(8 * nu) / r

# theta <- c(sigma, kappa)

# Q <- spde_precision(kappa = kappa, tau = tau, alpha = 1, graph = graph)

# sizeQ <- nrow(Q)

# nsim <- 30

# Z <- rnorm(sizeQ * nsim)
# dim(Z) <- c(sizeQ, nsim)

# sizeA <- nrow(A)

# eps <- rnorm(sizeA * nsim)
# dim(eps) <- c(sizeA, nsim)

# LQ <- chol(Q)
# u <- solve(LQ, Z)

# y <- A%*%u + sigma.e * eps

# graph$.__enclos_env__$private$data$y <- as.vector(y)

# # graph$plot(line_width = 0.3, data=TRUE)

# spde_model <- graph_spde(graph)

# spde_model_check <- graph_spde(graph, start_kappa = kappa,
#                                     start_tau = tau,
#                                     parameterization = "spde")

# class(spde_model_check$f$cgeneric) <- c(class(spde_model_check$f$cgeneric), "inla.cgeneric")

# Q_chk <- INLA::inla.cgeneric.q(spde_model_check)$Q

# expect_equal(sum((Q_chk@i - Q@i)^2), 0)
# expect_equal(sum((Q_chk@p - Q@p)^2), 0)
# expect_equal(sum((Q_chk@x-Q@x)^2), 0)

#   INLA::inla.setOption(num.threads = old_threads)
# })


# test_that("Check cgeneric precision matrices for alpha = 2", {
# set.seed(1)
# testthat::skip_on_cran()
#   if (!requireNamespace("INLA", quietly=TRUE))
#     testthat::skip(message = 'INLA package is not installed. (see www.r-inla.org/download-install)')
  
#   old_threads <- INLA::inla.getOption("num.threads")
#   INLA::inla.setOption(num.threads = "16:1")


# 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 = 20)
# edge4 <- cbind(sin(theta),1+ cos(theta))
# edges = list(edge1, edge2, edge3, edge4)
# graph <- metric_graph$new(edges = edges)

# obs.per.edge <- 20
# obs.loc <- NULL
# for(i in 1:(graph$nE)) {
#   obs.loc <- rbind(obs.loc,
#                    cbind(rep(i,obs.per.edge), runif(obs.per.edge) * 
#                           graph$edge_lengths[i]))
# }

# n.obs <- nrow(obs.loc)

# y <- rep(NA, obs.per.edge * graph$nE)

# df_temp <- data.frame(y = y, edge_number = obs.loc[,1], distance_on_edge = obs.loc[,2])

# graph$add_observations(data = df_temp, normalized = FALSE)

# graph$observation_to_vertex()

# sigma <- 1

# sigma.e <- 0.1

# nu <- 1.5

# r <- 0.3

# kappa <- sqrt(8 * nu) / r

# tau <- sqrt(gamma(nu) / (sigma^2 * kappa^(2 * nu) * (4 * pi)^(1 / 2) * gamma(nu + 1 / 2)))   


# Q <- spde_precision(kappa = kappa, tau = tau, alpha = 2, graph = graph, BC = 0)

# graph$buildC(2)
# n_const <- length(graph$CoB$S)
# ind.const <- c(1:n_const)
# Tc <- graph$CoB$T[-ind.const, ]         

# Q <- Tc%*%Q%*%t(Tc)

# spde_model_check <- graph_spde(graph, alpha = 2, start_kappa = kappa,
#                                     start_sigma = sigma,
#                                     parameterization = "spde", stationary_endpoints="none")

# class(spde_model_check$f$cgeneric) <- c(class(spde_model_check$f$cgeneric), "inla.cgeneric")                                    

# Q_chk <- INLA::inla.cgeneric.q(spde_model_check)$Q

# expect_equal(sum((Q_chk@i - Q@i)^2), 0)
# expect_equal(sum((Q_chk@p - Q@p)^2), 0)
# expect_equal(sum((Q_chk@x-Q@x)^2), 0, tol = 1e-3)


# Q <- spde_precision(kappa = kappa, tau = tau, alpha = 2, graph = graph, BC = 1)

# graph$buildC(2)
# n_const <- length(graph$CoB$S)
# ind.const <- c(1:n_const)
# Tc <- graph$CoB$T[-ind.const, ]         

# Q <- Tc%*%Q%*%t(Tc)

# spde_model_check <- graph_spde(graph, alpha = 2, start_kappa = kappa,
#                                     start_sigma = sigma,
#                                     parameterization = "spde", stationary_endpoints="all")

# class(spde_model_check$f$cgeneric) <- c(class(spde_model_check$f$cgeneric), "inla.cgeneric")

# Q_chk <- INLA::inla.cgeneric.q(spde_model_check)$Q

# expect_equal(sum((Q_chk@i - Q@i)^2), 0)
# expect_equal(sum((Q_chk@p - Q@p)^2), 0)
# expect_equal(sum((Q_chk@x-Q@x)^2), 0, tol = 1e-3)

#   INLA::inla.setOption(num.threads = old_threads)
# })

Try the MetricGraph package in your browser

Any scripts or data that you put into this service are public.

MetricGraph documentation built on Nov. 11, 2026, 1:09 a.m.