tests/testthat/test-mig_resid.R

# Age/year sequence of all the data from above
interval <- 5
ages <- seq(0, 100, by = interval)
years <- seq(1950, 2050, by = interval)
ages_asfr <- seq(15, 45, by = interval)

# Vector of population for males
pop_m <-
  c(835, 671, 570.999, 544, 552, 550, 513, 487.998, 432.002,
    378.001, 338.001, 295.999, 263.001, 220.999, 156, 92.001, 42.204,
    18.001, 4.331, 0.639, 0.07, 989.592, 880.029, 697.435, 575.575,
    561.146, 595.087, 582.08, 544.567, 507.247, 440.909, 373.935,
    316.617, 265.548, 235.035, 182.951, 110.75, 57.555, 18.737, 5.459,
    0.807, 0.065, 1133.424, 1037.502, 905.664, 698.771, 586.798,
    616.9, 638.007, 615.889, 550.076, 504.215, 430.131, 353.696,
    286.403, 238.634, 194.496, 130.76, 69.399, 26.238, 5.809, 1.011,
    0.083, 1149.519, 1149.942, 1042.707, 897.087, 691.317, 605.519,
    628.132, 641.442, 608.262, 532.131, 486.313, 401.381, 321.04,
    251.388, 197.567, 138.127, 81.716, 32.255, 8.386, 1.152, 0.112,
    959.81, 1167.439, 1166.318, 1054.263, 929.596, 770.233, 655.408,
    649.175, 641.542, 602.148, 513.273, 463.968, 371.397, 286.345,
    203.426, 141.486, 87.742, 38.904, 10.533, 1.647, 0.131, 904.577,
    990.842, 1195.275, 1192.299, 1096.101, 1010.808, 817.203, 673.368,
    657.377, 631.756, 591.218, 483.434, 432.18, 330.595, 237.691,
    147.718, 86.849, 42.396, 13.172, 2.217, 0.2, 914.965, 926.002,
    1008.784, 1224.923, 1217.653, 1099.911, 1019.023, 813.755, 676.39,
    643.237, 614.691, 562.266, 449.355, 383.548, 273.038, 176.289,
    92.764, 43.563, 15.48, 3.067, 0.316, 943.289, 927.824, 937.572,
    1036.631, 1265.185, 1225.442, 1108.122, 1014.48, 806.487, 659.135,
    627.186, 594.356, 527.096, 404.537, 320.095, 205.116, 112.331,
    47.821, 16.391, 3.791, 0.466, 982.718, 980.995, 958.024, 988.65,
    1076.907, 1293.734, 1267.032, 1135.973, 1031.081, 804.56, 654.945,
    615.926, 567.992, 487.466, 348.485, 248.7, 133.025, 58.273, 17.722,
    3.888, 0.553, 1012.228, 1014.939, 1019.3, 1013.749, 1016.987,
    1100.959, 1315.154, 1281.21, 1143.215, 1026.976, 788.317, 641.579,
    592.945, 521.839, 418.599, 270.316, 163.215, 70.003, 21.874,
    4.186, 0.549, 914.063, 1038.649, 1049.297, 1073.621, 1055.672,
    1047.487, 1138.313, 1344.753, 1295.431, 1144.702, 1016.992, 771.165,
    615.065, 550.259, 457.748, 334.624, 185.735, 87.221, 26.085,
    4.898, 0.538, 879.05, 952.912, 1081.565, 1116.92, 1130.826, 1092.34,
    1097.179, 1186.645, 1381.02, 1305.446, 1143.75, 989.552, 751.696,
    577.344, 489.506, 377.661, 245.003, 105.627, 34.645, 6.138, 0.619,
    967.471, 930.238, 993.227, 1141.416, 1220.943, 1218.465, 1152.732,
    1154.005, 1219.105, 1404.229, 1303.056, 1126.891, 960.563, 712.717,
    522.094, 415.611, 295.598, 149.226, 44.788, 8.64, 0.793, 996.168,
    1002.283, 955.049, 1097.332, 1284.199, 1236.929, 1269.189, 1192.192,
    1174.32, 1216.076, 1400.008, 1311.348, 1099.315, 941.078, 652.11,
    452.242, 319.415, 186.665, 65.652, 11.458, 1.11, 1020.925, 1011.765,
    1014.23, 1017.281, 1208.233, 1389.541, 1315.956, 1321.892, 1224.232,
    1189.671, 1215.983, 1380.774, 1275.239, 1047.397, 867.426, 568.286,
    355.943, 205.33, 83.6, 17, 1.474, 1028.191, 1035.869, 1023.388,
    1074.814, 1125.935, 1311.078, 1465.662, 1367.022, 1352.787, 1239.604,
    1191.356, 1203.245, 1346.713, 1220.838, 973.366, 765.273, 455.163,
    235.912, 97.148, 22.528, 2.061, 1035.917, 1043.755, 1047.947,
    1086.086, 1187.203, 1232.794, 1390.453, 1518.531, 1399.596, 1369.011,
    1242.733, 1181.749, 1178.339, 1295.414, 1142.767, 869.016, 624.221,
    310.175, 115.614, 27.037, 2.784, 1045.967, 1052.236, 1056.4,
    1113.398, 1203.477, 1298.811, 1316.2, 1446.37, 1552.621, 1417.392,
    1372.669, 1234.502, 1160.392, 1138.346, 1219.39, 1029.28, 718.949,
    434.576, 156.257, 33.016, 3.417, 1068.682, 1062.337, 1064.906,
    1121.9, 1230.868, 1315.242, 1382.275, 1372.643, 1481.225, 1570.215,
    1421.698, 1364.113, 1214.291, 1124.645, 1077.332, 1107.059, 862.313,
    510.375, 224.598, 45.72, 4.224, 1102.822, 1085.121, 1075.057,
    1130.572, 1239.683, 1342.955, 1399.057, 1438.921, 1408.243, 1499.967,
    1574.252, 1414.059, 1343.384, 1179.959, 1069.144, 985.391, 938.14,
    623.089, 270.065, 67.236, 5.855, 1140.8, 1119.328, 1097.897,
    1140.924, 1248.743, 1352.213, 1427.121, 1456.061, 1474.768, 1428.008,
    1505.633, 1566.169, 1394.527, 1308.152, 1126.043, 984.266, 844,
    689.026, 337.068, 82.618, 8.662)

# Vector of population for females
pop_f <-
  c(801, 645, 554.001, 534, 557.999, 564, 521.001, 478.001,
    410.999, 352.999, 318, 276, 239, 196.998, 147, 92, 49.354, 23.001,
    6.5, 1.164, 0.135, 948.057, 844.618, 670.281, 562.978, 556.695,
    589.153, 595.677, 545.622, 488.336, 410.687, 346.698, 303.994,
    257.339, 222.218, 177.703, 111.516, 66.076, 25.155, 8.267, 1.423,
    0.145, 1082.85, 993.185, 866.81, 675.391, 590.032, 594.559, 626.777,
    624.823, 547.167, 485.337, 406.556, 337.02, 285.388, 243.185,
    202.252, 135.464, 80.488, 34.773, 9.379, 1.88, 0.182, 1091.999,
    1101.593, 998.637, 870.126, 696.038, 607.414, 608.372, 634.123,
    618.287, 534.182, 476.099, 390.902, 323.659, 270.819, 222.629,
    158.211, 99.985, 43.526, 13.425, 2.209, 0.243, 916.285, 1113.846,
    1116.448, 1022.769, 918.839, 752.674, 635.897, 619.411, 627.163,
    613.115, 524.316, 470.068, 384.21, 315.25, 247.486, 184.146,
    119.509, 56.879, 17.577, 3.347, 0.307, 858.616, 944.546, 1141.242,
    1148.666, 1080.544, 977.493, 784.947, 647.137, 627.282, 623.897,
    615.885, 510.514, 460.492, 370.614, 286.566, 207.64, 136.385,
    70.415, 24.027, 4.599, 0.483, 868.926, 877.749, 959.791, 1175.057,
    1195.136, 1093.776, 991.924, 782.722, 651.731, 622.015, 618.687,
    605.778, 500.641, 442.912, 341.607, 245.476, 158.844, 84.752,
    32.378, 7.171, 0.792, 895.173, 880.805, 893.824, 982.635, 1215.112,
    1205.436, 1108.77, 989.171, 783.703, 645.936, 620.163, 616.473,
    594.188, 483.99, 407.959, 294.664, 188.001, 102.344, 41.293,
    10.454, 1.36, 936.591, 930.873, 909.761, 937.09, 1040.314, 1261.087,
    1253.31, 1138.156, 1013.326, 789.43, 647.395, 618.625, 607.737,
    578.414, 450.459, 356.927, 227.408, 122.534, 50.048, 13.221,
    1.968, 962.734, 966.476, 967.107, 957.99, 982.293, 1078.452,
    1289.82, 1276.239, 1152.444, 1021.414, 790.697, 651.846, 612.561,
    584.997, 533.268, 389.96, 277.225, 150.795, 61.026, 16.175, 2.495,
    872.149, 989.229, 998.47, 1014.575, 1007.4, 1019.803, 1118.597,
    1322.793, 1295.408, 1160.933, 1017.656, 782.288, 640.445, 591.004,
    544.271, 468.132, 312.504, 184.648, 74.05, 18.888, 2.818, 831.614,
    907.289, 1032.916, 1054.491, 1087.81, 1064.803, 1079.765, 1163.223,
    1358.369, 1304.687, 1162.716, 1010.927, 777.628, 621.991, 554.866,
    485.032, 385.873, 214.234, 93.771, 23.583, 3.281, 918.838, 876.318,
    944.356, 1087.72, 1156.023, 1189.243, 1155.079, 1142.97, 1200.737,
    1385.864, 1307.092, 1157.85, 1000.01, 756.228, 585.003, 501.751,
    414.881, 275.154, 114.55, 31.799, 4.292, 950.252, 955.845, 901.601,
    1034.682, 1232.517, 1230.147, 1278.363, 1207.768, 1177.793, 1204.356,
    1388.633, 1309.444, 1137.271, 975.544, 718.972, 536.404, 423.543,
    303.137, 151.219, 39.364, 5.675, 972.207, 966.024, 968.771, 971.038,
    1155.236, 1343.606, 1310.409, 1330.404, 1239.077, 1193.687, 1207.497,
    1378.179, 1287.326, 1102.782, 925.427, 657.537, 458.654, 313.372,
    169.081, 52.593, 7.072, 976.405, 987.332, 978.581, 1036.217,
    1088.349, 1263.153, 1421.22, 1360.854, 1360.488, 1254.47, 1197.429,
    1200.365, 1355.979, 1250.443, 1049.613, 850.295, 565.065, 343.486,
    179.947, 59.846, 9.474, 983.61, 992.108, 1000.337, 1048.292,
    1157.406, 1200.03, 1343.652, 1473.399, 1392.3, 1376.507, 1258.974,
    1191.956, 1183.853, 1320.389, 1194.661, 970.4, 737.82, 429.619,
    201.231, 64.936, 11.111, 993.232, 1000.063, 1005.704, 1073.068,
    1174.732, 1273.909, 1284.275, 1398.562, 1506.421, 1409.749, 1381.642,
    1254.544, 1177.896, 1156.569, 1266.895, 1112.005, 851.243, 570.482,
    257.474, 74.251, 12.326, 1014.748, 1009.727, 1013.685, 1078.453,
    1199.539, 1291.299, 1358.178, 1339.471, 1432.104, 1523.907, 1415.474,
    1377.117, 1241.194, 1153.335, 1113.937, 1185.875, 984.267, 667.614,
    348.639, 96.877, 14.172, 1047.114, 1031.299, 1023.389, 1086.588,
    1205.189, 1316.366, 1375.813, 1413.531, 1373.503, 1450.422, 1529.735,
    1411.833, 1363.681, 1217.645, 1114.502, 1048.481, 1058.718, 782.58,
    415.883, 133.756, 18.362, 1083.119, 1063.736, 1045.012, 1096.489,
    1213.687, 1322.39, 1401.18, 1431.462, 1447.757, 1392.552, 1457.502,
    1526.287, 1399.672, 1340.065, 1180.192, 1054.342, 943.985, 853.051,
    496.821, 162.711, 25.414)

