gevdfit.R 28.7 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,taul,eta2l 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.
25
#' @param mutlink,sigma0link,xilink,thetalink,etalink,taulink,eta2link 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.
Felix Fauer's avatar
Felix Fauer committed
32
33
#' @param theta_zero Logical value, indicating whether theta should be estimated (FALSE, the default) or
#' should stay zero.
34
#' @param tau_zero,eta2_zero Logical values, indicating whether tau,eta2 should be estimated (TRUE, the default) or
Laura Mack's avatar
Laura Mack committed
35
#' should stay zero.
36
37
38
39
#' @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
40
41
42
43
44
45
46
#' @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.}
47
#' \item{se}{numeric vector giving the standard errors for the MLE's (in the same order)}
48
#' \item{trans}{A logical indicator for a non-stationary fit.}
Laura Mack's avatar
Laura Mack committed
49
#' \item{model}{A list with components mutl, sigma0l, xil, thetal and etal.}
50
#' \item{link}{A character vector giving inverse link functions.}
Laura Mack's avatar
Laura Mack committed
51
#' \item{conv}{The convergence code, taken from the list returned by \code{\link{optim}}.
52
#' A zero indicates successful convergence.}
Laura Mack's avatar
Laura Mack committed
53
54
#' \item{data}{data is standardized to standard Gumbel.}
#' \item{cov}{The covariance matrix.}
55
#' \item{vals}{Parameter values for every data point.}
56
#' \item{init.vals}{Initial values that were used.}
57
#' \item{ds}{Durations for every data point.}
58
59
#' @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}}
60
#' @export
Laura Mack's avatar
Laura Mack committed
61
62
63
64
#' @importFrom stats optim
#' @importFrom stats make.link
#'
#' @examples
65
66
#' # sampled random data from d-gev with covariates
#' # GEV parameters:
67
68
#' # mut = 4 + 0.2*cov1 +0.5*cov2
#' # sigma0 = 2+0.5*cov1
69
70
71
#' # xi = 0.5
#' # theta = 0
#' # eta = 0.5
Laura Mack's avatar
Laura Mack committed
72
#'
73
#' data('example',package ='IDF')
Laura Mack's avatar
Laura Mack committed
74
#'
75
#' gev.d.fit(xdat=example$dat,ds = example$d,ydat=as.matrix(example[,c('cov1','cov2')])
76
#' ,mutl=c(1,2),sigma0l=1)
77
78

gev.d.fit<-
79
  function(xdat, ds, ydat = NULL, mutl = NULL, sigma0l = NULL, xil = NULL, thetal = NULL, etal = NULL, taul = NULL, eta2l = NULL,
80
           mutlink = make.link("identity"), sigma0link = make.link("identity"), xilink = make.link("identity"),
81
82
           thetalink = make.link("identity"), etalink = make.link("identity"), taulink = make.link("identity"), eta2link = make.link("identity"),
           init.vals = NULL, theta_zero = FALSE, tau_zero=TRUE, eta_zero=TRUE
83
           show = TRUE, method = "Nelder-Mead", maxit = 10000, ...)
84
  {
85
86
    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
87
    }
88
    z <- list()
Laura Mack's avatar
Laura Mack committed
89
    # number of parameters (betas) to estimate for each parameter:
90
91
92
    npmu <- length(mutl) + 1
    npsc <- length(sigma0l) + 1
    npsh <- length(xil) + 1
93
    npth <- ifelse(!theta_zero,length(thetal) + 1,0)
94
    npet <- length(etal) + 1
95
    npta <- ifelse(!tau_zero,  length(taul)   + 1,0)
96
    npe2 <- ifelse(!eta2_zero,  length(eta2l)   + 1,0)
97
    z$trans <- FALSE  # indicates if fit is non-stationary
98
    z$model <- list(mutl, sigma0l, xil, thetal, etal, taul)
99
    z$link <- list(mutlink=mutlink, sigma0link=sigma0link, xilink=xilink, thetalink=thetalink, etalink=etalink, taulink=taulink, eta2link=eta2link)
Laura Mack's avatar
Laura Mack committed
100

