-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopmodels_basic logistic.R
228 lines (182 loc) · 10 KB
/
Popmodels_basic logistic.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
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
###-------------Discrete logistic model
log_d <- function(par, suppar){
output <- numeric(suppar[2])
output[1] <- suppar[1]
for (t in 1:(length(output)-1)){
output[t+1] <- output[t]+output[t]*par[1]*(1-output[t]/par[2])
}
return(output)
}
maf <- function(par, data, suppar){
model <- log_d(par, suppar)
error <- mean(abs(model-data))
return (error)
}
mss <- function(par, data, suppar){
model <- log_d(par, suppar)
error <- sum((abs(model-data))^2, na.rm=TRUE)/length(data)
return (error)
}
### Discrete logistic model for each macrounit = on AllMeans
Popdata <- data.frame(MDperKMsqFall_mean=AllMeans$MDperKMsqFall_mean, year=AllMeans$year, macrounit=AllMeans$macrounit, RepRateFall_mean =AllMeans$RepRateFall_mean)#remove NAs
Popdata <- Popdata[which(complete.cases(Popdata)==TRUE),]
png("PopDen.png", width=2200, height=1500)
par(mfrow=c(2,2),oma=c(10,10,10,10),mar=c(10, 10, 10, 10))
par(mfrow=c(2,2),oma=c(5,5,5,5),mar=c(5,5,5,5))
macrounits <- levels(Popdata$macrounit)
result_out <- list()
rd_out <- list()
parinfo <- data.frame("0-1" = numeric(3), "0-2" = numeric(3), "0-3" = numeric(3), "0-4" = numeric(3), row.names=c("rd", "K", "MSS"))
ylimmax <- c(3.5,3.5,3.5,6.5)
for (i in 1:length(macrounits)){
cond = which(Popdata$macrounit==macrounits[i])
N_0 <- Popdata$MDperKMsqFall_mean[cond[1]]
tsteps <- length(Popdata$MDperKMsqFall_mean[cond])
opt <- optim(par= c(rd=0.3, K=2), fn=mss, method="L-BFGS-B", lower=c(0 , 0), suppar=c(N_0, tsteps), data=Popdata$MDperKMsqFall_mean[cond])
result <- log_d(c(opt$par[1], opt$par[2]),c(N0=N_0, steps=tsteps))
names(result) <- Popdata$year[cond]
result_out <- append(result_out, list(c(result)))
names(result) <- Popdata$year[cond]
rd <- c(((result[-1]-result[-tsteps])/result[-tsteps]),NA)
rd_out <- append(rd_out, list(c(rd)))
parinfo[,i] <- c(opt$par, opt$value)
plot(Popdata$MDperKMsqFall_mean[cond]~Popdata$year[cond], xlab="",ylab="", cex.axis=2.5, cex.main = 3, main=macrounits[i], type="l", ylim=c(0,ylimmax[i]))
lines(result~Popdata$year[cond], col="red",lwd=2)
#plot(rd~result, type="l", lty=2, main=macrounits[i], xlab="Predicted Population Density",ylab="Predicted Reproduction rate")
remove(result)
remove(rd)
}
mtext("Year", 1, 1, outer=TRUE, cex=3)
mtext("Population Density", 2, 1, outer=TRUE, las=0, cex=3)
mtext("Basic1", 3, 1, outer=TRUE, cex=3.5)
#print(parinfo)
dev.off()
# parinfolist <- append(parinfolist, as.list(parinfo))
# }
# w <- which(names(parinfolist) == "X0.4")
# parinfolist[w]
# hist(parinfolist[[which(names(parinfolist) == "X0.4")]])
# visualization of density dependence observed vs. modelled
par(mfrow=c(2,2))
for (i in 1:length(macrounits)){
cond = which(Popdata$macrounit==macrounits[i])
plot(Popdata$RepRateFall_mean[cond]~Popdata$MDperKMsqFall_mean[cond], main=macrounits[i], type="p",cex=0.5,xlab="Observed Population Density",ylab="Observed and Predicted Reproduction rate")
curve ((parinfo[1,i]*(1-(x/parinfo[2,i])^parinfo[3,i])), from=min(Popdata$MDperKMsqFall_mean[cond]), to=max(Popdata$MDperKMsqFall_mean[cond]), n=1000, col="red", add=TRUE)
#lines(rd_out[[i]]~result_out[[i]], type="l", lty=2, col="red")
}
legend("topright", legend=c("Observed", "Predicted"), col=c("black", "red"), lty=c(1,2))
title("Observed and Predicted Density Dependences Theta Model MAF", outer=TRUE)
#recalculation using nls
nls <- nls(RepRateFall_mean[cond] ~ rd*MDperKMsqFall_mean[cond]*((1-MDperKMsqFall_mean[cond]/K)^theta), data=Popdata, lower=c(0,0,0), start=c(rd=8.66, K=1.49, theta = 0.023))
#error "missing values or infitiy produced when evaluating model"
#NAs?
count_nas(Popdata$MDperKMsqFall_mean)#0
count_nas(Popdata$RepRateFall_mean)#0
# Including Harvest in basic logistic model for each macrounit = on AllMeans-----
Popdata <- data.frame(MDperKMsqFall_mean=AllMeans$MDperKMsqFall_mean, year=AllMeans$year, macrounit=AllMeans$macrounit, HuntDen_All_mean=AllMeans$HuntDen_All_mean)#remove NAs
Popdata <- Popdata[which(complete.cases(Popdata)==TRUE),]
##huntdenAll no NAs
log_dh <- function(par, suppar){
output <- numeric(suppar[2])#steps
output[1] <- suppar[1]#n0
for (t in 1:(length(output)-1)){
output[t+1] <- output[t]+(output[t]*par[1]*(1-(output[t]/par[2])))-(par[3]*suppar[t+2])
#from suppar 3 on: harvest data, weight term is fitted to avoid negative values if HuntDen<PopDen
}
return(output)
}
maf_dh <- function(par, data, suppar){
model <- log_dh(par, suppar)
error <- mean(abs(model-data))
if (length(which(model < 0)) != 0){error <- length(which(model < 0))}# penalty: the more negative values, the worse MSS
return (error)
}
mss_dh <- function(par, data, suppar){
model <- log_dh(par, suppar)
error <- sum((abs(model-data))^2, na.rm=TRUE)/length(data)
if (length(which(model < 0)) != 0){error <- length(which(model < 0))}# penalty: the more negative values, the worse MSS
return (error)
}
png("Basic2_MSS_weight0,1.png", width=2200, height=1500)
par(mfrow=c(2,2),oma=c(10,10,10,10),mar=c(10, 10, 10, 10))
macrounits <- levels(Popdata$macrounit)
result_out2 <- list()
rd_out <- list()
parinfo <- data.frame("0-1" = numeric(4), "0-2" = numeric(4), "0-3" = numeric(4), "0-4" = numeric(4), row.names=c("rd", "K", "weight of harvest density", "MSS"))
ylimmax <- c(3.5,3.5,3.5,6.5)
for (i in 1:length(macrounits)){
cond = which(Popdata$macrounit==macrounits[i])
N_0 <- Popdata$MDperKMsqFall_mean[cond[1]]
tsteps <- length(Popdata$MDperKMsqFall_mean[cond])
opt <- optim(par= c(rd=0.3, K=2, weight=0.1), fn=mss_dh, method="L-BFGS-B", lower=c(0, 0 , 0, 0), suppar=c(N_0, tsteps, Popdata$HuntDen_All_mean[cond]), data=Popdata$MDperKMsqFall_mean[cond])
result <- log_dh(par=c(opt$par[1], opt$par[2], opt$par[3]),suppar=c(N0=N_0, steps=tsteps, Popdata$HuntDen_All_mean[cond]))
names(result) <- Popdata$year[cond]
result_out2 <- append(result_out2, list(c(result)))
names(result) <- Popdata$year[cond]
rd <- c(((result[-1]-result[-tsteps])/result[-tsteps]),NA)
rd_out <- append(rd_out, list(c(rd)))
parinfo[,i] <- c(opt$par, opt$value)
plot(Popdata$MDperKMsqFall_mean[cond]~Popdata$year[cond], xlab="",ylab="", cex.axis=2.5, cex.main = 3, main=macrounits[i], ylim=c(0,ylimmax[i]), type="l")
lines(result~Popdata$year[cond], col="blue", lwd=2)
lines(result_out[[i]], col="red", lwd=2)
#plot(rd~result, type="l", lty=2, main=macrounits[i], xlab="Predicted Population Density",ylab="Predicted Reproduction rate")
remove(result)
remove(rd)
}
mtext("Year", 1, 1, outer=TRUE, cex=3)
mtext("Population Density", 2, 1, outer=TRUE, las=0, cex=3)
mtext("Basic2", 3, 1, outer=TRUE, cex=3.5)
parinfo
dev.off()
xyplot(MDperKMsqFall_mean + HuntDen_All_mean ~ year |macrounit, data=Popdata, type="l",auto.key = list(space = "top", text = c("PopDen", "HuntDen"), points = FALSE, lines = TRUE))
length(which(Popdata$HuntDen_All_mean[cond]>Popdata$MDperKMsqFall_mean[cond]))
### PLOTTING BOTH ----------
par(mfrow=c(2,2))
for (i in 1:length(macrounits)){
cond = which(Popdata$macrounit==macrounits[i])
plot(Popdata$MDperKMsqFall_mean[cond]~Popdata$year[cond], xlab="",ylab="", cex.axis=2.5, cex.main = 3, main=macrounits[i], ylim=c(0,ylimmax[i]), type="l")
lines(result_out2[[i]]~Popdata$year[cond], col="blue", lwd=2)
lines(result_out[[i]], col="red", lwd=2)
png("Basic1_2.png", width=2200, height=1500)
par(mfrow=c(2,2),oma=c(10,10,10,10),mar=c(10, 10, 10, 10))
macrounits <- levels(Popdata$macrounit)
result_out <- list()
rd_out <- list()
parinfo <- data.frame("0-1" = numeric(4), "0-2" = numeric(4), "0-3" = numeric(4), "0-4" = numeric(4), row.names=c("rd", "K", "weight of harvest density", "MSS"))
ylimmax <- c(3.5,3.5,3.5,6.5)
for (i in 1:length(macrounits)){
cond = which(Popdata$macrounit==macrounits[i])
N_0 <- Popdata$MDperKMsqFall_mean[cond[1]]
tsteps <- length(Popdata$MDperKMsqFall_mean[cond])
opt1 <- optim(par= c(rd=0.3, K=2, weight=0.1), fn=mss_dh, method="L-BFGS-B", lower=c(0, 0 , 0, 0), suppar=c(N_0, tsteps, Popdata$HuntDen_All_mean[cond]), data=Popdata$MDperKMsqFall_mean[cond])
result1 <- log_dh(par=c(opt1$par[1], opt1$par[2], opt1$par[3]),suppar=c(N0=N_0, steps=tsteps, Popdata$HuntDen_All_mean[cond]))
names(result1) <- Popdata$year[cond]
opt2 <- optim(par= c(rd=0.3, K=2), fn=mss, method="L-BFGS-B", lower=c(0, 0 , 0, 0), suppar=c(N_0, tsteps, Popdata$HuntDen_All_mean[cond]), data=Popdata$MDperKMsqFall_mean[cond])
result2 <- log_d(par=c(opt2$par[1], opt2$par[2], opt2$par[3]),suppar=c(N0=N_0, steps=tsteps, Popdata$HuntDen_All_mean[cond]))
names(result2) <- Popdata$year[cond]
plot(Popdata$MDperKMsqFall_mean[cond]~Popdata$year[cond], xlab="",ylab="", cex.axis=2.5, cex.main = 3, main=macrounits[i], ylim=c(0,ylimmax[i]), type="l")
lines(result1~Popdata$year[cond], col="darkred", lwd=2)
lines(result2~Popdata$year[cond], col="red", lwd=2)
}
mtext("Year", 1, 1, outer=TRUE, cex=3)
mtext("Population Density", 2, 1, outer=TRUE, las=0, cex=3)
mtext("Basic1 and Basic2", 3, 1, outer=TRUE, cex=3.5)
parinfo
dev.off()
# On WholeAreaMeans
# opt_maf <- optim(par= c(rd=0.3, K=2), fn=maf, suppar=c(N0=N_0, steps=tsteps), data=WholeAreaMeans$MDperKMsqFall_mean)
# result_maf <- log_d(c(opt_maf$par[1], opt_maf$par[2]),c(N0=N_0, steps=tsteps)) #MAF: 0.3056459, rd:0.05669221, K:2.12359422
# rd_maf <- c(((result_maf[-1]-result_maf[-tsteps])/result_maf[-tsteps]),NA)
# opt_mss <- optim(par= c(rd=0.3, K=2), fn=mss, suppar=c(N0=N_0, steps=tsteps), data=WholeAreaMeans$MDperKMsqFall_mean)
# result_mss <- log_d(c(opt_mss$par[1], opt_mss$par[2]),c(N0=N_0, steps=tsteps)) #MSS:8.654571, rd:0.03246672, K:3.33934080
# rd_mss <- c(((result_mss[-1]-result_mss[-tsteps])/result_mss[-tsteps]),NA)
#
# plot(rd_maf~result_maf, type="l", col="red")#correct:linear relationship of rd and N
# cor(rd_maf,result_maf, use="pairwise.complete.obs")#--1
# lines(rd_mss~result_mss, type="l", col="blue")#correct:linear
# cor(rd_mss,result_mss, use="pairwise.complete.obs") #-1
#
# plot(WholeAreaMeans$MDperKMsqFall_mean, cex=0.5, main="Discrete logistic model")
# lines(result_maf, col="red")
# lines(result_mss, col="blue")
# legend("topleft", legend=c("MAF", "MSS"), col=c("red", "blue"), lty=1)