optimum-mbp

library(telemetR)
fish <- telemetR::fish
receivers <- telemetR::receivers
detects <- telemetR::filtered_detections
local_tz <- "America/Los_Angeles"

fish <- format_org(fish, 
                   var_Id = "TagCode", 
                   var_release = "Release_Date",
                   var_tag_life = "TagLife",
                   var_ping_rate = "PRI",
                   local_time_zone = local_tz,
                   time_format = "%Y-%m-%d %H:%M:%S")
receivers <- format_receivers(receivers,
                              var_receiver_serial = "receiver_serial_number",
                              var_receiver_make = "receiver_make",
                              var_receiver_deploy = "receiver_start",
                              var_receiver_retrieve = "receiver_end",
                              local_time_zone = local_tz,
                              time_format = "%m/%d/%Y %H:%M:%S")
detects <- format_detects(detects,
                          var_Id = "Tag_Code",
                          var_datetime_local = "DateTime_Local",
                          var_receiver_serial = "ReceiverSN",
                          local_time_zone = local_tz,
                          time_format = "%Y-%m-%d %H:%M:%S")
# Build a new df which uses a range of blanking times to reprocess the original
# detection data (this is a slow process, especially for large datasets or
# large sequences of n)

blanked <- blanking_event(detects,
                     #Each general location is distinct enough to be a site
                     var_site = "receiver_general_location",
                     #Group the data by species, run, and origin (optional)
                     var_groups = "fish_type",
                     var_Id = "Tag_Code",
                     var_datetime = "DateTime_Local",
                     var_ping_rate = "tag_pulse_rate_interval_nominal",
                     n_val = seq(1,1000,5),
                     #Pulse rates for these tags are in seconds
                     time_unit = "secs"
                     )
head(blanked)
#> # A tibble: 6 × 9
#>   fish_type     Tag_Code mbp_n event_change receiver_general_location
#>   <chr>         <chr>    <dbl>        <int> <chr>                    
#> 1 MRH Steelhead 70A3         3            0 Grant_Line_DS            
#> 2 MRH Steelhead 70A3         3            1 Grant_Line_DS            
#> 3 MRH Steelhead 70A3         3            2 Grant_Line_DS            
#> 4 MRH Steelhead 70A3         3            3 Grant_Line_DS            
#> 5 MRH Steelhead 70A3         3            4 Grant_Line_DS            
#> 6 MRH Steelhead 70A3         3            5 Grant_Line_DS            
#> # ℹ 4 more variables: start_time <dttm>, end_time <dttm>, n_det <int>,
#> #   duration <dbl>

# This function compares the durations of the events created above. The function
# collects all the events for each given blanking period and compares each
# event duration to a sequence of given times. It then calculates the proportion
# of events which are longer than each given time for each blanking period for
# each organism group

compared <- duration_compare(blanked, 
                             var_groups = "fish_type", 
                             time_seq = seq(1,10000,10))
