-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealDataAnalysis.R
More file actions
235 lines (183 loc) · 8.02 KB
/
RealDataAnalysis.R
File metadata and controls
235 lines (183 loc) · 8.02 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
###########################
## Kidney data analysis ##
###########################
##### Settings
# Choose a analysis case for the kidney data:
# Options: in-sample, out-of-sample
# in-sample - In sample prediction for kidney data
# out-of-sample - Out-of-sample leave-one-out prediction for kidney data
analysis.case <- "out-of-sample"
# Load the PAVedr function
source("./PAV.R") # Load PAV.edr function
source("./RidgeCv.R") # Load cross-validation function for Ridge
source("./RidgeEstimator.R") # Load estimation function for Ridge
source("./Fridge.R") # Load Fridge function
# Fix a random seed for reproducibility
# seed = 1
# Load kidney data
load("./data/E-GEOD-33070_gene.RData")
load("./data/E-GEOD-33070_clinical.RData")
exp <- as.matrix(exp)
# Set outcome vector and design matrix
y <- rev(as.vector(weightData$WeightChangePct))
X <- t(exp)
# Normalize design matrix
X.test <- matrix(rep(0,dim(X)[1]*dim(X)[2]),nrow=dim(X)[1],ncol=dim(X)[2])
for (i in 1:dim(X)[2])
{
nv <- as.vector(X[,i])
X.test[,i] <- nv/as.numeric(sqrt(t(nv)%*%nv))
}
# Generate a set of tuning parameters
num.tuning = 300 # Number of tuning parameters
power <- seq(from = -5, to = 5, length.out = num.tuning)
tuning.parameters <- 10 ^ power
# Initialize absolute error and computation time arrays
errors.pav <- rep(NA, dim(X)[1])
errors.CV10 <- rep(NA, dim(X)[1])
errors.CV5 <- rep(NA, dim(X)[1])
errors.fridge <- rep(NA, dim(X)[1])
compTime.pav <- rep(NA, dim(X)[1])
compTime.CV10 <- rep(NA, dim(X)[1])
compTime.CV5 <- rep(NA, dim(X)[1])
compTime.fridge <- rep(NA, dim(X)[1])
cat(paste0("analysis case is : "), analysis.case, "\n")
if (analysis.case == "in-sample"){
##### in-sample prediction
for (s in 1:dim(X)[1]){
cat(sprintf("run %d out of %d testing vector\n", s, dim(X)[1]))
# Testing vector
Z <- as.matrix(X.test[s, ])
test <- as.vector(X.test[s,])
##### Tuning parameter calibration
### PAVedr
cat(" -> compute PAVedr tuning parameters... ")
start.time <- Sys.time()
pav <- PavEdr(y, X.test, Z, tuning.parameters = tuning.parameters)
beta.hat <- pav$beta.hat
compTime.pav[s] <- as.numeric(difftime(Sys.time(), start.time,
units = "sec"))
cat("done\n")
### Fridge
cat(" -> compute Fridge tuning parameters... ")
start.time <- Sys.time()
Fridge.fit <- fridge(X,y,x0=Z,plug.in = 'RLOOCV',plot.curve=FALSE)
fridge.lambda <- Fridge.fit$fridge.tuning
fridge.estimator <- RidgeEstimator(y, X.test, fridge.lambda)
compTime.fridge[s] <- as.numeric(difftime(Sys.time(), start.time,
units = "sec"))
cat("done\n")
### CV (K=10 fold)
cat(" -> compute CV (K=10) tuning parameters... ")
start.time <- Sys.time()
CV10.tuning <- RidgeCv(y, X.test, tuning.parameters = tuning.parameters,
K = 10)
CV10.estimator <- RidgeEstimator(y, X.test, CV10.tuning)
compTime.CV10[s] <- as.numeric(difftime(Sys.time(), start.time,
units = "sec"))
cat("done\n")
### CV (K=5 fold)
cat(" -> compute CV (K=5) tuning parameters... ")
start.time <- Sys.time()
CV5.tuning <- RidgeCv(y, X.test, tuning.parameters = tuning.parameters,
K = 5)
CV5.estimator <- RidgeEstimator(y, X.test, CV5.tuning)
compTime.CV5[s] <- as.numeric(difftime(Sys.time(), start.time,
units = "sec"))
cat("done\n")
errors.fridge[s] <- abs(y[s]-t(test)%*%fridge.estimator)
errors.pav[s] <- abs(y[s]-t(test)%*%beta.hat)
errors.CV10[s] <- abs(y[s]-t(test)%*%CV10.estimator)
errors.CV5[s] <- abs(y[s]-t(test)%*%CV5.estimator)
cat(" -> Mean errors:\n",
" - PAV: ", mean(na.omit(errors.pav)), "\n",
" - CV5: ", mean(na.omit(errors.CV5)), "\n",
" - CV10: ", mean(na.omit(errors.CV10)), "\n",
" - Fridge: ", mean(na.omit(errors.fridge)), "\n"
)
}
# end of in-sample prediction
} else {
##### Out-of-sample prediction
for (s in 1:dim(X)[1])
{
cat(sprintf("run %d out of %d testing vector\n", s, dim(X)[1]))
X.run <- X.test[-s, ]
y.run <- y[-s]
for (i in 1:dim(X.run)[2])
{
nv <- as.vector(X.run[,i])
X.run[,i] <- nv/as.numeric(sqrt(t(nv)%*%nv))
}
test <- as.vector(X.test[s,])
Z <- as.matrix(test)
##### Tuning parameter calibration with PAVedr and CV
# Perform the PAVedr pipeline
cat(" -> compute PAVedr tuning parameters... ")
start.time <- Sys.time()
pav <- PavEdr(y.run, X.run, Z, tuning.parameters = tuning.parameters)
beta.hat <- as.vector(pav$beta.hat)
compTime.pav[s] <- as.numeric(difftime(Sys.time(), start.time,
units = "sec"))
cat("done\n")
# Fridge
cat(" -> compute Fridge tuning parameters... ")
start.time <- Sys.time()
Fridge.fit <- fridge(X,y,x0=Z,plug.in = 'RLOOCV',plot.curve=FALSE)
fridge.lambda <- Fridge.fit$fridge.tuning
fridge.estimator <- RidgeEstimator(y.run, X.run, fridge.lambda)
compTime.fridge[s] <- as.numeric(difftime(Sys.time(), start.time,
units = "sec"))
cat("done\n")
# CV k=10
cat(" -> compute CV (K=10) tuning parameters... ")
start.time <- Sys.time()
CV10.tuning <- RidgeCv(y.run, X.run, tuning.parameters = tuning.parameters,
K = 10)
CV10.estimator <- RidgeEstimator(y.run, X.run, CV10.tuning)
compTime.CV10[s] <- as.numeric(difftime(Sys.time(), start.time,
units = "sec"))
cat("done\n")
# CV k=5
cat(" -> compute CV (K=5) tuning parameters... ")
start.time <- Sys.time()
CV5.tuning <- RidgeCv(y.run, X.run, tuning.parameters = tuning.parameters,
K = 5)
CV5.estimator <- RidgeEstimator(y.run, X.run, CV5.tuning)
compTime.CV5[s] <- as.numeric(difftime(Sys.time(), start.time, units = "sec"))
cat("done\n")
errors.fridge[s] <- abs(y[s]-t(test)%*%fridge.estimator)
errors.pav[s] <- abs(y[s]-t(test)%*%beta.hat)
errors.CV10[s] <- abs(y[s]-t(test)%*%CV10.estimator)
errors.CV5[s] <- abs(y[s]-t(test)%*%CV5.estimator)
cat(" -> Mean errors:\n",
" - PAV: ", mean(na.omit(errors.pav)), "\n",
" - CV5: ", mean(na.omit(errors.CV5)), "\n",
" - CV10: ", mean(na.omit(errors.CV10)), "\n",
" - Fridge: ", mean(na.omit(errors.fridge)), "\n"
)
}
}
##### Data processing
compTime.pav.mean <- mean(as.numeric(compTime.pav))
compTime.pav.scaled <- 1.00
compTime.CV10.scaled <- mean(compTime.CV10) / compTime.pav.mean
compTime.CV5.scaled <- mean(compTime.CV5) / compTime.pav.mean
compTime.fridge.scaled <- mean(compTime.fridge) / compTime.pav.mean
##### Output results
output.Data <- matrix(c(
mean(errors.pav), mean(errors.fridge), mean(errors.CV10), mean(errors.CV5),
sd(errors.pav), sd(errors.fridge), sd(errors.CV10), sd(errors.CV5),
compTime.pav.scaled, compTime.fridge.scaled, compTime.CV10.scaled, compTime.CV5.scaled),
nrow = 4, ncol = 3)
rownames(output.Data) <- c("Pav.edr", "Fridge", "10-fold CV", "5-fold CV")
colnames(output.Data)<- c("mean", "sd", "Scaled-compTime")
results <- data.frame(
"mean"=c(mean(errors.pav), mean(errors.fridge), mean(errors.CV5), mean(errors.CV10)),
"sd"=c(sd(errors.pav), sd(errors.fridge), sd(errors.CV5), sd(errors.CV10)),
"comp.time"=c(mean(compTime.pav), mean(compTime.fridge), mean(compTime.CV10), mean(compTime.CV5)),
row.names=c("Pav.edr", "Fridge", "5-fold CV", "10-fold CV")
)
cat("Simulation Results : \n")
print(output.Data)
print(results)