-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrmarkdown.rmd
197 lines (130 loc) · 4.43 KB
/
rmarkdown.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
---
title: "Simple demo of rmarkdown"
author: "Soumya Banerjee"
date: "30 August 2022"
output:
pdf_document:
toc: yes
number_sections: yes
fig_caption: yes
html_document:
toc: yes
df_print: paged
urlcolor: blue
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```
# Summary
This is a document that outlines a demo of an R markdown.
```{r, include = FALSE}
####################
# Load library
####################
library(knitr)
library(rmarkdown)
library(tinytex)
library(pROC)
library(precrec)
library(PRROC)
library(boot)
library(rlib)
# LIBRARY_PREFIX <- "https://egret.psychol.cam.ac.uk/rlib/"
# source(paste0(LIBRARY_PREFIX, "cris_common.R"))
# source("https://raw.githubusercontent.com/neelsoumya/rlib/master/cris_common.R")
source("cris_common.R")
source("miscstat.R")
# source("https://raw.githubusercontent.com/neelsoumya/rlib/master/miscstat.R")
#######################
# Get data
#######################
df_metagene_score_final = read.csv('metagene_score.csv',
sep = ',', header = TRUE,
stringsAsFactors=FALSE, na.strings="..")
```
# Visualize data
todo histogram
```{r, include=FALSE}
TRAIN = sample(c(TRUE,FALSE),
nrow(df_metagene_score_final),
replace = TRUE)
TEST = (!TRAIN)
df_metagene_score_final_TRAIN = df_metagene_score_final[TRAIN,]
df_metagene_score_final_TEST = df_metagene_score_final[TEST,]
```
# Perform logistic regression
```{r, echo=FALSE}
mylogit <- glm(flag_yes_no ~ metagene_score,
data = df_metagene_score_final_TRAIN,
family = "binomial")
######################################
# Check linear model distributions
#######################################
# miscstat$check_distribution(model = mylogit)
#######################################
# Visualize parameter distributions
# holds for linear mixed effects models lmer()
#######################################
# cris$visualize_fixed_effects_from_lmer(lmer_result = glm_object_best)
# cris$fixed_effects_from_lmer(lmer_result = glm_object_best)
###############################
# predict on test set
###############################
prob = predict(mylogit,
type=c("response"),
newdata = df_metagene_score_final_TEST)
df_metagene_score_final_TEST$prob = prob
###############################
# Additional code to do
# cross-validation NOT USED
# on TRAINING SET
###############################
cost <- function(r, pi=0) mean(abs(r-pi)>0.5)
cv_err <- cv.glm(data = df_metagene_score_final_TRAIN,
K = 4,
cost = cost,
glmfit = mylogit
)
# can use the following to perform model selection if necessary
cv_err$delta[1]
###############################
# Generate ROC curves
# on TEST SET
###############################
g <- pROC::roc(flag_yes_no ~ prob, data=df_metagene_score_final_TEST)
plot(g)
cat("AUC is:", g$auc)
############################
# Precision recall curve
# on test set
############################
mmdata_flag = mmdata(df_metagene_score_final_TEST$metagene_score,
df_metagene_score_final_TEST$flag_yes_no)
smcurves <- evalmod(mmdata_flag, raw_curves = TRUE)
# We also show the precision recall curve below
# which is better suited for cases in which there are class imbalances.
plot(smcurves, raw_curves = FALSE)
############################
# even better AUC and AUPR
# curves with areas reportet
# on TEST SET
############################
fg <- prob[df_metagene_score_final_TEST$flag_yes_no == 1]
bg <- prob[df_metagene_score_final_TEST$flag_yes_no == 0]
# ROC Curve
roc <- roc.curve(scores.class0 = fg, scores.class1 = bg, curve = TRUE)
plot(roc)
# PR Curve
pr <- pr.curve(scores.class0 = fg, scores.class1 = bg, curve = TRUE)
plot(pr)
cat("AUPR is:", pr$auc.integral)
##################################
# Better AUPR plot using ggplot
##################################
source("convert_aupr_to_ggplot.R")
i_y_line_threshold_signif_aupr = length(which(df_metagene_score_final$flag_yes_no == 1))/(length(which(df_metagene_score_final$flag_yes_no == 1)) + length(which(df_metagene_score_final$flag_yes_no == 0)))
convert_aupr_to_ggplot(i_y_line_threshold_signif=i_y_line_threshold_signif_aupr,
prroc_object=pr,
str_filename_save="aupr_ggplot.pdf")
```
\newpage