Skip to content

Commit 8649d9d

Browse files
committed
fixed demo gwas
1 parent 131d1a8 commit 8649d9d

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

demo/gwas.R

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
### install
22
# library(devtools)
33
# install_github("variani/matlm")
4-
# install_github("variani/gls")
4+
# install_github("variani/wlm")
55
# install_github("variani/qq")
6+
#
7+
# re-install lme4qtl if necessary (e.g. for `varcov` fun. was not exported prev.)
68
# install_github("variani/lme4qtl")
79

810
### inc
@@ -13,7 +15,7 @@ library(ggplot2)
1315
library(lme4qtl)
1416
# for Step 2: association tests
1517
library(matlm)
16-
library(gls)
18+
library(wlm)
1719
# for Step 3: explore GWAS results
1820
library(qq)
1921

@@ -25,11 +27,12 @@ data(dat40, package = "lme4qtl")
2527
N <- nrow(dat40) # the number of ind.
2628
ids <- dat40$ID # individual IDs
2729

28-
# simulate (null) random genotypes (0/1 with 50% prob.)
30+
# simulate (null) random genotypes (0/1 with 50% prob.; not linked to `kin2`
2931
gdat40 <- matrix(rbinom(N*M, 1, 0.5), nrow = N, ncol = M)
3032
rownames(gdat40) <- ids
3133
colnames(gdat40) <- paste0("snp", seq(1, ncol(gdat40)))
3234

35+
# simulate (null) random cont. predictors (linked to `kin2`) by MVN ~ (0, kin2)
3336
pdat40 <- t(mvrnorm(M, rep(0, N), kin2))
3437
rownames(gdat40) <- ids
3538
colnames(gdat40) <- paste0("pred", seq(1, ncol(gdat40)))
@@ -40,6 +43,9 @@ colnames(gdat40) <- paste0("pred", seq(1, ncol(gdat40)))
4043
# - extract variance-covariance matrix `V`
4144
#-----------------------------------------------
4245
mod <- lme4qtl::relmatLmer(trait1 ~ AGE + (1|FAMID) + (1|ID), dat40, relmat = list(ID = kin2))
46+
# in case of convergence problems, one might try/experiment with:
47+
# - relmatLmer(trait1 ~ AGE + (1|FAMID) + (1|ID), dat40, relmat = list(ID = kin2), calc.derivs = FALSE)
48+
# - relmatLmer(trait1 ~ AGE + (1|FAMID) + (1|ID), dat40, relmat = list(ID = kin2), calc.derivs = FALSE, control = lmerControl(optimizer = "bobyqa", check.conv.grad = list(action = "warning", tol = 0.005, relTol = NULL)))
4349

4450
V <- lme4qtl::varcov(mod, idvar = "ID")
4551

@@ -55,11 +61,11 @@ Matrix::image(V_thr[1:20, 1:20], main = "Estimated V (with artifacts removed)")
5561
# - perform association test on M predictors
5662
# - examimed several combinations:
5763
# - linear models (least squares) vs. generalized least squares that takes V as input
58-
# - binary genotypes (simulated with no structure) vs. cont. predictors (simultaed as ~ MVN(0, kin2))
64+
# - binary genotypes (simulated with no structure) vs. cont. predictors
5965
#-------
6066
# transformation on data (due to structure in V) needs to be computed once
6167
# (note: EVD (not Cholesky) is required; otherwise, missing data would produce messy results)
62-
decomp <- gls::decompose_varcov(V, method = "evd", output = "all")
68+
decomp <- wlm::decompose_varcov(V, method = "evd", output = "all")
6369
W <- decomp$transform
6470

6571
gassoc_lm <- matlm::matlm(trait1 ~ AGE, dat40, pred = gdat40, ids = ids,
@@ -75,13 +81,17 @@ passoc_gls <- matlm::matlm(trait1 ~ AGE, dat40, pred = pdat40, ids = ids, transf
7581
#-------
7682
# Step 3:
7783
# - QQ plots
78-
# - the first two plots shows that both LS & LMM/WLS approches gives valid results,
84+
# - the first two plots shows that both LS & LMM/WLS approches give valid results,
7985
# because binary genotype predictors (gdat40) were simulated without any link to
8086
# data structure in kin2
81-
# - the last plots shows that LMM/GWAS produced a valid distribution of p-values
87+
# - the last plots shows that LMM/GWAS produces a valid distribution of p-values,
88+
# while LS shows an inflated Type I error rate
8289
#-------
83-
qq:qq_plot(gassoc_lm$tab$pval) + ggtitle("LS: (null) random binary genotypes (not linked to kin2)")
84-
qq:qq_plot(gassoc_gls$tab$pval) + ggtitle("LMM/WLS: (null) random binary genotypes (not linked to kin2)")
90+
qq::qq_plot(gassoc_lm$tab$pval) + ggtitle("LS: (null) random binary genotypes (not linked to kin2)")
91+
qq::qq_plot(gassoc_gls$tab$pval) + ggtitle("LMM/WLS: (null) random binary genotypes (not linked to kin2)")
92+
93+
qq::qq_plot(passoc_lm$tab$pval) + ggtitle("LS: (null) random cont. predictors (linked to kin2)")
94+
qq::qq_plot(passoc_gls$tab$pval) + ggtitle("LMM/WLS: (null) random cont. predictors (linked to kin2)")
95+
96+
8597

86-
qq:qq_plot(passoc_lm$tab$pval) + ggtitle("LS: (null) random cont. predictors (linked to kin2)")
87-
qq:qq_plot(passoc_gls$tab$pval) + ggtitle("LMM/WLS: (null) random cont. predictors (linked to kin2)")

0 commit comments

Comments
 (0)