PairApply.Rd
Implements a logic to run pairwise calculations on the columns of a data.frame or a matrix.
PairApply(x, FUN = NULL, ..., symmetric = FALSE)
a list, a data.frame or a matrix with columns to be processed pairwise.
a function to be calculated. It is assumed, that the first 2 arguments denominate x and y.
the dots are passed to FUN.
logical. Does the function yield the same result for FUN(x, y) and FUN(y, x)?
If TRUE
just the lower triangular matrix is calculated and transposed. Default is FALSE.
This code is based on the logic of cor()
and extended for asymmetric functions.
a matrix with the results of FUN.
PairApply(d.diamonds[,c("colour","clarity","cut","polish")], FUN = CramerV,
symmetric=TRUE)
#> colour clarity cut polish
#> colour 1.0000000 0.2385027 0.1716311 0.1749971
#> clarity 0.2385027 1.0000000 0.2726831 0.2842774
#> cut 0.1716311 0.2726831 1.0000000 0.2196234
#> polish 0.1749971 0.2842774 0.2196234 1.0000000
# user defined functions are ok as well
PairApply(d.diamonds[,c("clarity","cut","polish","symmetry")],
FUN = function(x,y) wilcox.test(as.numeric(x), as.numeric(y))$p.value, symmetric=TRUE)
#> clarity cut polish symmetry
#> clarity 1.000000e+00 1.528415e-12 1.381603e-37 1.420752e-40
#> cut 1.528415e-12 1.000000e+00 1.702690e-18 2.090998e-21
#> polish 1.381603e-37 1.702690e-18 1.000000e+00 2.307068e-01
#> symmetry 1.420752e-40 2.090998e-21 2.307068e-01 1.000000e+00
# asymetric measure
PairApply(d.diamonds[,c("colour", "clarity", "cut", "polish")],
FUN = Lambda, direction = "row")
#> colour clarity cut polish
#> colour 1.00000000 0.10526316 0.03047091 0.03878116
#> clarity 0.11111111 1.00000000 0.10802469 0.08333333
#> cut 0.02405498 0.05154639 1.00000000 0.04810997
#> polish 0.06779661 0.18644068 0.10593220 1.00000000
# ... compare to:
Lambda(x=d.diamonds$colour, y=d.diamonds$clarity, direction="row")
#> [1] 0.1052632
Lambda(x=d.diamonds$colour, y=d.diamonds$clarity, direction="column")
#> [1] 0.1111111
# the data.frame
dfrm <- d.diamonds[, c("colour","clarity","cut","polish")]
PairApply(dfrm, FUN = CramerV, symmetric=TRUE)
#> colour clarity cut polish
#> colour 1.0000000 0.2385027 0.1716311 0.1749971
#> clarity 0.2385027 1.0000000 0.2726831 0.2842774
#> cut 0.1716311 0.2726831 1.0000000 0.2196234
#> polish 0.1749971 0.2842774 0.2196234 1.0000000
# the same as matrix (columnwise)
m <- as.matrix(dfrm)
PairApply(m, FUN = CramerV, symmetric=TRUE)
#> colour clarity cut polish
#> colour 1.0000000 0.2385027 0.1716311 0.1749971
#> clarity 0.2385027 1.0000000 0.2726831 0.2842774
#> cut 0.1716311 0.2726831 1.0000000 0.2196234
#> polish 0.1749971 0.2842774 0.2196234 1.0000000
# ... and the list interface
lst <- as.list(dfrm)
PairApply(lst, FUN = CramerV, symmetric=TRUE)
#> colour clarity cut polish
#> colour 1.0000000 0.2385027 0.1716311 0.1749971
#> clarity 0.2385027 1.0000000 0.2726831 0.2842774
#> cut 0.1716311 0.2726831 1.0000000 0.2196234
#> polish 0.1749971 0.2842774 0.2196234 1.0000000