# Vector of age-specific fertility rates
asfr <- c(50.369, 202.131, 206.141, 149.211, 87.253, 31.052,
          2.843, 57.919, 226.709, 222.516, 148.992, 87.888, 29.736, 2.64,
          54.096, 223.587, 211.46, 140.311, 76.881, 26.533, 2.132, 45.049,
          159.679, 156.131, 93.96, 50.059, 15.713, 1.409, 37.188, 119.39,
          132.748, 70.029, 28.02, 7.311, 0.514, 30.209, 101.658, 125.692,
          65.483, 19.804, 3.711, 0.243, 24.9, 88.815, 121.231, 68.621,
          20.031, 3.039, 0.163, 23.238, 78.247, 118.743, 75.403, 24.014,
          3.426, 0.129, 25.141, 75.764, 118.592, 85.555, 29.309, 4.303,
          0.136, 20.117, 64.41, 104.081, 85.589, 32.737, 5.247, 0.219,
          14.645, 53.484, 98.176, 92.658, 37.567, 6.397, 0.273, 13.677,
          51.37, 100.418, 104.868, 48.196, 8.278, 0.393, 11.494, 43.287,
          93.809, 106.904, 53.5, 10.662, 0.544, 8.387, 37.053, 86.307,
          106.038, 55.169, 11.345, 0.701, 6.625, 31.576, 80.064, 106.128,
          58.423, 13.087, 0.917, 5.468, 27.869, 76.196, 107.843, 62.296,
          15.036, 1.172, 4.686, 25.34, 73.943, 110.575, 66.487, 17.107,
          1.462, 4.134, 23.539, 72.551, 113.398, 70.423, 19.099, 1.756,
          3.732, 22.206, 71.53, 115.597, 73.588, 20.803, 2.024, 3.467,
          21.39, 71.244, 117.758, 76.268, 22.224, 2.249) / 1000

# Vector of survival rates for males
sr_m <- c(0.95557549, 0.9921273, 0.99594764, 0.99510483, 0.99178483,
          0.99134461, 0.99100899, 0.98929784, 0.98473229, 0.97588706, 0.96048519,
          0.93812765, 0.90615821, 0.8622277, 0.8047363, 0.71333856, 0.596832,
          0.44396816, 0.30330032, 0.18642771, 0.0911662462413327, 0.96275471,
          0.99399428, 0.9968488, 0.99563281, 0.99229006, 0.99196446, 0.99180061,
          0.99013625, 0.98594365, 0.97719516, 0.96239426, 0.93950426, 0.90620399,
          0.86117682, 0.80225284, 0.71307413, 0.60022645, 0.4558758, 0.31005161,
          0.18518342, 0.0956313878791117, 0.96951141, 0.99496609, 0.99727649,
          0.99607245, 0.99233725, 0.99205108, 0.99228027, 0.99056435, 0.98605767,
          0.97783685, 0.96314897, 0.9406932, 0.90642888, 0.86286999, 0.80387894,
          0.71498269, 0.6066814, 0.46479967, 0.31958557, 0.19836001, 0.101989015830425,
          0.97545992, 0.99563858, 0.99741385, 0.99592654, 0.99152023, 0.99192115,
          0.9924163, 0.990644, 0.98617665, 0.97752991, 0.96353815, 0.94114166,
          0.90833701, 0.86170391, 0.80301014, 0.72151551, 0.6128495, 0.47608317,
          0.32653048, 0.19655555, 0.103730263806538, 0.98060776, 0.99617149,
          0.99761403, 0.99542383, 0.99054495, 0.99194397, 0.99266261, 0.99089428,
          0.98611279, 0.97789594, 0.96434779, 0.94341044, 0.91191009, 0.86748795,
          0.80686197, 0.72532159, 0.61846149, 0.48319275, 0.33857582, 0.2104904,
          0.112385162790671, 0.98550309, 0.99688352, 0.99803301, 0.9959062,
          0.99126003, 0.99231634, 0.99301369, 0.99160392, 0.98761021, 0.97986329,
          0.96646197, 0.94623899, 0.91718051, 0.87584219, 0.81662218, 0.73700918,
          0.6303803, 0.50160307, 0.3651526, 0.23288489, 0.130850768617506,
          0.98931819, 0.99775357, 0.9984583, 0.99673481, 0.99301824, 0.99346278,
          0.99369545, 0.99294003, 0.98994327, 0.98356936, 0.97206458, 0.95389895,
          0.92644026, 0.88661213, 0.82907732, 0.74960973, 0.64331764, 0.51551648,
          0.37629084, 0.24487989, 0.137677217644374, 0.99112504, 0.99814201,
          0.99879926, 0.99711429, 0.99389061, 0.99409107, 0.99396033, 0.99290953,
          0.99073493, 0.98586868, 0.97625645, 0.95984456, 0.93401387, 0.89576747,
          0.84146086, 0.76230416, 0.6528808, 0.51875327, 0.37059318, 0.23716232,
          0.129879589178461, 0.99268153, 0.99853182, 0.99902021, 0.99755244,
          0.99471893, 0.99453053, 0.99383731, 0.99242559, 0.99039498, 0.98664094,
          0.97888902, 0.96514568, 0.94232807, 0.90695918, 0.85686765, 0.78068285,
          0.6693777, 0.52625072, 0.37539746, 0.23620331, 0.123653858706926,
          0.99378702, 0.99880557, 0.99916867, 0.99795758, 0.99537754, 0.99534488,
          0.99484455, 0.99357678, 0.99141921, 0.9878473, 0.98134482, 0.96932265,
          0.94941573, 0.91742454, 0.86776992, 0.79533967, 0.68311452, 0.5343893,
          0.37262307, 0.2239178, 0.113720633638293, 0.99399248, 0.99897976,
          0.99932852, 0.99822431, 0.99601855, 0.99591387, 0.99569633, 0.99470259,
          0.99268715, 0.98892298, 0.98249955, 0.97238094, 0.9552462, 0.92844911,
          0.88653064, 0.82114141, 0.71875477, 0.56869525, 0.39721252, 0.23529357,
          0.113792166251756, 0.99425766, 0.99909623, 0.99940998, 0.99846006,
          0.99631257, 0.99603178, 0.99592291, 0.99518752, 0.99322132, 0.98983274,
          0.98368064, 0.9741733, 0.9602258, 0.93745239, 0.9016288, 0.8444638,
          0.75222073, 0.60907965, 0.4240168, 0.24939352, 0.117336219766853,
          0.99471736, 0.99933256, 0.99947765, 0.99871556, 0.99694493, 0.99649396,
          0.99655801, 0.99589338, 0.99413263, 0.99084735, 0.98542046, 0.97725847,
          0.96497949, 0.9452114, 0.9134356, 0.8610204, 0.7737601, 0.63147622,
          0.43996206, 0.25585397, 0.117677375365884, 0.99489165, 0.99937715,
          0.99951763, 0.99880472, 0.99710652, 0.99665932, 0.99675852, 0.99614922,
          0.99444005, 0.99122406, 0.98598437, 0.97827319, 0.96683874, 0.94835124,
          0.91833471, 0.86821514, 0.78398104, 0.64282533, 0.44786339, 0.25892654,
          0.117310656081295, 0.99547488, 0.99923281, 0.99957341, 0.99911343,
          0.99802214, 0.99709297, 0.99680896, 0.99635099, 0.99498899, 0.99222347,
          0.9875931, 0.98060053, 0.97014661, 0.9536392, 0.92634894, 0.87987019,
          0.79856151, 0.66278044, 0.47312791, 0.2694788, 0.111570323438865,
          0.99610316, 0.99933917, 0.9996362, 0.99923203, 0.99825186, 0.99737383,
          0.99710434, 0.99670571, 0.99548655, 0.99299216, 0.9888173, 0.98253469,
          0.97316181, 0.95828527, 0.93341992, 0.89062075, 0.81386423, 0.68145805,
          0.49006978, 0.27830681, 0.113218864970809, 0.99653042, 0.99941146,
          0.99967816, 0.99931356, 0.99841616, 0.99758578, 0.9973301, 0.99697294,
          0.99585907, 0.99356834, 0.9897352, 0.98398011, 0.97540572, 0.96174959,
          0.93872789, 0.89876001, 0.8256409, 0.69618678, 0.50377247, 0.28557404,
          0.114591438080939, 0.99688677, 0.99947174, 0.99971275, 0.99938206,
          0.99855794, 0.99777511, 0.99753337, 0.99721139, 0.99619015, 0.99408084,
          0.99055187, 0.98526355, 0.97739317, 0.9648224, 0.94346085, 0.90607063,
          0.83636949, 0.70989474, 0.51681708, 0.29260349, 0.115932510195963,
          0.99718332, 0.99952193, 0.9997412, 0.99943948, 0.99868001, 0.99794372,
          0.99771578, 0.99742349, 0.99648352, 0.99453532, 0.99127626, 0.98639967,
          0.97914803, 0.96753912, 0.94766613, 0.91261037, 0.84609618, 0.7225789,
          0.52915469, 0.29935668, 0.117233382382913, 0.99743526, 0.99956461,
          0.99976514, 0.9994886, 0.99878699, 0.99809597, 0.99788161, 0.99761483,
          0.99674727, 0.99494418, 0.99192807, 0.98742013, 0.98072071, 0.96997658,
          0.95145635, 0.91854221, 0.85503156, 0.73446104, 0.54096047, 0.30591861,
          0.118509238191645)

