-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale_transformation_code_HUMAN_HEREDITY_PAPER.R
1121 lines (1121 loc) · 50.2 KB
/
scale_transformation_code_HUMAN_HEREDITY_PAPER.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#############################################################
#############################################################
### Transforming summary statistics from logistic ###########
### regression to the liability scale: Application to #######
### genetic and environmental risk scores ###################
#############################################################
### Human Heredity. DOI: 10.1159/000495697 ##################
#############################################################
### Contact details: [email protected] ################
### Please contact me with any problems or if you want to ###
### use this method for your work and require assistance ####
#############################################################
### ORDER OF SCRIPT #########################################
#############################################################
### 1. FIGURE 1: Normal and standardised Logistic CDFs
### 2.1. FIGURE 2: Log OR versus liability effect size for a
# binary risk factor, for a range of disease prevalences and
# risk factor PDFs
### 2.2. FIGURES A1 and A2: Difference between equations 4
# and 5, and 4 and 6, respectively, for a binary risk factor,
# for a range of disease prevalences and risk factor PDFs
### 3. SCHIZOPHRENIA RISK EXAMPLE
### 3.1. Liability threshold models (LTMs)
### 3.1.1. General code and functions
### 3.1.2. Parameterising PRS only, ERS only and joint LTMs
### 3.1.3. Risk estimation; all LTMs
### 3.1.4. FIGURE 3: Schizophrenia risk; all LTMs
### 3.1.5. Specific risk examples from text in paper
### 3.2. Logistic ERS only model
### 3.2.1. General code and functions
### 3.2.2. Parameterising model
### 3.1.3. Risk estimation ERS only logistic model
### 3.2.4. FIGURE 4: ERS only logistic model expected risk
# versus ERS only liability model expected risk
### 3.3. Additional possible approximate logistic models
# (presented in the Appendix of the paper)
### Sincere apologies that the code for this section is
# not easy to follow, and irregularly named. I aim to change
# this after my maternity leave (by start of 2020)
### 3.3.1. Possible approximate PRS only logistic model
### 3.3.2. Possible approximate joint PRS and ERS logistic
# model
### 3.3.3. FIGURE A3. Estimated liability thresmodel model
# risks against estimated logistic model risks for ERS only,
# PRS only and, PRS and ERS models
### NOTE!!!
### PLEASE DOWNLOAD AND SAVE FILES:
### A. scz_risk_ers_prs.txt
# Contains the expected schizophrenia risks for the joint PRS and
# ERS liability threshold model (downloaded the pre-calculated risks
# will save the reader time)
### B. scz_prs_snps.txt
# Originally downloaded from the PGC website, contained the ORs
# for the SNPs used in the PRS analysis (therefore contains
# more SNPs than the final PRS)
### C. SCZ_1000G_AF.txt
# Originally obtained from the 1000G project website, contains
# the MAF for the PRS SNPs in the European 1000G population
# This is available from:
# https://github.com/alexgillett/scale_transformation
#############################################################
### Libraries needed and source other scripts ###############
#############################################################
### INSTALL REQUIRED LIBRARIES:
library(BB) # dfsane function for solving simultaneous eqns
library(ggplot2)
library(data.table)
#############################################################
### 1. FIGURE 1 #############################################
#############################################################
### Normal and standardised Logistic CDFs
xrange <- seq(from = -3, to = 3, by = 0.01)
CDF.norm <- pnorm(xrange)
CDF.logis <- plogis(xrange, s=sqrt(3)/pi)
plot(x=xrange, y=CDF.norm, bty="l", xlab="x", ylab="Probability", type="l", lwd=2)
points(x=xrange, y=CDF.logis, col="dark grey", type="l", lty=2, lwd=2)
leg.txt <- c("Standard normal CDF", "Standardised logistic CDF")
legend("topleft", legend=leg.txt, col=c("black", "dark grey"), lty=c(1,2), lwd=c(2,2), text.col=c("black", "dark grey"), bty="n")
#############################################################
### 2.1. FIGURE 2 ###########################################
#############################################################
### Log OR versus liability effect size for a binary risk
# factor, for a range of disease prevalences and risk factor
# PDFs
### Input values:
K.vec <- c(1/10000, 1/1000, 1/100, 1/10, 1/5)
OR.vec <- c(1.001, 1.005, 1.01, 1.05, 1.1, 1.5, 2, 5, 10)
logOR.vec <- log(OR.vec)
pdfRF.vec <- c(1/10000, 1/100, 1/5)
### Scale transformation functions
### Equation 3 in paper is equivalent to equation 4 for a binary RF
### Eqns 4, 5 and 6 function:
logit2liab.f <- function(OR, K, prob){
functionb0.e4 <- function(p){
r <- rep(NA, length(p))
r[1] <- ((1-prob)*(1/(1 + exp(-p[1])))) + (prob*(1/(1 + exp(-p[1])*exp(-log(OR))))) - K
r
}
root.out <- uniroot(functionb0.e4, c(-100,100))
b0.e4 <- root.out$root
pDlogit.RF1.e4 <- 1/(1 + (exp(-b0.e4)*exp(-log(OR))))
pDlogit.RF0.e4 <- 1/(1 + (exp(-b0.e4)))
b0.e6 <- log(K/(1-K))
pDlogit.RF1.e6 <- 1/(1 + (exp(-b0.e6)*exp(-log(OR))))
pDlogit.RF0.e6 <- 1/(1 + (exp(-b0.e6)))
varRF <- prob*(1-prob)
a <- qnorm(pDlogit.RF1.e4) - qnorm(pDlogit.RF0.e4)
tau.e4 <- a/sqrt(1 + (a^2)*varRF)
tau.e5 <- a
tau.e6 <- qnorm(plogis(log(K/(1-K)) + log(OR))) - qnorm(K)
out <- c(b0.e4, b0.e6, pDlogit.RF0.e4, pDlogit.RF1.e4, pDlogit.RF0.e6, pDlogit.RF1.e6, tau.e4, tau.e5, tau.e6)
out
}
### Generation of output data
logit2liab.out <- NULL
names.logit2liab.out <- c("K", "OR", "logOR", "prob", "b0.e4", "b0.e6", "pDlogit.RF0.e4", "pDlogit.RF1.e4", "pDlogit.RF0.e6", "pDlogit.RF1.e6", "tau.e4", "tau.e5", "tau.e6")
for(i in 1:length(K.vec)){
for(j in 1:length(OR.vec)){
for(k in 1:length(pdfRF.vec)){
out.ijk <- c(K.vec[i], OR.vec[j], logOR.vec[j], pdfRF.vec[k])
work.out.ijk <- logit2liab.f(OR=OR.vec[j], K=K.vec[i], prob= pdfRF.vec[k])
out.ijk <- c(out.ijk, work.out.ijk)
out.ijk <- t(matrix(out.ijk))
logit2liab.out <- rbind(logit2liab.out, out.ijk)
rm(out.ijk, work.out.ijk)
}
}
}
colnames(logit2liab.out) <- names.logit2liab.out
logit2liab.out <- data.frame(logit2liab.out)
K_prob <- rep("", dim(logit2liab.out)[1])
for(i in 1:dim(logit2liab.out)[1]){
K_prob[i] <- paste(as.character(logit2liab.out$K[i]), as.character(logit2liab.out$prob[i]), sep="_")
}
logit2liab.out$K_prob <- K_prob
logit2liab.out$d.e4e5 <- logit2liab.out$tau.e4 - logit2liab.out$tau.e5
logit2liab.out$d.e4e6 <- logit2liab.out$tau.e4 - logit2liab.out$tau.e6
# Change inputs (K and PDF values) into factors for ease of plotting
K.f <- rep("", dim(logit2liab.out)[1])
prob.f <- rep("", dim(logit2liab.out)[1])
K.f[logit2liab.out$K == 1/1000] <- "1/1000"
K.f[logit2liab.out$K == 1/100] <- "1/100"
K.f[logit2liab.out$K == 1/5] <- "1/5"
K.f[logit2liab.out$K == 1/10000] <- "1/10000"
K.f[logit2liab.out$K == 1/10] <- "1/10"
K.f <- factor(K.f, levels=c("1/5","1/10", "1/100","1/1000", "1/10000"), ordered=T)
logit2liab.out$Prevalence <- K.f
prob.f[logit2liab.out$prob==1/10000] <- "p(X=1)=1/10000"
prob.f[logit2liab.out$prob==1/100] <- "p(X=1)=1/100"
prob.f[logit2liab.out$prob==1/5] <- "p(X=1)=1/5"
prob.f <- factor(prob.f, levels = c("p(X=1)=1/10000","p(X=1)=1/100","p(X=1)=1/5"), ordered=T)
logit2liab.out$prob.f <- prob.f
### Figure 2
p2 <- ggplot(logit2liab.out, aes(x=logOR, y=tau.e4, colour=Prevalence)) + geom_point() + geom_line(lwd=1) + xlab("Log odds ratio") + ylab("Effect size estimate on the liability scale") + scale_color_grey() +theme_bw()
p2 + facet_grid(prob.f~.)
#############################################################
### 2.2. FIGURES A1 and A2 ##################################
#############################################################
# FIGURES A1 and A2: Difference between equations 4
# and 5, and 4 and 6, respectively, for a binary risk factor,
# for a range of disease prevalences and risk factor PDFs
pA1 <- ggplot(logit2liab.out, aes(x=logOR, y=d.e4e5, colour=Prevalence)) + geom_point() + geom_line(lwd=1) + xlab("Log odds ratio") + ylab("Difference in liability effect size: Equations (4) - (5)") + scale_color_grey() +theme_bw()
pA1 + facet_grid(prob.f~.)
pA2 <- ggplot(logit2liab.out, aes(x=logOR, y=d.e4e6, colour=Prevalence)) + geom_point() + geom_line(lwd=1) + xlab("Log odds ratio") + ylab("Difference in liability effect size: Equations (4) - (6)") + scale_color_grey() +theme_bw()
pA2 + facet_grid(prob.f~.)
#############################################################
#############################################################
### 3. SCHIZOPHRENIA RISK EXAMPLE ###########################
#############################################################
#############################################################
### 3.1. Liability threshold models (LTMs) ##################
#############################################################
### 3.1.1. General code and functions to be used ############
#############################################################
### Joint risk function: ERS and PRS in model
risk_function <- function(ers, prs, E_ERS, V_ERS, E_PRS=0, V_PRS, K){
E_L <- E_PRS + E_ERS
L_T <- E_L - qnorm(K)
denom <- sqrt(1 - V_ERS - V_PRS)
risk <- pnorm(-(L_T - ers - prs)/denom)
risk
}
### Risk function: only ERS:
risk_single_ERS_function <- function(ers, E_ERS, E_PRS = 0, V_ERS, K){
E_L <- E_PRS + E_ERS
L_T <- E_L - qnorm(K)
denom <- sqrt(1 - V_ERS)
risk <- pnorm(-(L_T - ers)/denom)
risk
}
### Risk function: only PRS:
risk_single_PRS_function <- function(prs, E_ERS, E_PRS = 0, V_PRS, K){
E_L <- E_PRS + E_ERS
L_T <- qnorm(1-K)
denom <- sqrt(1 - V_PRS)
risk <- pnorm(-(L_T - prs)/denom)
risk
}
#############################################################
### 3.1.2. Parameterising PRS only, ERS only and joint LTMs #
#############################################################
### General known parameters:
###################################################
K <- 0.01 ### prevalence
H2 <- 0.81 ### Broad-sense heritability
h2 <- 0.81 ### Narrow-sense heritability
VML <- 0.07 ### Var[PRS] on L-scale
VD <- H2-h2 ### (Quasi) Dominant variance component
###################################################
### Environmental risk variables:
###################################################
### For each of the 5 environmental risk factors
# calculate the effect size estimates on the liability
# scale (denoted tau)
###################################################
### E1 = cannabis usage
### Number of categories contained within E1
levels_E1 <- 3
### probability distribution function (known)
pvecE1 <- c(0.70, 0.15, 0.15)
### OR estimates (known)
OR_E1_0 <- 1
OR_E1_1 <- 1.41
OR_E1_2 <- 2.78
lOR_E1_1 <- log(OR_E1_1)
lOR_E1_2 <- log(OR_E1_2)
### Penetrance function calculated using K and OR
functionb0logit_E1 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE1[1]*(1/(1 + exp(-p[1])))) + (pvecE1[2]*(1/(1 + exp(-p[1])*exp(-lOR_E1_1)))) + (pvecE1[3]*(1/(1 + exp(-p[1])*exp(-lOR_E1_2)))) - K
r
}
functionb0logit_E1_2 <- function(p){
(pvecE1[1]*(1/(1 + exp(-p)))) + (pvecE1[2]*(1/(1 + exp(-p)*exp(-lOR_E1_1)))) + (pvecE1[3]*(1/(1 + exp(-p)*exp(-lOR_E1_2)))) - K
}
p0 <- rep(0.001, 1)
outE1 <- dfsane(par=p0, fn=functionb0logit_E1, control=list(trace=F))
outE1
#spg(par=p0, fn=functionb0logit_E1_2, control=list(ftol = 1.e-16, gtol=1.e-16))
uniroot(functionb0logit_E1_2, c(-10,10))
b0logitE1 <- outE1$par[1]
#pD_E1_0 <- 0.007562538
pD_E1_0 <- 1/(1 + exp(-1*b0logitE1))
1/(1 + exp(-1*-12.00201))
#pD_E1_1 <- 0.010630218
pD_E1_1 <- 1/(1 + exp(-1*b0logitE1)*exp(-1* lOR_E1_1))
#pD_E1_2 <- 0.020744605
pD_E1_2 <- 1/(1 + exp(-1*b0logitE1)*exp(-1* lOR_E1_2))
b1logitE1 <- lOR_E1_1
b2logitE1 <- lOR_E1_2
###
tau_work <- c(b0logitE1, b0logitE1+b1logitE1, b0logitE1+b2logitE1)
tau_work <- qnorm(plogis(tau_work)) - qnorm(plogis(b0logitE1))
C <- sum((tau_work^2)*pvecE1) - sum(as.matrix(tau_work*pvecE1)%*%t(as.matrix(tau_work*pvecE1)))
tauE1vec <- sqrt(1/(1+C))*tau_work
CE1 <- C
VE1 <- CE1/(1+CE1)
tau0E1 <- tauE1vec[1]
tau1E1 <- tauE1vec[2]
tau2E1 <- tauE1vec[3]
E_E1 <- sum(tauE1vec*pvecE1)
###################################################
### E2 = Migration
levels_E2 <- 2
pvecE2 <- c(0.924, 1-0.924)
OR_E2_0 <- 1
OR_E2_1 <- 2.3
lOR_E2_1 <- log(OR_E2_1)
### Penetrance function calculated using K and OR
functionb0logit_E2 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE2[1]*(1/(1 + exp(-p[1])))) + (pvecE2[2]*(1/(1 + exp(-p[1])*exp(-lOR_E2_1)))) - K
r
}
p0 <- rep(0.001, 1)
outE2 <- dfsane(par=p0, fn=functionb0logit_E2, control=list(trace=F))
outE2
b0logitE2 <- outE2$par[1]
pD_E2_0 <- 1/(1 + exp(-1*b0logitE2))
pD_E2_1 <- 1/(1 + exp(-1*b0logitE2)*exp(-1*lOR_E2_1))
#b0logitE2 <- log(pD_E2_0/(1-pD_E2_0))
b1logitE2 <- lOR_E2_1
b0probitE2 <- qnorm(plogis(b0logitE2))
b1probitE2 <- qnorm(plogis(b0logitE2 + b1logitE2)) - b0probitE2
tau0E2 <- 0
### Calculate effect size estimate on the L-scale. Note
# different method used compared to E1. Both give
# the same answer
tau1E2 <- b1probitE2/sqrt(1 + ((b1probitE2^2)*pvecE2[2]*(1-pvecE2[2])))
tauE2vec <- c(tau0E2, tau1E2)
VE2 <- ((tau1E2^2)*pvecE2[2]*(1-pvecE2[2]))
E_E2 <- sum(tauE2vec*pvecE2)
###################################################
### E3 = Urbanicity
levels_E3 <- 3
pvecE3 <- c(0.25, 0.25, 0.5)
OR_E3_0 <- 1
OR_E3_1 <- 1.5
OR_E3_2 <- 2
lOR_E3_1 <- log(OR_E3_1)
lOR_E3_2 <- log(OR_E3_2)
### Penetrance function calculated using K and OR
functionb0logit_E3 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE3[1]*(1/(1 + exp(-p[1])))) + (pvecE3[2]*(1/(1 + exp(-p[1])*exp(-lOR_E3_1)))) + (pvecE3[3]*(1/(1 + exp(-p[1])*exp(-lOR_E3_2)))) - K
r
}
p0 <- rep(0.001, 1)
outE3 <- dfsane(par=p0, fn=functionb0logit_E3, control=list(trace=F))
outE3
b0logitE3 <- outE3$par[1]
pD_E3_0 <- 1/(1 + exp(-1*b0logitE3))
pD_E3_1 <- 1/(1 + exp(-1*b0logitE3)*exp(-1*lOR_E3_1))
pD_E3_2 <- 1/(1 + exp(-1*b0logitE3)*exp(-1*lOR_E3_2))
###
b1logitE3 <- lOR_E3_1
b2logitE3 <- lOR_E3_2
b0probitE3 <- qnorm(plogis(b0logitE3))
b1probitE3 <- qnorm(plogis(b0logitE3 + b1logitE3)) - b0probitE3
b2probitE3 <- qnorm(plogis(b0logitE3 + b2logitE3)) - b0probitE3
###
tau_work <- c(b0logitE3, b0logitE3+b1logitE3, b0logitE3+b2logitE3)
tau_work <- qnorm(plogis(tau_work)) - qnorm(plogis(b0logitE3))
C <- sum((tau_work^2)*pvecE3) - sum(as.matrix(tau_work*pvecE3)%*%t(as.matrix(tau_work*pvecE3)))
tauE3vec <- sqrt(1/(1+C))*tau_work
CE3 <- C
VE3 <- CE3/(1+CE3)
tau0E3 <- tauE3vec[1]
tau1E3 <- tauE3vec[2]
tau2E3 <- tauE3vec[3]
E_E3 <- sum(tauE3vec*pvecE3)
###################################################
### E4 = Paternal age
levels_E4 <- 7
pvecE4 <- c(0.342, 0.204, 0.252, 0.123, 0.052, 0.019, 0.008)
OR_E4_0 <- 1
OR_E4_1 <- 1.06
OR_E4_2 <- 1.06
OR_E4_3 <- 1.13
OR_E4_4 <- 1.22
OR_E4_5 <- 1.21
OR_E4_6 <- 1.66
lOR_E4_1 <- log(OR_E4_1)
lOR_E4_2 <- log(OR_E4_2)
lOR_E4_3 <- log(OR_E4_3)
lOR_E4_4 <- log(OR_E4_4)
lOR_E4_5 <- log(OR_E4_5)
lOR_E4_6 <- log(OR_E4_6)
### Penetrance function calculated using K and OR
functionb0logit_E4 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE4[1]*(1/(1 + exp(-p[1])))) + (pvecE4[2]*(1/(1 + exp(-p[1])*exp(-lOR_E4_1)))) + (pvecE4[3]*(1/(1 + exp(-p[1])*exp(-lOR_E4_2)))) + (pvecE4[4]*(1/(1 + exp(-p[1])*exp(-lOR_E4_3)))) + (pvecE4[5]*(1/(1 + exp(-p[1])*exp(-lOR_E4_4)))) + (pvecE4[6]*(1/(1 + exp(-p[1])*exp(-lOR_E4_5)))) + (pvecE4[7]*(1/(1 + exp(-p[1])*exp(-lOR_E4_6)))) - K
r
}
p0 <- rep(0.001, 1)
outE4 <- dfsane(par=p0, fn=functionb0logit_E4, control=list(trace=F))
outE4
b0logitE4 <- outE4$par[1]
pD_E4_0 <- 1/(1 + exp(-1*b0logitE4))
pD_E4_1 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_1))
pD_E4_2 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_2))
pD_E4_3 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_3))
pD_E4_4 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_4))
pD_E4_5 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_5))
pD_E4_6 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_6))
b1logitE4 <- lOR_E4_1
b2logitE4 <- lOR_E4_2
b3logitE4 <- lOR_E4_3
b4logitE4 <- lOR_E4_4
b5logitE4 <- lOR_E4_5
b6logitE4 <- lOR_E4_6
###
tau_work <- c(b0logitE4, b0logitE4+b1logitE4, b0logitE4+b2logitE4, b0logitE4+b3logitE4, b0logitE4+b4logitE4,b0logitE4+b5logitE4, b0logitE4+b6logitE4)
tau_work <- qnorm(plogis(tau_work)) - qnorm(plogis(b0logitE4))
C <- sum((tau_work^2)*pvecE4) - sum(as.matrix(tau_work*pvecE4)%*%t(as.matrix(tau_work*pvecE4)))
tauE4vec <- sqrt(1/(1+C))*tau_work
CE4 <- C
VE4 <- CE4/(1+CE4)
tau0E4 <- tauE4vec[1]
tau1E4 <- tauE4vec[2]
tau2E4 <- tauE4vec[3]
tau3E4 <- tauE4vec[4]
tau4E4 <- tauE4vec[5]
tau5E4 <- tauE4vec[6]
tau6E4 <- tauE4vec[7]
E_E4 <- sum(tauE4vec*pvecE4)
###################################################
### E5 = Childhood adversity
levels_E5 <- 2
pvecE5 <- c(0.73, 1-0.73)
OR_E5_0 <- 1
OR_E5_1 <- 2.78
lOR_E5_1 <- log(OR_E5_1)
### Penetrance function calculated using K and OR
functionb0logit_E5 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE5[1]*(1/(1 + exp(-p[1])))) + (pvecE5[2]*(1/(1 + exp(-p[1])*exp(-lOR_E5_1)))) - K
r
}
p0 <- rep(0.001, 1)
outE5 <- dfsane(par=p0, fn=functionb0logit_E5, control=list(trace=F))
outE5
b0logitE5 <- outE5$par[1]
pD_E5_0 <- 1/(1 + exp(-1*b0logitE5))
pD_E5_1 <- 1/(1 + exp(-1*b0logitE5)*exp(-1*lOR_E5_1))
b1logitE5 <- lOR_E5_1
b0probitE5 <- qnorm(plogis(b0logitE5))
b1probitE5 <- qnorm(plogis(b0logitE5 + b1logitE5)) - b0probitE5
tau0E5 <- 0
tau1E5 <- b1probitE5/sqrt(1 + ((b1probitE5^2)*pvecE5[2]*(1-pvecE5[2])))
tauE5vec <- c(tau0E5, tau1E5)
VE5 <- ((tau1E5^2)*pvecE5[2]*(1-pvecE5[2]))
E_E5 <- sum(tauE5vec*pvecE5)
###################################################
### Extra parameters relating to the ERS:
# Mean of the ERS (on the liaility scale)
E_ERS <- E_E1 + E_E2 + E_E3 + E_E4 + E_E5
# Variance of the ERS (on the liaility scale)
V_ERS <- VE1 + VE2 + VE3 + VE4 + VE5
# Disease threshold approximation
D_threshold <- qnorm(1-K) + E_ERS
# All possible combinations of the environmental risk factors
combos_E <- expand.grid(1:levels_E1, 1:levels_E2, 1:levels_E3, 1:levels_E4, 1:levels_E5)
# ers_vec: vector containing all possible (unique) ERS values
# prob_ers_vec: vector containing the probability for each entry in ers_vec
ers_vec <- rep(0, dim(combos_E)[1])
prob_ers_vec <- rep(0, dim(combos_E)[1])
for(i in 1:dim(combos_E)[1]){
EI_i <- unlist(combos_E[i,])
prob_ers_vec[i] <- pvecE1[EI_i[1]]*pvecE2[EI_i[2]]*pvecE3[EI_i[3]]*pvecE4[EI_i[4]]*pvecE5[EI_i[5]]
ers_vec[i] <- tauE1vec[EI_i[1]] + tauE2vec[EI_i[2]] + tauE3vec[EI_i[3]] + tauE4vec[EI_i[4]] + tauE5vec[EI_i[5]]
}
range(ers_vec)
length(unique(ers_vec)) ### 216 versus 252 combinations
### multiple combinations of the environmental risk variables
# can therefore give the same ERS value: we want unique values
# only in the ERS vector- combine duplicates and update
# ERS probability to reflect this:
ers_vec_old <- ers_vec
ers_vec <- unique(ers_vec)
prob_ers_vec_old <- prob_ers_vec
prob_ers_vec <- rep(0, length(ers_vec))
for(i in 1:length(ers_vec)){
ers_i <- ers_vec[i]
match_i <- rep(0, length(ers_vec_old))
for(j in 1:length(ers_vec_old)){
match_i[j] <- as.numeric(ers_vec_old[j] == ers_i)
}
probs_ij <- prob_ers_vec_old[match_i == 1]
prob_ers_vec[i] <- sum(probs_ij)
}
### Obtain ERS descriptive statistics
# Median ERS:
med_ers <- quantile(ers_vec)[3]
med_ers ## 0.7156674
# Modal ERS:
max(prob_ers_vec) ## 0.1076534
modeERS <- ers_vec[prob_ers_vec == max(prob_ers_vec)] ## 0.2737725
modeERS
###################################################
### Range of ERS and PRS to use in risk estimation:
# ERS values to use:
ersI <- c(ers_vec, 0.72, 0.27)
# PRS values to use:
# Standardised/ normalised PRS:
prsI <- c(0, -1.96, 1.96, -1.64, 1.64, 0.67, -0.67, seq(from=-3.5, to = 3, length.out=1000))
# Corresponding PRS values on the liability scale:
prsI_L <- sqrt(VML)*prsI
#############################################################
### 3.1.3. Risk estimation; all LTMs ########################
#############################################################
### Joint risk: PRS, ERS
###################################################
### Schizophrenia risk generation for the above
# defined ranges of ERS and PRS
### Blanked out as data can be downloaded form GitHub
### Code provided so that user can generate their own
# dataset if required
### Please update FILEPATH in code!!!
###################################################
# # names.out <- c("ERS", "PRS", "specialE", "E1", "E2", "E3", "E4", "E5", "risk")
# write.table(t(matrix(names.out)), file="FILEPATH/scz_risk_ers_prs.txt", row.names=F, col.names=F)
# ### NOTE specialE == {Min ERS, Median ERS, Max ERS, Modal ERS} = {1, 2, 3, 4}
# for(i in 1:length(ersI)){
# for(j in 1:length(prsI)){
# risk_ij <- risk_function(ers=ersI[i], prs=prsI_L[j], E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
# out_ij <- rep(0, length(names.out))
# out_ij[1] <- ersI[i]
# out_ij[2] <- prsI[j]
# if(out_ij[1] == 0){
# out_ij[3] <- 1
# }
# if(out_ij[1] == 0.72){
# out_ij[3] <- 2
# }
# if(out_ij[1] == max(ersI)){
# out_ij[3] <- 3
# }
# if(out_ij[1] == 0.27){
# out_ij[3] <- 4
# }
# if(i <= dim(combos_E)[1]){
# out_ij[4:8] <- unlist(combos_E[i,])
# }else{
# out_ij[4:8] <- NA
# }
# out_ij[9] <- risk_ij
# out_ij <- t(matrix(out_ij))
# write.table(out_ij, file="FILEPATH/scz_risk_ers_prs.txt", row.names=F, col.names=F, append=T, quote=F)
# rm(risk_ij)
# }
# }
###################################################
### READ IN GENERATED SCHIZOPHRENIA RISK DATA
###################################################
### Please update FILEPATH!!!
scz <- read.table(file="FILEPATH/scz_risk_ers_prs.txt", header=T)
dim(scz) # [1] 219526 9
scz <- data.frame(scz)
###################################################
### Single risk: PRS
###################################################
risk_prs <- risk_single_PRS_function(prs=sqrt(VML)*unique(scz$PRS), E_ERS=E_ERS, E_PRS = 0, V_PRS=VML, K=K)
scz_noERS <- cbind(unique(scz$PRS), risk_prs)
colnames(scz_noERS) <- c("PRS", "risk")
scz_noERS <- data.frame(scz_noERS)
scz_noERS <- scz_noERS[order(scz_noERS$PRS), ]
###################################################
### Single risk: ERS
###################################################
risk_ers <- risk_single_ERS_function(ers=unique(scz$ERS), E_ERS=E_ERS, E_PRS = 0, V_ERS= V_ERS, K=K)
scz_noPRS <- cbind(unique(scz$ERS), risk_ers)
colnames(scz_noPRS) <- c("ERS", "risk")
scz_noPRS <- data.frame(scz_noPRS)
scz_noPRS <- scz_noPRS[order(scz_noPRS$ERS), ]
#############################################################
### 3.1.4. FIGURE 3: Schizophrenia risk; all LTMs ###########
#############################################################
### NOTE: axis labels added externally (in Word)
###################################################
### Create Figure 2(a)
###################################################
scz1 <- scz[scz$specialE == 1, ]
scz2 <- scz[scz$specialE == 2, ]
scz3 <- scz[scz$specialE == 3, ]
scz4 <- scz[scz$specialE == 4, ]
scz1 <- scz1[order(scz1$risk), ]
scz2 <- scz2[order(scz2$risk), ]
scz3 <- scz3[order(scz3$risk), ]
scz4 <- scz4[order(scz4$risk), ]
### Group = ERS, PRS ranging betwen +/- 3:
nf <- layout(matrix(c(1,2),2,1,byrow = TRUE), c(4,3), c(3,1), TRUE)
layout.show(nf)
par(mar = c(3,3,1,1))
plot(x=scz_noERS$PRS, y=scz_noERS$risk, type="l", ylim = c(0, 0.3), xlab = "Polygenic risk score", ylab="Risk of schizophrenia", bty="n", axes=F, lwd=3, xlim=c(-3, 3), col="dark grey")
points(x=scz1$PRS, y=scz1$risk, type="l", lwd=2)
points(x=scz2$PRS, y=scz2$risk, type="l", lty=2, lwd=2)
points(x=scz3$PRS, y=scz3$risk, type="l", lty=3, lwd=2)
points(x=scz4$PRS, y=scz4$risk, type="l", lty=4, lwd=2)
leg.txt <- c("No ERS", "Maximum (ERS = 1.53)", "Median (ERS = 0.72)", "Mode (ERS = 0.27)","Minimum (ERS = 0)")
legend("topleft", legend=leg.txt, bty="n", lty=c(1, 3,2,4,1), col = c("dark grey", rep("black", 4)), lwd=c(3,rep(2,4)), text.col = c("dark grey", rep("black", 4)))
box(bty="l")
axis(2)
par(mar = c(3,3,1,1))
curve(dnorm(x, sd=1), from=-3, to=3, axes=F)
box(bty="l")
axis(1)
###################################################
### Create Figure 2(b)
###################################################
PRS1 <- scz[scz$PRS == 0, ]
### 95% CI
PRS2 <- scz[scz$PRS == 1.96, ]
PRS1$High95 <- PRS2$risk
PRS3 <- scz[scz$PRS == -1.96, ]
PRS1$Low95 <- PRS3$risk
PRS1 <- PRS1[order(PRS1$ERS), ]
nf <- layout(matrix(c(1,2),2,1,byrow = TRUE), c(4,3), c(3,1), TRUE)
pos.gray <- gray(seq(0.1,0.9,length=10))
layout.show(nf)
par(mar = c(3,3,1,1))
plot(x = scz_noPRS$ERS, y= scz_noPRS$risk, type="l", lwd = 3, ylim=c(0, 0.3), ylab="Risk of schizophrenia", bty="n", axes=F, col="dark grey")
points(x = PRS1$ERS, y=PRS1$risk, type="l", lwd = 2)
points(x = PRS1$ERS, y=PRS1$Low95, type="l", lwd = 2, lty=2, col=pos.gray[2])
points(x = PRS1$ERS, y=PRS1$High95, type="l", lwd = 2, lty=2, col=pos.gray[2])
leg.txt <- c("No PRS", "Mean PRS", "PRS = +/- 1.96")
legend("topleft", legend=leg.txt, lwd=c(3,2,2), lty=c(1,1,2), col = c("dark grey", "black", pos.gray[2]), text.col=c("dark grey", "black", pos.gray[2]), bty="n")
box(bty="l")
axis(2)
par(mar = c(3,3,1,1))
plot(ers_vec[order(ers_vec)], prob_ers_vec[order(ers_vec)], type="h", lwd=2, axes=F)
box(bty="l")
axis(1)
axis(2, labels = c("0.00", "0.11"), at=range(prob_ers_vec))
#############################################################
### 3.1.5. Specific risk examples from text in paper ########
#############################################################
### PRS only LTM:
# 95% of individuals have estimated risk between:
PRS_risk95L <- risk_single_PRS_function(prs = -1.96*sqrt(VML), E_ERS = E_ERS, V_PRS= VML, K=K)
PRS_risk95U <- risk_single_PRS_function(prs = 1.96*sqrt(VML), E_ERS = E_ERS, V_PRS= VML, K=K)
PRS_risk95L ### 0.0016
PRS_risk95U ### 0.0304
### an individual with PRS > 95th percentile risk and relative risk
risk_single_PRS_function(prs = qnorm(0.95)*sqrt(VML), E_ERS = E_ERS, V_PRS= VML, K=K) ### 0.025
risk_single_PRS_function(prs = qnorm(0.95)*sqrt(VML), E_ERS = E_ERS, V_PRS= VML, K=K)/K
### Risk PRS == 0
risk_single_PRS_function(prs = 0*sqrt(VML), E_ERS = E_ERS, V_PRS= VML, K=K) ### 0.0079
### ERS only LTM:
# Minimum risk (will be for minimum ERS here due to reference
# category definitions)
minERS_risk <- risk_single_ERS_function(ers = 0, E_ERS = E_ERS, V_ERS=V_ERS, K=K)
minERS_risk
# minERS_risk/K
# K/minERS_risk
# Maximum risk (will be for maximum ERS here due to reference
# category definitions)
maxERS_risk <- risk_single_ERS_function(ers = max(ers_vec), E_ERS = E_ERS, V_ERS=V_ERS, K=K)
maxERS_risk
maxERS_risk/K
# 95% of individuals have estimated risk between [X,Y]:
# Because ERS doesn't follow a standard distribution we opt
# to calculate intervals using bootstrap approach as follows...
# [NOTE: we also provide code to calculate the bootstrap 95% CI
# for risks from all LTMs (ERS only, PRS only and joint)]
###################################################
### A. GENERATE POPULATION SAMPLE:
###################################################
### This is to approximate 95% CI and other summary measures
# for joint risk estimates in the population under the
# liability model
### N = size of population to generate
N = 1500000
#E_ers <- sum(ers_vec*prob_ers_vec)
# PRS value (on the liability scale) for simulated population
prs_sample <- rnorm(N, sd=sqrt(VML))
# Set up vectors to contain ERS values, joint risk estimates,
# PRS-only risk estimates and ERS-only risk estimates:
ers_sample <- rep(0, N)
risk_sample <- rep(0, N)
ers_risk_sample <- rep(0, N)
prs_risk_sample <- rep(0, N)
# Set up vector to contain joint risk estimates when PRS = 0, ERS varying:
risk_prs0 <- rep(0, N)
# Set up vector to contain joint risk estimates when PRS = 1.96, ERS varying:
risk_prsU95 <- rep(0, N)
# Set up vector to contain joint risk estimates when PRS = -1.96, ERS varying:
risk_prsL95 <- rep(0, N)
# Set up vector to contain joint risk estimates when PRS varying, ERS = min = 0:
risk_ers0 <- rep(0, N)
# Set up vector to contain joint risk estimates when PRS varying, ERS = median = 0.27:
risk_ers0.27 <- rep(0, N)
# Set up vector to contain joint risk estimates when PRS varying, ERS = max = 1.53:
risk_ers1.53 <- rep(0, N)
# Set up vector to contain joint risk estimates when PRS varying, ERS = mean = 0.3884:
risk_mean_ers <- rep(0, N)
### Loop to generate risk ERS values and risk in sample:
for(i in 1:N){
sample_i <- unique((1:length(ers_vec))*rmultinom(1,1,prob=prob_ers_vec))
sample_i <- sample_i[sample_i>0]
ers_sample[i] <- ers_vec[sample_i]
risk_sample[i] <- risk_function(ers=ers_sample[i], prs=prs_sample[i],E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
ers_risk_sample[i] <- risk_single_ERS_function(ers = ers_sample[i], E_ERS = E_ERS, V_ERS=V_ERS, K=K)
prs_risk_sample[i] <- risk_single_PRS_function(prs = prs_sample[i], E_ERS = E_ERS, V_PRS=VML, K=K)
risk_prs0[i] <- risk_function(ers=ers_sample[i], prs=0,E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
risk_prsU95[i] <- risk_function(ers=ers_sample[i], prs=1.96*sqrt(VML),E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
risk_prsL95[i] <- risk_function(ers=ers_sample[i], prs=-1.96*sqrt(VML),E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
risk_ers0[i] <- risk_function(ers=0, prs=prs_sample[i],E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
risk_ers0.27[i] <- risk_function(ers=0.27, prs=prs_sample[i],E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
risk_ers1.53[i] <- risk_function(ers=max(ers_vec), prs=prs_sample[i],E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
risk_mean_ers[i] <- risk_function(ers=E_ERS, prs=prs_sample[i],E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K)
}
pop_sample <- cbind(prs_sample, ers_sample, risk_sample, ers_risk_sample, prs_risk_sample, risk_prs0, risk_prsL95, risk_prsU95, risk_ers0, risk_ers0.27, risk_ers1.53, risk_mean_ers)
pop_sample <- data.frame(pop_sample)
colnames(pop_sample)[1:5] <- c("prs", "ers", "risk", "ers_risk", "prs_risk")
###################################################
### B. Approx 95% of sampled individuals have a risk
# between...
###################################################
### PRS:
quantile(pop_sample$prs_risk, probs=c(0.025, 0.975))
### ERS:
#quantile(pop_sample$ers_risk, probs=c(0.025, 0.975))
### Use below due to ERS distribution properties:
quantile(pop_sample$ers_risk, probs=c(0.0, 0.95))
### PRS and ERS:
quantile(pop_sample$risk, probs=c(0.025, 0.975))
###################################################
### PRS and ERS LTM:
# Minimum and maximum risk when PRS == 0
risk_function(ers=0, prs=0, E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K) ### 0.0017
risk_function(ers=max(ers_vec), prs=0, E_ERS= E_ERS, V_ERS= V_ERS, V_PRS=VML, K=K) ### 0.1018
# See B. above for approximate 95% CI for risk
#############################################################
### 3.2. Logistic ERS only model ############################
#############################################################
### 3.2.1. General code and functions #######################
#############################################################
### Finding intercept (b0) for univariate logistic models
# Uses the penetrance function calculated using K and OR as
# inputs
### Function for variable E1 (Cannabis usage)
functionb0logit_E1 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE1[1]*(1/(1 + exp(-p[1])))) + (pvecE1[2]*(1/(1 + exp(-p[1])*exp(-lOR_E1_1)))) + (pvecE1[3]*(1/(1 + exp(-p[1])*exp(-lOR_E1_2)))) - K
r
}
### Function for variable E2 (Migration)
functionb0logit_E2 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE2[1]*(1/(1 + exp(-p[1])))) + (pvecE2[2]*(1/(1 + exp(-p[1])*exp(-lOR_E2_1)))) - K
r
}
### Function for variable E3 (Urbanicity)
functionb0logit_E3 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE3[1]*(1/(1 + exp(-p[1])))) + (pvecE3[2]*(1/(1 + exp(-p[1])*exp(-lOR_E3_1)))) + (pvecE3[3]*(1/(1 + exp(-p[1])*exp(-lOR_E3_2)))) - K
r
}
### Function for variable E4 (Paternal age)
functionb0logit_E4 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE4[1]*(1/(1 + exp(-p[1])))) + (pvecE4[2]*(1/(1 + exp(-p[1])*exp(-lOR_E4_1)))) + (pvecE4[3]*(1/(1 + exp(-p[1])*exp(-lOR_E4_2)))) + (pvecE4[4]*(1/(1 + exp(-p[1])*exp(-lOR_E4_3)))) + (pvecE4[5]*(1/(1 + exp(-p[1])*exp(-lOR_E4_4)))) + (pvecE4[6]*(1/(1 + exp(-p[1])*exp(-lOR_E4_5)))) + (pvecE4[7]*(1/(1 + exp(-p[1])*exp(-lOR_E4_6)))) - K
r
}
### Function for variable E5 (Childhood adversity)
functionb0logit_E5 <- function(p){
r <- rep(NA, length(p))
r[1] <- (pvecE5[1]*(1/(1 + exp(-p[1])))) + (pvecE5[2]*(1/(1 + exp(-p[1])*exp(-lOR_E5_1)))) - K
r
}
#############################################################
### 3.2.2. Parameterising model #############################
#############################################################
# To parameterise the logistic ERS only model we need to
# fully parameterise each univariate logistic model for
# variables E1 - E5 (specifically find each intercept). We
# then use numerical optimization with the univariate model
# parameters as inputs (this is function joint_logit_function
# given below in subheading 'Parameterising ERS only logistic
# model').
###################################################
### Variable E1 (Cannabis usage)
###################################################
# Find the intercept using functionb0logit_E1
p0 <- rep(0.001, 1)
outE1 <- dfsane(par=p0, fn=functionb0logit_E1, control=list(trace=F))
outE1
b0logitE1 <- outE1$par[1]
# E1 penetrance values for the logistic model
pD_E1_0 <- 1/(1 + exp(-1*b0logitE1))
pD_E1_1 <- 1/(1 + exp(-1*b0logitE1)*exp(-1*lOR_E1_1))
pD_E1_2 <- 1/(1 + exp(-1*b0logitE1)*exp(-1*lOR_E1_2))
# Additional univariate beta estimates
b1logitE1 <- lOR_E1_1
b2logitE1 <- lOR_E1_2
###################################################
### Variable E2 (Migration)
###################################################
# Find the intercept using functionb0logit_E2
p0 <- rep(0.001, 1)
outE2 <- dfsane(par=p0, fn=functionb0logit_E2, control=list(trace=F))
b0logitE2 <- outE2$par[1]
# E2 penetrance values for the logistic model
pD_E2_0 <- 1/(1 + exp(-1*b0logitE2))
pD_E2_1 <- 1/(1 + exp(-1*b0logitE2)*exp(-1*lOR_E2_1))
# Additional univariate beta estimates
b1logitE2 <- lOR_E2_1
###################################################
### Variable E3 (Urbanicity)
###################################################
# Find the intercept using functionb0logit_E3
p0 <- rep(0.001, 1)
outE3 <- dfsane(par=p0, fn=functionb0logit_E3, control=list(trace=F))
b0logitE3 <- outE3$par[1]
# E3 penetrance values for the logistic model
pD_E3_0 <- 1/(1 + exp(-1*b0logitE3))
pD_E3_1 <- 1/(1 + exp(-1*b0logitE3)*exp(-1*lOR_E3_1))
pD_E3_2 <- 1/(1 + exp(-1*b0logitE3)*exp(-1*lOR_E3_2))
# Additional univariate beta estimates
b1logitE3 <- lOR_E3_1
b2logitE3 <- lOR_E3_2
###################################################
### Variable E4 (Paternal age)
###################################################
# Find the intercept using functionb0logit_E4
p0 <- rep(0.001, 1)
outE4 <- dfsane(par=p0, fn=functionb0logit_E4, control=list(trace=F))
b0logitE4 <- outE4$par[1]
# E4 penetrance values for the logistic model
pD_E4_0 <- 1/(1 + exp(-1*b0logitE4))
pD_E4_1 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_1))
pD_E4_2 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_2))
pD_E4_3 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_3))
pD_E4_4 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_4))
pD_E4_5 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_5))
pD_E4_6 <- 1/(1 + exp(-1*b0logitE4)*exp(-1*lOR_E4_6))
# Additional univariate beta estimates
b1logitE4 <- lOR_E4_1
b2logitE4 <- lOR_E4_2
b3logitE4 <- lOR_E4_3
b4logitE4 <- lOR_E4_4
b5logitE4 <- lOR_E4_5
b6logitE4 <- lOR_E4_6
###################################################
### Variable E5 (Childhood adversity)
###################################################
# Find the intercept using functionb0logit_E5
p0 <- rep(0.001, 1)
outE5 <- dfsane(par=p0, fn=functionb0logit_E5, control=list(trace=F))
b0logitE5 <- outE5$par[1]
# E5 penetrance values for the logistic model
pD_E5_0 <- 1/(1 + exp(-1*b0logitE5))
pD_E5_1 <- 1/(1 + exp(-1*b0logitE5)*exp(-1*lOR_E5_1))
# Additional univariate beta estimates
b1logitE5 <- lOR_E5_1
###################################################
### Parameterising ERS only logistic model
###################################################
# Create a design matrix (called combos_p) which would
# be multipled by ERS only model parameters to calculate
# ERS
combos_p <- matrix(0, nrow=dim(combos_E)[1], ncol=(1 + (levels_E1-1) + (levels_E2-1) + (levels_E3-1) + (levels_E4-1) + (levels_E5-1)))
combos_p[,1] <- 1
for(i in 1:dim(combos_E)[1]){
out.work <- combos_E[i,]
if(out.work[1] == 2){combos_p[i,2] <- 1}
if(out.work[1] == 3){combos_p[i,3] <- 1}
if(out.work[2] == 2){combos_p[i,4] <- 1}
if(out.work[3] == 2){combos_p[i,5] <- 1}
if(out.work[3] == 3){combos_p[i,6] <- 1}
if(out.work[4] == 2){combos_p[i,7] <- 1}
if(out.work[4] == 3){combos_p[i,8] <- 1}
if(out.work[4] == 4){combos_p[i,9] <- 1}
if(out.work[4] == 5){combos_p[i,10] <- 1}
if(out.work[4] == 6){combos_p[i,11] <- 1}
if(out.work[4] == 7){combos_p[i,12] <- 1}
if(out.work[5] == 2){combos_p[i,13] <- 1}
}
### Probability vector for risk factor combinations in
# combos_p
prob_ers_vec_logistic_old <- rep(0, dim(combos_E)[1])
for(i in 1:dim(combos_E)[1]){
EI_i <- unlist(combos_E[i,])
prob_ers_vec_logistic_old[i] <- pvecE1[EI_i[1]]*pvecE2[EI_i[2]]*pvecE3[EI_i[3]]*pvecE4[EI_i[4]]*pvecE5[EI_i[5]]
}
### Function to use in numerical optimization to find ERS only
### logistic model parameters
# Each model parameter requires a separate line of code...
joint_logit_function <- function(p){
r <- rep(NA, length(p))
r[1] <- t(matrix(prob_ers_vec_logistic_old))%*%plogis(combos_p%*%matrix(p)) - K
## E1 Level 2 and 3
r[2] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,1] == 2]/pvecE1[2]))%*%plogis(combos_p[combos_E[,1] == 2,]%*%matrix(p)) - plogis(b0logitE1 + b1logitE1)
r[3] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,1] == 3]/pvecE1[3]))%*%plogis(combos_p[combos_E[,1] == 3,]%*%matrix(p)) - plogis(b0logitE1 + b2logitE1)
## E2 Level 2
r[4] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,2] == 2]/pvecE2[2]))%*%plogis(combos_p[combos_E[,2] == 2,]%*%matrix(p)) - plogis(b0logitE2 + b1logitE2)
## E3 Level 2 and 3
r[5] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,3] == 2]/pvecE3[2]))%*%plogis(combos_p[combos_E[,3] == 2,]%*%matrix(p)) - plogis(b0logitE3 + b1logitE3)
r[6] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,3] == 3]/pvecE3[3]))%*%plogis(combos_p[combos_E[,3] == 3,]%*%matrix(p)) - plogis(b0logitE3 + b2logitE3)
## E4 Level 2-7
r[7] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,4] == 2]/pvecE4[2]))%*%plogis(combos_p[combos_E[,4] == 2,]%*%matrix(p)) - plogis(b0logitE4 + b1logitE4)
r[8] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,4] == 3]/pvecE4[3]))%*%plogis(combos_p[combos_E[,4] == 3,]%*%matrix(p)) - plogis(b0logitE4 + b2logitE4)
r[9] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,4] == 4]/pvecE4[4]))%*%plogis(combos_p[combos_E[,4] == 4,]%*%matrix(p)) - plogis(b0logitE4 + b3logitE4)
r[10] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,4] == 5]/pvecE4[5]))%*%plogis(combos_p[combos_E[,4] == 5,]%*%matrix(p)) - plogis(b0logitE4 + b4logitE4)
r[11] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,4] == 6]/pvecE4[6]))%*%plogis(combos_p[combos_E[,4] == 6,]%*%matrix(p)) - plogis(b0logitE4 + b5logitE4)
r[12] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,4] == 7]/pvecE4[7]))%*%plogis(combos_p[combos_E[,4] == 7,]%*%matrix(p)) - plogis(b0logitE4 + b6logitE4)
## E5 Level 2
r[13] <- t(matrix(prob_ers_vec_logistic_old[combos_E[,5] == 2]/pvecE5[2]))%*%plogis(combos_p[combos_E[,5] == 2,]%*%matrix(p)) - plogis(b0logitE5 + b1logitE5)
r
}
### Use function to obtain model parameters
p0 <- c(-4, b1logitE1, b2logitE1, b1logitE2, b1logitE3, b2logitE3, b1logitE4, b2logitE4, b3logitE4, b4logitE4, b5logitE4, b6logitE4, b1logitE5)
out_logistic_joint <- dfsane(par=p0, fn= joint_logit_function, control=list(trace=F))
out_logistic_joint
logOR_joint <- out_logistic_joint$par
### Parameters for each risk factor
jlOR_E1 <- c(0, logOR_joint[2:3])
jlOR_E2 <- c(0, logOR_joint[4])
jlOR_E3 <- c(0, logOR_joint[5:6])
jlOR_E4 <- c(0, logOR_joint[7:12])
jlOR_E5 <- c(0, logOR_joint[13])
### Variance component, E1
vE1_jli <- sum((jlOR_E1^2)*pvecE1) - sum(matrix(jlOR_E1)%*%t(matrix(jlOR_E1))*matrix(pvecE1)%*%t(matrix(pvecE1)))
### Variance component, E2
vE2_jli <- sum((jlOR_E2^2)*pvecE2) - sum(matrix(jlOR_E2)%*%t(matrix(jlOR_E2))*matrix(pvecE2)%*%t(matrix(pvecE2)))
### Variance component, E3
vE3_jli <- sum((jlOR_E3^2)*pvecE3) - sum(matrix(jlOR_E3)%*%t(matrix(jlOR_E3))*matrix(pvecE3)%*%t(matrix(pvecE3)))
### Variance component, E4
vE4_jli <- sum((jlOR_E4^2)*pvecE4) - sum(matrix(jlOR_E4)%*%t(matrix(jlOR_E4))*matrix(pvecE4)%*%t(matrix(pvecE4)))
### Variance component, E5
vE5_jli <- sum((jlOR_E5^2)*pvecE5) - sum(matrix(jlOR_E5)%*%t(matrix(jlOR_E5))*matrix(pvecE5)%*%t(matrix(pvecE5)))
### Total ERS variance
vERSi <- vE1_jli + vE2_jli + vE3_jli + vE4_jli + vE5_jli
### Proportion of variability in the logistic-liability of the ERS
# only logistic model attributable to each risk factor
prop_v_RF <- (c(vE1_jli, vE2_jli, vE3_jli, vE4_jli, vE5_jli)/(vERSi + ((pi^2)/3)))
prop_v_RF
### Proportion of variability in the logistic-liability of the ERS
# only logistic model attributable to the ERS
prop_vERS <- sum(c(vE1_jli, vE2_jli, vE3_jli, vE4_jli, vE5_jli)/(vERSi + ((pi^2)/3)))
prop_vERS
#############################################################
### 3.2.3. Risk estimation (ERS only logistic model) ########
#############################################################
### Find ERS for every risk factor combination under the ERS
# only logistic model
score_logistic_joint <- combos_p%*%matrix(logOR_joint)
logistic_risk_joint <- plogis(combos_p%*%matrix(logOR_joint))
#############################################################
### 3.2.4. FIGURE 4 #########################################
#############################################################
### ERS only logistic model expected risk versus ERS only
# liability model expected risk
### Need vector of expected risks from the ERS only LTM for
# all risk factor combinations
liability_risk2 <- rep(0, length(ers_vec_old))
for(i in 1:length(ers_vec_old)){
liability_risk2[i] <- risk_single_ERS_function(ers = ers_vec_old[i], E_ERS = E_ERS, V_ERS=V_ERS, K=K)
}
### Fig. 4
plot(x=liability_risk2, y=logistic_risk_joint, xlab="Liability risk", ylab="Logistic risk")
abline(a=0, b=1)
#############################################################
#############################################################
### 3.3. Additional possible approximate logistic models ####
#############################################################
### Presented in the appendix of the paper ##################
#############################################################
### Logistic risk functions:
### Joint risk function: ERS and PRS in model
logis_risk_function <- function(ers, prs, E_ERS, V_ERS, E_PRS=0, V_PRS, K){
E_L <- E_PRS + E_ERS
L_T <- E_L - qlogis(K, s = sqrt(3)/pi)
denom <- sqrt(1 - V_ERS - V_PRS)
risk <- plogis(-(L_T - ers - prs)/denom, s = sqrt(3)/pi)
risk
}
### Risk function: only ERS:
logis_risk_single_ERS_function <- function(ers, E_ERS, E_PRS = 0, V_ERS, K){
E_L <- E_PRS + E_ERS
L_T <- E_L - qlogis(K, s = sqrt(3)/pi)
denom <- sqrt(1 - V_ERS)
risk <- plogis(-(L_T - ers)/denom, s = sqrt(3)/pi)
risk
}
### Risk function: only PRS:
logis_risk_single_PRS_function <- function(prs, E_ERS, E_PRS = 0, V_PRS, K){
E_L <- E_PRS + E_ERS
L_T <- - qlogis(K, s = sqrt(3)/pi)
denom <- sqrt(1 - V_PRS)
risk <- plogis(-(L_T - prs)/denom, s = sqrt(3)/pi)
risk
}
#########################################################
### 3.3.1. Possible approximate PRS only logistic model #
#########################################################
### To find this approximate model we first need the ORs from the
# PGC Schizophrenia working groups PRS SNPs.
### We also need the corresponding MAF. We are working with OR
# estimates from a European population and therefore require
# MAFs from a European population- we use the 1000G European
# population.
### Both datasets are provided onthe GitHub
### scz_prs_snps.txt == PGC schizophrenia SNP info
### SCZ_1000G_AF.txt == 1000G European MAFs
### CHANGE FILEPATH in below code
###################################################
### Read in PGC data
PGCdata <- fread(file="/FILEPATH/scz_prs_snps.txt")
### Select final PRS SNPs
PRSdata <- PGCdata[p <= 0.05,]
dim(PRSdata)
length(unique(PRSdata[,snpid]))
### Isolate rs names for future use
PRS_rs <- data.frame(PRSdata[,1])
PRS_rs <- as.character(unlist(PRS_rs))
colnames(PRS_rs) <- NULL
### Read in 1000G MAF data
scz_maf <- read.table(file="/FILEPATH/SCZ_1000G_AF.txt")
scz_maf$SNP1 <- as.character(unlist(scz_maf$SNP))
scz_maf <- data.table(scz_maf)
### Match SNPs between 2 datasets
setkey(scz_maf, SNP1)
PRSdata[, SNP1:= as.character(unlist(snpid))]
RRdata1 <- merge(scz_maf, PRSdata, by="SNP1")
dim(RRdata1)
### Create risk allele frequencies (RAF) for PRS SNPs
# and find ORs corresponding to these RAF (ORprs)
RAF <- rep(0, dim(RRdata1)[1])
ORprs <- RRdata1$or
for(i in 1:dim(RRdata1)[1]){
mA1i <- as.character(unlist(RRdata1$A1[i]))
if(RRdata1$or[i] < 1){
ORprs[i] <- 1/RRdata1$or[i]
refA1i <- as.character(unlist(RRdata1$a2[i]))
if(mA1i == refA1i){
RAF[i] <- 1 - RRdata1$MAF[i]
}else{
RAF[i] <- RRdata1$MAF[i]