-
Notifications
You must be signed in to change notification settings - Fork 4
/
CovMatchSubroutines.R
153 lines (105 loc) · 3.88 KB
/
CovMatchSubroutines.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# MIT License
#
# Copyright (c) 2020 Nitesh Kumar, Abhinav Prakash, and Yu Ding
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#' @importFrom matrixStats colMins colMaxs colSds
#' @useDynLib DSWE, .registration = TRUE
#' @importFrom Rcpp sourceCpp
CovMatch.Mult = function(dname, cov, wgt, cov.circ){
# Store data sets to be compared
fname_ = dname
# Weight assigning
wgt_ = wgt
# Covariates column number for matching
covcol_ = cov
# Circular variable position indicator
pos = 0
# Circular variable presence indicator
flag = 0
# Ensuring circular variable to be between 0 to 360 degree
if(length(cov.circ) > 0) {
# Circular variable after data subsetting position
pos = which(cov.circ == covcol_)
# Circular variable indicator
flag = 1
}
# Setting up the reference data set and threshold
refid_ = length(fname_)
ref_ = as.matrix(fname_[[refid_]][, covcol_, drop = FALSE])
# Test files
testid_ = c(1:length(fname_))[-refid_]
# Setting up thresholds
ratio_ = colSds(as.matrix(ref_))
thres_ = ratio_ * wgt_
# Matching data sets with ref as reference
matchID_ = lapply(testid_, function(x) Match.Cov(ref_, fname_[[x]][, covcol_, drop = FALSE], thres_, pos, flag))
# creating list of matched data set
matched_ = rep(list(c()), (length(fname_)))
# selecting indices of matched data sets
# matched reference set
ref.id_ = ((matchID_[[1]]) > 0)
if(length(fname_) < 3){
ref.id_ = ref.id_
} else{
for(i in 2:(length(fname_)-1))
{
ref.id_ = ref.id_ & ((matchID_[[i]]) > 0)
}
}
refID_ = which(ref.id_)
matched_[[refid_]] = fname_[[refid_]][refID_, ]
# matched test set
for(j in (1:(length(fname_)-1))){
matched_[[testid_[j]]] = fname_[[testid_[j]]][matchID_[[j]][refID_], ]
}
return(matched_)
}
Match.Cov = function(ref, obj, thres, circ.pos = 0, flag = 0){
# Ensuring that the data sets are converted to matrix
ref = as.matrix(ref)
obj = as.matrix(obj)
match = matchcov(ref = ref , obj = obj, thres = thres, circ_pos = circ.pos, flag = flag)
# Returns matched index
return(match)
}
######### Function to convert circular variable values to positive ##################
# data : data set consiting of features and vlues
# circ : position of circular variable supplied by user
Circ.Positive = function(data, circ){
# Iterating over circular variabe
for(i in circ){
while(sum(data[ , i] < 0) != 0){
data[, i] = data[, i] + 360
}
}
# Returns manipulated data
return(data)
}
MinMaxData = function(data, xcol){
if(is.list(data)){
comData = rbind(data[[1]], data[[2]])
}
filteredData = matrix(nrow = 2, ncol = length(xcol))
colnames(filteredData) = as.character(xcol)
row.names(filteredData) = c('Min', 'Max')
filteredData[1 , ] = colMins(as.matrix(comData[, xcol]))
filteredData[2 , ] = colMaxs(as.matrix(comData[, xcol]))
return(filteredData)
}