-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknockoff_5distributions_glm.R
179 lines (144 loc) · 5.82 KB
/
knockoff_5distributions_glm.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
#install.packages("doMC", repos="http://R-Forge.R-project.org")
#install.packages("knockoff")
#install.packages("dplyr")
#install.packages("glmnet")
#install.packages("stringr")
library(knockoff)
library(dplyr)
library(stringr)
n = 1000
p = 400
k = 40
rep = 100
norm_fdp = matrix(NA,rep,4)
norm_pow = matrix(NA,rep,4)
exp_fdp = matrix(NA,rep,4)
exp_pow = matrix(NA,rep,4)
pois_fdp = matrix(NA,rep,4)
pois_pow = matrix(NA,rep,4)
logis_fdp = matrix(NA,rep,4)
logis_pow = matrix(NA,rep,4)
cauchy_fdp = matrix(NA,rep,4)
cauchy_pow = matrix(NA,rep,4)
for(i in 1:rep){
ind = sample(p,k)
beta = rep(0,p)
beta[ind] = 4.5 / sqrt(n) * sample(c(1,-1),k, replace=T)
fdp = function(selected) sum(beta[selected] == 0) / max(1, length(selected))
power = function(x) length(intersect(ind,x))/length(ind)
### normal
X = matrix(rnorm(n*p),n)
y = rbinom( n, 1, 1/(1+exp(-X%*%beta)) )
fit = glm(y~X+0,family=binomial)
p_values = summary(fit)$coefficient[,4]
bh = which(p.adjust(p_values, method="fdr") <= 0.1)
norm_fdp[i,1] = fdp(bh)
norm_pow[i,1] = power(bh)
result = knockoff.filter(X, y, offset=0)
norm_fdp[i,2] = fdp(result$selected)
norm_pow[i,2] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='sdp', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
norm_fdp[i,3] = fdp(result$selected)
norm_pow[i,3] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='equi', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
norm_fdp[i,4] = fdp(result$selected)
norm_pow[i,4] = power(result$selected)
### exponential
X = matrix(rexp(n*p),n)
y = rbinom( n, 1, 1/(1+exp(-X%*%beta)) )
fit = glm(y~X+0,family=binomial)
p_values = summary(fit)$coefficient[,4]
bh = which(p.adjust(p_values, method="fdr") <= 0.1)
exp_fdp[i,1] = fdp(bh)
exp_pow[i,1] = power(bh)
result = knockoff.filter(X, y, offset=0)
exp_fdp[i,2] = fdp(result$selected)
exp_pow[i,2] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='sdp', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
exp_fdp[i,3] = fdp(result$selected)
exp_pow[i,3] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='equi', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
exp_fdp[i,4] = fdp(result$selected)
exp_pow[i,4] = power(result$selected)
### poisson
X = matrix(rexp(n*p),n)
y = rbinom( n, 1, 1/(1+exp(-X%*%beta)) )
fit = glm(y~X+0,family=binomial)
p_values = summary(fit)$coefficient[,4]
bh = which(p.adjust(p_values, method="fdr") <= 0.1)
pois_fdp[i,1] = fdp(bh)
pois_pow[i,1] = power(bh)
result = knockoff.filter(X, y, offset=0)
pois_fdp[i,2] = fdp(result$selected)
pois_pow[i,2] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='sdp', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
pois_fdp[i,3] = fdp(result$selected)
pois_pow[i,3] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='equi', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
pois_fdp[i,4] = fdp(result$selected)
pois_pow[i,4] = power(result$selected)
###logistic
X = matrix(rlogis(n*p,s=sqrt(3)/pi),n)
y = rbinom( n, 1, 1/(1+exp(-X%*%beta)) )
fit = glm(y~X+0,family=binomial)
p_values = summary(fit)$coefficient[,4]
bh = which(p.adjust(p_values, method="fdr") <= 0.1)
logis_fdp[i,1] = fdp(bh)
logis_pow[i,1] = power(bh)
result = knockoff.filter(X, y, offset=0)
logis_fdp[i,2] = fdp(result$selected)
logis_pow[i,2] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='sdp', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
logis_fdp[i,3] = fdp(result$selected)
logis_pow[i,3] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='equi', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
logis_fdp[i,4] = fdp(result$selected)
logis_pow[i,4] = power(result$selected)
###cauchy
X = matrix(rcauchy(n*p),n)
y = rbinom( n, 1, 1/(1+exp(-X%*%beta)) )
fit = glm(y~X+0,family=binomial)
p_values = summary(fit)$coefficient[,4]
bh = which(p.adjust(p_values, method="fdr") <= 0.1)
cauchy_fdp[i,1] = fdp(bh)
cauchy_pow[i,1] = power(bh)
result = knockoff.filter(X, y, offset=0)
cauchy_fdp[i,2] = fdp(result$selected)
cauchy_pow[i,2] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='sdp', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
cauchy_fdp[i,3] = fdp(result$selected)
cauchy_pow[i,3] = power(result$selected)
gaussian_knockoffs = function(X) create.second_order(X, method='equi', shrink=T)
result = knockoff.filter(X, y, knockoffs = gaussian_knockoffs, offset=0)
cauchy_fdp[i,4] = fdp(result$selected)
cauchy_pow[i,4] = power(result$selected)
print(i)
}
fdp = matrix(NA,5,4)
pow = matrix(NA,5,4)
rownames(fdp) = c("normal","exponential",'poisson','logistic','cauchy')
rownames(pow) = c("normal","exponential",'poisson','logistic','cauchy')
colnames(fdp) = c('BH','default','sdp','equi')
colnames(pow) = c('BH','default','sdp','equi')
fdp[1,] = apply(norm_fdp,2,mean)
fdp[2,] = apply(exp_fdp,2,mean)
fdp[3,] = apply(pois_fdp,2,mean)
fdp[4,] = apply(logis_fdp,2,mean)
fdp[5,] = apply(cauchy_fdp,2,mean)
pow[1,] = apply(norm_pow,2,mean)
pow[2,] = apply(exp_pow,2,mean)
pow[3,] = apply(pois_pow,2,mean)
pow[4,] = apply(logis_pow,2,mean)
pow[5,] = apply(cauchy_pow,2,mean)
# (procedure predict nonzero and it is actually non zero coef) / (total number of nonzero )
# p-values are exact (correct)
# p-values are asymptotic (knockoffs)