# Vector of survival rates for females
sr_f <- c(0.854489854276296, 0.935421167801612, 0.97813792986728,
          0.982021189677661, 0.976828336081795, 0.97244561985297, 0.968812772150047,
          0.96483427499772, 0.96010802339363, 0.954056165687121, 0.943306039954761,
          0.92448836548943, 0.890690237758345, 0.835639114030282, 0.754796751406155,
          0.644175707707241, 0.510754359186887, 0.367690608641792, 0.24038748937665,
          0.145450728453873, 0.0826258994519641, 0.872081445760557, 0.944846444000478,
          0.981301676540409, 0.98454923599414, 0.980025670920247, 0.976133157582757,
          0.972813894527646, 0.968976434023376, 0.964396307993652, 0.958650557701456,
          0.948256461919103, 0.930336619590153, 0.898721683064412, 0.846943744756808,
          0.7693050373115, 0.660761281137989, 0.526356065457763, 0.380513624627523,
          0.249631099810745, 0.150641910916079, 0.0845984581684562, 0.886848633625797,
          0.952485090106336, 0.983750072193038, 0.986500893000163, 0.982460467395807,
          0.978978117640805, 0.975938233552867, 0.972378413253193, 0.967941794833695,
          0.962223931611845, 0.952268831689409, 0.935291261627555, 0.905449822961756,
          0.856292233575997, 0.781888442354377, 0.676068647825169, 0.542224475987347,
          0.394762631381521, 0.260252774164775, 0.156857215747202, 0.0874135544568921,
          0.900784558263659, 0.9596839642243, 0.986196123803518, 0.988385232322206,
          0.984769248387878, 0.981657532920333, 0.978917213857464, 0.975642526638743,
          0.971423457946261, 0.965729507599766, 0.956020853088256, 0.939966236835617,
          0.912171280137383, 0.865872642393594, 0.794508147678775, 0.691547047753295,
          0.558414706244368, 0.408689574652693, 0.269878505194327, 0.1624396495176,
          0.0898671492416105, 0.912633835108388, 0.965550681132028, 0.987920059015778,
          0.989848240747598, 0.986695608763104, 0.983981235542121, 0.981570190639542,
          0.97861812228183, 0.974635289736998, 0.9691431860219, 0.959749667932423,
          0.944374872196883, 0.918112298204692, 0.874309263183862, 0.80582736469469,
          0.705435057343639, 0.573891155573389, 0.423123546270893, 0.280818298136084,
          0.169320472983824, 0.0930347806338448, 0.922791200429312, 0.970414095250172,
          0.989432301168788, 0.990708732285749, 0.987862933459543, 0.98554556950358,
          0.983439950663867, 0.980726138018158, 0.976956585172812, 0.971679032024458,
          0.962795297767379, 0.948303265698793, 0.923561621562827, 0.882274284142424,
          0.817184362828982, 0.720298723984997, 0.590336398050365, 0.439061608307152,
          0.293893306863672, 0.178752339638971, 0.0979770657163587, 0.933750711567667,
          0.975650023350237, 0.991430129981753, 0.99247477931302, 0.989942622594004,
          0.987846767986695, 0.98591292962169, 0.983362542545618, 0.979767042495056,
          0.974726611965329, 0.96628359885727, 0.95251185213316, 0.929005505885616,
          0.889790085932051, 0.827762556372604, 0.734212960407364, 0.606322257595734,
          0.453895037584414, 0.305625301261282, 0.186403154530675, 0.101364444633525,
          0.942022185331379, 0.979147123918558, 0.992515619501369, 0.9933310350342,
          0.990999245807151, 0.98905130373017, 0.987257739978207, 0.984897031588263,
          0.981492787306857, 0.9766673141557, 0.968686240948038, 0.955634366723833,
          0.9333801029294, 0.895907358522987, 0.836661271636903, 0.746801163214231,
          0.621415950298903, 0.468910608066693, 0.317915153427838, 0.195039481469627,
          0.105384282613558, 0.94718229582512, 0.980790142908247, 0.992803300931434,
          0.993541163348349, 0.991307476656705, 0.989225443921528, 0.987176469101693,
          0.98460623719428, 0.981246509709472, 0.976718447241337, 0.969160984177711,
          0.956833654346736, 0.935783935128507, 0.900223655022325, 0.843512027545961,
          0.75698552882669, 0.634658767009268, 0.482935300037292, 0.329848456620383,
          0.20331169504299, 0.10978246168398, 0.953756599857967, 0.984059901645376,
          0.993955214747401, 0.994268297381336, 0.991878910452198, 0.989316711233698,
          0.98661050317541, 0.983509725457937, 0.980068840413112, 0.975865841537748,
          0.96873162752321, 0.957191306973029, 0.937336013374256, 0.903694171906013,
          0.84952642553648, 0.76536853834578, 0.645403389194791, 0.494479119379901,
          0.339863899010747, 0.210826549541289, 0.113471459046826, 0.960713535654886,
          0.987033073900882, 0.994716009106623, 0.994894234746082, 0.992525302465177,
          0.9895538842758, 0.986152805334012, 0.982472321949589, 0.979073662581534,
          0.975395328209274, 0.968932259296626, 0.958359612326248, 0.939967835580358,
          0.908680780255339, 0.857541392392623, 0.777353828146545, 0.661131513296524,
          0.512711981039796, 0.357346432815676, 0.224662101978509, 0.121638559000661,
          0.967668081909087, 0.990008068984907, 0.995560889886471, 0.995641991716643,
          0.993624470509278, 0.991059373658537, 0.988040353436121, 0.984673329703723,
          0.981470430737054, 0.977934939728761, 0.971766487949973, 0.961780792273926,
          0.944588362056491, 0.915303175185752, 0.867175365253056, 0.790635307536064,
          0.677656963580601, 0.530729783448463, 0.374404597176352, 0.237203123872316,
          0.127388492872508, 0.973550993968318, 0.99237559671888, 0.996334437474569,
          0.996302569319733, 0.994665042785698, 0.992854350176394, 0.990804628097226,
          0.988299827070781, 0.985427217612243, 0.98180549401774, 0.975726794918091,
          0.966103852460658, 0.949838547130851, 0.92234926803937, 0.877071883537886,
          0.804140728455995, 0.694271895287286, 0.548487335263838, 0.389191763586342,
          0.24733085854494, 0.131770634512774, 0.977644495019607, 0.993809059507753,
          0.996921848401461, 0.996813848508293, 0.995383460235151, 0.993952685083984,
          0.992408034278954, 0.990393195059418, 0.987756840918104, 0.984187864274535,
          0.978346485320431, 0.969132680765175, 0.953701455005333, 0.92763044136048,
          0.884677298385547, 0.814988789377917, 0.708878868603095, 0.565946568832964,
          0.40677396957209, 0.26150417331884, 0.138468877454496, 0.99589669,
          0.99939439, 0.99968965, 0.99946356, 0.99900599, 0.99861604, 0.99832041,
          0.99779148, 0.99668581, 0.9946399, 0.9913872, 0.98672374, 0.97979077,
          0.96855904, 0.94971941, 0.9172498, 0.85796829, 0.74889563, 0.57422822,
          0.35395265, 0.158786622360056, 0.9963443, 0.99946056, 0.99972569,
          0.99951979, 0.99909662, 0.99871978, 0.99844108, 0.9979583, 0.99694158,
          0.99505178, 0.99204825, 0.98775485, 0.98138426, 0.97102475, 0.95351064,
          0.92310744, 0.86661424, 0.76030246, 0.58585433, 0.36085561, 0.160294341407215,
          0.99679414, 0.99952694, 0.99976136, 0.99957681, 0.99919131, 0.99883301,
          0.99857404, 0.99814009, 0.99721897, 0.99549888, 0.99276597, 0.98887133,
          0.98310375, 0.97368874, 0.95762803, 0.92951136, 0.87619312, 0.77320377,
          0.59930763, 0.36898278, 0.162083812814283, 0.9971375, 0.99957757,
          0.99978821, 0.99962071, 0.99926645, 0.99892672, 0.99868509, 0.9982903,
          0.99744714, 0.99586696, 0.99335694, 0.98978824, 0.98451115, 0.97587178,
          0.96101941, 0.93482131, 0.88424374, 0.78427805, 0.61113191, 0.37625625,
          0.163698785477625, 0.99744017, 0.99962217, 0.9998116, 0.99965971,
          0.99933486, 0.99901498, 0.99879044, 0.99843159, 0.99766096, 0.99621214,
          0.99391125, 0.99064648, 0.985825, 0.97791169, 0.9642029, 0.93983665,
          0.89194494, 0.79508552, 0.62293587, 0.38364594, 0.165352911452495,
          0.99771115, 0.99966211, 0.99983233, 0.99969485, 0.99939794, 0.99909887,
          0.9988912, 0.99856572, 0.9978633, 0.996539, 0.9944362, 0.99145779,
          0.98706409, 0.97983722, 0.96722115, 0.94462117, 0.89938678, 0.80574358,
          0.63485152, 0.39124343, 0.167067858821075)


