Turn a dbscan clustering object into a tidy tibble
Source:R/broom-dbscan-tidiers.R
dbscan_tidiers.RdProvides tidy(), augment(), and
glance() verbs for clusterings created with algorithms
in package dbscan to work with tidymodels.
Usage
tidy(x, ...)
# S3 method for class 'dbscan'
tidy(x, ...)
# S3 method for class 'hdbscan'
tidy(x, ...)
# S3 method for class 'general_clustering'
tidy(x, ...)
augment(x, ...)
# S3 method for class 'dbscan'
augment(x, data = NULL, newdata = NULL, ...)
# S3 method for class 'hdbscan'
augment(x, data = NULL, newdata = NULL, ...)
# S3 method for class 'general_clustering'
augment(x, data = NULL, newdata = NULL, ...)
glance(x, ...)
# S3 method for class 'dbscan'
glance(x, ...)
# S3 method for class 'hdbscan'
glance(x, ...)
# S3 method for class 'general_clustering'
glance(x, ...)Arguments
- x
A clustering object returned by
dbscan(),hdbscan(),jpclust()orsNNclust().- ...
further arguments are ignored without a warning.
- data
The data used to create the clustering.
- newdata
New data to predict cluster labels for.
Value
A tibble (see Details for the columns).
Details
Methods are available for the clustering objects created by dbscan(),
hdbscan(), jpclust() and sNNclust(). Clusterings created by optics()
are not supported.
All three verbs return a tibble. Cluster 0 represents noise points. Noise points are not assigned to a cluster.
tidy()
Returns one row per cluster, including the noise cluster 0, with the columns:
cluster: the cluster label as a factor (the levels are0to the number of clusters).size: the number of points in the cluster.noise:TRUEfor cluster 0 (the noise points).cluster_score(only forhdbscan()): the stability score of the cluster (see the componentcluster_scoresinhdbscan()). It isNAfor the noise cluster.
augment()
Returns the data (or newdata) as a tibble with additional columns. The
original data used to create the clustering always needs to be passed as
data because the clustering object does not store it.
If newdata is specified, then the cluster labels of the new points are
predicted with predict(). Otherwise, the labels in the clustering are
used. The added columns are:
.cluster: the cluster label as a factor.noise(only fordbscan()):TRUEif the point is a noise point..coredist,.membership_proband.outlier_scores(only forhdbscan()): the core distance, the cluster membership probability and the GLOSH outlier score of each point (seehdbscan()). They areNAfor points innewdata.
Augmenting newdata is not supported for jpclust() and sNNclust().
Examples
data(iris)
x <- scale(iris[, 1:4])
## dbscan
db <- dbscan(x, eps = .9, minPts = 5)
db
#> DBSCAN clustering for 150 objects.
#> Parameters: eps = 0.9, minPts = 5
#> Using euclidean distances and borderpoints = TRUE
#> The clustering contains 2 cluster(s) and 4 noise points.
#>
#> 0 1 2
#> 4 49 97
#>
#> Available fields: cluster, eps, minPts, metric, borderPoints
# summarize model fit with tidiers
tidy(db)
#> # A tibble: 3 × 3
#> cluster size noise
#> <fct> <int> <lgl>
#> 1 0 4 TRUE
#> 2 1 49 FALSE
#> 3 2 97 FALSE
glance(db)
#> # A tibble: 1 × 3
#> nobs n.clusters nexcluded
#> <int> <int> <int>
#> 1 150 2 4
# augment for this model needs the original data
augment(db, x)
#> # A tibble: 150 × 6
#> Sepal.Length Sepal.Width Petal.Length Petal.Width .cluster noise
#> <dbl> <dbl> <dbl> <dbl> <fct> <lgl>
#> 1 -0.898 1.02 -1.34 -1.31 1 FALSE
#> 2 -1.14 -0.132 -1.34 -1.31 1 FALSE
#> 3 -1.38 0.327 -1.39 -1.31 1 FALSE
#> 4 -1.50 0.0979 -1.28 -1.31 1 FALSE
#> 5 -1.02 1.25 -1.34 -1.31 1 FALSE
#> 6 -0.535 1.93 -1.17 -1.05 1 FALSE
#> 7 -1.50 0.786 -1.34 -1.18 1 FALSE
#> 8 -1.02 0.786 -1.28 -1.31 1 FALSE
#> 9 -1.74 -0.361 -1.34 -1.31 1 FALSE
#> 10 -1.14 0.0979 -1.28 -1.44 1 FALSE
#> # ℹ 140 more rows
# to augment new data, the original data is also needed
augment(db, x, newdata = x[1:5, ])
#> # A tibble: 5 × 6
#> Sepal.Length Sepal.Width Petal.Length Petal.Width .cluster noise
#> <dbl> <dbl> <dbl> <dbl> <fct> <lgl>
#> 1 -0.898 1.02 -1.34 -1.31 1 FALSE
#> 2 -1.14 -0.132 -1.34 -1.31 1 FALSE
#> 3 -1.38 0.327 -1.39 -1.31 1 FALSE
#> 4 -1.50 0.0979 -1.28 -1.31 1 FALSE
#> 5 -1.02 1.25 -1.34 -1.31 1 FALSE
## hdbscan
hdb <- hdbscan(x, minPts = 5)
# summarize model fit with tidiers
tidy(hdb)
#> # A tibble: 3 × 4
#> cluster size cluster_score noise
#> <fct> <int> <dbl> <lgl>
#> 1 0 2 NA TRUE
#> 2 1 98 118. FALSE
#> 3 2 50 87.4 FALSE
glance(hdb)
#> # A tibble: 1 × 3
#> nobs n.clusters nexcluded
#> <int> <int> <int>
#> 1 150 2 2
# augment for this model needs the original data
augment(hdb, x)
#> # A tibble: 150 × 8
#> Sepal.Length Sepal.Width Petal.Length Petal.Width .cluster .coredist
#> <dbl> <dbl> <dbl> <dbl> <fct> <dbl>
#> 1 -0.898 1.02 -1.34 -1.31 2 0.236
#> 2 -1.14 -0.132 -1.34 -1.31 2 0.236
#> 3 -1.38 0.327 -1.39 -1.31 2 0.310
#> 4 -1.50 0.0979 -1.28 -1.31 2 0.283
#> 5 -1.02 1.25 -1.34 -1.31 2 0.291
#> 6 -0.535 1.93 -1.17 -1.05 2 0.463
#> 7 -1.50 0.786 -1.34 -1.18 2 0.496
#> 8 -1.02 0.786 -1.28 -1.31 2 0.248
#> 9 -1.74 -0.361 -1.34 -1.31 2 0.551
#> 10 -1.14 0.0979 -1.28 -1.44 2 0.270
#> # ℹ 140 more rows
#> # ℹ 2 more variables: .membership_prob <dbl>, .outlier_scores <dbl>
# to augment new data, the original data is also needed
augment(hdb, x, newdata = x[1:5, ])
#> # A tibble: 5 × 8
#> Sepal.Length Sepal.Width Petal.Length Petal.Width .cluster .coredist
#> <dbl> <dbl> <dbl> <dbl> <fct> <dbl>
#> 1 -0.898 1.02 -1.34 -1.31 2 NA
#> 2 -1.14 -0.132 -1.34 -1.31 2 NA
#> 3 -1.38 0.327 -1.39 -1.31 2 NA
#> 4 -1.50 0.0979 -1.28 -1.31 2 NA
#> 5 -1.02 1.25 -1.34 -1.31 2 NA
#> # ℹ 2 more variables: .membership_prob <dbl>, .outlier_scores <dbl>
## Jarvis-Patrick clustering
cl <- jpclust(x, k = 20, kt = 15)
# summarize model fit with tidiers
tidy(cl)
#> # A tibble: 7 × 3
#> cluster size noise
#> <fct> <int> <lgl>
#> 1 0 0 TRUE
#> 2 1 49 FALSE
#> 3 2 1 FALSE
#> 4 3 13 FALSE
#> 5 4 36 FALSE
#> 6 5 2 FALSE
#> 7 6 49 FALSE
glance(cl)
#> # A tibble: 1 × 3
#> nobs n.clusters nexcluded
#> <int> <int> <int>
#> 1 150 6 0
# augment for this model needs the original data
augment(cl, x)
#> # A tibble: 150 × 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width .cluster
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 -0.898 1.02 -1.34 -1.31 1
#> 2 -1.14 -0.132 -1.34 -1.31 1
#> 3 -1.38 0.327 -1.39 -1.31 1
#> 4 -1.50 0.0979 -1.28 -1.31 1
#> 5 -1.02 1.25 -1.34 -1.31 1
#> 6 -0.535 1.93 -1.17 -1.05 1
#> 7 -1.50 0.786 -1.34 -1.18 1
#> 8 -1.02 0.786 -1.28 -1.31 1
#> 9 -1.74 -0.361 -1.34 -1.31 1
#> 10 -1.14 0.0979 -1.28 -1.44 1
#> # ℹ 140 more rows
## Shared Nearest Neighbor clustering
cl <- sNNclust(x, k = 20, eps = 0.8, minPts = 15)
# summarize model fit with tidiers
tidy(cl)
#> # A tibble: 3 × 3
#> cluster size noise
#> <fct> <int> <lgl>
#> 1 0 10 TRUE
#> 2 1 45 FALSE
#> 3 2 95 FALSE
glance(cl)
#> # A tibble: 1 × 3
#> nobs n.clusters nexcluded
#> <int> <int> <int>
#> 1 150 2 10
# augment for this model needs the original data
augment(cl, x)
#> # A tibble: 150 × 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width .cluster
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 -0.898 1.02 -1.34 -1.31 1
#> 2 -1.14 -0.132 -1.34 -1.31 1
#> 3 -1.38 0.327 -1.39 -1.31 1
#> 4 -1.50 0.0979 -1.28 -1.31 1
#> 5 -1.02 1.25 -1.34 -1.31 1
#> 6 -0.535 1.93 -1.17 -1.05 1
#> 7 -1.50 0.786 -1.34 -1.18 1
#> 8 -1.02 0.786 -1.28 -1.31 1
#> 9 -1.74 -0.361 -1.34 -1.31 1
#> 10 -1.14 0.0979 -1.28 -1.44 1
#> # ℹ 140 more rows