tests/testthat/test-aggregate.R

test_that("Aggregated Gaussian observations, using fm_block_eval", {
  skip_on_cran()
  local_bru_safe_inla()

  obs <- data.frame(
    x = c(10, 20, 30),
    y = c(10, 20, 30),
    z = c(10, 20, 30)
  )
  pred <- data.frame(
    x = c(1, 2, 3, 4, 5, 6),
    y = c(1, 20, 3, 40, 5, 60),
    weights = c(1, 1, 1, 1, 1, 1),
    grp = c(1, 1, 2, 2, 2, 3)
  )

  comp <- ~ Intercept(1) + x

  fit <- bru(
    comp,
    bru_obs(
      z ~ fmesher::fm_block_eval(
        block = grp,
        n_block = nrow(obs),
        weights = weights,
        rescale = TRUE,
        values = Intercept + x
      ),
      family = "normal",
      response_data = obs,
      data = pred,
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      ),
      is_rowwise = FALSE
    )
  )

  expect_equal(
    fit$summary.fixed$mean,
    c(3.033, 4.426),
    tolerance = midtol
  )

  # With basic sf storage:

  obs_sf <- sf::st_as_sf(obs, coords = c("x", "y"))
  pred_sf <- sf::st_as_sf(pred, coords = c("x", "y"))

  comp_sf <- ~ Intercept(1) + x(sf::st_coordinates(pred_sf)[, "X"])

  fit_sf <- bru(
    comp_sf,
    bru_obs(
      z ~ fmesher::fm_block_eval(
        block = grp,
        weights = weights,
        rescale = TRUE,
        n_block = nrow(obs),
        values = Intercept + x
      ),
      family = "normal",
      response_data = obs_sf,
      data = pred_sf,
      control.family = list(
        hyper = list(prec = list(initial = 6, fixed = TRUE))
      ),
      is_rowwise = FALSE
    )
  )

  expect_equal(
    fit_sf$summary.fixed$mean,
    c(3.033, 4.426),
    tolerance = midtol
  )
})


test_that("Aggregated Gaussian observations, using aggregate feature", {
  skip_on_cran()
  local_bru_safe_inla()

  obs <- data.frame(
    x = c(10, 20, 30),
    y = c(1000, 2000, 3000),
    z = c(10, 20, 30),
    the_group_char = c("a", "b", "c")
  )
  pred <- data.frame(
    x = c(1, 2, 3, 4, 5, 6),
    y = c(1, 20, 3, 40, 5, 60),
    weights = c(1, 1, 1, 1, 1, 1),
    grp = c(1, 1, 2, 2, 2, 3),
    grp_char = c("a", "a", "b", "b", "b", "c")
  )
  domain <- list()

  comp <- ~ Intercept(1) + x + y
  comp_list <- bru_comp_list(comp)

  fit <- bru(
    comp,
    bru_obs(
      z ~ Intercept + x + y,
      family = "normal",
      response_data = obs,
      data = pred,
      aggregate = "average",
      aggregate_input = list(
        weights = weights,
        block = grp,
        n_block = bru_response_size(.response_data.)
      ),
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      )
    )
  )

  expect_equal(
    fit$summary.fixed$mean,
    c(3.636, 3.889, 0.0505),
    tolerance = midtol
  )

  expect_no_error({
    bru_obs(
      z ~ Intercept + x + y,
      family = "normal",
      response_data = obs,
      data = pred,
      aggregate = "average",
      aggregate_input = list(
        weights = weights,
        block = grp
      ),
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      )
    )
  })

  # Missing block information
  expect_error(
    {
      obs_ <- bru_obs(
        z ~ Intercept + x + y,
        family = "normal",
        response_data = obs,
        data = c(as.list(pred), list(.block = NULL)),
        aggregate = "average",
        aggregate_input = list(
          weights = weights,
          n_block = bru_response_size(.response_data.)
        ),
        control.family = list(
          hyper = list(
            prec = list(
              initial = 6,
              fixed = TRUE
            )
          )
        )
      )
      inp <- bru_input(obs_, comp_list)
    },
    paste0(
      "Aggregation requested, but `aggregate_input[['block']]` ",
      "evaluates to NULL."
    ),
    fixed = TRUE
  )

  # Character block information without matching response block information
  expect_error(
    {
      obs_ <- bru_obs(
        z ~ Intercept + x + y,
        family = "normal",
        response_data = obs,
        data = pred,
        aggregate = "average",
        aggregate_input = list(
          weights = weights,
          block = grp_char,
          n_block = bru_response_size(.response_data.)
        ),
        control.family = list(
          hyper = list(
            prec = list(
              initial = 6,
              fixed = TRUE
            )
          )
        )
      )
      inp <- bru_input(obs_, comp_list)
    },
    paste0(
      "'character' aggregation block information detected."
    ),
    fixed = TRUE
  )

  # Character block information with matching response block information
  local_bru_options_set(bru_method = list(agg = "pandemic"))
  expect_warning(
    {
      expect_no_error({
        bru_obs(
          z ~ Intercept + x + y,
          family = "normal",
          response_data = obs,
          data = pred,
          aggregate = "average",
          aggregate_input = list(
            weights = weights,
            block = grp_char,
            n_block = bru_response_size(.response_data.),
            block_response = "the_group_char"
          ),
          control.family = list(
            hyper = list(
              prec = list(
                initial = 6,
                fixed = TRUE
              )
            )
          )
        )
      })
    },
    "Using `block_response` in `aggregate_input` was deprecated in inlabru",
    fixed = TRUE
  )

  expect_no_error({
    bru_obs(
      z ~ Intercept + x + y,
      family = "normal",
      response_data = obs,
      data = pred,
      aggregate = "average",
      aggregate_input = list(
        weights = weights,
        block = grp_char,
        n_block = bru_response_size(.response_data.)
      ),
      response_block = the_group_char,
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      )
    )
  })

  local_bru_options_set(bru_method = list(autodiff = "fullchain"))
  expect_warning(
    {
      expect_no_error({
        bru_obs(
          z ~ Intercept + x + y,
          family = "normal",
          response_data = obs,
          data = pred,
          aggregate = "average",
          aggregate_input = list(
            weights = weights,
            block = grp_char,
            n_block = bru_response_size(.response_data.),
            block_response = "the_group_char"
          ),
          control.family = list(
            hyper = list(
              prec = list(
                initial = 6,
                fixed = TRUE
              )
            )
          )
        )
      })
    },
    "Using `block_response` in `aggregate_input` was deprecated in inlabru"
  )
})


