l_scale3D scales its argument in a variety of ways used for 3D visualization.

l_scale3D(x, center = TRUE, method = c("box", "sphere"))

Arguments

x

the matrix or data.frame whose columns are to be scaled. Any NA entries will be preserved but ignored in calculations. x must have exactly 3 columns for method = "sphere".

center

either a logical value or numeric-alike vector of length equal to the number of columns of x, where ‘numeric-alike’ means that as.numeric(.) will be applied successfully if is.numeric(.) is not true.

method

the scaling method to use. If method = "box" (the default) then the columns are scaled to have equal ranges and, when center = TRUE, to be centred by the average of the min and max; If method = "sphere" then x must be three dimensional. For sphering, on each of the original 3 dimensions x is first centred (mean centred when center = TRUE) and scaled to equal standard deviation on. The V matrix of the singular value decomposition (svd) is applied to the right resulting in uncorrelated variables. Coordinates are then divided by (non-zero as tested by !all.equal(0, .)) singular values. If x contains no NAs, the resulting coordinates are simply the U matrix of the svd.

Value

a data.frame whose columns are centred and scaled according to the given arguments. For method = "sphere"), the three variable names are x1, x2, and x3.

See also

l_plot3D, scale, and prcomp.

Other three-dimensional plotting functions: l_plot3D()

Examples

##### Iris data # # All variables (including Species as a factor) result_box <- l_scale3D(iris) head(result_box, n = 3)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 -0.2777778 0.12500000 -0.4322034 -0.4583333 -0.5 #> 2 -0.3333333 -0.08333333 -0.4322034 -0.4583333 -0.5 #> 3 -0.3888889 0.00000000 -0.4491525 -0.4583333 -0.5
apply(result_box, 2, FUN = range)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> [1,] -0.5 -0.5 -0.5 -0.5 -0.5 #> [2,] 0.5 0.5 0.5 0.5 0.5
# Note mean is not zero. apply(result_box, 2, FUN = mean)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> -0.07129630 -0.05944444 -0.03254237 -0.04194444 0.00000000
# Sphering only on 3D data. result_sphere <- l_scale3D(iris[, 1:3], method = "sphere") head(result_sphere, n = 3)
#> x1 x2 x3 #> 1 -0.10665359 -0.03635358 -0.039744517 #> 2 -0.09153699 0.06124692 -0.080344512 #> 3 -0.11208702 0.03517574 -0.009633385
apply(result_sphere, 2, FUN = range)
#> x1 x2 x3 #> [1,] -0.1423020 -0.2338372 -0.1627308 #> [2,] 0.1737036 0.2262592 0.2203348
# Note mean is numerically zero. apply(result_sphere, 2, FUN = mean)
#> x1 x2 x3 #> -2.074395e-17 3.842760e-18 1.056550e-16
# With NAs x <- iris x[c(1, 3), 1] <- NA x[2, 3] <- NA result_box <- l_scale3D(x) head(result_box, n = 5)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 NA 0.12500000 -0.4322034 -0.4583333 -0.5 #> 2 -0.3333333 -0.08333333 NA -0.4583333 -0.5 #> 3 NA 0.00000000 -0.4491525 -0.4583333 -0.5 #> 4 -0.4166667 -0.04166667 -0.4152542 -0.4583333 -0.5 #> 5 -0.3055556 0.16666667 -0.4322034 -0.4583333 -0.5
apply(result_box, 2, FUN = function(x) {range(x, na.rm = TRUE)})
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> [1,] -0.5 -0.5 -0.5 -0.5 -0.5 #> [2,] 0.5 0.5 0.5 0.5 0.5
# Sphering only on 3D data. result_sphere <- l_scale3D(x[, 1:3], method = "sphere") # Rows having had any NA are all NA after sphering. head(result_sphere, n = 5)
#> x1 x2 x3 #> 1 NA NA NA #> 2 NA NA NA #> 3 NA NA NA #> 4 -0.1097604 -0.05847447 0.021141688 #> 5 -0.1188225 0.04803699 0.001622979
# Note with NAs mean is no longer numerically zero. # because centring was based on all non-NAs in each column apply(result_sphere, 2, FUN = function(x) {mean(x, na.rm = TRUE)})
#> x1 x2 x3 #> 0.0012171739 -0.0002142758 0.0019700846