gevdfit.R 23.2 KB
Newer Older
1
2
3
# This file contains the functions:
# - gev.d.fit, gev.d.init for fitting
# - gev.d.diag for diagnostic plots
4
# - gev.d.params for calculation of parameters
5
6
7
8
# and the documentation of the example data

#### gev.d.fit ####

9
#' @title Maximum-likelihood Fitting of the duration-dependent GEV Distribution
Laura Mack's avatar
Laura Mack committed
10
11
12
#' @description Modified \code{\link[ismev]{gev.fit}} function for Maximum-likelihood fitting
#' for the duration-dependent generalized extreme
#' value distribution, following Koutsoyiannis et al. (1998), including generalized linear
13
#' modeling of each parameter.
Laura Mack's avatar
Laura Mack committed
14
#' @param xdat A vector containing maxima for different durations.
15
#' This can be obtained from \code{\link{IDF.agg}}.
Laura Mack's avatar
Laura Mack committed
16
#' @param ds A vector of aggregation levels corresponding to the maxima in xdat.
17
#' 1/60 corresponds to 1 minute, 1 corresponds to 1 hour.
Laura Mack's avatar
Laura Mack committed
18
19
#' @param ydat A matrix of covariates for generalized linear modeling of the parameters
#' (or NULL (the default) for stationary fitting). The number of rows should be the same as the
20
#' length of xdat.
21
#' @param  mutl,sigma0l,xil,thetal,etal Numeric vectors of integers, giving the columns of ydat that contain
Laura Mack's avatar
Laura Mack committed
22
#'  covariates for generalized linear modeling of the parameters (or NULL (the default)
23
#'  if the corresponding parameter is stationary).
24
#'  Parameters are: modified location, scale offset, shape, duration offset, duration exponent, respectively.
Laura Mack's avatar
Laura Mack committed
25
#' @param mutlink,sigma0link,xilink,thetalink,etalink Link functions for generalized linear
26
#' modeling of the parameters, created with \code{\link{make.link}}. The default is \code{make.link("identity")}.
27
#' @param init.vals list of length 5, giving initial values for all or some parameters
Laura Mack's avatar
Laura Mack committed
28
29
#' (order: mut, sigma0, xi, theta, eta). If as.list(rep(NA,5)) (the default) is given, initial parameters are obtained
#' internally by fitting the GEV separately for each duration and applying a linear model to obtain the
30
#' duration dependency of the location and shape parameter.
31
#' Initial values for covariate parameters are assumed as 0 if not given.
32
#' @param theta_zero Logical value, indicating if theta should be estimated (FALSE, the default) or
Laura Mack's avatar
Laura Mack committed
33
#' should stay zero.
34
35
36
37
#' @param show Logical; if TRUE (the default), print details of the fit.
#' @param method The optimization method used in \code{\link{optim}}.
#' @param maxit The maximum number of iterations.
#' @param ... Other control parameters for the optimization.
Laura Mack's avatar
Laura Mack committed
38
39
40
41
42
43
44
#' @return A list containing the following components.
#' A subset of these components are printed after the fit.
#' If \code{show} is TRUE, then assuming that successful convergence is indicated,
#' the components nllh, mle and se are always printed.
#' \item{nllh}{single numeric giving the negative log-likelihood value}
#' \item{mle}{numeric vector giving the MLE's for the modified location, scale_0, shape,
#' duration offset and duration exponent, resp.}
45
#' \item{se}{numeric vector giving the standard errors for the MLE's (in the same order)}
46
#' \item{trans}{A logical indicator for a non-stationary fit.}
Laura Mack's avatar
Laura Mack committed
47
#' \item{model}{A list with components mutl, sigma0l, xil, thetal and etal.}
48
#' \item{link}{A character vector giving inverse link functions.}
Laura Mack's avatar
Laura Mack committed
49
#' \item{conv}{The convergence code, taken from the list returned by \code{\link{optim}}.
50
#' A zero indicates successful convergence.}
Laura Mack's avatar
Laura Mack committed
51
52
#' \item{data}{data is standardized to standard Gumbel.}
#' \item{cov}{The covariance matrix.}
53
#' \item{vals}{Parameter values for every data point.}
54
#' \item{init.vals}{Initial values that were used.}
55
#' \item{ds}{Durations for every data point.}
56
57
#' @details For details on the d-GEV and the parameter definitions, see \link{IDF-package}.
#' @seealso \code{\link{IDF-package}}, \code{\link{IDF.agg}}, \code{\link{gev.fit}}, \code{\link{optim}}
58
#' @export
Laura Mack's avatar
Laura Mack committed
59
60
61
62
#' @importFrom stats optim
#' @importFrom stats make.link
#'
#' @examples
63
64
#' # sampled random data from d-gev with covariates
#' # GEV parameters:
65
66
#' # mut = 4 + 0.2*cov1 +0.5*cov2
#' # sigma0 = 2+0.5*cov1
67
68
69
#' # xi = 0.5
#' # theta = 0
#' # eta = 0.5
Laura Mack's avatar
Laura Mack committed
70
#'
71
#' data('example',package ='IDF')
Laura Mack's avatar
Laura Mack committed
72
#'
73
#' gev.d.fit(xdat=example$dat,ds = example$d,ydat=as.matrix(example[,c('cov1','cov2')])
74
#' ,mutl=c(1,2),sigma0l=1)
75
76