all_years <- c("1950", "1955", "1960", "1965", "1970", "1975",
               "1980", "1985", "1990", "1995", "2000", "2005",
               "2010", "2015", "2020", "2025", "2030", "2035",
               "2040", "2045", "2050")

# Population for males as matrix
pop_m_mat <- matrix(pop_m, nrow = 21, ncol = 21)
colnames(pop_m_mat) <- all_years
rownames(pop_m_mat) <- ages
# Population for females as matrix
pop_f_mat <- matrix(pop_f, nrow = 21, ncol = 21)
colnames(pop_f_mat) <- all_years
rownames(pop_f_mat) <- ages
# Age-specific-fertility-rate for as matrix
asfr_mat <- matrix(asfr, nrow = 7, ncol = 20)
colnames(asfr_mat) <- all_years[-length(all_years)]
rownames(asfr_mat) <- ages_asfr
# Sex ratio at birth as vector
srb_vec <- c(1.058, 1.057, 1.055, 1.055, 1.06, 1.056, 1.056, 1.052, 1.056,
             1.054, 1.054, 1.053, 1.054, 1.053, 1.056, 1.056, 1.056, 1.056,
             1.056, 1.056)

names(srb_vec) <- all_years[-length(all_years)]

# Survival ratio for males as matrix
sr_m_mat <- matrix(sr_m, nrow = 21, ncol = 20)
colnames(sr_m_mat) <- all_years[-length(all_years)]

# Survival ratio for females as matrix
sr_f_mat <- matrix(sr_f, nrow = 21, ncol = 20)
colnames(sr_f_mat) <- all_years[-length(all_years)]