101
102
    # test for NA values:
    if(any(is.na(xdat))) stop('xdat contains NA values. NA values need to be removed first.')
103
104
    # 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
105

106
107
    # test if covariates matrix is given correctly
    npar <- max(sapply(z$model,function(x){return(ifelse(is.null(x),0,max(x)))}))
108
    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
109

110
    # initial values
111
    init.necessary.length = 4 + as.numeric(!theta_zero) + as.numeric(!eta2_zero) + as.numeric(!tau_zero)  # mut, sigma0, xi, theta, eta, eta2, tau
112
113
114
115
116
    if (is.null(init.vals)) {init.vals = as.list(rep(NA,init.necessary.length))
    }else{init.vals = as.list(init.vals)}
    
    if(length(init.vals)!=init.necessary.length | !is.list(init.vals)) {
      warning(paste0('Parameter init.vals is not used, because it is no list of length ',init.necessary.length,'.'))
Felix Fauer's avatar
Felix Fauer committed
117
118
119
120
121
122
123
124
      init.vals <- gev.d.init(xdat,ds,z$link)
      
    }else{ # length of given values is correct

      # name given initial values
      names1=c('mu','sigma','xi')                 # standard set of parameters
      if (!theta_zero){names1=c(names1,'theta')}  # add theta (in case)
      names1=c(names1,'eta')                      # add eta   (always)
125
      if (!eta2_zero){names1=c(names1,'eta2')}    # add eta2  (in case)
Felix Fauer's avatar
Felix Fauer committed
126
127
128
129
      if (!tau_zero){names1=c(names1,'tau')}      # add tau   (in case)
      names(init.vals) <- names1
      # add missing initial value (fixed internal number of parameters: 7)
      if (theta_zero) init.vals$theta = 0
130
      if (eta2_zero) init.vals$eta2 = init.vals$eta
Felix Fauer's avatar
Felix Fauer committed
131
      if (tau_zero) init.vals$tau = 0
132
      
Felix Fauer's avatar
Felix Fauer committed
133
134
135
      if(!any(is.na(init.vals))){ #all initial values are given
        # do nothing
      }else if(any(!is.na(init.vals))) { #some initial values are given
136
        if (!eta2_zero) print("autmoatic inital value setting not implemented yet for multiscaling (eta2_zero=FALSE)")
Felix Fauer's avatar
Felix Fauer committed
137
138
139
140
141
142
        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
          if(!is.na(init.vals.user[[names(init.vals.user)[i]]])) {
            init.vals[[names(init.vals.user)[i]]]<-init.vals.user[[names(init.vals.user)[i]]]
          } 
143
        }
Felix Fauer's avatar
Felix Fauer committed
144
      }else{ #no initial values are given
145
        if (!eta2_zero) print("autmoatic inital value setting not implemented yet for multiscaling (eta2_zero=FALSE)")
Felix Fauer's avatar
Felix Fauer committed
146
        init.vals <- gev.d.init(xdat,ds,z$link)
147
      }
Felix Fauer's avatar
Felix Fauer committed
148
    } 
Laura Mack's avatar
Laura Mack committed
149
150

    # generate covariates matrices:
151
    if (is.null(mutl)) { #stationary
152
      mumat <- as.matrix(rep(1, length(xdat)))
153
      muinit <- init.vals$mu
154
    }else { #non stationary
155
      z$trans <- TRUE
156
157
      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
158
    }
159
    if (is.null(sigma0l)) {
160
      sigmat <- as.matrix(rep(1, length(xdat)))
161
      siginit <- init.vals$sigma
162
163
    }else {
      z$trans <- TRUE
164
165
      sigmat <- cbind(rep(1, length(xdat)), ydat[, sigma0l])
      siginit <- c(init.vals$sigma, rep(0, length(sigma0l)))[1:npsc]
166
    }