test_that("Aggregated Gaussian observations, using domain/samplers feature", {
  skip_on_cran()
  local_bru_safe_inla()

  obs <- data.frame(
    x = c(10, 20, 30),
    z = c(10, 20, 30)
  )
  pred <- data.frame(
    y = c((1 + 20) / 2, (3 + 40 + 5) / 3, 60)
  )
  domain <- list(x = 1:6)
  samplers <- list(x = list(1:2, 3:5, 6))

  comp <- ~ Intercept(1) + x + y

  fit <- bru(
    comp,
    bru_obs(
      z ~ Intercept + x + y,
      family = "normal",
      response_data = obs,
      data = pred,
      aggregate = "average",
      domain = domain,
      samplers = samplers,
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      )
    ),
    options = list(
      control.inla = list(
        int.strategy = "eb"
      )
    )
  )

  expect_equal(
    fit$summary.fixed$mean,
    c(3.636, 3.889, 0.0505),
    tolerance = midtol
  )

  expect_error(
    {
      bru_model(
        comp,
        bru_obs(
          z ~ Intercept + x + y,
          family = "normal",
          response_data = obs,
          data = NULL,
          aggregate = "average",
          domain = domain,
          samplers = samplers,
          control.family = list(
            hyper = list(
              prec = list(
                initial = 6,
                fixed = TRUE
              )
            )
          )
        )
      )
    },
    paste0(
      "The input evaluation 'y' for 'y' failed.\n",
      "Perhaps the data object doesn't contain the needed variables?"
    ),
    fixed = TRUE
  )
})


