| Title: | A Unified Tidy Interface to R's Machine Learning Ecosystem |
|---|---|
| Description: | Provides a unified tidyverse-compatible interface to R's machine learning ecosystem - from data ingestion to model publishing. The tl_read() family reads data from files ('CSV', 'Excel', 'Parquet', 'JSON'), databases ('SQLite', 'PostgreSQL', 'MySQL', 'BigQuery'), and cloud sources ('S3', 'GitHub', 'Kaggle'). The tl_model() function wraps established implementations from 'glmnet', 'randomForest', 'xgboost', 'e1071', 'rpart', 'gbm', 'nnet', 'cluster', 'dbscan', and others with consistent function signatures and tidy tibble output. Results flow into unified 'ggplot2'-based visualization and optional formatted 'gt' tables via the tl_table() family. The underlying algorithms are unchanged; 'tidylearn' simply makes them easier to use together. Access raw model objects via the $fit slot for a supervised method, or $fit$model for an unsupervised one. Methods include random forests Breiman (2001) <doi:10.1023/A:1010933404324>, LASSO regression Tibshirani (1996) <doi:10.1111/j.2517-6161.1996.tb02080.x>, elastic net Zou and Hastie (2005) <doi:10.1111/j.1467-9868.2005.00503.x>, support vector machines Cortes and Vapnik (1995) <doi:10.1007/BF00994018>, and gradient boosting Friedman (2001) <doi:10.1214/aos/1013203451>. |
| Authors: | Cesaire Tobias [aut, cre] |
| Maintainer: | Cesaire Tobias <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.7.0.9000 |
| Built: | 2026-11-02 21:30:03 UTC |
| Source: | https://github.com/ces0491/tidylearn |
Augment Data with DBSCAN Cluster Assignments
augment_dbscan(dbscan_obj, data)augment_dbscan(dbscan_obj, data)
dbscan_obj |
A tidy_dbscan object |
data |
Original data frame |
A tibble containing the original data with additional columns
cluster (factor), is_noise (logical), and is_core
(logical).
db <- tidy_dbscan(iris[, 1:4], eps = 0.5, minPts = 5) augmented <- augment_dbscan(db, iris)db <- tidy_dbscan(iris[, 1:4], eps = 0.5, minPts = 5) augmented <- augment_dbscan(db, iris)
Add cluster assignments to original data
augment_hclust(hclust_obj, data, k = NULL, h = NULL)augment_hclust(hclust_obj, data, k = NULL, h = NULL)
hclust_obj |
A tidy_hclust object |
data |
Original data frame |
k |
Number of clusters (optional) |
h |
Height at which to cut (optional) |
A tibble containing the original data with an additional
cluster integer column indicating cluster assignments.
hc <- tidy_hclust(USArrests, method = "ward.D2") augmented <- augment_hclust(hc, USArrests, k = 3)hc <- tidy_hclust(USArrests, method = "ward.D2") augmented <- augment_hclust(hc, USArrests, k = 3)
Augment Data with K-Means Cluster Assignments
augment_kmeans(kmeans_obj, data)augment_kmeans(kmeans_obj, data)
kmeans_obj |
A tidy_kmeans object |
data |
Original data frame |
A tibble containing the original data with an additional
cluster factor column indicating cluster assignments.
km <- tidy_kmeans(iris[, 1:4], k = 3) augmented <- augment_kmeans(km, iris)km <- tidy_kmeans(iris[, 1:4], k = 3) augmented <- augment_kmeans(km, iris)
Augment Data with PAM Cluster Assignments
augment_pam(pam_obj, data)augment_pam(pam_obj, data)
pam_obj |
A tidy_pam object |
data |
Original data frame |
A tibble containing the original data with an additional
cluster factor column indicating cluster assignments.
pm <- tidy_pam(iris[, 1:4], k = 3) augmented <- augment_pam(pm, iris)pm <- tidy_pam(iris[, 1:4], k = 3) augmented <- augment_pam(pm, iris)
Add PC scores to the original dataset
augment_pca(pca_obj, data, n_components = NULL)augment_pca(pca_obj, data, n_components = NULL)
pca_obj |
A tidy_pca object |
data |
Original data frame |
n_components |
Number of PCs to add (default: all) |
A tibble containing the original data with additional columns
for each principal component score (named PC1, PC2, etc.).
pca <- tidy_pca(USArrests) augmented <- augment_pca(pca, USArrests, n_components = 2)pca <- tidy_pca(USArrests) augmented <- augment_pca(pca, USArrests, n_components = 2)
Validation metrics for a clustering result
calc_validation_metrics(clusters, data = NULL, dist_mat = NULL)calc_validation_metrics(clusters, data = NULL, dist_mat = NULL)
clusters |
Vector of cluster assignments: numeric, factor or
character. A label of 0 marks noise, as |
data |
Original data frame (for WSS calculation). WSS is taken over its numeric columns, so it needs at least one. |
dist_mat |
Distance matrix (for silhouette) |
A single-row tibble with columns k, min_size,
max_size, avg_size, n_noise, and optionally
avg_silhouette, min_silhouette (if dist_mat
provided; NA for a single cluster), and total_wss (if
data provided).
km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) metrics <- calc_validation_metrics(km$cluster, iris[, 1:4], d)km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) metrics <- calc_validation_metrics(km$cluster, iris[, 1:4], d)
Used for elbow method to determine optimal k
calc_wss(data, max_k = 10, nstart = 25)calc_wss(data, max_k = 10, nstart = 25)
data |
A data frame or tibble |
max_k |
Maximum number of clusters to test (default: 10) |
nstart |
Number of random starts for each k (default: 25) |
A tibble with columns k (number of clusters) and
tot_withinss (total within-cluster sum of squares).
wss <- calc_wss(iris[, 1:4], max_k = 6) plot(wss$k, wss$tot_withinss, type = "b")wss <- calc_wss(iris[, 1:4], max_k = 6) plot(wss$k, wss$tot_withinss, type = "b")
Compare Multiple Clustering Results
compare_clusterings(cluster_list, data, dist_mat = NULL)compare_clusterings(cluster_list, data, dist_mat = NULL)
cluster_list |
Named list of cluster assignment vectors. An entry
without a name is reported as |
data |
Original data |
dist_mat |
Distance matrix |
A tibble with one row per clustering method and columns for each
validation metric (see calc_validation_metrics), plus a
method column identifying the clustering.
km3 <- kmeans(iris[, 1:4], 3, nstart = 25)$cluster km4 <- kmeans(iris[, 1:4], 4, nstart = 25)$cluster compare_clusterings(list(k3 = km3, k4 = km4), iris[, 1:4])km3 <- kmeans(iris[, 1:4], 3, nstart = 25)$cluster km4 <- kmeans(iris[, 1:4], 4, nstart = 25)$cluster compare_clusterings(list(k3 = km3, k4 = km4), iris[, 1:4])
Compute distances using multiple methods for comparison
compare_distances(data, methods = c("euclidean", "manhattan", "maximum"))compare_distances(data, methods = c("euclidean", "manhattan", "maximum"))
data |
A data frame or tibble |
methods |
Character vector of methods to compare |
A named list of dist objects, one per method.
dists <- compare_distances( iris[, 1:4], methods = c("euclidean", "manhattan") )dists <- compare_distances( iris[, 1:4], methods = c("euclidean", "manhattan") )
Generate a multi-panel summary of clustering results
create_cluster_dashboard( data, cluster_col = "cluster", validation_metrics = NULL )create_cluster_dashboard( data, cluster_col = "cluster", validation_metrics = NULL )
data |
Data frame with cluster assignments |
cluster_col |
Cluster column name |
validation_metrics |
Optional tibble of validation metrics |
Invisibly returns a named list of the
ggplot objects drawn: clusters, the
scatter plot, when the data has two numeric columns besides
cluster_col; sizes; and metrics, when
validation_metrics is given. The combined plot grid is drawn as
a side effect via grid.arrange.
df <- iris[, 1:4] df$cluster <- kmeans(df, 3)$cluster create_cluster_dashboard(df)df <- iris[, 1:4] df$cluster <- kmeans(df, 3)$cluster create_cluster_dashboard(df)
Test multiple eps and minPts combinations
explore_dbscan_params(data, eps_values, minPts_values)explore_dbscan_params(data, eps_values, minPts_values)
data |
A data frame or matrix |
eps_values |
Vector of eps values to test |
minPts_values |
Vector of minPts values to test |
A tibble with columns eps, minPts, n_clusters,
n_noise, and prop_noise for each parameter combination.
params <- explore_dbscan_params(iris[, 1:4], eps_values = c(0.3, 0.5, 0.8), minPts_values = c(3, 5))params <- explore_dbscan_params(iris[, 1:4], eps_values = c(0.3, 0.5, 0.8), minPts_values = c(3, 5))
Subset rules containing specific items
filter_rules_by_item(rules_obj, item, where = "both")filter_rules_by_item(rules_obj, item, where = "both")
rules_obj |
A tidy_apriori object, an arules rules object, or a
tibble of rules from |
item |
Character; one item name, matched against whole items, so "coffee" does not match "instant coffee" |
where |
Character; "lhs", "rhs", or "both" (default: "both") |
A tibble of rules containing the specified item in the
requested position.
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) filter_rules_by_item(res, "whole milk", where = "rhs") }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) filter_rules_by_item(res, "whole milk", where = "rhs") }
Get PCA Loadings in Wide Format
get_pca_loadings(pca_obj, n_components = NULL)get_pca_loadings(pca_obj, n_components = NULL)
pca_obj |
A |
n_components |
Number of components to include (default: all) |
A tibble with one row per variable and one column per principal component, containing the loading values.
pca <- tidy_pca(USArrests) get_pca_loadings(pca, n_components = 2)pca <- tidy_pca(USArrests) get_pca_loadings(pca, n_components = 2)
Get Variance Explained Summary
get_pca_variance(pca_obj)get_pca_variance(pca_obj)
pca_obj |
A |
A tibble with columns component, sdev,
variance, prop_variance, and cum_variance.
pca <- tidy_pca(USArrests) get_pca_variance(pca) # The same accessor works on a tl_model() PCA fit get_pca_variance(tl_model(USArrests, method = "pca"))pca <- tidy_pca(USArrests) get_pca_variance(pca) # The same accessor works on a tl_model() PCA fit get_pca_variance(tl_model(USArrests, method = "pca"))
View rules sorted by various quality measures
inspect_rules(rules_obj, by = "lift", n = 10, decreasing = TRUE)inspect_rules(rules_obj, by = "lift", n = 10, decreasing = TRUE)
rules_obj |
A tidy_apriori object, an arules rules or itemsets object, or a tibble of rules |
by |
Sort by: "support", "confidence", "lift" (default), "count". Itemsets have no lift, so for them the default sorts by support. |
n |
Number of rules to display (default: 10) |
decreasing |
If TRUE (default), the |
A tibble of the n rules ranked highest (or, with
decreasing = FALSE, lowest) by the quality measure by.
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) inspect_rules(res, by = "lift", n = 5) }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) inspect_rules(res, by = "lift", n = 5) }
Use multiple methods to suggest optimal k
optimal_clusters(data, max_k = 10, methods = c("silhouette", "gap", "wss"))optimal_clusters(data, max_k = 10, methods = c("silhouette", "gap", "wss"))
data |
A data frame or tibble |
max_k |
Maximum k to test (default: 10) |
methods |
Vector of methods: "silhouette", "gap", "wss" (default: all) |
A list of class "optimal_k_results" containing one or more of:
wss: tibble from calc_wss (if "wss" method used)
silhouette: tibble from tidy_silhouette_analysis
(if "silhouette" method used)
gap: a tidy_gap object from tidy_gap_stat
(if "gap" method used)
opt <- optimal_clusters(iris[, 1:4], max_k = 6, methods = "wss")opt <- optimal_clusters(iris[, 1:4], max_k = 6, methods = "wss")
Use silhouette or gap statistic to find optimal k
optimal_hclust_k(hclust_obj, method = "silhouette", max_k = 10)optimal_hclust_k(hclust_obj, method = "silhouette", max_k = 10)
hclust_obj |
A tidy_hclust object |
method |
Character; "silhouette" (default) or "gap". The gap
statistic resamples the observations' numeric columns, so it refuses a
tree built from a dist object, and one built with
|
max_k |
Maximum number of clusters to test (default: 10) |
A list containing:
optimal_k: the recommended number of clusters
method: the evaluation method used
values: numeric vector of evaluation scores (for silhouette)
k_range: integer vector of k values tested (for silhouette)
If method = "gap", returns a tidy_gap object instead.
hc <- tidy_hclust(USArrests, method = "ward.D2") opt <- optimal_hclust_k(hc, method = "silhouette", max_k = 6)hc <- tidy_hclust(USArrests, method = "ward.D2") opt <- optimal_hclust_k(hc, method = "silhouette", max_k = 6)
Compare multiple clustering results side-by-side
plot_cluster_comparison(data, cluster_cols, x_col, y_col)plot_cluster_comparison(data, cluster_cols, x_col, y_col)
data |
Data frame with multiple cluster columns |
cluster_cols |
Vector of cluster column names |
x_col |
X-axis variable |
y_col |
Y-axis variable |
The return value of grid.arrange, a
gtable drawn as a side effect.
df <- iris[, 1:4] df$km3 <- kmeans(df, 3)$cluster df$km4 <- kmeans(df, 4)$cluster plot_cluster_comparison(df, c("km3", "km4"), "Sepal.Length", "Sepal.Width")df <- iris[, 1:4] df$km3 <- kmeans(df, 3)$cluster df$km4 <- kmeans(df, 4)$cluster plot_cluster_comparison(df, c("km3", "km4"), "Sepal.Length", "Sepal.Width")
Create bar plot of cluster sizes
plot_cluster_sizes(clusters, title = "Cluster Size Distribution")plot_cluster_sizes(clusters, title = "Cluster Size Distribution")
clusters |
Vector of cluster assignments |
title |
Plot title (default: "Cluster Size Distribution") |
A ggplot object.
clusters <- kmeans(iris[, 1:4], 3)$cluster plot_cluster_sizes(clusters)clusters <- kmeans(iris[, 1:4], 3)$cluster plot_cluster_sizes(clusters)
Visualize clustering results using first two dimensions or specified dimensions
plot_clusters( data, cluster_col = "cluster", x_col = NULL, y_col = NULL, centers = NULL, title = "Cluster Plot", color_noise_black = TRUE )plot_clusters( data, cluster_col = "cluster", x_col = NULL, y_col = NULL, centers = NULL, title = "Cluster Plot", color_noise_black = TRUE )
data |
A data frame with cluster assignments |
cluster_col |
Name of cluster column (default: "cluster") |
x_col |
X-axis variable (if NULL, uses the first numeric column
other than |
y_col |
Y-axis variable (if NULL, uses the second numeric column
other than |
centers |
Optional data frame of cluster centers |
title |
Plot title |
color_noise_black |
If TRUE, color noise points (cluster 0) black |
A ggplot object.
km <- tidy_kmeans(iris[, 1:4], k = 3) clustered <- augment_kmeans(km, iris[, 1:4]) plot_clusters(clustered)km <- tidy_kmeans(iris[, 1:4], k = 3) clustered <- augment_kmeans(km, iris[, 1:4]) plot_clusters(clustered)
Enhanced dendrogram with colored cluster rectangles
plot_dendrogram( hclust_obj, k = NULL, title = "Hierarchical Clustering Dendrogram" )plot_dendrogram( hclust_obj, k = NULL, title = "Hierarchical Clustering Dendrogram" )
hclust_obj |
Hierarchical clustering object: an |
k |
Number of clusters to highlight |
title |
Plot title |
Invisibly returns the hclust object. The
dendrogram is drawn as a side effect.
hc <- hclust(dist(iris[, 1:4])) plot_dendrogram(hc, k = 3)hc <- hclust(dist(iris[, 1:4])) plot_dendrogram(hc, k = 3)
Visualize distance matrix as heatmap
plot_distance_heatmap( dist_mat, cluster_order = NULL, title = "Distance Heatmap" )plot_distance_heatmap( dist_mat, cluster_order = NULL, title = "Distance Heatmap" )
dist_mat |
Distance matrix (dist object) |
cluster_order |
Optional vector to reorder observations by cluster |
title |
Plot title |
A ggplot object.
d <- dist(iris[1:20, 1:4]) plot_distance_heatmap(d)d <- dist(iris[1:20, 1:4]) plot_distance_heatmap(d)
Plot total within-cluster sum of squares vs number of clusters
plot_elbow(wss_data, add_line = FALSE, suggested_k = NULL)plot_elbow(wss_data, add_line = FALSE, suggested_k = NULL)
wss_data |
A tibble with columns k and tot_withinss (from calc_wss) |
add_line |
Add vertical line at suggested optimal k? (default: FALSE) |
suggested_k |
If add_line=TRUE, which k to highlight |
A ggplot object.
wss <- data.frame(k = 2:6, tot_withinss = c(150, 90, 60, 50, 45)) plot_elbow(wss)wss <- data.frame(k = 2:6, tot_withinss = c(150, 90, 60, 50, 45)) plot_elbow(wss)
Plot Gap Statistic
plot_gap_stat(gap_obj, show_methods = FALSE)plot_gap_stat(gap_obj, show_methods = FALSE)
gap_obj |
A tidy_gap object |
show_methods |
Logical; show all three k selection methods? (default: FALSE) |
A ggplot object.
gap <- tidy_gap_stat(iris[, 1:4], max_k = 6, B = 10) plot_gap_stat(gap)gap <- tidy_gap_stat(iris[, 1:4], max_k = 6, B = 10) plot_gap_stat(gap)
Visualize k-NN distances to help choose eps
plot_knn_dist(data, k = 4, add_suggestion = TRUE, percentile = 0.95)plot_knn_dist(data, k = 4, add_suggestion = TRUE, percentile = 0.95)
data |
A data frame, matrix, or tidy_knn_dist result |
k |
If data is a data frame, k for k-NN (default: 4) |
add_suggestion |
Add suggested eps line? (default: TRUE) |
percentile |
Percentile for suggestion (default: 0.95) |
A ggplot object.
plot_knn_dist(iris[, 1:4], k = 5)plot_knn_dist(iris[, 1:4], k = 5)
Visualize MDS results
plot_mds(mds_obj, color_by = NULL, label_points = TRUE, dim_x = 1, dim_y = 2)plot_mds(mds_obj, color_by = NULL, label_points = TRUE, dim_x = 1, dim_y = 2)
mds_obj |
A tidy_mds object |
color_by |
Optional grouping to colour points by: either a column name present in the MDS configuration, or a vector as long as the data. |
label_points |
Logical; add point labels? (default: TRUE) |
dim_x |
Which dimension for x-axis (default: 1) |
dim_y |
Which dimension for y-axis (default: 2) |
A ggplot object.
mds <- tidy_mds(USArrests, method = "classical") plot_mds(mds)mds <- tidy_mds(USArrests, method = "classical") plot_mds(mds)
Plot Silhouette Analysis
plot_silhouette(sil_obj)plot_silhouette(sil_obj)
sil_obj |
A tidy_silhouette object or tibble from tidy_silhouette_analysis |
A ggplot object.
km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) sil <- tidy_silhouette(km$cluster, d) plot_silhouette(sil)km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) sil <- tidy_silhouette(km$cluster, d) plot_silhouette(sil)
Create combined scree plot showing individual and cumulative variance
plot_variance_explained(variance_tbl, threshold = 0.8)plot_variance_explained(variance_tbl, threshold = 0.8)
variance_tbl |
Variance tibble from tidy_pca |
threshold |
Horizontal line for variance threshold (default: 0.8 for 80%) |
A ggplot object.
model <- tl_model(iris[, 1:4], method = "pca") plot_variance_explained(model$fit$variance_explained)model <- tl_model(iris[, 1:4], method = "pca") plot_variance_explained(model$fit$variance_explained)
Plot EDA results
## S3 method for class 'tidylearn_eda' plot(x, ...)## S3 method for class 'tidylearn_eda' plot(x, ...)
x |
A tidylearn_eda object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly. Called for its
side effect of plotting a PCA scatter plot coloured by cluster.
eda <- tl_explore(iris, response = "Species") plot(eda)eda <- tl_explore(iris, response = "Species") plot(eda)
Plot method for tidylearn models
## S3 method for class 'tidylearn_model' plot(x, type = "auto", ...)## S3 method for class 'tidylearn_model' plot(x, type = "auto", ...)
x |
A tidylearn model object |
type |
Plot type (default: "auto"). An unsupervised model takes only
|
... |
Additional arguments passed to plotting functions |
A ggplot object. The specific plot depends
on the model paradigm and type argument.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") plot(model, type = "actual_predicted")model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") plot(model, type = "actual_predicted")
Unified prediction interface for both supervised and unsupervised models
## S3 method for class 'tidylearn_model' predict(object, new_data = NULL, type = "response", ...)## S3 method for class 'tidylearn_model' predict(object, new_data = NULL, type = "response", ...)
object |
A tidylearn model object |
new_data |
A data frame containing the new data. If NULL, uses training data. |
type |
Type of prediction, for supervised models only:
|
... |
Additional arguments |
For supervised models, a tibble with a
.pred column; with type = "prob", one column per class
instead. It has one row per row of new_data, in order, and
zero rows for zero-row new_data. For unsupervised models, the
method's natural output: an .obs_id column, the row names of
the data predicted on, plus component scores for "pca" and
"mds" (as many as the model keeps), or plus a cluster
column for the clustering methods.
Unsupervised models differ in whether they can handle new data.
"pca" projects it and "kmeans" assigns it to the
nearest centre; "pam", "clara", "dbscan",
"mds" and "hclust" have no out-of-sample projection
and error if new_data is supplied. For hierarchical
clustering, cut the tree with tidy_cutree() instead.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") predict(model) predict(model, new_data = mtcars[1:5, ])model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") predict(model) predict(model, new_data = mtcars[1:5, ])
Predict from stratified models
## S3 method for class 'tidylearn_stratified' predict(object, new_data = NULL, ...)## S3 method for class 'tidylearn_stratified' predict(object, new_data = NULL, ...)
object |
A tidylearn_stratified model object |
new_data |
New data for predictions. NULL predicts the training rows from their stored cluster assignments, which works for every clustering method; new rows can be assigned by k-means only. |
... |
Additional arguments passed to each cluster's model, such as
|
A tibble of the columns each cluster's model
returns for the requested type – .pred by default, one
column per class for type = "prob" – and a .cluster
column with cluster assignments. Rows of a single-class cluster are
predicted as that class, with probability 1.
models <- tl_stratified_models(mtcars, mpg ~ ., cluster_method = "kmeans", k = 2, supervised_method = "linear") preds <- predict(models)models <- tl_stratified_models(mtcars, mpg ~ ., cluster_method = "kmeans", k = 2, supervised_method = "linear") preds <- predict(models)
Predict with transfer learning model
## S3 method for class 'tidylearn_transfer' predict(object, new_data, ...)## S3 method for class 'tidylearn_transfer' predict(object, new_data, ...)
object |
A tidylearn_transfer model object |
new_data |
New data for predictions |
... |
Additional arguments |
A tibble with a .pred column containing
predictions.
model <- tl_transfer_learning(iris, Species ~ ., pretrain_method = "pca", supervised_method = "tree") preds <- predict(model, iris[1:5, ])model <- tl_transfer_learning(iris, Species ~ ., pretrain_method = "pca", supervised_method = "tree") preds <- predict(model, iris[1:5, ])
Print Method for tidy_apriori
## S3 method for class 'tidy_apriori' print(x, ...)## S3 method for class 'tidy_apriori' print(x, ...)
x |
A tidy_apriori object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) print(res) }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) print(res) }
Print Method for tidy_dbscan
## S3 method for class 'tidy_dbscan' print(x, ...)## S3 method for class 'tidy_dbscan' print(x, ...)
x |
A tidy_dbscan object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
db <- tidy_dbscan(iris[, 1:4], eps = 0.5, minPts = 5) print(db)db <- tidy_dbscan(iris[, 1:4], eps = 0.5, minPts = 5) print(db)
Print Method for tidy_gap
## S3 method for class 'tidy_gap' print(x, ...)## S3 method for class 'tidy_gap' print(x, ...)
x |
A tidy_gap object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
gap <- tidy_gap_stat(iris[, 1:4], max_k = 6, B = 10) print(gap)gap <- tidy_gap_stat(iris[, 1:4], max_k = 6, B = 10) print(gap)
Print Method for tidy_hclust
## S3 method for class 'tidy_hclust' print(x, ...)## S3 method for class 'tidy_hclust' print(x, ...)
x |
A tidy_hclust object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
hc <- tidy_hclust(USArrests, method = "ward.D2") print(hc)hc <- tidy_hclust(USArrests, method = "ward.D2") print(hc)
Print Method for tidy_kmeans
## S3 method for class 'tidy_kmeans' print(x, ...)## S3 method for class 'tidy_kmeans' print(x, ...)
x |
A tidy_kmeans object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
km <- tidy_kmeans(iris[, 1:4], k = 3) print(km)km <- tidy_kmeans(iris[, 1:4], k = 3) print(km)
Print Method for tidy_mds
## S3 method for class 'tidy_mds' print(x, ...)## S3 method for class 'tidy_mds' print(x, ...)
x |
A tidy_mds object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
mds <- tidy_mds(USArrests, method = "classical") print(mds)mds <- tidy_mds(USArrests, method = "classical") print(mds)
Print Method for tidy_pam
## S3 method for class 'tidy_pam' print(x, ...)## S3 method for class 'tidy_pam' print(x, ...)
x |
A tidy_pam object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
pm <- tidy_pam(iris[, 1:4], k = 3) print(pm)pm <- tidy_pam(iris[, 1:4], k = 3) print(pm)
Print Method for tidy_pca
## S3 method for class 'tidy_pca' print(x, ...)## S3 method for class 'tidy_pca' print(x, ...)
x |
A tidy_pca object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
pca <- tidy_pca(USArrests) print(pca)pca <- tidy_pca(USArrests) print(pca)
Print Method for tidy_silhouette
## S3 method for class 'tidy_silhouette' print(x, ...)## S3 method for class 'tidy_silhouette' print(x, ...)
x |
A tidy_silhouette object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) sil <- tidy_silhouette(km$cluster, d) print(sil)km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) sil <- tidy_silhouette(km$cluster, d) print(sil)
Print auto ML results
## S3 method for class 'tidylearn_automl' print(x, ...)## S3 method for class 'tidylearn_automl' print(x, ...)
x |
A tidylearn_automl object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
result <- tl_auto_ml(iris, Species ~ ., time_budget = 10, use_reduction = FALSE, use_clustering = FALSE, cv_folds = 2) # The leaderboard, the winner and the metric it was ranked on print(result)result <- tl_auto_ml(iris, Species ~ ., time_budget = 10, use_reduction = FALSE, use_clustering = FALSE, cv_folds = 2) # The leaderboard, the winner and the metric it was ranked on print(result)
tidylearn_compute_advice objectsPrint method for tidylearn_compute_advice objects
## S3 method for class 'tidylearn_compute_advice' print(x, ...)## S3 method for class 'tidylearn_compute_advice' print(x, ...)
x |
A |
... |
Unused. |
The input x, invisibly.
# Runtime, peak memory and cost per tier, before committing to a fit advice <- tl_compute_advisor("forest", data = iris, formula = Species ~ .) print(advice)# Runtime, peak memory and cost per tier, before committing to a fit advice <- tl_compute_advisor("forest", data = iris, formula = Species ~ .) print(advice)
Print a tidylearn_data object
## S3 method for class 'tidylearn_data' print(x, ...)## S3 method for class 'tidylearn_data' print(x, ...)
x |
A |
... |
Additional arguments passed to the tibble print method. |
The input object x, returned invisibly.
f <- tempfile(fileext = ".csv") write.csv(iris, f, row.names = FALSE) d <- tl_read(f) print(d) unlink(f)f <- tempfile(fileext = ".csv") write.csv(iris, f, row.names = FALSE) d <- tl_read(f) print(d) unlink(f)
Print EDA results
## S3 method for class 'tidylearn_eda' print(x, ...)## S3 method for class 'tidylearn_eda' print(x, ...)
x |
A tidylearn_eda object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
eda <- tl_explore(iris, response = "Species") print(eda)eda <- tl_explore(iris, response = "Species") print(eda)
tidylearn_gpu_check objectsPrint method for tidylearn_gpu_check objects
## S3 method for class 'tidylearn_gpu_check' print(x, ...)## S3 method for class 'tidylearn_gpu_check' print(x, ...)
x |
A |
... |
Unused. |
The input x, invisibly.
# Reports no GPU rather than failing on a machine without one print(tl_check_gpu())# Reports no GPU rather than failing on a machine without one print(tl_check_gpu())
Print method for tidylearn models
## S3 method for class 'tidylearn_model' print(x, ...)## S3 method for class 'tidylearn_model' print(x, ...)
x |
A tidylearn model object |
... |
Additional arguments (ignored) |
The input object x, returned invisibly.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") print(model)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") print(model)
Print a tidylearn pipeline
## S3 method for class 'tidylearn_pipeline' print(x, ...)## S3 method for class 'tidylearn_pipeline' print(x, ...)
x |
A tidylearn pipeline object |
... |
Additional arguments (not used) |
The input pipeline object x, returned invisibly.
pipe <- tl_pipeline(iris, Species ~ .) print(pipe)pipe <- tl_pipeline(iris, Species ~ .) print(pipe)
Get product recommendations based on basket contents
recommend_products(rules_obj, basket, top_n = 5, min_confidence = 0.5)recommend_products(rules_obj, basket, top_n = 5, min_confidence = 0.5)
rules_obj |
A tidy_apriori object, an arules rules object, or a
tibble of rules from |
basket |
Character vector of items in current basket |
top_n |
Number of recommendations to return (default: 5) |
min_confidence |
Minimum confidence threshold (default: 0.5) |
A tibble with columns rhs (recommended item),
confidence, lift, and support, sorted by lift in
descending order. A rule is used when the basket holds its whole
left-hand side and none of its right-hand side, and each product is
listed once, from its highest-lift rule.
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) # The basket has to cover the whole left-hand side of a rule, so a # basket of very common items usually matches nothing above the # confidence floor recommend_products(res, basket = c("flour", "baking powder")) }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) # The basket has to cover the whole left-hand side of a rule, so a # basket of very common items usually matches nothing above the # confidence floor recommend_products(res, basket = c("flour", "baking powder")) }
Center and/or scale numeric variables
standardize_data(data, center = TRUE, scale = TRUE)standardize_data(data, center = TRUE, scale = TRUE)
data |
A data frame or tibble |
center |
Logical; center variables? (default: TRUE) |
scale |
Logical; scale variables to unit variance? (default: TRUE) |
A tibble with numeric variables centered and/or scaled as specified;
non-numeric columns are returned unchanged. A grouped tibble is
standardised within each group, as dplyr::mutate() works on it;
a rowwise tibble is standardised over its whole columns, since a single
value has no spread. Grouping and rowwise identifier columns are left
as they are.
std <- standardize_data(iris[, 1:4])std <- standardize_data(iris[, 1:4])
Use k-NN distance plot to suggest eps value
suggest_eps(data, minPts = 5, method = "percentile", percentile = 0.95)suggest_eps(data, minPts = 5, method = "percentile", percentile = 0.95)
data |
A data frame or matrix |
minPts |
The |
method |
Method to suggest eps: "percentile" (default), "knee" |
percentile |
If method="percentile", which percentile to use (default: 0.95) |
A list containing:
eps: suggested epsilon value
knn_distances: full tibble of k-NN distances
method: method used
eps_info <- suggest_eps(iris, minPts = 5) eps_info$epseps_info <- suggest_eps(iris, minPts = 5) eps_info$eps
Get summary statistics about rules
summarize_rules(rules_obj)summarize_rules(rules_obj)
rules_obj |
A tidy_apriori object, an arules rules object, or a rules tibble |
A list with n_rules and summary statistics (min,
max, mean, median) for support,
confidence, and lift.
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) summarize_rules(res) }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) summarize_rules(res) }
Summary method for tidylearn models
## S3 method for class 'tidylearn_model' summary(object, ...)## S3 method for class 'tidylearn_model' summary(object, ...)
object |
A tidylearn model object |
... |
Additional arguments (ignored) |
The input object, returned invisibly. Called for its
side effect of printing model summary and training performance.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") summary(model)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") summary(model)
Summarize a tidylearn pipeline
## S3 method for class 'tidylearn_pipeline' summary(object, ...)## S3 method for class 'tidylearn_pipeline' summary(object, ...)
object |
A tidylearn pipeline object |
... |
Additional arguments (not used) |
The input pipeline object, returned invisibly. Called
for its side effect of printing detailed pipeline and model results.
pipe <- tl_pipeline(iris, Species ~ .) summary(pipe)pipe <- tl_pipeline(iris, Species ~ .) summary(pipe)
Mine association rules using the Apriori algorithm with tidy output
tidy_apriori( transactions, support = 0.01, confidence = 0.5, minlen = 2, maxlen = 10, target = "rules", control = list(verbose = FALSE), ... )tidy_apriori( transactions, support = 0.01, confidence = 0.5, minlen = 2, maxlen = 10, target = "rules", control = list(verbose = FALSE), ... )
transactions |
A transactions object or data frame |
support |
Minimum support (default: 0.01) |
confidence |
Minimum confidence (default: 0.5) |
minlen |
Minimum rule length (default: 2) |
maxlen |
Maximum rule length (default: 10) |
target |
Type of association mined: "rules" (default), "frequent itemsets", "maximally frequent itemsets" |
control |
A list of algorithmic controls for
|
... |
Further arguments passed to |
A list of class "tidy_apriori" containing:
rules_tbl: tibble of rules, as tidy_rules returns
it, or NULL for an itemset target
rules: original arules object, rules or itemsets
parameters: parameters used
n_rules: number of rules, or of itemsets for an itemset target
itemsets_tbl: for an itemset target only, a tibble with
itemset_id, itemset (its label), size, the
quality measures, and items, a list column holding each
itemset's items
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") # Basic apriori rules <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) # Access rules rules$rules_tbl }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") # Basic apriori rules <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) # Access rules rules$rules_tbl }
Performs CLARA clustering (scalable version of PAM)
tidy_clara(data, k, metric = "euclidean", samples = 50, sampsize = NULL, ...)tidy_clara(data, k, metric = "euclidean", samples = 50, sampsize = NULL, ...)
data |
A data frame or tibble. CLARA samples observations, so it
takes no distance matrix; use |
k |
Number of clusters |
metric |
Distance metric (default: "euclidean") |
samples |
Number of samples to draw (default: 50) |
sampsize |
Sample size (default: min(n, 40 + 2*k)) |
... |
Further arguments passed to |
A list of class "tidy_clara" containing:
clusters: tibble with observation IDs and cluster assignments
medoids: tibble of medoid values
silhouette_avg: average silhouette width
model: original clara object
# CLARA for large datasets large_data <- iris[rep(1:nrow(iris), 10), 1:4] clara_result <- tidy_clara(large_data, k = 3, samples = 50) print(clara_result)# CLARA for large datasets large_data <- iris[rep(1:nrow(iris), 10), 1:4] clara_result <- tidy_clara(large_data, k = 3, samples = 50) print(clara_result)
Cut dendrogram to obtain cluster assignments
tidy_cutree(hclust_obj, k = NULL, h = NULL)tidy_cutree(hclust_obj, k = NULL, h = NULL)
hclust_obj |
A tidy_hclust object or hclust object |
k |
Number of clusters (optional) |
h |
Height at which to cut (optional) |
A tibble with columns .obs_id (observation identifier) and
cluster (integer cluster assignment).
hc <- tidy_hclust(USArrests, method = "ward.D2") clusters <- tidy_cutree(hc, k = 3)hc <- tidy_hclust(USArrests, method = "ward.D2") clusters <- tidy_cutree(hc, k = 3)
Performs density-based clustering with tidy output
tidy_dbscan(data, eps, minPts = 5, cols = NULL, distance = "euclidean")tidy_dbscan(data, eps, minPts = 5, cols = NULL, distance = "euclidean")
data |
A data frame, tibble, numeric matrix, or dist object |
eps |
Neighborhood radius (epsilon) |
minPts |
Minimum number of points to form a dense region (default: 5) |
cols |
Columns to include (tidy select).
If NULL, uses all numeric columns, or every column for
|
distance |
Distance metric if data is not a dist object (default:
"euclidean"): any method |
A list of class "tidy_dbscan" containing:
clusters: tibble with observation IDs, cluster assignments
(0 = noise), and the logical flags is_noise and
is_core
summary: tibble with each cluster's size and number of core points
n_clusters: number of clusters (excluding noise)
n_noise: number of noise points
eps, minPts: the parameters used
model: original dbscan object
# Basic DBSCAN db_result <- tidy_dbscan(iris, eps = 0.5, minPts = 5) # With suggested eps from k-NN distance plot eps_suggestion <- suggest_eps(iris, minPts = 5) db_result <- tidy_dbscan(iris, eps = eps_suggestion$eps, minPts = 5)# Basic DBSCAN db_result <- tidy_dbscan(iris, eps = 0.5, minPts = 5) # With suggested eps from k-NN distance plot eps_suggestion <- suggest_eps(iris, minPts = 5) db_result <- tidy_dbscan(iris, eps = eps_suggestion$eps, minPts = 5)
Create dendrogram visualization
tidy_dendrogram(hclust_obj, k = NULL, hang = 0.01, cex = 0.7)tidy_dendrogram(hclust_obj, k = NULL, hang = 0.01, cex = 0.7)
hclust_obj |
A tidy_hclust object or hclust object |
k |
Optional; number of clusters to highlight with rectangles |
hang |
Fraction of plot height to hang labels (default: 0.01) |
cex |
Label size (default: 0.7) |
The hclust object, returned invisibly. The
dendrogram is plotted as a side effect.
hc <- tidy_hclust(USArrests, method = "ward.D2") tidy_dendrogram(hc, k = 3)hc <- tidy_hclust(USArrests, method = "ward.D2") tidy_dendrogram(hc, k = 3)
Compute distance matrices with tidy output
tidy_dist(data, method = "euclidean", cols = NULL, ...)tidy_dist(data, method = "euclidean", cols = NULL, ...)
data |
A data frame or tibble |
method |
Character; distance method (default: "euclidean"). Options: "euclidean", "manhattan", "maximum", "gower" |
cols |
Columns to include (tidy select).
If NULL, uses all numeric columns, or every column for
|
... |
Additional arguments passed to distance functions |
A dist object containing the computed
distance matrix.
d <- tidy_dist(iris[, 1:4], method = "euclidean")d <- tidy_dist(iris[, 1:4], method = "euclidean")
Compute gap statistic for determining optimal number of clusters
tidy_gap_stat(data, FUN_cluster = NULL, max_k = 10, B = 50, nstart = 25)tidy_gap_stat(data, FUN_cluster = NULL, max_k = 10, B = 50, nstart = 25)
data |
A data frame or tibble |
FUN_cluster |
Clustering function (default: uses kmeans internally) |
max_k |
Maximum number of clusters (default: 10) |
B |
Number of bootstrap samples (default: 50) |
nstart |
If using kmeans, number of random starts (default: 25) |
A list of class "tidy_gap" containing:
gap_data: tibble with gap statistics for each k
k_firstSEmax: optimal k via maxSE's
firstSEmax method, the smallest k within one standard error of the
first local maximum (most conservative)
k_globalmax: optimal k via the globalmax method, the k with the largest gap (most liberal)
k_firstmax: optimal k via the firstmax method, the first local maximum of the gap
recommended_k: recommended k (uses firstSEmax)
model: the clusGap result
gap <- tidy_gap_stat(iris[, 1:4], max_k = 6, B = 10) gap$recommended_kgap <- tidy_gap_stat(iris[, 1:4], max_k = 6, B = 10) gap$recommended_k
Computes Gower distance for mixed data types (numeric, factor, ordered)
tidy_gower(data, weights = NULL)tidy_gower(data, weights = NULL)
data |
A data frame or tibble |
weights |
Optional named vector of variable weights (default: equal weights) |
Gower distance handles mixed data types:
Numeric: range-normalized Manhattan distance
Factor/Character: 0 if same, 1 if different
Ordered: treated as numeric ranks
Formula: d_ij = sum(w_k * d_ijk) / sum(w_k) where d_ijk is the dissimilarity for variable k between obs i and j
A dist object containing Gower distances, with
the method attribute set to "gower". A pair of rows with
no variable observed in both has no defined distance and is NA,
as in daisy.
# Create example data with mixed types car_data <- data.frame( horsepower = c(130, 250, 180), weight = c(1200, 1650, 1420), color = factor(c("red", "black", "blue")) ) # Compute Gower distance gower_dist <- tidy_gower(car_data)# Create example data with mixed types car_data <- data.frame( horsepower = c(130, 250, 180), weight = c(1200, 1650, 1420), color = factor(c("red", "black", "blue")) ) # Compute Gower distance gower_dist <- tidy_gower(car_data)
Performs hierarchical clustering with tidy output
tidy_hclust(data, method = "average", distance = "euclidean", cols = NULL)tidy_hclust(data, method = "average", distance = "euclidean", cols = NULL)
data |
A data frame, tibble, or dist object |
method |
Agglomeration method: "ward.D2", "single", "complete", "average" (default), "mcquitty", "median", "centroid" |
distance |
Distance metric if data is not a dist object (default: "euclidean") |
cols |
Columns to include (tidy select).
If NULL, uses all numeric columns, or every column for
|
A list of class "tidy_hclust" containing:
model: hclust object
dist: distance matrix used
method: linkage method used
distance_method: distance metric used
data: original data (for plotting)
# Basic hierarchical clustering hc_result <- tidy_hclust(USArrests, method = "average") # With specific distance hc_result <- tidy_hclust(mtcars, method = "complete", distance = "manhattan")# Basic hierarchical clustering hc_result <- tidy_hclust(USArrests, method = "average") # With specific distance hc_result <- tidy_hclust(mtcars, method = "complete", distance = "manhattan")
Performs k-means clustering with tidy output
tidy_kmeans( data, k, cols = NULL, nstart = 25, iter_max = 100, algorithm = "Hartigan-Wong" )tidy_kmeans( data, k, cols = NULL, nstart = 25, iter_max = 100, algorithm = "Hartigan-Wong" )
data |
A data frame or tibble |
k |
Number of clusters |
cols |
Columns to include (tidy select). If NULL, uses all numeric columns. |
nstart |
Number of random starts (default: 25) |
iter_max |
Maximum iterations (default: 100) |
algorithm |
K-means algorithm: "Hartigan-Wong" (default), "Lloyd", "Forgy", "MacQueen" |
A list of class "tidy_kmeans" containing:
clusters: tibble with observation IDs and cluster assignments
centers: tibble of cluster centers
metrics: tibble with clustering quality metrics
sizes: integer vector of cluster sizes
model: original kmeans object
# Basic k-means km_result <- tidy_kmeans(iris, k = 3)# Basic k-means km_result <- tidy_kmeans(iris, k = 3)
Calculate distances to k-th nearest neighbor for each point
tidy_knn_dist(data, k = 4, cols = NULL)tidy_knn_dist(data, k = 4, cols = NULL)
data |
A data frame or matrix |
k |
Number of nearest neighbors (default: 4) |
cols |
Columns to include (tidy select). If NULL, uses all numeric columns. |
A tibble with columns .obs_id (observation identifier),
knn_dist (distance to k-th nearest neighbor), and rank
(rank of the k-NN distance).
knn <- tidy_knn_dist(iris[, 1:4], k = 5)knn <- tidy_knn_dist(iris[, 1:4], k = 5)
Unified interface for MDS methods with tidy output
tidy_mds(data, method = "classical", ndim = 2, distance = "euclidean", ...)tidy_mds(data, method = "classical", ndim = 2, distance = "euclidean", ...)
data |
A data frame, tibble, or distance matrix |
method |
Character; "classical" (default), "metric", "nonmetric", "sammon", or "kruskal" |
ndim |
Number of dimensions for output (default: 2) |
distance |
Character; distance metric if data is
not already a dist object (default: "euclidean"): any method
|
... |
Additional arguments passed to specific MDS functions |
A list of class "tidy_mds" containing:
config: tibble of MDS configuration (coordinates)
stress: goodness-of-fit measure (if applicable)
method: character string of method used
model: original model object
# Classical MDS mds_result <- tidy_mds(eurodist, method = "classical") print(mds_result)# Classical MDS mds_result <- tidy_mds(eurodist, method = "classical") print(mds_result)
Performs classical multidimensional scaling using cmdscale()
tidy_mds_classical(dist_mat, ndim = 2, add_rownames = TRUE)tidy_mds_classical(dist_mat, ndim = 2, add_rownames = TRUE)
dist_mat |
A distance matrix (dist object) |
ndim |
Number of dimensions (default: 2) |
add_rownames |
Preserve row names from distance matrix (default: TRUE) |
A list of class "tidy_mds" containing:
config: tibble of MDS coordinates
stress: NA (not applicable for classical MDS)
gof: goodness-of-fit (proportion of variance retained)
eigenvalues: numeric vector of eigenvalues
method: "Classical MDS"
model: the cmdscale result
d <- dist(USArrests) mds <- tidy_mds_classical(d) print(mds)d <- dist(USArrests) mds <- tidy_mds_classical(d) print(mds)
Performs Kruskal's isoMDS
tidy_mds_kruskal(dist_mat, ndim = 2, ...)tidy_mds_kruskal(dist_mat, ndim = 2, ...)
dist_mat |
A distance matrix (dist object) |
ndim |
Number of dimensions (default: 2) |
... |
Additional arguments passed to MASS::isoMDS() |
A list of class "tidy_mds" containing:
config: tibble of MDS coordinates
stress: Kruskal stress value
method: "Kruskal's isoMDS"
model: the isoMDS result
d <- dist(USArrests) mds <- tidy_mds_kruskal(d)d <- dist(USArrests) mds <- tidy_mds_kruskal(d)
Performs Sammon's non-linear mapping
tidy_mds_sammon(dist_mat, ndim = 2, ...)tidy_mds_sammon(dist_mat, ndim = 2, ...)
dist_mat |
A distance matrix (dist object) |
ndim |
Number of dimensions (default: 2) |
... |
Additional arguments passed to MASS::sammon() |
A list of class "tidy_mds" containing:
config: tibble of MDS coordinates
stress: Sammon stress value
method: "Sammon Mapping"
model: the sammon result
d <- dist(USArrests) mds <- tidy_mds_sammon(d)d <- dist(USArrests) mds <- tidy_mds_sammon(d)
Performs MDS using SMACOF algorithm from the smacof package
tidy_mds_smacof(dist_mat, ndim = 2, type = "ratio", ...)tidy_mds_smacof(dist_mat, ndim = 2, type = "ratio", ...)
dist_mat |
A distance matrix (dist object) |
ndim |
Number of dimensions (default: 2) |
type |
Character; "ratio" for metric, "ordinal" for non-metric (default: "ratio") |
... |
Additional arguments passed to smacof::mds() |
A list of class "tidy_mds" containing:
config: tibble of MDS coordinates
stress: stress value from the SMACOF algorithm
method: character string describing the MDS type
model: the mds result
d <- dist(USArrests) mds <- tidy_mds_smacof(d, type = "ratio")d <- dist(USArrests) mds <- tidy_mds_smacof(d, type = "ratio")
Performs PAM clustering with tidy output
tidy_pam(data, k, metric = "euclidean", cols = NULL, ...)tidy_pam(data, k, metric = "euclidean", cols = NULL, ...)
data |
A data frame, tibble, or dist object |
k |
Number of clusters |
metric |
Distance metric (default: "euclidean"). Use "gower" for mixed data types. |
cols |
Columns to include (tidy select). If NULL, uses all columns. |
... |
Further arguments passed to |
A list of class "tidy_pam" containing:
clusters: tibble with observation IDs and cluster assignments
medoids: tibble with one row per cluster: the medoid's row
position in the data (medoid_index, an integer) and, unless
data was a dist object, its values
silhouette_avg: average silhouette width
silhouette_data: the silhouette information pam()
returns (its silinfo)
model: original pam object
# PAM with Euclidean distance pam_result <- tidy_pam(iris, k = 3) # PAM with Gower distance for mixed data pam_result <- tidy_pam(mtcars, k = 3, metric = "gower")# PAM with Euclidean distance pam_result <- tidy_pam(iris, k = 3) # PAM with Gower distance for mixed data pam_result <- tidy_pam(mtcars, k = 3, metric = "gower")
Performs PCA on a dataset using tidyverse principles. Returns a tidy list containing scores, loadings, variance explained, and the original model.
tidy_pca(data, cols = NULL, scale = TRUE, center = TRUE, method = "prcomp")tidy_pca(data, cols = NULL, scale = TRUE, center = TRUE, method = "prcomp")
data |
A data frame or tibble |
cols |
Columns to include in PCA (tidy select syntax). If NULL, uses all numeric columns. |
scale |
Logical; should variables be scaled to unit variance? Default TRUE. |
center |
Logical; should variables be centered? Default TRUE.
|
method |
Character; "prcomp" (default, recommended) or "princomp" |
A list of class "tidy_pca" containing:
scores: tibble of PC scores with observation identifiers
loadings: tibble of variable loadings in long format
variance: tibble of variance explained by each PC
model: the original prcomp/princomp object
settings: list of scale, center, method used
# Basic PCA pca_result <- tidy_pca(USArrests) # Access components pca_result$scores pca_result$loadings pca_result$variance# Basic PCA pca_result <- tidy_pca(USArrests) # Access components pca_result$scores pca_result$loadings pca_result$variance
Visualize both observations and variables in PC space
tidy_pca_biplot( pca_obj, pc_x = 1, pc_y = 2, color_by = NULL, arrow_scale = 1, label_obs = FALSE, label_vars = TRUE )tidy_pca_biplot( pca_obj, pc_x = 1, pc_y = 2, color_by = NULL, arrow_scale = 1, label_obs = FALSE, label_vars = TRUE )
pca_obj |
A tidy_pca object |
pc_x |
Principal component for x-axis (default: 1) |
pc_y |
Principal component for y-axis (default: 2) |
color_by |
Optional grouping to colour points by: either a column name present in the PCA scores, or a vector as long as the data. |
arrow_scale |
Scaling factor for variable arrows (default: 1) |
label_obs |
Logical; label observations? (default: FALSE) |
label_vars |
Logical; label variables? (default: TRUE) |
A ggplot object.
pca <- tidy_pca(USArrests) tidy_pca_biplot(pca)pca <- tidy_pca(USArrests) tidy_pca_biplot(pca)
Visualize variance explained by each principal component
tidy_pca_screeplot(pca_obj, type = "proportion", add_line = TRUE)tidy_pca_screeplot(pca_obj, type = "proportion", add_line = TRUE)
pca_obj |
A tidy_pca object |
type |
Character; "variance" or "proportion" (default) |
add_line |
Logical; add horizontal line at eigenvalue = 1? (for Kaiser criterion) |
A ggplot object.
pca <- tidy_pca(USArrests) tidy_pca_screeplot(pca)pca <- tidy_pca(USArrests) tidy_pca_screeplot(pca)
Convert Association Rules to Tidy Tibble
tidy_rules(rules)tidy_rules(rules)
rules |
A rules object from arules |
A tibble with columns rule_id, lhs, rhs,
the quality measures (e.g., support, confidence,
lift), and the list columns lhs_items and
rhs_items, each rule's items on that side as a character
vector. The lhs and rhs labels are for reading; the
helpers that match items read the lists, since an item name can hold
the comma that separates items in a label. An empty rule set gives a
zero-row tibble with the same columns.
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") rules_obj <- arules::apriori(Groceries, parameter = list(supp = 0.001, conf = 0.5)) rules_tbl <- tidy_rules(rules_obj) }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") rules_obj <- arules::apriori(Groceries, parameter = list(supp = 0.001, conf = 0.5)) rules_tbl <- tidy_rules(rules_obj) }
Compute silhouette statistics for cluster validation
tidy_silhouette(clusters, dist_mat)tidy_silhouette(clusters, dist_mat)
clusters |
Vector of cluster assignments: numeric, factor or
character. Numeric labels, or labels that read as whole numbers (such
as the |
dist_mat |
Distance matrix (dist object) |
A list of class "tidy_silhouette" containing:
silhouette_data: tibble with silhouette values for each observation
avg_width: average silhouette width
cluster_avg: average silhouette width by cluster
km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) sil <- tidy_silhouette(km$cluster, d)km <- kmeans(iris[, 1:4], centers = 3, nstart = 25) d <- dist(iris[, 1:4]) sil <- tidy_silhouette(km$cluster, d)
Silhouette Analysis Across Multiple k Values
tidy_silhouette_analysis( data, max_k = 10, method = "kmeans", nstart = 25, dist_method = "euclidean", linkage_method = "average" )tidy_silhouette_analysis( data, max_k = 10, method = "kmeans", nstart = 25, dist_method = "euclidean", linkage_method = "average" )
data |
A data frame or tibble |
max_k |
Maximum number of clusters to test (default: 10) |
method |
Clustering method: "kmeans" (default) or "hclust" |
nstart |
If kmeans, number of random starts (default: 25) |
dist_method |
Distance metric (default: "euclidean") |
linkage_method |
If hclust, linkage method (default: "average") |
A tibble with columns k and avg_sil_width. The
"optimal_k" attribute contains the k with the highest average
silhouette width.
sil_analysis <- tidy_silhouette_analysis(iris[, 1:4], max_k = 6)sil_analysis <- tidy_silhouette_analysis(iris[, 1:4], max_k = 6)
Logistic regression and classification metrics functionality
The consent gate that stands between a compute = "cloud" call and any training data leaving the user's machine.
Bounding what a cloud fit can cost, and keeping in-flight jobs visible.
Resolving and validating the Modal Web Function endpoint
that compute = "cloud" submits to.
Converting a fitted tidylearn model to bytes and back, so it can cross the boundary between a remote worker and the caller's R session.
Coefficients, standard errors and confidence intervals as a
tibble, for the methods that have them. tl_table_coefficients()
formats the same numbers as a gt table.
Core functionality for tidylearn. This package provides a unified tidyverse-compatible interface to established R machine learning packages including glmnet, randomForest, xgboost, e1071, rpart, gbm, nnet, cluster, and dbscan. The underlying algorithms are unchanged - tidylearn wraps them with consistent function signatures, tidy tibble output, and unified ggplot2-based visualization. Supervised models keep the wrapped object at model$fit; unsupervised ones put it at model$fit$model, alongside the tidied components.
Functions for advanced model diagnostics, assumption checking, and outlier detection
Functions for testing, visualizing, and analyzing interactions
Functions for stepwise model selection, cross-validation, and hyperparameter tuning
Neural network functionality for classification and regression
Functions for creating end-to-end model pipelines
Functions for reading data from diverse sources into tidy
tidylearn_data objects. The main dispatcher
tl_read() auto-detects the format from the file
extension and routes to the appropriate reader.
All readers return a tidylearn_data object,
which is a tibble subclass carrying metadata about
the data source.
Supported file formats:
CSV: .csv files via readr
(with base R fallback), and .txt files named directly
TSV: .tsv files via readr
(with base R fallback)
Excel: .xls, .xlsx,
.xlsm files via readxl
Parquet: .parquet files via nanoparquet
JSON: .json files, and newline-delimited
.ndjson files, via jsonlite
RDS: .rds files via base readRDS()
RData: .rdata, .rda
files via base load()
CSV and TSV files compressed with gzip, bzip2 or xz
(data.csv.gz) are recognised by the extension under the
compression one.
Supported databases (via DBI):
SQLite: .sqlite, .db files via RSQLite
PostgreSQL: via RPostgres
MySQL/MariaDB: via RMariaDB
BigQuery: bigquery://project/dataset URIs via
bigrquery
Supported cloud/API sources:
S3: s3:// URIs via paws.storage
GitHub: raw file download from repositories
Kaggle: dataset download via Kaggle CLI
A file:// URL is read as the local path it names. Other web
URLs, and URLs with any other scheme such as ftp://, are not
read; download the file first.
Multi-file reading:
Multiple paths: pass a character vector to tl_read()
Directories: tl_read_dir() scans for data files with
optional pattern/format filtering and recursive scanning
Zip archives: tl_read_zip() extracts and reads from
.zip files
When combining multiple files, a source_file column is added to
identify the origin of each row: the file's path below the directory
or archive it came from, or, for paths given directly, below the
deepest folder they share. Files in one folder are labelled by their
bare names.
Directory and archive scans read the extensions listed above except
.txt, which in a folder is as likely to hold notes as data.
Name a .txt file directly, or select it with pattern,
to read it.
Backend readers for databases and cloud/API sources.
All backends are optional dependencies checked at call time via
tl_check_packages().
Database backends (via DBI):
SQLite: via RSQLite
PostgreSQL: via RPostgres
MySQL/MariaDB: via RMariaDB
BigQuery: via bigrquery
Cloud/API backends:
S3: via paws.storage
GitHub: via base download.file()
Kaggle: via Kaggle CLI
Ridge, Lasso, and Elastic Net regularization functionality
SVM functionality for classification and regression
Functions for producing formatted gt tables
from tidylearn models. Provides a parallel interface to
the plot functions: tl_table(model, type)
dispatches to the appropriate table formatter based on model type.
Requires the gt package (suggested dependency).
Decision trees, random forests, and boosting functionality
Functions for automatic hyperparameter tuning and selection
General visualization functions for tidylearn models
End-to-end workflows that combine supervised and unsupervised learning.
Add cluster assignments as features for supervised learning. This semi-supervised approach can capture non-linear patterns.
tl_add_cluster_features(data, response = NULL, method = "kmeans", ...)tl_add_cluster_features(data, response = NULL, method = "kmeans", ...)
data |
A data frame |
response |
Response variable name (will be excluded from clustering) |
method |
Clustering method: "kmeans", "pam", "hclust", "dbscan" |
... |
Additional arguments for clustering |
The original data frame with an additional factor column named
cluster_<method> containing cluster assignments. The fitted
cluster model is stored as an attribute "cluster_model".
# Add cluster features before supervised learning data_with_clusters <- tl_add_cluster_features(iris, response = "Species", method = "kmeans", k = 3) model <- tl_model(data_with_clusters, Species ~ ., method = "forest")# Add cluster features before supervised learning data_with_clusters <- tl_add_cluster_features(iris, response = "Species", method = "kmeans", k = 3) model <- tl_model(data_with_clusters, Species ~ ., method = "forest")
Detect outliers using DBSCAN or other methods, then optionally remove them or down-weight them before supervised learning.
tl_anomaly_aware( data, formula, response, anomaly_method = "dbscan", action = "flag", supervised_method = "tree", ... )tl_anomaly_aware( data, formula, response, anomaly_method = "dbscan", action = "flag", supervised_method = "tree", ... )
data |
A data frame |
formula |
Model formula |
response |
Response variable name, left out of the detection |
anomaly_method |
Method for anomaly detection. Only "dbscan" is implemented; its noise points are the anomalies. |
action |
Action to take: "remove", "flag", "downweight".
|
supervised_method |
Supervised learning method (default:
|
... |
Additional arguments for DBSCAN, such as |
DBSCAN runs on the predictors the formula names, on their own scale: its
eps and minPts (defaults 0.5 and 5, passed through
...) are a distance and a count in those units. If every row comes
out as noise the call stops, since no normal data would be left to model.
A tidylearn model object with additional class
"tidylearn_anomaly_aware". The model includes an
anomaly_info element with anomaly_model,
is_anomaly (logical vector), n_anomalies, and
action.
model <- tl_anomaly_aware(iris, Species ~ ., response = "Species", anomaly_method = "dbscan", action = "flag")model <- tl_anomaly_aware(iris, Species ~ ., response = "Species", anomaly_method = "dbscan", action = "flag")
Find important interactions automatically
tl_auto_interactions( data, formula, top_n = 3, min_r2_change = 0.01, max_p_value = 0.05, exclude_vars = NULL )tl_auto_interactions( data, formula, top_n = 3, min_r2_change = 0.01, max_p_value = 0.05, exclude_vars = NULL )
data |
A data frame containing the data |
formula |
A formula specifying the base model without interactions |
top_n |
Number of top interactions to return, a whole number. With
|
min_r2_change |
Minimum change in R-squared to consider |
max_p_value |
Maximum p-value for significance |
exclude_vars |
Character vector of predictor variables that may not
appear in a selected interaction. They stay in the model as main effects.
Every name must be a predictor in |
A tidylearn model object (class "tidylearn_model") fitted
with the top significant interaction terms added to the formula.
The interaction test results and selected interactions are stored as
attributes "interaction_tests" and
"selected_interactions", data frames in the layout
tl_test_interactions returns. Both are present when no
interaction is added, with no rows where there is nothing to report.
model <- tl_auto_interactions(mtcars, mpg ~ wt + hp + cyl, top_n = 2)model <- tl_auto_interactions(mtcars, mpg ~ wt + hp + cyl, top_n = 2)
Automatically explores multiple modeling approaches including dimensionality reduction, clustering, and various supervised methods. Returns the best performing model, scored by cross-validation where the time budget allows.
tl_auto_ml( data, formula, task = "auto", use_reduction = TRUE, use_clustering = TRUE, time_budget = 300, cv_folds = 5, metric = NULL )tl_auto_ml( data, formula, task = "auto", use_reduction = TRUE, use_clustering = TRUE, time_budget = 300, cv_folds = 5, metric = NULL )
data |
A data frame |
formula |
Model formula (for supervised learning) |
task |
Task type: "classification", "regression", or "auto"
(default), which takes it from the response the formula computes, so
|
use_reduction |
Whether to try dimensionality reduction (default: TRUE) |
use_clustering |
Whether to add cluster features (default: TRUE) |
time_budget |
Time budget in seconds (default: 300). The budget is checked between model fits, not during them: once a model starts training it runs to completion, because R cannot safely interrupt C-level code (randomForest, xgboost, e1071). A run can therefore overshoot the budget by the length of the last fit it started. The budget gates the workflow as follows:
The example below, with |
cv_folds |
Number of cross-validation folds (default: 5). Reducing this (e.g. to 2 or 3) is an effective way to stay closer to the time budget since CV is typically the most expensive step. |
metric |
Evaluation metric (default: "accuracy" for classification, "rmse" for regression). Classification takes "accuracy", "precision", "recall", "sensitivity", "specificity", "f1", "auc" or "pr_auc"; regression takes "rmse", "mse", "mae", "mape" or "rsq". It is checked before any model is fitted. |
The PCA and cluster variants are built from the formula's predictors
only, so a column the formula leaves out (y ~ . - id) reaches no
candidate. The cluster variants add the cluster assignment to the
formula's terms.
A list with class "tidylearn_automl" containing:
The best tidylearn model object
Named list of all successfully trained models
Tibble ranking models by the chosen metric, with
columns model, score and evaluation. The
evaluation column records how each score was obtained –
"cv" for cross-validated, "train" for training-set
metrics, which are optimistic. Scores of different kinds are not
directly comparable; a mixed leaderboard means the budget ran
short of cross-validating every model.
Detected or specified task type
Metric used for ranking
Total elapsed time as a difftime object
# Quick run with fast models only (< 30s budget skips forest/SVM/XGBoost) result <- tl_auto_ml(iris, Species ~ ., time_budget = 10, use_reduction = FALSE, use_clustering = FALSE, cv_folds = 2) result$leaderboard# Quick run with fast models only (< 30s budget skips forest/SVM/XGBoost) result <- tl_auto_ml(iris, Species ~ ., time_budget = 10, use_reduction = FALSE, use_clustering = FALSE, cv_folds = 2) result$leaderboard
Scores predicted classes, and optionally class probabilities, against observed classes.
tl_calc_classification_metrics( actuals, predicted, predicted_probs = NULL, metrics = c("accuracy", "precision", "recall", "f1", "auc"), thresholds = NULL, ... )tl_calc_classification_metrics( actuals, predicted, predicted_probs = NULL, metrics = c("accuracy", "precision", "recall", "f1", "auc"), thresholds = NULL, ... )
actuals |
Observed classes: a factor, or a character, logical or numeric vector. |
predicted |
Predicted classes, one for each element of
|
predicted_probs |
Class probabilities, needed for |
metrics |
Character vector of metrics to compute, from
|
thresholds |
Optional numeric vector of cut-offs on the positive
class's probability, for binary classification. Each adds rows
scoring the classes that cut-off assigns. Needs
|
... |
Not used. |
The classes, in order, are the levels of predicted when it is a
factor of two or more levels – which is how predict() returns
them, in the model's order – and otherwise the classes present in
actuals. Any other class found in actuals or
predicted follows them. For two classes the second is the
positive class. tl_evaluate instead leaves out rows of a
class the model was never trained on, since it knows the model's
classes.
A row missing its observed class, its prediction or one of its
probabilities is dropped before anything is computed, so every metric
describes the same rows. With no row left – actuals empty, or
every row incomplete – there is nothing to score, and it is an error
of class tidylearn_no_scored_rows.
A tibble with columns metric (character)
and value (numeric), one row per requested metric. For more
than two classes, "auc" is followed by an auc_<class>
row for each class.
"auc" and "pr_auc" need at least two classes among the
scored rows, and are NA, with a warning, when there is only
one. A class with no scored row has no one-vs-rest area: its
auc_<class> row is NA, the averages cover the other
classes, and a warning names it.
With thresholds, six rows per cut-off follow – for a cut-off
of 0.5, accuracy_t0.5, precision_t0.5,
recall_t0.5, f1_t0.5, f2_t0.5 and
f0.5_t0.5 – and the tibble gains a threshold column,
NA on the other rows.
model <- tl_model(iris, Species ~ ., method = "forest") preds <- predict(model) tl_calc_classification_metrics(iris$Species, preds$.pred)model <- tl_model(iris, Species ~ ., method = "forest") preds <- predict(model) tl_calc_classification_metrics(iris$Species, preds$.pred)
Check model assumptions
tl_check_assumptions(model, test = TRUE, verbose = TRUE)tl_check_assumptions(model, test = TRUE, verbose = TRUE)
model |
A tidylearn model object |
test |
Logical; whether to perform statistical tests |
verbose |
Logical; whether to print test results and explanations |
A named list with one element per assumption checked
(linearity, independence, homoscedasticity,
normality, multicollinearity, outliers), each
containing assumption (character label), check (logical,
NA when the test could not decide, or NULL when no test
was run), details (character), and recommendation
(character). An additional overall element summarises the
number of assumptions checked, violated, and satisfied; an NA or
NULL check counts as neither. A check is NA when the fit
leaves it nothing to read: on a perfect fit the residuals are rounding
error, so every check of them is.
Logistic regression assumes neither normal residuals nor a constant
variance, so for a logistic model normality and
homoscedasticity have a NULL check and a note saying so.
For a factor, multicollinearity is judged on GVIF^(1/Df), the
generalised VIF on the scale of an ordinary one.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_check_assumptions(model)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_check_assumptions(model)
Reports whether the local machine has a CUDA-capable GPU and which
tidylearn backends (xgboost, keras3, tensorflow) are positioned
to use it. Detection is intentionally cheap: it parses nvidia-smi
output, giving up with a warning if nvidia-smi has not answered
within 10 seconds, and reads which R packages are installed from the
library without loading them, so it neither starts Python nor fits a
model. A backend reported as gpu_likely_works = TRUE may still fall
back to CPU if it was not compiled or configured with CUDA support —
confirm with a small real fit before relying on it for production
workloads.
tl_check_gpu(verbose = FALSE)tl_check_gpu(verbose = FALSE)
verbose |
Logical. If |
Apple MPS (Metal Performance Shaders) is intentionally not detected in this iteration; see the issue tracker for the MPS feature request.
An object of class tidylearn_gpu_check: a list with components
any_gpu (logical), cuda (driver info list with driver_present,
device_count, device_names, driver_version), backends (per-backend
status list each containing installed, gpu_likely_works, notes),
and messages (character vector). A print() method is provided.
# Safe to call anywhere: probes for nvidia-smi on the PATH and checks # which backend packages are installed. Reports no GPU rather than # failing when there isn't one. gpu <- tl_check_gpu() gpu$any_gpu if (gpu$any_gpu && gpu$backends$xgboost$gpu_likely_works) { # xgboost with GPU is worth trying for this workload }# Safe to call anywhere: probes for nvidia-smi on the PATH and checks # which backend packages are installed. Reports no GPU rather than # failing when there isn't one. gpu <- tl_check_gpu() gpu$any_gpu if (gpu$any_gpu && gpu$backends$xgboost$gpu_likely_works) { # xgboost with GPU is worth trying for this workload }
tidylearn uploads training data only to Modal's own hosts
(*.modal.run, *.modal.com). Modal customers serving Web Functions
from a custom domain can add that domain here.
tl_cloud_allow_host(host)tl_cloud_allow_host(host)
host |
A character vector of host names to allow, or |
This widens the set of destinations your data may be sent to, so it
is deliberately a per-session call rather than an option or an
environment variable: a shared .Rprofile or an inherited environment
should not be able to add a destination without you writing the call.
Additions are never persisted and are forgotten when the session ends.
Hosts match themselves and their subdomains. Adding
"fits.example.com" accepts https://fits.example.com and
https://a.fits.example.com, and nothing else.
Give the endpoint's full host name. A name of fewer than three labels,
such as "example.com" or "co.uk", is refused: two labels can be a
public suffix, under which anyone can register a site, and allowing
one would admit all of them.
The full allowlist after the change, invisibly.
tl_cloud_allowed_hosts(), and T9 in
system.file("security/threat-model.md", package = "tidylearn").
tl_cloud_allow_host("fits.example.com") tl_cloud_allowed_hosts() tl_cloud_allow_host(NULL)tl_cloud_allow_host("fits.example.com") tl_cloud_allowed_hosts() tl_cloud_allow_host(NULL)
Modal's own hosts, plus anything added with tl_cloud_allow_host()
during this session.
tl_cloud_allowed_hosts()tl_cloud_allowed_hosts()
A character vector of host names.
tl_cloud_allowed_hosts()tl_cloud_allowed_hosts()
Fitting with compute = "cloud" uploads your training data to your
own Modal account, which is a third party. tidylearn will not do that
without explicit consent on every call.
tl_cloud_consent(consent = TRUE)tl_cloud_consent(consent = TRUE)
consent |
|
There are two ways to give it. Pass confirm_upload = TRUE to each
tl_model() call, or call tl_cloud_consent() once to opt in for the
rest of the session. The session lock exists for batch and
non-interactive work, where a per-call argument is repetitive.
The lock is not persisted. It is forgotten when the R session
ends, and it is never written to disk. Revoke it early with
tl_cloud_consent(FALSE).
tidylearn never prompts interactively for consent, so scripts, CI and
Rscript behave the same as an interactive session.
The previous consent state, invisibly.
The full contract is in
system.file("security/threat-model.md", package = "tidylearn").
# Opt in for the session, then revoke. old <- tl_cloud_consent(TRUE) tl_cloud_consent(FALSE)# Opt in for the session, then revoke. old <- tl_cloud_consent(TRUE) tl_cloud_consent(FALSE)
Every cloud submission is recorded here so that no job runs invisibly. A job disappears from this list when its result is collected or it is cancelled.
tl_cloud_jobs()tl_cloud_jobs()
A submitted job runs on Modal whatever this R session does. If the session ends while a job is still listed here, that job keeps running and keeps billing until it finishes or its timeout kills it — this list cannot survive the session, but the timeout does.
A tibble with one row per in-flight job: call_id, method,
submitted_at, timeout_seconds and worst_case_cost. Zero rows
when nothing is in flight.
# Nothing in flight in a fresh session tl_cloud_jobs()# Nothing in flight in a fresh session tl_cloud_jobs()
Returns the coefficients of a fitted tidylearn model as a tibble, with
standard errors, test statistics, p-values and – on request – confidence
intervals. Available for "linear", "polynomial",
"logistic", "ridge", "lasso" and
"elastic_net". Other methods have no coefficients.
tl_table_importance covers "tree", "forest",
"boost" and "xgboost".
tl_coefficients( model, conf_int = FALSE, level = 0.95, exponentiate = FALSE, lambda = "1se" )tl_coefficients( model, conf_int = FALSE, level = 0.95, exponentiate = FALSE, lambda = "1se" )
model |
A tidylearn supervised model object from
|
conf_int |
Whether to add |
level |
Confidence level for the interval (default 0.95), a number
strictly between 0 and 1. Used only when |
exponentiate |
Whether to report |
lambda |
For regularised methods: |
Intervals are Wald intervals, computed from the same standard errors as
the statistic and p_value columns beside them, so the
interval and the p-value always agree about whether zero is excluded. For
"linear" and "polynomial" that means t quantiles on
the residual degrees of freedom, which is exactly what
confint returns for an lm. For
"logistic" it means z quantiles, which is what the reported
z statistic implies but not what confint() gives – that
profiles the likelihood, which is the better interval when the sample is
small or a class is nearly separated. Call
stats::confint(model$fit) when you want it.
A rank-deficient fit – two perfectly collinear predictors, or an
interaction of factors with a combination no row has – cannot estimate
every term. Those terms are
returned with an NA estimate rather than dropped, so a term named
in the formula never disappears from the output without saying so.
A tibble, one row per model term. For "linear",
"polynomial" and "logistic": term,
estimate, std_error, statistic, p_value,
plus conf_low and conf_high when conf_int = TRUE.
For regularised methods: term, estimate and the
lambda the estimate came from – glmnet reports no standard
errors, so there is nothing to test or bound. A multiclass
regularised model has one set of coefficients per class, so its rows
are led by a class column; exponentiate is not available
for it, because glmnet's multinomial coefficients are not relative to
a reference class.
tl_table_coefficients for the same numbers as a
formatted table.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_coefficients(model) tl_coefficients(model, conf_int = TRUE) # Odds ratios from a logistic fit am_data <- transform(mtcars, am = factor(am)) model <- tl_model(am_data, am ~ wt, method = "logistic") tl_coefficients(model, conf_int = TRUE, exponentiate = TRUE)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_coefficients(model) tl_coefficients(model, conf_int = TRUE) # Odds ratios from a logistic fit am_data <- transform(mtcars, am = factor(am)) model <- tl_model(am_data, am ~ wt, method = "logistic") tl_coefficients(model, conf_int = TRUE, exponentiate = TRUE)
Each model is refitted on every fold from its formula, method and
fitting arguments. A model a refit would not reproduce is refused: one
built by tl_semisupervised or tl_anomaly_aware,
or one of tl_auto_ml's candidates fitted on features it
engineered.
tl_compare_cv(data, models, folds = 5, metrics = NULL, ...)tl_compare_cv(data, models, folds = 5, metrics = NULL, ...)
data |
A data frame containing the training data |
models |
A named list of supervised tidylearn models, all of them
classification or all regression. An unnamed model is named
|
folds |
Number of cross-validation folds, a whole number between 2
and |
metrics |
Character vector of metrics to compute, from those
|
... |
Arguments passed to |
A list with two elements:
$fold_metricsA data frame with columns
metric, value, fold, and model
containing per-fold results for every model. A metric undefined
on a fold – "auc" on a fold holding one class – is
NA there, and so is every metric of a fold none of whose
rows can be scored, with a warning naming the fold.
$summaryA data frame with columns model,
metric, mean_value, sd_value,
min_value, and max_value summarizing
cross-validation performance over the folds with a value. A
metric with no value on any fold is NA throughout.
m1 <- tl_model(mtcars, mpg ~ wt, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") cv <- tl_compare_cv(mtcars, list(simple = m1, full = m2), folds = 3) cv$summarym1 <- tl_model(mtcars, mpg ~ wt, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") cv <- tl_compare_cv(mtcars, list(simple = m1, full = m2), folds = 3) cv$summary
Compare models from a pipeline
tl_compare_pipeline_models(pipeline, metrics = NULL)tl_compare_pipeline_models(pipeline, metrics = NULL)
pipeline |
A tidylearn pipeline object with results |
metrics |
Character vector of metrics to compare, each one the pipeline scored (if NULL, uses all available) |
A ggplot object showing a faceted bar
chart comparing metric values across models, with the best model
highlighted.
pipe <- tl_pipeline(iris, Species ~ ., models = list( tree = list(method = "tree"), forest = list(method = "forest", ntree = 100) ), evaluation = list(validation = "cv", cv_folds = 3)) pipe <- tl_run_pipeline(pipe, verbose = FALSE) tl_compare_pipeline_models(pipe) # Restrict the comparison to one metric tl_compare_pipeline_models(pipe, metrics = "accuracy")pipe <- tl_pipeline(iris, Species ~ ., models = list( tree = list(method = "tree"), forest = list(method = "forest", ntree = 100) ), evaluation = list(validation = "cv", cv_folds = 3)) pipe <- tl_run_pipeline(pipe, verbose = FALSE) tl_compare_pipeline_models(pipe) # Restrict the comparison to one metric tl_compare_pipeline_models(pipe, metrics = "accuracy")
Estimates runtime and feasibility on local CPU, local GPU (when available and applicable), and cloud (planning figures only, since cloud submission does not exist yet), then returns a structured recommendation. Useful before kicking off a long fit — call this first to see whether the problem is laptop-sized, GPU-sized, or cloud-sized.
tl_compute_advisor(x, ...) ## S3 method for class 'character' tl_compute_advisor( x, data, formula = NULL, hyperparams = list(), gpu_check = NULL, ... ) ## S3 method for class 'tidylearn_supervised' tl_compute_advisor( x, data = NULL, formula = NULL, hyperparams = list(), gpu_check = NULL, ... ) ## Default S3 method: tl_compute_advisor(x, ...)tl_compute_advisor(x, ...) ## S3 method for class 'character' tl_compute_advisor( x, data, formula = NULL, hyperparams = list(), gpu_check = NULL, ... ) ## S3 method for class 'tidylearn_supervised' tl_compute_advisor( x, data = NULL, formula = NULL, hyperparams = list(), gpu_check = NULL, ... ) ## Default S3 method: tl_compute_advisor(x, ...)
x |
Either a method name (character scalar — same names as
accepted by |
... |
Must be empty: a misspelt argument is an error. |
data |
A data frame. Required when |
formula |
Optional formula. Used to determine the number of
effective predictors: the terms it expands to against |
hyperparams |
Named list of hyperparameters that affect runtime:
|
gpu_check |
Optional |
Estimates are deliberately rough. Each method's runtime constant comes from timing fits of three sizes on one laptop, where the estimates land within about a factor of 3 of those fits. Two runs on that laptop differed by up to 2.5x, and other hardware or differently shaped data can be further off, so treat the recommendation as a starting point. The memory multiples and GPU speedups are assumptions with no measurement behind them.
An object of class tidylearn_compute_advice containing
problem, local_cpu, local_gpu, cloud, recommendation,
and reasoning. A print() method is provided.
# Estimating from a method name needs neither a GPU nor the backend # package -- it is arithmetic over the problem dimensions advice <- tl_compute_advisor("xgboost", iris, Species ~ ., hyperparams = list(nrounds = 1000)) advice$recommendation print(advice) # Dispatching on a fitted model requires the backend to be installed. # CRAN asks examples to use at most two threads. if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "xgboost", nthread = 2) tl_compute_advisor(model) }# Estimating from a method name needs neither a GPU nor the backend # package -- it is arithmetic over the problem dimensions advice <- tl_compute_advisor("xgboost", iris, Species ~ ., hyperparams = list(nrounds = 1000)) advice$recommendation print(advice) # Dispatching on a fitted model requires the backend to be installed. # CRAN asks examples to use at most two threads. if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "xgboost", nthread = 2) tl_compute_advisor(model) }
Each fold's model is scored with tl_evaluate, so the
response is read as that function reads it: a transformed left-hand
side on its own scale, and classes against the fold model's.
tl_cv(data, formula, method, folds = 5, metrics = NULL, transform = NULL, ...)tl_cv(data, formula, method, folds = 5, metrics = NULL, transform = NULL, ...)
data |
Data frame |
formula |
Model formula |
method |
Modeling method |
folds |
Number of cross-validation folds, a whole number between 2
and |
metrics |
Character vector of metrics to compute on each fold,
passed to |
transform |
Optional function for feature engineering that has to
be refitted per fold. It is called with the training rows of each
fold and must return a list with an |
... |
Additional arguments passed to |
A list with two elements:
$foldsA list of per-fold evaluation
tibbles, each with metric and
value columns.
$summaryA tibble with columns
metric, mean, and sd summarizing
performance across folds. A metric undefined on a fold – auc
on a fold holding one class – is NA there and left out
of the mean and sd. So is every metric of a fold none of whose
rows can be scored, with a warning giving the reason. A metric
with no value on any fold has NA mean and sd.
cv <- tl_cv(mtcars, mpg ~ wt + hp, method = "linear", folds = 3) cv$summarycv <- tl_cv(mtcars, mpg ~ wt + hp, method = "linear", folds = 3) cv$summary
Create interactive visualization dashboard for a model
tl_dashboard(model, new_data = NULL, ...)tl_dashboard(model, new_data = NULL, ...)
model |
A tidylearn model object |
new_data |
Optional data frame for evaluation (if NULL, uses training data) |
... |
Additional arguments |
A shinyApp object.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") app <- tl_dashboard(model)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") app <- tl_dashboard(model)
Create pre-defined parameter grids for common models
tl_default_param_grid(method, size = "medium", is_classification = TRUE)tl_default_param_grid(method, size = "medium", is_classification = TRUE)
method |
Model method ("tree", "forest", "boost", "svm", "xgboost", etc.) |
size |
Grid size: "small", "medium", "large" |
is_classification |
Whether the grid is for a classification task
(the default) or a regression one. For regression, an |
A named list of parameter values suitable for passing to
tl_tune_grid or tl_tune_random. Each
element is a numeric or character vector of candidate values for
that hyperparameter, or for "deep"'s hidden_layers a list
of layer-size vectors. The grid is built without the data, so a
"forest" mtry can exceed the number of predictors; the
tuners cap it. "polynomial" tunes degree. The
"xgboost" grids draw on the values tl_tune_xgboost
searches by default, and add nrounds, which that function
chooses by early stopping and tl_tune_grid has to tune.
"linear" and "logistic" have no tuneable
hyperparameter and return an empty list with a warning, as does an
unknown method.
grid <- tl_default_param_grid("tree", size = "small") grid <- tl_default_param_grid("forest", size = "medium") grid <- tl_default_param_grid("svm", is_classification = FALSE)grid <- tl_default_param_grid("tree", size = "small") grid <- tl_default_param_grid("forest", size = "medium") grid <- tl_default_param_grid("svm", is_classification = FALSE)
Detect outliers in the data
tl_detect_outliers( data, variables = NULL, method = "iqr", threshold = NULL, plot = TRUE )tl_detect_outliers( data, variables = NULL, method = "iqr", threshold = NULL, plot = TRUE )
data |
A data frame containing the data |
variables |
Character vector of variables to check for outliers |
method |
Method for outlier detection: "boxplot", "z-score", "cook", "iqr", "mahalanobis" |
threshold |
Threshold for outlier detection |
plot |
Logical; whether to create a plot of outliers |
A list with outlier detection results:
The detection method used (character).
Human-readable method name (character).
The threshold value used (numeric).
Formatted threshold description (character).
A logical matrix (observations x variables),
NA where a value is missing. For "cook" and
"mahalanobis" a row's flags are the same in every column,
and NA when any of its values is missing.
Logical vector indicating if each observation is an
outlier in any variable. Missing flags are ignored, so a row with
no flag at all is FALSE.
List with total, by_variable, and
by_observation counts.
Integer vector of outlier row indices.
A ggplot object, or NULL if
plot = FALSE.
tl_detect_outliers(mtcars, variables = c("mpg", "wt"), method = "iqr")tl_detect_outliers(mtcars, variables = c("mpg", "wt"), method = "iqr")
Create a diagnostic dashboard
tl_diagnostic_dashboard( model, include_influence = TRUE, include_assumptions = TRUE, include_performance = TRUE, arrange_plots = "grid" )tl_diagnostic_dashboard( model, include_influence = TRUE, include_assumptions = TRUE, include_performance = TRUE, arrange_plots = "grid" )
model |
A tidylearn model object whose fit is an |
include_influence |
Logical; whether to include influence diagnostics |
include_assumptions |
Logical; whether to include assumption checks |
include_performance |
Logical; whether to include performance metrics |
arrange_plots |
Layout arrangement (e.g., "grid", "row", "column") |
A grid.arrange object (a
grob) containing the arranged diagnostic plots.
if (requireNamespace("gridExtra")) { model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_diagnostic_dashboard(model) }if (requireNamespace("gridExtra")) { model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_diagnostic_dashboard(model) }
Scores a supervised model's predictions against the observed response.
tl_evaluate(object, new_data = NULL, metrics = NULL, ...)tl_evaluate(object, new_data = NULL, metrics = NULL, ...)
object |
A tidylearn model object |
new_data |
Optional new data for evaluation (if NULL, uses training data) |
metrics |
Character vector of metrics to compute. If |
... |
Additional arguments passed to |
The observed response is the formula's left-hand side evaluated on the
scored rows, so a model of log(mpg) is scored against
log(mpg), the scale it predicts on. For classification, the
observed classes are read against the classes the model was trained on,
whose second is the positive class; rows of a class the model never saw
are left out with a warning. Rows missing the response or a prediction
are dropped. tl_calc_classification_metrics describes
"auc" and "pr_auc" when the scored rows hold a single
class or lack one.
With no row left to score – new_data has no rows, or every row
is dropped – it is an error of class tidylearn_no_scored_rows,
naming the reason. tl_cv catches that class and leaves the
fold out.
A tibble with columns metric (character)
and value (numeric), containing one row per requested metric,
and for "auc" on more than two classes an auc_<class>
row per class as well. An unsupervised model has no response to score
against, so it is an error: calc_validation_metrics and
tidy_silhouette assess a clustering.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_evaluate(model) tl_evaluate(model, metrics = c("rmse", "mape"))model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_evaluate(model) tl_evaluate(model, metrics = c("rmse", "mape"))
EDA combining unsupervised learning techniques to understand data structure before modeling
tl_explore(data, response = NULL, max_components = 5, k_range = 2:6)tl_explore(data, response = NULL, max_components = 5, k_range = 2:6)
data |
A data frame |
response |
Optional response variable for colored visualizations |
max_components |
Maximum number of PCA components to keep (default: 5), or fewer if the data has fewer numeric columns |
k_range |
Range of k values for clustering (default: 2:6). Each is a whole number from 2 to one less than the number of rows, the range a silhouette is defined over. |
A list with class "tidylearn_eda" containing:
The original data frame.
The response variable name, or NULL.
The fitted PCA model, keeping the first
max_components components, as
prcomp(rank. = max_components) would.
List with optimal cluster count results.
The fitted k-means model.
The fitted hierarchical clustering model.
List with n_obs, n_vars,
n_components (the number kept), and best_k.
eda <- tl_explore(iris, response = "Species") plot(eda)eda <- tl_explore(iris, response = "Species") plot(eda)
Get the best model from a pipeline
tl_get_best_model(pipeline)tl_get_best_model(pipeline)
pipeline |
A tidylearn pipeline object with results |
The best tidylearn_model object from the pipeline,
selected by the metric specified in evaluation$best_metric.
The model was fitted on the preprocessed training data, so under the
default preprocessing it expects predictors imputed and standardised
the same way. Predict through tl_predict_pipeline, which
replays that preprocessing on raw rows; predict() on the model
itself reads raw values as if they were already standardised, and
returns wrong predictions without a warning.
pipe <- tl_pipeline(iris, Species ~ ., models = list(tree = list(method = "tree")), evaluation = list(metrics = "accuracy", validation = "cv", cv_folds = 2, best_metric = "accuracy")) pipe <- tl_run_pipeline(pipe, verbose = FALSE) best <- tl_get_best_model(pipe) best$spec$method # Predict through the pipeline, which preprocesses the new rows first tl_predict_pipeline(pipe, iris[c(1, 51, 101), ])pipe <- tl_pipeline(iris, Species ~ ., models = list(tree = list(method = "tree")), evaluation = list(metrics = "accuracy", validation = "cv", cv_folds = 2, best_metric = "accuracy")) pipe <- tl_run_pipeline(pipe, verbose = FALSE) best <- tl_get_best_model(pipe) best$spec$method # Predict through the pipeline, which preprocesses the new rows first tl_predict_pipeline(pipe, iris[c(1, 51, 101), ])
Calculate influence measures for a linear model
tl_influence_measures( model, threshold_cook = NULL, threshold_leverage = NULL, threshold_dffits = NULL )tl_influence_measures( model, threshold_cook = NULL, threshold_leverage = NULL, threshold_dffits = NULL )
model |
A tidylearn model object |
threshold_cook |
Cook's distance threshold (default: 4/n) |
threshold_leverage |
Leverage threshold (default: 2*(p+1)/n) |
threshold_dffits |
DFFITS threshold (default: 2*sqrt((p+1)/n)) |
A data frame with one row per observation containing influence
measures: cooks_distance, leverage, dffits,
std_residual, stud_residual, boolean flags for each
threshold (is_cook_influential, is_leverage_influential,
is_dffits_influential, is_outlier), per-coefficient
dfbetas_* columns, and an overall is_influential flag.
Threshold values are stored as attributes.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_influence_measures(model)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_influence_measures(model)
Calculate partial effects based on a model with interactions
tl_interaction_effects(model, var, by_var, at_values = NULL, intervals = TRUE)tl_interaction_effects(model, var, by_var, at_values = NULL, intervals = TRUE)
model |
A tidylearn model object |
var |
Variable to calculate effects for |
by_var |
Variable to calculate effects by (interaction variable) |
at_values |
Named list of values at which to hold other variables. A variable not named is held at its median if it is numeric, and otherwise at its most frequent value, a tie going to the earlier level. |
intervals |
Logical; whether to add 95\
|
For numeric var: a list with effects (data frame of
predicted values across the variable range for each value of
by_var) and slopes (data frame with the slope of
var at each value of by_var). For categorical
var: a data frame of predicted values at each factor level for
each level of by_var. A numeric by_var is evaluated at its
quartiles; quartiles that tie are evaluated once, with a label naming
each quartile they stand for, such as "Q0/Q25". A factor,
character or logical variable is categorical, and is evaluated at the
values the data holds: a level no row uses, and a missing value, are
left out. A factor is returned as a factor with the model's levels.
fit, lower, upper and slope are on the
response scale whatever intervals is set to. For a
classification model that is the predicted probability of the second
class, so the response must have two classes. slope is the slope
of a straight line fitted to fit across the range of var,
so for a non-linear link it is an average rate of change over that
range. A model variable named fit, by_value or
by_label, or with intervals lower or upper, would
be overwritten by these columns and is refused.
slopes$slope_se is the standard error of a straight line fitted
to the prediction grid, not the sampling uncertainty of the marginal
effect. For a linear model the grid is exactly linear in var,
so this is near zero by construction and should not be read as a
precise estimate. Use summary(model$fit) for inference on the
interaction coefficient itself.
model <- tl_model(mtcars, mpg ~ wt * hp, method = "linear") # How the effect of weight changes across horsepower effects <- tl_interaction_effects(model, var = "wt", by_var = "hp") head(effects$effects) effects$slopes # slopes$slope_se describes the fitted grid, not the sampling # uncertainty of the marginal effect -- for that, read the coefficient summary(model$fit)$coefficients["wt:hp", ]model <- tl_model(mtcars, mpg ~ wt * hp, method = "linear") # How the effect of weight changes across horsepower effects <- tl_interaction_effects(model, var = "wt", by_var = "hp") head(effects$effects) effects$slopes # slopes$slope_se describes the fitted grid, not the sampling # uncertainty of the marginal effect -- for that, read the coefficient summary(model$fit)$coefficients["wt:hp", ]
Load a pipeline from disk
tl_load_pipeline(file)tl_load_pipeline(file)
file |
Path to the pipeline file |
A tidylearn_pipeline object previously saved with
tl_save_pipeline.
pipe <- tl_pipeline(iris, Species ~ .) f <- tempfile(fileext = ".rds") tl_save_pipeline(pipe, f) pipe2 <- tl_load_pipeline(f)pipe <- tl_pipeline(iris, Species ~ .) f <- tempfile(fileext = ".rds") tl_save_pipeline(pipe, f) pipe2 <- tl_load_pipeline(f)
Unified interface for creating machine learning models by wrapping established R packages. This function dispatches to the appropriate underlying package based on the method.
tl_model(data, formula = NULL, method = "linear", ..., compute = "cpu")tl_model(data, formula = NULL, method = "linear", ..., compute = "cpu")
data |
A data frame containing the training data |
formula |
A formula specifying the model. For
unsupervised methods, use |
method |
The modeling method. Supervised: "linear"
(stats::lm), "polynomial" (stats::lm on polynomial terms),
"logistic" (stats::glm), "tree" (rpart),
"forest" (randomForest), "boost" (gbm),
"ridge"/"lasso"/"elastic_net" (glmnet), "svm" (e1071),
"nn" (nnet), "deep" (keras3), "xgboost" (xgboost).
The method and the response have to agree, and a mismatch is an
error rather than a meaningless fit: |
... |
Arguments for the method: see the Method arguments section. Anything else is passed to the underlying model function. |
compute |
Compute tier for the fit. One of |
The wrapped packages include: stats (lm, glm, prcomp, kmeans, hclust), glmnet, randomForest, xgboost, gbm, e1071, nnet, rpart, cluster, and dbscan. The underlying algorithms are unchanged - this function provides a consistent interface and returns tidy output.
For a supervised method, model$fit is the object the wrapped
function returned. An unsupervised method returns tidied components as
well, so the wrapped object sits at model$fit$model and
model$fit is the list holding both.
For classification, the response is reduced to the classes it actually contains: subsetting a data frame keeps every factor level, and a level no row uses would otherwise be reported as a class, given its own (zero) probability column, and counted when deciding whether the problem is binary. The fit is unaffected.
Whether a supervised model is a classification or a regression is
decided by the response the formula computes, so factor(cyl) ~ wt
is a classification even though cyl is numeric. A factor or text
response is a classification. A logical response is a regression for
every method but "logistic" – with "linear", a linear
probability model – so write factor(y) ~ ... to classify it.
A categorical predictor stored as text, as tl_read() returns it,
is made a factor before the fit and stored as one in $data, so
every method treats it as a category. At predict(), categorical
columns in new data are read against the levels seen in training: new
data may hold only some of them, and a level the model was not trained
on is an error that names the column.
A tidylearn_model object (S3) containing the fitted model
($fit, or $fit$model for an unsupervised method),
model specification ($spec), and training data
($data). update() and step() on $fit
refit on the training rows and weights, whatever is in the calling
environment. The object also inherits from a method-specific class
(e.g., tidylearn_linear) and a paradigm class
(tidylearn_supervised or tidylearn_unsupervised).
Arguments in ... are passed to the function the method wraps,
except for these, which tidylearn takes itself:
"polynomial"degree (default 2). Each numeric main
effect is replaced by poly(term, degree, raw = TRUE) and the
result fitted with lm(). A numeric term is one that computes a
numeric vector, such as wt or log(wt), or a one-column
matrix, such as scale(wt). One that is also part of an
interaction keeps its own term and gains I(x^2) up to
I(x^degree), so the interaction is coded as written. Factor
and other non-numeric terms, interactions, I() terms, bases
such as poly() or a spline's, the response as written, an
offset() and a removed intercept are kept as they are.
"ridge", "lasso", "elastic_net"
alpha, glmnet's mixing parameter (by default 0, 1 and 0.5);
lambda, a single penalty to fit at, a sequence of penalties for
glmnet::cv.glmnet() to choose from, or NULL (the
default) to let it choose its own; and cv_folds (default 5),
the number of folds for that cross-validation, which takes the place
of glmnet's nfolds. predict() uses the lambda.1se
penalty. tidylearn sets x, y, family and
nfolds itself and refuses them, along with any argument glmnet
does not take.
"svm"tune (default FALSE) and
tune_folds (default 5), to choose cost by
cross-validation before the fit, with gamma for a non-linear
kernel and degree for a polynomial one.
"deep"hidden_layers, activation,
dropout, epochs, batch_size,
validation_split and learning_rate.
"pca"scale and center (both TRUE).
"mds"mds_method, the variant:
"classical" (the default, stats::cmdscale()),
"metric" or "nonmetric" (smacof), or "sammon"
or "kruskal" (MASS). k, or its alias ndim, is
the number of dimensions (default 2).
"kmeans", "pam", "clara"
k, the
number of clusters (default 3); for "pam", metric
as well (default "euclidean").
"hclust"hclust_method, the linkage:
"average" (the default), "ward.D", "ward.D2",
"single", "complete", "mcquitty",
"median" or "centroid"; and distance (default
"euclidean").
"dbscan"eps (default 0.5), minPts
(default 5) and distance (default "euclidean").
Some defaults differ from the wrapped function's: "forest" computes
importance (importance = TRUE), "boost" grows trees of
interaction.depth = 3, and "nn" fits size = 5
hidden units with trace = FALSE.
weights and subset take values, one per row of
data (such as weights = data$w), not column names. A
subset is applied before the fit, and $data holds only
the rows it selects. Case weights are applied by every supervised method
except "svm" and "deep", which refuse them. An offset,
written as offset() in the formula, is applied by
"linear", "polynomial" and "logistic"; the other
methods refuse one, because their predict() would not add it
back.
# Classification -> wraps randomForest::randomForest() model <- tl_model(iris, Species ~ ., method = "forest") model$fit # Access the raw randomForest object # Regression -> wraps stats::lm() model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") model$fit # Access the raw lm object # PCA -> wraps stats::prcomp() model <- tl_model(iris, ~ ., method = "pca") model$fit$model # The raw prcomp object, alongside tidied components # Clustering -> wraps stats::kmeans() model <- tl_model(iris, method = "kmeans", k = 3) model$fit$model # The raw kmeans object# Classification -> wraps randomForest::randomForest() model <- tl_model(iris, Species ~ ., method = "forest") model$fit # Access the raw randomForest object # Regression -> wraps stats::lm() model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") model$fit # Access the raw lm object # PCA -> wraps stats::prcomp() model <- tl_model(iris, ~ ., method = "pca") model$fit$model # The raw prcomp object, alongside tidied components # Clustering -> wraps stats::kmeans() model <- tl_model(iris, method = "kmeans", k = 3) model$fit$model # The raw kmeans object
Create a modeling pipeline
tl_pipeline( data, formula, preprocessing = NULL, models = NULL, evaluation = NULL, ... )tl_pipeline( data, formula, preprocessing = NULL, models = NULL, evaluation = NULL, ... )
data |
A data frame containing the data |
formula |
A formula specifying the model |
preprocessing |
A named list of preprocessing switches, each
|
models |
A list of models to train |
evaluation |
A list of evaluation criteria |
... |
Not used. Anything passed here is an error, so a misspelt
argument such as |
A tidylearn_pipeline object (S3 list) with components
$formula, $data, $preprocessing,
$models, $evaluation, and $results
(initially NULL; populated after tl_run_pipeline).
pipe <- tl_pipeline(iris, Species ~ ., models = list(tree = list(method = "tree"))) print(pipe)pipe <- tl_pipeline(iris, Species ~ ., models = list(tree = list(method = "tree"))) print(pipe)
Plot comparison of cross-validation results
tl_plot_cv_comparison(cv_results, metrics = NULL)tl_plot_cv_comparison(cv_results, metrics = NULL)
cv_results |
Results from tl_compare_cv function |
metrics |
Character vector of metrics to plot (if NULL, plots all metrics) |
A ggplot object showing boxplots of
cross-validation metric distributions for each model.
m1 <- tl_model(mtcars, mpg ~ wt, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") cv <- tl_compare_cv(mtcars, list(simple = m1, full = m2), folds = 3) tl_plot_cv_comparison(cv)m1 <- tl_model(mtcars, mpg ~ wt, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") cv <- tl_compare_cv(mtcars, list(simple = m1, full = m2), folds = 3) tl_plot_cv_comparison(cv)
Plot cross-validation results
tl_plot_cv_results(cv_results, metrics = NULL)tl_plot_cv_results(cv_results, metrics = NULL)
cv_results |
Cross-validation results from tl_cv function |
metrics |
Character vector of metrics to plot (if NULL, plots all metrics) |
A ggplot object.
cv <- tl_cv(mtcars, mpg ~ wt + hp, method = "linear", folds = 5) tl_plot_cv_results(cv) # One metric rather than every one the folds scored tl_plot_cv_results(cv, metrics = "rmse")cv <- tl_cv(mtcars, mpg ~ wt + hp, method = "linear", folds = 5) tl_plot_cv_results(cv) # One metric rather than every one the folds scored tl_plot_cv_results(cv, metrics = "rmse")
Plot deep learning model architecture
tl_plot_deep_architecture(model, ...)tl_plot_deep_architecture(model, ...)
model |
A tidylearn deep learning model object |
... |
Additional arguments passed to the |
NULL, invisibly. Called for its side effect: keras draws
the architecture diagram on the current graphics device, or writes it
to to_file. keras renders it through the Python packages
pydot and graphviz, and errors saying so when they are
not installed.
## Not run: if (requireNamespace("keras3", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "deep", epochs = 5) tl_plot_deep_architecture(model) } ## End(Not run)## Not run: if (requireNamespace("keras3", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "deep", epochs = 5) tl_plot_deep_architecture(model) } ## End(Not run)
Plot deep learning model training history
tl_plot_deep_history(model, metrics = c("loss", "val_loss"), ...)tl_plot_deep_history(model, metrics = c("loss", "val_loss"), ...)
model |
A tidylearn deep learning model object |
metrics |
Which metrics to plot (default: c("loss", "val_loss")) |
... |
Additional arguments |
A ggplot object.
## Not run: if (requireNamespace("keras3", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "deep", epochs = 5) tl_plot_deep_history(model) } ## End(Not run)## Not run: if (requireNamespace("keras3", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "deep", epochs = 5) tl_plot_deep_history(model) } ## End(Not run)
Plot gain chart for a classification model
tl_plot_gain(model, new_data = NULL, bins = 10, ...)tl_plot_gain(model, new_data = NULL, bins = 10, ...)
model |
A tidylearn classification model object |
new_data |
Optional data frame for evaluation (if NULL, uses training data) |
bins |
Number of bins for grouping predictions (default: 10) |
... |
Additional arguments |
A ggplot object.
iris_bin <- iris[iris$Species != "setosa", ] iris_bin$Species <- factor(iris_bin$Species) model <- tl_model(iris_bin, Species ~ ., method = "logistic") tl_plot_gain(model)iris_bin <- iris[iris$Species != "setosa", ] iris_bin$Species <- factor(iris_bin$Species) model <- tl_model(iris_bin, Species ~ ., method = "logistic") tl_plot_gain(model)
Each model's importance is rescaled on its own: its largest value
becomes 100, or, when no value is positive (a forest's permutation
importance can be negative throughout), its largest magnitude becomes
-100. A factor predictor
appears once, under its own name, for every model: the largest of its
design columns stands for it where a method ranks those columns
separately (ridge, lasso, elastic net and xgboost). A predictor a model
was given but did not use scores zero for that model; one it was never
given has no bar for it. Tree, forest and boost models are given the
variables of an interaction such as wt:hp rather than the
interaction itself, so it has no bar for them. Features are ranked on
their mean importance over the models that were given them.
tl_plot_importance_comparison(..., top_n = 10, names = NULL)tl_plot_importance_comparison(..., top_n = 10, names = NULL)
... |
tidylearn model objects to compare |
top_n |
Number of top features to display (default: 10) |
names |
Optional character vector of model names, one unique name per model |
A ggplot object.
m1 <- tl_model(iris, Sepal.Length ~ ., method = "forest") m2 <- tl_model(iris, Sepal.Length ~ ., method = "boost") tl_plot_importance_comparison(m1, m2, names = c("Forest", "Boost"))m1 <- tl_model(iris, Sepal.Length ~ ., method = "forest") m2 <- tl_model(iris, Sepal.Length ~ ., method = "boost") tl_plot_importance_comparison(m1, m2, names = c("Forest", "Boost"))
Plot variable importance for a regularized model
tl_plot_importance_regularized(model, lambda = "1se", top_n = 20, ...)tl_plot_importance_regularized(model, lambda = "1se", top_n = 20, ...)
model |
A tidylearn regularized model object |
lambda |
Which lambda to use: "1se" (default), "min", or a numeric penalty within the fitted path |
top_n |
Number of top features to display (default: 20) |
... |
Additional arguments |
A ggplot object.
model <- tl_model(mtcars, mpg ~ ., method = "lasso") tl_plot_importance_regularized(model)model <- tl_model(mtcars, mpg ~ ., method = "lasso") tl_plot_importance_regularized(model)
Plot influence diagnostics
tl_plot_influence( model, plot_type = "cook", threshold_cook = NULL, threshold_leverage = NULL, threshold_dffits = NULL, n_labels = 3, label_size = 3 )tl_plot_influence( model, plot_type = "cook", threshold_cook = NULL, threshold_leverage = NULL, threshold_dffits = NULL, n_labels = 3, label_size = 3 )
model |
A tidylearn model object |
plot_type |
Type of influence plot: "cook", "leverage", "index" |
threshold_cook |
Cook's distance threshold (default: 4/n) |
threshold_leverage |
Leverage threshold (default: 2*(p+1)/n) |
threshold_dffits |
DFFITS threshold (default: 2*sqrt((p+1)/n)) |
n_labels |
Number of points to label (default: 3) |
label_size |
Text size for labels (default: 3) |
A ggplot object.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_plot_influence(model, plot_type = "cook")model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_plot_influence(model, plot_type = "cook")
Plot interaction effects
tl_plot_interaction( model, var1, var2, n_points = 100, fixed_values = NULL, confidence = TRUE, ... )tl_plot_interaction( model, var1, var2, n_points = 100, fixed_values = NULL, confidence = TRUE, ... )
model |
A tidylearn model object |
var1 |
First variable in the interaction |
var2 |
Second variable in the interaction |
n_points |
Number of points to use for continuous variables |
fixed_values |
Named list of values for other variables in the model. A variable not named is held at its median if it is numeric, and otherwise at its most frequent value, a tie going to the earlier level. |
confidence |
Logical; whether to show a 95\
band is drawn when one variable is numeric and the other categorical,
and needs a model whose underlying fit is an |
... |
Additional arguments to pass to predict() |
A ggplot object. Two numeric variables are
drawn as a filled contour of the prediction; a numeric and a categorical
variable as one line per category; two categorical variables as dodged
bars. A factor, character or logical variable is categorical, and only
the values the data holds are drawn. For a classification model the
prediction is the probability of the second class, so the response
must have two classes. The plot's data holds the prediction in a column
named prediction, and a band in .lower and .upper,
so a model variable with one of those names is refused.
model <- tl_model(mtcars, mpg ~ wt * hp, method = "linear") # Two numeric variables are drawn as a filled contour over both ranges tl_plot_interaction(model, var1 = "wt", var2 = "hp") # A numeric by categorical interaction is drawn as one line per level, # each with a confidence band am_model <- tl_model(transform(mtcars, am = factor(am)), mpg ~ wt * am, method = "linear") tl_plot_interaction(am_model, var1 = "wt", var2 = "am") # Coarser grid, no band tl_plot_interaction(am_model, var1 = "wt", var2 = "am", n_points = 20, confidence = FALSE)model <- tl_model(mtcars, mpg ~ wt * hp, method = "linear") # Two numeric variables are drawn as a filled contour over both ranges tl_plot_interaction(model, var1 = "wt", var2 = "hp") # A numeric by categorical interaction is drawn as one line per level, # each with a confidence band am_model <- tl_model(transform(mtcars, am = factor(am)), mpg ~ wt * am, method = "linear") tl_plot_interaction(am_model, var1 = "wt", var2 = "am") # Coarser grid, no band tl_plot_interaction(am_model, var1 = "wt", var2 = "am", n_points = 20, confidence = FALSE)
Create confidence and prediction interval plots
tl_plot_intervals(model, new_data = NULL, level = 0.95, ...)tl_plot_intervals(model, new_data = NULL, level = 0.95, ...)
model |
A tidylearn regression model object |
new_data |
Optional data frame for prediction (if NULL, uses training data) |
level |
Confidence level (default: 0.95) |
... |
Additional arguments |
A ggplot object.
model <- tl_model(mtcars, mpg ~ wt, method = "linear") tl_plot_intervals(model)model <- tl_model(mtcars, mpg ~ wt, method = "linear") tl_plot_intervals(model)
Plot lift chart for a classification model
tl_plot_lift(model, new_data = NULL, bins = 10, ...)tl_plot_lift(model, new_data = NULL, bins = 10, ...)
model |
A tidylearn classification model object |
new_data |
Optional data frame for evaluation (if NULL, uses training data) |
bins |
Number of bins for grouping predictions (default: 10) |
... |
Additional arguments |
A ggplot object.
iris_bin <- iris[iris$Species != "setosa", ] iris_bin$Species <- factor(iris_bin$Species) model <- tl_model(iris_bin, Species ~ ., method = "logistic") tl_plot_lift(model)iris_bin <- iris[iris$Species != "setosa", ] iris_bin$Species <- factor(iris_bin$Species) model <- tl_model(iris_bin, Species ~ ., method = "logistic") tl_plot_lift(model)
Plot model comparison
tl_plot_model_comparison(..., new_data = NULL, metrics = NULL, names = NULL)tl_plot_model_comparison(..., new_data = NULL, metrics = NULL, names = NULL)
... |
tidylearn model objects to compare |
new_data |
Optional data frame for evaluation. If NULL, the models
are scored on their training data, which they must share: models
fitted on different data are an error asking for |
metrics |
Character vector of metrics to compute |
names |
Optional character vector of model names |
A ggplot object.
m1 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "lasso") tl_plot_model_comparison(m1, m2, names = c("Linear", "Lasso"))m1 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "lasso") tl_plot_model_comparison(m1, m2, names = c("Linear", "Lasso"))
Plot neural network architecture
tl_plot_nn_architecture(model, ...)tl_plot_nn_architecture(model, ...)
model |
A tidylearn neural network model object |
... |
Additional arguments |
The return value of plotnet, called for
its side effect of drawing the network diagram, or NULL if the
NeuralNetTools package is not installed.
if (requireNamespace("NeuralNetTools", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "nn", size = 3) tl_plot_nn_architecture(model) }if (requireNamespace("NeuralNetTools", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "nn", size = 3) tl_plot_nn_architecture(model) }
Draws the size-by-decay grid as a heatmap of cross-validated error.
tl_plot_nn_tuning(model, ...)tl_plot_nn_tuning(model, ...)
model |
The list returned by |
... |
Additional arguments |
A ggplot object.
tuned <- tl_tune_nn(iris, Species ~ ., is_classification = TRUE, sizes = c(2, 5), decays = c(0, 0.01), folds = 3) # The tuning result itself, not tuned$model tl_plot_nn_tuning(tuned)tuned <- tl_tune_nn(iris, Species ~ ., is_classification = TRUE, sizes = c(2, 5), decays = c(0, 0.01), folds = 3) # The tuning result itself, not tuned$model tl_plot_nn_tuning(tuned)
Plot partial dependence for tree-based models
tl_plot_partial_dependence(model, var, n.pts = 20, ...)tl_plot_partial_dependence(model, var, n.pts = 20, ...)
model |
A tidylearn tree-based model object |
var |
Variable name to plot |
n.pts |
Number of points for continuous variables (default: 20) |
... |
Additional arguments |
A ggplot object. Its data has a
var_value column and the mean prediction over the model's
training rows, y, at each value. For classification, y
is a mean class probability and a class column says which
class: the positive class (the second level) alone for a two-class
model, and every class, one line each, for more.
model <- tl_model(mtcars, mpg ~ ., method = "forest") tl_plot_partial_dependence(model, var = "wt")model <- tl_model(mtcars, mpg ~ ., method = "forest") tl_plot_partial_dependence(model, var = "wt")
Shows the cross-validation error as a function of lambda for ridge, lasso, or elastic net models fitted with cv.glmnet.
tl_plot_regularization_cv(model, ...)tl_plot_regularization_cv(model, ...)
model |
A tidylearn regularized model object (ridge, lasso, or elastic_net) |
... |
Additional arguments (currently unused) |
A ggplot object.
model <- tl_model(mtcars, mpg ~ ., method = "ridge") tl_plot_regularization_cv(model)model <- tl_model(mtcars, mpg ~ ., method = "ridge") tl_plot_regularization_cv(model)
Plot regularization path for a regularized model
tl_plot_regularization_path(model, label_n = 5, ...)tl_plot_regularization_path(model, label_n = 5, ...)
model |
A tidylearn regularized model object |
label_n |
Number of top features to label (default: 5) |
... |
Additional arguments |
A ggplot object.
model <- tl_model(mtcars, mpg ~ ., method = "lasso") tl_plot_regularization_path(model)model <- tl_model(mtcars, mpg ~ ., method = "lasso") tl_plot_regularization_path(model)
Plot SVM decision boundary
tl_plot_svm_boundary(model, x_var = NULL, y_var = NULL, grid_size = 100, ...)tl_plot_svm_boundary(model, x_var = NULL, y_var = NULL, grid_size = 100, ...)
model |
A tidylearn SVM model object |
x_var |
Name of the x-axis variable. Defaults to the first numeric predictor in the model's formula. |
y_var |
Name of the y-axis variable. Defaults to the next numeric predictor in the model's formula. |
grid_size |
Number of points in each dimension for the grid (default: 100) |
... |
Additional arguments |
A ggplot object. The other predictors are
held at their mean, or their most frequent level. A two-class model
fitted with probabilities also gets the 0.5 probability contour.
if (requireNamespace("e1071", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "svm") tl_plot_svm_boundary(model, x_var = "Sepal.Length", y_var = "Sepal.Width") }if (requireNamespace("e1071", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "svm") tl_plot_svm_boundary(model, x_var = "Sepal.Length", y_var = "Sepal.Width") }
Plot SVM tuning results
tl_plot_svm_tuning(model, ...)tl_plot_svm_tuning(model, ...)
model |
A tidylearn SVM model object |
... |
Additional arguments |
A ggplot object.
if (requireNamespace("e1071", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "svm", kernel = "linear", tune = TRUE, tune_folds = 2) tl_plot_svm_tuning(model) }if (requireNamespace("e1071", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "svm", kernel = "linear", tune = TRUE, tune_folds = 2) tl_plot_svm_tuning(model) }
Plot a decision tree
tl_plot_tree(model, ...)tl_plot_tree(model, ...)
model |
A tidylearn tree model object |
... |
Additional arguments to pass to rpart.plot() |
The return value of rpart.plot, called
for its side effect of drawing the tree.
model <- tl_model(iris, Species ~ ., method = "tree") tl_plot_tree(model)model <- tl_model(iris, Species ~ ., method = "tree") tl_plot_tree(model)
Plot hyperparameter tuning results
tl_plot_tuning_results( model, top_n = 5, param1 = NULL, param2 = NULL, plot_type = "scatter" )tl_plot_tuning_results( model, top_n = 5, param1 = NULL, param2 = NULL, plot_type = "scatter" )
model |
A tidylearn model object with tuning results |
top_n |
Number of top parameter sets to highlight |
param1 |
First parameter to plot (for 2D grid or scatter plots) |
param2 |
Second parameter to plot (for 2D grid or scatter plots) |
plot_type |
Type of plot: "scatter", "grid", "parallel", "importance" |
A parameter whose candidates are not single values, such as
hidden_layers = list(c(10), c(20, 10)) or a parms list,
is drawn as a categorical one, each value labelled as the verbose
messages print it. The importance of a numeric parameter is the
absolute correlation of its values with the score; that of a
categorical one is eta squared from a one-way ANOVA of the score,
and 0 when the sets that were scored all share one value.
A ggplot object.
model <- tl_tune_grid(iris, Species ~ ., method = "tree", param_grid = list(cp = c(0.01, 0.1), minsplit = c(10, 20)), folds = 2, verbose = FALSE) tl_plot_tuning_results(model)model <- tl_tune_grid(iris, Species ~ ., method = "tree", param_grid = list(cp = c(0.01, 0.1), minsplit = c(10, 20)), folds = 2, verbose = FALSE) tl_plot_tuning_results(model)
Plot feature importance for an XGBoost model
tl_plot_xgboost_importance(model, top_n = 10, importance_type = "gain", ...)tl_plot_xgboost_importance(model, top_n = 10, importance_type = "gain", ...)
model |
A tidylearn XGBoost model object |
top_n |
Number of top features to display (default: 10) |
importance_type |
Type of importance: "gain" (default), "cover",
"frequency" or "weight", read from the matching column of
|
... |
Additional arguments passed to |
A ggplot object. Its data holds the
top_n features and their importance, relative to the
most important feature's.
if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(mtcars, mpg ~ ., method = "xgboost", nthread = 2) tl_plot_xgboost_importance(model) }if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(mtcars, mpg ~ ., method = "xgboost", nthread = 2) tl_plot_xgboost_importance(model) }
Plot SHAP dependence for a specific feature
tl_plot_xgboost_shap_dependence( model, feature, interaction_feature = NULL, data = NULL, n_samples = 100 )tl_plot_xgboost_shap_dependence( model, feature, interaction_feature = NULL, data = NULL, n_samples = 100 )
model |
A tidylearn XGBoost model object |
feature |
Feature name to plot |
interaction_feature |
Feature to use for coloring (default: NULL) |
data |
Data for SHAP value calculation (default: NULL, uses training data) |
n_samples |
Number of samples to use (default: 100, NULL for all) |
A ggplot object. A multiclass model is
drawn one panel per class, from that class's SHAP values.
if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "xgboost", nrounds = 10, nthread = 2) # One panel per class tl_plot_xgboost_shap_dependence(model, feature = "Petal.Length") # Colour the points by a second feature to read the interaction tl_plot_xgboost_shap_dependence(model, feature = "Petal.Length", interaction_feature = "Petal.Width") }if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "xgboost", nrounds = 10, nthread = 2) # One panel per class tl_plot_xgboost_shap_dependence(model, feature = "Petal.Length") # Colour the points by a second feature to read the interaction tl_plot_xgboost_shap_dependence(model, feature = "Petal.Length", interaction_feature = "Petal.Width") }
Plot SHAP summary for XGBoost model
tl_plot_xgboost_shap_summary(model, data = NULL, top_n = 10, n_samples = 100)tl_plot_xgboost_shap_summary(model, data = NULL, top_n = 10, n_samples = 100)
model |
A tidylearn XGBoost model object |
data |
Data for SHAP value calculation (default: NULL, uses training data) |
top_n |
Number of top features to display (default: 10) |
n_samples |
Number of samples to use (default: 100, NULL for all) |
A ggplot object. Features are ranked by
mean absolute SHAP value; a multiclass model is drawn one panel per
class.
if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(mtcars, mpg ~ ., method = "xgboost", nthread = 2) tl_plot_xgboost_shap_summary(model, n_samples = 20) }if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(mtcars, mpg ~ ., method = "xgboost", nthread = 2) tl_plot_xgboost_shap_summary(model, n_samples = 20) }
Plot XGBoost tree visualization
tl_plot_xgboost_tree(model, tree_index = 0, ...)tl_plot_xgboost_tree(model, tree_index = 0, ...)
model |
A tidylearn XGBoost model object |
tree_index |
Index of the tree to plot, counting the first tree as 0 (default: 0). A multiclass model grows one tree per class in each round. |
... |
Additional arguments passed to |
The return value of xgb.plot.tree, a
tree diagram rendered via the DiagrammeR package.
if (requireNamespace("xgboost", quietly = TRUE) && requireNamespace("DiagrammeR", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "xgboost", nrounds = 10, nthread = 2) # tree_index is zero-based, so this is the first tree tl_plot_xgboost_tree(model, tree_index = 0) }if (requireNamespace("xgboost", quietly = TRUE) && requireNamespace("DiagrammeR", quietly = TRUE)) { model <- tl_model(iris, Species ~ ., method = "xgboost", nrounds = 10, nthread = 2) # tree_index is zero-based, so this is the first tree tl_plot_xgboost_tree(model, tree_index = 0) }
Make predictions using a pipeline
tl_predict_pipeline( pipeline, new_data, type = "response", model_name = NULL, ... )tl_predict_pipeline( pipeline, new_data, type = "response", model_name = NULL, ... )
pipeline |
A tidylearn pipeline object with results |
new_data |
A data frame containing the new data |
type |
Type of prediction (default: "response") |
model_name |
Name of model to use (if NULL, uses the best model) |
... |
Additional arguments passed to predict |
A tibble with a .pred column containing
predictions from the selected (or best) pipeline model, after
applying the same preprocessing steps used during training.
train <- iris[c(1:40, 51:90, 101:140), ] test <- iris[c(41:50, 91:100, 141:150), ] pipe <- tl_pipeline(train, Species ~ ., models = list( tree = list(method = "tree"), forest = list(method = "forest", ntree = 100) ), evaluation = list(validation = "cv", cv_folds = 3)) pipe <- tl_run_pipeline(pipe, verbose = FALSE) # The best model, with the preprocessing learned on the training rows tl_predict_pipeline(pipe, test) # Or a named candidate instead of the winner tl_predict_pipeline(pipe, test, model_name = "tree")train <- iris[c(1:40, 51:90, 101:140), ] test <- iris[c(41:50, 91:100, 141:150), ] pipe <- tl_pipeline(train, Species ~ ., models = list( tree = list(method = "tree"), forest = list(method = "forest", ntree = 100) ), evaluation = list(validation = "cv", cv_folds = 3)) pipe <- tl_run_pipeline(pipe, verbose = FALSE) # The best model, with the preprocessing learned on the training rows tl_predict_pipeline(pipe, test) # Or a named candidate instead of the winner tl_predict_pipeline(pipe, test, model_name = "tree")
Unified preprocessing functions that work with both supervised and unsupervised workflows Prepare Data for Machine Learning
tl_prepare_data( data, formula = NULL, impute_method = "mean", scale_method = "standardize", encode_categorical = TRUE, remove_zero_variance = TRUE, remove_correlated = FALSE, correlation_cutoff = 0.95 )tl_prepare_data( data, formula = NULL, impute_method = "mean", scale_method = "standardize", encode_categorical = TRUE, remove_zero_variance = TRUE, remove_correlated = FALSE, correlation_cutoff = 0.95 )
data |
A data frame. A grouped tibble is prepared as a whole and returned ungrouped. |
formula |
Optional two-sided formula (for supervised learning),
whose response must be a column of |
impute_method |
Method for imputing a missing numeric value: "mean", "median" or "mode". A missing categorical value is always filled with the column's most frequent value. |
scale_method |
Scaling method: "standardize",
"normalize", "robust", "none". A column whose spread is zero or not
finite, such as one holding an |
encode_categorical |
Whether to encode categorical variables (default: TRUE) |
remove_zero_variance |
Remove zero-variance features (default: TRUE) |
remove_correlated |
Remove highly correlated features (default: FALSE) |
correlation_cutoff |
Correlation threshold for removal, greater than 0 and at most 1 (default: 0.95) |
Preprocessing pipeline covering imputation, scaling, encoding, and feature engineering
The statistics are learned from, and applied to, the data passed in.
Preparing a whole dataset and then splitting it lets the test rows
shape the imputation values and scaling their own scores are measured
against. To evaluate a model, split first, or use
tl_pipeline, which learns its preprocessing inside each
resampling fold.
A list with components:
dataThe processed data frame.
original_dataThe original unprocessed data frame.
preprocessing_stepsA record of each step applied (imputation values, encoding maps, scaling parameters, etc.). It is for inspection: no function applies it to new data.
formulaThe formula passed in (or NULL).
processed <- tl_prepare_data(iris, Species ~ ., scale_method = "standardize") model <- tl_model(processed$data, Species ~ ., method = "tree")processed <- tl_prepare_data(iris, Species ~ ., scale_method = "standardize") model <- tl_model(processed$data, Species ~ ., method = "tree")
Auto-detects the data format from the file extension or source pattern and
dispatches to the appropriate reader. All readers
return a tidylearn_data object, which is a
tibble subclass carrying metadata about the data
source.
tl_read(source, ..., format = NULL, .quiet = FALSE)tl_read(source, ..., format = NULL, .quiet = FALSE)
source |
A file path or |
... |
Additional arguments passed to the format-specific reader. |
format |
Optional explicit format override.
One of |
.quiet |
Logical. If |
When source is a character vector of multiple paths, each file is read
and row-bound into a single result with a source_file column
giving each file's path below the deepest folder they share. When
source is a directory path, it is equivalent to calling
tl_read_dir(). When source is a local .zip file, it
is equivalent to calling tl_read_zip().
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
# The format is detected from the extension csv <- tempfile(fileext = ".csv") write.csv(mtcars, csv, row.names = FALSE) tl_read(csv) # Several files are row-bound, with a source_file column naming each jan <- tempfile(fileext = ".csv") feb <- tempfile(fileext = ".csv") write.csv(mtcars[1:16, ], jan, row.names = FALSE) write.csv(mtcars[17:32, ], feb, row.names = FALSE) both <- tl_read(c(jan, feb), .quiet = TRUE) table(both$source_file) # A .txt file is read as CSV unless told otherwise txt <- tempfile(fileext = ".txt") write.table(mtcars, txt, sep = "\t", row.names = FALSE) tl_read(txt, format = "tsv", .quiet = TRUE) unlink(c(csv, jan, feb, txt))# The format is detected from the extension csv <- tempfile(fileext = ".csv") write.csv(mtcars, csv, row.names = FALSE) tl_read(csv) # Several files are row-bound, with a source_file column naming each jan <- tempfile(fileext = ".csv") feb <- tempfile(fileext = ".csv") write.csv(mtcars[1:16, ], jan, row.names = FALSE) write.csv(mtcars[17:32, ], feb, row.names = FALSE) both <- tl_read(c(jan, feb), .quiet = TRUE) table(both$source_file) # A .txt file is read as CSV unless told otherwise txt <- tempfile(fileext = ".txt") write.table(mtcars, txt, sep = "\t", row.names = FALSE) tl_read(txt, format = "tsv", .quiet = TRUE) unlink(c(csv, jan, feb, txt))
Executes a SQL query against Google BigQuery and returns the result as a
tidylearn_data object. Requires the bigrquery package and
valid Google Cloud authentication.
tl_read_bigquery(project, query, dataset = NULL, ...)tl_read_bigquery(project, query, dataset = NULL, ...)
project |
Google Cloud project ID, or a
|
query |
A SQL query string (Standard SQL). |
dataset |
Optional default dataset for unqualified table names,
in |
... |
Additional arguments passed to
|
A tidylearn_data object containing the query results.
## Not run: # Needs Google Cloud credentials tl_read_bigquery( project = "my-project", query = "SELECT * FROM `my_dataset.my_table` LIMIT 1000" ) # Unqualified table names resolve against `dataset` tl_read_bigquery( project = "my-project", query = "SELECT * FROM my_table LIMIT 1000", dataset = "my_dataset" ) ## End(Not run)## Not run: # Needs Google Cloud credentials tl_read_bigquery( project = "my-project", query = "SELECT * FROM `my_dataset.my_table` LIMIT 1000" ) # Unqualified table names resolve against `dataset` tl_read_bigquery( project = "my-project", query = "SELECT * FROM my_table LIMIT 1000", dataset = "my_dataset" ) ## End(Not run)
Reads a CSV file into a tidylearn_data object. Uses readr when
available for faster parsing, with a base R fallback.
tl_read_csv(path, ...)tl_read_csv(path, ...)
path |
Path to a CSV file. |
... |
Additional arguments passed to |
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
path <- tempfile(fileext = ".csv") write.csv(mtcars, path, row.names = FALSE) tl_read_csv(path) unlink(path)path <- tempfile(fileext = ".csv") write.csv(mtcars, path, row.names = FALSE) tl_read_csv(path) unlink(path)
Executes a SQL query against an existing DBI connection and returns
the result as a tidylearn_data object. The connection is not closed
by this function — the caller is responsible for managing the connection
lifecycle.
tl_read_db(conn, query, ...)tl_read_db(conn, query, ...)
conn |
A DBI connection object (e.g., from
|
query |
A SQL query string. |
... |
Additional arguments passed to |
A tidylearn_data object containing the query results.
# RSQLite imports DBI, so both are available here conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") DBI::dbWriteTable(conn, "cars", mtcars) tl_read_db(conn, "SELECT mpg, cyl, hp FROM cars WHERE cyl = 6") DBI::dbDisconnect(conn)# RSQLite imports DBI, so both are available here conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") DBI::dbWriteTable(conn, "cars", mtcars) tl_read_db(conn, "SELECT mpg, cyl, hp FROM cars WHERE cyl = 6") DBI::dbDisconnect(conn)
Scans a directory for files matching a pattern or format, reads each one,
and row-binds them into a single tidylearn_data object with a
source_file column identifying the origin of each row.
tl_read_dir( path, pattern = NULL, format = NULL, recursive = FALSE, .quiet = FALSE, ... )tl_read_dir( path, pattern = NULL, format = NULL, recursive = FALSE, .quiet = FALSE, ... )
path |
Path to a directory. |
pattern |
Optional regex pattern to filter file names (e.g.,
|
format |
File format to read. If |
recursive |
Logical. Should subdirectories be scanned? Default is
|
.quiet |
Suppress messages. Default is |
... |
Additional arguments passed to the format-specific reader. |
A tidylearn_data object with an additional
source_file column giving each row's file as a path below
path, such as "2024/sales.csv".
dir <- tempfile("sales_") dir.create(file.path(dir, "2024"), recursive = TRUE) write.csv(mtcars[1:16, ], file.path(dir, "jan.csv"), row.names = FALSE) write.csv(mtcars[17:32, ], file.path(dir, "2024", "feb.csv"), row.names = FALSE) # Only the top level unless asked to recurse tl_read_dir(dir, format = "csv") # Files in subfolders are labelled by their path below dir all_months <- tl_read_dir(dir, recursive = TRUE, .quiet = TRUE) table(all_months$source_file) # Or select files by a regular expression tl_read_dir(dir, pattern = "^jan", .quiet = TRUE) unlink(dir, recursive = TRUE)dir <- tempfile("sales_") dir.create(file.path(dir, "2024"), recursive = TRUE) write.csv(mtcars[1:16, ], file.path(dir, "jan.csv"), row.names = FALSE) write.csv(mtcars[17:32, ], file.path(dir, "2024", "feb.csv"), row.names = FALSE) # Only the top level unless asked to recurse tl_read_dir(dir, format = "csv") # Files in subfolders are labelled by their path below dir all_months <- tl_read_dir(dir, recursive = TRUE, .quiet = TRUE) table(all_months$source_file) # Or select files by a regular expression tl_read_dir(dir, pattern = "^jan", .quiet = TRUE) unlink(dir, recursive = TRUE)
Reads an Excel file (.xls, .xlsx, or .xlsm) into a
tidylearn_data object. Requires the readxl package.
tl_read_excel(path, sheet = 1, ...)tl_read_excel(path, sheet = 1, ...)
path |
Path to an Excel file. |
sheet |
Sheet to read. Either a string (the name of a sheet) or an integer (the position of the sheet). Defaults to the first sheet. |
... |
Additional arguments passed to |
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
# readxl ships a workbook with one data set per sheet path <- readxl::readxl_example("datasets.xlsx") tl_read_excel(path) tl_read_excel(path, sheet = "mtcars")# readxl ships a workbook with one data set per sheet path <- readxl::readxl_example("datasets.xlsx") tl_read_excel(path) tl_read_excel(path, sheet = "mtcars")
Downloads a raw file from a GitHub repository and reads it into a
tidylearn_data object. Accepts either a full GitHub URL or a
owner/repo shorthand with a file path.
tl_read_github(source, path = NULL, ref = "main", ..., trust_rds = FALSE)tl_read_github(source, path = NULL, ref = "main", ..., trust_rds = FALSE)
source |
A GitHub URL or |
path |
Path to the file within the repository (required when
|
ref |
Branch, tag, or commit SHA. Default is |
... |
Additional arguments passed to the format-specific reader. |
trust_rds |
Logical. Read an |
A tidylearn_data object containing the downloaded data.
An .rds or .rdata file is rebuilt with readRDS() or
load(), which recreate whatever R objects the file describes.
Read them only from a repository you trust, on any version of R.
## Not run: # Downloads over the network tl_read_github("user/repo", path = "data/file.csv") tl_read_github("https://github.com/user/repo/blob/main/data/file.csv") ## End(Not run)## Not run: # Downloads over the network tl_read_github("user/repo", path = "data/file.csv") tl_read_github("https://github.com/user/repo/blob/main/data/file.csv") ## End(Not run)
Reads a JSON file into a tidylearn_data object. Expects the JSON to
represent tabular data (array of objects or similar). A file with the
.ndjson extension is read as newline-delimited JSON, one record
per line, and so is a .json file that does not parse as a single
document but does as one record per line. Requires the jsonlite
package.
tl_read_json(path, flatten = TRUE, ...)tl_read_json(path, flatten = TRUE, ...)
path |
Path to a JSON file. |
flatten |
Logical. Automatically flatten nested data frames? Default is
|
... |
Additional arguments passed to |
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
path <- tempfile(fileext = ".json") jsonlite::write_json(mtcars, path) tl_read_json(path) # Newline-delimited JSON: one record per line lines <- tempfile(fileext = ".ndjson") jsonlite::stream_out(mtcars, file(lines), verbose = FALSE) tl_read_json(lines) unlink(c(path, lines))path <- tempfile(fileext = ".json") jsonlite::write_json(mtcars, path) tl_read_json(path) # Newline-delimited JSON: one record per line lines <- tempfile(fileext = ".ndjson") jsonlite::stream_out(mtcars, file(lines), verbose = FALSE) tl_read_json(lines) unlink(c(path, lines))
Downloads a dataset file from Kaggle using the Kaggle CLI and reads it into
a tidylearn_data object. Requires the Kaggle CLI to be installed and
configured (pip install kaggle).
tl_read_kaggle(source, file = NULL, dest = NULL, type = "dataset", ...)tl_read_kaggle(source, file = NULL, dest = NULL, type = "dataset", ...)
source |
A Kaggle dataset slug (e.g., |
file |
The specific file to read from the dataset, as a path within
it; a file the CLI saved under its base name is found too. If
|
dest |
Directory to keep the download in. The default is a fresh
per-dataset directory under |
type |
Either |
... |
Additional arguments passed to the format-specific reader. |
A tidylearn_data object containing the downloaded data.
## Not run: # Needs the Kaggle CLI and Kaggle credentials tl_read_kaggle("zillow/zecon", file = "Zip_time_series.csv") tl_read_kaggle("titanic", file = "train.csv", type = "competition") tl_read_kaggle("https://www.kaggle.com/competitions/titanic/data", file = "train.csv") ## End(Not run)## Not run: # Needs the Kaggle CLI and Kaggle credentials tl_read_kaggle("zillow/zecon", file = "Zip_time_series.csv") tl_read_kaggle("titanic", file = "train.csv", type = "competition") tl_read_kaggle("https://www.kaggle.com/competitions/titanic/data", file = "train.csv") ## End(Not run)
Connects to a MySQL or MariaDB database, executes a SQL query, and returns
the result as a tidylearn_data object. Accepts either a connection
string or individual connection parameters. Requires DBI and
RMariaDB.
tl_read_mysql( dsn, query, dbname = NULL, user = NULL, password = NULL, port = 3306, ... )tl_read_mysql( dsn, query, dbname = NULL, user = NULL, password = NULL, port = 3306, ... )
dsn |
A MySQL connection string (e.g.,
|
query |
A SQL query string. |
dbname |
Database name (if not in |
user |
Username (if not in |
password |
Password (if not in |
port |
Port number (if not in |
... |
Additional arguments passed to |
A tidylearn_data object containing the query results.
## Not run: # Needs a running MySQL or MariaDB server tl_read_mysql( dsn = "localhost", query = "SELECT * FROM my_table", dbname = "mydb", user = "myuser", password = Sys.getenv("MYSQL_PWD") ) ## End(Not run)## Not run: # Needs a running MySQL or MariaDB server tl_read_mysql( dsn = "localhost", query = "SELECT * FROM my_table", dbname = "mydb", user = "myuser", password = Sys.getenv("MYSQL_PWD") ) ## End(Not run)
Reads a Parquet file into a tidylearn_data object. Requires the
nanoparquet package.
tl_read_parquet(path, ...)tl_read_parquet(path, ...)
path |
Path to a Parquet file. |
... |
Additional arguments passed to |
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
path <- tempfile(fileext = ".parquet") nanoparquet::write_parquet(mtcars, path) tl_read_parquet(path) unlink(path)path <- tempfile(fileext = ".parquet") nanoparquet::write_parquet(mtcars, path) tl_read_parquet(path) unlink(path)
Connects to a PostgreSQL database, executes a SQL query, and returns the
result as a tidylearn_data object. Accepts either a connection string
or individual connection parameters. Requires DBI and RPostgres.
tl_read_postgres( dsn, query, dbname = NULL, user = NULL, password = NULL, port = 5432, ... )tl_read_postgres( dsn, query, dbname = NULL, user = NULL, password = NULL, port = 5432, ... )
dsn |
A PostgreSQL connection string (e.g.,
|
query |
A SQL query string. |
dbname |
Database name (if not in |
user |
Username (if not in |
password |
Password (if not in |
port |
Port number. Default is 5432. |
... |
Additional arguments passed to |
A tidylearn_data object containing the query results.
## Not run: # Needs a running PostgreSQL server tl_read_postgres( dsn = "localhost", query = "SELECT * FROM my_table", dbname = "mydb", user = "myuser", password = Sys.getenv("PGPASSWORD") ) # The same connection as a URL, with the password still kept out of it tl_read_postgres( "postgres://myuser@localhost:5432/mydb?sslmode=require", query = "SELECT * FROM my_table", password = Sys.getenv("PGPASSWORD") ) ## End(Not run)## Not run: # Needs a running PostgreSQL server tl_read_postgres( dsn = "localhost", query = "SELECT * FROM my_table", dbname = "mydb", user = "myuser", password = Sys.getenv("PGPASSWORD") ) # The same connection as a URL, with the password still kept out of it tl_read_postgres( "postgres://myuser@localhost:5432/mydb?sslmode=require", query = "SELECT * FROM my_table", password = Sys.getenv("PGPASSWORD") ) ## End(Not run)
Reads an RData (.rdata or .rda) file
into a tidylearn_data object. Since RData files
can contain multiple objects, use the name
argument to specify which object to extract.
If name is NULL and
the file contains exactly one data frame, it is returned automatically.
tl_read_rdata(path, name = NULL, ...)tl_read_rdata(path, name = NULL, ...)
path |
Path to an RData file. |
name |
Optional name of the object to extract from the RData file. If
|
... |
Currently unused. |
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
load() rebuilds whatever R objects the file describes, so read
only files from a source you trust. On R before 4.4.0 a crafted file
can run code as it is read (CVE-2024-27322). The remote readers
tl_read_github() and tl_read_s3() refuse .rds
and .rdata files on those versions unless
trust_rds = TRUE.
path <- tempfile(fileext = ".rdata") cars <- mtcars flowers <- iris save(cars, flowers, file = path) # With more than one data frame in the file, name the one to read tl_read_rdata(path, name = "flowers") unlink(path)path <- tempfile(fileext = ".rdata") cars <- mtcars flowers <- iris save(cars, flowers, file = path) # With more than one data frame in the file, name the one to read tl_read_rdata(path, name = "flowers") unlink(path)
Reads an RDS file into a tidylearn_data object. Uses base R
readRDS() — no additional packages required.
tl_read_rds(path)tl_read_rds(path)
path |
Path to an RDS file. |
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
readRDS() rebuilds whatever R objects the file describes, so
read only files from a source you trust. On R before 4.4.0 a crafted
file can run code as it is read (CVE-2024-27322). The remote readers
tl_read_github() and tl_read_s3() refuse .rds
and .rdata files on those versions unless
trust_rds = TRUE.
path <- tempfile(fileext = ".rds") saveRDS(mtcars, path) tl_read_rds(path) unlink(path)path <- tempfile(fileext = ".rds") saveRDS(mtcars, path) tl_read_rds(path) unlink(path)
Downloads a file from an S3 bucket and reads it into a tidylearn_data
object. The file format is auto-detected from the key's extension, or can be
specified explicitly. Requires the paws.storage package and valid AWS
credentials.
tl_read_s3(source, format = NULL, region = NULL, ..., trust_rds = FALSE)tl_read_s3(source, format = NULL, region = NULL, ..., trust_rds = FALSE)
source |
An S3 URI (e.g., |
format |
Optional format override for the downloaded file. If
|
region |
AWS region. If |
... |
Additional arguments passed to the format-specific reader. |
trust_rds |
Logical. Read an |
A tidylearn_data object containing the downloaded data.
An .rds or .rdata object is rebuilt with readRDS()
or load(), which recreate whatever R objects the file describes.
Read them only from a bucket you trust, on any version of R.
## Not run: # Needs AWS credentials tl_read_s3("s3://my-bucket/data/sales.csv") tl_read_s3("s3://my-bucket/data/results.parquet", region = "eu-west-1") ## End(Not run)## Not run: # Needs AWS credentials tl_read_s3("s3://my-bucket/data/sales.csv") tl_read_s3("s3://my-bucket/data/results.parquet", region = "eu-west-1") ## End(Not run)
Opens a SQLite database file, executes a SQL query, and returns the result
as a tidylearn_data object. The connection is automatically closed
when done. Requires DBI and RSQLite.
tl_read_sqlite(path, query, ...)tl_read_sqlite(path, query, ...)
path |
Path to a SQLite database file ( |
query |
A SQL query string. |
... |
Additional arguments passed to |
A tidylearn_data object containing the query results.
# RSQLite imports DBI, so both are available here path <- tempfile(fileext = ".sqlite") conn <- DBI::dbConnect(RSQLite::SQLite(), path) DBI::dbWriteTable(conn, "cars", mtcars) DBI::dbDisconnect(conn) tl_read_sqlite(path, "SELECT mpg, cyl, hp FROM cars WHERE cyl = 6") unlink(path)# RSQLite imports DBI, so both are available here path <- tempfile(fileext = ".sqlite") conn <- DBI::dbConnect(RSQLite::SQLite(), path) DBI::dbWriteTable(conn, "cars", mtcars) DBI::dbDisconnect(conn) tl_read_sqlite(path, "SELECT mpg, cyl, hp FROM cars WHERE cyl = 6") unlink(path)
Reads a tab-separated file into a
tidylearn_data object. Uses readr when
available for faster parsing, with a base R fallback.
tl_read_tsv(path, ...)tl_read_tsv(path, ...)
path |
Path to a TSV file. |
... |
Additional arguments passed to |
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp.
path <- tempfile(fileext = ".tsv") write.table(mtcars, path, sep = "\t", row.names = FALSE) tl_read_tsv(path) unlink(path)path <- tempfile(fileext = ".tsv") write.table(mtcars, path, sep = "\t", row.names = FALSE) tl_read_tsv(path) unlink(path)
Extracts a zip archive to a temporary directory and reads the contents.
If the archive contains a single data file, it is read directly. If
multiple data files are found, they are row-bound with a source_file
column. Use the file argument to select a specific file from
the archive.
tl_read_zip(path, file = NULL, format = NULL, .quiet = FALSE, ...)tl_read_zip(path, file = NULL, format = NULL, .quiet = FALSE, ...)
path |
Path to a zip file. |
file |
Optional name of a specific file within the archive to read:
its path within the archive ( |
format |
Optional format override for the file(s) inside the
archive. Without |
.quiet |
Suppress messages. Default is |
... |
Additional arguments passed to the format-specific reader. |
An archive with a member whose name could reach outside the extraction
directory – an absolute path, a drive letter on Windows, or any
.. component – is refused before anything is extracted.
A tidylearn_data object (a tibble subclass)
with attributes tl_source, tl_format, and
tl_timestamp. The archive is extracted to a temporary directory
that is cleaned up automatically. If multiple data files are found,
a source_file column gives each row's member as its path within
the archive.
# readr ships a zip archive holding one CSV archive <- readr::readr_example("mtcars.csv.zip") tl_read_zip(archive) # Name a member to read just that one tl_read_zip(archive, file = "mtcars.csv", .quiet = TRUE)# readr ships a zip archive holding one CSV archive <- readr::readr_example("mtcars.csv.zip") tl_read_zip(archive) # Name a member to read just that one tl_read_zip(archive, file = "mtcars.csv", .quiet = TRUE)
Use PCA, MDS, or other dimensionality reduction as a preprocessing step for supervised learning. This can improve model performance and interpretability.
tl_reduce_dimensions( data, response = NULL, method = "pca", n_components = NULL, ... )tl_reduce_dimensions( data, response = NULL, method = "pca", n_components = NULL, ... )
data |
A data frame |
response |
Response variable name (will be preserved) |
method |
Dimensionality reduction method: "pca" or "mds" |
n_components |
Number of components to retain, at most the number
the method computes: one per numeric column for PCA, and |
... |
Additional arguments for the dimensionality reduction method |
A list with components:
The transformed data frame with reduced-dimension columns and the response variable (if provided).
The fitted tidylearn dimensionality reduction model.
The original input data frame.
The response variable name, or NULL.
# Reduce dimensions before classification reduced <- tl_reduce_dimensions( iris, response = "Species", method = "pca", n_components = 3 ) model <- tl_model(reduced$data, Species ~ ., method = "tree")# Reduce dimensions before classification reduced <- tl_reduce_dimensions( iris, response = "Species", method = "pca", n_components = 3 ) model <- tl_model(reduced$data, Species ~ ., method = "tree")
Run a tidylearn pipeline
tl_run_pipeline(pipeline, verbose = TRUE)tl_run_pipeline(pipeline, verbose = TRUE)
pipeline |
A tidylearn pipeline object |
verbose |
Logical; whether to print progress |
The input tidylearn_pipeline object with its
$results component populated. Results include
$processed_data (the training data after preprocessing),
$preprocessing_stats (the medians, modes, centres and scales
learned from the training data, replayed by
tl_predict_pipeline), $model_results (a named
list of per-model fits and metrics), $best_model_name,
$best_model (the winning tidylearn_model), and
$metric_values.
The training data is every row when validation = "cv": each
model is scored across the folds and then refitted on all of them.
With validation = "split" it is the training split alone, since
the models kept are the ones fitted there and scored on the test rows.
pipe <- tl_pipeline(iris, Species ~ ., models = list(tree = list(method = "tree")), evaluation = list(metrics = "accuracy", validation = "cv", cv_folds = 2, best_metric = "accuracy")) pipe <- tl_run_pipeline(pipe, verbose = FALSE)pipe <- tl_pipeline(iris, Species ~ ., models = list(tree = list(method = "tree")), evaluation = list(metrics = "accuracy", validation = "cv", cv_folds = 2, best_metric = "accuracy")) pipe <- tl_run_pipeline(pipe, verbose = FALSE)
Save a pipeline to disk
tl_save_pipeline(pipeline, file)tl_save_pipeline(pipeline, file)
pipeline |
A tidylearn pipeline object |
file |
Path to save the pipeline |
Called for its side effect of saving to disk; returns
NULL invisibly.
pipe <- tl_pipeline(iris, Species ~ .) tl_save_pipeline(pipe, tempfile(fileext = ".rds"))pipe <- tl_pipeline(iris, Species ~ .) tl_save_pipeline(pipe, tempfile(fileext = ".rds"))
Train a supervised model with limited labels by first clustering the data and propagating labels within clusters.
tl_semisupervised( data, formula, labeled_indices, cluster_method = "kmeans", supervised_method = "tree", ..., cluster_args = list() )tl_semisupervised( data, formula, labeled_indices, cluster_method = "kmeans", supervised_method = "tree", ..., cluster_args = list() )
data |
A data frame |
formula |
Model formula. The response must be categorical: a
factor, character or logical column, or an expression that computes a
factor or character vector from a column, such as |
labeled_indices |
Indices of labeled observations |
cluster_method |
Clustering method for label propagation:
|
supervised_method |
Supervised learning method for the final
model (default: |
... |
Additional arguments for the supervised model |
cluster_args |
A named list of arguments for the clustering step,
such as |
Labels are propagated by majority vote within each cluster, so the response must be categorical. The rows are clustered on the predictors the formula names, into as many clusters as the labelled rows hold classes. A labelled row whose label is missing takes no part in the vote. Rows in a cluster where no labelled observation carries a label have no label to take, and labelled rows whose own label is missing have none either; both are left out of training, with a warning giving the counts. The pseudo-labels keep the response's level order, so the second level stays the positive class.
A tidylearn model object with additional class
"tidylearn_semisupervised", trained on pseudo-labeled data. The
model includes a semisupervised_info element with
labeled_indices, cluster_model, label_mapping,
and n_unlabelled_dropped, the number of rows left out for
having no label to train on.
# Use only 10% of labels labeled_idx <- sample(nrow(iris), size = 15) model <- tl_semisupervised(iris, Species ~ ., labeled_indices = labeled_idx, cluster_method = "kmeans", supervised_method = "tree" )# Use only 10% of labels labeled_idx <- sample(nrow(iris), size = 15) model <- tl_semisupervised(iris, Species ~ ., labeled_indices = labeled_idx, cluster_method = "kmeans", supervised_method = "tree" )
Split data into train and test sets
tl_split(data, prop = 0.8, stratify = NULL, seed = NULL)tl_split(data, prop = 0.8, stratify = NULL, seed = NULL)
data |
A data frame |
prop |
Proportion for training set (default: 0.8) |
stratify |
Column name for stratified splitting. Each stratum is
split at |
seed |
Random seed for reproducibility |
A list with two elements:
$trainA data frame containing the training subset.
$testA data frame containing the test subset.
A split that leaves the test set empty, as a single row does, warns.
split_data <- tl_split(iris, prop = 0.7, stratify = "Species") train <- split_data$train test <- split_data$testsplit_data <- tl_split(iris, prop = 0.7, stratify = "Species") train <- split_data$train test <- split_data$test
Perform stepwise selection on a linear model
tl_step_selection( data, formula, direction = "backward", criterion = "AIC", trace = FALSE, steps = 1000, ... )tl_step_selection( data, formula, direction = "backward", criterion = "AIC", trace = FALSE, steps = 1000, ... )
data |
A data frame containing the training data |
formula |
A formula specifying the initial model |
direction |
Direction of stepwise selection: "forward", "backward", or "both" |
criterion |
Criterion for selection: "AIC" or "BIC" |
trace |
Logical; whether to print progress |
steps |
Maximum number of steps to take |
... |
Additional arguments to pass to step() |
Every candidate model is fitted on the same rows: those with no
missing value in any variable of formula. When that leaves out
rows a candidate could otherwise use, a message gives the count, and
the fit records the rows in its na.action, as lm()
records the rows it drops itself.
Forward and both-direction selection start from a model holding the
intercept, or none if formula removes it with - 1, and
any offset() terms in formula. Neither is ever dropped.
A tidylearn_model object of class
tidylearn_linear wrapping the selected lm
model. Access the underlying model via $fit and the selected
formula via $spec$formula. $data is the data passed in.
model <- tl_step_selection(mtcars, mpg ~ ., direction = "backward") summary(model)model <- tl_step_selection(mtcars, mpg ~ ., direction = "backward") summary(model)
Create cluster-specific supervised models for heterogeneous data
tl_stratified_models( data, formula, cluster_method = "kmeans", k = 3, supervised_method = "tree", ..., cluster_args = list() )tl_stratified_models( data, formula, cluster_method = "kmeans", k = 3, supervised_method = "tree", ..., cluster_args = list() )
data |
A data frame |
formula |
Model formula |
cluster_method |
Clustering method: |
k |
Number of clusters |
supervised_method |
Supervised learning method (default:
|
... |
Additional arguments for the supervised models |
cluster_args |
A named list of arguments for the clustering step,
such as |
The rows are clustered on the predictors the formula names, and a model is fitted to each cluster. A cluster whose rows all hold one class has nothing for a classifier to separate, so it gets no model and its rows are predicted as that class.
A list with class "tidylearn_stratified" containing:
The fitted clustering model.
The training rows' cluster assignments.
Named list of tidylearn models, one per cluster that holds more than one class.
Named character vector giving, for each cluster whose rows all hold one class, that class.
The model formula.
The original training data.
models <- tl_stratified_models(mtcars, mpg ~ ., cluster_method = "kmeans", k = 3, supervised_method = "linear")models <- tl_stratified_models(mtcars, mpg ~ ., cluster_method = "kmeans", k = 3, supervised_method = "linear")
Dispatches to the appropriate table function based on model type and requested table type. Requires the gt package.
tl_table(model, type = "auto", ...)tl_table(model, type = "auto", ...)
model |
A tidylearn model object |
type |
Table type (default: "auto"). For supervised models: "metrics", "coefficients", "confusion", "importance". For unsupervised models: "variance", "loadings", "clusters". MDS models are not supported. |
... |
Additional arguments passed to the underlying table function |
A gt table object.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_table(model) tl_table(model, type = "coefficients")model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_table(model) tl_table(model, type = "coefficients")
Produces a styled gt table showing cluster sizes and mean feature values for the columns the clustering used. Supports kmeans, pam, clara, dbscan, and hclust models.
tl_table_clusters(model, k = 3, digits = 2, ...)tl_table_clusters(model, k = 3, digits = 2, ...)
model |
A tidylearn clustering model object |
k |
For hclust models, the number of clusters to cut (default: 3) |
digits |
Number of decimal places (default: 2) |
... |
Additional arguments (currently unused) |
A gt table object.
model <- tl_model(iris[, 1:4], method = "kmeans", k = 3) tl_table_clusters(model)model <- tl_model(iris[, 1:4], method = "kmeans", k = 3) tl_table_clusters(model)
Produces a styled gt table of model coefficients. Supports linear,
polynomial, logistic, ridge, lasso, and elastic net models. The numbers
come from tl_coefficients, which returns them as a tibble
if you would rather format them yourself.
tl_table_coefficients( model, lambda = "1se", digits = 4, conf_int = FALSE, level = 0.95, exponentiate = FALSE, ... )tl_table_coefficients( model, lambda = "1se", digits = 4, conf_int = FALSE, level = 0.95, exponentiate = FALSE, ... )
model |
A tidylearn model object |
lambda |
For regularised models: |
digits |
Number of decimal places (default: 4) |
conf_int |
Whether to add a confidence interval (default:
|
level |
Confidence level for the interval (default: 0.95) |
exponentiate |
Whether to report odds ratios rather than log odds
(default: |
... |
Additional arguments (currently unused) |
A gt table object.
tl_coefficients for the underlying tibble.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_table_coefficients(model) tl_table_coefficients(model, conf_int = TRUE)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_table_coefficients(model) tl_table_coefficients(model, conf_int = TRUE)
Evaluates multiple tidylearn models and presents the results side-by-side in a styled gt table.
tl_table_comparison(..., new_data = NULL, names = NULL, digits = 4)tl_table_comparison(..., new_data = NULL, names = NULL, digits = 4)
... |
tidylearn model objects to compare |
new_data |
Optional test data for evaluation. If NULL, the models
are scored on their training data, which they must share: models
fitted on different data are an error asking for |
names |
Optional character vector of model names |
digits |
Number of decimal places (default: 4) |
A gt table object. Its source note counts the
rows scored, per model when the models scored different rows.
m1 <- tl_model(mtcars, mpg ~ ., method = "linear") m2 <- tl_model(mtcars, mpg ~ ., method = "lasso") tl_table_comparison(m1, m2, names = c("Linear", "Lasso"))m1 <- tl_model(mtcars, mpg ~ ., method = "linear") m2 <- tl_model(mtcars, mpg ~ ., method = "lasso") tl_table_comparison(m1, m2, names = c("Linear", "Lasso"))
Produces a styled gt confusion matrix with correct predictions highlighted. Only available for classification models.
tl_table_confusion(model, new_data = NULL, ...)tl_table_confusion(model, new_data = NULL, ...)
model |
A tidylearn classification model |
new_data |
Optional test data. If NULL, uses training data. |
... |
Additional arguments (currently unused) |
A gt table object, with a row and a column for
each class the model was trained on. Rows of a class the model never
saw are left out, with a warning.
model <- tl_model(iris, Species ~ ., method = "forest") tl_table_confusion(model)model <- tl_model(iris, Species ~ ., method = "forest") tl_table_confusion(model)
Produces a styled gt table of feature importance with a colour gradient. Supports tree-based, regularised, and xgboost models.
tl_table_importance(model, top_n = 20, digits = 2, ...)tl_table_importance(model, top_n = 20, digits = 2, ...)
model |
A tidylearn model object |
top_n |
Maximum number of features to display (default: 20) |
digits |
Number of decimal places (default: 2) |
... |
Additional arguments (currently unused) |
A gt table object.
model <- tl_model(iris, Species ~ ., method = "forest") tl_table_importance(model)model <- tl_model(iris, Species ~ ., method = "forest") tl_table_importance(model)
Produces a styled gt table of variable loadings on each principal component, with a diverging colour scale to highlight strong loadings.
tl_table_loadings(model, n_components = NULL, digits = 3, ...)tl_table_loadings(model, n_components = NULL, digits = 3, ...)
model |
A tidylearn PCA model object |
n_components |
Number of components to show (default: all) |
digits |
Number of decimal places (default: 3) |
... |
Additional arguments (currently unused) |
A gt table object.
model <- tl_model(iris[, 1:4], method = "pca") tl_table_loadings(model)model <- tl_model(iris[, 1:4], method = "pca") tl_table_loadings(model)
Produces a styled gt table of model evaluation metrics from
tl_evaluate.
tl_table_metrics(model, new_data = NULL, digits = 4, ...)tl_table_metrics(model, new_data = NULL, digits = 4, ...)
model |
A tidylearn supervised model object |
new_data |
Optional test data. If NULL, uses training data. |
digits |
Number of decimal places (default: 4) |
... |
Additional arguments passed to |
A gt table object. Its source note counts the
rows scored.
model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_table_metrics(model)model <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") tl_table_metrics(model)
Produces a styled gt table of variance explained by each principal component, with a colour gradient on cumulative variance.
tl_table_variance(model, n_components = NULL, digits = 4, ...)tl_table_variance(model, n_components = NULL, digits = 4, ...)
model |
A tidylearn PCA model object |
n_components |
Maximum number of components to show (default: all) |
digits |
Number of decimal places (default: 4) |
... |
Additional arguments (currently unused) |
A gt table object.
model <- tl_model(iris[, 1:4], method = "pca") tl_table_variance(model)model <- tl_model(iris[, 1:4], method = "pca") tl_table_variance(model)
Test for significant interactions between variables
tl_test_interactions( data, formula, var1 = NULL, var2 = NULL, all_pairs = FALSE, categorical_only = FALSE, numeric_only = FALSE, mixed_only = FALSE, alpha = 0.05 )tl_test_interactions( data, formula, var1 = NULL, var2 = NULL, all_pairs = FALSE, categorical_only = FALSE, numeric_only = FALSE, mixed_only = FALSE, alpha = 0.05 )
data |
A data frame containing the data |
formula |
A formula specifying the base model without interactions,
or a string that parses as one. |
var1 |
First variable to test for interactions |
var2 |
Second variable to test for interactions (if NULL, tests var1 with all others) |
all_pairs |
Logical; whether to test all variable pairs |
categorical_only |
Logical; whether to only test categorical variables |
numeric_only |
Logical; whether to only test numeric variables |
mixed_only |
Logical; whether to only test numeric-categorical pairs |
alpha |
Significance level for interaction tests |
A data frame with one row per tested interaction pair, containing
columns var1, var2, p_value, significant
(logical), delta_r2 (change in R-squared), and
f_statistic, sorted by p_value ascending.
results <- tl_test_interactions(mtcars, mpg ~ wt + hp + cyl, var1 = "wt", var2 = "hp")results <- tl_test_interactions(mtcars, mpg ~ wt + hp + cyl, var1 = "wt", var2 = "hp")
Perform statistical comparison of models using cross-validation
tl_test_model_difference( cv_results, baseline_model = NULL, test = "t.test", metric = NULL )tl_test_model_difference( cv_results, baseline_model = NULL, test = "t.test", metric = NULL )
cv_results |
Results from tl_compare_cv function |
baseline_model |
Name of the model to use as baseline for comparison |
test |
Type of statistical test: |
metric |
Name of the metric to compare |
A data frame with columns metric, model,
baseline, mean_diff, p_value, and
p_adj (Holm-adjusted p-value) containing pairwise
statistical comparisons against the baseline model. Each comparison
pairs the folds both models have a value for, and mean_diff is
the mean of those paired differences. With fewer than two such folds
the p-values are NA, with a warning.
m1 <- tl_model(mtcars, mpg ~ wt, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") cv <- tl_compare_cv(mtcars, list(simple = m1, full = m2), folds = 3) tl_test_model_difference(cv, baseline_model = "simple", metric = "rmse")m1 <- tl_model(mtcars, mpg ~ wt, method = "linear") m2 <- tl_model(mtcars, mpg ~ wt + hp, method = "linear") cv <- tl_compare_cv(mtcars, list(simple = m1, full = m2), folds = 3) tl_test_model_difference(cv, baseline_model = "simple", metric = "rmse")
Use unsupervised pre-training before supervised learning: the predictors the formula names are projected onto their principal components, and the supervised model is fitted on the component scores.
tl_transfer_learning( data, formula, pretrain_method = "pca", supervised_method = "tree", ... )tl_transfer_learning( data, formula, pretrain_method = "pca", supervised_method = "tree", ... )
data |
Training data |
formula |
Model formula, or a string that parses as one. The PCA is fitted on the numeric predictors it names. |
pretrain_method |
Pre-training method. Only |
supervised_method |
Supervised learning method (default:
|
... |
Additional arguments passed to
|
A list with class "tidylearn_transfer" containing:
The fitted dimensionality reduction model.
The fitted supervised tidylearn model.
The model formula.
The supervised learning method used.
model <- tl_transfer_learning(iris, Species ~ ., pretrain_method = "pca", supervised_method = "tree")model <- tl_transfer_learning(iris, Species ~ ., pretrain_method = "pca", supervised_method = "tree")
Tune a deep learning model
tl_tune_deep( data, formula, is_classification = NULL, hidden_layers_options = list(c(32), c(64, 32), c(128, 64, 32)), learning_rates = c(0.01, 0.001, 1e-04), batch_sizes = c(16, 32, 64), epochs = 30, validation_split = 0.2, ... )tl_tune_deep( data, formula, is_classification = NULL, hidden_layers_options = list(c(32), c(64, 32), c(128, 64, 32)), learning_rates = c(0.01, 0.001, 1e-04), batch_sizes = c(16, 32, 64), epochs = 30, validation_split = 0.2, ... )
data |
A data frame containing the training data |
formula |
A formula specifying the model |
is_classification |
Logical indicating if this is a
classification problem. |
|
List of vectors defining hidden layer configurations to try |
|
learning_rates |
Learning rates to try (default: c(0.01, 0.001, 0.0001)) |
batch_sizes |
Batch sizes to try (default: c(16, 32, 64)) |
epochs |
Number of training epochs (default: 30) |
validation_split |
Proportion of the rows held out to score each configuration on, drawn at random (default: 0.2). Every configuration is scored on the same rows. |
... |
Additional arguments passed to keras's fit() for every
configuration; |
A list with elements model (the best configuration refitted
as a tidylearn_model, so predict() and the deep plots take
it; the keras model is at $model$fit$model),
best_hidden_layers (optimal layer configuration),
best_learning_rate, best_batch_size, and
tuning_results (a data frame of all hyperparameter combinations
and their validation losses).
## Not run: if (requireNamespace("keras3", quietly = TRUE)) { result <- tl_tune_deep(iris, Species ~ ., hidden_layers_options = list(c(10), c(10, 5)), learning_rates = c(0.01, 0.001), batch_sizes = c(32), epochs = 5) predict(result$model, iris[1:5, ]) } ## End(Not run)## Not run: if (requireNamespace("keras3", quietly = TRUE)) { result <- tl_tune_deep(iris, Species ~ ., hidden_layers_options = list(c(10), c(10, 5)), learning_rates = c(0.01, 0.001), batch_sizes = c(32), epochs = 5) predict(result$model, iris[1:5, ]) } ## End(Not run)
Tune hyperparameters for a model using grid search
tl_tune_grid( data, formula, method, param_grid, folds = 5, metric = NULL, maximize = NULL, verbose = TRUE, ... )tl_tune_grid( data, formula, method, param_grid, folds = 5, metric = NULL, maximize = NULL, verbose = TRUE, ... )
data |
A data frame containing the training data |
formula |
A formula specifying the model |
method |
The modeling method to tune, one of the supervised methods
|
param_grid |
A named list of candidate values, one element for each
|
folds |
Number of cross-validation folds, a whole number between 2
and |
metric |
Metric to optimize: one of the names
|
maximize |
Logical; whether to maximize (TRUE)
or minimize (FALSE) the metric. |
verbose |
Logical; whether to print progress |
... |
Additional arguments passed to |
A tidylearn model object fitted with the best hyperparameters.
Tuning results are stored as an attribute "tuning_results",
a list containing param_grid, results, best_params,
best_metric, metric, and maximize.
results has one row per evaluated combination: mean_metric
(the mean over the folds that produced a score), n_folds_ok (how
many of the folds did), and a column per parameter. A parameter
with a vector-valued candidate, such as hidden_layers, is a list
column. A fold produces no score when its fit fails, when the metric
is undefined on it – "auc" on a fold holding one class, or
"precision" on one where nothing is predicted positive – or
when none of its rows can be scored, as when every predictor is
missing there, which is warned about.
Only combinations with n_folds_ok equal to folds are
eligible to be best, since a mean over the folds that happened to
succeed is not comparable with a mean over all of them. If no
combination was scored on every fold, the best of those scored on the
most folds is used, with a warning saying whether fits failed or the
metric was undefined. If no combination was scored on any fold, the
function stops.
For method = "forest", an mtry above the number of
predictors is capped at that number, with a warning, and duplicate
combinations that result are evaluated once. Predictors are counted as
the forest is fitted on them, so a column removed with - id is
not one, and a matrix-valued term such as poly(hp, 2) is one per
column.
model <- tl_tune_grid(iris, Species ~ ., method = "tree", param_grid = list(cp = c(0.01, 0.1), minsplit = c(10, 20)), folds = 2, verbose = FALSE)model <- tl_tune_grid(iris, Species ~ ., method = "tree", param_grid = list(cp = c(0.01, 0.1), minsplit = c(10, 20)), folds = 2, verbose = FALSE)
Tune a neural network model
tl_tune_nn( data, formula, is_classification = NULL, sizes = c(1, 2, 5, 10), decays = c(0, 0.001, 0.01, 0.1), folds = 5, ... )tl_tune_nn( data, formula, is_classification = NULL, sizes = c(1, 2, 5, 10), decays = c(0, 0.001, 0.01, 0.1), folds = 5, ... )
data |
A data frame containing the training data |
formula |
A formula specifying the model |
is_classification |
Logical indicating if this is a
classification problem. |
sizes |
Vector of hidden layer sizes to try |
decays |
Vector of weight decay parameters to try |
folds |
Number of cross-validation folds (default: 5) |
... |
Additional arguments to pass to nnet(). |
A list with elements model (the best fitted nnet
model), best_size (optimal hidden-layer size), best_decay
(optimal weight decay), and tuning_results (a data frame of all
parameter combinations and their cross-validated errors: the
misclassification rate for classification, the mean squared error for
regression).
tuned <- tl_tune_nn(iris, Species ~ ., is_classification = TRUE, sizes = c(2, 5), decays = c(0, 0.01), folds = 3) tuned$best_size tuned$best_decay tuned$tuning_results # The grid this searched, drawn as a heatmap tl_plot_nn_tuning(tuned)tuned <- tl_tune_nn(iris, Species ~ ., is_classification = TRUE, sizes = c(2, 5), decays = c(0, 0.01), folds = 3) tuned$best_size tuned$best_decay tuned$tuning_results # The grid this searched, drawn as a heatmap tl_plot_nn_tuning(tuned)
Tune hyperparameters using random search
tl_tune_random( data, formula, method, param_space, n_iter = 10, folds = 5, metric = NULL, maximize = NULL, verbose = TRUE, seed = NULL, ... )tl_tune_random( data, formula, method, param_space, n_iter = 10, folds = 5, metric = NULL, maximize = NULL, verbose = TRUE, seed = NULL, ... )
data |
A data frame containing the training data |
formula |
A formula specifying the model |
method |
The modeling method to tune, one of the supervised methods
|
param_space |
A named list of parameter spaces to sample from, one
element for each
|
n_iter |
Number of random parameter combinations to try, a whole number of at least 1 |
folds |
Number of cross-validation folds, a whole number between 2
and |
metric |
Metric to optimize, as for |
maximize |
Logical; whether to maximize (TRUE)
or minimize (FALSE) the metric. |
verbose |
Logical; whether to print progress |
seed |
Random seed for reproducibility |
... |
Additional arguments passed to |
A tidylearn model object fitted with the best hyperparameters.
Tuning results are stored as an attribute "tuning_results",
a list containing param_space, results, best_params,
best_metric, metric, and maximize.
results has one row per iteration: iteration,
mean_metric, n_folds_ok, and a column per parameter, as
described for tl_tune_grid. The best parameters are chosen
by the same rules, and mtry is capped the same way; duplicate
draws are kept, so there are always n_iter rows.
model <- tl_tune_random(mtcars, mpg ~ ., method = "tree", param_space = list(cp = c(0.01, 0.1), minsplit = c(10, 20)), n_iter = 3, folds = 2, verbose = FALSE)model <- tl_tune_random(mtcars, mpg ~ ., method = "tree", param_space = list(cp = c(0.01, 0.1), minsplit = c(10, 20)), n_iter = 3, folds = 2, verbose = FALSE)
Tune XGBoost hyperparameters
tl_tune_xgboost( data, formula, is_classification = NULL, param_grid = NULL, cv_folds = 5, nrounds = 1000, early_stopping_rounds = 10, verbose = TRUE, ... )tl_tune_xgboost( data, formula, is_classification = NULL, param_grid = NULL, cv_folds = 5, nrounds = 1000, early_stopping_rounds = 10, verbose = TRUE, ... )
data |
A data frame containing the training data |
formula |
A formula specifying the model |
is_classification |
Logical indicating if this is a
classification problem. |
param_grid |
Named list of parameter values to try. NULL (default)
tries every combination of |
cv_folds |
Number of cross-validation folds (default: 5) |
nrounds |
Upper bound on boosting rounds per parameter set (default: 1000). Early stopping normally halts well short of it, so this is a ceiling rather than a target; lower it to cap the search. |
early_stopping_rounds |
Early stopping rounds (default: 10) |
verbose |
Logical indicating whether to print progress (default: TRUE) |
... |
Arguments |
A tidylearn_model object (the refit on full data using the
best hyperparameters, built by tl_model so that it records
them in $spec$args) with an attribute "tuning_results"
containing a list with elements param_grid, results
(per-combination CV output), best_params, best_iteration,
best_score, and minimize.
if (requireNamespace("xgboost", quietly = TRUE)) { # The default grid is every combination of the large xgboost grid # without nrounds -- this many: default_grid <- tl_default_param_grid("xgboost", size = "large") prod(lengths(default_grid[names(default_grid) != "nrounds"])) # Name a smaller one to see it run, and cap nrounds so early stopping # has less ground to cover. nthread = 2 keeps xgboost from taking # every core it is offered. tuned <- tl_tune_xgboost(iris, Species ~ ., param_grid = list(max_depth = c(2, 4)), cv_folds = 3, nrounds = 20, verbose = FALSE, nthread = 2) results <- attr(tuned, "tuning_results") results$best_params results$best_iteration # tuned is an ordinary model, refit on all rows at those settings predict(tuned, iris[1:5, ]) }if (requireNamespace("xgboost", quietly = TRUE)) { # The default grid is every combination of the large xgboost grid # without nrounds -- this many: default_grid <- tl_default_param_grid("xgboost", size = "large") prod(lengths(default_grid[names(default_grid) != "nrounds"])) # Name a smaller one to see it run, and cap nrounds so early stopping # has less ground to cover. nthread = 2 keeps xgboost from taking # every core it is offered. tuned <- tl_tune_xgboost(iris, Species ~ ., param_grid = list(max_depth = c(2, 4)), cv_folds = 3, nrounds = 20, verbose = FALSE, nthread = 2) results <- attr(tuned, "tuning_results") results$best_params results$best_iteration # tuned is an ordinary model, refit on all rows at those settings predict(tuned, iris[1:5, ]) }
Get tidylearn version information
tl_version()tl_version()
A package_version object containing the version number
tl_version()tl_version()
Generate SHAP values for XGBoost model interpretation
tl_xgboost_shap(model, data = NULL, n_samples = 100, trees_idx = NULL)tl_xgboost_shap(model, data = NULL, n_samples = 100, trees_idx = NULL)
model |
A tidylearn XGBoost model object |
data |
Data for SHAP value calculation (default: NULL, uses training data). The response column is not needed. |
n_samples |
Number of samples to use (default: 100, NULL for all) |
trees_idx |
Boosting rounds to include, as a run of consecutive
rounds counted from 1 such as |
A data frame with one column of SHAP values per feature (the
columns of the model's design matrix), a BIAS column and a
row_id column giving the row of the (sampled) data each row
explains. For a multiclass model there is one block of rows per class,
told apart by a class column. The columns of data whose
names are not already taken – the response, and any factor predictor,
whose SHAP columns are named after its levels – are appended for
reference.
if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(mtcars, mpg ~ ., method = "xgboost", nthread = 2) shap <- tl_xgboost_shap(model, n_samples = 20) }if (requireNamespace("xgboost", quietly = TRUE)) { model <- tl_model(mtcars, mpg ~ ., method = "xgboost", nthread = 2) shap <- tl_xgboost_shap(model, n_samples = 20) }
Create visualizations of association rules
visualize_rules(rules_obj, method = "scatter", top_n = 50, ...)visualize_rules(rules_obj, method = "scatter", top_n = 50, ...)
rules_obj |
A tidy_apriori object or an arules rules object. A table of rules is refused: the plots need the rules object. |
method |
Visualization method: "scatter" (default), drawn by
tidylearn, or a method of arulesViz's |
top_n |
Number of rules to visualize, those with the highest lift (default: 50) |
... |
Additional arguments passed to plot() for rules visualization |
For method = "scatter", a ggplot
object. Other methods return what arulesViz's plot()
returns: a ggplot object for "graph", "grouped" and "matrix", and for
"paracoord", which draws with grid, a grid vpPath.
if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) visualize_rules(res, method = "scatter") }if (requireNamespace("arules", quietly = TRUE)) { data("Groceries", package = "arules") res <- tidy_apriori(Groceries, support = 0.001, confidence = 0.5) visualize_rules(res, method = "scatter") }