tests/testthat/test-init-movement-mat.R

test_that("init_movement_mat() - Tests for argument errors", {
  # n_space
  expect_error(init_movement_mat(n_space = NULL,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = "a",
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = c(2, 3),
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # n_season
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = NULL,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = c(4, 5, 6),
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = matrix(1:9, nrow = 3),
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # n_yr
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = TRUE,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = c(2, 3),
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = NULL,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # move_max
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = NULL,
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 3),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(3L, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # move_slope
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = NULL,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = c(0.1, 0.4),
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = "a",
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # move_fifty
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = NULL,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = c(4, 5, 6),
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = "a",
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # move_south
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = NULL,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = c(0.05, 0.1),
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = "b",
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # move_out
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = NULL,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = c(0.8, 0.9),
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = "no",
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # move_init
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = NULL,
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.1, 0.2, 0.3),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c("a", "b"),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # ages_no_move
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = NULL,
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c("a", "b", "c"),
                                 ages = 0:20,
                                 f_space = c(0.26, 0.74)))
  # ages
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = NULL,
                                 f_space = c(0.26, 0.74)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = "a",
                                 f_space = c(0.26, 0.74)))
  # f_space
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = NULL))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(1L, 2L)))
  expect_error(init_movement_mat(n_space = 2,
                                 n_season = 4,
                                 n_yr = 2,
                                 move_max = rep(0.35, 4),
                                 move_slope = 0.9,
                                 move_fifty = 6,
                                 move_south = 0.05,
                                 move_out = 0.85,
                                 move_init = c(0.25, 0.75),
                                 ages_no_move = c(0, 1),
                                 ages = 0:20,
                                 f_space = c(1.1, 2.1, 3.1)))

})

test_that("init_movement_mat() - Tests for correct output", {
  ages <- 0:4
  out <- init_movement_mat(n_space = 2,
                           space_names = c("Canada", "US"),
                           n_season = 4,
                           season_names = c("Season1", "Season2", "Season3", "Season4"),
                           n_yr = 2,
                           yrs = 2000:2001,
                           move_max = rep(0.35, 4),
                           move_slope = 0.9,
                           move_fifty = 6,
                           move_south = 0.05,
                           move_out = 0.85,
                           move_init = c(0.25, 0.75),
                           ages_no_move = c(0, 1),
                           ages = ages,
                           age_names = paste("age", ages),
                           f_space = c(0.26, 0.74))
  expect_equal(names(out), c("move_mat", "move_init", "f_space"))
  expect_equal(dim(out$move_mat), c(2, 5, 4, 2))
  expect_equal(length(out$move_mat[1,,,]), 40)
  expect_equal(length(out$move_mat[,1,,]), 16)
  expect_equal(length(out$move_mat[,,1,]), 20)
  expect_equal(length(out$move_mat[,,,1]), 40)
  df <- out$move_mat[,,1,1] %>%
    as.data.frame()
  df2 <- out$move_mat[,,1,2] %>%
    as.data.frame()
  comp_df <- data.frame(V1 = c(0, 0),
                        V2 = c(0, 0),
                        V3 = c(0.05, 0.00931),
                        V4 = c(0.05, 0.02204),
                        V5 = c(0.05, 0.04965))
  rownames(comp_df) <- c("Canada", "US")
  names(comp_df) <- names(df)
  expect_equal(df, comp_df, tolerance = 0.0002)
  expect_equal(df2, comp_df, tolerance = 0.0002)
  df3 <- out$move_mat[1,,1,] %>%
    as.data.frame()
  df4 <- out$move_mat[1,,2,] %>%
    as.data.frame()
  comp_df2 <- data.frame(V1 = c(0, 0, 0.05, 0.05, 0.05),
                         V2 = c(0, 0, 0.05, 0.05, 0.05))
  rownames(comp_df2) <- rownames(df3)
  names(comp_df2) <- names(df3)
  expect_equal(df3, comp_df2, tolerance = 0.0001)
  expect_equal(df4, comp_df2, tolerance = 0.0001)
})
pacific-hake/pacifichakemse documentation built on June 11, 2024, 4:07 a.m.