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 | ---
title: "Using Genetic Algorithms for Feature Selection"
author: "Kevin R. Coombes"
data: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Genetic Algorithm and Feature Selection}
%\VignetteKeywords{OOMPA,GenAlgo,Feature Selection}
%\VignetteDepends{GenAlgo,Umpire,oompaBase}
%\VignettePackage{GenAlgo}
%\VignetteEngine{knitr::rmarkdown}
---
```{r makeHappy,echo=FALSE}
if (!require(Umpire)) {
knitr::opts_chunk$set(eval = FALSE)
}
```
In this vignette, we ilustrate how to apply the `GenAlgo` package
to the problem of feature selection in an "omics-scale" data set. We
start by loading the packages that we will need.
```{r lib}
library(GenAlgo)
library(Umpire)
library(oompaBase)
```
# Simulating a Data Set
We will use the `Umpire` package to simulate a comnplex enough data set
to stress our feature selection algorithm. We begin by setting up a
time-to-event model, built on an exponential baseline.
```{r survmodel}
set.seed(391629)
sm <- SurvivalModel(baseHazard=1/5, accrual=5, followUp=1)
```
Next, we build a "cancer model" with six subtypes.
```{r cancModel}
nBlocks <- 20 # number of possible hits
cm <- CancerModel(name="cansim",
nPossible=nBlocks,
nPattern=6,
OUT = function(n) rnorm(n, 0, 1),
SURV= function(n) rnorm(n, 0, 1),
survivalModel=sm)
### Include 100 blocks/pathways that are not hit by cancer
nTotalBlocks <- nBlocks + 100
```
Now define the hyperparameters for the models.
```{r hyper}
### block size
blockSize <- round(rnorm(nTotalBlocks, 100, 30))
### log normal mean hypers
mu0 <- 6
sigma0 <- 1.5
### log normal sigma hypers
rate <- 28.11
shape <- 44.25
### block corr
p <- 0.6
w <- 5
```
Now set up the baseline "Engine".
```{r engine}
rho <- rbeta(nTotalBlocks, p*w, (1-p)*w)
base <- lapply(1:nTotalBlocks,
function(i) {
bs <- blockSize[i]
co <- matrix(rho[i], nrow=bs, ncol=bs)
diag(co) <- 1
mu <- rnorm(bs, mu0, sigma0)
sigma <- matrix(1/rgamma(bs, rate=rate, shape=shape), nrow=1)
covo <- co *(t(sigma) %*% sigma)
MVN(mu, covo)
})
eng <- Engine(base)
```
We alter the means if there is a hit, or else build it using the original
engine components.
```{r alter}
altered <- alterMean(eng, normalOffset, delta=0, sigma=1)
object <- CancerEngine(cm, eng, altered)
summary(object)
```
```{r clean1}
rm(altered, base, blockSize, cm, eng, mu0, nBlocks, nTotalBlocks,
p, rate, rho, shape, sigma0, sm, w)
```
Now we can use this elaborate setup to generate the simulated data.
```{r traind}
train <- rand(object, 198)
tdata <- train$data
pid <- paste("PID", sample(1001:9999, 198+93), sep='')
rownames(train$clinical) <- colnames(tdata) <- pid[1:198]
```
Of course, to make things harder, we will add noise to the simulated measurements.
```{r noise}
noise <- NoiseModel(3, 1, 1e-16)
train$data <- log2(blur(noise, 2^(tdata)))
sum(is.na(train$data))
rm(tdata)
summary(train$clinical)
summary(train$data[, 1:3])
```
Now we can also simualte a validation data set.
```{r validd}
valid <- rand(object, 93)
vdata <- valid$data
vdata <- log2(blur(noise, 2^(vdata))) # add noise
sum(is.na(vdata))
vdata[is.na(vdata)] <- 0.26347
valid$data <- vdata
colnames(valid$data) <- rownames(valid$clinical) <- pid[199:291]
rm(vdata, noise, object, pid)
summary(valid$clinical)
summary(valid$data[, 1:3])
```
# Setting up the Genetic Algorithm
Now we can start using the `GenAlgo` package. The key step is to define
sensible functions that can measure the "fitness" of a solution and to
introduce "mutations". When these functions are called, they are passed a
`context` argument that can be used to access extra information about
how to proceed. In this case, that context will be the `train` object,
which includes the clinical information about the samples.
## Fitness
Now we can define the fitness function. The idea is to compute the Mahalanobis
distance between the two groups (of "Good" or "Bad" outcome samples) in the
space defined by the selected features.
```{r measureFitness}
measureFitness <- function(arow, context) {
predictors <- t(context$data[arow, ]) # space defined by features
groups <- context$clinical$Outcome # good or bad outcome
maha(predictors, groups, method='var')
}
```
## Mutations
The mutation function randomly chooses any other feature/row to swap out
possible predictors of the outcome.
```{r mutator}
mutator <- function(allele, context) {
sample(1:nrow(context$data),1)
}
```
## Initialization
We need to decide how many features to include in a potential predictor
(here we use ten). We also need to decide how big a population of feature-sets
(here we use 200) should be used in each generation of the genetic algorithm.
```{r initialize}
set.seed(821831)
n.individuals <- 200
n.features <- 10
y <- matrix(0, n.individuals, n.features)
for (i in 1:n.individuals) {
y[i,] <- sample(1:nrow(train$data), n.features)
}
```
Having chosen the staring population, we can run the first step of the
genetic algorithm.
```{r round1}
my.ga <- GenAlg(y, measureFitness, mutator, context=train) # initialize
summary(my.ga)
```
To be able to evaluate things later, we save the starting generation
```{r save0}
recurse <- my.ga
pop0 <- sort(table(as.vector(my.ga@data)))
```
## Multiple Generations
Realistically, we probably want to run a couple of hundred or even a
couple thousand iterations of the algorithm. But, in interests of making
the vignette complete ins a reasonable amount of time, we are only going
to terate through 20 generations.
```{r recurse}
NGEN <- 20
diversity <- meanfit <- fitter <- rep(NA, NGEN)
for (i in 1:NGEN) {
recurse <- newGeneration(recurse)
fitter[i] <- recurse@best.fit
meanfit[i] <- mean(recurse@fitness)
diversity[i] <- popDiversity(recurse)
}
```
Plot max and mean fitness by generation. This figure shows that both the mean
and the maximum fitness are increasing.
```{r fig.cap="Fitness by generation."}
plot(fitter, type='l', ylim=c(0, 1.5), xlab="Generation", ylab="Fitness")
abline(h=max(fitter), col='gray', lty=2)
lines(fitter)
lines(meanfit, col='gray')
points(meanfit, pch=16, col=jetColors(NGEN))
legend("bottomleft", c("Maximum", "Mean"), col=c("black", "blue"), lwd=2)
```
Plot the diversity of the population, to see that it is deceasing.
```{r fig.cap="Diversity."}
plot(diversity, col='gray', type='l', ylim=c(0,10), xlab='', ylab='', yaxt='n')
points(diversity, pch=16, col=jetColors(NGEN))
```
See which predictors get selected most frequently in the latest
generation.
```{r div}
sort(table(as.vector(recurse@data)))
```
|