nscale: Scale n-th Dimension of Array

Description Usage Arguments Details Value Note Author(s) Examples

Description

Slab-scale within each level of the specified mode. Can input 2-way, 3-way, and 4-way arrays, or input a list containing array elements (see Note).

Usage

1
nscale(X, mode = 1, ssnew = NULL, newscale = 1)

Arguments

X

Array (2-way, 3-way, or 4-way) or a list containing array elements.

mode

Mode to scale within (set mode = 0 to scale across all modes).

ssnew

Desired sum-of-squares for each level of scaled mode.

newscale

Desired root-mean-square for each level of scaled mode. Ignored if ssnew is supplied.

Details

Default (as of ver 1.0-5) uses newscale argument...

With X a matrix (I-by-J) there are two options:

mode=1: x[i,j] * newscale / sqrt(meansq(x[i,]))
mode=2: x[i,j] * newscale / sqrt(meansq(x[,j]))

With X a 3-way array (I-by-J-by-K) there are three options:

mode=1: x[i,j,k] * newscale / sqrt(meansq(x[i,,]))
mode=2: x[i,j,k] * newscale / sqrt(meansq(x[,j,])))
mode=3: x[i,j,k] * newscale / sqrt(meansq(x[,,k]))

With X a 4-way array (I-by-J-by-K-by-L) there are four options:

mode=1: x[i,j,k,l] * newscale / sqrt(meansq(x[i,,,]))
mode=2: x[i,j,k,l] * newscale / sqrt(meansq(x[,j,,]))
mode=3: x[i,j,k,l] * newscale / sqrt(meansq(x[,,k,]))
mode=4: x[i,j,k,l] * newscale / sqrt(meansq(x[,,,l]))

If argument ssnew is provided...

With X a matrix (I-by-J) there are two options:

mode=1: x[i,j] * sqrt(ssnew / sumsq(x[i,]))
mode=2: x[i,j] * sqrt(ssnew / sumsq(x[,j]))

With X a 3-way array (I-by-J-by-K) there are three options:

mode=1: x[i,j,k] * sqrt(ssnew / sumsq(x[i,,]))
mode=2: x[i,j,k] * sqrt(ssnew / sumsq(x[,j,])))
mode=3: x[i,j,k] * sqrt(ssnew / sumsq(x[,,k]))

With X a 4-way array (I-by-J-by-K-by-L) there are four options:

mode=1: x[i,j,k,l] * sqrt(ssnew / sumsq(x[i,,,]))
mode=2: x[i,j,k,l] * sqrt(ssnew / sumsq(x[,j,,]))
mode=3: x[i,j,k,l] * sqrt(ssnew / sumsq(x[,,k,]))
mode=4: x[i,j,k,l] * sqrt(ssnew / sumsq(x[,,,l]))

Value

Returns scaled version of X.

Note

When entering a list with array elements, each element must be a 2-way or 3-way array. The list elements are treated as the 3rd mode (for list of 2-way arrays) or the 4th mode (for list of 3-way arrays) in the formulas provided in the Description.

Author(s)

Nathaniel E. Helwig <helwig@umn.edu>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
##########   EXAMPLE 1   ##########
X <- matrix(rnorm(2000), nrow = 100, ncol = 20)
Xr <- nscale(X, mode = 2)                # scale columns to newscale=1
sqrt(colMeans(Xr^2))
Xr <- nscale(X, mode = 2, newscale = 2)  # scale columns to newscale=2
sqrt(colMeans(Xr^2))


##########   EXAMPLE 2   ##########
Xold <- X <- matrix(rnorm(400), nrow = 20, ncol = 20)
iter <- 0
chk <- 1
# iterative scaling of modes 1 and 2
while(iter<500 & chk>=10^-9){
  Xr <- nscale(Xold, mode = 1)
  Xr <- nscale(Xr, mode = 2)
  chk <- sum((Xold-Xr)^2)
  Xold <- Xr
  iter <- iter + 1
}
iter
sqrt(rowMeans(Xr^2))
sqrt(colMeans(Xr^2))


##########   EXAMPLE 3   ##########
X <- array(rnorm(20000), dim = c(100,20,10))
Xc <- nscale(X, mode = 2)   # scale within columns
sqrt(rowMeans(aperm(Xc, perm = c(2,1,3))^2))


##########   EXAMPLE 4   ##########
X <- array(rnorm(100000), dim = c(100,20,10,5))
Xc <- nscale(X, mode = 4)   # scale across 4-th mode
sqrt(rowMeans(aperm(Xc, perm = c(4,1,2,3))^2))


##########   EXAMPLE 5   ##########
X <- replicate(5, array(rnorm(20000), dim = c(100,20,10)), simplify = FALSE)
# mean square of 1 (new way)
Xc <- nscale(X)
rowSums(sapply(Xc, function(x) rowSums(x^2))) / (20*10*5)
# mean square of 1 (old way)
Xc <- nscale(X, ssnew = (20*10*5))
rowSums(sapply(Xc, function(x) rowSums(x^2))) / (20*10*5)

multiway documentation built on May 2, 2019, 6:47 a.m.