Last updated: 2025-12-01
Checks: 6 1
Knit directory: Improved_LD_SuSiE/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20250821) was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Using absolute paths to the files within your workflowr project makes it difficult for you and others to run your code on a different machine. Change the absolute path(s) below to the suggested relative path(s) to make your code more reproducible.
| absolute | relative |
|---|---|
| ~/Documents/Improved_LD_SuSiE | . |
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version b9f08b6. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish or
wflow_git_commit). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .DS_Store
Ignored: .RData
Ignored: .Rhistory
Ignored: analysis/.DS_Store
Ignored: analysis/.RData
Ignored: analysis/.Rhistory
Unstaged changes:
Modified: analysis/index.Rmd
Modified: code_push.R
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/df-nu-low-rank.Rmd) and
HTML (docs/df-nu-low-rank.html) files. If you’ve configured
a remote Git repository (see ?wflow_git_remote), click on
the hyperlinks in the table below to view the files as they were in that
past version.
| File | Version | Author | Date | Message |
|---|---|---|---|---|
| Rmd | b9f08b6 | dodat | 2025-12-01 | wflow_publish(c("analysis/df-nu-low-rank.Rmd")) |
seed = 4 ## change this value to see other examples
library(susieR)
Warning: package 'susieR' was built under R version 4.3.3
library(Matrix)
set.seed(seed)
setwd("~/Documents/Improved_LD_SuSiE")
gtex = readRDS("data/Thyroid_ENSG00000132855.rds")
maf = apply(gtex, 2, function(x) sum(x)/2/length(x))
X0 = gtex[, maf > 0.01]
dim(X0)
[1] 574 7154
X = na.omit(X0)
snp_total = ncol(X0)
n = nrow(X0)
p = 30
# Start from a random point on the genome
indx_start = sample(1: (snp_total - p), 1)
X = X0[, indx_start:(indx_start + p -1)]
# View(cor(X)[1:10, 1:10])
## sub-sample into two
out_sample_size = 250
out_sample = sample(1:n, out_sample_size)
X_out = X[out_sample, ]
X_in = X[setdiff(1:n, out_sample), ]
rm_p = c(which(diag(cov(X_in))==0), which(diag(cov(X_out))==0))
indx_p = setdiff(1:p, rm_p)
X_in = X_in[, indx_p]
X_out = X_out[, indx_p]
## out-sample LD matrix
p = length(indx_p)
Rp = cov(X_out)
R0 = cov(X_in)
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
library(reshape2)
df1 <- melt(R0)
df2 <- melt(Rp)
N_in = nrow(X_in)
N_out = nrow(X_out)
p1 <- ggplot(df1, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low="blue", mid="white", high="red") +
coord_fixed() +
ggtitle(paste0("In-sample Cov, sample =", nrow(X_in)))
p2 <- ggplot(df2, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low="blue", mid="white", high="red") +
coord_fixed() +
ggtitle(paste0("Out-of-sample Cov, sample =", nrow(X_out)))
library(gridExtra)
grid.arrange(p1, p2, ncol = 2)

eig <- eigen(Rp)
plot(eig$values,
main = "Eigenvalues of Rp",
ylab = "Value",
xlab = "Eigenvalue index")

print(eig$values)
[1] 1.434888e+00 1.232170e+00 7.860173e-01 3.563616e-01 2.524898e-01
[6] 1.633233e-01 7.745090e-02 5.945454e-02 5.034101e-02 3.878267e-02
[11] 1.884220e-02 1.494242e-02 8.782745e-03 5.369828e-03 5.118441e-03
[16] 3.705699e-03 2.443832e-03 2.165237e-03 1.968695e-03 1.796069e-03
[21] 1.880547e-04 5.819595e-05 3.450706e-06 1.609377e-06 3.925945e-16
[26] 3.028957e-16 1.453242e-17 2.324990e-18 -3.267752e-17 -4.824910e-17
### Select rank
eig <- eigen(Rp)
eig_cumsum = cumsum(eig$values)
eig_cumsum[p]
[1] 4.516665
r_p = sum(eig_cumsum < 0.95 * eig_cumsum[p]) ## percentage variance explained
print(paste0("Chosen rank is ", r_p))
[1] "Chosen rank is 6"
Vp = eig$vectors[, c(1:r_p)]
Dp = diag(eig$values[c(1:r_p)])
print(sum((Rp - Vp %*% Dp %*% t(Vp))**2))
[1] 0.0143138
Rp_FA = Vp %*% Dp %*% t(Vp) + diag(rep(1, p)) * sum(eig$values[c(r_p + 1, p)])
eig <- eigen(R0)
V0 = eig$vectors[, c(1:r_p)]
D0 = diag(eig$values[c(1:r_p)])
print(sum((R0 - V0 %*% D0 %*% t(V0))**2))
[1] 0.005421943
R0_FA = V0 %*% D0 %*% t(V0) + diag(rep(1, p)) * sum(eig$values[c(r_p + 1, p)])
df3 <- melt(R0_FA)
df4 <- melt(Rp_FA)
p3 <- ggplot(df3, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low="blue", mid="white", high="red") +
coord_fixed() +
ggtitle(paste0("In-sample FA"))
p4 <- ggplot(df4, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low="blue", mid="white", high="red") +
coord_fixed() +
ggtitle(paste0("Out-of-sample FA"))
grid.arrange(p1, p2, p3, p4, ncol = 2, nrow = 2)