#> 
t = 1
t = 11
t = 21
t = 31
t = 41
t = 51
t = 61
t = 71
t = 81
t = 91
t = 101
t = 111
t = 121
t = 131
t = 141
t = 151
t = 161
t = 171
t = 181
t = 191
t = 201
t = 211
t = 221
t = 231
t = 241
t = 251
t = 261
t = 271
t = 281
t = 291
t = 301
t = 311
t = 321
t = 331
t = 341
t = 351
t = 361
t = 371
t = 381
t = 391
t = 401
t = 411
t = 421
t = 431
t = 441
t = 451
t = 461
t = 471
t = 481
t = 491
t = 501
t = 511
t = 521
t = 531
t = 541
t = 551
t = 561
t = 571
t = 581
t = 591
t = 601
t = 611
t = 621
t = 631
t = 641
t = 651
t = 661
t = 671
t = 681
t = 691
t = 701
t = 711
t = 721
t = 731
t = 741
t = 751
t = 761
t = 771
t = 781
t = 791
t = 801
t = 811
t = 821
t = 831
t = 841
t = 851
t = 861
t = 871
t = 881
t = 891
t = 901
t = 911
t = 921
t = 931
t = 941
t = 951
t = 961
t = 971
t = 981
t = 991
t = 1001
t = 1011
t = 1021
t = 1031
t = 1041
t = 1051
t = 1061
t = 1071
t = 1081
t = 1091
t = 1101
t = 1111
t = 1121
t = 1131
t = 1141
t = 1151
t = 1161
t = 1171
t = 1181
t = 1191
t = 1201
t = 1211
t = 1221
t = 1231
t = 1241
t = 1251
t = 1261
t = 1271
t = 1281
t = 1291
t = 1301
t = 1311
t = 1321
t = 1331
t = 1341
t = 1351
t = 1361
t = 1371
t = 1381
t = 1391
t = 1401
t = 1411
t = 1421
t = 1431
t = 1441
t = 1451
t = 1461
t = 1471
t = 1481
t = 1491
t = 1501
t = 1511
t = 1521
t = 1531
t = 1541
t = 1551
t = 1561
t = 1571
t = 1581
t = 1591
t = 1601
t = 1611
t = 1621
t = 1631
t = 1641
t = 1651
t = 1661
t = 1671
t = 1681
t = 1691
t = 1701
t = 1711
t = 1721
t = 1731
t = 1741
t = 1751
t = 1761
t = 1771
t = 1781
t = 1791
t = 1801
t = 1811
t = 1821
t = 1831
t = 1841
t = 1851
t = 1861
t = 1871
t = 1881
t = 1891
t = 1901
t = 1911
t = 1921
t = 1931
t = 1941
t = 1951
t = 1961
t = 1971
t = 1981
t = 1991
t = 2001
t = 2011
t = 2021
t = 2031
t = 2041
t = 2051
t = 2061
t = 2071
t = 2081
t = 2091
t = 2101
t = 2111
t = 2121
t = 2131
t = 2141
t = 2151
t = 2161
t = 2171
t = 2181
t = 2191
t = 2201
t = 2211
t = 2221
t = 2231
t = 2241
t = 2251
t = 2261
t = 2271
t = 2281
t = 2291
t = 2301
t = 2311
t = 2321
t = 2331
t = 2341
t = 2351
t = 2361
t = 2371
t = 2381
t = 2391
t = 2401
t = 2411
t = 2421
t = 2431
t = 2441
t = 2451
t = 2461
t = 2471
t = 2481
t = 2491
t = 2501
t = 2511
t = 2521
t = 2531
t = 2541
t = 2551
t = 2561
t = 2571
t = 2581
t = 2591
t = 2601
t = 2611
t = 2621
t = 2631
t = 2641
t = 2651
t = 2661
t = 2671
t = 2681
t = 2691
t = 2701
t = 2711
t = 2721
t = 2731
t = 2741
t = 2751
t = 2761
t = 2771
t = 2781
t = 2791
t = 2801
t = 2811
t = 2821
t = 2831
t = 2841
t = 2851
t = 2861
t = 2871
t = 2881
t = 2891
t = 2901
t = 2911
t = 2921
t = 2931
t = 2941
t = 2951
t = 2961
t = 2971
t = 2981
t = 2991
t = 3001
t = 3011
t = 3021
t = 3031
t = 3041
t = 3051
t = 3061
t = 3071
t = 3081
t = 3091
t = 3101
t = 3111
t = 3121
t = 3131
t = 3141
t = 3151
t = 3161
t = 3171
t = 3181
t = 3191
t = 3201
t = 3211
t = 3221
t = 3231
t = 3241
t = 3251
t = 3261
t = 3271
t = 3281
t = 3291
t = 3301
t = 3311
t = 3321
t = 3331
t = 3341
t = 3351
t = 3361
t = 3371
t = 3381
t = 3391
t = 3401
t = 3411
t = 3421
t = 3431
t = 3441
t = 3451
t = 3461
t = 3471
t = 3481
t = 3491
t = 3501
t = 3511
t = 3521
t = 3531
t = 3541
t = 3551
t = 3561
t = 3571
t = 3581
t = 3591
t = 3601
t = 3611
t = 3621
t = 3631
t = 3641
t = 3651
t = 3661
t = 3671
t = 3681
t = 3691
t = 3701
t = 3711
t = 3721
t = 3731
t = 3741
t = 3751
t = 3761
t = 3771
t = 3781
t = 3791
t = 3801
t = 3811
t = 3821
t = 3831
t = 3841
t = 3851
t = 3861
t = 3871
t = 3881
t = 3891
t = 3901
t = 3911
t = 3921
t = 3931
t = 3941
t = 3951
t = 3961
t = 3971
t = 3981
t = 3991
t = 4001
t = 4011
t = 4021
t = 4031
t = 4041
t = 4051
t = 4061
t = 4071
t = 4081
t = 4091
t = 4101
t = 4111
t = 4121
t = 4131
t = 4141
t = 4151
t = 4161
t = 4171
t = 4181
t = 4191
t = 4201
t = 4211
t = 4221
t = 4231
t = 4241
t = 4251
t = 4261
t = 4271
t = 4281
t = 4291
t = 4301
t = 4311
t = 4321
t = 4331
t = 4341
t = 4351
t = 4361
t = 4371
t = 4381
t = 4391
t = 4401
t = 4411
t = 4421
t = 4431
t = 4441
t = 4451
t = 4461
t = 4471
t = 4481
t = 4491
t = 4501
t = 4511
t = 4521
t = 4531
t = 4541
t = 4551
t = 4561
t = 4571
t = 4581
t = 4591
t = 4601
t = 4611
t = 4621
t = 4631
t = 4641
t = 4651
t = 4661
t = 4671
t = 4681
t = 4691
t = 4701
t = 4711
t = 4721
t = 4731
t = 4741
t = 4751
t = 4761
t = 4771
t = 4781
t = 4791
t = 4801
t = 4811
t = 4821
t = 4831
t = 4841
t = 4851
t = 4861
t = 4871
t = 4881
t = 4891
t = 4901
t = 4911
t = 4921
t = 4931
t = 4941
t = 4951
t = 4961
t = 4971
t = 4981
t = 4991
t = 5001
t = 5011
t = 5021
t = 5031
t = 5041
t = 5051
t = 5061
t = 5071
t = 5081
t = 5091
t = 5101
t = 5111
t = 5121
t = 5131
t = 5141
t = 5151
t = 5161
t = 5171
t = 5181
t = 5191
t = 5201
t = 5211
t = 5221
t = 5231
t = 5241
t = 5251
t = 5261
t = 5271
t = 5281
t = 5291
t = 5301
t = 5311
t = 5321
t = 5331
t = 5341
t = 5351
t = 5361
t = 5371
t = 5381
t = 5391
t = 5401
t = 5411
t = 5421
t = 5431
t = 5441
t = 5451
t = 5461
t = 5471
t = 5481
t = 5491
t = 5501
t = 5511
t = 5521
t = 5531
t = 5541
t = 5551
t = 5561
t = 5571
t = 5581
t = 5591
t = 5601
t = 5611
t = 5621
t = 5631
t = 5641
t = 5651
t = 5661
t = 5671
t = 5681
t = 5691
t = 5701
t = 5711
t = 5721
t = 5731
t = 5741
t = 5751
t = 5761
t = 5771
t = 5781
t = 5791
t = 5801
t = 5811
t = 5821
t = 5831
t = 5841
t = 5851
t = 5861
t = 5871
t = 5881
t = 5891
t = 5901
t = 5911
t = 5921
t = 5931
t = 5941
t = 5951
t = 5961
t = 5971
t = 5981
t = 5991
t = 6001
t = 6011
t = 6021
t = 6031
t = 6041
t = 6051
t = 6061
t = 6071
t = 6081
t = 6091
t = 6101
t = 6111
t = 6121
t = 6131
t = 6141
t = 6151
t = 6161
t = 6171
t = 6181
t = 6191
t = 6201
t = 6211
t = 6221
t = 6231
t = 6241
t = 6251
t = 6261
t = 6271
t = 6281
t = 6291
t = 6301
t = 6311
t = 6321
t = 6331
t = 6341
t = 6351
t = 6361
t = 6371
t = 6381
t = 6391
t = 6401
t = 6411
t = 6421
t = 6431
t = 6441
t = 6451
t = 6461
t = 6471
t = 6481
t = 6491
t = 6501
t = 6511
t = 6521
t = 6531
t = 6541
t = 6551
t = 6561
t = 6571
t = 6581
t = 6591
t = 6601
t = 6611
t = 6621
t = 6631
t = 6641
t = 6651
t = 6661
t = 6671
t = 6681
t = 6691
t = 6701
t = 6711
t = 6721
t = 6731
t = 6741
t = 6751
t = 6761
t = 6771
t = 6781
t = 6791
t = 6801
t = 6811
t = 6821
t = 6831
t = 6841
t = 6851
t = 6861
t = 6871
t = 6881
t = 6891
t = 6901
t = 6911
t = 6921
t = 6931
t = 6941
t = 6951
t = 6961
t = 6971
t = 6981
t = 6991
t = 7001
t = 7011
t = 7021
t = 7031
t = 7041
t = 7051
t = 7061
t = 7071
t = 7081
t = 7091
t = 7101
t = 7111
t = 7121
t = 7131
t = 7141
t = 7151
t = 7161
t = 7171
t = 7181
t = 7191
t = 7201
t = 7211
t = 7221
t = 7231
t = 7241
t = 7251
t = 7261
t = 7271
t = 7281
t = 7291
t = 7301
t = 7311
t = 7321
t = 7331
t = 7341
t = 7351
t = 7361
t = 7371
t = 7381
t = 7391
t = 7401
t = 7411
t = 7421
t = 7431
t = 7441
t = 7451
t = 7461
t = 7471
t = 7481
t = 7491
t = 7501
t = 7511
t = 7521
t = 7531
t = 7541
t = 7551
t = 7561
t = 7571
t = 7581
t = 7591
t = 7601
t = 7611
t = 7621
t = 7631
t = 7641
t = 7651
t = 7661
t = 7671
t = 7681
t = 7691
t = 7701
t = 7711
t = 7721
t = 7731
t = 7741
t = 7751
t = 7761
t = 7771
t = 7781
t = 7791
t = 7801
t = 7811
t = 7821
t = 7831
t = 7841
t = 7851
t = 7861
t = 7871
t = 7881
t = 7891
t = 7901
t = 7911
t = 7921
t = 7931
t = 7941
t = 7951
t = 7961
t = 7971
t = 7981
t = 7991
t = 8001
t = 8011
t = 8021
t = 8031
t = 8041
t = 8051
t = 8061
t = 8071
t = 8081
t = 8091
t = 8101
t = 8111
t = 8121
t = 8131
t = 8141
t = 8151
t = 8161
t = 8171
t = 8181
t = 8191
t = 8201
t = 8211
t = 8221
t = 8231
t = 8241
t = 8251
t = 8261
t = 8271
t = 8281
t = 8291
t = 8301
t = 8311
t = 8321
t = 8331
t = 8341
t = 8351
t = 8361
t = 8371
t = 8381
t = 8391
t = 8401
t = 8411
t = 8421
t = 8431
t = 8441
t = 8451
t = 8461
t = 8471
t = 8481
t = 8491
t = 8501
t = 8511
t = 8521
t = 8531
t = 8541
t = 8551
t = 8561
t = 8571
t = 8581
t = 8591
t = 8601
t = 8611
t = 8621
t = 8631
t = 8641
t = 8651
t = 8661
t = 8671
t = 8681
t = 8691
t = 8701
t = 8711
t = 8721
t = 8731
t = 8741
t = 8751
t = 8761
t = 8771
t = 8781
t = 8791
t = 8801
t = 8811
t = 8821
t = 8831
t = 8841
t = 8851
t = 8861
t = 8871
t = 8881
t = 8891
t = 8901
t = 8911
t = 8921
t = 8931
t = 8941
t = 8951
t = 8961
t = 8971
t = 8981
t = 8991
t = 9001
t = 9011
t = 9021
t = 9031
t = 9041
t = 9051
t = 9061
t = 9071
t = 9081
t = 9091
t = 9101
t = 9111
t = 9121
t = 9131
t = 9141
t = 9151
t = 9161
t = 9171
t = 9181
t = 9191
t = 9201
t = 9211
t = 9221
t = 9231
t = 9241
t = 9251
t = 9261
t = 9271
t = 9281
t = 9291
t = 9301
t = 9311
t = 9321
t = 9331
t = 9341
t = 9351
t = 9361
t = 9371
t = 9381
t = 9391
t = 9401
t = 9411
t = 9421
t = 9431
t = 9441
t = 9451
t = 9461
t = 9471
t = 9481
t = 9491
t = 9501
t = 9511
t = 9521
t = 9531
t = 9541
t = 9551
t = 9561
t = 9571
t = 9581
t = 9591
t = 9601
t = 9611
t = 9621
t = 9631
t = 9641
t = 9651
t = 9661
t = 9671
t = 9681
t = 9691
t = 9701
t = 9711
t = 9721
t = 9731
t = 9741
t = 9751
t = 9761
t = 9771
t = 9781
t = 9791
t = 9801
t = 9811
t = 9821
t = 9831
t = 9841
t = 9851
t = 9861
t = 9871
t = 9881
t = 9891
t = 9901
t = 9911
t = 9921
t = 9931
t = 9941
t = 9951
t = 9961
t = 9971
t = 9981
t = 9991

