-
Notifications
You must be signed in to change notification settings - Fork 0
/
runoff.Rnw
384 lines (305 loc) · 15.4 KB
/
runoff.Rnw
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
\documentclass[12pt]{article}
\usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper} % ... or a4paper or a5paper or ...
%\geometry{landscape} % Activate for for rotated page geometry
\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{subfig}
\usepackage{verbatim}
\usepackage{fullpage}
\usepackage{pstricks,pst-node,pst-tree}
\title{Breakpoints for farm field runoff}
\author{Wesley Brooks}
\date{} % Activate to display a given date or no date
\begin{document}
\setkeys{Gin}{width=0.9\textwidth} %make figures a bit wider than the Sweave default.
\maketitle
<<label=read_data, echo=FALSE, include=FALSE, keep.source=True>>=
setwd('c:/Users/wrbrooks/git/disco_farms')
data_file = 'Dataset020312long.csv'
data = read.csv(data_file, header=T, na.strings=c('NA', 'na', '', "-9"))
farms = levels(data$loc)
sites = levels(data$site)
farm_names = list(DF3="DF3", DF1="DF1", DF2="DF2", DF1a="DF1a", DF1b="DF1b", DF2a="DF2a", DF2b="DF2b", DF2c="DF2c")
@
<<label=piecewise, results=hide, echo=FALSE, include=FALSE, keep.source=True>>=
piecewise <- function(th, x, y) {
X <- cbind( ifelse(x>=th, 1, 0), x, pmax(0, x-th) )
fit = lsfit(X, y) #actually fit the model
#Counts how many observations lie above and below the breakpoint
upper = as.logical( ifelse(x-th>=0, 1, 0) )
n_upper = sum(upper)
n_lower = dim(X)[1] - n_upper
#Sum of squared residuals on either side of the breakpoint
ssr_upper = sum( fit$resid[upper]**2 )
ssr_lower = sum( fit$resid[!upper]**2 )
#Return the residual standard error:
return( n_lower*sqrt(ssr_lower/(n_lower-2)) + n_upper*sqrt(ssr_upper/(n_upper-2)) )
}
piecewise_conts <- function(th, x, y) {
X <- cbind( x, pmax(0, x-th) )
fit = lsfit(X, y) #actually fit the model
#Counts how many observations lie above and below the breakpoint
upper = as.logical( ifelse(x-th>=0, 1, 0) )
n_upper = sum(upper)
n_lower = dim(X)[1] - n_upper
#Sum of squared residuals on either side of the breakpoint
ssr_upper = sum( fit$resid[upper]**2 )
ssr_lower = sum( fit$resid[!upper]**2 )
#Return the residual standard error:
return( n_lower*sqrt(ssr_lower/(n_lower-2)) + n_upper*sqrt(ssr_upper/(n_upper-2)) )
}
piecewise_disjoint <- function(th, x, y) {
X <- cbind( ifelse(x>=th, 1, 0), x, pmax(0, x-th) )
fit = lsfit(X, y) #actually fit the model
#Counts how many observations lie above and below the breakpoint
upper = as.logical( ifelse(x-th>=0, 1, 0) )
n_upper = sum(upper)
n_lower = dim(X)[1] - n_upper
#Sum of squared residuals on either side of the breakpoint
ssr_upper = sum( fit$resid[upper]**2 )
ssr_lower = sum( fit$resid[!upper]**2 )
#Return the residual standard error:
return( n_lower*sqrt(ssr_lower/(n_lower-2)) + n_upper*sqrt(ssr_upper/(n_upper-2)) )
}
piecewise_constant <- function(th, x, y) {
X <- ifelse(x>=th, 1, 0)
fit = lsfit(X, y) #actually fit the model
#Counts how many observations lie above and below the breakpoint
upper = as.logical( ifelse(x-th>=0, 1, 0) )
n_upper = sum(upper)
n_lower = dim(X)[1] - n_upper
#Sum of squared residuals on either side of the breakpoint
ssr_upper = sum( fit$resid[upper]**2 )
ssr_lower = sum( fit$resid[!upper]**2 )
#Return the residual standard error:
return( n_lower*sqrt(ssr_lower/(n_lower-2)) + n_upper*sqrt(ssr_upper/(n_upper-2)) )
}
@
<<echo=False, include=False>>=
anova.bp <- function(data, split.on, site=NA, conts=FALSE, discrete.xrange=FALSE, xrange=NA, min.in.node=4, return.models=FALSE, return.X=FALSE) {
#First, remove datapoints that have NA for the splitting variable:
data = data[!is.na(data[,split.on]),]
#Now generate the matrix of predictors (i.e. the X matrix):
if(!conts) {
if(discrete.xrange)
th <- which.min( sapply(X=xrange, FUN=piecewise_disjoint, x=data[data$site==site,split.on], y=data[data$site==site,]$rc) ) + xrange[1]-1
else {
lower_lim = sort( data[data$site==site,split.on] )[min.in.node]
upper_lim = sort(data[data$site==site,split.on], decreasing=T )[min.in.node]
th <- optimize(piecewise_disjoint, upper=upper_lim, lower=lower_lim, x=data[data$site==site,split.on], y=data[data$site==site,]$rc)$minimum
}
X.bp = cbind( ifelse(data[data$site==site,split.on]>=th, 1, 0), data[data$site==site,split.on], pmax(0, data[data$site==site,split.on]-th) )
}
else {
if(discrete.xrange)
th <- which.min( sapply(X=xrange, FUN=piecewise_conts, x=data[data$site==site,split.on], y=data[data$site==site,]$rc) ) + xrange[1]-1
else {
lower_lim = sort( data[data$site==site,split.on] )[min.in.node]
upper_lim = sort(data[data$site==site,split.on], decreasing=T )[min.in.node]
th <- optimize(piecewise_conts, upper=upper_lim, lower=lower_lim, x=data[data$site==site,split.on], y=data[data$site==site,]$rc)$minimum
}
X.bp = cbind( data[data$site==site,split.on], pmax(0, data[data$site==site,split.on]-th) )
}
X.line = data[data$site==site,split.on]
y = data[data$site==site,]$rc
if(return.X)
return(list(line=X.line, bp=X.bp, theta=th))
fit.bp = lm(y~X.bp)
fit.line = lm(y~X.line)
if(return.models)
return(list(bp=fit.bp, line=fit.line, theta=th))
#Calculate the p-value for the hypothesis of no difference between the two lines:
pval = anova(fit.bp, fit.line)[["Pr(>F)"]][2]
#Return the p-value in a legible format:
if (pval < 0.0001)
return("p < 0.0001")
else
return(paste("p = ", round(pval,4), sep=""))
}
@
<<label=piecewise_plot, include=False, echo=FALSE, keep.source=True>>=
piecewise_plot <- function(x,y,th, xlim=FALSE, ylim=FALSE, xlab='', ylab='', title='', p=1) {
#define the limits of the plot
if( identical(xlim, FALSE) ) { xx = range(x, na.rm=TRUE) }
else { xx = xlim }
if( identical(ylim, FALSE) ) { yy = range(y, na.rm=TRUE) }
else { yy = ylim }
#put the data points on the plot
plot(x, y, xlab=xlab, ylab=ylab, xlim=xx, ylim=yy, pch=20, bty='n', xaxt='s', yaxt='s', ann=T, main=title)
#create the piecewise regression model
X <- cbind(ifelse(x>=th, 1, 0), x, pmax(0, x-th))
fit = lsfit(X, y) #actually fit the model
#extent of the lower, upper pieces:
xints = c( xx[1], th, xx[2] )
#draw the lower regression line
xints_low = xints[1:2]
yints_low = fit$coef[1] + (fit$coef[3]*xints_low)
par(new=T, ann=F, xaxt='n', yaxt='n', bty='n')
plot(xints_low, yints_low, type='l', xlim=xx, ylim=yy)
#draw the upper regression line
xints_high = xints[2:3]
yints_high = fit$coef[1] + fit$coef[2] + (fit$coef[3]*xints_high) + (fit$coef[4]*(xints_high - th))
par(new=T, ann=F, xaxt='n', yaxt='n')
plot(xints_high, yints_high, type='l', xlim=xx, ylim=yy)
#draw a vertical line to separate the two regimes
abline(v=th, lty=3)
text(x=th, y=0.85, pos=4, labels=paste("breakpoint: ", round(th,2), "\n", p, sep=""))
}
@
The piecewise models we use here allow a seaparate slope and intercept on either side of the breakpoint. The breakpoint chosen is the one that minimizes the residual standard error, with the condition that there must be at least four data points on both sides of the breakpoint. The p-values cited in this paper are based on an F test of the improvement in model fit between the piecewise model (with four parameters: a slope and an intercept for each line segment) and a linear model (with two parameters: one slope and one intercept covering the entire range of data).
<<label=soil moisture breakpoints, echo=FALSE, include=FALSE, keep.source=True>>=
xs = seq(33, 43)
sm.breakpoints = vector()
sm.pval = list()
breakpoints = list()
fit = list()
cat("Soil moisture breakpoints by farm:\n")
for(site in sites) {
farm = data[data$site==site,]
th <- which.min( sapply(X=xs, FUN=piecewise, x=farm$sm, y=farm$rc) ) + 32
sm.breakpoints = c(sm.breakpoints, th)
breakpoints[[site]] = th
sm.pval[[site]] = anova.bp(data, split.on="sm", site=site, discrete.xrange=TRUE, xrange=xs, min.in.node=4)
X <- cbind( ifelse(farm$sm>=th, 1, 0), farm$sm, pmax(0, farm$sm-th) )
fit[[site]] = lsfit(x=X, y=farm$rc)
cat( paste(site, ": ", th, "; ", sm.pval[[site]], "\n", sep=""))
}
@
<<label=sm_plots, echo=FALSE, include=FALSE, keep.source=True>>=
layout(matrix(1:8,4,2))
titles = c(sites)
for( i in 1:length(sites) ) {
farm = data[data$site==sites[i],]
piecewise_plot(x=farm$sm, y=farm$rc, th=sm.breakpoints[i],
ylim=range(data$rc, na.rm=T), xlim=range(data$sm, na.rm=T),
xlab="soil moisture", ylab="runoff coefficient",
title=farm_names[[sites[i]]],
p=sm.pval[[sites[i]]])
}
@
\begin{figure}
\begin{center}
<<label=sm, fig=True, echo=False, width=7, height=9>>=
<<sm_plots>>
@
\end{center}
\caption{caption.\label{sm}}
\end{figure}
Now get the I30 breakpoints:\\
<<label=I30_breakpoints, echo=FALSE>>=
I30.pval = list()
cat("I30 breakpoints by farm:\n")
for(site in sites) {
farm = data[data$site==site & !is.na(data$I30),]
lower_lim = sort( farm$I30 )[4]
upper_lim = sort(farm$I30, decreasing=T )[4]
th <- optimize(piecewise, c(upper_lim, lower_lim), x=farm$I30, y=farm$rc)$minimum
I30.pval[[site]] = anova.bp(data, split.on="I30", site=site, discrete.xrange=FALSE, min.in.node=4)
cat( paste(site, ": ", round(th,1), "; p=", I30.pval[[site]], "\n", sep=""))
}
@
Now split the data into two bins based on the soil moisure breakpoint (above vs. below the breakpoint) and find the I30 breakpoint in each bin:\\
<<label=I30_by_binned_SM, echo=False>>=
I30.breakpoints = list()
limits = list()
I30.pval = list()
for(site in farms) {
limits[[site]] = c(0, breakpoints[[site]], Inf)
site_data = data[data$site==site,]
bp = vector()
cat(paste("Intensity breakpoints at ", site, " when binned by soil moisture:\n", sep=""))
for(i in 1:2) {
bin = site_data[site_data$sm>=limits[[site]][i] & site_data$sm<limits[[site]][i+1] & !is.na(site_data$I30),]
lower_lim = sort( bin$I30 )[4]
upper_lim = sort(bin$I30, decreasing=T )[4]
th <- optimize(piecewise, c(upper_lim, lower_lim), x=bin$I30, y=bin$rc)$minimum
I30.pval[[site]][i] = anova.bp(data[data$sm>=limits[[site]][i] &
data$sm<limits[[site]][i+1] & !is.na(data$I30),], split.on="I30",
site=site, discrete.xrange=FALSE, min.in.node=4)
cat( paste(limits[[site]][i], " <= SM < ", limits[[site]][i+1], ": ", round(th,1), "; p=", I30.pval[[site]][i], "\n", sep=""))
bp = c(bp, th)
}
I30.breakpoints[[site]] = bp
cat("\n")
}
@
<<label=binned_I30_plots, include=False, echo=False>>=
layout(t(matrix(1:6,2,3)))
for(site in farms) {
titles = c(paste(farm_names[[site]], " (SM < ", limits[[site]][2], "%)", sep=""), paste(farm_names[[site]], " (", limits[[site]][2], "% <= SM)", sep=""))
site_data = data[data$site==site,]
for(i in 1:2) {
bin = site_data[site_data$sm>=limits[[site]][i] & site_data$sm<limits[[site]][i+1] & !is.na(site_data$I30),]
piecewise_plot(x=bin$I30,
y=bin$rc,
th=I30.breakpoints[[site]][i],
ylim=range(data$rc, na.rm=T),
xlim=range(data$I30, na.rm=T),
xlab="I30",
ylab="runoff coefficient",
title=titles[i],
p=I30.pval[[site]][i])
}
}
@
\begin{figure}
\begin{center}
<<label=I30_binned, fig=True, echo=False, width=7, height=8>>=
<<binned_I30_plots>>
@
\end{center}
\caption{Top row: DF1, middle row: DF2, bottom row: DF3.\label{I30_binned}}
\end{figure}
When we put the events in bins based on their antecedent soil moisture (SM: high or low), the following are the precipitation breakpoints (units are centimeters of rain):\\
<<label=precip_by_binned_SM, include=False, echo=False>>=
precip.breakpoints = list()
precip.pval = list()
for(site in farms) {
site_data = data[data$site==site,]
bp = vector()
cat(paste("Precipitation breakpoints at ", site, " when binned by soil moisture:\n", sep=""))
for(i in 1:2) {
bin = site_data[site_data$sm>=limits[[site]][i] & site_data$sm<limits[[site]][i+1] & !is.na(site_data$prec),]
lower_lim = sort( bin$prec )[4]
upper_lim = sort(bin$prec, decreasing=T )[4]
th <- optimize(piecewise, c(upper_lim, lower_lim), x=bin$prec, y=bin$rc)$minimum
precip.pval[[site]][i] = anova.bp(data[data$sm>=limits[[site]][i] &
data$sm<limits[[site]][i+1] & !is.na(data$prec),], split.on="prec",
site=site, discrete.xrange=FALSE, min.in.node=4)
cat( paste(limits[[site]][i], " <= SM < ", limits[[site]][i+1], ": ", round(th,2), "; p=", precip.pval[[site]][i], "\n", sep=""))
bp = c(bp, th)
}
precip.breakpoints[[site]] = bp
cat("\n")
}
@
<<label=binned_precip_plots, include=False, echo=False>>=
layout(t(matrix(1:6,2,3)))
for(site in farms) {
titles = c(paste(farm_names[[site]], " (SM < ", limits[[site]][2], "%)", sep=""), paste(farm_names[[site]], " (", limits[[site]][2], "% <= SM)", sep=""))
site_data = data[data$site==site,]
for(i in 1:2) {
bin = site_data[site_data$sm>=limits[[site]][i] & site_data$sm<limits[[site]][i+1] & !is.na(site_data$prec),]
piecewise_plot(x=bin$prec,
y=bin$rc,
th=precip.breakpoints[[site]][i],
ylim=range(data$rc, na.rm=T),
xlim=range(data$prec, na.rm=T),
xlab="precip",
ylab="runoff coefficient",
title=titles[i],
p=precip.pval[[site]][i])
}
}
@
\begin{figure}
\begin{center}
<<label=precip_binned, fig=True, echo=False, width=7, height=8>>=
<<binned_precip_plots>>
@
\end{center}
\caption{Top row: DF1, middle row: DF1, bottom row: DF1.\label{precip_binned}}
\end{figure}
\end{document}