Package {sgmean}


Type: Package
Title: Proportional Trimmed Mean
Version: 0.2.0
Date: 2026-11-27
Description: Computes a proportional trimmed mean that resolves the integer truncation problem of base R's mean(..., trim). When k = trim * n is non-integer, a fractional discount (1 - delta) is applied to boundary observations, where delta = k - floor(k). The resulting estimator is continuous in alpha for any fixed n, syntactically identical to mean(..., trim), and compatible with the 'Statgraphics' implementation. The package also provides standard errors, confidence intervals and Yuen-type one-sample, paired and two-sample t tests for trimmed means (following Yuen (1974) <doi:10.1093/biomet/61.1.165>), a robust effect size, a truncation diagnostic, and descriptive statistics (winsorized mean and sigma, quantiles following Hyndman and Fan (1996) <doi:10.1080/00031305.1996.10473566>, biweight scale, skewness, kurtosis). See Gaviria Chaverra (2026) <doi:10.32614/CRAN.package.sgmean>.
License: MIT + file LICENSE
Encoding: UTF-8
Imports: graphics, stats
Suggests: knitr, rmarkdown, testthat (≥ 3.0.0)
Config/testthat/edition: 3
VignetteBuilder: knitr
URL: https://github.com/jcarlosgaviria/sgmean
BugReports: https://github.com/jcarlosgaviria/sgmean/issues
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-11-27 17:59:11 UTC; Personal
Author: Juan C. Gaviria-Chaverra ORCID iD [aut, cre]
Maintainer: Juan C. Gaviria-Chaverra <jcarlos.gaviria@udea.edu.co>
Repository: CRAN
Date/Publication: 2026-11-28 11:20:02 UTC

Confidence interval for the proportional trimmed mean

Description

Confidence interval for the proportional trimmed mean

Usage

ci_trimmed(x, trim = 0.05, conf = 0.95, na.rm = FALSE)

Arguments

x

A numeric vector (or a one-column data frame).

trim

Fraction trimmed from each tail, in [0, 0.5).

conf

Confidence level in (0, 1).

na.rm

Logical. Drop missing values? If FALSE (default) a missing value is an error (equivalent to nan_policy = "raise" in the Python package; TRUE is nan_policy = "omit").

Details

Uses T \pm t_{h-1} SE, with h = n - 2k and the standard error of se_trimmed.

Value

A named numeric vector with lower and upper.

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
ci_trimmed(x, trim = 0.05)

Coefficient of variation (percent)

Description

100\, s / \bar{x}. Needs at least 2 observations.

Usage

cv(x, na.rm = FALSE)

Arguments

x

A numeric vector.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number (in percent).

Examples

cv(c(2, 4, 4, 5, 7, 9, 30))

DAM: median absolute deviation about the median (not rescaled)

Description

Unlike stats::mad, the result is not multiplied by 1.4826 (this is the "DAM" of Statgraphics).

Usage

dam(x, na.rm = FALSE)

Arguments

x

A numeric vector.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number.

See Also

sbi

Examples

dam(c(2, 4, 4, 5, 7, 9, 30))

Diagnose integer truncation of the trimmed mean

Description

Compares sgmean with base R's mean(x, trim = trim), which silently uses floor(trim * n) observations per tail.

Usage

diagnose_truncation(x, trim = 0.05, na.rm = FALSE)

Arguments

x

A numeric vector without missing values.

trim

Fraction trimmed from each tail, in [0, 0.5).

na.rm

Logical. Drop missing values? If FALSE (default) a missing value is an error.

Value

An object of class "sg_truncation": a list with n, trim, k (trim \times n), k_floor, delta, effective_trim, proportional, floor_mean, difference (floor_mean - proportional), relative_difference and truncated (TRUE when k is not an integer).

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
diagnose_truncation(x, trim = 0.05)

Robust standardized difference of trimmed means

Description

d = c(trim) (T_x - T_y) / S_w (Algina, Keselman and Penfield, 2005), with S_w the pooled winsorized standard deviation and c(trim) the standard deviation of the winsorized standard normal (0.642 at 20 %). With trim = 0 it is Cohen's d with the pooled standard deviation. For fractional k the winsorized variance is interpolated between \lfloor k \rfloor and \lfloor k \rfloor + 1 (a heuristic, like the standard error).

Usage

effect_size_trimmed(x, y, trim = 0.05, na.rm = FALSE)