gev.d.fit<-
Laura Mack's avatar
Laura Mack committed
77
  function(xdat, ds, ydat = NULL, mutl = NULL, sigma0l = NULL, xil = NULL, thetal = NULL, etal = NULL,
78
           mutlink = make.link("identity"), sigma0link = make.link("identity"), xilink = make.link("identity"),
Laura Mack's avatar
Laura Mack committed
79
           thetalink = make.link("identity"), etalink = make.link("identity"),
80
           init.vals = as.list(rep(NA,5)), theta_zero = FALSE,
81
           show = TRUE, method = "Nelder-Mead", maxit = 10000, ...)
82
  {
83
84
    if (length(xdat) != length(ds)) {
      stop(paste0('The length of xdat is ',length(xdat),', but the length of ds is ',length(ds),'.'))
Laura Mack's avatar
Laura Mack committed
85
    }
86
    z <- list()
Laura Mack's avatar
Laura Mack committed
87
    # number of parameters (betas) to estimate for each parameter:
88
89
90
    npmu <- length(mutl) + 1
    npsc <- length(sigma0l) + 1
    npsh <- length(xil) + 1
91
    npth <- ifelse(!theta_zero,length(thetal) + 1,0)
92
93
    npet <- length(etal) + 1
    z$trans <- FALSE  # indicates if fit is non-stationary
94
95
    z$model <- list(mutl, sigma0l, xil, thetal, etal)
    z$link <- list(mutlink=mutlink, sigma0link=sigma0link, xilink=xilink, thetalink=thetalink, etalink=etalink)
Laura Mack's avatar
Laura Mack committed
96

97
98
    # test for NA values:
    if(any(is.na(xdat))) stop('xdat contains NA values. NA values need to be removed first.')
99
100
    # test for finite values:
    if(any(is.infinite(xdat))) stop('xdat contains non finite values. Inf and -Inf need to be removed first.')
Laura Mack's avatar
Laura Mack committed
101

102
103
    # test if covariates matrix is given correctly
    npar <- max(sapply(z$model,function(x){return(ifelse(is.null(x),0,max(x)))}))
104
    if(any(npar>ncol(ydat),npar>0 & is.null(ydat)))stop("Not enough columns in covariates matrix 'ydat'.")
Laura Mack's avatar
Laura Mack committed
105

106
    # initial values
107
108
109
    if(length(init.vals)!=5 | !is.list(init.vals)) {
      warning('Parameter init.vals is not used, because it is no list of length 5.')
      init.vals <- as.list(rep(NA,5))
110
111
    }
    if(!any(is.na(init.vals))){ #all initial values are given
112
      names(init.vals) <- c('mu','sigma','xi','theta','eta')
113
114
115
116
    }else if(any(!is.na(init.vals))) { #some initial values are given
      init.vals.user <- init.vals
      init.vals <- gev.d.init(xdat,ds,z$link) #calculate init.vals using gev.d.init
      for (i in 1:length(init.vals)){ #overwrite the calculated initial values with the ones given by the user
117
118
        if(!is.na(init.vals.user[[i]])) {
          init.vals[[i]]<-init.vals.user[[i]]
119
120
        }
      }
Laura Mack's avatar
Laura Mack committed
121
    }else{ #no initial values are given
122
      init.vals <- gev.d.init(xdat,ds,z$link)
123
    }
Laura Mack's avatar
Laura Mack committed
124
125

    # generate covariates matrices:
126
    if (is.null(mutl)) { #stationary
127
      mumat <- as.matrix(rep(1, length(xdat)))
128
      muinit <- init.vals$mu
129
    }else { #non stationary
130
      z$trans <- TRUE
131
132
      mumat <- cbind(rep(1, length(xdat)), ydat[, mutl])
      muinit <- c(init.vals$mu, rep(0, length(mutl)))[1:npmu] #fill with 0s to length npmu
133
    }
134
    if (is.null(sigma0l)) {
135
      sigmat <- as.matrix(rep(1, length(xdat)))
136
      siginit <- init.vals$sigma
137
138
    }else {
      z$trans <- TRUE
139
140
      sigmat <- cbind(rep(1, length(xdat)), ydat[, sigma0l])
      siginit <- c(init.vals$sigma, rep(0, length(sigma0l)))[1:npsc]
141
    }
142
    if (is.null(xil)) {
143
      shmat <- as.matrix(rep(1, length(xdat)))
Laura Mack's avatar
Laura Mack committed
144
      shinit <- init.vals$xi
145
146
    }else {
      z$trans <- TRUE
147
148
      shmat <- cbind(rep(1, length(xdat)), ydat[, xil])
      shinit <- c(init.vals$xi, rep(0, length(xil)))[1:npsh]
149
150
151
    }
    if (is.null(thetal)) {
      thmat <- as.matrix(rep(1, length(xdat)))
152
      thetainit <- init.vals$theta
153
154
155
    }else {
      z$trans <- TRUE
      thmat <- cbind(rep(1, length(xdat)), ydat[, thetal])
156
      thetainit <- c(init.vals$theta, rep(0, length(thetal)))[1:npth]
157
158
159
    }
    if (is.null(etal)) {
      etmat <- as.matrix(rep(1, length(xdat)))
160
      etainit <- init.vals$eta
161
162
163
    }else {
      z$trans <- TRUE
      etmat <- cbind(rep(1, length(xdat)), ydat[, etal])
164
      etainit <- c(init.vals$eta, rep(0, length(etal)))[1:npet]
165
    }
Laura Mack's avatar
Laura Mack committed
166

167

168
    if(!theta_zero){#When theta parameter is not included (default)
169
      init <- c(muinit, siginit, shinit, thetainit, etainit)
170
    }else{ #Do not return initial value for theta, if user does not want theta, as Hessian will fail.
171
172
      init <- c(muinit, siginit, shinit, etainit)
    }
173
174


175
176
177
    # function to calculate neg log-likelihood:
    gev.lik <- function(a) {
      # computes neg log lik of d-gev model
178
179
180
      mu <- mutlink$linkinv(mumat %*% (a[1:npmu]))
      sigma <- sigma0link$linkinv(sigmat %*% (a[seq(npmu + 1, length = npsc)]))
      xi <- xilink$linkinv(shmat %*% (a[seq(npmu + npsc + 1, length = npsh)]))
Laura Mack's avatar
Laura Mack committed
181
      # Next line will set the theta likelihood as non-existent in case user requested it.
182
      if(!theta_zero) {theta <- thetalink$linkinv(thmat %*% (a[seq(npmu + npsc + npsh + 1, length = npth)]))}
183
      eta <- etalink$linkinv(etmat %*% (a[seq(npmu + npsc + npsh + npth + 1, length = npet)]))
Laura Mack's avatar
Laura Mack committed
184

185
      ifelse(!theta_zero, ds.t <- ds+theta, ds.t <- ds) #Don't use theta if user requested not to have it.
186
187
188
      sigma.d <- sigma/(ds.t^eta)
      y <- xdat/sigma.d - mu
      y <- 1 + xi * y
Laura Mack's avatar
Laura Mack committed
189

190
      if(!theta_zero){ #When user wants to estimate theta parameter (default)
191
        if(any(eta <= 0) || any(theta < 0) || any(sigma.d <= 0) || any(y <= 0)) return(10^6)
Laura Mack's avatar
Laura Mack committed
192
      }else{
193
194
        if(any(eta <= 0) || any(sigma.d <= 0) || any(y <= 0)) return(10^6)
      }
Laura Mack's avatar
Laura Mack committed
195

196
197
      sum(log(sigma.d)) + sum(y^(-1/xi)) + sum(log(y) * (1/xi + 1))
    }
Laura Mack's avatar
Laura Mack committed
198
199


200
201
202
    # finding minimum of log-likelihood:
    x <- optim(init, gev.lik, hessian = TRUE, method = method,
               control = list(maxit = maxit, ...))
Laura Mack's avatar
Laura Mack committed
203

204
205
    # saving output parameters:
    z$conv <- x$convergence
206
207
208
    mut <- mutlink$linkinv(mumat %*% (x$par[1:npmu]))
    sc0 <- sigma0link$linkinv(sigmat %*% (x$par[seq(npmu + 1, length = npsc)]))
    xi <- xilink$linkinv(shmat %*% (x$par[seq(npmu + npsc + 1, length = npsh)]))
209
210
211
212
213
    if(!theta_zero){ #When user does NOT set theta parameter to zero (default)
      theta <- thetalink$linkinv(thmat %*% (x$par[seq(npmu + npsc + npsh + 1, length = npth)]))
    }else{ #When user requests theta_parameter to be zero
      theta <- thetalink$linkinv(thmat %*% (0))
    }
214
    eta <- etalink$linkinv(etmat %*% (x$par[seq(npmu + npsc + npsh + npth + 1, length = npet)]))
215
    z$nllh <- x$value
216
    # normalize data to standard Gumbel:
217
    sc.d <- sc0/((ds+theta)^eta)
Laura Mack's avatar
Laura Mack committed
218
    z$data <-  - log(as.vector((1 + xi * (xdat/sc.d-mut))^(-1/xi)))
219
    z$mle <- x$par
Laura Mack's avatar
Laura Mack committed
220
    test <- try(              # catch error
221
    z$cov <- solve(x$hessian) # invert hessian to get estimation on var-covar-matrix
222
223
224
225
226
    ,silent = TRUE )
    if("try-error" %in% class(test)){
      warning("Hessian could not be inverted. NAs were produced.")
      z$cov <- matrix(NA,length(z$mle),length(z$mle))
        }
Laura Mack's avatar
Laura Mack committed
227
    z$se <- sqrt(diag(z$cov)) # sqrt(digonal entries) = standart error of mle's
228
229
230
231
232
    if (!theta_zero) {#When theta parameter is returned (default)
      z$vals <- cbind(mut, sc0, xi, theta, eta)
    } else {#When theta parameter is not returned, asked by user
      z$vals <- cbind(mut, sc0, xi, eta)
    }
233
    z$init.vals <- unlist(init.vals)
234
235
236
237
238
    if(!theta_zero){ #When theta parameter is returned (default)
      colnames(z$vals) <-c('mut','sigma0','xi','theta','eta')
    } else { #When theta parameter is not returned, asked by user
      colnames(z$vals) <-c('mut','sigma0','xi','eta')
    }
239
    z$ds <- ds
Laura Mack's avatar
Laura Mack committed
240
    z$theta_zero <- theta_zero #Indicates if theta parameter was set to zero by user.
241
    if(show) {
Laura Mack's avatar
Laura Mack committed
242
      if(z$trans) { # for nonstationary fit
Laura Mack's avatar
Laura Mack committed
243
        print(z[c(2, 4)]) # print model, link (3) , conv
Laura Mack's avatar
Laura Mack committed
244
        # print names of link functions:
245
        cat('$link\n')
246
        print(c(z$link$mutlink$name,z$link$sigma0link$name,z$link$xilink$name,z$link$thetalink$name,z$link$etalink$name))
247
        cat('\n')
Laura Mack's avatar
Laura Mack committed
248
      }else{print(z[4])} # for stationary fit print only conv
Laura Mack's avatar
Laura Mack committed
249
      if(!z$conv){ # if fit converged
250
        print(z[c(5, 7, 9)]) # print nll, mle, se
Laura Mack's avatar
Laura Mack committed
251
      }
252
    }
253
    class(z) <- "gev.d.fit"
254
255
256
257
258
259
260
    invisible(z)
}


