6-Growth_Calibration.Rmd 24.9 KB
Newer Older
Poulet Camille's avatar
Poulet Camille committed
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
---
title: "Growth Calibration"
author: "Camille POULET"
date: "23/02/2021"
output: word_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE, include = TRUE, 
                      warning = FALSE,
                      fig.cap = TRUE)

rm(list = ls())
```

```{r library, include=FALSE}
library(dplyr)
library(tidyr)
library(ggplot2)
library(readr)
library(XML)
library(forcats)
library(knitr)
library(officedown)
library(flextable)
library(stringr)
library(tictoc)
patrick.lambert's avatar
patrick.lambert committed
29
library(tibble)
Poulet Camille's avatar
Poulet Camille committed
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

```

```{r GR3D functions and data, include=FALSE, echo = FALSE}

source("../GR3D_Rdescription/GR3Dfunction.R")
source("../GR3D_Rdescription/GR3D_NEA_env_data.R")
source("../GR3D_Rdescription/GR3D_NEA_XML_parameters.R")

#source_rmd <- function(rmd_file){
#knitr::knit(rmd_file, output = tempfile())
#}

#source_rmd("NEAgrowth.Rmd")

#source(knitr::purl("NEAgrowth.Rmd", quiet=TRUE))

```

```{r Build the connections file with temperature experienced by fish according to their offshore basins, echo = FALSE, warning=FALSE}

#Overview of connections existing between summering and wintering offshore basins
connections %>% 
  distinct(wintering_offshore_basin_name,summering_offshore_basin_name) 

#Compute an average temperature experienced by juveniles during their life at sea according to connections files i.e. wintering and summering areas.

connections_trans <- connections %>% 
  dplyr::select(-c(wintering_offshore_basin_id,
                   summering_offshore_basin_id)) %>% 
  pivot_longer(cols = c("wintering_offshore_basin_name",
                        "summering_offshore_basin_name"),
               names_to = "offshore_basin_type", 
               values_to = "offshore_basin_name") %>% 
  select(inshore_basin_name, offshore_basin_name, offshore_basin_type) %>% 
  inner_join(tempInOffshore, by = c("offshore_basin_name" = "basin_name")) %>% 
  filter(between(year,  1981, 2010)) %>% 
  mutate(offshore_basin_type = as.factor(offshore_basin_type)) %>% 
  mutate(offshore_basin_type = fct_recode(offshore_basin_type, 
                                          "summering" = "summering_offshore_basin_name",
                                          "wintering" = "wintering_offshore_basin_name"))  %>% 
  pivot_longer(cols = contains('temperature'),  names_to = "season", values_to = "temperature") %>% 
  mutate(season = str_remove(season, "_sea_surface_temperature")) 

##Select the temperature experienced by fish during the corresponding season 
#in winter
patrick.lambert's avatar
patrick.lambert committed
76
wint <- connections_trans %>% 
Poulet Camille's avatar
Poulet Camille committed
77
78
79
80
  filter(season %in% c("winter","spring"), offshore_basin_type == "wintering") %>% 
  select(inshore_basin_name,offshore_basin_name,year,season,temperature) 

#in summer
patrick.lambert's avatar
patrick.lambert committed
81
sum <- connections_trans %>% 
Poulet Camille's avatar
Poulet Camille committed
82
83
84
85
86
  filter(season %in% c("summer","fall"), offshore_basin_type == "summering") %>% 
  select(inshore_basin_name,offshore_basin_name,year,season,temperature)

#Combine the two files to build a summary dataframe
tempEffectInOffshore <- bind_rows(wint,sum) %>% 
87
88
89
  ungroup() %>% 
  arrange(inshore_basin_name, year,season) %>%
  group_by(inshore_basin_name,offshore_basin_name,season) %>% 
patrick.lambert's avatar
patrick.lambert committed
90
  summarise(temperature_O = mean(temperature), .groups = 'drop')
91
#mutate(temperatureEffect = temperatureEffect(Tmean, growPar$tempMinGrow, growPar$tempOptGrow, growPar$tempMaxGrow))
Poulet Camille's avatar
Poulet Camille committed
92
93
94
95
96
97

#delete intermediate files 

remove(sum,wint)


