View source: R/fkruskal_test.R
| f_kruskal_test | R Documentation |
Performs the Kruskal-Wallis rank sum test to assess whether there are statistically significant differences in the distributions (mean ranks) of three or more independent groups. It provides detailed outputs, including plots, assumption checks, and post hoc analyses using Dunn's test. Results can be saved in various formats ('pdf', 'Word', 'Excel', or console only) with customizable output options.
f_kruskal_test(
formula,
data = NULL,
plot = TRUE,
alpha = 0.05,
output_type = "default",
save_as = NULL,
save_in_wdir = FALSE,
intro_text = TRUE,
adjust = "bonferroni",
close_generated_files = FALSE,
open_generated_files = interactive(),
...
)
formula |
A formula specifying the response and predictor variable (e.g., |
data |
A |
plot |
Logical. If |
alpha |
Numeric. The significance level for the Kruskal-Wallis test and Dunn's
test. Default is |
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 |
intro_text |
Logical. If |
adjust |
Character string. Adjustment method for pairwise comparisons in Dunn's test. Options include |
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 forwarded to |
This function offers a comprehensive workflow for non-parametric analysis using the Kruskal-Wallis test:
Assumption Checks: Optionally includes a summary of assumptions in the output.
Visualization: Generates density plots and boxplots to visualize group distributions.
Post hoc Analysis: Conducts Dunn's test with specified correction methods if significant differences are found.
———–
Output files are generated in the format specified by output_type = and saved to the working directory, options are "pdf", "word" or "excel". If output_type = "rmd" is used it is advised to use it in a chunk with {r, echo=FALSE, results='asis'}
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.
An object of class 'f_kruskal_test' (a named list, one entry per response-predictor combination) containing:
The htest object from kruskal.test().
Data frame of pairwise Dunn's test results from rstatix::dunn_test().
Descriptive statistics with compact letter display (Letters column).
The significance level used.
The p-value adjustment method used.
ggplot density plot (if plot = TRUE).
ggplot boxplot with CLD letters (if plot = TRUE).
Using the option output_type, it can also generate output in the form of: R Markdown code, 'Word', 'pdf', or 'Excel' files. Includes print and plot methods for 'f_kruskal_test' objects.
When several response variables are analysed in a single call
(e.g. y1 + y2 + y3 ~ treatment), each Kruskal-Wallis test is an
independent null-hypothesis test at level alpha. The post hoc
adjustment (e.g. adjust = "bonferroni") only controls the
family-wise error rate within one test (across pairwise Dunn
comparisons for that response). It does not protect against
the inflation of Type I error across the set of responses.
Practical implication: With k independent response
variables all tested at \alpha = 0.05, the probability of
obtaining at least one false positive is
1-(1-0.05)^k, which reaches ~40% for k = 10.
Sander H. van Delden plantmind@proton.me
# Example usage:
data(iris)
# Perform Kruskal-Wallis test on Sepal.Length and Sepal.Width by Species
# with "holm" correction for posthoc dunn_test, without showing the output.
output <- f_kruskal_test(
Sepal.Width + Sepal.Length ~ Species,
data = iris,
plot = FALSE,
output_type = "word",
adjust = "holm"
)
# Save Kruskal-Wallis test and posthoc to Excel sheets: Sepal.Width and Sepal.Length.
f_kruskal_out <- f_kruskal_test(
Sepal.Width + Sepal.Length ~ Species,
data = iris,
plot = FALSE,
output_type = "excel",
adjust = "holm"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.