treat_outliers: Treats Outliers in Numerical Data

Description Usage Arguments Details Value Examples

View source: R/treat_outliers.R

Description

Treats numerical outliers either by winsorizing or by truncating.

Usage

1
treat_outliers(x, percentile = 0.01, truncate = FALSE, by = NULL, ...)

Arguments

x

Data that is coercible into a numeric vector or matrix. If it is a data frame then all numerical variables of the data frame are coerced into a matrix.

percentile

A numeric scalar. The percentile below which observations are considered to be outliers. Is treated symmetrical so that c(percentile, 1-percentile) are used as boundaries. Defaults to 0.01 and needs to be > 0 and < 0.5.

truncate

A logical scalar. If TRUE then data are truncated (i.e., set to NA if out of bounds). Defaults to FALSE.

by

NULL or either a factor vector or a character string identifying a factor variable in the data frame provided by x. The factor indicated by 'by' is being used to identify groups by which the outlier treatment is applied. Defaults to NULL (no grouping). If provided, the resulting vector must not contain NAs and needs to be such so that length(byvec) == nrows(as.matrix(x)).

...

Additional parameters forwarded to quantile (notably, type)

Details

All members of the numerical matrix are checked for finiteness and are set to NA if they are not finite. NAs are removed when calculating the outlier cut-offs.

Value

A numeric vector or matrix containing the outlier-treated x. if a data frame was provided in x, a data frame with its numeric variables replaced by their outlier-treated values.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
treat_outliers(seq(1:100), 0.05)
treat_outliers(seq(1:100), truncate = TRUE, 0.05)

# When you like the percentiles calculated like STATA's summary or pctile:
treat_outliers(seq(1:100), 0.05, type = 2)

df <- data.frame(a = seq(1:1000), b = rnorm(1000), c = sample(LETTERS[1:5], 1000, replace=TRUE))
winsorized_df <- treat_outliers(df)
summary(df)
summary(winsorized_df)

winsorized_df <- treat_outliers(df, 0.05, by="c")
by(df, df$c, summary)
by(winsorized_df, df$c, summary)

hist(treat_outliers(rnorm(1000)), breaks=100)

joachim-gassen/ExPanDaR documentation built on April 15, 2021, 6:07 p.m.