-
Notifications
You must be signed in to change notification settings - Fork 0
/
decay-model-two-pool.R
314 lines (231 loc) · 10.2 KB
/
decay-model-two-pool.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# --------------------
# Title: Decay.models.plus.loop.R
# Author: TW modified by DBJ
# Date: 2021-Jan-7
#
# Purpose: Script for fitting two-pool models to exponential decay data
# + loops
# --------------------
# Load package
library(minpack.lm)
library(dplyr)
library(ggplot2)
library(tidyverse)
library(knitr)
# --------------------
setwd("C:/Users/danab/Box/WhitmanLab/Projects/WoodBuffalo/FireSim2019/data")
### Running model on our own data ##
### Two-pool model ###
# Mt = M1*e^(-k1*t)+M2*e^(-k2*t) The basic two-pool model
# Mt = 1 = M1+M2 Setting Mt as total initial C to 1, M1+M2 make up total
# Mt = M1*e^(-k1*t)+(1-M1)*e^(-k2*t) Rearranging equation
# Mt = M1*e^(-k1*t)-M1*e^(-k2*t)+e^(-k2*t) Rearranging equation
# Describe function form
TwoPoolDecay = function(params,t) params$M1*(exp(-params$k1*t))-params$M1*exp(-params$k2*t)+exp(-params$k2*t)
## Define residual function (our actual value minus the exponential function)
residualsFun.twopool = function(p, Mt, t) Mt - TwoPoolDecay(p, t)
# (k1=0.005,k2=0.00001,M1=0.02) was pretty good except for a few dry burns (3, 8, 12, 13, 17) - too linear
# Trying different start parameters for those ones
# Looks like it's decaying faster than we'd think, so starting with a larger M1 might help.
# StartPar.TwoPool = list(k1=0.06,k2=0.00005,M1=0.01)
# OBJECTIVE: set up input and output files for loop
df <- read.csv("fractional-c-remaining.csv", row.names = 1)
df <- subset(df, select = c(core.id, core.id.incub, site, burn.trtmt, DayFactor, fractional.C.remaining))
df <- subset(df, DayFactor < 35)
head(df)
# Create a vector of Core IDs
# Optionally filter to pull out cores of interest
names.df <- df %>%
subset(DayFactor == 3) %>%
#filter(core.id %in% c("19UW-WB-03-02","19UW-WB-08-10","19UW-WB-12-02","19UW-WB-13-03","19UW-WB-17-01"))%>%
subset(select = c(core.id.incub))
# Convert df to vector
names.vec <- names.df[,"core.id.incub"]
# Check if this worked.
class(names.vec)
# --------------------
# Create an output df to populate with coefficients later
coefs.df <- data.frame("core.id.incub" = "sample", "k1" = 0, "k2" = 0, "M1" = 0, "M2" = 0)
output.fit.df <- data.frame("core.id" = "sample",
"core.id.incub" = "sample",
'r.squared' = 0,
'pvalue' = 0,
"site" = 0,
"burn.trtmt" = "trtmt",
"t" = 0,
"Mt" = 0, "Mt.fit" = 0)
# List start parameters - can set these however
# StartPar.TwoPool = list(k1=0.005,k2=0.00001,M1=0.02)
# Dry: k1=0.005,k2=0.00001,M1=0.002
# Wet: k1=0.005,k2=0.00001,M1=0.02
# Control: k1=0.005,k2=0.00001,M1=0.02
# Run the decay.models.R script in a for loop.
for (i in seq_along(names.vec)) {
df.x <- df %>%
rename(t = DayFactor, Mt = fractional.C.remaining) %>%
subset(core.id.incub == names.vec[[i]])
if (df.x$burn.trtmt[1] == "dry") {
StartPar.TwoPool = list(k1=0.005,k2=0.00001,M1=0.002)
} else if (df.x$burn.trtmt[1] == "wet") {
StartPar.TwoPool = list(k1=0.005,k2=0.00001,M1=0.02)
} else {
StartPar.TwoPool = list(k1=0.005,k2=0.00001,M1=0.01)
}
TwoPool.nls.fit <- nls.lm(par=StartPar.TwoPool, fn = residualsFun.twopool,
Mt = df.x$Mt,
t = df.x$t,
lower = c(0,0,0), upper = c(Inf, Inf, 1),
control = nls.lm.control(nprint=1,maxiter=500,ftol=sqrt(.Machine$double.eps)/10))
df.x$Mt.fit <- TwoPoolDecay(as.list(coef(TwoPool.nls.fit)),df.x$t)
df.lm <- lm(Mt ~ Mt.fit, data = df.x)
df.x$r.squared <- summary(df.lm)$r.squared
df.x$pvalue <- summary(df.lm)$coef[2,4]
## summary information on parameter estimates
coefs <- summary(TwoPool.nls.fit)
k1 <- coefs$coefficients[[1]]
k2 <- coefs$coefficients[[2]]
M1 <- coefs$coefficients[[3]]
M2 <- df.x$Mt[1] - M1
pvalue <- df.x$pvalue
coefs.df <- coefs.df %>%
add_row("core.id.incub" = names.vec[[i]], "k1" = k1, "k2" = k2, "M1" = M1, "M2" = M2)
coefs.df
output.fit.df <- rbind(output.fit.df, df.x)
}
# --------------------
### Second iteration:
# Set cutoff for R squared
rsquared = 0.98
# If R-squared is above cut-off, it's good. Otherwise, redo parameters.
output.fit.df <- merge(output.fit.df, coefs.df)
df.rsquared <- subset(output.fit.df, r.squared >rsquared)
df.redo <- subset(output.fit.df, r.squared < rsquared)
names.vec <- df.redo %>%
subset(t == 3) %>%
subset(select = c(core.id.incub))
df <- read.csv("fractional-c-remaining.csv", row.names = 1)
df <- merge(names.vec, df)
df <- df %>%
separate(core.id.incub, c('year','project','Site','core','incub'), sep = '-', remove = FALSE) %>%
subset(select = c(core.id.incub,
core.id,
site,
burn.trtmt,
DayFactor,
fractional.C.remaining))
names.vec = as.vector(names.vec$core.id.incub)
# Create an output df to populate with coefficients later
coefs.df <- data.frame("core.id.incub" = "sample", "k1" = 0, "k2" = 0, "M1" = 0, "M2" = 0)
output.fit.df <- data.frame("core.id" = "sample",
"core.id.incub" = "sample",
'r.squared' = 0,
'pvalue' = 0,
"site" = 0,
"burn.trtmt" = "trtmt",
"t" = 0,
"Mt" = 0, "Mt.fit" = 0)
# List start parameters - can set these however
StartPar.TwoPool = list(k1=0.005,k2=0.0001,M1=0.01)
head(df)
# Run the decay.models.R script in a for loop.
for (i in seq_along(names.vec)) {
df.x <- df %>%
rename(t = DayFactor, Mt = fractional.C.remaining) %>%
subset(core.id.incub == names.vec[[i]])
TwoPool.nls.fit <- nls.lm(par=StartPar.TwoPool, fn = residualsFun.twopool,
Mt = df.x$Mt,
t = df.x$t,
lower = c(0,0,0), upper = c(Inf, Inf, 1),
control = nls.lm.control(nprint=1,maxiter=500,ftol=sqrt(.Machine$double.eps)/10))
df.x$Mt.fit <- TwoPoolDecay(as.list(coef(TwoPool.nls.fit)),df.x$t)
df.lm <- lm(Mt ~ Mt.fit, data = df.x)
df.x$r.squared <- summary(df.lm)$r.squared
df.x$pvalue <- summary(df.lm)$coef[2,4]
## summary information on parameter estimates
coefs <- summary(TwoPool.nls.fit)
k1 <- coefs$coefficients[[1]]
k2 <- coefs$coefficients[[2]]
M1 <- coefs$coefficients[[3]]
M2 <- df.x$Mt[1] - M1
coefs.df <- coefs.df %>%
add_row("core.id.incub" = names.vec[[i]], "k1" = k1, "k2" = k2, "M1" = M1, "M2" = M2)
coefs.df
output.fit.df <- rbind(output.fit.df, df.x)
}
# --------------------
### Third iteration
# If R-squared is above cut-off, it's good. Otherwise, redo parameters.
output.fit.df <- merge(output.fit.df, coefs.df)
df.rsquared <- df.rsquared %>%
rbind(output.fit.df) %>%
subset(r.squared >rsquared)
df.redo <- subset(output.fit.df, r.squared < rsquared)
names.vec <- df.redo %>%
subset(t == 3) %>%
subset(select = c(core.id.incub))
df <- read.csv("fractional-c-remaining.csv", row.names = 1)
df <- merge(names.vec, df)
df <- df %>%
separate(core.id.incub, c('year','project','Site','core','incub'), sep = '-', remove = FALSE) %>%
subset(select = c(core.id.incub,
core.id,
site,
burn.trtmt,
DayFactor,
fractional.C.remaining))
names.vec = as.vector(names.vec$core.id.incub)
# Create an output df to populate with coefficients later
coefs.df <- data.frame("core.id.incub" = "sample", "k1" = 0, "k2" = 0, "M1" = 0, "M2" = 0)
output.fit.df <- data.frame("core.id" = "sample",
"core.id.incub" = "sample",
'r.squared' = 0,
'pvalue' = 0,
"site" = 0,
"burn.trtmt" = "trtmt",
"t" = 0,
"Mt" = 0, "Mt.fit" = 0)
# List start parameters - can set these however
StartPar.TwoPool = list(k1=0.05,k2=0.0001,M1=0.001)
# Run the decay.models.R script in a for loop.
for (i in seq_along(names.vec)) {
df.x <- df %>%
rename(t = DayFactor, Mt = fractional.C.remaining) %>%
subset(core.id.incub == names.vec[[i]])
TwoPool.nls.fit <- nls.lm(par=StartPar.TwoPool, fn = residualsFun.twopool,
Mt = df.x$Mt,
t = df.x$t,
lower = c(0,0,0), upper = c(Inf, Inf, 1),
control = nls.lm.control(nprint=1,maxiter=500,ftol=sqrt(.Machine$double.eps)/10))
df.x$Mt.fit <- TwoPoolDecay(as.list(coef(TwoPool.nls.fit)),df.x$t)
df.lm <- lm(Mt ~ Mt.fit, data = df.x)
df.x$r.squared <- summary(df.lm)$r.squared
df.x$pvalue <- summary(df.lm)$coef[2,4]
## summary information on parameter estimates
coefs <- summary(TwoPool.nls.fit)
k1 <- coefs$coefficients[[1]]
k2 <- coefs$coefficients[[2]]
M1 <- coefs$coefficients[[3]]
M2 <- df.x$Mt[1] - M1
pvalue <- df.x$pvalue
coefs.df <- coefs.df %>%
add_row("core.id.incub" = names.vec[[i]], "k1" = k1, "k2" = k2, "M1" = M1, "M2" = M2)
coefs.df
output.fit.df <- rbind(output.fit.df, df.x)
}
# If R-squared is above cut-off, it's good. Otherwise, redo parameters.
output.fit.df <- merge(output.fit.df, coefs.df)
df.rsquared <- df.rsquared %>%
rbind(output.fit.df) %>%
subset(r.squared >rsquared)
df.redo <- subset(output.fit.df, r.squared < rsquared)
names.vec <- df.redo %>%
subset(t == 3) %>%
subset(select = c(core.id.incub))
# Complete at R^2 = 0.98
head(df.rsquared)
max(df.rsquared$k1)
# --------------------
# Clean up output files
df.rsquared <- df.rsquared %>%
separate(core.id.incub, c("Year", "Project", "Site", "Core", "incub.trtmt"), sep = "-", remove = FALSE)
#write.csv(df.rsquared,'../data/2-pool-decay-model-output.csv')