167
    if (is.null(xil)) {
168
      shmat <- as.matrix(rep(1, length(xdat)))
Laura Mack's avatar
Laura Mack committed
169
      shinit <- init.vals$xi
170
171
    }else {
      z$trans <- TRUE
172
173
      shmat <- cbind(rep(1, length(xdat)), ydat[, xil])
      shinit <- c(init.vals$xi, rep(0, length(xil)))[1:npsh]
174
175
176
    }
    if (is.null(thetal)) {
      thmat <- as.matrix(rep(1, length(xdat)))
177
      thetainit <- init.vals$theta
178
179
180
    }else {
      z$trans <- TRUE
      thmat <- cbind(rep(1, length(xdat)), ydat[, thetal])
181
      thetainit <- c(init.vals$theta, rep(0, length(thetal)))[1:npth]
182
183
184
    }
    if (is.null(etal)) {
      etmat <- as.matrix(rep(1, length(xdat)))
185
      etainit <- init.vals$eta
186
187
188
    }else {
      z$trans <- TRUE
      etmat <- cbind(rep(1, length(xdat)), ydat[, etal])
189
      etainit <- c(init.vals$eta, rep(0, length(etal)))[1:npet]
190
    }
Felix Fauer's avatar
Felix Fauer committed
191
192
193
194
195
196
197
    if (is.null(taul)) {
      tamat <- as.matrix(rep(1, length(xdat)))
      tauinit <- init.vals$tau
    }else {
      z$trans <- TRUE
      tamat <- cbind(rep(1, length(xdat)), ydat[, taul])
      tauinit <- c(init.vals$tau, rep(0, length(taul)))[1:npta]
198
    }
199
200
201
202
203
204
205
206
207
    if (is.null(eta2l)) {
      e2mat <- as.matrix(rep(1, length(xdat)))
      eta2init <- init.vals$eta2
    }else {
      z$trans <- TRUE
      e2mat <- cbind(rep(1, length(xdat)), ydat[, eta2l])
      eta2init <- c(init.vals$eta2, rep(0, length(eta2l)))[1:npe2]
    }
    
208
    init <- c(muinit,siginit,shinit)
Felix Fauer's avatar
Felix Fauer committed
209
210
    if (!theta_zero) {init <- c(init,thetainit)} # add theta init (in case)
    init <- c(init,etainit)                      # add eta init   (always)
211
    if (!eta2_zero)  {init <- c(init,eta2init)}  # add eta2 init  (in case)
Felix Fauer's avatar
Felix Fauer committed
212
    if (!tau_zero)   {init <- c(init,tauinit)}   # add tau init   (in case)
213
     
214
215
216
    # function to calculate neg log-likelihood:
    gev.lik <- function(a) {
      # computes neg log lik of d-gev model
217
218
219
      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)]))
220
      # Next line will set the theta likelihood as non-existent in case user requested it. (same for tau)
221
      if(!theta_zero) {theta <- thetalink$linkinv(thmat %*% (a[seq(npmu + npsc + npsh + 1, length = npth)]))}
222
      eta <- etalink$linkinv(etmat %*% (a[seq(npmu + npsc + npsh + npth + 1, length = npet)]))
223
224
      if(!eta2_zero)  {eta2  <- eta2link$linkinv( e2mat %*% (a[seq(npmu + npsc + npsh + npth + npet + 1, length = npe2)]))}
      if(!tau_zero)   {tau   <- taulink$linkinv(  tamat %*% (a[seq(npmu + npsc + npsh + npth + npet + npe2 + 1, length = npta)]))}
225
      
226
      ifelse(!theta_zero, ds.t <- ds+theta, ds.t <- ds) #Don't use theta if user requested not to have it.
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
      #ifelse(!tau_zero,   sigma.d <- sigma/(ds.t^eta)+tau, sigma.d <- sigma/(ds.t^eta)) # don't use tau if user requested not to have it.
      if (tau_zero){ # don't use tau if user requested not to have it.
        if (eta2_zero){ # don't use eta2
          sigma.d <- sigma/(ds.t^eta)
          mu.d    <- mu*sigma.d
        }else{ # use eta2 (and no tau)
          sigma.d <-    sigma/(ds.t^eta2)
          mu.d    <- mu*sigma/(ds.t^eta)
        }
      }else{ # use tau
        if (eta2_zero){ # don't use eta2 
          sigma.d <- sigma/(ds.t^eta)+tau
          mu.d    <- mu*sigma.d
        }else{ # use eta2 (and tau)
          sigma.d <-     sigma/(ds.t^eta2)+tau
          mu.d    <- mu*(sigma/(ds.t^eta)+tau)
        }
      }
