| Title: | Discrete Distribution Data Analysis with Wasserstein Distance |
|---|---|
| Description: | S4 classes and methods for the analysis of data described by discrete distributions with finite support (e.g. frequency distributions of ratings, scores or counts observed on groups of individuals), using the L2 Wasserstein metric between quantile functions. It is the discrete counterpart of 'HistDAWass' and provides exact Wasserstein distances (in C++), barycenters, Wasserstein variance, covariance and correlation, k-means and hierarchical clustering, quantile-based principal component and multiple factor analysis, two-component linear regression and reliability (Cronbach's alpha) indices. See Crocetta, Irpino, Antonucci and Marin (2025) <doi:10.1007/s11135-024-01956-0>. |
| Authors: | Antonio Irpino [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-9293-7180>) |
| Maintainer: | Antonio Irpino <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.0 |
| Built: | 2026-11-05 01:27:58 UTC |
| Source: | https://github.com/cran/DDAWASS |
Extract a sub-matrix of a MatD
## S4 method for signature 'MatD,ANY,ANY,ANY' x[i, j, ..., drop = FALSE]## S4 method for signature 'MatD,ANY,ANY,ANY' x[i, j, ..., drop = FALSE]
x |
a |
i, j
|
row and column indices (numbers, names or logicals). |
... |
unused. |
drop |
ignored: the result is always a |
a MatD.
Satisfaction[1:3, c("Staff", "Price")]Satisfaction[1:3, c("Staff", "Price")]
Operations are defined on quantile functions, consistently with the
L2 Wasserstein geometry: the sum of two distributions is the distribution
whose quantile function is ; a distribution plus a
number is a shift; a distribution times a number is a rescaling (a
negative number also reverses the support, ).
## S4 method for signature 'distributionD,distributionD' e1 + e2## S4 method for signature 'distributionD,distributionD' e1 + e2
e1, e2
|
|
a distributionD.
a <- distributionD(x = 1:3, p = c(0.2, 0.5, 0.3)) b <- distributionD(x = c(10, 20), p = c(0.5, 0.5)) a + b a * 2 + 1a <- distributionD(x = 1:3, p = c(0.2, 0.5, 0.3)) b <- distributionD(x = c(10, 20), p = c(0.5, 0.5)) a + b a * 2 + 1
Methods of the HistDAWass generics compQ and compP
for distributionD objects. The quantile function is
the left-continuous inverse of the cdf, ,
with .
## S4 method for signature 'distributionD,ANY' compQ(object, p) ## S4 method for signature 'distributionD,ANY' compP(object, q)## S4 method for signature 'distributionD,ANY' compQ(object, p) ## S4 method for signature 'distributionD,ANY' compP(object, q)
object |
a |
p |
a vector of probability levels in |
q |
a vector of values. |
a numeric vector.
d <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) compQ(d, c(0, 0.25, 0.5, 0.75, 1)) compP(d, c(2, 3.5))d <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) compQ(d, c(0, 0.25, 0.5, 0.75, 1)) compP(d, c(2, 3.5))
Each row of data is an observation (e.g. a respondent) belonging to
a group defined by one or more categorical columns (unit); for each
group and each variable in vars the frequency distribution of the
observed values is computed.
data2MatD(data, unit, vars, support = NULL, min.n = 1, sep = "_")data2MatD(data, unit, vars, support = NULL, min.n = 1, sep = "_")
data |
a |
unit |
name(s) or index(es) of the grouping column(s). With several
columns, groups are their combinations (labels joined by |
vars |
names or indices of the discrete numeric variables. |
support |
(optional) a list (one element per variable, or a single
vector for all) with the admissible values (e.g. |
min.n |
minimum number of observations for a group to be kept (groups with fewer rows are dropped with a message). |
sep |
separator used to build the labels of combined groups. |
a MatD with one row per group. The number of observations
of each distribution is stored in its n slot (see
get.MatD.stats(x, "n")).
set.seed(2) raw <- data.frame(shop = rep(c("A", "B", "C"), each = 50), gender = sample(c("F", "M"), 150, replace = TRUE), q1 = sample(1:5, 150, replace = TRUE), q2 = sample(0:10, 150, replace = TRUE)) data2MatD(raw, unit = "shop", vars = c("q1", "q2")) data2MatD(raw, unit = c("shop", "gender"), vars = c("q1", "q2"), support = list(1:5, 0:10), min.n = 20)set.seed(2) raw <- data.frame(shop = rep(c("A", "B", "C"), each = 50), gender = sample(c("F", "M"), 150, replace = TRUE), q1 = sample(1:5, 150, replace = TRUE), q2 = sample(0:10, 150, replace = TRUE)) data2MatD(raw, unit = "shop", vars = c("q1", "q2")) data2MatD(raw, unit = c("shop", "gender"), vars = c("q1", "q2"), support = list(1:5, 0:10), min.n = 20)
Builds a distributionD object. Three ways to call it:
distributionD(x, p): support values and probabilities;
distributionD(x, counts = k): support values and absolute
frequencies;
distributionD(obs): a vector of raw observations, which is
tabulated.
Duplicated support values are merged, values with zero mass are dropped and the probabilities are normalised to sum to one.
distributionD(x = numeric(0), p = NULL, counts = NULL, keep.zeros = FALSE)distributionD(x = numeric(0), p = NULL, counts = NULL, keep.zeros = FALSE)
x |
numeric vector of support values, or of raw observations when
both |
p |
(optional) numeric vector of probabilities/relative frequencies. |
counts |
(optional) numeric vector of absolute frequencies. |
keep.zeros |
logical; keep support values with zero mass? Default
|
A distributionD object.
# from support and probabilities d1 <- distributionD(x = 1:5, p = c(0.1, 0.2, 0.4, 0.2, 0.1)) # from counts d2 <- distributionD(x = 0:10, counts = c(1, 0, 2, 3, 5, 10, 12, 20, 25, 15, 7)) # from raw observations d3 <- distributionD(c(3, 4, 4, 5, 5, 5, 2, 4)) d1; meanD(d2); stdD(d3)# from support and probabilities d1 <- distributionD(x = 1:5, p = c(0.1, 0.2, 0.4, 0.2, 0.1)) # from counts d2 <- distributionD(x = 0:10, counts = c(1, 0, 2, 3, 5, 10, 12, 20, 25, 15, 7)) # from raw observations d3 <- distributionD(c(3, 4, 4, 5, 5, 5, 2, 4)) d1; meanD(d2); stdD(d3)
Class distributionD describes a discrete distribution with a finite
support (e.g. the frequency distribution of a Likert/0-10 rating, of a
count, or of any variable observed on a finite set of numeric values).
It is the discrete counterpart of the distributionH class of
HistDAWass.
xnumeric vector: the support values, sorted in increasing order and without duplicates.
pnumeric vector: the probability masses (relative frequencies)
associated with x; they are positive and sum to one.
cdfnumeric vector: the cumulative distribution function evaluated
at x (cumsum(p), last value equal to 1).
nnumeric: the number of observations the distribution was computed
from (NA when unknown).
mnumeric: the mean of the distribution.
snumeric: the standard deviation of the distribution.
distributionD for the constructor.
dotpD(a, b) computes , the
building block of Wasserstein-based covariances.
dotpD(a, b)dotpD(a, b)
a, b
|
two |
a number.
a <- distributionD(x = 1:3, p = c(0.2, 0.5, 0.3)) b <- distributionD(x = c(2, 4), p = c(0.5, 0.5)) dotpD(a, b)a <- distributionD(x = 1:3, p = c(0.2, 0.5, 0.3)) b <- distributionD(x = c(2, 4), p = c(0.5, 0.5)) dotpD(a, b)
Returns a matrix with a summary statistic computed for each cell.
get.MatD.stats(object, stat = "mean", prob = 0.5)get.MatD.stats(object, stat = "mean", prob = 0.5)
object |
a |
stat |
one of |
prob |
probability level used when |
a list with elements stat and mat.
get.MatD.stats(Satisfaction)$mat[1:5, ] get.MatD.stats(Satisfaction, stat = "quantile", prob = 0.1)$mat[1:5, ]get.MatD.stats(Satisfaction)$mat[1:5, ] get.MatD.stats(Satisfaction, stat = "quantile", prob = 0.1)$mat[1:5, ]
Build a MatD from a long frequency table
long2MatD( data, unit = "unit", variable = "variable", value = "value", freq = "freq" )long2MatD( data, unit = "unit", variable = "variable", value = "value", freq = "freq" )
data |
a |
unit, variable, value, freq
|
names of the columns with the row labels, the variable names, the support values and the (absolute or relative) frequencies. |
a MatD.
tab <- expand.grid(unit = c("u1", "u2"), variable = c("A", "B"), value = 1:3) tab$freq <- c(5, 1, 2, 2, 3, 3, 2, 2, 2, 6, 6, 6) long2MatD(tab)tab <- expand.grid(unit = c("u1", "u2"), variable = c("A", "B"), value = 1:3) tab$freq <- c(5, 1, 2, 2, 3, 3, 2, 2, 2, 6, 6, 6) long2MatD(tab)
Constructor of a matrix of discrete distributions
MatD( x = list(), nrows = 1, ncols = 1, rownames = NULL, varnames = NULL, by.row = FALSE )MatD( x = list(), nrows = 1, ncols = 1, rownames = NULL, varnames = NULL, by.row = FALSE )
x |
a list of |
nrows |
number of rows (units). |
ncols |
number of columns (variables). |
rownames |
(optional) names of the rows. |
varnames |
(optional) names of the columns. |
by.row |
logical; if |
A MatD object.
d <- list(distributionD(c(1, 2, 2, 3)), distributionD(c(2, 3, 3, 3)), distributionD(c(1, 1, 2)), distributionD(c(3, 3, 2))) MatD(d, nrows = 2, ncols = 2, rownames = c("A", "B"), varnames = c("Q1", "Q2"))d <- list(distributionD(c(1, 2, 2, 3)), distributionD(c(2, 3, 3, 3)), distributionD(c(1, 1, 2)), distributionD(c(3, 3, 2))) MatD(d, nrows = 2, ncols = 2, rownames = c("A", "B"), varnames = c("Q1", "Q2"))
Accessors of a MatD object
get.MatD.nrows(object) get.MatD.ncols(object) get.MatD.rownames(object) get.MatD.varnames(object) get.cell.MatD(object, r, c) set.cell.MatD(object, r, c, value)get.MatD.nrows(object) get.MatD.ncols(object) get.MatD.rownames(object) get.MatD.varnames(object) get.cell.MatD(object, r, c) set.cell.MatD(object, r, c, value)
object |
a |
r, c
|
row and column index. |
value |
a |
get.MatD.nrows and get.MatD.ncols return integers,
get.MatD.rownames and get.MatD.varnames character
vectors, get.cell.MatD a distributionD.
get.MatD.nrows(Satisfaction) get.MatD.varnames(Satisfaction) get.cell.MatD(Satisfaction, 1, 1)get.MatD.nrows(Satisfaction) get.MatD.varnames(Satisfaction) get.cell.MatD(Satisfaction, 1, 1)
Class MatD is a matrix whose cells are
distributionD objects: rows are statistical units
(individuals, groups, territories, ...) and columns are discrete
distributional variables. It mirrors the MatH class of
HistDAWass.
Ma matrix of lists, each cell containing a distributionD.
Convert a MatD into a long data.frame
MatD2long(object)MatD2long(object)
object |
a |
a data.frame with columns row, variable
(factors, in the order of the MatD), x, p and
cdf.
head(MatD2long(Satisfaction))head(MatD2long(Satisfaction))
Each histogram of a HistDAWass::MatH is transformed into a discrete
distribution on n_levels classes: the masses are the probabilities
of the classes defined by the breaks. This is the transformation used to
compare continuous and discrete distributional data and to draw Green Eyes
plots and distributional heatmaps of histogram data.
MatH2MatD( object, n_levels = 10, scheme = c("variable", "global", "standardized"), breaks = NULL, values = c("index", "midpoint"), return.breaks = FALSE )MatH2MatD( object, n_levels = 10, scheme = c("variable", "global", "standardized"), breaks = NULL, values = c("index", "midpoint"), return.breaks = FALSE )
object |
a |
n_levels |
number of classes. |
scheme |
how the breaks are defined: |
breaks |
(optional) a list with a vector of breaks for each variable
(overrides |
values |
support values of the discrete distributions: |
return.breaks |
logical; if |
a MatD (or a list).
library(HistDAWass) B <- MatH2MatD(BLOOD, n_levels = 10) Blibrary(HistDAWass) B <- MatH2MatD(BLOOD, n_levels = 10) B
Plot a discrete distribution
## S4 method for signature 'distributionD' plot(x, y, type = "BAR", col = "#2E7D32", ...)## S4 method for signature 'distributionD' plot(x, y, type = "BAR", col = "#2E7D32", ...)
x |
a |
y |
unused. |
type |
|
col |
fill/line colour. |
... |
unused. |
a ggplot object.
d <- distributionD(x = 0:10, counts = c(1, 0, 2, 3, 5, 10, 12, 20, 25, 15, 7)) plot(d) plot(d, type = "CDF")d <- distributionD(x = 0:10, counts = c(1, 0, 2, 3, 5, 10, 12, 20, 25, 15, 7)) plot(d) plot(d, type = "CDF")
Draws a grid of bar charts (rows = units, columns = variables).
## S4 method for signature 'MatD' plot(x, y, type = "BAR", col = "#2E7D32", ...)## S4 method for signature 'MatD' plot(x, y, type = "BAR", col = "#2E7D32", ...)
x |
a |
y |
unused. |
type |
|
col |
fill colour. |
... |
unused. |
a ggplot object.
plot(Satisfaction[1:4, 1:4])plot(Satisfaction[1:4, 1:4])
Method of the HistDAWass generic rQQ: the correlation between
the quantile functions of two distributionD.
## S4 method for signature 'distributionD,distributionD' rQQ(e1, e2)## S4 method for signature 'distributionD,distributionD' rQQ(e1, e2)
e1, e2
|
two |
a number in .
a <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) b <- distributionD(x = 1:5, p = c(0.3, 0.3, 0.2, 0.1, 0.1)) rQQ(a, b)a <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) b <- distributionD(x = 1:5, p = c(0.3, 0.3, 0.2, 0.1, 0.1)) rQQ(a, b)
A MatD with 30 fictitious hotels (rows) and the
distributions of the 0-10 scores given by their guests to seven items and
to the overall satisfaction (columns). Data are simulated (see
data-raw/Satisfaction.R in the source package);
Satisfaction_raw contains the simulated individual answers.
SatisfactionSatisfaction
Satisfaction: a MatD with 30 rows and 8 columns
(Staff, Cleanliness, Comfort, Location,
Price, Food, WiFi, Overall).
Satisfaction_raw: a data.frame with one row per guest.
Satisfaction plot(Satisfaction[1:3, 1:3])Satisfaction plot(Satisfaction[1:3, 1:3])
simplifyD moves the mass of each support value of a distribution to
the nearest value of a given support (e.g. the categories
1:5 of a Likert scale): the value receives the masses of
all the points in ; masses
below the first midpoint go to and masses above the last one to
. Ties (points exactly at a midpoint) go to the lower value.
simplifyD(object, support = NULL, keep.zeros = TRUE)simplifyD(object, support = NULL, keep.zeros = TRUE)
object |
a |
support |
the target support values. For a |
keep.zeros |
logical; keep the support values that receive zero mass? |
The nearest-point rule gives the distribution supported on support
closest to the original one in the L2 Wasserstein distance (indeed, in any
Wasserstein distance): it is the Wasserstein projection on the set of
distributions supported on support. It is typically used to express
a barycenter (whose support is generally made of non-integer values) on the
original categories of the variable, see WD.vec.mean.simplified.
The mean of the projected distribution may differ from the original one,
by at most half of the largest gap between consecutive support values.
For a barycenter of , since
, the projection of on the support is
also a barycenter of the data constrained to have that support. A proof
is given in the file barycenter_projection.pdf installed with the
package: system.file("doc-theory", "barycenter_projection.pdf",
package = "DDAWASS").
an object of the same class as object.
b <- distributionD(x = c(1, 1.1, 1.35, 1.6, 1.8, 2.5, 2.6, 3.4, 4.9), p = c(1, 2, 4, 8, 3, 5, 10, 6, 1)) s <- simplifyD(b, support = 1:5) s WassSqDistD(b, s) # the smallest possible among distributions on 1:5b <- distributionD(x = c(1, 1.1, 1.35, 1.6, 1.8, 2.5, 2.6, 3.4, 4.9), p = c(1, 2, 4, 8, 3, 5, 10, 6, 1)) s <- simplifyD(b, support = 1:5) s WassSqDistD(b, s) # the smallest possible among distributions on 1:5
meanD, stdD, varD, skewD and kurtD
return the mean, the standard deviation, the variance, the (Pearson)
skewness and the (non-excess) kurtosis of a
distributionD.
meanD(object) stdD(object) varD(object) skewD(object) kurtD(object) ## S4 method for signature 'distributionD' meanD(object) ## S4 method for signature 'distributionD' stdD(object) ## S4 method for signature 'distributionD' varD(object) ## S4 method for signature 'distributionD' skewD(object) ## S4 method for signature 'distributionD' kurtD(object)meanD(object) stdD(object) varD(object) skewD(object) kurtD(object) ## S4 method for signature 'distributionD' meanD(object) ## S4 method for signature 'distributionD' stdD(object) ## S4 method for signature 'distributionD' varD(object) ## S4 method for signature 'distributionD' skewD(object) ## S4 method for signature 'distributionD' kurtD(object)
object |
a |
a number.
d <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) meanD(d); stdD(d); skewD(d); kurtD(d)d <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) meanD(d); stdD(d); skewD(d); kurtD(d)
The research code that preceded this package stored discrete
distributional data as a tibble whose cells are data.frames with
the support in the first column and a freq column (optionally
n and cdf). tibble2MatD converts such objects into a
MatD; MatD2tibble does the reverse.
tibble2MatD(tib, labels = NULL) MatD2tibble(object)tibble2MatD(tib, labels = NULL) MatD2tibble(object)
tib |
a tibble/data.frame of list-columns. |
labels |
(optional) name or index of a character column holding the row labels; it is excluded from the variables. |
object |
a |
a MatD (tibble2MatD) or a tibble (MatD2tibble).
tb <- MatD2tibble(Satisfaction[1:3, 1:2]) tb tibble2MatD(tb, labels = "ID_name")tb <- MatD2tibble(Satisfaction[1:3, 1:2]) tb tibble2MatD(tb, labels = "ID_name")
Computes the (squared) L2 Wasserstein distance between two
distributionD objects, i.e. the L2 distance between
their quantile functions. The computation is exact: quantile functions of
discrete distributions are step functions and the integral is evaluated on
the merged grid of cumulative probabilities.
WassSqDistD(object1, object2, ...) ## S4 method for signature 'distributionD,distributionD' WassSqDistD(object1, object2, details = FALSE, ...)WassSqDistD(object1, object2, ...) ## S4 method for signature 'distributionD,distributionD' WassSqDistD(object1, object2, details = FALSE, ...)
object1, object2
|
two |
... |
unused. |
details |
logical; return the decomposition? |
With details = TRUE the squared distance is decomposed (Irpino and
Romano, 2007) into a location, a size and a shape component:
the squared distance or, if details = TRUE, a named vector
with the components SQ.W, POSITION, SIZE,
SHAPE and rQQ.
Irpino A., Romano E. (2007). Optimal histogram representation of large data sets: Fisher vs piecewise linear approximation. RNTI E-9, 99–110.
a <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) b <- distributionD(x = 1:5, p = c(0.3, 0.3, 0.2, 0.1, 0.1)) WassSqDistD(a, b) WassSqDistD(a, b, details = TRUE)a <- distributionD(x = 1:5, p = c(0.05, 0.1, 0.2, 0.4, 0.25)) b <- distributionD(x = 1:5, p = c(0.3, 0.3, 0.2, 0.1, 0.1)) WassSqDistD(a, b) WassSqDistD(a, b, details = TRUE)
Agglomerative clustering (hclust) based on the L2
Wasserstein distance between rows.
WD_hclust(x, simplify = FALSE, method = "complete", weights = 1)WD_hclust(x, simplify = FALSE, method = "complete", weights = 1)
x |
a |
simplify |
ignored (compatibility with |
method |
agglomeration method passed to |
weights |
(optional) weights of the variables. |
an object of class hclust.
hc <- WD_hclust(Satisfaction, method = "ward.D2") plot(hc)hc <- WD_hclust(Satisfaction, method = "ward.D2") plot(hc)
Dynamic clustering (k-means) of the rows of a MatD
using the L2 Wasserstein distance; prototypes are the Wasserstein
barycenters of the clusters. Initial prototypes are chosen with the
k-means++ seeding, the best of rep runs is returned.
WD_kmeans( x, k, rep = 5, simplify = FALSE, qua = 10, standardize = FALSE, weights = 1, iter.max = 100, verbose = FALSE )WD_kmeans( x, k, rep = 5, simplify = FALSE, qua = 10, standardize = FALSE, weights = 1, iter.max = 100, verbose = FALSE )
x |
a |
k |
number of clusters. |
rep |
number of repetitions (random starts). |
simplify |
ignored (compatibility with |
qua |
ignored (compatibility with |
standardize |
logical; if |
weights |
(optional) weights of the variables (overridden by
|
iter.max |
maximum number of iterations. |
verbose |
print progress? |
a list with solution (a list with IDX, the cluster
labels, cardinality, centers, a MatD of prototypes,
and Crit, the within sum of squares), quality (the ratio
between the between and the total sum of squares), TSS and
all.crit, the criteria of all the repetitions.
set.seed(1) km <- WD_kmeans(Satisfaction, k = 3, rep = 5) km$solution$IDX km$qualityset.seed(1) km <- WD_kmeans(Satisfaction, k = 3, rep = 5) km$solution$IDX km$quality
The distance between two rows (units) is
.
The computation is done in C++ with an exact linear-time algorithm.
WD_MAT_DIST(object, simplify = FALSE, weights = 1, squared = FALSE)WD_MAT_DIST(object, simplify = FALSE, weights = 1, squared = FALSE)
object |
a |
simplify |
ignored (kept for compatibility with |
weights |
(optional) weights |
squared |
logical: return squared distances? Default |
a symmetric n x n matrix.
D <- WD_MAT_DIST(Satisfaction) round(D[1:5, 1:5], 3)D <- WD_MAT_DIST(Satisfaction) round(D[1:5, 1:5], 3)
Performs the quantile-based PCA of one distributional variable (Verde and
Irpino, 2010): a classical non-standardised PCA (with
PCA) of the matrix of quantiles, with the means
and the standard deviations of the distributions as supplementary
variables.
WD.1d.PCA(data, var = 1, quantiles = 10, plots = TRUE, ncp = 5)WD.1d.PCA(data, var = 1, quantiles = 10, plots = TRUE, ncp = 5)
data |
a |
var |
the column to analyse. |
quantiles |
number of quantile intervals (default 10). |
plots |
logical; draw the FactoMineR plots? |
ncp |
number of components kept. |
the PCA object of FactoMineR with the additional
element quantile_matrix.
res <- WD.1d.PCA(Satisfaction, var = "Staff", plots = FALSE) res$eig[1:3, ] WD.plot.Spanish.fan(res)res <- WD.1d.PCA(Satisfaction, var = "Staff", plots = FALSE) res$eig[1:3, ] WD.plot.Spanish.fan(res)
Bind MatD objects by rows or by columns
WD.bind.col(a, b) WD.bind.row(a, b)WD.bind.col(a, b) WD.bind.row(a, b)
a, b
|
two |
a MatD.
WD.bind.col(Satisfaction[, 1:2], Satisfaction[, 3])WD.bind.col(Satisfaction[, 1:2], Satisfaction[, 3])
Computes Cronbach's alpha using the Wasserstein covariance matrix of a set
of distributional items (GLOB), and its decomposition into the part
due to the covariance of the means (AVE) and the part due to the
covariance of the centred distributions (VAR). The
alpha.if.deleted matrix reports the values obtained removing each
item in turn.
WD.cronbach(object)WD.cronbach(object)
object |
a |
a list with alpha and alpha.if.deleted.
WD.cronbach(Satisfaction)WD.cronbach(Satisfaction)
Quantile-based multiple factor analysis (MFA) of several distributional
variables: each variable is represented by a group of quantile columns
and the groups are balanced as in MFA.
WD.MultiplePCA( data, list.of.vars, quantiles = 10, ncp = 5, pp = c(0, 1), plots = TRUE )WD.MultiplePCA( data, list.of.vars, quantiles = 10, ncp = 5, pp = c(0, 1), plots = TRUE )
data |
a |
list.of.vars |
the columns to analyse (at least two). |
quantiles |
number of quantile intervals (default 10). |
ncp |
number of components kept. |
pp |
range of probability levels. |
plots |
logical; draw the FactoMineR plots? |
the MFA object with the additional element
quantile_matrix.
Crocetta C., Irpino A., Antonucci L., Marin C. (2025). A performance indicator and its decomposition according to the impacts of different aspects based on distributional data. Quality & Quantity, 59(3), 2091–2110. doi:10.1007/s11135-024-01956-0
res <- WD.MultiplePCA(Satisfaction, 1:4, quantiles = 5, plots = FALSE) res$eig[1:3, ]res <- WD.MultiplePCA(Satisfaction, 1:4, quantiles = 5, plots = FALSE) res$eig[1:3, ]
Draws the plot of the quantile variables of a WD.1d.PCA
as consecutive coloured triangles (the "Spanish fan"): colours go from
red (extreme quantiles) to yellow (median).
WD.plot.Spanish.fan(res, axes = c(1, 2))WD.plot.Spanish.fan(res, axes = c(1, 2))
res |
the output of |
axes |
the two axes to plot. |
a ggplot object.
res <- WD.1d.PCA(Satisfaction, var = "Price", plots = FALSE) WD.plot.Spanish.fan(res, axes = c(1, 2))res <- WD.1d.PCA(Satisfaction, var = "Price", plots = FALSE) WD.plot.Spanish.fan(res, axes = c(1, 2))
For each row and each selected variable, the quantiles at levels
seq(pp[1], pp[2], length.out = quantiles + 1) are computed and
arranged in a numeric matrix (one block of quantiles + 1 columns
per variable).
WD.quantile.matrix( object, quantiles = 10, vars = seq_len(ncol(object@M)), pp = c(0, 1) )WD.quantile.matrix( object, quantiles = 10, vars = seq_len(ncol(object@M)), pp = c(0, 1) )
object |
a |
quantiles |
number of quantile intervals. |
vars |
columns to use (default all). |
pp |
range of probability levels (default |
a numeric matrix.
WD.quantile.matrix(Satisfaction, quantiles = 4, vars = 1:2)[1:5, ]WD.quantile.matrix(Satisfaction, quantiles = 4, vars = 1:2)[1:5, ]
Computes the root mean squared Wasserstein error (RMSE_W), the
OMEGA index and the pseudo- of Irpino and Verde (2015),
with the same definitions of HistDAWass::WH.regression.GOF.
WD.regression.GOF(observed, predicted)WD.regression.GOF(observed, predicted)
observed |
a one-column |
predicted |
a one-column |
a list with RMSE_W, OMEGA and PSEUDOR2.
xv <- c("Staff", "Cleanliness") pars <- WD.regression.two.components(Satisfaction, "Overall", xv) pred <- WD.regression.two.components.predict(Satisfaction[, xv], pars) gof <- WD.regression.GOF(Satisfaction[, "Overall"], pred) gof$OMEGAxv <- c("Staff", "Cleanliness") pars <- WD.regression.two.components(Satisfaction, "Overall", xv) pred <- WD.regression.two.components.predict(Satisfaction[, xv], pars) gof <- WD.regression.GOF(Satisfaction[, "Overall"], pred) gof$OMEGA
Fits the two-component linear model of Irpino and Verde (2015) to discrete distributional data:
where is the centred quantile function () and
so that the predicted quantile functions are
non-decreasing. Since centred quantile functions are orthogonal to
constants, the least-squares problem splits into an OLS regression on the
means () and a non-negative least-squares problem on the centred
parts (), both solved exactly.
WD.regression.two.components(data, Yvar, Xvars)WD.regression.two.components(data, Yvar, Xvars)
data |
a |
Yvar |
the response column. |
Xvars |
the explanatory columns. |
a named vector of parameters: (AV_Intercept), the
AV_ coefficients of the means and the CEN_ coefficients of
the centred distributions.
Irpino A., Verde R. (2015). Linear regression for numeric symbolic variables: a least squares approach based on Wasserstein distance. Advances in Data Analysis and Classification, 9(1), 81–106. doi:10.1007/s11634-015-0197-7
pars <- WD.regression.two.components(Satisfaction, Yvar = "Overall", Xvars = c("Staff", "Price", "Cleanliness")) pars xv <- c("Staff", "Price", "Cleanliness") pred <- WD.regression.two.components.predict(Satisfaction[, xv], pars) WD.regression.GOF(Satisfaction[, "Overall"], pred)[c("RMSE_W", "OMEGA")]pars <- WD.regression.two.components(Satisfaction, Yvar = "Overall", Xvars = c("Staff", "Price", "Cleanliness")) pars xv <- c("Staff", "Price", "Cleanliness") pred <- WD.regression.two.components.predict(Satisfaction[, xv], pars) WD.regression.GOF(Satisfaction[, "Overall"], pred)[c("RMSE_W", "OMEGA")]
Predictions of the two-component regression
WD.regression.two.components.predict(data, parameters)WD.regression.two.components.predict(data, parameters)
data |
a |
parameters |
the output of |
a one-column MatD of predicted distributions.
xv <- c("Staff", "Price") pars <- WD.regression.two.components(Satisfaction, "Overall", xv) pred <- WD.regression.two.components.predict(Satisfaction[, xv], pars) pred[1:3, ]xv <- c("Staff", "Price") pars <- WD.regression.two.components(Satisfaction, "Overall", xv) pred <- WD.regression.two.components.predict(Satisfaction[, xv], pars) pred[1:3, ]
WD.SSQ returns the (weighted) sum of squared Wasserstein distances
of the distributions of each column from their barycenter (the deviance).
WD.SSQ(object, w = 1)WD.SSQ(object, w = 1)
object |
a |
w |
(optional) weights of the rows (not normalised). |
a named numeric vector (one value per column).
WD.SSQ(Satisfaction[, 1:3])WD.SSQ(Satisfaction[, 1:3])
WD.var.covar computes the covariance matrix of the discrete
distributional variables of a MatD, following the L2 Wasserstein
(quantile) approach of Irpino and Verde (2015):
where is the quantile function of the barycenter of
variable . The diagonal contains the Wasserstein (Frechet)
variances. WD.correlation returns the corresponding correlation
matrix.
WD.var.covar(object, w = 1) WD.correlation(object, w = 1)WD.var.covar(object, w = 1) WD.correlation(object, w = 1)
object |
a |
w |
(optional) weights of the rows. |
a p x p matrix.
Irpino A., Verde R. (2015). Basic statistics for distributional symbolic variables: a new metric-based approach. Advances in Data Analysis and Classification, 9(2), 143–175. doi:10.1007/s11634-014-0176-4
WD.var.covar(Satisfaction) round(WD.correlation(Satisfaction), 3)WD.var.covar(Satisfaction) round(WD.correlation(Satisfaction), 3)
Computes, for each column of a MatD, the (weighted)
L2 Wasserstein barycenter of its distributions, i.e. the distribution
whose quantile function is the weighted average of the quantile functions.
The barycenter of discrete distributions is itself discrete; its support
is in general not a subset of the original supports.
WD.vec.mean(object, w = 1)WD.vec.mean(object, w = 1)
object |
a |
w |
(optional) weights of the rows (default: equal weights). |
a distributionD if object has one column, otherwise
a one-row MatD.
WD.vec.mean(Satisfaction[, "Staff"]) WD.vec.mean(Satisfaction)WD.vec.mean(Satisfaction[, "Staff"]) WD.vec.mean(Satisfaction)
Computes the exact Wasserstein barycenter of each column of a MatD
(see WD.vec.mean) and then projects it on the support of the
variable with simplifyD, so that the result has the same
categories of the data (e.g. the 1-7 scores of a Likert item) and new
probabilities. The result is the distribution on the given support closest
to the exact barycenter, and it is also the barycenter of the data
constrained to that support (see simplifyD). It is an
approximation, useful for presentation and
interpretation: the exact barycenter, returned by WD.vec.mean, is
the one used in all the computations of the package (variances,
covariances, k-means prototypes, ...).
WD.vec.mean.simplified(object, w = 1, support = NULL)WD.vec.mean.simplified(object, w = 1, support = NULL)
object |
a |
w |
(optional) weights of the rows. |
support |
(optional) the target support (a vector, or a list with one vector per column); by default the union of the support values observed in each column. |
a distributionD if object has one column, otherwise
a one-row MatD.
exact <- WD.vec.mean(Satisfaction[, "Staff"]) approx <- WD.vec.mean.simplified(Satisfaction[, "Staff"], support = 0:10) approx WassSqDistD(exact, approx)exact <- WD.vec.mean(Satisfaction[, "Staff"]) approx <- WD.vec.mean.simplified(Satisfaction[, "Staff"], support = 0:10) approx WassSqDistD(exact, approx)