Takes a vector, matrix or data frame and replaces some numeric values by outliers.
generateOutliers(x, p = 0.05, sd_factor = 5, seed = NULL)
A vector, matrix or data.frame
.
Proportion of outliers to add to x
. In case x
is a data.frame
, p
can
also be a vector of probabilities per column or a named vector (see examples).
Each outlier is generated by shifting the original value by a
realization of a normal random variable with sd_factor
times
the original sample standard deviation.
An integer seed.
x
with outliers.
generateOutliers(1:10, seed = 334, p = 0.3)
#> [1] -12.525043 2.000000 3.000000 4.000000 -5.546526 6.000000
#> [7] 7.000000 8.000000 22.458611 10.000000
generateOutliers(cbind(1:10, 10:1), p = 0.2)
#> [,1] [,2]
#> [1,] 1.00000 10.000000
#> [2,] 2.00000 9.000000
#> [3,] 3.00000 8.000000
#> [4,] 4.00000 7.000000
#> [5,] 24.12636 6.000000
#> [6,] 6.00000 5.000000
#> [7,] 7.00000 4.000000
#> [8,] 8.00000 -12.074577
#> [9,] 9.00000 10.836409
#> [10,] 10.00000 -9.730771
head(generateOutliers(iris))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.100000 3.500000 1.4 0.2 setosa
#> 2 4.900000 3.000000 1.4 0.2 setosa
#> 3 4.700000 3.200000 1.3 0.2 setosa
#> 4 4.600000 4.943788 1.5 0.2 setosa
#> 5 5.000000 3.600000 1.4 0.2 setosa
#> 6 -5.855577 3.900000 1.7 0.4 setosa
head(generateOutliers(iris, p = 0.2))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.500000 1.40000 0.2000000 setosa
#> 2 4.9 3.000000 1.40000 -0.6984204 setosa
#> 3 4.7 3.200000 1.30000 0.2000000 setosa
#> 4 4.6 8.002914 1.50000 0.2000000 setosa
#> 5 5.0 3.600000 -13.17379 0.2000000 setosa
#> 6 5.4 3.900000 1.70000 6.7699826 setosa
head(generateOutliers(iris, p = c(0, 0, 0.5, 0.5, 0.5)))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 -6.3561338 9.850624 setosa
#> 2 4.9 3.0 1.4000000 2.472597 setosa
#> 3 4.7 3.2 1.3000000 0.200000 setosa
#> 4 4.6 3.1 -0.4460012 9.622920 setosa
#> 5 5.0 3.6 17.0968761 -6.240265 setosa
#> 6 5.4 3.9 1.7000000 10.023424 setosa
head(generateOutliers(iris, p = c(Sepal.Length = 0.2)))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa