-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALSCL_User_Guide.Rmd
More file actions
executable file
·1707 lines (1305 loc) · 65.9 KB
/
Copy pathALSCL_User_Guide.Rmd
File metadata and controls
executable file
·1707 lines (1305 loc) · 65.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "ALSCL Package User Guide / ALSCL 套件使用指南"
subtitle: "Age-and-Length-Structured Catch-at-Length Stock Assessment Models"
author: "ALSCL Development Team"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_depth: 4
toc_float: true
number_sections: true
theme: flatly
highlight: tango
code_folding: show
fig_width: 10
fig_height: 7
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE, message = FALSE, warning = FALSE,
fig.align = "center", fig.width = 10, fig.height = 7, dpi = 300
)
```
# Introduction / 简介
The **ALSCL** package provides two integrated statistical models for length-based fisheries stock assessment, built on the Template Model Builder (TMB) framework for fast maximum likelihood estimation via automatic differentiation. The package includes a comprehensive suite of diagnostic visualization, model comparison, and retrospective analysis tools.
**ALSCL** 套件提供两个整合式的体长渔获量资源评估统计模型,基于 TMB 框架利用自动微分进行快速最大概似估计。包含完整的诊断可视化、模型对比和回顾分析工具。
## Two Core Models / 两大核心模型
**ACL (Age-structured Catch-at-Length)**:Tracks population in age space $N(A, Y)$. Projects catch to length space via a **pla matrix** (proportion-at-length-given-age). Estimates $F_A$ (fishing mortality at age). Classic approach — fast, well-suited when growth is predictable.
**ACL(年龄结构化体长渔获量模型)**:在年龄空间追踪种群 $N(A, Y)$。通过 **pla 矩阵**将渔获投影至体长空间。估计 $F_A$。经典方法 — 快速,适合生长可预测的物种。
**ALSCL (Age-and-Length-Structured Catch-at-Length)**:Maintains a full 3D array $N(L, A, Y)$ — numbers by length, age, and year. Growth via a **transition matrix** $G$. Estimates $F_L$ (fishing mortality at length) directly; $F_A$ is derived. Superior when individual growth variability is high or selectivity is length-dependent.
**ALSCL(年龄-体长结构化体长渔获量模型)**:维护完整三维阵列 $N(L, A, Y)$。通过**生长转移矩阵** $G$ 建模。直接估计 $F_L$,$F_A$ 为衍生量。适合生长变异大或选择性依赖体长的物种。
# Installation / 安装
```{r install-pkg, eval=FALSE}
install.packages(c("TMB", "reshape2", "ggplot2", "patchwork", "ggridges", "cowplot"))
# devtools::install_github("Linbojun99/ALSCL")
```
```{r load-libs}
library(ALSCL)
library(ggplot2)
library(patchwork)
```
# Data Preparation / 数据准备
Both models require three input data frames: `data.CatL` (catch-at-length), `data.wgt` (weight-at-length), `data.mat` (maturity-at-length). All must share the same `LengthBin` labels and year column names.
两个模型都需要三个数据框:`data.CatL`(体长别渔获量)、`data.wgt`(体长别体重)、`data.mat`(体长别成熟度),它们必须共享相同的体长标签和年份列名。
| LengthBin | 2020 | 2021 | ... | 2039 |
|:----------|-----:|-----:|----:|-----:|
| <7 | 12.5 | 14.2 | ... | 9.7 |
| 7-9 | 45.8 | 51.3 | ... | 38.1 |
| ... | ... | ... | ... | ... |
| 47-49 | 3.1 | 2.8 | ... | 4.2 |
| >49 | 1.3 | 0.7 | ... | 1.2 |
First column = `LengthBin` (string labels). Supported formats include open-ended labels (`"<7"`, `">49"`), continuous ranges (`"7-9"`, `"9-11"`), and non-overlapping ranges (`"0-20"`, `"21-25"`). Bins can be unequal width. No NA allowed — use 0.
第一列 = `LengthBin`(字符串标签)。支持的格式包括开放式标签(`"<7"`, `">49"`)、连续区间(`"7-9"`, `"9-11"`)和非重叠区间(`"0-20"`, `"21-25"`)。区间宽度可以不等。不允许 NA — 用 0 代替。
## Simulating Example Data / 模拟示例数据
For this guide we use `sim_data()` with a tuna-like parameter set (M6 operating model) to generate realistic survey catch-at-length data. The M6 model features quarterly time steps, dome-shaped selectivity, and a growth transition matrix — making it a comprehensive demonstration of both ACL and ALSCL capabilities. The simulation runs for 100 years with a 95-year burn-in, producing 20 quarterly time steps of observation data.
本 Guide 使用 `sim_data()` 配合类金枪鱼参数集(M6 运行模型)生成模拟调查数据。M6 模型具有季度时间步长、穹顶形选择性和生长转移矩阵 — 全面展示 ACL 和 ALSCL 的功能。模拟运行 100 年,95 年 burn-in,产生 20 个季度时间步的观测数据。
```{r simulate-data}
# Step 1: Initialize biological parameters (M6 tuna preset)
# 第一步:初始化生物参数(M6 金枪鱼预设)
params <- initialize_params(species = "tuna")
# Key params pre-loaded: nage=20, M=0.2, Linf=152, vbk=0.38,
# rec.age=0.25, growth_step=0.25, dome-shaped selectivity
# You can override any param: initialize_params(species = "tuna", M = 0.25)
# Step 2: Calculate derived biological variables
# 第二步:计算衍生生物变量(pla + Gij 矩阵、体重、成熟度等)
bio_vars <- sim_cal(params)
# Step 3: Simulate population dynamics (single replicate, seed = 88)
# 第三步:模拟种群动态(单次重复,种子 = 88)
sim <- sim_data(bio_vars, params,
sim_year = 100,
output_dir = tempdir(),
iter_range = 55,
return_iter = 55)
```
```{r format-data}
# Convert sim_data() output to run_acl/run_alscl input format
# 将 sim_data() 输出转换为 run_acl/run_alscl 输入格式
nyear_obs <- sim$nyear # 20 quarterly steps
nlen <- sim$nlen # 23 length bins (5mm)
year_char <- as.character(2020:(2020 + nyear_obs - 1)) # "2020" ... "2039"
# Bin labels with open-ended first/last / 开放式首尾标签
finite_borders <- seq(15, 120, 5)
len_labels <- c(
paste0("<", finite_borders[1]), # "<15"
paste0(finite_borders[-length(finite_borders)], "-", finite_borders[-1]), # "15-20" ...
paste0(">", finite_borders[length(finite_borders)]) # ">120"
)
# Survey catch-at-length (SN_at_len: nyear x nlen)
data.CatL <- data.frame(LengthBin = len_labels, t(sim$SN_at_len), check.names = FALSE)
colnames(data.CatL) <- c("LengthBin", year_char)
# Weight-at-length (constant across years)
data.wgt <- data.frame(LengthBin = len_labels, sim$weight, check.names = FALSE)
colnames(data.wgt) <- c("LengthBin", year_char)
# Maturity-at-length (constant across years)
data.mat <- data.frame(LengthBin = len_labels, sim$mat, check.names = FALSE)
colnames(data.mat) <- c("LengthBin", year_char)
```
## Using Your Own Data / 使用自己的数据
If you have real survey data as CSV files, simply read them in and set the biological parameters for your species. The format must match the table above.
如果您有自己的调查数据 CSV 文件,直接读取并设定物种的生物参数即可。格式需与上表一致。
```{r user-data, eval=FALSE}
# Read your own CSV files / 读取自己的 CSV 数据
data.CatL <- read.csv("your_catch_at_length.csv", check.names = FALSE)
data.wgt <- read.csv("your_weight_at_length.csv", check.names = FALSE)
data.mat <- read.csv("your_maturity_at_length.csv", check.names = FALSE)
# Set biological parameters for your species / 设定您物种的生物参数
rec.age <- 1 # recruitment age / 补充年龄
nage <- 7 # max age (plus group) / 最大年龄
M <- 0.8 # natural mortality / 自然死亡率
sel_L50 <- 28 # 50% survey selectivity length / 调查选择性 L50
sel_L95 <- 36 # 95% survey selectivity length / 调查选择性 L95
# Fit model / 拟合模型
result <- run_acl(data.CatL, data.wgt, data.mat,
rec.age = rec.age, nage = nage, M = M,
sel_L50 = sel_L50, sel_L95 = sel_L95,
train_times = 3, silent = TRUE)
```
# Model Configuration & Fitting / 模型设定与拟合
```{r model-config}
# Biological parameters matching the M6 tuna operating model
# 与 M6 金枪鱼运行模型匹配的生物学参数
rec.age <- 0.25 # quarterly recruitment age / 季度补充年龄
nage <- 20 # quarterly ages 0.25–5.0 / 季度年龄
M <- 0.2 # quarterly natural mortality / 季度自然死亡率
sel_L50 <- 30 # 50% survey selectivity / 调查选择性 L50
sel_L95 <- 50 # 95% survey selectivity / 调查选择性 L95
```
## Running the ACL Model / 运行 ACL 模型
```{r run-acl}
result_acl <- run_acl(
data.CatL = data.CatL, data.wgt = data.wgt, data.mat = data.mat,
rec.age = rec.age, nage = nage, M = M,
sel_L50 = sel_L50, sel_L95 = sel_L95,
# Starting values (tuna: Linf=152, vbk=0.38, quarterly F~0.2)
# 起始值(金枪鱼:Linf=152, vbk=0.38, 季度 F~0.2)
parameters = list(
log_init_Z = log(1), # initial total mortality / 初始总死亡率
log_std_log_N0 = log(0.5), # SD of initial age-structure / 初始年龄结构 SD
mean_log_R = 5, # mean log-recruitment / 平均对数补充量
log_std_log_R = log(0.3), # SD of log-R deviations / 对数 R 偏差 SD
logit_log_R = log(0.75 / 0.25),# recruitment AR1 correlation / 补充量 AR1
mean_log_F = log(0.2), # mean log-F (quarterly) / 平均对数 F
log_std_log_F = log(0.5), # SD of log-F deviations / 对数 F 偏差 SD
logit_log_F_y = log(0.75 / 0.25),# F year-correlation / F 年间相关
logit_log_F_a = log(0.75 / 0.25),# F age-correlation / F 年龄相关
log_vbk = log(0.38), # VB growth rate k / VB 生长速率 k
log_Linf = log(152), # asymptotic length / 渐近体长
t0 = 1 / 152, # VB t0 / VB t0
log_cv_len = log(0.2), # CV of length-at-age / 体长变异系数
log_std_index = log(0.1) # survey observation error / 调查观测误差
),
# Lower bounds / 下界
parameters.L = list(
log_init_Z = log(0.1),
log_std_log_N0 = log(0.01),
mean_log_R = 1,
log_std_log_R = log(0.01),
logit_log_R = -20,
mean_log_F = log(0.0001),
log_vbk = log(0.1), # k >= 0.1
log_Linf = log(100), # Linf >= 100
log_cv_len = log(0.01),
log_std_index = log(0.01)
),
# Upper bounds / 上界
parameters.U = list(
log_init_Z = log(3),
log_std_log_N0 = log(5),
mean_log_R = 10,
log_std_log_R = log(1),
logit_log_R = 10,
mean_log_F = log(1),
log_vbk = log(0.5), # k <= 0.5
log_Linf = log(200), # Linf <= 200
log_cv_len = log(1),
log_std_index = log(1)
),
train_times = 3, # 3 random starting points / 3 个随机起始点
output = FALSE, # suppress auto figure output / 不自动输出图
silent = F # suppress progress messages / 静默模式
)
cat(sprintf("ACL -- converge: %s | bound_hit: %s | Linf=%.1f (true %.1f) | k=%.3f (true %.3f)\n",
result_acl$converge, result_acl$bound_hit,
result_acl$report$Linf, params$Linf,
result_acl$report$vbk, params$vbk))
```
## Running the ALSCL Model / 运行 ALSCL 模型
ALSCL uses the same data inputs and biological parameters as ACL, with the addition of `growth_step` to control the time resolution of the growth transition matrix.
ALSCL 使用与 ACL 相同的数据输入和生物学参数,额外需要 `growth_step` 控制生长转移矩阵的时间分辨率。
```{r run-alscl}
result_alscl <- run_alscl(
data.CatL = data.CatL, data.wgt = data.wgt, data.mat = data.mat,
rec.age = rec.age, nage = nage, M = M,
sel_L50 = sel_L50, sel_L95 = sel_L95,
growth_step = 0.25, # quarterly growth transition / 季度生长转移
# Starting values (tuna: Linf=152, vbk=0.38, quarterly dynamics)
# 起始值(金枪鱼:Linf=152, vbk=0.38, 季度动态)
parameters = list(
log_init_Z = log(1),
log_sigma_log_N0 = log(0.5),
mean_log_R = 5,
log_sigma_log_R = log(0.3),
logit_log_R = log(0.75 / 0.25),
mean_log_F = log(0.2),
log_sigma_log_F = log(0.5),
logit_log_F_y = log(0.75 / 0.25),
logit_log_F_l = log(0.75 / 0.25),
log_vbk = log(0.38),
log_Linf = log(152),
log_t0 = log(1 / 152),
log_cv_len = log(0.2),
log_cv_grow = log(0.2),
log_sigma_index = log(0.1)
),
# Lower bounds / 下界
parameters.L = list(
log_init_Z = log(0.1),
log_sigma_log_N0 = log(0.01),
mean_log_R = 1,
log_sigma_log_R = log(0.01),
logit_log_R = -20,
mean_log_F = log(0.0001),
logit_log_F_y = -20,
logit_log_F_l = -10,
log_vbk = log(0.1),
log_Linf = log(100),
log_cv_len = log(0.01),
log_cv_grow = log(0.01),
log_sigma_index = log(0.01)
),
# Upper bounds / 上界
parameters.U = list(
log_init_Z = log(3),
log_sigma_log_N0 = log(5),
mean_log_R = 10,
log_sigma_log_R = log(1),
logit_log_R = 10,
mean_log_F = log(1),
logit_log_F_y = 20,
logit_log_F_l = 10,
log_vbk = log(0.5),
log_Linf = log(200),
log_cv_len = log(1),
log_cv_grow = log(1),
log_sigma_index = log(1)
),
train_times = 3,
silent = TRUE
)
cat(sprintf("ALSCL -- converge: %s | bound_hit: %s | Linf=%.1f (true %.1f) | k=%.3f (true %.3f)\n",
result_alscl$converge, result_alscl$bound_hit,
result_alscl$report$Linf, params$Linf,
result_alscl$report$vbk, params$vbk))
```
### A Note on Parameter Tuning / 关于参数调试的说明
In practice, both ACL and ALSCL may hit parameter boundaries or converge to local optima with default starting values. When `result$bound_hit` reports `TRUE`, the estimates at that boundary should be treated with caution. This is **expected behavior** in nonlinear optimization -- the user is responsible for adjusting starting values, bounds, and `train_times` to achieve reliable convergence for their specific dataset and species.
在实际应用中,ACL 和 ALSCL 都可能在默认起始值下撞到参数边界或收敛到局部最优。当 `result$bound_hit` 报告 `TRUE` 时,边界处的估计值需谨慎对待。这是非线性优化中的 **正常现象** -- 用户需要根据自己的数据集和物种调整起始值、边界和 `train_times` 以获得可靠的收敛结果。
Common strategies include: adjusting bounds to bracket biologically plausible ranges, using multiple random starts (`train_times`), narrowing bounds around known values, or fixing certain parameters via `map`. See the "Customizing Parameters" section below for detailed guidance.
常用策略包括:调整边界以包围生物学合理范围、使用多个随机起始点(`train_times`)、缩小已知值附近的边界、或通过 `map` 固定某些参数。详见下方"自定义参数"章节。
This guide focuses on demonstrating the package's functionality and API usage. The parameter values shown here serve as examples -- users should calibrate them to their own species and data.
本指南重点展示包的功能和 API 用法。此处所示参数值仅作示例 -- 用户应根据自己的物种和数据进行校准。
# Customizing Parameters, Bounds & Map / 自定义参数、边界和 Map
Both `run_acl()` and `run_alscl()` accept three optional arguments that give you full control over the optimization: `parameters` (starting values), `parameters.L` / `parameters.U` (lower/upper bounds), and `map` (which parameters to fix or estimate). You only need to specify the entries you want to change — everything else keeps its default value.
`run_acl()` 和 `run_alscl()` 都接受三个可选参数来完全控制优化过程:`parameters`(起始值)、`parameters.L` / `parameters.U`(上下界)和 `map`(固定/估计哪些参数)。你只需指定需要修改的条目,其余保持默认值。
## Viewing Default Values / 查看默认值
`create_parameters()` returns the initial values, lower bounds, and upper bounds for all optimizer parameters. It supports three built-in species presets (`"flatfish"`, `"tuna"`, `"krill"`) that provide biologically-calibrated starting values from actual assessment scripts, or generic wide-range defaults when no species is specified.
`create_parameters()` 返回优化器所有参数的初始值、下界和上界。它支持三种内置物种预设(`"flatfish"`、`"tuna"`、`"krill"`),提供来自实际评估脚本的生物学校准起始值;也可不指定物种使用通用宽范围默认值。
```{r view-defaults, eval=FALSE}
# List available species presets / 列出可用物种预设
create_parameters("acl", species = "?")
#> Available species presets:
#> 'flatfish' — M7 yellowtail flounder (annual, Linf=60, nage=15)
#> 'tuna' — M6 tuna (quarterly, Linf=152, nage=20)
#> 'krill' — Antarctic krill (ACL: Linf=90 fixed; ALSCL: Linf=60 estimated)
# Generic defaults (wide bounds) / 通用默认值(宽边界)
p_generic <- create_parameters("acl")
# Species-specific: flatfish ACL / 物种预设:比目鱼 ACL
p_flatfish_acl <- create_parameters("acl", species = "flatfish")
# Species-specific: tuna ALSCL / 物种预设:金枪鱼 ALSCL
p_tuna_alscl <- create_parameters("alscl", species = "tuna")
# Species-specific: krill ACL (Linf=90, typically fixed via map)
p_krill_acl <- create_parameters("acl", species = "krill")
# Species-specific + custom override / 物种预设 + 自定义覆盖
p_krill_custom <- create_parameters("acl", species = "krill",
parameters = list(log_Linf = log(85))) # override just Linf
```
The key parameters and their defaults are shown below. All growth/variance parameters are on the **log or logit scale** to ensure positivity/bounded range during optimization.
关键参数及其默认值如下表。所有生长/方差参数均在 **对数或 logit 尺度**上以确保优化中的正性/有界性。
| Parameter | ACL name | ALSCL name | Default | Meaning |
|:----------|:---------|:-----------|:--------|:--------|
| Initial Z | `log_init_Z` | `log_init_Z` | 0.5 / log(0.5) | log(total mortality) for equilibrium age structure |
| Mean Recruitment | `mean_log_R` | `mean_log_R` | 5 | log-scale mean recruitment |
| SD of R deviations | `log_std_log_R` | `log_sigma_log_R` | log(0.2) / log(1) | log(SD) of recruitment random effects |
| AR(1) correlation of R | `logit_log_R` | `logit_log_R` | logit(0.75) | logit-scale autocorrelation |
| Mean F | `mean_log_F` | `mean_log_F` | log(0.3) | log-scale mean fishing mortality |
| SD of F deviations | `log_std_log_F` | `log_sigma_log_F` | log(0.8) / log(1) | **fixed by default** (via map) |
| AR(1) corr. F (year) | `logit_log_F_y` | `logit_log_F_y` | logit(0.75) | **fixed by default** (via map) |
| AR(1) corr. F (age/len) | `logit_log_F_a` | `logit_log_F_l` | logit(0.75) | **fixed by default** (ACL map) |
| VB growth k | `log_vbk` | `log_vbk` | log(0.2) / log(0.4) | log(Von Bertalanffy k) |
| VB Linf | `log_Linf` | `log_Linf` | log(60) | log(asymptotic length) |
| VB t0 | `t0` | `log_t0` | 1/60 / log(1/60) | **fixed by default** (via map) |
| Length CV | `log_cv_len` | `log_cv_len` | log(0.3) | log(CV of length-at-age) |
| Growth CV | — | `log_cv_grow` | log(0.3) | ALSCL only: log(CV of growth increment) |
| Observation error | `log_std_index` | `log_sigma_index` | log(0.1) | log(SD) of survey observation error |
## Overriding Starting Values / 覆盖起始值
Pass a named list to `parameters` — only include the entries you want to change. This is what we did earlier to fix the ALSCL convergence problem.
通过 `parameters` 传入一个命名列表 — 只需包含要修改的条目。这就是我们之前修复 ALSCL 收敛问题的方法。
```{r custom-start, eval=FALSE}
# Example: override Linf, vbk, and mean_log_R for ACL
# 示例:覆盖 ACL 的 Linf、vbk 和 mean_log_R 起始值
result <- run_acl(data.CatL, data.wgt, data.mat,
rec.age = 1, nage = 15, M = 0.2, sel_L50 = 15, sel_L95 = 20,
parameters = list(
log_Linf = log(60), # asymptotic length ≈ 60mm
log_vbk = log(0.2), # growth rate
mean_log_R = 8 # log(R) ≈ 8 → R ≈ 3000
),
train_times = 3, silent = TRUE)
```
## Overriding Bounds / 覆盖参数边界
Pass named lists to `parameters.L` and `parameters.U`. The optimizer (`nlminb`) will constrain estimated parameters within these bounds. Again, only list the entries you want to change.
通过 `parameters.L` 和 `parameters.U` 传入命名列表。优化器 (`nlminb`) 会将估计参数限制在这些边界内。同样,只列出需要修改的条目。
```{r custom-bounds, eval=FALSE}
# Example: tighter Linf bounds for ALSCL
# 示例:为 ALSCL 设置更紧的 Linf 边界
result <- run_alscl(data.CatL, data.wgt, data.mat,
rec.age = 1, nage = 15, M = 0.2, sel_L50 = 15, sel_L95 = 20,
growth_step = 1,
parameters = list(log_Linf = log(60), log_vbk = log(0.2)),
parameters.L = list(log_Linf = log(40), log_vbk = log(0.1)), # Linf ≥ 40, k ≥ 0.1
parameters.U = list(log_Linf = log(100), log_vbk = log(1.0)), # Linf ≤ 100, k ≤ 1.0
train_times = 5, silent = TRUE)
```
If a parameter hits its bound exactly (`result$bound_hit == TRUE`), consider widening the bounds — or investigate whether the model specification is appropriate for your data.
如果参数精确撞到边界(`result$bound_hit == TRUE`),考虑放宽边界 — 或检查模型设定是否适合您的数据。
## Customizing the Map (Fix / Estimate Parameters) / 自定义 Map(固定/估计参数)
The `map` argument controls which parameters are **fixed** (not estimated) during optimization. A parameter mapped to `factor(NA)` is held at its starting value; all others are freely estimated.
`map` 参数控制优化过程中哪些参数被 **固定**(不估计)。映射为 `factor(NA)` 的参数保持在起始值不变;其余参数自由估计。
The default map fixes these parameters (because they are usually not identifiable from length-composition data alone):
默认 map 固定以下参数(因为它们通常无法仅从体长组成数据中辨识):
```{r show-default-map, eval=FALSE}
# Default map (same for ACL and ALSCL conceptually)
# 默认 map(ACL 和 ALSCL 概念上相同)
generate_map()
# $log_std_log_F = factor(NA) — SD of F deviations: fixed
# $logit_log_F_y = factor(NA) — AR(1) of F over years: fixed
# $logit_log_F_a = factor(NA) — AR(1) of F over ages: fixed
# $t0 = factor(NA) — VB t0 parameter: fixed
```
To **free a normally-fixed parameter**, override it with an estimable factor. To **fix additional parameters**, add them to the map.
要 **释放通常固定的参数**,用可估计的 factor 覆盖它。要 **额外固定参数**,将其添加到 map。
```{r custom-map, eval=FALSE}
# Example 1: Fix Linf at 90mm (e.g., from external data) while estimating vbk
# 示例 1:将 Linf 固定为 90mm(如来自外部数据),同时估计 vbk
result <- run_acl(data.CatL, data.wgt, data.mat,
rec.age = 1, nage = 15, M = 0.2, sel_L50 = 15, sel_L95 = 20,
parameters = list(log_Linf = log(90)), # starting value = fixed value
map = list(log_Linf = factor(NA)), # fix Linf during optimization
train_times = 3, silent = TRUE)
# Example 2: Free F year-correlation (normally fixed) + fix Linf
# 示例 2:释放 F 年相关性(通常固定)+ 固定 Linf
result <- run_acl(data.CatL, data.wgt, data.mat,
rec.age = 1, nage = 15, M = 0.2, sel_L50 = 15, sel_L95 = 20,
parameters = list(log_Linf = log(90)),
map = list(
log_Linf = factor(NA), # fix Linf
logit_log_F_y = factor(1) # FREE: estimate F year-correlation
),
train_times = 3, silent = TRUE)
```
The `generate_map()` function merges your custom map entries with the defaults: entries you specify replace the defaults, while unspecified entries keep their default (`factor(NA)`) value. If you pass a map entry that is already in the default (like `t0 = factor(NA)`), it simply reaffirms the default.
`generate_map()` 函数将你的自定义 map 条目与默认值合并:你指定的条目替换默认值,未指定的条目保持默认。
## Full Customization Example / 完整自定义示例
Here is a comprehensive example showing, showing `parameters`, bounds, and `map` all together:
下面是一个完整示例,同时展示 `parameters`、边界和 `map`:
```{r full-custom-example, eval=FALSE}
# --- ACL with custom parameters, bounds, and map ---
result_acl <- run_acl(data.CatL, data.wgt, data.mat,
rec.age = 1, nage = 15, M = 0.2, sel_L50 = 15, sel_L95 = 20,
parameters = list(
log_init_Z = 0.5,
mean_log_R = 5,
log_std_log_R = log(1),
mean_log_F = log(0.3),
log_vbk = log(0.2),
log_Linf = log(90), # fixed via map below
log_cv_len = log(0.3),
log_std_index = log(0.1)
),
parameters.L = list(
mean_log_R = log(10),
log_vbk = log(0.1),
log_std_index = log(0.01)
),
parameters.U = list(
mean_log_R = 20,
log_vbk = log(1),
log_std_index = log(1)
),
map = list(
log_Linf = factor(NA), # fix Linf at 90mm
t0 = factor(NA) # fix t0 (default)
),
train_times = 3, silent = TRUE
)
# --- ALSCL with custom parameters, bounds, and map ---
result_alscl <- run_alscl(data.CatL, data.wgt, data.mat,
rec.age = 1, nage = 15, M = 0.2, sel_L50 = 15, sel_L95 = 20,
growth_step = 1,
parameters = list(
log_init_Z = log(0.5),
mean_log_R = 5,
mean_log_F = log(0.3),
log_vbk = log(0.2),
log_Linf = log(60), # estimated (not fixed)
log_cv_len = log(0.3),
log_cv_grow = log(0.3),
log_sigma_index = log(0.1)
),
parameters.L = list(
log_Linf = log(40),
log_vbk = log(0.1)
),
parameters.U = list(
log_Linf = log(100),
log_vbk = log(1)
),
map = list(
log_sigma_log_F = factor(NA), # fix F SD (default)
log_t0 = factor(NA) # fix t0 (default)
# Note: log_Linf NOT in map → it will be estimated
),
train_times = 5, silent = TRUE
)
```
Note the key difference: in the ACL run above, `log_Linf` is in the `map` (fixed at 90mm — a common approach when Linf is known from external tagging data). In the ALSCL run, `log_Linf` is **not** in the `map` so it is freely estimated within bounds 40–100mm.
注意关键区别:上面的 ACL 运行中 `log_Linf` 在 `map` 中(固定为 90mm — 当 Linf 可从外部标记数据获得时常用的方法)。ALSCL 运行中 `log_Linf` **不在** `map` 中,因此在 40–100mm 范围内自由估计。
# Single-Model Diagnostic Plots / 单模型诊断图
All `plot_*()` functions return `ggplot2` objects. You can add layers with `+`, combine with `patchwork`, or store in variables.
> **Common theme parameters / 通用主题参数**: The following functions accept per-call theme overrides — `title`, `xlab`, `ylab`, `font_family`, `title_size`, `axis_title_size`, `axis_text_size`, `strip_text_size`, `legend_text_size`, `x_breaks`, `base_theme`, `title_hjust`: **plot_CatL, plot_residuals, plot_VB, plot_pla, plot_recruitment, plot_SSB_Rec, plot_ridges, plot_retro, plot_fishing_mortality**. For the remaining functions (**plot_biomass, plot_abundance, plot_catch, plot_SSB, plot_deviance**), styling comes from the global `acl_theme_set()` system or direct ggplot2 layers.
---
## plot_CatL — Catch-at-Length Fit / 体长渔获量拟合
Compares observed vs predicted catch-at-length. Two display modes with distinct aesthetic controls.
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `type` | `"length"` | `"length"`: time series per length bin; `"year"`: length composition per year |
| `exp_transform` | `FALSE` | Apply `exp()` back-transform from log scale |
| `point_size` | 2.5 | Observed point size (type="length") |
| `point_color` | `"#D32F2F"` | Observed point color (type="length") |
| `point_shape` | 16 | Observed point shape (type="length") |
| `line_size` | 1.2 | Predicted line thickness (type="length") |
| `line_color` | `"black"` | Predicted line color (type="length") |
| `line_type` | `"solid"` | Predicted line type (type="length") |
| `line_size1` | 1.8 | **Estimated** line thickness (type="year") |
| `line_color1` | `"#D32F2F"` | **Estimated** line color (type="year") |
| `line_type1` | `"solid"` | **Estimated** line type (type="year") |
| `line_alpha1` | 1 | **Estimated** line opacity (type="year") |
| `line_size2` | 1.0 | **Observed** line thickness (type="year") |
| `line_color2` | `"#1976D2"` | **Observed** line color (type="year") |
| `line_type2` | `"solid"` | **Observed** line type (type="year") |
| `line_alpha2` | 0.6 | **Observed** line opacity (type="year") |
| `facet_ncol` | `NULL` | Facet columns |
| `facet_scales` | `"free"` | Facet scales: `"free"`, `"free_y"`, `"free_x"`, `"fixed"` |
| `return_data` | `FALSE` | Return `list(plot, data)` |
```{r catl-year, fig.height=12}
plot_CatL(result_acl, type = "year")
```
```{r catl-length, fig.height=8}
plot_CatL(result_acl, type = "length")
```
```{r catl-custom-year, fig.height=12}
# Customize both lines / 自定义双线
plot_CatL(result_acl, type = "year",
line_size1 = 2.0, line_color1 = "darkred", line_alpha1 = 1,
line_size2 = 1.2, line_color2 = "grey50", line_alpha2 = 0.5,
facet_ncol = 5)
```
```{r catl-exp, fig.height=12}
# Back-transform from log scale / 指数转换回自然尺度
plot_CatL(result_acl, type = "year", exp_transform = TRUE)
```
```{r catl-length-style, fig.height=8}
# Custom points + line for "length" mode / 自定义 "length" 模式
plot_CatL(result_acl, type = "length",
point_size = 3, point_color = "navy", point_shape = 17,
line_color = "firebrick", line_size = 1.5, line_type = "dashed",
facet_ncol = 3, facet_scales = "fixed")
```
---
## plot_residuals — Residual Diagnostics / 残差诊断
Residuals (observed − predicted) with LOESS smoother. Random scatter around zero = good fit.
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `type` | `"length"` | `"length"` (by length bin) or `"year"` (by year) |
| `f` | 0.4 | LOESS span (0–1); smaller = more responsive |
| `smooth_color` | `"blue"` | Smoother line color |
| `hline_color` | `"red"` | Zero-reference line color |
| `line_color` | `"black"` | Data line color |
| `line_size` | 1 | Line thickness |
| `facet_ncol` | `NULL` | Facet columns |
| `facet_scales` | `"free"` | Facet scales |
| `resid_cap` | `NULL` | Cap residuals at +/- this value. Tail bins (e.g. `>120` for tuna) with near-zero observations produce extreme log-residuals that distort the plot. Try `resid_cap = 1`. |
| `return_data` | `FALSE` | Return data |
```{r resid-year, fig.height=5}
plot_residuals(result_acl, type = "year")
```
```{r resid-length, fig.height=8}
plot_residuals(result_acl, type = "length")
```
```{r resid-custom, fig.height=5}
plot_residuals(result_acl, type = "year",
f = 0.25, smooth_color = "darkgreen",
hline_color = "grey60", line_size = 1.5)
```
---
## plot_VB — Von Bertalanffy Growth Curve / VB 生长曲线
$L(a) = L_\infty (1 - e^{-k(a - t_0)})$ with parameter annotations.
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `age_range` | `c(1, 25)` | Age range to plot |
| `se` | `FALSE` | Show confidence interval |
| `se_type` | `"ribbon"` | `"ribbon"` or `"errorbar"` |
| `se_color` | `"red"` | CI color |
| `se_alpha` | 0.2 | CI transparency |
| `line_size` | 1.2 | Curve thickness |
| `line_color` | `"red"` | Curve color |
| `line_type` | `"solid"` | Curve line type |
| `text_size` | 5 | Parameter annotation font size |
| `text_color` | `"black"` | Annotation text color |
```{r vb-basic}
plot_VB(result_acl)
```
```{r vb-ribbon}
plot_VB(result_acl, se = TRUE, se_color = "steelblue", se_alpha = 0.3)
```
```{r vb-errorbar}
plot_VB(result_acl, se = TRUE, se_type = "errorbar",
age_range = c(0, 10), line_color = "darkblue",
text_size = 4.5, text_color = "grey30")
```
---
## plot_pla — Proportion-at-Length-given-Age / 年龄对应体长比例矩阵
ACL-specific heatmap. Each column = body length probability distribution for a given age class.
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `low_col` | `"white"` | Gradient low endpoint |
| `high_col` | `"red"` | Gradient high endpoint |
```{r pla-basic}
plot_pla(result_acl)
```
```{r pla-custom}
plot_pla(result_acl, low_col = "#F7FBFF", high_col = "#08306B")
```
---
## plot_recruitment — Recruitment Time Series / 补充量时间序列
Annual recruitment estimates (numbers entering at `rec.age`).
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `line_size` | 1.5 | Line thickness |
| `line_color` | `"#D32F2F"` | Line color |
| `line_type` | `"solid"` | Line type |
| `se` | `FALSE` | Show CI |
| `se_type` | `"ribbon"` | `"ribbon"` or `"errorbar"` |
| `se_color` | `"#D32F2F"` | CI color |
| `se_alpha` | 0.2 | CI transparency |
| `return_data` | `FALSE` | Return `list(plot, data)` |
```{r rec-basic}
plot_recruitment(result_acl)
```
```{r rec-ribbon}
plot_recruitment(result_acl, se = TRUE, se_alpha = 0.25)
```
```{r rec-errorbar}
plot_recruitment(result_acl, se = TRUE, se_type = "errorbar",
line_color = "navy", se_color = "navy", line_size = 1.8)
```
---
## plot_fishing_mortality — Fishing Mortality / 捕捞死亡率
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `type` | `"year"` | `"year"` (total F), `"age"` (F-at-age, ACL), `"length"` (F-at-length, ALSCL only) |
| `se` | `FALSE` | CI (type="year" only) |
| `se_type` | `"ribbon"` | CI style |
| `se_color` | `"red"` | CI color |
| `se_alpha` | 0.2 | CI transparency |
| `line_size` | 1 | Line thickness |
| `line_color` | `"red"` | Line color |
| `line_type` | `"solid"` | Line type |
| `facet_ncol` | `NULL` | Facet columns (for age/length) |
| `facet_scales` | `"free"` | Facet scales |
| `return_data` | `FALSE` | Return data |
```{r f-year}
plot_fishing_mortality(result_acl, type = "year")
```
```{r f-year-se}
plot_fishing_mortality(result_acl, type = "year", se = TRUE)
```
```{r f-age, fig.height=8}
plot_fishing_mortality(result_acl, type = "age", facet_ncol = 4)
```
```{r f-length-alscl, fig.height=8}
# ALSCL-specific: F at each length bin / ALSCL 特有:各体长组 F
plot_fishing_mortality(result_alscl, type = "length", facet_ncol = 3)
```
---
## plot_biomass — Biomass / 生物量
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `type` | `"B"` | `"B"` (total), `"BL"` (at-length), `"BA"` (at-age) |
| `se` | `FALSE` | CI (type="B" only) |
| `se_color` | `"red"` | CI color |
| `se_alpha` | 0.2 | CI transparency |
| `line_size` | 1.2 | Line thickness |
| `line_color` | `"red"` | Line color |
| `line_type` | `"solid"` | Line type |
| `facet_ncol` | `NULL` | Facet columns |
| `facet_scales` | `"free"` | Facet scales |
| `return_data` | `FALSE` | Return data |
```{r biomass-total}
plot_biomass(result_acl, type = "B")
```
```{r biomass-se}
plot_biomass(result_acl, type = "B", se = TRUE,
line_color = "steelblue", se_color = "steelblue")
```
```{r biomass-BL, fig.height=8}
plot_biomass(result_acl, type = "BL", facet_ncol = 3, line_color = "darkgreen")
```
---
## plot_abundance — Abundance / 丰度
Same structure as `plot_biomass`. `type`: `"N"` (total), `"NL"` (at-length), `"NA"` (at-age). Same parameters.
```{r abundance-N}
plot_abundance(result_acl, type = "N")
```
```{r abundance-NL, fig.height=8}
plot_abundance(result_acl, type = "NL", facet_ncol = 3)
```
---
## plot_catch — Predicted Catch / 预测渔获量
Same structure. `type`: `"CN"` (total), `"CNA"` (at-age), `"CNL"` (at-length). Same parameters.
```{r catch-CN}
plot_catch(result_acl, type = "CN")
```
```{r catch-CNA, fig.height=8}
plot_catch(result_acl, type = "CNA", facet_ncol = 4)
```
---
## plot_SSB — Spawning Stock Biomass / 产卵亲鱼生物量
SSB = biomass of mature individuals. Same structure. `type`: `"SSB"` (total), `"SBL"` (at-length), `"SBA"` (at-age). Same parameters.
```{r ssb-total}
plot_SSB(result_acl, type = "SSB")
```
```{r ssb-se}
plot_SSB(result_acl, type = "SSB", se = TRUE, line_color = "darkred")
```
---
## plot_SSB_Rec — SSB-Recruitment Relationship / SSB-补充量关系
Scatterplot of SSB (x) vs subsequent recruitment (y), connected by temporal trajectory.
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `age_at_recruitment` | 1 | Time lag (years) between SSB and recruitment |
| `point_size` | 2 | Point size |
| `point_color` | `"black"` | Point color |
| `point_shape` | 16 | Point shape |
| `return_data` | `FALSE` | Return data |
```{r ssb-rec}
plot_SSB_Rec(result_acl)
```
```{r ssb-rec-custom}
plot_SSB_Rec(result_acl, age_at_recruitment = 2,
point_size = 3, point_color = "darkblue", point_shape = 17)
```
---
## plot_ridges -- Ridgeline Length Frequency / 山脊图体长频率
Stacked density curves. Left = observed, right = estimated (side by side).
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `ridges_alpha` | 0.8 | Fill transparency |
| `ridges_scale` | `NULL` (auto) | Ridge height multiplier. `NULL` auto-scales so peaks fill ~80% of inter-year spacing. Increase for taller ridges. Essential for species with many length bins where proportions are small. |
| `palette` | `"viridis"` | 8 presets or custom color vector |
**8 built-in palettes:** `"viridis"`, `"ocean"`, `"sunset"`, `"forest"`, `"fire"`, `"spectral"`, `"turbo"`, `"plasma"`. Or pass a custom vector like `c("navy", "white", "firebrick")`.
```{r ridges-viridis}
plot_ridges(result_acl)
```
```{r ridges-ocean}
plot_ridges(result_acl, palette = "ocean")
```
```{r ridges-custom}
plot_ridges(result_acl, palette = c("#0D1B2A", "#2E86AB", "#72EFDD"))
```
---
## plot_deviance — Deviance Contributions / 偏差贡献
### Parameters (complete)
| Parameter | Default | Description |
|:----------|:--------|:------------|
| `type` | `"R"` | `"R"` (recruitment) or `"F"` (fishing mortality — by age/ACL, by length/ALSCL) |
| `se` | `TRUE` | Show error bars |
| `log` | `TRUE` | Log-transform y-axis |
| `point_size` | 3 | Point size |
| `point_color` | `"white"` | Point fill color |
| `point_shape` | 21 | Point shape |
| `line_size` | 1 | Line thickness |
| `line_color` | `"black"` | Line color |
| `line_type` | `"solid"` | Line type |
| `se_color` | `"black"` | Error bar color |
| `se_width` | 0.5 | Error bar cap width |
| `facet_ncol` | `NULL` | Facet columns (type="F") |
| `facet_scales` | `"free"` | Facet scales |
```{r deviance-R}
plot_deviance(result_acl, type = "R")
```
```{r deviance-F, fig.height=8}
plot_deviance(result_acl, type = "F", facet_ncol = 4)
```
```{r deviance-custom}
plot_deviance(result_acl, type = "R", log = FALSE,
point_color = "steelblue", point_size = 4, se_width = 0.3)
```
# ALSCL Diagnostics (Parallel to ACL) / ALSCL 诊断(与 ACL 对照)
All `plot_*()` functions auto-detect model type. The same function calls work for both ACL and ALSCL — the only difference is the model object passed in. Below we show each major diagnostic for ALSCL alongside its ACL counterpart for easy comparison.
所有 `plot_*()` 函数自动检测模型类型。相同的函数调用对 ACL 和 ALSCL 均适用 — 唯一区别是传入的模型对象。下面展示 ALSCL 的每项主要诊断及其 ACL 对照。
## CatL Fit / 体长渔获量拟合
```{r alscl-catl-year, fig.height=12}
# ALSCL: CatL by year / ALSCL:按年份体长渔获量
plot_CatL(result_alscl, type = "year")
```
```{r alscl-catl-length, fig.height=8}
# ALSCL: CatL by length bin / ALSCL:按体长组
plot_CatL(result_alscl, type = "length")
```
## Residuals / 残差
```{r alscl-resid-year, fig.height=5}
plot_residuals(result_alscl, type = "year")
```
```{r alscl-resid-length, fig.height=8}
# resid_cap = 1 clips extreme tail-bin residuals (e.g. >120 bin)
# where near-zero observations produce log-residuals of +/-3
# resid_cap = 1 截断极端尾部体长组残差(如 >120 组)
plot_residuals(result_alscl, type = "length", resid_cap = 1)
```
## Growth Curve / 生长曲线
```{r alscl-vb, fig.height=5}
# ALSCL growth curve with CI / ALSCL 生长曲线含置信区间
plot_VB(result_alscl, se = TRUE)
```
## Recruitment / 补充量
```{r alscl-rec, fig.height=5}
plot_recruitment(result_alscl, se = TRUE, se_type = "ribbon")
```
## Fishing Mortality / 捕捞死亡率
```{r alscl-f-year, fig.height=5}
# ALSCL: F by year / ALSCL:年度 F
plot_fishing_mortality(result_alscl, type = "year", se = TRUE)
```