Arguments

x, y

Numeric vectors.

trim

Fraction trimmed from each tail, in [0, 0.5).

na.rm

Logical. Drop missing values? If FALSE (default) a missing value is an error (equivalent to nan_policy = "raise" in the Python package; TRUE is nan_policy = "omit").

Value

A single number.

References

Algina, J., Keselman, H. J. and Penfield, R. D. (2005). An alternative to Cohen's standardized mean difference effect size. Psychological Methods, 10, 317-328.

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
y <- c(900, 1010, 1100, 1180, 1230, 1300, 1390, 1450, 1520, 1600, 1680,
       1760, 1900, 2050, 2200, 2400, 2650, 3000, 3400, 4100, 5200, 9800)
effect_size_trimmed(x, y, trim = 0.1)

Geometric mean

Description

\exp(\mathrm{mean}(\log x)); all observations must be strictly positive.

Usage

geometric_mean(x, na.rm = FALSE)

Arguments

x

A numeric vector.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number.

Examples

geometric_mean(c(1, 2, 4, 8))

Interquartile range

Description

Difference between the third and the first quartile computed with quartiles (default type = 2). Note that stats::IQR uses type 7 by default.

Usage

iqr(x, type = 2L, na.rm = FALSE)

Arguments

x

A numeric vector.

type

Integer from 1 to 9. Default 2.

na.rm

Logical. If FALSE (default) an NA in x is an error; if TRUE they are dropped.

Value

A single number.

See Also

quartiles

Examples

iqr(c(3, 5, 7, 8, 12, 13, 14, 18, 21))

Adjusted excess kurtosis (G2)

Description

Needs at least 4 observations. With standardized = TRUE the value is divided by \sqrt{24/n}.

Usage

kurtosis(x, standardized = FALSE, na.rm = FALSE)

Arguments

x

A numeric vector.

standardized

Logical. Default FALSE.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number.

See Also

skewness

Examples

kurtosis(c(2, 4, 4, 5, 7, 9, 30))

Plot the trimmed mean against the trim proportion

Description

Draws the proportional trimmed mean (smooth) and base R's mean(x, trim = ) (step function, integer truncation) over a grid of trims.

Usage

plot_trim_profile(x, trim = NULL, trims = NULL, na.rm = FALSE, ...)

Arguments

x

A numeric vector (or one-column data frame).

trim

Optional chosen trim, drawn as a vertical dotted line.

trims

Optional grid of trims in [0, 0.5); default 200 points.

na.rm

Logical. Drop missing values? Default FALSE (error).

...

Further arguments passed to plot.

Value

Invisibly, a data frame with columns trim, proportional and floor_mean.

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
plot_trim_profile(x, trim = 0.05)

Quartiles (Q1, median, Q3)

Description

Quartiles (Q1, median, Q3)

Usage

quartiles(x, type = 2L, na.rm = FALSE)

Arguments

x

A numeric vector.

type

Integer from 1 to 9. Default 2.

na.rm

Logical. If FALSE (default) an NA in x is an error; if TRUE they are dropped.

Value

A named numeric vector with Q1, median and Q3. The default type = 2 matches Statgraphics.

See Also

sg_quantile

Examples

quartiles(c(3, 5, 7, 8, 12, 13, 14, 18, 21))

Tukey biweight scale (Sbi)

Description

Square root of the biweight midvariance. Centre = median, scale = unscaled MAD, tuning constant c = 9. Returns NA when the MAD is zero (Statgraphics leaves the value blank).

Usage

sbi(x, c = 9, na.rm = FALSE)

Arguments

x

A numeric vector.

c

Tuning constant. Default 9.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number, or NA_real_.

See Also

dam

Examples

sbi(c(2, 4, 4, 5, 7, 9, 30))

Standard error of the proportional trimmed mean

Description

Standard error of sgmean when k = trim \times n is not an integer ("method D").

Usage

se_trimmed(x, trim = 0.05, na.rm = FALSE)

Arguments

x

A numeric vector (or a one-column data frame).

trim

Fraction trimmed from each tail, in [0, 0.5).

na.rm

Logical. Drop missing values? If FALSE (default) a missing value is an error (equivalent to nan_policy = "raise" in the Python package; TRUE is nan_policy = "omit").

Details

