-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathREADME.Rmd
197 lines (149 loc) · 5.4 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# tidyclust <img src="man/figures/logo.svg" align="right" height="139" />
<!-- badges: start -->
[![Codecov test coverage](https://codecov.io/gh/tidymodels/tidyclust/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidymodels/tidyclust?branch=main)
[![R-CMD-check](https://github.com/tidymodels/tidyclust/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidymodels/tidyclust/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of tidyclust is to provide a tidy, unified interface to clustering models. The packages is closely modeled after the [parsnip](https://parsnip.tidymodels.org/) package.
## Installation
You can install the released version of tidyclust from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("tidyclust")
```
and the development version of tidyclust from [GitHub](https://github.com/) with:
``` r
# install.packages("pak")
pak::pak("tidymodels/tidyclust")
```
## Example
The first thing you do is to create a `cluster specification`. For this example we are creating a K-means model, using the `stats` engine.
```{r}
library(tidyclust)
set.seed(1234)
kmeans_spec <- k_means(num_clusters = 3) %>%
set_engine("stats")
kmeans_spec
```
This specification can then be fit using data.
```{r}
kmeans_spec_fit <- kmeans_spec %>%
fit(~., data = mtcars)
kmeans_spec_fit
```
Once you have a fitted tidyclust object, you can do a number of things. `predict()` returns the cluster a new observation belongs to
```{r}
predict(kmeans_spec_fit, mtcars[1:4, ])
```
`extract_cluster_assignment()` returns the cluster assignments of the training observations
```{r}
extract_cluster_assignment(kmeans_spec_fit)
```
and `extract_centroids()` returns the locations of the clusters
```{r}
extract_centroids(kmeans_spec_fit)
```
## Visual comparison of clustering methods
Below is a visualization of the available models and how they compare using 2 dimensional toy data sets.
```{r comparison, echo=FALSE, message=FALSE, fig.asp=1/2, dpi=105, dev="svglite"}
#| fig-alt: "Mock comparison for different clustering methods for different data sets. Each row correspods to a clustering method, each column corresponds to a data set type."
library(tidymodels)
library(tidyclust)
set.seed(1234)
make_circles <- function(n) {
x <- seq(0, pi * 2, length.out = n)
x <- cos(x) * c(0.5, 1)
x <- x + rnorm(n, sd = 0.05)
y <- seq(0, pi * 2, length.out = n)
y <- sin(y) * c(0.5, 1)
y <- y + rnorm(n, sd = 0.05)
out <- data.frame(x, y)
attr(out, "name") <- "circles"
out
}
make_halves <- function(n) {
x <- seq(0, pi * 2, length.out = n)
x <- cos(x) + rep(c(0, 1), each = n / 2)
x <- x + rnorm(n, sd = 0.05)
y <- seq(0, pi * 2, length.out = n)
y <- sin(y) + rep(c(0, 0.5), each = n / 2)
y <- y + rnorm(n, sd = 0.05)
y <- y - 0.25
y <- y * (1 / 0.75)
out <- data.frame(x, y)
attr(out, "name") <- "halves"
out
}
make_uniform <- function(n) {
x <- runif(n, min = -1, max = 1)
y <- runif(n, min = -1, max = 1)
out <- data.frame(x, y)
attr(out, "name") <- "uniform"
out
}
make_blobs <- function(n) {
x <- rep(c(1, 2, 3), length.out = n)
x <- x + rnorm(n, sd = 0.5)
x <- (x - min(x)) / (max(x) - min(x)) * 2 - 1
y <- rep(c(1, 4, 2), length.out = n)
y <- y + rnorm(n, sd = 0.5)
y <- (y - min(y)) / (max(y) - min(y)) * 2 - 1
out <- data.frame(x, y)
attr(out, "name") <- "blobs"
out
}
augment_model <- function(model, data) {
model |>
fit(~., data = data) |>
augment(new_data = data) |>
mutate(
data_name = attr(data, "name"),
model_name = class(model)[1]
)
}
circle_data <- make_circles(500)
halves_data <- make_halves(500)
uniform_data <- make_uniform(500)
blobs_data <- make_blobs(500)
colors <- c("#E49E68", "#6899E4", "#E068E4")
expand_grid(
models = list(
k_means(num_clusters = 3),
hier_clust(num_clusters = 3)
),
datasets = list(
circle_data,
halves_data,
blobs_data,
uniform_data
)
) |>
pmap_dfr(~ augment_model(.x, .y)) |>
ggplot(aes(x, y, color = .pred_cluster)) +
geom_point(alpha = 0.5) +
facet_grid(model_name ~ data_name, scales = "free", switch = "y") +
scale_color_manual(values = colors) +
theme_void() +
theme(
strip.text.x = element_blank(),
strip.text.y = element_text(size = 15)
) +
guides(color = "none")
```
## Contributing
This project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.
- For questions and discussions about tidymodels packages, modeling, and machine learning, please [post on RStudio Community](https://forum.posit.co/new-topic?category_id=15&tags=tidymodels,question).
- If you think you have encountered a bug, please [submit an issue](https://github.com/tidymodels/tidyclust/issues).
- Either way, learn how to create and share a [reprex](https://reprex.tidyverse.org/articles/articles/learn-reprex.html) (a minimal, reproducible example), to clearly communicate about your code.
- Check out further details on [contributing guidelines for tidymodels packages](https://www.tidymodels.org/contribute/) and [how to get help](https://www.tidymodels.org/help/).
Footer