Repository navigation
Expand file tree
/
Copy pathp.value.analysis.R
More file actions
200 lines (163 loc) · 9.9 KB
/
Copy pathp.value.analysis.R
File metadata and controls
200 lines (163 loc) · 9.9 KB
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
199
200
p.value.analysis <- function( data, LABEL, n.boot.runs = 100){
# prende il df dei dati contenenti un problema binario e da in outpput una serie di statistiche
# per le var categoriche e binarie clacola il p-value con chi square
# per le variabili numeriche usa one-sided Mann Whitney U test (il p-value è il minore tra il one sided < e one-sided per il >)
# LABEL è la colonna delle LABEL
# n.boot.runs = numero di bootstrapped samples per stimare l'optimal cutpoint; se n.boot.runs =1
# l'optimal cutpoint è stimato su tutto il campione
# il tipo delle features viene inferito dal nome:
# nomi che cominciano con :
# INT.* : sono variabili a valori interi
# NUM.* : variabili a valori reali
# BIN.* : sono variabili a valori categorici BINARI
# CAT.* : sono variabili a valori categorici
# ORD.* : sono variabili a valori ordinali
library(pracma)
print("Chi square (categorical) and Mann Whitney UTest (numeric): Statistical analysis of input data \n")
var_types <- extract_type(data)
if (!(is.factor(LABEL))){
LABEL = as.factor(LABEL)
}
lab.levels = levels(LABEL)
if (length(lab.levels)>2){
print('only binary levels are analyzed')
}
# 1 row per ogni feature
strcls0 = paste("class ", lab.levels[1], sep ="")
strcls1 = paste("class ", lab.levels[2], sep ="")
colnames.df = c("all-str",paste(strcls0,"-str", sep =""), paste(strcls1,"-str", sep =""),"direction",
"all-mean/media","all-s.e.", "all-min", "all-max",
paste(strcls0,"-mean/median", sep =""),
paste(strcls0,"-s.e.", sep =""),paste(strcls0,"-min", sep =""),paste(strcls0,"-max", sep =""),
paste(strcls1,"-mean/median", sep =""),
paste(strcls1,"-s.e.", sep =""),paste(strcls1,"-min", sep =""),paste(strcls1,"-max", sep =""),
"p.value",
"cutpoint", "sens", "spec", "acc","PPV", "NPV", "MCC", "F1", "Auc")
df = as.data.frame(matrix(nrow = ncol(data), ncol = length(colnames.df)))
rownames(df ) = colnames(data)
colnames(df) = colnames.df
for (nc in 1:ncol(data)){
var.now = data[,nc]
idx_use = !is.na(var.now)
lab = LABEL[idx_use]
var.now = var.now[idx_use]
var.now_numeric= var.now
cat(colnames(var.now), '\n')
if(strcmp(unlist(var_types[nc]),'bin') || strcmp(unlist(var_types[nc]),'ord') || strcmp(unlist(var_types[nc]),'cat')){
var.now = as.factor(var.now)
levels_var = levels(as.factor(var.now))
label_on = levels_var[length(levels_var)]
cat('for ', var_types[nc], ' var: ', colnames(data)[nc] , ' ON/max category value is = ', label_on, '\n')
p.value = chisq.test(x = var.now, y = lab, simulate.p.value = TRUE)
str_chi = paste("p-value = ", round(p.value[["p.value"]],4), '\n', sep ="")
perc.on = round(100*length(which(var.now == label_on))/ length(var.now),1)
perc.on.in.low = round(100*(length(which(var.now == label_on & lab==lab.levels[1]))/ length(which(lab==lab.levels[1]))),1)
perc.on.in.high = round(100*(length(which(var.now == label_on & lab==lab.levels[2]))/ length(which(lab==lab.levels[2]))),1)
# cat(colnames(data)[nc], '\t' ,
# perc.on,'(', length(which(var.now == label_on)),')', '\t' ,
# perc.on.in.low, '(',length(which(var.now ==label_on & lab==lab.levels[1])), ')', '\t' ,
# perc.on.in.high, '(',length(which(var.now ==label_on & lab==lab.levels[2])), ')', '\t', str_chi)
if (perc.on.in.high>=perc.on.in.low){
direction =paste("class ", lab.levels[2], " >= class ", lab.levels[1])
est_dir = ">="
}else{
est_dir = "<="
direction = paste("class ", lab.levels[1], " >= class ", lab.levels[2])
}
first_estimates = c(paste(perc.on, '%', sep=""),
paste(perc.on.in.low, '%', sep=""),
paste(perc.on.in.high, '%', sep=""), direction,
perc.on, NaN, NaN, NaN,
perc.on.in.low, NaN, NaN, NaN,
perc.on.in.high, NaN , NaN, NaN,
round(p.value[["p.value"]],4))
}else{
true = TRUE
if (strcmp(unlist(var_types[nc]),'num') ) {
mean_v = round(mean(var.now, na.rm = true),2)
se_v = round(sd(var.now, na.rm = true)/sqrt(length(which(is.finite(var.now)))),2)
range_v = c(min(var.now, na.rm = true),max(var.now, na.rm = true))
mean_low = round(mean(var.now[which(lab ==lab.levels[1])], na.rm = true),2)
se_low = round(sd(var.now[which(lab ==lab.levels[1])], na.rm = true)/sqrt(length(which(is.finite(var.now[which(lab ==lab.levels[1])])))),2)
range_low = c(min(var.now[which(lab ==lab.levels[1])], na.rm = true),max(var.now[which(lab ==lab.levels[1])], na.rm = true))
mean_high = round(mean(var.now[which(lab ==lab.levels[2])], na.rm = TRUE),2)
se_high = round(sd(var.now[which(lab ==lab.levels[2])], na.rm = true)/sqrt(length(which(is.finite(var.now[which(lab ==lab.levels[2])])))),2)
range_high = c(min(var.now[which(lab ==lab.levels[2])], na.rm = true),max(var.now[which(lab ==lab.levels[2])], na.rm = true))
}else{
mean_v = median(var.now, na.rm = true)
se_v = round(1.253*sd(var.now, na.rm = true)/sqrt(length(which(is.finite(var.now)))),2)
range_v = c(min(var.now, na.rm = true),max(var.now, na.rm = true))
mean_low = median(var.now[which(lab ==lab.levels[1])], na.rm = true)
se_low = round(1.253*sd(var.now[which(lab ==lab.levels[1])], na.rm = true)/sqrt(length(which(is.finite(var.now[which(lab ==lab.levels[1])])))),2)
range_low = c(min(var.now[which(lab ==lab.levels[1])], na.rm = true),max(var.now[which(lab ==lab.levels[1])], na.rm = true))
mean_high = median(var.now[which(lab ==lab.levels[2])], na.rm = true)
se_high = round(1.253*sd(var.now[which(lab ==lab.levels[2])], na.rm = true)/sqrt(length(which(is.finite(var.now[which(lab ==lab.levels[2])])))),2)
range_high = c(min(var.now[which(lab ==lab.levels[2])], na.rm = true),max(var.now[which(lab ==lab.levels[2])], na.rm = true))
}
pgreat= wilcox.test(data[lab==lab.levels[1],nc],data[lab==lab.levels[2],nc],na.action = "na.omit", alternative = 'greater', PAIRED = FALSE)
pless = wilcox.test(data[lab==lab.levels[1],nc],data[lab==lab.levels[2],nc], na.action = "na.omit", alternative = 'less', PAIRED = FALSE)
if (pgreat$p.value < pless$p.value){
str_w = paste("class ", lab.levels[1], " >= class ", lab.levels[2], " p-value = ", round(pgreat$p.value,4), '\n', sep ="")
direction = paste("class ", lab.levels[1], " >= class ", lab.levels[2])
est_dir ="<=" # per cutpointr x is supposed to be smaller for the positive class
p.val = pgreat$p.value
}else{
est_dir = ">=" # per cutpointr x is supposed to be smaller for the positive class
str_w = paste("class ", lab.levels[1], " <= class ", lab.levels[2], " p-value = ", round(pless$p.value,4), '\n', sep ="")
direction = paste("class ", lab.levels[1], " >= class ", lab.levels[2])
p.val = pless$p.value
}
first_estimates = c(paste( mean_v, ' ± ', se_v, ' [',range_v[1],', ',range_v[2],']', sep =""),
paste(mean_low, ' ± ', se_low, ' [',range_low[1],', ',range_low[2],']', sep =""),
paste(mean_high, ' ± ', se_high,' [',range_high[1],', ',range_high[2],']', sep =""),
direction,
mean_v, se_v, range_v[1],range_v[2],
mean_low, se_low,range_low[1],range_low[2],
mean_high, se_high, range_high[1],range_high[2],
round(p.val,4))
}
if (n.boot.runs==1){
boot_stratify = FALSE
}else{
boot_stratify = TRUE
}
opt_cut <- cutpointr::cutpointr(x = var.now_numeric, class = as.factor(lab ==1),
silent = TRUE,
boot_runs = n.boot.runs,
boot_stratify = boot_stratify,
method = maximize_boot_metric,
metric = youden,
na.rm = TRUE, direction = est_dir)
cutpoint = median(opt_cut$boot[[1]]$optimal_cutpoint)
TP = mean(opt_cut$boot[[1]]$TP_b)
FP = mean(opt_cut$boot[[1]]$FP_b)
TN = mean(opt_cut$boot[[1]]$TN_b)
FN = mean(opt_cut$boot[[1]]$FN_b)
sens = mean(opt_cut$boot[[1]]$sensitivity_b)
spec = mean(opt_cut$boot[[1]]$specificity_b)
Acc = (TP+TN)/(TP+FP+TN+FN)
PPV = TP/(TP+FP)
NPV = TN/(TN+FN)
F1.score = 2*(PPV *sens)/(PPV+sens)
MCC = ((TP*TN) - (FP*FN))/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))
AUC = mean(opt_cut$boot[[1]]$AUC_b)
# cat(direction,'\t', cutpoint,
# '\t', sens,
# '\t', spec,
# '\t', Acc,
# '\t', PPV,
# '\t', NPV,
# '\t', F1.score,
# '\t', Acc,
# '\t',AUC,
# '\t', MCC, '\n')
#
first_estimates = c(first_estimates, cutpoint, sens,spec, Acc,PPV,NPV, MCC, F1.score, AUC)
print(first_estimates)
if (length(first_estimates)< ncol(df)){
cat("ERROR!", colnames(data)[nc], ':', first_estimates, '\n')
}
df[colnames(data)[nc], ] = first_estimates
}
return(df)
}