Let g = \lfloor k \rfloor, \delta = k - g and h = n - 2k. With SSW(m) the sum of squared deviations of the sample winsorized with m values replaced at each tail, SE(m) = \sqrt{SSW(m) / (h (h - 1))} and

SE = (1 - \delta) SE(g) + \delta SE(g + 1).

When k is an integer this is the classical Tukey-McLaughlin standard error; it is continuous in trim. For fractional k the interpolation is a heuristic supported by simulation, not a proven result. A warning is given when n - 2k < 8.

Value

A single number.

References

Tukey, J. W. and McLaughlin, D. H. (1963). Less vulnerable confidence and significance procedures for location based on a single sample: trimming / winsorization. Sankhya A, 25, 331-352.

See Also

ci_trimmed, ttest_trimmed

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
se_trimmed(x, trim = 0.05)

Set the default language of printed tables

Description

Only the labels of the printed tables (sg_summary) are translated; list names and function names do not change. Priority, from highest to lowest: the lang argument of the printing call, set_language(), the SGMEAN_LANG environment variable, and English. "auto" follows the system locale.

Usage

set_language(lang)

Arguments

lang

"en", "es", "auto" or NULL to reset.

Value

Invisibly, the previous setting.

Examples

old <- set_language("es")
set_language(old)

Mode (unique most frequent value)

Description

Statgraphics shows the mode only when exactly one value is the most frequent (also when it occurs just twice); it is left blank when every value is different or when several values tie. In those cases this function returns NA_real_. Called sg_mode so that it does not mask base::mode.

Usage

sg_mode(x, na.rm = FALSE)

Arguments

x

A numeric vector.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number or NA_real_.

Examples

sg_mode(c(1, 2, 2, 3))   # 2
sg_mode(c(1, 1, 2, 2))   # NA (tie)

Sample quantiles with the nine Hyndman-Fan definitions

Description

Quantiles of x using Hyndman and Fan (1996) definition type (1 to 9, the numbering of quantile). The default type = 2 reproduces the quartiles of the Statgraphics "Summary Statistics" procedure in every case tested; base R and NumPy default to type 7, which gives different values.

Usage

sg_quantile(x, probs = c(0.25, 0.5, 0.75), type = 2L, na.rm = FALSE)

Arguments

x

A numeric vector.

probs

Probabilities in [0, 1].

type

Integer from 1 to 9. Default 2.

na.rm

Logical. If FALSE (default) an NA in x is an error; if TRUE they are dropped.

Details

Position rule for type = 2: with the sorted sample and a probability p, the position is np + 1/2. If it is an integer the quantile is that order statistic; if it ends in .5 it is the mean of the two neighbouring order statistics; otherwise it is the observation at the nearest position. This equals x_{(\lceil np \rceil)} when np is not an integer and the mean of x_{(np)} and x_{(np+1)} when it is. It shows that Statgraphics behaves consistently with type 2 on the data sets tested; it does not prove which formula the program uses internally.

The function is called sg_quantile so that it does not mask stats::quantile.

Value

A numeric vector of quantiles, one per element of probs.

References

Hyndman, R. J. and Fan, Y. (1996). Sample quantiles in statistical packages. The American Statistician, 50(4), 361-365.

See Also

quartiles, iqr

Examples

x <- c(2.35, 3.10, 4.72, 5.05, 6.48, 7.91, 8.26, 9.63, 11.07, 12.84, 15.30, 21.75)
sg_quantile(x, c(1/6, 5/6))   # 5/6 sextile = 14.07 (Statgraphics)
sg_quantile(x, 0.25, type = 7) # base R default, different value

Statgraphics-style summary statistics

Description

All the "Summary Statistics" results of Statgraphics Centurion in one call: 29 values per variable. x can be a numeric vector (one variable) or a data frame, a named list of columns or a matrix with several columns (one column of results per variable). The function is called sg_summary so that it does not mask the generic base::summary.

Usage

sg_summary(x, trim = 0.05, quartile_type = 2L, lang = NULL, na.rm = FALSE)

Arguments

x

A numeric vector, data frame, list or matrix.

trim

Trimming fraction in [0, 0.5). Default 0.05.

quartile_type

Hyndman-Fan type for quartiles and sextiles. Default 2.

lang

Language of the printed labels: "en", "es" or "auto"; see set_language.

na.rm

Logical. If FALSE (default) an NA is an error; if TRUE each variable drops its own NA.

Details

