-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCspTempToggle.R
164 lines (138 loc) · 6.9 KB
/
CspTempToggle.R
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
#############################
# C sp temperature toggle
############################
#Code for HPC
#library(lubridate, lib.loc = "/home/ib/kurthena/R_libs/4.2.1")
library(lubridate)
source("C_1sp_Model.R")
source("1spFunctions.R")
source("NegExpSurv.R")
Time <- c(1:36500)
Date <- rep(1:365, times = 100)
Day <- seq(as.Date("2022-01-01"), as.Date("2121-12-31"), by="days")
# find and remove leap days
find_leap = function(x){
day(x) == 29 & month(x) == 2
}
Day <- Day[which(find_leap(Day) == F)]
Temperature <- -7.374528 * (cos(((2*pi)/365)*Date)) + (-1.649263* sin(2*pi/(365)*Date)) + 13.956243
temp <- as.data.frame(cbind(Time, Day, Temperature))
temp$Day <- as.Date(temp$Day, origin= "1970-01-01")
colnames(temp) <- c("Time", "Date", "Temperature")
temp <- TimestepTemperature(temp)
temp <- temp[c(1,3)]
Year <- year(temp$dts)
uYear <- unique(Year)
Month <- month(temp$dts)
discharge <- rep(0.1, time = length(temp$dts))
temp_regime <- vector()
temp_means <- vector()
temp_seq <- seq(-10, 10, by = 1)
for (te in 1:length(temp_seq)){
temp$Temperature <- temp$Temperature + temp_seq[te]
temp_regime[te] <- mean(temp$Temperature)
out <- Cmodel(discharge, temp, baselineK = 10000, disturbanceK = 40000, Qmin = 0.25, extinct = 50, iteration = 2, peaklist = 0, peakeach = length(temp$Temperature))
temp$Temperature <- temp$Temperature - temp_seq[te]
#means.list.C <- out[-c(1:250)]
means.list.C <- mean.data.frame(out, burnin = 250, iteration = 2)
#temp_means[te] <- mean(means.list.C)
temp_means[te] <- mean(means.list.C$mean.abund)
}
c_temp_adjust_df <- as.data.frame(cbind(temp_regime, temp_means, rep("C", times = length(temp_means))))
c_temp_adjust_df$temp_regime <- as.numeric(c_temp_adjust_df$temp_regime)
c_temp_adjust_df$temp_means <- as.numeric(c_temp_adjust_df$temp_means)
binary <- as.integer(c_temp_adjust_df$temp_means != 0)
stage3s_means <- vector()
size_means <- vector()
for (te in 1:length(temp_seq)){
temp$Temperature <- temp$Temperature + temp_seq[te]
temp_regime[te] <- mean(temp$Temperature)
out <- Cmodel(discharge, temp, baselineK = 10000, disturbanceK = 40000, Qmin = 0.25, extinct = 50, iteration = 1, peaklist = 0, peakeach = length(temp$Temperature), stage_output = "size")
temp$Temperature <- temp$Temperature - temp_seq[te]
size_means[te] <- colMeans(out)
size_means[te] <- sum(0.0056*(size_means[te])^2.839)
stage3s_means[te] <- mean(out[,3,1])
}
stage3s_means <- 0.0056*(stage3s_means)^2.839 # multiply relative size (which is also biologically plausible) by Benke et al 1999 Table 2 a and b params (M(mg) = aL^b)
stage3s_means <- stage3s_means * binary
stage3s_means[which(stage3s_means == 0)] <- NA
c_size_df <- as.data.frame(cbind(temp_regime, size_means, stage3s_means, rep("C", times = length(temp_means))))
c_size_df$stage3s_means <- as.numeric(c_size_df$stage3s_means)
c_size_df$temp_regime <- as.numeric(c_size_df$temp_regime)
c_size_df$size_means <- as.numeric(c_size_df$size_means)
# ctemp <- ggplot(data = temp_adjust_df, mapping = aes(x = temp_seq, y = temp_means/10000))+
# geom_line(size = 1, col = "#EE6677")+
# xlab("Degree C Change")+
# ylab("C sp Abundance Relative to K")+
# theme_bw()
# Disturbance by temp
temp_regime <- vector()
temp_means <- vector()
temp_seq <- seq(-10, 10, by = 1)
short <- vector()
discharge[259] <- 1
for (te in 1:length(temp_seq)){
temp$Temperature <- temp$Temperature + temp_seq[te]
temp_regime[te] <- mean(temp$Temperature)
out <- Cmodel(discharge, temp, baselineK = 10000, disturbanceK = 40000, Qmin = 0.25, extinct = 50, iteration = 2, peaklist = 0, peakeach = length(temp$Temperature))
temp$Temperature <- temp$Temperature - temp_seq[te]
#means.list.C <- out[-c(1:250)]
means.list.C<- mean.data.frame(out, burnin = 250, iteration = 2)
short[te] <- mean(means.list.C$mean.abund[10:16])
}
winter <- as.data.frame(cbind(temp_regime, short, log(short)))
size_means <- vector()
for (te in 1:length(temp_seq)){
temp$Temperature <- temp$Temperature + temp_seq[te]
temp_regime[te] <- mean(temp$Temperature)
out <- Cmodel(discharge, temp, baselineK = 10000, disturbanceK = 40000, Qmin = 0.25, extinct = 50, iteration = 1, peaklist = 0, peakeach = length(temp$Temperature), stage_output = "size")
temp$Temperature <- temp$Temperature - temp_seq[te]
size_means[te] <- colMeans(out)
size_means[te] <- 0.0056*(size_means[te])^2.839
}
#size_means <- 0.0056*(size_means)^2.839 # multiply relative size (which is also biologically plausible) by Benke et al 1999 Table 2 a and b params (M(mg) = aL^b)
winter_size_means <- as.data.frame(cbind(temp_regime, size_means))
temp_regime <- vector()
temp_means <- vector()
temp_seq <- seq(-10, 10, by = 1)
short <- vector()
discharge[272] <- 1
for (te in 1:length(temp_seq)){
temp$Temperature <- temp$Temperature + temp_seq[te]
temp_regime[te] <- mean(temp$Temperature)
out <- Cmodel(discharge, temp, baselineK = 10000, disturbanceK = 40000, Qmin = 0.25, extinct = 50, iteration = 2, peaklist = 0, peakeach = length(temp$Temperature))
temp$Temperature <- temp$Temperature - temp_seq[te]
#means.list.C <- out[-c(1:250)]
means.list.C<- mean.data.frame(out, burnin = 250, iteration = 2)
short[te] <- mean(means.list.C$mean.abund[23:39])
}
summer <- as.data.frame(cbind(temp_regime, short, log(short)))
size_means <- vector()
for (te in 1:length(temp_seq)){
temp$Temperature <- temp$Temperature + temp_seq[te]
temp_regime[te] <- mean(temp$Temperature)
out <- Cmodel(discharge, temp, baselineK = 10000, disturbanceK = 40000, Qmin = 0.25, extinct = 50, iteration = 1, peaklist = 0, peakeach = length(temp$Temperature), stage_output = "size")
temp$Temperature <- temp$Temperature - temp_seq[te]
size_means[te] <- colMeans(out)
size_means[te] <- 0.0056*(size_means[te])^2.839
}
#size_means <- 0.0056*(size_means)^2.839 # multiply relative size (which is also biologically plausible) by Benke et al 1999 Table 2 a and b params (M(mg) = aL^b)
summer_size_means <- as.data.frame(cbind(temp_regime, size_means))
# bind together, 1 = winter 2 = summer
temp_dist_c <- bind_rows(winter, summer, .id = "season")
sizes <- rbind(winter_size_means, summer_size_means)
deltatemp_c <- as.data.frame(cbind(rep(3, times = length(temp_regime)),temp_regime, summer[,3]-winter[,3]))
temp_size_c <- bind_rows(winter_size_means, summer_size_means, .id = "season")
deltasize_c <- as.data.frame(cbind(rep(3, times = length(temp_regime)), temp_regime, (summer_size_means[,2]*summer[,2])-(winter_size_means[,2]*winter[2])))
temp_size_c <- mutate(.data = temp_size_c, size_means = temp_dist_c$short * sizes$size_means )
deltatemp_c <- setNames(deltatemp_c, names(temp_dist_c[c(1,2,4)]))
temp_dist_c <- rbind(temp_dist_c[c(1,2,4)], deltatemp_c)
deltasize_c <- setNames(deltasize_c, names(temp_size_c))
temp_size_c <- rbind(temp_size_c, deltasize_c)
# ggplot(data = temp_dist_c, aes(x = temp_regime, y = short))+
# geom_line()+
# facet_grid(.~season)
#
# ggplot(data = temp_size_c, aes(x = temp_regime, y = size_means))+
# geom_line()+
# facet_grid(.~season)