tests/testthat/test-jsdgam.R

context("jsdgam")

# Reconstruct the spider data from mvabund
spiderdat <- structure(
  list(
    abundance = c(
      25L,
      0L,
      15L,
      2L,
      1L,
      0L,
      2L,
      0L,
      1L,
      3L,
      15L,
      16L,
      3L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      7L,
      17L,
      11L,
      9L,
      3L,
      29L,
      15L,
      10L,
      2L,
      20L,
      6L,
      20L,
      6L,
      7L,
      11L,
      1L,
      0L,
      1L,
      13L,
      43L,
      2L,
      0L,
      3L,
      0L,
      1L,
      1L,
      2L,
      1L,
      0L,
      0L,
      0L,
      1L,
      0L,
      0L,
      0L,
      0L,
      0L,
      2L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      1L,
      2L,
      0L,
      1L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      16L,
      15L,
      20L,
      9L,
      6L,
      11L,
      14L,
      0L,
      0L,
      2L,
      1L,
      2L,
      6L,
      12L,
      0L,
      0L,
      0L,
      0L,
      0L,
      2L,
      1L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      4L,
      7L,
      5L,
      0L,
      18L,
      4L,
      1L,
      4L,
      30L,
      9L,
      24L,
      9L,
      6L,
      16L,
      7L,
      0L,
      0L,
      1L,
      0L,
      18L,
      4L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      2L,
      0L,
      0L,
      0L,
      0L,
      1L,
      1L,
      1L,
      1L,
      0L,
      1L,
      55L,
      0L,
      0L,
      0L,
      0L,
      1L,
      3L,
      6L,
      6L,
      2L,
      5L,
      12L,
      13L,
      16L,
      0L,
      2L,
      0L,
      1L,
      0L,
      0L,
      0L,
      60L,
      1L,
      29L,
      7L,
      2L,
      11L,
      30L,
      2L,
      26L,
      22L,
      95L,
      96L,
      24L,
      14L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      1L,
      2L,
      6L,
      3L,
      11L,
      0L,
      1L,
      6L,
      12L,
      15L,
      18L,
      29L,
      135L,
      27L,
      89L,
      2L,
      1L,
      0L,
      0L,
      1L,
      53L,
      15L,
      0L,
      2L,
      0L,
      0L,
      1L,
      0L,
      0L,
      0L,
      0L,
      0L,
      6L,
      0L,
      0L,
      0L,
      45L,
      37L,
      45L,
      94L,
      76L,
      24L,
      105L,
      1L,
      1L,
      0L,
      1L,
      8L,
      72L,
      72L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      1L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      0L,
      57L,
      65L,
      66L,
      86L,
      91L,
      63L,
      118L,
      30L,
      2L,
      1L,
      4L,
      13L,
      97L,
      94L,
      25L,
      28L,
      23L,
      25L,
      22L,
      22L,
      18L,
      1L,
      1L,
      0L,
      16L,
      1L,
      0L,
      2L,
      4L,
      9L,
      1L,
      25L,
      17L,
      34L,
      16L,
      3L,
      0L,
      0L,
      0L,
      0L,
      22L,
      32L,
      3L,
      4L,
      2L,
      0L,
      3L,
      2L,
      2L,
      0L,
      0L,
      0L,
      6L,
      0L,
      0L,
      0L
    ),
    taxon = structure(
      c(
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        5L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        6L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        7L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        8L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        9L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        10L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        11L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L,
        12L
      ),
      levels = c(
        "Alopacce",
        "Alopcune",
        "Alopfabr",
        "Arctlute",
        "Arctperi",
        "Auloalbi",
        "Pardlugu",
        "Pardmont",
        "Pardnigr",
        "Pardpull",
        "Trocterr",
        "Zoraspin"
      ),
      class = "factor"
    ),
    site = c(
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L,
      1L,
      2L,
      3L,
      4L,
      5L,
      6L,
      7L,
      8L,
      9L,
      10L,
      11L,
      12L,
      13L,
      14L,
      15L,
      16L,
      17L,
      18L,
      19L,
      20L,
      21L,
      22L,
      23L,
      24L,
      25L,
      26L,
      27L,
      28L
    ),
    soil.dry = c(
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555,
      2.3321,
      3.0493,
      2.5572,
      2.6741,
      3.0155,
      3.381,
      3.1781,
      2.6247,
      2.4849,
      2.1972,
      2.2192,
      2.2925,
      3.5175,
      3.0865,
      3.2696,
      3.0301,
      3.3322,
      3.1224,
      2.9232,
      3.1091,
      2.9755,
      1.2528,
      1.1939,
      1.6487,
      1.8245,
      0.9933,
      0.9555,
      0.9555
    ),
    bare.sand = c(
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434,
      0,
      0,
      0,
      0,
      0,
      2.3979,
      0,
      0,
      0,
      3.9318,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      3.2581,
      3.0445,
      3.2581,
      3.5835,
      4.5109,
      2.3979,
      3.434
    ),
    fallen.leaves = c(
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      0,
      0,
      3.434,
      0,
      4.2627,
      0,
      0,
      0,
      0,
      1.7918,
      0,
      4.3944,
      4.6052,
      4.4543,
      4.3944,
      4.5109,
      4.5951,
      4.5643,
      0,
      0,
      0,
      0,
      0,
      0,
      0
    ),
    moss = c(
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136,
      3.0445,
      1.0986,
      2.3979,
      2.3979,
      0,
      2.3979,
      0.6931,
      1.0986,
      4.3307,
      3.434,
      4.1109,
      3.8286,
      0.6931,
      1.7918,
      0.6931,
      0.6931,
      0.6931,
      0,
      1.6094,
      0.6931,
      0.6931,
      4.3307,
      4.0254,
      4.0254,
      1.0986,
      1.7918,
      3.8286,
      3.7136
    ),
    herb.layer = c(
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434,
      4.4543,
      4.5643,
      4.6052,
      4.6151,
      4.6151,
      3.434,
      4.6151,
      3.434,
      3.2581,
      3.0445,
      3.7136,
      4.0254,
      4.5109,
      4.5643,
      3.0445,
      0.6931,
      3.0445,
      3.0445,
      1.6094,
      0.6931,
      1.7918,
      0.6931,
      3.2581,
      3.0445,
      4.1109,
      1.7918,
      3.434,
      3.434
    ),
    reflection = c(
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889,
      3.912,
      1.6094,
      3.6889,
      2.9957,
      2.3026,
      0.6931,
      2.3026,
      0.6931,
      3.4012,
      3.6889,
      3.6889,
      3.6889,
      3.4012,
      1.0986,
      0.6931,
      0,
      1.0986,
      1.0986,
      0,
      0,
      0,
      3.912,
      4.0943,
      4.0073,
      2.3026,
      4.382,
      3.6889,
      3.6889
    )
  ),
  row.names = c(NA, -336L),
  class = "data.frame"
)

