-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEARLI_PLACENTA_PV.Rmd
520 lines (402 loc) · 18.6 KB
/
EARLI_PLACENTA_PV.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
---
title: "MARBLES_CORD_ASD"
author: "John Dou"
date: "June 25, 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r library, message=F, warning=F}
library(minfi)
library(IlluminaHumanMethylation450kanno.ilmn12.hg19)
library(sas7bdat)
library(openxlsx)
res.path <- "/dcl01/NDEpi/data/Projects/InProgress/jdou/DNAm_EARLI_MARBLES/EARLI_placenta/"
```
```{r}
library(tidyr)
library(corrplot)
cells <- pd.cov[,c('COI_ID','Trophoblasts','Stromal','Hofbauer','Endothelial','nRBC','Syncytiotrophoblast')]
M <- cor(cells[,-1], method='spearman', use='pairwise.complete.obs')
pdf(file.path(res.path,'EARLI_cell_correlation.pdf'), height=14, width=14)
corrplot(M, method='ellipse', type='upper', addCoef.col='black')
dev.off()
cells.long <- pivot_longer(cells, cols=c('Trophoblasts','Stromal','Hofbauer','Endothelial','nRBC','Syncytiotrophoblast'))
cells.long$COI_ID <- factor(cells.long$COI_ID)
pdf('/dcl01/NDEpi/data/Projects/InProgress/jdou/DNAm_EARLI_MARBLES/EARLI_placenta/cell.pdf')
ggplot(cells.long, aes(fill=name, y=value, x=COI_ID)) +
geom_bar(position="fill", stat="identity")
dev.off()
cells <- pd.cov[,c('Trophoblasts','Stromal','Hofbauer','Endothelial','nRBC','Syncytiotrophoblast')]
pc <- princomp(cells)
pca <- pc$scores
pc.cell <- prcomp(t(cells))
pc.cell <- pc.cell$x
pc.cell <- data.frame(pc.cell)
pc.cell$cell <- rownames(pc.cell)
pdf('/dcl01/NDEpi/data/Projects/InProgress/jdou/DNAm_EARLI_MARBLES/EARLI_placenta/cell_pc.pdf')
ggplot(pc.cell, aes(y=PC2, x=PC1, label=cell)) +
geom_point() + geom_text()
dev.off()
cor.test(pca[,1],cells$Trophoblasts)
# Pearson's product-moment correlation
#
# data: pca[, 1] and cells$Trophoblasts
# t = 18.176, df = 125, p-value < 2.2e-16
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# 0.7956125 0.8934078
# sample estimates:
# cor
# 0.8517661
cor.test(pca[,1],cells$Stromal)
# Pearson's product-moment correlation
#
# data: pca[, 1] and cells$Stromal
# t = 6.1455, df = 125, p-value = 9.845e-09
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# 0.3356471 0.6051281
# sample estimates:
# cor
# 0.4816948
cor.test(pca[,1],cells$Hofbauer)
# Pearson's product-moment correlation
#
# data: pca[, 1] and cells$Hofbauer
# t = 4.6305, df = 125, p-value = 9.009e-06
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# 0.2233131 0.5220549
# sample estimates:
# cor
# 0.3826413
cor.test(pca[,1],cells$Endothelial)
# Pearson's product-moment correlation
#
# data: pca[, 1] and cells$Endothelial
# t = 6.5897, df = 125, p-value = 1.107e-09
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# 0.3659221 0.6265563
# sample estimates:
# cor
# 0.5077671
cor.test(pca[,1],cells$nRBC)
# Pearson's product-moment correlation
#
# data: pca[, 1] and cells$nRBC
# t = 2.9998, df = 125, p-value = 0.003261
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# 0.08894524 0.41463932
# sample estimates:
# cor
# 0.2591443
cor.test(pca[,1],cells$Syncytiotrophoblast)
# Pearson's product-moment correlation
#
# data: pca[, 1] and cells$Syncytiotrophoblast
# t = -110.56, df = 125, p-value < 2.2e-16
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# -0.9964289 -0.9927927
# sample estimates:
# cor
# -0.9949259
summary(lm(meanDNAm ~ Trophoblasts + Stromal + Hofbauer + Endothelial + nRBC + Syncytiotrophoblast, data=pd.cov))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 44.6954 0.3854 115.970 <2e-16 ***
Trophoblasts -3.8197 2.0057 -1.904 0.0592 .
Stromal 7.0843 2.8078 2.523 0.0129 *
Hofbauer -5.9737 9.5329 -0.627 0.5321
Endothelial -1.6531 4.0482 -0.408 0.6837
nRBC 11.8406 7.0471 1.680 0.0955 .
Syncytiotrophoblast NA NA NA NA
summary(lm(meanDNAm ~ Trophoblasts + Stromal + Hofbauer + Endothelial + nRBC, data=pd.cov))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 44.6954 0.3854 115.970 <2e-16 ***
Trophoblasts -3.8197 2.0057 -1.904 0.0592 .
Stromal 7.0843 2.8078 2.523 0.0129 *
Hofbauer -5.9737 9.5329 -0.627 0.5321
Endothelial -1.6531 4.0482 -0.408 0.6837
nRBC 11.8406 7.0471 1.680 0.0955 .
summary(lm(meanDNAm ~ Stromal + Hofbauer + Endothelial + nRBC + Syncytiotrophoblast, data=pd.cov))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 40.876 1.892 21.601 < 2e-16 ***
Stromal 10.904 3.752 2.906 0.00435 **
Hofbauer -2.154 9.502 -0.227 0.82106
Endothelial 2.167 5.151 0.421 0.67481
nRBC 15.660 7.744 2.022 0.04536 *
Syncytiotrophoblast 3.820 2.006 1.904 0.05923 .
summary(lm(meanDNAm ~ Trophoblasts, data=pd.cov))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 45.1816 0.2774 162.855 <2e-16 ***
# Trophoblasts -2.9007 1.8428 -1.574 0.118
summary(lm(meanDNAm ~ Stromal, data=pd.cov))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 44.363 0.265 167.434 <2e-16 ***
# Stromal 4.142 2.484 1.668 0.0979 .
summary(lm(meanDNAm ~ Hofbauer, data=pd.cov))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 44.5934 0.1887 236.364 <2e-16 ***
# Hofbauer 6.6209 5.8996 1.122 0.264
summary(lm(meanDNAm ~ Endothelial, data=pd.cov))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 45.034 0.242 186.124 <2e-16 ***
# Endothelial -4.441 3.760 -1.181 0.24
summary(lm(meanDNAm ~ nRBC, data=pd.cov))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 44.701 0.116 385.456 <2e-16 ***
# nRBC 4.957 4.110 1.206 0.23
summary(lm(meanDNAm ~ Syncytiotrophoblast, data=pd.cov))
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 44.7631 0.7273 61.549 <2e-16 ***
# Syncytiotrophoblast 0.0157 1.0921 0.014 0.989
```
## Load preprocessed data and covariates
```{r}
pd.cov <- readRDS(file.path(res.path,'QC/pd.rds'))
beta <- readRDS(file.path(res.path,'QC/beta.rds'))
supp <- read.sas7bdat('/dcl01/NDEpi/data/Projects/InProgress/jdou/MARBLES/Placenta_Supp/e_m_vit_supp_20190411.sas7bdat')
cov <- read.sas7bdat("/dcl01/NDEpi/data/Projects/InProgress/jdou/e_m_covars_v8_05may21.sas7bdat")
load("/dcl01/NDEpi/data/MasterCohortData/EARLI/EARLIBiologicalSamples/450k/earli.450k.pd.N=1225.20161117.rda")
pd.cov$Round <- pd[match(pd.cov$COI_ID, pd$Subject.ID), 'Round']
```
## Get supplement
```{r}
supp <- supp[match(pd.cov$COI_ID, supp$coi_id),]
pd.cov$PVmo1 <- supp$PV_YN_MO1
table(pd.cov$PVmo1,exclude = NULL)
```
# model fitting prep
```{r models}
library(qqman)
library(limma)
### no missing, no smokers
#get rid of smoking (n=6)
pd.mod <- pd.cov[pd.cov$smk_preg!=1,]
#get rid missing
pd.mod <- pd.mod[,c('COI_ID', 'PVmo1', 'mom_edu_bn', 'PC1', 'PC2', 'PC3', 'PC4', 'PC5',
'sex', 'mom_age', 'gest_age', 'Hofbauer', 'Syncytiotrophoblast', 'Round')]
pd.mod <- na.omit(pd.mod)
nrow(pd.cov) #127
nrow(pd.mod) #88
#put back in vars only for descriptive purposes
pd.mod$ancest_cat <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'ancest_cat']
pd.mod$meanDNAm.sea <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'meanDNAm.sea']
pd.mod$meanDNAm.shelf <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'meanDNAm.shelf']
pd.mod$meanDNAm.shore <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'meanDNAm.shore']
pd.mod$meanDNAm.island <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'meanDNAm.island']
pd.mod$meanDNAm <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'meanDNAm']
pd.mod$Trophoblasts <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'Trophoblasts'] * 100
pd.mod$Stromal <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'Stromal'] * 100
pd.mod$Hofbauer <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'Hofbauer'] * 100
pd.mod$Endothelial <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'Endothelial'] * 100
pd.mod$nRBC <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'nRBC'] * 100
pd.mod$Syncytiotrophoblast <- pd.cov[match(pd.mod$COI_ID, pd.cov$COI_ID),'Syncytiotrophoblast'] * 100
pd.mod$ancest_cat <- ifelse(pd.mod$ancest_cat=='EARLI Eur', 'European',
ifelse(pd.mod$ancest_cat=='EARLI Latin', 'Hispanic', 'Other/Mix'))
#check out BMI
cov.mod <- cov[match(pd.mod$COI_ID, cov$COI_ID),]
pd.mod$bmi <- cov.mod$Mat_BMI_PrePreg
### supplement by month table
supp <- supp[match(pd.mod$COI_ID, supp$coi_id),]
PV_months <- matrix(NA, nrow=3, ncol=12)
colnames(PV_months) <- colnames(supp)[22:33]
rownames(PV_months) <- c("Yes","No","Missing")
for(month in colnames(supp)[22:33]){
tab <- table(supp[,month],exclude=NULL)
PV_months["Yes", month] <- tab['1']
PV_months["No", month] <- tab['0']
PV_months["Missing", month] <- tab['NaN']
}
write.csv(PV_months, file=file.path(res.path,"PV/PV_months.csv"))
### table 1
library(compareGroups)
restab <- compareGroups(PVmo1 ~ mom_edu_bn + ancest_cat + mom_age +
sex + gest_age + Trophoblasts + Stromal + Hofbauer + Endothelial + nRBC + Syncytiotrophoblast +
meanDNAm + meanDNAm.sea + meanDNAm.shelf + meanDNAm.shore + meanDNAm.island + bmi + Round,
data=pd.mod, method=c(ancest_cat=3, Round=3))
tab1 <- createTable(restab)
export2word(tab1, file=file.path(res.path,'PV/table1.docx'))
```
# PC plot
```{r}
library(ggplot2)
library(reshape2)
pc <- prcomp(t(beta), center=T)
head(pc$x)
identical(colnames(pc$x), colnames(pd.cov))
for.plot <- data.frame(pc$x[,1:6])
table(rownames(pd.mod) %in% rownames(for.plot))
for.plot <- for.plot[rownames(pd.mod),]
for.plot$PVmo1 <- pd.mod$PVmo1
for.plot$batch <- as.factor(pd.mod$Round)
for.plot$sex <- pd.mod$sex
for.plot$mom_edu_bn <- pd.mod$mom_edu_bn
for.plot$mom_age <- pd.mod$mom_age
for.plot$gest_age <- pd.mod$gest_age
for.plot$Syncytiotrophoblast <- pd.mod$Syncytiotrophoblast
for.plot$Hofbauer <- pd.mod$Hofbauer
# pdf(file.path(res.path,"PC_plot.pdf"))
# pairs(for.plot[,1:4], col=as.factor(for.plot$sex), labels=c("PC1", "PC2", "PC3", "PC4"), pch=1, cex=0.5)
# pairs(for.plot[,1:4], col=for.plot$batch, labels=c("PC1", "PC2", "PC3", "PC4"), pch=1, cex=0.5)
# dev.off()
sex.plot <- ggplot(for.plot, aes(x=PC1, y=PC2, col=sex))+
geom_point() + theme_classic()
batch.plot <- ggplot(for.plot, aes(x=PC2, y=PC3, col=batch))+
geom_point() + theme_classic()
ggsave(batch.plot, file=file.path(res.path,"PC_batch.svg"))
ggsave(sex.plot, file=file.path(res.path,"PC_sex.svg"))
#heatmap PC vs covars
heatmap.pc <- matrix(NA, nrow=8, ncol=6)
colnames(heatmap.pc) <- paste0("PC",1:6)
rownames(heatmap.pc) <- c("PVmo1","mom_edu_bn","mom_age","gest_age","sex","Syncytiotrophoblast","Hofbauer","batch")
for(PC in colnames(heatmap.pc)){
heatmap.pc["PVmo1",PC] <- t.test(for.plot[,PC] ~ for.plot$PVmo1)$p.value
heatmap.pc["mom_edu_bn",PC] <- t.test(for.plot[,PC] ~ for.plot$mom_edu_bn)$p.value
heatmap.pc["mom_age",PC] <- cor.test(for.plot[,PC],for.plot$mom_age)$p.value
heatmap.pc["gest_age",PC] <- cor.test(for.plot[,PC],for.plot$gest_age)$p.value
heatmap.pc["sex",PC] <- t.test(for.plot[,PC] ~ for.plot$sex)$p.value
heatmap.pc["Syncytiotrophoblast",PC] <- cor.test(for.plot[,PC],for.plot$Syncytiotrophoblast)$p.value
heatmap.pc["Hofbauer",PC] <- cor.test(for.plot[,PC],for.plot$Hofbauer)$p.value
heatmap.pc["batch",PC] <- t.test(for.plot[,PC] ~ for.plot$batch)$p.value
}
heatmap.pc.wide <- melt(heatmap.pc)
heatmap.pc.wide$text <- round(heatmap.pc.wide$value, 3)
heatmap.pc.wide$text <- ifelse(heatmap.pc.wide$text < 0.001, "< 0.001", heatmap.pc.wide$text)
heatmap.pc.wide$groups <- cut(heatmap.pc.wide$value, breaks=c(0,0.001,0.05,0.1,0.5,1.0))
heatmap.plot <- ggplot(heatmap.pc.wide, aes(x=Var2, y=Var1, fill=groups)) +
geom_tile() +
scale_fill_manual(values=c("firebrick","tomato1","goldenrod1","khaki","white")) +
geom_text(aes(label=text), color="black") + xlab("") + ylab("") +
theme_bw() + theme(legend.position="none") +
scale_y_discrete(labels=c("Prenatal Vitamins\nMonth 1", "Maternal\nEducation", "Maternal\nAge","Gestational\nAge","Sex",
"Syncytiotrophoblast\nProportion","Hofbauer\nProportion","Batch"))
ggsave(heatmap.plot, file=file.path(res.path,"PC_heatmap.svg"), width=8, height=7)
```
### model
```{r}
mod <- model.matrix(~factor(pd.mod$PVmo1) + pd.mod$sex + pd.mod$mom_age + pd.mod$gest_age + pd.mod$mom_edu_bn + pd.mod$PC1 + pd.mod$PC2 + pd.mod$PC3 + pd.mod$PC4 + pd.mod$PC5 + pd.mod$Syncytiotrophoblast + pd.mod$Hofbauer + pd.mod$Round)
out <- lmFit(beta[,rownames(pd.mod)],mod)
out <- eBayes(out)
ss.hits.pv <- topTable(out, coef=2, number=nrow(beta))
lambda <- qchisq(median(ss.hits.pv$P.Value, na.rm=T), df = 1, lower.tail = F) / qchisq(0.5, 1)
png(file.path(res.path,'PV/QQ_pv_mo1.png'))
qq(ss.hits.pv$P.Value,main=paste0("Lambda = ", round(lambda,3)))
dev.off()
saveRDS(ss.hits.pv, file=file.path(res.path,"PV/model_results_pv_mo1.rds"))
### write table
setwd(file.path(res.path,"PV"))
source("/dcl01/NDEpi/data/Projects/InProgress/jdou/Folate/EARLI_folate_intake_functions.R")
ss.pv <- append.annotate(ss.hits.pv, what=c("chr","pos","gene"), array="450k")
nice.format <- function(ss.hits){
ss.hits$sd <- ss.hits$logFC/ss.hits$t
ss.hits <- ss.hits[,c("chr","pos","logFC","sd","P.Value","adj.P.Val","gene","AveExpr")]
names(ss.hits) <- c("chr","pos","DNAm_diff","sd","P.Value","adj.P.Val","gene","AveDNAm")
ss.hits$DNAm_diff <- signif(ss.hits$DNAm_diff*100,4)
ss.hits$sd <- signif(ss.hits$sd*100,4)
ss.hits$P.Value <- signif(ss.hits$P.Value, 3)
ss.hits$adj.P.Val <- round(ss.hits$adj.P.Val,2)
ss.hits$AveDNAm <- round(ss.hits$AveDNAm*100,2)
ss.hits$gene <- gsub(";.*","",ss.hits$gene)
ss.hits$gene <- paste0(" ", ss.hits$gene)
ss.hits
}
ss.pv.fmt <- nice.format(ss.pv)
write.csv(ss.pv.fmt, file="Supplementary Table 1 - EARLI Placenta Single Site Results.csv")
```
## single site results visualization
```{r ss visual}
setwd(file.path(res.path,"PV"))
ss.hits.pv <- readRDS("model_results_pv_mo1.rds")
plot.volcano <- function(ss.hits,main=NULL){
par(mar=c(5,5,3,3))
#get betas and pvals from single sites results
betas <- ss.hits$logFC
pvals <- -log(ss.hits$P.Value,10)
#assign colors
pcol <- ifelse(pvals < -log(0.05,10), 'darkslategrey', ifelse(betas>0,'cadetblue4','cadetblue3'))
#get percent hyper/hypo for sites above threshold
total <- table(pcol)['cadetblue4'] + table(pcol)['cadetblue3']
pct <- c(round(table(pcol)['cadetblue4']/total * 100,1),round(table(pcol)['cadetblue3']/total * 100,1))
#plotting stuff
plot(betas,pvals, las=1, main=main, xlab=paste0('Effect Estimate'), ylab=expression('-Log'[10]*'(P-Value)'),
xlim=c(-0.46,0.46),ylim=c(0,7),cex.axis=1.4,cex.lab=1.4,cex.main=1.4,cex=0.7,pch=21,col=pcol)
abline(h=-log(0.05,10),col='blue')
abline(h=7,col='red')
text(0.3,6,paste0(pct[1],'%'),col='cadetblue4',cex=2.2)
text(-0.3,6,paste0(pct[2],'%'),col='cadetblue3',cex=2.2)
}
png("volcano_pv_mo1.png")
plot.volcano(ss.hits.pv)
dev.off()
### manhattan plots
#append chr and bp info
data(Locations)
ss.hits.pv <- ss.hits.pv[rownames(ss.hits.pv) %in% rownames(Locations), ]
ss.hits.pv$CHR <- Locations[rownames(ss.hits.pv),"chr"]
ss.hits.pv$BP <- Locations[rownames(ss.hits.pv),"pos"]
ss.hits.pv$CHR <- as.numeric(gsub("chr","",ss.hits.pv$CHR))
png("manhattan_pv_mo1.png")
manhattan(ss.hits.pv, p="P.Value")
dev.off()
```
# pathway analysis
```{r pathway}
library(missMethyl)
setwd(file.path(res.path,"PV"))
ss.hits.pv <- readRDS("model_results_pv_mo1.rds")
table(ss.hits.pv$P.Value<0.01) #3647
top.pv <- rownames(ss.hits.pv[ss.hits.pv$P.Value<0.01, ])
mm.pv <- gometh(sig.cpg=top.pv, all.cpg=rownames(ss.hits.pv), collection="GO", array.type="450k")
path.pv <- topGSA(mm.pv, number=nrow(mm.pv))
path.pv <- path.pv[path.pv$ONTOLOGY=='BP',]
write.csv(path.pv, file="pathway_pv_mo1.csv")
```
# global
```{r global}
globalRegions <- function(pd, x, adj=NULL, summary.name){
#set up blank results
regions <- c('meanDNAm','meanDNAm.sea','meanDNAm.shore','meanDNAm.shelf','meanDNAm.island')
fits <- list()
fits[regions] <- NA
results <- matrix(NA, nrow=5, ncol=4)
rownames(results) <- regions
colnames(results) <- c('est', 'std_err', 't', 'p')
#do linear model for each region
for(region in regions){
eqn <- paste0(region, '~', x, '+', adj)
fits[[region]] <- lm(as.formula(eqn), data=pd)
results[region,] <- summary(fits[[region]])$coefficients[2,]
}
#print out summaries
sink(paste0(summary.name,'.txt'), append=TRUE)
print(lapply(fits, summary))
sink()
return(results)
}
pd.mod$PVmo1 <- factor(pd.mod$PVmo1)
PV_all <- globalRegions(pd.mod, x='PVmo1', adj='sex + mom_age + gest_age + mom_edu_bn + PC1 + PC2 + PC3 + PC4 + PC5 + Syncytiotrophoblast + Hofbauer', summary.name='PV_all')
saveRDS(PV_all, file='global_pv_mo1.rds')
PV_M <- globalRegions(pd.mod[pd.mod$sex=='F',], x='PVmo1', adj='mom_age + gest_age + mom_edu_bn + PC1 + PC2 + PC3 + PC4 + PC5 + Syncytiotrophoblast + Hofbauer', summary.name='PV_all')
PV_F <- globalRegions(pd.mod[pd.mod$sex=='M',], x='PVmo1', adj='mom_age + gest_age + mom_edu_bn + PC1 + PC2 + PC3 + PC4 + PC5 + Syncytiotrophoblast + Hofbauer', summary.name='PV_all')
```
#PV_M
n=53
est std_err t p
meanDNAm -0.36953881 0.4630136 -0.7981166 0.4329667
meanDNAm.sea -0.61619080 0.5676966 -1.0854227 0.2889807
meanDNAm.shore -0.28990007 0.4849779 -0.5977594 0.5558410
meanDNAm.shelf -0.63891939 0.5924246 -1.0784822 0.2919999
meanDNAm.island -0.06445147 0.3812652 -0.1690463 0.8672382
#PV_F
n=35
est std_err t p
meanDNAm -0.6683505 0.3958199 -1.688522 0.09890423
meanDNAm.sea -0.6039177 0.4897448 -1.233127 0.22455176
meanDNAm.shore -0.8006360 0.4025803 -1.988761 0.05342954
meanDNAm.shelf -0.6164033 0.5365575 -1.148811 0.25729201
meanDNAm.island -0.6594311 0.3240802 -2.034778 0.04837425