Skip to contents

Fit response-column-specific random intercepts and/or slopes whose source covariance is induced by a labelled SPDE mesh over response-column locations. The fitted covariance across the coefficient basis is full for | and diagonal for ||.

Usage

spatial_coef(formula, mesh, rho = 1)

Arguments

formula

A coefficient-basis bar expression such as 1 + x | trait, 0 + x | trait, or 1 + x || trait.

mesh

A labelled gllvmTMBmesh built with one location per response column and id_col equal to the response-column factor.

rho

The fixed spatial source strength. The first public version accepts only 1.

Value

A formula marker; never evaluated directly.

Details

This point-model route is covered for Gaussian multivariate data in long or traits(...) wide form, with one labelled make_mesh() object and bare numeric row predictors. The first public version fixes the spatial source strength at rho = 1; IID mixtures, estimated source strength, interval inference, non-Gaussian coefficient models, and simultaneous spatial axes are not available. Existing spatial_slope() remains current and warning-free.

A no-intercept rho = 1 fit uses the released spatial_slope() route exactly. Intercept-bearing fits use the same projected-SPDE likelihood with an intercept column added to the coefficient basis.

Examples

set.seed(12)
locations <- expand.grid(x = 0:3, y = 0:3, KEEP.OUT.ATTRS = FALSE)
locations$trait <- paste0("sp", seq_len(nrow(locations)))
locations <- locations[c("trait", "x", "y")]
column_mesh <- make_mesh(locations, c("x", "y"), cutoff = 0.1,
  id_col = "trait")
dat <- expand.grid(unit = factor(1:24), trait = factor(locations$trait,
  levels = locations$trait))
site_moisture <- seq(-1, 1, length.out = 24)
dat$moisture <- rep(site_moisture, times = nrow(locations))
trait_id <- as.integer(dat$trait)
intercept_deviation <- 0.18 * scale(locations$x + locations$y)[, 1]
slope_deviation <- 0.25 * scale(locations$x - locations$y)[, 1]
dat$value <- 0.4 + intercept_deviation[trait_id] +
  (0.15 + slope_deviation[trait_id]) * dat$moisture +
  rnorm(nrow(dat), sd = 0.15)
fit <- gllvmTMB(value ~ 1 + spatial_coef(1 + moisture | trait,
    mesh = column_mesh), data = dat, trait = "trait", unit = "unit",
  family = gaussian(), control = gllvmTMBcontrol(se = FALSE), silent = TRUE)
