Skip to content

Commit 0593c59

Browse files
author
Ilaria Prosdocimi
committed
First load
0 parents  commit 0593c59

File tree

7 files changed

+1080
-0
lines changed

7 files changed

+1080
-0
lines changed

Data/dat.RDa

365 KB
Binary file not shown.

footnote.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This app is provided as is with no warranty and is only intended to be a demonstrative tool.
2+
It was developed by Ilaria Prosdocimi while she was at CEH Wallingford within the EA funded FEH Local project.
3+
The R code underlying the estimation is contained in the [ilaprosUtils](https://github.com/ilapros/ilaprosUtils) package. The package vignette gives a quick introduction of how to directly use the R functions.
4+
The code underlying this shinyApp can be viewed here.

introText.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This application gives a user-friendly interface for the estimation of flood frequency curves when combining historical data with systematic records from peak river flow.
2+
Information on the systematic peak flow value is given in the left panel below: if the systematic data come from one of the [NRFA](http://nrfa.ceh.ac.uk/peak-flow-data) Peak River Flow stations it can be requested by inserting the gauging station number, alternatively it can be given in the second field as comma separated values. Make sure you delete the station number if the peak flow values are inserted manually.
3+
The default distribution for peak flow data in the UK is the GLO distribution - currently the methods used in this application have only been developed for the GEV and the GLO distribution.
4+
If there are reasons to think that the data under study come from a GEV distribution - change the selected distribution.
5+
To combine some historical data with the systematic data some information is needed: the historical peak flow values, the length of the time spanned by the historical information (h) and the threshold above which historical data have been recorded (perception threshold X0).
6+
If the information available is that the historical flows were above the perception threshold and the actual flow values are unknown - indicate that in the options.
7+
Once you have included all the information available to you (which might be just the systematic data) - push the submit button: results will appear and be updated in the tabs below.
8+
9+
10+
Please note: it might happen that the numerical optimisation underlying the estimation procedure would fail. When this happens some cryptic error message will appear in the panels rather than the expected output. Try changing some of the underlying data and information and see if convergence can be achieved, unluckily this is a difficult step to automate.

ismevExtension.R

+444
Large diffs are not rendered by default.

server.R

+276
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
# P:/HRRD/FRF_Group/FEH Local/StatModel/develop
2+
library(shiny)
3+
# gumbX <- function(x, emp=FALSE) {
4+
# if(emp) x <- (1:length(x)-0.44)/(length(x) + 1 - 0.88)
5+
# -log(-log(x))
6+
# }
7+
8+
library(ismev)
9+
library(lmom)
10+
library(markdown)
11+
# devtools::install_github("ilapros/ilaprosUtils")
12+
# library(ilaprosUtils)
13+
14+
15+
source("withHist.R")
16+
source("ismevExtension.R")
17+
18+
# dat <- read.table("http://gist.githubusercontent.com/ilapros/44c09e95ab5be7591f74/raw/410b165001be83f5e4efddb7d078cf1780b48d69/amData",header = TRUE)
19+
# dat <- read.csv("Data/amData_v3.3.4.csv", header = TRUE,stringsAsFactors = FALSE)
20+
# dat <- dat[dat$Rejected == "False",-6]
21+
load("Data/dat.RDa")
22+
23+
#
24+
# fitdat <- function(x,k,X0,dist){
25+
# if(h == "NULL" & dist == "glo") glo.fit(xdat=x)
26+
# if(h == "NULL" & dist == "glo") glo.hist.fit(xdat=x, k = k, h = h, X0 = as.numeric(X0), binomialcens = FALSE)
27+
# if(h == "NULL" & dist == "gev") gev.fit(xdat=x)
28+
# if(h == "NULL" & dist == "gev") gev.hist.fit(xdat=x, k = k, h = h, X0 = as.numeric(X0), binomialcens = FALSE)
29+
# }
30+
#
31+
32+
33+
34+
# Define server logic for random distribution application
35+
shinyServer(function(input, output) {
36+
# GiveInfo <- input$GiveInfo
37+
#observe({reactive({input$GiveInfo})})
38+
#Station <- reactive({Station})
39+
#st <- Station()
40+
# output$summary <- renderPrint(selDat)
41+
42+
43+
44+
st <- reactive({
45+
selDat <- dat[dat$Station %in% input$Station,c("Year","Flow")]
46+
})
47+
48+
output$dataplot <- renderPlot({
49+
selDat <- st()
50+
if(max(1,dim(selDat)[1]) < 2) {
51+
selDat <- data.frame(Flow = as.numeric(unlist(strsplit(input$addAMAX,","))));
52+
selDat$Year = seq(from = 1, to = length(selDat$Flow))}
53+
plot(selDat[,c("Year","Flow")], col = "grey20", type = "h")
54+
})
55+
56+
output$ffaplot <- renderPlot({
57+
58+
selDat <- st()
59+
if(max(1,dim(selDat)[1]) < 2) {selDat <- data.frame(Flow = as.numeric(unlist(strsplit(input$addAMAX,","))));
60+
selDat$Year = seq(from = 1, to = length(selDat$Flow))}
61+
ff <- seq(0.25,0.995,length=300)
62+
63+
sfit <- switch (input$dist,
64+
"glo" = glo.fit(selDat$Flow,show=FALSE),
65+
"gev" = ismev::gev.fit(selDat$Flow,show=FALSE))
66+
sRet <- retPlot(sfit, pch = 16, p = c(seq(0.005,0.99,l=50),seq(0.991, 0.999, l=200)))
67+
68+
# estmle <- switch (input$dist,
69+
# "glo" = glo.fit(selDat$Flow,show=FALSE),
70+
# "gev" = ismev::gev.fit(selDat$Flow,show=FALSE))
71+
#
72+
#
73+
# fitret <- switch (input$dist,
74+
# "glo" = lmom::quaglo(ff,estmle$mle),
75+
# "gev" = lmom::quagev(ff,estmle$mle*c(1,1,-1)))
76+
# # print(estmle$mle)
77+
# estSE <- switch (input$dist,
78+
# "glo" = apply(t(glo.rl.gradient(a=estmle$mle, p=1-ff)), 1, q.form, m = estmle$cov),
79+
# "gev" = apply(t(gev.rl.gradient(a=estmle$mle, p=1-ff)), 1, q.form, m = estmle$cov))
80+
#
81+
# plot(gumbX(selDat$Flow, emp=TRUE), sort(selDat$Flow), col = 0, pch = 16,type="n",
82+
# xlab = paste(paste("Gumbel reduced variate","\n"),"-log(-log(1-1/T))"),
83+
# ylab = " ",bty="l",
84+
# xlim = gumbX(ff[c(1,length(ff))]),
85+
# ylim = range(c(fitret + 1.96 * sqrt(estSE), fitret - 1.96 * sqrt(estSE))))
86+
# mtext(expression(paste("Peak flow ",(m^3/s))),2,line = 2)
87+
# lines(gumbX(ff), fitret, col = 2)
88+
# lines(gumbX(ff), fitret + 1.96 * sqrt(estSE), lty = 4, col = 2)
89+
# lines(gumbX(ff), fitret - 1.96 * sqrt(estSE), lty = 4, col = 2)
90+
# legend("topleft", col = c(2),legend="Systematic data", bty = "n", lty = 1)
91+
legend("topleft", col = c(1),legend=c("Flood freq. curve with systematic data only"), bty = "n", lty = c(1))
92+
93+
if(input$hdata != "NULL"){
94+
hdat <- as.numeric(unlist(strsplit(input$hdata,",")))
95+
96+
hfit <- switch (input$dist,
97+
"glo" =
98+
glo.hist.fit(c(hdat,selDat$Flow),show=FALSE,
99+
h=as.numeric(unlist(strsplit(input$h,","))),
100+
k = length(hdat),
101+
X0 = as.numeric(unlist(strsplit(input$X0,","))),
102+
binomialcens = as.logical(input$binCens)),
103+
"gev" =
104+
gev.hist.fit(c(hdat,selDat$Flow),show=FALSE,
105+
h=as.numeric(unlist(strsplit(input$h,","))),
106+
k = length(hdat),
107+
X0 = as.numeric(unlist(strsplit(input$X0,","))),
108+
binomialcens = as.logical(input$binCens)))
109+
110+
hRet <- retPlot(hfit, col = 2, sign.alpha = 0, pch = 16, p = c(seq(0.005,0.99,l=50),seq(0.991, 0.999, l=100)))
111+
lines(log((hRet$p)/(1-hRet$p)), hRet$retLev - 1.96*hRet$se, col = 2, lty = 2)
112+
lines(log((hRet$p)/(1-hRet$p)), hRet$retLev + 1.96*hRet$se, col = 2, lty = 2)
113+
lines(log((sRet$p)/(1-sRet$p)), sRet$retLev, col = 1)
114+
lines(log((sRet$p)/(1-sRet$p)), sRet$retLev - 1.96*sRet$se, col = 1, lty = 2)
115+
lines(log((sRet$p)/(1-sRet$p)), sRet$retLev + 1.96*sRet$se, col = 1, lty = 2)
116+
117+
118+
#
119+
# gumbX(ff), fitret, col = 4)
120+
# lines(gumbX(ff), fitret + 1.96 * sqrt(estSE), lty = 4, col = 4)
121+
# lines(gumbX(ff), fitret - 1.96 * sqrt(estSE), lty = 4, col = 4)
122+
legend("topleft", col = c(1,2),legend=c("Flood freq. curve with systematic data only","Flood freq. curve with additional historical data"), bty = "n", lty = c(1,1))
123+
124+
}
125+
})
126+
127+
output$varplot <- renderPlot({
128+
129+
selDat <- st()
130+
if(max(1,dim(selDat)[1]) < 2) {selDat <- data.frame(Flow = as.numeric(unlist(strsplit(input$addAMAX,","))));
131+
selDat$Year = seq(from = 1, to = length(selDat$Flow))}
132+
ff <- seq(0.5,0.995,length=300)
133+
134+
sfit <- switch (input$dist,
135+
"glo" = glo.fit(selDat$Flow,show=FALSE),
136+
"gev" = ismev::gev.fit(selDat$Flow,show=FALSE))
137+
sRet <- retPlot(sfit, pch = 16, p = c(seq(0.005,0.99,l=50),seq(0.991, 0.999, l=200)), plot.out = FALSE)
138+
# estmle <- switch (input$dist,
139+
# "glo" = glo.fit(selDat$Flow,show=FALSE),
140+
# "gev" = ismev::gev.fit(selDat$Flow,show=FALSE))
141+
#
142+
#
143+
# fitret <- switch (input$dist,
144+
# "glo" = lmom::quaglo(ff,estmle$mle),
145+
# "gev" = lmom::quagev(ff,estmle$mle*c(1,1,-1)))
146+
# # print(estmle$mle)
147+
# estSE <- switch (input$dist,
148+
# "glo" = apply(t(glo.rl.gradient(a=estmle$mle, p=1-ff)), 1, q.form, m = estmle$cov),
149+
# "gev" = apply(t(gev.rl.gradient(a=estmle$mle, p=1-ff)), 1, q.form, m = estmle$cov))
150+
151+
plot(log((sRet$p)/(1-sRet$p)), 2*1.96*sRet$se/sRet$retLev, col = 1,
152+
ylab = "CI width over estimate",bty="l", type="l",
153+
xlab = paste(paste("Gumbel reduced variate","\n"),"-log(-log(1-1/T))"), lwd=2)
154+
155+
# plot(gumbX(ff), 2*1.96*sqrt(estSE)/fitret, col = 2,
156+
# ylab = "CI width over estimate",bty="l", type="l",
157+
# xlab = paste(paste("Gumbel reduced variate","\n"),"-log(-log(1-1/T))"), lwd=2)
158+
# legend("topleft", col = c(2),legend="Systematic data", bty = "n", lty = 1)
159+
legend("topleft", col = c(1),legend=c("Systematic only"), bty = "n", lty = 1)
160+
if(input$hdata != "NULL"){
161+
hdat <- as.numeric(unlist(strsplit(input$hdata,",")))
162+
# estmle <- switch (input$dist,
163+
# "glo" =
164+
# glo.hist.fit(c(hdat,selDat$Flow),show=FALSE,
165+
# h=as.numeric(unlist(strsplit(input$h,","))),
166+
# k = length(hdat),
167+
# X0 = as.numeric(unlist(strsplit(input$X0,","))),
168+
# binomialcens = as.logical(input$binCens)),
169+
# "gev" =
170+
# gev.hist.fit(c(hdat,selDat$Flow),show=FALSE,
171+
# h=as.numeric(unlist(strsplit(input$h,","))),
172+
# k = length(hdat),
173+
# X0 = as.numeric(unlist(strsplit(input$X0,","))),
174+
# binomialcens = as.logical(input$binCens)))
175+
# fitret <- switch (input$dist,
176+
# "glo" = lmom::quaglo(ff,estmle$mle),
177+
# "gev" = lmom::quagev(ff,estmle$mle*c(1,1,-1)))
178+
#
179+
# estSE <- switch (input$dist,
180+
# "glo" = apply(t(glo.rl.gradient(a=estmle$mle, p=1-ff)), 1, q.form, m = estmle$cov),
181+
# "gev" = apply(t(gev.rl.gradient(a=estmle$mle, p=1-ff)), 1, q.form, m = estmle$cov))
182+
#
183+
#lines(gumbX(ff), 2*1.96*sqrt(estSE)/fitret, col = 4, lwd=2)
184+
185+
hdat <- as.numeric(unlist(strsplit(input$hdata,",")))
186+
187+
hfit <- switch (input$dist,
188+
"glo" =
189+
glo.hist.fit(c(hdat,selDat$Flow),show=FALSE,
190+
h=as.numeric(unlist(strsplit(input$h,","))),
191+
k = length(hdat),
192+
X0 = as.numeric(unlist(strsplit(input$X0,","))),
193+
binomialcens = as.logical(input$binCens)),
194+
"gev" =
195+
gev.hist.fit(c(hdat,selDat$Flow),show=FALSE,
196+
h=as.numeric(unlist(strsplit(input$h,","))),
197+
k = length(hdat),
198+
X0 = as.numeric(unlist(strsplit(input$X0,","))),
199+
binomialcens = as.logical(input$binCens)))
200+
201+
hRet <- retPlot(hfit, col = 2, sign.alpha = 0, pch = 16, p = c(seq(0.005,0.99,l=50),seq(0.991, 0.999, l=100)), plot.out = FALSE)
202+
lines(log((hRet$p)/(1-hRet$p)), 2*1.96*hRet$se/hRet$retLev, col = 2, lwd=2)
203+
legend("topleft", col = c(0,2),legend=c(" ","Historical data"), bty = "n", lty = c(0,1))
204+
}
205+
})
206+
207+
208+
209+
210+
211+
output$summary <- renderPrint({
212+
213+
selDat <- st()
214+
if(max(1,dim(selDat)[1]) < 2) {selDat <- data.frame(Flow = as.numeric(unlist(strsplit(input$addAMAX,","))));
215+
selDat$Year = seq(from = 1, to = length(selDat$Flow))}
216+
ff <- seq(0.25,0.995,length=300)
217+
218+
estmle <- switch (input$dist,
219+
"glo" = glo.fit(selDat$Flow,show=FALSE)$mle,
220+
"gev" = ismev::gev.fit(selDat$Flow,show=FALSE)$mle*c(1,1,-1))
221+
estlmom <- switch (input$dist,
222+
"glo" = lmom::lmrglo(estmle),
223+
"gev" = lmom::lmrgev(estmle))
224+
225+
cat("Parameter estimates - systematic only\n")
226+
cat(estmle[1]," ",estmle[2]," ",estmle[3]," \n")
227+
cat("and corresponding L-moment\n")
228+
cat(estlmom[1]," ",estlmom[2]," ",estlmom[3]," \n\n")
229+
230+
if(input$hdata != "NULL"){
231+
hdat <- as.numeric(unlist(strsplit(input$hdata,",")))
232+
estmle <- switch (input$dist,
233+
"glo" =
234+
glo.hist.fit(c(hdat,selDat$Flow),show=FALSE,
235+
h=as.numeric(unlist(strsplit(input$h,","))),
236+
k = length(hdat),
237+
X0 = as.numeric(unlist(strsplit(input$X0,","))),
238+
binomialcens = as.logical(input$binCens))$mle,
239+
"gev" =
240+
gev.hist.fit(c(hdat,selDat$Flow),show=FALSE,
241+
h=as.numeric(unlist(strsplit(input$h,","))),
242+
k = length(hdat),
243+
X0 = as.numeric(unlist(strsplit(input$X0,","))),
244+
binomialcens = as.logical(input$binCens))$mle*c(1,1,-1))
245+
estlmom <- switch (input$dist,
246+
"glo" = lmom::lmrglo(estmle),
247+
"gev" = lmom::lmrgev(estmle))
248+
cat("Parameter estimates - with Historical data\n")
249+
cat(estmle[1]," ",estmle[2]," ",estmle[3]," \n")
250+
cat("and corresponding L-moment\n")
251+
cat(estlmom[1]," ",estlmom[2]," ",estlmom[3]," \n")
252+
}
253+
#print(lmom::samlmu(selDat$Flow))
254+
})
255+
256+
# Generate a plot of the data. Also uses the inputs to build the
257+
# plot label. Note that the dependencies on both the inputs and
258+
# the 'data' reactive expression are both tracked, and all expressions
259+
# are called in the sequence implied by the dependency graph
260+
# output$plot <- renderPlot({
261+
# plot(selDat[,c("Year","Flow")], col = "grey70", type = "h")
262+
# # points(dat[dat$Component == comp,c("hour","value")],
263+
# # col = ifelse(comp == "PM10",2,4), pch =16)
264+
# })
265+
#
266+
# # Generate a summary of the data
267+
# output$summary <- renderPrint({
268+
# # comp <- input$comp
269+
# summary(selDat)
270+
# })
271+
272+
# Generate an HTML table view of the data
273+
# output$table <- renderTable({
274+
# data.frame(x=data())
275+
# })
276+
})

ui.R

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# P:\HRRD\FRF_Group\FEH Local\StatModel\develop
2+
library(shiny)
3+
4+
5+
6+
# Define UI for random distribution application
7+
#shinyUI(pageWithSidebar(
8+
shinyUI(fixedPage(
9+
10+
headerPanel("Combining historical flood data with systematic records"),
11+
12+
fixedRow(
13+
column(12,
14+
h5(includeMarkdown("introText.md"), style = "line-height: 1.4"),
15+
fixedRow(
16+
column(5,
17+
strong("Systematic data information"),
18+
textInput("Station", h5("NRFA Gauging station number:"),value="39001"),
19+
textInput("addAMAX", h5("or give a series of AMAX values:"),value="100,12, 18, 163, 12, 17"),
20+
radioButtons("dist", h5("Distribution type:"), c("GLO" = "glo","GEV" = "gev"))),
21+
column(7,strong("Historical peak flows information"),
22+
textInput("hdata", h5("Add historical peak flow information:"),value="NULL"),
23+
textInput("h", h5("give the length of period length:"),value="give h"),
24+
textInput("X0", h5("and the perception threshold:"),value="give X0"),
25+
radioButtons("binCens", "Indicate the type of historical data:", c("Peak values" = "FALSE","Information that the X0 threshold is exceeded" = "TRUE"))),
26+
27+
submitButton("Submit"), p()),
28+
29+
tabsetPanel(
30+
tabPanel("Floofreq Plot", plotOutput("ffaplot")),
31+
# tabPanel("Variance ratio Plot",plotOutput("varplot")),
32+
tabPanel("AMAX Plot", plotOutput("dataplot")),
33+
tabPanel("Estimated pars", verbatimTextOutput("summary"))),
34+
35+
h5(includeMarkdown("footnote.md"), style = "line-height: 1.4")
36+
)
37+
)
38+
)
39+
40+
)
41+
42+
43+
44+
45+
#
46+
#
47+
# titlePanel("Combining flood historical data with systematic records"),
48+
# # headerPanel("Effects of adding historical data"),
49+
#
50+
#
51+
# sidebarLayout(
52+
# sidebarPanel("Station Information",
53+
# textInput("Station", "Gauging station number:",value="39001"),
54+
# textInput("addAMAX", "or give a series of AMAX values:",value="100,12, 18, 163, 12, 17"),
55+
# radioButtons("dist", "Distribution type:", c("GLO" = "glo","GEV" = "gev")),
56+
# "Give information the historical peak flows",
57+
# textInput("hdata", "Add historical peak flow information:",value="NULL"),
58+
# textInput("h", "give the length of period length:",value="give h"),
59+
# textInput("X0", "and the perception threshold:",value="give X0"),
60+
# radioButtons("binCens", "Indicate the type of historical data:", c("Peak values" = "FALSE","Information that the X0 threshold is exceeded" = "TRUE"))),
61+
#
62+
#
63+
# # c("Nitrate dioxide" = "NO2",
64+
# # "Pm 10" = "PM10"))),
65+
#
66+
# mainPanel(
67+
# tabsetPanel(
68+
# tabPanel("AMAX Plot", plotOutput("dataplot")),
69+
# tabPanel("Floofreq Plot", plotOutput("ffaplot")),
70+
# tabPanel("Variance ratio Plot", plotOutput("varplot")),
71+
# tabPanel("Estimated pars", verbatimTextOutput("summary")))
72+
# )
73+
# )

0 commit comments

Comments
 (0)