#### gev.d.init ####

# function to get initial values for gev.d.fit:
Laura Mack's avatar
Laura Mack committed
261
# obtain initial values
262
# by fitting every duration separately
263
264

# possible ways to improve:
265
# take given initial values into account, if there are any
266
267
268
269
270
271
272
273
# xi -> mean vs. median ... how do we improve that?
# mu_tilde -> is not very good for small sample sizes yet
# improved inital value for eta, by fitting both mu~d and sigma~d in log-log scale

#' @title get initial values for gev.d.fit
#' @description obtain initial values by fitting every duration seperately
#' @param xdat vector of maxima for differnt durations
#' @param ds vector of durations belonging to maxima in xdat
274
#' @param link list of 5, link functions for parameters, created with \code{\link{make.link}}
275
#' @return list of initail values for mu_tilde, sigma_0, xi, eta
Laura Mack's avatar
Laura Mack committed
276
277
#' @importFrom stats lm
#' @importFrom stats median
278
#' @importFrom ismev gev.fit
Laura Mack's avatar
Laura Mack committed
279
#' @keywords internal
280

281
gev.d.init <- function(xdat,ds,link){
282
283
284
  durs <- unique(ds)
  mles <- matrix(NA, nrow=length(durs), ncol= 3)
  for(i in 1:length(durs)){
285
    test <- try(fit <- ismev::gev.fit(xdat[ds==durs[i]],show = FALSE),silent = TRUE)
286
    if("try-error" %in% class(test) | fit$conv!=0){mles[i,] <- rep(NA,3)}else{mles[i,] <- fit$mle}
287
  }
288
  if(all(is.na(mles))){stop('Initial values could not be computed for this dataset.')}
289
  # get values for sig0 and eta (also mu_0) from linear model in log-log scale
290
291
  lmsig <- lm(log(mles[,2])~log(durs))
  lmmu <- lm(log(mles[,1])~log(durs))
Laura Mack's avatar
Laura Mack committed
292

293
  # sig0 <- exp Intercept
294
  siginit <- link$sigma0link$linkfun(exp(lmsig$coefficients[[1]]))
Laura Mack's avatar
Laura Mack committed
295
  # eta <- mean of negativ slopes
296
  etainit <- link$etalink$linkfun(mean(c(-lmsig$coefficients[[2]],-lmmu$coefficients[[2]])))
Laura Mack's avatar
Laura Mack committed
297
  # mean of mu_d/sig_d
298
  # could try:
299
  # mu0/sig0 = exp(lmmu$coefficients[[1]])/exp(lmsig$coefficients[[1]])
300
  muinit <- link$mutlink$linkfun(median(c(mles[,1]/mles[,2]),na.rm = TRUE))
Laura Mack's avatar
Laura Mack committed
301
  # mean of shape parameters
302
  shinit <- link$xilink$linkfun(median(mles[,3],na.rm = TRUE))
303
  thetainit <- link$thetalink$linkfun(0)
Laura Mack's avatar
Laura Mack committed
304

Jana Ulrich's avatar
Jana Ulrich committed
305
  return(list(mu=muinit,sigma=siginit,xi=shinit,theta=thetainit,eta=etainit))
306
307
}