test_that("mig_resid_stock returns correct result", {
  mig_m_correct <- c(1.78993, 51.6027, 29.15413, 7.37114, 21.61505, 47.86478,
                     37.02506, 37.05721, 26.69961, 19.32384, 10.87064, -0.47108, -2.67392,
                     8.26825, 5.10508, -0.53082, 2.64586, -0.00023, -0.00071, -0.00042,
                     0.00036, 3.91902, 53.85321, 28.40815, 4.38183, 15.66065, 60.26311,
                     47.79935, 39.55049, 13.16362, 8.53569, 5.80271, 2.38247, -0.51659,
                     9.95022, 5.9385, 0.30237, 2.92392, 7e-05, -0.00044, 8e-05, -0.00039,
                     -5.85431, 22.22355, 8.03065, -5.01996, -2.09949, 23.38541, 15.9943,
                     9.45501, 0.95993, -5.75358, 0.67884, -3.24031, 0.43973, 4.25945,
                     5.73415, -0.93427, 2.38634, -0.00163, 0.00071, -0.00027, 0.00042,
                     12.90924, 22.93354, 19.34992, 15.80343, 40.11609, 84.50105, 54.48107,
                     26.9198, 8.96688, 7.5537, 0.54448, 6.27858, 6.80778, 9.70358,
                     1.55889, -1.06165, 3.09094, 0.00039, 0.00076, -0.00131, -0.00012,
                     29.21119, 34.70664, 30.62147, 31.31827, 51.80611, 88.70085, 52.6215,
                     23.92796, 17.21723, 4.39468, 10.53791, -0.79311, 9.0829, 8.41258,
                     6.65011, 0.16873, -0.65464, -3e-04, 5e-05, -1e-04, 0.00018, 14.48194,
                     24.2441, 19.89098, 34.54122, 35.77466, 12.23207, 15.27682, 3.4133,
                     11.36489, -0.90259, 4.12285, 2.83248, 5.95876, 5.02652, 3.06679,
                     1.10855, -0.35452, -0.00073, -0.00101, -0.00056, -0.00027, 15.00503,
                     14.9144, 12.99762, 31.14087, 48.81412, 15.74907, 15.14544, 2.65127,
                     0.91571, -6.14148, 1.9181, 8.0029, 6.19014, 6.13341, 2.10405,
                     0.44406, -1.07882, -0.00037, -0.00136, 0.00026, 0.00024, 30.61609,
                     39.45862, 31.31408, 53.78356, 46.60918, 36.02489, 48.99127, 35.70811,
                     26.00023, 9.46973, 11.4602, 13.92493, 12.85525, 15.31055, 8.08295,
                     4.69025, -0.8913, 0.00093, -0.00014, 0.00067, 1e-04, 22.79134,
                     33.66381, 39.26617, 58.06982, 33.55813, 29.94211, 29.39288, 23.77502,
                     18.15304, 9.66927, 0.74205, 9.46166, 12.54064, 6.69344, 0.90515,
                     -1.74026, -3.25923, -0.0015, -0.00154, 0, -0.00015, 33.40944,
                     27.63004, 35.20175, 56.40284, 46.60901, 35.2342, 43.02994, 38.04652,
                     25.21479, 15.38015, 9.17442, 7.03148, 5.93981, 6.27671, 4.91181,
                     1.69561, 1.07822, 0.00065, 0.00027, 2e-05, -0.00047, 35.20631,
                     39.78156, 43.61343, 69.48623, 61.47957, 40.98161, 54.20004, 54.36211,
                     46.10098, 24.36452, 19.0808, 0.64836, 15.04356, 6.28745, 1.68454,
                     1.78516, 4.4904, 0.00039, -0.00027, 0.00037, 0.00043, 25.74073,
                     51.98246, 40.87724, 61.51655, 108.14156, 92.12637, 64.84557,
                     62.10615, 40.50389, 37.25019, 18.91404, 12.68029, 10.36964, 8.03779,
                     1.54402, 2.2409, 11.51357, -0.00034, 0.00038, -0.00024, 0.00016,
                     16.86618, 35.45773, 25.29691, 105.38074, 146.27011, 20.26667,
                     54.91794, 44.19383, 27.08597, 8.12904, 16.25201, 37.92549, 11.8883,
                     33.1429, 1.08892, 2.70842, -2.16821, 0.00189, -0.00178, -0.00119,
                     -5e-05, 37.26188, 16.21746, 12.43047, 63.37355, 114.07611, 109.6321,
                     83.03648, 57.59037, 38.66853, 25.65676, 16.95107, 11.18371, 7.37695,
                     4.86026, 3.20141, 2.11423, 1.39385, 0.00195, -0.00042, 0.00095,
                     -0.00036, 35.96241, 15.72724, 12.05461, 61.48319, 110.66604,
                     106.35737, 80.55508, 55.86794, 37.51901, 24.89228, 16.44513,
                     10.85143, 7.15978, 4.7201, 3.1109, 2.05072, 1.35167, -6e-05,
                     0.00065, -0.00043, -0.00015, 37.13937, 16.24346, 12.45485, 63.48393,
                     114.26793, 109.8159, 83.17144, 57.69732, 38.74399, 25.70411,
                     16.99112, 11.2004, 7.38692, 4.87877, 3.21249, 2.11604, 1.39268,
                     0.00051, 0.00066, 5e-05, 6e-05, 38.72296, 16.92868, 12.98092,
                     66.17035, 119.11119, 114.47417, 86.69744, 60.12598, 40.37813,
                     26.79773, 17.71062, 11.67745, 7.70727, 5.07895, 3.34875, 2.20672,
                     1.45385, 0.00159, -0.00063, -0.00036, -0.00023, 38.71024, 16.92254,
                     12.97225, 66.15279, 119.07559, 114.4426, 86.66769, 60.11337,
                     40.36545, 26.78421, 17.6977, 11.67227, 7.69718, 5.07281, 3.34712,
                     2.20553, 1.45461, -0.00311, 0.0017, -0.00134, 0.00023, 38.75942,
                     16.9499, 12.99493, 66.2629, 119.2639, 114.61801, 86.8193, 60.20745,
                     40.42687, 26.83642, 17.73515, 11.69656, 7.71544, 5.08495, 3.35603,
                     2.20664, 1.46161, -0.00018, -0.00232, 0.00109, -1e-04, 38.84919,
                     16.98616, 13.03085, 66.41678, 119.5424, 114.89039, 87.0109, 60.34099,
                     40.52742, 26.88482, 17.77363, 11.72089, 7.73005, 5.10098, 3.36352,
                     2.21211, 1.4596, -0.00128, 0.00148, 9e-05, 4e-05)

  mig_m_correct <- matrix(mig_m_correct, nrow = 21, ncol = 20)
  colnames(mig_m_correct) <- all_years[-length(all_years)]
  rownames(mig_m_correct) <- ages
  mig_f_correct <- c(113.17294, 95.34564, 39.38204, 18.93728, 35.06867,
                     46.52932, 49.2666, 42.94238, 29.4034, 18.57087, 13.71191, 10.0067,
                     11.50849, 22.50025, 29.00955, 16.82217, 19.0866, 7.008, 2.73785,
                     0.47757, 0.03767, 114.89636, 97.41671, 37.98494, 15.46635, 38.29911,
                     51.15055, 53.64078, 47.62602, 20.97116, 17.19342, 17.1194, 14.47415,
                     12.182, 25.23334, 31.29857, 18.04474, 21.79088, 9.63018, 3.09953,
                     0.63464, 0.04935, 90.23263, 70.19452, 21.59118, 15.01716, 32.49304,
                     29.78558, 28.11914, 24.65858, 13.4947, 7.68482, 13.9277, 10.65373,
                     18.5043, 26.44347, 32.48546, 21.47476, 26.5331, 11.75235, 4.37523,
                     0.73784, 0.06275, 87.45873, 65.87207, 30.06125, 35.73094, 61.96567,
                     69.40305, 41.28898, 25.8574, 11.16104, 16.017, 13.62687, 22.55101,
                     27.64042, 35.00253, 32.3181, 30.18757, 31.16165, 16.01617, 5.83027,
                     1.16625, 0.08665, 90.0433, 59.82639, 40.85119, 43.55191, 71.38232,
                     73.37267, 46.14464, 24.83667, 23.58218, 16.08625, 27.44808, 15.36314,
                     28.91679, 34.69564, 32.52892, 33.0547, 30.70524, 19.84793, 8.05434,
                     1.62285, 0.14305, 70.45879, 44.53593, 25.22668, 44.41858, 60.41144,
                     28.85065, 30.61833, 12.90396, 19.50625, 12.49823, 18.0019, 21.73224,
                     29.14986, 36.63175, 38.74703, 39.06288, 36.26655, 24.87058, 11.6835,
                     2.87612, 0.29408, 65.49059, 33.03733, 23.59719, 30.06664, 51.87299,
                     24.82477, 30.4021, 13.75009, 16.81778, 10.67645, 19.12011, 27.1663,
                     31.4169, 38.5246, 41.33303, 43.85171, 39.16344, 30.2455, 15.39064,
                     4.41864, 0.55283, 76.38913, 54.36693, 35.54828, 49.22688, 66.52346,
                     59.27889, 63.23398, 46.13172, 42.4618, 24.0129, 21.68568, 25.97592,
                     32.33337, 46.0766, 45.52331, 52.26274, 44.29909, 34.37834, 17.51129,
                     5.16723, 0.72299, 68.71302, 47.87678, 42.93321, 54.105, 53.34868,
                     49.34692, 44.90459, 42.22216, 35.6324, 31.6798, 25.61224, 32.39668,
                     33.66166, 37.89778, 45.36883, 48.96906, 50.69815, 40.97165, 20.60835,
                     5.99966, 0.82751, 70.27022, 41.84107, 37.83614, 53.01117, 57.18992,
                     48.00412, 54.58493, 54.24249, 44.60592, 36.30227, 28.17995, 25.43971,
                     29.44627, 37.43619, 47.30059, 59.98545, 60.82249, 47.56603, 22.80022,
                     6.0221, 0.69949, 57.8078, 46.44909, 48.91408, 61.11895, 80.81864,
                     67.92642, 74.08341, 64.23241, 63.25721, 41.15209, 37.85057, 35.64659,
                     42.30244, 40.03094, 48.05561, 61.94085, 76.37618, 54.00946, 27.7877,
                     6.94677, 0.64071, 48.42448, 53.01343, 41.09456, 59.30546, 108.25494,
                     111.1587, 103.01067, 79.7542, 59.06802, 57.46749, 39.2409, 39.57208,
                     45.10012, 44.46262, 45.62773, 63.05435, 86.19569, 70.35971, 34.33981,
                     9.55623, 0.86984, 40.89386, 44.01259, 28.4952, 93.81769, 150.59994,
                     82.38454, 100.05553, 66.20362, 51.47925, 25.46582, 36.40836,
                     46.65738, 37.50044, 53.18551, 55.70568, 65.97926, 75.19138, 75.58003,
                     44.13133, 11.03225, 0.91927, 54.2482, 21.65495, 15.86824, 72.30964,
                     125.33065, 118.54242, 89.60123, 64.32198, 46.0959, 34.51742,
                     29.21954, 32.40938, 38.50735, 47.8148, 62.38537, 71.58288, 78.40854,
                     73.66929, 45.77276, 13.0486, 0.8355, 36.39646, 15.71378, 12.85681,
                     67.96569, 118.27622, 109.5158, 79.87071, 53.33906, 34.49321,
                     22.03458, 14.02299, 8.89904, 5.65194, 3.59177, 2.27953, 1.44727,
                     0.9191, 0.00202, -5e-05, -0.00067, 0, 37.56888, 16.22971, 13.27584,
                     70.18092, 122.1251, 113.07433, 82.46815, 55.0807, 35.60806, 22.75099,
                     14.47923, 9.1897, 5.83368, 3.69983, 2.35029, 1.49443, 0.94224,
                     -0.00131, -0.00176, 0.00112, -6e-04, 39.15, 16.91831, 13.83276,
                     73.15433, 127.28774, 117.85368, 85.95619, 57.40907, 37.11957,
                     23.71591, 15.09269, 9.58071, 6.07959, 3.86466, 2.45348, 1.55403,
                     0.9852, -0.00321, 6e-05, 0.00023, 1e-05, 39.15065, 16.91457,
                     13.8338, 73.13045, 127.25815, 117.82782, 85.94408, 57.39172,
                     37.11233, 23.7121, 15.09005, 9.584, 6.08144, 3.85953, 2.45174,
                     1.55456, 0.98354, 0.0028, -0.00075, 8e-04, -0.00055, 39.21944,
                     16.9344, 13.85223, 73.24795, 127.45332, 118.00857, 86.0759, 57.48318,
                     37.16508, 23.74261, 15.10669, 9.59866, 6.08463, 3.86688, 2.45305,
                     1.56218, 0.98279, 0.00356, 0.00229, 0.00206, -0.00028, 39.31634,
                     16.97581, 13.88592, 73.41229, 127.75319, 118.28703, 86.27359,
                     57.6223, 37.24629, 23.80269, 15.14986, 9.61932, 6.10234, 3.8796,
                     2.46, 1.55982, 0.99505, -0.00423, -0.0011, -0.00049, -3e-05)

  mig_f_correct <- matrix(mig_f_correct, nrow = 21, ncol = 20)
  colnames(mig_f_correct) <- all_years[-length(all_years)]
  rownames(mig_f_correct) <- ages

  mig_res <-
    mig_resid_stock(
      pop_m_mat = pop_m_mat,
      pop_f_mat = pop_f_mat,
      sr_m_mat = sr_m_mat,
      sr_f_mat = sr_f_mat,
      asfr_mat = asfr_mat,
      srb_vec = srb_vec,
      ages = ages,
      ages_asfr = ages_asfr
    )

  mig_resid_cohortet_mig_m <- mig_res$mig_m
  net_mig_m <- mig_res$mig_m
  net_mig_f <- mig_res$mig_f

  expect_equal(
    mig_m_correct,
    net_mig_m,
    tolerance = 0.00001
  )

  expect_equal(
    mig_f_correct,
    net_mig_f,
    tolerance = 0.00001
  )

  # Test that mig_resid, the generic method maker, dispatches
  # correctly
  mig_res <-
    mig_resid(
      pop_m_mat = pop_m_mat,
      pop_f_mat = pop_f_mat,
      sr_m_mat = sr_m_mat,
      sr_f_mat = sr_f_mat,
      asfr_mat = asfr_mat,
      srb_vec = srb_vec,
      ages = ages,
      ages_asfr = ages_asfr,
      method = "stock"
    )

  expect_equal(
    mig_m_correct,
    mig_res$mig_m,
    tolerance = 0.00001
  )

  expect_equal(
    mig_f_correct,
    mig_res$mig_f,
    tolerance = 0.00001
  )
})

