View source: R/fbestnormalize.R
| f_bestNormalize | R Documentation |
Applies optimal normalization transformations using 'bestNormalize', provides diagnostic checks, and generates comprehensive reports.
f_bestNormalize(
data,
alpha = 0.05,
plots = FALSE,
data_name = NULL,
output_type = "default",
save_as = NULL,
save_in_wdir = FALSE,
close_generated_files = FALSE,
open_generated_files = interactive(),
...
)
data |
Numeric vector or single-column data frame. |
alpha |
Numeric. Significance level for normality tests (default = |
plots |
Logical. If |
data_name |
A character string to manually set the name of the data for plot axis and reporting. Default extracts name from input object. |
output_type |
Character string specifying the output format. Default is
|
save_as |
Character string specifying the output file path (without extension).
If a full path is provided, output is saved to that location.
If only a filename is given, the file is saved in |
save_in_wdir |
Logical. If |
close_generated_files |
Logical. Closes open Excel or Word (NOT pdf) files before writing, depending on the output format. Works on Windows (taskkill), macOS (pkill) and Linux (pkill/soffice). Default |
open_generated_files |
Logical. Whether to open the generated output
files after creation. Defaults to |
... |
Additional arguments passed to bestNormalize. |
This is a wrapper around the 'bestNormalize' package. Providing a fancy output and the settings of 'bestNormalize' are tuned based on sample size n.
If n < 100, loo = TRUE, allow_orderNorm = FALSE and r doesn't matter as loo = TRUE.
If 100 <= n < 200, loo = FALSE, allow_orderNorm = TRUE and r = 50.
If n >= 200, loo = FALSE, allow_orderNorm = TRUE, r = 10. These setting can be overwritten by user options.
This function requires [Pandoc](https://github.com/jgm/pandoc/releases/tag) (version 1.12.3 or higher), a universal document converter.
Windows: Install Pandoc and ensure the installation folder
(e.g., "C:/Users/your_username/AppData/Local/Pandoc") is added to your system PATH.
macOS: If using Homebrew, Pandoc is typically installed in "/usr/local/bin". Alternatively, download the .pkg installer and verify that the binary's location is in your PATH.
Linux: Install Pandoc through your distribution's package manager (commonly installed in "/usr/bin" or "/usr/local/bin") or manually, and ensure the directory containing Pandoc is in your PATH.
If Pandoc is not found, this function may not work as intended.
Returns an object of class 'f_bestNormalize' containing:
transformed_data Normalized vector.
bestNormalize Full bestNormalize object from original package.
data_name Name of the analyzed dataset.
transformation_name Name of selected transformation.
shapiro_original Shapiro-Wilk test results for original data.
shapiro_transformed Shapiro-Wilk test results for transformed data.
norm_stats Data frame of normality statistics for all methods.
rmd Rmd code if outputype = "rmd".
Also generates reports in 'Word', or 'pdf' files. When using output to console and plots = TRUE, the function prints QQ-plots, Histograms and a summary data transformation report. Includes print and plot methods for objects of class 'f_bestNormalize'.
Sander H. van Delden plantmind@proton.me
Peterson, C. (2025). bestNormalize: Flexibly calculate the best normalizing transformation for a vector. Available at: https://cran.r-project.org/package=bestNormalize
# Use set.seed to keep the outcome of bestNormalize stable.
set.seed(123)
# Create some skewed data (e.g., using a log-normal distribution).
skewed_data <- rlnorm(100, meanlog = 0, sdlog = 1)
# Basic usage: transform and store the full result object.
result <- f_bestNormalize(skewed_data, data_name = "Skewed log-normal data")
# Print a summary of the transformation.
print(result)
# Inspect normality statistics for all candidate transformations.
result$norm_stats
# Plot histograms and QQ-plots for original vs. transformed data.
plot(result)
# Use plots = TRUE to auto-plot when output_type = "default" (default).
result2 <- f_bestNormalize(skewed_data, plots = TRUE)
# Extract only the transformed (data) vector directly.
transformed_data <- f_bestNormalize(skewed_data)$transformed_data
# data.frame input: column name is used as data_name automatically.
df <- data.frame(measurement = skewed_data)
result_df <- f_bestNormalize(df)
# Data with NAs: NAs are preserved at their original positions.
skewed_na <- skewed_data
skewed_na[c(5, 20)] <- NA
result_na <- f_bestNormalize(skewed_na)
# Access a specific alternative transformation (first check what is available).
names(result$bestNormalize$other_transforms)
# Then extract the one you want, e.g.:
# result$bestNormalize$other_transforms$yeojohnson$x.t
# Force output to console (prints report + plots automatically).
f_bestNormalize(skewed_data, output_type = "console")
# Generate a PDF report saved to a custom path.
f_bestNormalize(skewed_data,
output_type = "pdf",
save_as = "my_report"
)
# Generate R Markdown output for use inside a .Rmd chunk
# (set chunk option results = 'asis').
rmd_result <- f_bestNormalize(skewed_data, output_type = "rmd")
cat(rmd_result$rmd)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.