245
246
      #sigma.d <- sigma/(ds.t^eta)
      
247
248
249
250
      y = (xdat - mu.d) / sigma.d # new
      #y = (xdat - mu*sigma.d) / sigma.d # derivation
      
      #y <- xdat/sigma.d - mu # old
251
      y <- 1 + xi * y
Laura Mack's avatar
Laura Mack committed
252

Felix Fauer's avatar
Felix Fauer committed
253
254
255
256
257
258
259
260
261
262
      #if(!theta_zero){ #When user wants to estimate theta parameter (default)
      #  if(any(eta <= 0) || any(theta < 0) || any(sigma.d <= 0) || any(y <= 0)) return(10^6)
      #}else{
      #  if(any(eta <= 0) || any(sigma.d <= 0) || any(y <= 0)) return(10^6)
      #}
      #if(!tau_zero) {if(any(tau < 0)) return(10^6)} # check condition for tau as well.
      
      if(!theta_zero) {if(any(theta < 0)) {return(10^6)} } # check definition condition for theta
      if(any(eta <= 0) || any(sigma.d <= 0) || any(y <= 0)) return(10^6)
      if(!tau_zero)   {if(any(tau < 0))    {return(10^6)} } # check definition condition for tau.
263
      if(!eta2_zero) {if(any(eta2 < 0))    {return(10^6)} } # check definition condition for eta2.
Felix Fauer's avatar
Felix Fauer committed
264
      
265
      sum(log(sigma.d)) + sum(y^(-1/xi)) + sum(log(y) * (1/xi + 1)) # xxx continue here
266
    }
Laura Mack's avatar
Laura Mack committed
267
268


269
270
271
    # 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
272

273
274
    # saving output parameters:
    z$conv <- x$convergence
275
276
277
    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)]))
278
279
280
281
282
    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))
    }
283
    eta <- etalink$linkinv(etmat %*% (x$par[seq(npmu + npsc + npsh + npth + 1, length = npet)]))
284
285
    if(!tau_zero){  #When user does NOT set tau parameter to zero (not default)
      tau <- taulink$linkinv  (tamat %*% (x$par[seq(npmu + npsc + npsh + npth + npet + 1,length = npta)]))
Felix Fauer's avatar
Felix Fauer committed
286
287
288
    }else{ #When user requests tau parameter to be zero
      tau <- taulink$linkinv(tamat %*% (0))
    }
289
      
290
    z$nllh <- x$value
291
    # normalize data to standard Gumbel:
Felix Fauer's avatar
Felix Fauer committed
292
293
    sc.d <- sc0/((ds+theta)^eta)+tau

Laura Mack's avatar
Laura Mack committed
294
    z$data <-  - log(as.vector((1 + xi * (xdat/sc.d-mut))^(-1/xi)))
295
    z$mle <- x$par
Laura Mack's avatar
Laura Mack committed
296
    test <- try(              # catch error
297
    z$cov <- solve(x$hessian) # invert hessian to get estimation on var-covar-matrix
298
299
300
301
302
    ,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
303
    z$se <- sqrt(diag(z$cov)) # sqrt(digonal entries) = standart error of mle's
Felix Fauer's avatar
Felix Fauer committed
304
    'if (!theta_zero) {#When theta parameter is returned (default)
305
306
307
308
309
      if (!tau_zero){ # when tau is returned
        z$vals <- cbind(mut, sc0, xi, theta, eta, tau)
      }else{ # when tau is not returned
        z$vals <- cbind(mut, sc0, xi, theta, eta)
      }
310
    } else {#When theta parameter is not returned, asked by user
311
312
313
314
315
      if (!tau_zero){ # if tau is returned
        z$vals <- cbind(mut, sc0, xi, eta, tau)
      }else{ # if tau is not returned
        z$vals <- cbind(mut, sc0, xi, eta)
      }
Felix Fauer's avatar
Felix Fauer committed
316
317
    }'
    z$vals <- cbind(mut, sc0, xi, theta, eta, tau)
318
    z$init.vals <- unlist(init.vals)
319

