| Title: | Chic and Sleek Functions for Beautiful Statisticians |
|---|---|
| Description: | Because your linear models deserve better than console output. A sleek color palette and kable styling to make your regression results look sharper than they are. Includes support for Partial Least Squares (PLS) regression via both the SVD and NIPALS algorithms, along with a unified interface for model fitting and fabulous LaTeX and console output formatting. See the package website at <https://finitesample.space/snazzier>. |
| Authors: | Aidan J. Wagner [aut, cre] |
| Maintainer: | Aidan J. Wagner <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-05-21 08:49:38 UTC |
| Source: | https://github.com/detectivefierce/snazzier |
This function creates a summary table for ANOVA results, including degrees of freedom, sum of squares, mean squares, F-values, and p-values. The table can be output as either LaTeX (for PDF reports) or plain text (for console viewing).
ANOVA.summary.table(model, caption, latex = TRUE)ANOVA.summary.table(model, caption, latex = TRUE)
model |
A model object for which ANOVA results are computed (e.g., output from 'lm()' or 'aov()'). |
caption |
A character string to be used as the caption for the table. |
latex |
Logical; if 'TRUE', returns a LaTeX-formatted table using 'kableExtra'. If 'FALSE', prints a plain-text version to the console. |
If 'latex = TRUE', a LaTeX-formatted table object. If 'latex = FALSE', prints the summary table and returns it (invisibly).
# Fit a linear model model <- lm(mpg ~ wt + hp, data = mtcars) # Generate a plain-text ANOVA summary table ANOVA.summary.table(model, caption = "ANOVA Summary", latex = FALSE)# Fit a linear model model <- lm(mpg ~ wt + hp, data = mtcars) # Generate a plain-text ANOVA summary table ANOVA.summary.table(model, caption = "ANOVA Summary", latex = FALSE)
This function generates a plot displaying a predefined color palette with color codes for easy reference. The palette includes shades of Red, Orange, Yellow, Green, Blue, Purple, and Grey.
color.ref()color.ref()
A plot displaying the color palette.
color.ref()color.ref()
A collection of named hex colors grouped by hue and tone.
Each color is available as an exported object (e.g., Red, Dark.Red).
color.listcolor.list
Each color is a character string representing a hex code.
An object of class character of length 1.
An object of class list of length 35.
| Name | Hex | Swatch | Name | Hex | Swatch | |
| Deep.Red | #590D21 |
|
Deep.Green | #304011 |
|
|
| Dark.Red | #9F193D |
|
Dark.Green | #54711E |
|
|
| Red | #C31E4A |
|
Green | #83B02F |
|
|
| Light.Red | #E66084 |
|
Light.Green | #ABD45E |
|
|
| Pale.Red | #F1A7BB |
|
Pale.Green | #C4E18E |
|
|
| Deep.Orange | #6F4B0B |
|
Deep.Blue | #002429 |
|
|
| Dark.Orange | #A77011 |
|
Dark.Blue | #004852 |
|
|
| Orange | #E99F1F |
|
Blue | #008C9E |
|
|
| Light.Orange | #F0BF6A |
|
Light.Blue | #1FE5FF |
|
|
| Pale.Orange | #F4CF90 |
|
Pale.Blue | #85F1FF |
|
|
| Deep.Yellow | #9D7F06 |
|
Deep.Purple | #271041 |
|
|
| Dark.Yellow | #CEA708 |
|
Dark.Purple | #4E2183 |
|
|
| Yellow | #E8D206 |
|
Purple | #743496 |
|
|
| Light.Yellow | #FFE373 |
|
Light.Purple | #A06CDA |
|
|
| Pale.Yellow | #FFF8DC |
|
Pale.Purple | #CAADEB |
|
|
| Deep.Grey | #151315 |
|
||||
| Dark.Grey | #403A3F |
|
||||
| Grey | #6F646C |
|
||||
| Light.Grey | #9E949B |
|
||||
| Pale.Grey | #CFC9CD |
|
Create K-Fold Cross Validation Splits
create_kfold_splits(data, k = 5, seed = NULL)create_kfold_splits(data, k = 5, seed = NULL)
data |
A data frame containing the dataset |
k |
Number of folds (default: 5) |
seed |
Random seed for reproducibility (optional) |
A list containing fold assignments for each observation
This function computes the eigenvalues and eigenvectors of a given covariance matrix, ensures sign consistency in the eigenvectors, and outputs either a LaTeX table or plaintext summary displaying the results.
eigen.summary( cov.matrix, caption = "Eigenvectors of Covariance Matrix", latex = TRUE )eigen.summary( cov.matrix, caption = "Eigenvectors of Covariance Matrix", latex = TRUE )
cov.matrix |
A square numeric matrix representing the covariance matrix. |
caption |
A character string specifying the table caption (default: "Eigenvectors of Covariance Matrix"). |
latex |
A logical indicating whether to output LaTeX table (default: TRUE). If FALSE, prints as plain text. |
A LaTeX formatted table (if latex = TRUE) or plaintext console output (if latex = FALSE).
cov_matrix <- matrix(c(4, 2, 2, 3), nrow = 2) eigen.summary(cov_matrix, caption = "Eigenvalues and Eigenvectors", latex = FALSE)cov_matrix <- matrix(c(4, 2, 2, 3), nrow = 2) eigen.summary(cov_matrix, caption = "Eigenvalues and Eigenvectors", latex = FALSE)
Formats and displays Partial Least Squares (PLS) model output from pls.regression()
as either LaTeX tables (for PDF rendering) or console-friendly output.
## S3 method for class 'pls' format(x, ..., include.scores = TRUE, latex = FALSE)## S3 method for class 'pls' format(x, ..., include.scores = TRUE, latex = FALSE)
x |
A list returned by |
... |
Further arguments passed to or from methods (unused). |
include.scores |
Logical. Whether to include score matrices (T and U). Default is |
latex |
Logical. If |
When latex = TRUE, returns a knitr::asis_output object (LaTeX code). When FALSE, prints formatted tables to console.
Perform K-Fold Cross Validation
kfold_cross_validation( data, formula, model_function, predict_function = predict, metric_function = NULL, k = 5, seed = NULL, ... )kfold_cross_validation( data, formula, model_function, predict_function = predict, metric_function = NULL, k = 5, seed = NULL, ... )
data |
A data frame containing the dataset |
formula |
A formula specifying the model (e.g., y ~ x1 + x2) |
model_function |
Function to fit the model (e.g., lm, glm) |
predict_function |
Function to make predictions (default: predict) |
metric_function |
Function to calculate performance metric |
k |
Number of folds (default: 5) |
seed |
Random seed for reproducibility (optional) |
... |
Additional arguments passed to model_function |
A list containing fold results and summary statistics
This function extracts and formats the equation from a linear model object. It includes an option to return the equation as a LaTeX-formatted string or print it to the console.
model.equation(model, latex = TRUE)model.equation(model, latex = TRUE)
model |
A linear model object (e.g., output from 'lm()'). |
latex |
A logical value indicating whether to return a LaTeX-formatted equation (default: TRUE). If FALSE, the equation is printed to the console. |
If 'latex' is TRUE, the equation is returned as LaTeX code using 'knitr::asis_output()'. If FALSE, the equation is printed to the console.
# Fit a linear model model <- lm(mpg ~ wt + hp, data = mtcars) # Get LaTeX equation model.equation(model) # Print equation to console model.equation(model, latex = FALSE)# Fit a linear model model <- lm(mpg ~ wt + hp, data = mtcars) # Get LaTeX equation model.equation(model) # Print equation to console model.equation(model, latex = FALSE)
This function creates a summary table for a linear model, including estimated coefficients, standard errors, p-values with significance codes, and model statistics such as MSE and R-squared. The table can be output as either LaTeX (for PDF reports) or plain text (for console viewing).
model.summary.table(model, caption, latex = TRUE)model.summary.table(model, caption, latex = TRUE)
model |
A linear model object (typically the result of 'lm()'). |
caption |
A character string to be used as the caption for the table. |
latex |
Logical; if 'TRUE' (default), returns a LaTeX-formatted table using 'kableExtra'. If 'FALSE', prints plain-text summary tables to the console. |
If 'latex = TRUE', returns a LaTeX-formatted 'kableExtra' table object. If 'latex = FALSE', prints formatted summary tables to the console and returns the underlying data frame.
# Fit a linear model model <- lm(mpg ~ wt + hp, data = mtcars) # Print a plain-text version to the console model.summary.table(model, caption = "Linear Model Summary", latex = FALSE)# Fit a linear model model <- lm(mpg ~ wt + hp, data = mtcars) # Print a plain-text version to the console model.summary.table(model, caption = "Linear Model Summary", latex = FALSE)
Performs Partial Least Squares (PLS) regression using either the NIPALS or SVD algorithm for component extraction.
This is the main user-facing function for computing PLS models. Internally, it delegates to either NIPALS.pls() or SVD.pls().
pls.regression(x, y, n.components = NULL, calc.method = c("SVD", "NIPALS"))pls.regression(x, y, n.components = NULL, calc.method = c("SVD", "NIPALS"))
x |
A numeric matrix or data frame of predictor variables (X), with dimensions n × p. |
y |
A numeric matrix or data frame of response variables (Y), with dimensions n × q. |
n.components |
Integer specifying the number of latent components (H) to extract. If NULL, defaults to the rank of |
calc.method |
Character string indicating the algorithm to use. Must be either |
This function provides a unified interface for Partial Least Squares regression. Based on the value of calc.method,
it computes latent variables using either:
"SVD" — A direct method using the singular value decomposition of the cross-covariance matrix ().
"NIPALS" — An iterative method that alternately estimates predictor and response scores until convergence.
The outputs from both methods include scores, weights, loadings, regression coefficients, and explained variance.
A list (from either SVD.pls() or NIPALS.pls()) containing:
Character string ("PLS Regression").
Score matrices for X and Y.
Weight matrices for X and Y.
Loading matrices.
Component-wise regression weights.
Final regression coefficient matrix (rescaled).
Intercept vector (typically zero due to centering).
Variance explained by each component.
Cumulative variance explained.
Abdi, H., & Williams, L. J. (2013). Partial least squares methods: Partial least squares correlation and partial least square regression. Methods in Molecular Biology (Clifton, N.J.), 930, 549–579. doi:10.1007/978-1-62703-059-5_23
de Jong, S. (1993). SIMPLS: An alternative approach to partial least squares regression. Chemometrics and Intelligent Laboratory Systems, 18(3), 251–263. doi:10.1016/0169-7439(93)85002-X
## Not run: X <- matrix(rnorm(100 * 10), 100, 10) Y <- matrix(rnorm(100 * 2), 100, 2) # Using SVD (default) model1 <- pls.regression(X, Y, n.components = 3) # Using NIPALS model2 <- pls.regression(X, Y, n.components = 3, calc.method = "NIPALS") ## End(Not run)## Not run: X <- matrix(rnorm(100 * 10), 100, 10) Y <- matrix(rnorm(100 * 2), 100, 2) # Using SVD (default) model1 <- pls.regression(X, Y, n.components = 3) # Using NIPALS model2 <- pls.regression(X, Y, n.components = 3, calc.method = "NIPALS") ## End(Not run)
Formats and displays Partial Least Squares (PLS) model output from pls.regression()
as LaTeX tables for PDF rendering.
pls.summary(x, ..., include.scores = TRUE)pls.summary(x, ..., include.scores = TRUE)
x |
A list returned by |
... |
Further arguments passed to or from methods (unused). |
include.scores |
Logical. Whether to include score matrices (T and U). Default is |
Returns a knitr::asis_output object (LaTeX code) for PDF rendering.
# Load example data data(mtcars) # Prepare data for PLS regression X <- mtcars[, c("wt", "hp", "disp")] Y <- mtcars[, "mpg", drop = FALSE] # Fit PLS model with 2 components pls.fit <- pls.regression(X, Y, n.components = 2) # Print a LaTeX-formatted summary pls.summary(pls.fit, include.scores = FALSE)# Load example data data(mtcars) # Prepare data for PLS regression X <- mtcars[, c("wt", "hp", "disp")] Y <- mtcars[, "mpg", drop = FALSE] # Fit PLS model with 2 components pls.fit <- pls.regression(X, Y, n.components = 2) # Print a LaTeX-formatted summary pls.summary(pls.fit, include.scores = FALSE)
Predicts response values for new data using a fitted ridge model.
## S3 method for class 'ridge.model' predict(object, newdata = NULL, ...)## S3 method for class 'ridge.model' predict(object, newdata = NULL, ...)
object |
A 'ridge.model' object |
newdata |
A data frame or matrix containing new predictor values |
... |
Additional arguments (not used) |
A numeric vector of predicted values
Prints a summary of the ridge model fit.
## S3 method for class 'ridge.model' print(x, ...)## S3 method for class 'ridge.model' print(x, ...)
x |
A 'ridge.model' object |
... |
Additional arguments (not used) |
Performs ridge regression with automatic selection of the optimal regularization parameter 'lambda' by minimizing k-fold cross-validated mean squared error (CV-MSE) using Brent's method. Supports both formula and matrix interfaces.
ridge.regression( formula = NULL, data = NULL, x = NULL, y = NULL, lambda.range = c(1e-04, 100), folds = 5, ... )ridge.regression( formula = NULL, data = NULL, x = NULL, y = NULL, lambda.range = c(1e-04, 100), folds = 5, ... )
formula |
A model formula like 'y ~ x1 + x2'. Mutually exclusive with 'x'/'y'. |
data |
A data frame containing all variables used in the formula. |
x |
A numeric matrix of predictor variables (n × p). Used when formula is not provided. |
y |
A numeric vector of response variables (n × 1). Used when formula is not provided. |
lambda.range |
A numeric vector of length 2 specifying the interval for lambda optimization. Default: 'c(1e-4, 100)'. |
folds |
An integer specifying the number of cross-validation folds. Default: '5'. |
... |
Additional arguments passed to internal methods. |
This function implements ridge regression with automatic hyperparameter tuning. The algorithm:
Standardizes predictor variables (centers and scales)
Centers the response variable
Uses k-fold cross-validation to find the optimal lambda
Fits the final model with the optimal lambda
Returns a structured object for prediction and analysis
The ridge regression solution is computed using the closed-form formula:
A 'ridge.model' object containing:
Final ridge coefficients (no intercept)
Standard errors of the coefficients
Intercept term (from y centering)
Best lambda minimizing CV-MSE
Minimum CV-MSE achieved
Data frame with lambda and CV-MSE pairs
Standardization info: mean and sd for each predictor
Centering constant for y
Final model predictions on training data
Training residuals (y - fitted)
Matched call (for debugging)
Always "ridge"
Number of CV folds used
Stored if formula interface used
Stored if formula interface used
Hoerl, A. E., & Kennard, R. W. (1970). Ridge regression: Biased estimation for nonorthogonal problems. Technometrics, 12(1), 55-67.
## Not run: # Formula interface model1 <- ridge.regression(mpg ~ wt + hp + disp, data = mtcars) # Matrix interface X <- as.matrix(mtcars[, c("wt", "hp", "disp")]) y <- mtcars$mpg model2 <- ridge.regression(x = X, y = y) # Custom lambda range and folds model3 <- ridge.regression(mpg ~ ., data = mtcars, lambda.range = c(0.1, 10), folds = 10 ) ## End(Not run)## Not run: # Formula interface model1 <- ridge.regression(mpg ~ wt + hp + disp, data = mtcars) # Matrix interface X <- as.matrix(mtcars[, c("wt", "hp", "disp")]) y <- mtcars$mpg model2 <- ridge.regression(x = X, y = y) # Custom lambda range and folds model3 <- ridge.regression(mpg ~ ., data = mtcars, lambda.range = c(0.1, 10), folds = 10 ) ## End(Not run)
Formats and displays ridge regression model output from ridge.regression()
as LaTeX tables for PDF rendering or plain text for console viewing.
ridge.summary(x, ..., include.cv.trace = TRUE, latex = TRUE)ridge.summary(x, ..., include.cv.trace = TRUE, latex = TRUE)
x |
A ridge model object returned by |
... |
Further arguments passed to or from methods (unused). |
include.cv.trace |
Logical. Whether to include cross-validation trace information. Default is |
latex |
Logical; if |
If latex = TRUE, returns a knitr::asis_output object (LaTeX code) for PDF rendering.
If latex = FALSE, prints formatted summary tables to the console and returns the underlying data frames.
# Load example data data(mtcars) # Fit ridge regression model ridge.fit <- ridge.regression(mpg ~ wt + hp + disp, data = mtcars) # Print a LaTeX-formatted summary ridge.summary(ridge.fit, include.cv.trace = FALSE) # Print a plain-text summary ridge.summary(ridge.fit, include.cv.trace = FALSE, latex = FALSE)# Load example data data(mtcars) # Fit ridge regression model ridge.fit <- ridge.regression(mpg ~ wt + hp + disp, data = mtcars) # Print a LaTeX-formatted summary ridge.summary(ridge.fit, include.cv.trace = FALSE) # Print a plain-text summary ridge.summary(ridge.fit, include.cv.trace = FALSE, latex = FALSE)
This theme provides a clean, polished look for ggplot2 plots, with a focus on readability and aesthetics. It includes a custom color palette and formatting for titles, axes, and legends.
snazzieR.theme()snazzieR.theme()
A ggplot2 theme object.
library(ggplot2) set.seed(123) chains.df <- data.frame( Iteration = 1:500, alpha.1 = cumsum(rnorm(500, mean = 0.01, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.2 = cumsum(rnorm(500, mean = 0.005, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.3 = cumsum(rnorm(500, mean = 0.000, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.4 = cumsum(rnorm(500, mean = -0.005, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.5 = cumsum(rnorm(500, mean = -0.01, sd = 0.2)) + rnorm(1, 5, 0.2) ) chain.colors <- c("Chain 1" = Red, "Chain 2" = Orange, "Chain 3" = Yellow, "Chain 4" = Green, "Chain 5" = Blue) ggplot(chains.df, aes(x = Iteration)) + geom_line(aes(y = alpha.1, color = "Chain 1"), linewidth = 1.2) + geom_line(aes(y = alpha.2, color = "Chain 2"), linewidth = 1.2) + geom_line(aes(y = alpha.3, color = "Chain 3"), linewidth = 1.2) + geom_line(aes(y = alpha.4, color = "Chain 4"), linewidth = 1.2) + geom_line(aes(y = alpha.5, color = "Chain 5"), linewidth = 1.2) + labs(x = "Iteration", y = expression(alpha), title = expression("Traceplot for " ~ alpha)) + scale_color_manual(values = chain.colors, name = "Chains") + snazzieR.theme()library(ggplot2) set.seed(123) chains.df <- data.frame( Iteration = 1:500, alpha.1 = cumsum(rnorm(500, mean = 0.01, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.2 = cumsum(rnorm(500, mean = 0.005, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.3 = cumsum(rnorm(500, mean = 0.000, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.4 = cumsum(rnorm(500, mean = -0.005, sd = 0.2)) + rnorm(1, 5, 0.2), alpha.5 = cumsum(rnorm(500, mean = -0.01, sd = 0.2)) + rnorm(1, 5, 0.2) ) chain.colors <- c("Chain 1" = Red, "Chain 2" = Orange, "Chain 3" = Yellow, "Chain 4" = Green, "Chain 5" = Blue) ggplot(chains.df, aes(x = Iteration)) + geom_line(aes(y = alpha.1, color = "Chain 1"), linewidth = 1.2) + geom_line(aes(y = alpha.2, color = "Chain 2"), linewidth = 1.2) + geom_line(aes(y = alpha.3, color = "Chain 3"), linewidth = 1.2) + geom_line(aes(y = alpha.4, color = "Chain 4"), linewidth = 1.2) + geom_line(aes(y = alpha.5, color = "Chain 5"), linewidth = 1.2) + labs(x = "Iteration", y = expression(alpha), title = expression("Traceplot for " ~ alpha)) + scale_color_manual(values = chain.colors, name = "Chains") + snazzieR.theme()