forked from stephenslab/susieR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_each_effect_rss.R
49 lines (42 loc) · 1.64 KB
/
update_each_effect_rss.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
# @title Update each effect once.
# @param R a p by p symmetric and positive semidefinite correlation matrix.
# @param z a p vector
# @param s_init a list with elements sigma2, V, alpha, mu, Xr
# @param Sigma sigma2*R + lambda I
# @param estimate_prior_variance boolean indicating whether to estimate
# prior variance
# @param check_null_threshold float a threshold on the log scale to
# compare likelihood between current estimate and zero the null
#
#' @importFrom Matrix diag
update_each_effect_rss = function (R, z, s_init, Sigma,
estimate_prior_variance = FALSE,
estimate_prior_method = "optim",
check_null_threshold = 0) {
if (!estimate_prior_variance)
estimate_prior_method = "none"
# Repeat for each effect to update.
s = s_init
L = nrow(s$alpha)
if(L > 0)
for (l in 1:L) {
# Remove lth effect from fitted values.
s$Rz = s$Rz - R %*% (s$alpha[l,] * s$mu[l,])
#compute residuals
r = z - s$Rz
res = single_effect_regression_rss(as.vector(r),Sigma,s$V[l],s$pi,
estimate_prior_method,check_null_threshold)
# Update the variational estimate of the posterior mean.
s$mu[l,] = res$mu
s$alpha[l,] = res$alpha
s$mu2[l,] = res$mu2
s$V[l] = res$V
s$lbf[l] = res$lbf_model
s$KL[l] = -res$lbf_model +
SER_posterior_e_loglik_rss(R,Sigma,r,res$alpha * res$mu,
res$alpha * res$mu2)
s$Rz = s$Rz + R %*% (s$alpha[l,] * s$mu[l,])
}
s$Rz = unname(as.matrix(s$Rz))
return(s)
}