Learn R Programming

miceadds (version 1.5-0)

ma.scale2: Standardization of a Matrix

Description

This function performs a z-standardization for a numeric matrix. Note that in a case of a zero standard deviation all matrix entries are divided by a small number such that no NaNs occur.

Usage

ma.scale2(x, missings=FALSE)

Arguments

x
A numeric matrix in which missing values are permitted
missings
A logical indicating whether missings occur (or could occur) in the dataset

Value

  • A matrix

See Also

scale

Examples

Run this code
#############################################################################
# EXAMPLE 1: z-standardization data.internet
#############################################################################

data(data.internet)
dat <- data.internet

# z-standardize all variables in this dataset
zdat <- ma.scale2( dat , missings=TRUE )
	
#############################################################################
# SIMULATED EXAMPLE 2: Speed comparison for many cases and many variables
#############################################################################

set.seed(9786)
# 3000 cases, 200 variables
N <- 3000 ; p <- 200
# simulate some data
x <- matrix( rnorm( N*p ) , N , p )
x <- round( x , 2 )

# compare computation times for 10 replications
B <- 10
    s1 <- Sys.time()		# scale in R
for (bb in 1:B){    
    res <- scale(x)
} ; s2 <- Sys.time() ; d1 <- s2-s1

    s1 <- Sys.time()		# scale in miceadds
for (bb in 1:B){    
    res1 <- ma.scale2(x)
} ; s2 <- Sys.time() ; d2 <- s2-s1

# scale in miceadds with missing handling
    s1 <- Sys.time()
for (bb in 1:B){    
    res1 <- ma.scale2(x,missings=TRUE)
} ; s2 <- Sys.time() ; d3 <- s2-s1
d1      # scale in R
d2      # scale in miceadds (no missing handling)
d3      # scale in miceadds (with missing handling)
  ##   > d1      # scale in R
  ##   Time difference of 1.622431 secs
  ##   > d2      # scale in miceadds (no missing handling)
  ##   Time difference of 0.156003 secs
  ##   > d3      # scale in miceadds (with missing handling)
  ##   Time difference of 0.2028039 secs

Run the code above in your browser using DataLab