head(compared)
#> # A tibble: 6 × 4
#> # Groups:   fish_type [1]
#>       t fish_type     mbp_n prop_res
#>   <dbl> <chr>         <dbl>    <dbl>
#> 1     1 MRH Steelhead     3    0.266
#> 2     1 MRH Steelhead    18    0.721
#> 3     1 MRH Steelhead    33    0.801
#> 4     1 MRH Steelhead    48    0.830
#> 5     1 MRH Steelhead    63    0.824
#> 6     1 MRH Steelhead    78    0.823
residence_plot(compared, var_groups = "fish_type", time_unit = "secs")
#> Warning: Transformation introduced infinite values in continuous y-axis

# NOTE: The curves last longer than the longest time! This should indicate we
# should add more times to test, and may need shorter intervals of n.

# Renormalized Sum of Squares
rSSR <- renorm_SSR(compared, var_groups = "fish_type")

head(rSSR)
#> # A tibble: 6 × 5
#> # Groups:   fish_type [1]
#>   fish_type     mbp_n    SSR     n      rSSR
#>   <chr>         <dbl>  <dbl> <int>     <dbl>
#> 1 MRH Steelhead     3 1.17    1000 0.00117  
#> 2 MRH Steelhead    18 0.457   1000 0.000457 
#> 3 MRH Steelhead    33 0.148   1000 0.000148 
#> 4 MRH Steelhead    48 0.0108  1000 0.0000108
#> 5 MRH Steelhead    63 0.0635  1000 0.0000635
#> 6 MRH Steelhead    78 0.0741  1000 0.0000741

# Thresholds for 99% and 99.5% of total range
thresh <- telemetR::conv_thresholds(rSSR, var_groups = "fish_type", 
                                    thresh_levels = c(0.01,0.005))
optimums <- opt_mbp(rSSR, thresh)

rSSR_plot(rSSR, optimums, var_groups = "fish_type")
#> Warning: Removed 6 rows containing missing values (`geom_line()`).

#> Warning: Transformation introduced infinite values in continuous y-axis
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Transformation introduced infinite values in continuous y-axis
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning: Removed 99 rows containing non-finite values (`stat_smooth()`).

# Note: This would be a bad choice of maximum blanking period. We have not yet 
# reached convergence