-
Notifications
You must be signed in to change notification settings - Fork 0
/
normInputData.R
44 lines (38 loc) · 1.24 KB
/
normInputData.R
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
# ====================================================
# normInputData: Data matrix column-wise normalization
#
# USAGE:
# x.norm <- normInputData(x,type=c('std','cov','cor','cos'));
#
# REFERENCES:
#
# AUTHOR:
# David Pinto
#
# LAST UPDATE:
# Nov. 16, 2014 at 15:09
# ====================================================
normInputData <- function(x, type='std')
{
switch(type,
# --- Center columns to zero mean and scale them to unit standard deviation ---
'std' = {
x <- apply(x, 2, function(x.col) (x.col-mean(x.col))/sd(x.col));
x[!is.finite(x)] <- 0;
},
# --- Center and scale columns such that x'x is the covariance matrix ---
'cov' = {
x <- apply(x, 2, function(x.col,n) (x.col-mean(x.col))/sqrt(n-1), n=nrow(x));
},
# --- Center and scale columns such that x'x is the correlation matrix ---
'cor' = {
x <- apply(x, 2, function(x.col,n) (x.col-mean(x.col))/sd(x.col)/sqrt(n-1), n=nrow(x));
x[!is.finite(x)] <- 0;
},
# --- Center and scale columns such that x'x is the cossine similarity matrix ---
'cos' = {
x <- apply(x, 2, function(x.col) x.col/norm(x.col,type='2'));
}
);
return(x)
}