|
| 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 | +}) |
0 commit comments