trim applies to the proportional trimmed mean (sgmean) and to the winsorized mean and sigma (integer floor(trim * n), as Statgraphics does). Skewness needs n \ge 3 and kurtosis n \ge 4; they are NA otherwise. The mode and Sbi are NA when Statgraphics leaves them blank.

Value

For one variable an object of class "sg_summary" (a named list of 29 numbers that prints as an aligned table). For several variables an object of class "sg_summary_multi" (a list of "sg_summary"), which can be converted with as.data.frame().

See Also

set_language

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250, 1320, 1400, 1480, 1550,
       1700, 1850, 2100, 8500)
s <- sg_summary(x, trim = 0.10)
s$trimmed_mean
s
print(s, lang = "es")
sg_summary(data.frame(a = x, b = rev(x)))

Proportional Trimmed Mean

Description

Computes a proportional trimmed mean that resolves the integer truncation problem of base R's mean(..., trim). When k = \text{trim} \times n is non-integer, a fractional discount (1 - \delta) is applied to boundary observations, where \delta = k - \lfloor k \rfloor.

Usage

sgmean(x, trim = 0.05, na.rm = FALSE)

Arguments

x

A numeric vector.

trim

Fraction of observations to trim symmetrically from each end of the sorted data. Must be in [0, 0.5).

na.rm

Logical. Should missing values be removed before computation? Default is FALSE; if FALSE and x contains NA, the result is NA, as in mean().

Details

The proportional trimmed mean generalizes mean(..., trim) by treating k = \text{trim} \times n as a continuous parameter. Three cases are handled:

k = 0

Returns the arithmetic mean.

0 < k < 1 (Type A)

Applies a discount (1-k) to X_{(1)} and X_{(n)}.

k exact integer

Reduces to mean(..., trim), producing identical results to R base.

k > 1, non-integer (Type B)

Eliminates \lfloor k \rfloor observations from each tail and applies a fractional discount (1-\delta) to the new boundary observations, where \delta = k - \lfloor k \rfloor.

Value

A single numeric scalar.

References

Gaviria Chaverra, J. C. (2026). sgmean: A Proportional Trimmed Mean for R Compatible with Statgraphics. The R Journal.

Examples

# Type A distortion: k = 0.05 * 15 = 0.75
x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
mean(x, trim = 0.05)    # 1816.667 — no trimming applied (k* = 0)
sgmean(x, trim = 0.05)  # 1499.074 — proportional discount applied

# Type B distortion: k = 0.10 * 15 = 1.50
mean(x, trim = 0.10)    # 1376.923 — effective trim 6.7%
sgmean(x, trim = 0.10)  # 1365.833 — effective trim 10% (Statgraphics)

# Control: k = 1/15 * 15 = 1 (exact integer)
mean(x, trim = 1/15)    # identical to sgmean
sgmean(x, trim = 1/15)  # identical to mean(..., trim)

Adjusted Fisher-Pearson skewness (G1)

Description

Needs at least 3 observations. With standardized = TRUE the value is divided by \sqrt{6/n}.

Usage

skewness(x, standardized = FALSE, na.rm = FALSE)

Arguments

x

A numeric vector.

standardized

Logical. Default FALSE.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number.

See Also

kurtosis

Examples

skewness(c(2, 4, 4, 5, 7, 9, 30))

Standard error of the mean

Description

s / \sqrt{n}. Needs at least 2 observations. For the standard error of the trimmed mean see se_trimmed.

Usage

std_error(x, na.rm = FALSE)

Arguments

x

A numeric vector.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number.

Examples

std_error(c(2, 4, 4, 5, 7, 9, 30))

Two-sample (Yuen-type) test with proportional trimming

Description

t = (T_x - T_y) / \sqrt{se_x^2 + se_y^2} with the standard errors of se_trimmed and Welch-Satterthwaite degrees of freedom based on h - 1. With integer k in both samples it reduces to Yuen's (1974) test; for fractional k it is a heuristic extension supported by simulation.

Usage

ttest_ind_trimmed(
  x,
  y,
  trim = 0.05,
  conf = 0.95,
  alternative = c("two.sided", "less", "greater"),
  na.rm = FALSE
)

Arguments

x, y

Numeric vectors.

trim

Fraction trimmed from each tail (same for both samples).

conf

Confidence level of the interval for the difference.

alternative

"two.sided", "less" or "greater" (for x - y).

na.rm