test_that("mig_resid_cohort returns correct result", {
  mig_m_correct <- c(27.73458, 40.51021, 18.30131, 14.54682, 34.88883,
                     42.63295, 37.22487, 32.08145, 23.23346, 15.32656, 5.30281, -1.64767,
                     3.0477, 7.2976, 2.53117, 1.39818, 1.71225, -0.00082, -0.00113,
                     -0.00097, 0.00036, 31.00194, 41.23434, 16.42222, 10.0564, 38.11404,
                     54.25162, 43.87177, 26.50207, 10.94592, 7.27485, 4.18649, 0.95766,
                     5.0898, 8.67619, 3.4941, 2.06607, 1.88708, -0.00034, -3e-04,
                     0.00019, -0.00039, 5.19421, 15.16065, 1.50588, -3.56871, 10.68566,
                     19.76765, 12.77817, 5.23331, -2.42587, -2.56336, -1.32459, -1.43951,
                     2.52365, 5.49046, 2.64529, 0.97942, 1.53067, -0.00057, 0.00032,
                     -0.00061, 0.00042, 24.56247, 21.17935, 17.60535, 28.06149, 62.56587,
                     69.76665, 40.86773, 18.03806, 8.33476, 4.09736, 3.51331, 6.80748,
                     8.79816, 6.09645, 0.24488, 1.34924, 1.97445, 0.00095, -0.00082,
                     -0.00297, -0.00012, 46.88525, 32.71567, 31.02412, 41.72148, 70.55654,
                     70.93797, 38.42664, 20.68785, 10.89106, 7.5875, 4.95719, 4.34747,
                     9.27188, 8.21782, 3.80074, -0.31716, -0.41643, -0.00017, -6e-05,
                     -0.00021, 0.00018, 26.72905, 22.09627, 27.26135, 35.27208, 24.10568,
                     13.8049, 9.37909, 7.43184, 5.26207, 1.64098, 3.5528, 4.5669,
                     5.79648, 4.38235, 2.34249, 0.42238, -0.22377, -0.00135, -0.00142,
                     -0.00116, -0.00027, 22.55141, 13.96942, 22.09974, 40.08863, 32.39311,
                     15.49711, 8.92705, 1.79052, -2.63611, -2.12355, 5.06973, 7.3126,
                     6.4725, 4.41229, 1.41184, -0.41608, -0.67278, -0.00136, -0.00084,
                     0.00052, 0.00024, 50.50052, 35.41411, 42.59711, 50.30677, 41.44201,
                     42.63584, 42.48763, 30.97845, 17.82944, 10.56804, 12.90598, 13.75741,
                     14.73921, 12.49419, 7.09175, 2.13444, -0.5509, 0.00053, 0.00058,
                     0.00138, 1e-04, 39.71947, 36.48698, 48.71322, 45.89405, 31.83571,
                     29.7541, 26.67472, 21.05322, 13.98769, 5.24226, 5.1905, 11.27484,
                     9.97354, 4.00311, -0.49588, -2.97662, -1.99285, -0.00229, -0.00125,
                     1e-05, -0.00015, 47.33699, 31.43147, 45.83846, 51.58882, 41.0168,
                     39.22889, 40.6553, 31.74649, 20.39907, 12.36784, 8.20155, 6.61893,
                     6.32454, 5.91294, 3.58704, 1.60292, 0.65272, 0.00066, 0.00024,
                     5e-05, -0.00047, 55.21348, 41.71498, 56.58804, 65.57516, 51.33396,
                     47.69132, 54.41186, 50.38857, 35.38553, 21.87529, 9.95375, 8.02472,
                     10.95858, 4.15716, 1.87955, 3.63329, 2.64854, 4e-05, 0.00016,
                     0.00076, 0.00043, 51.81794, 46.44763, 51.22663, 84.95273, 100.3256,
                     78.64393, 63.61715, 51.449, 39.04144, 28.25565, 15.95878, 11.71475,
                     9.44191, 4.96384, 2.03231, 7.85682, 6.63732, 7e-05, 5e-05, -0.00048,
                     0.00016, 34.64569, 30.38654, 65.376, 125.97127, 83.39818, 37.6575,
                     49.64878, 35.72532, 17.66614, 12.26915, 27.36801, 25.23316, 23.09601,
                     17.61464, 2.02909, 0.22697, -1.23126, -0.00015, -0.00251, -0.00235,
                     -5e-05, 45.46868, 14.32799, 37.92246, 88.82648, 112.02859, 96.49354,
                     70.43648, 48.23902, 32.27328, 21.42061, 14.18917, 9.40481, 6.24662,
                     4.16579, 2.80487, 1.92161, 0.78832, 9e-04, 0.00062, 0.00188,
                     -0.00036, 43.9107, 13.89523, 36.78382, 86.14305, 108.64397, 93.59811,
                     68.32703, 46.79176, 31.3015, 20.76887, 13.75317, 9.11367, 6.05129,
                     4.03284, 2.70922, 1.84941, 0.75625, 0.00043, 6e-05, -0.00082,
                     -0.00015, 45.33636, 14.35297, 37.98272, 88.93813, 112.16416,
                     96.62625, 70.54235, 48.31215, 32.31319, 21.44079, 14.19322, 9.3938,
                     6.23596, 4.15446, 2.78366, 1.89298, 0.77218, 0.00078, 0.00052,
                     9e-05, 6e-05, 47.25714, 14.95834, 39.58804, 92.69935, 116.90911,
                     100.713, 73.5153, 50.33959, 33.67316, 22.34327, 14.78717, 9.78797,
                     6.4914, 4.31764, 2.892, 1.96385, 0.80096, 0.00051, -0.00077,
                     -0.00067, -0.00023, 47.23415, 14.95057, 39.57368, 92.66739, 116.86583,
                     100.67245, 73.48607, 50.31996, 33.6532, 22.32287, 14.77057, 9.77246,
                     6.47507, 4.3052, 2.88149, 1.95379, 0.79343, -0.00066, -6e-05,
                     -0.00248, 0.00023, 47.2911, 14.97529, 39.63905, 92.81208, 117.03936,
                     100.82733, 73.6019, 50.39165, 33.70409, 22.36153, 14.79497, 9.78704,
                     6.48336, 4.3085, 2.87866, 1.94944, 0.79439, -0.0017, -6e-04,
                     0.00199, -1e-04, 47.39403, 15.01112, 39.73308, 93.02437, 117.30745,
                     101.05157, 73.75814, 50.50329, 33.77327, 22.39942, 14.82056,
                     9.80049, 6.49249, 4.31379, 2.87818, 1.9433, 0.7885, 0.00026,
                     0.00109, 0.00016, 4e-05)

  mig_m_correct <- matrix(mig_m_correct, nrow = 21, ncol = 20)
  colnames(mig_m_correct) <- all_years[-length(all_years)]
  rownames(mig_m_correct) <- ages
  mig_f_correct <- c(171.72132, 69.20081, 29.46478, 27.29601, 41.33304,
                     48.61861, 46.88562, 36.86299, 24.5104, 16.56536, 12.26265, 11.3008,
                     18.40401, 29.00225, 27.17509, 23.83314, 19.13202, 8.57065, 3.41816,
                     1.25222, 0.03767, 173.14456, 69.28239, 26.96618, 27.13734, 45.22977,
                     53.07861, 51.38378, 34.8686, 19.45753, 17.57029, 16.29328, 13.92818,
                     20.13441, 31.55144, 28.94144, 26.11712, 22.82359, 10.90765, 3.91939,
                     1.63515, 0.04935, 131.77821, 46.84641, 18.44417, 23.95068, 31.44274,
                     29.28368, 26.73501, 19.36135, 10.77529, 11.05335, 12.6443, 15.23128,
                     24.01143, 32.65724, 31.42784, 31.07521, 27.36889, 13.64066, 5.21967,
                     1.86298, 0.06275, 125.77017, 48.75619, 33.10559, 49.19164, 66.24574,
                     55.88984, 33.95471, 18.7511, 13.81137, 15.11775, 18.5984, 26.10027,
                     33.27825, 36.93669, 36.27914, 39.00073, 33.37685, 18.138, 7.05826,
                     2.89364, 0.08665, 124.69687, 50.99224, 42.43743, 57.81831, 72.91472,
                     60.27166, 35.84118, 24.4968, 20.1137, 22.17903, 21.91343, 22.99397,
                     33.64233, 36.67127, 37.79607, 39.9437, 35.52237, 22.85592, 9.57148,
                     3.94389, 0.14305, 95.95206, 35.28539, 34.99371, 52.7039, 44.92143,
                     29.96825, 21.95259, 16.38255, 16.20701, 15.51274, 20.33159, 26.32446,
                     34.6657, 40.93091, 44.44455, 46.61404, 42.36772, 29.5427, 14.17711,
                     6.80269, 0.29408, 84.49758, 28.57301, 26.93969, 41.15812, 38.55642,
                     27.79777, 22.24223, 15.42823, 13.90226, 15.1324, 23.64304, 30.21525,
                     36.71803, 43.13549, 48.30363, 50.73629, 47.59447, 36.3665, 19.03697,
                     10.23439, 0.55283, 106.17616, 45.31243, 42.53695, 58.10836, 63.21548,
                     61.62342, 55.06241, 44.67217, 33.57915, 23.16572, 24.30273, 30.01971,
                     41.07359, 49.22441, 55.12297, 58.33635, 53.19997, 40.6307, 21.37876,
                     11.7003, 0.72299, 94.77454, 45.71595, 48.68456, 53.93132, 51.5985,
                     47.40512, 43.87312, 39.26111, 34.01322, 29.03585, 29.56796, 33.95843,
                     37.37014, 44.67055, 52.84069, 59.96094, 61.29814, 47.42013, 24.59435,
                     13.30593, 0.82751, 93.04289, 40.0648, 45.55738, 55.29366, 52.84309,
                     51.60834, 54.8247, 49.87627, 40.90281, 32.68978, 27.31674, 28.2085,
                     34.89756, 45.34973, 59.94264, 72.13774, 71.6761, 53.37645, 26.1127,
                     13.11551, 0.69949, 82.35453, 47.89851, 55.1597, 71.19899, 74.7032,
                     71.44286, 69.70228, 64.36625, 52.79878, 40.06025, 37.43269, 40.02258,
                     42.81333, 46.9441, 61.0737, 82.09279, 84.68009, 60.95627, 30.57027,
                     14.65609, 0.64071, 75.86696, 47.23318, 50.31059, 84.0184, 110.13033,
                     107.6456, 92.00237, 69.99775, 58.8676, 48.95953, 40.07877, 43.37739,
                     46.43919, 47.73597, 59.95542, 87.81067, 100.64409, 76.35063,
                     37.87123, 19.62123, 0.86984, 63.53631, 36.36446, 61.26959, 122.49745,
                     116.84188, 91.59961, 83.55655, 59.22642, 38.7796, 31.27957, 42.16363,
                     42.97337, 46.92849, 57.43028, 66.5292, 81.90888, 96.1467, 86.39627,
                     46.46163, 22.18324, 0.91927, 65.72608, 18.80751, 44.15891, 99.02303,
                     122.26172, 104.4229, 77.28825, 55.50694, 40.58714, 32.16736,
                     31.23129, 36.17621, 44.53795, 57.98596, 72.80986, 86.21005, 95.52677,
                     84.84706, 48.64233, 25.51671, 0.8355, 44.33063, 14.28867, 40.42136,
                     93.15949, 113.96336, 94.76476, 66.66796, 43.97429, 28.32219,
                     18.08882, 11.52124, 7.33432, 4.67976, 2.99435, 1.92512, 1.25171,
                     0.4973, 0.00114, -0.00059, -0.00112, 0, 45.75479, 14.75587, 41.73772,
                     96.18904, 117.66354, 97.83964, 68.83476, 45.39983, 29.23507,
                     18.67232, 11.89181, 7.56762, 4.82169, 3.08077, 1.98117, 1.2838,
                     0.50533, -0.0019, -0.00022, 0.00186, -6e-04, 47.67406, 15.37836,
                     43.50211, 100.25453, 122.63088, 101.97003, 71.74004, 47.31691,
                     30.47038, 19.45852, 12.39102, 7.88304, 5.02407, 3.21185, 2.05953,
                     1.33219, 0.52443, -0.00179, 0.00022, 0.00037, 1e-05, 47.66587,
                     15.37671, 43.4898, 100.22459, 122.59798, 101.94587, 71.72074,
                     47.30032, 30.46052, 19.45084, 12.38686, 7.8812, 5.01803, 3.20396,
                     2.0544, 1.32689, 0.52455, 0.0011, 0.00017, 0.0013, -0.00055,
                     47.73854, 15.39557, 43.55698, 100.37807, 122.78123, 102.09737,
                     71.82816, 47.36847, 30.49816, 19.4703, 12.39838, 7.88605, 5.01926,
                     3.20424, 2.05479, 1.32602, 0.52231, 0.00345, 0.00312, 0.00333,
                     -0.00028, 47.85075, 15.43288, 43.65529, 100.60758, 123.06601,
                     102.33091, 71.99256, 47.47491, 30.56507, 19.51809, 12.42642,
                     7.90143, 5.03076, 3.21033, 2.05311, 1.32706, 0.52226, -0.00305,
                     -0.00108, -0.00079, -3e-05)

  mig_f_correct <- matrix(mig_f_correct, nrow = 21, ncol = 20)
  colnames(mig_f_correct) <- all_years[-length(all_years)]
  rownames(mig_f_correct) <- ages
  mig_res <-
    mig_resid_cohort(
      pop_m_mat = pop_m_mat,
      pop_f_mat = pop_f_mat,
      sr_m_mat = sr_m_mat,
      sr_f_mat = sr_f_mat,
      asfr_mat = asfr_mat,
      srb_vec = srb_vec,
      ages = ages,
      ages_asfr = ages_asfr
    )

  net_mig_m <- mig_res$mig_m
  net_mig_f <- mig_res$mig_f

  expect_equal(
    mig_m_correct,
    net_mig_m,
    tolerance = 0.00001
  )

  expect_equal(
    mig_f_correct,
    net_mig_f,
    tolerance = 0.00001
  )

  # Test that mig_resid, the generic method maker, dispatches
  # correctly
  mig_res <-
    mig_resid(
      pop_m_mat = pop_m_mat,
      pop_f_mat = pop_f_mat,
      sr_m_mat = sr_m_mat,
      sr_f_mat = sr_f_mat,
      asfr_mat = asfr_mat,
      srb_vec = srb_vec,
      ages = ages,
      ages_asfr = ages_asfr,
      method = "cohort"
    )

  expect_equal(
    mig_m_correct,
    mig_res$mig_m,
    tolerance = 0.00001
  )

  expect_equal(
    mig_f_correct,
    mig_res$mig_f,
    tolerance = 0.00001
  )

})

