| Title: | Tidy Common R Statistical Functions |
|---|---|
| Description: | Provides functions to scale, log-transform and fit linear models within a 'tidyverse'-style R code framework. Intended to smooth over inconsistencies in output of base R statistical functions, allowing ease of teaching, learning and daily use. Inspired by the tidy principles used in 'broom' Robinson (2017) <doi:10.21105/joss.00341>. |
| Authors: | Brendan Ansell [aut, cre] (ORCID: <https://orcid.org/0000-0003-0297-897X>) |
| Maintainer: | Brendan Ansell <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-06-07 08:10:22 UTC |
| Source: | https://github.com/bansell/tidyrstats |
Applies a linear model to a data frame and returns tidy model summaries. Supports ungrouped, grouped (dplyr::group_by()), and nested (tidyr::nest_by()) input data.
lm_test(input_data, formula)lm_test(input_data, formula)
input_data |
A data frame or tibble. Can be ungrouped, grouped, or nested. |
formula |
A model formula, either quoted or unquoted (e.g., y ~ x * z , or "y ~ x * z"). |
Designed to allow seamless 'in-line' chaining to fit linear models to columns of a tibble. Compatible with ungrouped, grouped or nested input. Compatible with native and magrittr pipe. Uses broom::tidy() to extract model summaries.
A tibble with tidy model output sorted by p value, including:
Model term (e.g., intercept, predictors, interactions)
Estimated coefficient / beta
Standard error of the estimate
t-statistic
p-value for the hypothesis test
If the input is grouped or nested, group identifiers are retained in the output. In the nested case, nested terms are relocated to the left-most column of the tibble.
library(ggplot2) library(dplyr) # Ungrouped mpg |> lm_test( cty ~ hwy * cyl) # Grouped mpg |> group_by(class) |> lm_test(cty ~ hwy * cyl) # Nested mpg |> nest_by(class) |> lm_test(cty ~ hwy * cyl)library(ggplot2) library(dplyr) # Ungrouped mpg |> lm_test( cty ~ hwy * cyl) # Grouped mpg |> group_by(class) |> lm_test(cty ~ hwy * cyl) # Nested mpg |> nest_by(class) |> lm_test(cty ~ hwy * cyl)
Computes the negative logarithm of a numeric input using base 10 by default.
neg_log(x, base = 10) neglog(x, base = 10)neg_log(x, base = 10) neglog(x, base = 10)
x |
A numeric vector. Values must be positive. |
base |
A numeric value specifying the base of the logarithm. Default is 10. |
This function returns the negative logarithm of 'x'. By default, it uses base 10, but you can specify a different base using the 'base' argument. Designed for quickly transforming p values for statistical analysis.
A numeric vector of negative logarithmic values.
pvals <- 10^runif(10, -15, -1) neg_log(pvals)pvals <- 10^runif(10, -15, -1) neg_log(pvals)
This function scales and centres a numeric vector by subtracting the mean and dividing by the standard deviation. Unlike 'scale()', it returns a numeric vector, not a matrix. Note this function does not allow control over centering or scaling.
scale_this(x)scale_this(x)
x |
A numeric vector. |
A numeric vector of scaled values.
iris_dat <- head(iris$Sepal.Length) scale_this(iris_dat) scale_this(c(iris_dat, NA))iris_dat <- head(iris$Sepal.Length) scale_this(iris_dat) scale_this(c(iris_dat, NA))