-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.R
54 lines (54 loc) · 1.22 KB
/
Code.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
45
46
47
48
49
50
51
52
53
54
#Define the arguments#
table<-read.csv('http://archive.ics.uci.edu/ml/machine-learning-databases/00265/CASP.csv', sep=",")
table<-as.numeric(unlist(table))
table<-matrix(table, ncol=10)
table1<-table
table<-to.dfs(table)
#reduce function sums a list of matrices#
reducer=function(.,A){
keyval(1,list(Reduce('+',A)))}
#1st map-reduce#
mapper3=function(.,Xr){
Xr<-Xr
keyval(1,list(nrow(Xr)))}
#Calculate number of rows#
nrow<-values(
from.dfs(
mapreduce(
input= table,
map=mapper3,
reduce=reducer,
combine=T)))[[1]]
N <- nrow
#2nd map-reduce#
mapper2=function(.,Xr){
Xr<-Xr
keyval(1,list(colSums(Xr)))}
#Calculate mu#
mu.N<-values(
from.dfs(
mapreduce(
input= table
, map=mapper2,
reduce=reducer,
combine=T)))[[1]]
mu<-mu.N/nrow
#Define new argument using command sweep(x) #
s.table<- sweep(table1, STATS=mu , MARGIN =2)
s.table<-to.dfs(s.table)
#3rd map-reduce#
mapper1=function(.,Xr){
Xr<-Xr
keyval(1,list(crossprod(Xr)))}
#Calculate crossprod(X) / (N-1) #
Cov.X.n1<-values(
from.dfs(
mapreduce(
input= s.table
, map=mapper1,
reduce=reducer,
combine=T )))[[1]]
cov.x<-Cov.X.n1/(nrow-1)
cov.x
#Transform to correlation matrix#
cor.x<-cov2cor(cov.x)