test_that("Aggregated Poisson observations, using mapper", {
  skip_on_cran()
  local_bru_safe_inla()

  obs <- data.frame(y = c(10, 20, 30))
  pred <- data.frame(
    x = c(1, 2, 3, 4, 5, 6),
    weights = c(1, 1, 1, 1, 1, 1),
    grp = c(1, 1, 2, 2, 2, 3)
  )

  # Aggregation by summation on the intensity/expectation scale
  # (log-sum-exp since the predictor is log-intensity)
  agg <- bm_logsumexp(rescale = FALSE, n_block = nrow(obs))

  comp <- ~ Intercept(1) + x

  fit <- bru(
    comp,
    bru_obs(
      y ~ ibm_eval(
        agg,
        input = list(weights = weights, block = grp),
        state = Intercept + x
      ),
      family = "poisson",
      response_data = obs,
      data = pred,
      is_rowwise = FALSE
    )
  )

  expect_equal(
    fit$summary.fixed$mean,
    c(0.337, 0.470),
    tolerance = midtol
  )

  # With E specification:

  obs <- data.frame(
    y = c(10, 20, 30),
    E = c(1, 2, 3)
  )

  fit <- bru(
    comp,
    bru_obs(
      y ~ ibm_eval(
        agg,
        input = list(weights = weights, block = grp),
        state = Intercept + x
      ),
      family = "poisson",
      E = E,
      response_data = obs,
      data = pred,
      is_rowwise = FALSE
    )
  )

  expect_equal(
    fit$summary.fixed$mean,
    c(0.639, 0.237),
    tolerance = midtol
  )
})

test_that("Aggregated Gaussian observations, using mapper", {
  skip_on_cran()
  local_bru_safe_inla()

  obs <- data.frame(
    x = c(10, 20, 30),
    y = c(10, 20, 30),
    z = c(10, 20, 30)
  )
  pred <- data.frame(
    x = c(1, 2, 3, 4, 5, 6),
    y = c(1, 20, 3, 40, 5, 60),
    weights = c(1, 1, 1, 1, 1, 1),
    grp = c(1, 1, 2, 2, 2, 3)
  )

  # Aggregation by average:
  agg <- bm_aggregate(rescale = TRUE, n_block = nrow(obs))

  comp <- ~ Intercept(1) + x

  fit <- bru(
    comp,
    bru_obs(
      z ~ ibm_eval(
        agg,
        input = list(weights = weights, block = grp),
        state = Intercept + x
      ),
      family = "normal",
      response_data = obs,
      data = pred,
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      ),
      is_rowwise = FALSE
    )
  )

  expect_equal(
    fit$summary.fixed$mean,
    c(3.033, 4.426),
    tolerance = midtol
  )

  # With basic sf storage:

  obs_sf <- sf::st_as_sf(obs, coords = c("x", "y"))
  pred_sf <- sf::st_as_sf(pred, coords = c("x", "y"))

  comp_sf <- ~ Intercept(1) + x(sf::st_coordinates(pred_sf)[, "X"])

  fit_sf <- bru(
    comp_sf,
    bru_obs(
      z ~ ibm_eval(
        agg,
        input = list(weights = weights, block = grp),
        state = Intercept + x
      ),
      family = "normal",
      response_data = obs_sf,
      data = pred_sf,
      control.family = list(
        hyper = list(prec = list(initial = 6, fixed = TRUE))
      ),
      is_rowwise = FALSE
    )
  )

  expect_equal(
    fit_sf$summary.fixed$mean,
    c(3.033, 4.426),
    tolerance = midtol
  )
})


