Last updated: 2025-05-12
Checks: 7 0
Knit directory:
symmetric_covariance_decomposition/
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(20250408)
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.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
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 755d361. 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: .Rhistory
Untracked files:
Untracked: analysis/symebcovmf_gb_point_exp_backfit.Rmd
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/unbal_nonoverlap_backfit.Rmd
) and HTML
(docs/unbal_nonoverlap_backfit.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 | 755d361 | Annie Xie | 2025-05-12 | Add analysis of backfit in unbal nonoverlap setting |
In this analysis, I explore backfitting in the unbalanced non-overlapping setting.
When applying greedy symEBcovMF with point-exponential prior to the unbalanced non-overlapping setting, the method did a relatively good job. However, the third factor it found was not a single group effect factor. For the third factor, the most prominent effect was the third group effect. But there was also a small non-zero loading on the first group effect. In my exploration, I found that the greedy method does prefer this solution (this solution yields a higher objective function). Therefore, I want to test if backfitting will change this factor to a single group effect factor. I hypothesize that it will since the first group effect is already captured in the fourth factor.
library(ebnm)
library(pheatmap)
library(ggplot2)
source('code/symebcovmf_functions.R')
source('code/visualization_functions.R')
optimize_factor <- function(R, ebnm_fn, maxiter, tol, v_init, lambda_k, R2k, n, KL){
R2 <- R2k - lambda_k^2
resid_s2 <- estimate_resid_s2(n = n, R2 = R2)
rank_one_KL <- 0
curr_elbo <- -Inf
obj_diff <- Inf
fitted_g_k <- NULL
iter <- 1
vec_elbo_full <- NULL
v <- v_init
while((iter <= maxiter) && (obj_diff > tol)){
# update l; power iteration step
v.old <- v
x <- R %*% v
e <- ebnm_fn(x = x, s = sqrt(resid_s2), g_init = fitted_g_k)
scaling_factor <- sqrt(sum(e$posterior$mean^2) + sum(e$posterior$sd^2))
if (scaling_factor == 0){ # check if scaling factor is zero
scaling_factor <- Inf
v <- e$posterior$mean/scaling_factor
print('Warning: scaling factor is zero')
break
}
v <- e$posterior$mean/scaling_factor
# update lambda and R2
lambda_k.old <- lambda_k
lambda_k <- max(as.numeric(t(v) %*% R %*% v), 0)
R2 <- R2k - lambda_k^2
#store estimate for g
fitted_g_k.old <- fitted_g_k
fitted_g_k <- e$fitted_g
# store KL
rank_one_KL.old <- rank_one_KL
rank_one_KL <- as.numeric(e$log_likelihood) +
- normal_means_loglik(x, sqrt(resid_s2), e$posterior$mean, e$posterior$mean^2 + e$posterior$sd^2)
# update resid_s2
resid_s2.old <- resid_s2
resid_s2 <- estimate_resid_s2(n = n, R2 = R2) # this goes negative?????
# check convergence - maybe change to rank-one obj function
curr_elbo.old <- curr_elbo
curr_elbo <- compute_elbo(resid_s2 = resid_s2,
n = n,
KL = c(KL, rank_one_KL),
R2 = R2)
if (iter > 1){
obj_diff <- curr_elbo - curr_elbo.old
}
if (obj_diff < 0){ # check if convergence_val < 0
v <- v.old
resid_s2 <- resid_s2.old
rank_one_KL <- rank_one_KL.old
lambda_k <- lambda_k.old
curr_elbo <- curr_elbo.old
fitted_g_k <- fitted_g_k.old
print(paste('elbo decreased by', abs(obj_diff)))
break
}
vec_elbo_full <- c(vec_elbo_full, curr_elbo)
iter <- iter + 1
}
return(list(v = v, lambda_k = lambda_k, resid_s2 = resid_s2, curr_elbo = curr_elbo, vec_elbo_full = vec_elbo_full, fitted_g_k = fitted_g_k, rank_one_KL = rank_one_KL))
}
#nullcheck function
nullcheck_factors <- function(sym_ebcovmf_obj, L2_tol = 10^(-8)){
null_lambda_idx <- which(sym_ebcovmf_obj$lambda == 0)
factor_L2_norms <- apply(sym_ebcovmf_obj$L_pm, 2, function(v){sqrt(sum(v^2))})
null_factor_idx <- which(factor_L2_norms < L2_tol)
null_idx <- unique(c(null_lambda_idx, null_factor_idx))
keep_idx <- setdiff(c(1:length(sym_ebcovmf_obj$lambda)), null_idx)
if (length(keep_idx) < length(sym_ebcovmf_obj$lambda)){
#remove factors
sym_ebcovmf_obj$L_pm <- sym_ebcovmf_obj$L_pm[,keep_idx]
sym_ebcovmf_obj$lambda <- sym_ebcovmf_obj$lambda[keep_idx]
sym_ebcovmf_obj$KL <- sym_ebcovmf_obj$KL[keep_idx]
sym_ebcovmf_obj$fitted_gs <- sym_ebcovmf_obj$fitted_gs[keep_idx]
}
#shouldn't need to recompute objective function or other things
return(sym_ebcovmf_obj)
}
sym_ebcovmf_backfit <- function(S, sym_ebcovmf_obj, ebnm_fn, backfit_maxiter = 100, backfit_tol = 10^(-8), optim_maxiter= 500, optim_tol = 10^(-8)){
K <- length(sym_ebcovmf_obj$lambda)
iter <- 1
obj_diff <- Inf
sym_ebcovmf_obj$backfit_vec_elbo_full <- NULL
# refit lambda
sym_ebcovmf_obj <- refit_lambda(S, sym_ebcovmf_obj, maxiter = 25)
while((iter <= backfit_maxiter) && (obj_diff > backfit_tol)){
#print(iter)
obj_old <- sym_ebcovmf_obj$elbo
# loop through each factor
for (k in 1:K){
#print(k)
# compute residual matrix
R <- S - tcrossprod(sym_ebcovmf_obj$L_pm[,-k] %*% diag(sqrt(sym_ebcovmf_obj$lambda[-k]), ncol = (K-1)))
R2k <- compute_R2(S, sym_ebcovmf_obj$L_pm[,-k], sym_ebcovmf_obj$lambda[-k], (K-1)) #this is right but I have one instance where the values don't match what I expect
# optimize factor
factor_proposed <- optimize_factor(R, ebnm_fn, optim_maxiter, optim_tol, sym_ebcovmf_obj$L_pm[,k], sym_ebcovmf_obj$lambda[k], R2k, sym_ebcovmf_obj$n, sym_ebcovmf_obj$KL[-k])
# update object
sym_ebcovmf_obj$L_pm[,k] <- factor_proposed$v
sym_ebcovmf_obj$KL[k] <- factor_proposed$rank_one_KL
sym_ebcovmf_obj$lambda[k] <- factor_proposed$lambda_k
sym_ebcovmf_obj$resid_s2 <- factor_proposed$resid_s2
sym_ebcovmf_obj$fitted_gs[[k]] <- factor_proposed$fitted_g_k
sym_ebcovmf_obj$elbo <- factor_proposed$curr_elbo
sym_ebcovmf_obj$backfit_vec_elbo_full <- c(sym_ebcovmf_obj$backfit_vec_elbo_full, factor_proposed$vec_elbo_full)
#print(sym_ebcovmf_obj$elbo)
sym_ebcovmf_obj <- refit_lambda(S, sym_ebcovmf_obj) # add refitting step?
#print(sym_ebcovmf_obj$elbo)
}
iter <- iter + 1
obj_diff <- abs(sym_ebcovmf_obj$elbo - obj_old)
# need to add check if it is negative?
}
# nullcheck
sym_ebcovmf_obj <- nullcheck_factors(sym_ebcovmf_obj)
return(sym_ebcovmf_obj)
}
# adapted from Jason's code
# args is a list containing pop_sizes, branch_sds, indiv_sd, n_genes, and seed
sim_star_data <- function(args) {
set.seed(args$seed)
n <- sum(args$pop_sizes)
p <- args$n_genes
K <- length(args$pop_sizes)
FF <- matrix(rnorm(K * p, sd = rep(args$branch_sds, each = p)), ncol = K)
LL <- matrix(0, nrow = n, ncol = K)
for (k in 1:K) {
vec <- rep(0, K)
vec[k] <- 1
LL[, k] <- rep(vec, times = args$pop_sizes)
}
E <- matrix(rnorm(n * p, sd = args$indiv_sd), nrow = n)
Y <- LL %*% t(FF) + E
YYt <- (1/p)*tcrossprod(Y)
return(list(Y = Y, YYt = YYt, LL = LL, FF = FF, K = ncol(LL)))
}
pop_sizes <- c(20,50,30,60)
n_genes <- 1000
branch_sds <- rep(2,4)
indiv_sd <- 1
seed <- 1
sim_args = list(pop_sizes = pop_sizes, branch_sds = branch_sds, indiv_sd = indiv_sd, n_genes = n_genes, seed = seed)
sim_data <- sim_star_data(sim_args)
This is a heatmap of the scaled Gram matrix:
plot_heatmap(sim_data$YYt, colors_range = c('blue','gray96','red'), brks = seq(-max(abs(sim_data$YYt)), max(abs(sim_data$YYt)), length.out = 50))
This is a scatter plot of the true loadings matrix:
pop_vec <- rep(c('A','B','C','D'), times = pop_sizes)
plot_loadings(sim_data$LL, pop_vec)
First, we apply greedy symEBcovMF with point-exponential prior to the Gram matrix:
symebcovmf_unbal_refit_fit <- sym_ebcovmf_fit(S = sim_data$YYt, ebnm_fn = ebnm_point_exponential, K = 4, maxiter = 100, rank_one_tol = 10^(-8), tol = 10^(-8), refit_lam = TRUE)
This is a scatter plot of \(\hat{L}\), the estimate from symEBcovMF:
plot_loadings(symebcovmf_unbal_refit_fit$L_pm %*% diag(sqrt(symebcovmf_unbal_refit_fit$lambda)), pop_vec)
This is the objective function value attained:
symebcovmf_unbal_refit_fit$elbo
[1] 1097.095
Now, we try backfitting (also with a point-exponential prior):
symebcovmf_fit_backfit <- sym_ebcovmf_backfit(sim_data$YYt, symebcovmf_unbal_refit_fit, ebnm_fn = ebnm::ebnm_point_exponential, backfit_maxiter = 100)
[1] "elbo decreased by 2.45563569478691e-10"
[1] "elbo decreased by 7.56699591875076e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 1.3369572116062e-09"
[1] "elbo decreased by 1.06592779047787e-09"
[1] "elbo decreased by 1.81898940354586e-12"
[1] "elbo decreased by 9.42236511036754e-10"
[1] "elbo decreased by 1.69893610291183e-09"
[1] "elbo decreased by 3.3160176826641e-09"
[1] "elbo decreased by 4.00177668780088e-10"
[1] "elbo decreased by 1.58433977048844e-09"
[1] "elbo decreased by 1.09685061033815e-09"
[1] "elbo decreased by 5.87533577345312e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 2.29192664846778e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 1.34241417981684e-09"
[1] "elbo decreased by 2.72848410531878e-11"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 5.23868948221207e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 1.77169567905366e-09"
[1] "elbo decreased by 1.0750227374956e-09"
[1] "elbo decreased by 2.89401214104146e-09"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 2.72848410531878e-09"
[1] "elbo decreased by 5.71162672713399e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 1.38243194669485e-10"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 1.28056854009628e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 2.2846506908536e-09"
[1] "elbo decreased by 1.70621206052601e-09"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 2.25554686039686e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 1.18234311230481e-10"
[1] "elbo decreased by 8.27640178613365e-10"
[1] "elbo decreased by 1.96450855582952e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 2.40106601268053e-10"
[1] "elbo decreased by 4.38376446254551e-10"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 3.70710040442646e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 7.6397554948926e-11"
[1] "elbo decreased by 1.03682396002114e-10"
[1] "elbo decreased by 2.20097717829049e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 5.45696821063757e-12"
[1] "elbo decreased by 1.92812876775861e-10"
[1] "elbo decreased by 1.03500497061759e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 1.45519152283669e-10"
[1] "elbo decreased by 6.16637407802045e-10"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 2.25554686039686e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 6.91215973347425e-11"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 8.02174326963723e-10"
[1] "elbo decreased by 2.09183781407773e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 1.30967237055302e-09"
[1] "elbo decreased by 1.98269844986498e-10"
[1] "elbo decreased by 3.63797880709171e-12"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 5.76619640924037e-10"
[1] "elbo decreased by 5.11136022396386e-10"
[1] "elbo decreased by 2.14640749618411e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 1.69893610291183e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 3.80532583221793e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 1.21872290037572e-09"
[1] "elbo decreased by 1.60616764333099e-09"
[1] "elbo decreased by 1.69893610291183e-09"
[1] "elbo decreased by 2.18278728425503e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 5.45696821063757e-10"
[1] "elbo decreased by 8.44011083245277e-10"
[1] "elbo decreased by 1.60616764333099e-09"
[1] "elbo decreased by 1.3405951904133e-09"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 1.67347025126219e-09"
[1] "elbo decreased by 8.40373104438186e-10"
[1] "elbo decreased by 1.69893610291183e-09"
[1] "elbo decreased by 1.55341695062816e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 2.56477505899966e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 5.49334799870849e-10"
[1] "elbo decreased by 3.87444742955267e-10"
[1] "elbo decreased by 9.27684595808387e-11"
[1] "elbo decreased by 3.27418092638254e-11"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 2.04454408958554e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.58562998473644e-10"
[1] "elbo decreased by 1.03864294942468e-09"
[1] "elbo decreased by 1.85536919161677e-10"
[1] "elbo decreased by 3.67072061635554e-09"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 3.21961124427617e-10"
[1] "elbo decreased by 5.16592990607023e-10"
[1] "elbo decreased by 2.99769453704357e-09"
[1] "elbo decreased by 4.11091605201364e-10"
[1] "elbo decreased by 1.54614099301398e-10"
[1] "elbo decreased by 2.18642526306212e-09"
[1] "elbo decreased by 1.70257408171892e-09"
[1] "elbo decreased by 1.45155354402959e-09"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 2.05363903660327e-09"
[1] "elbo decreased by 1.70257408171892e-09"
[1] "elbo decreased by 7.56699591875076e-10"
[1] "elbo decreased by 1.2732925824821e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 4.36557456851006e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 1.12049747258425e-09"
[1] "elbo decreased by 5.54791768081486e-10"
[1] "elbo decreased by 9.62245394475758e-10"
[1] "elbo decreased by 3.74711817130446e-09"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 7.20319803804159e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 7.31233740225434e-10"
[1] "elbo decreased by 1.65346136782318e-09"
[1] "elbo decreased by 9.45874489843845e-10"
[1] "elbo decreased by 8.51287040859461e-10"
[1] "elbo decreased by 8.47649062052369e-10"
[1] "elbo decreased by 2.50838638748974e-09"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 4.05634636990726e-10"
[1] "elbo decreased by 2.01544025912881e-09"
[1] "elbo decreased by 8.54925019666553e-10"
[1] "elbo decreased by 8.18545231595635e-11"
[1] "elbo decreased by 1.01863406598568e-10"
This is a scatter plot of \(\hat{L}_{backfit}\), the estimate from symEBcovMF with backfit:
plot_loadings(symebcovmf_fit_backfit$L_pm %*% diag(sqrt(symebcovmf_fit_backfit$lambda)), pop_vec)
This is the objective function value attained:
symebcovmf_fit_backfit$elbo
[1] 10300.7
We see that after backfitting, the third factor looks closer to a single group effect factor. The first group effect that was originally present has shrunk closer to zero. Furthermore, the objective function value after the backfit was quite a bit higher. One interesting observation is that some of the values that were previously zero are now small, non-zero values. Some of the other methods that work in the covariance space also have this characteristic, e.g. GBCD, EBMFcov. So perhaps this is related to working in the covariance space?
sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.4.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] ggplot2_3.5.1 pheatmap_1.0.12 ebnm_1.1-34 workflowr_1.7.1
loaded via a namespace (and not attached):
[1] gtable_0.3.5 xfun_0.48 bslib_0.8.0 processx_3.8.4
[5] lattice_0.22-6 callr_3.7.6 vctrs_0.6.5 tools_4.3.2
[9] ps_1.7.7 generics_0.1.3 tibble_3.2.1 fansi_1.0.6
[13] highr_0.11 pkgconfig_2.0.3 Matrix_1.6-5 SQUAREM_2021.1
[17] RColorBrewer_1.1-3 lifecycle_1.0.4 truncnorm_1.0-9 farver_2.1.2
[21] compiler_4.3.2 stringr_1.5.1 git2r_0.33.0 munsell_0.5.1
[25] getPass_0.2-4 httpuv_1.6.15 htmltools_0.5.8.1 sass_0.4.9
[29] yaml_2.3.10 later_1.3.2 pillar_1.9.0 jquerylib_0.1.4
[33] whisker_0.4.1 cachem_1.1.0 trust_0.1-8 RSpectra_0.16-2
[37] tidyselect_1.2.1 digest_0.6.37 stringi_1.8.4 dplyr_1.1.4
[41] ashr_2.2-66 labeling_0.4.3 splines_4.3.2 rprojroot_2.0.4
[45] fastmap_1.2.0 grid_4.3.2 colorspace_2.1-1 cli_3.6.3
[49] invgamma_1.1 magrittr_2.0.3 utf8_1.2.4 withr_3.0.1
[53] scales_1.3.0 promises_1.3.0 horseshoe_0.2.0 rmarkdown_2.28
[57] httr_1.4.7 deconvolveR_1.2-1 evaluate_1.0.0 knitr_1.48
[61] irlba_2.3.5.1 rlang_1.1.4 Rcpp_1.0.13 mixsqp_0.3-54
[65] glue_1.8.0 rstudioapi_0.16.0 jsonlite_1.8.9 R6_2.5.1
[69] fs_1.6.4