43  grouped_rppp()

The examples in Chapter 43 require

library(groupedHyperframe.random)
# Loading required package: groupedHyperframe

Function groupedHyperframe.random::grouped_rppp() implements the matrix parameterization using advanced R language operations.

Listing 43.1 copies the R code in Chapter 4, for user’s convenience, to create the subject-specific parameters p_Matern (Listing 4.5) and the number of point-patterns per subject n (Listing 4.7).

Listing 43.1: Previously: p_Matern and n(Listing 4.5, Listing 4.7)
Code
set.seed(39); p_Matern = mapply(
  FUN = mvrnorm2, 
  mu = list(kappa = c(3,2), mu = c(10,5), scale = c(.4,.2), meanlog = c(3,5), sdlog = c(.4,.2)), 
  sd = list(kappa = .2, mu = .5, scale = .05, meanlog = .1, sdlog = .01), 
  MoreArgs = list(n = 3L, row.prefix = 'Subj', col.prefix = 'Pattern'), 
  SIMPLIFY = FALSE
) |>
  within.list(expr = {
    kappa = pmax(kappa, 1 + .Machine$double.eps)
    mu = pmax(mu, 1 + .Machine$double.eps)
    scale = pmax(scale, .Machine$double.eps)
    sdlog = pmax(sdlog, .Machine$double.eps)
  })
set.seed(37); (n = sample(x = 1:4, size = 3L, replace = TRUE)) 

Listing 43.2 shows that the code snippet inside function grouped_rppp() (Listing 4.8) cannot be taken outside function grouped_rppp()!

Listing 43.2: Advanced: without language operation
tryCatch(expr = {
  p_Matern |> 
    with.default(expr = {
      spatstat.random::rMatClust(kappa = kappa, scale = scale, mu = mu)
    })
}, error = identity)
# <simpleError: 'scale' should be a single number>

Listing 43.3 shows that the native pipe operator |> successfully passes the code snippet into function grouped_rppp().

Listing 43.3: Advanced: language operation via native pipe |>
p_Matern |> 
  with.default(expr = {
    rMatClust(kappa = kappa, scale = scale, mu = mu) |> 
      grouped_rppp(n = n)
  })
# Grouped Hyperframe: ~g1/g2
# 
# 9 g2 nested in
# 3 g1
# 
#     ppp g1 g2
# 1 (ppp)  1  1
# 2 (ppp)  1  2
# 3 (ppp)  2  1
# 4 (ppp)  2  2
# ✂️ --- output truncated --- ✂️

Listing 43.4 shows that the pipe operator magrittr::`%>%` (Bache and Wickham 2025, v2.0.4) does not pass the code snippet into function grouped_rppp()!

Listing 43.4: Advanced: language operation via magrittr::`%>%`
suppressPackageStartupMessages(library(magrittr))
tryCatch(expr = {
  p_Matern |> 
    with.default(expr = {
      rMatClust(kappa = kappa, scale = scale, mu = mu) %>% 
        grouped_rppp(n = n)
    })
}, error = identity)
# <notSubsettableError in i[[1L]]: object of type 'symbol' is not subsettable>