Skip to contents

This function implements a *Step Function*. See the **Details** section below for further information.

Usage

stepFunction(x, y = c(-1, 0, 1), thr = 0, by = c("rows", "cols"))

Arguments

x

a (named) numerical vector or a matrix features-by-samples

y

numerical vector of length 3, the values to use for the piecewise function. The first value is used for the left interval, the second value is used when x == threshold, and the third value is used for the right interval

thr

numerical value, the threshold to use to divide values in x in two intervals

by

character string, indicating whether to apply the threshold to rows or columns of x. In the first case, the threshold for the \(i\)-th row is the same across columns. In the second case, the threshold is the same within the same column vector. It is used when x is a matrix

Value

A numerical vector or matrix, containing the output of the step function for each element of x. The default settings of thr and y

makes this function equivalent to a sign function.

Details

This function implements a piecewise function that is defined as follows:

$$SF = f(x) = \left\{ \begin{array}{l} y_{1}, \quad \textrm{if} \quad x > threshold \\ y_{2}, \quad \textrm{if} \quad x = threshold \\ y_{3}, \quad \textrm{if} \quad x < threshold \end{array} \right\} $$

Author

Alessandro Barberis

Examples

#set seed for reproducibility
set.seed(seed = 5381L)

#x is a vector
x = sample(x = -10:10, size = 10)
stepFunction(x, thr = 0)
#>  [1] -1 -1  1 -1  1 -1 -1  1  1  0

#x is a matrix
#Define row/col size
nr = 20
nc = 10

#Create input matrix
x = matrix(
 data = stats::runif(n = nr*nc, min = 0, max = 1000),
 nrow = nr,
 ncol = nc,
 dimnames = list(
   paste0("g",seq(nr)),
   paste0("S",seq(nc))
 )
)
#compute
stepFunction(x, thr = 500)
#>     S1 S2 S3 S4 S5 S6 S7 S8 S9 S10
#> g1  -1  1 -1 -1 -1 -1  1 -1 -1   1
#> g2   1 -1  1  1 -1  1  1 -1 -1  -1
#> g3   1  1 -1 -1 -1 -1 -1  1  1  -1
#> g4  -1  1 -1  1  1 -1 -1  1 -1  -1
#> g5   1  1  1 -1  1 -1  1 -1  1  -1
#> g6  -1 -1 -1  1 -1  1  1  1 -1   1
#> g7   1  1 -1 -1 -1 -1 -1  1 -1   1
#> g8  -1 -1 -1  1  1 -1 -1  1 -1   1
#> g9  -1  1  1 -1  1  1 -1  1 -1  -1
#> g10 -1 -1  1  1  1 -1  1 -1 -1   1
#> g11 -1  1  1  1 -1  1  1 -1  1   1
#> g12  1  1 -1 -1  1  1  1  1  1  -1
#> g13 -1 -1  1 -1  1  1 -1  1 -1  -1
#> g14 -1 -1 -1 -1  1 -1 -1 -1 -1  -1
#> g15  1  1 -1  1 -1  1  1  1  1   1
#> g16 -1 -1 -1  1  1  1  1  1 -1   1
#> g17  1 -1  1  1  1 -1 -1  1  1   1
#> g18  1  1 -1  1 -1  1 -1  1  1  -1
#> g19  1 -1  1  1  1 -1  1  1 -1   1
#> g20  1  1 -1  1 -1  1 -1  1  1  -1