308
309
310
#### gev.d.lik ####

#' d-GEV Likelihood
311
#'
312
#' Computes (log-) likelihood of d-GEV model
313
#' @param xdat numeric vector containing observations
314
315
316
#' @param ds numeric vector containing corresponding durations (1/60 corresponds to 1 minute, 1 corresponds to 1 hour)
#' @param mut,sigma0,xi,theta,eta numeric vectors containing corresponding estimates for each of the parameters
#' @param log Logical; if TRUE, the log likelihood is returned.
317
#'
Laura Mack's avatar
Laura Mack committed
318
#' @return single value containing (log) likelihood
319
320
321
#' @export
#'
#' @examples
322
#' # compute log-likelihood of observation values not included in fit
323
324
#' train.set <- example[example$d!=2,]
#' test.set <- example[example$d==2,]
325
#' fit <- gev.d.fit(train.set$dat,train.set$d,mutl = c(1,2),sigma0l = 1
326
327
#'           ,ydat = as.matrix(train.set[c('cov1','cov2')]))
#' params <- gev.d.params(fit,ydat = as.matrix(test.set[c('cov1','cov2')]))
328
329
330
331
#' gev.d.lik(xdat = test.set$dat,ds = test.set$d,mut = params[,1],sigma0 = params[,2],xi = params[,3]
#'           ,theta = params[,4],eta = params[,5],log=TRUE)
gev.d.lik <- function(xdat,ds,mut,sigma0,xi,theta,eta,log=FALSE) {
  if(any(xi==0)){stop('Function is not defined for shape parameter of zero.')}
Laura Mack's avatar
Laura Mack committed
332
  if(any(! c(length(ds),length(mut),length(sigma0),length(xi),length(theta),length(eta)) %in%
333
         c(1,length(xdat)))){
334
    stop('Input vectors differ in length, but must have the same length.')
335
  }
Laura Mack's avatar
Laura Mack committed
336

337
  ds.t <- ds+theta
338
  sigma.d <- sigma0/(ds.t^eta)
339
340
  y <- xdat/sigma.d - mut
  y <- 1 + xi * y
Laura Mack's avatar
Laura Mack committed
341

342
343
344
345
346
  if(log){
    return(sum(log(sigma.d) + y^(-1/xi) + log(y) * (1/xi + 1)))
  }else{
    return(prod(sigma.d * exp(y^(-1/xi)) * y ^ (1/xi + 1)))
  }
Laura Mack's avatar
Laura Mack committed
347

348
}
349
350
351
352
353

