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
|
# 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
################################################################################
# FUNCTION: DESCRIPTION:
# assetsDendrogramPlot Displays hierarchical clustering dendrogram
# assetsCorEigenPlot Displays ratio of the largest two eigenvalues
################################################################################
assetsDendrogramPlot <-
function(x, labels = TRUE, title = TRUE, box = TRUE,
method = c(dist = "euclidian", clust = "complete"), ...)
{
# A function implemented by Diethelm Wuertz
# Description:
# Displays hierarchical clustering dendrogram
# FUNCTION:
# Compute Distance Matrix:
if (class(x) == "dist") {
DIST = x
} else {
X = t(series(x))
DIST = dist(X, method[1])
}
# Hierarchical Clustering:
ans = hclust(DIST, method = method[2])
# Plot Dendrogram:
if (labels) {
plot(ans, xlab = "", main = "", sub = "", ...)
mtext(paste(
"Distance Method:", method[1], " | ",
"Clustering Method:", method[2]),
side = 4, line = 0.1, adj = 0, col = "darkgrey")
} else {
plot(ans, ann = FALSE, ...)
}
# Add Box:
if (box) {
box()
}
# Add Optional Title:
if (title) {
title(main = "Dendrogram", sub = "", xlab = "", ylab = "Heights")
}
# Return Value:
invisible(list(dist = DIST, hclust = ans))
}
# ------------------------------------------------------------------------------
assetsCorEigenPlot <-
function(x, labels = TRUE, title = TRUE, box = TRUE,
method = c("pearson", "kendall", "spearman"), ...)
{
# A function implemented by Diethelm Wuertz
# Description:
# Displays ratio of the largest two eigenvalues
# Arguments:
# x - a timeSeries object or any other rectangular object
# which can be transformed by the function as. matrix
# into a numeric matrix.
# Example:
# assetsCorEigenPlot(x=100*as.timeSeries(data(LPP2005REC)))
# FUNCTION:
# Settings:
stopifnot(is.timeSeries(x))
x = series(x)
method = match.arg(method)
# Plot:
x.cor = cor(x, use = 'pair', method = method)
x.eig = eigen(x.cor)$vectors[, 1:2]
e1 = x.eig[, 1]
e2 = x.eig[, 2]
plot(e1, e2, col = 'white', ann = FALSE,
xlim = range(e1, e2), ylim = range(e1, e2), ...)
abline(h = 0, lty = 3, col = "grey")
abline(v = 0, lty = 3, col = "grey")
arrows(0, 0, e1, e2, cex = 0.5, col = "steelblue", length = 0.1)
text(e1, e2, rownames(x.cor), ...)
# Labels:
if (labels) {
mtext(method, side = 4, adj = 0, cex = 0.7, col = "grey")
}
# Add Box:
if (box) {
box()
}
# Add Title:
if(title) {
title(main = "Eigenvalue Ratio Plot", sub = "",
xlab = "Eigenvalue 1", ylab = "Eigenvalue 2")
}
# Return Value:
invisible()
}
################################################################################
|