forked from HyunsuLee/clustering_allen_neuron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eline_model.Rmd
228 lines (174 loc) · 8.79 KB
/
eline_model.Rmd
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
---
title: "excitatory line classification LASSO and RF"
author: "incheol, hyunsu"
date: "July 25, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Data load
```{r, message=FALSE, warning=FALSE}
setwd("../Data_prep/divided_revised_notmerged_data")
Etrain <- read.csv("Etrain.csv", row.names = "X")
Etrain.l <- read.csv("Etrain_long.csv", row.names = "X")
Etrain.s <- read.csv("Etrain_short.csv", row.names = "X")
Etrain.r <- read.csv("Etrain_ramp.csv", row.names = "X")
Etest <- read.csv("Etest.csv", row.names = "X")
Etest.l <- read.csv("Etest_long.csv", row.names = "X")
Etest.s <- read.csv("Etest_short.csv", row.names = "X")
Etest.r <- read.csv("Etest_ramp.csv", row.names = "X")
```
# Factor add to test set of excitatory line
```{r, message=F, warning=F}
levels(Etest$transgenic_line) <- c(levels(Etest$transgenic_line),"Slc17a6-IRES-Cre")
levels(Etest.l$transgenic_line) <- c(levels(Etest.l$transgenic_line),"Slc17a6-IRES-Cre")
levels(Etest.s$transgenic_line) <- c(levels(Etest.s$transgenic_line),"Slc17a6-IRES-Cre")
levels(Etest.r$transgenic_line) <- c(levels(Etest.r$transgenic_line),"Slc17a6-IRES-Cre")
```
# set Cross-validation as k-fold
```{r, message=FALSE, warning=FALSE}
require(caret)
seeds <- as.vector(c(1:51), mode = "list")
for (i in 1:50) seeds[[i]] <- sample.int(1000,3)
seeds[[51]] <- 849
fitControl <- trainControl(method = "repeatedcv", number = 10, repeats = 5 , seeds = seeds)
noCV <- trainControl(method = "none" , seeds = seeds)
tuneGrid <- expand.grid(alpha = 1, lambda = seq(0.0001, 1, length = 10))
noGrid <- expand.grid(alpha = 1, lambda = 0.01)
```
# LASSO training
```{r, message=FALSE, warning=FALSE}
require(caret)
doMC::registerDoMC(cores = 4) # multi core calculation
set.seed(849)
E.reg.full.cv <- caret::train(transgenic_line ~ ., data = Etrain, method = "glmnet", trControl = fitControl, tuneGrid = tuneGrid)
E.reg.full.nocv <-caret::train(transgenic_line ~ ., data = Etrain, method = "glmnet", trControl = noCV, tuneGrid = noGrid)
E.reg.long.cv <- caret::train(transgenic_line ~ ., data = Etrain.l, method = "glmnet", trControl = fitControl, tuneGrid = tuneGrid)
E.reg.long.nocv <-caret::train(transgenic_line ~ ., data = Etrain.l, method = "glmnet", trControl = noCV, tuneGrid = noGrid)
E.reg.short.cv <- caret::train(transgenic_line ~ ., data = Etrain.s, method = "glmnet", trControl = fitControl, tuneGrid = tuneGrid)
E.reg.short.nocv <-caret::train(transgenic_line ~ ., data = Etrain.s, method = "glmnet", trControl = noCV, tuneGrid = noGrid)
E.reg.ramp.cv <- caret::train(transgenic_line ~ ., data = Etrain.r, method = "glmnet", trControl = fitControl, tuneGrid = tuneGrid)
E.reg.ramp.nocv <-caret::train(transgenic_line ~ ., data = Etrain.r, method = "glmnet", trControl = noCV, tuneGrid = noGrid)
```
# Confusion matrix and prediction accuracy
```{r, message=FALSE, warning=FALSE}
library(magrittr)
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Full model / LASSO with CV ----------\n")
a<-predict(E.reg.full.cv, newdata = Etest) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Full model / LASSO without CV ----------\n")
a<-predict(E.reg.full.nocv, newdata = Etest) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Long model / LASSO with CV ----------\n")
a<-predict(E.reg.long.cv, newdata = Etest.l) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Long model / LASSO without CV ----------\n")
a<-predict(E.reg.long.nocv, newdata = Etest.l) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Short model / LASSO with CV ----------\n")
a<-predict(E.reg.short.cv, newdata = Etest.s) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Short model / LASSO without CV ----------\n")
a<-predict(E.reg.short.nocv, newdata = Etest.s) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Ramp model / LASSO with CV ----------\n")
a<-predict(E.reg.ramp.cv, newdata = Etest.r) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic_line Excitatory prediction / Ramp model / LASSO without CV ----------\n")
a<-predict(E.reg.ramp.nocv, newdata = Etest.r) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
```
# Random forest training
```{r, message=FALSE, warning=FALSE}
doMC::registerDoMC(cores = 4) # multi core calculation
set.seed(849)
E.rf.full.cv <- caret::train(transgenic_line ~ ., data = Etrain, method = "rf", trControl = fitControl)
E.rf.full.nocv <-caret::train(transgenic_line ~ ., data = Etrain, method = "rf", trControl = noCV)
E.rf.long.cv <- caret::train(transgenic_line ~ ., data = Etrain.l, method = "rf", trControl = fitControl)
E.rf.long.nocv <-caret::train(transgenic_line ~ ., data = Etrain.l, method = "rf", trControl = noCV)
E.rf.short.cv <- caret::train(transgenic_line ~ ., data = Etrain.s, method = "rf", trControl = fitControl)
E.rf.short.nocv <-caret::train(transgenic_line ~ ., data = Etrain.s, method = "rf", trControl = noCV)
E.rf.ramp.cv <- caret::train(transgenic_line ~ ., data = Etrain.r, method = "rf", trControl = fitControl)
E.rf.ramp.nocv <-caret::train(transgenic_line ~ ., data = Etrain.r, method = "rf", trControl = noCV)
```
# Confusion matrix and prediction accuracy of RF
```{r, message=FALSE, warning=FALSE}
library(magrittr)
cat("\n\n\n--------- Transgenic line Excitatory prediction / Full model / RANDOM FOREST with CV ----------\n")
a<-predict(E.rf.full.cv, newdata = Etest) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic line Excitatory prediction / Full model / RANDOM FOREST without CV ----------\n")
a<-predict(E.rf.full.nocv, newdata = Etest) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic line Excitatory prediction / Long model / RANDOM FOREST with CV ----------\n")
a<-predict(E.rf.long.cv, newdata = Etest.l) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic line Excitatory prediction / Long model / RANDOM FOREST without CV ----------\n")
a<-predict(E.rf.long.nocv, newdata = Etest.l) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic line Excitatory prediction / Short model / RANDOM FOREST with CV ----------\n")
a<-predict(E.rf.short.cv, newdata = Etest.s) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic line Excitatory prediction / Short model / RANDOM FOREST without CV ----------\n")
a<-predict(E.rf.short.nocv, newdata = Etest.s) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic line Excitatory prediction / Ramp model / RANDOM FOREST with CV ----------\n")
a<-predict(E.rf.ramp.cv, newdata = Etest.r) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
cat("\n\n\n--------- Transgenic line Excitatory prediction / Ramp model / RANDOM FOREST without CV ----------\n")
a<-predict(E.rf.ramp.nocv, newdata = Etest.r) %>% confusionMatrix(Etest$transgenic_line)
a$table
cat("\n")
a$overall[c(1,3:4)]
```
# Saving models
```{r, message=FALSE, warning=FALSE}
setwd("../lasso_rf")
save(E.reg.full.cv, file = "./R_notmerged_models/E.reg.full.cv.rda")
save(E.reg.full.nocv, file = "./R_notmerged_models/E.reg.full.nocv.rda")
save(E.reg.long.cv, file = "./R_notmerged_models/E.reg.long.cv.rda")
save(E.reg.long.nocv, file = "./R_notmerged_models/E.reg.long.nocv.rda")
save(E.reg.short.cv, file = "./R_notmerged_models/E.reg.short.cv.rda")
save(E.reg.short.nocv, file = "./R_notmerged_models/E.reg.short.nocv.rda")
save(E.reg.ramp.cv, file = "./R_notmerged_models/E.reg.ramp.cv.rda")
save(E.reg.ramp.nocv, file = "./R_notmerged_models/E.reg.ramp.nocv.rda")
save(E.rf.full.cv, file = "./R_notmerged_models/E.rf.full.cv.rda")
save(E.rf.full.nocv, file = "./R_notmerged_models/E.rf.full.nocv.rda")
save(E.rf.long.cv, file = "./R_notmerged_models/E.rf.long.cv.rda")
save(E.rf.long.nocv, file = "./R_notmerged_models/E.rf.long.nocv.rda")
save(E.rf.short.cv, file = "./R_notmerged_models/E.rf.short.cv.rda")
save(E.rf.short.nocv, file = "./R_notmerged_models/E.rf.short.nocv.rda")
save(E.rf.ramp.cv, file = "./R_notmerged_models/E.rf.ramp.cv.rda")
save(E.rf.ramp.nocv, file = "./R_notmerged_models/E.rf.ramp.nocv.rda")
```