#### gev.d.diag ####

#' Diagnostic Plots for d-gev Models
#'
Laura Mack's avatar
Laura Mack committed
354
355
#' @description  Produces diagnostic plots for d-gev models using
#' the output of the function \code{\link{gev.d.fit}}. Values for different durations can be plotted in
356
357
358
#' different colors of with different symbols.
#' @param fit object returned by \code{\link{gev.d.fit}}
#' @param subset an optional vector specifying a subset of observations to be used in the plot
359
#' @param cols optional either one value or vector of same length as \code{unique(fit$ds)} to
Laura Mack's avatar
Laura Mack committed
360
#' specify the colors of plotting points.
361
#' The default uses the \code{rainbow} function.
362
#' @param pch optional either one value or vector of same length as \code{unique(fit$ds)} containing
363
364
#' integers or symbols to specify the plotting points.
#' @param which string containing 'both', 'pp' or 'qq' to specify, which plots should be produced.
365
#' @param mfrow vector specifying layout of plots. If both plots should be produced separately,
366
367
#' set to \code{c(1,1)}.
#' @param legend logical indicating if legends should be plotted
Jana Ulrich's avatar
Jana Ulrich committed
368
369
#' @param title character vector of length 2, giving the titles for the pp- and the qq-plot
#' @param emp.lab,mod.lab character string containing names for empirical and model axis
Laura Mack's avatar
Laura Mack committed
370
#' @param ... additional parameters passed on to the plotting function
371
372
373
374
375
376
377
#'
#' @export
#' @importFrom graphics plot abline par title
#' @importFrom grDevices rainbow
#'
#' @examples
#' data('example',package ='IDF')
Laura Mack's avatar
Laura Mack committed
378
#'
379
#' fit <- gev.d.fit(xdat=example$dat,ds = example$d,ydat=as.matrix(example[,c('cov1','cov2')])
380
#'                  ,mutl=c(1,2),sigma0l=1)
Laura Mack's avatar
Laura Mack committed
381
382
383
#' # diagnostic plots for complete data
#' gev.d.diag(fit,pch=1)
#' # diagnostic plots for subset of data (e.g. one station)
384
#' gev.d.diag(fit,subset = example$cov1==1,pch=1)
Jana Ulrich's avatar
Jana Ulrich committed
385
386
387
gev.d.diag <- function(fit,subset=NULL,cols=NULL,pch=NULL,which='both',mfrow=c(1,2),legend=TRUE,
                       title=c('Residual Probability Plot','Residual Quantile Plot'),
                       emp.lab='Empirical',mod.lab='Model',...){
388
  # check parameter:
Jana Ulrich's avatar
Jana Ulrich committed
389
  if(!is.element(which,c('both','pp','qq'))) stop("Parameter 'which'= ",which,
390
391
392
                                                 " but only 'both','pp' or 'qq' are allowed.")
  # subset data
  df <- data.frame(data=fit$data,ds=fit$ds)
393
  if(!is.null(subset)){
Laura Mack's avatar
Laura Mack committed
394
    if(dim(df)[1]!=length(subset)){stop("Length of 'subset' does not match length of data
395
396
397
                                        'xdat' used for fitting.")}
    df <- df[subset,]
  }
Jana Ulrich's avatar
Jana Ulrich committed
398
399
400
401
402
  # get single durations
  durs <- sort(unique(df$ds))
  # rescale durations to assign colors
  df$cval <- sapply(df$ds,function(d){which(durs==d)})

Laura Mack's avatar
Laura Mack committed
403
  # sort data
404
  df <- df[order(df$data),]
Laura Mack's avatar
Laura Mack committed
405

406
407
408
409
410
411
412
  # plotting position
  n <- length(df$data)
  px <- (1:n)/(n + 1)

  # create plots:
  if(which=='both') par(mfrow=mfrow) # 2 subplots
  # colors and symbols
Jana Ulrich's avatar
Jana Ulrich committed
413
  if(is.null(cols))cols <- rainbow(length(durs))
414
  if(is.null(pch))pch <- df$cval
Laura Mack's avatar
Laura Mack committed
415

416
417
418
  if(which=='both'|which=='pp'){
    # pp
    plot(px, exp( - exp( - df$data)), xlab =
Jana Ulrich's avatar
Jana Ulrich committed
419
420
421
           emp.lab, ylab = mod.lab,col=cols[df$cval],pch=pch,...)
    abline(0, 1, col = 1,lwd=1)
    title(title[1])
Jana Ulrich's avatar
Jana Ulrich committed
422
    if(legend){legend('bottomright',legend = round(durs,digits = 2),pch=pch,
423
                      col = cols[1:length(durs)],title = 'Duration [h]',ncol = 2)}
424
425
426
427
  }
  if(which=='both'|which=='qq'){
    # qq
    plot( - log( - log(px)), df$data, ylab =
Jana Ulrich's avatar
Jana Ulrich committed
428
429
430
            emp.lab, xlab = mod.lab,col=cols[df$cval],pch=pch,...)
    abline(0, 1, col = 1,lwd=1)
    title(title[2])
Jana Ulrich's avatar
Jana Ulrich committed
431
    if(legend){legend('bottomright',legend = round(durs,digits = 2),pch=pch,
432
                      col = cols[1:length(durs)],title = 'Duration [h]',ncol = 2)}
433
434
435
436
437
438
439
440
  }
  if(which=='both') par(mfrow=c(1,1)) # reset par
}

#### gev.d.params ####

#' Calculate gev(d) parameters from \code{gev.d.fit} output
#'
Laura Mack's avatar
Laura Mack committed
441
442
#' @description function to calculate mut, sigma0, xi, theta, eta
#' (modified location, scale offset, shape, duration offset, duration exponent)
443
444
#' from results of \code{\link{gev.d.fit}} with covariates or link funktions other than identity.
#' @param fit fit object returned by \code{\link{gev.d.fit}} or \code{\link{gev.fit}}
445
#' @param ydat A matrix containing the covariates in the same order as used in \code{gev.d.fit}.
446
#' @seealso \code{\link{IDF-package}}
447
#' @return data.frame containing mu_tilde, sigma0, xi, theta, eta (or mu, sigma, xi for gev.fit objects)
448
#' @export
Laura Mack's avatar
Laura Mack committed
449
#'
450
451
452
#' @examples
#' data('example',package = 'IDF')
#' fit <- gev.d.fit(example$dat,example$d,ydat = as.matrix(example[,c("cov1","cov2")])
453
#'                  ,mutl = c(1,2),sigma0l = 1)
454
455
456
#' gev.d.params(fit = fit,ydat = cbind(c(0.9,1),c(0.5,1)))


457
gev.d.params <- function(fit,ydat=NULL){
458
  if(!class(fit)%in%c("gev.d.fit","gev.fit"))stop("'fit' must be an object returned by 'gev.d.fit' or 'gev.fit'.")
459
  if(!is.null(ydat)){
Laura Mack's avatar
Laura Mack committed
460
    # check covariates matrix
461
462
463
    if(!is.matrix(ydat))stop("'ydat' must be of class matrix.")
    n.par <- max(sapply(fit$model,function(x){return(ifelse(is.null(x),0,max(x)))}))
    if(n.par>ncol(ydat))stop("Covariates-Matrix 'ydat' has ",ncol(ydat), " columns, but ", n.par," are required.")
464
465
466
467
  }else{if(!fit$trans){# no model -> no covariates matrix
    ydat <- matrix(1)
    }else{stop("To calculate parameter estimates, covariates matrix 'ydat' must be provided.")}
  }
Laura Mack's avatar
Laura Mack committed
468

469
470
471
472
  # number of parameters
  npmu <- length(fit$model[[1]]) + 1
  npsc <- length(fit$model[[2]]) + 1
  npsh <- length(fit$model[[3]]) + 1
473
474
475
476
477
  if(class(fit)=="gev.d.fit"){
    if(!fit$theta_zero){npth <- length(fit$model[[4]]) + 1 #Including theta parameter (default)]
    }else{npth <- 0}#With no theta parameter, asked by user
    npet <- length(fit$model[[5]]) + 1
  }
Laura Mack's avatar
Laura Mack committed
478

479
  # inverse link functions
480
  if(class(fit)=="gev.d.fit"){
481
482
483
    mulink <- fit$link$mutlink$linkinv
    siglink <- fit$link$sigma0link$linkinv
    shlink <- fit$link$xilink$linkinv
484
    if(!fit$theta_zero) thetalink <- fit$link$thetalink$linkinv
485
486
487
488
489
490
    etalink <- fit$link$etalink$linkinv
  }else{
    mulink <- eval(parse(text=fit$link))[[1]]
    siglink <- eval(parse(text=fit$link))[[2]]
    shlink <- eval(parse(text=fit$link))[[3]]
  }
Laura Mack's avatar
Laura Mack committed
491

492
493
494
495
  # covariates matrices
  mumat <- cbind(rep(1, dim(ydat)[1]), matrix(ydat[, fit$model[[1]]],dim(ydat)[1],npmu-1))
  sigmat <- cbind(rep(1, dim(ydat)[1]), matrix(ydat[, fit$model[[2]]],dim(ydat)[1],npsc-1))
  shmat <- cbind(rep(1, dim(ydat)[1]), matrix(ydat[, fit$model[[3]]],dim(ydat)[1],npsh-1))
496
497
498
499
  if(class(fit)=="gev.d.fit"){
    if(!fit$theta_zero){thmat <- cbind(rep(1, dim(ydat)[1]), matrix(ydat[, fit$model[[4]]],dim(ydat)[1],npth-1))}
    etmat <- cbind(rep(1, dim(ydat)[1]), matrix(ydat[, fit$model[[5]]],dim(ydat)[1],npet-1))
  }
Laura Mack's avatar
Laura Mack committed
500

501
502
503
504
  # calculate parameters
  mut <- mulink(mumat %*% (fit$mle[1:npmu]))
  sc0 <- siglink(sigmat %*% (fit$mle[seq(npmu + 1, length = npsc)]))
  xi <- shlink(shmat %*% (fit$mle[seq(npmu + npsc + 1, length = npsh)]))
505
506
507
  if(class(fit)=="gev.d.fit" ){
    if(!fit$theta_zero){theta <- thetalink(thmat %*% (fit$mle[seq(npmu + npsc + npsh + 1, length = npth)]))
    }else{theta <- rep(0,dim(ydat)[1])}}
508
  if(class(fit)=="gev.d.fit"){eta <- etalink(etmat %*% (fit$mle[seq(npmu + npsc + npsh + npth + 1, length = npet)]))}
Laura Mack's avatar
Laura Mack committed
509

510
  if(class(fit)=="gev.d.fit"){
511
    return(data.frame(mut=mut,sigma0=sc0,xi=xi,theta=theta,eta=eta))
512
  }else{return(data.frame(mu=mut,sig=sc0,xi=xi))}
513
514
515
516
517
}


#### example data ####

518
#' Sampled data for duration-dependent GEV
519
#'
Laura Mack's avatar
Laura Mack committed
520
#' @description
521
#' Randomly sampled data set used for running the example code, containing:
522
523
524
525
#' \itemize{
#'   \item \code{$xdat}: 'annual' maxima values
#'   \item \code{$ds}: corresponding durations
#'   \item \code{$cov1}, \code{$cov2}: covariates}
526
#' d-GEV parameters used for sampling:
527
#' \itemize{
528
529
530
531
532
#'   \item \eqn{\tilde{\mu} = 4 + 0.2 cov_1 +0.5 cov_2}
#'   \item \eqn{\sigma_0 = 2+0.5 cov_1}
#'   \item \eqn{\xi = 0.5}
#'   \item \eqn{\theta = 0}
#'   \item \eqn{\eta = 0.5}}
Laura Mack's avatar
Laura Mack committed
533
#'
534
535
536
537
538
#'
#' @docType data
#' @keywords datasets
#' @name example
#' @usage data('example',package ='IDF')
539
#' @format A data frame with 330 rows and 4 variables
540
NULL