-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysis.R
69 lines (58 loc) · 1.98 KB
/
analysis.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
# analysis.R
# Implements statistical functions which may be run after clustering.
#
# Author: akowalew
source("k-medoids-clustering.R")
source("data-loading.R")
# Calculates variance between elements in cluster and its medoid
calcClusterVariance <- function(data, cluster, medoid) {
clusterSize <- length(cluster)
if(clusterSize == 0)
return(0)
v <- data[medoid, ]
squares <- sapply(cluster, function(elem) {
x <- data[elem, ]
square <- (x-v)*(x-v)
})
variance <- sum(squares) / clusterSize
}
# Returns a vector of variances for every cluster
calcClustersVariances <- function(data, clusters, medoids) {
clustersVariances <- sapply(1:length(clusters), function(k) {
cluster <- clusters[[k]]
medoid <- medoids[k]
clusterVariance <- calcClusterVariance(data, cluster, medoid)
})
}
# Calculates dispersion factor for final clustering result
calcClusteringDispersion <- function(data, clusters, medoids) {
clustersVariance <- calcClustersVariances(data, clusters, medoids)
variance <- calcVariance(data)
dispersion <- (sum(clustersVariance) / variance)
}
# Calculates variance for whole dataset
calcVariance <- function(data) {
xmean <- colMeans(data)
squares <- apply(data, 1, function(x) (x-xmean)*(x-xmean))
variance <- sum(squares) / nrow(data)
}
# Runs kMedoidsClustering algorithm and adds some statistics to result
doClustering <- function(data, nclusters, metric) {
result <- kMedoidsClustering(data, nclusters, metric)
clusters <- result$clusters
medoids <- result$medoids
# perform statistical analysis
clustersVariances <- calcClustersVariances(data, clusters, medoids)
clustersStdDevs <- sqrt(clustersVariances)
variance <- calcVariance(data)
dispersion <- calcClusteringDispersion(data, clusters, medoids)
stdDev <- sqrt(variance)
xmean <- colMeans(data)
result$clustersVariances <- clustersVariances
result$clustersStdDevs <- clustersStdDevs
result$variance <- variance
result$dispersion <- dispersion
result$stdDev <- stdDev
result$xmean <- xmean
return(result)
}