Felix Fauer's avatar
Felix Fauer committed
320
321
322
323
324
325
    'names2 = c("mut","sigma0","xi")               # fixed part for set of names
    if(!theta_zero){names2=c(names2,"theta")}     # add theta (in case)
    names2 = c(names2, "eta")                     # add eta (always)
    if(!tau_zero){names2=c(names2, "tau")}        # add tau (in case)
    colnames(z$vals) <- names2'
    colnames(z$vals) <- c("mut","sigma0","xi","theta","eta","tau")
326
    
327
    z$ds <- ds
Laura Mack's avatar
Laura Mack committed
328
    z$theta_zero <- theta_zero #Indicates if theta parameter was set to zero by user.
329
    z$tau_zero <- tau_zero     #Indicates if tau parameter was set to zero by user.
330
    if(show) {
Laura Mack's avatar
Laura Mack committed
331
      if(z$trans) { # for nonstationary fit
Laura Mack's avatar
Laura Mack committed
332
        print(z[c(2, 4)]) # print model, link (3) , conv
Laura Mack's avatar
Laura Mack committed
333
        # print names of link functions:
334
        cat('$link\n')
Felix Fauer's avatar
Felix Fauer committed
335
336
337
338
339
        #if(!tau_zero){
        print(c(z$link$mutlink$name,z$link$sigma0link$name,z$link$xilink$name,z$link$thetalink$name,z$link$etalink$name, z$link$taulink$name))
        #} else{
        #  print(c(z$link$mutlink$name,z$link$sigma0link$name,z$link$xilink$name,z$link$thetalink$name,z$link$etalink$name))
        #}
340
        cat('\n')
Laura Mack's avatar
Laura Mack committed
341
      }else{print(z[4])} # for stationary fit print only conv
Laura Mack's avatar
Laura Mack committed
342
      if(!z$conv){ # if fit converged
343
        print(z[c(5, 7, 9)]) # print nll, mle, se
Laura Mack's avatar
Laura Mack committed
344
      }
345
    }
346
    class(z) <- "gev.d.fit"
347
348
349
350
351
352
353
    invisible(z)
}


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

# function to get initial values for gev.d.fit:
Laura Mack's avatar
Laura Mack committed
354
# obtain initial values
355
# by fitting every duration separately
356
357

# possible ways to improve:
358
# take given initial values into account, if there are any
359
360
# xi -> mean vs. median ... how do we improve that?
# mu_tilde -> is not very good for small sample sizes yet
Jana Ulrich's avatar
Jana Ulrich committed
361
# improved initial value for eta, by fitting both mu~d and sigma~d in log-log scale
362
363

#' @title get initial values for gev.d.fit
Jana Ulrich's avatar
Jana Ulrich committed
364
365
#' @description obtain initial values by fitting every duration separately
#' @param xdat vector of maxima for different durations
366
#' @param ds vector of durations belonging to maxima in xdat
367
#' @param link list of 5, link functions for parameters, created with \code{\link{make.link}}
Jana Ulrich's avatar
Jana Ulrich committed
368
#' @return list of initial values for mu_tilde, sigma_0, xi, eta
Laura Mack's avatar
Laura Mack committed
369
370
#' @importFrom stats lm
#' @importFrom stats median
371
#' @importFrom ismev gev.fit
Laura Mack's avatar
Laura Mack committed
372
#' @keywords internal
373

374
gev.d.init <- function(xdat,ds,link){
375
376
377
  durs <- unique(ds)
  mles <- matrix(NA, nrow=length(durs), ncol= 3)
  for(i in 1:length(durs)){
378
    test <- try(fit <- ismev::gev.fit(xdat[ds==durs[i]],show = FALSE),silent = TRUE)
379
    if("try-error" %in% class(test) | fit$conv!=0){mles[i,] <- rep(NA,3)}else{mles[i,] <- fit$mle}
380
  }
381
  if(all(is.na(mles))){stop('Initial values could not be computed for this dataset.')}
382
  # get values for sig0 and eta (also mu_0) from linear model in log-log scale
383
384
  lmsig <- lm(log(mles[,2])~log(durs))
  lmmu <- lm(log(mles[,1])~log(durs))
Laura Mack's avatar
Laura Mack committed
385

386
  # sig0 <- exp Intercept
387
  siginit <- link$sigma0link$linkfun(exp(lmsig$coefficients[[1]]))
Laura Mack's avatar
Laura Mack committed
388
  # eta <- mean of negativ slopes
389
  etainit <- link$etalink$linkfun(mean(c(-lmsig$coefficients[[2]],-lmmu$coefficients[[2]])))
Laura Mack's avatar
Laura Mack committed
390
  # mean of mu_d/sig_d
391
  # could try:
392
  # mu0/sig0 = exp(lmmu$coefficients[[1]])/exp(lmsig$coefficients[[1]])
393
  muinit <- link$mutlink$linkfun(median(c(mles[,1]/mles[,2]),na.rm = TRUE))
Laura Mack's avatar
Laura Mack committed
394
  # mean of shape parameters
395
  shinit <- link$xilink$linkfun(median(mles[,3],na.rm = TRUE))
396
  thetainit <- link$thetalink$linkfun(0)
Felix Fauer's avatar
Felix Fauer committed
397
  tauinit <- link$taulink$linkfun(0)
Laura Mack's avatar
Laura Mack committed
398

Felix Fauer's avatar
Felix Fauer committed
399
  return(list(mu=muinit,sigma=siginit,xi=shinit,theta=thetainit,eta=etainit, tau=tauinit))
400
401
}

402
403
404
#### gev.d.lik ####

#' d-GEV Likelihood
405
#'
406
#' Computes (log-) likelihood of d-GEV model
407
#' @param xdat numeric vector containing observations
408
#' @param ds numeric vector containing corresponding durations (1/60 corresponds to 1 minute, 1 corresponds to 1 hour)
409
#' @param mut,sigma0,xi,theta,eta,tau numeric vectors containing corresponding estimates for each of the parameters
410
#' @param log Logical; if TRUE, the log likelihood is returned.
411
#'
Laura Mack's avatar
Laura Mack committed
412
#' @return single value containing (log) likelihood
413
414
415
#' @export
#'
#' @examples
416
#' # compute log-likelihood of observation values not included in fit
417
418
#' train.set <- example[example$d!=2,]
#' test.set <- example[example$d==2,]
419
#' fit <- gev.d.fit(train.set$dat,train.set$d,mutl = c(1,2),sigma0l = 1
420
421
#'           ,ydat = as.matrix(train.set[c('cov1','cov2')]))
#' params <- gev.d.params(fit,ydat = as.matrix(test.set[c('cov1','cov2')]))
422
#' gev.d.lik(xdat = test.set$dat,ds = test.set$d,mut = params[,1],sigma0 = params[,2],xi = params[,3]
Felix Fauer's avatar
Felix Fauer committed
423
424
#'           ,theta = params[,4],eta = params[,5],log=TRUE)
gev.d.lik <- function(xdat,ds,mut,sigma0,xi,theta,eta,log=FALSE,tau=0) {
425
  if(any(xi==0)){stop('Function is not defined for shape parameter of zero.')}
426
  if(any(! c(length(ds),length(mut),length(sigma0),length(xi),length(theta),length(eta),length(tau)) %in%
427
         c(1,length(xdat)))){
428
    stop('Input vectors differ in length, but must have the same length.')
429
  }
Laura Mack's avatar
Laura Mack committed
430

431
  ds.t <- ds+theta
432
  sigma.d <- sigma0/(ds.t^eta) + tau
433
434
  y <- xdat/sigma.d - mut
  y <- 1 + xi * y
Laura Mack's avatar
Laura Mack committed
435

436
437
438
439
440
  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
441

442
}
443
444
445
446
447

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

