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
|
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
# Copyrights (C)
# for this R-port:
# 1999 - 2004, Diethelm Wuertz, GPL
# Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
# info@rmetrics.org
# www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
# see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
# see Rmetrics's copyright file
################################################################################
# FUNCTION DESCRIPTION:
# findThreshold Finds threshold values
# blocks Creates data blocks on vectors and time series
# blockMaxima Calculates block maxima on vectors and time series
# deCluster Declusters a point process
################################################################################
findThreshold =
function(x, n = NA)
{ # A function implemented by Diethelm Wuertz
# Description:
# Finds upper thresold for a given number of Extremes.
# Arguments:
# n - a numeric value or vector giving number of extremes
# above the threshold. If "n" is not specified, "n"
# is set to an integer representing 5% of the data
# from the whole data set "x".
# Note:
# Imported from R-package evir/EVIS.
# FUNCTION:
# Settings:
if(is.na(n[1])) n = floor(0.05*length(x))
# Continue:
x = rev(sort(as.numeric(x)))
thresholds = unique(x)
indices = match(x[n], thresholds)
indices = pmin(indices + 1, length(thresholds))
# Return Value:
thresholds[indices]
}
# ------------------------------------------------------------------------------
blocks =
function(x, block = "month", FUN = max)
{ # A function implemented by Diethelm Wuertz
# Description:
# Creates data blocks on vectors and time series.
# Note:
# Imported from R-package evir/EVIS.
# FUNCTION:
# Settings:
data = x
# Compute:
n.all = length(data)
if(is.character(block)) {
times = as.POSIXlt(attributes(data)$times)
if(block %in% c("semester", "quarter")) {
sem = quart = times$mon
sem[sem %in% 0:5] = quart[quart %in% 0:2] = 0
sem[sem %in% 6:11] = quart[quart %in% 3:5] = 1
quart[quart %in% 6:8] = 2
quart[quart %in% 9:11] = 3 }
grouping = switch(block,
semester = paste(times$year, sem),
quarter = paste(times$year, quart),
quarters = paste(times$year, quart),
month = paste(times$year, times$mon),
months = paste(times$year, times$mon),
year = times$year,
years = times$year,
stop("unknown time period"))
newdata = tapply(data, grouping, FUN=FUN) }
else {
data = as.numeric(data)
nblocks = (length(data) %/% block) + 1
grouping = rep(1:nblocks, rep(block, nblocks))[1:length(data)]
newdata = tapply(data, grouping, FUN=FUN)}
# Return Value:
result = newdata
result
}
# -----------------------------------------------------------------------------
blockMaxima =
function(x, block = "month", details = FALSE, doplot = TRUE, ...)
{ # A function implemented by Diethelm Wuertz
# Description:
# Calculates block maxima on vectors and time series.
# Arguments:
# x - may be alternatively as.vector or as.ts
# block - as.numeric: length of a block
# as.character: year | semester | quarter | month
# Note:
# Calls McNeils Splus function blocks()
# Output data as vector of transposed
# result to get proper order of data!
# FUNCTION:
# Settings
x = blocks(x, block)
# Plot:
if (doplot) {
plot(as.vector(x), type="h", ylab = "Block Maxima", ...)
title(main = paste(block, "- Block Maxima"))
grid() }
# Details:
# if details == FALSE a vector is returned, i.e details are removed!
if (!details) x = as.vector(x[is.na(x) == FALSE])
# Return Value:
x
}
# -----------------------------------------------------------------------------
deCluster =
function(x, run = NA, doplot = TRUE)
{ # A function implemented by Diethelm Wuertz
# Description:
# Declusters a point process
# Note:
# Imported from R-package evir/EVIS.
# FUNCTION:
# Settings:
labels = TRUE
# Imported Function:
series = x
picture = doplot
n = length(as.numeric(series))
times = attributes(series)$times
if (is.null(times))
stop("`series' must have a `times' attribute")
as.posix = is.character(times) || inherits(times, "POSIXt") ||
inherits(times, "date") || inherits(times, "dates")
if (as.posix)
gaps = as.numeric(difftime(as.POSIXlt(times)[2:n],
as.POSIXlt(times)[1:(n - 1)], units = "days"))
else gaps = as.numeric(diff(times))
longgaps = gaps > run
if (sum(longgaps) <= 1)
stop("Decluster parameter too large")
cluster = c(0, cumsum(longgaps))
cmax = tapply(as.numeric(series), cluster, max)
newtimes = times[match(cmax, series)]
newseries = structure(series[match(cmax, series)], times = newtimes)
n = length(as.numeric(newseries))
if (as.posix) {
newgaps = as.numeric(difftime(as.POSIXlt(newtimes)[2:n],
as.POSIXlt(newtimes)[1:(n - 1)], units = "days"))
times = as.POSIXlt(times)
newtimes = as.POSIXlt(newtimes) }
else {
newgaps = as.numeric(diff(newtimes)) }
# Plot:
if (doplot) {
# cat("Declustering picture...\n")
# cat(paste("Data reduced from", length(as.numeric(series)),
# "to", length(as.numeric(newseries)), "\n"))
# par(mfrow = c(2, 2))
if (labels) {
main = "de-Clustering"
plot(times, series, type = "h", main = main)
qPlot(gaps)
plot(newtimes, newseries, type = "h", main = main)
qPlot(newgaps) }
}
# Result:
ans = newseries
# Return Value:
ans
}
# ******************************************************************************
|