98
99
100
101
102

```

```{r Build the temperature expericenced by fish during their residency in river and inshore basins, echo = FALSE, warning=FALSE}

Poulet Camille's avatar
Poulet Camille committed
103
104
105
#TODO
#Contruire un tableau, du même style que celui-ci avec les temperatures du natal basins

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#season = c("spring","summer","fall","winter")
#zone = c("river_tempertaure","inshore_temperature") 

tempEfffectInRiver <- tempInriver  %>% 
  pivot_longer(cols = contains('temperature'),  names_to = "season", values_to = "river_temperature") %>% 
  mutate(season = str_remove(season, "_river_temperature")) %>% 
  inner_join(tempInInshore %>% 
               pivot_longer(cols = contains('temperature'),  names_to = "season", values_to = "inshore_temperature") %>% 
               mutate(season = str_remove(season, "_inshore_temperature")),
             by = c("basin_id", "basin_name", "year","season")) %>% 
  filter(between(year,  1981, 2010)) %>% 
  pivot_longer(c("river_temperature", "inshore_temperature"), names_to = "zone", values_to = "temperature") 

#Optimzation
tempRiver <- tempEfffectInRiver %>% 
patrick.lambert's avatar
patrick.lambert committed
121
  filter(season %in% c("spring","summer") & zone == "river_temperature")
122
123

tempInshore <- tempEfffectInRiver %>% 
patrick.lambert's avatar
patrick.lambert committed
124
  filter(season %in% c("fall","winter") & zone == "inshore_temperature")
125
126
127
128
129
130


tempEffectInFreshWat <- bind_rows(tempRiver,tempInshore) %>% 
  arrange(basin_name,year,season) %>% 
  select(basin_name,year, season, temperature) %>% 
  group_by(basin_name,season) %>% 
patrick.lambert's avatar
patrick.lambert committed
131
  summarize(temperature_RI = mean(temperature), .groups = 'drop') 
132
133
134
135
136
137

# tempEfffectInRiver %>% 
#   {bind_rows(
#     filter(season == "summer" & zone == "river_temperature"),
#     filter(season == "fall" & zone == "inshore_temperature"))
#     }
patrick.lambert's avatar
patrick.lambert committed
138

Poulet Camille's avatar
Poulet Camille committed
139
140
```

141

Poulet Camille's avatar
Poulet Camille committed
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
The growth parameters used in GR3D give the following curves when random and temperature effects are not considered.

```{r, include = TRUE, fig.cap = "Growth curve without temperature effect"}

dfGrowth <- data.frame(age = seq(0,19.75,.25), season = c("spring","summer","fall","winter"), basin_name = 'reference') %>% 
  mutate(gender = 'male',
         L = vonBertalanffyGrowth(age, growPar$lengthAtHatching, growPar$linfVonBertForMale, growPar$kOptForMale)) %>% 
  bind_rows(select(.data = ., age, season, basin_name) %>%  
              mutate(gender = 'female',
                     L = vonBertalanffyGrowth(age, growPar$lengthAtHatching, growPar$linfVonBertForFemale, growPar$kOptForFemale)))

dfGrowth %>% filter(basin_name == 'reference') %>% 
  ggplot(aes(x = age)) + 
  geom_line(aes(y = L, linetype = gender, color = basin_name)) +
  labs( x = 'Age (year)', y = 'Fork length (cm)') 


```

To optimize the growth parameters included in GR3D for the Amercian shad, we used values gave by Stich et al, 2020. 

```{r Growth from Stich et al, 2020, echo = FALSE, warning=FALSE}

#function to compute L0 based on growth paramters from Stich et al. 
#K, Linf and t0 were provided for the three metapopulations.
#Linf is in millimeters.

vonBertalanffyLengthAtHatch = function(Linf, K, t0){
  
patrick.lambert's avatar
patrick.lambert committed
171
  L0 = Linf * (1 - exp(t0*K))
Poulet Camille's avatar
Poulet Camille committed
172
173
174
175
176
177
178
179
180
181
182
183
  
  return(L0)
}

#vonBertalanffyTry(454,0.49,-0.21)

