7-Survival_After_Repro_Logit2.R 3.76 KB
Newer Older
1
library(tydiverse)
Poulet Camille's avatar
Poulet Camille committed
2
library(dplyr)
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

#x50 = Tref
#x5 = 25°C, ie the temperature above which there is no spanwer survival after reproduction (based on Limbrug et al, 2003)


logit2 = function(x, x50, x5){
  return( 1/ (1+exp((log(19)/(x5-x50))*(x-x50))))
}


logit2 = function(Triver, Tref,minTempForIteroparity){
  return( 1/ (1+exp((log(19)/(minTempForIteroparity-Tref))*(temp-Tref))))
}


#-------------------------------------------
data.frame(temperature = seq(0,40,.1)) %>% 
  mutate(pctSurvieMax = logit2(temperature, x50=20, x5=25),
         survie = pctSurvieMax *.2) %>% 
  ggplot(aes(x=temperature, y = survie )) + 
  geom_path()


> log(19)/(25-19.9)

parLogit = c(Tref = 19.9, minTempForIteroparity = 25)

optim_logit2_fn <- function(par,temperature, target, logitPar){
  
 
Poulet Camille's avatar
Poulet Camille committed
33
  survival = logit2(Triver= temperature, Tref = logitPar['Tref'], 
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
                    minTempForIteroparity = logitPar['minTempForIteroparity']) * par[1]
  
  squareDistance = (mean(survival) - target)^2
  
  return(squareDistance)
}


optim_logit2_fn(par = 0.2,temperature = tempInRiverAvg$summer_river_temperature,
                target = 0.1, logitPar = parLogit)

optim_logit2 <- optim(c(coeffb = 0.2),
                      optim_logit2_fn, 
                      temperature = tempInRiverAvg$summer_river_temperature, 
                      target = 0.1, 
                      logitPar = parLogit,
                      method = "L-BFGS-B")

Poulet Camille's avatar
Poulet Camille committed
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
#--------------------- compute survival after reproduction

tempInriverAvg = tempInriver %>% 
    filter(between(year,  1900, 1950)) %>% 
    group_by(basin_id, basin_name) %>% 
    mutate(spring_avg = mean(spring_river_temperature),
           summer_avg = mean(summer_river_temperature),
           fall_avg = mean(fall_river_temperature),
           winter_avg = mean(winter_river_temperature))



survivalAfterReproductionByYearInSummer <- tempInriverAvg %>% 
  mutate(survival = (logit2(summer_river_temperature,Tref = 19.9,minTempForIteroparity = 25) * 0.18))


survivalAfterReproductionByYearInSummer = survivalAfterReproductionByYearInSummer %>% 
  right_join(nea_riverBasinFeatures %>% select(basin_name,lat_outlet), by = "basin_name") %>% 
  arrange(lat_outlet) %>% 
  mutate(metapop = case_when
         (lat_outlet <=33.8 ~ "Semelparous",
           lat_outlet >33.8 & lat_outlet <= 41.28793 ~ "SouthIteroparous",
           lat_outlet > 41.28793 ~ "NorthIteroparous")) %>% 
  group_by(basin_name) %>%  
  mutate(mean_surv = mean(survival))

survivalAfterReproductionByYearInSummer%>% 
  filter(basin_name %in% basinSel) %>% 
  group_by(basin_name) %>% 
  summarize(meanTemp = mean(summer_river_temperature))


survivalAfterReproductionByYearInSummer%>% 
  filter(basin_name %in% basinSel) %>% 
  select(basin_name,metapop) %>% 
  distinct()



#plot 
survivalAfterReproductionByYearInSummer%>% 
  filter(basin_name %in% basinSel) %>% 
  #mutate(year = as.factor(year)) %>% 
  #filter(metapop == "Semelparous") %>% 
  ggplot(aes(fct_reorder(basin_name, lat_outlet), survival, color = year))+
  #geom_line(aes(spring_avg, sp_spg), col = "orange")+
  #geom_line(aes(spring_avg, sp_sum), col = "red")+
  #geom_line(aes(spring_avg,surv))+
  geom_jitter() +
  geom_point(aes(basin_name,mean_surv), col = 'red')+
  # geom_text(data = survivalBeforeReproduction_long %>%  filter(basin_name %in% basinSel), aes(label = basin_name, color = metapop), size = 3, check_overlap = TRUE)+
  # labs(x = "Mean river temperature (°C)", 
  #      y = "Spawner survival before reproduction (100 spawners)") +
  #facet_wrap(~surv_type, nrow = 2, ncol = 3)+
  theme(axis.text.x = element_text(angle = 90, size = 12),
        axis.text.y = element_text(size = 12))+
  #geom_hline(yintercept = 0.20, col ="red")+
  xlab('Basin name (ordered along a latitudinal gradient)')+
  ylab('Spawner survival after reproduction')

112
113
114
115
116