test_that("mig_resid_time returns correct result", {
  mig_m_correct <- c(3.57986, 99.65372, -40.94163, 55.48348, -11.79757,
                     107.42501, -32.40904, 106.17661, -51.15632, 88.57047, -63.32935,
                     58.46884, -58.32987, 66.83013, -43.57047, 30.01887, -12.6245,
                     5.60441, -1.70124, 0.31632, -0.02811, 7.83804, 99.91546, -42.78431,
                     51.36112, -19.64384, 140.01221, -43.26549, 121.93972, -93.89844,
                     108.82847, -93.13048, 92.26143, -84.64086, 92.79118, -62.56498,
                     45.21822, -21.29333, 9.70725, -3.01062, 0.55768, -0.05411, -11.70861,
                     56.09678, -39.88271, 29.68615, -33.65766, 80.16093, -47.55351,
                     66.01483, -63.17458, 50.26726, -47.05718, 37.78575, -33.37064,
                     37.31341, -18.52716, 11.37805, -2.13017, 0.98684, -0.31395, 0.06173,
                     -0.00545, 25.81848, 20.1612, 18.59079, 13.09179, 67.25141, 102.294,
                     7.44392, 46.46533, -27.88927, 42.37, -39.73615, 49.9545, -31.75995,
                     46.77483, -34.44289, 22.72777, -7.74683, 3.68891, -1.20302, 0.23383,
                     -0.02449, 58.42238, 11.21458, 50.05513, 12.81047, 90.92288, 87.2113,
                     18.6716, 29.35435, 5.48776, 3.4229, 17.77494, -18.35528, 34.90416,
                     -13.45379, 24.15557, -17.18309, 9.3178, -4.50289, 1.52466, -0.32112,
                     0.03645, 28.96388, 19.61458, 20.20595, 48.9592, 23.01802, 1.62298,
                     28.94199, -21.87239, 44.33117, -45.24367, 51.97199, -43.51297,
                     51.82676, -35.33902, 34.99221, -23.57248, 14.15059, -7.09943,
                     2.59036, -0.60437, 0.07855, 30.01007, -0.11384, 26.1089, 36.25809,
                     61.62329, -29.72231, 59.82581, -54.1009, 55.38825, -66.76115,
                     68.73234, -49.5579, 58.29272, -39.41622, 36.8872, -26.76289,
                     15.05939, -7.7641, 2.91885, -0.71425, 0.09881, 61.23217, 17.79884,
                     44.85068, 62.84587, 30.75645, 41.47507, 56.75796, 15.0607, 37.0793,
                     -17.61587, 40.11802, -10.6572, 35.66448, -1.32598, 17.28165,
                     -3.79338, 0.69403, -0.35818, 0.13246, -0.03007, 0.00411, 45.58267,
                     21.81187, 56.74185, 59.53667, 7.894, 52.0334, 7.07303, 40.53058,
                     -3.8352, 23.12251, -21.15027, 39.33642, -11.98653, 24.25817,
                     -18.97574, 11.33351, -14.10486, 7.41969, -2.78841, 0.65864, -0.08174,
                     66.81888, -11.479, 81.87295, 31.09995, 62.26184, 8.49639, 77.60729,
                     -1.01575, 51.43662, -20.05123, 38.02601, -22.79652, 33.52299,
                     -18.2014, 25.61825, -16.98399, 13.75844, -7.35106, 2.73971, -0.61343,
                     0.06882, 70.41263, 9.22234, 78.01073, 61.10026, 62.10215, 20.11483,
                     88.37181, 20.82055, 71.53366, -22.01225, 59.78863, -56.8406,
                     84.38389, -65.77126, 61.67731, -47.07547, 42.81652, -24.34878,
                     9.67109, -2.27481, 0.25971, 51.48146, 52.52999, 29.25547, 93.82267,
                     122.80642, 61.93363, 68.01001, 56.52959, 24.86138, 49.89177,
                     -11.24948, 36.31952, -14.13566, 29.32709, -23.3541, 24.2035,
                     4.82076, -2.93691, 1.24605, -0.31123, 0.03684, 33.73236, 37.20561,
                     13.40764, 197.37106, 95.77215, -54.90302, 164.54993, -75.48652,
                     129.21556, -111.77482, 142.64921, -63.55418, 85.10507, -14.15648,
                     15.10887, -7.59222, 1.53814, -0.96751, 0.42211, -0.11037, 0.01289,
                     74.52377, -42.04242, 66.88309, 59.94396, 168.38171, 51.44499,
                     114.79474, 0.82805, 76.51362, -24.52861, 58.08697, -34.45751,
                     48.06876, -35.86556, 39.3394, -29.92661, 26.24959, -16.87001,
                     7.55462, -1.95418, 0.22853, 71.92483, -40.41516, 64.50714, 58.51642,
                     162.93139, 50.25699, 111.01355, 1.12741, 73.91626, -23.5569,
                     56.15489, -33.36266, 46.68624, -35.08163, 38.71963, -29.96681,
                     26.63369, -17.6524, 8.35314, -2.25185, 0.25094, 74.27873, -41.74273,
                     66.63724, 60.3818, 168.25961, 51.81406, 114.67885, 1.09357, 76.39933,
                     -24.45571, 58.16447, -34.7478, 48.58907, -36.80465, 40.77918,
                     -32.0867, 28.89957, -19.69283, 9.65218, -2.68617, 0.30425, 77.44592,
                     -43.54298, 69.49081, 62.89759, 175.42439, 53.94745, 119.59145,
                     1.02253, 79.73797, -25.62967, 60.78783, -36.45913, 50.97697,
                     -38.86918, 43.18508, -34.39959, 31.3094, -21.79401, 10.97797,
                     -3.13574, 0.35887, 77.42049, -43.5345, 69.46651, 62.88201, 175.35984,
                     53.91552, 119.55284, 1.00728, 79.72746, -25.68711, 60.83983,
                     -36.59873, 51.1657, -39.2202, 43.69696, -35.18146, 32.33392,
                     -22.95991, 11.86947, -3.47574, 0.40341, 77.51884, -43.58197,
                     69.56056, 63.00423, 175.60672, 53.9904, 119.77154, 0.95195, 79.90514,
                     -25.79565, 61.04091, -36.81761, 51.48077, -39.63975, 44.2773,
                     -35.99464, 33.37814, -24.1187, 12.75787, -3.81698, 0.44727, 77.69838,
                     -43.69223, 69.74367, 63.12556, 176.0358, 54.08017, 120.0562,
                     0.91213, 80.14567, -25.97083, 61.30845, -37.09543, 51.84036,
                     -40.08197, 44.86328, -36.7846, 34.37119, -25.24686, 13.66052,
                     -4.17882, 0.49531)

  mig_m_correct <- matrix(mig_m_correct, nrow = 21, ncol = 20)
  colnames(mig_m_correct) <- all_years[-length(all_years)]
  rownames(mig_m_correct) <- ages
  mig_f_correct <- c(226.34587, -21.03743, 99.34158, -59.68098, 128.43541,
                     -31.83782, 129.37808, -38.94365, 96.19692, -54.63552, 78.96184,
                     -52.98591, 70.21102, -13.67057, 68.3376, -10.37708, 43.47334,
                     -1.96874, 5.94896, 0.08986, 0.06791, 229.79272, -22.2854, 97.83858,
                     -65.3942, 140.6862, -35.02737, 141.35666, -41.71922, 82.17617,
                     -44.39139, 76.33322, -42.06728, 62.17078, -2.18847, 64.28074,
                     -6.38475, 46.9424, 1.39814, 5.85004, 0.38803, 0.06587, 180.46527,
                     -31.50144, 74.17191, -43.13634, 107.36583, -45.53763, 100.6802,
                     -48.5821, 74.01405, -55.84846, 81.03815, -54.48682, 86.34369,
                     -21.04848, 81.42848, -12.10172, 59.62805, -0.03424, 8.75937,
                     0.1017, 0.11662, 174.91745, -36.12133, 95.74522, -23.17129, 146.74971,
                     -5.25186, 87.71909, -33.86766, 55.22193, -21.29545, 47.61263,
                     0.34777, 54.96362, 22.41355, 46.82844, 27.99107, 46.69268, 12.94954,
                     8.16574, 1.00606, 0.08288, 180.08659, -54.22995, 135.27724, -46.80011,
                     188.9421, -39.17015, 130.73753, -78.26877, 123.44787, -87.46615,
                     138.84178, -100.3924, 150.00507, -61.75955, 114.82538, -14.89245,
                     69.95712, 10.09535, 13.27371, 0.9982, 0.19323, 140.91759, -47.67655,
                     97.62607, -7.88183, 128.60904, -69.04877, 129.14198, -100.845,
                     137.53368, -108.64212, 140.60393, -89.87068, 141.30083, -51.40259,
                     119.49946, -7.94956, 77.22602, 15.83419, 18.71344, 2.40716, 0.35231,
                     130.98119, -61.71714, 108.38262, -47.43374, 150.70267, -99.22162,
                     158.62808, -128.48872, 159.52458, -134.13995, 167.85745, -105.55361,
                     160.89369, -66.11241, 137.39144, -13.17114, 86.31283, 21.31403,
                     24.26718, 4.3138, 0.6684, 152.77826, -40.85853, 111.64929, -12.45094,
                     145.38578, -25.23621, 151.3826, -56.83284, 140.70462, -89.39581,
                     129.96786, -72.24991, 132.10336, -26.19917, 112.96646, 20.16201,
                     76.06919, 33.08703, 24.50372, 5.55528, 0.86054, 137.42604, -39.03255,
                     124.61807, -15.60318, 122.16491, -22.1548, 111.67987, -25.51638,
                     96.30265, -30.70097, 80.97867, -12.68977, 79.19821, 4.49945,
                     86.94233, 32.12403, 81.00851, 42.82143, 27.09211, 6.49117, 0.94241,
                     140.54044, -54.61806, 129.96018, -23.19295, 137.38444, -39.90848,
                     148.54399, -37.60949, 126.07173, -50.42457, 105.20778, -49.82456,
                     105.59489, -20.5531, 112.06158, 34.20249, 99.57058, 45.89647,
                     30.00189, 5.719, 0.75003, 115.6156, -21.21824, 118.93427, 3.91088,
                     157.75563, -20.25487, 168.14122, -36.72928, 162.4751, -76.17328,
                     149.50788, -71.98913, 152.27235, -58.30508, 146.11024, 10.30236,
                     145.94115, 33.19314, 43.71394, 4.07268, 0.78603, 96.84895, 10.14562,
                     72.08853, 46.83655, 169.97193, 53.86512, 152.80042, 9.04991,
                     109.25382, 8.09186, 70.61839, 11.22476, 79.59747, 16.06943, 77.32044,
                     64.97643, 128.35964, 72.59512, 41.49966, 9.2686, 0.55896, 81.78773,
                     6.86104, 50.15451, 137.66631, 164.26802, 1.67485, 198.45162,
                     -63.72246, 165.75235, -111.80493, 181.90779, -82.42705, 153.29327,
                     -35.01891, 142.12547, 17.66964, 138.11523, 75.4056, 58.91542,
                     7.4929, 0.85119, 108.4964, -64.5148, 96.05268, 48.87263, 202.01429,
                     36.29219, 143.1858, -13.16628, 105.19687, -34.49864, 92.1907,
                     -24.52627, 100.40544, 2.49046, 122.56748, 43.27463, 126.14061,
                     75.94973, 60.65114, 10.23667, 0.25354, 72.79292, -41.32127, 67.02206,
                     68.94527, 167.6757, 51.58795, 108.2401, -1.32292, 70.30496, -25.85896,
                     53.68222, -35.17143, 45.76452, -37.14211, 39.83364, -33.64286,
                     30.70271, -22.98908, 13.20089, -4.67383, 0.74213, 75.13776, -42.63781,
                     69.17778, 71.21728, 173.09725, 53.273, 111.74636, -1.3568, 72.56877,
                     -26.7077, 55.45379, -36.39535, 47.38519, -38.61253, 41.51805,
                     -35.33676, 32.50783, -24.7184, 14.47786, -5.22219, 0.83588, 78.3,
                     -44.42634, 72.08125, 74.25792, 180.37762, 55.54025, 116.45133,
                     -1.4166, 75.65179, -27.87946, 57.86316, -38.05781, 49.57395,
                     -40.54027, 43.72946, -37.53897, 34.86178, -26.96167, 16.15845,
                     -5.96174, 0.96633, 78.30129, -44.43907, 72.09727, 74.19099, 180.37973,
                     55.46951, 116.49158, -1.50897, 75.72978, -27.99259, 57.98673,
                     -38.22659, 49.79739, -40.8768, 44.18688, -38.19772, 35.74318,
                     -28.02699, 17.12668, -6.44242, 1.05352, 78.43888, -44.54044,
                     72.23651, 74.28396, 180.67209, 55.52301, 116.69596, -1.54657,
                     75.87311, -28.10049, 58.14277, -38.4016, 50.02653, -41.18777,
                     44.61947, -38.81065, 36.58255, -29.07913, 18.11902, -6.94716,
                     1.14818, 78.63268, -44.6545, 72.41884, 74.42782, 181.12336, 55.61392,
                     116.99493, -1.58252, 76.07173, -28.20305, 58.34585, -38.60881,
                     50.31406, -41.54039, 45.09875, -39.4816, 37.49933, -30.22331,
                     19.18511, -7.50703, 1.25413)

  mig_f_correct <- matrix(mig_f_correct, nrow = 21, ncol = 20)
  colnames(mig_f_correct) <- all_years[-length(all_years)]
  rownames(mig_f_correct) <- ages
  mig_res <-
    mig_resid_time(
      pop_m_mat = pop_m_mat,
      pop_f_mat = pop_f_mat,
      sr_m_mat = sr_m_mat,
      sr_f_mat = sr_f_mat,
      asfr_mat = asfr_mat,
      srb_vec = srb_vec,
      ages = ages,
      ages_asfr = ages_asfr
    )

  net_mig_m <- mig_res$mig_m
  net_mig_f <- mig_res$mig_f

  expect_equal(
    mig_m_correct,
    net_mig_m,
    tolerance = 0.00001
  )

  expect_equal(
    mig_f_correct,
    net_mig_f,
    tolerance = 0.00001
  )

  # Test that mig_resid, the generic method maker, dispatches
  # correctly
  mig_res <-
    mig_resid(
      pop_m_mat = pop_m_mat,
      pop_f_mat = pop_f_mat,
      sr_m_mat = sr_m_mat,
      sr_f_mat = sr_f_mat,
      asfr_mat = asfr_mat,
      srb_vec = srb_vec,
      ages = ages,
      ages_asfr = ages_asfr,
      method = "time"
    )

  expect_equal(
    mig_m_correct,
    mig_res$mig_m,
    tolerance = 0.00001
  )

  expect_equal(
    mig_f_correct,
    mig_res$mig_f,
    tolerance = 0.00001
  )
})