#parameters from Stich
Stich2020_sel <- read_csv("../NEA_calibration_offline/Stich_Table 9.csv") %>% 
  filter(parameter == "K" | parameter == "Linf"|parameter == "t0") %>% 
  dplyr::rename("catchment"="cachtment") %>% 
  select(parameter,catchment,mean) %>% 
  pivot_wider(names_from = parameter, values_from = mean) %>% 
patrick.lambert's avatar
patrick.lambert committed
184
  mutate(L0_theo = vonBertalanffyLengthAtHatch(Linf, K, t0)) 
Poulet Camille's avatar
Poulet Camille committed
185
186
#create a data frame and compute L at age with values from Stich

187
growthInOffshore <- expand_grid(basin_name = nea_riverBasinFeatures$basin_name , age = seq(0,9.75,.25)) %>%
Poulet Camille's avatar
Poulet Camille committed
188
189
190
191
192
193
194
195
196
197
198
199
200
  mutate(season = case_when (
    (age - floor(age)) == 0.00 ~ 'spring',
    (age - floor(age)) == 0.25 ~ 'summer',
    (age - floor(age)) == 0.50 ~ 'fall',
    (age - floor(age)) == 0.75 ~ 'winter'
  )) %>% inner_join(nea_riverBasinFeatures %>% select(basin_name, lat_outlet), by ='basin_name') %>%
  select(basin_name, lat_outlet, age, season)%>% 
  mutate(metapop = case_when
         (lat_outlet < 33.8 ~ "semelparous",
           lat_outlet >=33.8 & lat_outlet <= 41.28793 ~ "southern iteroparous",
           lat_outlet > 41.28793 ~ "northern iteroparous")) %>% 
  inner_join(Stich2020_sel, by = c("metapop" = "catchment")) %>% 
  group_by(basin_name,metapop) %>% 
patrick.lambert's avatar
patrick.lambert committed
201
  mutate(LStich = vonBertalanffyGrowth(age, L0_theo, Linf, K) / 10) %>% # in cm
Poulet Camille's avatar
Poulet Camille committed
202
  ungroup()
203

204
growthInBasin <-growthInOffshore %>% 
patrick.lambert's avatar
patrick.lambert committed
205
206
207
208
209
  inner_join(tempEffectInOffshore, 
             by = c("basin_name" = "inshore_basin_name", "season")) %>% 
  right_join(tempEffectInFreshWat, 
             by = c("basin_name","season"))

210
211
growthInBasin <- growthInBasin %>% 
  mutate(temperature_RIO = ifelse(season == "spring" & age == 0, temperature_RI,
patrick.lambert's avatar
patrick.lambert committed
212
213
                              ifelse(season == "summer" & age == 0.25, temperature_RI,
                                     ifelse(season == "fall" & age == 0.50, temperature_RI, temperature_O)))) %>% 
214
  arrange(basin_name,age,season) %>% 
215
  select(basin_name, offshore_basin_name, metapop,age, season, temperature_O, temperature_RIO, LStich)
216

Poulet Camille's avatar
Poulet Camille committed
217
218
219
#group_by(metapop,age,season) %>% 
#summarize(LStich = unique(LStich))

patrick.lambert's avatar
patrick.lambert committed
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
# NEW =====
# add presence and reorder level for metapop and season
growthInBasin <- growthInBasin %>% 
  inner_join(nea_presence %>% select(-basin_id), by ='basin_name') %>% 
  mutate(metapop = factor(metapop, levels = c('northern iteroparous',  'southern iteroparous', 'semelparous')),
         season = factor(season, levels = c('winter', 'spring', 'summer', 'fall')))
```

There is  1 river ( North (MA)) in Stich et al paper that is not referenced in our database. 
```{r join between Stich and GR3D database}
# NEW =====
Stich2020_sel <- Stich2020_sel %>% mutate(basin_name = catchment) %>% 
  mutate(basin_name = replace(basin_name, basin_name == "Tar-Pamlico", "Pamlico-Tar"),
         basin_name = replace(basin_name, basin_name == "Albemarle", "Roanoke"),
         basin_name = replace(basin_name, basin_name == "Upper Chesapeake Bay", "Susquehanna")) 