extract_Sigma(fit, level = "column_coef")
#> $Sigma
#>              (Intercept)     moisture
#> (Intercept)  0.157301128 -0.007463472
#> moisture    -0.007463472  0.312352551
#> 
#> $R
#>             (Intercept)    moisture
#> (Intercept)  1.00000000 -0.03367073
#> moisture    -0.03367073  1.00000000
#> 
#> $level
#> [1] "column_coef"
#> 
#> $part
#> [1] "dep"
#> 
#> $basis
#> [1] "(Intercept)" "moisture"   
#> 
#> $source
#> $source$type
#> [1] "spatial"
#> 
#> $source$grouping
#> [1] "trait"
#> 
#> $source$labels
#>  [1] "sp1"  "sp2"  "sp3"  "sp4"  "sp5"  "sp6"  "sp7"  "sp8"  "sp9"  "sp10"
#> [11] "sp11" "sp12" "sp13" "sp14" "sp15" "sp16"
#> 
#> $source$coordinates
#>      x y
#> sp1  0 0
#> sp2  1 0
#> sp3  2 0
#> sp4  3 0
#> sp5  0 1
#> sp6  1 1
#> sp7  2 1
#> sp8  3 1
#> sp9  0 2
#> sp10 1 2
#> sp11 2 2
#> sp12 3 2
#> sp13 0 3
#> sp14 1 3
#> sp15 2 3
#> sp16 3 3
#> 
#> $source$coordinate_columns
#> [1] "x" "y"
#> 
#> $source$coordinate_units
#> [1] "as_supplied"
#> 
#> $source$kappa
#> [1] 0.3368819
#> 
#> $source$practical_range
#> [1] 8.395901
#> 
#> $source$normalization
#> [1] "exact_projected_unit_diagonal"
#> 
#> $source$K_column
#>            [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
#>  [1,] 1.0000000 0.9777315 0.9337408 0.8872211 0.9777315 0.9657277 0.9324011
#>  [2,] 0.9777315 1.0000000 0.9717050 0.9290437 0.9668022 0.9810964 0.9634418
#>  [3,] 0.9337408 0.9717050 1.0000000 0.9749300 0.9323986 0.9626353 0.9808794
#>  [4,] 0.8872211 0.9290437 0.9749300 1.0000000 0.8900855 0.9259994 0.9608421
#>  [5,] 0.9777315 0.9668022 0.9323986 0.8900855 1.0000000 0.9810964 0.9450314
#>  [6,] 0.9657277 0.9810964 0.9626353 0.9259994 0.9810964 1.0000000 0.9769307
#>  [7,] 0.9324011 0.9634418 0.9808794 0.9608421 0.9450314 0.9769307 1.0000000
#>  [8,] 0.8965674 0.9331581 0.9670391 0.9749300 0.9084374 0.9445881 0.9808794
#>  [9,] 0.9337408 0.9323986 0.9083188 0.8717641 0.9717050 0.9626353 0.9332535
#> [10,] 0.9324011 0.9450314 0.9332535 0.9021903 0.9634418 0.9769307 0.9614703
#> [11,] 0.9099412 0.9346807 0.9445881 0.9259994 0.9346807 0.9620495 0.9769307
#> [12,] 0.8806666 0.9108522 0.9331581 0.9290437 0.9019792 0.9346807 0.9634418
#> [13,] 0.8872211 0.8900855 0.8717641 0.8392197 0.9290437 0.9259994 0.9021903
#> [14,] 0.8965674 0.9084374 0.8996729 0.8717641 0.9331581 0.9445881 0.9332535
#> [15,] 0.8806666 0.9019792 0.9084374 0.8900855 0.9108522 0.9346807 0.9450314
#> [16,] 0.8546264 0.8806666 0.8965674 0.8872211 0.8806666 0.9099412 0.9324011
#>            [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
#>  [1,] 0.8965674 0.9337408 0.9324011 0.9099412 0.8806666 0.8872211 0.8965674
#>  [2,] 0.9331581 0.9323986 0.9450314 0.9346807 0.9108522 0.8900855 0.9084374
#>  [3,] 0.9670391 0.9083188 0.9332535 0.9445881 0.9331581 0.8717641 0.8996729
#>  [4,] 0.9749300 0.8717641 0.9021903 0.9259994 0.9290437 0.8392197 0.8717641
#>  [5,] 0.9084374 0.9717050 0.9634418 0.9346807 0.9019792 0.9290437 0.9331581
#>  [6,] 0.9445881 0.9626353 0.9769307 0.9620495 0.9346807 0.9259994 0.9445881
#>  [7,] 0.9808794 0.9332535 0.9614703 0.9769307 0.9634418 0.9021903 0.9332535
#>  [8,] 1.0000000 0.8996729 0.9332535 0.9626353 0.9717050 0.8717641 0.9083188
#>  [9,] 0.8996729 1.0000000 0.9808794 0.9445881 0.9084374 0.9749300 0.9670391
#> [10,] 0.9332535 0.9808794 1.0000000 0.9769307 0.9450314 0.9608421 0.9808794
#> [11,] 0.9626353 0.9445881 0.9769307 1.0000000 0.9810964 0.9259994 0.9626353
#> [12,] 0.9717050 0.9084374 0.9450314 0.9810964 1.0000000 0.8900855 0.9323986
#> [13,] 0.8717641 0.9749300 0.9608421 0.9259994 0.8900855 1.0000000 0.9749300
#> [14,] 0.9083188 0.9670391 0.9808794 0.9626353 0.9323986 0.9749300 1.0000000
#> [15,] 0.9323986 0.9331581 0.9634418 0.9810964 0.9668022 0.9290437 0.9717050
#> [16,] 0.9337408 0.8965674 0.9324011 0.9657277 0.9777315 0.8872211 0.9337408
#>           [,15]     [,16]
#>  [1,] 0.8806666 0.8546264
#>  [2,] 0.9019792 0.8806666
#>  [3,] 0.9084374 0.8965674
#>  [4,] 0.8900855 0.8872211
#>  [5,] 0.9108522 0.8806666
#>  [6,] 0.9346807 0.9099412
#>  [7,] 0.9450314 0.9324011
#>  [8,] 0.9323986 0.9337408
#>  [9,] 0.9331581 0.8965674
#> [10,] 0.9634418 0.9324011
#> [11,] 0.9810964 0.9657277
#> [12,] 0.9668022 0.9777315
#> [13,] 0.9290437 0.8872211
#> [14,] 0.9717050 0.9337408
#> [15,] 1.0000000 0.9777315
#> [16,] 0.9777315 1.0000000
#> 
#> 
#> $rho
#> [1] 1
#> 
#> $rho_status
#> [1] "fixed"
#> 
#> $K_rho
#>            sp1       sp2       sp3       sp4       sp5       sp6       sp7
#> sp1  1.0000000 0.9777315 0.9337408 0.8872211 0.9777315 0.9657277 0.9324011
#> sp2  0.9777315 1.0000000 0.9717050 0.9290437 0.9668022 0.9810964 0.9634418
#> sp3  0.9337408 0.9717050 1.0000000 0.9749300 0.9323986 0.9626353 0.9808794
#> sp4  0.8872211 0.9290437 0.9749300 1.0000000 0.8900855 0.9259994 0.9608421
#> sp5  0.9777315 0.9668022 0.9323986 0.8900855 1.0000000 0.9810964 0.9450314
#> sp6  0.9657277 0.9810964 0.9626353 0.9259994 0.9810964 1.0000000 0.9769307
#> sp7  0.9324011 0.9634418 0.9808794 0.9608421 0.9450314 0.9769307 1.0000000
#> sp8  0.8965674 0.9331581 0.9670391 0.9749300 0.9084374 0.9445881 0.9808794
#> sp9  0.9337408 0.9323986 0.9083188 0.8717641 0.9717050 0.9626353 0.9332535
#> sp10 0.9324011 0.9450314 0.9332535 0.9021903 0.9634418 0.9769307 0.9614703
#> sp11 0.9099412 0.9346807 0.9445881 0.9259994 0.9346807 0.9620495 0.9769307
#> sp12 0.8806666 0.9108522 0.9331581 0.9290437 0.9019792 0.9346807 0.9634418
#> sp13 0.8872211 0.8900855 0.8717641 0.8392197 0.9290437 0.9259994 0.9021903
#> sp14 0.8965674 0.9084374 0.8996729 0.8717641 0.9331581 0.9445881 0.9332535
#> sp15 0.8806666 0.9019792 0.9084374 0.8900855 0.9108522 0.9346807 0.9450314
#> sp16 0.8546264 0.8806666 0.8965674 0.8872211 0.8806666 0.9099412 0.9324011
#>            sp8       sp9      sp10      sp11      sp12      sp13      sp14
#> sp1  0.8965674 0.9337408 0.9324011 0.9099412 0.8806666 0.8872211 0.8965674
#> sp2  0.9331581 0.9323986 0.9450314 0.9346807 0.9108522 0.8900855 0.9084374
#> sp3  0.9670391 0.9083188 0.9332535 0.9445881 0.9331581 0.8717641 0.8996729
#> sp4  0.9749300 0.8717641 0.9021903 0.9259994 0.9290437 0.8392197 0.8717641
#> sp5  0.9084374 0.9717050 0.9634418 0.9346807 0.9019792 0.9290437 0.9331581
#> sp6  0.9445881 0.9626353 0.9769307 0.9620495 0.9346807 0.9259994 0.9445881
#> sp7  0.9808794 0.9332535 0.9614703 0.9769307 0.9634418 0.9021903 0.9332535
#> sp8  1.0000000 0.8996729 0.9332535 0.9626353 0.9717050 0.8717641 0.9083188
#> sp9  0.8996729 1.0000000 0.9808794 0.9445881 0.9084374 0.9749300 0.9670391
#> sp10 0.9332535 0.9808794 1.0000000 0.9769307 0.9450314 0.9608421 0.9808794
#> sp11 0.9626353 0.9445881 0.9769307 1.0000000 0.9810964 0.9259994 0.9626353
#> sp12 0.9717050 0.9084374 0.9450314 0.9810964 1.0000000 0.8900855 0.9323986
#> sp13 0.8717641 0.9749300 0.9608421 0.9259994 0.8900855 1.0000000 0.9749300
#> sp14 0.9083188 0.9670391 0.9808794 0.9626353 0.9323986 0.9749300 1.0000000
#> sp15 0.9323986 0.9331581 0.9634418 0.9810964 0.9668022 0.9290437 0.9717050
#> sp16 0.9337408 0.8965674 0.9324011 0.9657277 0.9777315 0.8872211 0.9337408
#>           sp15      sp16
#> sp1  0.8806666 0.8546264
#> sp2  0.9019792 0.8806666
#> sp3  0.9084374 0.8965674
#> sp4  0.8900855 0.8872211
#> sp5  0.9108522 0.8806666
#> sp6  0.9346807 0.9099412
#> sp7  0.9450314 0.9324011
#> sp8  0.9323986 0.9337408
#> sp9  0.9331581 0.8965674
#> sp10 0.9634418 0.9324011
#> sp11 0.9810964 0.9657277
#> sp12 0.9668022 0.9777315
#> sp13 0.9290437 0.8872211
#> sp14 0.9717050 0.9337408
#> sp15 1.0000000 0.9777315
#> sp16 0.9777315 1.0000000
#> 
#> $note
#> [1] "Response-column coefficient covariance; the response-column source supplies the other Kronecker factor."
#>