test_that("all mig_resid methods throw warnings when data is trimmed", {

  all_funs <- list(mig_resid_stock, mig_resid_cohort, mig_resid_time)

  for (fun in all_funs) {
    expect_output(
      fun(
        pop_m_mat = pop_m_mat,
        pop_f_mat = pop_f_mat,
        sr_m_mat = sr_m_mat,
        sr_f_mat = sr_f_mat,
        asfr_mat = asfr_mat,
        srb_vec = srb_vec[1:16],
        ages = ages,
        ages_asfr = ages_asfr,
        verbose = TRUE
      ),
      regexp = "Years 2035, 2040, 2045, 2050 have been trimmed from all the data"
    )
  }

})

test_that("all mig_resid methods throw errors when ages do not begin at zero ", {

  all_funs <- list(mig_resid_stock, mig_resid_cohort, mig_resid_time)
  ages[1] <- 1

  for (fun in all_funs) {
    # when age is supplied
    expect_error(
      fun(
        pop_m_mat = pop_m_mat,
        pop_f_mat = pop_f_mat,
        sr_m_mat = sr_m_mat,
        sr_f_mat = sr_f_mat,
        asfr_mat = asfr_mat,
        srb_vec = srb_vec,
        ages = ages,
        ages_asfr = ages_asfr,
        verbose = TRUE
      ),
      regexp = "Ages must begin at zero. Ages currently begin at 1"
    )
  }

  # TODO we should add a test checking that this passes alright when ages
  # is not provided but inferred from the matrices
})
timriffe/DemoTools documentation built on Jan. 28, 2024, 5:13 a.m.