Stich2020_sel %>% filter(!catchment %in% c('coastwide', 'northern iteroparous', 'semelparous', 'southern iteroparous' )) %>%
  select(catchment, basin_name) %>% 
  left_join(growthInBasin %>% distinct(basin_name, metapop), by = c("basin_name") ) %>% 
  arrange(metapop) %>% 
  flextable() %>% 
  autofit()

```



```{r range of temperature}
# NEW =====
growthInBasin %>% 
  group_by(obs_1900_1950) %>% 
  summarise(Tmin = min(temperature_RIO), Tmax = max(temperature_RIO), .groups = 'drop')

growthInBasin %>% 
  inner_join(Stich2020_sel %>% select(basin_name), by ='basin_name') %>% 
  group_by(obs_1900_1950) %>% 
  summarise(Tmin = min(temperature_RIO), Tmax = max(temperature_RIO), .groups = 'drop')

growthInBasin %>% 
  filter(temperature_RIO < 1.6) %>% 
  distinct(basin_name, obs_1900_1950)
Poulet Camille's avatar
Poulet Camille committed
261
262
263
264
265
266
267
268
269
270
271
272
273
```

```{r Growth in GR3D with temperature effect, echo = FALSE, warning=FALSE}

#define parameters for both sex based on value from the XML file 

growParModifed <- growPar %>% 
  transmute(tempMinGrow,tempMaxGrow,tempOptGrow, sigmaDeltaLVonBert,lengthAtHatching,
            kOpt = mean(c(kOptForFemale,kOptForMale)),
            Linf = mean(c(linfVonBertForFemale,linfVonBertForMale))) 

#merge the temperature effect to compute the relative VBGF 
#growthInOffshore<-growthInOffshore %>% 
274
275
#inner_join(tempEffectInOffshore %>% 
#select(inshore_basin_name,season,temperatureEffect), by =c("basin_name" ="inshore_basin_name", "season" ="season")) %>% 
Poulet Camille's avatar
Poulet Camille committed
276
277
278
279
#select(basin_name,lat_outlet, metapop,age,season,LStich, temperatureEffect)

```

patrick.lambert's avatar
patrick.lambert committed
280
281
282
283
284
285
286
287
288
289
290
291
292

```{r}
# new ====
save(growParModifed,growthInBasin, nea_presence, Stich2020_sel,  file  = 'SOS.rdata' )

rm(list = ls())
load('SOS.rdata')
source("../GR3D_Rdescription/GR3Dfunction.R")
```



```{r growth optimisation function Camille, echo = FALSE, warning=FALSE}
Poulet Camille's avatar
Poulet Camille committed
293
294
295
296
297
298
299
300
#vector on paramters to get optimized 
#par[1] = Tmin
#par[2] = Topt
#par [3] = Tmax
#par[4] = L0
#par[5] = Linf
#par[6] = k

301
302
#Return data
computeGrowOpt = function(par,data){
Poulet Camille's avatar
Poulet Camille committed
303
304
305
306
307
308
  data <- data %>% arrange(age, basin_name) %>% 
    mutate(L = if_else(age == 0, par[4], 0))
  ages <- data %>% select(age) %>% distinct() %>% arrange(age) %>% unlist(use.names = FALSE)   
  
  
  data <- data %>% 
309
310
311
312
313
314
315
    arrange(basin_name,age, season) %>% 
    group_by(basin_name,age,season) %>% 
    mutate(temperatureEffect = temperatureEffect(temperature, 
                                                 Tmin = par[1],
                                                 Topt = par[2],
                                                 Tmax = par[3])) %>% 
    ungroup()
Poulet Camille's avatar
Poulet Camille committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  
  for (i in 2:length(ages)) {
    previousAge  = ages[i - 1]
    currentAge = ages[i]
    tempEffect = data %>% filter(age == currentAge) %>% select(temperatureEffect) %>% unlist(use.names = FALSE)
    previousL <- data  %>% filter(age == previousAge) %>% select(L) %>% unlist(use.names = FALSE)
    
    currentL <- vonBertalanffyWithNextIncrement(L = previousL, 
                                                L0 = par[4], 
                                                Linf = par[5], 
                                                K = par[6],
                                                timeStepDuration = currentAge - previousAge, 
                                                sigma = 0, 
                                                tempEffect = tempEffect)
    data = data %>% mutate(L =replace(L, age == ages[i], currentL)) 
    
  }
  
334
335
336
337
338
339
340
341
342
  
  return(data)
}