test_that("family must be correctly specified", {
  expect_error(
    mod <- jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      data = spiderdat,
      unit = site,
      species = taxon,
      n_lv = 3,
      family = 'banana'
    ),
    'family not recognized'
  )
})

test_that("response variable must be specified", {
  expect_error(
    jsdgam(
      formula = ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection)
      ,
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      data = spiderdat,
      unit = site,
      species = taxon,
      n_lv = 3,
      family = nb()
    ),
    'Not sure how to deal with this response variable specification'
  )
})

test_that("unit must exist in data", {
  expect_error(
    jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      data = spiderdat,
      unit = banana,
      species = taxon,
      n_lv = 3,
      family = nb()
    ),
    'Variable "banana" not found in data'
  )
})


test_that("species must exist in data", {
  expect_error(
    jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      data = spiderdat,
      unit = site,
      species = banana,
      n_lv = 3,
      family = nb()
    ),
    'Variable "banana" not found in data'
  )
})

test_that("species must be a factor in data", {
  spiderdat$taxon <- as.numeric(spiderdat$taxon)
  expect_error(
    jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      data = spiderdat,
      unit = site,
      species = taxon,
      n_lv = 3,
      family = nb()
    ),
    'Variable "taxon" must be a factor type'
  )
})

test_that("unit must be a numeric / integer in data", {
  spiderdat$site <- as.factor(spiderdat$site)
  expect_error(
    jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      data = spiderdat,
      unit = site,
      species = taxon,
      n_lv = 3,
      family = nb()
    ),
    'Variable "site" must be either numeric or integer type'
  )
})

test_that("n_lv must be <= number of species", {
  expect_error(
    jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      data = spiderdat,
      unit = site,
      species = taxon,
      n_lv = 15,
      family = nb()
    ),
    'Number of factors must be <= number of levels in species'
  )
})

test_that("knots must be a list", {
  expect_error(
    jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend) - 1,
      # supplying knots as a vector should fail
      factor_knots = seq(
        min(spiderdat$soil.dry),
        max(spiderdat$soil.dry),
        length.out = 4
      ),
      data = spiderdat,
      unit = site,
      species = taxon,
      n_lv = 3,
      family = nb(),
      run_model = FALSE
    ),
    'all "knot" arguments must be supplied as lists'
  )
})

test_that("errors about knot lengths should be propagated from mgcv", {
  expect_error(
    jsdgam(
      formula = abundance ~
        # Environmental model includes species-level intercepts
        # and random slopes for a linear effect of reflection
        s(taxon, bs = 're') +
          s(taxon, bs = 're', by = reflection),
      # Each factor estimates a different, possibly nonlinear effect of soil.dry
      factor_formula = ~ s(soil.dry, k = 5, by = trend, bs = 'cr') - 1,
      # knot length should be 5 for this CR basis
      factor_knots = list(
        soil.dry = seq(
          min(spiderdat$soil.dry),
          max(spiderdat$soil.dry),
          length.out = 4
        )
      ),
      data = spiderdat,
      unit = site,
      species = taxon,
      n_lv = 3,
      family = nb(),
      run_model = FALSE
    ),
    'number of supplied knots != k for a cr smooth'
  )
})