test_that("New method `fullchain` works", {
  skip_on_cran()
  local_bru_safe_inla()

  obs <- data.frame(
    x = c(10, 20, 30),
    z = c(10, 20, 30)
  )
  pred <- data.frame(
    y = c((1 + 20) / 2, (3 + 40 + 5) / 3, 60)
  )
  domain <- list(x = 1:6)
  samplers <- list(x = list(1:2, 3:5, 6))

  comp <- ~ Intercept(1) + x + y

  local_bru_options_set(
    bru_method = list(
      agg = "pandemic",
      autodiff = "pandemic"
    )
  )
  fit <- bru(
    comp,
    bru_obs(
      z ~ Intercept + x + y,
      family = "normal",
      response_data = obs,
      data = pred,
      aggregate = "average",
      domain = domain,
      samplers = samplers,
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      )
    ),
    options = list(
      control.inla = list(
        int.strategy = "eb"
      )
    )
  )

  expect_equal(
    fit$summary.fixed$mean,
    c(3.636, 3.889, 0.0505),
    tolerance = midtol
  )

  obs_list <- as_bru_obs_list(fit)
  comp_list <- as_bru_comp_list(fit)
  comp_mappers <- as_bm_list(comp_list)
  ibm_eval(
    obs_list,
    bru_input(
      fit$bru_info$model
    ),
    state = list(Intercept = 1, x = 2, y = 3),
    comp_mappers = comp_mappers
  )

  local_bru_options_set(
    bru_method = list(
      agg = "pandemic",
      autodiff = "fullchain"
    )
  )
  fit2 <- bru(
    comp,
    bru_obs(
      z ~ Intercept + x + y,
      family = "normal",
      response_data = obs,
      data = pred,
      aggregate = "average",
      domain = domain,
      samplers = samplers,
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      )
    ),
    options = list(
      control.inla = list(
        int.strategy = "eb"
      )
    )
  )

  expect_equal(
    fit2$summary.fixed$mean,
    c(3.636, 3.889, 0.0505),
    tolerance = midtol
  )

  obs2_list <- as_bru_obs_list(fit2)
  comp2_list <- as_bru_comp_list(fit2)
  comp2_mappers <- as_bm_list(comp2_list)
  ibm_eval(
    obs2_list,
    bru_input(
      fit2$bru_info$model
    ),
    state = list(Intercept = 1, x = 2, y = 3),
    comp_mappers = comp2_mappers
  )

  local_bru_options_set(
    bru_method = list(
      agg = "fullchain",
      autodiff = "fullchain"
    )
  )
  fit3 <- bru(
    comp,
    bru_obs(
      z ~ Intercept + x + y,
      family = "normal",
      response_data = obs,
      data = pred,
      aggregate = "average",
      domain = domain,
      samplers = samplers,
      control.family = list(
        hyper = list(
          prec = list(
            initial = 6,
            fixed = TRUE
          )
        )
      )
    ),
    options = list(
      control.inla = list(
        int.strategy = "eb"
      )
    )
  )

  expect_equal(
    fit3$summary.fixed$mean,
    c(3.636, 3.889, 0.0505),
    tolerance = midtol
  )

  obs3_list <- as_bru_obs_list(fit3)
  comp3_list <- as_bru_comp_list(fit3)
  comp3_mappers <- as_bm_list(comp3_list)
  ibm_eval(
    obs3_list,
    bru_input(
      fit3$bru_info$model
    ),
    state = list(Intercept = 1, x = 2, y = 3),
    comp_mappers = comp3_mappers
  )

  # bench::mark(A={
  # local_bru_options_set(bru_method = list(
  #   agg = "pandemic",
  #   autodiff = "pandemic"
  # ))
  #   fit <- bru(
  #     comp,
  #     bru_obs(
  #       z ~ Intercept + exp(x) + y,
  #       family = "normal",
  #       response_data = obs,
  #       data = pred,
  #       aggregate = "average",
  #       domain = domain,
  #       samplers = samplers,
  #       control.family = list(
  #         hyper = list(
  #           prec = list(
  #             initial = 6,
  #             fixed = TRUE
  #           )
  #         )
  #       )
  #     ),
  #     options = list(control.inla = list(
  #       int.strategy = "eb"
  #     ))
  #   )
  # },
  # B={local_bru_options_set(bru_method = list(
  #     agg = "pandemic",
  #     autodiff = "fullchain"
  #   ))
  #   fit2 <- bru(
  #     comp,
  #     bru_obs(
  #       z ~ Intercept + exp(x) + y,
  #       family = "normal",
  #       response_data = obs,
  #       data = pred,
  #       aggregate = "average",
  #       domain = domain,
  #       samplers = samplers,
  #       control.family = list(
  #         hyper = list(
  #           prec = list(
  #             initial = 6,
  #             fixed = TRUE
  #           )
  #         )
  #       )
  #     ),
  #     options = list(control.inla = list(
  #       int.strategy = "eb"
  #     ))
  #   )
  # },
  # C={
  #   local_bru_options_set(bru_method = list(
  #     agg = "fullchain",
  #     autodiff = "fullchain"
  #   ))
  #   fit3 <- bru(
  #     comp,
  #     bru_obs(
  #       z ~ Intercept + exp(x) + y,
  #       family = "normal",
  #       response_data = obs,
  #       data = pred,
  #       aggregate = "average",
  #       domain = domain,
  #       samplers = samplers,
  #       control.family = list(
  #         hyper = list(
  #           prec = list(
  #             initial = 6,
  #             fixed = TRUE
  #           )
  #         )
  #       )
  #     ),
  #     options = list(control.inla = list(
  #       int.strategy = "eb"
  #     ))
  #   )
  # },
  # check = FALSE
  # )
})

Try the inlabru package in your browser

Any scripts or data that you put into this service are public.

inlabru documentation built on July 28, 2026, 9:07 a.m.