#computeGrowOpt(vecPar,growthInOffshore)

#Function to be optimized 
computeSSE = function(par,data){
  
patrick.lambert's avatar
patrick.lambert committed
343
344
  data = computeGrowOpt(par,data)
  
Poulet Camille's avatar
Poulet Camille committed
345
  SSE = data %>% 
346
347
348
349
350
351
    group_by(metapop) %>% 
    mutate(W = 1/n()) %>% 
    ungroup() %>% 
    transmute(squareError = W * ((L - LStich)^2)) %>% 
    summarise(sumSquare = sum(squareError)) %>% 
    unlist()
Poulet Camille's avatar
Poulet Camille committed
352
353
354
355
356
  #ungroup() %>% 
  #select(sumSquare) 
  #summarize(sum = sum(sumSquare))
  
  #return(SSE = as.vector(SSE$sum))
357
  return(SSE)
Poulet Camille's avatar
Poulet Camille committed
358
359
}

360
361
#computeSSE (vecPar, growthInOffshore)

patrick.lambert's avatar
patrick.lambert committed
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
```

```{r growth optimisation function Patrick, echo = FALSE, warning=FALSE}
computeGrowAllBasins = function(data, growPar){
  data <- data %>% arrange(age, basin_name) %>% 
    mutate(L = if_else(age == 0, growPar$lengthAtHatching, 0),
           temperatureEffect = temperatureEffect(temperature, growPar$tempMinGrow, growPar$tempOptGrow, growPar$tempMaxGrow))
  ages <- data %>% distinct(age) %>% arrange(age) %>% unlist(use.names = FALSE)   
  
  for (i in 2:length(ages)) {
    previousAge  = ages[i - 1]
    currentAge = ages[i]
    tempEffect = data %>% filter(age == currentAge) %>% select(temperatureEffect) %>% unlist(use.names = FALSE)
    previousL <- data %>% filter(age == previousAge) %>% select(L) %>% unlist(use.names = FALSE)
    
    currentL <- vonBertalanffyWithNextIncrement(L = previousL, 
                                                L0 = growPar$lengthAtHatching, 
                                                Linf = growPar$Linf, 
                                                K = growPar$kOpt,
                                                timeStepDuration = currentAge - previousAge, 
                                                sigma = growPar$sigmaDeltaLVonBert, 
                                                tempEffect = tempEffect)
    data = data %>% mutate(L =replace(L, age == ages[i], currentL) ) 
  }
  return(data)
}


par2parGrowth = function(par){
  parGrowth = data.frame(tempMinGrow = unname(par['tempOptGrow'] - par['epsilonMinus']),
                         tempOptGrow = unname(par['tempOptGrow']),
                         tempMaxGrow = unname(par['tempOptGrow'] + par['epsilonPlus']),
                         lengthAtHatching = unname(par['lengthAtHatching']),
                         Linf = unname(par['Linf']),
                         kOpt = unname(par['kOpt']), 
                         sigmaDeltaLVonBert = 0 )
  return(parGrowth)
}

#  avec Topt, Tmin = Topt - espsilonMinus, Tmax = Topt + epsilonPlus 
objFn = function(par, data) {
  growPar = par2parGrowth(par)
  data = computeGrowAllBasins(data, growPar) 
  
  SSE <-  data %>% 
    # fit only on one-season L to avoid smoothing of seasonal growth
    # filter(season == 'spring') %>%
    # mutate(age_year = floor(age)) %>% 
    # group_by(basin_name, metapop, age_year) %>% 
    # summarise( L = mean(L), LStich = mean(LStich), .groups = 'drop') %>% 
    # on pondère chaque basin versant pour garder des points équivalents entre les metapop
    group_by(metapop) %>% mutate(W = 1/n()) %>% ungroup() %>% 
    transmute(squareError = W * ((L - LStich)^2)) %>% summarise(sum(squareError)) %>% unlist(use.names = FALSE)
  return(SSE)
}

# équivalent de la fonction computeSSE (un peu plus rapide)
objFn_B = function(par, data) {
  growPar = enframe(par) %>% 
    pivot_wider() %>% mutate(sigmaDeltaLVonBert = 0)
  data = computeGrowAllBasins(data, growPar) 
  
  SSE <-  data %>% 
    # fit only on one-season L to avoid smoothing of seasonal growth
    # filter(season == 'spring') %>%
    # mutate(age_year = floor(age)) %>% 
    # group_by(basin_name, metapop, age_year) %>% 
    # summarise( L = mean(L), LStich = mean(LStich), .groups = 'drop') %>% 
    # on pondère chaque basin versant pour garder des points équivalents entre les metapop
    group_by(metapop) %>% mutate(W = 1/n()) %>% ungroup() %>% 
    transmute(squareError = W * ((L - LStich)^2)) %>% summarise(sum(squareError)) %>% unlist(use.names = FALSE)
  return(SSE)
}

# équivalent de la fonction objFn_B mais avec la possibilité de fixer des parametres
objFn_C = function(par, data, fixedPar) {
  growPar = enframe(c(par, fixedPar)) %>% pivot_wider() %>% 
    mutate(sigmaDeltaLVonBert = 0)
  data = computeGrowAllBasins(data, growPar) 
  
  SSE <-  data %>% 
    # fit only on one-season L to avoid smoothing of seasonal growth
    # filter(season == 'spring') %>%
    # mutate(age_year = floor(age)) %>% 
    # group_by(basin_name, metapop, age_year) %>% 
    # summarise( L = mean(L), LStich = mean(LStich), .groups = 'drop') %>% 
    # on pondère chaque basin versant pour garder des points équivalents entre les metapop
    group_by(metapop) %>% mutate(W = 1/n()) %>% ungroup() %>% 
    transmute(squareError = W * ((L - LStich)^2)) %>% summarise(sum(squareError)) %>% unlist(use.names = FALSE)
  return(SSE)
}

```

```{r starting parameters for optimisation}
# starting point from XML
vecPar = growParModifed %>% 
  select(tempMinGrow, tempOptGrow, tempMaxGrow, lengthAtHatching, Linf, kOpt) %>% 
  pivot_longer(tempMinGrow:kOpt, names_to = "parameter", values_to = "value") 
vecPar = as.vector(vecPar$value)

par0 =  c(tempOptGrow = vecPar[2], 
          epsilonMinus = vecPar[2] - vecPar[1], 
          epsilonPlus =  vecPar[3] - vecPar[2], 
          lengthAtHatching = vecPar[4], 
          Linf = vecPar[5], 
          kOpt = vecPar[6])

par0_B = c(tempMinGrow = vecPar[1], 
           tempOptGrow = vecPar[2], 
           tempMaxGrow = vecPar[3], 
           lengthAtHatching = vecPar[4], 
           Linf = vecPar[5], 
           kOpt = vecPar[6])

par0_C = par0_B[c('tempOptGrow', 'lengthAtHatching', 'Linf', 'kOpt')]
#fixedPar_C =  par0_B[c('tempMinGrow', 'tempMaxGrow')]
fixedPar_C =  c(tempMinGrow = 1.6,  tempMaxGrow = 27.9)

# # another starting point
# par0 =  c(tempOptGrow = 7, 
#           epsilonMinus = 5, 
#           epsilonPlus = 10,
#           lengthAtHatching = 5, 
#           Linf = 45, 
#           kOpt = .5)
# 
# par0_B = par2parGrowth(par0) %>% 
#   select(-sigmaDeltaLVonBert) %>% 
#   unlist()
# 
# par0_C = par0_B[c('tempOptGrow', 'lengthAtHatching', 'Linf', 'kOpt')]
# #fixedPar_C =  par0_B[c('tempMinGrow', 'tempMaxGrow')]
# fixedPar_C =  c(tempMinGrow = 1.6,  tempMaxGrow = 27.9)
# 
# vecPar = par2parGrowth(par0) %>% 
#   select(-sigmaDeltaLVonBert) %>% 
#   pivot_longer(tempMinGrow:kOpt, names_to = "parameter",values_to = "value") 
# vecPar = as.vector(vecPar$value)

