Commit 327a1ac2 authored by patrick.lambert's avatar patrick.lambert
Browse files

new approach to calibrate maturation and growth simultaneously

No related merge requests found
Showing with 231 additions and 31 deletions
+231 -31
...@@ -28,10 +28,19 @@ load('SOS.rdata') ...@@ -28,10 +28,19 @@ load('SOS.rdata')
``` ```
```{r} ```{r}
source("../GR3D_Rdescription/GR3D_NEA_XML_parameters.R")
growParXML = data.frame(t(unlist(fishXML[[1]][["processes"]][["processesEachStep"]][["species.Grow"]]))) %>%
select(-synchronisationMode ) %>%
mutate(linfVonBertForMale = fishXML[[1]][["linfVonBertForMale"]],
linfVonBertForFemale = fishXML[[1]][["linfVonBertForFemale"]],
lengthAtHatching = fishXML[[1]][["lengthAtHatching"]],
lFirstMaturityForMale = fishXML[[1]][["lFirstMaturityForMale"]],
lFirstMaturityForFemale = fishXML[[1]][["lFirstMaturityForFemale"]]) %>%
mutate_all(~as.numeric(as.character(.)))
growPar <- Stich2020_sel %>% filter(catchment == "semelparous") %>% growParStich <- Stich2020_sel %>% filter(catchment == "semelparous") %>%
select(Linf, K, L0_theo) %>% select(Linf, K, L0_theo) %>%
mutate(tempMinGrow = 1.6, tempOptGrow = 5, tempMaxGrow = 27.9, mutate(tempMinGrow = 1.6, tempOptGrow = 5, tempMaxGrow = 27.9,
Linf =Linf/10, lengthAtHatching= L0_theo/10, kOpt = K, Linf =Linf/10, lengthAtHatching= L0_theo/10, kOpt = K,
...@@ -49,6 +58,10 @@ temperaturePattern <- growthInBasin %>% ...@@ -49,6 +58,10 @@ temperaturePattern <- growthInBasin %>%
# )) %>% # )) %>%
select(age, season,temperature) select(age, season,temperature)
```
```{r local function}
# generate a cohort of length trajectories # generate a cohort of length trajectories
computeMultipleLengthTrajectories = function(temperaturePattern, Nind = 10, growPar){ computeMultipleLengthTrajectories = function(temperaturePattern, Nind = 10, growPar){
ages = temperaturePattern$age ages = temperaturePattern$age
...@@ -80,19 +93,64 @@ computeMultipleLengthTrajectories = function(temperaturePattern, Nind = 10, grow ...@@ -80,19 +93,64 @@ computeMultipleLengthTrajectories = function(temperaturePattern, Nind = 10, grow
return(res) return(res)
} }
computeMultipleLengthTrajectoriesWithRandomSeed = function(temperaturePattern,
Nind = 10,
growPar,
RNGseed =1){
set.seed(RNGseed)
ages = temperaturePattern$age
res <- expand_grid(ind = seq.int(Nind), age = ages) %>%
mutate(random = rnorm(Nind * length(ages))) %>%
inner_join(temperaturePattern, by = 'age') %>%
arrange(age, ind) %>%
mutate(temperatureEffect = temperatureEffect(temperature,
growPar$tempMinGrow,
growPar$tempOptGrow,
growPar$tempMaxGrow),
L = if_else(age == 0, growPar$lengthAtHatching, 0))
for (i in 2:length(ages)) {
previousAge = ages[i - 1]
currentAge = ages[i]
tempEffect <- res %>% filter(age == currentAge) %>%
select(temperatureEffect) %>% unlist(use.names = FALSE)
previousL <- res %>% filter(age == previousAge) %>%
select(L) %>% unlist(use.names = FALSE)
rnd <- res %>% filter(age == currentAge) %>% select(random) %>% unlist(use.names = FALSE)
currentL <- vonBertalanffyWithRandomVector(L = previousL,
L0 = growPar$lengthAtHatching,
Linf = growPar$Linf,
K = growPar$kOpt,
timeStepDuration = currentAge - previousAge,
randomVector = rnd,
sigma = growPar$sigmaDeltaLVonBert,
tempEffect = tempEffect)
res = res %>% mutate(L = replace(L, age == ages[i], currentL) )
}
return(res)
}
tic() vonBertalanffyWithRandomVector = function(L, L0, Linf, K, timeStepDuration, randomVector, sigma, tempEffect ){
computeMultipleLengthTrajectories(temperaturePattern, Nind = 1000, growPar) %>% if (sigma == 0) {
filter(season == 'spring') mu = log((Linf - L) * (1 - exp(-K * timeStepDuration)))
toc() increment = exp(mu)
}
else {
mu = log((Linf - L) * (1 - exp(-K * timeStepDuration))) - (sigma * sigma) / 2
increment = exp(randomVector * sigma + mu)
}
L = L + increment * tempEffect
return(L)
}
computeOgive = function(yearlyLengthTrajectories, lengthAtMaturity) { computeOgive = function(lengthTrajectories, lengthAtMaturity) {
ogive <- yearlyLengthTrajectories %>% ogive <- lengthTrajectories %>%
distinct(age) %>% arrange(age) %>% distinct(age) %>% arrange(age) %>%
left_join(yearlyLengthTrajectories %>% filter(L > lengthAtMaturity) %>% left_join(lengthTrajectories %>% filter(L > lengthAtMaturity) %>%
group_by(ind) %>% group_by(ind) %>%
summarise(age = min(age), .groups = 'drop') %>% summarise(age = min(age), .groups = 'drop') %>%
group_by(age) %>% group_by(age) %>%
...@@ -100,14 +158,26 @@ computeOgive = function(yearlyLengthTrajectories, lengthAtMaturity) { ...@@ -100,14 +158,26 @@ computeOgive = function(yearlyLengthTrajectories, lengthAtMaturity) {
by = 'age') %>% by = 'age') %>%
replace_na(list(mature = 0)) %>% replace_na(list(mature = 0)) %>%
mutate(immature = sum(mature) - cumsum(mature), mutate(immature = sum(mature) - cumsum(mature),
p = if_else(mature + immature == 0, 1, mature/(mature + immature))) %>% p = if_else(mature + immature == 0, 0, mature/(mature + immature))) %>%
select(age, p) select(age, p)
return(ogive) return(ogive)
} }
```
```{r test}
tic()
computeMultipleLengthTrajectories(temperaturePattern, Nind = 1000, growParStich) %>%
filter(season == 'spring')
toc()
tic()
computeMultipleLengthTrajectoriesWithRandomSeed(temperaturePattern, Nind = 100000, growParStich) %>%
filter(season == 'spring')
toc()
computeMultipleLengthTrajectories(temperaturePattern %>% filter(age < 10) , computeMultipleLengthTrajectories(temperaturePattern %>% filter(age < 10) ,
Nind = 10000, growPar) %>% Nind = 10000, growPar) %>%
...@@ -116,21 +186,18 @@ computeMultipleLengthTrajectories(temperaturePattern %>% filter(age < 10) , ...@@ -116,21 +186,18 @@ computeMultipleLengthTrajectories(temperaturePattern %>% filter(age < 10) ,
``` ```
```{r ogive varaibility} ```{r ogive variability}
tic(msg = 'ogive variability')
replicate(10, computeMultipleLengthTrajectories(temperaturePattern %>% filter(age < 10) , replicate(10, computeMultipleLengthTrajectories(temperaturePattern %>% filter(age < 10) ,
Nind = 100000, growPar) %>% Nind = 100000, growPar) %>%
filter(season == 'spring') %>% filter(season == 'spring') %>%
computeOgive(lengthAtMaturity = 35) %>% select(p) %>% unlist(use.names = FALSE)) computeOgive(lengthAtMaturity = 35) %>% select(p) %>% unlist(use.names = FALSE))
toc()
``` ```
```{r growth curve envelope}
# envelope of growth curves
```{r growth curve envelop} computeMultipleLengthTrajectories(temperaturePattern , Nind = 1000, growParStich) %>%
# envelop of growth curves
computeMultipleLengthTrajectories(temperaturePattern , Nind = 1000, growPar) %>%
group_by(age) %>% group_by(age) %>%
summarise(Lmin = min(L), summarise(Lmin = min(L),
L025 = quantile(L, .25), L025 = quantile(L, .25),
...@@ -149,11 +216,11 @@ computeMultipleLengthTrajectories(temperaturePattern , Nind = 1000, growPar) %>% ...@@ -149,11 +216,11 @@ computeMultipleLengthTrajectories(temperaturePattern , Nind = 1000, growPar) %>%
# verify that all L < Linf # verify that all L < Linf
computeMultipleLengthTrajectories(temperaturePattern , Nind = 10000, growPar) %>% computeMultipleLengthTrajectories(temperaturePattern , Nind = 10000, growParStich) %>%
filter(L>growPar$Linf) filter(L>growParStich$Linf)
``` ```
```{r} ```{r optimisation}
ogiveObs = data.frame(age = seq.int(9), p = c(0, 0, 0.01, 0.09, 0.33, 0.63, 0.92, 1, 1)) ogiveObs = data.frame(age = seq.int(9), p = c(0, 0, 0.01, 0.09, 0.33, 0.63, 0.92, 1, 1))
lengthTrajectoriesInSpring = computeMultipleLengthTrajectories(temperaturePattern, Nind = 1000, growPar) %>% lengthTrajectoriesInSpring = computeMultipleLengthTrajectories(temperaturePattern, Nind = 1000, growPar) %>%
...@@ -161,18 +228,26 @@ lengthTrajectoriesInSpring = computeMultipleLengthTrajectories(temperaturePatter ...@@ -161,18 +228,26 @@ lengthTrajectoriesInSpring = computeMultipleLengthTrajectories(temperaturePatter
# calibrate only lengthAtMaturity # calibrate only lengthAtMaturity
objFn_A = function(par, ogiveObs, yearlyLengthTrajectories){ objFn_A = function(par, ogiveObs, yearlyLengthTrajectories){
SSE = ogiveObs %>% SSE = ogiveObs %>%
inner_join(computeOgive(yearlyLengthTrajectories, inner_join(computeOgive(yearlyLengthTrajectories,
lengthAtMaturity = par[1]), lengthAtMaturity = par[1]),
by = 'age', suffix = c('.obs','.pred')) %>% by = 'age', suffix = c('.obs','.pred')) %>%
mutate(squareError = (p.obs - p.pred)^2) %>% mutate(squareError = (p.obs - p.pred)^2) %>%
summarise(SSE = sum(squareError)) %>% summarise(SSE = sum(squareError)) %>%
unlist(use.names = FALSE) unlist(use.names = FALSE)
return(SSE) return(SSE)
# epsilon = 1e-6 # to avoid log(0)
# minusSumLogL = ogiveObs %>%
# inner_join(computeOgive(yearlyLengthTrajectories,
# lengthAtMaturity = par[1]),
# by = 'age', suffix = c('.obs','.pred')) %>%
# summarise(minusSumLogL = - sum(p.obs * log(p.pred + epsilon) + (1 - p.obs) * log(1 - p.pred + epsilon))) %>%
# unlist(use.names = FALSE)
# return(minusSumLogL)
} }
objFn_A(par = c(lengthAtMaturity = 35), objFn_A(par = c(lengthAtMaturity = 40),
ogiveObs, ogiveObs,
yearlyLengthTrajectories = lengthTrajectoriesInSpring) yearlyLengthTrajectories = lengthTrajectoriesInSpring)
...@@ -191,12 +266,12 @@ computeOgive(yearlyLengthTrajectories = lengthTrajectoriesInSpring, ...@@ -191,12 +266,12 @@ computeOgive(yearlyLengthTrajectories = lengthTrajectoriesInSpring,
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
# calibrate lengthAtMaturity and Kopt # calibrate lengthAtMaturity and Kopt
objFn_B = function(par, growPar, ogiveObs, temperaturePattern, Nind = 100000, season = 'spring', sigmaDeltaLVonBert = .2){ objFn_B = function(par, growPar, ogiveObs, temperaturePattern, Nind = 100000, seasonSelect = 'spring', sigmaDeltaLVonBert = .2){
growPar$K = par[2] growPar$K = par[2]
growPar$sigmaDeltaLVonBert = sigmaDeltaLVonBert growPar$sigmaDeltaLVonBert = sigmaDeltaLVonBert
ogivePredict <- computeMultipleLengthTrajectories(temperaturePattern , Nind = Nind, growPar) %>% ogivePredict <- computeMultipleLengthTrajectories(temperaturePattern , Nind = Nind, growPar) %>%
filter(season == season) %>% filter(season == seasonSelect) %>%
computeOgive(lengthAtMaturity = par[1]) computeOgive(lengthAtMaturity = par[1])
SSE = ogiveObs %>% SSE = ogiveObs %>%
...@@ -252,4 +327,129 @@ computeMultipleLengthTrajectories(temperaturePattern, ...@@ -252,4 +327,129 @@ computeMultipleLengthTrajectories(temperaturePattern,
geom_line(aes(y = Lmed)) + geom_line(aes(y = Lmed)) +
labs(x = 'Age (y)', y = 'Length (cm)') + labs(x = 'Age (y)', y = 'Length (cm)') +
geom_abline(slope =0, intercept = 44) geom_abline(slope =0, intercept = 44)
``` ```
\ No newline at end of file
```{r calibration K and lengthAtMaturity by gender with ogive}
#par = res_D$par
objFn_D = function(par, fixedPar, ogivesObs, parGrowRef, temperaturePattern,
Nind = 1000, seasonSelected = 'spring',
RNGseed =1) {
fullPar <- enframe(c(par, fixedPar)) %>% pivot_wider()
parFemale = fullPar %>% select(tempMinGrow, tempOptGrow, tempMaxGrow,
lengthAtHatching,
Linf = linfVonBert,
kOpt = kOptForFemale,
sigmaDeltaLVonBert,
lengthAtMaturity = lFirstMaturityForFemale)
parMale = fullPar %>% select(tempMinGrow, tempOptGrow, tempMaxGrow,
lengthAtHatching,
Linf = linfVonBert,
kOpt = kOptForMale,
sigmaDeltaLVonBert,
lengthAtMaturity = lFirstMaturityForMale)
# SSE for female ogive
ogivePredictFemale <- computeMultipleLengthTrajectoriesWithRandomSeed(temperaturePattern ,
Nind = Nind,
growPar = parFemale,
RNGseed = RNGseed) %>%
filter(season == seasonSelected) %>%
computeOgive(lengthAtMaturity = parFemale$lengthAtMaturity) %>%
# add a penalty when p(max(age)) < 1
mutate(p = if_else(age == max(age) & p < 1, 10*(p-1)^2, p))
SSEfemale = ogivesObs %>% select(age, p = p_female) %>%
inner_join(ogivePredictFemale,
by = 'age', suffix = c('.obs','.pred')) %>%
mutate(squareError = (p.obs - p.pred)^2) %>%
summarise(SSE = sum(squareError)) %>%
unlist(use.names = FALSE)
# SSE for male ogive
ogivePredictMale <- computeMultipleLengthTrajectoriesWithRandomSeed(temperaturePattern ,
Nind = Nind,
growPar = parMale,
RNGseed = RNGseed) %>%
filter(season == seasonSelected) %>%
computeOgive(lengthAtMaturity = parMale$lengthAtMaturity) %>%
# add a penalty when p(max(age)) < 1
mutate(p = if_else(age == max(age) & p < 1, 10*(p-1)^2, p))
SSEmale = ogivesObs %>% select(age, p = p_male) %>%
inner_join(ogivePredictMale,
by = 'age', suffix = c('.obs','.pred')) %>%
mutate(squareError = (p.obs - p.pred)^2) %>%
summarise(SSE = sum(squareError)) %>%
unlist(use.names = FALSE)
### SSE for growth curve
femaleCurve <- computeMultipleLengthTrajectoriesWithRandomSeed(temperaturePattern ,
Nind = 1,
growPar = parFemale %>% mutate(sigmaDeltaLVonBert=0) ,
RNGseed = RNGseed) %>%
filter(season == seasonSelected) %>%
select(age, Lfemale = L)
maleCurve <- computeMultipleLengthTrajectoriesWithRandomSeed(temperaturePattern ,
Nind = 1,
growPar = parMale %>% mutate(sigmaDeltaLVonBert=0) ,
RNGseed = RNGseed) %>%
filter(season == seasonSelected) %>%
select(age, Lmale = L)
SCE1growth <-
femaleCurve %>%
inner_join(maleCurve, by = 'age') %>%
mutate(Lref = vonBertalanffyGrowth(age, parGrowRef$L0, parGrowRef$Linf, parGrowRef$K),
L2genders = (Lfemale + Lmale)/2,
SE = ((Lfemale + Lmale)/2 - Lref)^2 ) %>%
summarise(SSE = sum(SE)) %>% unlist(use.names = FALSE)
return((SSEmale + SSEfemale)*10 + SCE1growth)
}
ogivesObs = data.frame(age = seq.int(9), p_female = c(0, 0, 0.01, 0.09, 0.33, 0.63, 0.92, 1, 1),
p_male = c(0, 0, 0.01, 0.15, 0.45, 0.75, 0.92, 1, 1))
#TODO verifier si ce sont bien les parametres de Stich
parGrowRef = growParStich %>% select(L0 = lengthAtHatching, K, Linf)
par = c(tempOptGrow = 5,
kOptForFemale = 0.32,
kOptForMale = .21,
linfVonBert = 76,
lFirstMaturityForMale = 40,
lFirstMaturityForFemale = 45,
sigmaDeltaLVonBert = .2)
fixedPar = c(tempMinGrow = 1.6,
tempMaxGrow = 27.9,
lengthAtHatching = parGrowRef$L0 )
# objFn_D(par = par,
# fixedPar = fixedPar,
# ogivesObs = ogivesObs,
# parGrowRef = parGrowRef,
# temperaturePattern = temperaturePattern,
# Nind = 5000)
res_D <- optim(par = par,
fn = objFn_D,
fixedPar = fixedPar,
ogivesObs = ogivesObs,
parGrowRef = parGrowRef,
temperaturePattern = temperaturePattern,
Nind = 5000,
RNGseed =1,
control = list(trace = 1,
maxit = 1000))
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment