centr: Centers of classes

View source: R/centr.R

centrR Documentation

Centers of classes

Description

Calculation of the centers (eg. mean or median) of classes of row observations of a data set.

Usage

centr(X, Y = NULL, FUN = mean, ...)

Arguments

X

A n x p matrix or data frame (usually spectra) on which are calculated the centers.

Y

A vector of length n defining the class membership of the row observations. Default to NULL (all the rows of are considered).

FUN

The function used in the aggregation (Default to mean).

...

Optionnal arguments to pass in the function defined in FUN.

Details

centr uses function aggregate.

Value

A list of outputs.

Examples


n <- 8
p <- 6
set.seed(1)
X <- matrix(rnorm(n * p, mean = 10), ncol = p, byrow = TRUE)
y <- sample(1:2, size = n, replace = TRUE)
set.seed(NULL)

centr(X, y)
centr(X, y, FUN = median)

data(datforages)

Xr <- datforages$Xr
yr <- datforages$yr
table(yr)

u <- centr(Xr, yr)$centers
v <- centr(Xr, yr, FUN = median)$centers
headm(u)
headm(v)

oldpar <- par(mfrow = c(1, 1))
par(mfrow = c(1, 2))
plotsp(u, col = 1:4, main = "Mean")
plotsp(v, col = 1:4, main = "Median")
par(oldpar)

x <- Xr[1:20, ]
plotsp(x, ylab = "Absorbance", col = "grey")
u <- centr(x)$centers
plotsp(u, col = "red", add = TRUE, lwd = 2)


mlesnoff/rnirs documentation built on April 24, 2023, 4:17 a.m.