```


```{r data for calibration}
dataCalibration = growthInBasin %>% 
  inner_join(Stich2020_sel %>% select(basin_name), by ='basin_name') %>% 
  mutate(temperature = temperature_RIO)
```

patrick.lambert's avatar
patrick.lambert committed
511
512
513

Avec uniqument le jeu de Stich cela part en vrille avec objFn_B et computeSSE ( sans donner les mémes resultats argh) avec Topt < Tmin. On a des résulats corrects avec objFn ( avec Topt +- epsilon). Mais comme on va utiliser l'option en fixant Tmin et Tmax

patrick.lambert's avatar
patrick.lambert committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
```{r optimisation run}

tic('objFn')
#  avec Topt, Tmin = Topt - espsilonMinus, Tmax = Topt + epsilonPlus 
res <- optim(par = par0, 
             fn = objFn, 
             data = dataCalibration,
             # method = "L-BFGS-B",
             # lower = c(2, 2, 2, 2, 30, 0.2),
             # upper = c(20, 20, 20, 20, 100, 2),
             control = list(trace = 0, 
                            #parscale = c(1,1,1,1,1,0.1),
                            maxit = 1000))
toc()
Poulet Camille's avatar
Poulet Camille committed
528

patrick.lambert's avatar
patrick.lambert committed
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

tic('objFn_B')
# équivalent de la fonction computeSSE (un peu plus rapide)
res_B <- optim(par = par0_B, 
               fn = objFn_B, 
               data = dataCalibration,
               # method = "L-BFGS-B",
               # lower = c(0,2,10,2,30,0.2),
               # upper = c(5,10,30,20,100,2),
               control = list(trace = 0, 
                              maxit = 1000))
toc()


tic('objFn_C')
# équivalent de la fonction objFn_B mais avec la possibilité de fixer des parametres
res_C <- optim(par = par0_C, 
               fn = objFn_C, 
               data = dataCalibration,
               fixedPar = fixedPar_C,
               # method = "L-BFGS-B",
               # lower = c(0,2,10,2,30,0.2),
               # upper = c(5,10,30,20,100,2),
               control = list(trace = 0, 
                              maxit = 1000))
toc()

tic('computeSSE')
Poulet Camille's avatar
Poulet Camille committed
557
growth_optimal <- optim(c(Tmin = vecPar[1],
558
559
560
561
562
                          Topt = vecPar[2],
                          Tmax = vecPar[3],
                          L0 = vecPar[4],
                          Linf = vecPar[5],
                          K = vecPar[6]),
patrick.lambert's avatar
patrick.lambert committed
563
564
565
566
567
568
569
                        fn = computeSSE, 
                        data = dataCalibration,
                        # lower = c(0,0,0,0,0,0),
                        # method = "L-BFGS-B",
                        control = list(trace = 0, 
                                       #parscale = c(1,1,1,0.1,1,0.01), 
                                       maxit = 10000))
Poulet Camille's avatar
Poulet Camille committed
570
571
572

toc()

patrick.lambert's avatar
patrick.lambert committed
573
574
575
576
577
578
579
580
581
582
583
584
# results
par2parGrowth(res$par)
res_B$par
c(res_C$par, fixedPar_C)
growth_optimal$par

res$value
res_B$value
res_C$value
growth_optimal$value


585
#Optimized parameters 
Poulet Camille's avatar
Poulet Camille committed
586
587
588
589
growParOptim <- data.frame(value = growth_optimal$par) %>% 
  rownames_to_column("parameter")

#Rdata
Poulet Camille's avatar
Poulet Camille committed
590
#save(growth_optimal, res, res_B, res_C, file = "growth_optimal.RData")
Poulet Camille's avatar
Poulet Camille committed
591
592
593
594
595
596

#csv
write_csv(growParOptim, "growParOptim.csv")

```

patrick.lambert's avatar
patrick.lambert committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615

```{r graph to present results}
# NEW 
# ===============================================================================
#parGrowthUsed = c(res_B$par) %>% enframe() %>% pivot_wider()
parGrowthUsed = c(res_C$par, fixedPar_C) %>% enframe() %>% pivot_wider()
# ================================================================================