#' Diagnostic Plots for d-gev Models
#'
Laura Mack's avatar
Laura Mack committed
448
449
#' @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
450
451
452
#' 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
453
#' @param cols optional either one value or vector of same length as \code{unique(fit$ds)} to
Laura Mack's avatar
Laura Mack committed
454
#' specify the colors of plotting points.
455
#' The default uses the \code{rainbow} function.
456
#' @param pch optional either one value or vector of same length as \code{unique(fit$ds)} containing
457
458
#' integers or symbols to specify the plotting points.
#' @param which string containing 'both', 'pp' or 'qq' to specify, which plots should be produced.
459
#' @param mfrow vector specifying layout of plots. If both plots should be produced separately,
460
461
#' set to \code{c(1,1)}.
#' @param legend logical indicating if legends should be plotted
Jana Ulrich's avatar
Jana Ulrich committed
462
463
#' @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
464
#' @param ... additional parameters passed on to the plotting function
465
466
467
468
469
470
471
#'
#' @export
#' @importFrom graphics plot abline par title
#' @importFrom grDevices rainbow
#'
#' @examples
#' data('example',package ='IDF')
Laura Mack's avatar
Laura Mack committed
472
#'
473
#' fit <- gev.d.fit(xdat=example$dat,ds = example$d,ydat=as.matrix(example[,c('cov1','cov2')])
474
#'                  ,mutl=c(1,2),sigma0l=1)
Laura Mack's avatar
Laura Mack committed
475
476
477
#' # diagnostic plots for complete data
#' gev.d.diag(fit,pch=1)
#' # diagnostic plots for subset of data (e.g. one station)
478
#' gev.d.diag(fit,subset = example$cov1==1,pch=1)
Jana Ulrich's avatar
Jana Ulrich committed
479
480
481
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',...){
482
  # check parameter:
Jana Ulrich's avatar
Jana Ulrich committed
483
  if(!is.element(which,c('both','pp','qq'))) stop("Parameter 'which'= ",which,
484
485
486
                                                 " but only 'both','pp' or 'qq' are allowed.")
  # subset data
  df <- data.frame(data=fit$data,ds=fit$ds)
