46  grouped_rppp()

ImportantDisclaimer

These packages (Note 1) are a one-person project undergoing rapid evolution. Backward compatibility (per Hadley Wickham) is provided as a courtesy rather than a guarantee.

Until further notice, these packages should

  • not be used as a basis for research grant applications,
  • not be cited as an actively maintained tool in a peer-reviewed manuscript,
  • not be used to support or fulfill requirements for pursuing an academic degree.

In addition, work primarily based on these packages (Note 1) should not be presented at academic conferences or similar scholarly venues.

Furthermore, a person’s ability to use these packages (Note 1) does not necessarily imply an understanding of their underlying mechanisms. Accordingly, demonstration of their use alone should not be considered sufficient evidence of expertise, nor should it be credited as a basis for academic promotion or advancement.

These statements do not apply to the contributors (Tip 1) to these packages (Note 1) with respect to their specific contributions.

These statements do not apply when the maintainer of these packages (Note 1), Tingting Zhan, is credited as the first author, the lead author, and/or the corresponding author in a peer-reviewed manuscript, or as the Principal Investigator or Co-Principal Investigator in a research grant application and/or a final research progress report.

These statements are advisory in nature and do not modify or restrict the rights granted under the GNU General Public License https://www.r-project.org/Licenses/.

Note

The examples in Chapter 46 require

library(groupedHyperframe.random)

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

Listing 46.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 46.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 46.2 shows that the code snippet inside function grouped_rppp() (Listing 4.8) cannot be taken outside function grouped_rppp()!

Listing 46.2: Advanced: without language operation
p_Matern |> 
  with.default(expr = {
    spatstat.random::rMatClust(kappa = kappa, scale = scale, mu = mu)
  })
# Error:
# ! 'scale' should be a single number

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

Listing 46.3: Advanced: language operation via native pipe |>
p_Matern |> 
  with.default(expr = {
    rMatClust(kappa = kappa, scale = scale, mu = mu) |> 
      grouped_rppp(n = n)
  })
# Grouped Hyper Data Frame: ~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 46.4 shows that the pipe operator magrittr::`%>%` (Bache and Wickham 2025, v2.0.4, MIT + file LICENSE) does not pass the code snippet into function grouped_rppp()!

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