Logical. Drop missing values? If FALSE (default) a missing value is an error (equivalent to nan_policy = "raise" in the Python package; TRUE is nan_policy = "omit").

Value

An object of class "htest"; it also carries effect_size (see effect_size_trimmed) when defined.

References

Yuen, K. K. (1974). The two-sample trimmed t for unequal population variances. Biometrika, 61, 165-170.

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
y <- c(900, 1010, 1100, 1180, 1230, 1300, 1390, 1450, 1520, 1600, 1680,
       1760, 1900, 2050, 2200, 2400, 2650, 3000, 3400, 4100, 5200, 9800)
ttest_ind_trimmed(x, y, trim = 0.05)

Paired t test for the proportional trimmed mean

Description

One-sample test on the differences x - y. It tests whether the trimmed mean of the differences is zero; this is in general not the difference of the two trimmed means.

Usage

ttest_rel_trimmed(
  x,
  y,
  trim = 0.05,
  conf = 0.95,
  alternative = c("two.sided", "less", "greater"),
  na.rm = FALSE
)

Arguments

x, y

Numeric vectors of the same length (paired observations).

trim

Fraction trimmed from each tail, in [0, 0.5).

conf

Confidence level of the interval stored in the result.

alternative

"two.sided" (default), "less" or "greater"; one-sided tests give a one-sided interval, as t.test.

na.rm

Logical. Drop pairs in which either value is missing?

Value

An object of class "htest".

Examples

before <- c(12.1, 14.3, 11.8, 15.2, 13.9, 12.7, 16.0, 14.8, 13.1, 12.5)
after  <- c(11.2, 13.9, 11.9, 14.1, 12.8, 12.9, 14.7, 13.5, 12.2, 12.0)
ttest_rel_trimmed(before, after, trim = 0.1)

One-sample t test for the proportional trimmed mean

Description

Tests H_0: trimmed mean = mu0, using se_trimmed and a t distribution with h - 1 degrees of freedom.

Usage

ttest_trimmed(
  x,
  mu0 = 0,
  trim = 0.05,
  conf = 0.95,
  alternative = c("two.sided", "less", "greater"),
  na.rm = FALSE
)

Arguments

x

A numeric vector (or a one-column data frame).

mu0

Hypothesized value.

trim

Fraction trimmed from each tail, in [0, 0.5).

conf

Confidence level of the interval stored in the result.

alternative

"two.sided" (default), "less" or "greater"; one-sided tests give a one-sided interval, as t.test.

na.rm

Logical. Drop missing values? If FALSE (default) a missing value is an error (equivalent to nan_policy = "raise" in the Python package; TRUE is nan_policy = "omit").

Value

An object of class "htest".

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250,
       1320, 1400, 1480, 1550, 1700, 1850, 2100, 8500)
ttest_trimmed(x, mu0 = 1000, trim = 0.05)
ttest_trimmed(x, mu0 = 1000, trim = 0.05, alternative = "greater")

Winsorized mean

Description

Mean of x after replacing floor(trim * n) observations at each tail: each of the k smallest values is replaced by the (k+1)-th smallest and each of the k largest by the (k+1)-th largest. Statgraphics Centurion was found to behave this way (integer truncation, no fractional treatment) on the data sets tested.

Usage

winsorized_mean(x, trim = 0.05, na.rm = FALSE)

Arguments

x

A numeric vector.

trim

Fraction replaced in each tail, in [0, 0.5). Default 0.05.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number.

See Also

winsorized_sigma, sgmean

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250, 1320, 1400, 1480, 1550,
       1700, 1850, 2100, 8500)
winsorized_mean(x, trim = 0.10)

Winsorized standard deviation

Description

Sample standard deviation of the winsorized sample multiplied by \sqrt{(n-1)/(n-2k-1)}, with k = \lfloor trim \cdot n \rfloor, as reported by Statgraphics.

Usage

winsorized_sigma(x, trim = 0.05, na.rm = FALSE)

Arguments

x

A numeric vector.

trim

Fraction replaced in each tail, in [0, 0.5). Default 0.05.

na.rm

Logical. If FALSE (default) an NA is an error.

Value

A single number.

See Also

winsorized_mean

Examples

x <- c(850, 920, 980, 1050, 1120, 1180, 1250, 1320, 1400, 1480, 1550,
       1700, 1850, 2100, 8500)
winsorized_sigma(x, trim = 0.10)