### Install statsExpressions Package (Development) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Use this command to install the development version of the statsExpressions package using the pak package manager. ```r pak::pak("IndrajeetPatil/statsExpressions") ``` -------------------------------- ### Install statsExpressions Package (Release) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Use this command to install the stable release version of the statsExpressions package from CRAN. ```r install.packages("statsExpressions") ``` -------------------------------- ### Check System Dependencies for statsExpressions Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Run this command on Linux to check for any additional system dependencies required for installing the statsExpressions package. ```r pak::pkg_sysreqs("statsExpressions") ``` -------------------------------- ### Perform Grouped One-Sample T-test with dplyr Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md This example shows how to perform a one-sample t-test for all levels of a grouping variable using dplyr for grouped operations. Ensure dplyr is loaded. ```r # for reproducibility set.seed(123) library(dplyr) # grouped operation ``` -------------------------------- ### Summarize Multiple Data Frames with purrr::map Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/long-to-wide-converter.md Apply the summary function to a list of data frames using purrr::map to get statistical summaries for each. This is useful for understanding data distributions. ```r purrr::map(list(df1, df2, df3, df4), summary) ``` -------------------------------- ### Get Column Names of Between-Subjects DataFrame Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-bayes.md Use `colnames()` to retrieve the names of all columns in a data frame prepared for Bayesian ANOVA. ```R colnames(df_between) ``` -------------------------------- ### Perform Kruskal-Wallis Test and Create Ridgeplot in R Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/paper/JOSS files/paper.md This snippet shows how to perform a non-parametric one-way ANOVA (Kruskal-Wallis test) using the `oneway_anova` function from the `statsExpressions` package and then visualize the results with a ridgeplot using `ggplot2` and `ggridges`. Ensure the necessary libraries are installed and loaded. ```r # needed libraries library(statsExpressions) library(ggplot2) library(ggridges) library(palmerpenguins) # `penguins` dataset is from this package # creating a dataframe res <- oneway_anova(penguins, species, body_mass_g, type = "nonparametric") # create a ridgeplot using `ggridges` package ggplot(penguins, aes(x = body_mass_g, y = species)) + geom_density_ridges( jittered_points = TRUE, quantile_lines = TRUE, scale = 0.9, vline_size = 1, vline_color = "red", position = position_raincloud(adjust_vlines = TRUE) ) + # use 'expression' column to display results in the subtitle labs( x = "Penguin species", y = "Body mass (in grams)", title = "Kruskal-Wallis Rank Sum Test", subtitle = res$expression[[1]] ) ``` -------------------------------- ### Get Column Names of Within-Subjects DataFrame Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-bayes.md Use `colnames()` to retrieve the names of all columns in a data frame prepared for within-subjects Bayesian ANOVA. ```R colnames(df_within) ``` -------------------------------- ### Run meta-analysis Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/meta-random-parametric.md Executes meta-analysis on a dataset, noting that missing required arguments will trigger an error. ```R meta_analysis(mtcars) ``` -------------------------------- ### Select Columns for Meta-Analysis Summary Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/meta-random-robust.md Use `select` to keep relevant columns for a meta-analysis summary. This is useful for preparing data for further analysis or display. ```R select(df, -expression) ``` -------------------------------- ### Get Number of Rows in Between-Subjects DataFrame Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-bayes.md Use `nrow()` to find the total number of rows in a data frame used for Bayesian ANOVA. ```R nrow(df_between) ``` -------------------------------- ### Citation for statsExpressions Package Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md This command displays the citation information for the statsExpressions package, including a BibTeX entry for LaTeX users. ```r citation("statsExpressions") ``` -------------------------------- ### Get Number of Rows in Within-Subjects DataFrame Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-bayes.md Use `nrow()` to find the total number of rows in a data frame used for within-subjects Bayesian ANOVA. ```R nrow(df_within) ``` -------------------------------- ### One-Sample Test (Bayes, g, 95% CI) - Inspecting Results Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/one-sample.md Shows how to inspect the names of the results object from a Bayesian one-sample test. This is useful for understanding the available output fields before accessing specific values. ```r names(res) ``` -------------------------------- ### One-Sample Test (Bayes, g, 95% CI) - Accessing BF10 Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/one-sample.md Demonstrates accessing the Bayes Factor (BF10) from the results of a Bayesian one-sample test. BF10 quantifies the evidence for the alternative hypothesis over the null hypothesis. ```r res$bf10[[1L]] ``` -------------------------------- ### Pairwise Comparisons Output (Yuen's Trimmed Means) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Presents the results of pairwise comparisons using Yuen's trimmed means with Hommel adjustment. The output includes group comparisons, estimates, confidence intervals, p-values, and adjusted p-values. ```r df3 ``` -------------------------------- ### Load ggplot2 Library for Visualization Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Loads the ggplot2 library, which is necessary for creating plots with the StatsExpressions package. ```r library(ggplot2) ``` -------------------------------- ### Perform One-Sample Robust T-test and Format Output Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md This snippet demonstrates performing a one-sample robust t-test and then selecting specific columns before formatting the output using knitr::kable. The 'expression' column is excluded as it's not needed for dataframe usage. ```r set.seed(123) # one-sample robust t-test # we will leave `expression` column out; it's not needed for using only the dataframe mtcars |> one_sample_test(wt, test.value = 3, type = "robust") |> dplyr::select(-expression) |> knitr::kable() ``` -------------------------------- ### Perform One-Way ANOVA with statsExpressions Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/paper/JOSS files/paper.md Demonstrates running non-parametric and robust one-way ANOVA tests by toggling the type argument. ```r mtcars %>% oneway_anova(cyl, wt, type = "nonparametric") #> # A tibble: 1 x 14 #> parameter1 parameter2 statistic df.error p.value #> #> 1 wt cyl 22.8 2 0.0000112 #> method estimate conf.level conf.low conf.high #> #> 1 Kruskal-Wallis rank sum test 0.736 0.95 0.613 0.831 #> effectsize conf.method conf.iterations expression #> #> 1 Epsilon2 (rank) bootstrap 100 mtcars %>% oneway_anova(cyl, wt, type = "robust") #> # A tibble: 1 x 11 #> statistic df df.error p.value estimate conf.level conf.low conf.high #> #> 1 12.7 2 12.2 0.00102 1.05 0.95 0.843 1.50 #> effectsize #> #> 1 Explanatory measure of effect size #> method expression #> #> 1 A heteroscedastic one-way ANOVA for trimmed means ``` -------------------------------- ### Basic Pairwise Contingency Table Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-contingency-table.md Generates a basic pairwise contingency table. Use this for a general overview of pairwise comparisons without specific adjustment methods applied to p-values. ```r select(df1, -expression) ``` ```r df1[["expression"]] ``` -------------------------------- ### Pairwise Comparisons Output (Bayesian t-test) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Illustrates the output for pairwise comparisons using a Bayesian t-test. This includes group comparisons, effect sizes, estimates, confidence intervals, and terms. ```r df4 ``` -------------------------------- ### Contingency Table Analysis with Pie Chart and Caption Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Analyzes categorical data from 'mpg$class' using a one-sample Bayesian test and visualizes the distribution with a pie chart. The caption displays the parsed statistical expression. Requires 'mpg' data and 'results_data$expression'. ```r set.seed(123) # dataframe with results results_data <- contingency_table( as.data.frame(table(mpg$class)), Var1, counts = Freq, type = "bayes" ) # create a pie chart ggplot(as.data.frame(table(mpg$class)), aes(x = "", y = Freq, fill = factor(Var1))) + geom_bar(width = 1, stat = "identity") + theme(axis.line = element_blank()) + # cleaning up the chart and adding results from one-sample proportion test coord_polar(theta = "y", start = 0) + labs( fill = "Class", x = NULL, y = NULL, title = "Pie Chart of class (type of car)", caption = parse(text = results_data$expression) ) ``` -------------------------------- ### Yuen's Trimmed Means Pairwise Comparisons Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Shows pairwise comparisons for Yuen's trimmed means test. The output includes estimates, confidence intervals, p-values, and the formatted expression. ```R df3 ``` ```R df3[["expression"]] ``` -------------------------------- ### Inspect Data Frames with purrr::walk Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/long-to-wide-converter.md Use purrr::walk to apply dplyr::glimpse to a list of data frames for inspecting their structure. Ensure purrr and dplyr are loaded. ```r purrr::walk(list(df1, df2, df3, df4), dplyr::glimpse) ``` -------------------------------- ### Perform Meta-Analysis Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Combines effect sizes from multiple studies using random-effects models. Requires estimate and standard error columns. ```r library(statsExpressions) set.seed(123) # Prepare data: requires 'estimate' and 'std.error' columns data(mag, package = "metaplus") dat <- dplyr::rename(mag, estimate = yi, std.error = sei) # Parametric random-effects meta-analysis (requires metafor package) meta_analysis(dat, type = "parametric") #> # A tibble: 1 × 11 #> term estimate std.error conf.level conf.low conf.high statistic p.value #> #> 1 Overall -0.096 0.044 0.95 -0.182 -0.009 -2.18 0.029 #> method effectsize n.obs #> #> 1 Random-effects meta-analysis (REML method) meta-analytic... 6 # Robust meta-analysis (requires metaplus package) meta_analysis(dat, type = "robust", random = "normal") # Bayesian meta-analysis (requires metaBMA package) meta_analysis(dat, type = "bayes") # Adjust confidence level meta_analysis(dat, conf.level = 0.99) ``` -------------------------------- ### One-Sample Test with Histogram and Subtitle Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Performs a one-sample test on 'mtcars' dataset and visualizes the results with a histogram, including a vertical line at the mean and a subtitle parsed from the test results. Requires 'mtcars' data and 'results_data$expression'. ```r set.seed(123) # dataframe with results results_data <- one_sample_test(mtcars, wt, test.value = 3, type = "bayes") # creating a histogram plot ggplot(mtcars, aes(wt)) + geom_histogram(alpha = 0.5) + geom_vline(xintercept = mean(mtcars$wt), color = "red") + labs(subtitle = parse(text = results_data$expression)) ``` -------------------------------- ### Select Columns from DataFrame (With Missing Data) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/centrality-description.md This snippet shows how to select columns from a dataframe containing missing values (NA), excluding the 'expression' column. This is useful for data preprocessing when dealing with incomplete datasets. ```r select(df_na, -expression) ``` -------------------------------- ### Displaying pairwise comparison tibbles Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Displays the resulting tibbles from various statistical tests performed on between-subjects designs. ```R df1 ``` ```R df2 ``` ```R df3 ``` ```R df4 ``` -------------------------------- ### Bayesian t-test Pairwise Comparisons Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Demonstrates pairwise comparisons for a Bayesian t-test. This snippet includes effect sizes, Bayes factors, and formatted expressions for reporting. ```R df4 ``` ```R df4[["expression"]] ``` -------------------------------- ### Within-Subjects Two-Sample Test (Non-Parametric) with Tidy Data Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Prepares data for a within-subjects two-sample test by pivoting it into a long format, then performs a non-parametric test using `two_sample_test` with `paired = TRUE` and `type = "np"`. ```r set.seed(123) library(tidyr) library(PairedData) data(PrisonStress) # get data in tidy format df <- pivot_longer(PrisonStress, starts_with("PSS"), names_to = "PSS", values_to = "stress") results_data <- two_sample_test( data = df, x = PSS, y = stress, paired = TRUE, subject.id = Subject, type = "np" ) ``` -------------------------------- ### Formatted Expressions for p-values (Yuen's Trimmed Means) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Retrieves and shows the formatted statistical expressions for the adjusted p-values from the Yuen's trimmed means test. These expressions are ready for use in statistical reporting. ```r df3[["expression"]] ``` -------------------------------- ### Display Mean for Each Level of 'cyl' with Parsed Expressions Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Generates a plot showing the mean of 'wt' for each 'cyl' level, using `centrality_description` and parsing the 'expression' column for display. ```r # displaying mean for each level of `cyl` centrality_description(mtcars, cyl, wt) |> ggplot(aes(cyl, wt)) + geom_point() + geom_label(aes(label = expression), parse = TRUE) ``` -------------------------------- ### Student's t-test Pairwise Comparisons Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Generates pairwise comparisons for a within-subjects design using Student's t-test. The output includes p-values and formatted expressions for reporting. ```R df1 ``` ```R df1[["expression"]] ``` -------------------------------- ### One-Way ANOVA with Robust Type and Ridgeplot Visualization Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Performs a robust one-way ANOVA on 'Sepal.Length' by 'Species' and visualizes the results using a ridgeplot, displaying the formatted expression in the subtitle. ```r set.seed(123) library(ggridges) results_data <- oneway_anova(iris, Species, Sepal.Length, type = "robust") # create a ridgeplot ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges() + labs( title = "A heteroscedastic one-way ANOVA for trimmed means", subtitle = results_data$expression[[1]] ) ``` -------------------------------- ### Generate Welch ANOVA subtitles Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-parametric.md Extracts statistical results and expression objects for Welch's ANOVA. ```R select(df, -expression) ``` ```R df[["expression"]] ``` -------------------------------- ### Perform Pairwise Comparisons Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Executes post-hoc tests between group pairs. Supports various test types and multiple testing corrections. ```r library(statsExpressions) set.seed(123) # Between-subjects parametric: Games-Howell test (unequal variances) pairwise_comparisons(mtcars, cyl, wt, type = "parametric") #> # A tibble: 3 × 8 #> group1 group2 statistic p.value p.adjust.method test expression #> #> 1 4 6 -3.06 0.0282 Holm Games-Howell #> 2 4 8 -7.37 0.0003 Holm Games-Howell #> 3 6 8 -3.60 0.0195 Holm Games-Howell # Student's t-test (equal variances assumed) pairwise_comparisons(mtcars, cyl, wt, type = "parametric", var.equal = TRUE) # Non-parametric Dunn test pairwise_comparisons(mtcars, cyl, wt, type = "nonparametric") # Robust Yuen's trimmed means test pairwise_comparisons(mtcars, cyl, wt, type = "robust") # Bayesian pairwise comparisons pairwise_comparisons(mtcars, cyl, wt, type = "bayes") # Custom p-value adjustment method pairwise_comparisons(mtcars, cyl, wt, p.adjust.method = "bonferroni") pairwise_comparisons(mtcars, cyl, wt, p.adjust.method = "fdr") pairwise_comparisons(mtcars, cyl, wt, p.adjust.method = "none") # Within-subjects (paired) pairwise comparisons pairwise_comparisons( bugs_long, condition, desire, subject.id = subject, paired = TRUE, p.adjust.method = "BH" ) # Within-subjects non-parametric (Durbin-Conover test) pairwise_comparisons( bugs_long, condition, desire, subject.id = subject, paired = TRUE, type = "nonparametric" ) ``` -------------------------------- ### Perform One-Sample Tests with statsExpressions Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Use `one_sample_test` for one-sample hypothesis tests. Supports parametric, non-parametric, robust, and Bayesian methods. Specify `effsize.type` for custom effect sizes like Cohen's d. ```r library(statsExpressions) set.seed(123) # Parametric one-sample t-test with Hedge's g effect size one_sample_test(mtcars, wt, test.value = 3) ``` ```r # Non-parametric Wilcoxon test one_sample_test(mtcars, wt, test.value = 3, type = "nonparametric") ``` ```r # Robust bootstrap-t method for trimmed means one_sample_test(mtcars, wt, test.value = 3, type = "robust") ``` ```r # Bayesian one-sample t-test with default Cauchy prior (r = 0.707) one_sample_test(mtcars, wt, test.value = 3, type = "bayes") ``` ```r # Custom effect size (Cohen's d instead of Hedge's g) one_sample_test(mtcars, wt, test.value = 3, effsize.type = "d") ``` ```r # Adjust confidence level and alternative hypothesis one_sample_test(mtcars, wt, test.value = 3, conf.level = 0.99, alternative = "greater") ``` -------------------------------- ### Contingency Table Expression Invariance Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/contingency-table.md Demonstrates that the contingency_table expression output is invariant to the `options(OutDec)` setting. This ensures consistent results regardless of decimal separator settings. ```R df[["expression"]] ``` ```R df[["expression"]] ``` -------------------------------- ### Inspect Bayesian One-Sample Test Results Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/one-sample.md Access the names of the result object or specific Bayes factor values for Bayesian one-sample tests. ```R names(res) ``` ```R res$bf10[[1L]] ``` -------------------------------- ### Meta-Analysis Forest Plot with Custom Subtitle Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Generates a meta-analysis forest plot using the 'metaviz' package and displays a custom subtitle parsed from the meta-analysis results. Requires 'mozart' data and 'results_data$expression'. ```r set.seed(123) library(metaviz) # dataframe with results results_data <- meta_analysis(dplyr::rename(mozart, estimate = d, std.error = se)) # meta-analysis forest plot with results random-effects meta-analysis suppressWarnings(viz_forest( x = mozart[, c("d", "se")], study_labels = mozart[, "study_name"], xlab = "Cohen's d", variant = "thick", type = "cumulative" )) + labs( title = "Meta-analysis of Pietschnig, Voracek, and Formann (2010) on the Mozart effect", subtitle = parse(text = results_data$expression) ) + theme(text = element_text(size = 12)) ``` -------------------------------- ### Two-Sample Welch's t-test with Boxplot and Parsed Expression Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Performs a two-sample Welch's t-test on 'len' by 'supp' using `two_sample_test` and displays the parsed expression as the subtitle of a boxplot. ```r set.seed(123) results_data <- two_sample_test(ToothGrowth, supp, len) ggplot(ToothGrowth, aes(supp, len)) + geom_boxplot() + labs( title = "Two-Sample Welch's t-test", subtitle = parse(text = results_data$expression) ) ``` -------------------------------- ### Perform One-Way ANOVA (Non-parametric) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Use this function to perform a non-parametric one-way ANOVA. Ensure the data is piped into the function. ```r mtcars |> oneway_anova(cyl, wt, type = "nonparametric") ``` -------------------------------- ### Inspect Parametric One-Sample Test Results Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/one-sample.md View the tibble of results excluding the expression column or access the expression object directly. ```R select(res, -expression) ``` ```R res[["expression"]] ``` -------------------------------- ### pairwise_comparisons Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Performs pairwise post-hoc comparisons between all group pairs following a one-way design, supporting various parametric, non-parametric, robust, and Bayesian tests. ```APIDOC ## pairwise_comparisons ### Description Performs pairwise post-hoc comparisons between all group pairs following a one-way design. Supports parametric (Games-Howell or Student's t), non-parametric (Dunn or Durbin-Conover), robust (Yuen's trimmed means), and Bayesian (BayesFactor) tests. Includes multiple testing correction methods. ### Parameters - **data** (data.frame) - Required - The dataset containing the variables. - **x** (variable) - Required - The grouping variable. - **y** (variable) - Required - The numeric response variable. - **type** (string) - Optional - Test type: "parametric", "nonparametric", "robust", or "bayes". - **p.adjust.method** (string) - Optional - Correction method (e.g., "holm", "bonferroni", "fdr"). - **var.equal** (boolean) - Optional - Whether to assume equal variances for parametric tests. - **subject.id** (variable) - Optional - Subject identifier for within-subjects designs. - **paired** (boolean) - Optional - Whether the design is paired (within-subjects). ### Response - Returns a tibble containing group comparisons, statistics, p-values, and adjustment methods. ``` -------------------------------- ### Perform One-Way ANOVA (Robust) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Use this function to perform a robust one-way ANOVA. Ensure the data is piped into the function. ```r mtcars |> oneway_anova(cyl, wt, type = "robust") ``` -------------------------------- ### centrality_description Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Computes centrality measures (mean, median, trimmed mean, or MAP) for a numeric variable across groups. ```APIDOC ## centrality_description ### Description Computes centrality measures for a numeric variable across groups. Returns a data frame with distribution statistics and pre-formatted expressions for labeling plots. ### Parameters - **data** (data.frame) - Required - The dataset. - **x** (variable) - Required - The grouping variable. - **y** (variable) - Required - The numeric variable to summarize. - **type** (string) - Optional - Centrality type: "parametric" (mean), "nonparametric" (median), "robust" (trimmed mean), or "bayes" (MAP). - **tr** (numeric) - Optional - Trim level for robust centrality. - **conf.level** (numeric) - Optional - Confidence level for intervals. ### Response - Returns a tibble with distribution statistics (iqr, min, max, skewness, kurtosis, n.obs) and formatted expressions. ``` -------------------------------- ### Perform Correlation Test Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Use corr_test to analyze the correlation between two continuous variables. Supports Pearson, Spearman, robust (Winsorized Pearson), and Bayesian correlation coefficients, all returning confidence intervals. ```r library(statsExpressions) set.seed(123) # Parametric Pearson correlation corr_test(mtcars, wt, mpg, type = "parametric") ``` ```r # Non-parametric Spearman correlation corr_test(mtcars, wt, mpg, type = "nonparametric") ``` ```r # Robust Winsorized correlation corr_test(mtcars, wt, mpg, type = "robust") ``` ```r # Bayesian correlation with HDI intervals corr_test(mtcars, wt, mpg, type = "bayes") ``` ```r # Adjust confidence level corr_test(mtcars, wt, mpg, conf.level = 0.99) ``` ```r # Adjust trim level for robust correlation corr_test(mtcars, wt, mpg, type = "robust", tr = 0.1) ``` -------------------------------- ### Extracting Contingency Table Expression without Plotting Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Demonstrates how to extract the statistical expression for a contingency table test (Pearson's chi-squared) without generating a plot. This is useful for using the expression in other contexts. Requires 'mtcars' data. ```r set.seed(123) # Pearson's chi-squared test of independence contingency_table(mtcars, am, vs)$expression[[1]] ``` -------------------------------- ### Select meta-analysis results Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/meta-random-bayes.md Removes the expression column from the data frame to display the primary meta-analytic results. ```R dplyr::select(df, -expression) ``` -------------------------------- ### One Sample t-test Output Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/one-two-sample-dataframes.md Displays the tibble output for a one-sample t-test analysis. ```R df_1 ``` -------------------------------- ### meta_analysis Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Performs random-effects meta-analysis combining effect sizes from multiple studies. ```APIDOC ## meta_analysis ### Description Performs random-effects meta-analysis combining effect sizes from multiple studies. Requires a data frame with 'estimate' and 'std.error' columns. ### Parameters - **data** (data.frame) - Required - Data frame containing 'estimate' and 'std.error'. - **type** (string) - Optional - Approach: "parametric", "robust", or "bayes". - **random** (string) - Optional - Random effects distribution type (e.g., "normal"). - **conf.level** (numeric) - Optional - Confidence level for intervals. ### Response - Returns a tibble with meta-analytic results including effect size, confidence intervals, statistics, and p-values. ``` -------------------------------- ### Generate Fisher ANOVA subtitles Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-parametric.md Extracts statistical results and expression objects for Fisher's ANOVA. ```R select(df1, -expression) ``` ```R df1[["expression"]] ``` -------------------------------- ### Pairwise Comparisons Output (Durbin-Conover) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Shows the results of pairwise comparisons using the Durbin-Conover test with BY adjustment. This output includes group comparisons, statistics, p-values, and adjusted p-values. ```r df2 ``` -------------------------------- ### Display Data Without NAs Tibble Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Display the tibble containing results from data without NAs. ```R df ``` -------------------------------- ### Select Data and Expressions for Between-Subjects Analysis Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-nonparametric.md Use select to remove the expression column from the tibble or access the expression list directly for between-subjects data. ```R select(df1, -expression) ``` ```R df1[["expression"]] ``` ```R select(df2, -expression) ``` ```R df2[["expression"]] ``` -------------------------------- ### Compute Centrality Measures Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Calculates group-level summary statistics like mean or median, returning formatted expressions for plot annotations. ```r library(statsExpressions) set.seed(123) # Parametric: Mean for each group centrality_description(iris, Species, Sepal.Length, type = "parametric") #> # A tibble: 3 × 13 #> Species Sepal.Length iqr min max skewness kurtosis n.obs #> #> 1 setosa 5.01 0.400 4.3 5.8 0.120 -0.253 50 #> 2 versicolor 5.94 0.700 4.9 7.0 0.105 -0.533 50 #> 3 virginica 6.59 0.675 4.9 7.9 0.118 0.033 50 #> expression n.expression std.dev conf.low conf.high #> #> 1 setosa... 0.352 4.91 5.11 #> 2 versic... 0.516 5.79 6.08 #> 3 virgin... 0.636 6.41 6.77 # Non-parametric: Median for each group centrality_description(mtcars, am, wt, type = "nonparametric") # Robust: Trimmed mean (20% trim by default) centrality_description(ToothGrowth, supp, len, type = "robust") # Bayesian: Maximum A Posteriori (MAP) estimate centrality_description(sleep, group, extra, type = "bayes") # Adjust trim level for robust centrality centrality_description(ToothGrowth, supp, len, type = "robust", tr = 0.1) # Adjust confidence level centrality_description(iris, Species, Sepal.Length, conf.level = 0.99) ``` -------------------------------- ### Perform Two-Sample Tests with statsExpressions Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Utilize `two_sample_test` for comparing two groups. Handles parametric, non-parametric, robust, and Bayesian approaches for both between-subjects and within-subjects designs. Specify `var.equal = TRUE` for Student's t-test. ```r library(statsExpressions) set.seed(123) # Between-subjects: Welch's t-test (unequal variances) two_sample_test(ToothGrowth, supp, len, type = "parametric") ``` ```r # Equal variances assumption (Student's t-test) two_sample_test(ToothGrowth, supp, len, var.equal = TRUE) ``` ```r # Non-parametric Mann-Whitney U test two_sample_test(ToothGrowth, supp, len, type = "nonparametric") ``` ```r # Robust Yuen's test for trimmed means two_sample_test(ToothGrowth, supp, len, type = "robust") ``` ```r # Bayesian t-test two_sample_test(ToothGrowth, supp, len, type = "bayes") ``` ```r # Within-subjects (paired) design df <- dplyr::filter(bugs_long, condition %in% c("LDLF", "LDHF")) two_sample_test(df, condition, desire, subject.id = subject, paired = TRUE) ``` ```r # Paired non-parametric (Wilcoxon signed-rank) two_sample_test(df, condition, desire, subject.id = subject, paired = TRUE, type = "np") ``` ```r # Paired robust test two_sample_test(df, condition, desire, subject.id = subject, paired = TRUE, type = "robust") ``` -------------------------------- ### Display Games-Howell Test Tibble Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Display the tibble containing Games-Howell test results. ```R df6 ``` -------------------------------- ### Access Meta-Analysis Expression Components Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/meta-random-robust.md Access the 'expression' column from a data frame to retrieve detailed meta-analysis results. This expression contains statistical components like z-values, p-values, and confidence intervals. ```R df[["expression"]] ``` -------------------------------- ### Two Sample Between-Subjects t-test Output Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/one-two-sample-dataframes.md Displays the tibble output for a between-subjects two-sample t-test. ```R df_2_between ``` -------------------------------- ### Analyze Contingency Tables Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Use contingency_table for categorical variable analysis. Supports chi-squared test of independence, McNemar's test, and goodness-of-fit tests with frequentist and Bayesian options. Returns effect sizes like Cramer's V or Cohen's g. ```r library(statsExpressions) set.seed(123) # Two-way contingency table: Chi-squared test of independence contingency_table(mtcars, am, vs) ``` ```r # Bayesian contingency table analysis contingency_table(mtcars, am, vs, type = "bayes") ``` ```r # Paired (McNemar's test) for repeated measures categorical data paired_data <- tibble::tibble( response_before = factor(c("no", "yes", "no", "yes"), levels = c("no", "yes")), response_after = factor(c("no", "no", "yes", "yes"), levels = c("no", "yes")), Freq = c(65L, 25L, 5L, 5L) ) contingency_table(paired_data, response_before, response_after, paired = TRUE, counts = Freq) ``` ```r # One-way goodness-of-fit test contingency_table(as.data.frame(HairEyeColor), Eye, counts = Freq) ``` ```r # Goodness-of-fit with custom expected proportions contingency_table( as.data.frame(HairEyeColor), Eye, counts = Freq, ratio = c(0.2, 0.2, 0.3, 0.3) ) ``` ```r # Bayesian one-way test contingency_table( as.data.frame(HairEyeColor), Eye, counts = Freq, type = "bayes" ) ``` -------------------------------- ### McNemar's Chi-squared Test Results Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/contingency-table.md Presents the results of McNemar's Chi-squared test for paired nominal data, showing the statistic, degrees of freedom, p-value, effect size (Cohen's g), and confidence intervals. Suitable for analyzing paired categorical data, such as before-and-after measurements. ```R select(df1, -expression) ``` ```R df1[["expression"]] ``` ```R select(df2, -expression) ``` ```R df2[["expression"]] ``` -------------------------------- ### Accessing 'expression' Column (With Missing Data) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/centrality-description.md This code accesses the 'expression' column of a dataframe that contains missing data. The output illustrates how the 'expression' column stores statistical results, similar to the non-missing data case, indicating the package's ability to handle NA values. ```r df_na[["expression"]] ``` -------------------------------- ### Access Games-Howell Test Expressions Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Access the expression column from the dataframe containing Games-Howell test results. ```R df6[["expression"]] ``` -------------------------------- ### Select and Remove Expression from Results Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/centrality-description.md Use `select(res, -expression)` to display centrality results while excluding the expression column. This is useful for cleaner output when the expression details are not needed. ```R select(res, -expression) ``` -------------------------------- ### Correlation Analysis Scatter Plot with Subtitle Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Conducts a non-parametric correlation test on 'mtcars' and visualizes the relationship using a scatter plot with a linear model fit. The subtitle is dynamically generated from the test results. Ensure 'mtcars' data and 'results_data$expression' are available. ```r set.seed(123) # dataframe with results results_data <- corr_test(mtcars, mpg, wt, type = "nonparametric") # create a scatter plot ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_smooth(method = "lm", formula = y ~ x) + labs( title = "Spearman's rank correlation coefficient", subtitle = parse(text = results_data$expression) ) ``` -------------------------------- ### Access Yuen's Trimmed Means Expressions Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Access the expression column from the dataframe containing Yuen's trimmed means results. ```R df4[["expression"]] ``` -------------------------------- ### One-Sample Test for Grouping Variable Levels Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Applies a one-sample test to each level of a grouping variable. Requires grouping the data and then applying the test using `group_modify`. ```r mtcars |> group_by(cyl) |> group_modify(~ one_sample_test(.x, wt, test.value = 3), .keep = TRUE) |> ungroup() ``` -------------------------------- ### Paired Test Plot with Custom Subtitle Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Generates a paired profiles plot for a two-sample Wilcoxon paired test and customizes the subtitle using parsed results. Ensure 'PrisonStress' data and 'results_data$expression' are available. ```r suppressWarnings(paired.plotProfiles( PrisonStress, "PSSbefore", "PSSafter", subjects = "Subject" )) + labs( title = "Two-sample Wilcoxon paired test", subtitle = parse(text = results_data$expression) ) ``` -------------------------------- ### Within-Subjects One-Way ANOVA (Non-Parametric) with Parsed Expressions Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Conducts a non-parametric within-subjects one-way ANOVA on 'Taste' by 'Wine' for each 'Taster', using `oneway_anova` with `paired = TRUE` and `type = "np"`. The parsed expression is used as the plot subtitle. ```r set.seed(123) library(WRS2) library(ggbeeswarm) results_data <- oneway_anova( WineTasting, Wine, Taste, paired = TRUE, subject.id = Taster, type = "np" ) ggplot2::ggplot(WineTasting, aes(Wine, Taste, color = Wine)) + geom_quasirandom() + labs( title = "Friedman's rank sum test", subtitle = parse(text = results_data$expression) ) ``` -------------------------------- ### Perform One-Way ANOVA Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Use oneway_anova for comparing three or more groups. Supports various ANOVA types (Welch's, Fisher's, Kruskal-Wallis, Friedman, robust, Bayesian) and designs (between-subjects, within-subjects). Returns effect sizes like partial omega-squared or Kendall's W. ```r library(statsExpressions) set.seed(123) # Between-subjects: Welch's one-way ANOVA (default, handles unequal variances) oneway_anova(mtcars, cyl, wt) ``` ```r # Biased effect size (partial eta-squared) oneway_anova(mtcars, cyl, wt, effsize.type = "eta") ``` ```r # Fisher's ANOVA (equal variances) oneway_anova(mtcars, cyl, wt, var.equal = TRUE) ``` ```r # Non-parametric Kruskal-Wallis test oneway_anova(mtcars, cyl, wt, type = "np") ``` ```r # Robust heteroscedastic ANOVA for trimmed means oneway_anova(mtcars, cyl, wt, type = "robust") ``` ```r # Bayesian ANOVA oneway_anova(mtcars, cyl, wt, type = "bayes") ``` ```r # Within-subjects (repeated measures) parametric ANOVA oneway_anova(iris_long, condition, value, subject.id = id, paired = TRUE) ``` ```r # Within-subjects non-parametric (Friedman test) oneway_anova(iris_long, condition, value, subject.id = id, paired = TRUE, type = "np") ``` ```r # Within-subjects robust ANOVA oneway_anova(iris_long, condition, value, subject.id = id, paired = TRUE, type = "robust") ``` -------------------------------- ### One-way Analysis of Means Output Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/one-two-sample-dataframes.md Displays the tibble output for a one-way analysis of means. ```R df_3_between ``` -------------------------------- ### Access expression list in z model Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/tidy-model-expressions.md Retrieves the expression list column from the z-test model data frame. ```R df_z[["expression"]] ``` -------------------------------- ### Select columns excluding expression (within-subjects, no NAs) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/two-sample-robust.md This snippet demonstrates selecting all columns from a dataframe except for the 'expression' column in a within-subjects design without missing values. ```r select(df1, -expression) ``` -------------------------------- ### Access Bayesian ANOVA Expression (Between-Subjects) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-bayes.md Access the 'expression' column for detailed Bayesian ANOVA results, including Bayes Factor, R-squared, and credible intervals. ```R df2[["expression"]][[1]] ``` -------------------------------- ### Access Bayesian ANOVA Expression (Between-Subjects) Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/oneway-anova-bayes.md Access the 'expression' column for detailed Bayesian ANOVA results, including Bayes Factor, R-squared, and credible intervals. ```R df1[["expression"]][[1]] ``` -------------------------------- ### Repeat Statistical Analysis with dplyr Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/paper/JOSS files/paper.md Combines dplyr grouping with statsExpressions functions to perform tests across multiple levels of a variable. ```r # running one-sample proportion test for all levels of `cyl` mtcars %>% group_by(cyl) %>% group_modify(~ contingency_table(.x, am), .keep = TRUE) %>% ungroup() #> # A tibble: 3 x 13 #> cyl statistic df p.value method #> #> 1 4 2.27 1 0.132 Chi-squared test for given probabilities #> 2 6 0.143 1 0.705 Chi-squared test for given probabilities #> 3 8 7.14 1 0.00753 Chi-squared test for given probabilities #> estimate conf.level conf.low conf.high effectsize conf.method #> #> 1 0.344 0.95 0 0.917 Cramer's V (adj.) ncp #> 2 0 0.95 0 0 Cramer's V (adj.) ncp #> 3 0.685 0.95 0.127 1.18 Cramer's V (adj.) ncp #> conf.distribution expression #> #> 1 chisq #> 2 chisq #> 3 chisq ``` -------------------------------- ### Display Student's t-test Tibble Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/tests/testthat/_snaps/pairwise-comparisons.md Display the tibble containing Student's t-test results. ```R df5 ``` -------------------------------- ### Customizing Statistical Expressions in Plots Source: https://github.com/indrajeetpatil/statsexpressions/blob/main/README.md Shows how to extract and selectively use parts of a statistical expression for plot subtitles, focusing on F-statistic and p-value from a one-way ANOVA. Requires 'iris' data and 'res_expr' containing the full expression. ```r set.seed(123) # extracting detailed expression (res_expr <- oneway_anova(iris, Species, Sepal.Length, var.equal = TRUE)$expression[[1]]) # adapting the details to your liking ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot() + labs(subtitle = ggplot2::expr(paste( NULL, italic("F"), "(", "2", ",", "147", ") = ", "119.26", ", ", italic("p"), " = ", "1.67e-31" ))) ``` -------------------------------- ### Generate statistical expressions with add_expression_col Source: https://context7.com/indrajeetpatil/statsexpressions/llms.txt Creates plotmath-compatible expressions from a data frame of statistical results for use in ggplot2 labels. ```r library(statsExpressions) set.seed(123) # Create a data frame with statistical results stats_df <- data.frame( statistic = 5.494, df = 29.234, p.value = 0.00001, estimate = -1.980, conf.level = 0.95, conf.low = -2.873, conf.high = -1.088, method = "Student's t-test" ) # Generate expression for t-statistic with Cohen's d effect size result <- add_expression_col( data = stats_df, statistic.text = list(quote(italic("t"))), effsize.text = list(quote(italic("d"))), n = 32L, n.text = list(quote(italic("n")["obs"])), digits = 3L, digits.df = 3L ) # The expression can be used in ggplot2 subtitle result$expression[[1]] #> list(italic("t")(29.234) == "5.494", italic(p) == "0.00001", #> italic("d") == "-1.980", CI["95%"] ~ "[" * "-2.873", "-1.088" * "]", #> italic("n")["obs"] == "32") # Use in a ggplot library(ggplot2) ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs(subtitle = result$expression[[1]]) ```