-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGHoteling.R
executable file
·198 lines (149 loc) · 6.84 KB
/
GHoteling.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
library(Hotelling)
get_pvalue <- function(case,control,case_vector,control_vector,d.l2.full,site_case,site_control) {
skin_case <- case[case_vector==1]
saliva_case <- case[case_vector==0]
saliva_control <- control[control_vector==0]
skin_control <- control[control_vector==1]
d.l2.case <- list()
if( length(saliva_case) > 0 )
{
for(i in 1:length(saliva_case))
{
temp <- d.l2.full[((d.l2.full$id == saliva_case[i]) & (d.l2.full$site == site_control)),]
d.l2.case <- rbind(d.l2.case,temp)
}
}
if(length(skin_case) > 0 )
{
for(i in 1:length(skin_case))
{
temp <- d.l2.full[((d.l2.full$id == skin_case[i]) & (d.l2.full$site == site_case)),]
d.l2.case <- rbind(d.l2.case,temp)
}
}
d.l2.control <- list()
for(i in 1:length(control))
{
temp <- d.l2.full[((d.l2.full$id == saliva_control[i]) & (d.l2.full$site == site_control)),]
d.l2.control <- rbind(d.l2.control,temp)
}
common_col_names <- c("id","case.dist.stool","case.dist.nasal","control.dist.stool","control.dist.nasal")
first <- cbind(d.l2.case[,c(1,5:6)],NA,NA)
names(first) <- common_col_names
fourth <- cbind(d.l2.control[,c(1)],NA,NA,d.l2.control[,c(5:6)])
names(fourth) <- common_col_names
#d.l2.final <- rbind(first,second,third,fourth)
d.l2.final <- rbind(first,fourth)
#row.names(d.l2.final) <- d.l2.final$id
d.l2.final <- d.l2.final[,-1]
#return(GHotelling(d.l2.final,nBoot=1000,print.details = F))
data <- d.l2.final
colnames(data) <- c('x1', 'y1', 'x2', 'y2')
n <- dim(data)[1]
t2 <- GHotelling.helper(data, print.details = F)
#result = ghotelling(data,nboot=1000)
#result$boot.summary[3,]
mean.mx <- matrix(rep(colMeans(data, na.rm = T), n), nrow = n, ncol = 4, byrow = T)
data.null <- data - mean.mx
boot.ix <- get.boot.ix(data.null, 1000)
t2.boot <- matrix(NA, nrow = 1000, ncol = 1)
for(i in 1:1000){
t2.boot[i] <- GHotelling.helper(data = data.null[boot.ix[,i], ], print.details = F)
}
pval <- mean(t2.boot >= t2) # wrt original data
return(pval)
}
get_pvalue_standard_hotelling <- function(case,control,case_vector,control_vector,d.l2.full,site_case,site_control) {
#rm(list=setdiff(ls(),c("case_id_1a","control_id_1a","case_id_1a_vector","control_id_1a_vector")))
#case_vector <- case_vector
#control_vector <- control_vector
##Normally skin is case, saliva is control
skin_case <- case[case_vector==1]
saliva_case <- case[case_vector==0]
saliva_control <- control[control_vector==0]
skin_control <- control[control_vector==1]
d.l2.case <- list()
if( length(saliva_case) > 0 )
{
for(i in 1:length(saliva_case))
{
temp <- d.l2.full[((d.l2.full$id == saliva_case[i]) & (d.l2.full$site == site_control)),]
d.l2.case <- rbind(d.l2.case,temp)
}
}
if(length(skin_case) > 0 )
{
for(i in 1:length(skin_case))
{
temp <- d.l2.full[((d.l2.full$id == skin_case[i]) & (d.l2.full$site == site_case)),]
d.l2.case <- rbind(d.l2.case,temp)
}
}
d.l2.control <- list()
for(i in 1:length(control))
{
temp <- d.l2.full[((d.l2.full$id == saliva_control[i]) & (d.l2.full$site == site_control)),]
d.l2.control <- rbind(d.l2.control,temp)
}
common_col_names <- c("id","dist.stool","dist.nasal")
first <- cbind(d.l2.case[,c(1,5:6)])
names(first) <- common_col_names
first <- first[,-1]
fourth <- cbind(d.l2.control[,c(1)],d.l2.control[,c(5:6)])
names(fourth) <- common_col_names
fourth <- fourth[,-1]
t2_standard = hotelling.test(first,fourth)
pval_standard = with(t2_standard$stats, 1-pf(m*statistic,df[1],df[2]))
#return(hotelling.test(first,fourth)$pval)
return(pval_standard)
}
get_pvalue_test <- function(case,control,case_vector,control_vector,d.l2.full,site_case,site_control) {
#rm(list=setdiff(ls(),c("case_id_1a","control_id_1a","case_id_1a_vector","control_id_1a_vector")))
#case_vector <- case_vector
#control_vector <- control_vector
##Normally skin is case, saliva is control
skin_case <- case[case_vector==1]
saliva_case <- case[case_vector==0]
saliva_control <- control[control_vector==0]
skin_control <- control[control_vector==1]
###
#load('mm-HMP_data_082816-5-sites-level2-nasal-ref-half-sample-mean.rdata')
#require('repmis')
#load('test-matrix.rdata')
#test.mx[c(1:3,20:22,85:69),]
#GHotelling(test.mx,nBoot=10000,print.details = F, seed = 1)
#GHotelling(test.mx[-(7:72),], nBoot = 10000, print.details = F, seed = 1)
#d.l2.hmp <- HMPdistance(tax.level = 'l2.phylum', d.new.filename = "HMP_L2_030317_phylum-example-input-file.csv", d.new.ix.col.not.rel.abu = 1:4, measure = 'bc')
#d.l2.full <- cbind(d[,c(1:3,5,5:6)],d.l2.hmp)
d.l2.case <- d.l2.full[((d.l2.full$id %in% skin_case) & (d.l2.full$site == site_case)) | ((d.l2.full$id %in% saliva_case) & (d.l2.full$site == site_control)) ,]
d.l2.control <- d.l2.full[(d.l2.full$id %in% control) & (d.l2.full$site == site_control),]
d.l2.case <- d.l2.case[order(d.l2.case$id),]
d.l2.control <- d.l2.control[order(d.l2.control$id),]
#Common elements which have both case and control data
#d.l2.case_control <- d.l2.full[((d.l2.full$id %in% skin_case) & (d.l2.full$site == site_control)) | ((d.l2.full$id %in% saliva_case) & (d.l2.full$site == site_case)),]
#d.l2.control_case <- d.l2.full[(d.l2.full$id %in% control) & (d.l2.full$site == site_case),]
#d.l2.case_control <- d.l2.case_control[order(d.l2.case_control$id),]
#d.l2.control_case <- d.l2.control_case[order(d.l2.control_case$id),]
#d.l2.case.common <- intersect(d.l2.case_control$id,d.l2.case$id)
#d.l2.case.unique <- setdiff(d.l2.case$id, d.l2.case_control$id)
#d.l2.control.common <- intersect(d.l2.control_case$id,d.l2.control$id)
#d.l2.control.unique <- setdiff(d.l2.control$id, d.l2.control_case$id)
common_col_names <- c("id","case.dist.stool","case.dist.nasal","control.dist.stool","control.dist.nasal")
#first <- cbind(d.l2.case[(d.l2.case$id %in% d.l2.case.unique),c(1,5:6)],NA,NA)
#names(first) <- common_col_names
#second <- cbind(d.l2.case[(d.l2.case$id %in% d.l2.case.common),c(1,5:6)],d.l2.case_control[(d.l2.case_control$id %in% d.l2.case.common),c(5:6)])
#names(second) <- common_col_names
#third <- cbind(d.l2.control[(d.l2.control$id %in% d.l2.control.common),c(1,5:6)],d.l2.control_case[(d.l2.control_case$id %in% d.l2.control.common),c(5:6)])
#names(third) <- common_col_names
#fourth <- cbind(d.l2.control[(d.l2.control$id %in% d.l2.control.unique),c(1)],NA,NA,d.l2.control[(d.l2.control$id %in% d.l2.control.unique),c(5:6)])
#names(fourth) <- common_col_names
first <- cbind(d.l2.case[,c(1,5:6)],NA,NA)
names(first) <- common_col_names
fourth <- cbind(d.l2.control[,c(1)],NA,NA,d.l2.control[,c(5:6)])
names(fourth) <- common_col_names
#d.l2.final <- rbind(first,second,third,fourth)
d.l2.final <- rbind(first,fourth)
row.names(d.l2.final) <- d.l2.final$id
d.l2.final <- d.l2.final[,-1]
return(GHotelling(d.l2.final,nBoot=1000,print.details = F))
}