#### log IW(R0 | nu0 * Rp, nu0 + J + 1)
log_multigamma_vec <- function(a, p) {
# vectorized multivariate gamma
j <- 1:p
# sum over j, but broadcasting a over j
(p*(p-1)/4)*log(pi) +
rowSums(matrix(lgamma(a), nrow=length(a), ncol=p, byrow=FALSE) +
matrix((1 - j)/2, nrow=length(a), ncol=p, byrow=TRUE))
}
log_iw <- function(R0, Rp, nu_vec) {
p <- nrow(R0)
jitter = 1e-10
R0 = R0 + jitter * diag(rep(1, p))
Rp = Rp + jitter * diag(rep(1, p))
# Precompute expensive shared quantities
logdet_nu_Rp <- determinant(Rp, logarithm = TRUE)$modulus + p * log(nu_vec)
logdetR0 <- determinant(R0, logarithm = TRUE)$modulus
tr_term <- nu_vec * sum(t(Rp) * solve(R0))
llhs = (.5 * (nu_vec + p + 1) * logdet_nu_Rp
- .5 * (nu_vec + p + 1) * p * log(2)
- log_multigamma_vec((nu_vec + p + 1) / 2, p)
- .5 * (nu_vec + 2 * (p + 1)) * logdetR0
- .5 * tr_term)
as.numeric(llhs)
}
nu_vec = c(1:100)
llhs = log_iw(R0_FA, Rp_FA, nu_vec)
plot(nu_vec, llhs, xlab = "nu value", ylab = "log-likelihood")

print(nu_vec[which.max(llhs)])
[1] 67
We can look at nu around the optimal point more closely.
nu_opt = nu_vec[which.max(llhs)]
nu_vec1 = c((nu_opt - 10): (nu_opt + 10))
llhs1 = log_iw(R0_FA, Rp_FA, nu_vec1)
plot(nu_vec1, llhs1, xlab = "nu value", ylab = "log-likelihood")

snp_total = ncol(X0)
n = nrow(X0)
p = 1000
# Start from a random point on the genome
indx_start = sample(1: (snp_total - p), 1)
X = X0[, indx_start:(indx_start + p -1)]
X_subsample = X[c(1:100), ]
# X_subsample = X
R_sample = cor(t(X_subsample))
df = melt(R_sample)
ggplot(df, aes(Var1, Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low="blue", mid="white", high="red") +
coord_fixed() +
ggtitle(paste0("Correlation matrix of 100 individuals"))

sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.6.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/Chicago
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] gridExtra_2.3 reshape2_1.4.4 ggplot2_3.5.2 Matrix_1.6-1.1
[5] susieR_0.14.2 workflowr_1.7.1
loaded via a namespace (and not attached):
[1] sass_0.4.7 utf8_1.2.4 generics_0.1.3 stringi_1.7.12
[5] lattice_0.22-5 digest_0.6.33 magrittr_2.0.3 evaluate_0.23
[9] grid_4.3.2 RColorBrewer_1.1-3 fastmap_1.1.1 plyr_1.8.9
[13] rprojroot_2.0.3 jsonlite_1.8.7 processx_3.8.2 whisker_0.4.1
[17] reshape_0.8.10 mixsqp_0.3-54 ps_1.7.5 promises_1.3.3
[21] httr_1.4.7 fansi_1.0.5 scales_1.4.0 jquerylib_0.1.4
[25] cli_3.6.1 rlang_1.1.1 crayon_1.5.2 withr_2.5.2
[29] cachem_1.0.8 yaml_2.3.7 tools_4.3.2 dplyr_1.1.3
[33] httpuv_1.6.16 vctrs_0.6.4 R6_2.5.1 matrixStats_1.0.0
[37] lifecycle_1.0.3 git2r_0.36.2 stringr_1.5.0 fs_1.6.3
[41] irlba_2.3.5.1 pkgconfig_2.0.3 callr_3.7.3 pillar_1.9.0
[45] bslib_0.5.1 later_1.4.2 gtable_0.3.6 glue_1.6.2
[49] Rcpp_1.1.0 xfun_0.52 tibble_3.2.1 tidyselect_1.2.0
[53] highr_0.10 rstudioapi_0.15.0 knitr_1.45 farver_2.1.1
[57] htmltools_0.5.7 labeling_0.4.3 rmarkdown_2.25 compiler_4.3.2
[61] getPass_0.2-4