spatbiome: User Guide

spatbiome

A collection of functions for spatial analysis of microbiome data. Currently contains ARShift, a random shift test for bivariate point patterns.

Installation

You can install the development version of spatbiome from GitHub with:


# Install a package from GitHub
pak::pak("asmita112358/spatbiome")

Data generating functions and simulation scripts

The package includes the data-generating functions in scripts/datagen.R. To locate this script and the results_in_paper directory in your installed copy of spatbiome, run

library(spatbiome)
system.file("results_in_paper", package = "spatbiome")
#> [1] "/tmp/RtmpkGRjJe/Rinstac12aaf7f00/spatbiome/results_in_paper"
system.file("scripts", package = "spatbiome")
#> [1] "/tmp/RtmpkGRjJe/Rinstac12aaf7f00/spatbiome/scripts"

These commands return the full paths to the files and directory. Open datagen.R in RStudio with:

file.edit(system.file("scripts", "datagen.R", package = "spatbiome"))

An example data generating function is provided below. The function generate_LGCP generates two Log Gaussian Cox Processes (LGCPs) in a specified window. The user can choose to generate independent point patterns or dependent point patterns where points of type 2 are placed close to points of type 1.

generate_LGCP <- function(win, corrfun = "exponential",mu = 5, scale = 0.1, dependant = FALSE){

  data1 <- spatstat.random::rLGCP(corrfun, mu = mu, win = win, scale = scale, var = 1)

  if(dependant == FALSE){
    data2 <- spatstat.random::rLGCP(corrfun, mu = mu, win = win, scale = scale, var = 1)
  } else {
    data2 <- data1
    runif_sh <- spatstat.random::runifdisc(data1$n, radius = 0.1)
    data2$x <- data1$x + runif_sh$x
    data2$y <- data1$y + runif_sh$y

    keep <- spatstat.geom::inside.owin(data2$x, data2$y, win)
    data2 <- data2[keep]
  }

  spatstat.geom::marks(data1) <- factor(rep(1, spatstat.geom::npoints(data1)), levels = c(1, 2))
  spatstat.geom::marks(data2) <- factor(rep(2, spatstat.geom::npoints(data2)), levels = c(1, 2))

  data_all <- spatstat.geom::superimpose(data1, data2, W = win)
  spatstat.geom::marks(data_all) <- factor(spatstat.geom::marks(data_all), levels = c(1, 2))

  return(data_all)
}

Example 1. Null hypothesis: independent point patterns

This is a basic example which demonstrates the implementation of ARShift for two independent Log Gaussian point processes. The null hypothesis is that the two point patterns are independent.

library(ggplot2)
X <- generate_LGCP(win = spatstat.geom::owin(c(0, 1), c(0, 1)), corrfun = "exponential", mu = 5, scale = 0.1, dependant = FALSE)
plot_X <- data.frame(X)
ggplot(plot_X, aes(x = x, y = y, color = marks)) + 
  geom_point() +
  labs(title = "Simulated independent LGCP", x = "X coordinate", y = "Y coordinate", color = "Point Type") +
  theme_minimal()

