forked from cran/Ckmeans.1d.dp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53beebe
commit 5eae0fb
Showing
10 changed files
with
244 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
Package: Ckmeans.1d.dp | ||
Type: Package | ||
Version: 3.4.6-2 | ||
Date: 2016-09-25 | ||
Version: 3.4.6-3 | ||
Date: 2016-10-04 | ||
Title: Optimal and Fast Univariate k-Means Clustering | ||
Authors@R: c(person("Joe", "Song", role = c("aut", "cre"), | ||
email = "joemsong@cs.nmsu.edu"), | ||
person("Haizhou", "Wang", role = "aut")) | ||
Author: Joe Song [aut, cre], Haizhou Wang [aut] | ||
Maintainer: Joe Song <joemsong@cs.nmsu.edu> | ||
Description: A dynamic programming algorithm for optimal one-dimensional | ||
k-means clustering. The algorithm minimizes the sum of squares of | ||
within-cluster distances. As an alternative to heuristic k-means algorithms, | ||
this method guarantees optimality and reproducibility. Its advantage in | ||
efficiency and accuracy over k-means is increasingly pronounced as the | ||
number of clusters k increases. | ||
Description: A dynamic programming algorithm for optimal univariate k-means | ||
clustering. Minimizing the sum of squares of within-cluster distances, the | ||
algorithm guarantees optimality and reproducibility. Its advantage over | ||
heuristic k-means algorithms in efficiency and accuracy is increasingly | ||
pronounced as the number of clusters k increases. It provides an alternative | ||
to heuristic k-means algorithms for univariate clustering. | ||
License: LGPL (>= 3) | ||
NeedsCompilation: yes | ||
Suggests: testthat | ||
Depends: R (>= 2.10.0) | ||
LazyData: true | ||
Packaged: 2016-09-25 19:50:13 UTC; joemsong | ||
Packaged: 2016-10-05 03:19:39 UTC; joemsong | ||
Repository: CRAN | ||
Date/Publication: 2016-09-26 00:51:35 | ||
Date/Publication: 2016-10-05 09:29:38 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# visualize.R -- visualization functions for Ckmeans.1d.dp | ||
# | ||
# Joe Song | ||
# Created: Oct 1, 2016 | ||
|
||
ahist <- function( | ||
x, k = c(1,9), plot = TRUE, xlab = deparse(substitute(x)), | ||
main = paste("Adaptive histogram of", deparse(substitute(x))), | ||
col = NULL, lwd = graphics::par("lwd"), col.stick = "gray", lwd.stick = 1, | ||
...) | ||
# adaptive histogram | ||
{ | ||
xs <- sort(x) | ||
r <- Ckmeans.1d.dp(xs, k=k) | ||
kopt <- length(r$size) | ||
breaks <- vector("double", length=kopt+1) | ||
i <- r$size[1] | ||
|
||
if(kopt > 1) { | ||
for(q in 2:kopt) { | ||
breaks[q] <- (xs[i] + xs[i+1])/2 | ||
i <- i + r$size[q] | ||
} | ||
} | ||
|
||
breaks[1] <- xs[1] | ||
breaks[kopt+1] <- xs[length(xs)] | ||
|
||
h <- graphics::hist(x, breaks=breaks, plot=FALSE, | ||
warn.unused=FALSE, ...) | ||
h$xname <- deparse(substitute(x)) | ||
|
||
if(plot) { | ||
opar <- graphics::par(lwd=lwd) | ||
graphics::plot(h, main=main, xlab=xlab, col=col, ...) | ||
if(h$equidist) { | ||
graphics::segments(x, -max(h$count)/10, x, 0, | ||
col=col.stick, lwd=lwd.stick) | ||
} else { | ||
graphics::segments(x, -max(h$density)/10, x, 0, | ||
col=col.stick, lwd=lwd.stick) | ||
} | ||
graphics::par(opar) | ||
invisible(h) | ||
} else { | ||
return(h) | ||
} | ||
} | ||
|
||
plot.Ckmeans.1d.dp <- | ||
function(x, xlab=NULL, ylab=NULL, main=NULL, | ||
sub=NULL, col.clusters=NULL, ...) | ||
{ | ||
ck <- x | ||
if(is.null(xlab)) xlab <- ck$xname | ||
if(is.null(ylab)) ylab <- ifelse(ck$yname=="1", "Weight", ck$yname) | ||
if(is.null(main)) main <- paste("Optimal k-means clustering of", ck$xname) | ||
if(is.null(sub)) sub=paste("n =", length(ck$cluster)) | ||
if(is.null(col.clusters)) col.clusters <- seq_along(x$size) | ||
|
||
if(exists(ck$xname, mode="numeric")) { | ||
x <- get(ck$xname, mode="numeric") | ||
} else { | ||
x <- eval(parse(text=ck$xname)) | ||
} | ||
|
||
if(exists(ck$yname, mode="numeric")) { | ||
y <- get(ck$yname, mode="numeric") | ||
} else { | ||
y <- eval(parse(text=ck$yname)) | ||
} | ||
|
||
if(length(y) == 1) { | ||
y <- rep(y, length(x)) | ||
} | ||
|
||
graphics::plot(x, y, type="p", | ||
xlab=xlab, ylab=ylab, main=main, sub=sub, | ||
col=col.clusters[ck$cluster], | ||
...) | ||
|
||
ks <- seq_along(ck$size) | ||
sapply(ks, function(q) { | ||
graphics::segments(x[ck$cluster == q], 0, | ||
x[ck$cluster == q], y[ck$cluster == q], | ||
col=col.clusters[q], ...) } ) | ||
|
||
invisible(ck) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.