Skip to content

Commit 09ac4dc

Browse files
committed
updated MphEM function and its documentation and tests.
1 parent 76212da commit 09ac4dc

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Suggests: covr,
1212
testthat,
1313
knitr,
1414
rmarkdown
15-
RoxygenNote: 6.0.1
15+
RoxygenNote: 6.1.0
1616
VignetteBuilder: knitr
1717
Imports: readr,
1818
Matrix

R/MphEM.R

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#' @param Y matrix of phenotype values
88
#' @param V_g genetic covariance matrix
99
#' @param V_e error covariance matrix
10+
#' @param verbose_output logical indicating whether to output entire collection of intermediate values for all iterations. Default is FALSE.
1011
#' @return a list of lists. Length of list corresponds to number of EM iterations
1112
#' @export
1213
MphEM <- function(max_iter = 10000, max_prec = 1 / 1000000,
13-
eval, X, Y, V_g, V_e){
14+
eval, X, Y, V_g, V_e,
15+
verbose_output = FALSE){
1416
n_size <- length(eval)
1517
# be careful with defining c_size here
1618

@@ -74,13 +76,23 @@ MphEM <- function(max_iter = 10000, max_prec = 1 / 1000000,
7476
# update V_g and V_e
7577
uv_out[[1]] -> V_g
7678
uv_out[[2]] -> V_e
77-
out[[t]] <- list(logl_new = logl_new, Vg = V_g, Ve = V_e, Sigma_uu = Sigma_uu,
79+
if (verbose_output) {
80+
out[[t]] <- list(logl_new = logl_new, Vg = V_g, Ve = V_e, Sigma_uu = Sigma_uu,
7881
Sigma_ee = Sigma_ee, B = B,
7982
U_hat = U_hat, E_hat = E_hat,
8083
OmegaU = OmegaU, OmegaE = OmegaE, logdet_Ve = logdet_Ve,
8184
UltVeh = UltVeh, UltVehi = UltVehi,
8285
Dl = D_l, xHiy = xHiy, logl_const = logl_const, UltVehiU = UltVehiU
8386
)
87+
} else {
88+
out[[1]] <- list(logl_new = logl_new, Vg = V_g, Ve = V_e, Sigma_uu = Sigma_uu,
89+
Sigma_ee = Sigma_ee, B = B,
90+
U_hat = U_hat, E_hat = E_hat,
91+
OmegaU = OmegaU, OmegaE = OmegaE, logdet_Ve = logdet_Ve,
92+
UltVeh = UltVeh, UltVehi = UltVehi,
93+
Dl = D_l, xHiy = xHiy, logl_const = logl_const, UltVehiU = UltVehiU
94+
)
95+
}
8496
}
8597
if (length(out) == max_iter){warning("EM algorithm didn't converge.")}
8698
return(out)

man/MphEM.Rd

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_MphEM.R

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ as.matrix(pheno[, c(1, 6)]) -> phe16
88
MphEM(max_iter = 10, eval = e_out$values, X = matrix(c(-10, rep(0, 99)), nrow = 1),
99
Y = t(phe16) %*% e_out$vectors,
1010
V_g = matrix(c(1.91352, 0, 0, 0.530827), nrow = 2),
11-
V_e = matrix(c( 0.320028, 0, 0, 0.561589), nrow = 2)
11+
V_e = matrix(c( 0.320028, 0, 0, 0.561589), nrow = 2), verbose_output = TRUE
1212
)-> foo
1313

14+
MphEM(max_iter = 10, eval = e_out$values, X = matrix(c(-10, rep(0, 99)), nrow = 1),
15+
Y = t(phe16) %*% e_out$vectors,
16+
V_g = matrix(c(1.91352, 0, 0, 0.530827), nrow = 2),
17+
V_e = matrix(c( 0.320028, 0, 0, 0.561589), nrow = 2), verbose_output = FALSE
18+
)-> bar
19+
20+
1421
context("Testing MphEM() with two phenotypes and intercept only (no genetic data)")
1522

1623

@@ -21,3 +28,11 @@ test_that("First calculations of for Vg and for Ve are accurate", {
2128
expect_equal(foo[[5]][[2]], matrix(c(1.89405, 0.112242, 0.112242, 0.531075), nrow = 2), tolerance = 0.00001)
2229
expect_equal(foo[[5]][[3]], matrix(c(0.324097, 0.153797, 0.153797, 0.561429), nrow = 2), tolerance = 0.00001)
2330
})
31+
32+
33+
test_that("MphEM gives same output, regardless of value of input `verbose_output`", {
34+
expect_equal(foo[[10]][[2]], bar[[1]][[2]])
35+
expect_equal(foo[[10]][[3]], bar[[1]][[3]])
36+
expect_length(foo, 10)
37+
expect_length(bar, 1)
38+
})

0 commit comments

Comments
 (0)