res <- ARShift(data = X, i = "1", j = "2", n_perm = 499, workers = 5, type = "hom")
res
#> $p
#> [1] 0.756
#> 
#> $K
#>  [1] 0.000000e+00 2.126122e-05 1.275673e-04 2.822428e-04 5.469725e-04
#>  [6] 6.745398e-04 9.876381e-04 1.379574e-03 1.728498e-03 2.159561e-03
#> [11] 2.733614e-03 3.131737e-03 3.825786e-03 4.637393e-03 5.307172e-03
#> [16] 5.991771e-03 6.535967e-03 7.689790e-03 8.922210e-03 1.030528e-02
#> [21] 1.150392e-02 1.249959e-02 1.367396e-02 1.528594e-02 1.692230e-02
#> [26] 1.832340e-02 1.976328e-02 2.134757e-02 2.295905e-02 2.421604e-02
#> [31] 2.593917e-02 2.734486e-02 2.903242e-02 3.122975e-02 3.338615e-02
#> [36] 3.581563e-02 3.794876e-02 4.012514e-02 4.207710e-02 4.413386e-02
#> [41] 4.634924e-02 4.911178e-02 5.128899e-02 5.356445e-02 5.607806e-02
#> [46] 5.869794e-02 6.104484e-02 6.361476e-02 6.646241e-02 6.897413e-02
#> 
#> $r
#>  [1] 0.000000000 0.003061224 0.006122449 0.009183673 0.012244898 0.015306122
#>  [7] 0.018367347 0.021428571 0.024489796 0.027551020 0.030612245 0.033673469
#> [13] 0.036734694 0.039795918 0.042857143 0.045918367 0.048979592 0.052040816
#> [19] 0.055102041 0.058163265 0.061224490 0.064285714 0.067346939 0.070408163
#> [25] 0.073469388 0.076530612 0.079591837 0.082653061 0.085714286 0.088775510
#> [31] 0.091836735 0.094897959 0.097959184 0.101020408 0.104081633 0.107142857
#> [37] 0.110204082 0.113265306 0.116326531 0.119387755 0.122448980 0.125510204
#> [43] 0.128571429 0.131632653 0.134693878 0.137755102 0.140816327 0.143877551
#> [49] 0.146938776 0.150000000
#> 
#> $n_attempts
#> [1] 549
#> 
#> $n_valid
#> [1] 549
#> 
#> $n_perm
#> [1] 499

Example 2. Alternative hypothesis: co-clustering point patterns

Here, points of type 2 are placed close to points of type 1 to mimic association between point types.

X <- generate_LGCP(win = spatstat.geom::owin(c(0, 1), c(0, 1)), corrfun = "exponential", mu = 5, scale = 0.1, dependant = TRUE)
plot_X <- data.frame(X)
ggplot(plot_X, aes(x = x, y = y, color = marks)) + 
  geom_point() +
  labs(title = "Simulated dependent LGCP", x = "X coordinate", y = "Y coordinate", color = "Point Type") +
  theme_minimal()

res <- ARShift(data = X, i = "1", j = "2", n_perm = 499, workers = 5, type = "hom")
res
#> $p
#> [1] 0.002
#> 
#> $K
#>  [1] 0.000000e+00 3.505267e-05 2.453687e-04 5.257900e-04 9.624299e-04
#>  [6] 1.400588e-03 1.957816e-03 2.613165e-03 3.525903e-03 4.489067e-03
#> [11] 5.609340e-03 7.128249e-03 8.189221e-03 9.496092e-03 1.097768e-02
#> [16] 1.241830e-02 1.398632e-02 1.573274e-02 1.752925e-02 1.943755e-02
#> [21] 2.153630e-02 2.386963e-02 2.622641e-02 2.859421e-02 3.091520e-02
#> [26] 3.308326e-02 3.516727e-02 3.752748e-02 3.997944e-02 4.313133e-02
#> [31] 4.665083e-02 4.933029e-02 5.206379e-02 5.499827e-02 5.786463e-02
#> [36] 6.100261e-02 6.425236e-02 6.751508e-02 7.089254e-02 7.456757e-02
#> [41] 7.836127e-02 8.179501e-02 8.534114e-02 8.852314e-02 9.149494e-02
#> [46] 9.459877e-02 9.739854e-02 1.002322e-01 1.036666e-01 1.068093e-01
#> 
#> $r
#>  [1] 0.000000000 0.003061224 0.006122449 0.009183673 0.012244898 0.015306122
#>  [7] 0.018367347 0.021428571 0.024489796 0.027551020 0.030612245 0.033673469
#> [13] 0.036734694 0.039795918 0.042857143 0.045918367 0.048979592 0.052040816
#> [19] 0.055102041 0.058163265 0.061224490 0.064285714 0.067346939 0.070408163
#> [25] 0.073469388 0.076530612 0.079591837 0.082653061 0.085714286 0.088775510
#> [31] 0.091836735 0.094897959 0.097959184 0.101020408 0.104081633 0.107142857
#> [37] 0.110204082 0.113265306 0.116326531 0.119387755 0.122448980 0.125510204
#> [43] 0.128571429 0.131632653 0.134693878 0.137755102 0.140816327 0.143877551
#> [49] 0.146938776 0.150000000
#> 
#> $n_attempts
#> [1] 549
#> 
#> $n_valid
#> [1] 549
#> 
#> $n_perm
#> [1] 499