-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitting-dist.R
298 lines (283 loc) · 12.2 KB
/
fitting-dist.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
# Term Project: Flood Frequency Analysis
library(readxl)
library(sqldf)
library(fitdistrib)
library(devtools)
library(EnvStats)
library(nsRFA)
library(MASS)
library(FAdist)
library(Lmoments)
library(evd)
#library(smwrBase)
library(fitdistrplus)
library(lmomco)
library(e1071)
library(goftest)
library(extRemes)
# -------------------------------------------------------------------------------------------
# IMPORT & PROCESS DATA
# -------------------------------------------------------------------------------------------
# read in data
#data <- read_excel('data/USGS02489500.xls', sheet = 1)
preprocessing <- function(data) {
# sort by date and convert to dataframe
data_processed <- data.frame(data[order(data$DecYear),])
colnames(data_processed)[2] <- "aps"
# remove period prior to 1900 (not enough data)
data_processed <- data_processed[data_processed$Year > 1899,]
# drop minimum value in years with multiple values
data_processed <- sqldf("select Year, max(aps) as aps from data_processed group by Year")
#-----------------------------------------------------------------------
# Water Resources method:
## high outlier checking
# K_values <- read.csv('data/k_values.csv', header = TRUE)
# K <- K_values[which(length(data_processed$aps) == K_values$n), "K"]
# lmu <- mean(log(data_processed$aps)); ls <- sd(log(data_processed$aps))
# upr <- exp(lmu + K*ls)
# # remove outliers
# data_cleaned <- data_processed[data_processed$aps < upr,]
#
# # low outlier checking
# lmu <- mean(log(data_cleaned$aps)); ls <- sd(log(data_cleaned$aps))
# lwr <- exp(lmu - K*ls)
#
# # remove outliers
# data_cleaned <- data_processed[data_processed$aps > lwr,]
#-----------------------------------------------------------------------
# IQR method
q <- quantile(data_processed$aps)
iqr <- q[4] - q[2]
upr <- q[4] + 3*iqr
lwr <- q[2] - 3*iqr
outliers <- data_processed[(data_processed$aps > upr) | (data_processed$aps < lwr),]
data_cleaned <- data_processed[(data_processed$aps < upr) & (data_processed$aps > lwr),]
#-----------------------------------------------------------------------
# # Standard deviation method: expect 99.7% of data to fall within this range
# upr <- mean(data_processed$aps) + 3*sd(data_processed$aps)
# lwr <- mean(data_processed$aps) - 3*sd(data_processed$aps)
# data_cleaned <- data_processed[(data_processed$aps < upr) & (data_processed$aps > lwr),]
#-----------------------------------------------------------------------
# rank observations in decreasing order
data_ranked <- data_cleaned[order(data_cleaned$aps, decreasing = TRUE),]
data_ranked["rank"] <- seq(1,dim(data_cleaned)[1])
data_ranked <- data_ranked[order(data_ranked$Year),]
#attach(data_ranked)
# order by rank
data_sorted <- data_ranked[order(data_ranked$rank),]
list("aps" = data_ranked$aps,
"ranked" = data_ranked,
"sorted" = data_sorted,
"processed" = data_processed,
"noutliers" = dim(outliers)[1])
}
# for plotting
xseq <- seq(1,100000,10)
# -------------------------------------------------------------------------------------------
# ESTIMATION
# -------------------------------------------------------------------------------------------
# estimation for (log)normal distribution
normal <- function(x, method, log = FALSE, rp=NULL, plotx=FALSE) {
n = length(x)
if (log) { dist = "lognormal" } else { dist = "normal" }
if ((method == "mle") || (method == "mme")) {
mu <- mean(x); sd <- sqrt(mean((x - mean(x))^2))
params <- fitdistr(aps, dist)$estimate
sds <- fitdistr(aps, dist)$sd
mu <- params[1]; sd <- params[2];
} else if (method == "pwme") {
lmom <- Lmoments(x)
mu <- lmom[1] ; sd <- lmom[2]*sqrt(pi)
sds <- c(sd/sqrt(n), 0.5113*sd/sqrt(n))
}
if (log) {
xt <- qlnorm(1-(1/rp), mu, sd)
ploty <- dlnorm(plotx, mu, sd)
lik <- sum(dlnorm(exp(x), mu, sd, log = TRUE))
ad <- ad.test(exp(x), null = "plnorm", mean = mu, sd=sd)$statistic
} else {
xt <- qnorm(1-(1/rp), mu, sd)
ploty <- dnorm(plotx, mu, sd)
lik <- sum(dnorm(x, mu, sd, log = TRUE))
ad <- ad.test(x, null = "pnorm", mean = mu, sd=sd)$statistic
}
list("par" = list("mu" = mu, "sigma" = sd),
"sd" = list("mu" = sds[1], "sigma" = sds[2]),
"xt" = xt,
"ploty" = ploty,
"likelihood" = lik,
"parno" = 2,
"ad" = ad)
}
# -------------------------------------------------------------------------------------------
# estimation for exponential distribution
exponential <- function(x, rp=FALSE, plotx=FALSE) {
lambda <- 1/mean(x); n <- length(x)
xt <- log(rp)*(1/lambda)
ad <- ad.test(x, null = "pexp", rate=lambda)$statistic
list("par" = list("lambda" = lambda),
"sd" = list("lambda" = sqrt(lambda^2 / n)),
"xt" = xt,
"ploty" = dexp(plotx, lambda),
"likelihood" = sum(dexp(x, lambda, log=TRUE)),
"parno" = 1,
"ad" = ad)
}
# -------------------------------------------------------------------------------------------
# estimation for gamma distribution
gam <- function(x, method, rp = NULL, plotx=FALSE) {
if ((method == "mle")) {
fit <- fitdist(x, "gamma", "mle" , start=list(shape=0.5, scale=0.5))
params <- fit$estimate; sds <- fit$sd
shape = params[1]; scale = params[2]
} else if ((method == "mme")) {
params <- fitdist(x, "gamma", "mme", start=list(shape=0.5, scale=0.5))$estimate
shape = params[1]; scale = 1/params[2]
sds <- NA
} else if (method == "pwme") {
params <- gamma_Lmom(x)$estimate
shape = params[1]; scale = 1/params[2]
sds <- NA
}
ad <- ad.test(x, null = "pgamma", shape=shape, scale=scale)$statistic
kt <- kt_gamma(x, rp)
xt <- shape*scale + kt*sqrt(shape*(scale)^2)
list("par" = list("shape" = shape, "scale" = scale),
"sd" = list("shape" = sds[1], "scale" = sds[2]),
"xt" = xt,
"ploty" = dgamma(plotx, shape=shape, scale=scale),
"likelihood" = sum(dgamma(x, shape=shape, scale=scale, log=TRUE)),
"parno" = 2,
"ad" = ad)
}
# frequency factor for the gamma/Pearson 3 distribution
kt_gamma <- function(x, rp) {
z <- qnorm(1 - (1/rp))
k <- skew(x)/6
kt <- z + (z^2 - 1)*k + (1/3)*(z^3-6*z)*k^2 - (z^2 - 1)*k^3+z*k^4+(1/3)*k^5
}
# -------------------------------------------------------------------------------------------
# estimation for Pearson 3 distribution
nll <- function(x, p) {
alpha <- p[1]; beta <- p[2]; epsilon <- p[3]; n <- length(x)
if(alpha>0 & epsilon <= min(x)) {
-sum(dgamma(x-epsilon, scale = alpha, shape = beta, log=TRUE))
} else {
return(Inf)
}
}
p3 <- function(x, method, rp=NULL, plotx=FALSE) {
if (method == "mle") {
fit <- optim(x = x, c(500,3,200), nll, hessian=TRUE)
params <- fit$par; hess <- fit$hessian
location <- params[3]; scale <- params[1]; shape <- params[2]
sds <- sqrt(abs(diag(solve(-hess))))
} else if (method == "mme") {
params <- pearson_mom(x)$estimate
location <- params[1]; scale <- params[2]; shape <- params[3]
sds <- NA
} else if (method == "pwme") {
params <- pearson_Lmom(x)$estimate
location <- params[1]; scale <- params[2]; shape <- params[3]
sds <- NA
}
kt <- kt_gamma(x, rp)
xt <- location + shape*scale + kt*sqrt(shape*(scale)^2)
ploty <- PearsonDS::dpearsonIII(plotx, scale = scale, shape = shape, location = location)
lik <- sum(PearsonDS::dpearsonIII(x, scale = scale, shape = shape, location = location, log=TRUE))
ad <- ad.test(x-location, null = "pgamma", shape=shape, scale=scale )$statistic
list("par" = list("location" = location, "scale" = scale, "shape" = shape),
"sd" = list("location" = sds[3], "scale" = sds[1], "shape" = sds[2]),
"xt" = xt,
"ploty" = ploty,
"likelihood" = lik,
"parno" = 3,
"ad" = ad)
}
# -------------------------------------------------------------------------------------------
# estimation for Log-Pearson 3 distribution
lp3 <- function(x, method, plotx=FALSE, rp=NULL) {
if (method == "mle") {
fit <- optim(x = x, c(3,3,4), nll, hessian=TRUE, method="BFGS")
params <- fit$par; hess <- fit$hessian
location <- params[3]; scale <- params[1]; shape <- params[2]
sds <- sqrt(abs(diag(solve(-hess))))
} else if (method == "mme") {
params <- pearson_mom(x)$estimate
location <- params[1]; scale <- params[2]; shape <- params[3]
sds <- NA
} else if (method == "pwme") {
params <- parpe3(lmoms(x), checklmom = TRUE)$para
mu <- params[1]; std <- params[2]; skw <- params[3]
shape <- (2/skw)^2; scale <- std/sqrt(shape); location <- mu - scale*shape
sds <- NA
}
kt <- kt_gamma(x, rp)
xt <- exp(location + shape*scale + kt*sqrt(shape*(scale)^2))
ad <- ad.test(x - location, null = "pgamma", shape = shape, scale = scale )$statistic
list("par" = list("location" = location, "scale" = scale, "shape" = shape),
"sd" = list("location" = sds[3], "scale" = sds[1], "shape" = sds[2]),
"ad" = ad,
"xt" = xt,
"ploty" = dgamma(log(plotx) - location, shape = shape, scale = scale)/plotx,
"likelihood" = sum(dgamma(x - location, shape = shape, scale = scale, log = TRUE)) - sum(x),
"parno" = 3)
}
# -------------------------------------------------------------------------------------------
# estimation for Gumbel distribution
gumb <- function(x, method, rp = NULL, plotx=FALSE) {
fit1 <- eevd(x, method = method, ci=TRUE, ci.parameter = "location")
fit2 <- eevd(x, method = method, ci=TRUE, ci.parameter = "scale")
params <- fit1$parameter; df <- fit1$interval$dof; n <- fit1$interval$dof
ub1 <- fit1$interval$limits[2]; ub2 <- fit2$interval$limits[2]
sd_loc <- (ub1-params[1])/qt(0.975,df); sd_scale <- (ub2-params[2])/qt(0.975,df)
xt <- params[1] - params[2]*log(-log(1-1/rp))
ad <- try(ad.test(x, null = "pgumbel", alpha=params[1], scale=params[2])$statistic, silent=TRUE)
if("try-error" %in% class(ad)) { ad <- ad.test(x, null = "pgumbel", loc=params[1], scale=params[2])$statistic }
list("par" = list("location" = params[1], "scale" = params[2]),
"xt" = xt,
"ploty" = evd::dgumbel(plotx, loc = params[1], scale = params[2]),
"likelihood" = sum(evd::dgumbel(x, loc = params[1], scale = params[2], log=TRUE)),
"parno" = 2,
"sd" = list("location" = sd_loc, "scale" = sd_scale),
"ad" = ad)
}
# -------------------------------------------------------------------------------------------
# estimation for Weibull distribution
weibull <- function(x, method, rp=FALSE, plotx=FALSE) {
if (method == "mle") {
fit <- fitdist(x, dweibull3, start = list(shape = skew(x), scale = sd(x), thres = min(x)-100))
params <- fit$estimate; sd <- fit$sd
shape <- params[1]; scale <- params[2]; threshold <- params[3]
sds <- list("shape" = sd[1], "scale" = sd[2], "threshold" = sd[3])
ad <- ad.test(x, null = "pweibull3", shape=shape, scale=scale, thres = threshold)$statistic
xt <- threshold + scale*(log(rp))^(1/shape)
} else if (method == "mme") {
params <- eweibull(x, method)$para
shape = params[1];scale=params[2]
ad <- ad.test(x, null = "pweibull", shape=shape, scale=scale)$statistic
xt <- scale*(log(rp))^(1/shape)
return(list("par" = list("shape" = shape, "scale" = scale),
"xt" = xt,
"ploty" = dweibull(plotx, shape = shape, scale = scale),
"likelihood" = sum(dweibull(x, shape = shape, scale = scale, log=TRUE)),
"parno" = 2,
"sd" = NA,
"ad" = ad))
} else if (method == "pwme") {
params <- parwei(lmoms(x),checklmom=TRUE)$para
shape = params[3]; scale = params[2]; threshold = -params[1]
ad <- ad.test(x, null = "pweibull3", shape=shape, scale=scale, thres = threshold)$statistic
xt <- threshold + scale*(log(rp))^(1/shape)
sds <- NA
}
if ((method=="mle") || (method=="pwme")) {
list("par" = list("shape" = shape, "scale" = scale, "threshold" = threshold),
"xt" = xt,
"ploty" = dweibull3(plotx, shape = shape, thres = threshold, scale = scale),
"likelihood" = sum(dweibull3(x, shape = shape, thres = threshold, scale = scale, log=TRUE)),
"parno" = 3,
"sd" = sds,
"ad" = ad)
}
}