-
Notifications
You must be signed in to change notification settings - Fork 0
/
MEM_clean.R
329 lines (268 loc) · 12.5 KB
/
MEM_clean.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
library(lme4)
library(ggplot2)
library(stargazer)
library(blme)
library(sjPlot)
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("biomaRt")
setwd('~/Documents/Mixed_Effects_Model/AT_rerun/')
##################################
# Test trial for MEM #
##################################
# Read in file, build dataframe (columns should be covariates and cell populations,
# row should be ID and subsequent info.)
me_memG = read.csv('grid_ME12_new.csv', sep = ';')
head(me_memG)
me_memG_df = data.frame(me_memG)
## Construct Mixed-Effect model
# Linear MEM
me_lmer.model = lmer(IgD.pos..Memory.B ~ sex + age + symptom_score + Group + active_treatment +
(1|id/Group), data=me_memG)
# Partial Bayesian MEM
me_blmer.model = blmer(IgD.pos..Memory.B ~ sex + age + symptom_score + Group + active_treatment +
(1|id/Group), data=me_memG)
# Rescale and center continuous parameters
numcols <- grep("^c\\.",names(me_memG_df))
dfs <- me_memG_df
dfs[,numcols] <- scale(dfs[,numcols])
me_lmer.model <- update(me_lmer.model,data=dfs)
# Evaluates whther a fitted mixed model is singular, if singular TRUE then opt for solution
# One of these solutions being a partial bayesion model especially with complex models
isSingular(me_lmer.model, tol=1e-05)
# Print model output
summary(me_lmer.model)
# Create table of MEM model either with tab_model() or stargazer()
library(sjPlot)
setwd('~/Documents/Mixed_Effects_Model/AT_rerun/MEM_table')
tab_model(me_lmer.model,me_mem4.model,me_mem20.model, file = 'tableME_grid_trial.html')
stargazer(me_lmer.model,
type = "html",
digits = 3,
star.cutoffs = c(0.05, 0.01, 0.001),
digit.separator = "", out = 'tableME_grid_trial.html')
## Statistical significance (ex. SS)
#1) construct null model first
me_lmer.null = lmer(IgD.pos..Memory.B ~ sex + age + (1|Group) +
(1|id), data=me_mem1, REML=FALSE)
#2) Re-do full model
me_lmer.model = lmer(IgD.pos..Memory.B ~ sex + age + symptom_score + (1|Group) +
(1|id), data=me_mem1, REML=FALSE)
#3) Perform likelihood ratio test
anova(me_lmer.null, me_lmer.model)
# Looks at the coefficients of the model by subject and by item
coef(me_lmer.model)
##################################
# Input and Modelling - GRID #
##################################
# Read in file, build dataframe (columns should be covariates and cell populations,
# row should be ID and subsequent info.)
me_memG = read.csv('grid_ME12_new.csv', sep = ';')
head(me_memG)
me_memG_df = data.frame(me_memG)
# List of cell population names in order to loop through, omitting first 6 columns which
# includes ID and covariates
G_list = colnames(me_memG_df[-(1:6)])
head(G_list)
# Mixed-effect modelling (Partial-bayesian)
varlist=G_list
blups.models_G <- lapply(varlist, function(x) {
mod2 = try(blmer(substitute(i ~ sex + age + symptom_score + Group + active_treatment +
(1|id/Group), list(i = as.name(x))),
data = me_memG_df, na.action=na.exclude))
if(isTRUE(class(mod2)=='try-error')) {return(NULL)} else{return(mod2)}
})
blups.models_nextG = as.list(blups.models_G)
# Remove NULL models that failed to converge
blups.models_nextG[sapply(blups.models_nextG, is.null)] <- NULL
##################################
# Extract info from model - GRID #
##################################
library(predictmeans)
# Mixed-effect model expression
varlist1 <- lapply(blups.models_nextG, function(f) summary(f)$call[2])
varlist1
# Correlation Coefficient
CC_AT <- lapply(blups.models_nextG, function(f) summary(f)$coefficients[6,1])
CC_KOS <- lapply(blups.models_nextG, function(f) summary(f)$coefficients[5,1])
CC_SS <- lapply(blups.models_nextG, function(f) summary(f)$coefficients[4,1])
# R^2 and adjusted
R2 <- lapply(blups.models_nextG, function(f) summary(f)$r.squared)
R2_adj <- lapply(blups.models_nextG, function(f) summary(f)$adj.r.squared)
# Median
med <- lapply(blups.models_nextG, function(f) summary(f)$residuals)
med_na <- lapply(med, function(f) na.exclude(f))
med_calc <- lapply(med_na, function(f) median(f))
## p-values for covariates (fixed-effect)
# SS
test_pSS <- lapply(blups.models_nextG, function(f) parameters::p_value_wald(f)[4,])
df.pvalueSS <- as.data.frame(test_pSS)
df.pvalueSS <-t(df.pvalueSS)
df.pvalueSS = df.pvalueSS[seq(0, nrow(df.pvalueSS), 2), ]
# KOS
test_pKOS <- lapply(blups.models_nextG, function(f) parameters::p_value_wald(f)[5,])
df.pvalueKOS <- as.data.frame(test_pKOS)
df.pvalueKOS <-t(df.pvalueKOS)
df.pvalueKOS = df.pvalueKOS[seq(0, nrow(df.pvalueKOS), 2), ]
# AT
test_pAT <- lapply(blups.models_nextG, function(f) parameters::p_value_wald(f)[6,])
df.pvalueAT <- as.data.frame(test_pAT)
df.pvalueAT <-t(df.pvalueAT)
df.pvalueAT = df.pvalueAT[seq(0, nrow(df.pvalueAT), 2), ]
# Prepare dataframe with extracted info. for downstream use
test_data = list(as.character(varlist1), med_calc, CC_AT, CC_KOS, CC_SS, as.numeric(t(df.pvalueSS)), as.numeric(t(df.pvalueKOS)), as.numeric(t(df.pvalueAT)))
names(test_data) <- c('cells', 'Median', 'Corr_coeff_AT', 'Corr_coeff_KOS', 'Corr_coeff_SS', 'pvalueSS', 'pvalueKOS', 'pvalueAT')
test_final <- as.data.frame(do.call(rbind, test_data))
t(test_final)
setwd('~/Documents/Mixed_Effects_Model/AT_rerun/MEM_table')
write.csv2(t(test_final), file='grid_MEMtable_newSS.csv')
##################################
# Input and Modelling - PP #
##################################
setwd('~/Documents/Mixed_Effects_Model/AT_rerun')
# Read in file, build dataframe (columns should be covariates and plasma proteins,
# row should be ID and subsequent info.)
me_mem1 = read.csv('Olink_ME12_new.csv', sep = ';')
head(me_mem1)
me_mem1_df = data.frame(me_mem1)
# List of plasma protein names in order to loop through, omitting first 6 columns which
# includes ID and covariates
PP_list = colnames(me_mem1_df[-(1:6)])
head(PP_list)
# Mixed-effect modelling (Partial-bayesian)
varlist=PP_list
blups.models_PP <- lapply(varlist, function(x) {
mod2 = try(blmer(substitute(i ~ sex + age + symptom_score + Group + active_treatment +
(1|id/Group), list(i = as.name(x))),
data = me_mem1_df, na.action=na.exclude))
if(isTRUE(class(mod2)=='try-error')) {return(NULL)} else{return(mod2)}
})
blups.models_nextPP = as.list(blups.models_PP)
# Remove NULL models that failed to converge
blups.models_nextPP[sapply(blups.models_nextPP, is.null)] <- NULL
##################################
# Extract info from model - PP #
##################################
# Mixed-effect model expression
varlist1 <- lapply(blups.models_nextPP, function(f) summary(f)$call[2])
varlist1
# Correlation Coefficient
estimate_AT <- lapply(blups.models_nextPP, function(f) summary(f)$coefficients[6,1])
estimate_KOS <- lapply(blups.models_nextPP, function(f) summary(f)$coefficients[5,1])
estimate_SS <- lapply(blups.models_nextPP, function(f) summary(f)$coefficients[4,1])
# Median
med <- lapply(blups.models_nextPP, function(f) summary(f)$residuals)
med_na <- lapply(med, function(f) na.exclude(f))
med_calc <- lapply(med_na, function(f) median(f))
## p-values for covariates (fixed-effect)
# SS
test_pSS <- lapply(blups.models_nextPP, function(f) parameters::p_value_wald(f)[4,])
df.pvalueSS <- as.data.frame(test_pSS)
df.pvalueSS <-t(df.pvalueSS)
df.pvalueSS = df.pvalueSS[seq(0, nrow(df.pvalueSS), 2), ]
# KOS
test_pKOS <- lapply(blups.models_nextPP, function(f) parameters::p_value_wald(f)[5,])
df.pvalueKOS <- as.data.frame(test_pKOS)
df.pvalueKOS <-t(df.pvalueKOS)
df.pvalueKOS = df.pvalueKOS[seq(0, nrow(df.pvalueKOS), 2), ]
# AT
test_pAT <- lapply(blups.models_nextPP, function(f) parameters::p_value_wald(f)[6,])
df.pvalueAT <- as.data.frame(test_pAT)
df.pvalueAT <-t(df.pvalueAT)
df.pvalueAT = df.pvalueAT[seq(0, nrow(df.pvalueAT), 2), ]
# Prepare dataframe with extracted info. for downstream use
test_data = list(as.character(varlist1), med_calc, estimate_AT, estimate_KOS, estimate_SS, as.numeric(t(df.pvalueSS)), as.numeric(t(df.pvalueKOS)), as.numeric(t(df.pvalueAT)))
names(test_data) <- c('proteins', 'Median', 'Estimate_AT', 'Estimate_KOS', 'Estimate_SS', 'pvalueSS', 'pvalueKOS', 'pvalueAT')
test_final <- as.data.frame(do.call(rbind, test_data))
t(test_final)
setwd('~/Documents/Mixed_Effects_Model/AT_rerun/MEM_table')
write.csv2(t(test_final), file='protein_MEMtable_newSS.csv')
##################################
# Input - mRNA #
##################################
setwd('~/Documents/Mixed_Effects_Model/AT_rerun/')
# Read in file, build dataframe (columns should be covariates and genes,
# row should be ID and subsequent info.)
me_memRNA = read.csv('deseq_ME12.csv', sep = ';', header = FALSE, row.names=1)
head(me_memRNA[1:7])
me_memRNA_df = as.data.frame(t(me_memRNA))
head(me_memRNA_df)
# List of genes names in order to loop through, omitting first 6 columns which
# includes ID and covariates
mRNA_list = colnames(me_memRNA_df[-(1:6)])
head(mRNA_list)
## If wish to convert Ensembl to HUGO should do so at this moment
##################################
# Convert Ensembl to HUGO #
##################################
library(biomaRt)
mart <- biomaRt::useDataset("hsapiens_gene_ensembl", biomaRt::useMart("ensembl"))
genes <- mRNA_list
df<-me_memRNA_df[,-1]
head(df)
G_list <- biomaRt::getBM(filters= "ensembl_gene_id", attributes= c("ensembl_gene_id","hgnc_symbol"),values=genes,mart= mart)
head(G_list)
write.table(as.data.frame(G_list),file="gene_list.csv", quote=F,sep=";",row.names=F)
df_0 <- data.frame(matrix(ncol = 2, nrow = 0))
x <- c("gene", "ensembl_gene_id")
colnames(df_0) <- x
df_0 <- merge(df,G_list,by.df_0="gene",by.df_0="ensembl_gene_id")
head(df_0)
write.table(as.data.frame(df_0),file="mRNA_HUGO.csv", quote=F,sep=";",row.names=F)
# Read in HUGO table now, and continue as before
me_memRNA = read.csv('mRNA_HUGO.csv', sep = ';', header = FALSE, row.names=1)
me_memRNA_df = as.data.frame(t(me_memRNA))
head(me_memRNA_df)
# List of genes names
mRNA_list = colnames(me_memRNA_df[-(1:6)])
head(mRNA_list)
##################################
# Modelling - mRNA #
##################################
# Mixed-effect modelling (Partial-bayesian)
varlist=mRNA_list
blups.models_1 <- lapply(varlist, function(x) {
mod2 = try(blmer(substitute(i ~ sex + age + symptom_score + Group + active_treatment +
(1|id/Group), list(i = as.name(x))),
data = me_memRNA_df1, na.action=na.exclude))
if(isTRUE(class(mod2)=='try-error')) {return(NULL)} else{return(mod2)}
})
blups.models_next1 = as.list(blups.models_1)
# Remove NULL models that failed to converge
blups.models_next1[sapply(blups.models_next1, is.null)] <- NULL
##################################
# Extract info from model - mRNA #
##################################
# Mixed-effect model expression
varlist1 <- lapply(blups.models_next1, function(f) summary(f)$call[2])
head(varlist1)
# Correlation Coefficient
estimate_AT <- lapply(blups.models_next1, function(f) summary(f)$coefficients[6,1])
estimate_KOS <- lapply(blups.models_next1, function(f) summary(f)$coefficients[5,1])
estimate_SS <- lapply(blups.models_next1, function(f) summary(f)$coefficients[4,1])
# Median
med <- lapply(blups.models_next1, function(f) summary(f)$residuals)
med_na <- lapply(med, function(f) na.exclude(f))
med_calc <- lapply(med_na, function(f) median(f))
## p-values for covariates (fixed-effect)
# SS
test_pSS <- lapply(blups.models_next1, function(f) parameters::p_value_wald(f)[4,])
df.pvalueSS <- as.data.frame(test_pSS)
df.pvalueSS <-t(df.pvalueSS)
df.pvalueSS = df.pvalueSS[seq(0, nrow(df.pvalueSS), 2), ]
# KOS
test_pKOS <- lapply(blups.models_next1, function(f) parameters::p_value_wald(f)[5,])
df.pvalueKOS <- as.data.frame(test_pKOS)
df.pvalueKOS <-t(df.pvalueKOS)
df.pvalueKOS = df.pvalueKOS[seq(0, nrow(df.pvalueKOS), 2), ]
# AT
test_pAT <- lapply(blups.models_next1, function(f) parameters::p_value_wald(f)[6,])
df.pvalueAT <- as.data.frame(test_pAT)
df.pvalueAT <-t(df.pvalueAT)
df.pvalueAT = df.pvalueAT[seq(0, nrow(df.pvalueAT), 2), ]
# Prepare dataframe with extracted info. for downstream use
test_data = list(as.character(varlist1), med_calc, estimate_AT, estimate_KOS, estimate_SS, as.numeric(t(df.pvalueSS)), as.numeric(t(df.pvalueKOS)), as.numeric(t(df.pvalueAT)))
names(test_data) <- c('genes', 'Median', 'Estimate_AT', 'Estimate_KOS', 'Estimate_SS', 'pvalueSS', 'pvalueKOS', 'pvalueAT')
test_final <- as.data.frame(do.call(rbind, test_data))
setwd('~/Documents/Mixed_Effects_Model/AT_rerun/MEM_table')
write.csv2(t(test_final), file='gene2_MEMtable_newSS.csv')