test_that("get_mvgam_priors accepts factor_formula", {
  expect_no_error(get_mvgam_priors(
    formula = abundance ~
      # Environmental model includes species-level intercepts
      # and random slopes for a linear effect of reflection
      s(taxon, bs = 're') +
        s(taxon, bs = 're', by = reflection),
    # Each factor estimates a different, possibly nonlinear effect of soil.dry
    factor_formula = ~ s(soil.dry, k = 5, by = trend, bs = 'cr') - 1,
    data = spiderdat,
    unit = site,
    species = taxon,
    n_lv = 3,
    trend_model = 'None'
  ))
})

# Skip the next test as it should actually initiate the model, and may take a few seconds
skip_on_cran()
test_that("jsdgam should initiate correctly", {
  mod <- jsdgam(
    formula = abundance ~
      # Environmental model includes species-level intercepts
      # and random slopes for a linear effect of reflection
      s(taxon, bs = 're') +
        s(taxon, bs = 're', by = reflection),
    # Each factor estimates a different, possibly nonlinear effect of soil.dry
    factor_formula = ~ s(soil.dry, k = 5, bs = 'cr', by = trend) - 1,
    # supplying knots should also work if k matches length(knots)
    factor_knots = list(
      soil.dry = seq(
        min(spiderdat$soil.dry),
        max(spiderdat$soil.dry),
        length.out = 5
      )
    ),
    data = spiderdat,
    unit = site,
    species = taxon,
    n_lv = 3,
    family = nb(),
    run_model = FALSE
  )
  expect_true(inherits(mod, 'mvgam_prefit'))
  expect_true(identical(
    attr(mod$trend_mgcv_model, 'knots'),
    list(
      soil.dry = seq(
        min(spiderdat$soil.dry),
        max(spiderdat$soil.dry),
        length.out = 5
      )
    )
  ))
  expect_true(is.null(attr(mod$mgcv_model, 'knots')))
  expect_true(any(grepl(
    '// raw latent factors (with linear predictors)',
    mod$model_file,
    fixed = TRUE
  )))
  expect_true(any(grepl(
    'matrix[n_series, n_lv] lv_coefs = rep_matrix(0, n_series, n_lv);',
    mod$model_file,
    fixed = TRUE
  )))
  expect_true(attr(mod$model_data, 'trend_model') == 'None')
  expect_true(inherits(
    attr(mod$model_data, 'prepped_trend_model'),
    'mvgam_trend'
  ))
  expect_true(attr(mod$model_data, 'prepped_trend_model')$subgr == 'taxon')
  expect_true(attr(mod$model_data, 'prepped_trend_model')$trend_model == 'ZMVN')
})

test_that("jsdgam post-processing works correctly", {
  # Run a short one to ensure post-processing and update work correctly
  mod <- SM(jsdgam(
    formula = abundance ~
      # Environmental model includes species-level intercepts
      # and random slopes for a linear effect of reflection
      s(taxon, bs = 're') +
        s(taxon, bs = 're', by = reflection),
    # Each factor estimates a different, possibly nonlinear effect of soil.dry
    factor_formula = ~ s(soil.dry, k = 5, bs = 'cr', by = trend) - 1,
    # supplying knots should also work if k matches length(knots)
    factor_knots = list(
      soil.dry = seq(
        min(spiderdat$soil.dry),
        max(spiderdat$soil.dry),
        length.out = 5
      )
    ),
    data = spiderdat %>%
      dplyr::filter(site < 7),
    unit = site,
    species = taxon,
    n_lv = 2,
    family = nb(),
    run_model = TRUE,
    residuals = FALSE,
    chains = 2,
    silent = 2
  ))
  expect_true(inherits(mod, 'jsdgam'))
  expect_true(is.null(mod$resids))

  # Calculate residual correlations to ensure it works
  post_cors <- residual_cor(mod, summary = FALSE)
  expect_equal(dim(post_cors$all_cormat)[1], 1000L)
  expect_equal(dim(post_cors$all_cormat)[2], nlevels(spiderdat$taxon))
  expect_true(all(post_cors$all_cormat <= 1))
  expect_true(all(post_cors$all_cormat >= -1))

  post_cors <- residual_cor(mod, summary = TRUE)
  expect_equal(dim(post_cors$cor)[1], nlevels(spiderdat$taxon))
  expect_ggplot(plot(post_cors))

  # Ensure ordination works
  expect_ggplot(ordinate(mod))
  expect_ggplot(ordinate(mod, biplot = FALSE))
  expect_ggplot(ordinate(mod, label_sites = FALSE))
})
nicholasjclark/mvgam documentation built on April 17, 2025, 9:39 p.m.