487
  if(!is.null(subset)){
Laura Mack's avatar
Laura Mack committed
488
    if(dim(df)[1]!=length(subset)){stop("Length of 'subset' does not match length of data
489
490
491
                                        'xdat' used for fitting.")}
    df <- df[subset,]
  }
Jana Ulrich's avatar
Jana Ulrich committed
492
493
494
495
496
  # 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
497
  # sort data
498
  df <- df[order(df$data),]
Laura Mack's avatar
Laura Mack committed
499

500
501
502
503
504
505
506
  # 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
507
  if(is.null(cols))cols <- rainbow(length(durs))
508
  if(is.null(pch))pch <- df$cval
Laura Mack's avatar
Laura Mack committed
509

510
511
512
  if(which=='both'|which=='pp'){
    # pp
    plot(px, exp( - exp( - df$data)), xlab =
Jana Ulrich's avatar
Jana Ulrich committed
513
514
515
           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
516
    if(legend){legend('bottomright',legend = round(durs,digits = 2),pch=pch,
517
                      col = cols[1:length(durs)],title = 'Duration [h]',ncol = 2)}
518
519
520
521
  }
  if(which=='both'|which=='qq'){
    # qq
    plot( - log( - log(px)), df$data, ylab =
Jana Ulrich's avatar
Jana Ulrich committed
522
523
524
            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
525
    if(legend){legend('bottomright',legend = round(durs,digits = 2),pch=pch,
526
                      col = cols[1:length(durs)],title = 'Duration [h]',ncol = 2)}
527
528
529
530
531
532
533
534
  }
  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
535
536
#' @description function to calculate mut, sigma0, xi, theta, eta
#' (modified location, scale offset, shape, duration offset, duration exponent)
Jana Ulrich's avatar
Jana Ulrich committed
537
#' from results of \code{\link{gev.d.fit}} with covariates or link functions other than identity.
538
#' @param fit fit object returned by \code{\link{gev.d.fit}} or \code{\link{gev.fit}}
539
#' @param ydat A matrix containing the covariates in the same order as used in \code{gev.d.fit}.
540
#' @seealso \code{\link{IDF-package}}
541
#' @return data.frame containing mu_tilde, sigma0, xi, theta, eta (or mu, sigma, xi for gev.fit objects)
542
#' @export
Laura Mack's avatar
Laura Mack committed
543
#'
544
545
546
#' @examples
#' data('example',package = 'IDF')
#' fit <- gev.d.fit(example$dat,example$d,ydat = as.matrix(example[,c("cov1","cov2")])
547
#'                  ,mutl = c(1,2),sigma0l = 1)
548
549
550
#' gev.d.params(fit = fit,ydat = cbind(c(0.9,1),c(0.5,1)))


551
gev.d.params <- function(fit,ydat=NULL){
552
  if(!class(fit)%in%c("gev.d.fit","gev.fit"))stop("'fit' must be an object returned by 'gev.d.fit' or 'gev.fit'.")
553
  if(!is.null(ydat)){
Laura Mack's avatar
Laura Mack committed
554
    # check covariates matrix
555
556
557
    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.")
558
559
560
561
  }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
562

563
564
565
566
  # number of parameters
  npmu <- length(fit$model[[1]]) + 1
  npsc <- length(fit$model[[2]]) + 1
  npsh <- length(fit$model[[3]]) + 1
567
  if(class(fit)=="gev.d.fit"){
568
569
570
571
572
    if(!fit$theta_zero){
      npth <- length(fit$model[[4]]) + 1 #Including theta parameter (default)]
    }else{
      npth <- 0
    }#With no theta parameter, asked by user
573
    npet <- length(fit$model[[5]]) + 1
574
575
576
577
578
    if(!fit$tau_zero){
      npta <- length(fit$model[[6]]) + 1 #Including tau parameter (not default)]
    }else{
      npta <- 0
    }#With no tau parameter, asked by user
579
  }
Laura Mack's avatar
Laura Mack committed
580

581
  # inverse link functions
582
  if(class(fit)=="gev.d.fit"){
583
584
585
    mulink <- fit$link$mutlink$linkinv
    siglink <- fit$link$sigma0link$linkinv
    shlink <- fit$link$xilink$linkinv
586
    if(!fit$theta_zero) thetalink <- fit$link$thetalink$linkinv
587
    etalink <- fit$link$etalink$linkinv
588
    taulink <- fit$link$taulink$linkinv
589
590
591
592
593
  }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
594

595
596
597
598
  # 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))
599
600
601
  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))
602
    if(!fit$tau_zero)  {tamat <- cbind(rep(1, dim(ydat)[1]), matrix(ydat[, fit$model[[6]]],dim(ydat)[1],npta-1))}
603
  }
Laura Mack's avatar
Laura Mack committed
604

605
606
607
608
  # 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)]))
609
  if(class(fit)=="gev.d.fit"){
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
    if(!fit$theta_zero){
      theta <- thetalink(thmat %*% (fit$mle[seq(npmu + npsc + npsh + 1, length = npth)]))
    }else{
      theta <- rep(0,dim(ydat)[1])
    }
    eta <- etalink(etmat %*% (fit$mle[seq(npmu + npsc + npsh + npth + 1, length = npet)]))
    if(!fit$tau_zero){
      tau <- taulink(tamat %*% (fit$mle[seq(npmu + npsc + npsh + npth + npet + 1, length = npta)]))
    }else{
      tau <- rep(0,dim(ydat)[1])
    }
    return(data.frame(mut=mut,sigma0=sc0,xi=xi,theta=theta,eta=eta, tau=tau))
  }else{
    return(data.frame(mu=mut,sig=sc0,xi=xi))
  }
625
626
627
628
629
}


#### example data ####

630
#' Sampled data for duration-dependent GEV
631
#'
Laura Mack's avatar
Laura Mack committed
632
#' @description
633
#' Randomly sampled data set used for running the example code, containing:
634
635
636
637
#' \itemize{
#'   \item \code{$xdat}: 'annual' maxima values
#'   \item \code{$ds}: corresponding durations
#'   \item \code{$cov1}, \code{$cov2}: covariates}
638
#' d-GEV parameters used for sampling:
639
#' \itemize{
640
641
642
643
#'   \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}
644
645
#'   \item \eqn{\eta = 0.5}
#'   \item \eqn{\tau = 0}}
Laura Mack's avatar
Laura Mack committed
646
#'
647
648
649
650
651
#'
#' @docType data
#' @keywords datasets
#' @name example
#' @usage data('example',package ='IDF')
652
#' @format A data frame with 330 rows and 4 variables
653
NULL