### Setup R environment for inlabru Source: https://inlabru-org.github.io/inlabru/articles/2d_lgcp_covars_groupcv.html Loads necessary libraries and sets INLA computation options for DIC and WAIC. Ensure these libraries are installed before running. ```R library(INLA) library(inlabru) library(fmesher) library(RColorBrewer) library(ggplot2) library(patchwork) # Activate DIC output bru_options_set(control.compute = list(dic = TRUE, waic = TRUE)) ``` -------------------------------- ### Convert SpatialPoints and boundary polygon to spatstat ppp object Source: https://inlabru-org.github.io/inlabru/reference/spatial.to.ppp.html This example demonstrates how to load spatial data, convert it into a spatstat ppp object using the `spatial.to.ppp` function, and then visualize the resulting point pattern. Ensure required packages like 'spatstat.geom', 'sp', and 'sf' are installed and loaded. ```R if (require("spatstat.geom") && bru_safe_sp() && require("sp") && bru_safe_terra(quietly = TRUE) && require("sf", quietly = TRUE)) { # Load Gorilla data gorillas <- gorillas_sp() # Use nest locations and survey boundary to create a spatstat ppp object gp <- spatial.to.ppp(gorillas$nests, gorillas$boundary) class(gp) # Plot it plot(gp) } ``` -------------------------------- ### Install inlabru using pak (universe) Source: https://inlabru-org.github.io/inlabru/index.html Installs inlabru from the inlabru-org R-universe, picking the most recent version. Configures the universe repository. ```R # Enable universe(s) by inlabru-org pak::repo_add(inlabruorg = "https://inlabru-org.r-universe.dev") pak::pkg_install("inlabru") ``` -------------------------------- ### Install inlabru using pak (development) Source: https://inlabru-org.github.io/inlabru/index.html Installs the development version of inlabru from GitHub using the 'pak' package manager. ```R pak::pkg_install("inlabru-org/inlabru") ``` -------------------------------- ### Example: Creating a Linear Component Source: https://inlabru-org.github.io/inlabru/reference/bru_comp_list.html Demonstrates how to use `bru_comp_list` with a formula to create a linear model component and provides equivalent shortcut syntaxes. ```APIDOC ## Examples __``` # As an example, let us create a linear component. Here, the component is # called "myLinearEffectOfX" while the covariate the component acts on is # called "x". Note that a list of components is returned because the # formula may define multiple components eff <- bru_comp_list(~ myLinearEffectOfX(main = x, model = "linear")) summary(eff[[1]]) #> Label: myLinearEffectOfX #> Type: main = linear #> Map: pipe = multi(main = autodetect(x)) #> INLA formula: #> ~ . + f(myLinearEffectOfX, model = #> BRU_myLinearEffectOfX_main_model) # Equivalent shortcuts: eff <- bru_comp_list(~ myLinearEffectOfX(x, model = "linear")) eff <- bru_comp_list(~ myLinearEffectOfX(x)) # Individual component eff <- bru_comp("myLinearEffectOfX", main = x, model = "linear") ``` ``` -------------------------------- ### Load Libraries and Setup Source: https://inlabru-org.github.io/inlabru/articles/2d_lgcp_sp.html Loads necessary R libraries for spatial analysis and inlabru, and forces safe SPDE usage. ```R library(fmesher) library(inlabru) library(INLA) library(mgcv) library(ggplot2) library(patchwork) bru_safe_sp(force = TRUE) ``` -------------------------------- ### Install inlabru using remotes (development) Source: https://inlabru-org.github.io/inlabru/index.html Installs the development version of inlabru from GitHub using the 'remotes' package. ```R remotes::install_github("inlabru-org/inlabru", ref = "devel") ``` -------------------------------- ### Example: Predicting with a spatial random field model Source: https://inlabru-org.github.io/inlabru/reference/bru_comp_eval.html This example demonstrates fitting a Gaussian model with a spatial random field and then using `generate` to predict values for new data. It requires the `sf` and `sn` packages and sets up a 2D mesh and SPDE model. ```R if (bru_safe_inla() && require("sf", quietly = TRUE) && requireNamespace("sn", quietly = TRUE)) { mesh <- fmesher::fm_mesh_2d_inla( cbind(0, 0), offset = 2, max.edge = 2.5 ) spde <- INLA::inla.spde2.pcmatern( mesh, prior.range = c(1, NA), prior.sigma = c(0.2, NA) ) set.seed(12345L) data <- sf::st_as_sf( data.frame( x = runif(50), y = runif(50), z = rnorm(50) ), coords = c("x", "y") ) fit <- bru( z ~ -1 + field(geometry, model = spde), family = "gaussian", data = data, options = list(control.inla = list(int.strategy = "eb")) ) pred <- generate( fit, newdata = data.frame(A = 0.5, B = 0.5), formula = ~ field_eval(cbind(A, B)), n.samples = 1L ) } #> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE ``` -------------------------------- ### Core Model Component Mapper Example Source: https://inlabru-org.github.io/inlabru/articles/bru_mapper.html An example of constructing a core model component mapper using a pipe, multi, and scale mapper. ```APIDOC ## Core Model Component Mapper ### Description All model component mappers are currently defined as a `pipe` mapper containing a `multi` mapper followed by a `scale` mapper. ### Usage Example ```R mapper <- bm_pipe( list( mapper = bm_multi(list(main = ..., group = ..., replicate = ...)), scale = bm_scale() ) ) ibm_eval(mapper, input = list( mapper = list(main = ..., group = ..., replicate = ...), scale = ... ), state = ... ) ``` ``` -------------------------------- ### Load Example Data Source: https://inlabru-org.github.io/inlabru/articles/1d_lgcp.html Loads the 'Poisson2_1D' dataset, which contains point data for a 1D spatial model. ```R data(Poisson2_1D, package = "inlabru") ``` -------------------------------- ### 1D LGCP bin count simulation and comparison Source: https://inlabru-org.github.io/inlabru/reference/bincount.html This example demonstrates fitting an LGCP model to 1D point data and then using the bincount function to compare the observed histogram counts with model simulations. It requires ggplot2, fmesher, and a working INLA installation. ```R if (FALSE) { # \dontrun{ if (require(ggplot2) && require(fmesher) && bru_safe_inla()) { # Load a point pattern data(Poisson2_1D) # Take a look at the point (and frequency) data ggplot(pts2) + geom_histogram( aes(x = x), binwidth = 55 / 20, boundary = 0, fill = NA, color = "black" ) + geom_point(aes(x), y = 0, pch = "|", cex = 4) + coord_fixed(ratio = 1) # Fit an LGCP model x <- seq(0, 55, length.out = 50) mesh1D <- fmesher::fm_mesh_1d(x, boundary = "free") matern <- INLA::inla.spde2.pcmatern( mesh1D, prior.range = c(1, 0.01), prior.sigma = c(1, 0.01), constr = TRUE ) mdl <- x ~ spde1D(x, model = matern) + Intercept(1) fit.spde <- lgcp(mdl, pts2, domain = list(x = mesh1D)) # Calculate bin statistics bc <- bincount( result = fit.spde, observations = pts2, breaks = seq(0, max(pts2), length.out = 12), predictor = x ~ exp(spde1D + Intercept) ) # Plot them! attributes(bc)$ggp } } # } ``` -------------------------------- ### Example of using bm_scale Source: https://inlabru-org.github.io/inlabru/reference/bm_scale.html Demonstrates how to create a scaling mapper and evaluate it. The output shows the offset and Jacobian resulting from the scaling operation. ```R m <- bm_scale() ibm_eval2(m, c(1, 2, 1, 2), 1:4) #> $offset #> [1] 1 4 3 8 #> #> $jacobian #> 4 x 4 diagonal matrix of class "ddiMatrix" #> [,1] [,2] [,3] [,4] #> [1,] 1 . . . #> [2,] . 2 . . #> [3,] . . 1 . #> [4,] . . . 2 # ``` -------------------------------- ### Install inlabru using remotes (universe) Source: https://inlabru-org.github.io/inlabru/index.html Installs inlabru from the inlabru-org R-universe using 'remotes'. Configures repositories for the universe and INLA. ```R # Enable universe(s) by inlabru-org options(repos = c( inlabruorg = "https://inlabru-org.r-universe.dev", INLA = "https://inla.r-inla-download.org/R/testing", CRAN = "https://cloud.r-project.org" )) # Install some packages install.packages("inlabru") ``` -------------------------------- ### Getting Bru Options Source: https://inlabru-org.github.io/inlabru/reference/bru_options.html Retrieves the current value of a specific bru option. This example fetches the 'bru_verbose' option. ```R bru_options_get("bru_verbose") #> [1] 0 ``` -------------------------------- ### Install INLA and inlabru in R Source: https://inlabru-org.github.io/inlabru/articles/Apptainer.html Within the R environment inside the Apptainer container, set the repository options and install the INLA and inlabru packages. You may be prompted to create a personal library path. ```r # Now, in a R environment options(repos = c( INLA = "https://inla.r-inla-download.org/R/testing", CRAN = "https://cloud.r-project.org" )) install.packages("INLA") # One may be asked to create a personal library path. install.packages("inlabru") # quit R q() ``` -------------------------------- ### Install inlabru from CRAN Source: https://inlabru-org.github.io/inlabru/index.html Installs the current CRAN version of the inlabru package. Ensure the INLA repository is configured. ```R options(repos = c( INLA = "https://inla.r-inla-download.org/R/testing", getOption("repos") )) install.packages("inlabru") ``` -------------------------------- ### Install inlabru using pak (stable) Source: https://inlabru-org.github.io/inlabru/index.html Installs the latest bugfix release of inlabru from GitHub using the 'pak' package manager. Adds the INLA repository. ```R # install.packages("pak") pak::repo_add(INLA = "https://inla.r-inla-download.org/R/testing") pak::pkg_install("inlabru-org/inlabru@stable") ``` -------------------------------- ### Examples of bru_used_vars with different inputs Source: https://inlabru-org.github.io/inlabru/reference/bru_used_vars.html Demonstrates the usage of bru_used_vars with various inputs, including formulas and expressions, with and without including function names. ```R bru_used_vars(~.) #> NULL ``` ```R bru_used_vars(~ a + b + c_latent + d_eval()) #> [1] "a" "b" "c_latent" ``` ```R bru_used_vars(expression(a + b + c_latent + d_eval())) #> [1] "a" "b" "c_latent" ``` ```R bru_used_vars(~., functions = TRUE) #> NULL ``` ```R bru_used_vars(~ a + b + c_latent + d_eval(), functions = TRUE) #> [1] "+" "a" "b" "c_latent" "d_eval" ``` ```R bru_used_vars(expression(a + b + c_latent + d_eval()), functions = TRUE) #> [1] "expression" "+" "a" "b" "c_latent" #> [6] "d_eval" ``` ```R bru_used_vars(a ~ b) #> [1] "b" ``` ```R bru_used_vars(expression(a ~ b)) #> [1] "a" "b" ``` -------------------------------- ### Example: Visualizing point counts in plots Source: https://inlabru-org.github.io/inlabru/reference/point2count.html This example demonstrates how to use `point2count` to aggregate point data into counts per plot and visualize the results using `ggplot2`. It requires several packages including `sp`, `raster`, `ggplot2`, `sf`, and `patchwork`. ```R # \donttest{ # Some features require the raster package if (bru_safe_sp() && require("sp") && require("raster", quietly = TRUE) && require("ggplot2", quietly = TRUE) && bru_safe_terra(quietly = TRUE) && require("sf", quietly = TRUE) && require("patchwork", quietly = TRUE)) { gorillas <- gorillas_sp() plotpts <- plotsample(gorillas$nests, gorillas$boundary, x.ppn = 0.4, y.ppn = 0.4, nx = 5, ny = 5 ) p1 <- ggplot() + gg(plotpts$plots) + gg(plotpts$dets) + gg(gorillas$boundary) countdata <- point2count(plotpts$plots, plotpts$dets) x <- sp::coordinates(countdata)[, 1] y <- sp::coordinates(countdata)[, 2] count <- countdata@data$n p2 <- ggplot() + gg(gorillas$boundary) + gg(plotpts$plots) + geom_text(aes(label = count, x = x, y = y)) (p1 | p2) } # } ``` -------------------------------- ### Example of using bm_index and ibm_jacobian Source: https://inlabru-org.github.io/inlabru/reference/bru_mapper.html Demonstrates creating an index mapper with `bm_index` and then calculating the Jacobian matrix using `ibm_jacobian`. This is useful for understanding how index mappers transform inputs. ```R mapper <- bm_index(5) ibm_jacobian(mapper, input = c(1, 3, 4, 5, 2)) ``` -------------------------------- ### Create a New Optimization Log Source: https://inlabru-org.github.io/inlabru/reference/index.html Initialize a new optimization log object. This is typically done at the start of a fitting process. ```R bru_log_new() ``` -------------------------------- ### Pipe Mapper Example Source: https://inlabru-org.github.io/inlabru/articles/bru_mapper.html Use `bm_pipe` to chain mappers sequentially, passing the output of one as the input to the next. Useful for creating complex processing pipelines. ```R mapper <- bm_pipe(list(name1 = ..., name2 = ..., ...)) ibm_eval(mapper, input = list(name1 = ..., name2 = ..., ...), state = ... ) ``` -------------------------------- ### Plotting a RasterLayer with gg.RasterLayer Source: https://inlabru-org.github.io/inlabru/reference/gg.RasterLayer.html Example demonstrating how to plot a RasterLayer object using the gg.RasterLayer function. Requires 'spatstat.data', 'raster', and 'ggplot2' packages. ```R if (FALSE) { # \dontrun{ # Some features require the raster and spatstat.data packages. if (require("spatstat.data", quietly = TRUE) && require("raster", quietly = TRUE) && require("ggplot2", quietly = TRUE)) { # Load Gorilla data data("gorillas", package = "spatstat.data", envir = environment()) # Convert elevation covariate to RasterLayer elev <- as(gorillas.extra$elevation, "RasterLayer") # Plot the elevation ggplot() + gg(elev) } } # } ``` -------------------------------- ### Load Libraries and Set Options Source: https://inlabru-org.github.io/inlabru/articles/2d_lgcp_covars.html Loads necessary R libraries for spatial modeling and sets computation options for INLA. Ensure these libraries are installed before running. ```R library(INLA) library(inlabru) library(fmesher) library(RColorBrewer) library(ggplot2) library(patchwork) bru_options_set(control.compute = list(dic = TRUE)) ``` -------------------------------- ### Create bru_options object Source: https://inlabru-org.github.io/inlabru/reference/bru_call_options.html Call `bru_call_options()` without arguments to get a `bru_options` object with default and global settings. The output shows the structure and default values of these options. ```R # \donttest{ opts <- bru_call_options() # Print them: opts #> $bru_verbose #> [1] 0 #> #> $bru_verbose_store #> [1] Inf #> #> $bru_max_iter #> [1] 10 #> #> $bru_run #> [1] TRUE #> #> $bru_int_args #> $bru_int_args$method #> [1] "stable" #> #> $bru_int_args$nsub1 #> [1] 30 #> #> $bru_int_args$nsub2 #> [1] 9 #> #> #> $bru_method #> $bru_method$taylor #> [1] "pandemic" #> #> $bru_method$search #> [1] "all" #> #> $bru_method$factor #> [1] 1.618034 #> #> $bru_method$rel_tol #> [1] 0.1 #> #> $bru_method$max_step #> [1] 2 #> #> $bru_method$line_opt_method #> [1] "onestep" #> #> #> $bru_compress_cp #> [1] FALSE #> #> $bru_debug #> [1] FALSE #> #> $bru_compat_pre_2_14_enable #> [1] TRUE #> #> $E #> [1] 1 #> #> $Ntrials #> [1] 1 #> #> $control.compute #> $control.compute$config #> [1] TRUE #> #> $control.compute$control.gcpo #> list() #> #> #> $control.inla #> $control.inla$int.strategy #> [1] "auto" #> #> #> $control.fixed #> $control.fixed$expand.factor.strategy #> [1] "inla" #> #> #> attr(,"class") #> [1] "bru_options" "list" # } ``` -------------------------------- ### Get global package options Source: https://inlabru-org.github.io/inlabru/reference/bru_options.html Use bru_options_get() to access global package options. Specify a name to retrieve a single option, or a list of names for multiple options. ```R bru_options_get(name = NULL, include_default = TRUE) ``` -------------------------------- ### Sum Mapper Example Source: https://inlabru-org.github.io/inlabru/articles/bru_mapper.html Use `bm_sum` to add the effects of multiple mappers. If `single_input` is `TRUE`, the same input is used for all sub-mappers; otherwise, the input must be structured to match the sub-mappers. ```R mapper <- bm_sum(mappers = ..., single_input = ...) ibm_eval(mapper, input = ..., state = ... ) ``` -------------------------------- ### Setting up Observation Models with bru_obs Source: https://inlabru-org.github.io/inlabru/reference/bru_obs.html Demonstrates how to set up observation models for single and multi-likelihood 'bru' models using the 'bru_obs' function. This includes defining different likelihoods (gaussian, poisson) and their corresponding formulas and data. ```R # # if (bru_safe_inla() && # require(ggplot2, quietly = TRUE) && # require(patchwork, quietly = TRUE)) { # # The 'bru_obs()' (previously 'like()') function's main purpose is to set # # up observation models, both for single- and multi-likelihood models. # # The following example generates some random covariates which are observed # # through two different random effect models with different likelihoods # # # Generate the data # # set.seed(123) # # n1 <- 200 # n2 <- 10 # # x1 <- runif(n1) # x2 <- runif(n2) # z2 <- runif(n2) # # y1 <- rnorm(n1, mean = 2 * x1 + 3) # y2 <- rpois(n2, lambda = exp(2 * x2 + z2 + 3)) # # df1 <- data.frame(y = y1, x = x1) # df2 <- data.frame(y = y2, x = x2, z = z2) # # # Single likelihood models and inference using bru are done via # # cmp1 <- y ~ -1 + Intercept(1) + x # fit1 <- bru(cmp1, family = "gaussian", data = df1) # summary(fit1) # # cmp2 <- y ~ -1 + Intercept(1) + x + z # fit2 <- bru(cmp2, family = "poisson", data = df2) # summary(fit2) # # # A joint model has two likelihoods, which are set up using the bru_obs # # function # # lik1 <- bru_obs( # "gaussian", # formula = y ~ x + Intercept, # data = df1, # tag = "norm" # ) # lik2 <- bru_obs( # "poisson", # formula = y ~ x + z + Intercept, # data = df2, # tag = "pois" # ) # # # The union of effects of both models gives the components needed to run # # bru # # jcmp <- ~ x + z + Intercept(1) # jfit <- bru(jcmp, lik1, lik2) # # bru_index(jfit, "norm") # bru_index(jfit, "pois") # # # Compare the estimates # # p1 <- ggplot() + # gg(fit1$summary.fixed, bar = TRUE) + # ylim(0, 4) + # ggtitle("Model 1") # p2 <- ggplot() + # gg(fit2$summary.fixed, bar = TRUE) + # ylim(0, 4) + # ggtitle("Model 2") # pj <- ggplot() + # gg(jfit$summary.fixed, bar = TRUE) + # ylim(0, 4) + # ggtitle("Joint model") # # (p1 / p2 / pj) # } # # ``` -------------------------------- ### bru_safe_terra Source: https://inlabru-org.github.io/inlabru/reference/bru_safe_terra.html Loads the terra package and checks if spatial data extraction works correctly. It identifies potential problems arising from incorrect GDAL/PROJ installation, which might prevent the 'proj.db' database from being found. This is useful for package tests and examples that rely on terra. ```APIDOC ## bru_safe_terra() ### Description Loads the terra package and checks if spatial data extraction works correctly. It identifies potential problems arising from incorrect GDAL/PROJ installation, which might prevent the 'proj.db' database from being found. This is useful for package tests and examples that rely on terra. ### Usage ``` bru_safe_terra(quietly = FALSE, minimum_version = "1.7-66") ``` ### Arguments * **quietly** (logical) - If `TRUE`, prints diagnostic messages. Default `FALSE`. * **minimum_version** (character) - The minimum required version. Default 1.7-66 (should always match the requirement in the package DESCRIPTION). ### Value Returns (invisibly) `FALSE` if a potential issue is detected, and gives a message if `quietly` is `FALSE`. Otherwise returns `TRUE`. ### Examples ```R if (FALSE) { # \dontrun{ if (bru_safe_terra()) { # Run terra dependent calculations } } # } ``` ``` -------------------------------- ### Mapper system documentation links Source: https://inlabru-org.github.io/inlabru/articles/bru_mapper.html Provides links to in-package documentation for mapper constructors, generics, and extraction methods. Regular users typically only need methods from `?bru_mapper` and predefined mappers. ```R ?bru_mapper # Mapper constructors, with links to predefined mappers ?bru_mapper_generics # Generic and default methods ?bru_get_mapper # Mapper extraction methods ``` -------------------------------- ### Example of setting log verbosity and checking messages Source: https://inlabru-org.github.io/inlabru/reference/bru_log_message.html Demonstrates how to set local logging options for verbosity and storage, add messages at different levels, and then retrieve and print log entries between bookmarks. This is useful for debugging and monitoring. ```R if (interactive()) { code_runner <- function() { bru_options_set_local( # Show messages up to and including level 2 (default 0) bru_verbose = 2, # Store messages to an including level 3 (default Inf, storing all) bru_verbose_store = 3 ) bru_log_bookmark("bookmark 1") bru_log_message("Test message 1", verbosity = 1) bru_log_message("Test message 2", verbosity = 2) bru_log_bookmark("bookmark 2") bru_log_message("Test message 3", verbosity = 3) bru_log_message("Test message 4", verbosity = 4) invisible() } message("Run code") code_runner() message("Check log from bookmark 1") print(bru_log()["bookmark 1"]) message("Check log from bookmark 2") print(bru_log()["bookmark 2"]) } ``` -------------------------------- ### Install inlabru using remotes (stable) Source: https://inlabru-org.github.io/inlabru/index.html Installs the latest bugfix release of inlabru from GitHub using the 'remotes' package. ```R # install.packages("remotes") remotes::install_github("inlabru-org/inlabru", ref = "stable") ``` -------------------------------- ### Create and summarize a bm_pipe mapper Source: https://inlabru-org.github.io/inlabru/reference/bm_summary.html Demonstrates creating a mapper using `bm_pipe` and then summarizing it with increased depth to show nested components. ```R mapper <- bm_pipe( list( bm_multi(list( A = bm_index(2), B = bm_index(3) )), bm_index(2) ) ) ) summary(mapper, depth = 2) ``` -------------------------------- ### Check Apptainer Version Source: https://inlabru-org.github.io/inlabru/articles/Apptainer.html Verify if Apptainer is installed on your HPC system by checking its version. If not present, request installation from the administrator. ```bash apptainer version ``` -------------------------------- ### Create a bru_mapper placeholder and check for input Source: https://inlabru-org.github.io/inlabru/reference/ibm_input.html Demonstrates creating a placeholder mapper using bm_autodetect() and then checking if input is available using ibm_input_available(). ```R (m <- bm_autodetect()) #> autodetect ibm_input_available(m) #> [1] FALSE ``` -------------------------------- ### Create and inspect a bm_list Source: https://inlabru-org.github.io/inlabru/reference/bm_list.html Demonstrates how to create a `bm_list` by combining `bru_mapper` objects and how to inspect its structure using `str()`. ```R m <- c(A = bm_const(), B = bm_scale()) str(m) #> List of 2 #> $ A: list() #> ..- attr(*, "class")= chr [1:2] "bm_const" "bru_mapper" #> $ B: list() #> ..- attr(*, "class")= chr [1:2] "bm_scale" "bru_mapper" #> - attr(*, "class")= chr [1:2] "bm_list" "list" ``` ```R str(m[2]) #> List of 1 #> $ B: list() #> ..- attr(*, "class")= chr [1:2] "bm_scale" "bru_mapper" #> - attr(*, "class")= chr [1:2] "bm_list" "list" ``` -------------------------------- ### Get Indices for a Specific Observation Tag Source: https://inlabru-org.github.io/inlabru/reference/bru_index.html Filter indices by a specific observation tag (e.g., 'A' or 'B') to get indices related to that particular dataset or observation group. ```R bru_index(fit, "A") #> [1] 1 2 3 ``` ```R bru_index(fit, "B") #> [1] 4 5 6 7 ``` -------------------------------- ### Initializing and Updating bru_input Source: https://inlabru-org.github.io/inlabru/reference/bru_input.html Demonstrates the creation of a new `bru_input` object and subsequently updating it with data. This is a common workflow for setting up models. ```R (inp <- new_bru_input(x, "LABEL")) #> LABEL = x bru_input(inp, data.frame(x = 1:3)) #> [1] 1 2 3 ``` -------------------------------- ### ibm_input_get Source: https://inlabru-org.github.io/inlabru/reference/ibm_input.html Get the bru_input associated with a bru_mapper. ```APIDOC ## ibm_input_get ### Description Get the `bru_input` associated with a `bru_mapper`. ### Usage ``` ibm_input_get(mapper) ``` ### Arguments - **mapper**: A `bru_mapper` object. ``` -------------------------------- ### Example of using bm_logitaverage with default n_block Source: https://inlabru-org.github.io/inlabru/reference/bm_logitaverage.html Demonstrates the usage of the bm_logitaverage mapper with default settings, evaluating it using ibm_eval2 with specified inputs and block indices. This shows the resulting offset and jacobian. ```R m <- bm_logitaverage() ibm_eval2(m, list(block = c(1, 2, 1, 2), weights = 1:4), 11:14) #> $offset #> [1] 12.04555 12.85907 #> #> $jacobian #> 2 x 4 sparse Matrix of class "dgCMatrix" #> #> [1,] 0.7112239 . 0.2887761 . #> [2,] . 0.7869824 . 0.2130176 #> ``` -------------------------------- ### Evaluate marginal distribution mapper with PDF Source: https://inlabru-org.github.io/inlabru/reference/bm_marginal.html This example shows evaluating the marginal distribution mapper when the PDF (dfun) is provided. It also demonstrates the output of ibm_eval2, which includes the offset and Jacobian. ```R m <- bm_marginal(qexp, pexp, dexp, rate = 1 / 8) ibm_eval2(m, state = -3:3) #> $offset #> [1] 0.01080648 0.18410327 1.38203023 5.54517744 14.72817316 30.26547467 #> [7] 52.86180977 #> #> $jacobian #> 7 x 7 diagonal matrix of class "ddiMatrix" #> [,1] [,2] [,3] [,4] [,5] [,6] [,7] #> [1,] 0.03550271 . . . . . . #> [2,] . 0.4419829 . . . . . #> [3,] . . 2.3008 . . . . #> [4,] . . . 6.383076 . . . #> [5,] . . . . 12.20108 . . #> [6,] . . . . . 18.98572 . #> [7,] . . . . . . 26.26479 #> ``` -------------------------------- ### Check terra installation and functionality Source: https://inlabru-org.github.io/inlabru/reference/bru_safe_terra.html Use this function to ensure that the `terra` package is properly installed and configured before running spatial computations. It returns `FALSE` and a message if potential issues are detected, otherwise `TRUE`. The `quietly` argument controls diagnostic messages, and `minimum_version` specifies the required `terra` version. ```R if (FALSE) { # \dontrun{ if (bru_safe_terra()) { # Run terra dependent calculations } } # } ``` -------------------------------- ### Forward and inverse transformation example Source: https://inlabru-org.github.io/inlabru/reference/bru_transformation.html Demonstrates transforming standard normal variables to an exponential distribution and back. Ensure the correct quantile and CDF functions, along with distribution parameters, are provided. ```R u <- rnorm(5, 0, 1) y <- bru_forward_transformation(qexp, u, rate = 2) v <- bru_inverse_transformation(pexp, y, rate = 2) rbind(u, y, v) #> [,1] [,2] [,3] [,4] [,5] #> u -1.78911563 0.2846580 1.194728 0.4832875 1.115947 #> y 0.01874611 0.4734355 1.076666 0.5784718 1.011635 #> v -1.78911563 0.2846580 1.194728 0.4832875 1.115947 ``` -------------------------------- ### Query Response Size Source: https://inlabru-org.github.io/inlabru/reference/index.html Get information about the size of the response variable in a model. ```R bru_response_size() ``` -------------------------------- ### Map with precomputed location mapping using inla.spde.make.A Source: https://inlabru-org.github.io/inlabru/articles/mesh_mapping.html Demonstrates using `inla.spde.make.A` with a precomputed `A.loc` and an index for mapping. Useful when the mapping is already determined. ```R index <- c(1, 3, 5, 2, 1, 2) inla.spde.make.A(A.loc = A.loc, index = index) ``` -------------------------------- ### Set observations to missing in INLA mdata object Source: https://inlabru-org.github.io/inlabru/reference/bru_set_missing.html Demonstrates using `bru_set_missing` with an `inla.mdata` object. The first example uses `keep = c(1, 4)` to retain the first and fourth observations. The second example uses the assignment operator `bru_set_missing(obs) <- -(1:2)` to set the first two observations to NA. ```R (obs <- INLA::inla.mdata(y = 1:4, X = matrix(1:8, 4, 2))) #> inla.cols = 2 1 2 #> Y1 X1 X2 #> 1 1 1 5 #> 2 2 2 6 #> 3 3 3 7 #> 4 4 4 8 bru_set_missing(obs, keep = c(1, 4)) #> inla.cols = 2 1 2 #> Y1 X1 X2 #> 1 1 1 5 #> 2 NA NA NA #> 3 NA NA NA #> 4 4 4 8 bru_set_missing(obs) <- -(1:2) obs #> inla.cols = 2 1 2 #> Y1 X1 X2 #> 1 NA NA NA #> 2 NA NA NA #> 3 3 3 7 #> 4 4 4 8 ``` -------------------------------- ### Access robins_subset Data Source: https://inlabru-org.github.io/inlabru/reference/robins_subset.html This snippet shows how to access the robins_subset dataset. Ensure the 'inlabru' package is installed. ```R robins_subset ``` -------------------------------- ### Creating and inspecting bru_input objects Source: https://inlabru-org.github.io/inlabru/reference/bru_input_text.html Shows how to create a new bru_input object and then use `bru_input_text` to inspect its input definition. Also demonstrates using `bru_input_text` with a `bru_comp` object, conditionally if `bru_safe_inla()` is true. ```R (inp <- new_bru_input(x, "LABEL")) #> LABEL = x bru_input_text(inp) #> [1] "x" if (bru_safe_inla()) { bru_input_text(bru_comp("x", cos(y))) #> $core #> $core$main #> [1] "cos(y)" #> #> ``` -------------------------------- ### Get length of bru_log object Source: https://inlabru-org.github.io/inlabru/reference/bru_log.html Returns the number of log entries contained within a `bru_log` object. ```R length(x) ``` -------------------------------- ### bru_comp_list Constructor Methods Source: https://inlabru-org.github.io/inlabru/reference/bru_comp_list.html Provides an overview of the different methods available for constructing `bru_comp_list` objects, including methods for formulas, lists, and existing `bru_comp` or `bru_comp_list` objects. ```APIDOC ## Usage __``` bru_comp_list(object, ..., .envir = parent.frame()) # S3 method for class 'formula' bru_comp_list(object, ..., lhoods = NULL, .envir = parent.frame()) # S3 method for class 'list' bru_comp_list( object, ..., lhoods = NULL, .envir = parent.frame(), inputs = NULL ) # S3 method for class 'bru_comp' bru_comp_list(object, ..., .envir = parent.frame()) # S3 method for class 'bru_comp_list' bru_comp_list(object, ..., .envir = parent.frame()) # S3 method for class 'bru_comp_list' c(...) # S3 method for class 'bru_comp' c(...) # S3 method for class 'bru_comp_list' x[i] ``` ``` -------------------------------- ### Get default options object Source: https://inlabru-org.github.io/inlabru/reference/bru_options.html Use bru_options_default() to retrieve an object containing the default package options. ```R bru_options_default() ``` -------------------------------- ### Create a 2->2 reparameterisation mapper Source: https://inlabru-org.github.io/inlabru/reference/bm_reparam.html This example demonstrates how to create a 2-to-2 dimensional reparameterisation mapper using `bm_reparam`. The input `B` matrix defines the transformation from the original state space to the new one. ```R (m <- bm_reparam(bm_index(2), B = matrix(c(1, 1, 0, 1), 2, 2))) ``` -------------------------------- ### Managing Bru Options Source: https://inlabru-org.github.io/inlabru/reference/bru_options.html Demonstrates combining and creating bru_options objects. Use bru_options to merge options and as.bru_options to create new option objects from lists or named arguments. ```R if (FALSE) { # \dontrun{ if (interactive()) { # Combine global and user options: options1 <- bru_options(bru_options_get(), bru_verbose = TRUE) # Create a proto-options object in two equivalent ways: options2 <- as.bru_options(bru_verbose = TRUE) options2 <- as.bru_options(list(bru_verbose = TRUE)) # Combine options objects: options3 <- bru_options(options1, options2) } } ``` -------------------------------- ### Load Libraries for inlabru Source: https://inlabru-org.github.io/inlabru/articles/2d_lgcp_distancesampling.html Loads necessary libraries for inlabru, fmesher, INLA, and ggplot2. Ensure these packages are installed before running. ```R library(inlabru) library(fmesher) library(INLA) library(ggplot2) ``` -------------------------------- ### Construct Harmonic Mapper with Scaling Source: https://inlabru-org.github.io/inlabru/reference/bm_harmonics.html Demonstrates how to construct a harmonic mapper with custom scaling factors for each frequency. The scaling affects the variance of each frequency contribution. ```R scaling = 1 / (1 + (0:4)^2) x <- seq(0, 1, length.out = 11) bmh1 = bm_harmonics(order = 4, interval = c(0, 1)) u1 <- ibm_eval( bmh1, input = x, state = rnorm(9, sd = rep(scaling, c(1, 2, 2, 2, 2))) ) ``` ```R bmh2 = bm_harmonics(order = 4, scaling = scaling) u2 = ibm_eval(bmh2, input = x, state = rnorm(9)) ``` -------------------------------- ### Shift Mapper Example Source: https://inlabru-org.github.io/inlabru/articles/bru_mapper.html The shift mapper adds the input vector to the state vector. It can be used as a stage in a pipe mapper. ```R mapper <- bm_shift() ibm_eval(mapper, input = ..., state = ... ) ``` -------------------------------- ### Extract Standardized Names Source: https://inlabru-org.github.io/inlabru/reference/index.html Get standardized names from a fitted bru or inla result object. Useful for consistent referencing. ```R bru_names() ``` -------------------------------- ### Create a log-sum-exp mapper Source: https://inlabru-org.github.io/inlabru/reference/bm_logsumexp.html Initializes a mapper for log-sum-exp aggregation. Use `rescale = TRUE` to normalize by weight sums for integration averages. ```R bm_logsumexp(rescale = FALSE, n_block = NULL) ``` -------------------------------- ### Summary for bru_mapper object Source: https://inlabru-org.github.io/inlabru/reference/bm_summary.html Generates a summary for a 'bru_mapper' object. Use this to get a concise overview of the mapper's structure. ```R summary(object, ..., prefix = "", initial = prefix, depth = 1) ``` -------------------------------- ### Clear Optimization Log Source: https://inlabru-org.github.io/inlabru/reference/index.html Reset and clear the contents of the optimization log. Use this to start fresh or remove old messages. ```R bru_log_reset() ``` -------------------------------- ### Creating and Manipulating bm_list Objects Source: https://inlabru-org.github.io/inlabru/reference/bm_list.html Demonstrates how to create a bm_list from various inputs and how to subset it. ```APIDOC ## as_bm_list ### Description Converts various list-like objects into a `bm_list`. ### Usage ``` as_bm_list(x) # S3 method for class 'list' as_bm_list(x) # S3 method for class 'bm_list' as_bm_list(x) # S3 method for class 'bru_comp_list' as_bm_list(x) ``` ### Arguments * `x`: An object that can be converted to a `bm_list`. ### Value A `bm_list` object. ## c ### Description Combines `bm_list` or `bru_mapper` objects. ### Usage ``` c(...) # S3 method for class 'bm_list' c(...) # S3 method for class 'bru_mapper' c(...) ``` ### Arguments * `...`: Objects to be combined. For `c.bm_list`, these should be `bm_list` objects. For `c.bru_mapper`, these should be `bru_mapper` objects. ### Value A `bm_list` object. ## [ ### Description Extracts elements from a `bm_list` object. ### Usage ``` x[i] ``` ### Arguments * `x`: A `bm_list` object from which to extract element(s). * `i`: Indices specifying elements to extract. ### Value A `bm_list` object containing the extracted elements. ## Examples ```R m <- c(A = bm_const(), B = bm_scale()) str(m) str(m[2]) ``` ``` -------------------------------- ### gg(SpatialLines) Source: https://inlabru-org.github.io/inlabru/reference/gg.Spatial.html Geom for SpatialLines objects. Extracts start and end points of the lines and calls geom_segment to plot lines between them. ```APIDOC ## gg(SpatialLines) ### Description Geom for `SpatialLines` objects. Extracts start and end points of the lines and calls `geom_segment` to plot lines between them. ### Usage ```R gg(data, mapping = NULL, crs = NULL, ...) ``` ### Arguments * `data` (Spatial*): A `Spatial*` object. * `mapping` (aesthetics): Aesthetic mappings created by `ggplot2::aes` or `ggplot2::aes_` used to update the default mapping. The default mapping is ```R ggplot2::aes( x = .data[[sp::coordnames(data)[1]]], y = .data[[sp::coordnames(data)[2]]], xend = .data[[paste0("end.", sp::coordnames(data)[1])]], yend = .data[[paste0("end.", sp::coordnames(data)[2])]]) ``` * `crs` (sp::CRS): A `sp::CRS` object defining the coordinate system to project the data to before plotting. * `...`: Arguments passed on to `geom_segment`. ### Value A `ggplot2` geom for plotting `SpatialLines`. ``` -------------------------------- ### Create an aggregation mapper Source: https://inlabru-org.github.io/inlabru/reference/bm_aggregate.html Initializes a basic aggregation mapper. Use `ibm_eval2` to evaluate the mapper with sample data, demonstrating how blocks and weights are processed to produce offset and Jacobian outputs. ```R m <- bm_aggregate() ibm_eval2(m, list(block = c(1, 2, 1, 2), weights = 1:4), 11:14) #> $offset #> [1] 50 80 #> #> $jacobian #> 2 x 4 sparse Matrix of class "dgCMatrix" #> #> [1,] 1 . 3 . #> [2,] . 2 . 4 #> ``` ```R ibm_eval2(m, list(block = c(1, 2, 1, 2), weights = 1:4, n_block = 3), 11:14) #> $offset #> [1] 50 80 0 #> #> $jacobian #> 3 x 4 sparse Matrix of class "dgCMatrix" #> #> [1,] 1 . 3 . #> [2,] . 2 . 4 #> [3,] . . . . #> ``` -------------------------------- ### Create and summarize a bm_reparam mapper Source: https://inlabru-org.github.io/inlabru/reference/bm_summary.html Illustrates creating a reparameterized mapper using `bm_reparam` and then summarizing it. `depth = 0` can be used to show only the top-level structure. ```R mapper <- bm_reparam( bm_multi( list( A = bm_index(2), B = bm_index(3) ) ), matrix(1:36, nrow = 6) ) summary(mapper) summary(mapper, depth = 0) ``` -------------------------------- ### Load function definitions for 2D LGCP residuals Source: https://inlabru-org.github.io/inlabru/articles/2d_lgcp_residuals.html Loads necessary function definitions from an R script. Ensure the 'inlabru' package is installed. ```r source( system.file("misc", "2d_lgcp_residuals_functions.R", package = "inlabru" ) ) ```