dataVerif <- computeGrowAllBasins(data = dataCalibration, 
                                  growPar = parGrowthUsed %>% mutate(sigmaDeltaLVonBert = 0) )

# graph by meta pop
dataVerif %>% group_by(metapop, age) %>% 
  summarise(L = mean(L), LStich = mean(LStich), .groups = 'drop') %>% 
  mutate(metapop = factor(metapop, levels = c('northern iteroparous',  'southern iteroparous', 'semelparous'))) %>% 
  ggplot(aes(x = age, color = metapop)) + 
  geom_line(aes(y = LStich)) +
  geom_line(aes(y = L)) 

Poulet Camille's avatar
Poulet Camille committed
616
save(dataVerif, growth_optimal,res, res_B, res_C, file = "growth_optimal.RData")
patrick.lambert's avatar
patrick.lambert committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637

# thermal response curve with average seasonal temperature experienced by fish 
dataTemperatureMetapop <- dataVerif %>% 
  group_by(metapop, season) %>% 
  summarise(temperature = mean(temperature), .groups = 'drop') %>% 
  mutate(tempEffect = temperatureEffect(temperature, parGrowthUsed$tempMinGrow, parGrowthUsed$tempOptGrow, parGrowthUsed$tempMaxGrow ),
         metapop = factor(metapop, levels = c('northern iteroparous',  'southern iteroparous', 'semelparous')),
         season = factor(season, levels = c('winter', 'spring', 'summer', 'fall')))


data.frame(temperature = seq(0,40,0.1)) %>% 
  mutate(tempEffect = temperatureEffect(temperature, parGrowthUsed$tempMinGrow, parGrowthUsed$tempOptGrow, parGrowthUsed$tempMaxGrow )) %>% 
  ggplot() + ylim(0,1)+
  geom_line(aes(x = temperature, y = tempEffect)) +
  geom_point(data = dataTemperatureMetapop, aes(x = temperature, y = tempEffect, color = metapop, shape = season ), size=3) 

```




Poulet Camille's avatar
Poulet Camille committed
638
639
```{r draw growth curves}

Poulet Camille's avatar
Poulet Camille committed
640
#load("growth_optimal.RData")
Poulet Camille's avatar
Poulet Camille committed
641

642
growthCurves = computeGrowOpt(growth_optimal$par, growthInOffshore)
643
save(growthCurves, file = "growthCurves.RData")
Poulet Camille's avatar
Poulet Camille committed
644
645
646
647
648
649
650

#growth curve according to metapopulations 
growthCurves %>% 
  ggplot(aes(x = age)) + 
  geom_line(aes(y = L, col = metapop), linetype = "dashed") +
  geom_line(aes(y = LStich, col = metapop))+
  labs( x = 'Age (year)', y = 'Fork length (cm)', color = "Metapopulation (Stich)") +
651
652
653
  #scale_x_continuous(limits = c(0, 10)) +
  geom_hline(yintercept = 45, color = "pink", linetype =1)+ #female
  geom_hline(yintercept = 40, color = "lightblue", linetype = 1)+
654
655
  geom_vline(aes(age), xintercept = 8, linetype = 1)+
  facet_wrap(.~ metapop, ncol =1)
Poulet Camille's avatar
Poulet Camille committed
656
657
658
659
660

#temperature effect 
temp <- seq(0,45,0.1)

growthCurves %>% 
661
  ggplot(aes(x = temperature)) + 
Poulet Camille's avatar
Poulet Camille committed
662
663
664
665
666
667
668
669
670
671
  geom_line(aes(y = temperatureEffect)) +
  geom_jitter(aes(y = temperatureEffect, col = metapop))


#long fromat for growParOptim
growParOptim <-growParOptim %>% 
  pivot_wider(names_from = parameter, values_from = value)

df <- data.frame(temperature = temp,
                 temperatureEffect = temperatureEffect(temp,
672
673
674
                                                       growParOptim$Tmin,
                                                       growParOptim$Topt,
                                                       growParOptim$Tmax))
Poulet Camille's avatar
Poulet Camille committed
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696

df %>% 
  ggplot(aes(x = temperature)) + 
  geom_line(aes(y = temperatureEffect)) +
  geom_point(aes(y = temperatureEffect), col = 'red', alpha = 0.4)

```