knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
An R package that produces a tidy output for tidymodels model evaluation!
Sptidy implements a tidy
and augment
function for base R's linear regression and Tidymodel's kmeans clustering to ease model selection and assessment tasks. This package is a simplified reimplementation of the existing tidy
and augment
functions in the Broom package. Sptidy’s family of tidy functions returns a dataframe that summarizes important model information, while the augment function expands the original dataframe to include additional model specific information by observation. This package is meant to complement Sktidy, a Python package that was created to tidy up the scikit-learn package.
The functions that this package currently support include:
tidy_kmeans()
: Returns inertia, cluster location, and number of associated points at the level of clusters in a tidy format.
tidy_lr()
: Returns coefficients and corresponding feature names in a tidy format.
augment_lr()
: Returns predictions and residuals for each point in the training data set in a tidy format.
augment_kmeans()
: Returns assigned cluster and distance from cluster center for the data the kmeans algorithm was fitted with in a tidy format.
Tidymodels is a “meta-package” for modeling and statistical analysis that share the underlying design philosophy, grammar, and data structures of the tidyverse. One of the packages it includes is broom which takes the messy output of built-in functions in R, such as lm, nls, or t.test, and turns them into tidy data frames. The tidy data refers to outputting the results in a data.frame
where each variable has its own column, each observation has its own row, and each value has its own cell. In sptidy
, we implement the functions tidy()
and augment()
for the linear regression model from base R using the function lm()
and the KMeans model from R stats
package using the function kmeans()
.
This package is not yet available on CRAN, but you can install the development version from GitHub with:
devtools::install_github("UBC-MDS/sptidy")
This is a basic example which shows you how to solve a common problem:
knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
When working with linear regression and kmeans clustering in R, it's nice to:
The sptidy package provides tools that make these tasks swift and easy.
This vignette is the package-wide documentation for R package sptidy. This helps those who want to use this package better understand what sptidy does. A full usage demonstration of all functions in this package is included in this vignette.
sptidy provides functions for model evaluation and analysis. These functions work with 2 types of models: lm() and kmeans().
tidy_lr
outputs summary on lm()augment_lr
augments a data frame with predictions and residuals
kmeans clustering
tidy_kmeans
outputs summary on kmeans()augment_kmeans
augments a data frame with cluster assignmentsFirst, let us load sptidy and 2 data set: longley
, iris
for function usage demonstration.
library(sptidy) data(longley) data(iris)
sptidy::tidy_lr() provides a tidy data frame that summarizes a fitted linear regression object lm(). The argument in the function needs to be a fitted lm() object. The output data frame has 4 columns, describing coefficient estimates, standard error, t-statistics and p-values.
my_lr <- lm(Employed~., data = longley) sptidy::tidy_lr(my_lr)
sptidy::augment_lr() augments the data frame with predictions and residuals from a fitted linear regression object lm(). The first argument is the fitted lm() object. The second and third argument refer to the feature data frame and target data frame that are fitted to the lm() object. The output data frame has additional 2 columns, describing predictions and residuals with respect to each observation.
my_lr <- lm(Employed~., data = longley) sptidy::augment_lr(my_lr, longley[1:6], as.data.frame(longley$Employed))
sptidy::tidy_kmeans() provides a tidy data frame that summarizes a fitted kmeans clustering object kmenas(). The first argument is the fitted kmeans() object. The second argument refers to the data that was fitted to the kmeans() object. The output data frame has 3 columns, describing cluster number, cluster center and number of points within each cluster.
data <- iris[,1:3] kclust <- kmeans(data, centers = 3) tidy_kmeans(kclust, data)
sptidy::augment_kmeans() augments the data frame with cluster assignment from a fitted kmeans clustering object kmeans(). The first argument is the fitted kmeans() object. The second argument refers to the data that was fitted to the kmeans() object. The output data frame has additional 1 column, describing cluster assignment with respect to each observation.
data <- iris[,1:3] kclust <- kmeans(data, centers = 3) augment_kmeans(kclust, data)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.